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; |
252b5132 | 91 | |
36af4a4e | 92 | if (! info->executable |
c152c796 | 93 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 94 | return FALSE; |
252b5132 | 95 | |
2517a57f AM |
96 | elf_hash_table (info)->hgot = h; |
97 | } | |
252b5132 RH |
98 | |
99 | /* The first bit of the global offset table is the header. */ | |
eea6121a | 100 | s->size += bed->got_header_size + bed->got_symbol_offset; |
252b5132 | 101 | |
b34976b6 | 102 | return TRUE; |
252b5132 RH |
103 | } |
104 | \f | |
45d6a902 AM |
105 | /* Create some sections which will be filled in with dynamic linking |
106 | information. ABFD is an input file which requires dynamic sections | |
107 | to be created. The dynamic sections take up virtual memory space | |
108 | when the final executable is run, so we need to create them before | |
109 | addresses are assigned to the output sections. We work out the | |
110 | actual contents and size of these sections later. */ | |
252b5132 | 111 | |
b34976b6 | 112 | bfd_boolean |
268b6b39 | 113 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 114 | { |
45d6a902 AM |
115 | flagword flags; |
116 | register asection *s; | |
117 | struct elf_link_hash_entry *h; | |
118 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 119 | const struct elf_backend_data *bed; |
252b5132 | 120 | |
0eddce27 | 121 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
122 | return FALSE; |
123 | ||
124 | if (elf_hash_table (info)->dynamic_sections_created) | |
125 | return TRUE; | |
126 | ||
127 | /* Make sure that all dynamic sections use the same input BFD. */ | |
128 | if (elf_hash_table (info)->dynobj == NULL) | |
129 | elf_hash_table (info)->dynobj = abfd; | |
130 | else | |
131 | abfd = elf_hash_table (info)->dynobj; | |
132 | ||
e5a52504 MM |
133 | bed = get_elf_backend_data (abfd); |
134 | ||
135 | flags = bed->dynamic_sec_flags; | |
45d6a902 AM |
136 | |
137 | /* A dynamically linked executable has a .interp section, but a | |
138 | shared library does not. */ | |
36af4a4e | 139 | if (info->executable) |
252b5132 | 140 | { |
45d6a902 AM |
141 | s = bfd_make_section (abfd, ".interp"); |
142 | if (s == NULL | |
143 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
144 | return FALSE; | |
145 | } | |
bb0deeff | 146 | |
0eddce27 | 147 | if (! info->traditional_format) |
45d6a902 AM |
148 | { |
149 | s = bfd_make_section (abfd, ".eh_frame_hdr"); | |
150 | if (s == NULL | |
151 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
152 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
153 | return FALSE; | |
154 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
155 | } | |
bb0deeff | 156 | |
45d6a902 AM |
157 | /* Create sections to hold version informations. These are removed |
158 | if they are not needed. */ | |
159 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
160 | if (s == NULL | |
161 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
162 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
163 | return FALSE; | |
164 | ||
165 | s = bfd_make_section (abfd, ".gnu.version"); | |
166 | if (s == NULL | |
167 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
168 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
169 | return FALSE; | |
170 | ||
171 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
172 | if (s == NULL | |
173 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
174 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
175 | return FALSE; | |
176 | ||
177 | s = bfd_make_section (abfd, ".dynsym"); | |
178 | if (s == NULL | |
179 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
180 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
181 | return FALSE; | |
182 | ||
183 | s = bfd_make_section (abfd, ".dynstr"); | |
184 | if (s == NULL | |
185 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
186 | return FALSE; | |
187 | ||
188 | /* Create a strtab to hold the dynamic symbol names. */ | |
189 | if (elf_hash_table (info)->dynstr == NULL) | |
190 | { | |
191 | elf_hash_table (info)->dynstr = _bfd_elf_strtab_init (); | |
192 | if (elf_hash_table (info)->dynstr == NULL) | |
193 | return FALSE; | |
252b5132 RH |
194 | } |
195 | ||
45d6a902 AM |
196 | s = bfd_make_section (abfd, ".dynamic"); |
197 | if (s == NULL | |
198 | || ! bfd_set_section_flags (abfd, s, flags) | |
199 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
200 | return FALSE; | |
201 | ||
202 | /* The special symbol _DYNAMIC is always set to the start of the | |
203 | .dynamic section. This call occurs before we have processed the | |
204 | symbols for any dynamic object, so we don't have to worry about | |
205 | overriding a dynamic definition. We could set _DYNAMIC in a | |
206 | linker script, but we only want to define it if we are, in fact, | |
207 | creating a .dynamic section. We don't want to define it if there | |
208 | is no .dynamic section, since on some ELF platforms the start up | |
209 | code examines it to decide how to initialize the process. */ | |
210 | bh = NULL; | |
211 | if (! (_bfd_generic_link_add_one_symbol | |
268b6b39 AM |
212 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE, |
213 | get_elf_backend_data (abfd)->collect, &bh))) | |
45d6a902 AM |
214 | return FALSE; |
215 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf | 216 | h->def_regular = 1; |
45d6a902 AM |
217 | h->type = STT_OBJECT; |
218 | ||
36af4a4e | 219 | if (! info->executable |
c152c796 | 220 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
221 | return FALSE; |
222 | ||
223 | s = bfd_make_section (abfd, ".hash"); | |
224 | if (s == NULL | |
225 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
226 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
227 | return FALSE; | |
228 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
229 | ||
230 | /* Let the backend create the rest of the sections. This lets the | |
231 | backend set the right flags. The backend will normally create | |
232 | the .got and .plt sections. */ | |
233 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
234 | return FALSE; | |
235 | ||
236 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
237 | ||
238 | return TRUE; | |
239 | } | |
240 | ||
241 | /* Create dynamic sections when linking against a dynamic object. */ | |
242 | ||
243 | bfd_boolean | |
268b6b39 | 244 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
245 | { |
246 | flagword flags, pltflags; | |
247 | asection *s; | |
9c5bfbb7 | 248 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 249 | |
252b5132 RH |
250 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
251 | .rel[a].bss sections. */ | |
e5a52504 | 252 | flags = bed->dynamic_sec_flags; |
252b5132 RH |
253 | |
254 | pltflags = flags; | |
255 | pltflags |= SEC_CODE; | |
256 | if (bed->plt_not_loaded) | |
5d1634d7 | 257 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
252b5132 RH |
258 | if (bed->plt_readonly) |
259 | pltflags |= SEC_READONLY; | |
260 | ||
261 | s = bfd_make_section (abfd, ".plt"); | |
262 | if (s == NULL | |
263 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
264 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
b34976b6 | 265 | return FALSE; |
252b5132 RH |
266 | |
267 | if (bed->want_plt_sym) | |
268 | { | |
269 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
270 | .plt section. */ | |
14a793b2 AM |
271 | struct elf_link_hash_entry *h; |
272 | struct bfd_link_hash_entry *bh = NULL; | |
273 | ||
252b5132 | 274 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
275 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL, |
276 | FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
b34976b6 | 277 | return FALSE; |
14a793b2 | 278 | h = (struct elf_link_hash_entry *) bh; |
f5385ebf | 279 | h->def_regular = 1; |
252b5132 RH |
280 | h->type = STT_OBJECT; |
281 | ||
36af4a4e | 282 | if (! info->executable |
c152c796 | 283 | && ! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 284 | return FALSE; |
252b5132 RH |
285 | } |
286 | ||
3e932841 | 287 | s = bfd_make_section (abfd, |
bf572ba0 | 288 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
289 | if (s == NULL |
290 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 291 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 292 | return FALSE; |
252b5132 RH |
293 | |
294 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 295 | return FALSE; |
252b5132 | 296 | |
3018b441 RH |
297 | if (bed->want_dynbss) |
298 | { | |
299 | /* The .dynbss section is a place to put symbols which are defined | |
300 | by dynamic objects, are referenced by regular objects, and are | |
301 | not functions. We must allocate space for them in the process | |
302 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
303 | initialize them at run time. The linker script puts the .dynbss | |
304 | section into the .bss section of the final image. */ | |
305 | s = bfd_make_section (abfd, ".dynbss"); | |
306 | if (s == NULL | |
77f3d027 | 307 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED)) |
b34976b6 | 308 | return FALSE; |
252b5132 | 309 | |
3018b441 | 310 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
311 | normally needed. We need to create it here, though, so that the |
312 | linker will map it to an output section. We can't just create it | |
313 | only if we need it, because we will not know whether we need it | |
314 | until we have seen all the input files, and the first time the | |
315 | main linker code calls BFD after examining all the input files | |
316 | (size_dynamic_sections) the input sections have already been | |
317 | mapped to the output sections. If the section turns out not to | |
318 | be needed, we can discard it later. We will never need this | |
319 | section when generating a shared object, since they do not use | |
320 | copy relocs. */ | |
3018b441 RH |
321 | if (! info->shared) |
322 | { | |
3e932841 KH |
323 | s = bfd_make_section (abfd, |
324 | (bed->default_use_rela_p | |
325 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
326 | if (s == NULL |
327 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 328 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 329 | return FALSE; |
3018b441 | 330 | } |
252b5132 RH |
331 | } |
332 | ||
b34976b6 | 333 | return TRUE; |
252b5132 RH |
334 | } |
335 | \f | |
252b5132 RH |
336 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
337 | read the input files, since we need to have a list of all of them | |
338 | before we can determine the final sizes of the output sections. | |
339 | Note that we may actually call this function even though we are not | |
340 | going to output any dynamic symbols; in some cases we know that a | |
341 | symbol should be in the dynamic symbol table, but only if there is | |
342 | one. */ | |
343 | ||
b34976b6 | 344 | bfd_boolean |
c152c796 AM |
345 | bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
346 | struct elf_link_hash_entry *h) | |
252b5132 RH |
347 | { |
348 | if (h->dynindx == -1) | |
349 | { | |
2b0f7ef9 | 350 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 351 | char *p; |
252b5132 | 352 | const char *name; |
252b5132 RH |
353 | bfd_size_type indx; |
354 | ||
7a13edea NC |
355 | /* XXX: The ABI draft says the linker must turn hidden and |
356 | internal symbols into STB_LOCAL symbols when producing the | |
357 | DSO. However, if ld.so honors st_other in the dynamic table, | |
358 | this would not be necessary. */ | |
359 | switch (ELF_ST_VISIBILITY (h->other)) | |
360 | { | |
361 | case STV_INTERNAL: | |
362 | case STV_HIDDEN: | |
9d6eee78 L |
363 | if (h->root.type != bfd_link_hash_undefined |
364 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 | 365 | { |
f5385ebf | 366 | h->forced_local = 1; |
b34976b6 | 367 | return TRUE; |
7a13edea | 368 | } |
0444bdd4 | 369 | |
7a13edea NC |
370 | default: |
371 | break; | |
372 | } | |
373 | ||
252b5132 RH |
374 | h->dynindx = elf_hash_table (info)->dynsymcount; |
375 | ++elf_hash_table (info)->dynsymcount; | |
376 | ||
377 | dynstr = elf_hash_table (info)->dynstr; | |
378 | if (dynstr == NULL) | |
379 | { | |
380 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 381 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 382 | if (dynstr == NULL) |
b34976b6 | 383 | return FALSE; |
252b5132 RH |
384 | } |
385 | ||
386 | /* We don't put any version information in the dynamic string | |
aad5d350 | 387 | table. */ |
252b5132 RH |
388 | name = h->root.root.string; |
389 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
390 | if (p != NULL) |
391 | /* We know that the p points into writable memory. In fact, | |
392 | there are only a few symbols that have read-only names, being | |
393 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
394 | by the backends. Most symbols will have names pointing into | |
395 | an ELF string table read from a file, or to objalloc memory. */ | |
396 | *p = 0; | |
397 | ||
398 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
399 | ||
400 | if (p != NULL) | |
401 | *p = ELF_VER_CHR; | |
252b5132 RH |
402 | |
403 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 404 | return FALSE; |
252b5132 RH |
405 | h->dynstr_index = indx; |
406 | } | |
407 | ||
b34976b6 | 408 | return TRUE; |
252b5132 | 409 | } |
45d6a902 AM |
410 | \f |
411 | /* Record an assignment to a symbol made by a linker script. We need | |
412 | this in case some dynamic object refers to this symbol. */ | |
413 | ||
414 | bfd_boolean | |
268b6b39 AM |
415 | bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED, |
416 | struct bfd_link_info *info, | |
417 | const char *name, | |
418 | bfd_boolean provide) | |
45d6a902 AM |
419 | { |
420 | struct elf_link_hash_entry *h; | |
421 | ||
0eddce27 | 422 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
423 | return TRUE; |
424 | ||
425 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); | |
426 | if (h == NULL) | |
427 | return FALSE; | |
428 | ||
02bb6eae AO |
429 | /* Since we're defining the symbol, don't let it seem to have not |
430 | been defined. record_dynamic_symbol and size_dynamic_sections | |
a010d60f AM |
431 | may depend on this. |
432 | ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or | |
433 | to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk | |
434 | of some later symbol manipulation setting the symbol back to | |
435 | bfd_link_hash_undefined, and the linker trying to add the symbol to | |
436 | the undefs list twice. */ | |
02bb6eae AO |
437 | if (h->root.type == bfd_link_hash_undefweak |
438 | || h->root.type == bfd_link_hash_undefined) | |
439 | h->root.type = bfd_link_hash_new; | |
440 | ||
45d6a902 | 441 | if (h->root.type == bfd_link_hash_new) |
f5385ebf | 442 | h->non_elf = 0; |
45d6a902 AM |
443 | |
444 | /* If this symbol is being provided by the linker script, and it is | |
445 | currently defined by a dynamic object, but not by a regular | |
446 | object, then mark it as undefined so that the generic linker will | |
447 | force the correct value. */ | |
448 | if (provide | |
f5385ebf AM |
449 | && h->def_dynamic |
450 | && !h->def_regular) | |
45d6a902 AM |
451 | h->root.type = bfd_link_hash_undefined; |
452 | ||
453 | /* If this symbol is not being provided by the linker script, and it is | |
454 | currently defined by a dynamic object, but not by a regular object, | |
455 | then clear out any version information because the symbol will not be | |
456 | associated with the dynamic object any more. */ | |
457 | if (!provide | |
f5385ebf AM |
458 | && h->def_dynamic |
459 | && !h->def_regular) | |
45d6a902 AM |
460 | h->verinfo.verdef = NULL; |
461 | ||
f5385ebf | 462 | h->def_regular = 1; |
45d6a902 | 463 | |
f5385ebf AM |
464 | if ((h->def_dynamic |
465 | || h->ref_dynamic | |
45d6a902 AM |
466 | || info->shared) |
467 | && h->dynindx == -1) | |
468 | { | |
c152c796 | 469 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
45d6a902 AM |
470 | return FALSE; |
471 | ||
472 | /* If this is a weak defined symbol, and we know a corresponding | |
473 | real symbol from the same dynamic object, make sure the real | |
474 | symbol is also made into a dynamic symbol. */ | |
f6e332e6 AM |
475 | if (h->u.weakdef != NULL |
476 | && h->u.weakdef->dynindx == -1) | |
45d6a902 | 477 | { |
f6e332e6 | 478 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
45d6a902 AM |
479 | return FALSE; |
480 | } | |
481 | } | |
482 | ||
483 | return TRUE; | |
484 | } | |
42751cf3 | 485 | |
8c58d23b AM |
486 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
487 | success, and 2 on a failure caused by attempting to record a symbol | |
488 | in a discarded section, eg. a discarded link-once section symbol. */ | |
489 | ||
490 | int | |
c152c796 AM |
491 | bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
492 | bfd *input_bfd, | |
493 | long input_indx) | |
8c58d23b AM |
494 | { |
495 | bfd_size_type amt; | |
496 | struct elf_link_local_dynamic_entry *entry; | |
497 | struct elf_link_hash_table *eht; | |
498 | struct elf_strtab_hash *dynstr; | |
499 | unsigned long dynstr_index; | |
500 | char *name; | |
501 | Elf_External_Sym_Shndx eshndx; | |
502 | char esym[sizeof (Elf64_External_Sym)]; | |
503 | ||
0eddce27 | 504 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
505 | return 0; |
506 | ||
507 | /* See if the entry exists already. */ | |
508 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
509 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
510 | return 1; | |
511 | ||
512 | amt = sizeof (*entry); | |
268b6b39 | 513 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
514 | if (entry == NULL) |
515 | return 0; | |
516 | ||
517 | /* Go find the symbol, so that we can find it's name. */ | |
518 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 519 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
520 | { |
521 | bfd_release (input_bfd, entry); | |
522 | return 0; | |
523 | } | |
524 | ||
525 | if (entry->isym.st_shndx != SHN_UNDEF | |
526 | && (entry->isym.st_shndx < SHN_LORESERVE | |
527 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
528 | { | |
529 | asection *s; | |
530 | ||
531 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
532 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
533 | { | |
534 | /* We can still bfd_release here as nothing has done another | |
535 | bfd_alloc. We can't do this later in this function. */ | |
536 | bfd_release (input_bfd, entry); | |
537 | return 2; | |
538 | } | |
539 | } | |
540 | ||
541 | name = (bfd_elf_string_from_elf_section | |
542 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
543 | entry->isym.st_name)); | |
544 | ||
545 | dynstr = elf_hash_table (info)->dynstr; | |
546 | if (dynstr == NULL) | |
547 | { | |
548 | /* Create a strtab to hold the dynamic symbol names. */ | |
549 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
550 | if (dynstr == NULL) | |
551 | return 0; | |
552 | } | |
553 | ||
b34976b6 | 554 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
555 | if (dynstr_index == (unsigned long) -1) |
556 | return 0; | |
557 | entry->isym.st_name = dynstr_index; | |
558 | ||
559 | eht = elf_hash_table (info); | |
560 | ||
561 | entry->next = eht->dynlocal; | |
562 | eht->dynlocal = entry; | |
563 | entry->input_bfd = input_bfd; | |
564 | entry->input_indx = input_indx; | |
565 | eht->dynsymcount++; | |
566 | ||
567 | /* Whatever binding the symbol had before, it's now local. */ | |
568 | entry->isym.st_info | |
569 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
570 | ||
571 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
572 | ||
573 | return 1; | |
574 | } | |
575 | ||
30b30c21 | 576 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 577 | |
30b30c21 | 578 | long |
268b6b39 AM |
579 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
580 | bfd *input_bfd, | |
581 | long input_indx) | |
30b30c21 RH |
582 | { |
583 | struct elf_link_local_dynamic_entry *e; | |
584 | ||
585 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
586 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
587 | return e->dynindx; | |
588 | return -1; | |
589 | } | |
590 | ||
591 | /* This function is used to renumber the dynamic symbols, if some of | |
592 | them are removed because they are marked as local. This is called | |
593 | via elf_link_hash_traverse. */ | |
594 | ||
b34976b6 | 595 | static bfd_boolean |
268b6b39 AM |
596 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
597 | void *data) | |
42751cf3 | 598 | { |
268b6b39 | 599 | size_t *count = data; |
30b30c21 | 600 | |
e92d460e AM |
601 | if (h->root.type == bfd_link_hash_warning) |
602 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
603 | ||
42751cf3 | 604 | if (h->dynindx != -1) |
30b30c21 RH |
605 | h->dynindx = ++(*count); |
606 | ||
b34976b6 | 607 | return TRUE; |
42751cf3 | 608 | } |
30b30c21 | 609 | |
aee6f5b4 AO |
610 | /* Return true if the dynamic symbol for a given section should be |
611 | omitted when creating a shared library. */ | |
612 | bfd_boolean | |
613 | _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, | |
614 | struct bfd_link_info *info, | |
615 | asection *p) | |
616 | { | |
617 | switch (elf_section_data (p)->this_hdr.sh_type) | |
618 | { | |
619 | case SHT_PROGBITS: | |
620 | case SHT_NOBITS: | |
621 | /* If sh_type is yet undecided, assume it could be | |
622 | SHT_PROGBITS/SHT_NOBITS. */ | |
623 | case SHT_NULL: | |
624 | if (strcmp (p->name, ".got") == 0 | |
625 | || strcmp (p->name, ".got.plt") == 0 | |
626 | || strcmp (p->name, ".plt") == 0) | |
627 | { | |
628 | asection *ip; | |
629 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
630 | ||
631 | if (dynobj != NULL | |
632 | && (ip = bfd_get_section_by_name (dynobj, p->name)) | |
633 | != NULL | |
634 | && (ip->flags & SEC_LINKER_CREATED) | |
635 | && ip->output_section == p) | |
636 | return TRUE; | |
637 | } | |
638 | return FALSE; | |
639 | ||
640 | /* There shouldn't be section relative relocations | |
641 | against any other section. */ | |
642 | default: | |
643 | return TRUE; | |
644 | } | |
645 | } | |
646 | ||
062e2358 | 647 | /* Assign dynsym indices. In a shared library we generate a section |
30b30c21 RH |
648 | symbol for each output section, which come first. Next come all of |
649 | the back-end allocated local dynamic syms, followed by the rest of | |
650 | the global symbols. */ | |
651 | ||
652 | unsigned long | |
268b6b39 | 653 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info) |
30b30c21 RH |
654 | { |
655 | unsigned long dynsymcount = 0; | |
656 | ||
657 | if (info->shared) | |
658 | { | |
aee6f5b4 | 659 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); |
30b30c21 RH |
660 | asection *p; |
661 | for (p = output_bfd->sections; p ; p = p->next) | |
8c37241b | 662 | if ((p->flags & SEC_EXCLUDE) == 0 |
aee6f5b4 AO |
663 | && (p->flags & SEC_ALLOC) != 0 |
664 | && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) | |
665 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 RH |
666 | } |
667 | ||
668 | if (elf_hash_table (info)->dynlocal) | |
669 | { | |
670 | struct elf_link_local_dynamic_entry *p; | |
671 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
672 | p->dynindx = ++dynsymcount; | |
673 | } | |
674 | ||
675 | elf_link_hash_traverse (elf_hash_table (info), | |
676 | elf_link_renumber_hash_table_dynsyms, | |
677 | &dynsymcount); | |
678 | ||
679 | /* There is an unused NULL entry at the head of the table which | |
680 | we must account for in our count. Unless there weren't any | |
681 | symbols, which means we'll have no table at all. */ | |
682 | if (dynsymcount != 0) | |
683 | ++dynsymcount; | |
684 | ||
685 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
686 | } | |
252b5132 | 687 | |
45d6a902 AM |
688 | /* This function is called when we want to define a new symbol. It |
689 | handles the various cases which arise when we find a definition in | |
690 | a dynamic object, or when there is already a definition in a | |
691 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
692 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
693 | OVERRIDE if the old symbol is overriding a new definition. We set | |
694 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
695 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
696 | change, we mean that we shouldn't warn if the type or size does | |
0f8a2703 | 697 | change. */ |
45d6a902 AM |
698 | |
699 | bfd_boolean | |
268b6b39 AM |
700 | _bfd_elf_merge_symbol (bfd *abfd, |
701 | struct bfd_link_info *info, | |
702 | const char *name, | |
703 | Elf_Internal_Sym *sym, | |
704 | asection **psec, | |
705 | bfd_vma *pvalue, | |
706 | struct elf_link_hash_entry **sym_hash, | |
707 | bfd_boolean *skip, | |
708 | bfd_boolean *override, | |
709 | bfd_boolean *type_change_ok, | |
0f8a2703 | 710 | bfd_boolean *size_change_ok) |
252b5132 | 711 | { |
7479dfd4 | 712 | asection *sec, *oldsec; |
45d6a902 AM |
713 | struct elf_link_hash_entry *h; |
714 | struct elf_link_hash_entry *flip; | |
715 | int bind; | |
716 | bfd *oldbfd; | |
717 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
79349b09 | 718 | bfd_boolean newweak, oldweak; |
45d6a902 AM |
719 | |
720 | *skip = FALSE; | |
721 | *override = FALSE; | |
722 | ||
723 | sec = *psec; | |
724 | bind = ELF_ST_BIND (sym->st_info); | |
725 | ||
726 | if (! bfd_is_und_section (sec)) | |
727 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
728 | else | |
729 | h = ((struct elf_link_hash_entry *) | |
730 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
731 | if (h == NULL) | |
732 | return FALSE; | |
733 | *sym_hash = h; | |
252b5132 | 734 | |
45d6a902 AM |
735 | /* This code is for coping with dynamic objects, and is only useful |
736 | if we are doing an ELF link. */ | |
737 | if (info->hash->creator != abfd->xvec) | |
738 | return TRUE; | |
252b5132 | 739 | |
45d6a902 AM |
740 | /* For merging, we only care about real symbols. */ |
741 | ||
742 | while (h->root.type == bfd_link_hash_indirect | |
743 | || h->root.type == bfd_link_hash_warning) | |
744 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
745 | ||
746 | /* If we just created the symbol, mark it as being an ELF symbol. | |
747 | Other than that, there is nothing to do--there is no merge issue | |
748 | with a newly defined symbol--so we just return. */ | |
749 | ||
750 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 751 | { |
f5385ebf | 752 | h->non_elf = 0; |
45d6a902 AM |
753 | return TRUE; |
754 | } | |
252b5132 | 755 | |
7479dfd4 L |
756 | /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the |
757 | existing symbol. */ | |
252b5132 | 758 | |
45d6a902 AM |
759 | switch (h->root.type) |
760 | { | |
761 | default: | |
762 | oldbfd = NULL; | |
7479dfd4 | 763 | oldsec = NULL; |
45d6a902 | 764 | break; |
252b5132 | 765 | |
45d6a902 AM |
766 | case bfd_link_hash_undefined: |
767 | case bfd_link_hash_undefweak: | |
768 | oldbfd = h->root.u.undef.abfd; | |
7479dfd4 | 769 | oldsec = NULL; |
45d6a902 AM |
770 | break; |
771 | ||
772 | case bfd_link_hash_defined: | |
773 | case bfd_link_hash_defweak: | |
774 | oldbfd = h->root.u.def.section->owner; | |
7479dfd4 | 775 | oldsec = h->root.u.def.section; |
45d6a902 AM |
776 | break; |
777 | ||
778 | case bfd_link_hash_common: | |
779 | oldbfd = h->root.u.c.p->section->owner; | |
7479dfd4 | 780 | oldsec = h->root.u.c.p->section; |
45d6a902 AM |
781 | break; |
782 | } | |
783 | ||
784 | /* In cases involving weak versioned symbols, we may wind up trying | |
785 | to merge a symbol with itself. Catch that here, to avoid the | |
786 | confusion that results if we try to override a symbol with | |
787 | itself. The additional tests catch cases like | |
788 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
789 | dynamic object, which we do want to handle here. */ | |
790 | if (abfd == oldbfd | |
791 | && ((abfd->flags & DYNAMIC) == 0 | |
f5385ebf | 792 | || !h->def_regular)) |
45d6a902 AM |
793 | return TRUE; |
794 | ||
795 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
796 | respectively, is from a dynamic object. */ | |
797 | ||
798 | if ((abfd->flags & DYNAMIC) != 0) | |
799 | newdyn = TRUE; | |
800 | else | |
801 | newdyn = FALSE; | |
802 | ||
803 | if (oldbfd != NULL) | |
804 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
805 | else | |
806 | { | |
807 | asection *hsec; | |
808 | ||
809 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
810 | indices used by MIPS ELF. */ | |
811 | switch (h->root.type) | |
252b5132 | 812 | { |
45d6a902 AM |
813 | default: |
814 | hsec = NULL; | |
815 | break; | |
252b5132 | 816 | |
45d6a902 AM |
817 | case bfd_link_hash_defined: |
818 | case bfd_link_hash_defweak: | |
819 | hsec = h->root.u.def.section; | |
820 | break; | |
252b5132 | 821 | |
45d6a902 AM |
822 | case bfd_link_hash_common: |
823 | hsec = h->root.u.c.p->section; | |
824 | break; | |
252b5132 | 825 | } |
252b5132 | 826 | |
45d6a902 AM |
827 | if (hsec == NULL) |
828 | olddyn = FALSE; | |
829 | else | |
830 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
831 | } | |
252b5132 | 832 | |
45d6a902 AM |
833 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
834 | respectively, appear to be a definition rather than reference. */ | |
835 | ||
836 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
837 | newdef = FALSE; | |
838 | else | |
839 | newdef = TRUE; | |
840 | ||
841 | if (h->root.type == bfd_link_hash_undefined | |
842 | || h->root.type == bfd_link_hash_undefweak | |
843 | || h->root.type == bfd_link_hash_common) | |
844 | olddef = FALSE; | |
845 | else | |
846 | olddef = TRUE; | |
847 | ||
7479dfd4 L |
848 | /* Check TLS symbol. */ |
849 | if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) | |
850 | && ELF_ST_TYPE (sym->st_info) != h->type) | |
851 | { | |
852 | bfd *ntbfd, *tbfd; | |
853 | bfd_boolean ntdef, tdef; | |
854 | asection *ntsec, *tsec; | |
855 | ||
856 | if (h->type == STT_TLS) | |
857 | { | |
858 | ntbfd = abfd; | |
859 | ntsec = sec; | |
860 | ntdef = newdef; | |
861 | tbfd = oldbfd; | |
862 | tsec = oldsec; | |
863 | tdef = olddef; | |
864 | } | |
865 | else | |
866 | { | |
867 | ntbfd = oldbfd; | |
868 | ntsec = oldsec; | |
869 | ntdef = olddef; | |
870 | tbfd = abfd; | |
871 | tsec = sec; | |
872 | tdef = newdef; | |
873 | } | |
874 | ||
875 | if (tdef && ntdef) | |
876 | (*_bfd_error_handler) | |
877 | (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), | |
878 | tbfd, tsec, ntbfd, ntsec, h->root.root.string); | |
879 | else if (!tdef && !ntdef) | |
880 | (*_bfd_error_handler) | |
881 | (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), | |
882 | tbfd, ntbfd, h->root.root.string); | |
883 | else if (tdef) | |
884 | (*_bfd_error_handler) | |
885 | (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), | |
886 | tbfd, tsec, ntbfd, h->root.root.string); | |
887 | else | |
888 | (*_bfd_error_handler) | |
889 | (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), | |
890 | tbfd, ntbfd, ntsec, h->root.root.string); | |
891 | ||
892 | bfd_set_error (bfd_error_bad_value); | |
893 | return FALSE; | |
894 | } | |
895 | ||
4cc11e76 | 896 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
897 | object or is weak in all dynamic objects. Internal and hidden |
898 | visibility will make it unavailable to dynamic objects. */ | |
f5385ebf | 899 | if (newdyn && !h->dynamic_def) |
45d6a902 AM |
900 | { |
901 | if (!bfd_is_und_section (sec)) | |
f5385ebf | 902 | h->dynamic_def = 1; |
45d6a902 | 903 | else |
252b5132 | 904 | { |
45d6a902 AM |
905 | /* Check if this symbol is weak in all dynamic objects. If it |
906 | is the first time we see it in a dynamic object, we mark | |
907 | if it is weak. Otherwise, we clear it. */ | |
f5385ebf | 908 | if (!h->ref_dynamic) |
79349b09 | 909 | { |
45d6a902 | 910 | if (bind == STB_WEAK) |
f5385ebf | 911 | h->dynamic_weak = 1; |
252b5132 | 912 | } |
45d6a902 | 913 | else if (bind != STB_WEAK) |
f5385ebf | 914 | h->dynamic_weak = 0; |
252b5132 | 915 | } |
45d6a902 | 916 | } |
252b5132 | 917 | |
45d6a902 AM |
918 | /* If the old symbol has non-default visibility, we ignore the new |
919 | definition from a dynamic object. */ | |
920 | if (newdyn | |
9c7a29a3 | 921 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
922 | && !bfd_is_und_section (sec)) |
923 | { | |
924 | *skip = TRUE; | |
925 | /* Make sure this symbol is dynamic. */ | |
f5385ebf | 926 | h->ref_dynamic = 1; |
45d6a902 AM |
927 | /* A protected symbol has external availability. Make sure it is |
928 | recorded as dynamic. | |
929 | ||
930 | FIXME: Should we check type and size for protected symbol? */ | |
931 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
c152c796 | 932 | return bfd_elf_link_record_dynamic_symbol (info, h); |
45d6a902 AM |
933 | else |
934 | return TRUE; | |
935 | } | |
936 | else if (!newdyn | |
9c7a29a3 | 937 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
f5385ebf | 938 | && h->def_dynamic) |
45d6a902 AM |
939 | { |
940 | /* If the new symbol with non-default visibility comes from a | |
941 | relocatable file and the old definition comes from a dynamic | |
942 | object, we remove the old definition. */ | |
943 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
944 | h = *sym_hash; | |
1de1a317 | 945 | |
f6e332e6 | 946 | if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) |
1de1a317 L |
947 | && bfd_is_und_section (sec)) |
948 | { | |
949 | /* If the new symbol is undefined and the old symbol was | |
950 | also undefined before, we need to make sure | |
951 | _bfd_generic_link_add_one_symbol doesn't mess | |
f6e332e6 | 952 | up the linker hash table undefs list. Since the old |
1de1a317 L |
953 | definition came from a dynamic object, it is still on the |
954 | undefs list. */ | |
955 | h->root.type = bfd_link_hash_undefined; | |
1de1a317 L |
956 | h->root.u.undef.abfd = abfd; |
957 | } | |
958 | else | |
959 | { | |
960 | h->root.type = bfd_link_hash_new; | |
961 | h->root.u.undef.abfd = NULL; | |
962 | } | |
963 | ||
f5385ebf | 964 | if (h->def_dynamic) |
252b5132 | 965 | { |
f5385ebf AM |
966 | h->def_dynamic = 0; |
967 | h->ref_dynamic = 1; | |
968 | h->dynamic_def = 1; | |
45d6a902 AM |
969 | } |
970 | /* FIXME: Should we check type and size for protected symbol? */ | |
971 | h->size = 0; | |
972 | h->type = 0; | |
973 | return TRUE; | |
974 | } | |
14a793b2 | 975 | |
79349b09 AM |
976 | /* Differentiate strong and weak symbols. */ |
977 | newweak = bind == STB_WEAK; | |
978 | oldweak = (h->root.type == bfd_link_hash_defweak | |
979 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 980 | |
15b43f48 AM |
981 | /* If a new weak symbol definition comes from a regular file and the |
982 | old symbol comes from a dynamic library, we treat the new one as | |
983 | strong. Similarly, an old weak symbol definition from a regular | |
984 | file is treated as strong when the new symbol comes from a dynamic | |
985 | library. Further, an old weak symbol from a dynamic library is | |
986 | treated as strong if the new symbol is from a dynamic library. | |
987 | This reflects the way glibc's ld.so works. | |
988 | ||
989 | Do this before setting *type_change_ok or *size_change_ok so that | |
990 | we warn properly when dynamic library symbols are overridden. */ | |
991 | ||
992 | if (newdef && !newdyn && olddyn) | |
0f8a2703 | 993 | newweak = FALSE; |
15b43f48 | 994 | if (olddef && newdyn) |
0f8a2703 AM |
995 | oldweak = FALSE; |
996 | ||
79349b09 AM |
997 | /* It's OK to change the type if either the existing symbol or the |
998 | new symbol is weak. A type change is also OK if the old symbol | |
999 | is undefined and the new symbol is defined. */ | |
252b5132 | 1000 | |
79349b09 AM |
1001 | if (oldweak |
1002 | || newweak | |
1003 | || (newdef | |
1004 | && h->root.type == bfd_link_hash_undefined)) | |
1005 | *type_change_ok = TRUE; | |
1006 | ||
1007 | /* It's OK to change the size if either the existing symbol or the | |
1008 | new symbol is weak, or if the old symbol is undefined. */ | |
1009 | ||
1010 | if (*type_change_ok | |
1011 | || h->root.type == bfd_link_hash_undefined) | |
1012 | *size_change_ok = TRUE; | |
45d6a902 | 1013 | |
45d6a902 AM |
1014 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
1015 | symbol, respectively, appears to be a common symbol in a dynamic | |
1016 | object. If a symbol appears in an uninitialized section, and is | |
1017 | not weak, and is not a function, then it may be a common symbol | |
1018 | which was resolved when the dynamic object was created. We want | |
1019 | to treat such symbols specially, because they raise special | |
1020 | considerations when setting the symbol size: if the symbol | |
1021 | appears as a common symbol in a regular object, and the size in | |
1022 | the regular object is larger, we must make sure that we use the | |
1023 | larger size. This problematic case can always be avoided in C, | |
1024 | but it must be handled correctly when using Fortran shared | |
1025 | libraries. | |
1026 | ||
1027 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
1028 | likewise for OLDDYNCOMMON and OLDDEF. | |
1029 | ||
1030 | Note that this test is just a heuristic, and that it is quite | |
1031 | possible to have an uninitialized symbol in a shared object which | |
1032 | is really a definition, rather than a common symbol. This could | |
1033 | lead to some minor confusion when the symbol really is a common | |
1034 | symbol in some regular object. However, I think it will be | |
1035 | harmless. */ | |
1036 | ||
1037 | if (newdyn | |
1038 | && newdef | |
79349b09 | 1039 | && !newweak |
45d6a902 AM |
1040 | && (sec->flags & SEC_ALLOC) != 0 |
1041 | && (sec->flags & SEC_LOAD) == 0 | |
1042 | && sym->st_size > 0 | |
45d6a902 AM |
1043 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
1044 | newdyncommon = TRUE; | |
1045 | else | |
1046 | newdyncommon = FALSE; | |
1047 | ||
1048 | if (olddyn | |
1049 | && olddef | |
1050 | && h->root.type == bfd_link_hash_defined | |
f5385ebf | 1051 | && h->def_dynamic |
45d6a902 AM |
1052 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 |
1053 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
1054 | && h->size > 0 | |
1055 | && h->type != STT_FUNC) | |
1056 | olddyncommon = TRUE; | |
1057 | else | |
1058 | olddyncommon = FALSE; | |
1059 | ||
45d6a902 AM |
1060 | /* If both the old and the new symbols look like common symbols in a |
1061 | dynamic object, set the size of the symbol to the larger of the | |
1062 | two. */ | |
1063 | ||
1064 | if (olddyncommon | |
1065 | && newdyncommon | |
1066 | && sym->st_size != h->size) | |
1067 | { | |
1068 | /* Since we think we have two common symbols, issue a multiple | |
1069 | common warning if desired. Note that we only warn if the | |
1070 | size is different. If the size is the same, we simply let | |
1071 | the old symbol override the new one as normally happens with | |
1072 | symbols defined in dynamic objects. */ | |
1073 | ||
1074 | if (! ((*info->callbacks->multiple_common) | |
1075 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1076 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1077 | return FALSE; | |
252b5132 | 1078 | |
45d6a902 AM |
1079 | if (sym->st_size > h->size) |
1080 | h->size = sym->st_size; | |
252b5132 | 1081 | |
45d6a902 | 1082 | *size_change_ok = TRUE; |
252b5132 RH |
1083 | } |
1084 | ||
45d6a902 AM |
1085 | /* If we are looking at a dynamic object, and we have found a |
1086 | definition, we need to see if the symbol was already defined by | |
1087 | some other object. If so, we want to use the existing | |
1088 | definition, and we do not want to report a multiple symbol | |
1089 | definition error; we do this by clobbering *PSEC to be | |
1090 | bfd_und_section_ptr. | |
1091 | ||
1092 | We treat a common symbol as a definition if the symbol in the | |
1093 | shared library is a function, since common symbols always | |
1094 | represent variables; this can cause confusion in principle, but | |
1095 | any such confusion would seem to indicate an erroneous program or | |
1096 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1097 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1098 | |
1099 | if (newdyn | |
1100 | && newdef | |
1101 | && (olddef | |
1102 | || (h->root.type == bfd_link_hash_common | |
79349b09 | 1103 | && (newweak |
0f8a2703 | 1104 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1105 | { |
1106 | *override = TRUE; | |
1107 | newdef = FALSE; | |
1108 | newdyncommon = FALSE; | |
252b5132 | 1109 | |
45d6a902 AM |
1110 | *psec = sec = bfd_und_section_ptr; |
1111 | *size_change_ok = TRUE; | |
252b5132 | 1112 | |
45d6a902 AM |
1113 | /* If we get here when the old symbol is a common symbol, then |
1114 | we are explicitly letting it override a weak symbol or | |
1115 | function in a dynamic object, and we don't want to warn about | |
1116 | a type change. If the old symbol is a defined symbol, a type | |
1117 | change warning may still be appropriate. */ | |
252b5132 | 1118 | |
45d6a902 AM |
1119 | if (h->root.type == bfd_link_hash_common) |
1120 | *type_change_ok = TRUE; | |
1121 | } | |
1122 | ||
1123 | /* Handle the special case of an old common symbol merging with a | |
1124 | new symbol which looks like a common symbol in a shared object. | |
1125 | We change *PSEC and *PVALUE to make the new symbol look like a | |
1126 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
1127 | the right thing. */ | |
1128 | ||
1129 | if (newdyncommon | |
1130 | && h->root.type == bfd_link_hash_common) | |
1131 | { | |
1132 | *override = TRUE; | |
1133 | newdef = FALSE; | |
1134 | newdyncommon = FALSE; | |
1135 | *pvalue = sym->st_size; | |
1136 | *psec = sec = bfd_com_section_ptr; | |
1137 | *size_change_ok = TRUE; | |
1138 | } | |
1139 | ||
1140 | /* If the old symbol is from a dynamic object, and the new symbol is | |
1141 | a definition which is not from a dynamic object, then the new | |
1142 | symbol overrides the old symbol. Symbols from regular files | |
1143 | always take precedence over symbols from dynamic objects, even if | |
1144 | they are defined after the dynamic object in the link. | |
1145 | ||
1146 | As above, we again permit a common symbol in a regular object to | |
1147 | override a definition in a shared object if the shared object | |
0f8a2703 | 1148 | symbol is a function or is weak. */ |
45d6a902 AM |
1149 | |
1150 | flip = NULL; | |
1151 | if (! newdyn | |
1152 | && (newdef | |
1153 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1154 | && (oldweak |
1155 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1156 | && olddyn |
1157 | && olddef | |
f5385ebf | 1158 | && h->def_dynamic) |
45d6a902 AM |
1159 | { |
1160 | /* Change the hash table entry to undefined, and let | |
1161 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1162 | new definition. */ | |
1163 | ||
1164 | h->root.type = bfd_link_hash_undefined; | |
1165 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1166 | *size_change_ok = TRUE; | |
1167 | ||
1168 | olddef = FALSE; | |
1169 | olddyncommon = FALSE; | |
1170 | ||
1171 | /* We again permit a type change when a common symbol may be | |
1172 | overriding a function. */ | |
1173 | ||
1174 | if (bfd_is_com_section (sec)) | |
1175 | *type_change_ok = TRUE; | |
1176 | ||
1177 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1178 | flip = *sym_hash; | |
1179 | else | |
1180 | /* This union may have been set to be non-NULL when this symbol | |
1181 | was seen in a dynamic object. We must force the union to be | |
1182 | NULL, so that it is correct for a regular symbol. */ | |
1183 | h->verinfo.vertree = NULL; | |
1184 | } | |
1185 | ||
1186 | /* Handle the special case of a new common symbol merging with an | |
1187 | old symbol that looks like it might be a common symbol defined in | |
1188 | a shared object. Note that we have already handled the case in | |
1189 | which a new common symbol should simply override the definition | |
1190 | in the shared library. */ | |
1191 | ||
1192 | if (! newdyn | |
1193 | && bfd_is_com_section (sec) | |
1194 | && olddyncommon) | |
1195 | { | |
1196 | /* It would be best if we could set the hash table entry to a | |
1197 | common symbol, but we don't know what to use for the section | |
1198 | or the alignment. */ | |
1199 | if (! ((*info->callbacks->multiple_common) | |
1200 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1201 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1202 | return FALSE; | |
1203 | ||
4cc11e76 | 1204 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1205 | larger, pretend that the new symbol has its size. */ |
1206 | ||
1207 | if (h->size > *pvalue) | |
1208 | *pvalue = h->size; | |
1209 | ||
1210 | /* FIXME: We no longer know the alignment required by the symbol | |
1211 | in the dynamic object, so we just wind up using the one from | |
1212 | the regular object. */ | |
1213 | ||
1214 | olddef = FALSE; | |
1215 | olddyncommon = FALSE; | |
1216 | ||
1217 | h->root.type = bfd_link_hash_undefined; | |
1218 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1219 | ||
1220 | *size_change_ok = TRUE; | |
1221 | *type_change_ok = TRUE; | |
1222 | ||
1223 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1224 | flip = *sym_hash; | |
1225 | else | |
1226 | h->verinfo.vertree = NULL; | |
1227 | } | |
1228 | ||
1229 | if (flip != NULL) | |
1230 | { | |
1231 | /* Handle the case where we had a versioned symbol in a dynamic | |
1232 | library and now find a definition in a normal object. In this | |
1233 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1234 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1235 | flip->root.type = h->root.type; |
1236 | h->root.type = bfd_link_hash_indirect; | |
1237 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1238 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1239 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
f5385ebf | 1240 | if (h->def_dynamic) |
45d6a902 | 1241 | { |
f5385ebf AM |
1242 | h->def_dynamic = 0; |
1243 | flip->ref_dynamic = 1; | |
45d6a902 AM |
1244 | } |
1245 | } | |
1246 | ||
45d6a902 AM |
1247 | return TRUE; |
1248 | } | |
1249 | ||
1250 | /* This function is called to create an indirect symbol from the | |
1251 | default for the symbol with the default version if needed. The | |
1252 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1253 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1254 | |
1255 | bfd_boolean | |
268b6b39 AM |
1256 | _bfd_elf_add_default_symbol (bfd *abfd, |
1257 | struct bfd_link_info *info, | |
1258 | struct elf_link_hash_entry *h, | |
1259 | const char *name, | |
1260 | Elf_Internal_Sym *sym, | |
1261 | asection **psec, | |
1262 | bfd_vma *value, | |
1263 | bfd_boolean *dynsym, | |
0f8a2703 | 1264 | bfd_boolean override) |
45d6a902 AM |
1265 | { |
1266 | bfd_boolean type_change_ok; | |
1267 | bfd_boolean size_change_ok; | |
1268 | bfd_boolean skip; | |
1269 | char *shortname; | |
1270 | struct elf_link_hash_entry *hi; | |
1271 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1272 | const struct elf_backend_data *bed; |
45d6a902 AM |
1273 | bfd_boolean collect; |
1274 | bfd_boolean dynamic; | |
1275 | char *p; | |
1276 | size_t len, shortlen; | |
1277 | asection *sec; | |
1278 | ||
1279 | /* If this symbol has a version, and it is the default version, we | |
1280 | create an indirect symbol from the default name to the fully | |
1281 | decorated name. This will cause external references which do not | |
1282 | specify a version to be bound to this version of the symbol. */ | |
1283 | p = strchr (name, ELF_VER_CHR); | |
1284 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1285 | return TRUE; | |
1286 | ||
1287 | if (override) | |
1288 | { | |
4cc11e76 | 1289 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1290 | need to create the indirect symbol from the default name. */ |
1291 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1292 | FALSE, FALSE); | |
1293 | BFD_ASSERT (hi != NULL); | |
1294 | if (hi == h) | |
1295 | return TRUE; | |
1296 | while (hi->root.type == bfd_link_hash_indirect | |
1297 | || hi->root.type == bfd_link_hash_warning) | |
1298 | { | |
1299 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1300 | if (hi == h) | |
1301 | return TRUE; | |
1302 | } | |
1303 | } | |
1304 | ||
1305 | bed = get_elf_backend_data (abfd); | |
1306 | collect = bed->collect; | |
1307 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1308 | ||
1309 | shortlen = p - name; | |
1310 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1311 | if (shortname == NULL) | |
1312 | return FALSE; | |
1313 | memcpy (shortname, name, shortlen); | |
1314 | shortname[shortlen] = '\0'; | |
1315 | ||
1316 | /* We are going to create a new symbol. Merge it with any existing | |
1317 | symbol with this name. For the purposes of the merge, act as | |
1318 | though we were defining the symbol we just defined, although we | |
1319 | actually going to define an indirect symbol. */ | |
1320 | type_change_ok = FALSE; | |
1321 | size_change_ok = FALSE; | |
1322 | sec = *psec; | |
1323 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1324 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1325 | &size_change_ok)) |
45d6a902 AM |
1326 | return FALSE; |
1327 | ||
1328 | if (skip) | |
1329 | goto nondefault; | |
1330 | ||
1331 | if (! override) | |
1332 | { | |
1333 | bh = &hi->root; | |
1334 | if (! (_bfd_generic_link_add_one_symbol | |
1335 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1336 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1337 | return FALSE; |
1338 | hi = (struct elf_link_hash_entry *) bh; | |
1339 | } | |
1340 | else | |
1341 | { | |
1342 | /* In this case the symbol named SHORTNAME is overriding the | |
1343 | indirect symbol we want to add. We were planning on making | |
1344 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1345 | is the name without a version. NAME is the fully versioned | |
1346 | name, and it is the default version. | |
1347 | ||
1348 | Overriding means that we already saw a definition for the | |
1349 | symbol SHORTNAME in a regular object, and it is overriding | |
1350 | the symbol defined in the dynamic object. | |
1351 | ||
1352 | When this happens, we actually want to change NAME, the | |
1353 | symbol we just added, to refer to SHORTNAME. This will cause | |
1354 | references to NAME in the shared object to become references | |
1355 | to SHORTNAME in the regular object. This is what we expect | |
1356 | when we override a function in a shared object: that the | |
1357 | references in the shared object will be mapped to the | |
1358 | definition in the regular object. */ | |
1359 | ||
1360 | while (hi->root.type == bfd_link_hash_indirect | |
1361 | || hi->root.type == bfd_link_hash_warning) | |
1362 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1363 | ||
1364 | h->root.type = bfd_link_hash_indirect; | |
1365 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
f5385ebf | 1366 | if (h->def_dynamic) |
45d6a902 | 1367 | { |
f5385ebf AM |
1368 | h->def_dynamic = 0; |
1369 | hi->ref_dynamic = 1; | |
1370 | if (hi->ref_regular | |
1371 | || hi->def_regular) | |
45d6a902 | 1372 | { |
c152c796 | 1373 | if (! bfd_elf_link_record_dynamic_symbol (info, hi)) |
45d6a902 AM |
1374 | return FALSE; |
1375 | } | |
1376 | } | |
1377 | ||
1378 | /* Now set HI to H, so that the following code will set the | |
1379 | other fields correctly. */ | |
1380 | hi = h; | |
1381 | } | |
1382 | ||
1383 | /* If there is a duplicate definition somewhere, then HI may not | |
1384 | point to an indirect symbol. We will have reported an error to | |
1385 | the user in that case. */ | |
1386 | ||
1387 | if (hi->root.type == bfd_link_hash_indirect) | |
1388 | { | |
1389 | struct elf_link_hash_entry *ht; | |
1390 | ||
45d6a902 AM |
1391 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1392 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1393 | ||
1394 | /* See if the new flags lead us to realize that the symbol must | |
1395 | be dynamic. */ | |
1396 | if (! *dynsym) | |
1397 | { | |
1398 | if (! dynamic) | |
1399 | { | |
1400 | if (info->shared | |
f5385ebf | 1401 | || hi->ref_dynamic) |
45d6a902 AM |
1402 | *dynsym = TRUE; |
1403 | } | |
1404 | else | |
1405 | { | |
f5385ebf | 1406 | if (hi->ref_regular) |
45d6a902 AM |
1407 | *dynsym = TRUE; |
1408 | } | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | /* We also need to define an indirection from the nondefault version | |
1413 | of the symbol. */ | |
1414 | ||
1415 | nondefault: | |
1416 | len = strlen (name); | |
1417 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1418 | if (shortname == NULL) | |
1419 | return FALSE; | |
1420 | memcpy (shortname, name, shortlen); | |
1421 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1422 | ||
1423 | /* Once again, merge with any existing symbol. */ | |
1424 | type_change_ok = FALSE; | |
1425 | size_change_ok = FALSE; | |
1426 | sec = *psec; | |
1427 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1428 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1429 | &size_change_ok)) |
45d6a902 AM |
1430 | return FALSE; |
1431 | ||
1432 | if (skip) | |
1433 | return TRUE; | |
1434 | ||
1435 | if (override) | |
1436 | { | |
1437 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1438 | the type of override we do in the case above unless it is | |
4cc11e76 | 1439 | overridden by a versioned definition. */ |
45d6a902 AM |
1440 | if (hi->root.type != bfd_link_hash_defined |
1441 | && hi->root.type != bfd_link_hash_defweak) | |
1442 | (*_bfd_error_handler) | |
d003868e AM |
1443 | (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), |
1444 | abfd, shortname); | |
45d6a902 AM |
1445 | } |
1446 | else | |
1447 | { | |
1448 | bh = &hi->root; | |
1449 | if (! (_bfd_generic_link_add_one_symbol | |
1450 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1451 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1452 | return FALSE; |
1453 | hi = (struct elf_link_hash_entry *) bh; | |
1454 | ||
1455 | /* If there is a duplicate definition somewhere, then HI may not | |
1456 | point to an indirect symbol. We will have reported an error | |
1457 | to the user in that case. */ | |
1458 | ||
1459 | if (hi->root.type == bfd_link_hash_indirect) | |
1460 | { | |
45d6a902 AM |
1461 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1462 | ||
1463 | /* See if the new flags lead us to realize that the symbol | |
1464 | must be dynamic. */ | |
1465 | if (! *dynsym) | |
1466 | { | |
1467 | if (! dynamic) | |
1468 | { | |
1469 | if (info->shared | |
f5385ebf | 1470 | || hi->ref_dynamic) |
45d6a902 AM |
1471 | *dynsym = TRUE; |
1472 | } | |
1473 | else | |
1474 | { | |
f5385ebf | 1475 | if (hi->ref_regular) |
45d6a902 AM |
1476 | *dynsym = TRUE; |
1477 | } | |
1478 | } | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | return TRUE; | |
1483 | } | |
1484 | \f | |
1485 | /* This routine is used to export all defined symbols into the dynamic | |
1486 | symbol table. It is called via elf_link_hash_traverse. */ | |
1487 | ||
1488 | bfd_boolean | |
268b6b39 | 1489 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1490 | { |
268b6b39 | 1491 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1492 | |
1493 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1494 | if (h->root.type == bfd_link_hash_indirect) | |
1495 | return TRUE; | |
1496 | ||
1497 | if (h->root.type == bfd_link_hash_warning) | |
1498 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1499 | ||
1500 | if (h->dynindx == -1 | |
f5385ebf AM |
1501 | && (h->def_regular |
1502 | || h->ref_regular)) | |
45d6a902 AM |
1503 | { |
1504 | struct bfd_elf_version_tree *t; | |
1505 | struct bfd_elf_version_expr *d; | |
1506 | ||
1507 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1508 | { | |
108ba305 | 1509 | if (t->globals.list != NULL) |
45d6a902 | 1510 | { |
108ba305 JJ |
1511 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1512 | if (d != NULL) | |
1513 | goto doit; | |
45d6a902 AM |
1514 | } |
1515 | ||
108ba305 | 1516 | if (t->locals.list != NULL) |
45d6a902 | 1517 | { |
108ba305 JJ |
1518 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1519 | if (d != NULL) | |
1520 | return TRUE; | |
45d6a902 AM |
1521 | } |
1522 | } | |
1523 | ||
1524 | if (!eif->verdefs) | |
1525 | { | |
1526 | doit: | |
c152c796 | 1527 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
1528 | { |
1529 | eif->failed = TRUE; | |
1530 | return FALSE; | |
1531 | } | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | return TRUE; | |
1536 | } | |
1537 | \f | |
1538 | /* Look through the symbols which are defined in other shared | |
1539 | libraries and referenced here. Update the list of version | |
1540 | dependencies. This will be put into the .gnu.version_r section. | |
1541 | This function is called via elf_link_hash_traverse. */ | |
1542 | ||
1543 | bfd_boolean | |
268b6b39 AM |
1544 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1545 | void *data) | |
45d6a902 | 1546 | { |
268b6b39 | 1547 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1548 | Elf_Internal_Verneed *t; |
1549 | Elf_Internal_Vernaux *a; | |
1550 | bfd_size_type amt; | |
1551 | ||
1552 | if (h->root.type == bfd_link_hash_warning) | |
1553 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1554 | ||
1555 | /* We only care about symbols defined in shared objects with version | |
1556 | information. */ | |
f5385ebf AM |
1557 | if (!h->def_dynamic |
1558 | || h->def_regular | |
45d6a902 AM |
1559 | || h->dynindx == -1 |
1560 | || h->verinfo.verdef == NULL) | |
1561 | return TRUE; | |
1562 | ||
1563 | /* See if we already know about this version. */ | |
1564 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1565 | { | |
1566 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1567 | continue; | |
1568 | ||
1569 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1570 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1571 | return TRUE; | |
1572 | ||
1573 | break; | |
1574 | } | |
1575 | ||
1576 | /* This is a new version. Add it to tree we are building. */ | |
1577 | ||
1578 | if (t == NULL) | |
1579 | { | |
1580 | amt = sizeof *t; | |
268b6b39 | 1581 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1582 | if (t == NULL) |
1583 | { | |
1584 | rinfo->failed = TRUE; | |
1585 | return FALSE; | |
1586 | } | |
1587 | ||
1588 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1589 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1590 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1591 | } | |
1592 | ||
1593 | amt = sizeof *a; | |
268b6b39 | 1594 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1595 | |
1596 | /* Note that we are copying a string pointer here, and testing it | |
1597 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1598 | discard the string data when low in memory, this will have to be | |
1599 | fixed. */ | |
1600 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1601 | ||
1602 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1603 | a->vna_nextptr = t->vn_auxptr; | |
1604 | ||
1605 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1606 | ++rinfo->vers; | |
1607 | ||
1608 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1609 | ||
1610 | t->vn_auxptr = a; | |
1611 | ||
1612 | return TRUE; | |
1613 | } | |
1614 | ||
1615 | /* Figure out appropriate versions for all the symbols. We may not | |
1616 | have the version number script until we have read all of the input | |
1617 | files, so until that point we don't know which symbols should be | |
1618 | local. This function is called via elf_link_hash_traverse. */ | |
1619 | ||
1620 | bfd_boolean | |
268b6b39 | 1621 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1622 | { |
1623 | struct elf_assign_sym_version_info *sinfo; | |
1624 | struct bfd_link_info *info; | |
9c5bfbb7 | 1625 | const struct elf_backend_data *bed; |
45d6a902 AM |
1626 | struct elf_info_failed eif; |
1627 | char *p; | |
1628 | bfd_size_type amt; | |
1629 | ||
268b6b39 | 1630 | sinfo = data; |
45d6a902 AM |
1631 | info = sinfo->info; |
1632 | ||
1633 | if (h->root.type == bfd_link_hash_warning) | |
1634 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1635 | ||
1636 | /* Fix the symbol flags. */ | |
1637 | eif.failed = FALSE; | |
1638 | eif.info = info; | |
1639 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1640 | { | |
1641 | if (eif.failed) | |
1642 | sinfo->failed = TRUE; | |
1643 | return FALSE; | |
1644 | } | |
1645 | ||
1646 | /* We only need version numbers for symbols defined in regular | |
1647 | objects. */ | |
f5385ebf | 1648 | if (!h->def_regular) |
45d6a902 AM |
1649 | return TRUE; |
1650 | ||
1651 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1652 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1653 | if (p != NULL && h->verinfo.vertree == NULL) | |
1654 | { | |
1655 | struct bfd_elf_version_tree *t; | |
1656 | bfd_boolean hidden; | |
1657 | ||
1658 | hidden = TRUE; | |
1659 | ||
1660 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1661 | not a hidden symbol. */ | |
1662 | ++p; | |
1663 | if (*p == ELF_VER_CHR) | |
1664 | { | |
1665 | hidden = FALSE; | |
1666 | ++p; | |
1667 | } | |
1668 | ||
1669 | /* If there is no version string, we can just return out. */ | |
1670 | if (*p == '\0') | |
1671 | { | |
1672 | if (hidden) | |
f5385ebf | 1673 | h->hidden = 1; |
45d6a902 AM |
1674 | return TRUE; |
1675 | } | |
1676 | ||
1677 | /* Look for the version. If we find it, it is no longer weak. */ | |
1678 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1679 | { | |
1680 | if (strcmp (t->name, p) == 0) | |
1681 | { | |
1682 | size_t len; | |
1683 | char *alc; | |
1684 | struct bfd_elf_version_expr *d; | |
1685 | ||
1686 | len = p - h->root.root.string; | |
268b6b39 | 1687 | alc = bfd_malloc (len); |
45d6a902 AM |
1688 | if (alc == NULL) |
1689 | return FALSE; | |
1690 | memcpy (alc, h->root.root.string, len - 1); | |
1691 | alc[len - 1] = '\0'; | |
1692 | if (alc[len - 2] == ELF_VER_CHR) | |
1693 | alc[len - 2] = '\0'; | |
1694 | ||
1695 | h->verinfo.vertree = t; | |
1696 | t->used = TRUE; | |
1697 | d = NULL; | |
1698 | ||
108ba305 JJ |
1699 | if (t->globals.list != NULL) |
1700 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1701 | |
1702 | /* See if there is anything to force this symbol to | |
1703 | local scope. */ | |
108ba305 | 1704 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1705 | { |
108ba305 JJ |
1706 | d = (*t->match) (&t->locals, NULL, alc); |
1707 | if (d != NULL | |
1708 | && h->dynindx != -1 | |
1709 | && info->shared | |
1710 | && ! info->export_dynamic) | |
1711 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1712 | } |
1713 | ||
1714 | free (alc); | |
1715 | break; | |
1716 | } | |
1717 | } | |
1718 | ||
1719 | /* If we are building an application, we need to create a | |
1720 | version node for this version. */ | |
36af4a4e | 1721 | if (t == NULL && info->executable) |
45d6a902 AM |
1722 | { |
1723 | struct bfd_elf_version_tree **pp; | |
1724 | int version_index; | |
1725 | ||
1726 | /* If we aren't going to export this symbol, we don't need | |
1727 | to worry about it. */ | |
1728 | if (h->dynindx == -1) | |
1729 | return TRUE; | |
1730 | ||
1731 | amt = sizeof *t; | |
108ba305 | 1732 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1733 | if (t == NULL) |
1734 | { | |
1735 | sinfo->failed = TRUE; | |
1736 | return FALSE; | |
1737 | } | |
1738 | ||
45d6a902 | 1739 | t->name = p; |
45d6a902 AM |
1740 | t->name_indx = (unsigned int) -1; |
1741 | t->used = TRUE; | |
1742 | ||
1743 | version_index = 1; | |
1744 | /* Don't count anonymous version tag. */ | |
1745 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1746 | version_index = 0; | |
1747 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1748 | ++version_index; | |
1749 | t->vernum = version_index; | |
1750 | ||
1751 | *pp = t; | |
1752 | ||
1753 | h->verinfo.vertree = t; | |
1754 | } | |
1755 | else if (t == NULL) | |
1756 | { | |
1757 | /* We could not find the version for a symbol when | |
1758 | generating a shared archive. Return an error. */ | |
1759 | (*_bfd_error_handler) | |
d003868e AM |
1760 | (_("%B: undefined versioned symbol name %s"), |
1761 | sinfo->output_bfd, h->root.root.string); | |
45d6a902 AM |
1762 | bfd_set_error (bfd_error_bad_value); |
1763 | sinfo->failed = TRUE; | |
1764 | return FALSE; | |
1765 | } | |
1766 | ||
1767 | if (hidden) | |
f5385ebf | 1768 | h->hidden = 1; |
45d6a902 AM |
1769 | } |
1770 | ||
1771 | /* If we don't have a version for this symbol, see if we can find | |
1772 | something. */ | |
1773 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1774 | { | |
1775 | struct bfd_elf_version_tree *t; | |
1776 | struct bfd_elf_version_tree *local_ver; | |
1777 | struct bfd_elf_version_expr *d; | |
1778 | ||
1779 | /* See if can find what version this symbol is in. If the | |
1780 | symbol is supposed to be local, then don't actually register | |
1781 | it. */ | |
1782 | local_ver = NULL; | |
1783 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1784 | { | |
108ba305 | 1785 | if (t->globals.list != NULL) |
45d6a902 AM |
1786 | { |
1787 | bfd_boolean matched; | |
1788 | ||
1789 | matched = FALSE; | |
108ba305 JJ |
1790 | d = NULL; |
1791 | while ((d = (*t->match) (&t->globals, d, | |
1792 | h->root.root.string)) != NULL) | |
1793 | if (d->symver) | |
1794 | matched = TRUE; | |
1795 | else | |
1796 | { | |
1797 | /* There is a version without definition. Make | |
1798 | the symbol the default definition for this | |
1799 | version. */ | |
1800 | h->verinfo.vertree = t; | |
1801 | local_ver = NULL; | |
1802 | d->script = 1; | |
1803 | break; | |
1804 | } | |
45d6a902 AM |
1805 | if (d != NULL) |
1806 | break; | |
1807 | else if (matched) | |
1808 | /* There is no undefined version for this symbol. Hide the | |
1809 | default one. */ | |
1810 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1811 | } | |
1812 | ||
108ba305 | 1813 | if (t->locals.list != NULL) |
45d6a902 | 1814 | { |
108ba305 JJ |
1815 | d = NULL; |
1816 | while ((d = (*t->match) (&t->locals, d, | |
1817 | h->root.root.string)) != NULL) | |
45d6a902 | 1818 | { |
108ba305 | 1819 | local_ver = t; |
45d6a902 | 1820 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1821 | explicit, perhaps even global, match. |
1822 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1823 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1824 | break; | |
45d6a902 AM |
1825 | } |
1826 | ||
1827 | if (d != NULL) | |
1828 | break; | |
1829 | } | |
1830 | } | |
1831 | ||
1832 | if (local_ver != NULL) | |
1833 | { | |
1834 | h->verinfo.vertree = local_ver; | |
1835 | if (h->dynindx != -1 | |
1836 | && info->shared | |
1837 | && ! info->export_dynamic) | |
1838 | { | |
1839 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1840 | } | |
1841 | } | |
1842 | } | |
1843 | ||
1844 | return TRUE; | |
1845 | } | |
1846 | \f | |
45d6a902 AM |
1847 | /* Read and swap the relocs from the section indicated by SHDR. This |
1848 | may be either a REL or a RELA section. The relocations are | |
1849 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1850 | which should have already been allocated to contain enough space. | |
1851 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1852 | relocations should be stored. | |
1853 | ||
1854 | Returns FALSE if something goes wrong. */ | |
1855 | ||
1856 | static bfd_boolean | |
268b6b39 | 1857 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1858 | asection *sec, |
268b6b39 AM |
1859 | Elf_Internal_Shdr *shdr, |
1860 | void *external_relocs, | |
1861 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1862 | { |
9c5bfbb7 | 1863 | const struct elf_backend_data *bed; |
268b6b39 | 1864 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1865 | const bfd_byte *erela; |
1866 | const bfd_byte *erelaend; | |
1867 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1868 | Elf_Internal_Shdr *symtab_hdr; |
1869 | size_t nsyms; | |
45d6a902 | 1870 | |
45d6a902 AM |
1871 | /* Position ourselves at the start of the section. */ |
1872 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1873 | return FALSE; | |
1874 | ||
1875 | /* Read the relocations. */ | |
1876 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1877 | return FALSE; | |
1878 | ||
243ef1e0 L |
1879 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1880 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1881 | ||
45d6a902 AM |
1882 | bed = get_elf_backend_data (abfd); |
1883 | ||
1884 | /* Convert the external relocations to the internal format. */ | |
1885 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1886 | swap_in = bed->s->swap_reloc_in; | |
1887 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1888 | swap_in = bed->s->swap_reloca_in; | |
1889 | else | |
1890 | { | |
1891 | bfd_set_error (bfd_error_wrong_format); | |
1892 | return FALSE; | |
1893 | } | |
1894 | ||
1895 | erela = external_relocs; | |
51992aec | 1896 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1897 | irela = internal_relocs; |
1898 | while (erela < erelaend) | |
1899 | { | |
243ef1e0 L |
1900 | bfd_vma r_symndx; |
1901 | ||
45d6a902 | 1902 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1903 | r_symndx = ELF32_R_SYM (irela->r_info); |
1904 | if (bed->s->arch_size == 64) | |
1905 | r_symndx >>= 24; | |
1906 | if ((size_t) r_symndx >= nsyms) | |
1907 | { | |
1908 | (*_bfd_error_handler) | |
d003868e AM |
1909 | (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" |
1910 | " for offset 0x%lx in section `%A'"), | |
1911 | abfd, sec, | |
1912 | (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); | |
243ef1e0 L |
1913 | bfd_set_error (bfd_error_bad_value); |
1914 | return FALSE; | |
1915 | } | |
45d6a902 AM |
1916 | irela += bed->s->int_rels_per_ext_rel; |
1917 | erela += shdr->sh_entsize; | |
1918 | } | |
1919 | ||
1920 | return TRUE; | |
1921 | } | |
1922 | ||
1923 | /* Read and swap the relocs for a section O. They may have been | |
1924 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1925 | not NULL, they are used as buffers to read into. They are known to | |
1926 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1927 | the return value is allocated using either malloc or bfd_alloc, | |
1928 | according to the KEEP_MEMORY argument. If O has two relocation | |
1929 | sections (both REL and RELA relocations), then the REL_HDR | |
1930 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1931 | REL_HDR2 relocations. */ | |
1932 | ||
1933 | Elf_Internal_Rela * | |
268b6b39 AM |
1934 | _bfd_elf_link_read_relocs (bfd *abfd, |
1935 | asection *o, | |
1936 | void *external_relocs, | |
1937 | Elf_Internal_Rela *internal_relocs, | |
1938 | bfd_boolean keep_memory) | |
45d6a902 AM |
1939 | { |
1940 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 1941 | void *alloc1 = NULL; |
45d6a902 | 1942 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 1943 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1944 | |
1945 | if (elf_section_data (o)->relocs != NULL) | |
1946 | return elf_section_data (o)->relocs; | |
1947 | ||
1948 | if (o->reloc_count == 0) | |
1949 | return NULL; | |
1950 | ||
1951 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1952 | ||
1953 | if (internal_relocs == NULL) | |
1954 | { | |
1955 | bfd_size_type size; | |
1956 | ||
1957 | size = o->reloc_count; | |
1958 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
1959 | if (keep_memory) | |
268b6b39 | 1960 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 1961 | else |
268b6b39 | 1962 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
1963 | if (internal_relocs == NULL) |
1964 | goto error_return; | |
1965 | } | |
1966 | ||
1967 | if (external_relocs == NULL) | |
1968 | { | |
1969 | bfd_size_type size = rel_hdr->sh_size; | |
1970 | ||
1971 | if (elf_section_data (o)->rel_hdr2) | |
1972 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 1973 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
1974 | if (alloc1 == NULL) |
1975 | goto error_return; | |
1976 | external_relocs = alloc1; | |
1977 | } | |
1978 | ||
243ef1e0 | 1979 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
1980 | external_relocs, |
1981 | internal_relocs)) | |
1982 | goto error_return; | |
51992aec AM |
1983 | if (elf_section_data (o)->rel_hdr2 |
1984 | && (!elf_link_read_relocs_from_section | |
1985 | (abfd, o, | |
1986 | elf_section_data (o)->rel_hdr2, | |
1987 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
1988 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
1989 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
1990 | goto error_return; |
1991 | ||
1992 | /* Cache the results for next time, if we can. */ | |
1993 | if (keep_memory) | |
1994 | elf_section_data (o)->relocs = internal_relocs; | |
1995 | ||
1996 | if (alloc1 != NULL) | |
1997 | free (alloc1); | |
1998 | ||
1999 | /* Don't free alloc2, since if it was allocated we are passing it | |
2000 | back (under the name of internal_relocs). */ | |
2001 | ||
2002 | return internal_relocs; | |
2003 | ||
2004 | error_return: | |
2005 | if (alloc1 != NULL) | |
2006 | free (alloc1); | |
2007 | if (alloc2 != NULL) | |
2008 | free (alloc2); | |
2009 | return NULL; | |
2010 | } | |
2011 | ||
2012 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2013 | section header for a section containing relocations for O. */ | |
2014 | ||
2015 | bfd_boolean | |
268b6b39 AM |
2016 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
2017 | Elf_Internal_Shdr *rel_hdr, | |
2018 | asection *o) | |
45d6a902 AM |
2019 | { |
2020 | bfd_size_type reloc_count; | |
2021 | bfd_size_type num_rel_hashes; | |
2022 | ||
2023 | /* Figure out how many relocations there will be. */ | |
2024 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2025 | reloc_count = elf_section_data (o)->rel_count; | |
2026 | else | |
2027 | reloc_count = elf_section_data (o)->rel_count2; | |
2028 | ||
2029 | num_rel_hashes = o->reloc_count; | |
2030 | if (num_rel_hashes < reloc_count) | |
2031 | num_rel_hashes = reloc_count; | |
2032 | ||
2033 | /* That allows us to calculate the size of the section. */ | |
2034 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2035 | ||
2036 | /* The contents field must last into write_object_contents, so we | |
2037 | allocate it with bfd_alloc rather than malloc. Also since we | |
2038 | cannot be sure that the contents will actually be filled in, | |
2039 | we zero the allocated space. */ | |
268b6b39 | 2040 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
2041 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
2042 | return FALSE; | |
2043 | ||
2044 | /* We only allocate one set of hash entries, so we only do it the | |
2045 | first time we are called. */ | |
2046 | if (elf_section_data (o)->rel_hashes == NULL | |
2047 | && num_rel_hashes) | |
2048 | { | |
2049 | struct elf_link_hash_entry **p; | |
2050 | ||
268b6b39 | 2051 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
2052 | if (p == NULL) |
2053 | return FALSE; | |
2054 | ||
2055 | elf_section_data (o)->rel_hashes = p; | |
2056 | } | |
2057 | ||
2058 | return TRUE; | |
2059 | } | |
2060 | ||
2061 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2062 | originated from the section given by INPUT_REL_HDR) to the | |
2063 | OUTPUT_BFD. */ | |
2064 | ||
2065 | bfd_boolean | |
268b6b39 AM |
2066 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
2067 | asection *input_section, | |
2068 | Elf_Internal_Shdr *input_rel_hdr, | |
2069 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 AM |
2070 | { |
2071 | Elf_Internal_Rela *irela; | |
2072 | Elf_Internal_Rela *irelaend; | |
2073 | bfd_byte *erel; | |
2074 | Elf_Internal_Shdr *output_rel_hdr; | |
2075 | asection *output_section; | |
2076 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 2077 | const struct elf_backend_data *bed; |
268b6b39 | 2078 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
2079 | |
2080 | output_section = input_section->output_section; | |
2081 | output_rel_hdr = NULL; | |
2082 | ||
2083 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2084 | == input_rel_hdr->sh_entsize) | |
2085 | { | |
2086 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2087 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2088 | } | |
2089 | else if (elf_section_data (output_section)->rel_hdr2 | |
2090 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2091 | == input_rel_hdr->sh_entsize)) | |
2092 | { | |
2093 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2094 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2095 | } | |
2096 | else | |
2097 | { | |
2098 | (*_bfd_error_handler) | |
d003868e AM |
2099 | (_("%B: relocation size mismatch in %B section %A"), |
2100 | output_bfd, input_section->owner, input_section); | |
45d6a902 AM |
2101 | bfd_set_error (bfd_error_wrong_object_format); |
2102 | return FALSE; | |
2103 | } | |
2104 | ||
2105 | bed = get_elf_backend_data (output_bfd); | |
2106 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2107 | swap_out = bed->s->swap_reloc_out; | |
2108 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2109 | swap_out = bed->s->swap_reloca_out; | |
2110 | else | |
2111 | abort (); | |
2112 | ||
2113 | erel = output_rel_hdr->contents; | |
2114 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2115 | irela = internal_relocs; | |
2116 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2117 | * bed->s->int_rels_per_ext_rel); | |
2118 | while (irela < irelaend) | |
2119 | { | |
2120 | (*swap_out) (output_bfd, irela, erel); | |
2121 | irela += bed->s->int_rels_per_ext_rel; | |
2122 | erel += input_rel_hdr->sh_entsize; | |
2123 | } | |
2124 | ||
2125 | /* Bump the counter, so that we know where to add the next set of | |
2126 | relocations. */ | |
2127 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2128 | ||
2129 | return TRUE; | |
2130 | } | |
2131 | \f | |
2132 | /* Fix up the flags for a symbol. This handles various cases which | |
2133 | can only be fixed after all the input files are seen. This is | |
2134 | currently called by both adjust_dynamic_symbol and | |
2135 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2136 | the face of future changes. */ | |
2137 | ||
2138 | bfd_boolean | |
268b6b39 AM |
2139 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2140 | struct elf_info_failed *eif) | |
45d6a902 AM |
2141 | { |
2142 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2143 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2144 | permit a non-ELF file to correctly refer to a symbol defined in | |
2145 | an ELF dynamic object. */ | |
f5385ebf | 2146 | if (h->non_elf) |
45d6a902 AM |
2147 | { |
2148 | while (h->root.type == bfd_link_hash_indirect) | |
2149 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2150 | ||
2151 | if (h->root.type != bfd_link_hash_defined | |
2152 | && h->root.type != bfd_link_hash_defweak) | |
f5385ebf AM |
2153 | { |
2154 | h->ref_regular = 1; | |
2155 | h->ref_regular_nonweak = 1; | |
2156 | } | |
45d6a902 AM |
2157 | else |
2158 | { | |
2159 | if (h->root.u.def.section->owner != NULL | |
2160 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2161 | == bfd_target_elf_flavour)) | |
f5385ebf AM |
2162 | { |
2163 | h->ref_regular = 1; | |
2164 | h->ref_regular_nonweak = 1; | |
2165 | } | |
45d6a902 | 2166 | else |
f5385ebf | 2167 | h->def_regular = 1; |
45d6a902 AM |
2168 | } |
2169 | ||
2170 | if (h->dynindx == -1 | |
f5385ebf AM |
2171 | && (h->def_dynamic |
2172 | || h->ref_dynamic)) | |
45d6a902 | 2173 | { |
c152c796 | 2174 | if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) |
45d6a902 AM |
2175 | { |
2176 | eif->failed = TRUE; | |
2177 | return FALSE; | |
2178 | } | |
2179 | } | |
2180 | } | |
2181 | else | |
2182 | { | |
f5385ebf | 2183 | /* Unfortunately, NON_ELF is only correct if the symbol |
45d6a902 AM |
2184 | was first seen in a non-ELF file. Fortunately, if the symbol |
2185 | was first seen in an ELF file, we're probably OK unless the | |
2186 | symbol was defined in a non-ELF file. Catch that case here. | |
2187 | FIXME: We're still in trouble if the symbol was first seen in | |
2188 | a dynamic object, and then later in a non-ELF regular object. */ | |
2189 | if ((h->root.type == bfd_link_hash_defined | |
2190 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 2191 | && !h->def_regular |
45d6a902 AM |
2192 | && (h->root.u.def.section->owner != NULL |
2193 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2194 | != bfd_target_elf_flavour) | |
2195 | : (bfd_is_abs_section (h->root.u.def.section) | |
f5385ebf AM |
2196 | && !h->def_dynamic))) |
2197 | h->def_regular = 1; | |
45d6a902 AM |
2198 | } |
2199 | ||
2200 | /* If this is a final link, and the symbol was defined as a common | |
2201 | symbol in a regular object file, and there was no definition in | |
2202 | any dynamic object, then the linker will have allocated space for | |
f5385ebf | 2203 | the symbol in a common section but the DEF_REGULAR |
45d6a902 AM |
2204 | flag will not have been set. */ |
2205 | if (h->root.type == bfd_link_hash_defined | |
f5385ebf AM |
2206 | && !h->def_regular |
2207 | && h->ref_regular | |
2208 | && !h->def_dynamic | |
45d6a902 | 2209 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) |
f5385ebf | 2210 | h->def_regular = 1; |
45d6a902 AM |
2211 | |
2212 | /* If -Bsymbolic was used (which means to bind references to global | |
2213 | symbols to the definition within the shared object), and this | |
2214 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2215 | need a PLT entry. Likewise, if the symbol has non-default |
2216 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2217 | will force it local. */ |
f5385ebf | 2218 | if (h->needs_plt |
45d6a902 | 2219 | && eif->info->shared |
0eddce27 | 2220 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2221 | && (eif->info->symbolic |
c1be741f | 2222 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
f5385ebf | 2223 | && h->def_regular) |
45d6a902 | 2224 | { |
9c5bfbb7 | 2225 | const struct elf_backend_data *bed; |
45d6a902 AM |
2226 | bfd_boolean force_local; |
2227 | ||
2228 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2229 | ||
2230 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2231 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2232 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2233 | } | |
2234 | ||
2235 | /* If a weak undefined symbol has non-default visibility, we also | |
2236 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2237 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2238 | && h->root.type == bfd_link_hash_undefweak) |
2239 | { | |
9c5bfbb7 | 2240 | const struct elf_backend_data *bed; |
45d6a902 AM |
2241 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2242 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2243 | } | |
2244 | ||
2245 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2246 | the real definition in the dynamic object, copy interesting flags | |
2247 | over to the real definition. */ | |
f6e332e6 | 2248 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2249 | { |
2250 | struct elf_link_hash_entry *weakdef; | |
2251 | ||
f6e332e6 | 2252 | weakdef = h->u.weakdef; |
45d6a902 AM |
2253 | if (h->root.type == bfd_link_hash_indirect) |
2254 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2255 | ||
2256 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2257 | || h->root.type == bfd_link_hash_defweak); | |
2258 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2259 | || weakdef->root.type == bfd_link_hash_defweak); | |
f5385ebf | 2260 | BFD_ASSERT (weakdef->def_dynamic); |
45d6a902 AM |
2261 | |
2262 | /* If the real definition is defined by a regular object file, | |
2263 | don't do anything special. See the longer description in | |
2264 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
f5385ebf | 2265 | if (weakdef->def_regular) |
f6e332e6 | 2266 | h->u.weakdef = NULL; |
45d6a902 AM |
2267 | else |
2268 | { | |
9c5bfbb7 | 2269 | const struct elf_backend_data *bed; |
45d6a902 AM |
2270 | |
2271 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2272 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2273 | } | |
2274 | } | |
2275 | ||
2276 | return TRUE; | |
2277 | } | |
2278 | ||
2279 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2280 | called via elf_link_hash_traverse, and also calls itself | |
2281 | recursively. */ | |
2282 | ||
2283 | bfd_boolean | |
268b6b39 | 2284 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2285 | { |
268b6b39 | 2286 | struct elf_info_failed *eif = data; |
45d6a902 | 2287 | bfd *dynobj; |
9c5bfbb7 | 2288 | const struct elf_backend_data *bed; |
45d6a902 | 2289 | |
0eddce27 | 2290 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2291 | return FALSE; |
2292 | ||
2293 | if (h->root.type == bfd_link_hash_warning) | |
2294 | { | |
2295 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2296 | h->got = elf_hash_table (eif->info)->init_offset; | |
2297 | ||
2298 | /* When warning symbols are created, they **replace** the "real" | |
2299 | entry in the hash table, thus we never get to see the real | |
2300 | symbol in a hash traversal. So look at it now. */ | |
2301 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2302 | } | |
2303 | ||
2304 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2305 | if (h->root.type == bfd_link_hash_indirect) | |
2306 | return TRUE; | |
2307 | ||
2308 | /* Fix the symbol flags. */ | |
2309 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2310 | return FALSE; | |
2311 | ||
2312 | /* If this symbol does not require a PLT entry, and it is not | |
2313 | defined by a dynamic object, or is not referenced by a regular | |
2314 | object, ignore it. We do have to handle a weak defined symbol, | |
2315 | even if no regular object refers to it, if we decided to add it | |
2316 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2317 | about symbols which are defined by one dynamic object and | |
2318 | referenced by another one? */ | |
f5385ebf AM |
2319 | if (!h->needs_plt |
2320 | && (h->def_regular | |
2321 | || !h->def_dynamic | |
2322 | || (!h->ref_regular | |
f6e332e6 | 2323 | && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) |
45d6a902 AM |
2324 | { |
2325 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2326 | return TRUE; | |
2327 | } | |
2328 | ||
2329 | /* If we've already adjusted this symbol, don't do it again. This | |
2330 | can happen via a recursive call. */ | |
f5385ebf | 2331 | if (h->dynamic_adjusted) |
45d6a902 AM |
2332 | return TRUE; |
2333 | ||
2334 | /* Don't look at this symbol again. Note that we must set this | |
2335 | after checking the above conditions, because we may look at a | |
2336 | symbol once, decide not to do anything, and then get called | |
2337 | recursively later after REF_REGULAR is set below. */ | |
f5385ebf | 2338 | h->dynamic_adjusted = 1; |
45d6a902 AM |
2339 | |
2340 | /* If this is a weak definition, and we know a real definition, and | |
2341 | the real symbol is not itself defined by a regular object file, | |
2342 | then get a good value for the real definition. We handle the | |
2343 | real symbol first, for the convenience of the backend routine. | |
2344 | ||
2345 | Note that there is a confusing case here. If the real definition | |
2346 | is defined by a regular object file, we don't get the real symbol | |
2347 | from the dynamic object, but we do get the weak symbol. If the | |
2348 | processor backend uses a COPY reloc, then if some routine in the | |
2349 | dynamic object changes the real symbol, we will not see that | |
2350 | change in the corresponding weak symbol. This is the way other | |
2351 | ELF linkers work as well, and seems to be a result of the shared | |
2352 | library model. | |
2353 | ||
2354 | I will clarify this issue. Most SVR4 shared libraries define the | |
2355 | variable _timezone and define timezone as a weak synonym. The | |
2356 | tzset call changes _timezone. If you write | |
2357 | extern int timezone; | |
2358 | int _timezone = 5; | |
2359 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2360 | you might expect that, since timezone is a synonym for _timezone, | |
2361 | the same number will print both times. However, if the processor | |
2362 | backend uses a COPY reloc, then actually timezone will be copied | |
2363 | into your process image, and, since you define _timezone | |
2364 | yourself, _timezone will not. Thus timezone and _timezone will | |
2365 | wind up at different memory locations. The tzset call will set | |
2366 | _timezone, leaving timezone unchanged. */ | |
2367 | ||
f6e332e6 | 2368 | if (h->u.weakdef != NULL) |
45d6a902 AM |
2369 | { |
2370 | /* If we get to this point, we know there is an implicit | |
2371 | reference by a regular object file via the weak symbol H. | |
2372 | FIXME: Is this really true? What if the traversal finds | |
f6e332e6 AM |
2373 | H->U.WEAKDEF before it finds H? */ |
2374 | h->u.weakdef->ref_regular = 1; | |
45d6a902 | 2375 | |
f6e332e6 | 2376 | if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) |
45d6a902 AM |
2377 | return FALSE; |
2378 | } | |
2379 | ||
2380 | /* If a symbol has no type and no size and does not require a PLT | |
2381 | entry, then we are probably about to do the wrong thing here: we | |
2382 | are probably going to create a COPY reloc for an empty object. | |
2383 | This case can arise when a shared object is built with assembly | |
2384 | code, and the assembly code fails to set the symbol type. */ | |
2385 | if (h->size == 0 | |
2386 | && h->type == STT_NOTYPE | |
f5385ebf | 2387 | && !h->needs_plt) |
45d6a902 AM |
2388 | (*_bfd_error_handler) |
2389 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2390 | h->root.root.string); | |
2391 | ||
2392 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2393 | bed = get_elf_backend_data (dynobj); | |
2394 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2395 | { | |
2396 | eif->failed = TRUE; | |
2397 | return FALSE; | |
2398 | } | |
2399 | ||
2400 | return TRUE; | |
2401 | } | |
2402 | ||
2403 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2404 | to reflect the object merging within the sections. */ | |
2405 | ||
2406 | bfd_boolean | |
268b6b39 | 2407 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2408 | { |
2409 | asection *sec; | |
2410 | ||
2411 | if (h->root.type == bfd_link_hash_warning) | |
2412 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2413 | ||
2414 | if ((h->root.type == bfd_link_hash_defined | |
2415 | || h->root.type == bfd_link_hash_defweak) | |
2416 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2417 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2418 | { | |
268b6b39 | 2419 | bfd *output_bfd = data; |
45d6a902 AM |
2420 | |
2421 | h->root.u.def.value = | |
2422 | _bfd_merged_section_offset (output_bfd, | |
2423 | &h->root.u.def.section, | |
2424 | elf_section_data (sec)->sec_info, | |
753731ee | 2425 | h->root.u.def.value); |
45d6a902 AM |
2426 | } |
2427 | ||
2428 | return TRUE; | |
2429 | } | |
986a241f RH |
2430 | |
2431 | /* Returns false if the symbol referred to by H should be considered | |
2432 | to resolve local to the current module, and true if it should be | |
2433 | considered to bind dynamically. */ | |
2434 | ||
2435 | bfd_boolean | |
268b6b39 AM |
2436 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2437 | struct bfd_link_info *info, | |
2438 | bfd_boolean ignore_protected) | |
986a241f RH |
2439 | { |
2440 | bfd_boolean binding_stays_local_p; | |
2441 | ||
2442 | if (h == NULL) | |
2443 | return FALSE; | |
2444 | ||
2445 | while (h->root.type == bfd_link_hash_indirect | |
2446 | || h->root.type == bfd_link_hash_warning) | |
2447 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2448 | ||
2449 | /* If it was forced local, then clearly it's not dynamic. */ | |
2450 | if (h->dynindx == -1) | |
2451 | return FALSE; | |
f5385ebf | 2452 | if (h->forced_local) |
986a241f RH |
2453 | return FALSE; |
2454 | ||
2455 | /* Identify the cases where name binding rules say that a | |
2456 | visible symbol resolves locally. */ | |
2457 | binding_stays_local_p = info->executable || info->symbolic; | |
2458 | ||
2459 | switch (ELF_ST_VISIBILITY (h->other)) | |
2460 | { | |
2461 | case STV_INTERNAL: | |
2462 | case STV_HIDDEN: | |
2463 | return FALSE; | |
2464 | ||
2465 | case STV_PROTECTED: | |
2466 | /* Proper resolution for function pointer equality may require | |
2467 | that these symbols perhaps be resolved dynamically, even though | |
2468 | we should be resolving them to the current module. */ | |
2469 | if (!ignore_protected) | |
2470 | binding_stays_local_p = TRUE; | |
2471 | break; | |
2472 | ||
2473 | default: | |
986a241f RH |
2474 | break; |
2475 | } | |
2476 | ||
aa37626c | 2477 | /* If it isn't defined locally, then clearly it's dynamic. */ |
f5385ebf | 2478 | if (!h->def_regular) |
aa37626c L |
2479 | return TRUE; |
2480 | ||
986a241f RH |
2481 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2482 | us that it remains local. */ | |
2483 | return !binding_stays_local_p; | |
2484 | } | |
f6c52c13 AM |
2485 | |
2486 | /* Return true if the symbol referred to by H should be considered | |
2487 | to resolve local to the current module, and false otherwise. Differs | |
2488 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2489 | undefined symbols and weak symbols. */ | |
2490 | ||
2491 | bfd_boolean | |
268b6b39 AM |
2492 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2493 | struct bfd_link_info *info, | |
2494 | bfd_boolean local_protected) | |
f6c52c13 AM |
2495 | { |
2496 | /* If it's a local sym, of course we resolve locally. */ | |
2497 | if (h == NULL) | |
2498 | return TRUE; | |
2499 | ||
7e2294f9 AO |
2500 | /* Common symbols that become definitions don't get the DEF_REGULAR |
2501 | flag set, so test it first, and don't bail out. */ | |
2502 | if (ELF_COMMON_DEF_P (h)) | |
2503 | /* Do nothing. */; | |
f6c52c13 AM |
2504 | /* If we don't have a definition in a regular file, then we can't |
2505 | resolve locally. The sym is either undefined or dynamic. */ | |
f5385ebf | 2506 | else if (!h->def_regular) |
f6c52c13 AM |
2507 | return FALSE; |
2508 | ||
2509 | /* Forced local symbols resolve locally. */ | |
f5385ebf | 2510 | if (h->forced_local) |
f6c52c13 AM |
2511 | return TRUE; |
2512 | ||
2513 | /* As do non-dynamic symbols. */ | |
2514 | if (h->dynindx == -1) | |
2515 | return TRUE; | |
2516 | ||
2517 | /* At this point, we know the symbol is defined and dynamic. In an | |
2518 | executable it must resolve locally, likewise when building symbolic | |
2519 | shared libraries. */ | |
2520 | if (info->executable || info->symbolic) | |
2521 | return TRUE; | |
2522 | ||
2523 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2524 | with default visibility might not resolve locally. */ | |
2525 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2526 | return FALSE; | |
2527 | ||
2528 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2529 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2530 | return TRUE; | |
2531 | ||
2532 | /* Function pointer equality tests may require that STV_PROTECTED | |
2533 | symbols be treated as dynamic symbols, even when we know that the | |
2534 | dynamic linker will resolve them locally. */ | |
2535 | return local_protected; | |
2536 | } | |
e1918d23 AM |
2537 | |
2538 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2539 | aligned. Returns the first TLS output section. */ | |
2540 | ||
2541 | struct bfd_section * | |
2542 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2543 | { | |
2544 | struct bfd_section *sec, *tls; | |
2545 | unsigned int align = 0; | |
2546 | ||
2547 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2548 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2549 | break; | |
2550 | tls = sec; | |
2551 | ||
2552 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2553 | if (sec->alignment_power > align) | |
2554 | align = sec->alignment_power; | |
2555 | ||
2556 | elf_hash_table (info)->tls_sec = tls; | |
2557 | ||
2558 | /* Ensure the alignment of the first section is the largest alignment, | |
2559 | so that the tls segment starts aligned. */ | |
2560 | if (tls != NULL) | |
2561 | tls->alignment_power = align; | |
2562 | ||
2563 | return tls; | |
2564 | } | |
0ad989f9 L |
2565 | |
2566 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2567 | static bfd_boolean | |
2568 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2569 | Elf_Internal_Sym *sym) | |
2570 | { | |
2571 | /* Local symbols do not count, but target specific ones might. */ | |
2572 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2573 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2574 | return FALSE; | |
2575 | ||
2576 | /* Function symbols do not count. */ | |
2577 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2578 | return FALSE; | |
2579 | ||
2580 | /* If the section is undefined, then so is the symbol. */ | |
2581 | if (sym->st_shndx == SHN_UNDEF) | |
2582 | return FALSE; | |
2583 | ||
2584 | /* If the symbol is defined in the common section, then | |
2585 | it is a common definition and so does not count. */ | |
2586 | if (sym->st_shndx == SHN_COMMON) | |
2587 | return FALSE; | |
2588 | ||
2589 | /* If the symbol is in a target specific section then we | |
2590 | must rely upon the backend to tell us what it is. */ | |
2591 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2592 | /* FIXME - this function is not coded yet: | |
2593 | ||
2594 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2595 | ||
2596 | Instead for now assume that the definition is not global, | |
2597 | Even if this is wrong, at least the linker will behave | |
2598 | in the same way that it used to do. */ | |
2599 | return FALSE; | |
2600 | ||
2601 | return TRUE; | |
2602 | } | |
2603 | ||
2604 | /* Search the symbol table of the archive element of the archive ABFD | |
2605 | whose archive map contains a mention of SYMDEF, and determine if | |
2606 | the symbol is defined in this element. */ | |
2607 | static bfd_boolean | |
2608 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2609 | { | |
2610 | Elf_Internal_Shdr * hdr; | |
2611 | bfd_size_type symcount; | |
2612 | bfd_size_type extsymcount; | |
2613 | bfd_size_type extsymoff; | |
2614 | Elf_Internal_Sym *isymbuf; | |
2615 | Elf_Internal_Sym *isym; | |
2616 | Elf_Internal_Sym *isymend; | |
2617 | bfd_boolean result; | |
2618 | ||
2619 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2620 | if (abfd == NULL) | |
2621 | return FALSE; | |
2622 | ||
2623 | if (! bfd_check_format (abfd, bfd_object)) | |
2624 | return FALSE; | |
2625 | ||
2626 | /* If we have already included the element containing this symbol in the | |
2627 | link then we do not need to include it again. Just claim that any symbol | |
2628 | it contains is not a definition, so that our caller will not decide to | |
2629 | (re)include this element. */ | |
2630 | if (abfd->archive_pass) | |
2631 | return FALSE; | |
2632 | ||
2633 | /* Select the appropriate symbol table. */ | |
2634 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2635 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2636 | else | |
2637 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2638 | ||
2639 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2640 | ||
2641 | /* The sh_info field of the symtab header tells us where the | |
2642 | external symbols start. We don't care about the local symbols. */ | |
2643 | if (elf_bad_symtab (abfd)) | |
2644 | { | |
2645 | extsymcount = symcount; | |
2646 | extsymoff = 0; | |
2647 | } | |
2648 | else | |
2649 | { | |
2650 | extsymcount = symcount - hdr->sh_info; | |
2651 | extsymoff = hdr->sh_info; | |
2652 | } | |
2653 | ||
2654 | if (extsymcount == 0) | |
2655 | return FALSE; | |
2656 | ||
2657 | /* Read in the symbol table. */ | |
2658 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2659 | NULL, NULL, NULL); | |
2660 | if (isymbuf == NULL) | |
2661 | return FALSE; | |
2662 | ||
2663 | /* Scan the symbol table looking for SYMDEF. */ | |
2664 | result = FALSE; | |
2665 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2666 | { | |
2667 | const char *name; | |
2668 | ||
2669 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2670 | isym->st_name); | |
2671 | if (name == NULL) | |
2672 | break; | |
2673 | ||
2674 | if (strcmp (name, symdef->name) == 0) | |
2675 | { | |
2676 | result = is_global_data_symbol_definition (abfd, isym); | |
2677 | break; | |
2678 | } | |
2679 | } | |
2680 | ||
2681 | free (isymbuf); | |
2682 | ||
2683 | return result; | |
2684 | } | |
2685 | \f | |
5a580b3a AM |
2686 | /* Add an entry to the .dynamic table. */ |
2687 | ||
2688 | bfd_boolean | |
2689 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2690 | bfd_vma tag, | |
2691 | bfd_vma val) | |
2692 | { | |
2693 | struct elf_link_hash_table *hash_table; | |
2694 | const struct elf_backend_data *bed; | |
2695 | asection *s; | |
2696 | bfd_size_type newsize; | |
2697 | bfd_byte *newcontents; | |
2698 | Elf_Internal_Dyn dyn; | |
2699 | ||
2700 | hash_table = elf_hash_table (info); | |
2701 | if (! is_elf_hash_table (hash_table)) | |
2702 | return FALSE; | |
2703 | ||
8fdd7217 NC |
2704 | if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL) |
2705 | _bfd_error_handler | |
2706 | (_("warning: creating a DT_TEXTREL in a shared object.")); | |
2707 | ||
5a580b3a AM |
2708 | bed = get_elf_backend_data (hash_table->dynobj); |
2709 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2710 | BFD_ASSERT (s != NULL); | |
2711 | ||
eea6121a | 2712 | newsize = s->size + bed->s->sizeof_dyn; |
5a580b3a AM |
2713 | newcontents = bfd_realloc (s->contents, newsize); |
2714 | if (newcontents == NULL) | |
2715 | return FALSE; | |
2716 | ||
2717 | dyn.d_tag = tag; | |
2718 | dyn.d_un.d_val = val; | |
eea6121a | 2719 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); |
5a580b3a | 2720 | |
eea6121a | 2721 | s->size = newsize; |
5a580b3a AM |
2722 | s->contents = newcontents; |
2723 | ||
2724 | return TRUE; | |
2725 | } | |
2726 | ||
2727 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2728 | otherwise just check whether one already exists. Returns -1 on error, | |
2729 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2730 | ||
4ad4eba5 AM |
2731 | static int |
2732 | elf_add_dt_needed_tag (struct bfd_link_info *info, | |
2733 | const char *soname, | |
2734 | bfd_boolean do_it) | |
5a580b3a AM |
2735 | { |
2736 | struct elf_link_hash_table *hash_table; | |
2737 | bfd_size_type oldsize; | |
2738 | bfd_size_type strindex; | |
2739 | ||
2740 | hash_table = elf_hash_table (info); | |
2741 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2742 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2743 | if (strindex == (bfd_size_type) -1) | |
2744 | return -1; | |
2745 | ||
2746 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2747 | { | |
2748 | asection *sdyn; | |
2749 | const struct elf_backend_data *bed; | |
2750 | bfd_byte *extdyn; | |
2751 | ||
2752 | bed = get_elf_backend_data (hash_table->dynobj); | |
2753 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2754 | BFD_ASSERT (sdyn != NULL); | |
2755 | ||
2756 | for (extdyn = sdyn->contents; | |
eea6121a | 2757 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
2758 | extdyn += bed->s->sizeof_dyn) |
2759 | { | |
2760 | Elf_Internal_Dyn dyn; | |
2761 | ||
2762 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); | |
2763 | if (dyn.d_tag == DT_NEEDED | |
2764 | && dyn.d_un.d_val == strindex) | |
2765 | { | |
2766 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2767 | return 1; | |
2768 | } | |
2769 | } | |
2770 | } | |
2771 | ||
2772 | if (do_it) | |
2773 | { | |
2774 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) | |
2775 | return -1; | |
2776 | } | |
2777 | else | |
2778 | /* We were just checking for existence of the tag. */ | |
2779 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2780 | ||
2781 | return 0; | |
2782 | } | |
2783 | ||
2784 | /* Sort symbol by value and section. */ | |
4ad4eba5 AM |
2785 | static int |
2786 | elf_sort_symbol (const void *arg1, const void *arg2) | |
5a580b3a AM |
2787 | { |
2788 | const struct elf_link_hash_entry *h1; | |
2789 | const struct elf_link_hash_entry *h2; | |
10b7e05b | 2790 | bfd_signed_vma vdiff; |
5a580b3a AM |
2791 | |
2792 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2793 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
10b7e05b NC |
2794 | vdiff = h1->root.u.def.value - h2->root.u.def.value; |
2795 | if (vdiff != 0) | |
2796 | return vdiff > 0 ? 1 : -1; | |
2797 | else | |
2798 | { | |
2799 | long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; | |
2800 | if (sdiff != 0) | |
2801 | return sdiff > 0 ? 1 : -1; | |
2802 | } | |
5a580b3a AM |
2803 | return 0; |
2804 | } | |
4ad4eba5 | 2805 | |
5a580b3a AM |
2806 | /* This function is used to adjust offsets into .dynstr for |
2807 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2808 | ||
2809 | static bfd_boolean | |
2810 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2811 | { | |
2812 | struct elf_strtab_hash *dynstr = data; | |
2813 | ||
2814 | if (h->root.type == bfd_link_hash_warning) | |
2815 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2816 | ||
2817 | if (h->dynindx != -1) | |
2818 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2819 | return TRUE; | |
2820 | } | |
2821 | ||
2822 | /* Assign string offsets in .dynstr, update all structures referencing | |
2823 | them. */ | |
2824 | ||
4ad4eba5 AM |
2825 | static bfd_boolean |
2826 | elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
5a580b3a AM |
2827 | { |
2828 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
2829 | struct elf_link_local_dynamic_entry *entry; | |
2830 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
2831 | bfd *dynobj = hash_table->dynobj; | |
2832 | asection *sdyn; | |
2833 | bfd_size_type size; | |
2834 | const struct elf_backend_data *bed; | |
2835 | bfd_byte *extdyn; | |
2836 | ||
2837 | _bfd_elf_strtab_finalize (dynstr); | |
2838 | size = _bfd_elf_strtab_size (dynstr); | |
2839 | ||
2840 | bed = get_elf_backend_data (dynobj); | |
2841 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2842 | BFD_ASSERT (sdyn != NULL); | |
2843 | ||
2844 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
2845 | for (extdyn = sdyn->contents; | |
eea6121a | 2846 | extdyn < sdyn->contents + sdyn->size; |
5a580b3a AM |
2847 | extdyn += bed->s->sizeof_dyn) |
2848 | { | |
2849 | Elf_Internal_Dyn dyn; | |
2850 | ||
2851 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
2852 | switch (dyn.d_tag) | |
2853 | { | |
2854 | case DT_STRSZ: | |
2855 | dyn.d_un.d_val = size; | |
2856 | break; | |
2857 | case DT_NEEDED: | |
2858 | case DT_SONAME: | |
2859 | case DT_RPATH: | |
2860 | case DT_RUNPATH: | |
2861 | case DT_FILTER: | |
2862 | case DT_AUXILIARY: | |
2863 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
2864 | break; | |
2865 | default: | |
2866 | continue; | |
2867 | } | |
2868 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
2869 | } | |
2870 | ||
2871 | /* Now update local dynamic symbols. */ | |
2872 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
2873 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
2874 | entry->isym.st_name); | |
2875 | ||
2876 | /* And the rest of dynamic symbols. */ | |
2877 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
2878 | ||
2879 | /* Adjust version definitions. */ | |
2880 | if (elf_tdata (output_bfd)->cverdefs) | |
2881 | { | |
2882 | asection *s; | |
2883 | bfd_byte *p; | |
2884 | bfd_size_type i; | |
2885 | Elf_Internal_Verdef def; | |
2886 | Elf_Internal_Verdaux defaux; | |
2887 | ||
2888 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2889 | p = s->contents; | |
2890 | do | |
2891 | { | |
2892 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
2893 | &def); | |
2894 | p += sizeof (Elf_External_Verdef); | |
2895 | for (i = 0; i < def.vd_cnt; ++i) | |
2896 | { | |
2897 | _bfd_elf_swap_verdaux_in (output_bfd, | |
2898 | (Elf_External_Verdaux *) p, &defaux); | |
2899 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
2900 | defaux.vda_name); | |
2901 | _bfd_elf_swap_verdaux_out (output_bfd, | |
2902 | &defaux, (Elf_External_Verdaux *) p); | |
2903 | p += sizeof (Elf_External_Verdaux); | |
2904 | } | |
2905 | } | |
2906 | while (def.vd_next); | |
2907 | } | |
2908 | ||
2909 | /* Adjust version references. */ | |
2910 | if (elf_tdata (output_bfd)->verref) | |
2911 | { | |
2912 | asection *s; | |
2913 | bfd_byte *p; | |
2914 | bfd_size_type i; | |
2915 | Elf_Internal_Verneed need; | |
2916 | Elf_Internal_Vernaux needaux; | |
2917 | ||
2918 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2919 | p = s->contents; | |
2920 | do | |
2921 | { | |
2922 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
2923 | &need); | |
2924 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
2925 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
2926 | (Elf_External_Verneed *) p); | |
2927 | p += sizeof (Elf_External_Verneed); | |
2928 | for (i = 0; i < need.vn_cnt; ++i) | |
2929 | { | |
2930 | _bfd_elf_swap_vernaux_in (output_bfd, | |
2931 | (Elf_External_Vernaux *) p, &needaux); | |
2932 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
2933 | needaux.vna_name); | |
2934 | _bfd_elf_swap_vernaux_out (output_bfd, | |
2935 | &needaux, | |
2936 | (Elf_External_Vernaux *) p); | |
2937 | p += sizeof (Elf_External_Vernaux); | |
2938 | } | |
2939 | } | |
2940 | while (need.vn_next); | |
2941 | } | |
2942 | ||
2943 | return TRUE; | |
2944 | } | |
2945 | \f | |
4ad4eba5 AM |
2946 | /* Add symbols from an ELF object file to the linker hash table. */ |
2947 | ||
2948 | static bfd_boolean | |
2949 | elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) | |
2950 | { | |
2951 | bfd_boolean (*add_symbol_hook) | |
555cd476 | 2952 | (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, |
4ad4eba5 AM |
2953 | const char **, flagword *, asection **, bfd_vma *); |
2954 | bfd_boolean (*check_relocs) | |
2955 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
85fbca6a NC |
2956 | bfd_boolean (*check_directives) |
2957 | (bfd *, struct bfd_link_info *); | |
4ad4eba5 AM |
2958 | bfd_boolean collect; |
2959 | Elf_Internal_Shdr *hdr; | |
2960 | bfd_size_type symcount; | |
2961 | bfd_size_type extsymcount; | |
2962 | bfd_size_type extsymoff; | |
2963 | struct elf_link_hash_entry **sym_hash; | |
2964 | bfd_boolean dynamic; | |
2965 | Elf_External_Versym *extversym = NULL; | |
2966 | Elf_External_Versym *ever; | |
2967 | struct elf_link_hash_entry *weaks; | |
2968 | struct elf_link_hash_entry **nondeflt_vers = NULL; | |
2969 | bfd_size_type nondeflt_vers_cnt = 0; | |
2970 | Elf_Internal_Sym *isymbuf = NULL; | |
2971 | Elf_Internal_Sym *isym; | |
2972 | Elf_Internal_Sym *isymend; | |
2973 | const struct elf_backend_data *bed; | |
2974 | bfd_boolean add_needed; | |
2975 | struct elf_link_hash_table * hash_table; | |
2976 | bfd_size_type amt; | |
2977 | ||
2978 | hash_table = elf_hash_table (info); | |
2979 | ||
2980 | bed = get_elf_backend_data (abfd); | |
2981 | add_symbol_hook = bed->elf_add_symbol_hook; | |
2982 | collect = bed->collect; | |
2983 | ||
2984 | if ((abfd->flags & DYNAMIC) == 0) | |
2985 | dynamic = FALSE; | |
2986 | else | |
2987 | { | |
2988 | dynamic = TRUE; | |
2989 | ||
2990 | /* You can't use -r against a dynamic object. Also, there's no | |
2991 | hope of using a dynamic object which does not exactly match | |
2992 | the format of the output file. */ | |
2993 | if (info->relocatable | |
2994 | || !is_elf_hash_table (hash_table) | |
2995 | || hash_table->root.creator != abfd->xvec) | |
2996 | { | |
9a0789ec NC |
2997 | if (info->relocatable) |
2998 | bfd_set_error (bfd_error_invalid_operation); | |
2999 | else | |
3000 | bfd_set_error (bfd_error_wrong_format); | |
4ad4eba5 AM |
3001 | goto error_return; |
3002 | } | |
3003 | } | |
3004 | ||
3005 | /* As a GNU extension, any input sections which are named | |
3006 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
3007 | symbol. This differs from .gnu.warning sections, which generate | |
3008 | warnings when they are included in an output file. */ | |
3009 | if (info->executable) | |
3010 | { | |
3011 | asection *s; | |
3012 | ||
3013 | for (s = abfd->sections; s != NULL; s = s->next) | |
3014 | { | |
3015 | const char *name; | |
3016 | ||
3017 | name = bfd_get_section_name (abfd, s); | |
3018 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
3019 | { | |
3020 | char *msg; | |
3021 | bfd_size_type sz; | |
3022 | bfd_size_type prefix_len; | |
3023 | const char * gnu_warning_prefix = _("warning: "); | |
3024 | ||
3025 | name += sizeof ".gnu.warning." - 1; | |
3026 | ||
3027 | /* If this is a shared object, then look up the symbol | |
3028 | in the hash table. If it is there, and it is already | |
3029 | been defined, then we will not be using the entry | |
3030 | from this shared object, so we don't need to warn. | |
3031 | FIXME: If we see the definition in a regular object | |
3032 | later on, we will warn, but we shouldn't. The only | |
3033 | fix is to keep track of what warnings we are supposed | |
3034 | to emit, and then handle them all at the end of the | |
3035 | link. */ | |
3036 | if (dynamic) | |
3037 | { | |
3038 | struct elf_link_hash_entry *h; | |
3039 | ||
3040 | h = elf_link_hash_lookup (hash_table, name, | |
3041 | FALSE, FALSE, TRUE); | |
3042 | ||
3043 | /* FIXME: What about bfd_link_hash_common? */ | |
3044 | if (h != NULL | |
3045 | && (h->root.type == bfd_link_hash_defined | |
3046 | || h->root.type == bfd_link_hash_defweak)) | |
3047 | { | |
3048 | /* We don't want to issue this warning. Clobber | |
3049 | the section size so that the warning does not | |
3050 | get copied into the output file. */ | |
eea6121a | 3051 | s->size = 0; |
4ad4eba5 AM |
3052 | continue; |
3053 | } | |
3054 | } | |
3055 | ||
eea6121a | 3056 | sz = s->size; |
4ad4eba5 AM |
3057 | prefix_len = strlen (gnu_warning_prefix); |
3058 | msg = bfd_alloc (abfd, prefix_len + sz + 1); | |
3059 | if (msg == NULL) | |
3060 | goto error_return; | |
3061 | ||
3062 | strcpy (msg, gnu_warning_prefix); | |
3063 | if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz)) | |
3064 | goto error_return; | |
3065 | ||
3066 | msg[prefix_len + sz] = '\0'; | |
3067 | ||
3068 | if (! (_bfd_generic_link_add_one_symbol | |
3069 | (info, abfd, name, BSF_WARNING, s, 0, msg, | |
3070 | FALSE, collect, NULL))) | |
3071 | goto error_return; | |
3072 | ||
3073 | if (! info->relocatable) | |
3074 | { | |
3075 | /* Clobber the section size so that the warning does | |
3076 | not get copied into the output file. */ | |
eea6121a | 3077 | s->size = 0; |
4ad4eba5 AM |
3078 | } |
3079 | } | |
3080 | } | |
3081 | } | |
3082 | ||
3083 | add_needed = TRUE; | |
3084 | if (! dynamic) | |
3085 | { | |
3086 | /* If we are creating a shared library, create all the dynamic | |
3087 | sections immediately. We need to attach them to something, | |
3088 | so we attach them to this BFD, provided it is the right | |
3089 | format. FIXME: If there are no input BFD's of the same | |
3090 | format as the output, we can't make a shared library. */ | |
3091 | if (info->shared | |
3092 | && is_elf_hash_table (hash_table) | |
3093 | && hash_table->root.creator == abfd->xvec | |
3094 | && ! hash_table->dynamic_sections_created) | |
3095 | { | |
3096 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3097 | goto error_return; | |
3098 | } | |
3099 | } | |
3100 | else if (!is_elf_hash_table (hash_table)) | |
3101 | goto error_return; | |
3102 | else | |
3103 | { | |
3104 | asection *s; | |
3105 | const char *soname = NULL; | |
3106 | struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; | |
3107 | int ret; | |
3108 | ||
3109 | /* ld --just-symbols and dynamic objects don't mix very well. | |
3110 | Test for --just-symbols by looking at info set up by | |
3111 | _bfd_elf_link_just_syms. */ | |
3112 | if ((s = abfd->sections) != NULL | |
3113 | && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) | |
3114 | goto error_return; | |
3115 | ||
3116 | /* If this dynamic lib was specified on the command line with | |
3117 | --as-needed in effect, then we don't want to add a DT_NEEDED | |
3118 | tag unless the lib is actually used. Similary for libs brought | |
e56f61be L |
3119 | in by another lib's DT_NEEDED. When --no-add-needed is used |
3120 | on a dynamic lib, we don't want to add a DT_NEEDED entry for | |
3121 | any dynamic library in DT_NEEDED tags in the dynamic lib at | |
3122 | all. */ | |
3123 | add_needed = (elf_dyn_lib_class (abfd) | |
3124 | & (DYN_AS_NEEDED | DYN_DT_NEEDED | |
3125 | | DYN_NO_NEEDED)) == 0; | |
4ad4eba5 AM |
3126 | |
3127 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
3128 | if (s != NULL) | |
3129 | { | |
3130 | bfd_byte *dynbuf; | |
3131 | bfd_byte *extdyn; | |
3132 | int elfsec; | |
3133 | unsigned long shlink; | |
3134 | ||
eea6121a | 3135 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
4ad4eba5 AM |
3136 | goto error_free_dyn; |
3137 | ||
3138 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
3139 | if (elfsec == -1) | |
3140 | goto error_free_dyn; | |
3141 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; | |
3142 | ||
3143 | for (extdyn = dynbuf; | |
eea6121a | 3144 | extdyn < dynbuf + s->size; |
4ad4eba5 AM |
3145 | extdyn += bed->s->sizeof_dyn) |
3146 | { | |
3147 | Elf_Internal_Dyn dyn; | |
3148 | ||
3149 | bed->s->swap_dyn_in (abfd, extdyn, &dyn); | |
3150 | if (dyn.d_tag == DT_SONAME) | |
3151 | { | |
3152 | unsigned int tagv = dyn.d_un.d_val; | |
3153 | soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3154 | if (soname == NULL) | |
3155 | goto error_free_dyn; | |
3156 | } | |
3157 | if (dyn.d_tag == DT_NEEDED) | |
3158 | { | |
3159 | struct bfd_link_needed_list *n, **pn; | |
3160 | char *fnm, *anm; | |
3161 | unsigned int tagv = dyn.d_un.d_val; | |
3162 | ||
3163 | amt = sizeof (struct bfd_link_needed_list); | |
3164 | n = bfd_alloc (abfd, amt); | |
3165 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3166 | if (n == NULL || fnm == NULL) | |
3167 | goto error_free_dyn; | |
3168 | amt = strlen (fnm) + 1; | |
3169 | anm = bfd_alloc (abfd, amt); | |
3170 | if (anm == NULL) | |
3171 | goto error_free_dyn; | |
3172 | memcpy (anm, fnm, amt); | |
3173 | n->name = anm; | |
3174 | n->by = abfd; | |
3175 | n->next = NULL; | |
3176 | for (pn = & hash_table->needed; | |
3177 | *pn != NULL; | |
3178 | pn = &(*pn)->next) | |
3179 | ; | |
3180 | *pn = n; | |
3181 | } | |
3182 | if (dyn.d_tag == DT_RUNPATH) | |
3183 | { | |
3184 | struct bfd_link_needed_list *n, **pn; | |
3185 | char *fnm, *anm; | |
3186 | unsigned int tagv = dyn.d_un.d_val; | |
3187 | ||
3188 | amt = sizeof (struct bfd_link_needed_list); | |
3189 | n = bfd_alloc (abfd, amt); | |
3190 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3191 | if (n == NULL || fnm == NULL) | |
3192 | goto error_free_dyn; | |
3193 | amt = strlen (fnm) + 1; | |
3194 | anm = bfd_alloc (abfd, amt); | |
3195 | if (anm == NULL) | |
3196 | goto error_free_dyn; | |
3197 | memcpy (anm, fnm, amt); | |
3198 | n->name = anm; | |
3199 | n->by = abfd; | |
3200 | n->next = NULL; | |
3201 | for (pn = & runpath; | |
3202 | *pn != NULL; | |
3203 | pn = &(*pn)->next) | |
3204 | ; | |
3205 | *pn = n; | |
3206 | } | |
3207 | /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ | |
3208 | if (!runpath && dyn.d_tag == DT_RPATH) | |
3209 | { | |
3210 | struct bfd_link_needed_list *n, **pn; | |
3211 | char *fnm, *anm; | |
3212 | unsigned int tagv = dyn.d_un.d_val; | |
3213 | ||
3214 | amt = sizeof (struct bfd_link_needed_list); | |
3215 | n = bfd_alloc (abfd, amt); | |
3216 | fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); | |
3217 | if (n == NULL || fnm == NULL) | |
3218 | goto error_free_dyn; | |
3219 | amt = strlen (fnm) + 1; | |
3220 | anm = bfd_alloc (abfd, amt); | |
3221 | if (anm == NULL) | |
3222 | { | |
3223 | error_free_dyn: | |
3224 | free (dynbuf); | |
3225 | goto error_return; | |
3226 | } | |
3227 | memcpy (anm, fnm, amt); | |
3228 | n->name = anm; | |
3229 | n->by = abfd; | |
3230 | n->next = NULL; | |
3231 | for (pn = & rpath; | |
3232 | *pn != NULL; | |
3233 | pn = &(*pn)->next) | |
3234 | ; | |
3235 | *pn = n; | |
3236 | } | |
3237 | } | |
3238 | ||
3239 | free (dynbuf); | |
3240 | } | |
3241 | ||
3242 | /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that | |
3243 | frees all more recently bfd_alloc'd blocks as well. */ | |
3244 | if (runpath) | |
3245 | rpath = runpath; | |
3246 | ||
3247 | if (rpath) | |
3248 | { | |
3249 | struct bfd_link_needed_list **pn; | |
3250 | for (pn = & hash_table->runpath; | |
3251 | *pn != NULL; | |
3252 | pn = &(*pn)->next) | |
3253 | ; | |
3254 | *pn = rpath; | |
3255 | } | |
3256 | ||
3257 | /* We do not want to include any of the sections in a dynamic | |
3258 | object in the output file. We hack by simply clobbering the | |
3259 | list of sections in the BFD. This could be handled more | |
3260 | cleanly by, say, a new section flag; the existing | |
3261 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
3262 | still implies that the section takes up space in the output | |
3263 | file. */ | |
3264 | bfd_section_list_clear (abfd); | |
3265 | ||
3266 | /* If this is the first dynamic object found in the link, create | |
3267 | the special sections required for dynamic linking. */ | |
3268 | if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) | |
3269 | goto error_return; | |
3270 | ||
3271 | /* Find the name to use in a DT_NEEDED entry that refers to this | |
3272 | object. If the object has a DT_SONAME entry, we use it. | |
3273 | Otherwise, if the generic linker stuck something in | |
3274 | elf_dt_name, we use that. Otherwise, we just use the file | |
3275 | name. */ | |
3276 | if (soname == NULL || *soname == '\0') | |
3277 | { | |
3278 | soname = elf_dt_name (abfd); | |
3279 | if (soname == NULL || *soname == '\0') | |
3280 | soname = bfd_get_filename (abfd); | |
3281 | } | |
3282 | ||
3283 | /* Save the SONAME because sometimes the linker emulation code | |
3284 | will need to know it. */ | |
3285 | elf_dt_name (abfd) = soname; | |
3286 | ||
3287 | ret = elf_add_dt_needed_tag (info, soname, add_needed); | |
3288 | if (ret < 0) | |
3289 | goto error_return; | |
3290 | ||
3291 | /* If we have already included this dynamic object in the | |
3292 | link, just ignore it. There is no reason to include a | |
3293 | particular dynamic object more than once. */ | |
3294 | if (ret > 0) | |
3295 | return TRUE; | |
3296 | } | |
3297 | ||
3298 | /* If this is a dynamic object, we always link against the .dynsym | |
3299 | symbol table, not the .symtab symbol table. The dynamic linker | |
3300 | will only see the .dynsym symbol table, so there is no reason to | |
3301 | look at .symtab for a dynamic object. */ | |
3302 | ||
3303 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
3304 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
3305 | else | |
3306 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3307 | ||
3308 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
3309 | ||
3310 | /* The sh_info field of the symtab header tells us where the | |
3311 | external symbols start. We don't care about the local symbols at | |
3312 | this point. */ | |
3313 | if (elf_bad_symtab (abfd)) | |
3314 | { | |
3315 | extsymcount = symcount; | |
3316 | extsymoff = 0; | |
3317 | } | |
3318 | else | |
3319 | { | |
3320 | extsymcount = symcount - hdr->sh_info; | |
3321 | extsymoff = hdr->sh_info; | |
3322 | } | |
3323 | ||
3324 | sym_hash = NULL; | |
3325 | if (extsymcount != 0) | |
3326 | { | |
3327 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
3328 | NULL, NULL, NULL); | |
3329 | if (isymbuf == NULL) | |
3330 | goto error_return; | |
3331 | ||
3332 | /* We store a pointer to the hash table entry for each external | |
3333 | symbol. */ | |
3334 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
3335 | sym_hash = bfd_alloc (abfd, amt); | |
3336 | if (sym_hash == NULL) | |
3337 | goto error_free_sym; | |
3338 | elf_sym_hashes (abfd) = sym_hash; | |
3339 | } | |
3340 | ||
3341 | if (dynamic) | |
3342 | { | |
3343 | /* Read in any version definitions. */ | |
3344 | if (! _bfd_elf_slurp_version_tables (abfd)) | |
3345 | goto error_free_sym; | |
3346 | ||
3347 | /* Read in the symbol versions, but don't bother to convert them | |
3348 | to internal format. */ | |
3349 | if (elf_dynversym (abfd) != 0) | |
3350 | { | |
3351 | Elf_Internal_Shdr *versymhdr; | |
3352 | ||
3353 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
3354 | extversym = bfd_malloc (versymhdr->sh_size); | |
3355 | if (extversym == NULL) | |
3356 | goto error_free_sym; | |
3357 | amt = versymhdr->sh_size; | |
3358 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
3359 | || bfd_bread (extversym, amt, abfd) != amt) | |
3360 | goto error_free_vers; | |
3361 | } | |
3362 | } | |
3363 | ||
3364 | weaks = NULL; | |
3365 | ||
3366 | ever = extversym != NULL ? extversym + extsymoff : NULL; | |
3367 | for (isym = isymbuf, isymend = isymbuf + extsymcount; | |
3368 | isym < isymend; | |
3369 | isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
3370 | { | |
3371 | int bind; | |
3372 | bfd_vma value; | |
3373 | asection *sec; | |
3374 | flagword flags; | |
3375 | const char *name; | |
3376 | struct elf_link_hash_entry *h; | |
3377 | bfd_boolean definition; | |
3378 | bfd_boolean size_change_ok; | |
3379 | bfd_boolean type_change_ok; | |
3380 | bfd_boolean new_weakdef; | |
3381 | bfd_boolean override; | |
3382 | unsigned int old_alignment; | |
3383 | bfd *old_bfd; | |
3384 | ||
3385 | override = FALSE; | |
3386 | ||
3387 | flags = BSF_NO_FLAGS; | |
3388 | sec = NULL; | |
3389 | value = isym->st_value; | |
3390 | *sym_hash = NULL; | |
3391 | ||
3392 | bind = ELF_ST_BIND (isym->st_info); | |
3393 | if (bind == STB_LOCAL) | |
3394 | { | |
3395 | /* This should be impossible, since ELF requires that all | |
3396 | global symbols follow all local symbols, and that sh_info | |
3397 | point to the first global symbol. Unfortunately, Irix 5 | |
3398 | screws this up. */ | |
3399 | continue; | |
3400 | } | |
3401 | else if (bind == STB_GLOBAL) | |
3402 | { | |
3403 | if (isym->st_shndx != SHN_UNDEF | |
3404 | && isym->st_shndx != SHN_COMMON) | |
3405 | flags = BSF_GLOBAL; | |
3406 | } | |
3407 | else if (bind == STB_WEAK) | |
3408 | flags = BSF_WEAK; | |
3409 | else | |
3410 | { | |
3411 | /* Leave it up to the processor backend. */ | |
3412 | } | |
3413 | ||
3414 | if (isym->st_shndx == SHN_UNDEF) | |
3415 | sec = bfd_und_section_ptr; | |
3416 | else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
3417 | { | |
3418 | sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3419 | if (sec == NULL) | |
3420 | sec = bfd_abs_section_ptr; | |
3421 | else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
3422 | value -= sec->vma; | |
3423 | } | |
3424 | else if (isym->st_shndx == SHN_ABS) | |
3425 | sec = bfd_abs_section_ptr; | |
3426 | else if (isym->st_shndx == SHN_COMMON) | |
3427 | { | |
3428 | sec = bfd_com_section_ptr; | |
3429 | /* What ELF calls the size we call the value. What ELF | |
3430 | calls the value we call the alignment. */ | |
3431 | value = isym->st_size; | |
3432 | } | |
3433 | else | |
3434 | { | |
3435 | /* Leave it up to the processor backend. */ | |
3436 | } | |
3437 | ||
3438 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3439 | isym->st_name); | |
3440 | if (name == NULL) | |
3441 | goto error_free_vers; | |
3442 | ||
3443 | if (isym->st_shndx == SHN_COMMON | |
3444 | && ELF_ST_TYPE (isym->st_info) == STT_TLS) | |
3445 | { | |
3446 | asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); | |
3447 | ||
3448 | if (tcomm == NULL) | |
3449 | { | |
3450 | tcomm = bfd_make_section (abfd, ".tcommon"); | |
3451 | if (tcomm == NULL | |
3452 | || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC | |
3453 | | SEC_IS_COMMON | |
3454 | | SEC_LINKER_CREATED | |
3455 | | SEC_THREAD_LOCAL))) | |
3456 | goto error_free_vers; | |
3457 | } | |
3458 | sec = tcomm; | |
3459 | } | |
3460 | else if (add_symbol_hook) | |
3461 | { | |
3462 | if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec, | |
3463 | &value)) | |
3464 | goto error_free_vers; | |
3465 | ||
3466 | /* The hook function sets the name to NULL if this symbol | |
3467 | should be skipped for some reason. */ | |
3468 | if (name == NULL) | |
3469 | continue; | |
3470 | } | |
3471 | ||
3472 | /* Sanity check that all possibilities were handled. */ | |
3473 | if (sec == NULL) | |
3474 | { | |
3475 | bfd_set_error (bfd_error_bad_value); | |
3476 | goto error_free_vers; | |
3477 | } | |
3478 | ||
3479 | if (bfd_is_und_section (sec) | |
3480 | || bfd_is_com_section (sec)) | |
3481 | definition = FALSE; | |
3482 | else | |
3483 | definition = TRUE; | |
3484 | ||
3485 | size_change_ok = FALSE; | |
3486 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; | |
3487 | old_alignment = 0; | |
3488 | old_bfd = NULL; | |
3489 | ||
3490 | if (is_elf_hash_table (hash_table)) | |
3491 | { | |
3492 | Elf_Internal_Versym iver; | |
3493 | unsigned int vernum = 0; | |
3494 | bfd_boolean skip; | |
3495 | ||
3496 | if (ever != NULL) | |
3497 | { | |
3498 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
3499 | vernum = iver.vs_vers & VERSYM_VERSION; | |
3500 | ||
3501 | /* If this is a hidden symbol, or if it is not version | |
3502 | 1, we append the version name to the symbol name. | |
3503 | However, we do not modify a non-hidden absolute | |
3504 | symbol, because it might be the version symbol | |
3505 | itself. FIXME: What if it isn't? */ | |
3506 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 | |
3507 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
3508 | { | |
3509 | const char *verstr; | |
3510 | size_t namelen, verlen, newlen; | |
3511 | char *newname, *p; | |
3512 | ||
3513 | if (isym->st_shndx != SHN_UNDEF) | |
3514 | { | |
3515 | if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info) | |
3516 | { | |
3517 | (*_bfd_error_handler) | |
d003868e AM |
3518 | (_("%B: %s: invalid version %u (max %d)"), |
3519 | abfd, name, vernum, | |
4ad4eba5 AM |
3520 | elf_tdata (abfd)->dynverdef_hdr.sh_info); |
3521 | bfd_set_error (bfd_error_bad_value); | |
3522 | goto error_free_vers; | |
3523 | } | |
3524 | else if (vernum > 1) | |
3525 | verstr = | |
3526 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
3527 | else | |
3528 | verstr = ""; | |
3529 | } | |
3530 | else | |
3531 | { | |
3532 | /* We cannot simply test for the number of | |
3533 | entries in the VERNEED section since the | |
3534 | numbers for the needed versions do not start | |
3535 | at 0. */ | |
3536 | Elf_Internal_Verneed *t; | |
3537 | ||
3538 | verstr = NULL; | |
3539 | for (t = elf_tdata (abfd)->verref; | |
3540 | t != NULL; | |
3541 | t = t->vn_nextref) | |
3542 | { | |
3543 | Elf_Internal_Vernaux *a; | |
3544 | ||
3545 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3546 | { | |
3547 | if (a->vna_other == vernum) | |
3548 | { | |
3549 | verstr = a->vna_nodename; | |
3550 | break; | |
3551 | } | |
3552 | } | |
3553 | if (a != NULL) | |
3554 | break; | |
3555 | } | |
3556 | if (verstr == NULL) | |
3557 | { | |
3558 | (*_bfd_error_handler) | |
d003868e AM |
3559 | (_("%B: %s: invalid needed version %d"), |
3560 | abfd, name, vernum); | |
4ad4eba5 AM |
3561 | bfd_set_error (bfd_error_bad_value); |
3562 | goto error_free_vers; | |
3563 | } | |
3564 | } | |
3565 | ||
3566 | namelen = strlen (name); | |
3567 | verlen = strlen (verstr); | |
3568 | newlen = namelen + verlen + 2; | |
3569 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3570 | && isym->st_shndx != SHN_UNDEF) | |
3571 | ++newlen; | |
3572 | ||
3573 | newname = bfd_alloc (abfd, newlen); | |
3574 | if (newname == NULL) | |
3575 | goto error_free_vers; | |
3576 | memcpy (newname, name, namelen); | |
3577 | p = newname + namelen; | |
3578 | *p++ = ELF_VER_CHR; | |
3579 | /* If this is a defined non-hidden version symbol, | |
3580 | we add another @ to the name. This indicates the | |
3581 | default version of the symbol. */ | |
3582 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0 | |
3583 | && isym->st_shndx != SHN_UNDEF) | |
3584 | *p++ = ELF_VER_CHR; | |
3585 | memcpy (p, verstr, verlen + 1); | |
3586 | ||
3587 | name = newname; | |
3588 | } | |
3589 | } | |
3590 | ||
3591 | if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, | |
3592 | sym_hash, &skip, &override, | |
3593 | &type_change_ok, &size_change_ok)) | |
3594 | goto error_free_vers; | |
3595 | ||
3596 | if (skip) | |
3597 | continue; | |
3598 | ||
3599 | if (override) | |
3600 | definition = FALSE; | |
3601 | ||
3602 | h = *sym_hash; | |
3603 | while (h->root.type == bfd_link_hash_indirect | |
3604 | || h->root.type == bfd_link_hash_warning) | |
3605 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3606 | ||
3607 | /* Remember the old alignment if this is a common symbol, so | |
3608 | that we don't reduce the alignment later on. We can't | |
3609 | check later, because _bfd_generic_link_add_one_symbol | |
3610 | will set a default for the alignment which we want to | |
3611 | override. We also remember the old bfd where the existing | |
3612 | definition comes from. */ | |
3613 | switch (h->root.type) | |
3614 | { | |
3615 | default: | |
3616 | break; | |
3617 | ||
3618 | case bfd_link_hash_defined: | |
3619 | case bfd_link_hash_defweak: | |
3620 | old_bfd = h->root.u.def.section->owner; | |
3621 | break; | |
3622 | ||
3623 | case bfd_link_hash_common: | |
3624 | old_bfd = h->root.u.c.p->section->owner; | |
3625 | old_alignment = h->root.u.c.p->alignment_power; | |
3626 | break; | |
3627 | } | |
3628 | ||
3629 | if (elf_tdata (abfd)->verdef != NULL | |
3630 | && ! override | |
3631 | && vernum > 1 | |
3632 | && definition) | |
3633 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
3634 | } | |
3635 | ||
3636 | if (! (_bfd_generic_link_add_one_symbol | |
3637 | (info, abfd, name, flags, sec, value, NULL, FALSE, collect, | |
3638 | (struct bfd_link_hash_entry **) sym_hash))) | |
3639 | goto error_free_vers; | |
3640 | ||
3641 | h = *sym_hash; | |
3642 | while (h->root.type == bfd_link_hash_indirect | |
3643 | || h->root.type == bfd_link_hash_warning) | |
3644 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3645 | *sym_hash = h; | |
3646 | ||
3647 | new_weakdef = FALSE; | |
3648 | if (dynamic | |
3649 | && definition | |
3650 | && (flags & BSF_WEAK) != 0 | |
3651 | && ELF_ST_TYPE (isym->st_info) != STT_FUNC | |
3652 | && is_elf_hash_table (hash_table) | |
f6e332e6 | 3653 | && h->u.weakdef == NULL) |
4ad4eba5 AM |
3654 | { |
3655 | /* Keep a list of all weak defined non function symbols from | |
3656 | a dynamic object, using the weakdef field. Later in this | |
3657 | function we will set the weakdef field to the correct | |
3658 | value. We only put non-function symbols from dynamic | |
3659 | objects on this list, because that happens to be the only | |
3660 | time we need to know the normal symbol corresponding to a | |
3661 | weak symbol, and the information is time consuming to | |
3662 | figure out. If the weakdef field is not already NULL, | |
3663 | then this symbol was already defined by some previous | |
3664 | dynamic object, and we will be using that previous | |
3665 | definition anyhow. */ | |
3666 | ||
f6e332e6 | 3667 | h->u.weakdef = weaks; |
4ad4eba5 AM |
3668 | weaks = h; |
3669 | new_weakdef = TRUE; | |
3670 | } | |
3671 | ||
3672 | /* Set the alignment of a common symbol. */ | |
3673 | if (isym->st_shndx == SHN_COMMON | |
3674 | && h->root.type == bfd_link_hash_common) | |
3675 | { | |
3676 | unsigned int align; | |
3677 | ||
3678 | align = bfd_log2 (isym->st_value); | |
3679 | if (align > old_alignment | |
3680 | /* Permit an alignment power of zero if an alignment of one | |
3681 | is specified and no other alignments have been specified. */ | |
3682 | || (isym->st_value == 1 && old_alignment == 0)) | |
3683 | h->root.u.c.p->alignment_power = align; | |
3684 | else | |
3685 | h->root.u.c.p->alignment_power = old_alignment; | |
3686 | } | |
3687 | ||
3688 | if (is_elf_hash_table (hash_table)) | |
3689 | { | |
4ad4eba5 | 3690 | bfd_boolean dynsym; |
4ad4eba5 AM |
3691 | |
3692 | /* Check the alignment when a common symbol is involved. This | |
3693 | can change when a common symbol is overridden by a normal | |
3694 | definition or a common symbol is ignored due to the old | |
3695 | normal definition. We need to make sure the maximum | |
3696 | alignment is maintained. */ | |
3697 | if ((old_alignment || isym->st_shndx == SHN_COMMON) | |
3698 | && h->root.type != bfd_link_hash_common) | |
3699 | { | |
3700 | unsigned int common_align; | |
3701 | unsigned int normal_align; | |
3702 | unsigned int symbol_align; | |
3703 | bfd *normal_bfd; | |
3704 | bfd *common_bfd; | |
3705 | ||
3706 | symbol_align = ffs (h->root.u.def.value) - 1; | |
3707 | if (h->root.u.def.section->owner != NULL | |
3708 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
3709 | { | |
3710 | normal_align = h->root.u.def.section->alignment_power; | |
3711 | if (normal_align > symbol_align) | |
3712 | normal_align = symbol_align; | |
3713 | } | |
3714 | else | |
3715 | normal_align = symbol_align; | |
3716 | ||
3717 | if (old_alignment) | |
3718 | { | |
3719 | common_align = old_alignment; | |
3720 | common_bfd = old_bfd; | |
3721 | normal_bfd = abfd; | |
3722 | } | |
3723 | else | |
3724 | { | |
3725 | common_align = bfd_log2 (isym->st_value); | |
3726 | common_bfd = abfd; | |
3727 | normal_bfd = old_bfd; | |
3728 | } | |
3729 | ||
3730 | if (normal_align < common_align) | |
3731 | (*_bfd_error_handler) | |
d003868e AM |
3732 | (_("Warning: alignment %u of symbol `%s' in %B" |
3733 | " is smaller than %u in %B"), | |
3734 | normal_bfd, common_bfd, | |
3735 | 1 << normal_align, name, 1 << common_align); | |
4ad4eba5 AM |
3736 | } |
3737 | ||
3738 | /* Remember the symbol size and type. */ | |
3739 | if (isym->st_size != 0 | |
3740 | && (definition || h->size == 0)) | |
3741 | { | |
3742 | if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) | |
3743 | (*_bfd_error_handler) | |
d003868e AM |
3744 | (_("Warning: size of symbol `%s' changed" |
3745 | " from %lu in %B to %lu in %B"), | |
3746 | old_bfd, abfd, | |
4ad4eba5 | 3747 | name, (unsigned long) h->size, |
d003868e | 3748 | (unsigned long) isym->st_size); |
4ad4eba5 AM |
3749 | |
3750 | h->size = isym->st_size; | |
3751 | } | |
3752 | ||
3753 | /* If this is a common symbol, then we always want H->SIZE | |
3754 | to be the size of the common symbol. The code just above | |
3755 | won't fix the size if a common symbol becomes larger. We | |
3756 | don't warn about a size change here, because that is | |
3757 | covered by --warn-common. */ | |
3758 | if (h->root.type == bfd_link_hash_common) | |
3759 | h->size = h->root.u.c.size; | |
3760 | ||
3761 | if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE | |
3762 | && (definition || h->type == STT_NOTYPE)) | |
3763 | { | |
3764 | if (h->type != STT_NOTYPE | |
3765 | && h->type != ELF_ST_TYPE (isym->st_info) | |
3766 | && ! type_change_ok) | |
3767 | (*_bfd_error_handler) | |
d003868e AM |
3768 | (_("Warning: type of symbol `%s' changed" |
3769 | " from %d to %d in %B"), | |
3770 | abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); | |
4ad4eba5 AM |
3771 | |
3772 | h->type = ELF_ST_TYPE (isym->st_info); | |
3773 | } | |
3774 | ||
3775 | /* If st_other has a processor-specific meaning, specific | |
3776 | code might be needed here. We never merge the visibility | |
3777 | attribute with the one from a dynamic object. */ | |
3778 | if (bed->elf_backend_merge_symbol_attribute) | |
3779 | (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, | |
3780 | dynamic); | |
3781 | ||
3782 | if (isym->st_other != 0 && !dynamic) | |
3783 | { | |
3784 | unsigned char hvis, symvis, other, nvis; | |
3785 | ||
3786 | /* Take the balance of OTHER from the definition. */ | |
3787 | other = (definition ? isym->st_other : h->other); | |
3788 | other &= ~ ELF_ST_VISIBILITY (-1); | |
3789 | ||
3790 | /* Combine visibilities, using the most constraining one. */ | |
3791 | hvis = ELF_ST_VISIBILITY (h->other); | |
3792 | symvis = ELF_ST_VISIBILITY (isym->st_other); | |
3793 | if (! hvis) | |
3794 | nvis = symvis; | |
3795 | else if (! symvis) | |
3796 | nvis = hvis; | |
3797 | else | |
3798 | nvis = hvis < symvis ? hvis : symvis; | |
3799 | ||
3800 | h->other = other | nvis; | |
3801 | } | |
3802 | ||
3803 | /* Set a flag in the hash table entry indicating the type of | |
3804 | reference or definition we just found. Keep a count of | |
3805 | the number of dynamic symbols we find. A dynamic symbol | |
3806 | is one which is referenced or defined by both a regular | |
3807 | object and a shared object. */ | |
4ad4eba5 AM |
3808 | dynsym = FALSE; |
3809 | if (! dynamic) | |
3810 | { | |
3811 | if (! definition) | |
3812 | { | |
f5385ebf | 3813 | h->ref_regular = 1; |
4ad4eba5 | 3814 | if (bind != STB_WEAK) |
f5385ebf | 3815 | h->ref_regular_nonweak = 1; |
4ad4eba5 AM |
3816 | } |
3817 | else | |
f5385ebf | 3818 | h->def_regular = 1; |
4ad4eba5 | 3819 | if (! info->executable |
f5385ebf AM |
3820 | || h->def_dynamic |
3821 | || h->ref_dynamic) | |
4ad4eba5 AM |
3822 | dynsym = TRUE; |
3823 | } | |
3824 | else | |
3825 | { | |
3826 | if (! definition) | |
f5385ebf | 3827 | h->ref_dynamic = 1; |
4ad4eba5 | 3828 | else |
f5385ebf AM |
3829 | h->def_dynamic = 1; |
3830 | if (h->def_regular | |
3831 | || h->ref_regular | |
f6e332e6 | 3832 | || (h->u.weakdef != NULL |
4ad4eba5 | 3833 | && ! new_weakdef |
f6e332e6 | 3834 | && h->u.weakdef->dynindx != -1)) |
4ad4eba5 AM |
3835 | dynsym = TRUE; |
3836 | } | |
3837 | ||
4ad4eba5 AM |
3838 | /* Check to see if we need to add an indirect symbol for |
3839 | the default name. */ | |
3840 | if (definition || h->root.type == bfd_link_hash_common) | |
3841 | if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, | |
3842 | &sec, &value, &dynsym, | |
3843 | override)) | |
3844 | goto error_free_vers; | |
3845 | ||
3846 | if (definition && !dynamic) | |
3847 | { | |
3848 | char *p = strchr (name, ELF_VER_CHR); | |
3849 | if (p != NULL && p[1] != ELF_VER_CHR) | |
3850 | { | |
3851 | /* Queue non-default versions so that .symver x, x@FOO | |
3852 | aliases can be checked. */ | |
3853 | if (! nondeflt_vers) | |
3854 | { | |
3855 | amt = (isymend - isym + 1) | |
3856 | * sizeof (struct elf_link_hash_entry *); | |
3857 | nondeflt_vers = bfd_malloc (amt); | |
3858 | } | |
3859 | nondeflt_vers [nondeflt_vers_cnt++] = h; | |
3860 | } | |
3861 | } | |
3862 | ||
3863 | if (dynsym && h->dynindx == -1) | |
3864 | { | |
c152c796 | 3865 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 | 3866 | goto error_free_vers; |
f6e332e6 | 3867 | if (h->u.weakdef != NULL |
4ad4eba5 | 3868 | && ! new_weakdef |
f6e332e6 | 3869 | && h->u.weakdef->dynindx == -1) |
4ad4eba5 | 3870 | { |
f6e332e6 | 3871 | if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) |
4ad4eba5 AM |
3872 | goto error_free_vers; |
3873 | } | |
3874 | } | |
3875 | else if (dynsym && h->dynindx != -1) | |
3876 | /* If the symbol already has a dynamic index, but | |
3877 | visibility says it should not be visible, turn it into | |
3878 | a local symbol. */ | |
3879 | switch (ELF_ST_VISIBILITY (h->other)) | |
3880 | { | |
3881 | case STV_INTERNAL: | |
3882 | case STV_HIDDEN: | |
3883 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
3884 | dynsym = FALSE; | |
3885 | break; | |
3886 | } | |
3887 | ||
3888 | if (!add_needed | |
3889 | && definition | |
3890 | && dynsym | |
f5385ebf | 3891 | && h->ref_regular) |
4ad4eba5 AM |
3892 | { |
3893 | int ret; | |
3894 | const char *soname = elf_dt_name (abfd); | |
3895 | ||
3896 | /* A symbol from a library loaded via DT_NEEDED of some | |
3897 | other library is referenced by a regular object. | |
e56f61be L |
3898 | Add a DT_NEEDED entry for it. Issue an error if |
3899 | --no-add-needed is used. */ | |
3900 | if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) | |
3901 | { | |
3902 | (*_bfd_error_handler) | |
3903 | (_("%s: invalid DSO for symbol `%s' definition"), | |
d003868e | 3904 | abfd, name); |
e56f61be L |
3905 | bfd_set_error (bfd_error_bad_value); |
3906 | goto error_free_vers; | |
3907 | } | |
3908 | ||
4ad4eba5 AM |
3909 | add_needed = TRUE; |
3910 | ret = elf_add_dt_needed_tag (info, soname, add_needed); | |
3911 | if (ret < 0) | |
3912 | goto error_free_vers; | |
3913 | ||
3914 | BFD_ASSERT (ret == 0); | |
3915 | } | |
3916 | } | |
3917 | } | |
3918 | ||
3919 | /* Now that all the symbols from this input file are created, handle | |
3920 | .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ | |
3921 | if (nondeflt_vers != NULL) | |
3922 | { | |
3923 | bfd_size_type cnt, symidx; | |
3924 | ||
3925 | for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) | |
3926 | { | |
3927 | struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; | |
3928 | char *shortname, *p; | |
3929 | ||
3930 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
3931 | if (p == NULL | |
3932 | || (h->root.type != bfd_link_hash_defined | |
3933 | && h->root.type != bfd_link_hash_defweak)) | |
3934 | continue; | |
3935 | ||
3936 | amt = p - h->root.root.string; | |
3937 | shortname = bfd_malloc (amt + 1); | |
3938 | memcpy (shortname, h->root.root.string, amt); | |
3939 | shortname[amt] = '\0'; | |
3940 | ||
3941 | hi = (struct elf_link_hash_entry *) | |
3942 | bfd_link_hash_lookup (&hash_table->root, shortname, | |
3943 | FALSE, FALSE, FALSE); | |
3944 | if (hi != NULL | |
3945 | && hi->root.type == h->root.type | |
3946 | && hi->root.u.def.value == h->root.u.def.value | |
3947 | && hi->root.u.def.section == h->root.u.def.section) | |
3948 | { | |
3949 | (*bed->elf_backend_hide_symbol) (info, hi, TRUE); | |
3950 | hi->root.type = bfd_link_hash_indirect; | |
3951 | hi->root.u.i.link = (struct bfd_link_hash_entry *) h; | |
3952 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
3953 | sym_hash = elf_sym_hashes (abfd); | |
3954 | if (sym_hash) | |
3955 | for (symidx = 0; symidx < extsymcount; ++symidx) | |
3956 | if (sym_hash[symidx] == hi) | |
3957 | { | |
3958 | sym_hash[symidx] = h; | |
3959 | break; | |
3960 | } | |
3961 | } | |
3962 | free (shortname); | |
3963 | } | |
3964 | free (nondeflt_vers); | |
3965 | nondeflt_vers = NULL; | |
3966 | } | |
3967 | ||
3968 | if (extversym != NULL) | |
3969 | { | |
3970 | free (extversym); | |
3971 | extversym = NULL; | |
3972 | } | |
3973 | ||
3974 | if (isymbuf != NULL) | |
3975 | free (isymbuf); | |
3976 | isymbuf = NULL; | |
3977 | ||
3978 | /* Now set the weakdefs field correctly for all the weak defined | |
3979 | symbols we found. The only way to do this is to search all the | |
3980 | symbols. Since we only need the information for non functions in | |
3981 | dynamic objects, that's the only time we actually put anything on | |
3982 | the list WEAKS. We need this information so that if a regular | |
3983 | object refers to a symbol defined weakly in a dynamic object, the | |
3984 | real symbol in the dynamic object is also put in the dynamic | |
3985 | symbols; we also must arrange for both symbols to point to the | |
3986 | same memory location. We could handle the general case of symbol | |
3987 | aliasing, but a general symbol alias can only be generated in | |
3988 | assembler code, handling it correctly would be very time | |
3989 | consuming, and other ELF linkers don't handle general aliasing | |
3990 | either. */ | |
3991 | if (weaks != NULL) | |
3992 | { | |
3993 | struct elf_link_hash_entry **hpp; | |
3994 | struct elf_link_hash_entry **hppend; | |
3995 | struct elf_link_hash_entry **sorted_sym_hash; | |
3996 | struct elf_link_hash_entry *h; | |
3997 | size_t sym_count; | |
3998 | ||
3999 | /* Since we have to search the whole symbol list for each weak | |
4000 | defined symbol, search time for N weak defined symbols will be | |
4001 | O(N^2). Binary search will cut it down to O(NlogN). */ | |
4002 | amt = extsymcount * sizeof (struct elf_link_hash_entry *); | |
4003 | sorted_sym_hash = bfd_malloc (amt); | |
4004 | if (sorted_sym_hash == NULL) | |
4005 | goto error_return; | |
4006 | sym_hash = sorted_sym_hash; | |
4007 | hpp = elf_sym_hashes (abfd); | |
4008 | hppend = hpp + extsymcount; | |
4009 | sym_count = 0; | |
4010 | for (; hpp < hppend; hpp++) | |
4011 | { | |
4012 | h = *hpp; | |
4013 | if (h != NULL | |
4014 | && h->root.type == bfd_link_hash_defined | |
4015 | && h->type != STT_FUNC) | |
4016 | { | |
4017 | *sym_hash = h; | |
4018 | sym_hash++; | |
4019 | sym_count++; | |
4020 | } | |
4021 | } | |
4022 | ||
4023 | qsort (sorted_sym_hash, sym_count, | |
4024 | sizeof (struct elf_link_hash_entry *), | |
4025 | elf_sort_symbol); | |
4026 | ||
4027 | while (weaks != NULL) | |
4028 | { | |
4029 | struct elf_link_hash_entry *hlook; | |
4030 | asection *slook; | |
4031 | bfd_vma vlook; | |
4032 | long ilook; | |
4033 | size_t i, j, idx; | |
4034 | ||
4035 | hlook = weaks; | |
f6e332e6 AM |
4036 | weaks = hlook->u.weakdef; |
4037 | hlook->u.weakdef = NULL; | |
4ad4eba5 AM |
4038 | |
4039 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
4040 | || hlook->root.type == bfd_link_hash_defweak | |
4041 | || hlook->root.type == bfd_link_hash_common | |
4042 | || hlook->root.type == bfd_link_hash_indirect); | |
4043 | slook = hlook->root.u.def.section; | |
4044 | vlook = hlook->root.u.def.value; | |
4045 | ||
4046 | ilook = -1; | |
4047 | i = 0; | |
4048 | j = sym_count; | |
4049 | while (i < j) | |
4050 | { | |
4051 | bfd_signed_vma vdiff; | |
4052 | idx = (i + j) / 2; | |
4053 | h = sorted_sym_hash [idx]; | |
4054 | vdiff = vlook - h->root.u.def.value; | |
4055 | if (vdiff < 0) | |
4056 | j = idx; | |
4057 | else if (vdiff > 0) | |
4058 | i = idx + 1; | |
4059 | else | |
4060 | { | |
a9b881be | 4061 | long sdiff = slook->id - h->root.u.def.section->id; |
4ad4eba5 AM |
4062 | if (sdiff < 0) |
4063 | j = idx; | |
4064 | else if (sdiff > 0) | |
4065 | i = idx + 1; | |
4066 | else | |
4067 | { | |
4068 | ilook = idx; | |
4069 | break; | |
4070 | } | |
4071 | } | |
4072 | } | |
4073 | ||
4074 | /* We didn't find a value/section match. */ | |
4075 | if (ilook == -1) | |
4076 | continue; | |
4077 | ||
4078 | for (i = ilook; i < sym_count; i++) | |
4079 | { | |
4080 | h = sorted_sym_hash [i]; | |
4081 | ||
4082 | /* Stop if value or section doesn't match. */ | |
4083 | if (h->root.u.def.value != vlook | |
4084 | || h->root.u.def.section != slook) | |
4085 | break; | |
4086 | else if (h != hlook) | |
4087 | { | |
f6e332e6 | 4088 | hlook->u.weakdef = h; |
4ad4eba5 AM |
4089 | |
4090 | /* If the weak definition is in the list of dynamic | |
4091 | symbols, make sure the real definition is put | |
4092 | there as well. */ | |
4093 | if (hlook->dynindx != -1 && h->dynindx == -1) | |
4094 | { | |
c152c796 | 4095 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
4ad4eba5 AM |
4096 | goto error_return; |
4097 | } | |
4098 | ||
4099 | /* If the real definition is in the list of dynamic | |
4100 | symbols, make sure the weak definition is put | |
4101 | there as well. If we don't do this, then the | |
4102 | dynamic loader might not merge the entries for the | |
4103 | real definition and the weak definition. */ | |
4104 | if (h->dynindx != -1 && hlook->dynindx == -1) | |
4105 | { | |
c152c796 | 4106 | if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) |
4ad4eba5 AM |
4107 | goto error_return; |
4108 | } | |
4109 | break; | |
4110 | } | |
4111 | } | |
4112 | } | |
4113 | ||
4114 | free (sorted_sym_hash); | |
4115 | } | |
4116 | ||
85fbca6a NC |
4117 | check_directives = get_elf_backend_data (abfd)->check_directives; |
4118 | if (check_directives) | |
4119 | check_directives (abfd, info); | |
4120 | ||
4ad4eba5 AM |
4121 | /* If this object is the same format as the output object, and it is |
4122 | not a shared library, then let the backend look through the | |
4123 | relocs. | |
4124 | ||
4125 | This is required to build global offset table entries and to | |
4126 | arrange for dynamic relocs. It is not required for the | |
4127 | particular common case of linking non PIC code, even when linking | |
4128 | against shared libraries, but unfortunately there is no way of | |
4129 | knowing whether an object file has been compiled PIC or not. | |
4130 | Looking through the relocs is not particularly time consuming. | |
4131 | The problem is that we must either (1) keep the relocs in memory, | |
4132 | which causes the linker to require additional runtime memory or | |
4133 | (2) read the relocs twice from the input file, which wastes time. | |
4134 | This would be a good case for using mmap. | |
4135 | ||
4136 | I have no idea how to handle linking PIC code into a file of a | |
4137 | different format. It probably can't be done. */ | |
4138 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4139 | if (! dynamic | |
4140 | && is_elf_hash_table (hash_table) | |
4141 | && hash_table->root.creator == abfd->xvec | |
4142 | && check_relocs != NULL) | |
4143 | { | |
4144 | asection *o; | |
4145 | ||
4146 | for (o = abfd->sections; o != NULL; o = o->next) | |
4147 | { | |
4148 | Elf_Internal_Rela *internal_relocs; | |
4149 | bfd_boolean ok; | |
4150 | ||
4151 | if ((o->flags & SEC_RELOC) == 0 | |
4152 | || o->reloc_count == 0 | |
4153 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
4154 | && (o->flags & SEC_DEBUGGING) != 0) | |
4155 | || bfd_is_abs_section (o->output_section)) | |
4156 | continue; | |
4157 | ||
4158 | internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, | |
4159 | info->keep_memory); | |
4160 | if (internal_relocs == NULL) | |
4161 | goto error_return; | |
4162 | ||
4163 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4164 | ||
4165 | if (elf_section_data (o)->relocs != internal_relocs) | |
4166 | free (internal_relocs); | |
4167 | ||
4168 | if (! ok) | |
4169 | goto error_return; | |
4170 | } | |
4171 | } | |
4172 | ||
4173 | /* If this is a non-traditional link, try to optimize the handling | |
4174 | of the .stab/.stabstr sections. */ | |
4175 | if (! dynamic | |
4176 | && ! info->traditional_format | |
4177 | && is_elf_hash_table (hash_table) | |
4178 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
4179 | { | |
4180 | asection *stabstr; | |
4181 | ||
4182 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
4183 | if (stabstr != NULL) | |
4184 | { | |
4185 | bfd_size_type string_offset = 0; | |
4186 | asection *stab; | |
4187 | ||
4188 | for (stab = abfd->sections; stab; stab = stab->next) | |
4189 | if (strncmp (".stab", stab->name, 5) == 0 | |
4190 | && (!stab->name[5] || | |
4191 | (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) | |
4192 | && (stab->flags & SEC_MERGE) == 0 | |
4193 | && !bfd_is_abs_section (stab->output_section)) | |
4194 | { | |
4195 | struct bfd_elf_section_data *secdata; | |
4196 | ||
4197 | secdata = elf_section_data (stab); | |
4198 | if (! _bfd_link_section_stabs (abfd, | |
3722b82f | 4199 | &hash_table->stab_info, |
4ad4eba5 AM |
4200 | stab, stabstr, |
4201 | &secdata->sec_info, | |
4202 | &string_offset)) | |
4203 | goto error_return; | |
4204 | if (secdata->sec_info) | |
4205 | stab->sec_info_type = ELF_INFO_TYPE_STABS; | |
4206 | } | |
4207 | } | |
4208 | } | |
4209 | ||
4ad4eba5 AM |
4210 | if (is_elf_hash_table (hash_table)) |
4211 | { | |
4212 | /* Add this bfd to the loaded list. */ | |
4213 | struct elf_link_loaded_list *n; | |
4214 | ||
4215 | n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); | |
4216 | if (n == NULL) | |
4217 | goto error_return; | |
4218 | n->abfd = abfd; | |
4219 | n->next = hash_table->loaded; | |
4220 | hash_table->loaded = n; | |
4221 | } | |
4222 | ||
4223 | return TRUE; | |
4224 | ||
4225 | error_free_vers: | |
4226 | if (nondeflt_vers != NULL) | |
4227 | free (nondeflt_vers); | |
4228 | if (extversym != NULL) | |
4229 | free (extversym); | |
4230 | error_free_sym: | |
4231 | if (isymbuf != NULL) | |
4232 | free (isymbuf); | |
4233 | error_return: | |
4234 | return FALSE; | |
4235 | } | |
4236 | ||
8387904d AM |
4237 | /* Return the linker hash table entry of a symbol that might be |
4238 | satisfied by an archive symbol. Return -1 on error. */ | |
4239 | ||
4240 | struct elf_link_hash_entry * | |
4241 | _bfd_elf_archive_symbol_lookup (bfd *abfd, | |
4242 | struct bfd_link_info *info, | |
4243 | const char *name) | |
4244 | { | |
4245 | struct elf_link_hash_entry *h; | |
4246 | char *p, *copy; | |
4247 | size_t len, first; | |
4248 | ||
4249 | h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); | |
4250 | if (h != NULL) | |
4251 | return h; | |
4252 | ||
4253 | /* If this is a default version (the name contains @@), look up the | |
4254 | symbol again with only one `@' as well as without the version. | |
4255 | The effect is that references to the symbol with and without the | |
4256 | version will be matched by the default symbol in the archive. */ | |
4257 | ||
4258 | p = strchr (name, ELF_VER_CHR); | |
4259 | if (p == NULL || p[1] != ELF_VER_CHR) | |
4260 | return h; | |
4261 | ||
4262 | /* First check with only one `@'. */ | |
4263 | len = strlen (name); | |
4264 | copy = bfd_alloc (abfd, len); | |
4265 | if (copy == NULL) | |
4266 | return (struct elf_link_hash_entry *) 0 - 1; | |
4267 | ||
4268 | first = p - name + 1; | |
4269 | memcpy (copy, name, first); | |
4270 | memcpy (copy + first, name + first + 1, len - first); | |
4271 | ||
4272 | h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); | |
4273 | if (h == NULL) | |
4274 | { | |
4275 | /* We also need to check references to the symbol without the | |
4276 | version. */ | |
4277 | copy[first - 1] = '\0'; | |
4278 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
4279 | FALSE, FALSE, FALSE); | |
4280 | } | |
4281 | ||
4282 | bfd_release (abfd, copy); | |
4283 | return h; | |
4284 | } | |
4285 | ||
0ad989f9 L |
4286 | /* Add symbols from an ELF archive file to the linker hash table. We |
4287 | don't use _bfd_generic_link_add_archive_symbols because of a | |
4288 | problem which arises on UnixWare. The UnixWare libc.so is an | |
4289 | archive which includes an entry libc.so.1 which defines a bunch of | |
4290 | symbols. The libc.so archive also includes a number of other | |
4291 | object files, which also define symbols, some of which are the same | |
4292 | as those defined in libc.so.1. Correct linking requires that we | |
4293 | consider each object file in turn, and include it if it defines any | |
4294 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
4295 | this; it looks through the list of undefined symbols, and includes | |
4296 | any object file which defines them. When this algorithm is used on | |
4297 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
4298 | bunch of symbols. This means that some of the other objects in the | |
4299 | archive are not included in the link, which is incorrect since they | |
4300 | precede libc.so.1 in the archive. | |
4301 | ||
4302 | Fortunately, ELF archive handling is simpler than that done by | |
4303 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
4304 | oddities. In ELF, if we find a symbol in the archive map, and the | |
4305 | symbol is currently undefined, we know that we must pull in that | |
4306 | object file. | |
4307 | ||
4308 | Unfortunately, we do have to make multiple passes over the symbol | |
4309 | table until nothing further is resolved. */ | |
4310 | ||
4ad4eba5 AM |
4311 | static bfd_boolean |
4312 | elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) | |
0ad989f9 L |
4313 | { |
4314 | symindex c; | |
4315 | bfd_boolean *defined = NULL; | |
4316 | bfd_boolean *included = NULL; | |
4317 | carsym *symdefs; | |
4318 | bfd_boolean loop; | |
4319 | bfd_size_type amt; | |
8387904d AM |
4320 | const struct elf_backend_data *bed; |
4321 | struct elf_link_hash_entry * (*archive_symbol_lookup) | |
4322 | (bfd *, struct bfd_link_info *, const char *); | |
0ad989f9 L |
4323 | |
4324 | if (! bfd_has_map (abfd)) | |
4325 | { | |
4326 | /* An empty archive is a special case. */ | |
4327 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
4328 | return TRUE; | |
4329 | bfd_set_error (bfd_error_no_armap); | |
4330 | return FALSE; | |
4331 | } | |
4332 | ||
4333 | /* Keep track of all symbols we know to be already defined, and all | |
4334 | files we know to be already included. This is to speed up the | |
4335 | second and subsequent passes. */ | |
4336 | c = bfd_ardata (abfd)->symdef_count; | |
4337 | if (c == 0) | |
4338 | return TRUE; | |
4339 | amt = c; | |
4340 | amt *= sizeof (bfd_boolean); | |
4341 | defined = bfd_zmalloc (amt); | |
4342 | included = bfd_zmalloc (amt); | |
4343 | if (defined == NULL || included == NULL) | |
4344 | goto error_return; | |
4345 | ||
4346 | symdefs = bfd_ardata (abfd)->symdefs; | |
8387904d AM |
4347 | bed = get_elf_backend_data (abfd); |
4348 | archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; | |
0ad989f9 L |
4349 | |
4350 | do | |
4351 | { | |
4352 | file_ptr last; | |
4353 | symindex i; | |
4354 | carsym *symdef; | |
4355 | carsym *symdefend; | |
4356 | ||
4357 | loop = FALSE; | |
4358 | last = -1; | |
4359 | ||
4360 | symdef = symdefs; | |
4361 | symdefend = symdef + c; | |
4362 | for (i = 0; symdef < symdefend; symdef++, i++) | |
4363 | { | |
4364 | struct elf_link_hash_entry *h; | |
4365 | bfd *element; | |
4366 | struct bfd_link_hash_entry *undefs_tail; | |
4367 | symindex mark; | |
4368 | ||
4369 | if (defined[i] || included[i]) | |
4370 | continue; | |
4371 | if (symdef->file_offset == last) | |
4372 | { | |
4373 | included[i] = TRUE; | |
4374 | continue; | |
4375 | } | |
4376 | ||
8387904d AM |
4377 | h = archive_symbol_lookup (abfd, info, symdef->name); |
4378 | if (h == (struct elf_link_hash_entry *) 0 - 1) | |
4379 | goto error_return; | |
0ad989f9 L |
4380 | |
4381 | if (h == NULL) | |
4382 | continue; | |
4383 | ||
4384 | if (h->root.type == bfd_link_hash_common) | |
4385 | { | |
4386 | /* We currently have a common symbol. The archive map contains | |
4387 | a reference to this symbol, so we may want to include it. We | |
4388 | only want to include it however, if this archive element | |
4389 | contains a definition of the symbol, not just another common | |
4390 | declaration of it. | |
4391 | ||
4392 | Unfortunately some archivers (including GNU ar) will put | |
4393 | declarations of common symbols into their archive maps, as | |
4394 | well as real definitions, so we cannot just go by the archive | |
4395 | map alone. Instead we must read in the element's symbol | |
4396 | table and check that to see what kind of symbol definition | |
4397 | this is. */ | |
4398 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
4399 | continue; | |
4400 | } | |
4401 | else if (h->root.type != bfd_link_hash_undefined) | |
4402 | { | |
4403 | if (h->root.type != bfd_link_hash_undefweak) | |
4404 | defined[i] = TRUE; | |
4405 | continue; | |
4406 | } | |
4407 | ||
4408 | /* We need to include this archive member. */ | |
4409 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
4410 | if (element == NULL) | |
4411 | goto error_return; | |
4412 | ||
4413 | if (! bfd_check_format (element, bfd_object)) | |
4414 | goto error_return; | |
4415 | ||
4416 | /* Doublecheck that we have not included this object | |
4417 | already--it should be impossible, but there may be | |
4418 | something wrong with the archive. */ | |
4419 | if (element->archive_pass != 0) | |
4420 | { | |
4421 | bfd_set_error (bfd_error_bad_value); | |
4422 | goto error_return; | |
4423 | } | |
4424 | element->archive_pass = 1; | |
4425 | ||
4426 | undefs_tail = info->hash->undefs_tail; | |
4427 | ||
4428 | if (! (*info->callbacks->add_archive_element) (info, element, | |
4429 | symdef->name)) | |
4430 | goto error_return; | |
4431 | if (! bfd_link_add_symbols (element, info)) | |
4432 | goto error_return; | |
4433 | ||
4434 | /* If there are any new undefined symbols, we need to make | |
4435 | another pass through the archive in order to see whether | |
4436 | they can be defined. FIXME: This isn't perfect, because | |
4437 | common symbols wind up on undefs_tail and because an | |
4438 | undefined symbol which is defined later on in this pass | |
4439 | does not require another pass. This isn't a bug, but it | |
4440 | does make the code less efficient than it could be. */ | |
4441 | if (undefs_tail != info->hash->undefs_tail) | |
4442 | loop = TRUE; | |
4443 | ||
4444 | /* Look backward to mark all symbols from this object file | |
4445 | which we have already seen in this pass. */ | |
4446 | mark = i; | |
4447 | do | |
4448 | { | |
4449 | included[mark] = TRUE; | |
4450 | if (mark == 0) | |
4451 | break; | |
4452 | --mark; | |
4453 | } | |
4454 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4455 | ||
4456 | /* We mark subsequent symbols from this object file as we go | |
4457 | on through the loop. */ | |
4458 | last = symdef->file_offset; | |
4459 | } | |
4460 | } | |
4461 | while (loop); | |
4462 | ||
4463 | free (defined); | |
4464 | free (included); | |
4465 | ||
4466 | return TRUE; | |
4467 | ||
4468 | error_return: | |
4469 | if (defined != NULL) | |
4470 | free (defined); | |
4471 | if (included != NULL) | |
4472 | free (included); | |
4473 | return FALSE; | |
4474 | } | |
4ad4eba5 AM |
4475 | |
4476 | /* Given an ELF BFD, add symbols to the global hash table as | |
4477 | appropriate. */ | |
4478 | ||
4479 | bfd_boolean | |
4480 | bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) | |
4481 | { | |
4482 | switch (bfd_get_format (abfd)) | |
4483 | { | |
4484 | case bfd_object: | |
4485 | return elf_link_add_object_symbols (abfd, info); | |
4486 | case bfd_archive: | |
4487 | return elf_link_add_archive_symbols (abfd, info); | |
4488 | default: | |
4489 | bfd_set_error (bfd_error_wrong_format); | |
4490 | return FALSE; | |
4491 | } | |
4492 | } | |
5a580b3a AM |
4493 | \f |
4494 | /* This function will be called though elf_link_hash_traverse to store | |
4495 | all hash value of the exported symbols in an array. */ | |
4496 | ||
4497 | static bfd_boolean | |
4498 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
4499 | { | |
4500 | unsigned long **valuep = data; | |
4501 | const char *name; | |
4502 | char *p; | |
4503 | unsigned long ha; | |
4504 | char *alc = NULL; | |
4505 | ||
4506 | if (h->root.type == bfd_link_hash_warning) | |
4507 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4508 | ||
4509 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
4510 | if (h->dynindx == -1) | |
4511 | return TRUE; | |
4512 | ||
4513 | name = h->root.root.string; | |
4514 | p = strchr (name, ELF_VER_CHR); | |
4515 | if (p != NULL) | |
4516 | { | |
4517 | alc = bfd_malloc (p - name + 1); | |
4518 | memcpy (alc, name, p - name); | |
4519 | alc[p - name] = '\0'; | |
4520 | name = alc; | |
4521 | } | |
4522 | ||
4523 | /* Compute the hash value. */ | |
4524 | ha = bfd_elf_hash (name); | |
4525 | ||
4526 | /* Store the found hash value in the array given as the argument. */ | |
4527 | *(*valuep)++ = ha; | |
4528 | ||
4529 | /* And store it in the struct so that we can put it in the hash table | |
4530 | later. */ | |
f6e332e6 | 4531 | h->u.elf_hash_value = ha; |
5a580b3a AM |
4532 | |
4533 | if (alc != NULL) | |
4534 | free (alc); | |
4535 | ||
4536 | return TRUE; | |
4537 | } | |
4538 | ||
4539 | /* Array used to determine the number of hash table buckets to use | |
4540 | based on the number of symbols there are. If there are fewer than | |
4541 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4542 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4543 | than 32771 buckets. */ | |
4544 | ||
4545 | static const size_t elf_buckets[] = | |
4546 | { | |
4547 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
4548 | 16411, 32771, 0 | |
4549 | }; | |
4550 | ||
4551 | /* Compute bucket count for hashing table. We do not use a static set | |
4552 | of possible tables sizes anymore. Instead we determine for all | |
4553 | possible reasonable sizes of the table the outcome (i.e., the | |
4554 | number of collisions etc) and choose the best solution. The | |
4555 | weighting functions are not too simple to allow the table to grow | |
4556 | without bounds. Instead one of the weighting factors is the size. | |
4557 | Therefore the result is always a good payoff between few collisions | |
4558 | (= short chain lengths) and table size. */ | |
4559 | static size_t | |
4560 | compute_bucket_count (struct bfd_link_info *info) | |
4561 | { | |
4562 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
4563 | size_t best_size = 0; | |
4564 | unsigned long int *hashcodes; | |
4565 | unsigned long int *hashcodesp; | |
4566 | unsigned long int i; | |
4567 | bfd_size_type amt; | |
4568 | ||
4569 | /* Compute the hash values for all exported symbols. At the same | |
4570 | time store the values in an array so that we could use them for | |
4571 | optimizations. */ | |
4572 | amt = dynsymcount; | |
4573 | amt *= sizeof (unsigned long int); | |
4574 | hashcodes = bfd_malloc (amt); | |
4575 | if (hashcodes == NULL) | |
4576 | return 0; | |
4577 | hashcodesp = hashcodes; | |
4578 | ||
4579 | /* Put all hash values in HASHCODES. */ | |
4580 | elf_link_hash_traverse (elf_hash_table (info), | |
4581 | elf_collect_hash_codes, &hashcodesp); | |
4582 | ||
4583 | /* We have a problem here. The following code to optimize the table | |
4584 | size requires an integer type with more the 32 bits. If | |
4585 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
4586 | #ifdef BFD_HOST_U_64_BIT | |
4587 | if (info->optimize) | |
4588 | { | |
4589 | unsigned long int nsyms = hashcodesp - hashcodes; | |
4590 | size_t minsize; | |
4591 | size_t maxsize; | |
4592 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
4593 | unsigned long int *counts ; | |
4594 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
4595 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
4596 | ||
4597 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
4598 | that the hashing table must at least have NSYMS/4 and at most | |
4599 | 2*NSYMS buckets. */ | |
4600 | minsize = nsyms / 4; | |
4601 | if (minsize == 0) | |
4602 | minsize = 1; | |
4603 | best_size = maxsize = nsyms * 2; | |
4604 | ||
4605 | /* Create array where we count the collisions in. We must use bfd_malloc | |
4606 | since the size could be large. */ | |
4607 | amt = maxsize; | |
4608 | amt *= sizeof (unsigned long int); | |
4609 | counts = bfd_malloc (amt); | |
4610 | if (counts == NULL) | |
4611 | { | |
4612 | free (hashcodes); | |
4613 | return 0; | |
4614 | } | |
4615 | ||
4616 | /* Compute the "optimal" size for the hash table. The criteria is a | |
4617 | minimal chain length. The minor criteria is (of course) the size | |
4618 | of the table. */ | |
4619 | for (i = minsize; i < maxsize; ++i) | |
4620 | { | |
4621 | /* Walk through the array of hashcodes and count the collisions. */ | |
4622 | BFD_HOST_U_64_BIT max; | |
4623 | unsigned long int j; | |
4624 | unsigned long int fact; | |
4625 | ||
4626 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
4627 | ||
4628 | /* Determine how often each hash bucket is used. */ | |
4629 | for (j = 0; j < nsyms; ++j) | |
4630 | ++counts[hashcodes[j] % i]; | |
4631 | ||
4632 | /* For the weight function we need some information about the | |
4633 | pagesize on the target. This is information need not be 100% | |
4634 | accurate. Since this information is not available (so far) we | |
4635 | define it here to a reasonable default value. If it is crucial | |
4636 | to have a better value some day simply define this value. */ | |
4637 | # ifndef BFD_TARGET_PAGESIZE | |
4638 | # define BFD_TARGET_PAGESIZE (4096) | |
4639 | # endif | |
4640 | ||
4641 | /* We in any case need 2 + NSYMS entries for the size values and | |
4642 | the chains. */ | |
4643 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
4644 | ||
4645 | # if 1 | |
4646 | /* Variant 1: optimize for short chains. We add the squares | |
4647 | of all the chain lengths (which favors many small chain | |
4648 | over a few long chains). */ | |
4649 | for (j = 0; j < i; ++j) | |
4650 | max += counts[j] * counts[j]; | |
4651 | ||
4652 | /* This adds penalties for the overall size of the table. */ | |
4653 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4654 | max *= fact * fact; | |
4655 | # else | |
4656 | /* Variant 2: Optimize a lot more for small table. Here we | |
4657 | also add squares of the size but we also add penalties for | |
4658 | empty slots (the +1 term). */ | |
4659 | for (j = 0; j < i; ++j) | |
4660 | max += (1 + counts[j]) * (1 + counts[j]); | |
4661 | ||
4662 | /* The overall size of the table is considered, but not as | |
4663 | strong as in variant 1, where it is squared. */ | |
4664 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
4665 | max *= fact; | |
4666 | # endif | |
4667 | ||
4668 | /* Compare with current best results. */ | |
4669 | if (max < best_chlen) | |
4670 | { | |
4671 | best_chlen = max; | |
4672 | best_size = i; | |
4673 | } | |
4674 | } | |
4675 | ||
4676 | free (counts); | |
4677 | } | |
4678 | else | |
4679 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
4680 | { | |
4681 | /* This is the fallback solution if no 64bit type is available or if we | |
4682 | are not supposed to spend much time on optimizations. We select the | |
4683 | bucket count using a fixed set of numbers. */ | |
4684 | for (i = 0; elf_buckets[i] != 0; i++) | |
4685 | { | |
4686 | best_size = elf_buckets[i]; | |
4687 | if (dynsymcount < elf_buckets[i + 1]) | |
4688 | break; | |
4689 | } | |
4690 | } | |
4691 | ||
4692 | /* Free the arrays we needed. */ | |
4693 | free (hashcodes); | |
4694 | ||
4695 | return best_size; | |
4696 | } | |
4697 | ||
4698 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4699 | called by the ELF linker emulation before_allocation routine. We | |
4700 | must set the sizes of the sections before the linker sets the | |
4701 | addresses of the various sections. */ | |
4702 | ||
4703 | bfd_boolean | |
4704 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
4705 | const char *soname, | |
4706 | const char *rpath, | |
4707 | const char *filter_shlib, | |
4708 | const char * const *auxiliary_filters, | |
4709 | struct bfd_link_info *info, | |
4710 | asection **sinterpptr, | |
4711 | struct bfd_elf_version_tree *verdefs) | |
4712 | { | |
4713 | bfd_size_type soname_indx; | |
4714 | bfd *dynobj; | |
4715 | const struct elf_backend_data *bed; | |
4716 | struct elf_assign_sym_version_info asvinfo; | |
4717 | ||
4718 | *sinterpptr = NULL; | |
4719 | ||
4720 | soname_indx = (bfd_size_type) -1; | |
4721 | ||
4722 | if (!is_elf_hash_table (info->hash)) | |
4723 | return TRUE; | |
4724 | ||
8c37241b | 4725 | elf_tdata (output_bfd)->relro = info->relro; |
5a580b3a AM |
4726 | if (info->execstack) |
4727 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
4728 | else if (info->noexecstack) | |
4729 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
4730 | else | |
4731 | { | |
4732 | bfd *inputobj; | |
4733 | asection *notesec = NULL; | |
4734 | int exec = 0; | |
4735 | ||
4736 | for (inputobj = info->input_bfds; | |
4737 | inputobj; | |
4738 | inputobj = inputobj->link_next) | |
4739 | { | |
4740 | asection *s; | |
4741 | ||
4742 | if (inputobj->flags & DYNAMIC) | |
4743 | continue; | |
4744 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
4745 | if (s) | |
4746 | { | |
4747 | if (s->flags & SEC_CODE) | |
4748 | exec = PF_X; | |
4749 | notesec = s; | |
4750 | } | |
4751 | else | |
4752 | exec = PF_X; | |
4753 | } | |
4754 | if (notesec) | |
4755 | { | |
4756 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
4757 | if (exec && info->relocatable | |
4758 | && notesec->output_section != bfd_abs_section_ptr) | |
4759 | notesec->output_section->flags |= SEC_CODE; | |
4760 | } | |
4761 | } | |
4762 | ||
4763 | /* Any syms created from now on start with -1 in | |
4764 | got.refcount/offset and plt.refcount/offset. */ | |
4765 | elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset; | |
4766 | ||
4767 | /* The backend may have to create some sections regardless of whether | |
4768 | we're dynamic or not. */ | |
4769 | bed = get_elf_backend_data (output_bfd); | |
4770 | if (bed->elf_backend_always_size_sections | |
4771 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
4772 | return FALSE; | |
4773 | ||
4774 | dynobj = elf_hash_table (info)->dynobj; | |
4775 | ||
4776 | /* If there were no dynamic objects in the link, there is nothing to | |
4777 | do here. */ | |
4778 | if (dynobj == NULL) | |
4779 | return TRUE; | |
4780 | ||
4781 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
4782 | return FALSE; | |
4783 | ||
4784 | if (elf_hash_table (info)->dynamic_sections_created) | |
4785 | { | |
4786 | struct elf_info_failed eif; | |
4787 | struct elf_link_hash_entry *h; | |
4788 | asection *dynstr; | |
4789 | struct bfd_elf_version_tree *t; | |
4790 | struct bfd_elf_version_expr *d; | |
4791 | bfd_boolean all_defined; | |
4792 | ||
4793 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
4794 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
4795 | ||
4796 | if (soname != NULL) | |
4797 | { | |
4798 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4799 | soname, TRUE); | |
4800 | if (soname_indx == (bfd_size_type) -1 | |
4801 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
4802 | return FALSE; | |
4803 | } | |
4804 | ||
4805 | if (info->symbolic) | |
4806 | { | |
4807 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
4808 | return FALSE; | |
4809 | info->flags |= DF_SYMBOLIC; | |
4810 | } | |
4811 | ||
4812 | if (rpath != NULL) | |
4813 | { | |
4814 | bfd_size_type indx; | |
4815 | ||
4816 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
4817 | TRUE); | |
4818 | if (indx == (bfd_size_type) -1 | |
4819 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
4820 | return FALSE; | |
4821 | ||
4822 | if (info->new_dtags) | |
4823 | { | |
4824 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
4825 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
4826 | return FALSE; | |
4827 | } | |
4828 | } | |
4829 | ||
4830 | if (filter_shlib != NULL) | |
4831 | { | |
4832 | bfd_size_type indx; | |
4833 | ||
4834 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4835 | filter_shlib, TRUE); | |
4836 | if (indx == (bfd_size_type) -1 | |
4837 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
4838 | return FALSE; | |
4839 | } | |
4840 | ||
4841 | if (auxiliary_filters != NULL) | |
4842 | { | |
4843 | const char * const *p; | |
4844 | ||
4845 | for (p = auxiliary_filters; *p != NULL; p++) | |
4846 | { | |
4847 | bfd_size_type indx; | |
4848 | ||
4849 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
4850 | *p, TRUE); | |
4851 | if (indx == (bfd_size_type) -1 | |
4852 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
4853 | return FALSE; | |
4854 | } | |
4855 | } | |
4856 | ||
4857 | eif.info = info; | |
4858 | eif.verdefs = verdefs; | |
4859 | eif.failed = FALSE; | |
4860 | ||
4861 | /* If we are supposed to export all symbols into the dynamic symbol | |
4862 | table (this is not the normal case), then do so. */ | |
4863 | if (info->export_dynamic) | |
4864 | { | |
4865 | elf_link_hash_traverse (elf_hash_table (info), | |
4866 | _bfd_elf_export_symbol, | |
4867 | &eif); | |
4868 | if (eif.failed) | |
4869 | return FALSE; | |
4870 | } | |
4871 | ||
4872 | /* Make all global versions with definition. */ | |
4873 | for (t = verdefs; t != NULL; t = t->next) | |
4874 | for (d = t->globals.list; d != NULL; d = d->next) | |
4875 | if (!d->symver && d->symbol) | |
4876 | { | |
4877 | const char *verstr, *name; | |
4878 | size_t namelen, verlen, newlen; | |
4879 | char *newname, *p; | |
4880 | struct elf_link_hash_entry *newh; | |
4881 | ||
4882 | name = d->symbol; | |
4883 | namelen = strlen (name); | |
4884 | verstr = t->name; | |
4885 | verlen = strlen (verstr); | |
4886 | newlen = namelen + verlen + 3; | |
4887 | ||
4888 | newname = bfd_malloc (newlen); | |
4889 | if (newname == NULL) | |
4890 | return FALSE; | |
4891 | memcpy (newname, name, namelen); | |
4892 | ||
4893 | /* Check the hidden versioned definition. */ | |
4894 | p = newname + namelen; | |
4895 | *p++ = ELF_VER_CHR; | |
4896 | memcpy (p, verstr, verlen + 1); | |
4897 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4898 | newname, FALSE, FALSE, | |
4899 | FALSE); | |
4900 | if (newh == NULL | |
4901 | || (newh->root.type != bfd_link_hash_defined | |
4902 | && newh->root.type != bfd_link_hash_defweak)) | |
4903 | { | |
4904 | /* Check the default versioned definition. */ | |
4905 | *p++ = ELF_VER_CHR; | |
4906 | memcpy (p, verstr, verlen + 1); | |
4907 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
4908 | newname, FALSE, FALSE, | |
4909 | FALSE); | |
4910 | } | |
4911 | free (newname); | |
4912 | ||
4913 | /* Mark this version if there is a definition and it is | |
4914 | not defined in a shared object. */ | |
4915 | if (newh != NULL | |
f5385ebf | 4916 | && !newh->def_dynamic |
5a580b3a AM |
4917 | && (newh->root.type == bfd_link_hash_defined |
4918 | || newh->root.type == bfd_link_hash_defweak)) | |
4919 | d->symver = 1; | |
4920 | } | |
4921 | ||
4922 | /* Attach all the symbols to their version information. */ | |
4923 | asvinfo.output_bfd = output_bfd; | |
4924 | asvinfo.info = info; | |
4925 | asvinfo.verdefs = verdefs; | |
4926 | asvinfo.failed = FALSE; | |
4927 | ||
4928 | elf_link_hash_traverse (elf_hash_table (info), | |
4929 | _bfd_elf_link_assign_sym_version, | |
4930 | &asvinfo); | |
4931 | if (asvinfo.failed) | |
4932 | return FALSE; | |
4933 | ||
4934 | if (!info->allow_undefined_version) | |
4935 | { | |
4936 | /* Check if all global versions have a definition. */ | |
4937 | all_defined = TRUE; | |
4938 | for (t = verdefs; t != NULL; t = t->next) | |
4939 | for (d = t->globals.list; d != NULL; d = d->next) | |
4940 | if (!d->symver && !d->script) | |
4941 | { | |
4942 | (*_bfd_error_handler) | |
4943 | (_("%s: undefined version: %s"), | |
4944 | d->pattern, t->name); | |
4945 | all_defined = FALSE; | |
4946 | } | |
4947 | ||
4948 | if (!all_defined) | |
4949 | { | |
4950 | bfd_set_error (bfd_error_bad_value); | |
4951 | return FALSE; | |
4952 | } | |
4953 | } | |
4954 | ||
4955 | /* Find all symbols which were defined in a dynamic object and make | |
4956 | the backend pick a reasonable value for them. */ | |
4957 | elf_link_hash_traverse (elf_hash_table (info), | |
4958 | _bfd_elf_adjust_dynamic_symbol, | |
4959 | &eif); | |
4960 | if (eif.failed) | |
4961 | return FALSE; | |
4962 | ||
4963 | /* Add some entries to the .dynamic section. We fill in some of the | |
ee75fd95 | 4964 | values later, in bfd_elf_final_link, but we must add the entries |
5a580b3a AM |
4965 | now so that we know the final size of the .dynamic section. */ |
4966 | ||
4967 | /* If there are initialization and/or finalization functions to | |
4968 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
4969 | h = (info->init_function | |
4970 | ? elf_link_hash_lookup (elf_hash_table (info), | |
4971 | info->init_function, FALSE, | |
4972 | FALSE, FALSE) | |
4973 | : NULL); | |
4974 | if (h != NULL | |
f5385ebf AM |
4975 | && (h->ref_regular |
4976 | || h->def_regular)) | |
5a580b3a AM |
4977 | { |
4978 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
4979 | return FALSE; | |
4980 | } | |
4981 | h = (info->fini_function | |
4982 | ? elf_link_hash_lookup (elf_hash_table (info), | |
4983 | info->fini_function, FALSE, | |
4984 | FALSE, FALSE) | |
4985 | : NULL); | |
4986 | if (h != NULL | |
f5385ebf AM |
4987 | && (h->ref_regular |
4988 | || h->def_regular)) | |
5a580b3a AM |
4989 | { |
4990 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
4991 | return FALSE; | |
4992 | } | |
4993 | ||
4994 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) | |
4995 | { | |
4996 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
4997 | if (! info->executable) | |
4998 | { | |
4999 | bfd *sub; | |
5000 | asection *o; | |
5001 | ||
5002 | for (sub = info->input_bfds; sub != NULL; | |
5003 | sub = sub->link_next) | |
5004 | for (o = sub->sections; o != NULL; o = o->next) | |
5005 | if (elf_section_data (o)->this_hdr.sh_type | |
5006 | == SHT_PREINIT_ARRAY) | |
5007 | { | |
5008 | (*_bfd_error_handler) | |
d003868e AM |
5009 | (_("%B: .preinit_array section is not allowed in DSO"), |
5010 | sub); | |
5a580b3a AM |
5011 | break; |
5012 | } | |
5013 | ||
5014 | bfd_set_error (bfd_error_nonrepresentable_section); | |
5015 | return FALSE; | |
5016 | } | |
5017 | ||
5018 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
5019 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
5020 | return FALSE; | |
5021 | } | |
5022 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
5023 | { | |
5024 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
5025 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
5026 | return FALSE; | |
5027 | } | |
5028 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
5029 | { | |
5030 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
5031 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
5032 | return FALSE; | |
5033 | } | |
5034 | ||
5035 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5036 | /* If .dynstr is excluded from the link, we don't want any of | |
5037 | these tags. Strictly, we should be checking each section | |
5038 | individually; This quick check covers for the case where | |
5039 | someone does a /DISCARD/ : { *(*) }. */ | |
5040 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
5041 | { | |
5042 | bfd_size_type strsize; | |
5043 | ||
5044 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
5045 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
5046 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
5047 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
5048 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
5049 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
5050 | bed->s->sizeof_sym)) | |
5051 | return FALSE; | |
5052 | } | |
5053 | } | |
5054 | ||
5055 | /* The backend must work out the sizes of all the other dynamic | |
5056 | sections. */ | |
5057 | if (bed->elf_backend_size_dynamic_sections | |
5058 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
5059 | return FALSE; | |
5060 | ||
5061 | if (elf_hash_table (info)->dynamic_sections_created) | |
5062 | { | |
5063 | bfd_size_type dynsymcount; | |
5064 | asection *s; | |
5065 | size_t bucketcount = 0; | |
5066 | size_t hash_entry_size; | |
5067 | unsigned int dtagcount; | |
5068 | ||
5069 | /* Set up the version definition section. */ | |
5070 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
5071 | BFD_ASSERT (s != NULL); | |
5072 | ||
5073 | /* We may have created additional version definitions if we are | |
5074 | just linking a regular application. */ | |
5075 | verdefs = asvinfo.verdefs; | |
5076 | ||
5077 | /* Skip anonymous version tag. */ | |
5078 | if (verdefs != NULL && verdefs->vernum == 0) | |
5079 | verdefs = verdefs->next; | |
5080 | ||
5081 | if (verdefs == NULL) | |
5082 | _bfd_strip_section_from_output (info, s); | |
5083 | else | |
5084 | { | |
5085 | unsigned int cdefs; | |
5086 | bfd_size_type size; | |
5087 | struct bfd_elf_version_tree *t; | |
5088 | bfd_byte *p; | |
5089 | Elf_Internal_Verdef def; | |
5090 | Elf_Internal_Verdaux defaux; | |
5091 | ||
5092 | cdefs = 0; | |
5093 | size = 0; | |
5094 | ||
5095 | /* Make space for the base version. */ | |
5096 | size += sizeof (Elf_External_Verdef); | |
5097 | size += sizeof (Elf_External_Verdaux); | |
5098 | ++cdefs; | |
5099 | ||
5100 | for (t = verdefs; t != NULL; t = t->next) | |
5101 | { | |
5102 | struct bfd_elf_version_deps *n; | |
5103 | ||
5104 | size += sizeof (Elf_External_Verdef); | |
5105 | size += sizeof (Elf_External_Verdaux); | |
5106 | ++cdefs; | |
5107 | ||
5108 | for (n = t->deps; n != NULL; n = n->next) | |
5109 | size += sizeof (Elf_External_Verdaux); | |
5110 | } | |
5111 | ||
eea6121a AM |
5112 | s->size = size; |
5113 | s->contents = bfd_alloc (output_bfd, s->size); | |
5114 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5115 | return FALSE; |
5116 | ||
5117 | /* Fill in the version definition section. */ | |
5118 | ||
5119 | p = s->contents; | |
5120 | ||
5121 | def.vd_version = VER_DEF_CURRENT; | |
5122 | def.vd_flags = VER_FLG_BASE; | |
5123 | def.vd_ndx = 1; | |
5124 | def.vd_cnt = 1; | |
5125 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5126 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5127 | + sizeof (Elf_External_Verdaux)); | |
5128 | ||
5129 | if (soname_indx != (bfd_size_type) -1) | |
5130 | { | |
5131 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5132 | soname_indx); | |
5133 | def.vd_hash = bfd_elf_hash (soname); | |
5134 | defaux.vda_name = soname_indx; | |
5135 | } | |
5136 | else | |
5137 | { | |
5138 | const char *name; | |
5139 | bfd_size_type indx; | |
5140 | ||
5141 | name = basename (output_bfd->filename); | |
5142 | def.vd_hash = bfd_elf_hash (name); | |
5143 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5144 | name, FALSE); | |
5145 | if (indx == (bfd_size_type) -1) | |
5146 | return FALSE; | |
5147 | defaux.vda_name = indx; | |
5148 | } | |
5149 | defaux.vda_next = 0; | |
5150 | ||
5151 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5152 | (Elf_External_Verdef *) p); | |
5153 | p += sizeof (Elf_External_Verdef); | |
5154 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5155 | (Elf_External_Verdaux *) p); | |
5156 | p += sizeof (Elf_External_Verdaux); | |
5157 | ||
5158 | for (t = verdefs; t != NULL; t = t->next) | |
5159 | { | |
5160 | unsigned int cdeps; | |
5161 | struct bfd_elf_version_deps *n; | |
5162 | struct elf_link_hash_entry *h; | |
5163 | struct bfd_link_hash_entry *bh; | |
5164 | ||
5165 | cdeps = 0; | |
5166 | for (n = t->deps; n != NULL; n = n->next) | |
5167 | ++cdeps; | |
5168 | ||
5169 | /* Add a symbol representing this version. */ | |
5170 | bh = NULL; | |
5171 | if (! (_bfd_generic_link_add_one_symbol | |
5172 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
5173 | 0, NULL, FALSE, | |
5174 | get_elf_backend_data (dynobj)->collect, &bh))) | |
5175 | return FALSE; | |
5176 | h = (struct elf_link_hash_entry *) bh; | |
f5385ebf AM |
5177 | h->non_elf = 0; |
5178 | h->def_regular = 1; | |
5a580b3a AM |
5179 | h->type = STT_OBJECT; |
5180 | h->verinfo.vertree = t; | |
5181 | ||
c152c796 | 5182 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
5a580b3a AM |
5183 | return FALSE; |
5184 | ||
5185 | def.vd_version = VER_DEF_CURRENT; | |
5186 | def.vd_flags = 0; | |
5187 | if (t->globals.list == NULL | |
5188 | && t->locals.list == NULL | |
5189 | && ! t->used) | |
5190 | def.vd_flags |= VER_FLG_WEAK; | |
5191 | def.vd_ndx = t->vernum + 1; | |
5192 | def.vd_cnt = cdeps + 1; | |
5193 | def.vd_hash = bfd_elf_hash (t->name); | |
5194 | def.vd_aux = sizeof (Elf_External_Verdef); | |
5195 | def.vd_next = 0; | |
5196 | if (t->next != NULL) | |
5197 | def.vd_next = (sizeof (Elf_External_Verdef) | |
5198 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
5199 | ||
5200 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
5201 | (Elf_External_Verdef *) p); | |
5202 | p += sizeof (Elf_External_Verdef); | |
5203 | ||
5204 | defaux.vda_name = h->dynstr_index; | |
5205 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5206 | h->dynstr_index); | |
5207 | defaux.vda_next = 0; | |
5208 | if (t->deps != NULL) | |
5209 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5210 | t->name_indx = defaux.vda_name; | |
5211 | ||
5212 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5213 | (Elf_External_Verdaux *) p); | |
5214 | p += sizeof (Elf_External_Verdaux); | |
5215 | ||
5216 | for (n = t->deps; n != NULL; n = n->next) | |
5217 | { | |
5218 | if (n->version_needed == NULL) | |
5219 | { | |
5220 | /* This can happen if there was an error in the | |
5221 | version script. */ | |
5222 | defaux.vda_name = 0; | |
5223 | } | |
5224 | else | |
5225 | { | |
5226 | defaux.vda_name = n->version_needed->name_indx; | |
5227 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
5228 | defaux.vda_name); | |
5229 | } | |
5230 | if (n->next == NULL) | |
5231 | defaux.vda_next = 0; | |
5232 | else | |
5233 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
5234 | ||
5235 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
5236 | (Elf_External_Verdaux *) p); | |
5237 | p += sizeof (Elf_External_Verdaux); | |
5238 | } | |
5239 | } | |
5240 | ||
5241 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
5242 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
5243 | return FALSE; | |
5244 | ||
5245 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
5246 | } | |
5247 | ||
5248 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
5249 | { | |
5250 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
5251 | return FALSE; | |
5252 | } | |
5253 | else if (info->flags & DF_BIND_NOW) | |
5254 | { | |
5255 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
5256 | return FALSE; | |
5257 | } | |
5258 | ||
5259 | if (info->flags_1) | |
5260 | { | |
5261 | if (info->executable) | |
5262 | info->flags_1 &= ~ (DF_1_INITFIRST | |
5263 | | DF_1_NODELETE | |
5264 | | DF_1_NOOPEN); | |
5265 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
5266 | return FALSE; | |
5267 | } | |
5268 | ||
5269 | /* Work out the size of the version reference section. */ | |
5270 | ||
5271 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
5272 | BFD_ASSERT (s != NULL); | |
5273 | { | |
5274 | struct elf_find_verdep_info sinfo; | |
5275 | ||
5276 | sinfo.output_bfd = output_bfd; | |
5277 | sinfo.info = info; | |
5278 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
5279 | if (sinfo.vers == 0) | |
5280 | sinfo.vers = 1; | |
5281 | sinfo.failed = FALSE; | |
5282 | ||
5283 | elf_link_hash_traverse (elf_hash_table (info), | |
5284 | _bfd_elf_link_find_version_dependencies, | |
5285 | &sinfo); | |
5286 | ||
5287 | if (elf_tdata (output_bfd)->verref == NULL) | |
5288 | _bfd_strip_section_from_output (info, s); | |
5289 | else | |
5290 | { | |
5291 | Elf_Internal_Verneed *t; | |
5292 | unsigned int size; | |
5293 | unsigned int crefs; | |
5294 | bfd_byte *p; | |
5295 | ||
5296 | /* Build the version definition section. */ | |
5297 | size = 0; | |
5298 | crefs = 0; | |
5299 | for (t = elf_tdata (output_bfd)->verref; | |
5300 | t != NULL; | |
5301 | t = t->vn_nextref) | |
5302 | { | |
5303 | Elf_Internal_Vernaux *a; | |
5304 | ||
5305 | size += sizeof (Elf_External_Verneed); | |
5306 | ++crefs; | |
5307 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5308 | size += sizeof (Elf_External_Vernaux); | |
5309 | } | |
5310 | ||
eea6121a AM |
5311 | s->size = size; |
5312 | s->contents = bfd_alloc (output_bfd, s->size); | |
5a580b3a AM |
5313 | if (s->contents == NULL) |
5314 | return FALSE; | |
5315 | ||
5316 | p = s->contents; | |
5317 | for (t = elf_tdata (output_bfd)->verref; | |
5318 | t != NULL; | |
5319 | t = t->vn_nextref) | |
5320 | { | |
5321 | unsigned int caux; | |
5322 | Elf_Internal_Vernaux *a; | |
5323 | bfd_size_type indx; | |
5324 | ||
5325 | caux = 0; | |
5326 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5327 | ++caux; | |
5328 | ||
5329 | t->vn_version = VER_NEED_CURRENT; | |
5330 | t->vn_cnt = caux; | |
5331 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5332 | elf_dt_name (t->vn_bfd) != NULL | |
5333 | ? elf_dt_name (t->vn_bfd) | |
5334 | : basename (t->vn_bfd->filename), | |
5335 | FALSE); | |
5336 | if (indx == (bfd_size_type) -1) | |
5337 | return FALSE; | |
5338 | t->vn_file = indx; | |
5339 | t->vn_aux = sizeof (Elf_External_Verneed); | |
5340 | if (t->vn_nextref == NULL) | |
5341 | t->vn_next = 0; | |
5342 | else | |
5343 | t->vn_next = (sizeof (Elf_External_Verneed) | |
5344 | + caux * sizeof (Elf_External_Vernaux)); | |
5345 | ||
5346 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
5347 | (Elf_External_Verneed *) p); | |
5348 | p += sizeof (Elf_External_Verneed); | |
5349 | ||
5350 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
5351 | { | |
5352 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
5353 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
5354 | a->vna_nodename, FALSE); | |
5355 | if (indx == (bfd_size_type) -1) | |
5356 | return FALSE; | |
5357 | a->vna_name = indx; | |
5358 | if (a->vna_nextptr == NULL) | |
5359 | a->vna_next = 0; | |
5360 | else | |
5361 | a->vna_next = sizeof (Elf_External_Vernaux); | |
5362 | ||
5363 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
5364 | (Elf_External_Vernaux *) p); | |
5365 | p += sizeof (Elf_External_Vernaux); | |
5366 | } | |
5367 | } | |
5368 | ||
5369 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
5370 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
5371 | return FALSE; | |
5372 | ||
5373 | elf_tdata (output_bfd)->cverrefs = crefs; | |
5374 | } | |
5375 | } | |
5376 | ||
5377 | /* Assign dynsym indicies. In a shared library we generate a | |
5378 | section symbol for each output section, which come first. | |
5379 | Next come all of the back-end allocated local dynamic syms, | |
5380 | followed by the rest of the global symbols. */ | |
5381 | ||
5382 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5383 | ||
5384 | /* Work out the size of the symbol version section. */ | |
5385 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
5386 | BFD_ASSERT (s != NULL); | |
5387 | if (dynsymcount == 0 | |
5388 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) | |
5389 | { | |
5390 | _bfd_strip_section_from_output (info, s); | |
5391 | /* The DYNSYMCOUNT might have changed if we were going to | |
5392 | output a dynamic symbol table entry for S. */ | |
5393 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
5394 | } | |
5395 | else | |
5396 | { | |
eea6121a AM |
5397 | s->size = dynsymcount * sizeof (Elf_External_Versym); |
5398 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5399 | if (s->contents == NULL) |
5400 | return FALSE; | |
5401 | ||
5402 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
5403 | return FALSE; | |
5404 | } | |
5405 | ||
5406 | /* Set the size of the .dynsym and .hash sections. We counted | |
5407 | the number of dynamic symbols in elf_link_add_object_symbols. | |
5408 | We will build the contents of .dynsym and .hash when we build | |
5409 | the final symbol table, because until then we do not know the | |
5410 | correct value to give the symbols. We built the .dynstr | |
5411 | section as we went along in elf_link_add_object_symbols. */ | |
5412 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5413 | BFD_ASSERT (s != NULL); | |
eea6121a AM |
5414 | s->size = dynsymcount * bed->s->sizeof_sym; |
5415 | s->contents = bfd_alloc (output_bfd, s->size); | |
5416 | if (s->contents == NULL && s->size != 0) | |
5a580b3a AM |
5417 | return FALSE; |
5418 | ||
5419 | if (dynsymcount != 0) | |
5420 | { | |
5421 | Elf_Internal_Sym isym; | |
5422 | ||
5423 | /* The first entry in .dynsym is a dummy symbol. */ | |
5424 | isym.st_value = 0; | |
5425 | isym.st_size = 0; | |
5426 | isym.st_name = 0; | |
5427 | isym.st_info = 0; | |
5428 | isym.st_other = 0; | |
5429 | isym.st_shndx = 0; | |
5430 | bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0); | |
5431 | } | |
5432 | ||
5433 | /* Compute the size of the hashing table. As a side effect this | |
5434 | computes the hash values for all the names we export. */ | |
5435 | bucketcount = compute_bucket_count (info); | |
5436 | ||
5437 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
5438 | BFD_ASSERT (s != NULL); | |
5439 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
eea6121a AM |
5440 | s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); |
5441 | s->contents = bfd_zalloc (output_bfd, s->size); | |
5a580b3a AM |
5442 | if (s->contents == NULL) |
5443 | return FALSE; | |
5444 | ||
5445 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
5446 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
5447 | s->contents + hash_entry_size); | |
5448 | ||
5449 | elf_hash_table (info)->bucketcount = bucketcount; | |
5450 | ||
5451 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
5452 | BFD_ASSERT (s != NULL); | |
5453 | ||
4ad4eba5 | 5454 | elf_finalize_dynstr (output_bfd, info); |
5a580b3a | 5455 | |
eea6121a | 5456 | s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); |
5a580b3a AM |
5457 | |
5458 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
5459 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
5460 | return FALSE; | |
5461 | } | |
5462 | ||
5463 | return TRUE; | |
5464 | } | |
c152c796 AM |
5465 | |
5466 | /* Final phase of ELF linker. */ | |
5467 | ||
5468 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5469 | ||
5470 | struct elf_final_link_info | |
5471 | { | |
5472 | /* General link information. */ | |
5473 | struct bfd_link_info *info; | |
5474 | /* Output BFD. */ | |
5475 | bfd *output_bfd; | |
5476 | /* Symbol string table. */ | |
5477 | struct bfd_strtab_hash *symstrtab; | |
5478 | /* .dynsym section. */ | |
5479 | asection *dynsym_sec; | |
5480 | /* .hash section. */ | |
5481 | asection *hash_sec; | |
5482 | /* symbol version section (.gnu.version). */ | |
5483 | asection *symver_sec; | |
5484 | /* Buffer large enough to hold contents of any section. */ | |
5485 | bfd_byte *contents; | |
5486 | /* Buffer large enough to hold external relocs of any section. */ | |
5487 | void *external_relocs; | |
5488 | /* Buffer large enough to hold internal relocs of any section. */ | |
5489 | Elf_Internal_Rela *internal_relocs; | |
5490 | /* Buffer large enough to hold external local symbols of any input | |
5491 | BFD. */ | |
5492 | bfd_byte *external_syms; | |
5493 | /* And a buffer for symbol section indices. */ | |
5494 | Elf_External_Sym_Shndx *locsym_shndx; | |
5495 | /* Buffer large enough to hold internal local symbols of any input | |
5496 | BFD. */ | |
5497 | Elf_Internal_Sym *internal_syms; | |
5498 | /* Array large enough to hold a symbol index for each local symbol | |
5499 | of any input BFD. */ | |
5500 | long *indices; | |
5501 | /* Array large enough to hold a section pointer for each local | |
5502 | symbol of any input BFD. */ | |
5503 | asection **sections; | |
5504 | /* Buffer to hold swapped out symbols. */ | |
5505 | bfd_byte *symbuf; | |
5506 | /* And one for symbol section indices. */ | |
5507 | Elf_External_Sym_Shndx *symshndxbuf; | |
5508 | /* Number of swapped out symbols in buffer. */ | |
5509 | size_t symbuf_count; | |
5510 | /* Number of symbols which fit in symbuf. */ | |
5511 | size_t symbuf_size; | |
5512 | /* And same for symshndxbuf. */ | |
5513 | size_t shndxbuf_size; | |
5514 | }; | |
5515 | ||
5516 | /* This struct is used to pass information to elf_link_output_extsym. */ | |
5517 | ||
5518 | struct elf_outext_info | |
5519 | { | |
5520 | bfd_boolean failed; | |
5521 | bfd_boolean localsyms; | |
5522 | struct elf_final_link_info *finfo; | |
5523 | }; | |
5524 | ||
5525 | /* When performing a relocatable link, the input relocations are | |
5526 | preserved. But, if they reference global symbols, the indices | |
5527 | referenced must be updated. Update all the relocations in | |
5528 | REL_HDR (there are COUNT of them), using the data in REL_HASH. */ | |
5529 | ||
5530 | static void | |
5531 | elf_link_adjust_relocs (bfd *abfd, | |
5532 | Elf_Internal_Shdr *rel_hdr, | |
5533 | unsigned int count, | |
5534 | struct elf_link_hash_entry **rel_hash) | |
5535 | { | |
5536 | unsigned int i; | |
5537 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5538 | bfd_byte *erela; | |
5539 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5540 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5541 | bfd_vma r_type_mask; | |
5542 | int r_sym_shift; | |
5543 | ||
5544 | if (rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
5545 | { | |
5546 | swap_in = bed->s->swap_reloc_in; | |
5547 | swap_out = bed->s->swap_reloc_out; | |
5548 | } | |
5549 | else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
5550 | { | |
5551 | swap_in = bed->s->swap_reloca_in; | |
5552 | swap_out = bed->s->swap_reloca_out; | |
5553 | } | |
5554 | else | |
5555 | abort (); | |
5556 | ||
5557 | if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) | |
5558 | abort (); | |
5559 | ||
5560 | if (bed->s->arch_size == 32) | |
5561 | { | |
5562 | r_type_mask = 0xff; | |
5563 | r_sym_shift = 8; | |
5564 | } | |
5565 | else | |
5566 | { | |
5567 | r_type_mask = 0xffffffff; | |
5568 | r_sym_shift = 32; | |
5569 | } | |
5570 | ||
5571 | erela = rel_hdr->contents; | |
5572 | for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) | |
5573 | { | |
5574 | Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; | |
5575 | unsigned int j; | |
5576 | ||
5577 | if (*rel_hash == NULL) | |
5578 | continue; | |
5579 | ||
5580 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5581 | ||
5582 | (*swap_in) (abfd, erela, irela); | |
5583 | for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) | |
5584 | irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift | |
5585 | | (irela[j].r_info & r_type_mask)); | |
5586 | (*swap_out) (abfd, irela, erela); | |
5587 | } | |
5588 | } | |
5589 | ||
5590 | struct elf_link_sort_rela | |
5591 | { | |
5592 | union { | |
5593 | bfd_vma offset; | |
5594 | bfd_vma sym_mask; | |
5595 | } u; | |
5596 | enum elf_reloc_type_class type; | |
5597 | /* We use this as an array of size int_rels_per_ext_rel. */ | |
5598 | Elf_Internal_Rela rela[1]; | |
5599 | }; | |
5600 | ||
5601 | static int | |
5602 | elf_link_sort_cmp1 (const void *A, const void *B) | |
5603 | { | |
5604 | const struct elf_link_sort_rela *a = A; | |
5605 | const struct elf_link_sort_rela *b = B; | |
5606 | int relativea, relativeb; | |
5607 | ||
5608 | relativea = a->type == reloc_class_relative; | |
5609 | relativeb = b->type == reloc_class_relative; | |
5610 | ||
5611 | if (relativea < relativeb) | |
5612 | return 1; | |
5613 | if (relativea > relativeb) | |
5614 | return -1; | |
5615 | if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) | |
5616 | return -1; | |
5617 | if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) | |
5618 | return 1; | |
5619 | if (a->rela->r_offset < b->rela->r_offset) | |
5620 | return -1; | |
5621 | if (a->rela->r_offset > b->rela->r_offset) | |
5622 | return 1; | |
5623 | return 0; | |
5624 | } | |
5625 | ||
5626 | static int | |
5627 | elf_link_sort_cmp2 (const void *A, const void *B) | |
5628 | { | |
5629 | const struct elf_link_sort_rela *a = A; | |
5630 | const struct elf_link_sort_rela *b = B; | |
5631 | int copya, copyb; | |
5632 | ||
5633 | if (a->u.offset < b->u.offset) | |
5634 | return -1; | |
5635 | if (a->u.offset > b->u.offset) | |
5636 | return 1; | |
5637 | copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); | |
5638 | copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); | |
5639 | if (copya < copyb) | |
5640 | return -1; | |
5641 | if (copya > copyb) | |
5642 | return 1; | |
5643 | if (a->rela->r_offset < b->rela->r_offset) | |
5644 | return -1; | |
5645 | if (a->rela->r_offset > b->rela->r_offset) | |
5646 | return 1; | |
5647 | return 0; | |
5648 | } | |
5649 | ||
5650 | static size_t | |
5651 | elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) | |
5652 | { | |
5653 | asection *reldyn; | |
5654 | bfd_size_type count, size; | |
5655 | size_t i, ret, sort_elt, ext_size; | |
5656 | bfd_byte *sort, *s_non_relative, *p; | |
5657 | struct elf_link_sort_rela *sq; | |
5658 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5659 | int i2e = bed->s->int_rels_per_ext_rel; | |
5660 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); | |
5661 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); | |
5662 | struct bfd_link_order *lo; | |
5663 | bfd_vma r_sym_mask; | |
5664 | ||
5665 | reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); | |
eea6121a | 5666 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5667 | { |
5668 | reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); | |
eea6121a | 5669 | if (reldyn == NULL || reldyn->size == 0) |
c152c796 AM |
5670 | return 0; |
5671 | ext_size = bed->s->sizeof_rel; | |
5672 | swap_in = bed->s->swap_reloc_in; | |
5673 | swap_out = bed->s->swap_reloc_out; | |
5674 | } | |
5675 | else | |
5676 | { | |
5677 | ext_size = bed->s->sizeof_rela; | |
5678 | swap_in = bed->s->swap_reloca_in; | |
5679 | swap_out = bed->s->swap_reloca_out; | |
5680 | } | |
eea6121a | 5681 | count = reldyn->size / ext_size; |
c152c796 AM |
5682 | |
5683 | size = 0; | |
5684 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5685 | if (lo->type == bfd_indirect_link_order) | |
5686 | { | |
5687 | asection *o = lo->u.indirect.section; | |
eea6121a | 5688 | size += o->size; |
c152c796 AM |
5689 | } |
5690 | ||
eea6121a | 5691 | if (size != reldyn->size) |
c152c796 AM |
5692 | return 0; |
5693 | ||
5694 | sort_elt = (sizeof (struct elf_link_sort_rela) | |
5695 | + (i2e - 1) * sizeof (Elf_Internal_Rela)); | |
5696 | sort = bfd_zmalloc (sort_elt * count); | |
5697 | if (sort == NULL) | |
5698 | { | |
5699 | (*info->callbacks->warning) | |
5700 | (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); | |
5701 | return 0; | |
5702 | } | |
5703 | ||
5704 | if (bed->s->arch_size == 32) | |
5705 | r_sym_mask = ~(bfd_vma) 0xff; | |
5706 | else | |
5707 | r_sym_mask = ~(bfd_vma) 0xffffffff; | |
5708 | ||
5709 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5710 | if (lo->type == bfd_indirect_link_order) | |
5711 | { | |
5712 | bfd_byte *erel, *erelend; | |
5713 | asection *o = lo->u.indirect.section; | |
5714 | ||
5715 | erel = o->contents; | |
eea6121a | 5716 | erelend = o->contents + o->size; |
c152c796 AM |
5717 | p = sort + o->output_offset / ext_size * sort_elt; |
5718 | while (erel < erelend) | |
5719 | { | |
5720 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5721 | (*swap_in) (abfd, erel, s->rela); | |
5722 | s->type = (*bed->elf_backend_reloc_type_class) (s->rela); | |
5723 | s->u.sym_mask = r_sym_mask; | |
5724 | p += sort_elt; | |
5725 | erel += ext_size; | |
5726 | } | |
5727 | } | |
5728 | ||
5729 | qsort (sort, count, sort_elt, elf_link_sort_cmp1); | |
5730 | ||
5731 | for (i = 0, p = sort; i < count; i++, p += sort_elt) | |
5732 | { | |
5733 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5734 | if (s->type != reloc_class_relative) | |
5735 | break; | |
5736 | } | |
5737 | ret = i; | |
5738 | s_non_relative = p; | |
5739 | ||
5740 | sq = (struct elf_link_sort_rela *) s_non_relative; | |
5741 | for (; i < count; i++, p += sort_elt) | |
5742 | { | |
5743 | struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; | |
5744 | if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) | |
5745 | sq = sp; | |
5746 | sp->u.offset = sq->rela->r_offset; | |
5747 | } | |
5748 | ||
5749 | qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); | |
5750 | ||
5751 | for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next) | |
5752 | if (lo->type == bfd_indirect_link_order) | |
5753 | { | |
5754 | bfd_byte *erel, *erelend; | |
5755 | asection *o = lo->u.indirect.section; | |
5756 | ||
5757 | erel = o->contents; | |
eea6121a | 5758 | erelend = o->contents + o->size; |
c152c796 AM |
5759 | p = sort + o->output_offset / ext_size * sort_elt; |
5760 | while (erel < erelend) | |
5761 | { | |
5762 | struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; | |
5763 | (*swap_out) (abfd, s->rela, erel); | |
5764 | p += sort_elt; | |
5765 | erel += ext_size; | |
5766 | } | |
5767 | } | |
5768 | ||
5769 | free (sort); | |
5770 | *psec = reldyn; | |
5771 | return ret; | |
5772 | } | |
5773 | ||
5774 | /* Flush the output symbols to the file. */ | |
5775 | ||
5776 | static bfd_boolean | |
5777 | elf_link_flush_output_syms (struct elf_final_link_info *finfo, | |
5778 | const struct elf_backend_data *bed) | |
5779 | { | |
5780 | if (finfo->symbuf_count > 0) | |
5781 | { | |
5782 | Elf_Internal_Shdr *hdr; | |
5783 | file_ptr pos; | |
5784 | bfd_size_type amt; | |
5785 | ||
5786 | hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
5787 | pos = hdr->sh_offset + hdr->sh_size; | |
5788 | amt = finfo->symbuf_count * bed->s->sizeof_sym; | |
5789 | if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 | |
5790 | || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) | |
5791 | return FALSE; | |
5792 | ||
5793 | hdr->sh_size += amt; | |
5794 | finfo->symbuf_count = 0; | |
5795 | } | |
5796 | ||
5797 | return TRUE; | |
5798 | } | |
5799 | ||
5800 | /* Add a symbol to the output symbol table. */ | |
5801 | ||
5802 | static bfd_boolean | |
5803 | elf_link_output_sym (struct elf_final_link_info *finfo, | |
5804 | const char *name, | |
5805 | Elf_Internal_Sym *elfsym, | |
5806 | asection *input_sec, | |
5807 | struct elf_link_hash_entry *h) | |
5808 | { | |
5809 | bfd_byte *dest; | |
5810 | Elf_External_Sym_Shndx *destshndx; | |
5811 | bfd_boolean (*output_symbol_hook) | |
5812 | (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, | |
5813 | struct elf_link_hash_entry *); | |
5814 | const struct elf_backend_data *bed; | |
5815 | ||
5816 | bed = get_elf_backend_data (finfo->output_bfd); | |
5817 | output_symbol_hook = bed->elf_backend_link_output_symbol_hook; | |
5818 | if (output_symbol_hook != NULL) | |
5819 | { | |
5820 | if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) | |
5821 | return FALSE; | |
5822 | } | |
5823 | ||
5824 | if (name == NULL || *name == '\0') | |
5825 | elfsym->st_name = 0; | |
5826 | else if (input_sec->flags & SEC_EXCLUDE) | |
5827 | elfsym->st_name = 0; | |
5828 | else | |
5829 | { | |
5830 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
5831 | name, TRUE, FALSE); | |
5832 | if (elfsym->st_name == (unsigned long) -1) | |
5833 | return FALSE; | |
5834 | } | |
5835 | ||
5836 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
5837 | { | |
5838 | if (! elf_link_flush_output_syms (finfo, bed)) | |
5839 | return FALSE; | |
5840 | } | |
5841 | ||
5842 | dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; | |
5843 | destshndx = finfo->symshndxbuf; | |
5844 | if (destshndx != NULL) | |
5845 | { | |
5846 | if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) | |
5847 | { | |
5848 | bfd_size_type amt; | |
5849 | ||
5850 | amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); | |
5851 | finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); | |
5852 | if (destshndx == NULL) | |
5853 | return FALSE; | |
5854 | memset ((char *) destshndx + amt, 0, amt); | |
5855 | finfo->shndxbuf_size *= 2; | |
5856 | } | |
5857 | destshndx += bfd_get_symcount (finfo->output_bfd); | |
5858 | } | |
5859 | ||
5860 | bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); | |
5861 | finfo->symbuf_count += 1; | |
5862 | bfd_get_symcount (finfo->output_bfd) += 1; | |
5863 | ||
5864 | return TRUE; | |
5865 | } | |
5866 | ||
5867 | /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in | |
5868 | allowing an unsatisfied unversioned symbol in the DSO to match a | |
5869 | versioned symbol that would normally require an explicit version. | |
5870 | We also handle the case that a DSO references a hidden symbol | |
5871 | which may be satisfied by a versioned symbol in another DSO. */ | |
5872 | ||
5873 | static bfd_boolean | |
5874 | elf_link_check_versioned_symbol (struct bfd_link_info *info, | |
5875 | const struct elf_backend_data *bed, | |
5876 | struct elf_link_hash_entry *h) | |
5877 | { | |
5878 | bfd *abfd; | |
5879 | struct elf_link_loaded_list *loaded; | |
5880 | ||
5881 | if (!is_elf_hash_table (info->hash)) | |
5882 | return FALSE; | |
5883 | ||
5884 | switch (h->root.type) | |
5885 | { | |
5886 | default: | |
5887 | abfd = NULL; | |
5888 | break; | |
5889 | ||
5890 | case bfd_link_hash_undefined: | |
5891 | case bfd_link_hash_undefweak: | |
5892 | abfd = h->root.u.undef.abfd; | |
5893 | if ((abfd->flags & DYNAMIC) == 0 | |
e56f61be | 5894 | || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) |
c152c796 AM |
5895 | return FALSE; |
5896 | break; | |
5897 | ||
5898 | case bfd_link_hash_defined: | |
5899 | case bfd_link_hash_defweak: | |
5900 | abfd = h->root.u.def.section->owner; | |
5901 | break; | |
5902 | ||
5903 | case bfd_link_hash_common: | |
5904 | abfd = h->root.u.c.p->section->owner; | |
5905 | break; | |
5906 | } | |
5907 | BFD_ASSERT (abfd != NULL); | |
5908 | ||
5909 | for (loaded = elf_hash_table (info)->loaded; | |
5910 | loaded != NULL; | |
5911 | loaded = loaded->next) | |
5912 | { | |
5913 | bfd *input; | |
5914 | Elf_Internal_Shdr *hdr; | |
5915 | bfd_size_type symcount; | |
5916 | bfd_size_type extsymcount; | |
5917 | bfd_size_type extsymoff; | |
5918 | Elf_Internal_Shdr *versymhdr; | |
5919 | Elf_Internal_Sym *isym; | |
5920 | Elf_Internal_Sym *isymend; | |
5921 | Elf_Internal_Sym *isymbuf; | |
5922 | Elf_External_Versym *ever; | |
5923 | Elf_External_Versym *extversym; | |
5924 | ||
5925 | input = loaded->abfd; | |
5926 | ||
5927 | /* We check each DSO for a possible hidden versioned definition. */ | |
5928 | if (input == abfd | |
5929 | || (input->flags & DYNAMIC) == 0 | |
5930 | || elf_dynversym (input) == 0) | |
5931 | continue; | |
5932 | ||
5933 | hdr = &elf_tdata (input)->dynsymtab_hdr; | |
5934 | ||
5935 | symcount = hdr->sh_size / bed->s->sizeof_sym; | |
5936 | if (elf_bad_symtab (input)) | |
5937 | { | |
5938 | extsymcount = symcount; | |
5939 | extsymoff = 0; | |
5940 | } | |
5941 | else | |
5942 | { | |
5943 | extsymcount = symcount - hdr->sh_info; | |
5944 | extsymoff = hdr->sh_info; | |
5945 | } | |
5946 | ||
5947 | if (extsymcount == 0) | |
5948 | continue; | |
5949 | ||
5950 | isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, | |
5951 | NULL, NULL, NULL); | |
5952 | if (isymbuf == NULL) | |
5953 | return FALSE; | |
5954 | ||
5955 | /* Read in any version definitions. */ | |
5956 | versymhdr = &elf_tdata (input)->dynversym_hdr; | |
5957 | extversym = bfd_malloc (versymhdr->sh_size); | |
5958 | if (extversym == NULL) | |
5959 | goto error_ret; | |
5960 | ||
5961 | if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 | |
5962 | || (bfd_bread (extversym, versymhdr->sh_size, input) | |
5963 | != versymhdr->sh_size)) | |
5964 | { | |
5965 | free (extversym); | |
5966 | error_ret: | |
5967 | free (isymbuf); | |
5968 | return FALSE; | |
5969 | } | |
5970 | ||
5971 | ever = extversym + extsymoff; | |
5972 | isymend = isymbuf + extsymcount; | |
5973 | for (isym = isymbuf; isym < isymend; isym++, ever++) | |
5974 | { | |
5975 | const char *name; | |
5976 | Elf_Internal_Versym iver; | |
5977 | unsigned short version_index; | |
5978 | ||
5979 | if (ELF_ST_BIND (isym->st_info) == STB_LOCAL | |
5980 | || isym->st_shndx == SHN_UNDEF) | |
5981 | continue; | |
5982 | ||
5983 | name = bfd_elf_string_from_elf_section (input, | |
5984 | hdr->sh_link, | |
5985 | isym->st_name); | |
5986 | if (strcmp (name, h->root.root.string) != 0) | |
5987 | continue; | |
5988 | ||
5989 | _bfd_elf_swap_versym_in (input, ever, &iver); | |
5990 | ||
5991 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
5992 | { | |
5993 | /* If we have a non-hidden versioned sym, then it should | |
5994 | have provided a definition for the undefined sym. */ | |
5995 | abort (); | |
5996 | } | |
5997 | ||
5998 | version_index = iver.vs_vers & VERSYM_VERSION; | |
5999 | if (version_index == 1 || version_index == 2) | |
6000 | { | |
6001 | /* This is the base or first version. We can use it. */ | |
6002 | free (extversym); | |
6003 | free (isymbuf); | |
6004 | return TRUE; | |
6005 | } | |
6006 | } | |
6007 | ||
6008 | free (extversym); | |
6009 | free (isymbuf); | |
6010 | } | |
6011 | ||
6012 | return FALSE; | |
6013 | } | |
6014 | ||
6015 | /* Add an external symbol to the symbol table. This is called from | |
6016 | the hash table traversal routine. When generating a shared object, | |
6017 | we go through the symbol table twice. The first time we output | |
6018 | anything that might have been forced to local scope in a version | |
6019 | script. The second time we output the symbols that are still | |
6020 | global symbols. */ | |
6021 | ||
6022 | static bfd_boolean | |
6023 | elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) | |
6024 | { | |
6025 | struct elf_outext_info *eoinfo = data; | |
6026 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
6027 | bfd_boolean strip; | |
6028 | Elf_Internal_Sym sym; | |
6029 | asection *input_sec; | |
6030 | const struct elf_backend_data *bed; | |
6031 | ||
6032 | if (h->root.type == bfd_link_hash_warning) | |
6033 | { | |
6034 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6035 | if (h->root.type == bfd_link_hash_new) | |
6036 | return TRUE; | |
6037 | } | |
6038 | ||
6039 | /* Decide whether to output this symbol in this pass. */ | |
6040 | if (eoinfo->localsyms) | |
6041 | { | |
f5385ebf | 6042 | if (!h->forced_local) |
c152c796 AM |
6043 | return TRUE; |
6044 | } | |
6045 | else | |
6046 | { | |
f5385ebf | 6047 | if (h->forced_local) |
c152c796 AM |
6048 | return TRUE; |
6049 | } | |
6050 | ||
6051 | bed = get_elf_backend_data (finfo->output_bfd); | |
6052 | ||
6053 | /* If we have an undefined symbol reference here then it must have | |
6054 | come from a shared library that is being linked in. (Undefined | |
6055 | references in regular files have already been handled). If we | |
6056 | are reporting errors for this situation then do so now. */ | |
6057 | if (h->root.type == bfd_link_hash_undefined | |
f5385ebf AM |
6058 | && h->ref_dynamic |
6059 | && !h->ref_regular | |
c152c796 AM |
6060 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h) |
6061 | && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) | |
6062 | { | |
6063 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
6064 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
6065 | NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) | |
6066 | { | |
6067 | eoinfo->failed = TRUE; | |
6068 | return FALSE; | |
6069 | } | |
6070 | } | |
6071 | ||
6072 | /* We should also warn if a forced local symbol is referenced from | |
6073 | shared libraries. */ | |
6074 | if (! finfo->info->relocatable | |
6075 | && (! finfo->info->shared) | |
f5385ebf AM |
6076 | && h->forced_local |
6077 | && h->ref_dynamic | |
6078 | && !h->dynamic_def | |
6079 | && !h->dynamic_weak | |
c152c796 AM |
6080 | && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) |
6081 | { | |
6082 | (*_bfd_error_handler) | |
d003868e AM |
6083 | (_("%B: %s symbol `%s' in %B is referenced by DSO"), |
6084 | finfo->output_bfd, h->root.u.def.section->owner, | |
c152c796 AM |
6085 | ELF_ST_VISIBILITY (h->other) == STV_INTERNAL |
6086 | ? "internal" | |
6087 | : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN | |
d003868e AM |
6088 | ? "hidden" : "local", |
6089 | h->root.root.string); | |
c152c796 AM |
6090 | eoinfo->failed = TRUE; |
6091 | return FALSE; | |
6092 | } | |
6093 | ||
6094 | /* We don't want to output symbols that have never been mentioned by | |
6095 | a regular file, or that we have been told to strip. However, if | |
6096 | h->indx is set to -2, the symbol is used by a reloc and we must | |
6097 | output it. */ | |
6098 | if (h->indx == -2) | |
6099 | strip = FALSE; | |
f5385ebf AM |
6100 | else if ((h->def_dynamic |
6101 | || h->ref_dynamic) | |
6102 | && !h->def_regular | |
6103 | && !h->ref_regular) | |
c152c796 AM |
6104 | strip = TRUE; |
6105 | else if (finfo->info->strip == strip_all) | |
6106 | strip = TRUE; | |
6107 | else if (finfo->info->strip == strip_some | |
6108 | && bfd_hash_lookup (finfo->info->keep_hash, | |
6109 | h->root.root.string, FALSE, FALSE) == NULL) | |
6110 | strip = TRUE; | |
6111 | else if (finfo->info->strip_discarded | |
6112 | && (h->root.type == bfd_link_hash_defined | |
6113 | || h->root.type == bfd_link_hash_defweak) | |
6114 | && elf_discarded_section (h->root.u.def.section)) | |
6115 | strip = TRUE; | |
6116 | else | |
6117 | strip = FALSE; | |
6118 | ||
6119 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
6120 | nothing else to do unless it is a forced local symbol. */ | |
6121 | if (strip | |
6122 | && h->dynindx == -1 | |
f5385ebf | 6123 | && !h->forced_local) |
c152c796 AM |
6124 | return TRUE; |
6125 | ||
6126 | sym.st_value = 0; | |
6127 | sym.st_size = h->size; | |
6128 | sym.st_other = h->other; | |
f5385ebf | 6129 | if (h->forced_local) |
c152c796 AM |
6130 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); |
6131 | else if (h->root.type == bfd_link_hash_undefweak | |
6132 | || h->root.type == bfd_link_hash_defweak) | |
6133 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
6134 | else | |
6135 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6136 | ||
6137 | switch (h->root.type) | |
6138 | { | |
6139 | default: | |
6140 | case bfd_link_hash_new: | |
6141 | case bfd_link_hash_warning: | |
6142 | abort (); | |
6143 | return FALSE; | |
6144 | ||
6145 | case bfd_link_hash_undefined: | |
6146 | case bfd_link_hash_undefweak: | |
6147 | input_sec = bfd_und_section_ptr; | |
6148 | sym.st_shndx = SHN_UNDEF; | |
6149 | break; | |
6150 | ||
6151 | case bfd_link_hash_defined: | |
6152 | case bfd_link_hash_defweak: | |
6153 | { | |
6154 | input_sec = h->root.u.def.section; | |
6155 | if (input_sec->output_section != NULL) | |
6156 | { | |
6157 | sym.st_shndx = | |
6158 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
6159 | input_sec->output_section); | |
6160 | if (sym.st_shndx == SHN_BAD) | |
6161 | { | |
6162 | (*_bfd_error_handler) | |
d003868e AM |
6163 | (_("%B: could not find output section %A for input section %A"), |
6164 | finfo->output_bfd, input_sec->output_section, input_sec); | |
c152c796 AM |
6165 | eoinfo->failed = TRUE; |
6166 | return FALSE; | |
6167 | } | |
6168 | ||
6169 | /* ELF symbols in relocatable files are section relative, | |
6170 | but in nonrelocatable files they are virtual | |
6171 | addresses. */ | |
6172 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
6173 | if (! finfo->info->relocatable) | |
6174 | { | |
6175 | sym.st_value += input_sec->output_section->vma; | |
6176 | if (h->type == STT_TLS) | |
6177 | { | |
6178 | /* STT_TLS symbols are relative to PT_TLS segment | |
6179 | base. */ | |
6180 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6181 | sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6182 | } | |
6183 | } | |
6184 | } | |
6185 | else | |
6186 | { | |
6187 | BFD_ASSERT (input_sec->owner == NULL | |
6188 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
6189 | sym.st_shndx = SHN_UNDEF; | |
6190 | input_sec = bfd_und_section_ptr; | |
6191 | } | |
6192 | } | |
6193 | break; | |
6194 | ||
6195 | case bfd_link_hash_common: | |
6196 | input_sec = h->root.u.c.p->section; | |
6197 | sym.st_shndx = SHN_COMMON; | |
6198 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
6199 | break; | |
6200 | ||
6201 | case bfd_link_hash_indirect: | |
6202 | /* These symbols are created by symbol versioning. They point | |
6203 | to the decorated version of the name. For example, if the | |
6204 | symbol foo@@GNU_1.2 is the default, which should be used when | |
6205 | foo is used with no version, then we add an indirect symbol | |
6206 | foo which points to foo@@GNU_1.2. We ignore these symbols, | |
6207 | since the indirected symbol is already in the hash table. */ | |
6208 | return TRUE; | |
6209 | } | |
6210 | ||
6211 | /* Give the processor backend a chance to tweak the symbol value, | |
6212 | and also to finish up anything that needs to be done for this | |
6213 | symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for | |
6214 | forced local syms when non-shared is due to a historical quirk. */ | |
6215 | if ((h->dynindx != -1 | |
f5385ebf | 6216 | || h->forced_local) |
c152c796 AM |
6217 | && ((finfo->info->shared |
6218 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
6219 | || h->root.type != bfd_link_hash_undefweak)) | |
f5385ebf | 6220 | || !h->forced_local) |
c152c796 AM |
6221 | && elf_hash_table (finfo->info)->dynamic_sections_created) |
6222 | { | |
6223 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
6224 | (finfo->output_bfd, finfo->info, h, &sym))) | |
6225 | { | |
6226 | eoinfo->failed = TRUE; | |
6227 | return FALSE; | |
6228 | } | |
6229 | } | |
6230 | ||
6231 | /* If we are marking the symbol as undefined, and there are no | |
6232 | non-weak references to this symbol from a regular object, then | |
6233 | mark the symbol as weak undefined; if there are non-weak | |
6234 | references, mark the symbol as strong. We can't do this earlier, | |
6235 | because it might not be marked as undefined until the | |
6236 | finish_dynamic_symbol routine gets through with it. */ | |
6237 | if (sym.st_shndx == SHN_UNDEF | |
f5385ebf | 6238 | && h->ref_regular |
c152c796 AM |
6239 | && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL |
6240 | || ELF_ST_BIND (sym.st_info) == STB_WEAK)) | |
6241 | { | |
6242 | int bindtype; | |
6243 | ||
f5385ebf | 6244 | if (h->ref_regular_nonweak) |
c152c796 AM |
6245 | bindtype = STB_GLOBAL; |
6246 | else | |
6247 | bindtype = STB_WEAK; | |
6248 | sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); | |
6249 | } | |
6250 | ||
6251 | /* If a non-weak symbol with non-default visibility is not defined | |
6252 | locally, it is a fatal error. */ | |
6253 | if (! finfo->info->relocatable | |
6254 | && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT | |
6255 | && ELF_ST_BIND (sym.st_info) != STB_WEAK | |
6256 | && h->root.type == bfd_link_hash_undefined | |
f5385ebf | 6257 | && !h->def_regular) |
c152c796 AM |
6258 | { |
6259 | (*_bfd_error_handler) | |
d003868e AM |
6260 | (_("%B: %s symbol `%s' isn't defined"), |
6261 | finfo->output_bfd, | |
6262 | ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED | |
6263 | ? "protected" | |
6264 | : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL | |
6265 | ? "internal" : "hidden", | |
6266 | h->root.root.string); | |
c152c796 AM |
6267 | eoinfo->failed = TRUE; |
6268 | return FALSE; | |
6269 | } | |
6270 | ||
6271 | /* If this symbol should be put in the .dynsym section, then put it | |
6272 | there now. We already know the symbol index. We also fill in | |
6273 | the entry in the .hash section. */ | |
6274 | if (h->dynindx != -1 | |
6275 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
6276 | { | |
6277 | size_t bucketcount; | |
6278 | size_t bucket; | |
6279 | size_t hash_entry_size; | |
6280 | bfd_byte *bucketpos; | |
6281 | bfd_vma chain; | |
6282 | bfd_byte *esym; | |
6283 | ||
6284 | sym.st_name = h->dynstr_index; | |
6285 | esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; | |
6286 | bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); | |
6287 | ||
6288 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
f6e332e6 | 6289 | bucket = h->u.elf_hash_value % bucketcount; |
c152c796 AM |
6290 | hash_entry_size |
6291 | = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; | |
6292 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents | |
6293 | + (bucket + 2) * hash_entry_size); | |
6294 | chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); | |
6295 | bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); | |
6296 | bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, | |
6297 | ((bfd_byte *) finfo->hash_sec->contents | |
6298 | + (bucketcount + 2 + h->dynindx) * hash_entry_size)); | |
6299 | ||
6300 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
6301 | { | |
6302 | Elf_Internal_Versym iversym; | |
6303 | Elf_External_Versym *eversym; | |
6304 | ||
f5385ebf | 6305 | if (!h->def_regular) |
c152c796 AM |
6306 | { |
6307 | if (h->verinfo.verdef == NULL) | |
6308 | iversym.vs_vers = 0; | |
6309 | else | |
6310 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
6311 | } | |
6312 | else | |
6313 | { | |
6314 | if (h->verinfo.vertree == NULL) | |
6315 | iversym.vs_vers = 1; | |
6316 | else | |
6317 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
6318 | } | |
6319 | ||
f5385ebf | 6320 | if (h->hidden) |
c152c796 AM |
6321 | iversym.vs_vers |= VERSYM_HIDDEN; |
6322 | ||
6323 | eversym = (Elf_External_Versym *) finfo->symver_sec->contents; | |
6324 | eversym += h->dynindx; | |
6325 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); | |
6326 | } | |
6327 | } | |
6328 | ||
6329 | /* If we're stripping it, then it was just a dynamic symbol, and | |
6330 | there's nothing else to do. */ | |
6331 | if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) | |
6332 | return TRUE; | |
6333 | ||
6334 | h->indx = bfd_get_symcount (finfo->output_bfd); | |
6335 | ||
6336 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) | |
6337 | { | |
6338 | eoinfo->failed = TRUE; | |
6339 | return FALSE; | |
6340 | } | |
6341 | ||
6342 | return TRUE; | |
6343 | } | |
6344 | ||
cdd3575c AM |
6345 | /* Return TRUE if special handling is done for relocs in SEC against |
6346 | symbols defined in discarded sections. */ | |
6347 | ||
c152c796 AM |
6348 | static bfd_boolean |
6349 | elf_section_ignore_discarded_relocs (asection *sec) | |
6350 | { | |
6351 | const struct elf_backend_data *bed; | |
6352 | ||
cdd3575c AM |
6353 | switch (sec->sec_info_type) |
6354 | { | |
6355 | case ELF_INFO_TYPE_STABS: | |
6356 | case ELF_INFO_TYPE_EH_FRAME: | |
6357 | return TRUE; | |
6358 | default: | |
6359 | break; | |
6360 | } | |
c152c796 AM |
6361 | |
6362 | bed = get_elf_backend_data (sec->owner); | |
6363 | if (bed->elf_backend_ignore_discarded_relocs != NULL | |
6364 | && (*bed->elf_backend_ignore_discarded_relocs) (sec)) | |
6365 | return TRUE; | |
6366 | ||
6367 | return FALSE; | |
6368 | } | |
6369 | ||
cdd3575c AM |
6370 | /* Return TRUE if we should complain about a reloc in SEC against a |
6371 | symbol defined in a discarded section. */ | |
6372 | ||
6373 | static bfd_boolean | |
6374 | elf_section_complain_discarded (asection *sec) | |
6375 | { | |
6376 | if (strncmp (".stab", sec->name, 5) == 0 | |
6377 | && (!sec->name[5] || | |
6378 | (sec->name[5] == '.' && ISDIGIT (sec->name[6])))) | |
6379 | return FALSE; | |
6380 | ||
6381 | if (strcmp (".eh_frame", sec->name) == 0) | |
6382 | return FALSE; | |
6383 | ||
6384 | if (strcmp (".gcc_except_table", sec->name) == 0) | |
6385 | return FALSE; | |
6386 | ||
27b56da8 DA |
6387 | if (strcmp (".PARISC.unwind", sec->name) == 0) |
6388 | return FALSE; | |
327c1315 AM |
6389 | |
6390 | if (strcmp (".fixup", sec->name) == 0) | |
6391 | return FALSE; | |
27b56da8 | 6392 | |
cdd3575c AM |
6393 | return TRUE; |
6394 | } | |
6395 | ||
3d7f7666 L |
6396 | /* Find a match between a section and a member of a section group. */ |
6397 | ||
6398 | static asection * | |
6399 | match_group_member (asection *sec, asection *group) | |
6400 | { | |
6401 | asection *first = elf_next_in_group (group); | |
6402 | asection *s = first; | |
6403 | ||
6404 | while (s != NULL) | |
6405 | { | |
6406 | if (bfd_elf_match_symbols_in_sections (s, sec)) | |
6407 | return s; | |
6408 | ||
6409 | if (s == first) | |
6410 | break; | |
6411 | } | |
6412 | ||
6413 | return NULL; | |
6414 | } | |
6415 | ||
c152c796 AM |
6416 | /* Link an input file into the linker output file. This function |
6417 | handles all the sections and relocations of the input file at once. | |
6418 | This is so that we only have to read the local symbols once, and | |
6419 | don't have to keep them in memory. */ | |
6420 | ||
6421 | static bfd_boolean | |
6422 | elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) | |
6423 | { | |
6424 | bfd_boolean (*relocate_section) | |
6425 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
6426 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
6427 | bfd *output_bfd; | |
6428 | Elf_Internal_Shdr *symtab_hdr; | |
6429 | size_t locsymcount; | |
6430 | size_t extsymoff; | |
6431 | Elf_Internal_Sym *isymbuf; | |
6432 | Elf_Internal_Sym *isym; | |
6433 | Elf_Internal_Sym *isymend; | |
6434 | long *pindex; | |
6435 | asection **ppsection; | |
6436 | asection *o; | |
6437 | const struct elf_backend_data *bed; | |
6438 | bfd_boolean emit_relocs; | |
6439 | struct elf_link_hash_entry **sym_hashes; | |
6440 | ||
6441 | output_bfd = finfo->output_bfd; | |
6442 | bed = get_elf_backend_data (output_bfd); | |
6443 | relocate_section = bed->elf_backend_relocate_section; | |
6444 | ||
6445 | /* If this is a dynamic object, we don't want to do anything here: | |
6446 | we don't want the local symbols, and we don't want the section | |
6447 | contents. */ | |
6448 | if ((input_bfd->flags & DYNAMIC) != 0) | |
6449 | return TRUE; | |
6450 | ||
6451 | emit_relocs = (finfo->info->relocatable | |
6452 | || finfo->info->emitrelocations | |
6453 | || bed->elf_backend_emit_relocs); | |
6454 | ||
6455 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
6456 | if (elf_bad_symtab (input_bfd)) | |
6457 | { | |
6458 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
6459 | extsymoff = 0; | |
6460 | } | |
6461 | else | |
6462 | { | |
6463 | locsymcount = symtab_hdr->sh_info; | |
6464 | extsymoff = symtab_hdr->sh_info; | |
6465 | } | |
6466 | ||
6467 | /* Read the local symbols. */ | |
6468 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
6469 | if (isymbuf == NULL && locsymcount != 0) | |
6470 | { | |
6471 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
6472 | finfo->internal_syms, | |
6473 | finfo->external_syms, | |
6474 | finfo->locsym_shndx); | |
6475 | if (isymbuf == NULL) | |
6476 | return FALSE; | |
6477 | } | |
6478 | ||
6479 | /* Find local symbol sections and adjust values of symbols in | |
6480 | SEC_MERGE sections. Write out those local symbols we know are | |
6481 | going into the output file. */ | |
6482 | isymend = isymbuf + locsymcount; | |
6483 | for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; | |
6484 | isym < isymend; | |
6485 | isym++, pindex++, ppsection++) | |
6486 | { | |
6487 | asection *isec; | |
6488 | const char *name; | |
6489 | Elf_Internal_Sym osym; | |
6490 | ||
6491 | *pindex = -1; | |
6492 | ||
6493 | if (elf_bad_symtab (input_bfd)) | |
6494 | { | |
6495 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6496 | { | |
6497 | *ppsection = NULL; | |
6498 | continue; | |
6499 | } | |
6500 | } | |
6501 | ||
6502 | if (isym->st_shndx == SHN_UNDEF) | |
6503 | isec = bfd_und_section_ptr; | |
6504 | else if (isym->st_shndx < SHN_LORESERVE | |
6505 | || isym->st_shndx > SHN_HIRESERVE) | |
6506 | { | |
6507 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
6508 | if (isec | |
6509 | && isec->sec_info_type == ELF_INFO_TYPE_MERGE | |
6510 | && ELF_ST_TYPE (isym->st_info) != STT_SECTION) | |
6511 | isym->st_value = | |
6512 | _bfd_merged_section_offset (output_bfd, &isec, | |
6513 | elf_section_data (isec)->sec_info, | |
753731ee | 6514 | isym->st_value); |
c152c796 AM |
6515 | } |
6516 | else if (isym->st_shndx == SHN_ABS) | |
6517 | isec = bfd_abs_section_ptr; | |
6518 | else if (isym->st_shndx == SHN_COMMON) | |
6519 | isec = bfd_com_section_ptr; | |
6520 | else | |
6521 | { | |
6522 | /* Who knows? */ | |
6523 | isec = NULL; | |
6524 | } | |
6525 | ||
6526 | *ppsection = isec; | |
6527 | ||
6528 | /* Don't output the first, undefined, symbol. */ | |
6529 | if (ppsection == finfo->sections) | |
6530 | continue; | |
6531 | ||
6532 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6533 | { | |
6534 | /* We never output section symbols. Instead, we use the | |
6535 | section symbol of the corresponding section in the output | |
6536 | file. */ | |
6537 | continue; | |
6538 | } | |
6539 | ||
6540 | /* If we are stripping all symbols, we don't want to output this | |
6541 | one. */ | |
6542 | if (finfo->info->strip == strip_all) | |
6543 | continue; | |
6544 | ||
6545 | /* If we are discarding all local symbols, we don't want to | |
6546 | output this one. If we are generating a relocatable output | |
6547 | file, then some of the local symbols may be required by | |
6548 | relocs; we output them below as we discover that they are | |
6549 | needed. */ | |
6550 | if (finfo->info->discard == discard_all) | |
6551 | continue; | |
6552 | ||
6553 | /* If this symbol is defined in a section which we are | |
6554 | discarding, we don't need to keep it, but note that | |
6555 | linker_mark is only reliable for sections that have contents. | |
6556 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
6557 | as well as linker_mark. */ | |
6558 | if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
6559 | && isec != NULL | |
6560 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) | |
6561 | || (! finfo->info->relocatable | |
6562 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
6563 | continue; | |
6564 | ||
6565 | /* Get the name of the symbol. */ | |
6566 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6567 | isym->st_name); | |
6568 | if (name == NULL) | |
6569 | return FALSE; | |
6570 | ||
6571 | /* See if we are discarding symbols with this name. */ | |
6572 | if ((finfo->info->strip == strip_some | |
6573 | && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) | |
6574 | == NULL)) | |
6575 | || (((finfo->info->discard == discard_sec_merge | |
6576 | && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) | |
6577 | || finfo->info->discard == discard_l) | |
6578 | && bfd_is_local_label_name (input_bfd, name))) | |
6579 | continue; | |
6580 | ||
6581 | /* If we get here, we are going to output this symbol. */ | |
6582 | ||
6583 | osym = *isym; | |
6584 | ||
6585 | /* Adjust the section index for the output file. */ | |
6586 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
6587 | isec->output_section); | |
6588 | if (osym.st_shndx == SHN_BAD) | |
6589 | return FALSE; | |
6590 | ||
6591 | *pindex = bfd_get_symcount (output_bfd); | |
6592 | ||
6593 | /* ELF symbols in relocatable files are section relative, but | |
6594 | in executable files they are virtual addresses. Note that | |
6595 | this code assumes that all ELF sections have an associated | |
6596 | BFD section with a reasonable value for output_offset; below | |
6597 | we assume that they also have a reasonable value for | |
6598 | output_section. Any special sections must be set up to meet | |
6599 | these requirements. */ | |
6600 | osym.st_value += isec->output_offset; | |
6601 | if (! finfo->info->relocatable) | |
6602 | { | |
6603 | osym.st_value += isec->output_section->vma; | |
6604 | if (ELF_ST_TYPE (osym.st_info) == STT_TLS) | |
6605 | { | |
6606 | /* STT_TLS symbols are relative to PT_TLS segment base. */ | |
6607 | BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); | |
6608 | osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; | |
6609 | } | |
6610 | } | |
6611 | ||
6612 | if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) | |
6613 | return FALSE; | |
6614 | } | |
6615 | ||
6616 | /* Relocate the contents of each section. */ | |
6617 | sym_hashes = elf_sym_hashes (input_bfd); | |
6618 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6619 | { | |
6620 | bfd_byte *contents; | |
6621 | ||
6622 | if (! o->linker_mark) | |
6623 | { | |
6624 | /* This section was omitted from the link. */ | |
6625 | continue; | |
6626 | } | |
6627 | ||
6628 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 6629 | || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) |
c152c796 AM |
6630 | continue; |
6631 | ||
6632 | if ((o->flags & SEC_LINKER_CREATED) != 0) | |
6633 | { | |
6634 | /* Section was created by _bfd_elf_link_create_dynamic_sections | |
6635 | or somesuch. */ | |
6636 | continue; | |
6637 | } | |
6638 | ||
6639 | /* Get the contents of the section. They have been cached by a | |
6640 | relaxation routine. Note that o is a section in an input | |
6641 | file, so the contents field will not have been set by any of | |
6642 | the routines which work on output files. */ | |
6643 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
6644 | contents = elf_section_data (o)->this_hdr.contents; | |
6645 | else | |
6646 | { | |
eea6121a AM |
6647 | bfd_size_type amt = o->rawsize ? o->rawsize : o->size; |
6648 | ||
c152c796 | 6649 | contents = finfo->contents; |
eea6121a | 6650 | if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) |
c152c796 AM |
6651 | return FALSE; |
6652 | } | |
6653 | ||
6654 | if ((o->flags & SEC_RELOC) != 0) | |
6655 | { | |
6656 | Elf_Internal_Rela *internal_relocs; | |
6657 | bfd_vma r_type_mask; | |
6658 | int r_sym_shift; | |
6659 | ||
6660 | /* Get the swapped relocs. */ | |
6661 | internal_relocs | |
6662 | = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, | |
6663 | finfo->internal_relocs, FALSE); | |
6664 | if (internal_relocs == NULL | |
6665 | && o->reloc_count > 0) | |
6666 | return FALSE; | |
6667 | ||
6668 | if (bed->s->arch_size == 32) | |
6669 | { | |
6670 | r_type_mask = 0xff; | |
6671 | r_sym_shift = 8; | |
6672 | } | |
6673 | else | |
6674 | { | |
6675 | r_type_mask = 0xffffffff; | |
6676 | r_sym_shift = 32; | |
6677 | } | |
6678 | ||
6679 | /* Run through the relocs looking for any against symbols | |
6680 | from discarded sections and section symbols from | |
6681 | removed link-once sections. Complain about relocs | |
6682 | against discarded sections. Zero relocs against removed | |
6683 | link-once sections. Preserve debug information as much | |
6684 | as we can. */ | |
6685 | if (!elf_section_ignore_discarded_relocs (o)) | |
6686 | { | |
6687 | Elf_Internal_Rela *rel, *relend; | |
cdd3575c | 6688 | bfd_boolean complain = elf_section_complain_discarded (o); |
c152c796 AM |
6689 | |
6690 | rel = internal_relocs; | |
6691 | relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6692 | for ( ; rel < relend; rel++) | |
6693 | { | |
6694 | unsigned long r_symndx = rel->r_info >> r_sym_shift; | |
cdd3575c AM |
6695 | asection **ps, *sec; |
6696 | struct elf_link_hash_entry *h = NULL; | |
6697 | const char *sym_name; | |
c152c796 | 6698 | |
ee75fd95 AM |
6699 | if (r_symndx == STN_UNDEF) |
6700 | continue; | |
6701 | ||
c152c796 AM |
6702 | if (r_symndx >= locsymcount |
6703 | || (elf_bad_symtab (input_bfd) | |
6704 | && finfo->sections[r_symndx] == NULL)) | |
6705 | { | |
c152c796 AM |
6706 | h = sym_hashes[r_symndx - extsymoff]; |
6707 | while (h->root.type == bfd_link_hash_indirect | |
6708 | || h->root.type == bfd_link_hash_warning) | |
6709 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
6710 | ||
cdd3575c AM |
6711 | if (h->root.type != bfd_link_hash_defined |
6712 | && h->root.type != bfd_link_hash_defweak) | |
6713 | continue; | |
6714 | ||
6715 | ps = &h->root.u.def.section; | |
6716 | sym_name = h->root.root.string; | |
c152c796 AM |
6717 | } |
6718 | else | |
6719 | { | |
cdd3575c AM |
6720 | Elf_Internal_Sym *sym = isymbuf + r_symndx; |
6721 | ps = &finfo->sections[r_symndx]; | |
6722 | sym_name = bfd_elf_local_sym_name (input_bfd, sym); | |
6723 | } | |
c152c796 | 6724 | |
cdd3575c AM |
6725 | /* Complain if the definition comes from a |
6726 | discarded section. */ | |
6727 | if ((sec = *ps) != NULL && elf_discarded_section (sec)) | |
6728 | { | |
6729 | if ((o->flags & SEC_DEBUGGING) != 0) | |
c152c796 | 6730 | { |
cdd3575c AM |
6731 | BFD_ASSERT (r_symndx != 0); |
6732 | ||
6733 | /* Try to preserve debug information. | |
6734 | FIXME: This is quite broken. Modifying | |
6735 | the symbol here means we will be changing | |
6736 | all uses of the symbol, not just those in | |
6737 | debug sections. The only thing that makes | |
6738 | this half reasonable is that debug sections | |
6739 | tend to come after other sections. Of | |
6740 | course, that doesn't help with globals. | |
6741 | ??? All link-once sections of the same name | |
6742 | ought to define the same set of symbols, so | |
6743 | it would seem that globals ought to always | |
6744 | be defined in the kept section. */ | |
3d7f7666 | 6745 | if (sec->kept_section != NULL) |
c152c796 | 6746 | { |
3d7f7666 L |
6747 | asection *member; |
6748 | ||
6749 | /* Check if it is a linkonce section or | |
6750 | member of a comdat group. */ | |
6751 | if (elf_sec_group (sec) == NULL | |
6752 | && sec->size == sec->kept_section->size) | |
6753 | { | |
6754 | *ps = sec->kept_section; | |
6755 | continue; | |
6756 | } | |
6757 | else if (elf_sec_group (sec) != NULL | |
6758 | && (member = match_group_member (sec, sec->kept_section)) | |
6759 | && sec->size == member->size) | |
6760 | { | |
6761 | *ps = member; | |
6762 | continue; | |
6763 | } | |
c152c796 AM |
6764 | } |
6765 | } | |
cdd3575c AM |
6766 | else if (complain) |
6767 | { | |
d003868e AM |
6768 | (*_bfd_error_handler) |
6769 | (_("`%s' referenced in section `%A' of %B: " | |
6770 | "defined in discarded section `%A' of %B\n"), | |
6771 | o, input_bfd, sec, sec->owner, sym_name); | |
cdd3575c AM |
6772 | } |
6773 | ||
6774 | /* Remove the symbol reference from the reloc, but | |
6775 | don't kill the reloc completely. This is so that | |
6776 | a zero value will be written into the section, | |
6777 | which may have non-zero contents put there by the | |
6778 | assembler. Zero in things like an eh_frame fde | |
6779 | pc_begin allows stack unwinders to recognize the | |
6780 | fde as bogus. */ | |
6781 | rel->r_info &= r_type_mask; | |
6782 | rel->r_addend = 0; | |
c152c796 AM |
6783 | } |
6784 | } | |
6785 | } | |
6786 | ||
6787 | /* Relocate the section by invoking a back end routine. | |
6788 | ||
6789 | The back end routine is responsible for adjusting the | |
6790 | section contents as necessary, and (if using Rela relocs | |
6791 | and generating a relocatable output file) adjusting the | |
6792 | reloc addend as necessary. | |
6793 | ||
6794 | The back end routine does not have to worry about setting | |
6795 | the reloc address or the reloc symbol index. | |
6796 | ||
6797 | The back end routine is given a pointer to the swapped in | |
6798 | internal symbols, and can access the hash table entries | |
6799 | for the external symbols via elf_sym_hashes (input_bfd). | |
6800 | ||
6801 | When generating relocatable output, the back end routine | |
6802 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
6803 | output symbol is going to be a section symbol | |
6804 | corresponding to the output section, which will require | |
6805 | the addend to be adjusted. */ | |
6806 | ||
6807 | if (! (*relocate_section) (output_bfd, finfo->info, | |
6808 | input_bfd, o, contents, | |
6809 | internal_relocs, | |
6810 | isymbuf, | |
6811 | finfo->sections)) | |
6812 | return FALSE; | |
6813 | ||
6814 | if (emit_relocs) | |
6815 | { | |
6816 | Elf_Internal_Rela *irela; | |
6817 | Elf_Internal_Rela *irelaend; | |
6818 | bfd_vma last_offset; | |
6819 | struct elf_link_hash_entry **rel_hash; | |
6820 | Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; | |
6821 | unsigned int next_erel; | |
6822 | bfd_boolean (*reloc_emitter) | |
6823 | (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *); | |
6824 | bfd_boolean rela_normal; | |
6825 | ||
6826 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
6827 | rela_normal = (bed->rela_normal | |
6828 | && (input_rel_hdr->sh_entsize | |
6829 | == bed->s->sizeof_rela)); | |
6830 | ||
6831 | /* Adjust the reloc addresses and symbol indices. */ | |
6832 | ||
6833 | irela = internal_relocs; | |
6834 | irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; | |
6835 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
6836 | + elf_section_data (o->output_section)->rel_count | |
6837 | + elf_section_data (o->output_section)->rel_count2); | |
6838 | last_offset = o->output_offset; | |
6839 | if (!finfo->info->relocatable) | |
6840 | last_offset += o->output_section->vma; | |
6841 | for (next_erel = 0; irela < irelaend; irela++, next_erel++) | |
6842 | { | |
6843 | unsigned long r_symndx; | |
6844 | asection *sec; | |
6845 | Elf_Internal_Sym sym; | |
6846 | ||
6847 | if (next_erel == bed->s->int_rels_per_ext_rel) | |
6848 | { | |
6849 | rel_hash++; | |
6850 | next_erel = 0; | |
6851 | } | |
6852 | ||
6853 | irela->r_offset = _bfd_elf_section_offset (output_bfd, | |
6854 | finfo->info, o, | |
6855 | irela->r_offset); | |
6856 | if (irela->r_offset >= (bfd_vma) -2) | |
6857 | { | |
6858 | /* This is a reloc for a deleted entry or somesuch. | |
6859 | Turn it into an R_*_NONE reloc, at the same | |
6860 | offset as the last reloc. elf_eh_frame.c and | |
6861 | elf_bfd_discard_info rely on reloc offsets | |
6862 | being ordered. */ | |
6863 | irela->r_offset = last_offset; | |
6864 | irela->r_info = 0; | |
6865 | irela->r_addend = 0; | |
6866 | continue; | |
6867 | } | |
6868 | ||
6869 | irela->r_offset += o->output_offset; | |
6870 | ||
6871 | /* Relocs in an executable have to be virtual addresses. */ | |
6872 | if (!finfo->info->relocatable) | |
6873 | irela->r_offset += o->output_section->vma; | |
6874 | ||
6875 | last_offset = irela->r_offset; | |
6876 | ||
6877 | r_symndx = irela->r_info >> r_sym_shift; | |
6878 | if (r_symndx == STN_UNDEF) | |
6879 | continue; | |
6880 | ||
6881 | if (r_symndx >= locsymcount | |
6882 | || (elf_bad_symtab (input_bfd) | |
6883 | && finfo->sections[r_symndx] == NULL)) | |
6884 | { | |
6885 | struct elf_link_hash_entry *rh; | |
6886 | unsigned long indx; | |
6887 | ||
6888 | /* This is a reloc against a global symbol. We | |
6889 | have not yet output all the local symbols, so | |
6890 | we do not know the symbol index of any global | |
6891 | symbol. We set the rel_hash entry for this | |
6892 | reloc to point to the global hash table entry | |
6893 | for this symbol. The symbol index is then | |
ee75fd95 | 6894 | set at the end of bfd_elf_final_link. */ |
c152c796 AM |
6895 | indx = r_symndx - extsymoff; |
6896 | rh = elf_sym_hashes (input_bfd)[indx]; | |
6897 | while (rh->root.type == bfd_link_hash_indirect | |
6898 | || rh->root.type == bfd_link_hash_warning) | |
6899 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
6900 | ||
6901 | /* Setting the index to -2 tells | |
6902 | elf_link_output_extsym that this symbol is | |
6903 | used by a reloc. */ | |
6904 | BFD_ASSERT (rh->indx < 0); | |
6905 | rh->indx = -2; | |
6906 | ||
6907 | *rel_hash = rh; | |
6908 | ||
6909 | continue; | |
6910 | } | |
6911 | ||
6912 | /* This is a reloc against a local symbol. */ | |
6913 | ||
6914 | *rel_hash = NULL; | |
6915 | sym = isymbuf[r_symndx]; | |
6916 | sec = finfo->sections[r_symndx]; | |
6917 | if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) | |
6918 | { | |
6919 | /* I suppose the backend ought to fill in the | |
6920 | section of any STT_SECTION symbol against a | |
6a8d1586 AM |
6921 | processor specific section. */ |
6922 | r_symndx = 0; | |
6923 | if (bfd_is_abs_section (sec)) | |
6924 | ; | |
c152c796 AM |
6925 | else if (sec == NULL || sec->owner == NULL) |
6926 | { | |
6927 | bfd_set_error (bfd_error_bad_value); | |
6928 | return FALSE; | |
6929 | } | |
6930 | else | |
6931 | { | |
6a8d1586 AM |
6932 | asection *osec = sec->output_section; |
6933 | ||
6934 | /* If we have discarded a section, the output | |
6935 | section will be the absolute section. In | |
6936 | case of discarded link-once and discarded | |
6937 | SEC_MERGE sections, use the kept section. */ | |
6938 | if (bfd_is_abs_section (osec) | |
6939 | && sec->kept_section != NULL | |
6940 | && sec->kept_section->output_section != NULL) | |
6941 | { | |
6942 | osec = sec->kept_section->output_section; | |
6943 | irela->r_addend -= osec->vma; | |
6944 | } | |
6945 | ||
6946 | if (!bfd_is_abs_section (osec)) | |
6947 | { | |
6948 | r_symndx = osec->target_index; | |
6949 | BFD_ASSERT (r_symndx != 0); | |
6950 | } | |
c152c796 AM |
6951 | } |
6952 | ||
6953 | /* Adjust the addend according to where the | |
6954 | section winds up in the output section. */ | |
6955 | if (rela_normal) | |
6956 | irela->r_addend += sec->output_offset; | |
6957 | } | |
6958 | else | |
6959 | { | |
6960 | if (finfo->indices[r_symndx] == -1) | |
6961 | { | |
6962 | unsigned long shlink; | |
6963 | const char *name; | |
6964 | asection *osec; | |
6965 | ||
6966 | if (finfo->info->strip == strip_all) | |
6967 | { | |
6968 | /* You can't do ld -r -s. */ | |
6969 | bfd_set_error (bfd_error_invalid_operation); | |
6970 | return FALSE; | |
6971 | } | |
6972 | ||
6973 | /* This symbol was skipped earlier, but | |
6974 | since it is needed by a reloc, we | |
6975 | must output it now. */ | |
6976 | shlink = symtab_hdr->sh_link; | |
6977 | name = (bfd_elf_string_from_elf_section | |
6978 | (input_bfd, shlink, sym.st_name)); | |
6979 | if (name == NULL) | |
6980 | return FALSE; | |
6981 | ||
6982 | osec = sec->output_section; | |
6983 | sym.st_shndx = | |
6984 | _bfd_elf_section_from_bfd_section (output_bfd, | |
6985 | osec); | |
6986 | if (sym.st_shndx == SHN_BAD) | |
6987 | return FALSE; | |
6988 | ||
6989 | sym.st_value += sec->output_offset; | |
6990 | if (! finfo->info->relocatable) | |
6991 | { | |
6992 | sym.st_value += osec->vma; | |
6993 | if (ELF_ST_TYPE (sym.st_info) == STT_TLS) | |
6994 | { | |
6995 | /* STT_TLS symbols are relative to PT_TLS | |
6996 | segment base. */ | |
6997 | BFD_ASSERT (elf_hash_table (finfo->info) | |
6998 | ->tls_sec != NULL); | |
6999 | sym.st_value -= (elf_hash_table (finfo->info) | |
7000 | ->tls_sec->vma); | |
7001 | } | |
7002 | } | |
7003 | ||
7004 | finfo->indices[r_symndx] | |
7005 | = bfd_get_symcount (output_bfd); | |
7006 | ||
7007 | if (! elf_link_output_sym (finfo, name, &sym, sec, | |
7008 | NULL)) | |
7009 | return FALSE; | |
7010 | } | |
7011 | ||
7012 | r_symndx = finfo->indices[r_symndx]; | |
7013 | } | |
7014 | ||
7015 | irela->r_info = ((bfd_vma) r_symndx << r_sym_shift | |
7016 | | (irela->r_info & r_type_mask)); | |
7017 | } | |
7018 | ||
7019 | /* Swap out the relocs. */ | |
7020 | if (bed->elf_backend_emit_relocs | |
7021 | && !(finfo->info->relocatable | |
7022 | || finfo->info->emitrelocations)) | |
7023 | reloc_emitter = bed->elf_backend_emit_relocs; | |
7024 | else | |
7025 | reloc_emitter = _bfd_elf_link_output_relocs; | |
7026 | ||
7027 | if (input_rel_hdr->sh_size != 0 | |
7028 | && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr, | |
7029 | internal_relocs)) | |
7030 | return FALSE; | |
7031 | ||
7032 | input_rel_hdr2 = elf_section_data (o)->rel_hdr2; | |
7033 | if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) | |
7034 | { | |
7035 | internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) | |
7036 | * bed->s->int_rels_per_ext_rel); | |
7037 | if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2, | |
7038 | internal_relocs)) | |
7039 | return FALSE; | |
7040 | } | |
7041 | } | |
7042 | } | |
7043 | ||
7044 | /* Write out the modified section contents. */ | |
7045 | if (bed->elf_backend_write_section | |
7046 | && (*bed->elf_backend_write_section) (output_bfd, o, contents)) | |
7047 | { | |
7048 | /* Section written out. */ | |
7049 | } | |
7050 | else switch (o->sec_info_type) | |
7051 | { | |
7052 | case ELF_INFO_TYPE_STABS: | |
7053 | if (! (_bfd_write_section_stabs | |
7054 | (output_bfd, | |
7055 | &elf_hash_table (finfo->info)->stab_info, | |
7056 | o, &elf_section_data (o)->sec_info, contents))) | |
7057 | return FALSE; | |
7058 | break; | |
7059 | case ELF_INFO_TYPE_MERGE: | |
7060 | if (! _bfd_write_merged_section (output_bfd, o, | |
7061 | elf_section_data (o)->sec_info)) | |
7062 | return FALSE; | |
7063 | break; | |
7064 | case ELF_INFO_TYPE_EH_FRAME: | |
7065 | { | |
7066 | if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, | |
7067 | o, contents)) | |
7068 | return FALSE; | |
7069 | } | |
7070 | break; | |
7071 | default: | |
7072 | { | |
c152c796 AM |
7073 | if (! (o->flags & SEC_EXCLUDE) |
7074 | && ! bfd_set_section_contents (output_bfd, o->output_section, | |
7075 | contents, | |
7076 | (file_ptr) o->output_offset, | |
eea6121a | 7077 | o->size)) |
c152c796 AM |
7078 | return FALSE; |
7079 | } | |
7080 | break; | |
7081 | } | |
7082 | } | |
7083 | ||
7084 | return TRUE; | |
7085 | } | |
7086 | ||
7087 | /* Generate a reloc when linking an ELF file. This is a reloc | |
7088 | requested by the linker, and does come from any input file. This | |
7089 | is used to build constructor and destructor tables when linking | |
7090 | with -Ur. */ | |
7091 | ||
7092 | static bfd_boolean | |
7093 | elf_reloc_link_order (bfd *output_bfd, | |
7094 | struct bfd_link_info *info, | |
7095 | asection *output_section, | |
7096 | struct bfd_link_order *link_order) | |
7097 | { | |
7098 | reloc_howto_type *howto; | |
7099 | long indx; | |
7100 | bfd_vma offset; | |
7101 | bfd_vma addend; | |
7102 | struct elf_link_hash_entry **rel_hash_ptr; | |
7103 | Elf_Internal_Shdr *rel_hdr; | |
7104 | const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); | |
7105 | Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; | |
7106 | bfd_byte *erel; | |
7107 | unsigned int i; | |
7108 | ||
7109 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
7110 | if (howto == NULL) | |
7111 | { | |
7112 | bfd_set_error (bfd_error_bad_value); | |
7113 | return FALSE; | |
7114 | } | |
7115 | ||
7116 | addend = link_order->u.reloc.p->addend; | |
7117 | ||
7118 | /* Figure out the symbol index. */ | |
7119 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
7120 | + elf_section_data (output_section)->rel_count | |
7121 | + elf_section_data (output_section)->rel_count2); | |
7122 | if (link_order->type == bfd_section_reloc_link_order) | |
7123 | { | |
7124 | indx = link_order->u.reloc.p->u.section->target_index; | |
7125 | BFD_ASSERT (indx != 0); | |
7126 | *rel_hash_ptr = NULL; | |
7127 | } | |
7128 | else | |
7129 | { | |
7130 | struct elf_link_hash_entry *h; | |
7131 | ||
7132 | /* Treat a reloc against a defined symbol as though it were | |
7133 | actually against the section. */ | |
7134 | h = ((struct elf_link_hash_entry *) | |
7135 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
7136 | link_order->u.reloc.p->u.name, | |
7137 | FALSE, FALSE, TRUE)); | |
7138 | if (h != NULL | |
7139 | && (h->root.type == bfd_link_hash_defined | |
7140 | || h->root.type == bfd_link_hash_defweak)) | |
7141 | { | |
7142 | asection *section; | |
7143 | ||
7144 | section = h->root.u.def.section; | |
7145 | indx = section->output_section->target_index; | |
7146 | *rel_hash_ptr = NULL; | |
7147 | /* It seems that we ought to add the symbol value to the | |
7148 | addend here, but in practice it has already been added | |
7149 | because it was passed to constructor_callback. */ | |
7150 | addend += section->output_section->vma + section->output_offset; | |
7151 | } | |
7152 | else if (h != NULL) | |
7153 | { | |
7154 | /* Setting the index to -2 tells elf_link_output_extsym that | |
7155 | this symbol is used by a reloc. */ | |
7156 | h->indx = -2; | |
7157 | *rel_hash_ptr = h; | |
7158 | indx = 0; | |
7159 | } | |
7160 | else | |
7161 | { | |
7162 | if (! ((*info->callbacks->unattached_reloc) | |
7163 | (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) | |
7164 | return FALSE; | |
7165 | indx = 0; | |
7166 | } | |
7167 | } | |
7168 | ||
7169 | /* If this is an inplace reloc, we must write the addend into the | |
7170 | object file. */ | |
7171 | if (howto->partial_inplace && addend != 0) | |
7172 | { | |
7173 | bfd_size_type size; | |
7174 | bfd_reloc_status_type rstat; | |
7175 | bfd_byte *buf; | |
7176 | bfd_boolean ok; | |
7177 | const char *sym_name; | |
7178 | ||
7179 | size = bfd_get_reloc_size (howto); | |
7180 | buf = bfd_zmalloc (size); | |
7181 | if (buf == NULL) | |
7182 | return FALSE; | |
7183 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); | |
7184 | switch (rstat) | |
7185 | { | |
7186 | case bfd_reloc_ok: | |
7187 | break; | |
7188 | ||
7189 | default: | |
7190 | case bfd_reloc_outofrange: | |
7191 | abort (); | |
7192 | ||
7193 | case bfd_reloc_overflow: | |
7194 | if (link_order->type == bfd_section_reloc_link_order) | |
7195 | sym_name = bfd_section_name (output_bfd, | |
7196 | link_order->u.reloc.p->u.section); | |
7197 | else | |
7198 | sym_name = link_order->u.reloc.p->u.name; | |
7199 | if (! ((*info->callbacks->reloc_overflow) | |
7200 | (info, sym_name, howto->name, addend, NULL, NULL, 0))) | |
7201 | { | |
7202 | free (buf); | |
7203 | return FALSE; | |
7204 | } | |
7205 | break; | |
7206 | } | |
7207 | ok = bfd_set_section_contents (output_bfd, output_section, buf, | |
7208 | link_order->offset, size); | |
7209 | free (buf); | |
7210 | if (! ok) | |
7211 | return FALSE; | |
7212 | } | |
7213 | ||
7214 | /* The address of a reloc is relative to the section in a | |
7215 | relocatable file, and is a virtual address in an executable | |
7216 | file. */ | |
7217 | offset = link_order->offset; | |
7218 | if (! info->relocatable) | |
7219 | offset += output_section->vma; | |
7220 | ||
7221 | for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) | |
7222 | { | |
7223 | irel[i].r_offset = offset; | |
7224 | irel[i].r_info = 0; | |
7225 | irel[i].r_addend = 0; | |
7226 | } | |
7227 | if (bed->s->arch_size == 32) | |
7228 | irel[0].r_info = ELF32_R_INFO (indx, howto->type); | |
7229 | else | |
7230 | irel[0].r_info = ELF64_R_INFO (indx, howto->type); | |
7231 | ||
7232 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
7233 | erel = rel_hdr->contents; | |
7234 | if (rel_hdr->sh_type == SHT_REL) | |
7235 | { | |
7236 | erel += (elf_section_data (output_section)->rel_count | |
7237 | * bed->s->sizeof_rel); | |
7238 | (*bed->s->swap_reloc_out) (output_bfd, irel, erel); | |
7239 | } | |
7240 | else | |
7241 | { | |
7242 | irel[0].r_addend = addend; | |
7243 | erel += (elf_section_data (output_section)->rel_count | |
7244 | * bed->s->sizeof_rela); | |
7245 | (*bed->s->swap_reloca_out) (output_bfd, irel, erel); | |
7246 | } | |
7247 | ||
7248 | ++elf_section_data (output_section)->rel_count; | |
7249 | ||
7250 | return TRUE; | |
7251 | } | |
7252 | ||
0b52efa6 PB |
7253 | |
7254 | /* Get the output vma of the section pointed to by the sh_link field. */ | |
7255 | ||
7256 | static bfd_vma | |
7257 | elf_get_linked_section_vma (struct bfd_link_order *p) | |
7258 | { | |
7259 | Elf_Internal_Shdr **elf_shdrp; | |
7260 | asection *s; | |
7261 | int elfsec; | |
7262 | ||
7263 | s = p->u.indirect.section; | |
7264 | elf_shdrp = elf_elfsections (s->owner); | |
7265 | elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); | |
7266 | elfsec = elf_shdrp[elfsec]->sh_link; | |
185d09ad L |
7267 | /* PR 290: |
7268 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
7269 | SHF_LINK_ORDER. But it doesn't set theh sh_link or | |
7270 | sh_info fields. Hence we could get the situation | |
7271 | where elfsec is 0. */ | |
7272 | if (elfsec == 0) | |
7273 | { | |
7274 | const struct elf_backend_data *bed | |
7275 | = get_elf_backend_data (s->owner); | |
7276 | if (bed->link_order_error_handler) | |
d003868e AM |
7277 | bed->link_order_error_handler |
7278 | (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); | |
185d09ad L |
7279 | return 0; |
7280 | } | |
7281 | else | |
7282 | { | |
7283 | s = elf_shdrp[elfsec]->bfd_section; | |
7284 | return s->output_section->vma + s->output_offset; | |
7285 | } | |
0b52efa6 PB |
7286 | } |
7287 | ||
7288 | ||
7289 | /* Compare two sections based on the locations of the sections they are | |
7290 | linked to. Used by elf_fixup_link_order. */ | |
7291 | ||
7292 | static int | |
7293 | compare_link_order (const void * a, const void * b) | |
7294 | { | |
7295 | bfd_vma apos; | |
7296 | bfd_vma bpos; | |
7297 | ||
7298 | apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); | |
7299 | bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); | |
7300 | if (apos < bpos) | |
7301 | return -1; | |
7302 | return apos > bpos; | |
7303 | } | |
7304 | ||
7305 | ||
7306 | /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same | |
7307 | order as their linked sections. Returns false if this could not be done | |
7308 | because an output section includes both ordered and unordered | |
7309 | sections. Ideally we'd do this in the linker proper. */ | |
7310 | ||
7311 | static bfd_boolean | |
7312 | elf_fixup_link_order (bfd *abfd, asection *o) | |
7313 | { | |
7314 | int seen_linkorder; | |
7315 | int seen_other; | |
7316 | int n; | |
7317 | struct bfd_link_order *p; | |
7318 | bfd *sub; | |
7319 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7320 | int elfsec; | |
7321 | struct bfd_link_order **sections; | |
7322 | asection *s; | |
7323 | bfd_vma offset; | |
7324 | ||
7325 | seen_other = 0; | |
7326 | seen_linkorder = 0; | |
7327 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7328 | { | |
7329 | if (p->type == bfd_indirect_link_order | |
7330 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7331 | == bfd_target_elf_flavour) | |
7332 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7333 | { | |
7334 | s = p->u.indirect.section; | |
7335 | elfsec = _bfd_elf_section_from_bfd_section (sub, s); | |
7336 | if (elfsec != -1 | |
7337 | && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) | |
7338 | seen_linkorder++; | |
7339 | else | |
7340 | seen_other++; | |
7341 | } | |
7342 | else | |
7343 | seen_other++; | |
7344 | } | |
7345 | ||
7346 | if (!seen_linkorder) | |
7347 | return TRUE; | |
7348 | ||
7349 | if (seen_other && seen_linkorder) | |
08ccf96b | 7350 | { |
d003868e AM |
7351 | (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), |
7352 | o); | |
08ccf96b L |
7353 | bfd_set_error (bfd_error_bad_value); |
7354 | return FALSE; | |
7355 | } | |
0b52efa6 PB |
7356 | |
7357 | sections = (struct bfd_link_order **) | |
7358 | xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); | |
7359 | seen_linkorder = 0; | |
7360 | ||
7361 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7362 | { | |
7363 | sections[seen_linkorder++] = p; | |
7364 | } | |
7365 | /* Sort the input sections in the order of their linked section. */ | |
7366 | qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), | |
7367 | compare_link_order); | |
7368 | ||
7369 | /* Change the offsets of the sections. */ | |
7370 | offset = 0; | |
7371 | for (n = 0; n < seen_linkorder; n++) | |
7372 | { | |
7373 | s = sections[n]->u.indirect.section; | |
7374 | offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); | |
7375 | s->output_offset = offset; | |
7376 | sections[n]->offset = offset; | |
7377 | offset += sections[n]->size; | |
7378 | } | |
7379 | ||
7380 | return TRUE; | |
7381 | } | |
7382 | ||
7383 | ||
c152c796 AM |
7384 | /* Do the final step of an ELF link. */ |
7385 | ||
7386 | bfd_boolean | |
7387 | bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) | |
7388 | { | |
7389 | bfd_boolean dynamic; | |
7390 | bfd_boolean emit_relocs; | |
7391 | bfd *dynobj; | |
7392 | struct elf_final_link_info finfo; | |
7393 | register asection *o; | |
7394 | register struct bfd_link_order *p; | |
7395 | register bfd *sub; | |
7396 | bfd_size_type max_contents_size; | |
7397 | bfd_size_type max_external_reloc_size; | |
7398 | bfd_size_type max_internal_reloc_count; | |
7399 | bfd_size_type max_sym_count; | |
7400 | bfd_size_type max_sym_shndx_count; | |
7401 | file_ptr off; | |
7402 | Elf_Internal_Sym elfsym; | |
7403 | unsigned int i; | |
7404 | Elf_Internal_Shdr *symtab_hdr; | |
7405 | Elf_Internal_Shdr *symtab_shndx_hdr; | |
7406 | Elf_Internal_Shdr *symstrtab_hdr; | |
7407 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7408 | struct elf_outext_info eoinfo; | |
7409 | bfd_boolean merged; | |
7410 | size_t relativecount = 0; | |
7411 | asection *reldyn = 0; | |
7412 | bfd_size_type amt; | |
7413 | ||
7414 | if (! is_elf_hash_table (info->hash)) | |
7415 | return FALSE; | |
7416 | ||
7417 | if (info->shared) | |
7418 | abfd->flags |= DYNAMIC; | |
7419 | ||
7420 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
7421 | dynobj = elf_hash_table (info)->dynobj; | |
7422 | ||
7423 | emit_relocs = (info->relocatable | |
7424 | || info->emitrelocations | |
7425 | || bed->elf_backend_emit_relocs); | |
7426 | ||
7427 | finfo.info = info; | |
7428 | finfo.output_bfd = abfd; | |
7429 | finfo.symstrtab = _bfd_elf_stringtab_init (); | |
7430 | if (finfo.symstrtab == NULL) | |
7431 | return FALSE; | |
7432 | ||
7433 | if (! dynamic) | |
7434 | { | |
7435 | finfo.dynsym_sec = NULL; | |
7436 | finfo.hash_sec = NULL; | |
7437 | finfo.symver_sec = NULL; | |
7438 | } | |
7439 | else | |
7440 | { | |
7441 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
7442 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
7443 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
7444 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
7445 | /* Note that it is OK if symver_sec is NULL. */ | |
7446 | } | |
7447 | ||
7448 | finfo.contents = NULL; | |
7449 | finfo.external_relocs = NULL; | |
7450 | finfo.internal_relocs = NULL; | |
7451 | finfo.external_syms = NULL; | |
7452 | finfo.locsym_shndx = NULL; | |
7453 | finfo.internal_syms = NULL; | |
7454 | finfo.indices = NULL; | |
7455 | finfo.sections = NULL; | |
7456 | finfo.symbuf = NULL; | |
7457 | finfo.symshndxbuf = NULL; | |
7458 | finfo.symbuf_count = 0; | |
7459 | finfo.shndxbuf_size = 0; | |
7460 | ||
7461 | /* Count up the number of relocations we will output for each output | |
7462 | section, so that we know the sizes of the reloc sections. We | |
7463 | also figure out some maximum sizes. */ | |
7464 | max_contents_size = 0; | |
7465 | max_external_reloc_size = 0; | |
7466 | max_internal_reloc_count = 0; | |
7467 | max_sym_count = 0; | |
7468 | max_sym_shndx_count = 0; | |
7469 | merged = FALSE; | |
7470 | for (o = abfd->sections; o != NULL; o = o->next) | |
7471 | { | |
7472 | struct bfd_elf_section_data *esdo = elf_section_data (o); | |
7473 | o->reloc_count = 0; | |
7474 | ||
7475 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7476 | { | |
7477 | unsigned int reloc_count = 0; | |
7478 | struct bfd_elf_section_data *esdi = NULL; | |
7479 | unsigned int *rel_count1; | |
7480 | ||
7481 | if (p->type == bfd_section_reloc_link_order | |
7482 | || p->type == bfd_symbol_reloc_link_order) | |
7483 | reloc_count = 1; | |
7484 | else if (p->type == bfd_indirect_link_order) | |
7485 | { | |
7486 | asection *sec; | |
7487 | ||
7488 | sec = p->u.indirect.section; | |
7489 | esdi = elf_section_data (sec); | |
7490 | ||
7491 | /* Mark all sections which are to be included in the | |
7492 | link. This will normally be every section. We need | |
7493 | to do this so that we can identify any sections which | |
7494 | the linker has decided to not include. */ | |
7495 | sec->linker_mark = TRUE; | |
7496 | ||
7497 | if (sec->flags & SEC_MERGE) | |
7498 | merged = TRUE; | |
7499 | ||
7500 | if (info->relocatable || info->emitrelocations) | |
7501 | reloc_count = sec->reloc_count; | |
7502 | else if (bed->elf_backend_count_relocs) | |
7503 | { | |
7504 | Elf_Internal_Rela * relocs; | |
7505 | ||
7506 | relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, | |
7507 | info->keep_memory); | |
7508 | ||
7509 | reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); | |
7510 | ||
7511 | if (elf_section_data (o)->relocs != relocs) | |
7512 | free (relocs); | |
7513 | } | |
7514 | ||
eea6121a AM |
7515 | if (sec->rawsize > max_contents_size) |
7516 | max_contents_size = sec->rawsize; | |
7517 | if (sec->size > max_contents_size) | |
7518 | max_contents_size = sec->size; | |
c152c796 AM |
7519 | |
7520 | /* We are interested in just local symbols, not all | |
7521 | symbols. */ | |
7522 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
7523 | && (sec->owner->flags & DYNAMIC) == 0) | |
7524 | { | |
7525 | size_t sym_count; | |
7526 | ||
7527 | if (elf_bad_symtab (sec->owner)) | |
7528 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
7529 | / bed->s->sizeof_sym); | |
7530 | else | |
7531 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
7532 | ||
7533 | if (sym_count > max_sym_count) | |
7534 | max_sym_count = sym_count; | |
7535 | ||
7536 | if (sym_count > max_sym_shndx_count | |
7537 | && elf_symtab_shndx (sec->owner) != 0) | |
7538 | max_sym_shndx_count = sym_count; | |
7539 | ||
7540 | if ((sec->flags & SEC_RELOC) != 0) | |
7541 | { | |
7542 | size_t ext_size; | |
7543 | ||
7544 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
7545 | if (ext_size > max_external_reloc_size) | |
7546 | max_external_reloc_size = ext_size; | |
7547 | if (sec->reloc_count > max_internal_reloc_count) | |
7548 | max_internal_reloc_count = sec->reloc_count; | |
7549 | } | |
7550 | } | |
7551 | } | |
7552 | ||
7553 | if (reloc_count == 0) | |
7554 | continue; | |
7555 | ||
7556 | o->reloc_count += reloc_count; | |
7557 | ||
7558 | /* MIPS may have a mix of REL and RELA relocs on sections. | |
7559 | To support this curious ABI we keep reloc counts in | |
7560 | elf_section_data too. We must be careful to add the | |
7561 | relocations from the input section to the right output | |
7562 | count. FIXME: Get rid of one count. We have | |
7563 | o->reloc_count == esdo->rel_count + esdo->rel_count2. */ | |
7564 | rel_count1 = &esdo->rel_count; | |
7565 | if (esdi != NULL) | |
7566 | { | |
7567 | bfd_boolean same_size; | |
7568 | bfd_size_type entsize1; | |
7569 | ||
7570 | entsize1 = esdi->rel_hdr.sh_entsize; | |
7571 | BFD_ASSERT (entsize1 == bed->s->sizeof_rel | |
7572 | || entsize1 == bed->s->sizeof_rela); | |
7573 | same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); | |
7574 | ||
7575 | if (!same_size) | |
7576 | rel_count1 = &esdo->rel_count2; | |
7577 | ||
7578 | if (esdi->rel_hdr2 != NULL) | |
7579 | { | |
7580 | bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; | |
7581 | unsigned int alt_count; | |
7582 | unsigned int *rel_count2; | |
7583 | ||
7584 | BFD_ASSERT (entsize2 != entsize1 | |
7585 | && (entsize2 == bed->s->sizeof_rel | |
7586 | || entsize2 == bed->s->sizeof_rela)); | |
7587 | ||
7588 | rel_count2 = &esdo->rel_count2; | |
7589 | if (!same_size) | |
7590 | rel_count2 = &esdo->rel_count; | |
7591 | ||
7592 | /* The following is probably too simplistic if the | |
7593 | backend counts output relocs unusually. */ | |
7594 | BFD_ASSERT (bed->elf_backend_count_relocs == NULL); | |
7595 | alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); | |
7596 | *rel_count2 += alt_count; | |
7597 | reloc_count -= alt_count; | |
7598 | } | |
7599 | } | |
7600 | *rel_count1 += reloc_count; | |
7601 | } | |
7602 | ||
7603 | if (o->reloc_count > 0) | |
7604 | o->flags |= SEC_RELOC; | |
7605 | else | |
7606 | { | |
7607 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
7608 | set it (this is probably a bug) and if it is set | |
7609 | assign_section_numbers will create a reloc section. */ | |
7610 | o->flags &=~ SEC_RELOC; | |
7611 | } | |
7612 | ||
7613 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
7614 | zero. This is done in elf_fake_sections as well, but forcing | |
7615 | the VMA to 0 here will ensure that relocs against these | |
7616 | sections are handled correctly. */ | |
7617 | if ((o->flags & SEC_ALLOC) == 0 | |
7618 | && ! o->user_set_vma) | |
7619 | o->vma = 0; | |
7620 | } | |
7621 | ||
7622 | if (! info->relocatable && merged) | |
7623 | elf_link_hash_traverse (elf_hash_table (info), | |
7624 | _bfd_elf_link_sec_merge_syms, abfd); | |
7625 | ||
7626 | /* Figure out the file positions for everything but the symbol table | |
7627 | and the relocs. We set symcount to force assign_section_numbers | |
7628 | to create a symbol table. */ | |
7629 | bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; | |
7630 | BFD_ASSERT (! abfd->output_has_begun); | |
7631 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
7632 | goto error_return; | |
7633 | ||
ee75fd95 | 7634 | /* Set sizes, and assign file positions for reloc sections. */ |
c152c796 AM |
7635 | for (o = abfd->sections; o != NULL; o = o->next) |
7636 | { | |
7637 | if ((o->flags & SEC_RELOC) != 0) | |
7638 | { | |
7639 | if (!(_bfd_elf_link_size_reloc_section | |
7640 | (abfd, &elf_section_data (o)->rel_hdr, o))) | |
7641 | goto error_return; | |
7642 | ||
7643 | if (elf_section_data (o)->rel_hdr2 | |
7644 | && !(_bfd_elf_link_size_reloc_section | |
7645 | (abfd, elf_section_data (o)->rel_hdr2, o))) | |
7646 | goto error_return; | |
7647 | } | |
7648 | ||
7649 | /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them | |
7650 | to count upwards while actually outputting the relocations. */ | |
7651 | elf_section_data (o)->rel_count = 0; | |
7652 | elf_section_data (o)->rel_count2 = 0; | |
7653 | } | |
7654 | ||
7655 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
7656 | ||
7657 | /* We have now assigned file positions for all the sections except | |
7658 | .symtab and .strtab. We start the .symtab section at the current | |
7659 | file position, and write directly to it. We build the .strtab | |
7660 | section in memory. */ | |
7661 | bfd_get_symcount (abfd) = 0; | |
7662 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7663 | /* sh_name is set in prep_headers. */ | |
7664 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7665 | /* sh_flags, sh_addr and sh_size all start off zero. */ | |
7666 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7667 | /* sh_link is set in assign_section_numbers. */ | |
7668 | /* sh_info is set below. */ | |
7669 | /* sh_offset is set just below. */ | |
7670 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; | |
7671 | ||
7672 | off = elf_tdata (abfd)->next_file_pos; | |
7673 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); | |
7674 | ||
7675 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
7676 | incorrect. We do not yet know the size of the .symtab section. | |
7677 | We correct next_file_pos below, after we do know the size. */ | |
7678 | ||
7679 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
7680 | continuously seeking to the right position in the file. */ | |
7681 | if (! info->keep_memory || max_sym_count < 20) | |
7682 | finfo.symbuf_size = 20; | |
7683 | else | |
7684 | finfo.symbuf_size = max_sym_count; | |
7685 | amt = finfo.symbuf_size; | |
7686 | amt *= bed->s->sizeof_sym; | |
7687 | finfo.symbuf = bfd_malloc (amt); | |
7688 | if (finfo.symbuf == NULL) | |
7689 | goto error_return; | |
7690 | if (elf_numsections (abfd) > SHN_LORESERVE) | |
7691 | { | |
7692 | /* Wild guess at number of output symbols. realloc'd as needed. */ | |
7693 | amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; | |
7694 | finfo.shndxbuf_size = amt; | |
7695 | amt *= sizeof (Elf_External_Sym_Shndx); | |
7696 | finfo.symshndxbuf = bfd_zmalloc (amt); | |
7697 | if (finfo.symshndxbuf == NULL) | |
7698 | goto error_return; | |
7699 | } | |
7700 | ||
7701 | /* Start writing out the symbol table. The first symbol is always a | |
7702 | dummy symbol. */ | |
7703 | if (info->strip != strip_all | |
7704 | || emit_relocs) | |
7705 | { | |
7706 | elfsym.st_value = 0; | |
7707 | elfsym.st_size = 0; | |
7708 | elfsym.st_info = 0; | |
7709 | elfsym.st_other = 0; | |
7710 | elfsym.st_shndx = SHN_UNDEF; | |
7711 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, | |
7712 | NULL)) | |
7713 | goto error_return; | |
7714 | } | |
7715 | ||
7716 | #if 0 | |
7717 | /* Some standard ELF linkers do this, but we don't because it causes | |
7718 | bootstrap comparison failures. */ | |
7719 | /* Output a file symbol for the output file as the second symbol. | |
7720 | We output this even if we are discarding local symbols, although | |
7721 | I'm not sure if this is correct. */ | |
7722 | elfsym.st_value = 0; | |
7723 | elfsym.st_size = 0; | |
7724 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7725 | elfsym.st_other = 0; | |
7726 | elfsym.st_shndx = SHN_ABS; | |
7727 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
7728 | &elfsym, bfd_abs_section_ptr, NULL)) | |
7729 | goto error_return; | |
7730 | #endif | |
7731 | ||
7732 | /* Output a symbol for each section. We output these even if we are | |
7733 | discarding local symbols, since they are used for relocs. These | |
7734 | symbols have no names. We store the index of each one in the | |
7735 | index field of the section, so that we can find it again when | |
7736 | outputting relocs. */ | |
7737 | if (info->strip != strip_all | |
7738 | || emit_relocs) | |
7739 | { | |
7740 | elfsym.st_size = 0; | |
7741 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7742 | elfsym.st_other = 0; | |
7743 | for (i = 1; i < elf_numsections (abfd); i++) | |
7744 | { | |
7745 | o = bfd_section_from_elf_index (abfd, i); | |
7746 | if (o != NULL) | |
7747 | o->target_index = bfd_get_symcount (abfd); | |
7748 | elfsym.st_shndx = i; | |
7749 | if (info->relocatable || o == NULL) | |
7750 | elfsym.st_value = 0; | |
7751 | else | |
7752 | elfsym.st_value = o->vma; | |
7753 | if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) | |
7754 | goto error_return; | |
7755 | if (i == SHN_LORESERVE - 1) | |
7756 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
7757 | } | |
7758 | } | |
7759 | ||
7760 | /* Allocate some memory to hold information read in from the input | |
7761 | files. */ | |
7762 | if (max_contents_size != 0) | |
7763 | { | |
7764 | finfo.contents = bfd_malloc (max_contents_size); | |
7765 | if (finfo.contents == NULL) | |
7766 | goto error_return; | |
7767 | } | |
7768 | ||
7769 | if (max_external_reloc_size != 0) | |
7770 | { | |
7771 | finfo.external_relocs = bfd_malloc (max_external_reloc_size); | |
7772 | if (finfo.external_relocs == NULL) | |
7773 | goto error_return; | |
7774 | } | |
7775 | ||
7776 | if (max_internal_reloc_count != 0) | |
7777 | { | |
7778 | amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; | |
7779 | amt *= sizeof (Elf_Internal_Rela); | |
7780 | finfo.internal_relocs = bfd_malloc (amt); | |
7781 | if (finfo.internal_relocs == NULL) | |
7782 | goto error_return; | |
7783 | } | |
7784 | ||
7785 | if (max_sym_count != 0) | |
7786 | { | |
7787 | amt = max_sym_count * bed->s->sizeof_sym; | |
7788 | finfo.external_syms = bfd_malloc (amt); | |
7789 | if (finfo.external_syms == NULL) | |
7790 | goto error_return; | |
7791 | ||
7792 | amt = max_sym_count * sizeof (Elf_Internal_Sym); | |
7793 | finfo.internal_syms = bfd_malloc (amt); | |
7794 | if (finfo.internal_syms == NULL) | |
7795 | goto error_return; | |
7796 | ||
7797 | amt = max_sym_count * sizeof (long); | |
7798 | finfo.indices = bfd_malloc (amt); | |
7799 | if (finfo.indices == NULL) | |
7800 | goto error_return; | |
7801 | ||
7802 | amt = max_sym_count * sizeof (asection *); | |
7803 | finfo.sections = bfd_malloc (amt); | |
7804 | if (finfo.sections == NULL) | |
7805 | goto error_return; | |
7806 | } | |
7807 | ||
7808 | if (max_sym_shndx_count != 0) | |
7809 | { | |
7810 | amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); | |
7811 | finfo.locsym_shndx = bfd_malloc (amt); | |
7812 | if (finfo.locsym_shndx == NULL) | |
7813 | goto error_return; | |
7814 | } | |
7815 | ||
7816 | if (elf_hash_table (info)->tls_sec) | |
7817 | { | |
7818 | bfd_vma base, end = 0; | |
7819 | asection *sec; | |
7820 | ||
7821 | for (sec = elf_hash_table (info)->tls_sec; | |
7822 | sec && (sec->flags & SEC_THREAD_LOCAL); | |
7823 | sec = sec->next) | |
7824 | { | |
eea6121a | 7825 | bfd_vma size = sec->size; |
c152c796 AM |
7826 | |
7827 | if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
7828 | { | |
7829 | struct bfd_link_order *o; | |
7830 | ||
7831 | for (o = sec->link_order_head; o != NULL; o = o->next) | |
7832 | if (size < o->offset + o->size) | |
7833 | size = o->offset + o->size; | |
7834 | } | |
7835 | end = sec->vma + size; | |
7836 | } | |
7837 | base = elf_hash_table (info)->tls_sec->vma; | |
7838 | end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); | |
7839 | elf_hash_table (info)->tls_size = end - base; | |
7840 | } | |
7841 | ||
0b52efa6 PB |
7842 | /* Reorder SHF_LINK_ORDER sections. */ |
7843 | for (o = abfd->sections; o != NULL; o = o->next) | |
7844 | { | |
7845 | if (!elf_fixup_link_order (abfd, o)) | |
7846 | return FALSE; | |
7847 | } | |
7848 | ||
c152c796 AM |
7849 | /* Since ELF permits relocations to be against local symbols, we |
7850 | must have the local symbols available when we do the relocations. | |
7851 | Since we would rather only read the local symbols once, and we | |
7852 | would rather not keep them in memory, we handle all the | |
7853 | relocations for a single input file at the same time. | |
7854 | ||
7855 | Unfortunately, there is no way to know the total number of local | |
7856 | symbols until we have seen all of them, and the local symbol | |
7857 | indices precede the global symbol indices. This means that when | |
7858 | we are generating relocatable output, and we see a reloc against | |
7859 | a global symbol, we can not know the symbol index until we have | |
7860 | finished examining all the local symbols to see which ones we are | |
7861 | going to output. To deal with this, we keep the relocations in | |
7862 | memory, and don't output them until the end of the link. This is | |
7863 | an unfortunate waste of memory, but I don't see a good way around | |
7864 | it. Fortunately, it only happens when performing a relocatable | |
7865 | link, which is not the common case. FIXME: If keep_memory is set | |
7866 | we could write the relocs out and then read them again; I don't | |
7867 | know how bad the memory loss will be. */ | |
7868 | ||
7869 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
7870 | sub->output_has_begun = FALSE; | |
7871 | for (o = abfd->sections; o != NULL; o = o->next) | |
7872 | { | |
7873 | for (p = o->link_order_head; p != NULL; p = p->next) | |
7874 | { | |
7875 | if (p->type == bfd_indirect_link_order | |
7876 | && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) | |
7877 | == bfd_target_elf_flavour) | |
7878 | && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) | |
7879 | { | |
7880 | if (! sub->output_has_begun) | |
7881 | { | |
7882 | if (! elf_link_input_bfd (&finfo, sub)) | |
7883 | goto error_return; | |
7884 | sub->output_has_begun = TRUE; | |
7885 | } | |
7886 | } | |
7887 | else if (p->type == bfd_section_reloc_link_order | |
7888 | || p->type == bfd_symbol_reloc_link_order) | |
7889 | { | |
7890 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
7891 | goto error_return; | |
7892 | } | |
7893 | else | |
7894 | { | |
7895 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
7896 | goto error_return; | |
7897 | } | |
7898 | } | |
7899 | } | |
7900 | ||
7901 | /* Output any global symbols that got converted to local in a | |
7902 | version script or due to symbol visibility. We do this in a | |
7903 | separate step since ELF requires all local symbols to appear | |
7904 | prior to any global symbols. FIXME: We should only do this if | |
7905 | some global symbols were, in fact, converted to become local. | |
7906 | FIXME: Will this work correctly with the Irix 5 linker? */ | |
7907 | eoinfo.failed = FALSE; | |
7908 | eoinfo.finfo = &finfo; | |
7909 | eoinfo.localsyms = TRUE; | |
7910 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
7911 | &eoinfo); | |
7912 | if (eoinfo.failed) | |
7913 | return FALSE; | |
7914 | ||
7915 | /* That wrote out all the local symbols. Finish up the symbol table | |
7916 | with the global symbols. Even if we want to strip everything we | |
7917 | can, we still need to deal with those global symbols that got | |
7918 | converted to local in a version script. */ | |
7919 | ||
7920 | /* The sh_info field records the index of the first non local symbol. */ | |
7921 | symtab_hdr->sh_info = bfd_get_symcount (abfd); | |
7922 | ||
7923 | if (dynamic | |
7924 | && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) | |
7925 | { | |
7926 | Elf_Internal_Sym sym; | |
7927 | bfd_byte *dynsym = finfo.dynsym_sec->contents; | |
7928 | long last_local = 0; | |
7929 | ||
7930 | /* Write out the section symbols for the output sections. */ | |
7931 | if (info->shared) | |
7932 | { | |
7933 | asection *s; | |
7934 | ||
7935 | sym.st_size = 0; | |
7936 | sym.st_name = 0; | |
7937 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7938 | sym.st_other = 0; | |
7939 | ||
7940 | for (s = abfd->sections; s != NULL; s = s->next) | |
7941 | { | |
7942 | int indx; | |
7943 | bfd_byte *dest; | |
7944 | long dynindx; | |
7945 | ||
c152c796 | 7946 | dynindx = elf_section_data (s)->dynindx; |
8c37241b JJ |
7947 | if (dynindx <= 0) |
7948 | continue; | |
7949 | indx = elf_section_data (s)->this_idx; | |
c152c796 AM |
7950 | BFD_ASSERT (indx > 0); |
7951 | sym.st_shndx = indx; | |
7952 | sym.st_value = s->vma; | |
7953 | dest = dynsym + dynindx * bed->s->sizeof_sym; | |
8c37241b JJ |
7954 | if (last_local < dynindx) |
7955 | last_local = dynindx; | |
c152c796 AM |
7956 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); |
7957 | } | |
c152c796 AM |
7958 | } |
7959 | ||
7960 | /* Write out the local dynsyms. */ | |
7961 | if (elf_hash_table (info)->dynlocal) | |
7962 | { | |
7963 | struct elf_link_local_dynamic_entry *e; | |
7964 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
7965 | { | |
7966 | asection *s; | |
7967 | bfd_byte *dest; | |
7968 | ||
7969 | sym.st_size = e->isym.st_size; | |
7970 | sym.st_other = e->isym.st_other; | |
7971 | ||
7972 | /* Copy the internal symbol as is. | |
7973 | Note that we saved a word of storage and overwrote | |
7974 | the original st_name with the dynstr_index. */ | |
7975 | sym = e->isym; | |
7976 | ||
7977 | if (e->isym.st_shndx != SHN_UNDEF | |
7978 | && (e->isym.st_shndx < SHN_LORESERVE | |
7979 | || e->isym.st_shndx > SHN_HIRESERVE)) | |
7980 | { | |
7981 | s = bfd_section_from_elf_index (e->input_bfd, | |
7982 | e->isym.st_shndx); | |
7983 | ||
7984 | sym.st_shndx = | |
7985 | elf_section_data (s->output_section)->this_idx; | |
7986 | sym.st_value = (s->output_section->vma | |
7987 | + s->output_offset | |
7988 | + e->isym.st_value); | |
7989 | } | |
7990 | ||
7991 | if (last_local < e->dynindx) | |
7992 | last_local = e->dynindx; | |
7993 | ||
7994 | dest = dynsym + e->dynindx * bed->s->sizeof_sym; | |
7995 | bed->s->swap_symbol_out (abfd, &sym, dest, 0); | |
7996 | } | |
7997 | } | |
7998 | ||
7999 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = | |
8000 | last_local + 1; | |
8001 | } | |
8002 | ||
8003 | /* We get the global symbols from the hash table. */ | |
8004 | eoinfo.failed = FALSE; | |
8005 | eoinfo.localsyms = FALSE; | |
8006 | eoinfo.finfo = &finfo; | |
8007 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
8008 | &eoinfo); | |
8009 | if (eoinfo.failed) | |
8010 | return FALSE; | |
8011 | ||
8012 | /* If backend needs to output some symbols not present in the hash | |
8013 | table, do it now. */ | |
8014 | if (bed->elf_backend_output_arch_syms) | |
8015 | { | |
8016 | typedef bfd_boolean (*out_sym_func) | |
8017 | (void *, const char *, Elf_Internal_Sym *, asection *, | |
8018 | struct elf_link_hash_entry *); | |
8019 | ||
8020 | if (! ((*bed->elf_backend_output_arch_syms) | |
8021 | (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) | |
8022 | return FALSE; | |
8023 | } | |
8024 | ||
8025 | /* Flush all symbols to the file. */ | |
8026 | if (! elf_link_flush_output_syms (&finfo, bed)) | |
8027 | return FALSE; | |
8028 | ||
8029 | /* Now we know the size of the symtab section. */ | |
8030 | off += symtab_hdr->sh_size; | |
8031 | ||
8032 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
8033 | if (symtab_shndx_hdr->sh_name != 0) | |
8034 | { | |
8035 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8036 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8037 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8038 | amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); | |
8039 | symtab_shndx_hdr->sh_size = amt; | |
8040 | ||
8041 | off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, | |
8042 | off, TRUE); | |
8043 | ||
8044 | if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 | |
8045 | || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) | |
8046 | return FALSE; | |
8047 | } | |
8048 | ||
8049 | ||
8050 | /* Finish up and write out the symbol string table (.strtab) | |
8051 | section. */ | |
8052 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8053 | /* sh_name was set in prep_headers. */ | |
8054 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8055 | symstrtab_hdr->sh_flags = 0; | |
8056 | symstrtab_hdr->sh_addr = 0; | |
8057 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
8058 | symstrtab_hdr->sh_entsize = 0; | |
8059 | symstrtab_hdr->sh_link = 0; | |
8060 | symstrtab_hdr->sh_info = 0; | |
8061 | /* sh_offset is set just below. */ | |
8062 | symstrtab_hdr->sh_addralign = 1; | |
8063 | ||
8064 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); | |
8065 | elf_tdata (abfd)->next_file_pos = off; | |
8066 | ||
8067 | if (bfd_get_symcount (abfd) > 0) | |
8068 | { | |
8069 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
8070 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
8071 | return FALSE; | |
8072 | } | |
8073 | ||
8074 | /* Adjust the relocs to have the correct symbol indices. */ | |
8075 | for (o = abfd->sections; o != NULL; o = o->next) | |
8076 | { | |
8077 | if ((o->flags & SEC_RELOC) == 0) | |
8078 | continue; | |
8079 | ||
8080 | elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, | |
8081 | elf_section_data (o)->rel_count, | |
8082 | elf_section_data (o)->rel_hashes); | |
8083 | if (elf_section_data (o)->rel_hdr2 != NULL) | |
8084 | elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, | |
8085 | elf_section_data (o)->rel_count2, | |
8086 | (elf_section_data (o)->rel_hashes | |
8087 | + elf_section_data (o)->rel_count)); | |
8088 | ||
8089 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
8090 | trying to swap the relocs out itself. */ | |
8091 | o->reloc_count = 0; | |
8092 | } | |
8093 | ||
8094 | if (dynamic && info->combreloc && dynobj != NULL) | |
8095 | relativecount = elf_link_sort_relocs (abfd, info, &reldyn); | |
8096 | ||
8097 | /* If we are linking against a dynamic object, or generating a | |
8098 | shared library, finish up the dynamic linking information. */ | |
8099 | if (dynamic) | |
8100 | { | |
8101 | bfd_byte *dyncon, *dynconend; | |
8102 | ||
8103 | /* Fix up .dynamic entries. */ | |
8104 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
8105 | BFD_ASSERT (o != NULL); | |
8106 | ||
8107 | dyncon = o->contents; | |
eea6121a | 8108 | dynconend = o->contents + o->size; |
c152c796 AM |
8109 | for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) |
8110 | { | |
8111 | Elf_Internal_Dyn dyn; | |
8112 | const char *name; | |
8113 | unsigned int type; | |
8114 | ||
8115 | bed->s->swap_dyn_in (dynobj, dyncon, &dyn); | |
8116 | ||
8117 | switch (dyn.d_tag) | |
8118 | { | |
8119 | default: | |
8120 | continue; | |
8121 | case DT_NULL: | |
8122 | if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) | |
8123 | { | |
8124 | switch (elf_section_data (reldyn)->this_hdr.sh_type) | |
8125 | { | |
8126 | case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; | |
8127 | case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; | |
8128 | default: continue; | |
8129 | } | |
8130 | dyn.d_un.d_val = relativecount; | |
8131 | relativecount = 0; | |
8132 | break; | |
8133 | } | |
8134 | continue; | |
8135 | ||
8136 | case DT_INIT: | |
8137 | name = info->init_function; | |
8138 | goto get_sym; | |
8139 | case DT_FINI: | |
8140 | name = info->fini_function; | |
8141 | get_sym: | |
8142 | { | |
8143 | struct elf_link_hash_entry *h; | |
8144 | ||
8145 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
8146 | FALSE, FALSE, TRUE); | |
8147 | if (h != NULL | |
8148 | && (h->root.type == bfd_link_hash_defined | |
8149 | || h->root.type == bfd_link_hash_defweak)) | |
8150 | { | |
8151 | dyn.d_un.d_val = h->root.u.def.value; | |
8152 | o = h->root.u.def.section; | |
8153 | if (o->output_section != NULL) | |
8154 | dyn.d_un.d_val += (o->output_section->vma | |
8155 | + o->output_offset); | |
8156 | else | |
8157 | { | |
8158 | /* The symbol is imported from another shared | |
8159 | library and does not apply to this one. */ | |
8160 | dyn.d_un.d_val = 0; | |
8161 | } | |
8162 | break; | |
8163 | } | |
8164 | } | |
8165 | continue; | |
8166 | ||
8167 | case DT_PREINIT_ARRAYSZ: | |
8168 | name = ".preinit_array"; | |
8169 | goto get_size; | |
8170 | case DT_INIT_ARRAYSZ: | |
8171 | name = ".init_array"; | |
8172 | goto get_size; | |
8173 | case DT_FINI_ARRAYSZ: | |
8174 | name = ".fini_array"; | |
8175 | get_size: | |
8176 | o = bfd_get_section_by_name (abfd, name); | |
8177 | if (o == NULL) | |
8178 | { | |
8179 | (*_bfd_error_handler) | |
d003868e | 8180 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8181 | goto error_return; |
8182 | } | |
eea6121a | 8183 | if (o->size == 0) |
c152c796 AM |
8184 | (*_bfd_error_handler) |
8185 | (_("warning: %s section has zero size"), name); | |
eea6121a | 8186 | dyn.d_un.d_val = o->size; |
c152c796 AM |
8187 | break; |
8188 | ||
8189 | case DT_PREINIT_ARRAY: | |
8190 | name = ".preinit_array"; | |
8191 | goto get_vma; | |
8192 | case DT_INIT_ARRAY: | |
8193 | name = ".init_array"; | |
8194 | goto get_vma; | |
8195 | case DT_FINI_ARRAY: | |
8196 | name = ".fini_array"; | |
8197 | goto get_vma; | |
8198 | ||
8199 | case DT_HASH: | |
8200 | name = ".hash"; | |
8201 | goto get_vma; | |
8202 | case DT_STRTAB: | |
8203 | name = ".dynstr"; | |
8204 | goto get_vma; | |
8205 | case DT_SYMTAB: | |
8206 | name = ".dynsym"; | |
8207 | goto get_vma; | |
8208 | case DT_VERDEF: | |
8209 | name = ".gnu.version_d"; | |
8210 | goto get_vma; | |
8211 | case DT_VERNEED: | |
8212 | name = ".gnu.version_r"; | |
8213 | goto get_vma; | |
8214 | case DT_VERSYM: | |
8215 | name = ".gnu.version"; | |
8216 | get_vma: | |
8217 | o = bfd_get_section_by_name (abfd, name); | |
8218 | if (o == NULL) | |
8219 | { | |
8220 | (*_bfd_error_handler) | |
d003868e | 8221 | (_("%B: could not find output section %s"), abfd, name); |
c152c796 AM |
8222 | goto error_return; |
8223 | } | |
8224 | dyn.d_un.d_ptr = o->vma; | |
8225 | break; | |
8226 | ||
8227 | case DT_REL: | |
8228 | case DT_RELA: | |
8229 | case DT_RELSZ: | |
8230 | case DT_RELASZ: | |
8231 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
8232 | type = SHT_REL; | |
8233 | else | |
8234 | type = SHT_RELA; | |
8235 | dyn.d_un.d_val = 0; | |
8236 | for (i = 1; i < elf_numsections (abfd); i++) | |
8237 | { | |
8238 | Elf_Internal_Shdr *hdr; | |
8239 | ||
8240 | hdr = elf_elfsections (abfd)[i]; | |
8241 | if (hdr->sh_type == type | |
8242 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
8243 | { | |
8244 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
8245 | dyn.d_un.d_val += hdr->sh_size; | |
8246 | else | |
8247 | { | |
8248 | if (dyn.d_un.d_val == 0 | |
8249 | || hdr->sh_addr < dyn.d_un.d_val) | |
8250 | dyn.d_un.d_val = hdr->sh_addr; | |
8251 | } | |
8252 | } | |
8253 | } | |
8254 | break; | |
8255 | } | |
8256 | bed->s->swap_dyn_out (dynobj, &dyn, dyncon); | |
8257 | } | |
8258 | } | |
8259 | ||
8260 | /* If we have created any dynamic sections, then output them. */ | |
8261 | if (dynobj != NULL) | |
8262 | { | |
8263 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
8264 | goto error_return; | |
8265 | ||
8266 | for (o = dynobj->sections; o != NULL; o = o->next) | |
8267 | { | |
8268 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
eea6121a | 8269 | || o->size == 0 |
c152c796 AM |
8270 | || o->output_section == bfd_abs_section_ptr) |
8271 | continue; | |
8272 | if ((o->flags & SEC_LINKER_CREATED) == 0) | |
8273 | { | |
8274 | /* At this point, we are only interested in sections | |
8275 | created by _bfd_elf_link_create_dynamic_sections. */ | |
8276 | continue; | |
8277 | } | |
3722b82f AM |
8278 | if (elf_hash_table (info)->stab_info.stabstr == o) |
8279 | continue; | |
eea6121a AM |
8280 | if (elf_hash_table (info)->eh_info.hdr_sec == o) |
8281 | continue; | |
c152c796 AM |
8282 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
8283 | != SHT_STRTAB) | |
8284 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
8285 | { | |
8286 | if (! bfd_set_section_contents (abfd, o->output_section, | |
8287 | o->contents, | |
8288 | (file_ptr) o->output_offset, | |
eea6121a | 8289 | o->size)) |
c152c796 AM |
8290 | goto error_return; |
8291 | } | |
8292 | else | |
8293 | { | |
8294 | /* The contents of the .dynstr section are actually in a | |
8295 | stringtab. */ | |
8296 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
8297 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
8298 | || ! _bfd_elf_strtab_emit (abfd, | |
8299 | elf_hash_table (info)->dynstr)) | |
8300 | goto error_return; | |
8301 | } | |
8302 | } | |
8303 | } | |
8304 | ||
8305 | if (info->relocatable) | |
8306 | { | |
8307 | bfd_boolean failed = FALSE; | |
8308 | ||
8309 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); | |
8310 | if (failed) | |
8311 | goto error_return; | |
8312 | } | |
8313 | ||
8314 | /* If we have optimized stabs strings, output them. */ | |
3722b82f | 8315 | if (elf_hash_table (info)->stab_info.stabstr != NULL) |
c152c796 AM |
8316 | { |
8317 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
8318 | goto error_return; | |
8319 | } | |
8320 | ||
8321 | if (info->eh_frame_hdr) | |
8322 | { | |
8323 | if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) | |
8324 | goto error_return; | |
8325 | } | |
8326 | ||
8327 | if (finfo.symstrtab != NULL) | |
8328 | _bfd_stringtab_free (finfo.symstrtab); | |
8329 | if (finfo.contents != NULL) | |
8330 | free (finfo.contents); | |
8331 | if (finfo.external_relocs != NULL) | |
8332 | free (finfo.external_relocs); | |
8333 | if (finfo.internal_relocs != NULL) | |
8334 | free (finfo.internal_relocs); | |
8335 | if (finfo.external_syms != NULL) | |
8336 | free (finfo.external_syms); | |
8337 | if (finfo.locsym_shndx != NULL) | |
8338 | free (finfo.locsym_shndx); | |
8339 | if (finfo.internal_syms != NULL) | |
8340 | free (finfo.internal_syms); | |
8341 | if (finfo.indices != NULL) | |
8342 | free (finfo.indices); | |
8343 | if (finfo.sections != NULL) | |
8344 | free (finfo.sections); | |
8345 | if (finfo.symbuf != NULL) | |
8346 | free (finfo.symbuf); | |
8347 | if (finfo.symshndxbuf != NULL) | |
8348 | free (finfo.symshndxbuf); | |
8349 | for (o = abfd->sections; o != NULL; o = o->next) | |
8350 | { | |
8351 | if ((o->flags & SEC_RELOC) != 0 | |
8352 | && elf_section_data (o)->rel_hashes != NULL) | |
8353 | free (elf_section_data (o)->rel_hashes); | |
8354 | } | |
8355 | ||
8356 | elf_tdata (abfd)->linker = TRUE; | |
8357 | ||
8358 | return TRUE; | |
8359 | ||
8360 | error_return: | |
8361 | if (finfo.symstrtab != NULL) | |
8362 | _bfd_stringtab_free (finfo.symstrtab); | |
8363 | if (finfo.contents != NULL) | |
8364 | free (finfo.contents); | |
8365 | if (finfo.external_relocs != NULL) | |
8366 | free (finfo.external_relocs); | |
8367 | if (finfo.internal_relocs != NULL) | |
8368 | free (finfo.internal_relocs); | |
8369 | if (finfo.external_syms != NULL) | |
8370 | free (finfo.external_syms); | |
8371 | if (finfo.locsym_shndx != NULL) | |
8372 | free (finfo.locsym_shndx); | |
8373 | if (finfo.internal_syms != NULL) | |
8374 | free (finfo.internal_syms); | |
8375 | if (finfo.indices != NULL) | |
8376 | free (finfo.indices); | |
8377 | if (finfo.sections != NULL) | |
8378 | free (finfo.sections); | |
8379 | if (finfo.symbuf != NULL) | |
8380 | free (finfo.symbuf); | |
8381 | if (finfo.symshndxbuf != NULL) | |
8382 | free (finfo.symshndxbuf); | |
8383 | for (o = abfd->sections; o != NULL; o = o->next) | |
8384 | { | |
8385 | if ((o->flags & SEC_RELOC) != 0 | |
8386 | && elf_section_data (o)->rel_hashes != NULL) | |
8387 | free (elf_section_data (o)->rel_hashes); | |
8388 | } | |
8389 | ||
8390 | return FALSE; | |
8391 | } | |
8392 | \f | |
8393 | /* Garbage collect unused sections. */ | |
8394 | ||
8395 | /* The mark phase of garbage collection. For a given section, mark | |
8396 | it and any sections in this section's group, and all the sections | |
8397 | which define symbols to which it refers. */ | |
8398 | ||
8399 | typedef asection * (*gc_mark_hook_fn) | |
8400 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8401 | struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
8402 | ||
ccfa59ea AM |
8403 | bfd_boolean |
8404 | _bfd_elf_gc_mark (struct bfd_link_info *info, | |
8405 | asection *sec, | |
8406 | gc_mark_hook_fn gc_mark_hook) | |
c152c796 AM |
8407 | { |
8408 | bfd_boolean ret; | |
8409 | asection *group_sec; | |
8410 | ||
8411 | sec->gc_mark = 1; | |
8412 | ||
8413 | /* Mark all the sections in the group. */ | |
8414 | group_sec = elf_section_data (sec)->next_in_group; | |
8415 | if (group_sec && !group_sec->gc_mark) | |
ccfa59ea | 8416 | if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) |
c152c796 AM |
8417 | return FALSE; |
8418 | ||
8419 | /* Look through the section relocs. */ | |
8420 | ret = TRUE; | |
8421 | if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) | |
8422 | { | |
8423 | Elf_Internal_Rela *relstart, *rel, *relend; | |
8424 | Elf_Internal_Shdr *symtab_hdr; | |
8425 | struct elf_link_hash_entry **sym_hashes; | |
8426 | size_t nlocsyms; | |
8427 | size_t extsymoff; | |
8428 | bfd *input_bfd = sec->owner; | |
8429 | const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); | |
8430 | Elf_Internal_Sym *isym = NULL; | |
8431 | int r_sym_shift; | |
8432 | ||
8433 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
8434 | sym_hashes = elf_sym_hashes (input_bfd); | |
8435 | ||
8436 | /* Read the local symbols. */ | |
8437 | if (elf_bad_symtab (input_bfd)) | |
8438 | { | |
8439 | nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
8440 | extsymoff = 0; | |
8441 | } | |
8442 | else | |
8443 | extsymoff = nlocsyms = symtab_hdr->sh_info; | |
8444 | ||
8445 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
8446 | if (isym == NULL && nlocsyms != 0) | |
8447 | { | |
8448 | isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, | |
8449 | NULL, NULL, NULL); | |
8450 | if (isym == NULL) | |
8451 | return FALSE; | |
8452 | } | |
8453 | ||
8454 | /* Read the relocations. */ | |
8455 | relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, | |
8456 | info->keep_memory); | |
8457 | if (relstart == NULL) | |
8458 | { | |
8459 | ret = FALSE; | |
8460 | goto out1; | |
8461 | } | |
8462 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8463 | ||
8464 | if (bed->s->arch_size == 32) | |
8465 | r_sym_shift = 8; | |
8466 | else | |
8467 | r_sym_shift = 32; | |
8468 | ||
8469 | for (rel = relstart; rel < relend; rel++) | |
8470 | { | |
8471 | unsigned long r_symndx; | |
8472 | asection *rsec; | |
8473 | struct elf_link_hash_entry *h; | |
8474 | ||
8475 | r_symndx = rel->r_info >> r_sym_shift; | |
8476 | if (r_symndx == 0) | |
8477 | continue; | |
8478 | ||
8479 | if (r_symndx >= nlocsyms | |
8480 | || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) | |
8481 | { | |
8482 | h = sym_hashes[r_symndx - extsymoff]; | |
20f0a1ad AM |
8483 | while (h->root.type == bfd_link_hash_indirect |
8484 | || h->root.type == bfd_link_hash_warning) | |
8485 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c152c796 AM |
8486 | rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); |
8487 | } | |
8488 | else | |
8489 | { | |
8490 | rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); | |
8491 | } | |
8492 | ||
8493 | if (rsec && !rsec->gc_mark) | |
8494 | { | |
8495 | if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) | |
8496 | rsec->gc_mark = 1; | |
ccfa59ea | 8497 | else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) |
c152c796 AM |
8498 | { |
8499 | ret = FALSE; | |
8500 | goto out2; | |
8501 | } | |
8502 | } | |
8503 | } | |
8504 | ||
8505 | out2: | |
8506 | if (elf_section_data (sec)->relocs != relstart) | |
8507 | free (relstart); | |
8508 | out1: | |
8509 | if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) | |
8510 | { | |
8511 | if (! info->keep_memory) | |
8512 | free (isym); | |
8513 | else | |
8514 | symtab_hdr->contents = (unsigned char *) isym; | |
8515 | } | |
8516 | } | |
8517 | ||
8518 | return ret; | |
8519 | } | |
8520 | ||
8521 | /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ | |
8522 | ||
8523 | static bfd_boolean | |
8524 | elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr) | |
8525 | { | |
8526 | int *idx = idxptr; | |
8527 | ||
8528 | if (h->root.type == bfd_link_hash_warning) | |
8529 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8530 | ||
8531 | if (h->dynindx != -1 | |
8532 | && ((h->root.type != bfd_link_hash_defined | |
8533 | && h->root.type != bfd_link_hash_defweak) | |
8534 | || h->root.u.def.section->gc_mark)) | |
8535 | h->dynindx = (*idx)++; | |
8536 | ||
8537 | return TRUE; | |
8538 | } | |
8539 | ||
8540 | /* The sweep phase of garbage collection. Remove all garbage sections. */ | |
8541 | ||
8542 | typedef bfd_boolean (*gc_sweep_hook_fn) | |
8543 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
8544 | ||
8545 | static bfd_boolean | |
8546 | elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook) | |
8547 | { | |
8548 | bfd *sub; | |
8549 | ||
8550 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8551 | { | |
8552 | asection *o; | |
8553 | ||
8554 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8555 | continue; | |
8556 | ||
8557 | for (o = sub->sections; o != NULL; o = o->next) | |
8558 | { | |
7c2c8505 AM |
8559 | /* Keep debug and special sections. */ |
8560 | if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 | |
8561 | || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0) | |
c152c796 AM |
8562 | o->gc_mark = 1; |
8563 | ||
8564 | if (o->gc_mark) | |
8565 | continue; | |
8566 | ||
8567 | /* Skip sweeping sections already excluded. */ | |
8568 | if (o->flags & SEC_EXCLUDE) | |
8569 | continue; | |
8570 | ||
8571 | /* Since this is early in the link process, it is simple | |
8572 | to remove a section from the output. */ | |
8573 | o->flags |= SEC_EXCLUDE; | |
8574 | ||
8575 | /* But we also have to update some of the relocation | |
8576 | info we collected before. */ | |
8577 | if (gc_sweep_hook | |
8578 | && (o->flags & SEC_RELOC) && o->reloc_count > 0) | |
8579 | { | |
8580 | Elf_Internal_Rela *internal_relocs; | |
8581 | bfd_boolean r; | |
8582 | ||
8583 | internal_relocs | |
8584 | = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, | |
8585 | info->keep_memory); | |
8586 | if (internal_relocs == NULL) | |
8587 | return FALSE; | |
8588 | ||
8589 | r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); | |
8590 | ||
8591 | if (elf_section_data (o)->relocs != internal_relocs) | |
8592 | free (internal_relocs); | |
8593 | ||
8594 | if (!r) | |
8595 | return FALSE; | |
8596 | } | |
8597 | } | |
8598 | } | |
8599 | ||
8600 | /* Remove the symbols that were in the swept sections from the dynamic | |
8601 | symbol table. GCFIXME: Anyone know how to get them out of the | |
8602 | static symbol table as well? */ | |
8603 | { | |
8604 | int i = 0; | |
8605 | ||
8606 | elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i); | |
8607 | ||
8608 | elf_hash_table (info)->dynsymcount = i; | |
8609 | } | |
8610 | ||
8611 | return TRUE; | |
8612 | } | |
8613 | ||
8614 | /* Propagate collected vtable information. This is called through | |
8615 | elf_link_hash_traverse. */ | |
8616 | ||
8617 | static bfd_boolean | |
8618 | elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) | |
8619 | { | |
8620 | if (h->root.type == bfd_link_hash_warning) | |
8621 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8622 | ||
8623 | /* Those that are not vtables. */ | |
f6e332e6 | 8624 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8625 | return TRUE; |
8626 | ||
8627 | /* Those vtables that do not have parents, we cannot merge. */ | |
f6e332e6 | 8628 | if (h->vtable->parent == (struct elf_link_hash_entry *) -1) |
c152c796 AM |
8629 | return TRUE; |
8630 | ||
8631 | /* If we've already been done, exit. */ | |
f6e332e6 | 8632 | if (h->vtable->used && h->vtable->used[-1]) |
c152c796 AM |
8633 | return TRUE; |
8634 | ||
8635 | /* Make sure the parent's table is up to date. */ | |
f6e332e6 | 8636 | elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); |
c152c796 | 8637 | |
f6e332e6 | 8638 | if (h->vtable->used == NULL) |
c152c796 AM |
8639 | { |
8640 | /* None of this table's entries were referenced. Re-use the | |
8641 | parent's table. */ | |
f6e332e6 AM |
8642 | h->vtable->used = h->vtable->parent->vtable->used; |
8643 | h->vtable->size = h->vtable->parent->vtable->size; | |
c152c796 AM |
8644 | } |
8645 | else | |
8646 | { | |
8647 | size_t n; | |
8648 | bfd_boolean *cu, *pu; | |
8649 | ||
8650 | /* Or the parent's entries into ours. */ | |
f6e332e6 | 8651 | cu = h->vtable->used; |
c152c796 | 8652 | cu[-1] = TRUE; |
f6e332e6 | 8653 | pu = h->vtable->parent->vtable->used; |
c152c796 AM |
8654 | if (pu != NULL) |
8655 | { | |
8656 | const struct elf_backend_data *bed; | |
8657 | unsigned int log_file_align; | |
8658 | ||
8659 | bed = get_elf_backend_data (h->root.u.def.section->owner); | |
8660 | log_file_align = bed->s->log_file_align; | |
f6e332e6 | 8661 | n = h->vtable->parent->vtable->size >> log_file_align; |
c152c796 AM |
8662 | while (n--) |
8663 | { | |
8664 | if (*pu) | |
8665 | *cu = TRUE; | |
8666 | pu++; | |
8667 | cu++; | |
8668 | } | |
8669 | } | |
8670 | } | |
8671 | ||
8672 | return TRUE; | |
8673 | } | |
8674 | ||
8675 | static bfd_boolean | |
8676 | elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) | |
8677 | { | |
8678 | asection *sec; | |
8679 | bfd_vma hstart, hend; | |
8680 | Elf_Internal_Rela *relstart, *relend, *rel; | |
8681 | const struct elf_backend_data *bed; | |
8682 | unsigned int log_file_align; | |
8683 | ||
8684 | if (h->root.type == bfd_link_hash_warning) | |
8685 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8686 | ||
8687 | /* Take care of both those symbols that do not describe vtables as | |
8688 | well as those that are not loaded. */ | |
f6e332e6 | 8689 | if (h->vtable == NULL || h->vtable->parent == NULL) |
c152c796 AM |
8690 | return TRUE; |
8691 | ||
8692 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
8693 | || h->root.type == bfd_link_hash_defweak); | |
8694 | ||
8695 | sec = h->root.u.def.section; | |
8696 | hstart = h->root.u.def.value; | |
8697 | hend = hstart + h->size; | |
8698 | ||
8699 | relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); | |
8700 | if (!relstart) | |
8701 | return *(bfd_boolean *) okp = FALSE; | |
8702 | bed = get_elf_backend_data (sec->owner); | |
8703 | log_file_align = bed->s->log_file_align; | |
8704 | ||
8705 | relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; | |
8706 | ||
8707 | for (rel = relstart; rel < relend; ++rel) | |
8708 | if (rel->r_offset >= hstart && rel->r_offset < hend) | |
8709 | { | |
8710 | /* If the entry is in use, do nothing. */ | |
f6e332e6 AM |
8711 | if (h->vtable->used |
8712 | && (rel->r_offset - hstart) < h->vtable->size) | |
c152c796 AM |
8713 | { |
8714 | bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; | |
f6e332e6 | 8715 | if (h->vtable->used[entry]) |
c152c796 AM |
8716 | continue; |
8717 | } | |
8718 | /* Otherwise, kill it. */ | |
8719 | rel->r_offset = rel->r_info = rel->r_addend = 0; | |
8720 | } | |
8721 | ||
8722 | return TRUE; | |
8723 | } | |
8724 | ||
715df9b8 EB |
8725 | /* Mark sections containing dynamically referenced symbols. This is called |
8726 | through elf_link_hash_traverse. */ | |
8727 | ||
8728 | static bfd_boolean | |
8729 | elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, | |
8730 | void *okp ATTRIBUTE_UNUSED) | |
8731 | { | |
8732 | if (h->root.type == bfd_link_hash_warning) | |
8733 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8734 | ||
8735 | if ((h->root.type == bfd_link_hash_defined | |
8736 | || h->root.type == bfd_link_hash_defweak) | |
f5385ebf | 8737 | && h->ref_dynamic) |
715df9b8 EB |
8738 | h->root.u.def.section->flags |= SEC_KEEP; |
8739 | ||
8740 | return TRUE; | |
8741 | } | |
8742 | ||
c152c796 AM |
8743 | /* Do mark and sweep of unused sections. */ |
8744 | ||
8745 | bfd_boolean | |
8746 | bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) | |
8747 | { | |
8748 | bfd_boolean ok = TRUE; | |
8749 | bfd *sub; | |
8750 | asection * (*gc_mark_hook) | |
8751 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, | |
8752 | struct elf_link_hash_entry *h, Elf_Internal_Sym *); | |
8753 | ||
8754 | if (!get_elf_backend_data (abfd)->can_gc_sections | |
8755 | || info->relocatable | |
8756 | || info->emitrelocations | |
715df9b8 EB |
8757 | || info->shared |
8758 | || !is_elf_hash_table (info->hash)) | |
c152c796 AM |
8759 | { |
8760 | (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); | |
8761 | return TRUE; | |
8762 | } | |
8763 | ||
8764 | /* Apply transitive closure to the vtable entry usage info. */ | |
8765 | elf_link_hash_traverse (elf_hash_table (info), | |
8766 | elf_gc_propagate_vtable_entries_used, | |
8767 | &ok); | |
8768 | if (!ok) | |
8769 | return FALSE; | |
8770 | ||
8771 | /* Kill the vtable relocations that were not used. */ | |
8772 | elf_link_hash_traverse (elf_hash_table (info), | |
8773 | elf_gc_smash_unused_vtentry_relocs, | |
8774 | &ok); | |
8775 | if (!ok) | |
8776 | return FALSE; | |
8777 | ||
715df9b8 EB |
8778 | /* Mark dynamically referenced symbols. */ |
8779 | if (elf_hash_table (info)->dynamic_sections_created) | |
8780 | elf_link_hash_traverse (elf_hash_table (info), | |
8781 | elf_gc_mark_dynamic_ref_symbol, | |
8782 | &ok); | |
8783 | if (!ok) | |
8784 | return FALSE; | |
c152c796 | 8785 | |
715df9b8 | 8786 | /* Grovel through relocs to find out who stays ... */ |
c152c796 AM |
8787 | gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; |
8788 | for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) | |
8789 | { | |
8790 | asection *o; | |
8791 | ||
8792 | if (bfd_get_flavour (sub) != bfd_target_elf_flavour) | |
8793 | continue; | |
8794 | ||
8795 | for (o = sub->sections; o != NULL; o = o->next) | |
8796 | { | |
8797 | if (o->flags & SEC_KEEP) | |
715df9b8 EB |
8798 | { |
8799 | /* _bfd_elf_discard_section_eh_frame knows how to discard | |
8800 | orphaned FDEs so don't mark sections referenced by the | |
8801 | EH frame section. */ | |
8802 | if (strcmp (o->name, ".eh_frame") == 0) | |
8803 | o->gc_mark = 1; | |
ccfa59ea | 8804 | else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) |
715df9b8 EB |
8805 | return FALSE; |
8806 | } | |
c152c796 AM |
8807 | } |
8808 | } | |
8809 | ||
8810 | /* ... and mark SEC_EXCLUDE for those that go. */ | |
8811 | if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook)) | |
8812 | return FALSE; | |
8813 | ||
8814 | return TRUE; | |
8815 | } | |
8816 | \f | |
8817 | /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ | |
8818 | ||
8819 | bfd_boolean | |
8820 | bfd_elf_gc_record_vtinherit (bfd *abfd, | |
8821 | asection *sec, | |
8822 | struct elf_link_hash_entry *h, | |
8823 | bfd_vma offset) | |
8824 | { | |
8825 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
8826 | struct elf_link_hash_entry **search, *child; | |
8827 | bfd_size_type extsymcount; | |
8828 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8829 | ||
8830 | /* The sh_info field of the symtab header tells us where the | |
8831 | external symbols start. We don't care about the local symbols at | |
8832 | this point. */ | |
8833 | extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; | |
8834 | if (!elf_bad_symtab (abfd)) | |
8835 | extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; | |
8836 | ||
8837 | sym_hashes = elf_sym_hashes (abfd); | |
8838 | sym_hashes_end = sym_hashes + extsymcount; | |
8839 | ||
8840 | /* Hunt down the child symbol, which is in this section at the same | |
8841 | offset as the relocation. */ | |
8842 | for (search = sym_hashes; search != sym_hashes_end; ++search) | |
8843 | { | |
8844 | if ((child = *search) != NULL | |
8845 | && (child->root.type == bfd_link_hash_defined | |
8846 | || child->root.type == bfd_link_hash_defweak) | |
8847 | && child->root.u.def.section == sec | |
8848 | && child->root.u.def.value == offset) | |
8849 | goto win; | |
8850 | } | |
8851 | ||
d003868e AM |
8852 | (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", |
8853 | abfd, sec, (unsigned long) offset); | |
c152c796 AM |
8854 | bfd_set_error (bfd_error_invalid_operation); |
8855 | return FALSE; | |
8856 | ||
8857 | win: | |
f6e332e6 AM |
8858 | if (!child->vtable) |
8859 | { | |
8860 | child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); | |
8861 | if (!child->vtable) | |
8862 | return FALSE; | |
8863 | } | |
c152c796 AM |
8864 | if (!h) |
8865 | { | |
8866 | /* This *should* only be the absolute section. It could potentially | |
8867 | be that someone has defined a non-global vtable though, which | |
8868 | would be bad. It isn't worth paging in the local symbols to be | |
8869 | sure though; that case should simply be handled by the assembler. */ | |
8870 | ||
f6e332e6 | 8871 | child->vtable->parent = (struct elf_link_hash_entry *) -1; |
c152c796 AM |
8872 | } |
8873 | else | |
f6e332e6 | 8874 | child->vtable->parent = h; |
c152c796 AM |
8875 | |
8876 | return TRUE; | |
8877 | } | |
8878 | ||
8879 | /* Called from check_relocs to record the existence of a VTENTRY reloc. */ | |
8880 | ||
8881 | bfd_boolean | |
8882 | bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, | |
8883 | asection *sec ATTRIBUTE_UNUSED, | |
8884 | struct elf_link_hash_entry *h, | |
8885 | bfd_vma addend) | |
8886 | { | |
8887 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8888 | unsigned int log_file_align = bed->s->log_file_align; | |
8889 | ||
f6e332e6 AM |
8890 | if (!h->vtable) |
8891 | { | |
8892 | h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); | |
8893 | if (!h->vtable) | |
8894 | return FALSE; | |
8895 | } | |
8896 | ||
8897 | if (addend >= h->vtable->size) | |
c152c796 AM |
8898 | { |
8899 | size_t size, bytes, file_align; | |
f6e332e6 | 8900 | bfd_boolean *ptr = h->vtable->used; |
c152c796 AM |
8901 | |
8902 | /* While the symbol is undefined, we have to be prepared to handle | |
8903 | a zero size. */ | |
8904 | file_align = 1 << log_file_align; | |
8905 | if (h->root.type == bfd_link_hash_undefined) | |
8906 | size = addend + file_align; | |
8907 | else | |
8908 | { | |
8909 | size = h->size; | |
8910 | if (addend >= size) | |
8911 | { | |
8912 | /* Oops! We've got a reference past the defined end of | |
8913 | the table. This is probably a bug -- shall we warn? */ | |
8914 | size = addend + file_align; | |
8915 | } | |
8916 | } | |
8917 | size = (size + file_align - 1) & -file_align; | |
8918 | ||
8919 | /* Allocate one extra entry for use as a "done" flag for the | |
8920 | consolidation pass. */ | |
8921 | bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); | |
8922 | ||
8923 | if (ptr) | |
8924 | { | |
8925 | ptr = bfd_realloc (ptr - 1, bytes); | |
8926 | ||
8927 | if (ptr != NULL) | |
8928 | { | |
8929 | size_t oldbytes; | |
8930 | ||
f6e332e6 | 8931 | oldbytes = (((h->vtable->size >> log_file_align) + 1) |
c152c796 AM |
8932 | * sizeof (bfd_boolean)); |
8933 | memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); | |
8934 | } | |
8935 | } | |
8936 | else | |
8937 | ptr = bfd_zmalloc (bytes); | |
8938 | ||
8939 | if (ptr == NULL) | |
8940 | return FALSE; | |
8941 | ||
8942 | /* And arrange for that done flag to be at index -1. */ | |
f6e332e6 AM |
8943 | h->vtable->used = ptr + 1; |
8944 | h->vtable->size = size; | |
c152c796 AM |
8945 | } |
8946 | ||
f6e332e6 | 8947 | h->vtable->used[addend >> log_file_align] = TRUE; |
c152c796 AM |
8948 | |
8949 | return TRUE; | |
8950 | } | |
8951 | ||
8952 | struct alloc_got_off_arg { | |
8953 | bfd_vma gotoff; | |
8954 | unsigned int got_elt_size; | |
8955 | }; | |
8956 | ||
8957 | /* We need a special top-level link routine to convert got reference counts | |
8958 | to real got offsets. */ | |
8959 | ||
8960 | static bfd_boolean | |
8961 | elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) | |
8962 | { | |
8963 | struct alloc_got_off_arg *gofarg = arg; | |
8964 | ||
8965 | if (h->root.type == bfd_link_hash_warning) | |
8966 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
8967 | ||
8968 | if (h->got.refcount > 0) | |
8969 | { | |
8970 | h->got.offset = gofarg->gotoff; | |
8971 | gofarg->gotoff += gofarg->got_elt_size; | |
8972 | } | |
8973 | else | |
8974 | h->got.offset = (bfd_vma) -1; | |
8975 | ||
8976 | return TRUE; | |
8977 | } | |
8978 | ||
8979 | /* And an accompanying bit to work out final got entry offsets once | |
8980 | we're done. Should be called from final_link. */ | |
8981 | ||
8982 | bfd_boolean | |
8983 | bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, | |
8984 | struct bfd_link_info *info) | |
8985 | { | |
8986 | bfd *i; | |
8987 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8988 | bfd_vma gotoff; | |
8989 | unsigned int got_elt_size = bed->s->arch_size / 8; | |
8990 | struct alloc_got_off_arg gofarg; | |
8991 | ||
8992 | if (! is_elf_hash_table (info->hash)) | |
8993 | return FALSE; | |
8994 | ||
8995 | /* The GOT offset is relative to the .got section, but the GOT header is | |
8996 | put into the .got.plt section, if the backend uses it. */ | |
8997 | if (bed->want_got_plt) | |
8998 | gotoff = 0; | |
8999 | else | |
9000 | gotoff = bed->got_header_size; | |
9001 | ||
9002 | /* Do the local .got entries first. */ | |
9003 | for (i = info->input_bfds; i; i = i->link_next) | |
9004 | { | |
9005 | bfd_signed_vma *local_got; | |
9006 | bfd_size_type j, locsymcount; | |
9007 | Elf_Internal_Shdr *symtab_hdr; | |
9008 | ||
9009 | if (bfd_get_flavour (i) != bfd_target_elf_flavour) | |
9010 | continue; | |
9011 | ||
9012 | local_got = elf_local_got_refcounts (i); | |
9013 | if (!local_got) | |
9014 | continue; | |
9015 | ||
9016 | symtab_hdr = &elf_tdata (i)->symtab_hdr; | |
9017 | if (elf_bad_symtab (i)) | |
9018 | locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9019 | else | |
9020 | locsymcount = symtab_hdr->sh_info; | |
9021 | ||
9022 | for (j = 0; j < locsymcount; ++j) | |
9023 | { | |
9024 | if (local_got[j] > 0) | |
9025 | { | |
9026 | local_got[j] = gotoff; | |
9027 | gotoff += got_elt_size; | |
9028 | } | |
9029 | else | |
9030 | local_got[j] = (bfd_vma) -1; | |
9031 | } | |
9032 | } | |
9033 | ||
9034 | /* Then the global .got entries. .plt refcounts are handled by | |
9035 | adjust_dynamic_symbol */ | |
9036 | gofarg.gotoff = gotoff; | |
9037 | gofarg.got_elt_size = got_elt_size; | |
9038 | elf_link_hash_traverse (elf_hash_table (info), | |
9039 | elf_gc_allocate_got_offsets, | |
9040 | &gofarg); | |
9041 | return TRUE; | |
9042 | } | |
9043 | ||
9044 | /* Many folk need no more in the way of final link than this, once | |
9045 | got entry reference counting is enabled. */ | |
9046 | ||
9047 | bfd_boolean | |
9048 | bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) | |
9049 | { | |
9050 | if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) | |
9051 | return FALSE; | |
9052 | ||
9053 | /* Invoke the regular ELF backend linker to do all the work. */ | |
9054 | return bfd_elf_final_link (abfd, info); | |
9055 | } | |
9056 | ||
9057 | bfd_boolean | |
9058 | bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) | |
9059 | { | |
9060 | struct elf_reloc_cookie *rcookie = cookie; | |
9061 | ||
9062 | if (rcookie->bad_symtab) | |
9063 | rcookie->rel = rcookie->rels; | |
9064 | ||
9065 | for (; rcookie->rel < rcookie->relend; rcookie->rel++) | |
9066 | { | |
9067 | unsigned long r_symndx; | |
9068 | ||
9069 | if (! rcookie->bad_symtab) | |
9070 | if (rcookie->rel->r_offset > offset) | |
9071 | return FALSE; | |
9072 | if (rcookie->rel->r_offset != offset) | |
9073 | continue; | |
9074 | ||
9075 | r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; | |
9076 | if (r_symndx == SHN_UNDEF) | |
9077 | return TRUE; | |
9078 | ||
9079 | if (r_symndx >= rcookie->locsymcount | |
9080 | || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) | |
9081 | { | |
9082 | struct elf_link_hash_entry *h; | |
9083 | ||
9084 | h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; | |
9085 | ||
9086 | while (h->root.type == bfd_link_hash_indirect | |
9087 | || h->root.type == bfd_link_hash_warning) | |
9088 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
9089 | ||
9090 | if ((h->root.type == bfd_link_hash_defined | |
9091 | || h->root.type == bfd_link_hash_defweak) | |
9092 | && elf_discarded_section (h->root.u.def.section)) | |
9093 | return TRUE; | |
9094 | else | |
9095 | return FALSE; | |
9096 | } | |
9097 | else | |
9098 | { | |
9099 | /* It's not a relocation against a global symbol, | |
9100 | but it could be a relocation against a local | |
9101 | symbol for a discarded section. */ | |
9102 | asection *isec; | |
9103 | Elf_Internal_Sym *isym; | |
9104 | ||
9105 | /* Need to: get the symbol; get the section. */ | |
9106 | isym = &rcookie->locsyms[r_symndx]; | |
9107 | if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) | |
9108 | { | |
9109 | isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); | |
9110 | if (isec != NULL && elf_discarded_section (isec)) | |
9111 | return TRUE; | |
9112 | } | |
9113 | } | |
9114 | return FALSE; | |
9115 | } | |
9116 | return FALSE; | |
9117 | } | |
9118 | ||
9119 | /* Discard unneeded references to discarded sections. | |
9120 | Returns TRUE if any section's size was changed. */ | |
9121 | /* This function assumes that the relocations are in sorted order, | |
9122 | which is true for all known assemblers. */ | |
9123 | ||
9124 | bfd_boolean | |
9125 | bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) | |
9126 | { | |
9127 | struct elf_reloc_cookie cookie; | |
9128 | asection *stab, *eh; | |
9129 | Elf_Internal_Shdr *symtab_hdr; | |
9130 | const struct elf_backend_data *bed; | |
9131 | bfd *abfd; | |
9132 | unsigned int count; | |
9133 | bfd_boolean ret = FALSE; | |
9134 | ||
9135 | if (info->traditional_format | |
9136 | || !is_elf_hash_table (info->hash)) | |
9137 | return FALSE; | |
9138 | ||
9139 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) | |
9140 | { | |
9141 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
9142 | continue; | |
9143 | ||
9144 | bed = get_elf_backend_data (abfd); | |
9145 | ||
9146 | if ((abfd->flags & DYNAMIC) != 0) | |
9147 | continue; | |
9148 | ||
9149 | eh = bfd_get_section_by_name (abfd, ".eh_frame"); | |
9150 | if (info->relocatable | |
9151 | || (eh != NULL | |
eea6121a | 9152 | && (eh->size == 0 |
c152c796 AM |
9153 | || bfd_is_abs_section (eh->output_section)))) |
9154 | eh = NULL; | |
9155 | ||
9156 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
9157 | if (stab != NULL | |
eea6121a | 9158 | && (stab->size == 0 |
c152c796 AM |
9159 | || bfd_is_abs_section (stab->output_section) |
9160 | || stab->sec_info_type != ELF_INFO_TYPE_STABS)) | |
9161 | stab = NULL; | |
9162 | ||
9163 | if (stab == NULL | |
9164 | && eh == NULL | |
9165 | && bed->elf_backend_discard_info == NULL) | |
9166 | continue; | |
9167 | ||
9168 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9169 | cookie.abfd = abfd; | |
9170 | cookie.sym_hashes = elf_sym_hashes (abfd); | |
9171 | cookie.bad_symtab = elf_bad_symtab (abfd); | |
9172 | if (cookie.bad_symtab) | |
9173 | { | |
9174 | cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; | |
9175 | cookie.extsymoff = 0; | |
9176 | } | |
9177 | else | |
9178 | { | |
9179 | cookie.locsymcount = symtab_hdr->sh_info; | |
9180 | cookie.extsymoff = symtab_hdr->sh_info; | |
9181 | } | |
9182 | ||
9183 | if (bed->s->arch_size == 32) | |
9184 | cookie.r_sym_shift = 8; | |
9185 | else | |
9186 | cookie.r_sym_shift = 32; | |
9187 | ||
9188 | cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
9189 | if (cookie.locsyms == NULL && cookie.locsymcount != 0) | |
9190 | { | |
9191 | cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
9192 | cookie.locsymcount, 0, | |
9193 | NULL, NULL, NULL); | |
9194 | if (cookie.locsyms == NULL) | |
9195 | return FALSE; | |
9196 | } | |
9197 | ||
9198 | if (stab != NULL) | |
9199 | { | |
9200 | cookie.rels = NULL; | |
9201 | count = stab->reloc_count; | |
9202 | if (count != 0) | |
9203 | cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, | |
9204 | info->keep_memory); | |
9205 | if (cookie.rels != NULL) | |
9206 | { | |
9207 | cookie.rel = cookie.rels; | |
9208 | cookie.relend = cookie.rels; | |
9209 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9210 | if (_bfd_discard_section_stabs (abfd, stab, | |
9211 | elf_section_data (stab)->sec_info, | |
9212 | bfd_elf_reloc_symbol_deleted_p, | |
9213 | &cookie)) | |
9214 | ret = TRUE; | |
9215 | if (elf_section_data (stab)->relocs != cookie.rels) | |
9216 | free (cookie.rels); | |
9217 | } | |
9218 | } | |
9219 | ||
9220 | if (eh != NULL) | |
9221 | { | |
9222 | cookie.rels = NULL; | |
9223 | count = eh->reloc_count; | |
9224 | if (count != 0) | |
9225 | cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, | |
9226 | info->keep_memory); | |
9227 | cookie.rel = cookie.rels; | |
9228 | cookie.relend = cookie.rels; | |
9229 | if (cookie.rels != NULL) | |
9230 | cookie.relend += count * bed->s->int_rels_per_ext_rel; | |
9231 | ||
9232 | if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, | |
9233 | bfd_elf_reloc_symbol_deleted_p, | |
9234 | &cookie)) | |
9235 | ret = TRUE; | |
9236 | ||
9237 | if (cookie.rels != NULL | |
9238 | && elf_section_data (eh)->relocs != cookie.rels) | |
9239 | free (cookie.rels); | |
9240 | } | |
9241 | ||
9242 | if (bed->elf_backend_discard_info != NULL | |
9243 | && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) | |
9244 | ret = TRUE; | |
9245 | ||
9246 | if (cookie.locsyms != NULL | |
9247 | && symtab_hdr->contents != (unsigned char *) cookie.locsyms) | |
9248 | { | |
9249 | if (! info->keep_memory) | |
9250 | free (cookie.locsyms); | |
9251 | else | |
9252 | symtab_hdr->contents = (unsigned char *) cookie.locsyms; | |
9253 | } | |
9254 | } | |
9255 | ||
9256 | if (info->eh_frame_hdr | |
9257 | && !info->relocatable | |
9258 | && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) | |
9259 | ret = TRUE; | |
9260 | ||
9261 | return ret; | |
9262 | } | |
082b7297 | 9263 | |
3d7f7666 L |
9264 | struct already_linked_section |
9265 | { | |
9266 | asection *sec; | |
9267 | asection *linked; | |
9268 | }; | |
9269 | ||
9270 | /* Check if the member of a single member comdat group matches a | |
9271 | linkonce section and vice versa. */ | |
9272 | static bfd_boolean | |
9273 | try_match_symbols_in_sections | |
9274 | (struct bfd_section_already_linked_hash_entry *h, void *info) | |
9275 | { | |
9276 | struct bfd_section_already_linked *l; | |
9277 | struct already_linked_section *s | |
9278 | = (struct already_linked_section *) info; | |
9279 | ||
9280 | if (elf_sec_group (s->sec) == NULL) | |
9281 | { | |
9282 | /* It is a linkonce section. Try to match it with the member of a | |
9283 | single member comdat group. */ | |
9284 | for (l = h->entry; l != NULL; l = l->next) | |
9285 | if ((l->sec->flags & SEC_GROUP)) | |
9286 | { | |
9287 | asection *first = elf_next_in_group (l->sec); | |
9288 | ||
9289 | if (first != NULL | |
9290 | && elf_next_in_group (first) == first | |
9291 | && bfd_elf_match_symbols_in_sections (first, s->sec)) | |
9292 | { | |
9293 | s->linked = first; | |
9294 | return FALSE; | |
9295 | } | |
9296 | } | |
9297 | } | |
9298 | else | |
9299 | { | |
9300 | /* It is the member of a single member comdat group. Try to match | |
9301 | it with a linkonce section. */ | |
9302 | for (l = h->entry; l != NULL; l = l->next) | |
9303 | if ((l->sec->flags & SEC_GROUP) == 0 | |
9304 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL | |
9305 | && bfd_elf_match_symbols_in_sections (l->sec, s->sec)) | |
9306 | { | |
9307 | s->linked = l->sec; | |
9308 | return FALSE; | |
9309 | } | |
9310 | } | |
9311 | ||
9312 | return TRUE; | |
9313 | } | |
9314 | ||
9315 | static bfd_boolean | |
9316 | already_linked (asection *sec, asection *group) | |
9317 | { | |
9318 | struct already_linked_section result; | |
9319 | ||
9320 | result.sec = sec; | |
9321 | result.linked = NULL; | |
9322 | ||
9323 | bfd_section_already_linked_table_traverse | |
9324 | (try_match_symbols_in_sections, &result); | |
9325 | ||
9326 | if (result.linked) | |
9327 | { | |
9328 | sec->output_section = bfd_abs_section_ptr; | |
9329 | sec->kept_section = result.linked; | |
9330 | ||
9331 | /* Also discard the group section. */ | |
9332 | if (group) | |
9333 | group->output_section = bfd_abs_section_ptr; | |
9334 | ||
9335 | return TRUE; | |
9336 | } | |
9337 | ||
9338 | return FALSE; | |
9339 | } | |
9340 | ||
082b7297 L |
9341 | void |
9342 | _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec) | |
9343 | { | |
9344 | flagword flags; | |
9345 | const char *name; | |
9346 | struct bfd_section_already_linked *l; | |
9347 | struct bfd_section_already_linked_hash_entry *already_linked_list; | |
3d7f7666 L |
9348 | asection *group; |
9349 | ||
9350 | /* A single member comdat group section may be discarded by a | |
9351 | linkonce section. See below. */ | |
9352 | if (sec->output_section == bfd_abs_section_ptr) | |
9353 | return; | |
082b7297 L |
9354 | |
9355 | flags = sec->flags; | |
3d7f7666 L |
9356 | |
9357 | /* Check if it belongs to a section group. */ | |
9358 | group = elf_sec_group (sec); | |
9359 | ||
9360 | /* Return if it isn't a linkonce section nor a member of a group. A | |
9361 | comdat group section also has SEC_LINK_ONCE set. */ | |
9362 | if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) | |
082b7297 L |
9363 | return; |
9364 | ||
3d7f7666 L |
9365 | if (group) |
9366 | { | |
9367 | /* If this is the member of a single member comdat group, check if | |
9368 | the group should be discarded. */ | |
9369 | if (elf_next_in_group (sec) == sec | |
9370 | && (group->flags & SEC_LINK_ONCE) != 0) | |
9371 | sec = group; | |
9372 | else | |
9373 | return; | |
9374 | } | |
9375 | ||
082b7297 L |
9376 | /* FIXME: When doing a relocatable link, we may have trouble |
9377 | copying relocations in other sections that refer to local symbols | |
9378 | in the section being discarded. Those relocations will have to | |
9379 | be converted somehow; as of this writing I'm not sure that any of | |
9380 | the backends handle that correctly. | |
9381 | ||
9382 | It is tempting to instead not discard link once sections when | |
9383 | doing a relocatable link (technically, they should be discarded | |
9384 | whenever we are building constructors). However, that fails, | |
9385 | because the linker winds up combining all the link once sections | |
9386 | into a single large link once section, which defeats the purpose | |
9387 | of having link once sections in the first place. | |
9388 | ||
9389 | Also, not merging link once sections in a relocatable link | |
9390 | causes trouble for MIPS ELF, which relies on link once semantics | |
9391 | to handle the .reginfo section correctly. */ | |
9392 | ||
9393 | name = bfd_get_section_name (abfd, sec); | |
9394 | ||
9395 | already_linked_list = bfd_section_already_linked_table_lookup (name); | |
9396 | ||
9397 | for (l = already_linked_list->entry; l != NULL; l = l->next) | |
9398 | { | |
9399 | /* We may have 3 different sections on the list: group section, | |
9400 | comdat section and linkonce section. SEC may be a linkonce or | |
9401 | group section. We match a group section with a group section, | |
9402 | a linkonce section with a linkonce section, and ignore comdat | |
9403 | section. */ | |
3d7f7666 | 9404 | if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) |
082b7297 L |
9405 | && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) |
9406 | { | |
9407 | /* The section has already been linked. See if we should | |
9408 | issue a warning. */ | |
9409 | switch (flags & SEC_LINK_DUPLICATES) | |
9410 | { | |
9411 | default: | |
9412 | abort (); | |
9413 | ||
9414 | case SEC_LINK_DUPLICATES_DISCARD: | |
9415 | break; | |
9416 | ||
9417 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
9418 | (*_bfd_error_handler) | |
d003868e AM |
9419 | (_("%B: ignoring duplicate section `%A'\n"), |
9420 | abfd, sec); | |
082b7297 L |
9421 | break; |
9422 | ||
9423 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
9424 | if (sec->size != l->sec->size) | |
9425 | (*_bfd_error_handler) | |
d003868e AM |
9426 | (_("%B: duplicate section `%A' has different size\n"), |
9427 | abfd, sec); | |
082b7297 | 9428 | break; |
ea5158d8 DJ |
9429 | |
9430 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
9431 | if (sec->size != l->sec->size) | |
9432 | (*_bfd_error_handler) | |
9433 | (_("%B: duplicate section `%A' has different size\n"), | |
9434 | abfd, sec); | |
9435 | else if (sec->size != 0) | |
9436 | { | |
9437 | bfd_byte *sec_contents, *l_sec_contents; | |
9438 | ||
9439 | if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) | |
9440 | (*_bfd_error_handler) | |
9441 | (_("%B: warning: could not read contents of section `%A'\n"), | |
9442 | abfd, sec); | |
9443 | else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, | |
9444 | &l_sec_contents)) | |
9445 | (*_bfd_error_handler) | |
9446 | (_("%B: warning: could not read contents of section `%A'\n"), | |
9447 | l->sec->owner, l->sec); | |
9448 | else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) | |
9449 | (*_bfd_error_handler) | |
9450 | (_("%B: warning: duplicate section `%A' has different contents\n"), | |
9451 | abfd, sec); | |
9452 | ||
9453 | if (sec_contents) | |
9454 | free (sec_contents); | |
9455 | if (l_sec_contents) | |
9456 | free (l_sec_contents); | |
9457 | } | |
9458 | break; | |
082b7297 L |
9459 | } |
9460 | ||
9461 | /* Set the output_section field so that lang_add_section | |
9462 | does not create a lang_input_section structure for this | |
9463 | section. Since there might be a symbol in the section | |
9464 | being discarded, we must retain a pointer to the section | |
9465 | which we are really going to use. */ | |
9466 | sec->output_section = bfd_abs_section_ptr; | |
9467 | sec->kept_section = l->sec; | |
9468 | ||
9469 | if (flags & SEC_GROUP) | |
3d7f7666 L |
9470 | { |
9471 | asection *first = elf_next_in_group (sec); | |
9472 | asection *s = first; | |
9473 | ||
9474 | while (s != NULL) | |
9475 | { | |
9476 | s->output_section = bfd_abs_section_ptr; | |
9477 | /* Record which group discards it. */ | |
9478 | s->kept_section = l->sec; | |
9479 | s = elf_next_in_group (s); | |
9480 | /* These lists are circular. */ | |
9481 | if (s == first) | |
9482 | break; | |
9483 | } | |
9484 | } | |
082b7297 L |
9485 | |
9486 | return; | |
9487 | } | |
9488 | } | |
9489 | ||
3d7f7666 L |
9490 | if (group) |
9491 | { | |
9492 | /* If this is the member of a single member comdat group and the | |
9493 | group hasn't be discarded, we check if it matches a linkonce | |
9494 | section. We only record the discarded comdat group. Otherwise | |
9495 | the undiscarded group will be discarded incorrectly later since | |
9496 | itself has been recorded. */ | |
9497 | if (! already_linked (elf_next_in_group (sec), group)) | |
9498 | return; | |
9499 | } | |
9500 | else | |
9501 | /* There is no direct match. But for linkonce section, we should | |
9502 | check if there is a match with comdat group member. We always | |
9503 | record the linkonce section, discarded or not. */ | |
9504 | already_linked (sec, group); | |
9505 | ||
082b7297 L |
9506 | /* This is the first section with this name. Record it. */ |
9507 | bfd_section_already_linked_table_insert (already_linked_list, sec); | |
9508 | } |