Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
051d5130 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 | 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 | |
45d6a902 AM |
106 | /* Create some sections which will be filled in with dynamic linking |
107 | information. ABFD is an input file which requires dynamic sections | |
108 | to be created. The dynamic sections take up virtual memory space | |
109 | when the final executable is run, so we need to create them before | |
110 | addresses are assigned to the output sections. We work out the | |
111 | actual contents and size of these sections later. */ | |
252b5132 | 112 | |
b34976b6 | 113 | bfd_boolean |
268b6b39 | 114 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 115 | { |
45d6a902 AM |
116 | flagword flags; |
117 | register asection *s; | |
118 | struct elf_link_hash_entry *h; | |
119 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 120 | const struct elf_backend_data *bed; |
252b5132 | 121 | |
0eddce27 | 122 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
123 | return FALSE; |
124 | ||
125 | if (elf_hash_table (info)->dynamic_sections_created) | |
126 | return TRUE; | |
127 | ||
128 | /* Make sure that all dynamic sections use the same input BFD. */ | |
129 | if (elf_hash_table (info)->dynobj == NULL) | |
130 | elf_hash_table (info)->dynobj = abfd; | |
131 | else | |
132 | abfd = elf_hash_table (info)->dynobj; | |
133 | ||
e5a52504 MM |
134 | bed = get_elf_backend_data (abfd); |
135 | ||
136 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
137 | |
138 | /* A dynamically linked executable has a .interp section, but a | |
139 | shared library does not. */ | |
36af4a4e | 140 | if (info->executable) |
252b5132 | 141 | { |
45d6a902 AM |
142 | s = bfd_make_section (abfd, ".interp"); |
143 | if (s == NULL | |
144 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
145 | return FALSE; | |
146 | } | |
bb0deeff | 147 | |
0eddce27 | 148 | if (! info->traditional_format) |
45d6a902 AM |
149 | { |
150 | s = bfd_make_section (abfd, ".eh_frame_hdr"); | |
151 | if (s == NULL | |
152 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
153 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
154 | return FALSE; | |
155 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
156 | } | |
bb0deeff | 157 | |
45d6a902 AM |
158 | /* Create sections to hold version informations. These are removed |
159 | if they are not needed. */ | |
160 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
161 | if (s == NULL | |
162 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
163 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
164 | return FALSE; | |
165 | ||
166 | s = bfd_make_section (abfd, ".gnu.version"); | |
167 | if (s == NULL | |
168 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
169 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
170 | return FALSE; | |
171 | ||
172 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
173 | if (s == NULL | |
174 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
175 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
176 | return FALSE; | |
177 | ||
178 | s = bfd_make_section (abfd, ".dynsym"); | |
179 | if (s == NULL | |
180 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
181 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
182 | return FALSE; | |
183 | ||
184 | s = bfd_make_section (abfd, ".dynstr"); | |
185 | if (s == NULL | |
186 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
187 | return FALSE; | |
188 | ||
189 | /* Create a strtab to hold the dynamic symbol names. */ | |
190 | if (elf_hash_table (info)->dynstr == NULL) | |
191 | { | |
192 | elf_hash_table (info)->dynstr = _bfd_elf_strtab_init (); | |
193 | if (elf_hash_table (info)->dynstr == NULL) | |
194 | return FALSE; | |
252b5132 RH |
195 | } |
196 | ||
45d6a902 AM |
197 | s = bfd_make_section (abfd, ".dynamic"); |
198 | if (s == NULL | |
199 | || ! bfd_set_section_flags (abfd, s, flags) | |
200 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
201 | return FALSE; | |
202 | ||
203 | /* The special symbol _DYNAMIC is always set to the start of the | |
204 | .dynamic section. This call occurs before we have processed the | |
205 | symbols for any dynamic object, so we don't have to worry about | |
206 | overriding a dynamic definition. We could set _DYNAMIC in a | |
207 | linker script, but we only want to define it if we are, in fact, | |
208 | creating a .dynamic section. We don't want to define it if there | |
209 | is no .dynamic section, since on some ELF platforms the start up | |
210 | code examines it to decide how to initialize the process. */ | |
211 | bh = NULL; | |
212 | if (! (_bfd_generic_link_add_one_symbol | |
268b6b39 AM |
213 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE, |
214 | get_elf_backend_data (abfd)->collect, &bh))) | |
45d6a902 AM |
215 | return FALSE; |
216 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf | 217 | h->def_regular = 1; |
45d6a902 AM |
218 | h->type = STT_OBJECT; |
219 | ||
36af4a4e | 220 | if (! info->executable |
c152c796 | 221 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
222 | return FALSE; |
223 | ||
224 | s = bfd_make_section (abfd, ".hash"); | |
225 | if (s == NULL | |
226 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
227 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
228 | return FALSE; | |
229 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
230 | ||
231 | /* Let the backend create the rest of the sections. This lets the | |
232 | backend set the right flags. The backend will normally create | |
233 | the .got and .plt sections. */ | |
234 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
235 | return FALSE; | |
236 | ||
237 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
238 | ||
239 | return TRUE; | |
240 | } | |
241 | ||
242 | /* Create dynamic sections when linking against a dynamic object. */ | |
243 | ||
244 | bfd_boolean | |
268b6b39 | 245 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
246 | { |
247 | flagword flags, pltflags; | |
248 | asection *s; | |
9c5bfbb7 | 249 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 250 | |
252b5132 RH |
251 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
252 | .rel[a].bss sections. */ | |
e5a52504 | 253 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
254 | |
255 | pltflags = flags; | |
252b5132 | 256 | if (bed->plt_not_loaded) |
6df4d94c MM |
257 | /* We do not clear SEC_ALLOC here because we still want the OS to |
258 | allocate space for the section; it's just that there's nothing | |
259 | to read in from the object file. */ | |
5d1634d7 | 260 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
6df4d94c MM |
261 | else |
262 | pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; | |
252b5132 RH |
263 | if (bed->plt_readonly) |
264 | pltflags |= SEC_READONLY; | |
265 | ||
266 | s = bfd_make_section (abfd, ".plt"); | |
267 | if (s == NULL | |
268 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
269 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
b34976b6 | 270 | return FALSE; |
252b5132 RH |
271 | |
272 | if (bed->want_plt_sym) | |
273 | { | |
274 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
275 | .plt section. */ | |
14a793b2 AM |
276 | struct elf_link_hash_entry *h; |
277 | struct bfd_link_hash_entry *bh = NULL; | |
278 | ||
252b5132 | 279 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
280 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL, |
281 | FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
b34976b6 | 282 | return FALSE; |
14a793b2 | 283 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf | 284 | h->def_regular = 1; |
252b5132 RH |
285 | h->type = STT_OBJECT; |
286 | ||
36af4a4e | 287 | if (! info->executable |
c152c796 | 288 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 289 | return FALSE; |
252b5132 RH |
290 | } |
291 | ||
3e932841 | 292 | s = bfd_make_section (abfd, |
bf572ba0 | 293 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
294 | if (s == NULL |
295 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 296 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 297 | return FALSE; |
252b5132 RH |
298 | |
299 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 300 | return FALSE; |
252b5132 | 301 | |
3018b441 RH |
302 | if (bed->want_dynbss) |
303 | { | |
304 | /* The .dynbss section is a place to put symbols which are defined | |
305 | by dynamic objects, are referenced by regular objects, and are | |
306 | not functions. We must allocate space for them in the process | |
307 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
308 | initialize them at run time. The linker script puts the .dynbss | |
309 | section into the .bss section of the final image. */ | |
310 | s = bfd_make_section (abfd, ".dynbss"); | |
311 | if (s == NULL | |
77f3d027 | 312 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED)) |
b34976b6 | 313 | return FALSE; |
252b5132 | 314 | |
3018b441 | 315 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
316 | normally needed. We need to create it here, though, so that the |
317 | linker will map it to an output section. We can't just create it | |
318 | only if we need it, because we will not know whether we need it | |
319 | until we have seen all the input files, and the first time the | |
320 | main linker code calls BFD after examining all the input files | |
321 | (size_dynamic_sections) the input sections have already been | |
322 | mapped to the output sections. If the section turns out not to | |
323 | be needed, we can discard it later. We will never need this | |
324 | section when generating a shared object, since they do not use | |
325 | copy relocs. */ | |
3018b441 RH |
326 | if (! info->shared) |
327 | { | |
3e932841 KH |
328 | s = bfd_make_section (abfd, |
329 | (bed->default_use_rela_p | |
330 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
331 | if (s == NULL |
332 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 333 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 334 | return FALSE; |
3018b441 | 335 | } |
252b5132 RH |
336 | } |
337 | ||
b34976b6 | 338 | return TRUE; |
252b5132 RH |
339 | } |
340 | \f | |
252b5132 RH |
341 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
342 | read the input files, since we need to have a list of all of them | |
343 | before we can determine the final sizes of the output sections. | |
344 | Note that we may actually call this function even though we are not | |
345 | going to output any dynamic symbols; in some cases we know that a | |
346 | symbol should be in the dynamic symbol table, but only if there is | |
347 | one. */ | |
348 | ||
b34976b6 | 349 | bfd_boolean |
c152c796 AM |
350 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
351 | struct elf_link_hash_entry *h) | |
252b5132 RH |
352 | { |
353 | if (h->dynindx == -1) | |
354 | { | |
2b0f7ef9 | 355 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 356 | char *p; |
252b5132 | 357 | const char *name; |
252b5132 RH |
358 | bfd_size_type indx; |
359 | ||
7a13edea NC |
360 | /* XXX: The ABI draft says the linker must turn hidden and |
361 | internal symbols into STB_LOCAL symbols when producing the | |
362 | DSO. However, if ld.so honors st_other in the dynamic table, | |
363 | this would not be necessary. */ | |
364 | switch (ELF_ST_VISIBILITY (h->other)) | |
365 | { | |
366 | case STV_INTERNAL: | |
367 | case STV_HIDDEN: | |
9d6eee78 L |
368 | if (h->root.type != bfd_link_hash_undefined |
369 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 370 | { |
f5385ebf | 371 | h->forced_local = 1; |
b34976b6 | 372 | return TRUE; |
7a13edea | 373 | } |
0444bdd4 | 374 | |
7a13edea NC |
375 | default: |
376 | break; | |
377 | } | |
378 | ||
252b5132 RH |
379 | h->dynindx = elf_hash_table (info)->dynsymcount; |
380 | ++elf_hash_table (info)->dynsymcount; | |
381 | ||
382 | dynstr = elf_hash_table (info)->dynstr; | |
383 | if (dynstr == NULL) | |
384 | { | |
385 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 386 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 387 | if (dynstr == NULL) |
b34976b6 | 388 | return FALSE; |
252b5132 RH |
389 | } |
390 | ||
391 | /* We don't put any version information in the dynamic string | |
aad5d350 | 392 | table. */ |
252b5132 RH |
393 | name = h->root.root.string; |
394 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
395 | if (p != NULL) |
396 | /* We know that the p points into writable memory. In fact, | |
397 | there are only a few symbols that have read-only names, being | |
398 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
399 | by the backends. Most symbols will have names pointing into | |
400 | an ELF string table read from a file, or to objalloc memory. */ | |
401 | *p = 0; | |
402 | ||
403 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
404 | ||
405 | if (p != NULL) | |
406 | *p = ELF_VER_CHR; | |
252b5132 RH |
407 | |
408 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 409 | return FALSE; |
252b5132 RH |
410 | h->dynstr_index = indx; |
411 | } | |
412 | ||
b34976b6 | 413 | return TRUE; |
252b5132 | 414 | } |
45d6a902 AM |
415 | \f |
416 | /* Record an assignment to a symbol made by a linker script. We need | |
417 | this in case some dynamic object refers to this symbol. */ | |
418 | ||
419 | bfd_boolean | |
268b6b39 AM |
420 | bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED, |
421 | struct bfd_link_info *info, | |
422 | const char *name, | |
423 | bfd_boolean provide) | |
45d6a902 AM |
424 | { |
425 | struct elf_link_hash_entry *h; | |
426 | ||
0eddce27 | 427 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
428 | return TRUE; |
429 | ||
430 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); | |
431 | if (h == NULL) | |
432 | return FALSE; | |
433 | ||
02bb6eae AO |
434 | /* Since we're defining the symbol, don't let it seem to have not |
435 | been defined. record_dynamic_symbol and size_dynamic_sections | |
a010d60f AM |
436 | may depend on this. |
437 | ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or | |
438 | to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk | |
439 | of some later symbol manipulation setting the symbol back to | |
440 | bfd_link_hash_undefined, and the linker trying to add the symbol to | |
441 | the undefs list twice. */ | |
02bb6eae AO |
442 | if (h->root.type == bfd_link_hash_undefweak |
443 | || h->root.type == bfd_link_hash_undefined) | |
444 | h->root.type = bfd_link_hash_new; | |
445 | ||
45d6a902 | 446 | if (h->root.type == bfd_link_hash_new) |
f5385ebf | 447 | h->non_elf = 0; |
45d6a902 AM |
448 | |
449 | /* If this symbol is being provided by the linker script, and it is | |
450 | currently defined by a dynamic object, but not by a regular | |
451 | object, then mark it as undefined so that the generic linker will | |
452 | force the correct value. */ | |
453 | if (provide | |
f5385ebf AM |
454 | && h->def_dynamic |
455 | && !h->def_regular) | |
45d6a902 AM |
456 | h->root.type = bfd_link_hash_undefined; |
457 | ||
458 | /* If this symbol is not being provided by the linker script, and it is | |
459 | currently defined by a dynamic object, but not by a regular object, | |
460 | then clear out any version information because the symbol will not be | |
461 | associated with the dynamic object any more. */ | |
462 | if (!provide | |
f5385ebf AM |
463 | && h->def_dynamic |
464 | && !h->def_regular) | |
45d6a902 AM |
465 | h->verinfo.verdef = NULL; |
466 | ||
f5385ebf | 467 | h->def_regular = 1; |
45d6a902 | 468 | |
f5385ebf AM |
469 | if ((h->def_dynamic |
470 | || h->ref_dynamic | |
45d6a902 AM |
471 | || info->shared) |
472 | && h->dynindx == -1) | |
473 | { | |
c152c796 | 474 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
475 | return FALSE; |
476 | ||
477 | /* If this is a weak defined symbol, and we know a corresponding | |
478 | real symbol from the same dynamic object, make sure the real | |
479 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
480 | if (h->u.weakdef != NULL |
481 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 482 | { |
f6e332e6 | 483 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
484 | return FALSE; |
485 | } | |
486 | } | |
487 | ||
488 | return TRUE; | |
489 | } | |
42751cf3 | 490 | |
8c58d23b AM |
491 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
492 | success, and 2 on a failure caused by attempting to record a symbol | |
493 | in a discarded section, eg. a discarded link-once section symbol. */ | |
494 | ||
495 | int | |
c152c796 AM |
496 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
497 | bfd *input_bfd, | |
498 | long input_indx) | |
8c58d23b AM |
499 | { |
500 | bfd_size_type amt; | |
501 | struct elf_link_local_dynamic_entry *entry; | |
502 | struct elf_link_hash_table *eht; | |
503 | struct elf_strtab_hash *dynstr; | |
504 | unsigned long dynstr_index; | |
505 | char *name; | |
506 | Elf_External_Sym_Shndx eshndx; | |
507 | char esym[sizeof (Elf64_External_Sym)]; | |
508 | ||
0eddce27 | 509 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
510 | return 0; |
511 | ||
512 | /* See if the entry exists already. */ | |
513 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
514 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
515 | return 1; | |
516 | ||
517 | amt = sizeof (*entry); | |
268b6b39 | 518 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
519 | if (entry == NULL) |
520 | return 0; | |
521 | ||
522 | /* Go find the symbol, so that we can find it's name. */ | |
523 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 524 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
525 | { |
526 | bfd_release (input_bfd, entry); | |
527 | return 0; | |
528 | } | |
529 | ||
530 | if (entry->isym.st_shndx != SHN_UNDEF | |
531 | && (entry->isym.st_shndx < SHN_LORESERVE | |
532 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
533 | { | |
534 | asection *s; | |
535 | ||
536 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
537 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
538 | { | |
539 | /* We can still bfd_release here as nothing has done another | |
540 | bfd_alloc. We can't do this later in this function. */ | |
541 | bfd_release (input_bfd, entry); | |
542 | return 2; | |
543 | } | |
544 | } | |
545 | ||
546 | name = (bfd_elf_string_from_elf_section | |
547 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
548 | entry->isym.st_name)); | |
549 | ||
550 | dynstr = elf_hash_table (info)->dynstr; | |
551 | if (dynstr == NULL) | |
552 | { | |
553 | /* Create a strtab to hold the dynamic symbol names. */ | |
554 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
555 | if (dynstr == NULL) | |
556 | return 0; | |
557 | } | |
558 | ||
b34976b6 | 559 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
560 | if (dynstr_index == (unsigned long) -1) |
561 | return 0; | |
562 | entry->isym.st_name = dynstr_index; | |
563 | ||
564 | eht = elf_hash_table (info); | |
565 | ||
566 | entry->next = eht->dynlocal; | |
567 | eht->dynlocal = entry; | |
568 | entry->input_bfd = input_bfd; | |
569 | entry->input_indx = input_indx; | |
570 | eht->dynsymcount++; | |
571 | ||
572 | /* Whatever binding the symbol had before, it's now local. */ | |
573 | entry->isym.st_info | |
574 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
575 | ||
576 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
577 | ||
578 | return 1; | |
579 | } | |
580 | ||
30b30c21 | 581 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 582 | |
30b30c21 | 583 | long |
268b6b39 AM |
584 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
585 | bfd *input_bfd, | |
586 | long input_indx) | |
30b30c21 RH |
587 | { |
588 | struct elf_link_local_dynamic_entry *e; | |
589 | ||
590 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
591 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
592 | return e->dynindx; | |
593 | return -1; | |
594 | } | |
595 | ||
596 | /* This function is used to renumber the dynamic symbols, if some of | |
597 | them are removed because they are marked as local. This is called | |
598 | via elf_link_hash_traverse. */ | |
599 | ||
b34976b6 | 600 | static bfd_boolean |
268b6b39 AM |
601 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
602 | void *data) | |
42751cf3 | 603 | { |
268b6b39 | 604 | size_t *count = data; |
30b30c21 | 605 | |
e92d460e AM |
606 | if (h->root.type == bfd_link_hash_warning) |
607 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
608 | ||
42751cf3 | 609 | if (h->dynindx != -1) |
30b30c21 RH |
610 | h->dynindx = ++(*count); |
611 | ||
b34976b6 | 612 | return TRUE; |
42751cf3 | 613 | } |
30b30c21 | 614 | |
aee6f5b4 AO |
615 | /* Return true if the dynamic symbol for a given section should be |
616 | omitted when creating a shared library. */ | |
617 | bfd_boolean | |
618 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
619 | struct bfd_link_info *info, | |
620 | asection *p) | |
621 | { | |
622 | switch (elf_section_data (p)->this_hdr.sh_type) | |
623 | { | |
624 | case SHT_PROGBITS: | |
625 | case SHT_NOBITS: | |
626 | /* If sh_type is yet undecided, assume it could be | |
627 | SHT_PROGBITS/SHT_NOBITS. */ | |
628 | case SHT_NULL: | |
629 | if (strcmp (p->name, ".got") == 0 | |
630 | || strcmp (p->name, ".got.plt") == 0 | |
631 | || strcmp (p->name, ".plt") == 0) | |
632 | { | |
633 | asection *ip; | |
634 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
635 | ||
636 | if (dynobj != NULL | |
1da212d6 | 637 | && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL |
aee6f5b4 AO |
638 | && (ip->flags & SEC_LINKER_CREATED) |
639 | && ip->output_section == p) | |
640 | return TRUE; | |
641 | } | |
642 | return FALSE; | |
643 | ||
644 | /* There shouldn't be section relative relocations | |
645 | against any other section. */ | |
646 | default: | |
647 | return TRUE; | |
648 | } | |
649 | } | |
650 | ||
062e2358 | 651 | /* Assign dynsym indices. In a shared library we generate a section |
30b30c21 RH |
652 | symbol for each output section, which come first. Next come all of |
653 | the back-end allocated local dynamic syms, followed by the rest of | |
654 | the global symbols. */ | |
655 | ||
656 | unsigned long | |
268b6b39 | 657 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info) |
30b30c21 RH |
658 | { |
659 | unsigned long dynsymcount = 0; | |
660 | ||
661 | if (info->shared) | |
662 | { | |
aee6f5b4 | 663 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
664 | asection *p; |
665 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 666 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
667 | && (p->flags & SEC_ALLOC) != 0 |
668 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
669 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 RH |
670 | } |
671 | ||
672 | if (elf_hash_table (info)->dynlocal) | |
673 | { | |
674 | struct elf_link_local_dynamic_entry *p; | |
675 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
676 | p->dynindx = ++dynsymcount; | |
677 | } | |
678 | ||
679 | elf_link_hash_traverse (elf_hash_table (info), | |
680 | elf_link_renumber_hash_table_dynsyms, | |
681 | &dynsymcount); | |
682 | ||
683 | /* There is an unused NULL entry at the head of the table which | |
684 | we must account for in our count. Unless there weren't any | |
685 | symbols, which means we'll have no table at all. */ | |
686 | if (dynsymcount != 0) | |
687 | ++dynsymcount; | |
688 | ||
689 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
690 | } | |
252b5132 | 691 | |
45d6a902 AM |
692 | /* This function is called when we want to define a new symbol. It |
693 | handles the various cases which arise when we find a definition in | |
694 | a dynamic object, or when there is already a definition in a | |
695 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
696 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
697 | OVERRIDE if the old symbol is overriding a new definition. We set | |
698 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
699 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
700 | change, we mean that we shouldn't warn if the type or size does | |
0f8a2703 | 701 | change. */ |
45d6a902 AM |
702 | |
703 | bfd_boolean | |
268b6b39 AM |
704 | _bfd_elf_merge_symbol (bfd *abfd, |
705 | struct bfd_link_info *info, | |
706 | const char *name, | |
707 | Elf_Internal_Sym *sym, | |
708 | asection **psec, | |
709 | bfd_vma *pvalue, | |
710 | struct elf_link_hash_entry **sym_hash, | |
711 | bfd_boolean *skip, | |
712 | bfd_boolean *override, | |
713 | bfd_boolean *type_change_ok, | |
0f8a2703 | 714 | bfd_boolean *size_change_ok) |
252b5132 | 715 | { |
7479dfd4 | 716 | asection *sec, *oldsec; |
45d6a902 AM |
717 | struct elf_link_hash_entry *h; |
718 | struct elf_link_hash_entry *flip; | |
719 | int bind; | |
720 | bfd *oldbfd; | |
721 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
a5db907e | 722 | bfd_boolean newweak, oldweak, old_asneeded; |
45d6a902 AM |
723 | |
724 | *skip = FALSE; | |
725 | *override = FALSE; | |
726 | ||
727 | sec = *psec; | |
728 | bind = ELF_ST_BIND (sym->st_info); | |
729 | ||
730 | if (! bfd_is_und_section (sec)) | |
731 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
732 | else | |
733 | h = ((struct elf_link_hash_entry *) | |
734 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
735 | if (h == NULL) | |
736 | return FALSE; | |
737 | *sym_hash = h; | |
252b5132 | 738 | |
45d6a902 AM |
739 | /* This code is for coping with dynamic objects, and is only useful |
740 | if we are doing an ELF link. */ | |
741 | if (info->hash->creator != abfd->xvec) | |
742 | return TRUE; | |
252b5132 | 743 | |
45d6a902 AM |
744 | /* For merging, we only care about real symbols. */ |
745 | ||
746 | while (h->root.type == bfd_link_hash_indirect | |
747 | || h->root.type == bfd_link_hash_warning) | |
748 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
749 | ||
750 | /* If we just created the symbol, mark it as being an ELF symbol. | |
751 | Other than that, there is nothing to do--there is no merge issue | |
752 | with a newly defined symbol--so we just return. */ | |
753 | ||
754 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 755 | { |
f5385ebf | 756 | h->non_elf = 0; |
45d6a902 AM |
757 | return TRUE; |
758 | } | |
252b5132 | 759 | |
7479dfd4 L |
760 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
761 | existing symbol. */ | |
252b5132 | 762 | |
45d6a902 AM |
763 | switch (h->root.type) |
764 | { | |
765 | default: | |
766 | oldbfd = NULL; | |
7479dfd4 | 767 | oldsec = NULL; |
45d6a902 | 768 | break; |
252b5132 | 769 | |
45d6a902 AM |
770 | case bfd_link_hash_undefined: |
771 | case bfd_link_hash_undefweak: | |
772 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 773 | oldsec = NULL; |
45d6a902 AM |
774 | break; |
775 | ||
776 | case bfd_link_hash_defined: | |
777 | case bfd_link_hash_defweak: | |
778 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 779 | oldsec = h->root.u.def.section; |
45d6a902 AM |
780 | break; |
781 | ||
782 | case bfd_link_hash_common: | |
783 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 784 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
785 | break; |
786 | } | |
787 | ||
788 | /* In cases involving weak versioned symbols, we may wind up trying | |
789 | to merge a symbol with itself. Catch that here, to avoid the | |
790 | confusion that results if we try to override a symbol with | |
791 | itself. The additional tests catch cases like | |
792 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
793 | dynamic object, which we do want to handle here. */ | |
794 | if (abfd == oldbfd | |
795 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 796 | || !h->def_regular)) |
45d6a902 AM |
797 | return TRUE; |
798 | ||
799 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
800 | respectively, is from a dynamic object. */ | |
801 | ||
802 | if ((abfd->flags & DYNAMIC) != 0) | |
803 | newdyn = TRUE; | |
804 | else | |
805 | newdyn = FALSE; | |
806 | ||
807 | if (oldbfd != NULL) | |
808 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
809 | else | |
810 | { | |
811 | asection *hsec; | |
812 | ||
813 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
814 | indices used by MIPS ELF. */ | |
815 | switch (h->root.type) | |
252b5132 | 816 | { |
45d6a902 AM |
817 | default: |
818 | hsec = NULL; | |
819 | break; | |
252b5132 | 820 | |
45d6a902 AM |
821 | case bfd_link_hash_defined: |
822 | case bfd_link_hash_defweak: | |
823 | hsec = h->root.u.def.section; | |
824 | break; | |
252b5132 | 825 | |
45d6a902 AM |
826 | case bfd_link_hash_common: |
827 | hsec = h->root.u.c.p->section; | |
828 | break; | |
252b5132 | 829 | } |
252b5132 | 830 | |
45d6a902 AM |
831 | if (hsec == NULL) |
832 | olddyn = FALSE; | |
833 | else | |
834 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
835 | } | |
252b5132 | 836 | |
45d6a902 AM |
837 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
838 | respectively, appear to be a definition rather than reference. */ | |
839 | ||
840 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
841 | newdef = FALSE; | |
842 | else | |
843 | newdef = TRUE; | |
844 | ||
845 | if (h->root.type == bfd_link_hash_undefined | |
846 | || h->root.type == bfd_link_hash_undefweak | |
847 | || h->root.type == bfd_link_hash_common) | |
848 | olddef = FALSE; | |
849 | else | |
850 | olddef = TRUE; | |
851 | ||
a5db907e AM |
852 | /* If the old definition came from an as-needed dynamic library which |
853 | wasn't found to be needed, treat the sym as undefined. */ | |
854 | old_asneeded = FALSE; | |
855 | if (newdyn | |
856 | && olddyn | |
857 | && (elf_dyn_lib_class (oldbfd) & DYN_AS_NEEDED) != 0) | |
858 | old_asneeded = TRUE; | |
859 | ||
7479dfd4 L |
860 | /* Check TLS symbol. */ |
861 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) | |
862 | && ELF_ST_TYPE (sym->st_info) != h->type) | |
863 | { | |
864 | bfd *ntbfd, *tbfd; | |
865 | bfd_boolean ntdef, tdef; | |
866 | asection *ntsec, *tsec; | |
867 | ||
868 | if (h->type == STT_TLS) | |
869 | { | |
870 | ntbfd = abfd; | |
871 | ntsec = sec; | |
872 | ntdef = newdef; | |
873 | tbfd = oldbfd; | |
874 | tsec = oldsec; | |
875 | tdef = olddef; | |
876 | } | |
877 | else | |
878 | { | |
879 | ntbfd = oldbfd; | |
880 | ntsec = oldsec; | |
881 | ntdef = olddef; | |
882 | tbfd = abfd; | |
883 | tsec = sec; | |
884 | tdef = newdef; | |
885 | } | |
886 | ||
887 | if (tdef && ntdef) | |
888 | (*_bfd_error_handler) | |
889 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
890 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
891 | else if (!tdef && !ntdef) | |
892 | (*_bfd_error_handler) | |
893 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
894 | tbfd, ntbfd, h->root.root.string); | |
895 | else if (tdef) | |
896 | (*_bfd_error_handler) | |
897 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
898 | tbfd, tsec, ntbfd, h->root.root.string); | |
899 | else | |
900 | (*_bfd_error_handler) | |
901 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
902 | tbfd, ntbfd, ntsec, h->root.root.string); | |
903 | ||
904 | bfd_set_error (bfd_error_bad_value); | |
905 | return FALSE; | |
906 | } | |
907 | ||
4cc11e76 | 908 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
909 | object or is weak in all dynamic objects. Internal and hidden |
910 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 911 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
912 | { |
913 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 914 | h->dynamic_def = 1; |
45d6a902 | 915 | else |
252b5132 | 916 | { |
45d6a902 AM |
917 | /* Check if this symbol is weak in all dynamic objects. If it |
918 | is the first time we see it in a dynamic object, we mark | |
919 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 920 | if (!h->ref_dynamic) |
79349b09 | 921 | { |
45d6a902 | 922 | if (bind == STB_WEAK) |
f5385ebf | 923 | h->dynamic_weak = 1; |
252b5132 | 924 | } |
45d6a902 | 925 | else if (bind != STB_WEAK) |
f5385ebf | 926 | h->dynamic_weak = 0; |
252b5132 | 927 | } |
45d6a902 | 928 | } |
252b5132 | 929 | |
45d6a902 AM |
930 | /* If the old symbol has non-default visibility, we ignore the new |
931 | definition from a dynamic object. */ | |
932 | if (newdyn | |
9c7a29a3 | 933 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
934 | && !bfd_is_und_section (sec)) |
935 | { | |
936 | *skip = TRUE; | |
937 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 938 | h->ref_dynamic = 1; |
45d6a902 AM |
939 | /* A protected symbol has external availability. Make sure it is |
940 | recorded as dynamic. | |
941 | ||
942 | FIXME: Should we check type and size for protected symbol? */ | |
943 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 944 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
945 | else |
946 | return TRUE; | |
947 | } | |
948 | else if (!newdyn | |
9c7a29a3 | 949 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 950 | && h->def_dynamic) |
45d6a902 AM |
951 | { |
952 | /* If the new symbol with non-default visibility comes from a | |
953 | relocatable file and the old definition comes from a dynamic | |
954 | object, we remove the old definition. */ | |
955 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
956 | h = *sym_hash; | |
1de1a317 | 957 | |
f6e332e6 | 958 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
959 | && bfd_is_und_section (sec)) |
960 | { | |
961 | /* If the new symbol is undefined and the old symbol was | |
962 | also undefined before, we need to make sure | |
963 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 964 | up the linker hash table undefs list. Since the old |
1de1a317 L |
965 | definition came from a dynamic object, it is still on the |
966 | undefs list. */ | |
967 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
968 | h->root.u.undef.abfd = abfd; |
969 | } | |
970 | else | |
971 | { | |
972 | h->root.type = bfd_link_hash_new; | |
973 | h->root.u.undef.abfd = NULL; | |
974 | } | |
975 | ||
f5385ebf | 976 | if (h->def_dynamic) |
252b5132 | 977 | { |
f5385ebf AM |
978 | h->def_dynamic = 0; |
979 | h->ref_dynamic = 1; | |
980 | h->dynamic_def = 1; | |
45d6a902 AM |
981 | } |
982 | /* FIXME: Should we check type and size for protected symbol? */ | |
983 | h->size = 0; | |
984 | h->type = 0; | |
985 | return TRUE; | |
986 | } | |
14a793b2 | 987 | |
79349b09 AM |
988 | /* Differentiate strong and weak symbols. */ |
989 | newweak = bind == STB_WEAK; | |
990 | oldweak = (h->root.type == bfd_link_hash_defweak | |
991 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 992 | |
15b43f48 AM |
993 | /* If a new weak symbol definition comes from a regular file and the |
994 | old symbol comes from a dynamic library, we treat the new one as | |
995 | strong. Similarly, an old weak symbol definition from a regular | |
996 | file is treated as strong when the new symbol comes from a dynamic | |
997 | library. Further, an old weak symbol from a dynamic library is | |
998 | treated as strong if the new symbol is from a dynamic library. | |
999 | This reflects the way glibc's ld.so works. | |
1000 | ||
1001 | Do this before setting *type_change_ok or *size_change_ok so that | |
1002 | we warn properly when dynamic library symbols are overridden. */ | |
1003 | ||
1004 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 1005 | newweak = FALSE; |
15b43f48 | 1006 | if (olddef && newdyn) |
0f8a2703 AM |
1007 | oldweak = FALSE; |
1008 | ||
79349b09 AM |
1009 | /* It's OK to change the type if either the existing symbol or the |
1010 | new symbol is weak. A type change is also OK if the old symbol | |
1011 | is undefined and the new symbol is defined. */ | |
252b5132 | 1012 | |
79349b09 AM |
1013 | if (oldweak |
1014 | || newweak | |
1015 | || (newdef | |
1016 | && h->root.type == bfd_link_hash_undefined)) | |
1017 | *type_change_ok = TRUE; | |
1018 | ||
1019 | /* It's OK to change the size if either the existing symbol or the | |
1020 | new symbol is weak, or if the old symbol is undefined. */ | |
1021 | ||
1022 | if (*type_change_ok | |
1023 | || h->root.type == bfd_link_hash_undefined) | |
1024 | *size_change_ok = TRUE; | |
45d6a902 | 1025 | |
45d6a902 AM |
1026 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1027 | symbol, respectively, appears to be a common symbol in a dynamic | |
1028 | object. If a symbol appears in an uninitialized section, and is | |
1029 | not weak, and is not a function, then it may be a common symbol | |
1030 | which was resolved when the dynamic object was created. We want | |
1031 | to treat such symbols specially, because they raise special | |
1032 | considerations when setting the symbol size: if the symbol | |
1033 | appears as a common symbol in a regular object, and the size in | |
1034 | the regular object is larger, we must make sure that we use the | |
1035 | larger size. This problematic case can always be avoided in C, | |
1036 | but it must be handled correctly when using Fortran shared | |
1037 | libraries. | |
1038 | ||
1039 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1040 | likewise for OLDDYNCOMMON and OLDDEF. | |
1041 | ||
1042 | Note that this test is just a heuristic, and that it is quite | |
1043 | possible to have an uninitialized symbol in a shared object which | |
1044 | is really a definition, rather than a common symbol. This could | |
1045 | lead to some minor confusion when the symbol really is a common | |
1046 | symbol in some regular object. However, I think it will be | |
1047 | harmless. */ | |
1048 | ||
1049 | if (newdyn | |
1050 | && newdef | |
79349b09 | 1051 | && !newweak |
45d6a902 AM |
1052 | && (sec->flags & SEC_ALLOC) != 0 |
1053 | && (sec->flags & SEC_LOAD) == 0 | |
1054 | && sym->st_size > 0 | |
45d6a902 AM |
1055 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1056 | newdyncommon = TRUE; | |
1057 | else | |
1058 | newdyncommon = FALSE; | |
1059 | ||
1060 | if (olddyn | |
1061 | && olddef | |
a5db907e | 1062 | && !old_asneeded |
45d6a902 | 1063 | && h->root.type == bfd_link_hash_defined |
f5385ebf | 1064 | && h->def_dynamic |
45d6a902 AM |
1065 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1066 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1067 | && h->size > 0 | |
1068 | && h->type != STT_FUNC) | |
1069 | olddyncommon = TRUE; | |
1070 | else | |
1071 | olddyncommon = FALSE; | |
1072 | ||
45d6a902 AM |
1073 | /* If both the old and the new symbols look like common symbols in a |
1074 | dynamic object, set the size of the symbol to the larger of the | |
1075 | two. */ | |
1076 | ||
1077 | if (olddyncommon | |
1078 | && newdyncommon | |
1079 | && sym->st_size != h->size) | |
1080 | { | |
1081 | /* Since we think we have two common symbols, issue a multiple | |
1082 | common warning if desired. Note that we only warn if the | |
1083 | size is different. If the size is the same, we simply let | |
1084 | the old symbol override the new one as normally happens with | |
1085 | symbols defined in dynamic objects. */ | |
1086 | ||
1087 | if (! ((*info->callbacks->multiple_common) | |
1088 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1089 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1090 | return FALSE; | |
252b5132 | 1091 | |
45d6a902 AM |
1092 | if (sym->st_size > h->size) |
1093 | h->size = sym->st_size; | |
252b5132 | 1094 | |
45d6a902 | 1095 | *size_change_ok = TRUE; |
252b5132 RH |
1096 | } |
1097 | ||
45d6a902 AM |
1098 | /* If we are looking at a dynamic object, and we have found a |
1099 | definition, we need to see if the symbol was already defined by | |
1100 | some other object. If so, we want to use the existing | |
1101 | definition, and we do not want to report a multiple symbol | |
1102 | definition error; we do this by clobbering *PSEC to be | |
1103 | bfd_und_section_ptr. | |
1104 | ||
1105 | We treat a common symbol as a definition if the symbol in the | |
1106 | shared library is a function, since common symbols always | |
1107 | represent variables; this can cause confusion in principle, but | |
1108 | any such confusion would seem to indicate an erroneous program or | |
1109 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1110 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1111 | |
1112 | if (newdyn | |
1113 | && newdef | |
a5db907e | 1114 | && ((olddef && !old_asneeded) |
45d6a902 | 1115 | || (h->root.type == bfd_link_hash_common |
79349b09 | 1116 | && (newweak |
0f8a2703 | 1117 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1118 | { |
1119 | *override = TRUE; | |
1120 | newdef = FALSE; | |
1121 | newdyncommon = FALSE; | |
252b5132 | 1122 | |
45d6a902 AM |
1123 | *psec = sec = bfd_und_section_ptr; |
1124 | *size_change_ok = TRUE; | |
252b5132 | 1125 | |
45d6a902 AM |
1126 | /* If we get here when the old symbol is a common symbol, then |
1127 | we are explicitly letting it override a weak symbol or | |
1128 | function in a dynamic object, and we don't want to warn about | |
1129 | a type change. If the old symbol is a defined symbol, a type | |
1130 | change warning may still be appropriate. */ | |
252b5132 | 1131 | |
45d6a902 AM |
1132 | if (h->root.type == bfd_link_hash_common) |
1133 | *type_change_ok = TRUE; | |
1134 | } | |
1135 | ||
1136 | /* Handle the special case of an old common symbol merging with a | |
1137 | new symbol which looks like a common symbol in a shared object. | |
1138 | We change *PSEC and *PVALUE to make the new symbol look like a | |
1139 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
1140 | the right thing. */ | |
1141 | ||
1142 | if (newdyncommon | |
1143 | && h->root.type == bfd_link_hash_common) | |
1144 | { | |
1145 | *override = TRUE; | |
1146 | newdef = FALSE; | |
1147 | newdyncommon = FALSE; | |
1148 | *pvalue = sym->st_size; | |
1149 | *psec = sec = bfd_com_section_ptr; | |
1150 | *size_change_ok = TRUE; | |
1151 | } | |
1152 | ||
1153 | /* If the old symbol is from a dynamic object, and the new symbol is | |
1154 | a definition which is not from a dynamic object, then the new | |
1155 | symbol overrides the old symbol. Symbols from regular files | |
1156 | always take precedence over symbols from dynamic objects, even if | |
1157 | they are defined after the dynamic object in the link. | |
1158 | ||
1159 | As above, we again permit a common symbol in a regular object to | |
1160 | override a definition in a shared object if the shared object | |
0f8a2703 | 1161 | symbol is a function or is weak. */ |
45d6a902 AM |
1162 | |
1163 | flip = NULL; | |
a5db907e | 1164 | if ((!newdyn || old_asneeded) |
45d6a902 AM |
1165 | && (newdef |
1166 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1167 | && (oldweak |
1168 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1169 | && olddyn |
1170 | && olddef | |
f5385ebf | 1171 | && h->def_dynamic) |
45d6a902 AM |
1172 | { |
1173 | /* Change the hash table entry to undefined, and let | |
1174 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1175 | new definition. */ | |
1176 | ||
1177 | h->root.type = bfd_link_hash_undefined; | |
1178 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1179 | *size_change_ok = TRUE; | |
1180 | ||
1181 | olddef = FALSE; | |
1182 | olddyncommon = FALSE; | |
1183 | ||
1184 | /* We again permit a type change when a common symbol may be | |
1185 | overriding a function. */ | |
1186 | ||
1187 | if (bfd_is_com_section (sec)) | |
1188 | *type_change_ok = TRUE; | |
1189 | ||
1190 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1191 | flip = *sym_hash; | |
1192 | else | |
1193 | /* This union may have been set to be non-NULL when this symbol | |
1194 | was seen in a dynamic object. We must force the union to be | |
1195 | NULL, so that it is correct for a regular symbol. */ | |
1196 | h->verinfo.vertree = NULL; | |
1197 | } | |
1198 | ||
1199 | /* Handle the special case of a new common symbol merging with an | |
1200 | old symbol that looks like it might be a common symbol defined in | |
1201 | a shared object. Note that we have already handled the case in | |
1202 | which a new common symbol should simply override the definition | |
1203 | in the shared library. */ | |
1204 | ||
1205 | if (! newdyn | |
1206 | && bfd_is_com_section (sec) | |
1207 | && olddyncommon) | |
1208 | { | |
1209 | /* It would be best if we could set the hash table entry to a | |
1210 | common symbol, but we don't know what to use for the section | |
1211 | or the alignment. */ | |
1212 | if (! ((*info->callbacks->multiple_common) | |
1213 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1214 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1215 | return FALSE; | |
1216 | ||
4cc11e76 | 1217 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1218 | larger, pretend that the new symbol has its size. */ |
1219 | ||
1220 | if (h->size > *pvalue) | |
1221 | *pvalue = h->size; | |
1222 | ||
1223 | /* FIXME: We no longer know the alignment required by the symbol | |
1224 | in the dynamic object, so we just wind up using the one from | |
1225 | the regular object. */ | |
1226 | ||
1227 | olddef = FALSE; | |
1228 | olddyncommon = FALSE; | |
1229 | ||
1230 | h->root.type = bfd_link_hash_undefined; | |
1231 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1232 | ||
1233 | *size_change_ok = TRUE; | |
1234 | *type_change_ok = TRUE; | |
1235 | ||
1236 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1237 | flip = *sym_hash; | |
1238 | else | |
1239 | h->verinfo.vertree = NULL; | |
1240 | } | |
1241 | ||
1242 | if (flip != NULL) | |
1243 | { | |
1244 | /* Handle the case where we had a versioned symbol in a dynamic | |
1245 | library and now find a definition in a normal object. In this | |
1246 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1247 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1248 | flip->root.type = h->root.type; |
1249 | h->root.type = bfd_link_hash_indirect; | |
1250 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1251 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1252 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
f5385ebf | 1253 | if (h->def_dynamic) |
45d6a902 | 1254 | { |
f5385ebf AM |
1255 | h->def_dynamic = 0; |
1256 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1257 | } |
1258 | } | |
1259 | ||
45d6a902 AM |
1260 | return TRUE; |
1261 | } | |
1262 | ||
1263 | /* This function is called to create an indirect symbol from the | |
1264 | default for the symbol with the default version if needed. The | |
1265 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1266 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1267 | |
1268 | bfd_boolean | |
268b6b39 AM |
1269 | _bfd_elf_add_default_symbol (bfd *abfd, |
1270 | struct bfd_link_info *info, | |
1271 | struct elf_link_hash_entry *h, | |
1272 | const char *name, | |
1273 | Elf_Internal_Sym *sym, | |
1274 | asection **psec, | |
1275 | bfd_vma *value, | |
1276 | bfd_boolean *dynsym, | |
0f8a2703 | 1277 | bfd_boolean override) |
45d6a902 AM |
1278 | { |
1279 | bfd_boolean type_change_ok; | |
1280 | bfd_boolean size_change_ok; | |
1281 | bfd_boolean skip; | |
1282 | char *shortname; | |
1283 | struct elf_link_hash_entry *hi; | |
1284 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1285 | const struct elf_backend_data *bed; |
45d6a902 AM |
1286 | bfd_boolean collect; |
1287 | bfd_boolean dynamic; | |
1288 | char *p; | |
1289 | size_t len, shortlen; | |
1290 | asection *sec; | |
1291 | ||
1292 | /* If this symbol has a version, and it is the default version, we | |
1293 | create an indirect symbol from the default name to the fully | |
1294 | decorated name. This will cause external references which do not | |
1295 | specify a version to be bound to this version of the symbol. */ | |
1296 | p = strchr (name, ELF_VER_CHR); | |
1297 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1298 | return TRUE; | |
1299 | ||
1300 | if (override) | |
1301 | { | |
4cc11e76 | 1302 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1303 | need to create the indirect symbol from the default name. */ |
1304 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1305 | FALSE, FALSE); | |
1306 | BFD_ASSERT (hi != NULL); | |
1307 | if (hi == h) | |
1308 | return TRUE; | |
1309 | while (hi->root.type == bfd_link_hash_indirect | |
1310 | || hi->root.type == bfd_link_hash_warning) | |
1311 | { | |
1312 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1313 | if (hi == h) | |
1314 | return TRUE; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | bed = get_elf_backend_data (abfd); | |
1319 | collect = bed->collect; | |
1320 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1321 | ||
1322 | shortlen = p - name; | |
1323 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1324 | if (shortname == NULL) | |
1325 | return FALSE; | |
1326 | memcpy (shortname, name, shortlen); | |
1327 | shortname[shortlen] = '\0'; | |
1328 | ||
1329 | /* We are going to create a new symbol. Merge it with any existing | |
1330 | symbol with this name. For the purposes of the merge, act as | |
1331 | though we were defining the symbol we just defined, although we | |
1332 | actually going to define an indirect symbol. */ | |
1333 | type_change_ok = FALSE; | |
1334 | size_change_ok = FALSE; | |
1335 | sec = *psec; | |
1336 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1337 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1338 | &size_change_ok)) |
45d6a902 AM |
1339 | return FALSE; |
1340 | ||
1341 | if (skip) | |
1342 | goto nondefault; | |
1343 | ||
1344 | if (! override) | |
1345 | { | |
1346 | bh = &hi->root; | |
1347 | if (! (_bfd_generic_link_add_one_symbol | |
1348 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1349 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1350 | return FALSE; |
1351 | hi = (struct elf_link_hash_entry *) bh; | |
1352 | } | |
1353 | else | |
1354 | { | |
1355 | /* In this case the symbol named SHORTNAME is overriding the | |
1356 | indirect symbol we want to add. We were planning on making | |
1357 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1358 | is the name without a version. NAME is the fully versioned | |
1359 | name, and it is the default version. | |
1360 | ||
1361 | Overriding means that we already saw a definition for the | |
1362 | symbol SHORTNAME in a regular object, and it is overriding | |
1363 | the symbol defined in the dynamic object. | |
1364 | ||
1365 | When this happens, we actually want to change NAME, the | |
1366 | symbol we just added, to refer to SHORTNAME. This will cause | |
1367 | references to NAME in the shared object to become references | |
1368 | to SHORTNAME in the regular object. This is what we expect | |
1369 | when we override a function in a shared object: that the | |
1370 | references in the shared object will be mapped to the | |
1371 | definition in the regular object. */ | |
1372 | ||
1373 | while (hi->root.type == bfd_link_hash_indirect | |
1374 | || hi->root.type == bfd_link_hash_warning) | |
1375 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1376 | ||
1377 | h->root.type = bfd_link_hash_indirect; | |
1378 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1379 | if (h->def_dynamic) |
45d6a902 | 1380 | { |
f5385ebf AM |
1381 | h->def_dynamic = 0; |
1382 | hi->ref_dynamic = 1; | |
1383 | if (hi->ref_regular | |
1384 | || hi->def_regular) | |
45d6a902 | 1385 | { |
c152c796 | 1386 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1387 | return FALSE; |
1388 | } | |
1389 | } | |
1390 | ||
1391 | /* Now set HI to H, so that the following code will set the | |
1392 | other fields correctly. */ | |
1393 | hi = h; | |
1394 | } | |
1395 | ||
1396 | /* If there is a duplicate definition somewhere, then HI may not | |
1397 | point to an indirect symbol. We will have reported an error to | |
1398 | the user in that case. */ | |
1399 | ||
1400 | if (hi->root.type == bfd_link_hash_indirect) | |
1401 | { | |
1402 | struct elf_link_hash_entry *ht; | |
1403 | ||
45d6a902 AM |
1404 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1405 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1406 | ||
1407 | /* See if the new flags lead us to realize that the symbol must | |
1408 | be dynamic. */ | |
1409 | if (! *dynsym) | |
1410 | { | |
1411 | if (! dynamic) | |
1412 | { | |
1413 | if (info->shared | |
f5385ebf | 1414 | || hi->ref_dynamic) |
45d6a902 AM |
1415 | *dynsym = TRUE; |
1416 | } | |
1417 | else | |
1418 | { | |
f5385ebf | 1419 | if (hi->ref_regular) |
45d6a902 AM |
1420 | *dynsym = TRUE; |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | /* We also need to define an indirection from the nondefault version | |
1426 | of the symbol. */ | |
1427 | ||
1428 | nondefault: | |
1429 | len = strlen (name); | |
1430 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1431 | if (shortname == NULL) | |
1432 | return FALSE; | |
1433 | memcpy (shortname, name, shortlen); | |
1434 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1435 | ||
1436 | /* Once again, merge with any existing symbol. */ | |
1437 | type_change_ok = FALSE; | |
1438 | size_change_ok = FALSE; | |
1439 | sec = *psec; | |
1440 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1441 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1442 | &size_change_ok)) |
45d6a902 AM |
1443 | return FALSE; |
1444 | ||
1445 | if (skip) | |
1446 | return TRUE; | |
1447 | ||
1448 | if (override) | |
1449 | { | |
1450 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1451 | the type of override we do in the case above unless it is | |
4cc11e76 | 1452 | overridden by a versioned definition. */ |
45d6a902 AM |
1453 | if (hi->root.type != bfd_link_hash_defined |
1454 | && hi->root.type != bfd_link_hash_defweak) | |
1455 | (*_bfd_error_handler) | |
d003868e AM |
1456 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1457 | abfd, shortname); | |
45d6a902 AM |
1458 | } |
1459 | else | |
1460 | { | |
1461 | bh = &hi->root; | |
1462 | if (! (_bfd_generic_link_add_one_symbol | |
1463 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1464 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1465 | return FALSE; |
1466 | hi = (struct elf_link_hash_entry *) bh; | |
1467 | ||
1468 | /* If there is a duplicate definition somewhere, then HI may not | |
1469 | point to an indirect symbol. We will have reported an error | |
1470 | to the user in that case. */ | |
1471 | ||
1472 | if (hi->root.type == bfd_link_hash_indirect) | |
1473 | { | |
45d6a902 AM |
1474 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1475 | ||
1476 | /* See if the new flags lead us to realize that the symbol | |
1477 | must be dynamic. */ | |
1478 | if (! *dynsym) | |
1479 | { | |
1480 | if (! dynamic) | |
1481 | { | |
1482 | if (info->shared | |
f5385ebf | 1483 | || hi->ref_dynamic) |
45d6a902 AM |
1484 | *dynsym = TRUE; |
1485 | } | |
1486 | else | |
1487 | { | |
f5385ebf | 1488 | if (hi->ref_regular) |
45d6a902 AM |
1489 | *dynsym = TRUE; |
1490 | } | |
1491 | } | |
1492 | } | |
1493 | } | |
1494 | ||
1495 | return TRUE; | |
1496 | } | |
1497 | \f | |
1498 | /* This routine is used to export all defined symbols into the dynamic | |
1499 | symbol table. It is called via elf_link_hash_traverse. */ | |
1500 | ||
1501 | bfd_boolean | |
268b6b39 | 1502 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1503 | { |
268b6b39 | 1504 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1505 | |
1506 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1507 | if (h->root.type == bfd_link_hash_indirect) | |
1508 | return TRUE; | |
1509 | ||
1510 | if (h->root.type == bfd_link_hash_warning) | |
1511 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1512 | ||
1513 | if (h->dynindx == -1 | |
f5385ebf AM |
1514 | && (h->def_regular |
1515 | || h->ref_regular)) | |
45d6a902 AM |
1516 | { |
1517 | struct bfd_elf_version_tree *t; | |
1518 | struct bfd_elf_version_expr *d; | |
1519 | ||
1520 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1521 | { | |
108ba305 | 1522 | if (t->globals.list != NULL) |
45d6a902 | 1523 | { |
108ba305 JJ |
1524 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1525 | if (d != NULL) | |
1526 | goto doit; | |
45d6a902 AM |
1527 | } |
1528 | ||
108ba305 | 1529 | if (t->locals.list != NULL) |
45d6a902 | 1530 | { |
108ba305 JJ |
1531 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1532 | if (d != NULL) | |
1533 | return TRUE; | |
45d6a902 AM |
1534 | } |
1535 | } | |
1536 | ||
1537 | if (!eif->verdefs) | |
1538 | { | |
1539 | doit: | |
c152c796 | 1540 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1541 | { |
1542 | eif->failed = TRUE; | |
1543 | return FALSE; | |
1544 | } | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | return TRUE; | |
1549 | } | |
1550 | \f | |
1551 | /* Look through the symbols which are defined in other shared | |
1552 | libraries and referenced here. Update the list of version | |
1553 | dependencies. This will be put into the .gnu.version_r section. | |
1554 | This function is called via elf_link_hash_traverse. */ | |
1555 | ||
1556 | bfd_boolean | |
268b6b39 AM |
1557 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1558 | void *data) | |
45d6a902 | 1559 | { |
268b6b39 | 1560 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1561 | Elf_Internal_Verneed *t; |
1562 | Elf_Internal_Vernaux *a; | |
1563 | bfd_size_type amt; | |
1564 | ||
1565 | if (h->root.type == bfd_link_hash_warning) | |
1566 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1567 | ||
1568 | /* We only care about symbols defined in shared objects with version | |
1569 | information. */ | |
f5385ebf AM |
1570 | if (!h->def_dynamic |
1571 | || h->def_regular | |
45d6a902 AM |
1572 | || h->dynindx == -1 |
1573 | || h->verinfo.verdef == NULL) | |
1574 | return TRUE; | |
1575 | ||
1576 | /* See if we already know about this version. */ | |
1577 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1578 | { | |
1579 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1580 | continue; | |
1581 | ||
1582 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1583 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1584 | return TRUE; | |
1585 | ||
1586 | break; | |
1587 | } | |
1588 | ||
1589 | /* This is a new version. Add it to tree we are building. */ | |
1590 | ||
1591 | if (t == NULL) | |
1592 | { | |
1593 | amt = sizeof *t; | |
268b6b39 | 1594 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1595 | if (t == NULL) |
1596 | { | |
1597 | rinfo->failed = TRUE; | |
1598 | return FALSE; | |
1599 | } | |
1600 | ||
1601 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1602 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1603 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1604 | } | |
1605 | ||
1606 | amt = sizeof *a; | |
268b6b39 | 1607 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1608 | |
1609 | /* Note that we are copying a string pointer here, and testing it | |
1610 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1611 | discard the string data when low in memory, this will have to be | |
1612 | fixed. */ | |
1613 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1614 | ||
1615 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1616 | a->vna_nextptr = t->vn_auxptr; | |
1617 | ||
1618 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1619 | ++rinfo->vers; | |
1620 | ||
1621 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1622 | ||
1623 | t->vn_auxptr = a; | |
1624 | ||
1625 | return TRUE; | |
1626 | } | |
1627 | ||
1628 | /* Figure out appropriate versions for all the symbols. We may not | |
1629 | have the version number script until we have read all of the input | |
1630 | files, so until that point we don't know which symbols should be | |
1631 | local. This function is called via elf_link_hash_traverse. */ | |
1632 | ||
1633 | bfd_boolean | |
268b6b39 | 1634 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1635 | { |
1636 | struct elf_assign_sym_version_info *sinfo; | |
1637 | struct bfd_link_info *info; | |
9c5bfbb7 | 1638 | const struct elf_backend_data *bed; |
45d6a902 AM |
1639 | struct elf_info_failed eif; |
1640 | char *p; | |
1641 | bfd_size_type amt; | |
1642 | ||
268b6b39 | 1643 | sinfo = data; |
45d6a902 AM |
1644 | info = sinfo->info; |
1645 | ||
1646 | if (h->root.type == bfd_link_hash_warning) | |
1647 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1648 | ||
1649 | /* Fix the symbol flags. */ | |
1650 | eif.failed = FALSE; | |
1651 | eif.info = info; | |
1652 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1653 | { | |
1654 | if (eif.failed) | |
1655 | sinfo->failed = TRUE; | |
1656 | return FALSE; | |
1657 | } | |
1658 | ||
1659 | /* We only need version numbers for symbols defined in regular | |
1660 | objects. */ | |
f5385ebf | 1661 | if (!h->def_regular) |
45d6a902 AM |
1662 | return TRUE; |
1663 | ||
1664 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1665 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1666 | if (p != NULL && h->verinfo.vertree == NULL) | |
1667 | { | |
1668 | struct bfd_elf_version_tree *t; | |
1669 | bfd_boolean hidden; | |
1670 | ||
1671 | hidden = TRUE; | |
1672 | ||
1673 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1674 | not a hidden symbol. */ | |
1675 | ++p; | |
1676 | if (*p == ELF_VER_CHR) | |
1677 | { | |
1678 | hidden = FALSE; | |
1679 | ++p; | |
1680 | } | |
1681 | ||
1682 | /* If there is no version string, we can just return out. */ | |
1683 | if (*p == '\0') | |
1684 | { | |
1685 | if (hidden) | |
f5385ebf | 1686 | h->hidden = 1; |
45d6a902 AM |
1687 | return TRUE; |
1688 | } | |
1689 | ||
1690 | /* Look for the version. If we find it, it is no longer weak. */ | |
1691 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1692 | { | |
1693 | if (strcmp (t->name, p) == 0) | |
1694 | { | |
1695 | size_t len; | |
1696 | char *alc; | |
1697 | struct bfd_elf_version_expr *d; | |
1698 | ||
1699 | len = p - h->root.root.string; | |
268b6b39 | 1700 | alc = bfd_malloc (len); |
45d6a902 AM |
1701 | if (alc == NULL) |
1702 | return FALSE; | |
1703 | memcpy (alc, h->root.root.string, len - 1); | |
1704 | alc[len - 1] = '\0'; | |
1705 | if (alc[len - 2] == ELF_VER_CHR) | |
1706 | alc[len - 2] = '\0'; | |
1707 | ||
1708 | h->verinfo.vertree = t; | |
1709 | t->used = TRUE; | |
1710 | d = NULL; | |
1711 | ||
108ba305 JJ |
1712 | if (t->globals.list != NULL) |
1713 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1714 | |
1715 | /* See if there is anything to force this symbol to | |
1716 | local scope. */ | |
108ba305 | 1717 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1718 | { |
108ba305 JJ |
1719 | d = (*t->match) (&t->locals, NULL, alc); |
1720 | if (d != NULL | |
1721 | && h->dynindx != -1 | |
1722 | && info->shared | |
1723 | && ! info->export_dynamic) | |
1724 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1725 | } |
1726 | ||
1727 | free (alc); | |
1728 | break; | |
1729 | } | |
1730 | } | |
1731 | ||
1732 | /* If we are building an application, we need to create a | |
1733 | version node for this version. */ | |
36af4a4e | 1734 | if (t == NULL && info->executable) |
45d6a902 AM |
1735 | { |
1736 | struct bfd_elf_version_tree **pp; | |
1737 | int version_index; | |
1738 | ||
1739 | /* If we aren't going to export this symbol, we don't need | |
1740 | to worry about it. */ | |
1741 | if (h->dynindx == -1) | |
1742 | return TRUE; | |
1743 | ||
1744 | amt = sizeof *t; | |
108ba305 | 1745 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1746 | if (t == NULL) |
1747 | { | |
1748 | sinfo->failed = TRUE; | |
1749 | return FALSE; | |
1750 | } | |
1751 | ||
45d6a902 | 1752 | t->name = p; |
45d6a902 AM |
1753 | t->name_indx = (unsigned int) -1; |
1754 | t->used = TRUE; | |
1755 | ||
1756 | version_index = 1; | |
1757 | /* Don't count anonymous version tag. */ | |
1758 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1759 | version_index = 0; | |
1760 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1761 | ++version_index; | |
1762 | t->vernum = version_index; | |
1763 | ||
1764 | *pp = t; | |
1765 | ||
1766 | h->verinfo.vertree = t; | |
1767 | } | |
1768 | else if (t == NULL) | |
1769 | { | |
1770 | /* We could not find the version for a symbol when | |
1771 | generating a shared archive. Return an error. */ | |
1772 | (*_bfd_error_handler) | |
d003868e AM |
1773 | (_("%B: undefined versioned symbol name %s"), |
1774 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1775 | bfd_set_error (bfd_error_bad_value); |
1776 | sinfo->failed = TRUE; | |
1777 | return FALSE; | |
1778 | } | |
1779 | ||
1780 | if (hidden) | |
f5385ebf | 1781 | h->hidden = 1; |
45d6a902 AM |
1782 | } |
1783 | ||
1784 | /* If we don't have a version for this symbol, see if we can find | |
1785 | something. */ | |
1786 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1787 | { | |
1788 | struct bfd_elf_version_tree *t; | |
1789 | struct bfd_elf_version_tree *local_ver; | |
1790 | struct bfd_elf_version_expr *d; | |
1791 | ||
1792 | /* See if can find what version this symbol is in. If the | |
1793 | symbol is supposed to be local, then don't actually register | |
1794 | it. */ | |
1795 | local_ver = NULL; | |
1796 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1797 | { | |
108ba305 | 1798 | if (t->globals.list != NULL) |
45d6a902 AM |
1799 | { |
1800 | bfd_boolean matched; | |
1801 | ||
1802 | matched = FALSE; | |
108ba305 JJ |
1803 | d = NULL; |
1804 | while ((d = (*t->match) (&t->globals, d, | |
1805 | h->root.root.string)) != NULL) | |
1806 | if (d->symver) | |
1807 | matched = TRUE; | |
1808 | else | |
1809 | { | |
1810 | /* There is a version without definition. Make | |
1811 | the symbol the default definition for this | |
1812 | version. */ | |
1813 | h->verinfo.vertree = t; | |
1814 | local_ver = NULL; | |
1815 | d->script = 1; | |
1816 | break; | |
1817 | } | |
45d6a902 AM |
1818 | if (d != NULL) |
1819 | break; | |
1820 | else if (matched) | |
1821 | /* There is no undefined version for this symbol. Hide the | |
1822 | default one. */ | |
1823 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1824 | } | |
1825 | ||
108ba305 | 1826 | if (t->locals.list != NULL) |
45d6a902 | 1827 | { |
108ba305 JJ |
1828 | d = NULL; |
1829 | while ((d = (*t->match) (&t->locals, d, | |
1830 | h->root.root.string)) != NULL) | |
45d6a902 | 1831 | { |
108ba305 | 1832 | local_ver = t; |
45d6a902 | 1833 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1834 | explicit, perhaps even global, match. |
1835 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1836 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1837 | break; | |
45d6a902 AM |
1838 | } |
1839 | ||
1840 | if (d != NULL) | |
1841 | break; | |
1842 | } | |
1843 | } | |
1844 | ||
1845 | if (local_ver != NULL) | |
1846 | { | |
1847 | h->verinfo.vertree = local_ver; | |
1848 | if (h->dynindx != -1 | |
1849 | && info->shared | |
1850 | && ! info->export_dynamic) | |
1851 | { | |
1852 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1853 | } | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | return TRUE; | |
1858 | } | |
1859 | \f | |
45d6a902 AM |
1860 | /* Read and swap the relocs from the section indicated by SHDR. This |
1861 | may be either a REL or a RELA section. The relocations are | |
1862 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1863 | which should have already been allocated to contain enough space. | |
1864 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1865 | relocations should be stored. | |
1866 | ||
1867 | Returns FALSE if something goes wrong. */ | |
1868 | ||
1869 | static bfd_boolean | |
268b6b39 | 1870 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1871 | asection *sec, |
268b6b39 AM |
1872 | Elf_Internal_Shdr *shdr, |
1873 | void *external_relocs, | |
1874 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1875 | { |
9c5bfbb7 | 1876 | const struct elf_backend_data *bed; |
268b6b39 | 1877 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1878 | const bfd_byte *erela; |
1879 | const bfd_byte *erelaend; | |
1880 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1881 | Elf_Internal_Shdr *symtab_hdr; |
1882 | size_t nsyms; | |
45d6a902 | 1883 | |
45d6a902 AM |
1884 | /* Position ourselves at the start of the section. */ |
1885 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1886 | return FALSE; | |
1887 | ||
1888 | /* Read the relocations. */ | |
1889 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1890 | return FALSE; | |
1891 | ||
243ef1e0 L |
1892 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1893 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1894 | ||
45d6a902 AM |
1895 | bed = get_elf_backend_data (abfd); |
1896 | ||
1897 | /* Convert the external relocations to the internal format. */ | |
1898 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1899 | swap_in = bed->s->swap_reloc_in; | |
1900 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1901 | swap_in = bed->s->swap_reloca_in; | |
1902 | else | |
1903 | { | |
1904 | bfd_set_error (bfd_error_wrong_format); | |
1905 | return FALSE; | |
1906 | } | |
1907 | ||
1908 | erela = external_relocs; | |
51992aec | 1909 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1910 | irela = internal_relocs; |
1911 | while (erela < erelaend) | |
1912 | { | |
243ef1e0 L |
1913 | bfd_vma r_symndx; |
1914 | ||
45d6a902 | 1915 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1916 | r_symndx = ELF32_R_SYM (irela->r_info); |
1917 | if (bed->s->arch_size == 64) | |
1918 | r_symndx >>= 24; | |
1919 | if ((size_t) r_symndx >= nsyms) | |
1920 | { | |
1921 | (*_bfd_error_handler) | |
d003868e AM |
1922 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
1923 | " for offset 0x%lx in section `%A'"), | |
1924 | abfd, sec, | |
1925 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
1926 | bfd_set_error (bfd_error_bad_value); |
1927 | return FALSE; | |
1928 | } | |
45d6a902 AM |
1929 | irela += bed->s->int_rels_per_ext_rel; |
1930 | erela += shdr->sh_entsize; | |
1931 | } | |
1932 | ||
1933 | return TRUE; | |
1934 | } | |
1935 | ||
1936 | /* Read and swap the relocs for a section O. They may have been | |
1937 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1938 | not NULL, they are used as buffers to read into. They are known to | |
1939 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1940 | the return value is allocated using either malloc or bfd_alloc, | |
1941 | according to the KEEP_MEMORY argument. If O has two relocation | |
1942 | sections (both REL and RELA relocations), then the REL_HDR | |
1943 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1944 | REL_HDR2 relocations. */ | |
1945 | ||
1946 | Elf_Internal_Rela * | |
268b6b39 AM |
1947 | _bfd_elf_link_read_relocs (bfd *abfd, |
1948 | asection *o, | |
1949 | void *external_relocs, | |
1950 | Elf_Internal_Rela *internal_relocs, | |
1951 | bfd_boolean keep_memory) | |
45d6a902 AM |
1952 | { |
1953 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 1954 | void *alloc1 = NULL; |
45d6a902 | 1955 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 1956 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1957 | |
1958 | if (elf_section_data (o)->relocs != NULL) | |
1959 | return elf_section_data (o)->relocs; | |
1960 | ||
1961 | if (o->reloc_count == 0) | |
1962 | return NULL; | |
1963 | ||
1964 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1965 | ||
1966 | if (internal_relocs == NULL) | |
1967 | { | |
1968 | bfd_size_type size; | |
1969 | ||
1970 | size = o->reloc_count; | |
1971 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
1972 | if (keep_memory) | |
268b6b39 | 1973 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 1974 | else |
268b6b39 | 1975 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
1976 | if (internal_relocs == NULL) |
1977 | goto error_return; | |
1978 | } | |
1979 | ||
1980 | if (external_relocs == NULL) | |
1981 | { | |
1982 | bfd_size_type size = rel_hdr->sh_size; | |
1983 | ||
1984 | if (elf_section_data (o)->rel_hdr2) | |
1985 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 1986 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
1987 | if (alloc1 == NULL) |
1988 | goto error_return; | |
1989 | external_relocs = alloc1; | |
1990 | } | |
1991 | ||
243ef1e0 | 1992 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
1993 | external_relocs, |
1994 | internal_relocs)) | |
1995 | goto error_return; | |
51992aec AM |
1996 | if (elf_section_data (o)->rel_hdr2 |
1997 | && (!elf_link_read_relocs_from_section | |
1998 | (abfd, o, | |
1999 | elf_section_data (o)->rel_hdr2, | |
2000 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2001 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2002 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
2003 | goto error_return; |
2004 | ||
2005 | /* Cache the results for next time, if we can. */ | |
2006 | if (keep_memory) | |
2007 | elf_section_data (o)->relocs = internal_relocs; | |
2008 | ||
2009 | if (alloc1 != NULL) | |
2010 | free (alloc1); | |
2011 | ||
2012 | /* Don't free alloc2, since if it was allocated we are passing it | |
2013 | back (under the name of internal_relocs). */ | |
2014 | ||
2015 | return internal_relocs; | |
2016 | ||
2017 | error_return: | |
2018 | if (alloc1 != NULL) | |
2019 | free (alloc1); | |
2020 | if (alloc2 != NULL) | |
2021 | free (alloc2); | |
2022 | return NULL; | |
2023 | } | |
2024 | ||
2025 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2026 | section header for a section containing relocations for O. */ | |
2027 | ||
2028 | bfd_boolean | |
268b6b39 AM |
2029 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2030 | Elf_Internal_Shdr *rel_hdr, | |
2031 | asection *o) | |
45d6a902 AM |
2032 | { |
2033 | bfd_size_type reloc_count; | |
2034 | bfd_size_type num_rel_hashes; | |
2035 | ||
2036 | /* Figure out how many relocations there will be. */ | |
2037 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2038 | reloc_count = elf_section_data (o)->rel_count; | |
2039 | else | |
2040 | reloc_count = elf_section_data (o)->rel_count2; | |
2041 | ||
2042 | num_rel_hashes = o->reloc_count; | |
2043 | if (num_rel_hashes < reloc_count) | |
2044 | num_rel_hashes = reloc_count; | |
2045 | ||
2046 | /* That allows us to calculate the size of the section. */ | |
2047 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2048 | ||
2049 | /* The contents field must last into write_object_contents, so we | |
2050 | allocate it with bfd_alloc rather than malloc. Also since we | |
2051 | cannot be sure that the contents will actually be filled in, | |
2052 | we zero the allocated space. */ | |
268b6b39 | 2053 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2054 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2055 | return FALSE; | |
2056 | ||
2057 | /* We only allocate one set of hash entries, so we only do it the | |
2058 | first time we are called. */ | |
2059 | if (elf_section_data (o)->rel_hashes == NULL | |
2060 | && num_rel_hashes) | |
2061 | { | |
2062 | struct elf_link_hash_entry **p; | |
2063 | ||
268b6b39 | 2064 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2065 | if (p == NULL) |
2066 | return FALSE; | |
2067 | ||
2068 | elf_section_data (o)->rel_hashes = p; | |
2069 | } | |
2070 | ||
2071 | return TRUE; | |
2072 | } | |
2073 | ||
2074 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2075 | originated from the section given by INPUT_REL_HDR) to the | |
2076 | OUTPUT_BFD. */ | |
2077 | ||
2078 | bfd_boolean | |
268b6b39 AM |
2079 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2080 | asection *input_section, | |
2081 | Elf_Internal_Shdr *input_rel_hdr, | |
2082 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 AM |
2083 | { |
2084 | Elf_Internal_Rela *irela; | |
2085 | Elf_Internal_Rela *irelaend; | |
2086 | bfd_byte *erel; | |
2087 | Elf_Internal_Shdr *output_rel_hdr; | |
2088 | asection *output_section; | |
2089 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2090 | const struct elf_backend_data *bed; |
268b6b39 | 2091 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2092 | |
2093 | output_section = input_section->output_section; | |
2094 | output_rel_hdr = NULL; | |
2095 | ||
2096 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2097 | == input_rel_hdr->sh_entsize) | |
2098 | { | |
2099 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2100 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2101 | } | |
2102 | else if (elf_section_data (output_section)->rel_hdr2 | |
2103 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2104 | == input_rel_hdr->sh_entsize)) | |
2105 | { | |
2106 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2107 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2108 | } | |
2109 | else | |
2110 | { | |
2111 | (*_bfd_error_handler) | |
d003868e AM |
2112 | (_("%B: relocation size mismatch in %B section %A"), |
2113 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2114 | bfd_set_error (bfd_error_wrong_object_format); |
2115 | return FALSE; | |
2116 | } | |
2117 | ||
2118 | bed = get_elf_backend_data (output_bfd); | |
2119 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2120 | swap_out = bed->s->swap_reloc_out; | |
2121 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2122 | swap_out = bed->s->swap_reloca_out; | |
2123 | else | |
2124 | abort (); | |
2125 | ||
2126 | erel = output_rel_hdr->contents; | |
2127 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2128 | irela = internal_relocs; | |
2129 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2130 | * bed->s->int_rels_per_ext_rel); | |
2131 | while (irela < irelaend) | |
2132 | { | |
2133 | (*swap_out) (output_bfd, irela, erel); | |
2134 | irela += bed->s->int_rels_per_ext_rel; | |
2135 | erel += input_rel_hdr->sh_entsize; | |
2136 | } | |
2137 | ||
2138 | /* Bump the counter, so that we know where to add the next set of | |
2139 | relocations. */ | |
2140 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2141 | ||
2142 | return TRUE; | |
2143 | } | |
2144 | \f | |
2145 | /* Fix up the flags for a symbol. This handles various cases which | |
2146 | can only be fixed after all the input files are seen. This is | |
2147 | currently called by both adjust_dynamic_symbol and | |
2148 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2149 | the face of future changes. */ | |
2150 | ||
2151 | bfd_boolean | |
268b6b39 AM |
2152 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2153 | struct elf_info_failed *eif) | |
45d6a902 AM |
2154 | { |
2155 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2156 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2157 | permit a non-ELF file to correctly refer to a symbol defined in | |
2158 | an ELF dynamic object. */ | |
f5385ebf | 2159 | if (h->non_elf) |
45d6a902 AM |
2160 | { |
2161 | while (h->root.type == bfd_link_hash_indirect) | |
2162 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2163 | ||
2164 | if (h->root.type != bfd_link_hash_defined | |
2165 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2166 | { |
2167 | h->ref_regular = 1; | |
2168 | h->ref_regular_nonweak = 1; | |
2169 | } | |
45d6a902 AM |
2170 | else |
2171 | { | |
2172 | if (h->root.u.def.section->owner != NULL | |
2173 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2174 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2175 | { |
2176 | h->ref_regular = 1; | |
2177 | h->ref_regular_nonweak = 1; | |
2178 | } | |
45d6a902 | 2179 | else |
f5385ebf | 2180 | h->def_regular = 1; |
45d6a902 AM |
2181 | } |
2182 | ||
2183 | if (h->dynindx == -1 | |
f5385ebf AM |
2184 | && (h->def_dynamic |
2185 | || h->ref_dynamic)) | |
45d6a902 | 2186 | { |
c152c796 | 2187 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2188 | { |
2189 | eif->failed = TRUE; | |
2190 | return FALSE; | |
2191 | } | |
2192 | } | |
2193 | } | |
2194 | else | |
2195 | { | |
f5385ebf | 2196 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2197 | was first seen in a non-ELF file. Fortunately, if the symbol |
2198 | was first seen in an ELF file, we're probably OK unless the | |
2199 | symbol was defined in a non-ELF file. Catch that case here. | |
2200 | FIXME: We're still in trouble if the symbol was first seen in | |
2201 | a dynamic object, and then later in a non-ELF regular object. */ | |
2202 | if ((h->root.type == bfd_link_hash_defined | |
2203 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2204 | && !h->def_regular |
45d6a902 AM |
2205 | && (h->root.u.def.section->owner != NULL |
2206 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2207 | != bfd_target_elf_flavour) | |
2208 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2209 | && !h->def_dynamic))) |
2210 | h->def_regular = 1; | |
45d6a902 AM |
2211 | } |
2212 | ||
2213 | /* If this is a final link, and the symbol was defined as a common | |
2214 | symbol in a regular object file, and there was no definition in | |
2215 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2216 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2217 | flag will not have been set. */ |
2218 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2219 | && !h->def_regular |
2220 | && h->ref_regular | |
2221 | && !h->def_dynamic | |
45d6a902 | 2222 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2223 | h->def_regular = 1; |
45d6a902 AM |
2224 | |
2225 | /* If -Bsymbolic was used (which means to bind references to global | |
2226 | symbols to the definition within the shared object), and this | |
2227 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2228 | need a PLT entry. Likewise, if the symbol has non-default |
2229 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2230 | will force it local. */ |
f5385ebf | 2231 | if (h->needs_plt |
45d6a902 | 2232 | && eif->info->shared |
0eddce27 | 2233 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2234 | && (eif->info->symbolic |
c1be741f | 2235 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2236 | && h->def_regular) |
45d6a902 | 2237 | { |
9c5bfbb7 | 2238 | const struct elf_backend_data *bed; |
45d6a902 AM |
2239 | bfd_boolean force_local; |
2240 | ||
2241 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2242 | ||
2243 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2244 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2245 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2246 | } | |
2247 | ||
2248 | /* If a weak undefined symbol has non-default visibility, we also | |
2249 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2250 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2251 | && h->root.type == bfd_link_hash_undefweak) |
2252 | { | |
9c5bfbb7 | 2253 | const struct elf_backend_data *bed; |
45d6a902 AM |
2254 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2255 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2256 | } | |
2257 | ||
2258 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2259 | the real definition in the dynamic object, copy interesting flags | |
2260 | over to the real definition. */ | |
f6e332e6 | 2261 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2262 | { |
2263 | struct elf_link_hash_entry *weakdef; | |
2264 | ||
f6e332e6 | 2265 | weakdef = h->u.weakdef; |
45d6a902 AM |
2266 | if (h->root.type == bfd_link_hash_indirect) |
2267 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2268 | ||
2269 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2270 | || h->root.type == bfd_link_hash_defweak); | |
2271 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2272 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2273 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2274 | |
2275 | /* If the real definition is defined by a regular object file, | |
2276 | don't do anything special. See the longer description in | |
2277 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2278 | if (weakdef->def_regular) |
f6e332e6 | 2279 | h->u.weakdef = NULL; |
45d6a902 AM |
2280 | else |
2281 | { | |
9c5bfbb7 | 2282 | const struct elf_backend_data *bed; |
45d6a902 AM |
2283 | |
2284 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2285 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2286 | } | |
2287 | } | |
2288 | ||
2289 | return TRUE; | |
2290 | } | |
2291 | ||
2292 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2293 | called via elf_link_hash_traverse, and also calls itself | |
2294 | recursively. */ | |
2295 | ||
2296 | bfd_boolean | |
268b6b39 | 2297 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2298 | { |
268b6b39 | 2299 | struct elf_info_failed *eif = data; |
45d6a902 | 2300 | bfd *dynobj; |
9c5bfbb7 | 2301 | const struct elf_backend_data *bed; |
45d6a902 | 2302 | |
0eddce27 | 2303 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2304 | return FALSE; |
2305 | ||
2306 | if (h->root.type == bfd_link_hash_warning) | |
2307 | { | |
2308 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2309 | h->got = elf_hash_table (eif->info)->init_offset; | |
2310 | ||
2311 | /* When warning symbols are created, they **replace** the "real" | |
2312 | entry in the hash table, thus we never get to see the real | |
2313 | symbol in a hash traversal. So look at it now. */ | |
2314 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2315 | } | |
2316 | ||
2317 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2318 | if (h->root.type == bfd_link_hash_indirect) | |
2319 | return TRUE; | |
2320 | ||
2321 | /* Fix the symbol flags. */ | |
2322 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2323 | return FALSE; | |
2324 | ||
2325 | /* If this symbol does not require a PLT entry, and it is not | |
2326 | defined by a dynamic object, or is not referenced by a regular | |
2327 | object, ignore it. We do have to handle a weak defined symbol, | |
2328 | even if no regular object refers to it, if we decided to add it | |
2329 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2330 | about symbols which are defined by one dynamic object and | |
2331 | referenced by another one? */ | |
f5385ebf AM |
2332 | if (!h->needs_plt |
2333 | && (h->def_regular | |
2334 | || !h->def_dynamic | |
2335 | || (!h->ref_regular | |
f6e332e6 | 2336 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 AM |
2337 | { |
2338 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2339 | return TRUE; | |
2340 | } | |
2341 | ||
2342 | /* If we've already adjusted this symbol, don't do it again. This | |
2343 | can happen via a recursive call. */ | |
f5385ebf | 2344 | if (h->dynamic_adjusted) |
45d6a902 AM |
2345 | return TRUE; |
2346 | ||
2347 | /* Don't look at this symbol again. Note that we must set this | |
2348 | after checking the above conditions, because we may look at a | |
2349 | symbol once, decide not to do anything, and then get called | |
2350 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2351 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2352 | |
2353 | /* If this is a weak definition, and we know a real definition, and | |
2354 | the real symbol is not itself defined by a regular object file, | |
2355 | then get a good value for the real definition. We handle the | |
2356 | real symbol first, for the convenience of the backend routine. | |
2357 | ||
2358 | Note that there is a confusing case here. If the real definition | |
2359 | is defined by a regular object file, we don't get the real symbol | |
2360 | from the dynamic object, but we do get the weak symbol. If the | |
2361 | processor backend uses a COPY reloc, then if some routine in the | |
2362 | dynamic object changes the real symbol, we will not see that | |
2363 | change in the corresponding weak symbol. This is the way other | |
2364 | ELF linkers work as well, and seems to be a result of the shared | |
2365 | library model. | |
2366 | ||
2367 | I will clarify this issue. Most SVR4 shared libraries define the | |
2368 | variable _timezone and define timezone as a weak synonym. The | |
2369 | tzset call changes _timezone. If you write | |
2370 | extern int timezone; | |
2371 | int _timezone = 5; | |
2372 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2373 | you might expect that, since timezone is a synonym for _timezone, | |
2374 | the same number will print both times. However, if the processor | |
2375 | backend uses a COPY reloc, then actually timezone will be copied | |
2376 | into your process image, and, since you define _timezone | |
2377 | yourself, _timezone will not. Thus timezone and _timezone will | |
2378 | wind up at different memory locations. The tzset call will set | |
2379 | _timezone, leaving timezone unchanged. */ | |
2380 | ||
f6e332e6 | 2381 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2382 | { |
2383 | /* If we get to this point, we know there is an implicit | |
2384 | reference by a regular object file via the weak symbol H. | |
2385 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2386 | H->U.WEAKDEF before it finds H? */ |
2387 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2388 | |
f6e332e6 | 2389 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2390 | return FALSE; |
2391 | } | |
2392 | ||
2393 | /* If a symbol has no type and no size and does not require a PLT | |
2394 | entry, then we are probably about to do the wrong thing here: we | |
2395 | are probably going to create a COPY reloc for an empty object. | |
2396 | This case can arise when a shared object is built with assembly | |
2397 | code, and the assembly code fails to set the symbol type. */ | |
2398 | if (h->size == 0 | |
2399 | && h->type == STT_NOTYPE | |
f5385ebf | 2400 | && !h->needs_plt) |
45d6a902 AM |
2401 | (*_bfd_error_handler) |
2402 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2403 | h->root.root.string); | |
2404 | ||
2405 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2406 | bed = get_elf_backend_data (dynobj); | |
2407 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2408 | { | |
2409 | eif->failed = TRUE; | |
2410 | return FALSE; | |
2411 | } | |
2412 | ||
2413 | return TRUE; | |
2414 | } | |
2415 | ||
2416 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2417 | to reflect the object merging within the sections. */ | |
2418 | ||
2419 | bfd_boolean | |
268b6b39 | 2420 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2421 | { |
2422 | asection *sec; | |
2423 | ||
2424 | if (h->root.type == bfd_link_hash_warning) | |
2425 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2426 | ||
2427 | if ((h->root.type == bfd_link_hash_defined | |
2428 | || h->root.type == bfd_link_hash_defweak) | |
2429 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2430 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2431 | { | |
268b6b39 | 2432 | bfd *output_bfd = data; |
45d6a902 AM |
2433 | |
2434 | h->root.u.def.value = | |
2435 | _bfd_merged_section_offset (output_bfd, | |
2436 | &h->root.u.def.section, | |
2437 | elf_section_data (sec)->sec_info, | |
753731ee | 2438 | h->root.u.def.value); |
45d6a902 AM |
2439 | } |
2440 | ||
2441 | return TRUE; | |
2442 | } | |
986a241f RH |
2443 | |
2444 | /* Returns false if the symbol referred to by H should be considered | |
2445 | to resolve local to the current module, and true if it should be | |
2446 | considered to bind dynamically. */ | |
2447 | ||
2448 | bfd_boolean | |
268b6b39 AM |
2449 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2450 | struct bfd_link_info *info, | |
2451 | bfd_boolean ignore_protected) | |
986a241f RH |
2452 | { |
2453 | bfd_boolean binding_stays_local_p; | |
2454 | ||
2455 | if (h == NULL) | |
2456 | return FALSE; | |
2457 | ||
2458 | while (h->root.type == bfd_link_hash_indirect | |
2459 | || h->root.type == bfd_link_hash_warning) | |
2460 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2461 | ||
2462 | /* If it was forced local, then clearly it's not dynamic. */ | |
2463 | if (h->dynindx == -1) | |
2464 | return FALSE; | |
f5385ebf | 2465 | if (h->forced_local) |
986a241f RH |
2466 | return FALSE; |
2467 | ||
2468 | /* Identify the cases where name binding rules say that a | |
2469 | visible symbol resolves locally. */ | |
2470 | binding_stays_local_p = info->executable || info->symbolic; | |
2471 | ||
2472 | switch (ELF_ST_VISIBILITY (h->other)) | |
2473 | { | |
2474 | case STV_INTERNAL: | |
2475 | case STV_HIDDEN: | |
2476 | return FALSE; | |
2477 | ||
2478 | case STV_PROTECTED: | |
2479 | /* Proper resolution for function pointer equality may require | |
2480 | that these symbols perhaps be resolved dynamically, even though | |
2481 | we should be resolving them to the current module. */ | |
2482 | if (!ignore_protected) | |
2483 | binding_stays_local_p = TRUE; | |
2484 | break; | |
2485 | ||
2486 | default: | |
986a241f RH |
2487 | break; |
2488 | } | |
2489 | ||
aa37626c | 2490 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2491 | if (!h->def_regular) |
aa37626c L |
2492 | return TRUE; |
2493 | ||
986a241f RH |
2494 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2495 | us that it remains local. */ | |
2496 | return !binding_stays_local_p; | |
2497 | } | |
f6c52c13 AM |
2498 | |
2499 | /* Return true if the symbol referred to by H should be considered | |
2500 | to resolve local to the current module, and false otherwise. Differs | |
2501 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2502 | undefined symbols and weak symbols. */ | |
2503 | ||
2504 | bfd_boolean | |
268b6b39 AM |
2505 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2506 | struct bfd_link_info *info, | |
2507 | bfd_boolean local_protected) | |
f6c52c13 AM |
2508 | { |
2509 | /* If it's a local sym, of course we resolve locally. */ | |
2510 | if (h == NULL) | |
2511 | return TRUE; | |
2512 | ||
7e2294f9 AO |
2513 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2514 | flag set, so test it first, and don't bail out. */ | |
2515 | if (ELF_COMMON_DEF_P (h)) | |
2516 | /* Do nothing. */; | |
f6c52c13 AM |
2517 | /* If we don't have a definition in a regular file, then we can't |
2518 | resolve locally. The sym is either undefined or dynamic. */ | |
f5385ebf | 2519 | else if (!h->def_regular) |
f6c52c13 AM |
2520 | return FALSE; |
2521 | ||
2522 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2523 | if (h->forced_local) |
f6c52c13 AM |
2524 | return TRUE; |
2525 | ||
2526 | /* As do non-dynamic symbols. */ | |
2527 | if (h->dynindx == -1) | |
2528 | return TRUE; | |
2529 | ||
2530 | /* At this point, we know the symbol is defined and dynamic. In an | |
2531 | executable it must resolve locally, likewise when building symbolic | |
2532 | shared libraries. */ | |
2533 | if (info->executable || info->symbolic) | |
2534 | return TRUE; | |
2535 | ||
2536 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2537 | with default visibility might not resolve locally. */ | |
2538 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2539 | return FALSE; | |
2540 | ||
2541 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2542 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2543 | return TRUE; | |
2544 | ||
2545 | /* Function pointer equality tests may require that STV_PROTECTED | |
2546 | symbols be treated as dynamic symbols, even when we know that the | |
2547 | dynamic linker will resolve them locally. */ | |
2548 | return local_protected; | |
2549 | } | |
e1918d23 AM |
2550 | |
2551 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2552 | aligned. Returns the first TLS output section. */ | |
2553 | ||
2554 | struct bfd_section * | |
2555 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2556 | { | |
2557 | struct bfd_section *sec, *tls; | |
2558 | unsigned int align = 0; | |
2559 | ||
2560 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2561 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2562 | break; | |
2563 | tls = sec; | |
2564 | ||
2565 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2566 | if (sec->alignment_power > align) | |
2567 | align = sec->alignment_power; | |
2568 | ||
2569 | elf_hash_table (info)->tls_sec = tls; | |
2570 | ||
2571 | /* Ensure the alignment of the first section is the largest alignment, | |
2572 | so that the tls segment starts aligned. */ | |
2573 | if (tls != NULL) | |
2574 | tls->alignment_power = align; | |
2575 | ||
2576 | return tls; | |
2577 | } | |
0ad989f9 L |
2578 | |
2579 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2580 | static bfd_boolean | |
2581 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2582 | Elf_Internal_Sym *sym) | |
2583 | { | |
2584 | /* Local symbols do not count, but target specific ones might. */ | |
2585 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2586 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2587 | return FALSE; | |
2588 | ||
2589 | /* Function symbols do not count. */ | |
2590 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2591 | return FALSE; | |
2592 | ||
2593 | /* If the section is undefined, then so is the symbol. */ | |
2594 | if (sym->st_shndx == SHN_UNDEF) | |
2595 | return FALSE; | |
2596 | ||
2597 | /* If the symbol is defined in the common section, then | |
2598 | it is a common definition and so does not count. */ | |
2599 | if (sym->st_shndx == SHN_COMMON) | |
2600 | return FALSE; | |
2601 | ||
2602 | /* If the symbol is in a target specific section then we | |
2603 | must rely upon the backend to tell us what it is. */ | |
2604 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2605 | /* FIXME - this function is not coded yet: | |
2606 | ||
2607 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2608 | ||
2609 | Instead for now assume that the definition is not global, | |
2610 | Even if this is wrong, at least the linker will behave | |
2611 | in the same way that it used to do. */ | |
2612 | return FALSE; | |
2613 | ||
2614 | return TRUE; | |
2615 | } | |
2616 | ||
2617 | /* Search the symbol table of the archive element of the archive ABFD | |
2618 | whose archive map contains a mention of SYMDEF, and determine if | |
2619 | the symbol is defined in this element. */ | |
2620 | static bfd_boolean | |
2621 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2622 | { | |
2623 | Elf_Internal_Shdr * hdr; | |
2624 | bfd_size_type symcount; | |
2625 | bfd_size_type extsymcount; | |
2626 | bfd_size_type extsymoff; | |
2627 | Elf_Internal_Sym *isymbuf; | |
2628 | Elf_Internal_Sym *isym; | |
2629 | Elf_Internal_Sym *isymend; | |
2630 | bfd_boolean result; | |
2631 | ||
2632 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2633 | if (abfd == NULL) | |
2634 | return FALSE; | |
2635 | ||
2636 | if (! bfd_check_format (abfd, bfd_object)) | |
2637 | return FALSE; | |
2638 | ||
2639 | /* If we have already included the element containing this symbol in the | |
2640 | link then we do not need to include it again. Just claim that any symbol | |
2641 | it contains is not a definition, so that our caller will not decide to | |
2642 | (re)include this element. */ | |
2643 | if (abfd->archive_pass) | |
2644 | return FALSE; | |
2645 | ||
2646 | /* Select the appropriate symbol table. */ | |
2647 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2648 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2649 | else | |
2650 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2651 | ||
2652 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2653 | ||
2654 | /* The sh_info field of the symtab header tells us where the | |
2655 | external symbols start. We don't care about the local symbols. */ | |
2656 | if (elf_bad_symtab (abfd)) | |
2657 | { | |
2658 | extsymcount = symcount; | |
2659 | extsymoff = 0; | |
2660 | } | |
2661 | else | |
2662 | { | |
2663 | extsymcount = symcount - hdr->sh_info; | |
2664 | extsymoff = hdr->sh_info; | |
2665 | } | |
2666 | ||
2667 | if (extsymcount == 0) | |
2668 | return FALSE; | |
2669 | ||
2670 | /* Read in the symbol table. */ | |
2671 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2672 | NULL, NULL, NULL); | |
2673 | if (isymbuf == NULL) | |
2674 | return FALSE; | |
2675 | ||
2676 | /* Scan the symbol table looking for SYMDEF. */ | |
2677 | result = FALSE; | |
2678 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2679 | { | |
2680 | const char *name; | |
2681 | ||
2682 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2683 | isym->st_name); | |
2684 | if (name == NULL) | |
2685 | break; | |
2686 | ||
2687 | if (strcmp (name, symdef->name) == 0) | |
2688 | { | |
2689 | result = is_global_data_symbol_definition (abfd, isym); | |
2690 | break; | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | free (isymbuf); | |
2695 | ||
2696 | return result; | |
2697 | } | |
2698 | \f | |
5a580b3a AM |
2699 | /* Add an entry to the .dynamic table. */ |
2700 | ||
2701 | bfd_boolean | |
2702 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2703 | bfd_vma tag, | |
2704 | bfd_vma val) | |
2705 | { | |
2706 | struct elf_link_hash_table *hash_table; | |
2707 | const struct elf_backend_data *bed; | |
2708 | asection *s; | |
2709 | bfd_size_type newsize; | |
2710 | bfd_byte *newcontents; | |
2711 | Elf_Internal_Dyn dyn; | |
2712 | ||
2713 | hash_table = elf_hash_table (info); | |
2714 | if (! is_elf_hash_table (hash_table)) | |
2715 | return FALSE; | |
2716 | ||
8fdd7217 NC |
2717 | if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL) |
2718 | _bfd_error_handler | |
2719 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
2720 | ||
5a580b3a AM |
2721 | bed = get_elf_backend_data (hash_table->dynobj); |
2722 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2723 | BFD_ASSERT (s != NULL); | |
2724 | ||
eea6121a | 2725 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2726 | newcontents = bfd_realloc (s->contents, newsize); |
2727 | if (newcontents == NULL) | |
2728 | return FALSE; | |
2729 | ||
2730 | dyn.d_tag = tag; | |
2731 | dyn.d_un.d_val = val; | |
eea6121a | 2732 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2733 | |
eea6121a | 2734 | s->size = newsize; |
5a580b3a AM |
2735 | s->contents = newcontents; |
2736 | ||
2737 | return TRUE; | |
2738 | } | |
2739 | ||
2740 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2741 | otherwise just check whether one already exists. Returns -1 on error, | |
2742 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2743 | ||
4ad4eba5 AM |
2744 | static int |
2745 | elf_add_dt_needed_tag (struct bfd_link_info *info, | |
2746 | const char *soname, | |
2747 | bfd_boolean do_it) | |
5a580b3a AM |
2748 | { |
2749 | struct elf_link_hash_table *hash_table; | |
2750 | bfd_size_type oldsize; | |
2751 | bfd_size_type strindex; | |
2752 | ||
2753 | hash_table = elf_hash_table (info); | |
2754 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2755 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2756 | if (strindex == (bfd_size_type) -1) | |
2757 | return -1; | |
2758 | ||
2759 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2760 | { | |
2761 | asection *sdyn; | |
2762 | const struct elf_backend_data *bed; | |
2763 | bfd_byte *extdyn; | |
2764 | ||
2765 | bed = get_elf_backend_data (hash_table->dynobj); | |
2766 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2767 | BFD_ASSERT (sdyn != NULL); | |
2768 | ||
2769 | for (extdyn = sdyn->contents; | |
eea6121a | 2770 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
2771 | extdyn += bed->s->sizeof_dyn) |
2772 | { | |
2773 | Elf_Internal_Dyn dyn; | |
2774 | ||
2775 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); | |
2776 | if (dyn.d_tag == DT_NEEDED | |
2777 | && dyn.d_un.d_val == strindex) | |
2778 | { | |
2779 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2780 | return 1; | |
2781 | } | |
2782 | } | |
2783 | } | |
2784 | ||
2785 | if (do_it) | |
2786 | { | |
2787 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) | |
2788 | return -1; | |
2789 | } | |
2790 | else | |
2791 | /* We were just checking for existence of the tag. */ | |
2792 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2793 | ||
2794 | return 0; | |
2795 | } | |
2796 | ||
2797 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
2798 | static int |
2799 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2800 | { |
2801 | const struct elf_link_hash_entry *h1; | |
2802 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2803 | bfd_signed_vma vdiff; |
5a580b3a AM |
2804 | |
2805 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2806 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2807 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2808 | if (vdiff != 0) | |
2809 | return vdiff > 0 ? 1 : -1; | |
2810 | else | |
2811 | { | |
2812 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2813 | if (sdiff != 0) | |
2814 | return sdiff > 0 ? 1 : -1; | |
2815 | } | |
5a580b3a AM |
2816 | return 0; |
2817 | } | |
4ad4eba5 | 2818 | |
5a580b3a AM |
2819 | /* This function is used to adjust offsets into .dynstr for |
2820 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2821 | ||
2822 | static bfd_boolean | |
2823 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2824 | { | |
2825 | struct elf_strtab_hash *dynstr = data; | |
2826 | ||
2827 | if (h->root.type == bfd_link_hash_warning) | |
2828 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2829 | ||
2830 | if (h->dynindx != -1) | |
2831 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2832 | return TRUE; | |
2833 | } | |
2834 | ||
2835 | /* Assign string offsets in .dynstr, update all structures referencing | |
2836 | them. */ | |
2837 | ||
4ad4eba5 AM |
2838 | static bfd_boolean |
2839 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
2840 | { |
2841 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
2842 | struct elf_link_local_dynamic_entry *entry; | |
2843 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
2844 | bfd *dynobj = hash_table->dynobj; | |
2845 | asection *sdyn; | |
2846 | bfd_size_type size; | |
2847 | const struct elf_backend_data *bed; | |
2848 | bfd_byte *extdyn; | |
2849 | ||
2850 | _bfd_elf_strtab_finalize (dynstr); | |
2851 | size = _bfd_elf_strtab_size (dynstr); | |
2852 | ||
2853 | bed = get_elf_backend_data (dynobj); | |
2854 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2855 | BFD_ASSERT (sdyn != NULL); | |
2856 | ||
2857 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
2858 | for (extdyn = sdyn->contents; | |
eea6121a | 2859 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
2860 | extdyn += bed->s->sizeof_dyn) |
2861 | { | |
2862 | Elf_Internal_Dyn dyn; | |
2863 | ||
2864 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
2865 | switch (dyn.d_tag) | |
2866 | { | |
2867 | case DT_STRSZ: | |
2868 | dyn.d_un.d_val = size; | |
2869 | break; | |
2870 | case DT_NEEDED: | |
2871 | case DT_SONAME: | |
2872 | case DT_RPATH: | |
2873 | case DT_RUNPATH: | |
2874 | case DT_FILTER: | |
2875 | case DT_AUXILIARY: | |
2876 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
2877 | break; | |
2878 | default: | |
2879 | continue; | |
2880 | } | |
2881 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
2882 | } | |
2883 | ||
2884 | /* Now update local dynamic symbols. */ | |
2885 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
2886 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
2887 | entry->isym.st_name); | |
2888 | ||
2889 | /* And the rest of dynamic symbols. */ | |
2890 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
2891 | ||
2892 | /* Adjust version definitions. */ | |
2893 | if (elf_tdata (output_bfd)->cverdefs) | |
2894 | { | |
2895 | asection *s; | |
2896 | bfd_byte *p; | |
2897 | bfd_size_type i; | |
2898 | Elf_Internal_Verdef def; | |
2899 | Elf_Internal_Verdaux defaux; | |
2900 | ||
2901 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2902 | p = s->contents; | |
2903 | do | |
2904 | { | |
2905 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
2906 | &def); | |
2907 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
2908 | if (def.vd_aux != sizeof (Elf_External_Verdef)) |
2909 | continue; | |
5a580b3a AM |
2910 | for (i = 0; i < def.vd_cnt; ++i) |
2911 | { | |
2912 | _bfd_elf_swap_verdaux_in (output_bfd, | |
2913 | (Elf_External_Verdaux *) p, &defaux); | |
2914 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
2915 | defaux.vda_name); | |
2916 | _bfd_elf_swap_verdaux_out (output_bfd, | |
2917 | &defaux, (Elf_External_Verdaux *) p); | |
2918 | p += sizeof (Elf_External_Verdaux); | |
2919 | } | |
2920 | } | |
2921 | while (def.vd_next); | |
2922 | } | |
2923 | ||
2924 | /* Adjust version references. */ | |
2925 | if (elf_tdata (output_bfd)->verref) | |
2926 | { | |
2927 | asection *s; | |
2928 | bfd_byte *p; | |
2929 | bfd_size_type i; | |
2930 | Elf_Internal_Verneed need; | |
2931 | Elf_Internal_Vernaux needaux; | |
2932 | ||
2933 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2934 | p = s->contents; | |
2935 | do | |
2936 | { | |
2937 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
2938 | &need); | |
2939 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
2940 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
2941 | (Elf_External_Verneed *) p); | |
2942 | p += sizeof (Elf_External_Verneed); | |
2943 | for (i = 0; i < need.vn_cnt; ++i) | |
2944 | { | |
2945 | _bfd_elf_swap_vernaux_in (output_bfd, | |
2946 | (Elf_External_Vernaux *) p, &needaux); | |
2947 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
2948 | needaux.vna_name); | |
2949 | _bfd_elf_swap_vernaux_out (output_bfd, | |
2950 | &needaux, | |
2951 | (Elf_External_Vernaux *) p); | |
2952 | p += sizeof (Elf_External_Vernaux); | |
2953 | } | |
2954 | } | |
2955 | while (need.vn_next); | |
2956 | } | |
2957 | ||
2958 | return TRUE; | |
2959 | } | |
2960 | \f | |
4ad4eba5 AM |
2961 | /* Add symbols from an ELF object file to the linker hash table. */ |
2962 | ||
2963 | static bfd_boolean | |
2964 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
2965 | { | |
2966 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 2967 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
2968 | const char **, flagword *, asection **, bfd_vma *); |
2969 | bfd_boolean (*check_relocs) | |
2970 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
85fbca6a NC |
2971 | bfd_boolean (*check_directives) |
2972 | (bfd *, struct bfd_link_info *); | |
4ad4eba5 AM |
2973 | bfd_boolean collect; |
2974 | Elf_Internal_Shdr *hdr; | |
2975 | bfd_size_type symcount; | |
2976 | bfd_size_type extsymcount; | |
2977 | bfd_size_type extsymoff; | |
2978 | struct elf_link_hash_entry **sym_hash; | |
2979 | bfd_boolean dynamic; | |
2980 | Elf_External_Versym *extversym = NULL; | |
2981 | Elf_External_Versym *ever; | |
2982 | struct elf_link_hash_entry *weaks; | |
2983 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
2984 | bfd_size_type nondeflt_vers_cnt = 0; | |
2985 | Elf_Internal_Sym *isymbuf = NULL; | |
2986 | Elf_Internal_Sym *isym; | |
2987 | Elf_Internal_Sym *isymend; | |
2988 | const struct elf_backend_data *bed; | |
2989 | bfd_boolean add_needed; | |
2990 | struct elf_link_hash_table * hash_table; | |
2991 | bfd_size_type amt; | |
2992 | ||
2993 | hash_table = elf_hash_table (info); | |
2994 | ||
2995 | bed = get_elf_backend_data (abfd); | |
2996 | add_symbol_hook = bed->elf_add_symbol_hook; | |
2997 | collect = bed->collect; | |
2998 | ||
2999 | if ((abfd->flags & DYNAMIC) == 0) | |
3000 | dynamic = FALSE; | |
3001 | else | |
3002 | { | |
3003 | dynamic = TRUE; | |
3004 | ||
3005 | /* You can't use -r against a dynamic object. Also, there's no | |
3006 | hope of using a dynamic object which does not exactly match | |
3007 | the format of the output file. */ | |
3008 | if (info->relocatable | |
3009 | || !is_elf_hash_table (hash_table) | |
3010 | || hash_table->root.creator != abfd->xvec) | |
3011 | { | |
9a0789ec NC |
3012 | if (info->relocatable) |
3013 | bfd_set_error (bfd_error_invalid_operation); | |
3014 | else | |
3015 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3016 | goto error_return; |
3017 | } | |
3018 | } | |
3019 | ||
3020 | /* As a GNU extension, any input sections which are named | |
3021 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3022 | symbol. This differs from .gnu.warning sections, which generate | |
3023 | warnings when they are included in an output file. */ | |
3024 | if (info->executable) | |
3025 | { | |
3026 | asection *s; | |
3027 | ||
3028 | for (s = abfd->sections; s != NULL; s = s->next) | |
3029 | { | |
3030 | const char *name; | |
3031 | ||
3032 | name = bfd_get_section_name (abfd, s); | |
3033 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
3034 | { | |
3035 | char *msg; | |
3036 | bfd_size_type sz; | |
3037 | bfd_size_type prefix_len; | |
3038 | const char * gnu_warning_prefix = _("warning: "); | |
3039 | ||
3040 | name += sizeof ".gnu.warning." - 1; | |
3041 | ||
3042 | /* If this is a shared object, then look up the symbol | |
3043 | in the hash table. If it is there, and it is already | |
3044 | been defined, then we will not be using the entry | |
3045 | from this shared object, so we don't need to warn. | |
3046 | FIXME: If we see the definition in a regular object | |
3047 | later on, we will warn, but we shouldn't. The only | |
3048 | fix is to keep track of what warnings we are supposed | |
3049 | to emit, and then handle them all at the end of the | |
3050 | link. */ | |
3051 | if (dynamic) | |
3052 | { | |
3053 | struct elf_link_hash_entry *h; | |
3054 | ||
3055 | h = elf_link_hash_lookup (hash_table, name, | |
3056 | FALSE, FALSE, TRUE); | |
3057 | ||
3058 | /* FIXME: What about bfd_link_hash_common? */ | |
3059 | if (h != NULL | |
3060 | && (h->root.type == bfd_link_hash_defined | |
3061 | || h->root.type == bfd_link_hash_defweak)) | |
3062 | { | |
3063 | /* We don't want to issue this warning. Clobber | |
3064 | the section size so that the warning does not | |
3065 | get copied into the output file. */ | |
eea6121a | 3066 | s->size = 0; |
4ad4eba5 AM |
3067 | continue; |
3068 | } | |
3069 | } | |
3070 | ||
eea6121a | 3071 | sz = s->size; |
4ad4eba5 AM |
3072 | prefix_len = strlen (gnu_warning_prefix); |
3073 | msg = bfd_alloc (abfd, prefix_len + sz + 1); | |
3074 | if (msg == NULL) | |
3075 | goto error_return; | |
3076 | ||
3077 | strcpy (msg, gnu_warning_prefix); | |
3078 | if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz)) | |
3079 | goto error_return; | |
3080 | ||
3081 | msg[prefix_len + sz] = '\0'; | |
3082 | ||
3083 | if (! (_bfd_generic_link_add_one_symbol | |
3084 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3085 | FALSE, collect, NULL))) | |
3086 | goto error_return; | |
3087 | ||
3088 | if (! info->relocatable) | |
3089 | { | |
3090 | /* Clobber the section size so that the warning does | |
3091 | not get copied into the output file. */ | |
eea6121a | 3092 | s->size = 0; |
4ad4eba5 AM |
3093 | } |
3094 | } | |
3095 | } | |
3096 | } | |
3097 | ||
3098 | add_needed = TRUE; | |
3099 | if (! dynamic) | |
3100 | { | |
3101 | /* If we are creating a shared library, create all the dynamic | |
3102 | sections immediately. We need to attach them to something, | |
3103 | so we attach them to this BFD, provided it is the right | |
3104 | format. FIXME: If there are no input BFD's of the same | |
3105 | format as the output, we can't make a shared library. */ | |
3106 | if (info->shared | |
3107 | && is_elf_hash_table (hash_table) | |
3108 | && hash_table->root.creator == abfd->xvec | |
3109 | && ! hash_table->dynamic_sections_created) | |
3110 | { | |
3111 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3112 | goto error_return; | |
3113 | } | |
3114 | } | |
3115 | else if (!is_elf_hash_table (hash_table)) | |
3116 | goto error_return; | |
3117 | else | |
3118 | { | |
3119 | asection *s; | |
3120 | const char *soname = NULL; | |
3121 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3122 | int ret; | |
3123 | ||
3124 | /* ld --just-symbols and dynamic objects don't mix very well. | |
3125 | Test for --just-symbols by looking at info set up by | |
3126 | _bfd_elf_link_just_syms. */ | |
3127 | if ((s = abfd->sections) != NULL | |
3128 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
3129 | goto error_return; | |
3130 | ||
3131 | /* If this dynamic lib was specified on the command line with | |
3132 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3133 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3134 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3135 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3136 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3137 | all. */ | |
3138 | add_needed = (elf_dyn_lib_class (abfd) | |
3139 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3140 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3141 | |
3142 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3143 | if (s != NULL) | |
3144 | { | |
3145 | bfd_byte *dynbuf; | |
3146 | bfd_byte *extdyn; | |
3147 | int elfsec; | |
3148 | unsigned long shlink; | |
3149 | ||
eea6121a | 3150 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3151 | goto error_free_dyn; |
3152 | ||
3153 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3154 | if (elfsec == -1) | |
3155 | goto error_free_dyn; | |
3156 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3157 | ||
3158 | for (extdyn = dynbuf; | |
eea6121a | 3159 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3160 | extdyn += bed->s->sizeof_dyn) |
3161 | { | |
3162 | Elf_Internal_Dyn dyn; | |
3163 | ||
3164 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3165 | if (dyn.d_tag == DT_SONAME) | |
3166 | { | |
3167 | unsigned int tagv = dyn.d_un.d_val; | |
3168 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3169 | if (soname == NULL) | |
3170 | goto error_free_dyn; | |
3171 | } | |
3172 | if (dyn.d_tag == DT_NEEDED) | |
3173 | { | |
3174 | struct bfd_link_needed_list *n, **pn; | |
3175 | char *fnm, *anm; | |
3176 | unsigned int tagv = dyn.d_un.d_val; | |
3177 | ||
3178 | amt = sizeof (struct bfd_link_needed_list); | |
3179 | n = bfd_alloc (abfd, amt); | |
3180 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3181 | if (n == NULL || fnm == NULL) | |
3182 | goto error_free_dyn; | |
3183 | amt = strlen (fnm) + 1; | |
3184 | anm = bfd_alloc (abfd, amt); | |
3185 | if (anm == NULL) | |
3186 | goto error_free_dyn; | |
3187 | memcpy (anm, fnm, amt); | |
3188 | n->name = anm; | |
3189 | n->by = abfd; | |
3190 | n->next = NULL; | |
3191 | for (pn = & hash_table->needed; | |
3192 | *pn != NULL; | |
3193 | pn = &(*pn)->next) | |
3194 | ; | |
3195 | *pn = n; | |
3196 | } | |
3197 | if (dyn.d_tag == DT_RUNPATH) | |
3198 | { | |
3199 | struct bfd_link_needed_list *n, **pn; | |
3200 | char *fnm, *anm; | |
3201 | unsigned int tagv = dyn.d_un.d_val; | |
3202 | ||
3203 | amt = sizeof (struct bfd_link_needed_list); | |
3204 | n = bfd_alloc (abfd, amt); | |
3205 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3206 | if (n == NULL || fnm == NULL) | |
3207 | goto error_free_dyn; | |
3208 | amt = strlen (fnm) + 1; | |
3209 | anm = bfd_alloc (abfd, amt); | |
3210 | if (anm == NULL) | |
3211 | goto error_free_dyn; | |
3212 | memcpy (anm, fnm, amt); | |
3213 | n->name = anm; | |
3214 | n->by = abfd; | |
3215 | n->next = NULL; | |
3216 | for (pn = & runpath; | |
3217 | *pn != NULL; | |
3218 | pn = &(*pn)->next) | |
3219 | ; | |
3220 | *pn = n; | |
3221 | } | |
3222 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3223 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3224 | { | |
3225 | struct bfd_link_needed_list *n, **pn; | |
3226 | char *fnm, *anm; | |
3227 | unsigned int tagv = dyn.d_un.d_val; | |
3228 | ||
3229 | amt = sizeof (struct bfd_link_needed_list); | |
3230 | n = bfd_alloc (abfd, amt); | |
3231 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3232 | if (n == NULL || fnm == NULL) | |
3233 | goto error_free_dyn; | |
3234 | amt = strlen (fnm) + 1; | |
3235 | anm = bfd_alloc (abfd, amt); | |
3236 | if (anm == NULL) | |
3237 | { | |
3238 | error_free_dyn: | |
3239 | free (dynbuf); | |
3240 | goto error_return; | |
3241 | } | |
3242 | memcpy (anm, fnm, amt); | |
3243 | n->name = anm; | |
3244 | n->by = abfd; | |
3245 | n->next = NULL; | |
3246 | for (pn = & rpath; | |
3247 | *pn != NULL; | |
3248 | pn = &(*pn)->next) | |
3249 | ; | |
3250 | *pn = n; | |
3251 | } | |
3252 | } | |
3253 | ||
3254 | free (dynbuf); | |
3255 | } | |
3256 | ||
3257 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3258 | frees all more recently bfd_alloc'd blocks as well. */ | |
3259 | if (runpath) | |
3260 | rpath = runpath; | |
3261 | ||
3262 | if (rpath) | |
3263 | { | |
3264 | struct bfd_link_needed_list **pn; | |
3265 | for (pn = & hash_table->runpath; | |
3266 | *pn != NULL; | |
3267 | pn = &(*pn)->next) | |
3268 | ; | |
3269 | *pn = rpath; | |
3270 | } | |
3271 | ||
3272 | /* We do not want to include any of the sections in a dynamic | |
3273 | object in the output file. We hack by simply clobbering the | |
3274 | list of sections in the BFD. This could be handled more | |
3275 | cleanly by, say, a new section flag; the existing | |
3276 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3277 | still implies that the section takes up space in the output | |
3278 | file. */ | |
3279 | bfd_section_list_clear (abfd); | |
3280 | ||
3281 | /* If this is the first dynamic object found in the link, create | |
3282 | the special sections required for dynamic linking. */ | |
3283 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3284 | goto error_return; | |
3285 | ||
3286 | /* Find the name to use in a DT_NEEDED entry that refers to this | |
3287 | object. If the object has a DT_SONAME entry, we use it. | |
3288 | Otherwise, if the generic linker stuck something in | |
3289 | elf_dt_name, we use that. Otherwise, we just use the file | |
3290 | name. */ | |
3291 | if (soname == NULL || *soname == '\0') | |
3292 | { | |
3293 | soname = elf_dt_name (abfd); | |
3294 | if (soname == NULL || *soname == '\0') | |
3295 | soname = bfd_get_filename (abfd); | |
3296 | } | |
3297 | ||
3298 | /* Save the SONAME because sometimes the linker emulation code | |
3299 | will need to know it. */ | |
3300 | elf_dt_name (abfd) = soname; | |
3301 | ||
3302 | ret = elf_add_dt_needed_tag (info, soname, add_needed); | |
3303 | if (ret < 0) | |
3304 | goto error_return; | |
3305 | ||
3306 | /* If we have already included this dynamic object in the | |
3307 | link, just ignore it. There is no reason to include a | |
3308 | particular dynamic object more than once. */ | |
3309 | if (ret > 0) | |
3310 | return TRUE; | |
3311 | } | |
3312 | ||
3313 | /* If this is a dynamic object, we always link against the .dynsym | |
3314 | symbol table, not the .symtab symbol table. The dynamic linker | |
3315 | will only see the .dynsym symbol table, so there is no reason to | |
3316 | look at .symtab for a dynamic object. */ | |
3317 | ||
3318 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3319 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3320 | else | |
3321 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3322 | ||
3323 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3324 | ||
3325 | /* The sh_info field of the symtab header tells us where the | |
3326 | external symbols start. We don't care about the local symbols at | |
3327 | this point. */ | |
3328 | if (elf_bad_symtab (abfd)) | |
3329 | { | |
3330 | extsymcount = symcount; | |
3331 | extsymoff = 0; | |
3332 | } | |
3333 | else | |
3334 | { | |
3335 | extsymcount = symcount - hdr->sh_info; | |
3336 | extsymoff = hdr->sh_info; | |
3337 | } | |
3338 | ||
3339 | sym_hash = NULL; | |
3340 | if (extsymcount != 0) | |
3341 | { | |
3342 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3343 | NULL, NULL, NULL); | |
3344 | if (isymbuf == NULL) | |
3345 | goto error_return; | |
3346 | ||
3347 | /* We store a pointer to the hash table entry for each external | |
3348 | symbol. */ | |
3349 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3350 | sym_hash = bfd_alloc (abfd, amt); | |
3351 | if (sym_hash == NULL) | |
3352 | goto error_free_sym; | |
3353 | elf_sym_hashes (abfd) = sym_hash; | |
3354 | } | |
3355 | ||
3356 | if (dynamic) | |
3357 | { | |
3358 | /* Read in any version definitions. */ | |
fc0e6df6 PB |
3359 | if (!_bfd_elf_slurp_version_tables (abfd, |
3360 | info->default_imported_symver)) | |
4ad4eba5 AM |
3361 | goto error_free_sym; |
3362 | ||
3363 | /* Read in the symbol versions, but don't bother to convert them | |
3364 | to internal format. */ | |
3365 | if (elf_dynversym (abfd) != 0) | |
3366 | { | |
3367 | Elf_Internal_Shdr *versymhdr; | |
3368 | ||
3369 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3370 | extversym = bfd_malloc (versymhdr->sh_size); | |
3371 | if (extversym == NULL) | |
3372 | goto error_free_sym; | |
3373 | amt = versymhdr->sh_size; | |
3374 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3375 | || bfd_bread (extversym, amt, abfd) != amt) | |
3376 | goto error_free_vers; | |
3377 | } | |
3378 | } | |
3379 | ||
3380 | weaks = NULL; | |
3381 | ||
3382 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3383 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3384 | isym < isymend; | |
3385 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3386 | { | |
3387 | int bind; | |
3388 | bfd_vma value; | |
3389 | asection *sec; | |
3390 | flagword flags; | |
3391 | const char *name; | |
3392 | struct elf_link_hash_entry *h; | |
3393 | bfd_boolean definition; | |
3394 | bfd_boolean size_change_ok; | |
3395 | bfd_boolean type_change_ok; | |
3396 | bfd_boolean new_weakdef; | |
3397 | bfd_boolean override; | |
3398 | unsigned int old_alignment; | |
3399 | bfd *old_bfd; | |
3400 | ||
3401 | override = FALSE; | |
3402 | ||
3403 | flags = BSF_NO_FLAGS; | |
3404 | sec = NULL; | |
3405 | value = isym->st_value; | |
3406 | *sym_hash = NULL; | |
3407 | ||
3408 | bind = ELF_ST_BIND (isym->st_info); | |
3409 | if (bind == STB_LOCAL) | |
3410 | { | |
3411 | /* This should be impossible, since ELF requires that all | |
3412 | global symbols follow all local symbols, and that sh_info | |
3413 | point to the first global symbol. Unfortunately, Irix 5 | |
3414 | screws this up. */ | |
3415 | continue; | |
3416 | } | |
3417 | else if (bind == STB_GLOBAL) | |
3418 | { | |
3419 | if (isym->st_shndx != SHN_UNDEF | |
3420 | && isym->st_shndx != SHN_COMMON) | |
3421 | flags = BSF_GLOBAL; | |
3422 | } | |
3423 | else if (bind == STB_WEAK) | |
3424 | flags = BSF_WEAK; | |
3425 | else | |
3426 | { | |
3427 | /* Leave it up to the processor backend. */ | |
3428 | } | |
3429 | ||
3430 | if (isym->st_shndx == SHN_UNDEF) | |
3431 | sec = bfd_und_section_ptr; | |
3432 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3433 | { | |
3434 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3435 | if (sec == NULL) | |
3436 | sec = bfd_abs_section_ptr; | |
529fcb95 PB |
3437 | else if (sec->kept_section) |
3438 | { | |
3439 | /* Symbols from discarded section are undefined. */ | |
3440 | sec = bfd_und_section_ptr; | |
3441 | isym->st_shndx = SHN_UNDEF; | |
3442 | } | |
4ad4eba5 AM |
3443 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
3444 | value -= sec->vma; | |
3445 | } | |
3446 | else if (isym->st_shndx == SHN_ABS) | |
3447 | sec = bfd_abs_section_ptr; | |
3448 | else if (isym->st_shndx == SHN_COMMON) | |
3449 | { | |
3450 | sec = bfd_com_section_ptr; | |
3451 | /* What ELF calls the size we call the value. What ELF | |
3452 | calls the value we call the alignment. */ | |
3453 | value = isym->st_size; | |
3454 | } | |
3455 | else | |
3456 | { | |
3457 | /* Leave it up to the processor backend. */ | |
3458 | } | |
3459 | ||
3460 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3461 | isym->st_name); | |
3462 | if (name == NULL) | |
3463 | goto error_free_vers; | |
3464 | ||
3465 | if (isym->st_shndx == SHN_COMMON | |
3466 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3467 | { | |
3468 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3469 | ||
3470 | if (tcomm == NULL) | |
3471 | { | |
3472 | tcomm = bfd_make_section (abfd, ".tcommon"); | |
3473 | if (tcomm == NULL | |
3474 | || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC | |
3475 | | SEC_IS_COMMON | |
3476 | | SEC_LINKER_CREATED | |
3477 | | SEC_THREAD_LOCAL))) | |
3478 | goto error_free_vers; | |
3479 | } | |
3480 | sec = tcomm; | |
3481 | } | |
3482 | else if (add_symbol_hook) | |
3483 | { | |
3484 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3485 | &value)) | |
3486 | goto error_free_vers; | |
3487 | ||
3488 | /* The hook function sets the name to NULL if this symbol | |
3489 | should be skipped for some reason. */ | |
3490 | if (name == NULL) | |
3491 | continue; | |
3492 | } | |
3493 | ||
3494 | /* Sanity check that all possibilities were handled. */ | |
3495 | if (sec == NULL) | |
3496 | { | |
3497 | bfd_set_error (bfd_error_bad_value); | |
3498 | goto error_free_vers; | |
3499 | } | |
3500 | ||
3501 | if (bfd_is_und_section (sec) | |
3502 | || bfd_is_com_section (sec)) | |
3503 | definition = FALSE; | |
3504 | else | |
3505 | definition = TRUE; | |
3506 | ||
3507 | size_change_ok = FALSE; | |
3508 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3509 | old_alignment = 0; | |
3510 | old_bfd = NULL; | |
3511 | ||
3512 | if (is_elf_hash_table (hash_table)) | |
3513 | { | |
3514 | Elf_Internal_Versym iver; | |
3515 | unsigned int vernum = 0; | |
3516 | bfd_boolean skip; | |
3517 | ||
fc0e6df6 | 3518 | if (ever == NULL) |
4ad4eba5 | 3519 | { |
fc0e6df6 PB |
3520 | if (info->default_imported_symver) |
3521 | /* Use the default symbol version created earlier. */ | |
3522 | iver.vs_vers = elf_tdata (abfd)->cverdefs; | |
3523 | else | |
3524 | iver.vs_vers = 0; | |
3525 | } | |
3526 | else | |
3527 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3528 | ||
3529 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3530 | ||
3531 | /* If this is a hidden symbol, or if it is not version | |
3532 | 1, we append the version name to the symbol name. | |
3533 | However, we do not modify a non-hidden absolute | |
3534 | symbol, because it might be the version symbol | |
3535 | itself. FIXME: What if it isn't? */ | |
3536 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 | |
3537 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
3538 | { | |
3539 | const char *verstr; | |
3540 | size_t namelen, verlen, newlen; | |
3541 | char *newname, *p; | |
3542 | ||
3543 | if (isym->st_shndx != SHN_UNDEF) | |
4ad4eba5 | 3544 | { |
fc0e6df6 PB |
3545 | if (vernum > elf_tdata (abfd)->cverdefs) |
3546 | verstr = NULL; | |
3547 | else if (vernum > 1) | |
3548 | verstr = | |
3549 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3550 | else | |
3551 | verstr = ""; | |
4ad4eba5 | 3552 | |
fc0e6df6 | 3553 | if (verstr == NULL) |
4ad4eba5 | 3554 | { |
fc0e6df6 PB |
3555 | (*_bfd_error_handler) |
3556 | (_("%B: %s: invalid version %u (max %d)"), | |
3557 | abfd, name, vernum, | |
3558 | elf_tdata (abfd)->cverdefs); | |
3559 | bfd_set_error (bfd_error_bad_value); | |
3560 | goto error_free_vers; | |
4ad4eba5 | 3561 | } |
fc0e6df6 PB |
3562 | } |
3563 | else | |
3564 | { | |
3565 | /* We cannot simply test for the number of | |
3566 | entries in the VERNEED section since the | |
3567 | numbers for the needed versions do not start | |
3568 | at 0. */ | |
3569 | Elf_Internal_Verneed *t; | |
3570 | ||
3571 | verstr = NULL; | |
3572 | for (t = elf_tdata (abfd)->verref; | |
3573 | t != NULL; | |
3574 | t = t->vn_nextref) | |
4ad4eba5 | 3575 | { |
fc0e6df6 | 3576 | Elf_Internal_Vernaux *a; |
4ad4eba5 | 3577 | |
fc0e6df6 PB |
3578 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
3579 | { | |
3580 | if (a->vna_other == vernum) | |
4ad4eba5 | 3581 | { |
fc0e6df6 PB |
3582 | verstr = a->vna_nodename; |
3583 | break; | |
4ad4eba5 | 3584 | } |
4ad4eba5 | 3585 | } |
fc0e6df6 PB |
3586 | if (a != NULL) |
3587 | break; | |
3588 | } | |
3589 | if (verstr == NULL) | |
3590 | { | |
3591 | (*_bfd_error_handler) | |
3592 | (_("%B: %s: invalid needed version %d"), | |
3593 | abfd, name, vernum); | |
3594 | bfd_set_error (bfd_error_bad_value); | |
3595 | goto error_free_vers; | |
4ad4eba5 | 3596 | } |
4ad4eba5 | 3597 | } |
fc0e6df6 PB |
3598 | |
3599 | namelen = strlen (name); | |
3600 | verlen = strlen (verstr); | |
3601 | newlen = namelen + verlen + 2; | |
3602 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3603 | && isym->st_shndx != SHN_UNDEF) | |
3604 | ++newlen; | |
3605 | ||
3606 | newname = bfd_alloc (abfd, newlen); | |
3607 | if (newname == NULL) | |
3608 | goto error_free_vers; | |
3609 | memcpy (newname, name, namelen); | |
3610 | p = newname + namelen; | |
3611 | *p++ = ELF_VER_CHR; | |
3612 | /* If this is a defined non-hidden version symbol, | |
3613 | we add another @ to the name. This indicates the | |
3614 | default version of the symbol. */ | |
3615 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3616 | && isym->st_shndx != SHN_UNDEF) | |
3617 | *p++ = ELF_VER_CHR; | |
3618 | memcpy (p, verstr, verlen + 1); | |
3619 | ||
3620 | name = newname; | |
4ad4eba5 AM |
3621 | } |
3622 | ||
3623 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, | |
3624 | sym_hash, &skip, &override, | |
3625 | &type_change_ok, &size_change_ok)) | |
3626 | goto error_free_vers; | |
3627 | ||
3628 | if (skip) | |
3629 | continue; | |
3630 | ||
3631 | if (override) | |
3632 | definition = FALSE; | |
3633 | ||
3634 | h = *sym_hash; | |
3635 | while (h->root.type == bfd_link_hash_indirect | |
3636 | || h->root.type == bfd_link_hash_warning) | |
3637 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3638 | ||
3639 | /* Remember the old alignment if this is a common symbol, so | |
3640 | that we don't reduce the alignment later on. We can't | |
3641 | check later, because _bfd_generic_link_add_one_symbol | |
3642 | will set a default for the alignment which we want to | |
3643 | override. We also remember the old bfd where the existing | |
3644 | definition comes from. */ | |
3645 | switch (h->root.type) | |
3646 | { | |
3647 | default: | |
3648 | break; | |
3649 | ||
3650 | case bfd_link_hash_defined: | |
3651 | case bfd_link_hash_defweak: | |
3652 | old_bfd = h->root.u.def.section->owner; | |
3653 | break; | |
3654 | ||
3655 | case bfd_link_hash_common: | |
3656 | old_bfd = h->root.u.c.p->section->owner; | |
3657 | old_alignment = h->root.u.c.p->alignment_power; | |
3658 | break; | |
3659 | } | |
3660 | ||
3661 | if (elf_tdata (abfd)->verdef != NULL | |
3662 | && ! override | |
3663 | && vernum > 1 | |
3664 | && definition) | |
3665 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3666 | } | |
3667 | ||
3668 | if (! (_bfd_generic_link_add_one_symbol | |
3669 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3670 | (struct bfd_link_hash_entry **) sym_hash))) | |
3671 | goto error_free_vers; | |
3672 | ||
3673 | h = *sym_hash; | |
3674 | while (h->root.type == bfd_link_hash_indirect | |
3675 | || h->root.type == bfd_link_hash_warning) | |
3676 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3677 | *sym_hash = h; | |
3678 | ||
3679 | new_weakdef = FALSE; | |
3680 | if (dynamic | |
3681 | && definition | |
3682 | && (flags & BSF_WEAK) != 0 | |
3683 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3684 | && is_elf_hash_table (hash_table) | |
f6e332e6 | 3685 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3686 | { |
3687 | /* Keep a list of all weak defined non function symbols from | |
3688 | a dynamic object, using the weakdef field. Later in this | |
3689 | function we will set the weakdef field to the correct | |
3690 | value. We only put non-function symbols from dynamic | |
3691 | objects on this list, because that happens to be the only | |
3692 | time we need to know the normal symbol corresponding to a | |
3693 | weak symbol, and the information is time consuming to | |
3694 | figure out. If the weakdef field is not already NULL, | |
3695 | then this symbol was already defined by some previous | |
3696 | dynamic object, and we will be using that previous | |
3697 | definition anyhow. */ | |
3698 | ||
f6e332e6 | 3699 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3700 | weaks = h; |
3701 | new_weakdef = TRUE; | |
3702 | } | |
3703 | ||
3704 | /* Set the alignment of a common symbol. */ | |
3705 | if (isym->st_shndx == SHN_COMMON | |
3706 | && h->root.type == bfd_link_hash_common) | |
3707 | { | |
3708 | unsigned int align; | |
3709 | ||
3710 | align = bfd_log2 (isym->st_value); | |
3711 | if (align > old_alignment | |
3712 | /* Permit an alignment power of zero if an alignment of one | |
3713 | is specified and no other alignments have been specified. */ | |
3714 | || (isym->st_value == 1 && old_alignment == 0)) | |
3715 | h->root.u.c.p->alignment_power = align; | |
3716 | else | |
3717 | h->root.u.c.p->alignment_power = old_alignment; | |
3718 | } | |
3719 | ||
3720 | if (is_elf_hash_table (hash_table)) | |
3721 | { | |
4ad4eba5 | 3722 | bfd_boolean dynsym; |
4ad4eba5 AM |
3723 | |
3724 | /* Check the alignment when a common symbol is involved. This | |
3725 | can change when a common symbol is overridden by a normal | |
3726 | definition or a common symbol is ignored due to the old | |
3727 | normal definition. We need to make sure the maximum | |
3728 | alignment is maintained. */ | |
3729 | if ((old_alignment || isym->st_shndx == SHN_COMMON) | |
3730 | && h->root.type != bfd_link_hash_common) | |
3731 | { | |
3732 | unsigned int common_align; | |
3733 | unsigned int normal_align; | |
3734 | unsigned int symbol_align; | |
3735 | bfd *normal_bfd; | |
3736 | bfd *common_bfd; | |
3737 | ||
3738 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3739 | if (h->root.u.def.section->owner != NULL | |
3740 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3741 | { | |
3742 | normal_align = h->root.u.def.section->alignment_power; | |
3743 | if (normal_align > symbol_align) | |
3744 | normal_align = symbol_align; | |
3745 | } | |
3746 | else | |
3747 | normal_align = symbol_align; | |
3748 | ||
3749 | if (old_alignment) | |
3750 | { | |
3751 | common_align = old_alignment; | |
3752 | common_bfd = old_bfd; | |
3753 | normal_bfd = abfd; | |
3754 | } | |
3755 | else | |
3756 | { | |
3757 | common_align = bfd_log2 (isym->st_value); | |
3758 | common_bfd = abfd; | |
3759 | normal_bfd = old_bfd; | |
3760 | } | |
3761 | ||
3762 | if (normal_align < common_align) | |
3763 | (*_bfd_error_handler) | |
d003868e AM |
3764 | (_("Warning: alignment %u of symbol `%s' in %B" |
3765 | " is smaller than %u in %B"), | |
3766 | normal_bfd, common_bfd, | |
3767 | 1 << normal_align, name, 1 << common_align); | |
4ad4eba5 AM |
3768 | } |
3769 | ||
3770 | /* Remember the symbol size and type. */ | |
3771 | if (isym->st_size != 0 | |
3772 | && (definition || h->size == 0)) | |
3773 | { | |
3774 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3775 | (*_bfd_error_handler) | |
d003868e AM |
3776 | (_("Warning: size of symbol `%s' changed" |
3777 | " from %lu in %B to %lu in %B"), | |
3778 | old_bfd, abfd, | |
4ad4eba5 | 3779 | name, (unsigned long) h->size, |
d003868e | 3780 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
3781 | |
3782 | h->size = isym->st_size; | |
3783 | } | |
3784 | ||
3785 | /* If this is a common symbol, then we always want H->SIZE | |
3786 | to be the size of the common symbol. The code just above | |
3787 | won't fix the size if a common symbol becomes larger. We | |
3788 | don't warn about a size change here, because that is | |
3789 | covered by --warn-common. */ | |
3790 | if (h->root.type == bfd_link_hash_common) | |
3791 | h->size = h->root.u.c.size; | |
3792 | ||
3793 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3794 | && (definition || h->type == STT_NOTYPE)) | |
3795 | { | |
3796 | if (h->type != STT_NOTYPE | |
3797 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3798 | && ! type_change_ok) | |
3799 | (*_bfd_error_handler) | |
d003868e AM |
3800 | (_("Warning: type of symbol `%s' changed" |
3801 | " from %d to %d in %B"), | |
3802 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
3803 | |
3804 | h->type = ELF_ST_TYPE (isym->st_info); | |
3805 | } | |
3806 | ||
3807 | /* If st_other has a processor-specific meaning, specific | |
3808 | code might be needed here. We never merge the visibility | |
3809 | attribute with the one from a dynamic object. */ | |
3810 | if (bed->elf_backend_merge_symbol_attribute) | |
3811 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3812 | dynamic); | |
3813 | ||
b58f81ae DJ |
3814 | /* If this symbol has default visibility and the user has requested |
3815 | we not re-export it, then mark it as hidden. */ | |
3816 | if (definition && !dynamic | |
3817 | && (abfd->no_export | |
3818 | || (abfd->my_archive && abfd->my_archive->no_export)) | |
3819 | && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) | |
3820 | isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)); | |
3821 | ||
4ad4eba5 AM |
3822 | if (isym->st_other != 0 && !dynamic) |
3823 | { | |
3824 | unsigned char hvis, symvis, other, nvis; | |
3825 | ||
3826 | /* Take the balance of OTHER from the definition. */ | |
3827 | other = (definition ? isym->st_other : h->other); | |
3828 | other &= ~ ELF_ST_VISIBILITY (-1); | |
3829 | ||
3830 | /* Combine visibilities, using the most constraining one. */ | |
3831 | hvis = ELF_ST_VISIBILITY (h->other); | |
3832 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
3833 | if (! hvis) | |
3834 | nvis = symvis; | |
3835 | else if (! symvis) | |
3836 | nvis = hvis; | |
3837 | else | |
3838 | nvis = hvis < symvis ? hvis : symvis; | |
3839 | ||
3840 | h->other = other | nvis; | |
3841 | } | |
3842 | ||
3843 | /* Set a flag in the hash table entry indicating the type of | |
3844 | reference or definition we just found. Keep a count of | |
3845 | the number of dynamic symbols we find. A dynamic symbol | |
3846 | is one which is referenced or defined by both a regular | |
3847 | object and a shared object. */ | |
4ad4eba5 AM |
3848 | dynsym = FALSE; |
3849 | if (! dynamic) | |
3850 | { | |
3851 | if (! definition) | |
3852 | { | |
f5385ebf | 3853 | h->ref_regular = 1; |
4ad4eba5 | 3854 | if (bind != STB_WEAK) |
f5385ebf | 3855 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
3856 | } |
3857 | else | |
f5385ebf | 3858 | h->def_regular = 1; |
4ad4eba5 | 3859 | if (! info->executable |
f5385ebf AM |
3860 | || h->def_dynamic |
3861 | || h->ref_dynamic) | |
4ad4eba5 AM |
3862 | dynsym = TRUE; |
3863 | } | |
3864 | else | |
3865 | { | |
3866 | if (! definition) | |
f5385ebf | 3867 | h->ref_dynamic = 1; |
4ad4eba5 | 3868 | else |
f5385ebf AM |
3869 | h->def_dynamic = 1; |
3870 | if (h->def_regular | |
3871 | || h->ref_regular | |
f6e332e6 | 3872 | || (h->u.weakdef != NULL |
4ad4eba5 | 3873 | && ! new_weakdef |
f6e332e6 | 3874 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
3875 | dynsym = TRUE; |
3876 | } | |
3877 | ||
4ad4eba5 AM |
3878 | /* Check to see if we need to add an indirect symbol for |
3879 | the default name. */ | |
3880 | if (definition || h->root.type == bfd_link_hash_common) | |
3881 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
3882 | &sec, &value, &dynsym, | |
3883 | override)) | |
3884 | goto error_free_vers; | |
3885 | ||
3886 | if (definition && !dynamic) | |
3887 | { | |
3888 | char *p = strchr (name, ELF_VER_CHR); | |
3889 | if (p != NULL && p[1] != ELF_VER_CHR) | |
3890 | { | |
3891 | /* Queue non-default versions so that .symver x, x@FOO | |
3892 | aliases can be checked. */ | |
3893 | if (! nondeflt_vers) | |
3894 | { | |
3895 | amt = (isymend - isym + 1) | |
3896 | * sizeof (struct elf_link_hash_entry *); | |
3897 | nondeflt_vers = bfd_malloc (amt); | |
3898 | } | |
3899 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
3900 | } | |
3901 | } | |
3902 | ||
3903 | if (dynsym && h->dynindx == -1) | |
3904 | { | |
c152c796 | 3905 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 3906 | goto error_free_vers; |
f6e332e6 | 3907 | if (h->u.weakdef != NULL |
4ad4eba5 | 3908 | && ! new_weakdef |
f6e332e6 | 3909 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 3910 | { |
f6e332e6 | 3911 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
3912 | goto error_free_vers; |
3913 | } | |
3914 | } | |
3915 | else if (dynsym && h->dynindx != -1) | |
3916 | /* If the symbol already has a dynamic index, but | |
3917 | visibility says it should not be visible, turn it into | |
3918 | a local symbol. */ | |
3919 | switch (ELF_ST_VISIBILITY (h->other)) | |
3920 | { | |
3921 | case STV_INTERNAL: | |
3922 | case STV_HIDDEN: | |
3923 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
3924 | dynsym = FALSE; | |
3925 | break; | |
3926 | } | |
3927 | ||
3928 | if (!add_needed | |
3929 | && definition | |
3930 | && dynsym | |
f5385ebf | 3931 | && h->ref_regular) |
4ad4eba5 AM |
3932 | { |
3933 | int ret; | |
3934 | const char *soname = elf_dt_name (abfd); | |
3935 | ||
3936 | /* A symbol from a library loaded via DT_NEEDED of some | |
3937 | other library is referenced by a regular object. | |
e56f61be L |
3938 | Add a DT_NEEDED entry for it. Issue an error if |
3939 | --no-add-needed is used. */ | |
3940 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
3941 | { | |
3942 | (*_bfd_error_handler) | |
3943 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 3944 | abfd, name); |
e56f61be L |
3945 | bfd_set_error (bfd_error_bad_value); |
3946 | goto error_free_vers; | |
3947 | } | |
3948 | ||
a5db907e AM |
3949 | elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; |
3950 | ||
4ad4eba5 AM |
3951 | add_needed = TRUE; |
3952 | ret = elf_add_dt_needed_tag (info, soname, add_needed); | |
3953 | if (ret < 0) | |
3954 | goto error_free_vers; | |
3955 | ||
3956 | BFD_ASSERT (ret == 0); | |
3957 | } | |
3958 | } | |
3959 | } | |
3960 | ||
3961 | /* Now that all the symbols from this input file are created, handle | |
3962 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
3963 | if (nondeflt_vers != NULL) | |
3964 | { | |
3965 | bfd_size_type cnt, symidx; | |
3966 | ||
3967 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
3968 | { | |
3969 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
3970 | char *shortname, *p; | |
3971 | ||
3972 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
3973 | if (p == NULL | |
3974 | || (h->root.type != bfd_link_hash_defined | |
3975 | && h->root.type != bfd_link_hash_defweak)) | |
3976 | continue; | |
3977 | ||
3978 | amt = p - h->root.root.string; | |
3979 | shortname = bfd_malloc (amt + 1); | |
3980 | memcpy (shortname, h->root.root.string, amt); | |
3981 | shortname[amt] = '\0'; | |
3982 | ||
3983 | hi = (struct elf_link_hash_entry *) | |
3984 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
3985 | FALSE, FALSE, FALSE); | |
3986 | if (hi != NULL | |
3987 | && hi->root.type == h->root.type | |
3988 | && hi->root.u.def.value == h->root.u.def.value | |
3989 | && hi->root.u.def.section == h->root.u.def.section) | |
3990 | { | |
3991 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
3992 | hi->root.type = bfd_link_hash_indirect; | |
3993 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
3994 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
3995 | sym_hash = elf_sym_hashes (abfd); | |
3996 | if (sym_hash) | |
3997 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
3998 | if (sym_hash[symidx] == hi) | |
3999 | { | |
4000 | sym_hash[symidx] = h; | |
4001 | break; | |
4002 | } | |
4003 | } | |
4004 | free (shortname); | |
4005 | } | |
4006 | free (nondeflt_vers); | |
4007 | nondeflt_vers = NULL; | |
4008 | } | |
4009 | ||
4010 | if (extversym != NULL) | |
4011 | { | |
4012 | free (extversym); | |
4013 | extversym = NULL; | |
4014 | } | |
4015 | ||
4016 | if (isymbuf != NULL) | |
4017 | free (isymbuf); | |
4018 | isymbuf = NULL; | |
4019 | ||
4020 | /* Now set the weakdefs field correctly for all the weak defined | |
4021 | symbols we found. The only way to do this is to search all the | |
4022 | symbols. Since we only need the information for non functions in | |
4023 | dynamic objects, that's the only time we actually put anything on | |
4024 | the list WEAKS. We need this information so that if a regular | |
4025 | object refers to a symbol defined weakly in a dynamic object, the | |
4026 | real symbol in the dynamic object is also put in the dynamic | |
4027 | symbols; we also must arrange for both symbols to point to the | |
4028 | same memory location. We could handle the general case of symbol | |
4029 | aliasing, but a general symbol alias can only be generated in | |
4030 | assembler code, handling it correctly would be very time | |
4031 | consuming, and other ELF linkers don't handle general aliasing | |
4032 | either. */ | |
4033 | if (weaks != NULL) | |
4034 | { | |
4035 | struct elf_link_hash_entry **hpp; | |
4036 | struct elf_link_hash_entry **hppend; | |
4037 | struct elf_link_hash_entry **sorted_sym_hash; | |
4038 | struct elf_link_hash_entry *h; | |
4039 | size_t sym_count; | |
4040 | ||
4041 | /* Since we have to search the whole symbol list for each weak | |
4042 | defined symbol, search time for N weak defined symbols will be | |
4043 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4044 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4045 | sorted_sym_hash = bfd_malloc (amt); | |
4046 | if (sorted_sym_hash == NULL) | |
4047 | goto error_return; | |
4048 | sym_hash = sorted_sym_hash; | |
4049 | hpp = elf_sym_hashes (abfd); | |
4050 | hppend = hpp + extsymcount; | |
4051 | sym_count = 0; | |
4052 | for (; hpp < hppend; hpp++) | |
4053 | { | |
4054 | h = *hpp; | |
4055 | if (h != NULL | |
4056 | && h->root.type == bfd_link_hash_defined | |
4057 | && h->type != STT_FUNC) | |
4058 | { | |
4059 | *sym_hash = h; | |
4060 | sym_hash++; | |
4061 | sym_count++; | |
4062 | } | |
4063 | } | |
4064 | ||
4065 | qsort (sorted_sym_hash, sym_count, | |
4066 | sizeof (struct elf_link_hash_entry *), | |
4067 | elf_sort_symbol); | |
4068 | ||
4069 | while (weaks != NULL) | |
4070 | { | |
4071 | struct elf_link_hash_entry *hlook; | |
4072 | asection *slook; | |
4073 | bfd_vma vlook; | |
4074 | long ilook; | |
4075 | size_t i, j, idx; | |
4076 | ||
4077 | hlook = weaks; | |
f6e332e6 AM |
4078 | weaks = hlook->u.weakdef; |
4079 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4080 | |
4081 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4082 | || hlook->root.type == bfd_link_hash_defweak | |
4083 | || hlook->root.type == bfd_link_hash_common | |
4084 | || hlook->root.type == bfd_link_hash_indirect); | |
4085 | slook = hlook->root.u.def.section; | |
4086 | vlook = hlook->root.u.def.value; | |
4087 | ||
4088 | ilook = -1; | |
4089 | i = 0; | |
4090 | j = sym_count; | |
4091 | while (i < j) | |
4092 | { | |
4093 | bfd_signed_vma vdiff; | |
4094 | idx = (i + j) / 2; | |
4095 | h = sorted_sym_hash [idx]; | |
4096 | vdiff = vlook - h->root.u.def.value; | |
4097 | if (vdiff < 0) | |
4098 | j = idx; | |
4099 | else if (vdiff > 0) | |
4100 | i = idx + 1; | |
4101 | else | |
4102 | { | |
a9b881be | 4103 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4104 | if (sdiff < 0) |
4105 | j = idx; | |
4106 | else if (sdiff > 0) | |
4107 | i = idx + 1; | |
4108 | else | |
4109 | { | |
4110 | ilook = idx; | |
4111 | break; | |
4112 | } | |
4113 | } | |
4114 | } | |
4115 | ||
4116 | /* We didn't find a value/section match. */ | |
4117 | if (ilook == -1) | |
4118 | continue; | |
4119 | ||
4120 | for (i = ilook; i < sym_count; i++) | |
4121 | { | |
4122 | h = sorted_sym_hash [i]; | |
4123 | ||
4124 | /* Stop if value or section doesn't match. */ | |
4125 | if (h->root.u.def.value != vlook | |
4126 | || h->root.u.def.section != slook) | |
4127 | break; | |
4128 | else if (h != hlook) | |
4129 | { | |
f6e332e6 | 4130 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4131 | |
4132 | /* If the weak definition is in the list of dynamic | |
4133 | symbols, make sure the real definition is put | |
4134 | there as well. */ | |
4135 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4136 | { | |
c152c796 | 4137 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4138 | goto error_return; |
4139 | } | |
4140 | ||
4141 | /* If the real definition is in the list of dynamic | |
4142 | symbols, make sure the weak definition is put | |
4143 | there as well. If we don't do this, then the | |
4144 | dynamic loader might not merge the entries for the | |
4145 | real definition and the weak definition. */ | |
4146 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4147 | { | |
c152c796 | 4148 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4149 | goto error_return; |
4150 | } | |
4151 | break; | |
4152 | } | |
4153 | } | |
4154 | } | |
4155 | ||
4156 | free (sorted_sym_hash); | |
4157 | } | |
4158 | ||
85fbca6a NC |
4159 | check_directives = get_elf_backend_data (abfd)->check_directives; |
4160 | if (check_directives) | |
4161 | check_directives (abfd, info); | |
4162 | ||
4ad4eba5 AM |
4163 | /* If this object is the same format as the output object, and it is |
4164 | not a shared library, then let the backend look through the | |
4165 | relocs. | |
4166 | ||
4167 | This is required to build global offset table entries and to | |
4168 | arrange for dynamic relocs. It is not required for the | |
4169 | particular common case of linking non PIC code, even when linking | |
4170 | against shared libraries, but unfortunately there is no way of | |
4171 | knowing whether an object file has been compiled PIC or not. | |
4172 | Looking through the relocs is not particularly time consuming. | |
4173 | The problem is that we must either (1) keep the relocs in memory, | |
4174 | which causes the linker to require additional runtime memory or | |
4175 | (2) read the relocs twice from the input file, which wastes time. | |
4176 | This would be a good case for using mmap. | |
4177 | ||
4178 | I have no idea how to handle linking PIC code into a file of a | |
4179 | different format. It probably can't be done. */ | |
4180 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4181 | if (! dynamic | |
4182 | && is_elf_hash_table (hash_table) | |
4183 | && hash_table->root.creator == abfd->xvec | |
4184 | && check_relocs != NULL) | |
4185 | { | |
4186 | asection *o; | |
4187 | ||
4188 | for (o = abfd->sections; o != NULL; o = o->next) | |
4189 | { | |
4190 | Elf_Internal_Rela *internal_relocs; | |
4191 | bfd_boolean ok; | |
4192 | ||
4193 | if ((o->flags & SEC_RELOC) == 0 | |
4194 | || o->reloc_count == 0 | |
4195 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4196 | && (o->flags & SEC_DEBUGGING) != 0) | |
4197 | || bfd_is_abs_section (o->output_section)) | |
4198 | continue; | |
4199 | ||
4200 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4201 | info->keep_memory); | |
4202 | if (internal_relocs == NULL) | |
4203 | goto error_return; | |
4204 | ||
4205 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4206 | ||
4207 | if (elf_section_data (o)->relocs != internal_relocs) | |
4208 | free (internal_relocs); | |
4209 | ||
4210 | if (! ok) | |
4211 | goto error_return; | |
4212 | } | |
4213 | } | |
4214 | ||
4215 | /* If this is a non-traditional link, try to optimize the handling | |
4216 | of the .stab/.stabstr sections. */ | |
4217 | if (! dynamic | |
4218 | && ! info->traditional_format | |
4219 | && is_elf_hash_table (hash_table) | |
4220 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4221 | { | |
4222 | asection *stabstr; | |
4223 | ||
4224 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4225 | if (stabstr != NULL) | |
4226 | { | |
4227 | bfd_size_type string_offset = 0; | |
4228 | asection *stab; | |
4229 | ||
4230 | for (stab = abfd->sections; stab; stab = stab->next) | |
4231 | if (strncmp (".stab", stab->name, 5) == 0 | |
4232 | && (!stab->name[5] || | |
4233 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4234 | && (stab->flags & SEC_MERGE) == 0 | |
4235 | && !bfd_is_abs_section (stab->output_section)) | |
4236 | { | |
4237 | struct bfd_elf_section_data *secdata; | |
4238 | ||
4239 | secdata = elf_section_data (stab); | |
4240 | if (! _bfd_link_section_stabs (abfd, | |
3722b82f | 4241 | &hash_table->stab_info, |
4ad4eba5 AM |
4242 | stab, stabstr, |
4243 | &secdata->sec_info, | |
4244 | &string_offset)) | |
4245 | goto error_return; | |
4246 | if (secdata->sec_info) | |
4247 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4248 | } | |
4249 | } | |
4250 | } | |
4251 | ||
4ad4eba5 AM |
4252 | if (is_elf_hash_table (hash_table)) |
4253 | { | |
4254 | /* Add this bfd to the loaded list. */ | |
4255 | struct elf_link_loaded_list *n; | |
4256 | ||
4257 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4258 | if (n == NULL) | |
4259 | goto error_return; | |
4260 | n->abfd = abfd; | |
4261 | n->next = hash_table->loaded; | |
4262 | hash_table->loaded = n; | |
4263 | } | |
4264 | ||
4265 | return TRUE; | |
4266 | ||
4267 | error_free_vers: | |
4268 | if (nondeflt_vers != NULL) | |
4269 | free (nondeflt_vers); | |
4270 | if (extversym != NULL) | |
4271 | free (extversym); | |
4272 | error_free_sym: | |
4273 | if (isymbuf != NULL) | |
4274 | free (isymbuf); | |
4275 | error_return: | |
4276 | return FALSE; | |
4277 | } | |
4278 | ||
8387904d AM |
4279 | /* Return the linker hash table entry of a symbol that might be |
4280 | satisfied by an archive symbol. Return -1 on error. */ | |
4281 | ||
4282 | struct elf_link_hash_entry * | |
4283 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4284 | struct bfd_link_info *info, | |
4285 | const char *name) | |
4286 | { | |
4287 | struct elf_link_hash_entry *h; | |
4288 | char *p, *copy; | |
4289 | size_t len, first; | |
4290 | ||
4291 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4292 | if (h != NULL) | |
4293 | return h; | |
4294 | ||
4295 | /* If this is a default version (the name contains @@), look up the | |
4296 | symbol again with only one `@' as well as without the version. | |
4297 | The effect is that references to the symbol with and without the | |
4298 | version will be matched by the default symbol in the archive. */ | |
4299 | ||
4300 | p = strchr (name, ELF_VER_CHR); | |
4301 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4302 | return h; | |
4303 | ||
4304 | /* First check with only one `@'. */ | |
4305 | len = strlen (name); | |
4306 | copy = bfd_alloc (abfd, len); | |
4307 | if (copy == NULL) | |
4308 | return (struct elf_link_hash_entry *) 0 - 1; | |
4309 | ||
4310 | first = p - name + 1; | |
4311 | memcpy (copy, name, first); | |
4312 | memcpy (copy + first, name + first + 1, len - first); | |
4313 | ||
4314 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4315 | if (h == NULL) | |
4316 | { | |
4317 | /* We also need to check references to the symbol without the | |
4318 | version. */ | |
4319 | copy[first - 1] = '\0'; | |
4320 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4321 | FALSE, FALSE, FALSE); | |
4322 | } | |
4323 | ||
4324 | bfd_release (abfd, copy); | |
4325 | return h; | |
4326 | } | |
4327 | ||
0ad989f9 L |
4328 | /* Add symbols from an ELF archive file to the linker hash table. We |
4329 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4330 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4331 | archive which includes an entry libc.so.1 which defines a bunch of | |
4332 | symbols. The libc.so archive also includes a number of other | |
4333 | object files, which also define symbols, some of which are the same | |
4334 | as those defined in libc.so.1. Correct linking requires that we | |
4335 | consider each object file in turn, and include it if it defines any | |
4336 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4337 | this; it looks through the list of undefined symbols, and includes | |
4338 | any object file which defines them. When this algorithm is used on | |
4339 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4340 | bunch of symbols. This means that some of the other objects in the | |
4341 | archive are not included in the link, which is incorrect since they | |
4342 | precede libc.so.1 in the archive. | |
4343 | ||
4344 | Fortunately, ELF archive handling is simpler than that done by | |
4345 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4346 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4347 | symbol is currently undefined, we know that we must pull in that | |
4348 | object file. | |
4349 | ||
4350 | Unfortunately, we do have to make multiple passes over the symbol | |
4351 | table until nothing further is resolved. */ | |
4352 | ||
4ad4eba5 AM |
4353 | static bfd_boolean |
4354 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4355 | { |
4356 | symindex c; | |
4357 | bfd_boolean *defined = NULL; | |
4358 | bfd_boolean *included = NULL; | |
4359 | carsym *symdefs; | |
4360 | bfd_boolean loop; | |
4361 | bfd_size_type amt; | |
8387904d AM |
4362 | const struct elf_backend_data *bed; |
4363 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4364 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4365 | |
4366 | if (! bfd_has_map (abfd)) | |
4367 | { | |
4368 | /* An empty archive is a special case. */ | |
4369 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4370 | return TRUE; | |
4371 | bfd_set_error (bfd_error_no_armap); | |
4372 | return FALSE; | |
4373 | } | |
4374 | ||
4375 | /* Keep track of all symbols we know to be already defined, and all | |
4376 | files we know to be already included. This is to speed up the | |
4377 | second and subsequent passes. */ | |
4378 | c = bfd_ardata (abfd)->symdef_count; | |
4379 | if (c == 0) | |
4380 | return TRUE; | |
4381 | amt = c; | |
4382 | amt *= sizeof (bfd_boolean); | |
4383 | defined = bfd_zmalloc (amt); | |
4384 | included = bfd_zmalloc (amt); | |
4385 | if (defined == NULL || included == NULL) | |
4386 | goto error_return; | |
4387 | ||
4388 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4389 | bed = get_elf_backend_data (abfd); |
4390 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4391 | |
4392 | do | |
4393 | { | |
4394 | file_ptr last; | |
4395 | symindex i; | |
4396 | carsym *symdef; | |
4397 | carsym *symdefend; | |
4398 | ||
4399 | loop = FALSE; | |
4400 | last = -1; | |
4401 | ||
4402 | symdef = symdefs; | |
4403 | symdefend = symdef + c; | |
4404 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4405 | { | |
4406 | struct elf_link_hash_entry *h; | |
4407 | bfd *element; | |
4408 | struct bfd_link_hash_entry *undefs_tail; | |
4409 | symindex mark; | |
4410 | ||
4411 | if (defined[i] || included[i]) | |
4412 | continue; | |
4413 | if (symdef->file_offset == last) | |
4414 | { | |
4415 | included[i] = TRUE; | |
4416 | continue; | |
4417 | } | |
4418 | ||
8387904d AM |
4419 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4420 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4421 | goto error_return; | |
0ad989f9 L |
4422 | |
4423 | if (h == NULL) | |
4424 | continue; | |
4425 | ||
4426 | if (h->root.type == bfd_link_hash_common) | |
4427 | { | |
4428 | /* We currently have a common symbol. The archive map contains | |
4429 | a reference to this symbol, so we may want to include it. We | |
4430 | only want to include it however, if this archive element | |
4431 | contains a definition of the symbol, not just another common | |
4432 | declaration of it. | |
4433 | ||
4434 | Unfortunately some archivers (including GNU ar) will put | |
4435 | declarations of common symbols into their archive maps, as | |
4436 | well as real definitions, so we cannot just go by the archive | |
4437 | map alone. Instead we must read in the element's symbol | |
4438 | table and check that to see what kind of symbol definition | |
4439 | this is. */ | |
4440 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4441 | continue; | |
4442 | } | |
4443 | else if (h->root.type != bfd_link_hash_undefined) | |
4444 | { | |
4445 | if (h->root.type != bfd_link_hash_undefweak) | |
4446 | defined[i] = TRUE; | |
4447 | continue; | |
4448 | } | |
4449 | ||
4450 | /* We need to include this archive member. */ | |
4451 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4452 | if (element == NULL) | |
4453 | goto error_return; | |
4454 | ||
4455 | if (! bfd_check_format (element, bfd_object)) | |
4456 | goto error_return; | |
4457 | ||
4458 | /* Doublecheck that we have not included this object | |
4459 | already--it should be impossible, but there may be | |
4460 | something wrong with the archive. */ | |
4461 | if (element->archive_pass != 0) | |
4462 | { | |
4463 | bfd_set_error (bfd_error_bad_value); | |
4464 | goto error_return; | |
4465 | } | |
4466 | element->archive_pass = 1; | |
4467 | ||
4468 | undefs_tail = info->hash->undefs_tail; | |
4469 | ||
4470 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4471 | symdef->name)) | |
4472 | goto error_return; | |
4473 | if (! bfd_link_add_symbols (element, info)) | |
4474 | goto error_return; | |
4475 | ||
4476 | /* If there are any new undefined symbols, we need to make | |
4477 | another pass through the archive in order to see whether | |
4478 | they can be defined. FIXME: This isn't perfect, because | |
4479 | common symbols wind up on undefs_tail and because an | |
4480 | undefined symbol which is defined later on in this pass | |
4481 | does not require another pass. This isn't a bug, but it | |
4482 | does make the code less efficient than it could be. */ | |
4483 | if (undefs_tail != info->hash->undefs_tail) | |
4484 | loop = TRUE; | |
4485 | ||
4486 | /* Look backward to mark all symbols from this object file | |
4487 | which we have already seen in this pass. */ | |
4488 | mark = i; | |
4489 | do | |
4490 | { | |
4491 | included[mark] = TRUE; | |
4492 | if (mark == 0) | |
4493 | break; | |
4494 | --mark; | |
4495 | } | |
4496 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4497 | ||
4498 | /* We mark subsequent symbols from this object file as we go | |
4499 | on through the loop. */ | |
4500 | last = symdef->file_offset; | |
4501 | } | |
4502 | } | |
4503 | while (loop); | |
4504 | ||
4505 | free (defined); | |
4506 | free (included); | |
4507 | ||
4508 | return TRUE; | |
4509 | ||
4510 | error_return: | |
4511 | if (defined != NULL) | |
4512 | free (defined); | |
4513 | if (included != NULL) | |
4514 | free (included); | |
4515 | return FALSE; | |
4516 | } | |
4ad4eba5 AM |
4517 | |
4518 | /* Given an ELF BFD, add symbols to the global hash table as | |
4519 | appropriate. */ | |
4520 | ||
4521 | bfd_boolean | |
4522 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4523 | { | |
4524 | switch (bfd_get_format (abfd)) | |
4525 | { | |
4526 | case bfd_object: | |
4527 | return elf_link_add_object_symbols (abfd, info); | |
4528 | case bfd_archive: | |
4529 | return elf_link_add_archive_symbols (abfd, info); | |
4530 | default: | |
4531 | bfd_set_error (bfd_error_wrong_format); | |
4532 | return FALSE; | |
4533 | } | |
4534 | } | |
5a580b3a AM |
4535 | \f |
4536 | /* This function will be called though elf_link_hash_traverse to store | |
4537 | all hash value of the exported symbols in an array. */ | |
4538 | ||
4539 | static bfd_boolean | |
4540 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4541 | { | |
4542 | unsigned long **valuep = data; | |
4543 | const char *name; | |
4544 | char *p; | |
4545 | unsigned long ha; | |
4546 | char *alc = NULL; | |
4547 | ||
4548 | if (h->root.type == bfd_link_hash_warning) | |
4549 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4550 | ||
4551 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4552 | if (h->dynindx == -1) | |
4553 | return TRUE; | |
4554 | ||
4555 | name = h->root.root.string; | |
4556 | p = strchr (name, ELF_VER_CHR); | |
4557 | if (p != NULL) | |
4558 | { | |
4559 | alc = bfd_malloc (p - name + 1); | |
4560 | memcpy (alc, name, p - name); | |
4561 | alc[p - name] = '\0'; | |
4562 | name = alc; | |
4563 | } | |
4564 | ||
4565 | /* Compute the hash value. */ | |
4566 | ha = bfd_elf_hash (name); | |
4567 | ||
4568 | /* Store the found hash value in the array given as the argument. */ | |
4569 | *(*valuep)++ = ha; | |
4570 | ||
4571 | /* And store it in the struct so that we can put it in the hash table | |
4572 | later. */ | |
f6e332e6 | 4573 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4574 | |
4575 | if (alc != NULL) | |
4576 | free (alc); | |
4577 | ||
4578 | return TRUE; | |
4579 | } | |
4580 | ||
4581 | /* Array used to determine the number of hash table buckets to use | |
4582 | based on the number of symbols there are. If there are fewer than | |
4583 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4584 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4585 | than 32771 buckets. */ | |
4586 | ||
4587 | static const size_t elf_buckets[] = | |
4588 | { | |
4589 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4590 | 16411, 32771, 0 | |
4591 | }; | |
4592 | ||
4593 | /* Compute bucket count for hashing table. We do not use a static set | |
4594 | of possible tables sizes anymore. Instead we determine for all | |
4595 | possible reasonable sizes of the table the outcome (i.e., the | |
4596 | number of collisions etc) and choose the best solution. The | |
4597 | weighting functions are not too simple to allow the table to grow | |
4598 | without bounds. Instead one of the weighting factors is the size. | |
4599 | Therefore the result is always a good payoff between few collisions | |
4600 | (= short chain lengths) and table size. */ | |
4601 | static size_t | |
4602 | compute_bucket_count (struct bfd_link_info *info) | |
4603 | { | |
4604 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4605 | size_t best_size = 0; | |
4606 | unsigned long int *hashcodes; | |
4607 | unsigned long int *hashcodesp; | |
4608 | unsigned long int i; | |
4609 | bfd_size_type amt; | |
4610 | ||
4611 | /* Compute the hash values for all exported symbols. At the same | |
4612 | time store the values in an array so that we could use them for | |
4613 | optimizations. */ | |
4614 | amt = dynsymcount; | |
4615 | amt *= sizeof (unsigned long int); | |
4616 | hashcodes = bfd_malloc (amt); | |
4617 | if (hashcodes == NULL) | |
4618 | return 0; | |
4619 | hashcodesp = hashcodes; | |
4620 | ||
4621 | /* Put all hash values in HASHCODES. */ | |
4622 | elf_link_hash_traverse (elf_hash_table (info), | |
4623 | elf_collect_hash_codes, &hashcodesp); | |
4624 | ||
4625 | /* We have a problem here. The following code to optimize the table | |
4626 | size requires an integer type with more the 32 bits. If | |
4627 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4628 | #ifdef BFD_HOST_U_64_BIT | |
4629 | if (info->optimize) | |
4630 | { | |
4631 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4632 | size_t minsize; | |
4633 | size_t maxsize; | |
4634 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4635 | unsigned long int *counts ; | |
4636 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4637 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4638 | ||
4639 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4640 | that the hashing table must at least have NSYMS/4 and at most | |
4641 | 2*NSYMS buckets. */ | |
4642 | minsize = nsyms / 4; | |
4643 | if (minsize == 0) | |
4644 | minsize = 1; | |
4645 | best_size = maxsize = nsyms * 2; | |
4646 | ||
4647 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4648 | since the size could be large. */ | |
4649 | amt = maxsize; | |
4650 | amt *= sizeof (unsigned long int); | |
4651 | counts = bfd_malloc (amt); | |
4652 | if (counts == NULL) | |
4653 | { | |
4654 | free (hashcodes); | |
4655 | return 0; | |
4656 | } | |
4657 | ||
4658 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4659 | minimal chain length. The minor criteria is (of course) the size | |
4660 | of the table. */ | |
4661 | for (i = minsize; i < maxsize; ++i) | |
4662 | { | |
4663 | /* Walk through the array of hashcodes and count the collisions. */ | |
4664 | BFD_HOST_U_64_BIT max; | |
4665 | unsigned long int j; | |
4666 | unsigned long int fact; | |
4667 | ||
4668 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4669 | ||
4670 | /* Determine how often each hash bucket is used. */ | |
4671 | for (j = 0; j < nsyms; ++j) | |
4672 | ++counts[hashcodes[j] % i]; | |
4673 | ||
4674 | /* For the weight function we need some information about the | |
4675 | pagesize on the target. This is information need not be 100% | |
4676 | accurate. Since this information is not available (so far) we | |
4677 | define it here to a reasonable default value. If it is crucial | |
4678 | to have a better value some day simply define this value. */ | |
4679 | # ifndef BFD_TARGET_PAGESIZE | |
4680 | # define BFD_TARGET_PAGESIZE (4096) | |
4681 | # endif | |
4682 | ||
4683 | /* We in any case need 2 + NSYMS entries for the size values and | |
4684 | the chains. */ | |
4685 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4686 | ||
4687 | # if 1 | |
4688 | /* Variant 1: optimize for short chains. We add the squares | |
4689 | of all the chain lengths (which favors many small chain | |
4690 | over a few long chains). */ | |
4691 | for (j = 0; j < i; ++j) | |
4692 | max += counts[j] * counts[j]; | |
4693 | ||
4694 | /* This adds penalties for the overall size of the table. */ | |
4695 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4696 | max *= fact * fact; | |
4697 | # else | |
4698 | /* Variant 2: Optimize a lot more for small table. Here we | |
4699 | also add squares of the size but we also add penalties for | |
4700 | empty slots (the +1 term). */ | |
4701 | for (j = 0; j < i; ++j) | |
4702 | max += (1 + counts[j]) * (1 + counts[j]); | |
4703 | ||
4704 | /* The overall size of the table is considered, but not as | |
4705 | strong as in variant 1, where it is squared. */ | |
4706 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4707 | max *= fact; | |
4708 | # endif | |
4709 | ||
4710 | /* Compare with current best results. */ | |
4711 | if (max < best_chlen) | |
4712 | { | |
4713 | best_chlen = max; | |
4714 | best_size = i; | |
4715 | } | |
4716 | } | |
4717 | ||
4718 | free (counts); | |
4719 | } | |
4720 | else | |
4721 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4722 | { | |
4723 | /* This is the fallback solution if no 64bit type is available or if we | |
4724 | are not supposed to spend much time on optimizations. We select the | |
4725 | bucket count using a fixed set of numbers. */ | |
4726 | for (i = 0; elf_buckets[i] != 0; i++) | |
4727 | { | |
4728 | best_size = elf_buckets[i]; | |
4729 | if (dynsymcount < elf_buckets[i + 1]) | |
4730 | break; | |
4731 | } | |
4732 | } | |
4733 | ||
4734 | /* Free the arrays we needed. */ | |
4735 | free (hashcodes); | |
4736 | ||
4737 | return best_size; | |
4738 | } | |
4739 | ||
4740 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4741 | called by the ELF linker emulation before_allocation routine. We | |
4742 | must set the sizes of the sections before the linker sets the | |
4743 | addresses of the various sections. */ | |
4744 | ||
4745 | bfd_boolean | |
4746 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4747 | const char *soname, | |
4748 | const char *rpath, | |
4749 | const char *filter_shlib, | |
4750 | const char * const *auxiliary_filters, | |
4751 | struct bfd_link_info *info, | |
4752 | asection **sinterpptr, | |
4753 | struct bfd_elf_version_tree *verdefs) | |
4754 | { | |
4755 | bfd_size_type soname_indx; | |
4756 | bfd *dynobj; | |
4757 | const struct elf_backend_data *bed; | |
4758 | struct elf_assign_sym_version_info asvinfo; | |
4759 | ||
4760 | *sinterpptr = NULL; | |
4761 | ||
4762 | soname_indx = (bfd_size_type) -1; | |
4763 | ||
4764 | if (!is_elf_hash_table (info->hash)) | |
4765 | return TRUE; | |
4766 | ||
8c37241b | 4767 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
4768 | if (info->execstack) |
4769 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4770 | else if (info->noexecstack) | |
4771 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4772 | else | |
4773 | { | |
4774 | bfd *inputobj; | |
4775 | asection *notesec = NULL; | |
4776 | int exec = 0; | |
4777 | ||
4778 | for (inputobj = info->input_bfds; | |
4779 | inputobj; | |
4780 | inputobj = inputobj->link_next) | |
4781 | { | |
4782 | asection *s; | |
4783 | ||
4784 | if (inputobj->flags & DYNAMIC) | |
4785 | continue; | |
4786 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4787 | if (s) | |
4788 | { | |
4789 | if (s->flags & SEC_CODE) | |
4790 | exec = PF_X; | |
4791 | notesec = s; | |
4792 | } | |
4793 | else | |
4794 | exec = PF_X; | |
4795 | } | |
4796 | if (notesec) | |
4797 | { | |
4798 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4799 | if (exec && info->relocatable | |
4800 | && notesec->output_section != bfd_abs_section_ptr) | |
4801 | notesec->output_section->flags |= SEC_CODE; | |
4802 | } | |
4803 | } | |
4804 | ||
4805 | /* Any syms created from now on start with -1 in | |
4806 | got.refcount/offset and plt.refcount/offset. */ | |
4807 | elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset; | |
4808 | ||
4809 | /* The backend may have to create some sections regardless of whether | |
4810 | we're dynamic or not. */ | |
4811 | bed = get_elf_backend_data (output_bfd); | |
4812 | if (bed->elf_backend_always_size_sections | |
4813 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
4814 | return FALSE; | |
4815 | ||
4816 | dynobj = elf_hash_table (info)->dynobj; | |
4817 | ||
4818 | /* If there were no dynamic objects in the link, there is nothing to | |
4819 | do here. */ | |
4820 | if (dynobj == NULL) | |
4821 | return TRUE; | |
4822 | ||
4823 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
4824 | return FALSE; | |
4825 | ||
4826 | if (elf_hash_table (info)->dynamic_sections_created) | |
4827 | { | |
4828 | struct elf_info_failed eif; | |
4829 | struct elf_link_hash_entry *h; | |
4830 | asection *dynstr; | |
4831 | struct bfd_elf_version_tree *t; | |
4832 | struct bfd_elf_version_expr *d; | |
4833 | bfd_boolean all_defined; | |
4834 | ||
4835 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
4836 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
4837 | ||
4838 | if (soname != NULL) | |
4839 | { | |
4840 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4841 | soname, TRUE); | |
4842 | if (soname_indx == (bfd_size_type) -1 | |
4843 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
4844 | return FALSE; | |
4845 | } | |
4846 | ||
4847 | if (info->symbolic) | |
4848 | { | |
4849 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
4850 | return FALSE; | |
4851 | info->flags |= DF_SYMBOLIC; | |
4852 | } | |
4853 | ||
4854 | if (rpath != NULL) | |
4855 | { | |
4856 | bfd_size_type indx; | |
4857 | ||
4858 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
4859 | TRUE); | |
4860 | if (indx == (bfd_size_type) -1 | |
4861 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
4862 | return FALSE; | |
4863 | ||
4864 | if (info->new_dtags) | |
4865 | { | |
4866 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
4867 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
4868 | return FALSE; | |
4869 | } | |
4870 | } | |
4871 | ||
4872 | if (filter_shlib != NULL) | |
4873 | { | |
4874 | bfd_size_type indx; | |
4875 | ||
4876 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4877 | filter_shlib, TRUE); | |
4878 | if (indx == (bfd_size_type) -1 | |
4879 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
4880 | return FALSE; | |
4881 | } | |
4882 | ||
4883 | if (auxiliary_filters != NULL) | |
4884 | { | |
4885 | const char * const *p; | |
4886 | ||
4887 | for (p = auxiliary_filters; *p != NULL; p++) | |
4888 | { | |
4889 | bfd_size_type indx; | |
4890 | ||
4891 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4892 | *p, TRUE); | |
4893 | if (indx == (bfd_size_type) -1 | |
4894 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
4895 | return FALSE; | |
4896 | } | |
4897 | } | |
4898 | ||
4899 | eif.info = info; | |
4900 | eif.verdefs = verdefs; | |
4901 | eif.failed = FALSE; | |
4902 | ||
4903 | /* If we are supposed to export all symbols into the dynamic symbol | |
4904 | table (this is not the normal case), then do so. */ | |
4905 | if (info->export_dynamic) | |
4906 | { | |
4907 | elf_link_hash_traverse (elf_hash_table (info), | |
4908 | _bfd_elf_export_symbol, | |
4909 | &eif); | |
4910 | if (eif.failed) | |
4911 | return FALSE; | |
4912 | } | |
4913 | ||
4914 | /* Make all global versions with definition. */ | |
4915 | for (t = verdefs; t != NULL; t = t->next) | |
4916 | for (d = t->globals.list; d != NULL; d = d->next) | |
4917 | if (!d->symver && d->symbol) | |
4918 | { | |
4919 | const char *verstr, *name; | |
4920 | size_t namelen, verlen, newlen; | |
4921 | char *newname, *p; | |
4922 | struct elf_link_hash_entry *newh; | |
4923 | ||
4924 | name = d->symbol; | |
4925 | namelen = strlen (name); | |
4926 | verstr = t->name; | |
4927 | verlen = strlen (verstr); | |
4928 | newlen = namelen + verlen + 3; | |
4929 | ||
4930 | newname = bfd_malloc (newlen); | |
4931 | if (newname == NULL) | |
4932 | return FALSE; | |
4933 | memcpy (newname, name, namelen); | |
4934 | ||
4935 | /* Check the hidden versioned definition. */ | |
4936 | p = newname + namelen; | |
4937 | *p++ = ELF_VER_CHR; | |
4938 | memcpy (p, verstr, verlen + 1); | |
4939 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4940 | newname, FALSE, FALSE, | |
4941 | FALSE); | |
4942 | if (newh == NULL | |
4943 | || (newh->root.type != bfd_link_hash_defined | |
4944 | && newh->root.type != bfd_link_hash_defweak)) | |
4945 | { | |
4946 | /* Check the default versioned definition. */ | |
4947 | *p++ = ELF_VER_CHR; | |
4948 | memcpy (p, verstr, verlen + 1); | |
4949 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4950 | newname, FALSE, FALSE, | |
4951 | FALSE); | |
4952 | } | |
4953 | free (newname); | |
4954 | ||
4955 | /* Mark this version if there is a definition and it is | |
4956 | not defined in a shared object. */ | |
4957 | if (newh != NULL | |
f5385ebf | 4958 | && !newh->def_dynamic |
5a580b3a AM |
4959 | && (newh->root.type == bfd_link_hash_defined |
4960 | || newh->root.type == bfd_link_hash_defweak)) | |
4961 | d->symver = 1; | |
4962 | } | |
4963 | ||
4964 | /* Attach all the symbols to their version information. */ | |
4965 | asvinfo.output_bfd = output_bfd; | |
4966 | asvinfo.info = info; | |
4967 | asvinfo.verdefs = verdefs; | |
4968 | asvinfo.failed = FALSE; | |
4969 | ||
4970 | elf_link_hash_traverse (elf_hash_table (info), | |
4971 | _bfd_elf_link_assign_sym_version, | |
4972 | &asvinfo); | |
4973 | if (asvinfo.failed) | |
4974 | return FALSE; | |
4975 | ||
4976 | if (!info->allow_undefined_version) | |
4977 | { | |
4978 | /* Check if all global versions have a definition. */ | |
4979 | all_defined = TRUE; | |
4980 | for (t = verdefs; t != NULL; t = t->next) | |
4981 | for (d = t->globals.list; d != NULL; d = d->next) | |
4982 | if (!d->symver && !d->script) | |
4983 | { | |
4984 | (*_bfd_error_handler) | |
4985 | (_("%s: undefined version: %s"), | |
4986 | d->pattern, t->name); | |
4987 | all_defined = FALSE; | |
4988 | } | |
4989 | ||
4990 | if (!all_defined) | |
4991 | { | |
4992 | bfd_set_error (bfd_error_bad_value); | |
4993 | return FALSE; | |
4994 | } | |
4995 | } | |
4996 | ||
4997 | /* Find all symbols which were defined in a dynamic object and make | |
4998 | the backend pick a reasonable value for them. */ | |
4999 | elf_link_hash_traverse (elf_hash_table (info), | |
5000 | _bfd_elf_adjust_dynamic_symbol, | |
5001 | &eif); | |
5002 | if (eif.failed) | |
5003 | return FALSE; | |
5004 | ||
5005 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 5006 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
5007 | now so that we know the final size of the .dynamic section. */ |
5008 | ||
5009 | /* If there are initialization and/or finalization functions to | |
5010 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
5011 | h = (info->init_function | |
5012 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5013 | info->init_function, FALSE, | |
5014 | FALSE, FALSE) | |
5015 | : NULL); | |
5016 | if (h != NULL | |
f5385ebf AM |
5017 | && (h->ref_regular |
5018 | || h->def_regular)) | |
5a580b3a AM |
5019 | { |
5020 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
5021 | return FALSE; | |
5022 | } | |
5023 | h = (info->fini_function | |
5024 | ? elf_link_hash_lookup (elf_hash_table (info), | |
5025 | info->fini_function, FALSE, | |
5026 | FALSE, FALSE) | |
5027 | : NULL); | |
5028 | if (h != NULL | |
f5385ebf AM |
5029 | && (h->ref_regular |
5030 | || h->def_regular)) | |
5a580b3a AM |
5031 | { |
5032 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
5033 | return FALSE; | |
5034 | } | |
5035 | ||
5036 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) | |
5037 | { | |
5038 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
5039 | if (! info->executable) | |
5040 | { | |
5041 | bfd *sub; | |
5042 | asection *o; | |
5043 | ||
5044 | for (sub = info->input_bfds; sub != NULL; | |
5045 | sub = sub->link_next) | |
5046 | for (o = sub->sections; o != NULL; o = o->next) | |
5047 | if (elf_section_data (o)->this_hdr.sh_type | |
5048 | == SHT_PREINIT_ARRAY) | |
5049 | { | |
5050 | (*_bfd_error_handler) | |
d003868e AM |
5051 | (_("%B: .preinit_array section is not allowed in DSO"), |
5052 | sub); | |
5a580b3a AM |
5053 | break; |
5054 | } | |
5055 | ||
5056 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5057 | return FALSE; | |
5058 | } | |
5059 | ||
5060 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5061 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5062 | return FALSE; | |
5063 | } | |
5064 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
5065 | { | |
5066 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5067 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5068 | return FALSE; | |
5069 | } | |
5070 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
5071 | { | |
5072 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5073 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5074 | return FALSE; | |
5075 | } | |
5076 | ||
5077 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5078 | /* If .dynstr is excluded from the link, we don't want any of | |
5079 | these tags. Strictly, we should be checking each section | |
5080 | individually; This quick check covers for the case where | |
5081 | someone does a /DISCARD/ : { *(*) }. */ | |
5082 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5083 | { | |
5084 | bfd_size_type strsize; | |
5085 | ||
5086 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5087 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
5088 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
5089 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5090 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5091 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5092 | bed->s->sizeof_sym)) | |
5093 | return FALSE; | |
5094 | } | |
5095 | } | |
5096 | ||
5097 | /* The backend must work out the sizes of all the other dynamic | |
5098 | sections. */ | |
5099 | if (bed->elf_backend_size_dynamic_sections | |
5100 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5101 | return FALSE; | |
5102 | ||
5103 | if (elf_hash_table (info)->dynamic_sections_created) | |
5104 | { | |
5105 | bfd_size_type dynsymcount; | |
5106 | asection *s; | |
5107 | size_t bucketcount = 0; | |
5108 | size_t hash_entry_size; | |
5109 | unsigned int dtagcount; | |
5110 | ||
5111 | /* Set up the version definition section. */ | |
5112 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5113 | BFD_ASSERT (s != NULL); | |
5114 | ||
5115 | /* We may have created additional version definitions if we are | |
5116 | just linking a regular application. */ | |
5117 | verdefs = asvinfo.verdefs; | |
5118 | ||
5119 | /* Skip anonymous version tag. */ | |
5120 | if (verdefs != NULL && verdefs->vernum == 0) | |
5121 | verdefs = verdefs->next; | |
5122 | ||
3e3b46e5 | 5123 | if (verdefs == NULL && !info->create_default_symver) |
5a580b3a AM |
5124 | _bfd_strip_section_from_output (info, s); |
5125 | else | |
5126 | { | |
5127 | unsigned int cdefs; | |
5128 | bfd_size_type size; | |
5129 | struct bfd_elf_version_tree *t; | |
5130 | bfd_byte *p; | |
5131 | Elf_Internal_Verdef def; | |
5132 | Elf_Internal_Verdaux defaux; | |
3e3b46e5 PB |
5133 | struct bfd_link_hash_entry *bh; |
5134 | struct elf_link_hash_entry *h; | |
5135 | const char *name; | |
5a580b3a AM |
5136 | |
5137 | cdefs = 0; | |
5138 | size = 0; | |
5139 | ||
5140 | /* Make space for the base version. */ | |
5141 | size += sizeof (Elf_External_Verdef); | |
5142 | size += sizeof (Elf_External_Verdaux); | |
5143 | ++cdefs; | |
5144 | ||
3e3b46e5 PB |
5145 | /* Make space for the default version. */ |
5146 | if (info->create_default_symver) | |
5147 | { | |
5148 | size += sizeof (Elf_External_Verdef); | |
5149 | ++cdefs; | |
5150 | } | |
5151 | ||
5a580b3a AM |
5152 | for (t = verdefs; t != NULL; t = t->next) |
5153 | { | |
5154 | struct bfd_elf_version_deps *n; | |
5155 | ||
5156 | size += sizeof (Elf_External_Verdef); | |
5157 | size += sizeof (Elf_External_Verdaux); | |
5158 | ++cdefs; | |
5159 | ||
5160 | for (n = t->deps; n != NULL; n = n->next) | |
5161 | size += sizeof (Elf_External_Verdaux); | |
5162 | } | |
5163 | ||
eea6121a AM |
5164 | s->size = size; |
5165 | s->contents = bfd_alloc (output_bfd, s->size); | |
5166 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5167 | return FALSE; |
5168 | ||
5169 | /* Fill in the version definition section. */ | |
5170 | ||
5171 | p = s->contents; | |
5172 | ||
5173 | def.vd_version = VER_DEF_CURRENT; | |
5174 | def.vd_flags = VER_FLG_BASE; | |
5175 | def.vd_ndx = 1; | |
5176 | def.vd_cnt = 1; | |
3e3b46e5 PB |
5177 | if (info->create_default_symver) |
5178 | { | |
5179 | def.vd_aux = 2 * sizeof (Elf_External_Verdef); | |
5180 | def.vd_next = sizeof (Elf_External_Verdef); | |
5181 | } | |
5182 | else | |
5183 | { | |
5184 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5185 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5186 | + sizeof (Elf_External_Verdaux)); | |
5187 | } | |
5a580b3a AM |
5188 | |
5189 | if (soname_indx != (bfd_size_type) -1) | |
5190 | { | |
5191 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5192 | soname_indx); | |
5193 | def.vd_hash = bfd_elf_hash (soname); | |
5194 | defaux.vda_name = soname_indx; | |
3e3b46e5 | 5195 | name = soname; |
5a580b3a AM |
5196 | } |
5197 | else | |
5198 | { | |
5a580b3a AM |
5199 | bfd_size_type indx; |
5200 | ||
5201 | name = basename (output_bfd->filename); | |
5202 | def.vd_hash = bfd_elf_hash (name); | |
5203 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5204 | name, FALSE); | |
5205 | if (indx == (bfd_size_type) -1) | |
5206 | return FALSE; | |
5207 | defaux.vda_name = indx; | |
5208 | } | |
5209 | defaux.vda_next = 0; | |
5210 | ||
5211 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5212 | (Elf_External_Verdef *) p); | |
5213 | p += sizeof (Elf_External_Verdef); | |
3e3b46e5 PB |
5214 | if (info->create_default_symver) |
5215 | { | |
5216 | /* Add a symbol representing this version. */ | |
5217 | bh = NULL; | |
5218 | if (! (_bfd_generic_link_add_one_symbol | |
5219 | (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5220 | 0, NULL, FALSE, | |
5221 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5222 | return FALSE; | |
5223 | h = (struct elf_link_hash_entry *) bh; | |
5224 | h->non_elf = 0; | |
5225 | h->def_regular = 1; | |
5226 | h->type = STT_OBJECT; | |
5227 | h->verinfo.vertree = NULL; | |
5228 | ||
5229 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
5230 | return FALSE; | |
5231 | ||
5232 | /* Create a duplicate of the base version with the same | |
5233 | aux block, but different flags. */ | |
5234 | def.vd_flags = 0; | |
5235 | def.vd_ndx = 2; | |
5236 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5237 | if (verdefs) | |
5238 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5239 | + sizeof (Elf_External_Verdaux)); | |
5240 | else | |
5241 | def.vd_next = 0; | |
5242 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5243 | (Elf_External_Verdef *) p); | |
5244 | p += sizeof (Elf_External_Verdef); | |
5245 | } | |
5a580b3a AM |
5246 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, |
5247 | (Elf_External_Verdaux *) p); | |
5248 | p += sizeof (Elf_External_Verdaux); | |
5249 | ||
5250 | for (t = verdefs; t != NULL; t = t->next) | |
5251 | { | |
5252 | unsigned int cdeps; | |
5253 | struct bfd_elf_version_deps *n; | |
5a580b3a AM |
5254 | |
5255 | cdeps = 0; | |
5256 | for (n = t->deps; n != NULL; n = n->next) | |
5257 | ++cdeps; | |
5258 | ||
5259 | /* Add a symbol representing this version. */ | |
5260 | bh = NULL; | |
5261 | if (! (_bfd_generic_link_add_one_symbol | |
5262 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5263 | 0, NULL, FALSE, | |
5264 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5265 | return FALSE; | |
5266 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5267 | h->non_elf = 0; |
5268 | h->def_regular = 1; | |
5a580b3a AM |
5269 | h->type = STT_OBJECT; |
5270 | h->verinfo.vertree = t; | |
5271 | ||
c152c796 | 5272 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5273 | return FALSE; |
5274 | ||
5275 | def.vd_version = VER_DEF_CURRENT; | |
5276 | def.vd_flags = 0; | |
5277 | if (t->globals.list == NULL | |
5278 | && t->locals.list == NULL | |
5279 | && ! t->used) | |
5280 | def.vd_flags |= VER_FLG_WEAK; | |
3e3b46e5 | 5281 | def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); |
5a580b3a AM |
5282 | def.vd_cnt = cdeps + 1; |
5283 | def.vd_hash = bfd_elf_hash (t->name); | |
5284 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5285 | def.vd_next = 0; | |
5286 | if (t->next != NULL) | |
5287 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5288 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5289 | ||
5290 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5291 | (Elf_External_Verdef *) p); | |
5292 | p += sizeof (Elf_External_Verdef); | |
5293 | ||
5294 | defaux.vda_name = h->dynstr_index; | |
5295 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5296 | h->dynstr_index); | |
5297 | defaux.vda_next = 0; | |
5298 | if (t->deps != NULL) | |
5299 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5300 | t->name_indx = defaux.vda_name; | |
5301 | ||
5302 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5303 | (Elf_External_Verdaux *) p); | |
5304 | p += sizeof (Elf_External_Verdaux); | |
5305 | ||
5306 | for (n = t->deps; n != NULL; n = n->next) | |
5307 | { | |
5308 | if (n->version_needed == NULL) | |
5309 | { | |
5310 | /* This can happen if there was an error in the | |
5311 | version script. */ | |
5312 | defaux.vda_name = 0; | |
5313 | } | |
5314 | else | |
5315 | { | |
5316 | defaux.vda_name = n->version_needed->name_indx; | |
5317 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5318 | defaux.vda_name); | |
5319 | } | |
5320 | if (n->next == NULL) | |
5321 | defaux.vda_next = 0; | |
5322 | else | |
5323 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5324 | ||
5325 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5326 | (Elf_External_Verdaux *) p); | |
5327 | p += sizeof (Elf_External_Verdaux); | |
5328 | } | |
5329 | } | |
5330 | ||
5331 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5332 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5333 | return FALSE; | |
5334 | ||
5335 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5336 | } | |
5337 | ||
5338 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5339 | { | |
5340 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5341 | return FALSE; | |
5342 | } | |
5343 | else if (info->flags & DF_BIND_NOW) | |
5344 | { | |
5345 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5346 | return FALSE; | |
5347 | } | |
5348 | ||
5349 | if (info->flags_1) | |
5350 | { | |
5351 | if (info->executable) | |
5352 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5353 | | DF_1_NODELETE | |
5354 | | DF_1_NOOPEN); | |
5355 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5356 | return FALSE; | |
5357 | } | |
5358 | ||
5359 | /* Work out the size of the version reference section. */ | |
5360 | ||
5361 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5362 | BFD_ASSERT (s != NULL); | |
5363 | { | |
5364 | struct elf_find_verdep_info sinfo; | |
5365 | ||
5366 | sinfo.output_bfd = output_bfd; | |
5367 | sinfo.info = info; | |
5368 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5369 | if (sinfo.vers == 0) | |
5370 | sinfo.vers = 1; | |
5371 | sinfo.failed = FALSE; | |
5372 | ||
5373 | elf_link_hash_traverse (elf_hash_table (info), | |
5374 | _bfd_elf_link_find_version_dependencies, | |
5375 | &sinfo); | |
5376 | ||
5377 | if (elf_tdata (output_bfd)->verref == NULL) | |
5378 | _bfd_strip_section_from_output (info, s); | |
5379 | else | |
5380 | { | |
5381 | Elf_Internal_Verneed *t; | |
5382 | unsigned int size; | |
5383 | unsigned int crefs; | |
5384 | bfd_byte *p; | |
5385 | ||
5386 | /* Build the version definition section. */ | |
5387 | size = 0; | |
5388 | crefs = 0; | |
5389 | for (t = elf_tdata (output_bfd)->verref; | |
5390 | t != NULL; | |
5391 | t = t->vn_nextref) | |
5392 | { | |
5393 | Elf_Internal_Vernaux *a; | |
5394 | ||
5395 | size += sizeof (Elf_External_Verneed); | |
5396 | ++crefs; | |
5397 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5398 | size += sizeof (Elf_External_Vernaux); | |
5399 | } | |
5400 | ||
eea6121a AM |
5401 | s->size = size; |
5402 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5403 | if (s->contents == NULL) |
5404 | return FALSE; | |
5405 | ||
5406 | p = s->contents; | |
5407 | for (t = elf_tdata (output_bfd)->verref; | |
5408 | t != NULL; | |
5409 | t = t->vn_nextref) | |
5410 | { | |
5411 | unsigned int caux; | |
5412 | Elf_Internal_Vernaux *a; | |
5413 | bfd_size_type indx; | |
5414 | ||
5415 | caux = 0; | |
5416 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5417 | ++caux; | |
5418 | ||
5419 | t->vn_version = VER_NEED_CURRENT; | |
5420 | t->vn_cnt = caux; | |
5421 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5422 | elf_dt_name (t->vn_bfd) != NULL | |
5423 | ? elf_dt_name (t->vn_bfd) | |
5424 | : basename (t->vn_bfd->filename), | |
5425 | FALSE); | |
5426 | if (indx == (bfd_size_type) -1) | |
5427 | return FALSE; | |
5428 | t->vn_file = indx; | |
5429 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5430 | if (t->vn_nextref == NULL) | |
5431 | t->vn_next = 0; | |
5432 | else | |
5433 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5434 | + caux * sizeof (Elf_External_Vernaux)); | |
5435 | ||
5436 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5437 | (Elf_External_Verneed *) p); | |
5438 | p += sizeof (Elf_External_Verneed); | |
5439 | ||
5440 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5441 | { | |
5442 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5443 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5444 | a->vna_nodename, FALSE); | |
5445 | if (indx == (bfd_size_type) -1) | |
5446 | return FALSE; | |
5447 | a->vna_name = indx; | |
5448 | if (a->vna_nextptr == NULL) | |
5449 | a->vna_next = 0; | |
5450 | else | |
5451 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5452 | ||
5453 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5454 | (Elf_External_Vernaux *) p); | |
5455 | p += sizeof (Elf_External_Vernaux); | |
5456 | } | |
5457 | } | |
5458 | ||
5459 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5460 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5461 | return FALSE; | |
5462 | ||
5463 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5464 | } | |
5465 | } | |
5466 | ||
5467 | /* Assign dynsym indicies. In a shared library we generate a | |
5468 | section symbol for each output section, which come first. | |
5469 | Next come all of the back-end allocated local dynamic syms, | |
5470 | followed by the rest of the global symbols. */ | |
5471 | ||
5472 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5473 | ||
5474 | /* Work out the size of the symbol version section. */ | |
5475 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5476 | BFD_ASSERT (s != NULL); | |
5477 | if (dynsymcount == 0 | |
3e3b46e5 PB |
5478 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL |
5479 | && !info->create_default_symver)) | |
5a580b3a AM |
5480 | { |
5481 | _bfd_strip_section_from_output (info, s); | |
5482 | /* The DYNSYMCOUNT might have changed if we were going to | |
5483 | output a dynamic symbol table entry for S. */ | |
5484 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5485 | } | |
5486 | else | |
5487 | { | |
eea6121a AM |
5488 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
5489 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5490 | if (s->contents == NULL) |
5491 | return FALSE; | |
5492 | ||
5493 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5494 | return FALSE; | |
5495 | } | |
5496 | ||
5497 | /* Set the size of the .dynsym and .hash sections. We counted | |
5498 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5499 | We will build the contents of .dynsym and .hash when we build | |
5500 | the final symbol table, because until then we do not know the | |
5501 | correct value to give the symbols. We built the .dynstr | |
5502 | section as we went along in elf_link_add_object_symbols. */ | |
5503 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5504 | BFD_ASSERT (s != NULL); | |
eea6121a AM |
5505 | s->size = dynsymcount * bed->s->sizeof_sym; |
5506 | s->contents = bfd_alloc (output_bfd, s->size); | |
5507 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5508 | return FALSE; |
5509 | ||
5510 | if (dynsymcount != 0) | |
5511 | { | |
5512 | Elf_Internal_Sym isym; | |
5513 | ||
5514 | /* The first entry in .dynsym is a dummy symbol. */ | |
5515 | isym.st_value = 0; | |
5516 | isym.st_size = 0; | |
5517 | isym.st_name = 0; | |
5518 | isym.st_info = 0; | |
5519 | isym.st_other = 0; | |
5520 | isym.st_shndx = 0; | |
5521 | bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0); | |
5522 | } | |
5523 | ||
5524 | /* Compute the size of the hashing table. As a side effect this | |
5525 | computes the hash values for all the names we export. */ | |
5526 | bucketcount = compute_bucket_count (info); | |
5527 | ||
5528 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5529 | BFD_ASSERT (s != NULL); | |
5530 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
eea6121a AM |
5531 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
5532 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5533 | if (s->contents == NULL) |
5534 | return FALSE; | |
5535 | ||
5536 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5537 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5538 | s->contents + hash_entry_size); | |
5539 | ||
5540 | elf_hash_table (info)->bucketcount = bucketcount; | |
5541 | ||
5542 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5543 | BFD_ASSERT (s != NULL); | |
5544 | ||
4ad4eba5 | 5545 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 5546 | |
eea6121a | 5547 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
5548 | |
5549 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5550 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5551 | return FALSE; | |
5552 | } | |
5553 | ||
5554 | return TRUE; | |
5555 | } | |
c152c796 AM |
5556 | |
5557 | /* Final phase of ELF linker. */ | |
5558 | ||
5559 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5560 | ||
5561 | struct elf_final_link_info | |
5562 | { | |
5563 | /* General link information. */ | |
5564 | struct bfd_link_info *info; | |
5565 | /* Output BFD. */ | |
5566 | bfd *output_bfd; | |
5567 | /* Symbol string table. */ | |
5568 | struct bfd_strtab_hash *symstrtab; | |
5569 | /* .dynsym section. */ | |
5570 | asection *dynsym_sec; | |
5571 | /* .hash section. */ | |
5572 | asection *hash_sec; | |
5573 | /* symbol version section (.gnu.version). */ | |
5574 | asection *symver_sec; | |
5575 | /* Buffer large enough to hold contents of any section. */ | |
5576 | bfd_byte *contents; | |
5577 | /* Buffer large enough to hold external relocs of any section. */ | |
5578 | void *external_relocs; | |
5579 | /* Buffer large enough to hold internal relocs of any section. */ | |
5580 | Elf_Internal_Rela *internal_relocs; | |
5581 | /* Buffer large enough to hold external local symbols of any input | |
5582 | BFD. */ | |
5583 | bfd_byte *external_syms; | |
5584 | /* And a buffer for symbol section indices. */ | |
5585 | Elf_External_Sym_Shndx *locsym_shndx; | |
5586 | /* Buffer large enough to hold internal local symbols of any input | |
5587 | BFD. */ | |
5588 | Elf_Internal_Sym *internal_syms; | |
5589 | /* Array large enough to hold a symbol index for each local symbol | |
5590 | of any input BFD. */ | |
5591 | long *indices; | |
5592 | /* Array large enough to hold a section pointer for each local | |
5593 | symbol of any input BFD. */ | |
5594 | asection **sections; | |
5595 | /* Buffer to hold swapped out symbols. */ | |
5596 | bfd_byte *symbuf; | |
5597 | /* And one for symbol section indices. */ | |
5598 | Elf_External_Sym_Shndx *symshndxbuf; | |
5599 | /* Number of swapped out symbols in buffer. */ | |
5600 | size_t symbuf_count; | |
5601 | /* Number of symbols which fit in symbuf. */ | |
5602 | size_t symbuf_size; | |
5603 | /* And same for symshndxbuf. */ | |
5604 | size_t shndxbuf_size; | |
5605 | }; | |
5606 | ||
5607 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5608 | ||
5609 | struct elf_outext_info | |
5610 | { | |
5611 | bfd_boolean failed; | |
5612 | bfd_boolean localsyms; | |
5613 | struct elf_final_link_info *finfo; | |
5614 | }; | |
5615 | ||
5616 | /* When performing a relocatable link, the input relocations are | |
5617 | preserved. But, if they reference global symbols, the indices | |
5618 | referenced must be updated. Update all the relocations in | |
5619 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5620 | ||
5621 | static void | |
5622 | elf_link_adjust_relocs (bfd *abfd, | |
5623 | Elf_Internal_Shdr *rel_hdr, | |
5624 | unsigned int count, | |
5625 | struct elf_link_hash_entry **rel_hash) | |
5626 | { | |
5627 | unsigned int i; | |
5628 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5629 | bfd_byte *erela; | |
5630 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5631 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5632 | bfd_vma r_type_mask; | |
5633 | int r_sym_shift; | |
5634 | ||
5635 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5636 | { | |
5637 | swap_in = bed->s->swap_reloc_in; | |
5638 | swap_out = bed->s->swap_reloc_out; | |
5639 | } | |
5640 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5641 | { | |
5642 | swap_in = bed->s->swap_reloca_in; | |
5643 | swap_out = bed->s->swap_reloca_out; | |
5644 | } | |
5645 | else | |
5646 | abort (); | |
5647 | ||
5648 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5649 | abort (); | |
5650 | ||
5651 | if (bed->s->arch_size == 32) | |
5652 | { | |
5653 | r_type_mask = 0xff; | |
5654 | r_sym_shift = 8; | |
5655 | } | |
5656 | else | |
5657 | { | |
5658 | r_type_mask = 0xffffffff; | |
5659 | r_sym_shift = 32; | |
5660 | } | |
5661 | ||
5662 | erela = rel_hdr->contents; | |
5663 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5664 | { | |
5665 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5666 | unsigned int j; | |
5667 | ||
5668 | if (*rel_hash == NULL) | |
5669 | continue; | |
5670 | ||
5671 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5672 | ||
5673 | (*swap_in) (abfd, erela, irela); | |
5674 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5675 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5676 | | (irela[j].r_info & r_type_mask)); | |
5677 | (*swap_out) (abfd, irela, erela); | |
5678 | } | |
5679 | } | |
5680 | ||
5681 | struct elf_link_sort_rela | |
5682 | { | |
5683 | union { | |
5684 | bfd_vma offset; | |
5685 | bfd_vma sym_mask; | |
5686 | } u; | |
5687 | enum elf_reloc_type_class type; | |
5688 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5689 | Elf_Internal_Rela rela[1]; | |
5690 | }; | |
5691 | ||
5692 | static int | |
5693 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5694 | { | |
5695 | const struct elf_link_sort_rela *a = A; | |
5696 | const struct elf_link_sort_rela *b = B; | |
5697 | int relativea, relativeb; | |
5698 | ||
5699 | relativea = a->type == reloc_class_relative; | |
5700 | relativeb = b->type == reloc_class_relative; | |
5701 | ||
5702 | if (relativea < relativeb) | |
5703 | return 1; | |
5704 | if (relativea > relativeb) | |
5705 | return -1; | |
5706 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5707 | return -1; | |
5708 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5709 | return 1; | |
5710 | if (a->rela->r_offset < b->rela->r_offset) | |
5711 | return -1; | |
5712 | if (a->rela->r_offset > b->rela->r_offset) | |
5713 | return 1; | |
5714 | return 0; | |
5715 | } | |
5716 | ||
5717 | static int | |
5718 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5719 | { | |
5720 | const struct elf_link_sort_rela *a = A; | |
5721 | const struct elf_link_sort_rela *b = B; | |
5722 | int copya, copyb; | |
5723 | ||
5724 | if (a->u.offset < b->u.offset) | |
5725 | return -1; | |
5726 | if (a->u.offset > b->u.offset) | |
5727 | return 1; | |
5728 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5729 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5730 | if (copya < copyb) | |
5731 | return -1; | |
5732 | if (copya > copyb) | |
5733 | return 1; | |
5734 | if (a->rela->r_offset < b->rela->r_offset) | |
5735 | return -1; | |
5736 | if (a->rela->r_offset > b->rela->r_offset) | |
5737 | return 1; | |
5738 | return 0; | |
5739 | } | |
5740 | ||
5741 | static size_t | |
5742 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5743 | { | |
5744 | asection *reldyn; | |
5745 | bfd_size_type count, size; | |
5746 | size_t i, ret, sort_elt, ext_size; | |
5747 | bfd_byte *sort, *s_non_relative, *p; | |
5748 | struct elf_link_sort_rela *sq; | |
5749 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5750 | int i2e = bed->s->int_rels_per_ext_rel; | |
5751 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5752 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5753 | struct bfd_link_order *lo; | |
5754 | bfd_vma r_sym_mask; | |
5755 | ||
5756 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 5757 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5758 | { |
5759 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 5760 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5761 | return 0; |
5762 | ext_size = bed->s->sizeof_rel; | |
5763 | swap_in = bed->s->swap_reloc_in; | |
5764 | swap_out = bed->s->swap_reloc_out; | |
5765 | } | |
5766 | else | |
5767 | { | |
5768 | ext_size = bed->s->sizeof_rela; | |
5769 | swap_in = bed->s->swap_reloca_in; | |
5770 | swap_out = bed->s->swap_reloca_out; | |
5771 | } | |
eea6121a | 5772 | count = reldyn->size / ext_size; |
c152c796 AM |
5773 | |
5774 | size = 0; | |
5775 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5776 | if (lo->type == bfd_indirect_link_order) | |
5777 | { | |
5778 | asection *o = lo->u.indirect.section; | |
eea6121a | 5779 | size += o->size; |
c152c796 AM |
5780 | } |
5781 | ||
eea6121a | 5782 | if (size != reldyn->size) |
c152c796 AM |
5783 | return 0; |
5784 | ||
5785 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
5786 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
5787 | sort = bfd_zmalloc (sort_elt * count); | |
5788 | if (sort == NULL) | |
5789 | { | |
5790 | (*info->callbacks->warning) | |
5791 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
5792 | return 0; | |
5793 | } | |
5794 | ||
5795 | if (bed->s->arch_size == 32) | |
5796 | r_sym_mask = ~(bfd_vma) 0xff; | |
5797 | else | |
5798 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
5799 | ||
5800 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5801 | if (lo->type == bfd_indirect_link_order) | |
5802 | { | |
5803 | bfd_byte *erel, *erelend; | |
5804 | asection *o = lo->u.indirect.section; | |
5805 | ||
1da212d6 AM |
5806 | if (o->contents == NULL && o->size != 0) |
5807 | { | |
5808 | /* This is a reloc section that is being handled as a normal | |
5809 | section. See bfd_section_from_shdr. We can't combine | |
5810 | relocs in this case. */ | |
5811 | free (sort); | |
5812 | return 0; | |
5813 | } | |
c152c796 | 5814 | erel = o->contents; |
eea6121a | 5815 | erelend = o->contents + o->size; |
c152c796 AM |
5816 | p = sort + o->output_offset / ext_size * sort_elt; |
5817 | while (erel < erelend) | |
5818 | { | |
5819 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5820 | (*swap_in) (abfd, erel, s->rela); | |
5821 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
5822 | s->u.sym_mask = r_sym_mask; | |
5823 | p += sort_elt; | |
5824 | erel += ext_size; | |
5825 | } | |
5826 | } | |
5827 | ||
5828 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
5829 | ||
5830 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
5831 | { | |
5832 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5833 | if (s->type != reloc_class_relative) | |
5834 | break; | |
5835 | } | |
5836 | ret = i; | |
5837 | s_non_relative = p; | |
5838 | ||
5839 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
5840 | for (; i < count; i++, p += sort_elt) | |
5841 | { | |
5842 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
5843 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
5844 | sq = sp; | |
5845 | sp->u.offset = sq->rela->r_offset; | |
5846 | } | |
5847 | ||
5848 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
5849 | ||
5850 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5851 | if (lo->type == bfd_indirect_link_order) | |
5852 | { | |
5853 | bfd_byte *erel, *erelend; | |
5854 | asection *o = lo->u.indirect.section; | |
5855 | ||
5856 | erel = o->contents; | |
eea6121a | 5857 | erelend = o->contents + o->size; |
c152c796 AM |
5858 | p = sort + o->output_offset / ext_size * sort_elt; |
5859 | while (erel < erelend) | |
5860 | { | |
5861 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5862 | (*swap_out) (abfd, s->rela, erel); | |
5863 | p += sort_elt; | |
5864 | erel += ext_size; | |
5865 | } | |
5866 | } | |
5867 | ||
5868 | free (sort); | |
5869 | *psec = reldyn; | |
5870 | return ret; | |
5871 | } | |
5872 | ||
5873 | /* Flush the output symbols to the file. */ | |
5874 | ||
5875 | static bfd_boolean | |
5876 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
5877 | const struct elf_backend_data *bed) | |
5878 | { | |
5879 | if (finfo->symbuf_count > 0) | |
5880 | { | |
5881 | Elf_Internal_Shdr *hdr; | |
5882 | file_ptr pos; | |
5883 | bfd_size_type amt; | |
5884 | ||
5885 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
5886 | pos = hdr->sh_offset + hdr->sh_size; | |
5887 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
5888 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
5889 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
5890 | return FALSE; | |
5891 | ||
5892 | hdr->sh_size += amt; | |
5893 | finfo->symbuf_count = 0; | |
5894 | } | |
5895 | ||
5896 | return TRUE; | |
5897 | } | |
5898 | ||
5899 | /* Add a symbol to the output symbol table. */ | |
5900 | ||
5901 | static bfd_boolean | |
5902 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
5903 | const char *name, | |
5904 | Elf_Internal_Sym *elfsym, | |
5905 | asection *input_sec, | |
5906 | struct elf_link_hash_entry *h) | |
5907 | { | |
5908 | bfd_byte *dest; | |
5909 | Elf_External_Sym_Shndx *destshndx; | |
5910 | bfd_boolean (*output_symbol_hook) | |
5911 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
5912 | struct elf_link_hash_entry *); | |
5913 | const struct elf_backend_data *bed; | |
5914 | ||
5915 | bed = get_elf_backend_data (finfo->output_bfd); | |
5916 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
5917 | if (output_symbol_hook != NULL) | |
5918 | { | |
5919 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
5920 | return FALSE; | |
5921 | } | |
5922 | ||
5923 | if (name == NULL || *name == '\0') | |
5924 | elfsym->st_name = 0; | |
5925 | else if (input_sec->flags & SEC_EXCLUDE) | |
5926 | elfsym->st_name = 0; | |
5927 | else | |
5928 | { | |
5929 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
5930 | name, TRUE, FALSE); | |
5931 | if (elfsym->st_name == (unsigned long) -1) | |
5932 | return FALSE; | |
5933 | } | |
5934 | ||
5935 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
5936 | { | |
5937 | if (! elf_link_flush_output_syms (finfo, bed)) | |
5938 | return FALSE; | |
5939 | } | |
5940 | ||
5941 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
5942 | destshndx = finfo->symshndxbuf; | |
5943 | if (destshndx != NULL) | |
5944 | { | |
5945 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
5946 | { | |
5947 | bfd_size_type amt; | |
5948 | ||
5949 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
5950 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
5951 | if (destshndx == NULL) | |
5952 | return FALSE; | |
5953 | memset ((char *) destshndx + amt, 0, amt); | |
5954 | finfo->shndxbuf_size *= 2; | |
5955 | } | |
5956 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
5957 | } | |
5958 | ||
5959 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
5960 | finfo->symbuf_count += 1; | |
5961 | bfd_get_symcount (finfo->output_bfd) += 1; | |
5962 | ||
5963 | return TRUE; | |
5964 | } | |
5965 | ||
5966 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
5967 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
5968 | versioned symbol that would normally require an explicit version. | |
5969 | We also handle the case that a DSO references a hidden symbol | |
5970 | which may be satisfied by a versioned symbol in another DSO. */ | |
5971 | ||
5972 | static bfd_boolean | |
5973 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
5974 | const struct elf_backend_data *bed, | |
5975 | struct elf_link_hash_entry *h) | |
5976 | { | |
5977 | bfd *abfd; | |
5978 | struct elf_link_loaded_list *loaded; | |
5979 | ||
5980 | if (!is_elf_hash_table (info->hash)) | |
5981 | return FALSE; | |
5982 | ||
5983 | switch (h->root.type) | |
5984 | { | |
5985 | default: | |
5986 | abfd = NULL; | |
5987 | break; | |
5988 | ||
5989 | case bfd_link_hash_undefined: | |
5990 | case bfd_link_hash_undefweak: | |
5991 | abfd = h->root.u.undef.abfd; | |
5992 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 5993 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
5994 | return FALSE; |
5995 | break; | |
5996 | ||
5997 | case bfd_link_hash_defined: | |
5998 | case bfd_link_hash_defweak: | |
5999 | abfd = h->root.u.def.section->owner; | |
6000 | break; | |
6001 | ||
6002 | case bfd_link_hash_common: | |
6003 | abfd = h->root.u.c.p->section->owner; | |
6004 | break; | |
6005 | } | |
6006 | BFD_ASSERT (abfd != NULL); | |
6007 | ||
6008 | for (loaded = elf_hash_table (info)->loaded; | |
6009 | loaded != NULL; | |
6010 | loaded = loaded->next) | |
6011 | { | |
6012 | bfd *input; | |
6013 | Elf_Internal_Shdr *hdr; | |
6014 | bfd_size_type symcount; | |
6015 | bfd_size_type extsymcount; | |
6016 | bfd_size_type extsymoff; | |
6017 | Elf_Internal_Shdr *versymhdr; | |
6018 | Elf_Internal_Sym *isym; | |
6019 | Elf_Internal_Sym *isymend; | |
6020 | Elf_Internal_Sym *isymbuf; | |
6021 | Elf_External_Versym *ever; | |
6022 | Elf_External_Versym *extversym; | |
6023 | ||
6024 | input = loaded->abfd; | |
6025 | ||
6026 | /* We check each DSO for a possible hidden versioned definition. */ | |
6027 | if (input == abfd | |
6028 | || (input->flags & DYNAMIC) == 0 | |
6029 | || elf_dynversym (input) == 0) | |
6030 | continue; | |
6031 | ||
6032 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
6033 | ||
6034 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
6035 | if (elf_bad_symtab (input)) | |
6036 | { | |
6037 | extsymcount = symcount; | |
6038 | extsymoff = 0; | |
6039 | } | |
6040 | else | |
6041 | { | |
6042 | extsymcount = symcount - hdr->sh_info; | |
6043 | extsymoff = hdr->sh_info; | |
6044 | } | |
6045 | ||
6046 | if (extsymcount == 0) | |
6047 | continue; | |
6048 | ||
6049 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
6050 | NULL, NULL, NULL); | |
6051 | if (isymbuf == NULL) | |
6052 | return FALSE; | |
6053 | ||
6054 | /* Read in any version definitions. */ | |
6055 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
6056 | extversym = bfd_malloc (versymhdr->sh_size); | |
6057 | if (extversym == NULL) | |
6058 | goto error_ret; | |
6059 | ||
6060 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
6061 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
6062 | != versymhdr->sh_size)) | |
6063 | { | |
6064 | free (extversym); | |
6065 | error_ret: | |
6066 | free (isymbuf); | |
6067 | return FALSE; | |
6068 | } | |
6069 | ||
6070 | ever = extversym + extsymoff; | |
6071 | isymend = isymbuf + extsymcount; | |
6072 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
6073 | { | |
6074 | const char *name; | |
6075 | Elf_Internal_Versym iver; | |
6076 | unsigned short version_index; | |
6077 | ||
6078 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
6079 | || isym->st_shndx == SHN_UNDEF) | |
6080 | continue; | |
6081 | ||
6082 | name = bfd_elf_string_from_elf_section (input, | |
6083 | hdr->sh_link, | |
6084 | isym->st_name); | |
6085 | if (strcmp (name, h->root.root.string) != 0) | |
6086 | continue; | |
6087 | ||
6088 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
6089 | ||
6090 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
6091 | { | |
6092 | /* If we have a non-hidden versioned sym, then it should | |
6093 | have provided a definition for the undefined sym. */ | |
6094 | abort (); | |
6095 | } | |
6096 | ||
6097 | version_index = iver.vs_vers & VERSYM_VERSION; | |
6098 | if (version_index == 1 || version_index == 2) | |
6099 | { | |
6100 | /* This is the base or first version. We can use it. */ | |
6101 | free (extversym); | |
6102 | free (isymbuf); | |
6103 | return TRUE; | |
6104 | } | |
6105 | } | |
6106 | ||
6107 | free (extversym); | |
6108 | free (isymbuf); | |
6109 | } | |
6110 | ||
6111 | return FALSE; | |
6112 | } | |
6113 | ||
6114 | /* Add an external symbol to the symbol table. This is called from | |
6115 | the hash table traversal routine. When generating a shared object, | |
6116 | we go through the symbol table twice. The first time we output | |
6117 | anything that might have been forced to local scope in a version | |
6118 | script. The second time we output the symbols that are still | |
6119 | global symbols. */ | |
6120 | ||
6121 | static bfd_boolean | |
6122 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6123 | { | |
6124 | struct elf_outext_info *eoinfo = data; | |
6125 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6126 | bfd_boolean strip; | |
6127 | Elf_Internal_Sym sym; | |
6128 | asection *input_sec; | |
6129 | const struct elf_backend_data *bed; | |
6130 | ||
6131 | if (h->root.type == bfd_link_hash_warning) | |
6132 | { | |
6133 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6134 | if (h->root.type == bfd_link_hash_new) | |
6135 | return TRUE; | |
6136 | } | |
6137 | ||
6138 | /* Decide whether to output this symbol in this pass. */ | |
6139 | if (eoinfo->localsyms) | |
6140 | { | |
f5385ebf | 6141 | if (!h->forced_local) |
c152c796 AM |
6142 | return TRUE; |
6143 | } | |
6144 | else | |
6145 | { | |
f5385ebf | 6146 | if (h->forced_local) |
c152c796 AM |
6147 | return TRUE; |
6148 | } | |
6149 | ||
6150 | bed = get_elf_backend_data (finfo->output_bfd); | |
6151 | ||
6152 | /* If we have an undefined symbol reference here then it must have | |
6153 | come from a shared library that is being linked in. (Undefined | |
6154 | references in regular files have already been handled). If we | |
6155 | are reporting errors for this situation then do so now. */ | |
6156 | if (h->root.type == bfd_link_hash_undefined | |
f5385ebf AM |
6157 | && h->ref_dynamic |
6158 | && !h->ref_regular | |
c152c796 AM |
6159 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) |
6160 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
6161 | { | |
6162 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6163 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6164 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6165 | { | |
6166 | eoinfo->failed = TRUE; | |
6167 | return FALSE; | |
6168 | } | |
6169 | } | |
6170 | ||
6171 | /* We should also warn if a forced local symbol is referenced from | |
6172 | shared libraries. */ | |
6173 | if (! finfo->info->relocatable | |
6174 | && (! finfo->info->shared) | |
f5385ebf AM |
6175 | && h->forced_local |
6176 | && h->ref_dynamic | |
6177 | && !h->dynamic_def | |
6178 | && !h->dynamic_weak | |
c152c796 AM |
6179 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6180 | { | |
6181 | (*_bfd_error_handler) | |
d003868e AM |
6182 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
6183 | finfo->output_bfd, h->root.u.def.section->owner, | |
c152c796 AM |
6184 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6185 | ? "internal" | |
6186 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6187 | ? "hidden" : "local", |
6188 | h->root.root.string); | |
c152c796 AM |
6189 | eoinfo->failed = TRUE; |
6190 | return FALSE; | |
6191 | } | |
6192 | ||
6193 | /* We don't want to output symbols that have never been mentioned by | |
6194 | a regular file, or that we have been told to strip. However, if | |
6195 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6196 | output it. */ | |
6197 | if (h->indx == -2) | |
6198 | strip = FALSE; | |
f5385ebf AM |
6199 | else if ((h->def_dynamic |
6200 | || h->ref_dynamic) | |
6201 | && !h->def_regular | |
6202 | && !h->ref_regular) | |
c152c796 AM |
6203 | strip = TRUE; |
6204 | else if (finfo->info->strip == strip_all) | |
6205 | strip = TRUE; | |
6206 | else if (finfo->info->strip == strip_some | |
6207 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6208 | h->root.root.string, FALSE, FALSE) == NULL) | |
6209 | strip = TRUE; | |
6210 | else if (finfo->info->strip_discarded | |
6211 | && (h->root.type == bfd_link_hash_defined | |
6212 | || h->root.type == bfd_link_hash_defweak) | |
6213 | && elf_discarded_section (h->root.u.def.section)) | |
6214 | strip = TRUE; | |
6215 | else | |
6216 | strip = FALSE; | |
6217 | ||
6218 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6219 | nothing else to do unless it is a forced local symbol. */ | |
6220 | if (strip | |
6221 | && h->dynindx == -1 | |
f5385ebf | 6222 | && !h->forced_local) |
c152c796 AM |
6223 | return TRUE; |
6224 | ||
6225 | sym.st_value = 0; | |
6226 | sym.st_size = h->size; | |
6227 | sym.st_other = h->other; | |
f5385ebf | 6228 | if (h->forced_local) |
c152c796 AM |
6229 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6230 | else if (h->root.type == bfd_link_hash_undefweak | |
6231 | || h->root.type == bfd_link_hash_defweak) | |
6232 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6233 | else | |
6234 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6235 | ||
6236 | switch (h->root.type) | |
6237 | { | |
6238 | default: | |
6239 | case bfd_link_hash_new: | |
6240 | case bfd_link_hash_warning: | |
6241 | abort (); | |
6242 | return FALSE; | |
6243 | ||
6244 | case bfd_link_hash_undefined: | |
6245 | case bfd_link_hash_undefweak: | |
6246 | input_sec = bfd_und_section_ptr; | |
6247 | sym.st_shndx = SHN_UNDEF; | |
6248 | break; | |
6249 | ||
6250 | case bfd_link_hash_defined: | |
6251 | case bfd_link_hash_defweak: | |
6252 | { | |
6253 | input_sec = h->root.u.def.section; | |
6254 | if (input_sec->output_section != NULL) | |
6255 | { | |
6256 | sym.st_shndx = | |
6257 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6258 | input_sec->output_section); | |
6259 | if (sym.st_shndx == SHN_BAD) | |
6260 | { | |
6261 | (*_bfd_error_handler) | |
d003868e AM |
6262 | (_("%B: could not find output section %A for input section %A"), |
6263 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
6264 | eoinfo->failed = TRUE; |
6265 | return FALSE; | |
6266 | } | |
6267 | ||
6268 | /* ELF symbols in relocatable files are section relative, | |
6269 | but in nonrelocatable files they are virtual | |
6270 | addresses. */ | |
6271 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6272 | if (! finfo->info->relocatable) | |
6273 | { | |
6274 | sym.st_value += input_sec->output_section->vma; | |
6275 | if (h->type == STT_TLS) | |
6276 | { | |
6277 | /* STT_TLS symbols are relative to PT_TLS segment | |
6278 | base. */ | |
6279 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6280 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6281 | } | |
6282 | } | |
6283 | } | |
6284 | else | |
6285 | { | |
6286 | BFD_ASSERT (input_sec->owner == NULL | |
6287 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6288 | sym.st_shndx = SHN_UNDEF; | |
6289 | input_sec = bfd_und_section_ptr; | |
6290 | } | |
6291 | } | |
6292 | break; | |
6293 | ||
6294 | case bfd_link_hash_common: | |
6295 | input_sec = h->root.u.c.p->section; | |
6296 | sym.st_shndx = SHN_COMMON; | |
6297 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
6298 | break; | |
6299 | ||
6300 | case bfd_link_hash_indirect: | |
6301 | /* These symbols are created by symbol versioning. They point | |
6302 | to the decorated version of the name. For example, if the | |
6303 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6304 | foo is used with no version, then we add an indirect symbol | |
6305 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6306 | since the indirected symbol is already in the hash table. */ | |
6307 | return TRUE; | |
6308 | } | |
6309 | ||
6310 | /* Give the processor backend a chance to tweak the symbol value, | |
6311 | and also to finish up anything that needs to be done for this | |
6312 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6313 | forced local syms when non-shared is due to a historical quirk. */ | |
6314 | if ((h->dynindx != -1 | |
f5385ebf | 6315 | || h->forced_local) |
c152c796 AM |
6316 | && ((finfo->info->shared |
6317 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6318 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 6319 | || !h->forced_local) |
c152c796 AM |
6320 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6321 | { | |
6322 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6323 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6324 | { | |
6325 | eoinfo->failed = TRUE; | |
6326 | return FALSE; | |
6327 | } | |
6328 | } | |
6329 | ||
6330 | /* If we are marking the symbol as undefined, and there are no | |
6331 | non-weak references to this symbol from a regular object, then | |
6332 | mark the symbol as weak undefined; if there are non-weak | |
6333 | references, mark the symbol as strong. We can't do this earlier, | |
6334 | because it might not be marked as undefined until the | |
6335 | finish_dynamic_symbol routine gets through with it. */ | |
6336 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 6337 | && h->ref_regular |
c152c796 AM |
6338 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6339 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6340 | { | |
6341 | int bindtype; | |
6342 | ||
f5385ebf | 6343 | if (h->ref_regular_nonweak) |
c152c796 AM |
6344 | bindtype = STB_GLOBAL; |
6345 | else | |
6346 | bindtype = STB_WEAK; | |
6347 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6348 | } | |
6349 | ||
6350 | /* If a non-weak symbol with non-default visibility is not defined | |
6351 | locally, it is a fatal error. */ | |
6352 | if (! finfo->info->relocatable | |
6353 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6354 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6355 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 6356 | && !h->def_regular) |
c152c796 AM |
6357 | { |
6358 | (*_bfd_error_handler) | |
d003868e AM |
6359 | (_("%B: %s symbol `%s' isn't defined"), |
6360 | finfo->output_bfd, | |
6361 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6362 | ? "protected" | |
6363 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6364 | ? "internal" : "hidden", | |
6365 | h->root.root.string); | |
c152c796 AM |
6366 | eoinfo->failed = TRUE; |
6367 | return FALSE; | |
6368 | } | |
6369 | ||
6370 | /* If this symbol should be put in the .dynsym section, then put it | |
6371 | there now. We already know the symbol index. We also fill in | |
6372 | the entry in the .hash section. */ | |
6373 | if (h->dynindx != -1 | |
6374 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6375 | { | |
6376 | size_t bucketcount; | |
6377 | size_t bucket; | |
6378 | size_t hash_entry_size; | |
6379 | bfd_byte *bucketpos; | |
6380 | bfd_vma chain; | |
6381 | bfd_byte *esym; | |
6382 | ||
6383 | sym.st_name = h->dynstr_index; | |
6384 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6385 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6386 | ||
6387 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
f6e332e6 | 6388 | bucket = h->u.elf_hash_value % bucketcount; |
c152c796 AM |
6389 | hash_entry_size |
6390 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6391 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6392 | + (bucket + 2) * hash_entry_size); | |
6393 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6394 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6395 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6396 | ((bfd_byte *) finfo->hash_sec->contents | |
6397 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6398 | ||
6399 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6400 | { | |
6401 | Elf_Internal_Versym iversym; | |
6402 | Elf_External_Versym *eversym; | |
6403 | ||
f5385ebf | 6404 | if (!h->def_regular) |
c152c796 AM |
6405 | { |
6406 | if (h->verinfo.verdef == NULL) | |
6407 | iversym.vs_vers = 0; | |
6408 | else | |
6409 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6410 | } | |
6411 | else | |
6412 | { | |
6413 | if (h->verinfo.vertree == NULL) | |
6414 | iversym.vs_vers = 1; | |
6415 | else | |
6416 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
3e3b46e5 PB |
6417 | if (finfo->info->create_default_symver) |
6418 | iversym.vs_vers++; | |
c152c796 AM |
6419 | } |
6420 | ||
f5385ebf | 6421 | if (h->hidden) |
c152c796 AM |
6422 | iversym.vs_vers |= VERSYM_HIDDEN; |
6423 | ||
6424 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6425 | eversym += h->dynindx; | |
6426 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6427 | } | |
6428 | } | |
6429 | ||
6430 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6431 | there's nothing else to do. */ | |
6432 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6433 | return TRUE; | |
6434 | ||
6435 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6436 | ||
6437 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6438 | { | |
6439 | eoinfo->failed = TRUE; | |
6440 | return FALSE; | |
6441 | } | |
6442 | ||
6443 | return TRUE; | |
6444 | } | |
6445 | ||
cdd3575c AM |
6446 | /* Return TRUE if special handling is done for relocs in SEC against |
6447 | symbols defined in discarded sections. */ | |
6448 | ||
c152c796 AM |
6449 | static bfd_boolean |
6450 | elf_section_ignore_discarded_relocs (asection *sec) | |
6451 | { | |
6452 | const struct elf_backend_data *bed; | |
6453 | ||
cdd3575c AM |
6454 | switch (sec->sec_info_type) |
6455 | { | |
6456 | case ELF_INFO_TYPE_STABS: | |
6457 | case ELF_INFO_TYPE_EH_FRAME: | |
6458 | return TRUE; | |
6459 | default: | |
6460 | break; | |
6461 | } | |
c152c796 AM |
6462 | |
6463 | bed = get_elf_backend_data (sec->owner); | |
6464 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6465 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6466 | return TRUE; | |
6467 | ||
6468 | return FALSE; | |
6469 | } | |
6470 | ||
9e66c942 AM |
6471 | enum action_discarded |
6472 | { | |
6473 | COMPLAIN = 1, | |
6474 | PRETEND = 2 | |
6475 | }; | |
6476 | ||
6477 | /* Return a mask saying how ld should treat relocations in SEC against | |
6478 | symbols defined in discarded sections. If this function returns | |
6479 | COMPLAIN set, ld will issue a warning message. If this function | |
6480 | returns PRETEND set, and the discarded section was link-once and the | |
6481 | same size as the kept link-once section, ld will pretend that the | |
6482 | symbol was actually defined in the kept section. Otherwise ld will | |
6483 | zero the reloc (at least that is the intent, but some cooperation by | |
6484 | the target dependent code is needed, particularly for REL targets). */ | |
6485 | ||
6486 | static unsigned int | |
6487 | elf_action_discarded (asection *sec) | |
cdd3575c | 6488 | { |
9e66c942 AM |
6489 | if (sec->flags & SEC_DEBUGGING) |
6490 | return PRETEND; | |
cdd3575c AM |
6491 | |
6492 | if (strcmp (".eh_frame", sec->name) == 0) | |
9e66c942 | 6493 | return 0; |
cdd3575c AM |
6494 | |
6495 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
9e66c942 | 6496 | return 0; |
cdd3575c | 6497 | |
27b56da8 | 6498 | if (strcmp (".PARISC.unwind", sec->name) == 0) |
9e66c942 | 6499 | return 0; |
327c1315 AM |
6500 | |
6501 | if (strcmp (".fixup", sec->name) == 0) | |
9e66c942 | 6502 | return 0; |
27b56da8 | 6503 | |
9e66c942 | 6504 | return COMPLAIN | PRETEND; |
cdd3575c AM |
6505 | } |
6506 | ||
3d7f7666 L |
6507 | /* Find a match between a section and a member of a section group. */ |
6508 | ||
6509 | static asection * | |
6510 | match_group_member (asection *sec, asection *group) | |
6511 | { | |
6512 | asection *first = elf_next_in_group (group); | |
6513 | asection *s = first; | |
6514 | ||
6515 | while (s != NULL) | |
6516 | { | |
6517 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
6518 | return s; | |
6519 | ||
6520 | if (s == first) | |
6521 | break; | |
6522 | } | |
6523 | ||
6524 | return NULL; | |
6525 | } | |
6526 | ||
c152c796 AM |
6527 | /* Link an input file into the linker output file. This function |
6528 | handles all the sections and relocations of the input file at once. | |
6529 | This is so that we only have to read the local symbols once, and | |
6530 | don't have to keep them in memory. */ | |
6531 | ||
6532 | static bfd_boolean | |
6533 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6534 | { | |
6535 | bfd_boolean (*relocate_section) | |
6536 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6537 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6538 | bfd *output_bfd; | |
6539 | Elf_Internal_Shdr *symtab_hdr; | |
6540 | size_t locsymcount; | |
6541 | size_t extsymoff; | |
6542 | Elf_Internal_Sym *isymbuf; | |
6543 | Elf_Internal_Sym *isym; | |
6544 | Elf_Internal_Sym *isymend; | |
6545 | long *pindex; | |
6546 | asection **ppsection; | |
6547 | asection *o; | |
6548 | const struct elf_backend_data *bed; | |
6549 | bfd_boolean emit_relocs; | |
6550 | struct elf_link_hash_entry **sym_hashes; | |
6551 | ||
6552 | output_bfd = finfo->output_bfd; | |
6553 | bed = get_elf_backend_data (output_bfd); | |
6554 | relocate_section = bed->elf_backend_relocate_section; | |
6555 | ||
6556 | /* If this is a dynamic object, we don't want to do anything here: | |
6557 | we don't want the local symbols, and we don't want the section | |
6558 | contents. */ | |
6559 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6560 | return TRUE; | |
6561 | ||
6562 | emit_relocs = (finfo->info->relocatable | |
6563 | || finfo->info->emitrelocations | |
6564 | || bed->elf_backend_emit_relocs); | |
6565 | ||
6566 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6567 | if (elf_bad_symtab (input_bfd)) | |
6568 | { | |
6569 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6570 | extsymoff = 0; | |
6571 | } | |
6572 | else | |
6573 | { | |
6574 | locsymcount = symtab_hdr->sh_info; | |
6575 | extsymoff = symtab_hdr->sh_info; | |
6576 | } | |
6577 | ||
6578 | /* Read the local symbols. */ | |
6579 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6580 | if (isymbuf == NULL && locsymcount != 0) | |
6581 | { | |
6582 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6583 | finfo->internal_syms, | |
6584 | finfo->external_syms, | |
6585 | finfo->locsym_shndx); | |
6586 | if (isymbuf == NULL) | |
6587 | return FALSE; | |
6588 | } | |
6589 | ||
6590 | /* Find local symbol sections and adjust values of symbols in | |
6591 | SEC_MERGE sections. Write out those local symbols we know are | |
6592 | going into the output file. */ | |
6593 | isymend = isymbuf + locsymcount; | |
6594 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6595 | isym < isymend; | |
6596 | isym++, pindex++, ppsection++) | |
6597 | { | |
6598 | asection *isec; | |
6599 | const char *name; | |
6600 | Elf_Internal_Sym osym; | |
6601 | ||
6602 | *pindex = -1; | |
6603 | ||
6604 | if (elf_bad_symtab (input_bfd)) | |
6605 | { | |
6606 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6607 | { | |
6608 | *ppsection = NULL; | |
6609 | continue; | |
6610 | } | |
6611 | } | |
6612 | ||
6613 | if (isym->st_shndx == SHN_UNDEF) | |
6614 | isec = bfd_und_section_ptr; | |
6615 | else if (isym->st_shndx < SHN_LORESERVE | |
6616 | || isym->st_shndx > SHN_HIRESERVE) | |
6617 | { | |
6618 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6619 | if (isec | |
6620 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6621 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6622 | isym->st_value = | |
6623 | _bfd_merged_section_offset (output_bfd, &isec, | |
6624 | elf_section_data (isec)->sec_info, | |
753731ee | 6625 | isym->st_value); |
c152c796 AM |
6626 | } |
6627 | else if (isym->st_shndx == SHN_ABS) | |
6628 | isec = bfd_abs_section_ptr; | |
6629 | else if (isym->st_shndx == SHN_COMMON) | |
6630 | isec = bfd_com_section_ptr; | |
6631 | else | |
6632 | { | |
6633 | /* Who knows? */ | |
6634 | isec = NULL; | |
6635 | } | |
6636 | ||
6637 | *ppsection = isec; | |
6638 | ||
6639 | /* Don't output the first, undefined, symbol. */ | |
6640 | if (ppsection == finfo->sections) | |
6641 | continue; | |
6642 | ||
6643 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6644 | { | |
6645 | /* We never output section symbols. Instead, we use the | |
6646 | section symbol of the corresponding section in the output | |
6647 | file. */ | |
6648 | continue; | |
6649 | } | |
6650 | ||
6651 | /* If we are stripping all symbols, we don't want to output this | |
6652 | one. */ | |
6653 | if (finfo->info->strip == strip_all) | |
6654 | continue; | |
6655 | ||
6656 | /* If we are discarding all local symbols, we don't want to | |
6657 | output this one. If we are generating a relocatable output | |
6658 | file, then some of the local symbols may be required by | |
6659 | relocs; we output them below as we discover that they are | |
6660 | needed. */ | |
6661 | if (finfo->info->discard == discard_all) | |
6662 | continue; | |
6663 | ||
6664 | /* If this symbol is defined in a section which we are | |
6665 | discarding, we don't need to keep it, but note that | |
6666 | linker_mark is only reliable for sections that have contents. | |
6667 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6668 | as well as linker_mark. */ | |
6669 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
6670 | && isec != NULL | |
6671 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
6672 | || (! finfo->info->relocatable | |
6673 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6674 | continue; | |
6675 | ||
6676 | /* Get the name of the symbol. */ | |
6677 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6678 | isym->st_name); | |
6679 | if (name == NULL) | |
6680 | return FALSE; | |
6681 | ||
6682 | /* See if we are discarding symbols with this name. */ | |
6683 | if ((finfo->info->strip == strip_some | |
6684 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6685 | == NULL)) | |
6686 | || (((finfo->info->discard == discard_sec_merge | |
6687 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6688 | || finfo->info->discard == discard_l) | |
6689 | && bfd_is_local_label_name (input_bfd, name))) | |
6690 | continue; | |
6691 | ||
6692 | /* If we get here, we are going to output this symbol. */ | |
6693 | ||
6694 | osym = *isym; | |
6695 | ||
6696 | /* Adjust the section index for the output file. */ | |
6697 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6698 | isec->output_section); | |
6699 | if (osym.st_shndx == SHN_BAD) | |
6700 | return FALSE; | |
6701 | ||
6702 | *pindex = bfd_get_symcount (output_bfd); | |
6703 | ||
6704 | /* ELF symbols in relocatable files are section relative, but | |
6705 | in executable files they are virtual addresses. Note that | |
6706 | this code assumes that all ELF sections have an associated | |
6707 | BFD section with a reasonable value for output_offset; below | |
6708 | we assume that they also have a reasonable value for | |
6709 | output_section. Any special sections must be set up to meet | |
6710 | these requirements. */ | |
6711 | osym.st_value += isec->output_offset; | |
6712 | if (! finfo->info->relocatable) | |
6713 | { | |
6714 | osym.st_value += isec->output_section->vma; | |
6715 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6716 | { | |
6717 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6718 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6719 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6720 | } | |
6721 | } | |
6722 | ||
6723 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6724 | return FALSE; | |
6725 | } | |
6726 | ||
6727 | /* Relocate the contents of each section. */ | |
6728 | sym_hashes = elf_sym_hashes (input_bfd); | |
6729 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6730 | { | |
6731 | bfd_byte *contents; | |
6732 | ||
6733 | if (! o->linker_mark) | |
6734 | { | |
6735 | /* This section was omitted from the link. */ | |
6736 | continue; | |
6737 | } | |
6738 | ||
6739 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 6740 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
6741 | continue; |
6742 | ||
6743 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6744 | { | |
6745 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6746 | or somesuch. */ | |
6747 | continue; | |
6748 | } | |
6749 | ||
6750 | /* Get the contents of the section. They have been cached by a | |
6751 | relaxation routine. Note that o is a section in an input | |
6752 | file, so the contents field will not have been set by any of | |
6753 | the routines which work on output files. */ | |
6754 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6755 | contents = elf_section_data (o)->this_hdr.contents; | |
6756 | else | |
6757 | { | |
eea6121a AM |
6758 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
6759 | ||
c152c796 | 6760 | contents = finfo->contents; |
eea6121a | 6761 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
6762 | return FALSE; |
6763 | } | |
6764 | ||
6765 | if ((o->flags & SEC_RELOC) != 0) | |
6766 | { | |
6767 | Elf_Internal_Rela *internal_relocs; | |
6768 | bfd_vma r_type_mask; | |
6769 | int r_sym_shift; | |
6770 | ||
6771 | /* Get the swapped relocs. */ | |
6772 | internal_relocs | |
6773 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
6774 | finfo->internal_relocs, FALSE); | |
6775 | if (internal_relocs == NULL | |
6776 | && o->reloc_count > 0) | |
6777 | return FALSE; | |
6778 | ||
6779 | if (bed->s->arch_size == 32) | |
6780 | { | |
6781 | r_type_mask = 0xff; | |
6782 | r_sym_shift = 8; | |
6783 | } | |
6784 | else | |
6785 | { | |
6786 | r_type_mask = 0xffffffff; | |
6787 | r_sym_shift = 32; | |
6788 | } | |
6789 | ||
6790 | /* Run through the relocs looking for any against symbols | |
6791 | from discarded sections and section symbols from | |
6792 | removed link-once sections. Complain about relocs | |
6793 | against discarded sections. Zero relocs against removed | |
6794 | link-once sections. Preserve debug information as much | |
6795 | as we can. */ | |
6796 | if (!elf_section_ignore_discarded_relocs (o)) | |
6797 | { | |
6798 | Elf_Internal_Rela *rel, *relend; | |
9e66c942 | 6799 | unsigned int action = elf_action_discarded (o); |
c152c796 AM |
6800 | |
6801 | rel = internal_relocs; | |
6802 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6803 | for ( ; rel < relend; rel++) | |
6804 | { | |
6805 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
6806 | asection **ps, *sec; |
6807 | struct elf_link_hash_entry *h = NULL; | |
6808 | const char *sym_name; | |
c152c796 | 6809 | |
ee75fd95 AM |
6810 | if (r_symndx == STN_UNDEF) |
6811 | continue; | |
6812 | ||
c152c796 AM |
6813 | if (r_symndx >= locsymcount |
6814 | || (elf_bad_symtab (input_bfd) | |
6815 | && finfo->sections[r_symndx] == NULL)) | |
6816 | { | |
c152c796 AM |
6817 | h = sym_hashes[r_symndx - extsymoff]; |
6818 | while (h->root.type == bfd_link_hash_indirect | |
6819 | || h->root.type == bfd_link_hash_warning) | |
6820 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6821 | ||
cdd3575c AM |
6822 | if (h->root.type != bfd_link_hash_defined |
6823 | && h->root.type != bfd_link_hash_defweak) | |
6824 | continue; | |
6825 | ||
6826 | ps = &h->root.u.def.section; | |
6827 | sym_name = h->root.root.string; | |
c152c796 AM |
6828 | } |
6829 | else | |
6830 | { | |
cdd3575c AM |
6831 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
6832 | ps = &finfo->sections[r_symndx]; | |
be8dd2ca | 6833 | sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym); |
cdd3575c | 6834 | } |
c152c796 | 6835 | |
cdd3575c AM |
6836 | /* Complain if the definition comes from a |
6837 | discarded section. */ | |
6838 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
6839 | { | |
87e5235d | 6840 | asection *kept; |
3d7f7666 | 6841 | |
87e5235d | 6842 | BFD_ASSERT (r_symndx != 0); |
9e66c942 | 6843 | if (action & COMPLAIN) |
cdd3575c | 6844 | { |
d003868e AM |
6845 | (*_bfd_error_handler) |
6846 | (_("`%s' referenced in section `%A' of %B: " | |
6847 | "defined in discarded section `%A' of %B\n"), | |
6848 | o, input_bfd, sec, sec->owner, sym_name); | |
cdd3575c AM |
6849 | } |
6850 | ||
87e5235d AM |
6851 | /* Try to do the best we can to support buggy old |
6852 | versions of gcc. If we've warned, or this is | |
6853 | debugging info, pretend that the symbol is | |
6854 | really defined in the kept linkonce section. | |
6855 | FIXME: This is quite broken. Modifying the | |
6856 | symbol here means we will be changing all later | |
6857 | uses of the symbol, not just in this section. | |
6858 | The only thing that makes this half reasonable | |
6859 | is that we warn in non-debug sections, and | |
6860 | debug sections tend to come after other | |
6861 | sections. */ | |
6862 | kept = sec->kept_section; | |
9e66c942 | 6863 | if (kept != NULL && (action & PRETEND)) |
87e5235d AM |
6864 | { |
6865 | if (elf_sec_group (sec) != NULL) | |
6866 | kept = match_group_member (sec, kept); | |
6867 | if (kept != NULL | |
6868 | && sec->size == kept->size) | |
6869 | { | |
6870 | *ps = kept; | |
6871 | continue; | |
6872 | } | |
6873 | } | |
6874 | ||
cdd3575c AM |
6875 | /* Remove the symbol reference from the reloc, but |
6876 | don't kill the reloc completely. This is so that | |
6877 | a zero value will be written into the section, | |
6878 | which may have non-zero contents put there by the | |
6879 | assembler. Zero in things like an eh_frame fde | |
6880 | pc_begin allows stack unwinders to recognize the | |
6881 | fde as bogus. */ | |
6882 | rel->r_info &= r_type_mask; | |
6883 | rel->r_addend = 0; | |
c152c796 AM |
6884 | } |
6885 | } | |
6886 | } | |
6887 | ||
6888 | /* Relocate the section by invoking a back end routine. | |
6889 | ||
6890 | The back end routine is responsible for adjusting the | |
6891 | section contents as necessary, and (if using Rela relocs | |
6892 | and generating a relocatable output file) adjusting the | |
6893 | reloc addend as necessary. | |
6894 | ||
6895 | The back end routine does not have to worry about setting | |
6896 | the reloc address or the reloc symbol index. | |
6897 | ||
6898 | The back end routine is given a pointer to the swapped in | |
6899 | internal symbols, and can access the hash table entries | |
6900 | for the external symbols via elf_sym_hashes (input_bfd). | |
6901 | ||
6902 | When generating relocatable output, the back end routine | |
6903 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
6904 | output symbol is going to be a section symbol | |
6905 | corresponding to the output section, which will require | |
6906 | the addend to be adjusted. */ | |
6907 | ||
6908 | if (! (*relocate_section) (output_bfd, finfo->info, | |
6909 | input_bfd, o, contents, | |
6910 | internal_relocs, | |
6911 | isymbuf, | |
6912 | finfo->sections)) | |
6913 | return FALSE; | |
6914 | ||
6915 | if (emit_relocs) | |
6916 | { | |
6917 | Elf_Internal_Rela *irela; | |
6918 | Elf_Internal_Rela *irelaend; | |
6919 | bfd_vma last_offset; | |
6920 | struct elf_link_hash_entry **rel_hash; | |
6921 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; | |
6922 | unsigned int next_erel; | |
6923 | bfd_boolean (*reloc_emitter) | |
6924 | (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *); | |
6925 | bfd_boolean rela_normal; | |
6926 | ||
6927 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
6928 | rela_normal = (bed->rela_normal | |
6929 | && (input_rel_hdr->sh_entsize | |
6930 | == bed->s->sizeof_rela)); | |
6931 | ||
6932 | /* Adjust the reloc addresses and symbol indices. */ | |
6933 | ||
6934 | irela = internal_relocs; | |
6935 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6936 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
6937 | + elf_section_data (o->output_section)->rel_count | |
6938 | + elf_section_data (o->output_section)->rel_count2); | |
6939 | last_offset = o->output_offset; | |
6940 | if (!finfo->info->relocatable) | |
6941 | last_offset += o->output_section->vma; | |
6942 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
6943 | { | |
6944 | unsigned long r_symndx; | |
6945 | asection *sec; | |
6946 | Elf_Internal_Sym sym; | |
6947 | ||
6948 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
6949 | { | |
6950 | rel_hash++; | |
6951 | next_erel = 0; | |
6952 | } | |
6953 | ||
6954 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
6955 | finfo->info, o, | |
6956 | irela->r_offset); | |
6957 | if (irela->r_offset >= (bfd_vma) -2) | |
6958 | { | |
6959 | /* This is a reloc for a deleted entry or somesuch. | |
6960 | Turn it into an R_*_NONE reloc, at the same | |
6961 | offset as the last reloc. elf_eh_frame.c and | |
6962 | elf_bfd_discard_info rely on reloc offsets | |
6963 | being ordered. */ | |
6964 | irela->r_offset = last_offset; | |
6965 | irela->r_info = 0; | |
6966 | irela->r_addend = 0; | |
6967 | continue; | |
6968 | } | |
6969 | ||
6970 | irela->r_offset += o->output_offset; | |
6971 | ||
6972 | /* Relocs in an executable have to be virtual addresses. */ | |
6973 | if (!finfo->info->relocatable) | |
6974 | irela->r_offset += o->output_section->vma; | |
6975 | ||
6976 | last_offset = irela->r_offset; | |
6977 | ||
6978 | r_symndx = irela->r_info >> r_sym_shift; | |
6979 | if (r_symndx == STN_UNDEF) | |
6980 | continue; | |
6981 | ||
6982 | if (r_symndx >= locsymcount | |
6983 | || (elf_bad_symtab (input_bfd) | |
6984 | && finfo->sections[r_symndx] == NULL)) | |
6985 | { | |
6986 | struct elf_link_hash_entry *rh; | |
6987 | unsigned long indx; | |
6988 | ||
6989 | /* This is a reloc against a global symbol. We | |
6990 | have not yet output all the local symbols, so | |
6991 | we do not know the symbol index of any global | |
6992 | symbol. We set the rel_hash entry for this | |
6993 | reloc to point to the global hash table entry | |
6994 | for this symbol. The symbol index is then | |
ee75fd95 | 6995 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
6996 | indx = r_symndx - extsymoff; |
6997 | rh = elf_sym_hashes (input_bfd)[indx]; | |
6998 | while (rh->root.type == bfd_link_hash_indirect | |
6999 | || rh->root.type == bfd_link_hash_warning) | |
7000 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
7001 | ||
7002 | /* Setting the index to -2 tells | |
7003 | elf_link_output_extsym that this symbol is | |
7004 | used by a reloc. */ | |
7005 | BFD_ASSERT (rh->indx < 0); | |
7006 | rh->indx = -2; | |
7007 | ||
7008 | *rel_hash = rh; | |
7009 | ||
7010 | continue; | |
7011 | } | |
7012 | ||
7013 | /* This is a reloc against a local symbol. */ | |
7014 | ||
7015 | *rel_hash = NULL; | |
7016 | sym = isymbuf[r_symndx]; | |
7017 | sec = finfo->sections[r_symndx]; | |
7018 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
7019 | { | |
7020 | /* I suppose the backend ought to fill in the | |
7021 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
7022 | processor specific section. */ |
7023 | r_symndx = 0; | |
7024 | if (bfd_is_abs_section (sec)) | |
7025 | ; | |
c152c796 AM |
7026 | else if (sec == NULL || sec->owner == NULL) |
7027 | { | |
7028 | bfd_set_error (bfd_error_bad_value); | |
7029 | return FALSE; | |
7030 | } | |
7031 | else | |
7032 | { | |
6a8d1586 AM |
7033 | asection *osec = sec->output_section; |
7034 | ||
7035 | /* If we have discarded a section, the output | |
7036 | section will be the absolute section. In | |
7037 | case of discarded link-once and discarded | |
7038 | SEC_MERGE sections, use the kept section. */ | |
7039 | if (bfd_is_abs_section (osec) | |
7040 | && sec->kept_section != NULL | |
7041 | && sec->kept_section->output_section != NULL) | |
7042 | { | |
7043 | osec = sec->kept_section->output_section; | |
7044 | irela->r_addend -= osec->vma; | |
7045 | } | |
7046 | ||
7047 | if (!bfd_is_abs_section (osec)) | |
7048 | { | |
7049 | r_symndx = osec->target_index; | |
7050 | BFD_ASSERT (r_symndx != 0); | |
7051 | } | |
c152c796 AM |
7052 | } |
7053 | ||
7054 | /* Adjust the addend according to where the | |
7055 | section winds up in the output section. */ | |
7056 | if (rela_normal) | |
7057 | irela->r_addend += sec->output_offset; | |
7058 | } | |
7059 | else | |
7060 | { | |
7061 | if (finfo->indices[r_symndx] == -1) | |
7062 | { | |
7063 | unsigned long shlink; | |
7064 | const char *name; | |
7065 | asection *osec; | |
7066 | ||
7067 | if (finfo->info->strip == strip_all) | |
7068 | { | |
7069 | /* You can't do ld -r -s. */ | |
7070 | bfd_set_error (bfd_error_invalid_operation); | |
7071 | return FALSE; | |
7072 | } | |
7073 | ||
7074 | /* This symbol was skipped earlier, but | |
7075 | since it is needed by a reloc, we | |
7076 | must output it now. */ | |
7077 | shlink = symtab_hdr->sh_link; | |
7078 | name = (bfd_elf_string_from_elf_section | |
7079 | (input_bfd, shlink, sym.st_name)); | |
7080 | if (name == NULL) | |
7081 | return FALSE; | |
7082 | ||
7083 | osec = sec->output_section; | |
7084 | sym.st_shndx = | |
7085 | _bfd_elf_section_from_bfd_section (output_bfd, | |
7086 | osec); | |
7087 | if (sym.st_shndx == SHN_BAD) | |
7088 | return FALSE; | |
7089 | ||
7090 | sym.st_value += sec->output_offset; | |
7091 | if (! finfo->info->relocatable) | |
7092 | { | |
7093 | sym.st_value += osec->vma; | |
7094 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
7095 | { | |
7096 | /* STT_TLS symbols are relative to PT_TLS | |
7097 | segment base. */ | |
7098 | BFD_ASSERT (elf_hash_table (finfo->info) | |
7099 | ->tls_sec != NULL); | |
7100 | sym.st_value -= (elf_hash_table (finfo->info) | |
7101 | ->tls_sec->vma); | |
7102 | } | |
7103 | } | |
7104 | ||
7105 | finfo->indices[r_symndx] | |
7106 | = bfd_get_symcount (output_bfd); | |
7107 | ||
7108 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7109 | NULL)) | |
7110 | return FALSE; | |
7111 | } | |
7112 | ||
7113 | r_symndx = finfo->indices[r_symndx]; | |
7114 | } | |
7115 | ||
7116 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7117 | | (irela->r_info & r_type_mask)); | |
7118 | } | |
7119 | ||
7120 | /* Swap out the relocs. */ | |
7121 | if (bed->elf_backend_emit_relocs | |
7122 | && !(finfo->info->relocatable | |
7123 | || finfo->info->emitrelocations)) | |
7124 | reloc_emitter = bed->elf_backend_emit_relocs; | |
7125 | else | |
7126 | reloc_emitter = _bfd_elf_link_output_relocs; | |
7127 | ||
7128 | if (input_rel_hdr->sh_size != 0 | |
7129 | && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr, | |
7130 | internal_relocs)) | |
7131 | return FALSE; | |
7132 | ||
7133 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7134 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7135 | { | |
7136 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7137 | * bed->s->int_rels_per_ext_rel); | |
7138 | if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2, | |
7139 | internal_relocs)) | |
7140 | return FALSE; | |
7141 | } | |
7142 | } | |
7143 | } | |
7144 | ||
7145 | /* Write out the modified section contents. */ | |
7146 | if (bed->elf_backend_write_section | |
7147 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7148 | { | |
7149 | /* Section written out. */ | |
7150 | } | |
7151 | else switch (o->sec_info_type) | |
7152 | { | |
7153 | case ELF_INFO_TYPE_STABS: | |
7154 | if (! (_bfd_write_section_stabs | |
7155 | (output_bfd, | |
7156 | &elf_hash_table (finfo->info)->stab_info, | |
7157 | o, &elf_section_data (o)->sec_info, contents))) | |
7158 | return FALSE; | |
7159 | break; | |
7160 | case ELF_INFO_TYPE_MERGE: | |
7161 | if (! _bfd_write_merged_section (output_bfd, o, | |
7162 | elf_section_data (o)->sec_info)) | |
7163 | return FALSE; | |
7164 | break; | |
7165 | case ELF_INFO_TYPE_EH_FRAME: | |
7166 | { | |
7167 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7168 | o, contents)) | |
7169 | return FALSE; | |
7170 | } | |
7171 | break; | |
7172 | default: | |
7173 | { | |
c152c796 AM |
7174 | if (! (o->flags & SEC_EXCLUDE) |
7175 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7176 | contents, | |
7177 | (file_ptr) o->output_offset, | |
eea6121a | 7178 | o->size)) |
c152c796 AM |
7179 | return FALSE; |
7180 | } | |
7181 | break; | |
7182 | } | |
7183 | } | |
7184 | ||
7185 | return TRUE; | |
7186 | } | |
7187 | ||
7188 | /* Generate a reloc when linking an ELF file. This is a reloc | |
7189 | requested by the linker, and does come from any input file. This | |
7190 | is used to build constructor and destructor tables when linking | |
7191 | with -Ur. */ | |
7192 | ||
7193 | static bfd_boolean | |
7194 | elf_reloc_link_order (bfd *output_bfd, | |
7195 | struct bfd_link_info *info, | |
7196 | asection *output_section, | |
7197 | struct bfd_link_order *link_order) | |
7198 | { | |
7199 | reloc_howto_type *howto; | |
7200 | long indx; | |
7201 | bfd_vma offset; | |
7202 | bfd_vma addend; | |
7203 | struct elf_link_hash_entry **rel_hash_ptr; | |
7204 | Elf_Internal_Shdr *rel_hdr; | |
7205 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
7206 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
7207 | bfd_byte *erel; | |
7208 | unsigned int i; | |
7209 | ||
7210 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7211 | if (howto == NULL) | |
7212 | { | |
7213 | bfd_set_error (bfd_error_bad_value); | |
7214 | return FALSE; | |
7215 | } | |
7216 | ||
7217 | addend = link_order->u.reloc.p->addend; | |
7218 | ||
7219 | /* Figure out the symbol index. */ | |
7220 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
7221 | + elf_section_data (output_section)->rel_count | |
7222 | + elf_section_data (output_section)->rel_count2); | |
7223 | if (link_order->type == bfd_section_reloc_link_order) | |
7224 | { | |
7225 | indx = link_order->u.reloc.p->u.section->target_index; | |
7226 | BFD_ASSERT (indx != 0); | |
7227 | *rel_hash_ptr = NULL; | |
7228 | } | |
7229 | else | |
7230 | { | |
7231 | struct elf_link_hash_entry *h; | |
7232 | ||
7233 | /* Treat a reloc against a defined symbol as though it were | |
7234 | actually against the section. */ | |
7235 | h = ((struct elf_link_hash_entry *) | |
7236 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7237 | link_order->u.reloc.p->u.name, | |
7238 | FALSE, FALSE, TRUE)); | |
7239 | if (h != NULL | |
7240 | && (h->root.type == bfd_link_hash_defined | |
7241 | || h->root.type == bfd_link_hash_defweak)) | |
7242 | { | |
7243 | asection *section; | |
7244 | ||
7245 | section = h->root.u.def.section; | |
7246 | indx = section->output_section->target_index; | |
7247 | *rel_hash_ptr = NULL; | |
7248 | /* It seems that we ought to add the symbol value to the | |
7249 | addend here, but in practice it has already been added | |
7250 | because it was passed to constructor_callback. */ | |
7251 | addend += section->output_section->vma + section->output_offset; | |
7252 | } | |
7253 | else if (h != NULL) | |
7254 | { | |
7255 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7256 | this symbol is used by a reloc. */ | |
7257 | h->indx = -2; | |
7258 | *rel_hash_ptr = h; | |
7259 | indx = 0; | |
7260 | } | |
7261 | else | |
7262 | { | |
7263 | if (! ((*info->callbacks->unattached_reloc) | |
7264 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
7265 | return FALSE; | |
7266 | indx = 0; | |
7267 | } | |
7268 | } | |
7269 | ||
7270 | /* If this is an inplace reloc, we must write the addend into the | |
7271 | object file. */ | |
7272 | if (howto->partial_inplace && addend != 0) | |
7273 | { | |
7274 | bfd_size_type size; | |
7275 | bfd_reloc_status_type rstat; | |
7276 | bfd_byte *buf; | |
7277 | bfd_boolean ok; | |
7278 | const char *sym_name; | |
7279 | ||
7280 | size = bfd_get_reloc_size (howto); | |
7281 | buf = bfd_zmalloc (size); | |
7282 | if (buf == NULL) | |
7283 | return FALSE; | |
7284 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7285 | switch (rstat) | |
7286 | { | |
7287 | case bfd_reloc_ok: | |
7288 | break; | |
7289 | ||
7290 | default: | |
7291 | case bfd_reloc_outofrange: | |
7292 | abort (); | |
7293 | ||
7294 | case bfd_reloc_overflow: | |
7295 | if (link_order->type == bfd_section_reloc_link_order) | |
7296 | sym_name = bfd_section_name (output_bfd, | |
7297 | link_order->u.reloc.p->u.section); | |
7298 | else | |
7299 | sym_name = link_order->u.reloc.p->u.name; | |
7300 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
7301 | (info, NULL, sym_name, howto->name, addend, NULL, |
7302 | NULL, (bfd_vma) 0))) | |
c152c796 AM |
7303 | { |
7304 | free (buf); | |
7305 | return FALSE; | |
7306 | } | |
7307 | break; | |
7308 | } | |
7309 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7310 | link_order->offset, size); | |
7311 | free (buf); | |
7312 | if (! ok) | |
7313 | return FALSE; | |
7314 | } | |
7315 | ||
7316 | /* The address of a reloc is relative to the section in a | |
7317 | relocatable file, and is a virtual address in an executable | |
7318 | file. */ | |
7319 | offset = link_order->offset; | |
7320 | if (! info->relocatable) | |
7321 | offset += output_section->vma; | |
7322 | ||
7323 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7324 | { | |
7325 | irel[i].r_offset = offset; | |
7326 | irel[i].r_info = 0; | |
7327 | irel[i].r_addend = 0; | |
7328 | } | |
7329 | if (bed->s->arch_size == 32) | |
7330 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7331 | else | |
7332 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7333 | ||
7334 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7335 | erel = rel_hdr->contents; | |
7336 | if (rel_hdr->sh_type == SHT_REL) | |
7337 | { | |
7338 | erel += (elf_section_data (output_section)->rel_count | |
7339 | * bed->s->sizeof_rel); | |
7340 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7341 | } | |
7342 | else | |
7343 | { | |
7344 | irel[0].r_addend = addend; | |
7345 | erel += (elf_section_data (output_section)->rel_count | |
7346 | * bed->s->sizeof_rela); | |
7347 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7348 | } | |
7349 | ||
7350 | ++elf_section_data (output_section)->rel_count; | |
7351 | ||
7352 | return TRUE; | |
7353 | } | |
7354 | ||
0b52efa6 PB |
7355 | |
7356 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
7357 | ||
7358 | static bfd_vma | |
7359 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
7360 | { | |
7361 | Elf_Internal_Shdr **elf_shdrp; | |
7362 | asection *s; | |
7363 | int elfsec; | |
7364 | ||
7365 | s = p->u.indirect.section; | |
7366 | elf_shdrp = elf_elfsections (s->owner); | |
7367 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
7368 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
7369 | /* PR 290: |
7370 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
7371 | SHF_LINK_ORDER. But it doesn't set theh sh_link or | |
7372 | sh_info fields. Hence we could get the situation | |
7373 | where elfsec is 0. */ | |
7374 | if (elfsec == 0) | |
7375 | { | |
7376 | const struct elf_backend_data *bed | |
7377 | = get_elf_backend_data (s->owner); | |
7378 | if (bed->link_order_error_handler) | |
d003868e AM |
7379 | bed->link_order_error_handler |
7380 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
7381 | return 0; |
7382 | } | |
7383 | else | |
7384 | { | |
7385 | s = elf_shdrp[elfsec]->bfd_section; | |
7386 | return s->output_section->vma + s->output_offset; | |
7387 | } | |
0b52efa6 PB |
7388 | } |
7389 | ||
7390 | ||
7391 | /* Compare two sections based on the locations of the sections they are | |
7392 | linked to. Used by elf_fixup_link_order. */ | |
7393 | ||
7394 | static int | |
7395 | compare_link_order (const void * a, const void * b) | |
7396 | { | |
7397 | bfd_vma apos; | |
7398 | bfd_vma bpos; | |
7399 | ||
7400 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
7401 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
7402 | if (apos < bpos) | |
7403 | return -1; | |
7404 | return apos > bpos; | |
7405 | } | |
7406 | ||
7407 | ||
7408 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
7409 | order as their linked sections. Returns false if this could not be done | |
7410 | because an output section includes both ordered and unordered | |
7411 | sections. Ideally we'd do this in the linker proper. */ | |
7412 | ||
7413 | static bfd_boolean | |
7414 | elf_fixup_link_order (bfd *abfd, asection *o) | |
7415 | { | |
7416 | int seen_linkorder; | |
7417 | int seen_other; | |
7418 | int n; | |
7419 | struct bfd_link_order *p; | |
7420 | bfd *sub; | |
7421 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7422 | int elfsec; | |
7423 | struct bfd_link_order **sections; | |
7424 | asection *s; | |
7425 | bfd_vma offset; | |
7426 | ||
7427 | seen_other = 0; | |
7428 | seen_linkorder = 0; | |
7429 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7430 | { | |
7431 | if (p->type == bfd_indirect_link_order | |
7432 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7433 | == bfd_target_elf_flavour) | |
7434 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7435 | { | |
7436 | s = p->u.indirect.section; | |
7437 | elfsec = _bfd_elf_section_from_bfd_section (sub, s); | |
7438 | if (elfsec != -1 | |
7439 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) | |
7440 | seen_linkorder++; | |
7441 | else | |
7442 | seen_other++; | |
7443 | } | |
7444 | else | |
7445 | seen_other++; | |
7446 | } | |
7447 | ||
7448 | if (!seen_linkorder) | |
7449 | return TRUE; | |
7450 | ||
7451 | if (seen_other && seen_linkorder) | |
08ccf96b | 7452 | { |
d003868e AM |
7453 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), |
7454 | o); | |
08ccf96b L |
7455 | bfd_set_error (bfd_error_bad_value); |
7456 | return FALSE; | |
7457 | } | |
0b52efa6 PB |
7458 | |
7459 | sections = (struct bfd_link_order **) | |
7460 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
7461 | seen_linkorder = 0; | |
7462 | ||
7463 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7464 | { | |
7465 | sections[seen_linkorder++] = p; | |
7466 | } | |
7467 | /* Sort the input sections in the order of their linked section. */ | |
7468 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
7469 | compare_link_order); | |
7470 | ||
7471 | /* Change the offsets of the sections. */ | |
7472 | offset = 0; | |
7473 | for (n = 0; n < seen_linkorder; n++) | |
7474 | { | |
7475 | s = sections[n]->u.indirect.section; | |
7476 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
7477 | s->output_offset = offset; | |
7478 | sections[n]->offset = offset; | |
7479 | offset += sections[n]->size; | |
7480 | } | |
7481 | ||
7482 | return TRUE; | |
7483 | } | |
7484 | ||
7485 | ||
c152c796 AM |
7486 | /* Do the final step of an ELF link. */ |
7487 | ||
7488 | bfd_boolean | |
7489 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7490 | { | |
7491 | bfd_boolean dynamic; | |
7492 | bfd_boolean emit_relocs; | |
7493 | bfd *dynobj; | |
7494 | struct elf_final_link_info finfo; | |
7495 | register asection *o; | |
7496 | register struct bfd_link_order *p; | |
7497 | register bfd *sub; | |
7498 | bfd_size_type max_contents_size; | |
7499 | bfd_size_type max_external_reloc_size; | |
7500 | bfd_size_type max_internal_reloc_count; | |
7501 | bfd_size_type max_sym_count; | |
7502 | bfd_size_type max_sym_shndx_count; | |
7503 | file_ptr off; | |
7504 | Elf_Internal_Sym elfsym; | |
7505 | unsigned int i; | |
7506 | Elf_Internal_Shdr *symtab_hdr; | |
7507 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7508 | Elf_Internal_Shdr *symstrtab_hdr; | |
7509 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7510 | struct elf_outext_info eoinfo; | |
7511 | bfd_boolean merged; | |
7512 | size_t relativecount = 0; | |
7513 | asection *reldyn = 0; | |
7514 | bfd_size_type amt; | |
7515 | ||
7516 | if (! is_elf_hash_table (info->hash)) | |
7517 | return FALSE; | |
7518 | ||
7519 | if (info->shared) | |
7520 | abfd->flags |= DYNAMIC; | |
7521 | ||
7522 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7523 | dynobj = elf_hash_table (info)->dynobj; | |
7524 | ||
7525 | emit_relocs = (info->relocatable | |
7526 | || info->emitrelocations | |
7527 | || bed->elf_backend_emit_relocs); | |
7528 | ||
7529 | finfo.info = info; | |
7530 | finfo.output_bfd = abfd; | |
7531 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7532 | if (finfo.symstrtab == NULL) | |
7533 | return FALSE; | |
7534 | ||
7535 | if (! dynamic) | |
7536 | { | |
7537 | finfo.dynsym_sec = NULL; | |
7538 | finfo.hash_sec = NULL; | |
7539 | finfo.symver_sec = NULL; | |
7540 | } | |
7541 | else | |
7542 | { | |
7543 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7544 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7545 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7546 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7547 | /* Note that it is OK if symver_sec is NULL. */ | |
7548 | } | |
7549 | ||
7550 | finfo.contents = NULL; | |
7551 | finfo.external_relocs = NULL; | |
7552 | finfo.internal_relocs = NULL; | |
7553 | finfo.external_syms = NULL; | |
7554 | finfo.locsym_shndx = NULL; | |
7555 | finfo.internal_syms = NULL; | |
7556 | finfo.indices = NULL; | |
7557 | finfo.sections = NULL; | |
7558 | finfo.symbuf = NULL; | |
7559 | finfo.symshndxbuf = NULL; | |
7560 | finfo.symbuf_count = 0; | |
7561 | finfo.shndxbuf_size = 0; | |
7562 | ||
7563 | /* Count up the number of relocations we will output for each output | |
7564 | section, so that we know the sizes of the reloc sections. We | |
7565 | also figure out some maximum sizes. */ | |
7566 | max_contents_size = 0; | |
7567 | max_external_reloc_size = 0; | |
7568 | max_internal_reloc_count = 0; | |
7569 | max_sym_count = 0; | |
7570 | max_sym_shndx_count = 0; | |
7571 | merged = FALSE; | |
7572 | for (o = abfd->sections; o != NULL; o = o->next) | |
7573 | { | |
7574 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7575 | o->reloc_count = 0; | |
7576 | ||
7577 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7578 | { | |
7579 | unsigned int reloc_count = 0; | |
7580 | struct bfd_elf_section_data *esdi = NULL; | |
7581 | unsigned int *rel_count1; | |
7582 | ||
7583 | if (p->type == bfd_section_reloc_link_order | |
7584 | || p->type == bfd_symbol_reloc_link_order) | |
7585 | reloc_count = 1; | |
7586 | else if (p->type == bfd_indirect_link_order) | |
7587 | { | |
7588 | asection *sec; | |
7589 | ||
7590 | sec = p->u.indirect.section; | |
7591 | esdi = elf_section_data (sec); | |
7592 | ||
7593 | /* Mark all sections which are to be included in the | |
7594 | link. This will normally be every section. We need | |
7595 | to do this so that we can identify any sections which | |
7596 | the linker has decided to not include. */ | |
7597 | sec->linker_mark = TRUE; | |
7598 | ||
7599 | if (sec->flags & SEC_MERGE) | |
7600 | merged = TRUE; | |
7601 | ||
7602 | if (info->relocatable || info->emitrelocations) | |
7603 | reloc_count = sec->reloc_count; | |
7604 | else if (bed->elf_backend_count_relocs) | |
7605 | { | |
7606 | Elf_Internal_Rela * relocs; | |
7607 | ||
7608 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7609 | info->keep_memory); | |
7610 | ||
7611 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7612 | ||
7613 | if (elf_section_data (o)->relocs != relocs) | |
7614 | free (relocs); | |
7615 | } | |
7616 | ||
eea6121a AM |
7617 | if (sec->rawsize > max_contents_size) |
7618 | max_contents_size = sec->rawsize; | |
7619 | if (sec->size > max_contents_size) | |
7620 | max_contents_size = sec->size; | |
c152c796 AM |
7621 | |
7622 | /* We are interested in just local symbols, not all | |
7623 | symbols. */ | |
7624 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7625 | && (sec->owner->flags & DYNAMIC) == 0) | |
7626 | { | |
7627 | size_t sym_count; | |
7628 | ||
7629 | if (elf_bad_symtab (sec->owner)) | |
7630 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7631 | / bed->s->sizeof_sym); | |
7632 | else | |
7633 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7634 | ||
7635 | if (sym_count > max_sym_count) | |
7636 | max_sym_count = sym_count; | |
7637 | ||
7638 | if (sym_count > max_sym_shndx_count | |
7639 | && elf_symtab_shndx (sec->owner) != 0) | |
7640 | max_sym_shndx_count = sym_count; | |
7641 | ||
7642 | if ((sec->flags & SEC_RELOC) != 0) | |
7643 | { | |
7644 | size_t ext_size; | |
7645 | ||
7646 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7647 | if (ext_size > max_external_reloc_size) | |
7648 | max_external_reloc_size = ext_size; | |
7649 | if (sec->reloc_count > max_internal_reloc_count) | |
7650 | max_internal_reloc_count = sec->reloc_count; | |
7651 | } | |
7652 | } | |
7653 | } | |
7654 | ||
7655 | if (reloc_count == 0) | |
7656 | continue; | |
7657 | ||
7658 | o->reloc_count += reloc_count; | |
7659 | ||
7660 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7661 | To support this curious ABI we keep reloc counts in | |
7662 | elf_section_data too. We must be careful to add the | |
7663 | relocations from the input section to the right output | |
7664 | count. FIXME: Get rid of one count. We have | |
7665 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7666 | rel_count1 = &esdo->rel_count; | |
7667 | if (esdi != NULL) | |
7668 | { | |
7669 | bfd_boolean same_size; | |
7670 | bfd_size_type entsize1; | |
7671 | ||
7672 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7673 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7674 | || entsize1 == bed->s->sizeof_rela); | |
7675 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7676 | ||
7677 | if (!same_size) | |
7678 | rel_count1 = &esdo->rel_count2; | |
7679 | ||
7680 | if (esdi->rel_hdr2 != NULL) | |
7681 | { | |
7682 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7683 | unsigned int alt_count; | |
7684 | unsigned int *rel_count2; | |
7685 | ||
7686 | BFD_ASSERT (entsize2 != entsize1 | |
7687 | && (entsize2 == bed->s->sizeof_rel | |
7688 | || entsize2 == bed->s->sizeof_rela)); | |
7689 | ||
7690 | rel_count2 = &esdo->rel_count2; | |
7691 | if (!same_size) | |
7692 | rel_count2 = &esdo->rel_count; | |
7693 | ||
7694 | /* The following is probably too simplistic if the | |
7695 | backend counts output relocs unusually. */ | |
7696 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7697 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7698 | *rel_count2 += alt_count; | |
7699 | reloc_count -= alt_count; | |
7700 | } | |
7701 | } | |
7702 | *rel_count1 += reloc_count; | |
7703 | } | |
7704 | ||
7705 | if (o->reloc_count > 0) | |
7706 | o->flags |= SEC_RELOC; | |
7707 | else | |
7708 | { | |
7709 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7710 | set it (this is probably a bug) and if it is set | |
7711 | assign_section_numbers will create a reloc section. */ | |
7712 | o->flags &=~ SEC_RELOC; | |
7713 | } | |
7714 | ||
7715 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7716 | zero. This is done in elf_fake_sections as well, but forcing | |
7717 | the VMA to 0 here will ensure that relocs against these | |
7718 | sections are handled correctly. */ | |
7719 | if ((o->flags & SEC_ALLOC) == 0 | |
7720 | && ! o->user_set_vma) | |
7721 | o->vma = 0; | |
7722 | } | |
7723 | ||
7724 | if (! info->relocatable && merged) | |
7725 | elf_link_hash_traverse (elf_hash_table (info), | |
7726 | _bfd_elf_link_sec_merge_syms, abfd); | |
7727 | ||
7728 | /* Figure out the file positions for everything but the symbol table | |
7729 | and the relocs. We set symcount to force assign_section_numbers | |
7730 | to create a symbol table. */ | |
7731 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7732 | BFD_ASSERT (! abfd->output_has_begun); | |
7733 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7734 | goto error_return; | |
7735 | ||
ee75fd95 | 7736 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
7737 | for (o = abfd->sections; o != NULL; o = o->next) |
7738 | { | |
7739 | if ((o->flags & SEC_RELOC) != 0) | |
7740 | { | |
7741 | if (!(_bfd_elf_link_size_reloc_section | |
7742 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
7743 | goto error_return; | |
7744 | ||
7745 | if (elf_section_data (o)->rel_hdr2 | |
7746 | && !(_bfd_elf_link_size_reloc_section | |
7747 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
7748 | goto error_return; | |
7749 | } | |
7750 | ||
7751 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
7752 | to count upwards while actually outputting the relocations. */ | |
7753 | elf_section_data (o)->rel_count = 0; | |
7754 | elf_section_data (o)->rel_count2 = 0; | |
7755 | } | |
7756 | ||
7757 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
7758 | ||
7759 | /* We have now assigned file positions for all the sections except | |
7760 | .symtab and .strtab. We start the .symtab section at the current | |
7761 | file position, and write directly to it. We build the .strtab | |
7762 | section in memory. */ | |
7763 | bfd_get_symcount (abfd) = 0; | |
7764 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7765 | /* sh_name is set in prep_headers. */ | |
7766 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7767 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
7768 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7769 | /* sh_link is set in assign_section_numbers. */ | |
7770 | /* sh_info is set below. */ | |
7771 | /* sh_offset is set just below. */ | |
7772 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
7773 | ||
7774 | off = elf_tdata (abfd)->next_file_pos; | |
7775 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
7776 | ||
7777 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
7778 | incorrect. We do not yet know the size of the .symtab section. | |
7779 | We correct next_file_pos below, after we do know the size. */ | |
7780 | ||
7781 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
7782 | continuously seeking to the right position in the file. */ | |
7783 | if (! info->keep_memory || max_sym_count < 20) | |
7784 | finfo.symbuf_size = 20; | |
7785 | else | |
7786 | finfo.symbuf_size = max_sym_count; | |
7787 | amt = finfo.symbuf_size; | |
7788 | amt *= bed->s->sizeof_sym; | |
7789 | finfo.symbuf = bfd_malloc (amt); | |
7790 | if (finfo.symbuf == NULL) | |
7791 | goto error_return; | |
7792 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
7793 | { | |
7794 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
7795 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
7796 | finfo.shndxbuf_size = amt; | |
7797 | amt *= sizeof (Elf_External_Sym_Shndx); | |
7798 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
7799 | if (finfo.symshndxbuf == NULL) | |
7800 | goto error_return; | |
7801 | } | |
7802 | ||
7803 | /* Start writing out the symbol table. The first symbol is always a | |
7804 | dummy symbol. */ | |
7805 | if (info->strip != strip_all | |
7806 | || emit_relocs) | |
7807 | { | |
7808 | elfsym.st_value = 0; | |
7809 | elfsym.st_size = 0; | |
7810 | elfsym.st_info = 0; | |
7811 | elfsym.st_other = 0; | |
7812 | elfsym.st_shndx = SHN_UNDEF; | |
7813 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
7814 | NULL)) | |
7815 | goto error_return; | |
7816 | } | |
7817 | ||
7818 | #if 0 | |
7819 | /* Some standard ELF linkers do this, but we don't because it causes | |
7820 | bootstrap comparison failures. */ | |
7821 | /* Output a file symbol for the output file as the second symbol. | |
7822 | We output this even if we are discarding local symbols, although | |
7823 | I'm not sure if this is correct. */ | |
7824 | elfsym.st_value = 0; | |
7825 | elfsym.st_size = 0; | |
7826 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7827 | elfsym.st_other = 0; | |
7828 | elfsym.st_shndx = SHN_ABS; | |
7829 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
7830 | &elfsym, bfd_abs_section_ptr, NULL)) | |
7831 | goto error_return; | |
7832 | #endif | |
7833 | ||
7834 | /* Output a symbol for each section. We output these even if we are | |
7835 | discarding local symbols, since they are used for relocs. These | |
7836 | symbols have no names. We store the index of each one in the | |
7837 | index field of the section, so that we can find it again when | |
7838 | outputting relocs. */ | |
7839 | if (info->strip != strip_all | |
7840 | || emit_relocs) | |
7841 | { | |
7842 | elfsym.st_size = 0; | |
7843 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7844 | elfsym.st_other = 0; | |
7845 | for (i = 1; i < elf_numsections (abfd); i++) | |
7846 | { | |
7847 | o = bfd_section_from_elf_index (abfd, i); | |
7848 | if (o != NULL) | |
7849 | o->target_index = bfd_get_symcount (abfd); | |
7850 | elfsym.st_shndx = i; | |
7851 | if (info->relocatable || o == NULL) | |
7852 | elfsym.st_value = 0; | |
7853 | else | |
7854 | elfsym.st_value = o->vma; | |
7855 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
7856 | goto error_return; | |
7857 | if (i == SHN_LORESERVE - 1) | |
7858 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
7859 | } | |
7860 | } | |
7861 | ||
7862 | /* Allocate some memory to hold information read in from the input | |
7863 | files. */ | |
7864 | if (max_contents_size != 0) | |
7865 | { | |
7866 | finfo.contents = bfd_malloc (max_contents_size); | |
7867 | if (finfo.contents == NULL) | |
7868 | goto error_return; | |
7869 | } | |
7870 | ||
7871 | if (max_external_reloc_size != 0) | |
7872 | { | |
7873 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
7874 | if (finfo.external_relocs == NULL) | |
7875 | goto error_return; | |
7876 | } | |
7877 | ||
7878 | if (max_internal_reloc_count != 0) | |
7879 | { | |
7880 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
7881 | amt *= sizeof (Elf_Internal_Rela); | |
7882 | finfo.internal_relocs = bfd_malloc (amt); | |
7883 | if (finfo.internal_relocs == NULL) | |
7884 | goto error_return; | |
7885 | } | |
7886 | ||
7887 | if (max_sym_count != 0) | |
7888 | { | |
7889 | amt = max_sym_count * bed->s->sizeof_sym; | |
7890 | finfo.external_syms = bfd_malloc (amt); | |
7891 | if (finfo.external_syms == NULL) | |
7892 | goto error_return; | |
7893 | ||
7894 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
7895 | finfo.internal_syms = bfd_malloc (amt); | |
7896 | if (finfo.internal_syms == NULL) | |
7897 | goto error_return; | |
7898 | ||
7899 | amt = max_sym_count * sizeof (long); | |
7900 | finfo.indices = bfd_malloc (amt); | |
7901 | if (finfo.indices == NULL) | |
7902 | goto error_return; | |
7903 | ||
7904 | amt = max_sym_count * sizeof (asection *); | |
7905 | finfo.sections = bfd_malloc (amt); | |
7906 | if (finfo.sections == NULL) | |
7907 | goto error_return; | |
7908 | } | |
7909 | ||
7910 | if (max_sym_shndx_count != 0) | |
7911 | { | |
7912 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
7913 | finfo.locsym_shndx = bfd_malloc (amt); | |
7914 | if (finfo.locsym_shndx == NULL) | |
7915 | goto error_return; | |
7916 | } | |
7917 | ||
7918 | if (elf_hash_table (info)->tls_sec) | |
7919 | { | |
7920 | bfd_vma base, end = 0; | |
7921 | asection *sec; | |
7922 | ||
7923 | for (sec = elf_hash_table (info)->tls_sec; | |
7924 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
7925 | sec = sec->next) | |
7926 | { | |
eea6121a | 7927 | bfd_vma size = sec->size; |
c152c796 AM |
7928 | |
7929 | if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
7930 | { | |
7931 | struct bfd_link_order *o; | |
7932 | ||
7933 | for (o = sec->link_order_head; o != NULL; o = o->next) | |
7934 | if (size < o->offset + o->size) | |
7935 | size = o->offset + o->size; | |
7936 | } | |
7937 | end = sec->vma + size; | |
7938 | } | |
7939 | base = elf_hash_table (info)->tls_sec->vma; | |
7940 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
7941 | elf_hash_table (info)->tls_size = end - base; | |
7942 | } | |
7943 | ||
0b52efa6 PB |
7944 | /* Reorder SHF_LINK_ORDER sections. */ |
7945 | for (o = abfd->sections; o != NULL; o = o->next) | |
7946 | { | |
7947 | if (!elf_fixup_link_order (abfd, o)) | |
7948 | return FALSE; | |
7949 | } | |
7950 | ||
c152c796 AM |
7951 | /* Since ELF permits relocations to be against local symbols, we |
7952 | must have the local symbols available when we do the relocations. | |
7953 | Since we would rather only read the local symbols once, and we | |
7954 | would rather not keep them in memory, we handle all the | |
7955 | relocations for a single input file at the same time. | |
7956 | ||
7957 | Unfortunately, there is no way to know the total number of local | |
7958 | symbols until we have seen all of them, and the local symbol | |
7959 | indices precede the global symbol indices. This means that when | |
7960 | we are generating relocatable output, and we see a reloc against | |
7961 | a global symbol, we can not know the symbol index until we have | |
7962 | finished examining all the local symbols to see which ones we are | |
7963 | going to output. To deal with this, we keep the relocations in | |
7964 | memory, and don't output them until the end of the link. This is | |
7965 | an unfortunate waste of memory, but I don't see a good way around | |
7966 | it. Fortunately, it only happens when performing a relocatable | |
7967 | link, which is not the common case. FIXME: If keep_memory is set | |
7968 | we could write the relocs out and then read them again; I don't | |
7969 | know how bad the memory loss will be. */ | |
7970 | ||
7971 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
7972 | sub->output_has_begun = FALSE; | |
7973 | for (o = abfd->sections; o != NULL; o = o->next) | |
7974 | { | |
7975 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7976 | { | |
7977 | if (p->type == bfd_indirect_link_order | |
7978 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7979 | == bfd_target_elf_flavour) | |
7980 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7981 | { | |
7982 | if (! sub->output_has_begun) | |
7983 | { | |
7984 | if (! elf_link_input_bfd (&finfo, sub)) | |
7985 | goto error_return; | |
7986 | sub->output_has_begun = TRUE; | |
7987 | } | |
7988 | } | |
7989 | else if (p->type == bfd_section_reloc_link_order | |
7990 | || p->type == bfd_symbol_reloc_link_order) | |
7991 | { | |
7992 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
7993 | goto error_return; | |
7994 | } | |
7995 | else | |
7996 | { | |
7997 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
7998 | goto error_return; | |
7999 | } | |
8000 | } | |
8001 | } | |
8002 | ||
8003 | /* Output any global symbols that got converted to local in a | |
8004 | version script or due to symbol visibility. We do this in a | |
8005 | separate step since ELF requires all local symbols to appear | |
8006 | prior to any global symbols. FIXME: We should only do this if | |
8007 | some global symbols were, in fact, converted to become local. | |
8008 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
8009 | eoinfo.failed = FALSE; | |
8010 | eoinfo.finfo = &finfo; | |
8011 | eoinfo.localsyms = TRUE; | |
8012 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8013 | &eoinfo); | |
8014 | if (eoinfo.failed) | |
8015 | return FALSE; | |
8016 | ||
8017 | /* That wrote out all the local symbols. Finish up the symbol table | |
8018 | with the global symbols. Even if we want to strip everything we | |
8019 | can, we still need to deal with those global symbols that got | |
8020 | converted to local in a version script. */ | |
8021 | ||
8022 | /* The sh_info field records the index of the first non local symbol. */ | |
8023 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
8024 | ||
8025 | if (dynamic | |
8026 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
8027 | { | |
8028 | Elf_Internal_Sym sym; | |
8029 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
8030 | long last_local = 0; | |
8031 | ||
8032 | /* Write out the section symbols for the output sections. */ | |
8033 | if (info->shared) | |
8034 | { | |
8035 | asection *s; | |
8036 | ||
8037 | sym.st_size = 0; | |
8038 | sym.st_name = 0; | |
8039 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8040 | sym.st_other = 0; | |
8041 | ||
8042 | for (s = abfd->sections; s != NULL; s = s->next) | |
8043 | { | |
8044 | int indx; | |
8045 | bfd_byte *dest; | |
8046 | long dynindx; | |
8047 | ||
c152c796 | 8048 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
8049 | if (dynindx <= 0) |
8050 | continue; | |
8051 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
8052 | BFD_ASSERT (indx > 0); |
8053 | sym.st_shndx = indx; | |
8054 | sym.st_value = s->vma; | |
8055 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
8056 | if (last_local < dynindx) |
8057 | last_local = dynindx; | |
c152c796 AM |
8058 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
8059 | } | |
c152c796 AM |
8060 | } |
8061 | ||
8062 | /* Write out the local dynsyms. */ | |
8063 | if (elf_hash_table (info)->dynlocal) | |
8064 | { | |
8065 | struct elf_link_local_dynamic_entry *e; | |
8066 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
8067 | { | |
8068 | asection *s; | |
8069 | bfd_byte *dest; | |
8070 | ||
8071 | sym.st_size = e->isym.st_size; | |
8072 | sym.st_other = e->isym.st_other; | |
8073 | ||
8074 | /* Copy the internal symbol as is. | |
8075 | Note that we saved a word of storage and overwrote | |
8076 | the original st_name with the dynstr_index. */ | |
8077 | sym = e->isym; | |
8078 | ||
8079 | if (e->isym.st_shndx != SHN_UNDEF | |
8080 | && (e->isym.st_shndx < SHN_LORESERVE | |
8081 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
8082 | { | |
8083 | s = bfd_section_from_elf_index (e->input_bfd, | |
8084 | e->isym.st_shndx); | |
8085 | ||
8086 | sym.st_shndx = | |
8087 | elf_section_data (s->output_section)->this_idx; | |
8088 | sym.st_value = (s->output_section->vma | |
8089 | + s->output_offset | |
8090 | + e->isym.st_value); | |
8091 | } | |
8092 | ||
8093 | if (last_local < e->dynindx) | |
8094 | last_local = e->dynindx; | |
8095 | ||
8096 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
8097 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
8098 | } | |
8099 | } | |
8100 | ||
8101 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8102 | last_local + 1; | |
8103 | } | |
8104 | ||
8105 | /* We get the global symbols from the hash table. */ | |
8106 | eoinfo.failed = FALSE; | |
8107 | eoinfo.localsyms = FALSE; | |
8108 | eoinfo.finfo = &finfo; | |
8109 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8110 | &eoinfo); | |
8111 | if (eoinfo.failed) | |
8112 | return FALSE; | |
8113 | ||
8114 | /* If backend needs to output some symbols not present in the hash | |
8115 | table, do it now. */ | |
8116 | if (bed->elf_backend_output_arch_syms) | |
8117 | { | |
8118 | typedef bfd_boolean (*out_sym_func) | |
8119 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8120 | struct elf_link_hash_entry *); | |
8121 | ||
8122 | if (! ((*bed->elf_backend_output_arch_syms) | |
8123 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8124 | return FALSE; | |
8125 | } | |
8126 | ||
8127 | /* Flush all symbols to the file. */ | |
8128 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8129 | return FALSE; | |
8130 | ||
8131 | /* Now we know the size of the symtab section. */ | |
8132 | off += symtab_hdr->sh_size; | |
8133 | ||
8134 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8135 | if (symtab_shndx_hdr->sh_name != 0) | |
8136 | { | |
8137 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8138 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8139 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8140 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8141 | symtab_shndx_hdr->sh_size = amt; | |
8142 | ||
8143 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8144 | off, TRUE); | |
8145 | ||
8146 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8147 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8148 | return FALSE; | |
8149 | } | |
8150 | ||
8151 | ||
8152 | /* Finish up and write out the symbol string table (.strtab) | |
8153 | section. */ | |
8154 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8155 | /* sh_name was set in prep_headers. */ | |
8156 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8157 | symstrtab_hdr->sh_flags = 0; | |
8158 | symstrtab_hdr->sh_addr = 0; | |
8159 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8160 | symstrtab_hdr->sh_entsize = 0; | |
8161 | symstrtab_hdr->sh_link = 0; | |
8162 | symstrtab_hdr->sh_info = 0; | |
8163 | /* sh_offset is set just below. */ | |
8164 | symstrtab_hdr->sh_addralign = 1; | |
8165 | ||
8166 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8167 | elf_tdata (abfd)->next_file_pos = off; | |
8168 | ||
8169 | if (bfd_get_symcount (abfd) > 0) | |
8170 | { | |
8171 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8172 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8173 | return FALSE; | |
8174 | } | |
8175 | ||
8176 | /* Adjust the relocs to have the correct symbol indices. */ | |
8177 | for (o = abfd->sections; o != NULL; o = o->next) | |
8178 | { | |
8179 | if ((o->flags & SEC_RELOC) == 0) | |
8180 | continue; | |
8181 | ||
8182 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8183 | elf_section_data (o)->rel_count, | |
8184 | elf_section_data (o)->rel_hashes); | |
8185 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8186 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8187 | elf_section_data (o)->rel_count2, | |
8188 | (elf_section_data (o)->rel_hashes | |
8189 | + elf_section_data (o)->rel_count)); | |
8190 | ||
8191 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
8192 | trying to swap the relocs out itself. */ | |
8193 | o->reloc_count = 0; | |
8194 | } | |
8195 | ||
8196 | if (dynamic && info->combreloc && dynobj != NULL) | |
8197 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
8198 | ||
8199 | /* If we are linking against a dynamic object, or generating a | |
8200 | shared library, finish up the dynamic linking information. */ | |
8201 | if (dynamic) | |
8202 | { | |
8203 | bfd_byte *dyncon, *dynconend; | |
8204 | ||
8205 | /* Fix up .dynamic entries. */ | |
8206 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8207 | BFD_ASSERT (o != NULL); | |
8208 | ||
8209 | dyncon = o->contents; | |
eea6121a | 8210 | dynconend = o->contents + o->size; |
c152c796 AM |
8211 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
8212 | { | |
8213 | Elf_Internal_Dyn dyn; | |
8214 | const char *name; | |
8215 | unsigned int type; | |
8216 | ||
8217 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
8218 | ||
8219 | switch (dyn.d_tag) | |
8220 | { | |
8221 | default: | |
8222 | continue; | |
8223 | case DT_NULL: | |
8224 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
8225 | { | |
8226 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
8227 | { | |
8228 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
8229 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
8230 | default: continue; | |
8231 | } | |
8232 | dyn.d_un.d_val = relativecount; | |
8233 | relativecount = 0; | |
8234 | break; | |
8235 | } | |
8236 | continue; | |
8237 | ||
8238 | case DT_INIT: | |
8239 | name = info->init_function; | |
8240 | goto get_sym; | |
8241 | case DT_FINI: | |
8242 | name = info->fini_function; | |
8243 | get_sym: | |
8244 | { | |
8245 | struct elf_link_hash_entry *h; | |
8246 | ||
8247 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
8248 | FALSE, FALSE, TRUE); | |
8249 | if (h != NULL | |
8250 | && (h->root.type == bfd_link_hash_defined | |
8251 | || h->root.type == bfd_link_hash_defweak)) | |
8252 | { | |
8253 | dyn.d_un.d_val = h->root.u.def.value; | |
8254 | o = h->root.u.def.section; | |
8255 | if (o->output_section != NULL) | |
8256 | dyn.d_un.d_val += (o->output_section->vma | |
8257 | + o->output_offset); | |
8258 | else | |
8259 | { | |
8260 | /* The symbol is imported from another shared | |
8261 | library and does not apply to this one. */ | |
8262 | dyn.d_un.d_val = 0; | |
8263 | } | |
8264 | break; | |
8265 | } | |
8266 | } | |
8267 | continue; | |
8268 | ||
8269 | case DT_PREINIT_ARRAYSZ: | |
8270 | name = ".preinit_array"; | |
8271 | goto get_size; | |
8272 | case DT_INIT_ARRAYSZ: | |
8273 | name = ".init_array"; | |
8274 | goto get_size; | |
8275 | case DT_FINI_ARRAYSZ: | |
8276 | name = ".fini_array"; | |
8277 | get_size: | |
8278 | o = bfd_get_section_by_name (abfd, name); | |
8279 | if (o == NULL) | |
8280 | { | |
8281 | (*_bfd_error_handler) | |
d003868e | 8282 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8283 | goto error_return; |
8284 | } | |
eea6121a | 8285 | if (o->size == 0) |
c152c796 AM |
8286 | (*_bfd_error_handler) |
8287 | (_("warning: %s section has zero size"), name); | |
eea6121a | 8288 | dyn.d_un.d_val = o->size; |
c152c796 AM |
8289 | break; |
8290 | ||
8291 | case DT_PREINIT_ARRAY: | |
8292 | name = ".preinit_array"; | |
8293 | goto get_vma; | |
8294 | case DT_INIT_ARRAY: | |
8295 | name = ".init_array"; | |
8296 | goto get_vma; | |
8297 | case DT_FINI_ARRAY: | |
8298 | name = ".fini_array"; | |
8299 | goto get_vma; | |
8300 | ||
8301 | case DT_HASH: | |
8302 | name = ".hash"; | |
8303 | goto get_vma; | |
8304 | case DT_STRTAB: | |
8305 | name = ".dynstr"; | |
8306 | goto get_vma; | |
8307 | case DT_SYMTAB: | |
8308 | name = ".dynsym"; | |
8309 | goto get_vma; | |
8310 | case DT_VERDEF: | |
8311 | name = ".gnu.version_d"; | |
8312 | goto get_vma; | |
8313 | case DT_VERNEED: | |
8314 | name = ".gnu.version_r"; | |
8315 | goto get_vma; | |
8316 | case DT_VERSYM: | |
8317 | name = ".gnu.version"; | |
8318 | get_vma: | |
8319 | o = bfd_get_section_by_name (abfd, name); | |
8320 | if (o == NULL) | |
8321 | { | |
8322 | (*_bfd_error_handler) | |
d003868e | 8323 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8324 | goto error_return; |
8325 | } | |
8326 | dyn.d_un.d_ptr = o->vma; | |
8327 | break; | |
8328 | ||
8329 | case DT_REL: | |
8330 | case DT_RELA: | |
8331 | case DT_RELSZ: | |
8332 | case DT_RELASZ: | |
8333 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
8334 | type = SHT_REL; | |
8335 | else | |
8336 | type = SHT_RELA; | |
8337 | dyn.d_un.d_val = 0; | |
8338 | for (i = 1; i < elf_numsections (abfd); i++) | |
8339 | { | |
8340 | Elf_Internal_Shdr *hdr; | |
8341 | ||
8342 | hdr = elf_elfsections (abfd)[i]; | |
8343 | if (hdr->sh_type == type | |
8344 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
8345 | { | |
8346 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
8347 | dyn.d_un.d_val += hdr->sh_size; | |
8348 | else | |
8349 | { | |
8350 | if (dyn.d_un.d_val == 0 | |
8351 | || hdr->sh_addr < dyn.d_un.d_val) | |
8352 | dyn.d_un.d_val = hdr->sh_addr; | |
8353 | } | |
8354 | } | |
8355 | } | |
8356 | break; | |
8357 | } | |
8358 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
8359 | } | |
8360 | } | |
8361 | ||
8362 | /* If we have created any dynamic sections, then output them. */ | |
8363 | if (dynobj != NULL) | |
8364 | { | |
8365 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
8366 | goto error_return; | |
8367 | ||
8368 | for (o = dynobj->sections; o != NULL; o = o->next) | |
8369 | { | |
8370 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8371 | || o->size == 0 |
c152c796 AM |
8372 | || o->output_section == bfd_abs_section_ptr) |
8373 | continue; | |
8374 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
8375 | { | |
8376 | /* At this point, we are only interested in sections | |
8377 | created by _bfd_elf_link_create_dynamic_sections. */ | |
8378 | continue; | |
8379 | } | |
3722b82f AM |
8380 | if (elf_hash_table (info)->stab_info.stabstr == o) |
8381 | continue; | |
eea6121a AM |
8382 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
8383 | continue; | |
c152c796 AM |
8384 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
8385 | != SHT_STRTAB) | |
8386 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
8387 | { | |
8388 | if (! bfd_set_section_contents (abfd, o->output_section, | |
8389 | o->contents, | |
8390 | (file_ptr) o->output_offset, | |
eea6121a | 8391 | o->size)) |
c152c796 AM |
8392 | goto error_return; |
8393 | } | |
8394 | else | |
8395 | { | |
8396 | /* The contents of the .dynstr section are actually in a | |
8397 | stringtab. */ | |
8398 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
8399 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
8400 | || ! _bfd_elf_strtab_emit (abfd, | |
8401 | elf_hash_table (info)->dynstr)) | |
8402 | goto error_return; | |
8403 | } | |
8404 | } | |
8405 | } | |
8406 | ||
8407 | if (info->relocatable) | |
8408 | { | |
8409 | bfd_boolean failed = FALSE; | |
8410 | ||
8411 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
8412 | if (failed) | |
8413 | goto error_return; | |
8414 | } | |
8415 | ||
8416 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 8417 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
8418 | { |
8419 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8420 | goto error_return; | |
8421 | } | |
8422 | ||
8423 | if (info->eh_frame_hdr) | |
8424 | { | |
8425 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8426 | goto error_return; | |
8427 | } | |
8428 | ||
8429 | if (finfo.symstrtab != NULL) | |
8430 | _bfd_stringtab_free (finfo.symstrtab); | |
8431 | if (finfo.contents != NULL) | |
8432 | free (finfo.contents); | |
8433 | if (finfo.external_relocs != NULL) | |
8434 | free (finfo.external_relocs); | |
8435 | if (finfo.internal_relocs != NULL) | |
8436 | free (finfo.internal_relocs); | |
8437 | if (finfo.external_syms != NULL) | |
8438 | free (finfo.external_syms); | |
8439 | if (finfo.locsym_shndx != NULL) | |
8440 | free (finfo.locsym_shndx); | |
8441 | if (finfo.internal_syms != NULL) | |
8442 | free (finfo.internal_syms); | |
8443 | if (finfo.indices != NULL) | |
8444 | free (finfo.indices); | |
8445 | if (finfo.sections != NULL) | |
8446 | free (finfo.sections); | |
8447 | if (finfo.symbuf != NULL) | |
8448 | free (finfo.symbuf); | |
8449 | if (finfo.symshndxbuf != NULL) | |
8450 | free (finfo.symshndxbuf); | |
8451 | for (o = abfd->sections; o != NULL; o = o->next) | |
8452 | { | |
8453 | if ((o->flags & SEC_RELOC) != 0 | |
8454 | && elf_section_data (o)->rel_hashes != NULL) | |
8455 | free (elf_section_data (o)->rel_hashes); | |
8456 | } | |
8457 | ||
8458 | elf_tdata (abfd)->linker = TRUE; | |
8459 | ||
8460 | return TRUE; | |
8461 | ||
8462 | error_return: | |
8463 | if (finfo.symstrtab != NULL) | |
8464 | _bfd_stringtab_free (finfo.symstrtab); | |
8465 | if (finfo.contents != NULL) | |
8466 | free (finfo.contents); | |
8467 | if (finfo.external_relocs != NULL) | |
8468 | free (finfo.external_relocs); | |
8469 | if (finfo.internal_relocs != NULL) | |
8470 | free (finfo.internal_relocs); | |
8471 | if (finfo.external_syms != NULL) | |
8472 | free (finfo.external_syms); | |
8473 | if (finfo.locsym_shndx != NULL) | |
8474 | free (finfo.locsym_shndx); | |
8475 | if (finfo.internal_syms != NULL) | |
8476 | free (finfo.internal_syms); | |
8477 | if (finfo.indices != NULL) | |
8478 | free (finfo.indices); | |
8479 | if (finfo.sections != NULL) | |
8480 | free (finfo.sections); | |
8481 | if (finfo.symbuf != NULL) | |
8482 | free (finfo.symbuf); | |
8483 | if (finfo.symshndxbuf != NULL) | |
8484 | free (finfo.symshndxbuf); | |
8485 | for (o = abfd->sections; o != NULL; o = o->next) | |
8486 | { | |
8487 | if ((o->flags & SEC_RELOC) != 0 | |
8488 | && elf_section_data (o)->rel_hashes != NULL) | |
8489 | free (elf_section_data (o)->rel_hashes); | |
8490 | } | |
8491 | ||
8492 | return FALSE; | |
8493 | } | |
8494 | \f | |
8495 | /* Garbage collect unused sections. */ | |
8496 | ||
8497 | /* The mark phase of garbage collection. For a given section, mark | |
8498 | it and any sections in this section's group, and all the sections | |
8499 | which define symbols to which it refers. */ | |
8500 | ||
8501 | typedef asection * (*gc_mark_hook_fn) | |
8502 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8503 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8504 | ||
ccfa59ea AM |
8505 | bfd_boolean |
8506 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
8507 | asection *sec, | |
8508 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
8509 | { |
8510 | bfd_boolean ret; | |
8511 | asection *group_sec; | |
8512 | ||
8513 | sec->gc_mark = 1; | |
8514 | ||
8515 | /* Mark all the sections in the group. */ | |
8516 | group_sec = elf_section_data (sec)->next_in_group; | |
8517 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 8518 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
8519 | return FALSE; |
8520 | ||
8521 | /* Look through the section relocs. */ | |
8522 | ret = TRUE; | |
8523 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) | |
8524 | { | |
8525 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8526 | Elf_Internal_Shdr *symtab_hdr; | |
8527 | struct elf_link_hash_entry **sym_hashes; | |
8528 | size_t nlocsyms; | |
8529 | size_t extsymoff; | |
8530 | bfd *input_bfd = sec->owner; | |
8531 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8532 | Elf_Internal_Sym *isym = NULL; | |
8533 | int r_sym_shift; | |
8534 | ||
8535 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8536 | sym_hashes = elf_sym_hashes (input_bfd); | |
8537 | ||
8538 | /* Read the local symbols. */ | |
8539 | if (elf_bad_symtab (input_bfd)) | |
8540 | { | |
8541 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8542 | extsymoff = 0; | |
8543 | } | |
8544 | else | |
8545 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8546 | ||
8547 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8548 | if (isym == NULL && nlocsyms != 0) | |
8549 | { | |
8550 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8551 | NULL, NULL, NULL); | |
8552 | if (isym == NULL) | |
8553 | return FALSE; | |
8554 | } | |
8555 | ||
8556 | /* Read the relocations. */ | |
8557 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8558 | info->keep_memory); | |
8559 | if (relstart == NULL) | |
8560 | { | |
8561 | ret = FALSE; | |
8562 | goto out1; | |
8563 | } | |
8564 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8565 | ||
8566 | if (bed->s->arch_size == 32) | |
8567 | r_sym_shift = 8; | |
8568 | else | |
8569 | r_sym_shift = 32; | |
8570 | ||
8571 | for (rel = relstart; rel < relend; rel++) | |
8572 | { | |
8573 | unsigned long r_symndx; | |
8574 | asection *rsec; | |
8575 | struct elf_link_hash_entry *h; | |
8576 | ||
8577 | r_symndx = rel->r_info >> r_sym_shift; | |
8578 | if (r_symndx == 0) | |
8579 | continue; | |
8580 | ||
8581 | if (r_symndx >= nlocsyms | |
8582 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8583 | { | |
8584 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
8585 | while (h->root.type == bfd_link_hash_indirect |
8586 | || h->root.type == bfd_link_hash_warning) | |
8587 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
8588 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
8589 | } | |
8590 | else | |
8591 | { | |
8592 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8593 | } | |
8594 | ||
8595 | if (rsec && !rsec->gc_mark) | |
8596 | { | |
8597 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8598 | rsec->gc_mark = 1; | |
ccfa59ea | 8599 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
8600 | { |
8601 | ret = FALSE; | |
8602 | goto out2; | |
8603 | } | |
8604 | } | |
8605 | } | |
8606 | ||
8607 | out2: | |
8608 | if (elf_section_data (sec)->relocs != relstart) | |
8609 | free (relstart); | |
8610 | out1: | |
8611 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8612 | { | |
8613 | if (! info->keep_memory) | |
8614 | free (isym); | |
8615 | else | |
8616 | symtab_hdr->contents = (unsigned char *) isym; | |
8617 | } | |
8618 | } | |
8619 | ||
8620 | return ret; | |
8621 | } | |
8622 | ||
8623 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8624 | ||
8625 | static bfd_boolean | |
8626 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr) | |
8627 | { | |
8628 | int *idx = idxptr; | |
8629 | ||
8630 | if (h->root.type == bfd_link_hash_warning) | |
8631 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8632 | ||
8633 | if (h->dynindx != -1 | |
8634 | && ((h->root.type != bfd_link_hash_defined | |
8635 | && h->root.type != bfd_link_hash_defweak) | |
8636 | || h->root.u.def.section->gc_mark)) | |
8637 | h->dynindx = (*idx)++; | |
8638 | ||
8639 | return TRUE; | |
8640 | } | |
8641 | ||
8642 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8643 | ||
8644 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8645 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8646 | ||
8647 | static bfd_boolean | |
8648 | elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook) | |
8649 | { | |
8650 | bfd *sub; | |
8651 | ||
8652 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8653 | { | |
8654 | asection *o; | |
8655 | ||
8656 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8657 | continue; | |
8658 | ||
8659 | for (o = sub->sections; o != NULL; o = o->next) | |
8660 | { | |
7c2c8505 AM |
8661 | /* Keep debug and special sections. */ |
8662 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
8663 | || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0) | |
c152c796 AM |
8664 | o->gc_mark = 1; |
8665 | ||
8666 | if (o->gc_mark) | |
8667 | continue; | |
8668 | ||
8669 | /* Skip sweeping sections already excluded. */ | |
8670 | if (o->flags & SEC_EXCLUDE) | |
8671 | continue; | |
8672 | ||
8673 | /* Since this is early in the link process, it is simple | |
8674 | to remove a section from the output. */ | |
8675 | o->flags |= SEC_EXCLUDE; | |
8676 | ||
8677 | /* But we also have to update some of the relocation | |
8678 | info we collected before. */ | |
8679 | if (gc_sweep_hook | |
8680 | && (o->flags & SEC_RELOC) && o->reloc_count > 0) | |
8681 | { | |
8682 | Elf_Internal_Rela *internal_relocs; | |
8683 | bfd_boolean r; | |
8684 | ||
8685 | internal_relocs | |
8686 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8687 | info->keep_memory); | |
8688 | if (internal_relocs == NULL) | |
8689 | return FALSE; | |
8690 | ||
8691 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8692 | ||
8693 | if (elf_section_data (o)->relocs != internal_relocs) | |
8694 | free (internal_relocs); | |
8695 | ||
8696 | if (!r) | |
8697 | return FALSE; | |
8698 | } | |
8699 | } | |
8700 | } | |
8701 | ||
8702 | /* Remove the symbols that were in the swept sections from the dynamic | |
8703 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8704 | static symbol table as well? */ | |
8705 | { | |
8706 | int i = 0; | |
8707 | ||
8708 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i); | |
8709 | ||
8710 | elf_hash_table (info)->dynsymcount = i; | |
8711 | } | |
8712 | ||
8713 | return TRUE; | |
8714 | } | |
8715 | ||
8716 | /* Propagate collected vtable information. This is called through | |
8717 | elf_link_hash_traverse. */ | |
8718 | ||
8719 | static bfd_boolean | |
8720 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8721 | { | |
8722 | if (h->root.type == bfd_link_hash_warning) | |
8723 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8724 | ||
8725 | /* Those that are not vtables. */ | |
f6e332e6 | 8726 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8727 | return TRUE; |
8728 | ||
8729 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 8730 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
8731 | return TRUE; |
8732 | ||
8733 | /* If we've already been done, exit. */ | |
f6e332e6 | 8734 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
8735 | return TRUE; |
8736 | ||
8737 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 8738 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 8739 | |
f6e332e6 | 8740 | if (h->vtable->used == NULL) |
c152c796 AM |
8741 | { |
8742 | /* None of this table's entries were referenced. Re-use the | |
8743 | parent's table. */ | |
f6e332e6 AM |
8744 | h->vtable->used = h->vtable->parent->vtable->used; |
8745 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
8746 | } |
8747 | else | |
8748 | { | |
8749 | size_t n; | |
8750 | bfd_boolean *cu, *pu; | |
8751 | ||
8752 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 8753 | cu = h->vtable->used; |
c152c796 | 8754 | cu[-1] = TRUE; |
f6e332e6 | 8755 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
8756 | if (pu != NULL) |
8757 | { | |
8758 | const struct elf_backend_data *bed; | |
8759 | unsigned int log_file_align; | |
8760 | ||
8761 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
8762 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 8763 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
8764 | while (n--) |
8765 | { | |
8766 | if (*pu) | |
8767 | *cu = TRUE; | |
8768 | pu++; | |
8769 | cu++; | |
8770 | } | |
8771 | } | |
8772 | } | |
8773 | ||
8774 | return TRUE; | |
8775 | } | |
8776 | ||
8777 | static bfd_boolean | |
8778 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
8779 | { | |
8780 | asection *sec; | |
8781 | bfd_vma hstart, hend; | |
8782 | Elf_Internal_Rela *relstart, *relend, *rel; | |
8783 | const struct elf_backend_data *bed; | |
8784 | unsigned int log_file_align; | |
8785 | ||
8786 | if (h->root.type == bfd_link_hash_warning) | |
8787 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8788 | ||
8789 | /* Take care of both those symbols that do not describe vtables as | |
8790 | well as those that are not loaded. */ | |
f6e332e6 | 8791 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8792 | return TRUE; |
8793 | ||
8794 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
8795 | || h->root.type == bfd_link_hash_defweak); | |
8796 | ||
8797 | sec = h->root.u.def.section; | |
8798 | hstart = h->root.u.def.value; | |
8799 | hend = hstart + h->size; | |
8800 | ||
8801 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
8802 | if (!relstart) | |
8803 | return *(bfd_boolean *) okp = FALSE; | |
8804 | bed = get_elf_backend_data (sec->owner); | |
8805 | log_file_align = bed->s->log_file_align; | |
8806 | ||
8807 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8808 | ||
8809 | for (rel = relstart; rel < relend; ++rel) | |
8810 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
8811 | { | |
8812 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
8813 | if (h->vtable->used |
8814 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
8815 | { |
8816 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 8817 | if (h->vtable->used[entry]) |
c152c796 AM |
8818 | continue; |
8819 | } | |
8820 | /* Otherwise, kill it. */ | |
8821 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
8822 | } | |
8823 | ||
8824 | return TRUE; | |
8825 | } | |
8826 | ||
715df9b8 EB |
8827 | /* Mark sections containing dynamically referenced symbols. This is called |
8828 | through elf_link_hash_traverse. */ | |
8829 | ||
8830 | static bfd_boolean | |
8831 | elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, | |
8832 | void *okp ATTRIBUTE_UNUSED) | |
8833 | { | |
8834 | if (h->root.type == bfd_link_hash_warning) | |
8835 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8836 | ||
8837 | if ((h->root.type == bfd_link_hash_defined | |
8838 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 8839 | && h->ref_dynamic) |
715df9b8 EB |
8840 | h->root.u.def.section->flags |= SEC_KEEP; |
8841 | ||
8842 | return TRUE; | |
8843 | } | |
8844 | ||
c152c796 AM |
8845 | /* Do mark and sweep of unused sections. */ |
8846 | ||
8847 | bfd_boolean | |
8848 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
8849 | { | |
8850 | bfd_boolean ok = TRUE; | |
8851 | bfd *sub; | |
8852 | asection * (*gc_mark_hook) | |
8853 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8854 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
8855 | ||
8856 | if (!get_elf_backend_data (abfd)->can_gc_sections | |
8857 | || info->relocatable | |
8858 | || info->emitrelocations | |
715df9b8 EB |
8859 | || info->shared |
8860 | || !is_elf_hash_table (info->hash)) | |
c152c796 AM |
8861 | { |
8862 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
8863 | return TRUE; | |
8864 | } | |
8865 | ||
8866 | /* Apply transitive closure to the vtable entry usage info. */ | |
8867 | elf_link_hash_traverse (elf_hash_table (info), | |
8868 | elf_gc_propagate_vtable_entries_used, | |
8869 | &ok); | |
8870 | if (!ok) | |
8871 | return FALSE; | |
8872 | ||
8873 | /* Kill the vtable relocations that were not used. */ | |
8874 | elf_link_hash_traverse (elf_hash_table (info), | |
8875 | elf_gc_smash_unused_vtentry_relocs, | |
8876 | &ok); | |
8877 | if (!ok) | |
8878 | return FALSE; | |
8879 | ||
715df9b8 EB |
8880 | /* Mark dynamically referenced symbols. */ |
8881 | if (elf_hash_table (info)->dynamic_sections_created) | |
8882 | elf_link_hash_traverse (elf_hash_table (info), | |
8883 | elf_gc_mark_dynamic_ref_symbol, | |
8884 | &ok); | |
8885 | if (!ok) | |
8886 | return FALSE; | |
c152c796 | 8887 | |
715df9b8 | 8888 | /* Grovel through relocs to find out who stays ... */ |
c152c796 AM |
8889 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
8890 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8891 | { | |
8892 | asection *o; | |
8893 | ||
8894 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8895 | continue; | |
8896 | ||
8897 | for (o = sub->sections; o != NULL; o = o->next) | |
8898 | { | |
8899 | if (o->flags & SEC_KEEP) | |
715df9b8 EB |
8900 | { |
8901 | /* _bfd_elf_discard_section_eh_frame knows how to discard | |
8902 | orphaned FDEs so don't mark sections referenced by the | |
8903 | EH frame section. */ | |
8904 | if (strcmp (o->name, ".eh_frame") == 0) | |
8905 | o->gc_mark = 1; | |
ccfa59ea | 8906 | else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
715df9b8 EB |
8907 | return FALSE; |
8908 | } | |
c152c796 AM |
8909 | } |
8910 | } | |
8911 | ||
8912 | /* ... and mark SEC_EXCLUDE for those that go. */ | |
8913 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) | |
8914 | return FALSE; | |
8915 | ||
8916 | return TRUE; | |
8917 | } | |
8918 | \f | |
8919 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
8920 | ||
8921 | bfd_boolean | |
8922 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
8923 | asection *sec, | |
8924 | struct elf_link_hash_entry *h, | |
8925 | bfd_vma offset) | |
8926 | { | |
8927 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
8928 | struct elf_link_hash_entry **search, *child; | |
8929 | bfd_size_type extsymcount; | |
8930 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8931 | ||
8932 | /* The sh_info field of the symtab header tells us where the | |
8933 | external symbols start. We don't care about the local symbols at | |
8934 | this point. */ | |
8935 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
8936 | if (!elf_bad_symtab (abfd)) | |
8937 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
8938 | ||
8939 | sym_hashes = elf_sym_hashes (abfd); | |
8940 | sym_hashes_end = sym_hashes + extsymcount; | |
8941 | ||
8942 | /* Hunt down the child symbol, which is in this section at the same | |
8943 | offset as the relocation. */ | |
8944 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
8945 | { | |
8946 | if ((child = *search) != NULL | |
8947 | && (child->root.type == bfd_link_hash_defined | |
8948 | || child->root.type == bfd_link_hash_defweak) | |
8949 | && child->root.u.def.section == sec | |
8950 | && child->root.u.def.value == offset) | |
8951 | goto win; | |
8952 | } | |
8953 | ||
d003868e AM |
8954 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
8955 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
8956 | bfd_set_error (bfd_error_invalid_operation); |
8957 | return FALSE; | |
8958 | ||
8959 | win: | |
f6e332e6 AM |
8960 | if (!child->vtable) |
8961 | { | |
8962 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
8963 | if (!child->vtable) | |
8964 | return FALSE; | |
8965 | } | |
c152c796 AM |
8966 | if (!h) |
8967 | { | |
8968 | /* This *should* only be the absolute section. It could potentially | |
8969 | be that someone has defined a non-global vtable though, which | |
8970 | would be bad. It isn't worth paging in the local symbols to be | |
8971 | sure though; that case should simply be handled by the assembler. */ | |
8972 | ||
f6e332e6 | 8973 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
8974 | } |
8975 | else | |
f6e332e6 | 8976 | child->vtable->parent = h; |
c152c796 AM |
8977 | |
8978 | return TRUE; | |
8979 | } | |
8980 | ||
8981 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
8982 | ||
8983 | bfd_boolean | |
8984 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
8985 | asection *sec ATTRIBUTE_UNUSED, | |
8986 | struct elf_link_hash_entry *h, | |
8987 | bfd_vma addend) | |
8988 | { | |
8989 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8990 | unsigned int log_file_align = bed->s->log_file_align; | |
8991 | ||
f6e332e6 AM |
8992 | if (!h->vtable) |
8993 | { | |
8994 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
8995 | if (!h->vtable) | |
8996 | return FALSE; | |
8997 | } | |
8998 | ||
8999 | if (addend >= h->vtable->size) | |
c152c796 AM |
9000 | { |
9001 | size_t size, bytes, file_align; | |
f6e332e6 | 9002 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
9003 | |
9004 | /* While the symbol is undefined, we have to be prepared to handle | |
9005 | a zero size. */ | |
9006 | file_align = 1 << log_file_align; | |
9007 | if (h->root.type == bfd_link_hash_undefined) | |
9008 | size = addend + file_align; | |
9009 | else | |
9010 | { | |
9011 | size = h->size; | |
9012 | if (addend >= size) | |
9013 | { | |
9014 | /* Oops! We've got a reference past the defined end of | |
9015 | the table. This is probably a bug -- shall we warn? */ | |
9016 | size = addend + file_align; | |
9017 | } | |
9018 | } | |
9019 | size = (size + file_align - 1) & -file_align; | |
9020 | ||
9021 | /* Allocate one extra entry for use as a "done" flag for the | |
9022 | consolidation pass. */ | |
9023 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
9024 | ||
9025 | if (ptr) | |
9026 | { | |
9027 | ptr = bfd_realloc (ptr - 1, bytes); | |
9028 | ||
9029 | if (ptr != NULL) | |
9030 | { | |
9031 | size_t oldbytes; | |
9032 | ||
f6e332e6 | 9033 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
9034 | * sizeof (bfd_boolean)); |
9035 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
9036 | } | |
9037 | } | |
9038 | else | |
9039 | ptr = bfd_zmalloc (bytes); | |
9040 | ||
9041 | if (ptr == NULL) | |
9042 | return FALSE; | |
9043 | ||
9044 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
9045 | h->vtable->used = ptr + 1; |
9046 | h->vtable->size = size; | |
c152c796 AM |
9047 | } |
9048 | ||
f6e332e6 | 9049 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
9050 | |
9051 | return TRUE; | |
9052 | } | |
9053 | ||
9054 | struct alloc_got_off_arg { | |
9055 | bfd_vma gotoff; | |
9056 | unsigned int got_elt_size; | |
9057 | }; | |
9058 | ||
9059 | /* We need a special top-level link routine to convert got reference counts | |
9060 | to real got offsets. */ | |
9061 | ||
9062 | static bfd_boolean | |
9063 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
9064 | { | |
9065 | struct alloc_got_off_arg *gofarg = arg; | |
9066 | ||
9067 | if (h->root.type == bfd_link_hash_warning) | |
9068 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9069 | ||
9070 | if (h->got.refcount > 0) | |
9071 | { | |
9072 | h->got.offset = gofarg->gotoff; | |
9073 | gofarg->gotoff += gofarg->got_elt_size; | |
9074 | } | |
9075 | else | |
9076 | h->got.offset = (bfd_vma) -1; | |
9077 | ||
9078 | return TRUE; | |
9079 | } | |
9080 | ||
9081 | /* And an accompanying bit to work out final got entry offsets once | |
9082 | we're done. Should be called from final_link. */ | |
9083 | ||
9084 | bfd_boolean | |
9085 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
9086 | struct bfd_link_info *info) | |
9087 | { | |
9088 | bfd *i; | |
9089 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9090 | bfd_vma gotoff; | |
9091 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
9092 | struct alloc_got_off_arg gofarg; | |
9093 | ||
9094 | if (! is_elf_hash_table (info->hash)) | |
9095 | return FALSE; | |
9096 | ||
9097 | /* The GOT offset is relative to the .got section, but the GOT header is | |
9098 | put into the .got.plt section, if the backend uses it. */ | |
9099 | if (bed->want_got_plt) | |
9100 | gotoff = 0; | |
9101 | else | |
9102 | gotoff = bed->got_header_size; | |
9103 | ||
9104 | /* Do the local .got entries first. */ | |
9105 | for (i = info->input_bfds; i; i = i->link_next) | |
9106 | { | |
9107 | bfd_signed_vma *local_got; | |
9108 | bfd_size_type j, locsymcount; | |
9109 | Elf_Internal_Shdr *symtab_hdr; | |
9110 | ||
9111 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
9112 | continue; | |
9113 | ||
9114 | local_got = elf_local_got_refcounts (i); | |
9115 | if (!local_got) | |
9116 | continue; | |
9117 | ||
9118 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
9119 | if (elf_bad_symtab (i)) | |
9120 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9121 | else | |
9122 | locsymcount = symtab_hdr->sh_info; | |
9123 | ||
9124 | for (j = 0; j < locsymcount; ++j) | |
9125 | { | |
9126 | if (local_got[j] > 0) | |
9127 | { | |
9128 | local_got[j] = gotoff; | |
9129 | gotoff += got_elt_size; | |
9130 | } | |
9131 | else | |
9132 | local_got[j] = (bfd_vma) -1; | |
9133 | } | |
9134 | } | |
9135 | ||
9136 | /* Then the global .got entries. .plt refcounts are handled by | |
9137 | adjust_dynamic_symbol */ | |
9138 | gofarg.gotoff = gotoff; | |
9139 | gofarg.got_elt_size = got_elt_size; | |
9140 | elf_link_hash_traverse (elf_hash_table (info), | |
9141 | elf_gc_allocate_got_offsets, | |
9142 | &gofarg); | |
9143 | return TRUE; | |
9144 | } | |
9145 | ||
9146 | /* Many folk need no more in the way of final link than this, once | |
9147 | got entry reference counting is enabled. */ | |
9148 | ||
9149 | bfd_boolean | |
9150 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
9151 | { | |
9152 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
9153 | return FALSE; | |
9154 | ||
9155 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9156 | return bfd_elf_final_link (abfd, info); | |
9157 | } | |
9158 | ||
9159 | bfd_boolean | |
9160 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
9161 | { | |
9162 | struct elf_reloc_cookie *rcookie = cookie; | |
9163 | ||
9164 | if (rcookie->bad_symtab) | |
9165 | rcookie->rel = rcookie->rels; | |
9166 | ||
9167 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
9168 | { | |
9169 | unsigned long r_symndx; | |
9170 | ||
9171 | if (! rcookie->bad_symtab) | |
9172 | if (rcookie->rel->r_offset > offset) | |
9173 | return FALSE; | |
9174 | if (rcookie->rel->r_offset != offset) | |
9175 | continue; | |
9176 | ||
9177 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
9178 | if (r_symndx == SHN_UNDEF) | |
9179 | return TRUE; | |
9180 | ||
9181 | if (r_symndx >= rcookie->locsymcount | |
9182 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
9183 | { | |
9184 | struct elf_link_hash_entry *h; | |
9185 | ||
9186 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
9187 | ||
9188 | while (h->root.type == bfd_link_hash_indirect | |
9189 | || h->root.type == bfd_link_hash_warning) | |
9190 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9191 | ||
9192 | if ((h->root.type == bfd_link_hash_defined | |
9193 | || h->root.type == bfd_link_hash_defweak) | |
9194 | && elf_discarded_section (h->root.u.def.section)) | |
9195 | return TRUE; | |
9196 | else | |
9197 | return FALSE; | |
9198 | } | |
9199 | else | |
9200 | { | |
9201 | /* It's not a relocation against a global symbol, | |
9202 | but it could be a relocation against a local | |
9203 | symbol for a discarded section. */ | |
9204 | asection *isec; | |
9205 | Elf_Internal_Sym *isym; | |
9206 | ||
9207 | /* Need to: get the symbol; get the section. */ | |
9208 | isym = &rcookie->locsyms[r_symndx]; | |
9209 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
9210 | { | |
9211 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
9212 | if (isec != NULL && elf_discarded_section (isec)) | |
9213 | return TRUE; | |
9214 | } | |
9215 | } | |
9216 | return FALSE; | |
9217 | } | |
9218 | return FALSE; | |
9219 | } | |
9220 | ||
9221 | /* Discard unneeded references to discarded sections. | |
9222 | Returns TRUE if any section's size was changed. */ | |
9223 | /* This function assumes that the relocations are in sorted order, | |
9224 | which is true for all known assemblers. */ | |
9225 | ||
9226 | bfd_boolean | |
9227 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
9228 | { | |
9229 | struct elf_reloc_cookie cookie; | |
9230 | asection *stab, *eh; | |
9231 | Elf_Internal_Shdr *symtab_hdr; | |
9232 | const struct elf_backend_data *bed; | |
9233 | bfd *abfd; | |
9234 | unsigned int count; | |
9235 | bfd_boolean ret = FALSE; | |
9236 | ||
9237 | if (info->traditional_format | |
9238 | || !is_elf_hash_table (info->hash)) | |
9239 | return FALSE; | |
9240 | ||
9241 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
9242 | { | |
9243 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
9244 | continue; | |
9245 | ||
9246 | bed = get_elf_backend_data (abfd); | |
9247 | ||
9248 | if ((abfd->flags & DYNAMIC) != 0) | |
9249 | continue; | |
9250 | ||
9251 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
9252 | if (info->relocatable | |
9253 | || (eh != NULL | |
eea6121a | 9254 | && (eh->size == 0 |
c152c796 AM |
9255 | || bfd_is_abs_section (eh->output_section)))) |
9256 | eh = NULL; | |
9257 | ||
9258 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
9259 | if (stab != NULL | |
eea6121a | 9260 | && (stab->size == 0 |
c152c796 AM |
9261 | || bfd_is_abs_section (stab->output_section) |
9262 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
9263 | stab = NULL; | |
9264 | ||
9265 | if (stab == NULL | |
9266 | && eh == NULL | |
9267 | && bed->elf_backend_discard_info == NULL) | |
9268 | continue; | |
9269 | ||
9270 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9271 | cookie.abfd = abfd; | |
9272 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
9273 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
9274 | if (cookie.bad_symtab) | |
9275 | { | |
9276 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9277 | cookie.extsymoff = 0; | |
9278 | } | |
9279 | else | |
9280 | { | |
9281 | cookie.locsymcount = symtab_hdr->sh_info; | |
9282 | cookie.extsymoff = symtab_hdr->sh_info; | |
9283 | } | |
9284 | ||
9285 | if (bed->s->arch_size == 32) | |
9286 | cookie.r_sym_shift = 8; | |
9287 | else | |
9288 | cookie.r_sym_shift = 32; | |
9289 | ||
9290 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9291 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
9292 | { | |
9293 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
9294 | cookie.locsymcount, 0, | |
9295 | NULL, NULL, NULL); | |
9296 | if (cookie.locsyms == NULL) | |
9297 | return FALSE; | |
9298 | } | |
9299 | ||
9300 | if (stab != NULL) | |
9301 | { | |
9302 | cookie.rels = NULL; | |
9303 | count = stab->reloc_count; | |
9304 | if (count != 0) | |
9305 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
9306 | info->keep_memory); | |
9307 | if (cookie.rels != NULL) | |
9308 | { | |
9309 | cookie.rel = cookie.rels; | |
9310 | cookie.relend = cookie.rels; | |
9311 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9312 | if (_bfd_discard_section_stabs (abfd, stab, | |
9313 | elf_section_data (stab)->sec_info, | |
9314 | bfd_elf_reloc_symbol_deleted_p, | |
9315 | &cookie)) | |
9316 | ret = TRUE; | |
9317 | if (elf_section_data (stab)->relocs != cookie.rels) | |
9318 | free (cookie.rels); | |
9319 | } | |
9320 | } | |
9321 | ||
9322 | if (eh != NULL) | |
9323 | { | |
9324 | cookie.rels = NULL; | |
9325 | count = eh->reloc_count; | |
9326 | if (count != 0) | |
9327 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
9328 | info->keep_memory); | |
9329 | cookie.rel = cookie.rels; | |
9330 | cookie.relend = cookie.rels; | |
9331 | if (cookie.rels != NULL) | |
9332 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9333 | ||
9334 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
9335 | bfd_elf_reloc_symbol_deleted_p, | |
9336 | &cookie)) | |
9337 | ret = TRUE; | |
9338 | ||
9339 | if (cookie.rels != NULL | |
9340 | && elf_section_data (eh)->relocs != cookie.rels) | |
9341 | free (cookie.rels); | |
9342 | } | |
9343 | ||
9344 | if (bed->elf_backend_discard_info != NULL | |
9345 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
9346 | ret = TRUE; | |
9347 | ||
9348 | if (cookie.locsyms != NULL | |
9349 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
9350 | { | |
9351 | if (! info->keep_memory) | |
9352 | free (cookie.locsyms); | |
9353 | else | |
9354 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
9355 | } | |
9356 | } | |
9357 | ||
9358 | if (info->eh_frame_hdr | |
9359 | && !info->relocatable | |
9360 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
9361 | ret = TRUE; | |
9362 | ||
9363 | return ret; | |
9364 | } | |
082b7297 L |
9365 | |
9366 | void | |
9367 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
9368 | { | |
9369 | flagword flags; | |
6d2cd210 | 9370 | const char *name, *p; |
082b7297 L |
9371 | struct bfd_section_already_linked *l; |
9372 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
9373 | asection *group; |
9374 | ||
9375 | /* A single member comdat group section may be discarded by a | |
9376 | linkonce section. See below. */ | |
9377 | if (sec->output_section == bfd_abs_section_ptr) | |
9378 | return; | |
082b7297 L |
9379 | |
9380 | flags = sec->flags; | |
3d7f7666 L |
9381 | |
9382 | /* Check if it belongs to a section group. */ | |
9383 | group = elf_sec_group (sec); | |
9384 | ||
9385 | /* Return if it isn't a linkonce section nor a member of a group. A | |
9386 | comdat group section also has SEC_LINK_ONCE set. */ | |
9387 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
9388 | return; |
9389 | ||
3d7f7666 L |
9390 | if (group) |
9391 | { | |
9392 | /* If this is the member of a single member comdat group, check if | |
9393 | the group should be discarded. */ | |
9394 | if (elf_next_in_group (sec) == sec | |
9395 | && (group->flags & SEC_LINK_ONCE) != 0) | |
9396 | sec = group; | |
9397 | else | |
9398 | return; | |
9399 | } | |
9400 | ||
082b7297 L |
9401 | /* FIXME: When doing a relocatable link, we may have trouble |
9402 | copying relocations in other sections that refer to local symbols | |
9403 | in the section being discarded. Those relocations will have to | |
9404 | be converted somehow; as of this writing I'm not sure that any of | |
9405 | the backends handle that correctly. | |
9406 | ||
9407 | It is tempting to instead not discard link once sections when | |
9408 | doing a relocatable link (technically, they should be discarded | |
9409 | whenever we are building constructors). However, that fails, | |
9410 | because the linker winds up combining all the link once sections | |
9411 | into a single large link once section, which defeats the purpose | |
9412 | of having link once sections in the first place. | |
9413 | ||
9414 | Also, not merging link once sections in a relocatable link | |
9415 | causes trouble for MIPS ELF, which relies on link once semantics | |
9416 | to handle the .reginfo section correctly. */ | |
9417 | ||
9418 | name = bfd_get_section_name (abfd, sec); | |
9419 | ||
6d2cd210 JJ |
9420 | if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0 |
9421 | && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) | |
9422 | p++; | |
9423 | else | |
9424 | p = name; | |
9425 | ||
9426 | already_linked_list = bfd_section_already_linked_table_lookup (p); | |
082b7297 L |
9427 | |
9428 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
9429 | { | |
9430 | /* We may have 3 different sections on the list: group section, | |
9431 | comdat section and linkonce section. SEC may be a linkonce or | |
9432 | group section. We match a group section with a group section, | |
9433 | a linkonce section with a linkonce section, and ignore comdat | |
9434 | section. */ | |
3d7f7666 | 9435 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
6d2cd210 | 9436 | && strcmp (name, l->sec->name) == 0 |
082b7297 L |
9437 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
9438 | { | |
9439 | /* The section has already been linked. See if we should | |
6d2cd210 | 9440 | issue a warning. */ |
082b7297 L |
9441 | switch (flags & SEC_LINK_DUPLICATES) |
9442 | { | |
9443 | default: | |
9444 | abort (); | |
9445 | ||
9446 | case SEC_LINK_DUPLICATES_DISCARD: | |
9447 | break; | |
9448 | ||
9449 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
9450 | (*_bfd_error_handler) | |
d003868e AM |
9451 | (_("%B: ignoring duplicate section `%A'\n"), |
9452 | abfd, sec); | |
082b7297 L |
9453 | break; |
9454 | ||
9455 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
9456 | if (sec->size != l->sec->size) | |
9457 | (*_bfd_error_handler) | |
d003868e AM |
9458 | (_("%B: duplicate section `%A' has different size\n"), |
9459 | abfd, sec); | |
082b7297 | 9460 | break; |
ea5158d8 DJ |
9461 | |
9462 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
9463 | if (sec->size != l->sec->size) | |
9464 | (*_bfd_error_handler) | |
9465 | (_("%B: duplicate section `%A' has different size\n"), | |
9466 | abfd, sec); | |
9467 | else if (sec->size != 0) | |
9468 | { | |
9469 | bfd_byte *sec_contents, *l_sec_contents; | |
9470 | ||
9471 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
9472 | (*_bfd_error_handler) | |
9473 | (_("%B: warning: could not read contents of section `%A'\n"), | |
9474 | abfd, sec); | |
9475 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
9476 | &l_sec_contents)) | |
9477 | (*_bfd_error_handler) | |
9478 | (_("%B: warning: could not read contents of section `%A'\n"), | |
9479 | l->sec->owner, l->sec); | |
9480 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
9481 | (*_bfd_error_handler) | |
9482 | (_("%B: warning: duplicate section `%A' has different contents\n"), | |
9483 | abfd, sec); | |
9484 | ||
9485 | if (sec_contents) | |
9486 | free (sec_contents); | |
9487 | if (l_sec_contents) | |
9488 | free (l_sec_contents); | |
9489 | } | |
9490 | break; | |
082b7297 L |
9491 | } |
9492 | ||
9493 | /* Set the output_section field so that lang_add_section | |
9494 | does not create a lang_input_section structure for this | |
9495 | section. Since there might be a symbol in the section | |
9496 | being discarded, we must retain a pointer to the section | |
9497 | which we are really going to use. */ | |
9498 | sec->output_section = bfd_abs_section_ptr; | |
9499 | sec->kept_section = l->sec; | |
9500 | ||
9501 | if (flags & SEC_GROUP) | |
3d7f7666 L |
9502 | { |
9503 | asection *first = elf_next_in_group (sec); | |
9504 | asection *s = first; | |
9505 | ||
9506 | while (s != NULL) | |
9507 | { | |
9508 | s->output_section = bfd_abs_section_ptr; | |
9509 | /* Record which group discards it. */ | |
9510 | s->kept_section = l->sec; | |
9511 | s = elf_next_in_group (s); | |
9512 | /* These lists are circular. */ | |
9513 | if (s == first) | |
9514 | break; | |
9515 | } | |
9516 | } | |
082b7297 L |
9517 | |
9518 | return; | |
9519 | } | |
9520 | } | |
9521 | ||
3d7f7666 L |
9522 | if (group) |
9523 | { | |
9524 | /* If this is the member of a single member comdat group and the | |
9525 | group hasn't be discarded, we check if it matches a linkonce | |
9526 | section. We only record the discarded comdat group. Otherwise | |
9527 | the undiscarded group will be discarded incorrectly later since | |
9528 | itself has been recorded. */ | |
6d2cd210 JJ |
9529 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9530 | if ((l->sec->flags & SEC_GROUP) == 0 | |
9531 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
9532 | && bfd_elf_match_symbols_in_sections (l->sec, | |
9533 | elf_next_in_group (sec))) | |
9534 | { | |
9535 | elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; | |
9536 | elf_next_in_group (sec)->kept_section = l->sec; | |
9537 | group->output_section = bfd_abs_section_ptr; | |
9538 | break; | |
9539 | } | |
9540 | if (l == NULL) | |
3d7f7666 L |
9541 | return; |
9542 | } | |
9543 | else | |
9544 | /* There is no direct match. But for linkonce section, we should | |
9545 | check if there is a match with comdat group member. We always | |
9546 | record the linkonce section, discarded or not. */ | |
6d2cd210 JJ |
9547 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
9548 | if (l->sec->flags & SEC_GROUP) | |
9549 | { | |
9550 | asection *first = elf_next_in_group (l->sec); | |
9551 | ||
9552 | if (first != NULL | |
9553 | && elf_next_in_group (first) == first | |
9554 | && bfd_elf_match_symbols_in_sections (first, sec)) | |
9555 | { | |
9556 | sec->output_section = bfd_abs_section_ptr; | |
9557 | sec->kept_section = l->sec; | |
9558 | break; | |
9559 | } | |
9560 | } | |
9561 | ||
082b7297 L |
9562 | /* This is the first section with this name. Record it. */ |
9563 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
9564 | } |