Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
051d5130 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | |
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #define ARCH_SIZE 0 | |
26 | #include "elf-bfd.h" | |
27 | ||
b34976b6 | 28 | bfd_boolean |
268b6b39 | 29 | _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
30 | { |
31 | flagword flags; | |
aad5d350 | 32 | asection *s; |
252b5132 | 33 | struct elf_link_hash_entry *h; |
14a793b2 | 34 | struct bfd_link_hash_entry *bh; |
9c5bfbb7 | 35 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
36 | int ptralign; |
37 | ||
38 | /* This function may be called more than once. */ | |
aad5d350 AM |
39 | s = bfd_get_section_by_name (abfd, ".got"); |
40 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 41 | return TRUE; |
252b5132 RH |
42 | |
43 | switch (bed->s->arch_size) | |
44 | { | |
bb0deeff AO |
45 | case 32: |
46 | ptralign = 2; | |
47 | break; | |
48 | ||
49 | case 64: | |
50 | ptralign = 3; | |
51 | break; | |
52 | ||
53 | default: | |
54 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 55 | return FALSE; |
252b5132 RH |
56 | } |
57 | ||
58 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
59 | | SEC_LINKER_CREATED); | |
60 | ||
61 | s = bfd_make_section (abfd, ".got"); | |
62 | if (s == NULL | |
63 | || !bfd_set_section_flags (abfd, s, flags) | |
64 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 65 | return FALSE; |
252b5132 RH |
66 | |
67 | if (bed->want_got_plt) | |
68 | { | |
69 | s = bfd_make_section (abfd, ".got.plt"); | |
70 | if (s == NULL | |
71 | || !bfd_set_section_flags (abfd, s, flags) | |
72 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 73 | return FALSE; |
252b5132 RH |
74 | } |
75 | ||
2517a57f AM |
76 | if (bed->want_got_sym) |
77 | { | |
78 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
79 | (or .got.plt) section. We don't do this in the linker script | |
80 | because we don't want to define the symbol if we are not creating | |
81 | a global offset table. */ | |
14a793b2 | 82 | bh = NULL; |
2517a57f AM |
83 | if (!(_bfd_generic_link_add_one_symbol |
84 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
268b6b39 | 85 | bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh))) |
b34976b6 | 86 | return FALSE; |
14a793b2 | 87 | h = (struct elf_link_hash_entry *) bh; |
2517a57f AM |
88 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
89 | h->type = STT_OBJECT; | |
252b5132 | 90 | |
36af4a4e | 91 | if (! info->executable |
2517a57f | 92 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 93 | return FALSE; |
252b5132 | 94 | |
2517a57f AM |
95 | elf_hash_table (info)->hgot = h; |
96 | } | |
252b5132 RH |
97 | |
98 | /* The first bit of the global offset table is the header. */ | |
99 | s->_raw_size += bed->got_header_size + bed->got_symbol_offset; | |
100 | ||
b34976b6 | 101 | return TRUE; |
252b5132 RH |
102 | } |
103 | \f | |
45d6a902 AM |
104 | /* Create some sections which will be filled in with dynamic linking |
105 | information. ABFD is an input file which requires dynamic sections | |
106 | to be created. The dynamic sections take up virtual memory space | |
107 | when the final executable is run, so we need to create them before | |
108 | addresses are assigned to the output sections. We work out the | |
109 | actual contents and size of these sections later. */ | |
252b5132 | 110 | |
b34976b6 | 111 | bfd_boolean |
268b6b39 | 112 | _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 113 | { |
45d6a902 AM |
114 | flagword flags; |
115 | register asection *s; | |
116 | struct elf_link_hash_entry *h; | |
117 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 118 | const struct elf_backend_data *bed; |
252b5132 | 119 | |
0eddce27 | 120 | if (! is_elf_hash_table (info->hash)) |
45d6a902 AM |
121 | return FALSE; |
122 | ||
123 | if (elf_hash_table (info)->dynamic_sections_created) | |
124 | return TRUE; | |
125 | ||
126 | /* Make sure that all dynamic sections use the same input BFD. */ | |
127 | if (elf_hash_table (info)->dynobj == NULL) | |
128 | elf_hash_table (info)->dynobj = abfd; | |
129 | else | |
130 | abfd = elf_hash_table (info)->dynobj; | |
131 | ||
132 | /* Note that we set the SEC_IN_MEMORY flag for all of these | |
133 | sections. */ | |
134 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
135 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
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 | bed = get_elf_backend_data (abfd); |
158 | ||
159 | /* Create sections to hold version informations. These are removed | |
160 | if they are not needed. */ | |
161 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
162 | if (s == NULL | |
163 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
164 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
165 | return FALSE; | |
166 | ||
167 | s = bfd_make_section (abfd, ".gnu.version"); | |
168 | if (s == NULL | |
169 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
170 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
171 | return FALSE; | |
172 | ||
173 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
174 | if (s == NULL | |
175 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
176 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
177 | return FALSE; | |
178 | ||
179 | s = bfd_make_section (abfd, ".dynsym"); | |
180 | if (s == NULL | |
181 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
182 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
183 | return FALSE; | |
184 | ||
185 | s = bfd_make_section (abfd, ".dynstr"); | |
186 | if (s == NULL | |
187 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
188 | return FALSE; | |
189 | ||
190 | /* Create a strtab to hold the dynamic symbol names. */ | |
191 | if (elf_hash_table (info)->dynstr == NULL) | |
192 | { | |
193 | elf_hash_table (info)->dynstr = _bfd_elf_strtab_init (); | |
194 | if (elf_hash_table (info)->dynstr == NULL) | |
195 | return FALSE; | |
252b5132 RH |
196 | } |
197 | ||
45d6a902 AM |
198 | s = bfd_make_section (abfd, ".dynamic"); |
199 | if (s == NULL | |
200 | || ! bfd_set_section_flags (abfd, s, flags) | |
201 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
202 | return FALSE; | |
203 | ||
204 | /* The special symbol _DYNAMIC is always set to the start of the | |
205 | .dynamic section. This call occurs before we have processed the | |
206 | symbols for any dynamic object, so we don't have to worry about | |
207 | overriding a dynamic definition. We could set _DYNAMIC in a | |
208 | linker script, but we only want to define it if we are, in fact, | |
209 | creating a .dynamic section. We don't want to define it if there | |
210 | is no .dynamic section, since on some ELF platforms the start up | |
211 | code examines it to decide how to initialize the process. */ | |
212 | bh = NULL; | |
213 | if (! (_bfd_generic_link_add_one_symbol | |
268b6b39 AM |
214 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE, |
215 | get_elf_backend_data (abfd)->collect, &bh))) | |
45d6a902 AM |
216 | return FALSE; |
217 | h = (struct elf_link_hash_entry *) bh; | |
218 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
219 | h->type = STT_OBJECT; | |
220 | ||
36af4a4e | 221 | if (! info->executable |
45d6a902 AM |
222 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
223 | return FALSE; | |
224 | ||
225 | s = bfd_make_section (abfd, ".hash"); | |
226 | if (s == NULL | |
227 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
228 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
229 | return FALSE; | |
230 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
231 | ||
232 | /* Let the backend create the rest of the sections. This lets the | |
233 | backend set the right flags. The backend will normally create | |
234 | the .got and .plt sections. */ | |
235 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
236 | return FALSE; | |
237 | ||
238 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
239 | ||
240 | return TRUE; | |
241 | } | |
242 | ||
243 | /* Create dynamic sections when linking against a dynamic object. */ | |
244 | ||
245 | bfd_boolean | |
268b6b39 | 246 | _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) |
45d6a902 AM |
247 | { |
248 | flagword flags, pltflags; | |
249 | asection *s; | |
9c5bfbb7 | 250 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 | 251 | |
252b5132 RH |
252 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
253 | .rel[a].bss sections. */ | |
254 | ||
255 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
256 | | SEC_LINKER_CREATED); | |
257 | ||
258 | pltflags = flags; | |
259 | pltflags |= SEC_CODE; | |
260 | if (bed->plt_not_loaded) | |
5d1634d7 | 261 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
252b5132 RH |
262 | if (bed->plt_readonly) |
263 | pltflags |= SEC_READONLY; | |
264 | ||
265 | s = bfd_make_section (abfd, ".plt"); | |
266 | if (s == NULL | |
267 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
268 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
b34976b6 | 269 | return FALSE; |
252b5132 RH |
270 | |
271 | if (bed->want_plt_sym) | |
272 | { | |
273 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
274 | .plt section. */ | |
14a793b2 AM |
275 | struct elf_link_hash_entry *h; |
276 | struct bfd_link_hash_entry *bh = NULL; | |
277 | ||
252b5132 | 278 | if (! (_bfd_generic_link_add_one_symbol |
268b6b39 AM |
279 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL, |
280 | FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
b34976b6 | 281 | return FALSE; |
14a793b2 | 282 | h = (struct elf_link_hash_entry *) bh; |
252b5132 RH |
283 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
284 | h->type = STT_OBJECT; | |
285 | ||
36af4a4e | 286 | if (! info->executable |
252b5132 | 287 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 288 | return FALSE; |
252b5132 RH |
289 | } |
290 | ||
3e932841 | 291 | s = bfd_make_section (abfd, |
bf572ba0 | 292 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
293 | if (s == NULL |
294 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 295 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 296 | return FALSE; |
252b5132 RH |
297 | |
298 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 299 | return FALSE; |
252b5132 | 300 | |
3018b441 RH |
301 | if (bed->want_dynbss) |
302 | { | |
303 | /* The .dynbss section is a place to put symbols which are defined | |
304 | by dynamic objects, are referenced by regular objects, and are | |
305 | not functions. We must allocate space for them in the process | |
306 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
307 | initialize them at run time. The linker script puts the .dynbss | |
308 | section into the .bss section of the final image. */ | |
309 | s = bfd_make_section (abfd, ".dynbss"); | |
310 | if (s == NULL | |
77f3d027 | 311 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED)) |
b34976b6 | 312 | return FALSE; |
252b5132 | 313 | |
3018b441 | 314 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
315 | normally needed. We need to create it here, though, so that the |
316 | linker will map it to an output section. We can't just create it | |
317 | only if we need it, because we will not know whether we need it | |
318 | until we have seen all the input files, and the first time the | |
319 | main linker code calls BFD after examining all the input files | |
320 | (size_dynamic_sections) the input sections have already been | |
321 | mapped to the output sections. If the section turns out not to | |
322 | be needed, we can discard it later. We will never need this | |
323 | section when generating a shared object, since they do not use | |
324 | copy relocs. */ | |
3018b441 RH |
325 | if (! info->shared) |
326 | { | |
3e932841 KH |
327 | s = bfd_make_section (abfd, |
328 | (bed->default_use_rela_p | |
329 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
330 | if (s == NULL |
331 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 332 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 333 | return FALSE; |
3018b441 | 334 | } |
252b5132 RH |
335 | } |
336 | ||
b34976b6 | 337 | return TRUE; |
252b5132 RH |
338 | } |
339 | \f | |
252b5132 RH |
340 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
341 | read the input files, since we need to have a list of all of them | |
342 | before we can determine the final sizes of the output sections. | |
343 | Note that we may actually call this function even though we are not | |
344 | going to output any dynamic symbols; in some cases we know that a | |
345 | symbol should be in the dynamic symbol table, but only if there is | |
346 | one. */ | |
347 | ||
b34976b6 | 348 | bfd_boolean |
268b6b39 AM |
349 | _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, |
350 | struct elf_link_hash_entry *h) | |
252b5132 RH |
351 | { |
352 | if (h->dynindx == -1) | |
353 | { | |
2b0f7ef9 | 354 | struct elf_strtab_hash *dynstr; |
68b6ddd0 | 355 | char *p; |
252b5132 | 356 | const char *name; |
252b5132 RH |
357 | bfd_size_type indx; |
358 | ||
7a13edea NC |
359 | /* XXX: The ABI draft says the linker must turn hidden and |
360 | internal symbols into STB_LOCAL symbols when producing the | |
361 | DSO. However, if ld.so honors st_other in the dynamic table, | |
362 | this would not be necessary. */ | |
363 | switch (ELF_ST_VISIBILITY (h->other)) | |
364 | { | |
365 | case STV_INTERNAL: | |
366 | case STV_HIDDEN: | |
9d6eee78 L |
367 | if (h->root.type != bfd_link_hash_undefined |
368 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 L |
369 | { |
370 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; | |
b34976b6 | 371 | return TRUE; |
7a13edea | 372 | } |
0444bdd4 | 373 | |
7a13edea NC |
374 | default: |
375 | break; | |
376 | } | |
377 | ||
252b5132 RH |
378 | h->dynindx = elf_hash_table (info)->dynsymcount; |
379 | ++elf_hash_table (info)->dynsymcount; | |
380 | ||
381 | dynstr = elf_hash_table (info)->dynstr; | |
382 | if (dynstr == NULL) | |
383 | { | |
384 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 385 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 386 | if (dynstr == NULL) |
b34976b6 | 387 | return FALSE; |
252b5132 RH |
388 | } |
389 | ||
390 | /* We don't put any version information in the dynamic string | |
aad5d350 | 391 | table. */ |
252b5132 RH |
392 | name = h->root.root.string; |
393 | p = strchr (name, ELF_VER_CHR); | |
68b6ddd0 AM |
394 | if (p != NULL) |
395 | /* We know that the p points into writable memory. In fact, | |
396 | there are only a few symbols that have read-only names, being | |
397 | those like _GLOBAL_OFFSET_TABLE_ that are created specially | |
398 | by the backends. Most symbols will have names pointing into | |
399 | an ELF string table read from a file, or to objalloc memory. */ | |
400 | *p = 0; | |
401 | ||
402 | indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); | |
403 | ||
404 | if (p != NULL) | |
405 | *p = ELF_VER_CHR; | |
252b5132 RH |
406 | |
407 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 408 | return FALSE; |
252b5132 RH |
409 | h->dynstr_index = indx; |
410 | } | |
411 | ||
b34976b6 | 412 | return TRUE; |
252b5132 | 413 | } |
45d6a902 AM |
414 | \f |
415 | /* Record an assignment to a symbol made by a linker script. We need | |
416 | this in case some dynamic object refers to this symbol. */ | |
417 | ||
418 | bfd_boolean | |
268b6b39 AM |
419 | bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED, |
420 | struct bfd_link_info *info, | |
421 | const char *name, | |
422 | bfd_boolean provide) | |
45d6a902 AM |
423 | { |
424 | struct elf_link_hash_entry *h; | |
425 | ||
0eddce27 | 426 | if (!is_elf_hash_table (info->hash)) |
45d6a902 AM |
427 | return TRUE; |
428 | ||
429 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); | |
430 | if (h == NULL) | |
431 | return FALSE; | |
432 | ||
02bb6eae AO |
433 | /* Since we're defining the symbol, don't let it seem to have not |
434 | been defined. record_dynamic_symbol and size_dynamic_sections | |
435 | may depend on this. */ | |
436 | if (h->root.type == bfd_link_hash_undefweak | |
437 | || h->root.type == bfd_link_hash_undefined) | |
438 | h->root.type = bfd_link_hash_new; | |
439 | ||
45d6a902 AM |
440 | if (h->root.type == bfd_link_hash_new) |
441 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; | |
442 | ||
443 | /* If this symbol is being provided by the linker script, and it is | |
444 | currently defined by a dynamic object, but not by a regular | |
445 | object, then mark it as undefined so that the generic linker will | |
446 | force the correct value. */ | |
447 | if (provide | |
448 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
449 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
450 | h->root.type = bfd_link_hash_undefined; | |
451 | ||
452 | /* If this symbol is not being provided by the linker script, and it is | |
453 | currently defined by a dynamic object, but not by a regular object, | |
454 | then clear out any version information because the symbol will not be | |
455 | associated with the dynamic object any more. */ | |
456 | if (!provide | |
457 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
458 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
459 | h->verinfo.verdef = NULL; | |
460 | ||
461 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
462 | ||
463 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
464 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
465 | || info->shared) | |
466 | && h->dynindx == -1) | |
467 | { | |
468 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
469 | return FALSE; | |
470 | ||
471 | /* If this is a weak defined symbol, and we know a corresponding | |
472 | real symbol from the same dynamic object, make sure the real | |
473 | symbol is also made into a dynamic symbol. */ | |
474 | if (h->weakdef != NULL | |
475 | && h->weakdef->dynindx == -1) | |
476 | { | |
477 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) | |
478 | return FALSE; | |
479 | } | |
480 | } | |
481 | ||
482 | return TRUE; | |
483 | } | |
42751cf3 | 484 | |
8c58d23b AM |
485 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
486 | success, and 2 on a failure caused by attempting to record a symbol | |
487 | in a discarded section, eg. a discarded link-once section symbol. */ | |
488 | ||
489 | int | |
268b6b39 AM |
490 | elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, |
491 | bfd *input_bfd, | |
492 | long input_indx) | |
8c58d23b AM |
493 | { |
494 | bfd_size_type amt; | |
495 | struct elf_link_local_dynamic_entry *entry; | |
496 | struct elf_link_hash_table *eht; | |
497 | struct elf_strtab_hash *dynstr; | |
498 | unsigned long dynstr_index; | |
499 | char *name; | |
500 | Elf_External_Sym_Shndx eshndx; | |
501 | char esym[sizeof (Elf64_External_Sym)]; | |
502 | ||
0eddce27 | 503 | if (! is_elf_hash_table (info->hash)) |
8c58d23b AM |
504 | return 0; |
505 | ||
506 | /* See if the entry exists already. */ | |
507 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
508 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
509 | return 1; | |
510 | ||
511 | amt = sizeof (*entry); | |
268b6b39 | 512 | entry = bfd_alloc (input_bfd, amt); |
8c58d23b AM |
513 | if (entry == NULL) |
514 | return 0; | |
515 | ||
516 | /* Go find the symbol, so that we can find it's name. */ | |
517 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
268b6b39 | 518 | 1, input_indx, &entry->isym, esym, &eshndx)) |
8c58d23b AM |
519 | { |
520 | bfd_release (input_bfd, entry); | |
521 | return 0; | |
522 | } | |
523 | ||
524 | if (entry->isym.st_shndx != SHN_UNDEF | |
525 | && (entry->isym.st_shndx < SHN_LORESERVE | |
526 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
527 | { | |
528 | asection *s; | |
529 | ||
530 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
531 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
532 | { | |
533 | /* We can still bfd_release here as nothing has done another | |
534 | bfd_alloc. We can't do this later in this function. */ | |
535 | bfd_release (input_bfd, entry); | |
536 | return 2; | |
537 | } | |
538 | } | |
539 | ||
540 | name = (bfd_elf_string_from_elf_section | |
541 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
542 | entry->isym.st_name)); | |
543 | ||
544 | dynstr = elf_hash_table (info)->dynstr; | |
545 | if (dynstr == NULL) | |
546 | { | |
547 | /* Create a strtab to hold the dynamic symbol names. */ | |
548 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
549 | if (dynstr == NULL) | |
550 | return 0; | |
551 | } | |
552 | ||
b34976b6 | 553 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
554 | if (dynstr_index == (unsigned long) -1) |
555 | return 0; | |
556 | entry->isym.st_name = dynstr_index; | |
557 | ||
558 | eht = elf_hash_table (info); | |
559 | ||
560 | entry->next = eht->dynlocal; | |
561 | eht->dynlocal = entry; | |
562 | entry->input_bfd = input_bfd; | |
563 | entry->input_indx = input_indx; | |
564 | eht->dynsymcount++; | |
565 | ||
566 | /* Whatever binding the symbol had before, it's now local. */ | |
567 | entry->isym.st_info | |
568 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
569 | ||
570 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
571 | ||
572 | return 1; | |
573 | } | |
574 | ||
30b30c21 | 575 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 576 | |
30b30c21 | 577 | long |
268b6b39 AM |
578 | _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, |
579 | bfd *input_bfd, | |
580 | long input_indx) | |
30b30c21 RH |
581 | { |
582 | struct elf_link_local_dynamic_entry *e; | |
583 | ||
584 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
585 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
586 | return e->dynindx; | |
587 | return -1; | |
588 | } | |
589 | ||
590 | /* This function is used to renumber the dynamic symbols, if some of | |
591 | them are removed because they are marked as local. This is called | |
592 | via elf_link_hash_traverse. */ | |
593 | ||
b34976b6 | 594 | static bfd_boolean |
268b6b39 AM |
595 | elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, |
596 | void *data) | |
42751cf3 | 597 | { |
268b6b39 | 598 | size_t *count = data; |
30b30c21 | 599 | |
e92d460e AM |
600 | if (h->root.type == bfd_link_hash_warning) |
601 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
602 | ||
42751cf3 | 603 | if (h->dynindx != -1) |
30b30c21 RH |
604 | h->dynindx = ++(*count); |
605 | ||
b34976b6 | 606 | return TRUE; |
42751cf3 | 607 | } |
30b30c21 | 608 | |
062e2358 | 609 | /* Assign dynsym indices. In a shared library we generate a section |
30b30c21 RH |
610 | symbol for each output section, which come first. Next come all of |
611 | the back-end allocated local dynamic syms, followed by the rest of | |
612 | the global symbols. */ | |
613 | ||
614 | unsigned long | |
268b6b39 | 615 | _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info) |
30b30c21 RH |
616 | { |
617 | unsigned long dynsymcount = 0; | |
618 | ||
619 | if (info->shared) | |
620 | { | |
621 | asection *p; | |
622 | for (p = output_bfd->sections; p ; p = p->next) | |
bc0ba537 AM |
623 | if ((p->flags & SEC_EXCLUDE) == 0) |
624 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 RH |
625 | } |
626 | ||
627 | if (elf_hash_table (info)->dynlocal) | |
628 | { | |
629 | struct elf_link_local_dynamic_entry *p; | |
630 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
631 | p->dynindx = ++dynsymcount; | |
632 | } | |
633 | ||
634 | elf_link_hash_traverse (elf_hash_table (info), | |
635 | elf_link_renumber_hash_table_dynsyms, | |
636 | &dynsymcount); | |
637 | ||
638 | /* There is an unused NULL entry at the head of the table which | |
639 | we must account for in our count. Unless there weren't any | |
640 | symbols, which means we'll have no table at all. */ | |
641 | if (dynsymcount != 0) | |
642 | ++dynsymcount; | |
643 | ||
644 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
645 | } | |
252b5132 | 646 | |
45d6a902 AM |
647 | /* This function is called when we want to define a new symbol. It |
648 | handles the various cases which arise when we find a definition in | |
649 | a dynamic object, or when there is already a definition in a | |
650 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
651 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
652 | OVERRIDE if the old symbol is overriding a new definition. We set | |
653 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
654 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
655 | change, we mean that we shouldn't warn if the type or size does | |
0f8a2703 | 656 | change. */ |
45d6a902 AM |
657 | |
658 | bfd_boolean | |
268b6b39 AM |
659 | _bfd_elf_merge_symbol (bfd *abfd, |
660 | struct bfd_link_info *info, | |
661 | const char *name, | |
662 | Elf_Internal_Sym *sym, | |
663 | asection **psec, | |
664 | bfd_vma *pvalue, | |
665 | struct elf_link_hash_entry **sym_hash, | |
666 | bfd_boolean *skip, | |
667 | bfd_boolean *override, | |
668 | bfd_boolean *type_change_ok, | |
0f8a2703 | 669 | bfd_boolean *size_change_ok) |
252b5132 | 670 | { |
45d6a902 AM |
671 | asection *sec; |
672 | struct elf_link_hash_entry *h; | |
673 | struct elf_link_hash_entry *flip; | |
674 | int bind; | |
675 | bfd *oldbfd; | |
676 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
79349b09 | 677 | bfd_boolean newweak, oldweak; |
45d6a902 AM |
678 | |
679 | *skip = FALSE; | |
680 | *override = FALSE; | |
681 | ||
682 | sec = *psec; | |
683 | bind = ELF_ST_BIND (sym->st_info); | |
684 | ||
685 | if (! bfd_is_und_section (sec)) | |
686 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
687 | else | |
688 | h = ((struct elf_link_hash_entry *) | |
689 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
690 | if (h == NULL) | |
691 | return FALSE; | |
692 | *sym_hash = h; | |
252b5132 | 693 | |
45d6a902 AM |
694 | /* This code is for coping with dynamic objects, and is only useful |
695 | if we are doing an ELF link. */ | |
696 | if (info->hash->creator != abfd->xvec) | |
697 | return TRUE; | |
252b5132 | 698 | |
45d6a902 AM |
699 | /* For merging, we only care about real symbols. */ |
700 | ||
701 | while (h->root.type == bfd_link_hash_indirect | |
702 | || h->root.type == bfd_link_hash_warning) | |
703 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
704 | ||
705 | /* If we just created the symbol, mark it as being an ELF symbol. | |
706 | Other than that, there is nothing to do--there is no merge issue | |
707 | with a newly defined symbol--so we just return. */ | |
708 | ||
709 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 710 | { |
45d6a902 AM |
711 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; |
712 | return TRUE; | |
713 | } | |
252b5132 | 714 | |
45d6a902 | 715 | /* OLDBFD is a BFD associated with the existing symbol. */ |
252b5132 | 716 | |
45d6a902 AM |
717 | switch (h->root.type) |
718 | { | |
719 | default: | |
720 | oldbfd = NULL; | |
721 | break; | |
252b5132 | 722 | |
45d6a902 AM |
723 | case bfd_link_hash_undefined: |
724 | case bfd_link_hash_undefweak: | |
725 | oldbfd = h->root.u.undef.abfd; | |
726 | break; | |
727 | ||
728 | case bfd_link_hash_defined: | |
729 | case bfd_link_hash_defweak: | |
730 | oldbfd = h->root.u.def.section->owner; | |
731 | break; | |
732 | ||
733 | case bfd_link_hash_common: | |
734 | oldbfd = h->root.u.c.p->section->owner; | |
735 | break; | |
736 | } | |
737 | ||
738 | /* In cases involving weak versioned symbols, we may wind up trying | |
739 | to merge a symbol with itself. Catch that here, to avoid the | |
740 | confusion that results if we try to override a symbol with | |
741 | itself. The additional tests catch cases like | |
742 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
743 | dynamic object, which we do want to handle here. */ | |
744 | if (abfd == oldbfd | |
745 | && ((abfd->flags & DYNAMIC) == 0 | |
746 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
747 | return TRUE; | |
748 | ||
749 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
750 | respectively, is from a dynamic object. */ | |
751 | ||
752 | if ((abfd->flags & DYNAMIC) != 0) | |
753 | newdyn = TRUE; | |
754 | else | |
755 | newdyn = FALSE; | |
756 | ||
757 | if (oldbfd != NULL) | |
758 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
759 | else | |
760 | { | |
761 | asection *hsec; | |
762 | ||
763 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
764 | indices used by MIPS ELF. */ | |
765 | switch (h->root.type) | |
252b5132 | 766 | { |
45d6a902 AM |
767 | default: |
768 | hsec = NULL; | |
769 | break; | |
252b5132 | 770 | |
45d6a902 AM |
771 | case bfd_link_hash_defined: |
772 | case bfd_link_hash_defweak: | |
773 | hsec = h->root.u.def.section; | |
774 | break; | |
252b5132 | 775 | |
45d6a902 AM |
776 | case bfd_link_hash_common: |
777 | hsec = h->root.u.c.p->section; | |
778 | break; | |
252b5132 | 779 | } |
252b5132 | 780 | |
45d6a902 AM |
781 | if (hsec == NULL) |
782 | olddyn = FALSE; | |
783 | else | |
784 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
785 | } | |
252b5132 | 786 | |
45d6a902 AM |
787 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
788 | respectively, appear to be a definition rather than reference. */ | |
789 | ||
790 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
791 | newdef = FALSE; | |
792 | else | |
793 | newdef = TRUE; | |
794 | ||
795 | if (h->root.type == bfd_link_hash_undefined | |
796 | || h->root.type == bfd_link_hash_undefweak | |
797 | || h->root.type == bfd_link_hash_common) | |
798 | olddef = FALSE; | |
799 | else | |
800 | olddef = TRUE; | |
801 | ||
4cc11e76 | 802 | /* We need to remember if a symbol has a definition in a dynamic |
45d6a902 AM |
803 | object or is weak in all dynamic objects. Internal and hidden |
804 | visibility will make it unavailable to dynamic objects. */ | |
805 | if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0) | |
806 | { | |
807 | if (!bfd_is_und_section (sec)) | |
808 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF; | |
809 | else | |
252b5132 | 810 | { |
45d6a902 AM |
811 | /* Check if this symbol is weak in all dynamic objects. If it |
812 | is the first time we see it in a dynamic object, we mark | |
813 | if it is weak. Otherwise, we clear it. */ | |
814 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) | |
79349b09 | 815 | { |
45d6a902 AM |
816 | if (bind == STB_WEAK) |
817 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK; | |
252b5132 | 818 | } |
45d6a902 AM |
819 | else if (bind != STB_WEAK) |
820 | h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK; | |
252b5132 | 821 | } |
45d6a902 | 822 | } |
252b5132 | 823 | |
45d6a902 AM |
824 | /* If the old symbol has non-default visibility, we ignore the new |
825 | definition from a dynamic object. */ | |
826 | if (newdyn | |
9c7a29a3 | 827 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
828 | && !bfd_is_und_section (sec)) |
829 | { | |
830 | *skip = TRUE; | |
831 | /* Make sure this symbol is dynamic. */ | |
832 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
833 | /* A protected symbol has external availability. Make sure it is | |
834 | recorded as dynamic. | |
835 | ||
836 | FIXME: Should we check type and size for protected symbol? */ | |
837 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
838 | return _bfd_elf_link_record_dynamic_symbol (info, h); | |
839 | else | |
840 | return TRUE; | |
841 | } | |
842 | else if (!newdyn | |
9c7a29a3 | 843 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
45d6a902 AM |
844 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) |
845 | { | |
846 | /* If the new symbol with non-default visibility comes from a | |
847 | relocatable file and the old definition comes from a dynamic | |
848 | object, we remove the old definition. */ | |
849 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
850 | h = *sym_hash; | |
1de1a317 L |
851 | |
852 | if ((h->root.und_next || info->hash->undefs_tail == &h->root) | |
853 | && bfd_is_und_section (sec)) | |
854 | { | |
855 | /* If the new symbol is undefined and the old symbol was | |
856 | also undefined before, we need to make sure | |
857 | _bfd_generic_link_add_one_symbol doesn't mess | |
858 | up the linker hash table undefs list. Since the old | |
859 | definition came from a dynamic object, it is still on the | |
860 | undefs list. */ | |
861 | h->root.type = bfd_link_hash_undefined; | |
862 | /* FIXME: What if the new symbol is weak undefined? */ | |
863 | h->root.u.undef.abfd = abfd; | |
864 | } | |
865 | else | |
866 | { | |
867 | h->root.type = bfd_link_hash_new; | |
868 | h->root.u.undef.abfd = NULL; | |
869 | } | |
870 | ||
45d6a902 | 871 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) |
252b5132 | 872 | { |
45d6a902 | 873 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; |
22d5e339 L |
874 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC |
875 | | ELF_LINK_DYNAMIC_DEF); | |
45d6a902 AM |
876 | } |
877 | /* FIXME: Should we check type and size for protected symbol? */ | |
878 | h->size = 0; | |
879 | h->type = 0; | |
880 | return TRUE; | |
881 | } | |
14a793b2 | 882 | |
79349b09 AM |
883 | /* Differentiate strong and weak symbols. */ |
884 | newweak = bind == STB_WEAK; | |
885 | oldweak = (h->root.type == bfd_link_hash_defweak | |
886 | || h->root.type == bfd_link_hash_undefweak); | |
14a793b2 | 887 | |
0f8a2703 AM |
888 | /* If a new weak symbol comes from a regular file and the old symbol |
889 | comes from a dynamic library, we treat the new one as strong. | |
890 | Similarly, an old weak symbol from a regular file is treated as | |
891 | strong when the new symbol comes from a dynamic library. Further, | |
892 | an old weak symbol from a dynamic library is treated as strong if | |
893 | the new symbol is from a dynamic library. This reflects the way | |
894 | glibc's ld.so works. */ | |
895 | if (!newdyn && olddyn) | |
896 | newweak = FALSE; | |
897 | if (newdyn) | |
898 | oldweak = FALSE; | |
899 | ||
79349b09 AM |
900 | /* It's OK to change the type if either the existing symbol or the |
901 | new symbol is weak. A type change is also OK if the old symbol | |
902 | is undefined and the new symbol is defined. */ | |
252b5132 | 903 | |
79349b09 AM |
904 | if (oldweak |
905 | || newweak | |
906 | || (newdef | |
907 | && h->root.type == bfd_link_hash_undefined)) | |
908 | *type_change_ok = TRUE; | |
909 | ||
910 | /* It's OK to change the size if either the existing symbol or the | |
911 | new symbol is weak, or if the old symbol is undefined. */ | |
912 | ||
913 | if (*type_change_ok | |
914 | || h->root.type == bfd_link_hash_undefined) | |
915 | *size_change_ok = TRUE; | |
45d6a902 | 916 | |
45d6a902 AM |
917 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old |
918 | symbol, respectively, appears to be a common symbol in a dynamic | |
919 | object. If a symbol appears in an uninitialized section, and is | |
920 | not weak, and is not a function, then it may be a common symbol | |
921 | which was resolved when the dynamic object was created. We want | |
922 | to treat such symbols specially, because they raise special | |
923 | considerations when setting the symbol size: if the symbol | |
924 | appears as a common symbol in a regular object, and the size in | |
925 | the regular object is larger, we must make sure that we use the | |
926 | larger size. This problematic case can always be avoided in C, | |
927 | but it must be handled correctly when using Fortran shared | |
928 | libraries. | |
929 | ||
930 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
931 | likewise for OLDDYNCOMMON and OLDDEF. | |
932 | ||
933 | Note that this test is just a heuristic, and that it is quite | |
934 | possible to have an uninitialized symbol in a shared object which | |
935 | is really a definition, rather than a common symbol. This could | |
936 | lead to some minor confusion when the symbol really is a common | |
937 | symbol in some regular object. However, I think it will be | |
938 | harmless. */ | |
939 | ||
940 | if (newdyn | |
941 | && newdef | |
79349b09 | 942 | && !newweak |
45d6a902 AM |
943 | && (sec->flags & SEC_ALLOC) != 0 |
944 | && (sec->flags & SEC_LOAD) == 0 | |
945 | && sym->st_size > 0 | |
45d6a902 AM |
946 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) |
947 | newdyncommon = TRUE; | |
948 | else | |
949 | newdyncommon = FALSE; | |
950 | ||
951 | if (olddyn | |
952 | && olddef | |
953 | && h->root.type == bfd_link_hash_defined | |
954 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
955 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 | |
956 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
957 | && h->size > 0 | |
958 | && h->type != STT_FUNC) | |
959 | olddyncommon = TRUE; | |
960 | else | |
961 | olddyncommon = FALSE; | |
962 | ||
45d6a902 AM |
963 | /* If both the old and the new symbols look like common symbols in a |
964 | dynamic object, set the size of the symbol to the larger of the | |
965 | two. */ | |
966 | ||
967 | if (olddyncommon | |
968 | && newdyncommon | |
969 | && sym->st_size != h->size) | |
970 | { | |
971 | /* Since we think we have two common symbols, issue a multiple | |
972 | common warning if desired. Note that we only warn if the | |
973 | size is different. If the size is the same, we simply let | |
974 | the old symbol override the new one as normally happens with | |
975 | symbols defined in dynamic objects. */ | |
976 | ||
977 | if (! ((*info->callbacks->multiple_common) | |
978 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
979 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
980 | return FALSE; | |
252b5132 | 981 | |
45d6a902 AM |
982 | if (sym->st_size > h->size) |
983 | h->size = sym->st_size; | |
252b5132 | 984 | |
45d6a902 | 985 | *size_change_ok = TRUE; |
252b5132 RH |
986 | } |
987 | ||
45d6a902 AM |
988 | /* If we are looking at a dynamic object, and we have found a |
989 | definition, we need to see if the symbol was already defined by | |
990 | some other object. If so, we want to use the existing | |
991 | definition, and we do not want to report a multiple symbol | |
992 | definition error; we do this by clobbering *PSEC to be | |
993 | bfd_und_section_ptr. | |
994 | ||
995 | We treat a common symbol as a definition if the symbol in the | |
996 | shared library is a function, since common symbols always | |
997 | represent variables; this can cause confusion in principle, but | |
998 | any such confusion would seem to indicate an erroneous program or | |
999 | shared library. We also permit a common symbol in a regular | |
79349b09 | 1000 | object to override a weak symbol in a shared object. */ |
45d6a902 AM |
1001 | |
1002 | if (newdyn | |
1003 | && newdef | |
1004 | && (olddef | |
1005 | || (h->root.type == bfd_link_hash_common | |
79349b09 | 1006 | && (newweak |
0f8a2703 | 1007 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) |
45d6a902 AM |
1008 | { |
1009 | *override = TRUE; | |
1010 | newdef = FALSE; | |
1011 | newdyncommon = FALSE; | |
252b5132 | 1012 | |
45d6a902 AM |
1013 | *psec = sec = bfd_und_section_ptr; |
1014 | *size_change_ok = TRUE; | |
252b5132 | 1015 | |
45d6a902 AM |
1016 | /* If we get here when the old symbol is a common symbol, then |
1017 | we are explicitly letting it override a weak symbol or | |
1018 | function in a dynamic object, and we don't want to warn about | |
1019 | a type change. If the old symbol is a defined symbol, a type | |
1020 | change warning may still be appropriate. */ | |
252b5132 | 1021 | |
45d6a902 AM |
1022 | if (h->root.type == bfd_link_hash_common) |
1023 | *type_change_ok = TRUE; | |
1024 | } | |
1025 | ||
1026 | /* Handle the special case of an old common symbol merging with a | |
1027 | new symbol which looks like a common symbol in a shared object. | |
1028 | We change *PSEC and *PVALUE to make the new symbol look like a | |
1029 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
1030 | the right thing. */ | |
1031 | ||
1032 | if (newdyncommon | |
1033 | && h->root.type == bfd_link_hash_common) | |
1034 | { | |
1035 | *override = TRUE; | |
1036 | newdef = FALSE; | |
1037 | newdyncommon = FALSE; | |
1038 | *pvalue = sym->st_size; | |
1039 | *psec = sec = bfd_com_section_ptr; | |
1040 | *size_change_ok = TRUE; | |
1041 | } | |
1042 | ||
1043 | /* If the old symbol is from a dynamic object, and the new symbol is | |
1044 | a definition which is not from a dynamic object, then the new | |
1045 | symbol overrides the old symbol. Symbols from regular files | |
1046 | always take precedence over symbols from dynamic objects, even if | |
1047 | they are defined after the dynamic object in the link. | |
1048 | ||
1049 | As above, we again permit a common symbol in a regular object to | |
1050 | override a definition in a shared object if the shared object | |
0f8a2703 | 1051 | symbol is a function or is weak. */ |
45d6a902 AM |
1052 | |
1053 | flip = NULL; | |
1054 | if (! newdyn | |
1055 | && (newdef | |
1056 | || (bfd_is_com_section (sec) | |
79349b09 AM |
1057 | && (oldweak |
1058 | || h->type == STT_FUNC))) | |
45d6a902 AM |
1059 | && olddyn |
1060 | && olddef | |
0f8a2703 | 1061 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) |
45d6a902 AM |
1062 | { |
1063 | /* Change the hash table entry to undefined, and let | |
1064 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1065 | new definition. */ | |
1066 | ||
1067 | h->root.type = bfd_link_hash_undefined; | |
1068 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1069 | *size_change_ok = TRUE; | |
1070 | ||
1071 | olddef = FALSE; | |
1072 | olddyncommon = FALSE; | |
1073 | ||
1074 | /* We again permit a type change when a common symbol may be | |
1075 | overriding a function. */ | |
1076 | ||
1077 | if (bfd_is_com_section (sec)) | |
1078 | *type_change_ok = TRUE; | |
1079 | ||
1080 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1081 | flip = *sym_hash; | |
1082 | else | |
1083 | /* This union may have been set to be non-NULL when this symbol | |
1084 | was seen in a dynamic object. We must force the union to be | |
1085 | NULL, so that it is correct for a regular symbol. */ | |
1086 | h->verinfo.vertree = NULL; | |
1087 | } | |
1088 | ||
1089 | /* Handle the special case of a new common symbol merging with an | |
1090 | old symbol that looks like it might be a common symbol defined in | |
1091 | a shared object. Note that we have already handled the case in | |
1092 | which a new common symbol should simply override the definition | |
1093 | in the shared library. */ | |
1094 | ||
1095 | if (! newdyn | |
1096 | && bfd_is_com_section (sec) | |
1097 | && olddyncommon) | |
1098 | { | |
1099 | /* It would be best if we could set the hash table entry to a | |
1100 | common symbol, but we don't know what to use for the section | |
1101 | or the alignment. */ | |
1102 | if (! ((*info->callbacks->multiple_common) | |
1103 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1104 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1105 | return FALSE; | |
1106 | ||
4cc11e76 | 1107 | /* If the presumed common symbol in the dynamic object is |
45d6a902 AM |
1108 | larger, pretend that the new symbol has its size. */ |
1109 | ||
1110 | if (h->size > *pvalue) | |
1111 | *pvalue = h->size; | |
1112 | ||
1113 | /* FIXME: We no longer know the alignment required by the symbol | |
1114 | in the dynamic object, so we just wind up using the one from | |
1115 | the regular object. */ | |
1116 | ||
1117 | olddef = FALSE; | |
1118 | olddyncommon = FALSE; | |
1119 | ||
1120 | h->root.type = bfd_link_hash_undefined; | |
1121 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1122 | ||
1123 | *size_change_ok = TRUE; | |
1124 | *type_change_ok = TRUE; | |
1125 | ||
1126 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1127 | flip = *sym_hash; | |
1128 | else | |
1129 | h->verinfo.vertree = NULL; | |
1130 | } | |
1131 | ||
1132 | if (flip != NULL) | |
1133 | { | |
1134 | /* Handle the case where we had a versioned symbol in a dynamic | |
1135 | library and now find a definition in a normal object. In this | |
1136 | case, we make the versioned symbol point to the normal one. */ | |
9c5bfbb7 | 1137 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1138 | flip->root.type = h->root.type; |
1139 | h->root.type = bfd_link_hash_indirect; | |
1140 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1141 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1142 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
1143 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1144 | { | |
1145 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; | |
1146 | flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1147 | } | |
1148 | } | |
1149 | ||
45d6a902 AM |
1150 | return TRUE; |
1151 | } | |
1152 | ||
1153 | /* This function is called to create an indirect symbol from the | |
1154 | default for the symbol with the default version if needed. The | |
1155 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
0f8a2703 | 1156 | set DYNSYM if the new indirect symbol is dynamic. */ |
45d6a902 AM |
1157 | |
1158 | bfd_boolean | |
268b6b39 AM |
1159 | _bfd_elf_add_default_symbol (bfd *abfd, |
1160 | struct bfd_link_info *info, | |
1161 | struct elf_link_hash_entry *h, | |
1162 | const char *name, | |
1163 | Elf_Internal_Sym *sym, | |
1164 | asection **psec, | |
1165 | bfd_vma *value, | |
1166 | bfd_boolean *dynsym, | |
0f8a2703 | 1167 | bfd_boolean override) |
45d6a902 AM |
1168 | { |
1169 | bfd_boolean type_change_ok; | |
1170 | bfd_boolean size_change_ok; | |
1171 | bfd_boolean skip; | |
1172 | char *shortname; | |
1173 | struct elf_link_hash_entry *hi; | |
1174 | struct bfd_link_hash_entry *bh; | |
9c5bfbb7 | 1175 | const struct elf_backend_data *bed; |
45d6a902 AM |
1176 | bfd_boolean collect; |
1177 | bfd_boolean dynamic; | |
1178 | char *p; | |
1179 | size_t len, shortlen; | |
1180 | asection *sec; | |
1181 | ||
1182 | /* If this symbol has a version, and it is the default version, we | |
1183 | create an indirect symbol from the default name to the fully | |
1184 | decorated name. This will cause external references which do not | |
1185 | specify a version to be bound to this version of the symbol. */ | |
1186 | p = strchr (name, ELF_VER_CHR); | |
1187 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1188 | return TRUE; | |
1189 | ||
1190 | if (override) | |
1191 | { | |
4cc11e76 | 1192 | /* We are overridden by an old definition. We need to check if we |
45d6a902 AM |
1193 | need to create the indirect symbol from the default name. */ |
1194 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1195 | FALSE, FALSE); | |
1196 | BFD_ASSERT (hi != NULL); | |
1197 | if (hi == h) | |
1198 | return TRUE; | |
1199 | while (hi->root.type == bfd_link_hash_indirect | |
1200 | || hi->root.type == bfd_link_hash_warning) | |
1201 | { | |
1202 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1203 | if (hi == h) | |
1204 | return TRUE; | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | bed = get_elf_backend_data (abfd); | |
1209 | collect = bed->collect; | |
1210 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1211 | ||
1212 | shortlen = p - name; | |
1213 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1214 | if (shortname == NULL) | |
1215 | return FALSE; | |
1216 | memcpy (shortname, name, shortlen); | |
1217 | shortname[shortlen] = '\0'; | |
1218 | ||
1219 | /* We are going to create a new symbol. Merge it with any existing | |
1220 | symbol with this name. For the purposes of the merge, act as | |
1221 | though we were defining the symbol we just defined, although we | |
1222 | actually going to define an indirect symbol. */ | |
1223 | type_change_ok = FALSE; | |
1224 | size_change_ok = FALSE; | |
1225 | sec = *psec; | |
1226 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1227 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1228 | &size_change_ok)) |
45d6a902 AM |
1229 | return FALSE; |
1230 | ||
1231 | if (skip) | |
1232 | goto nondefault; | |
1233 | ||
1234 | if (! override) | |
1235 | { | |
1236 | bh = &hi->root; | |
1237 | if (! (_bfd_generic_link_add_one_symbol | |
1238 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
268b6b39 | 1239 | 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1240 | return FALSE; |
1241 | hi = (struct elf_link_hash_entry *) bh; | |
1242 | } | |
1243 | else | |
1244 | { | |
1245 | /* In this case the symbol named SHORTNAME is overriding the | |
1246 | indirect symbol we want to add. We were planning on making | |
1247 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1248 | is the name without a version. NAME is the fully versioned | |
1249 | name, and it is the default version. | |
1250 | ||
1251 | Overriding means that we already saw a definition for the | |
1252 | symbol SHORTNAME in a regular object, and it is overriding | |
1253 | the symbol defined in the dynamic object. | |
1254 | ||
1255 | When this happens, we actually want to change NAME, the | |
1256 | symbol we just added, to refer to SHORTNAME. This will cause | |
1257 | references to NAME in the shared object to become references | |
1258 | to SHORTNAME in the regular object. This is what we expect | |
1259 | when we override a function in a shared object: that the | |
1260 | references in the shared object will be mapped to the | |
1261 | definition in the regular object. */ | |
1262 | ||
1263 | while (hi->root.type == bfd_link_hash_indirect | |
1264 | || hi->root.type == bfd_link_hash_warning) | |
1265 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1266 | ||
1267 | h->root.type = bfd_link_hash_indirect; | |
1268 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
1269 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1270 | { | |
1271 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC; | |
1272 | hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1273 | if (hi->elf_link_hash_flags | |
1274 | & (ELF_LINK_HASH_REF_REGULAR | |
1275 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1276 | { | |
1277 | if (! _bfd_elf_link_record_dynamic_symbol (info, hi)) | |
1278 | return FALSE; | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | /* Now set HI to H, so that the following code will set the | |
1283 | other fields correctly. */ | |
1284 | hi = h; | |
1285 | } | |
1286 | ||
1287 | /* If there is a duplicate definition somewhere, then HI may not | |
1288 | point to an indirect symbol. We will have reported an error to | |
1289 | the user in that case. */ | |
1290 | ||
1291 | if (hi->root.type == bfd_link_hash_indirect) | |
1292 | { | |
1293 | struct elf_link_hash_entry *ht; | |
1294 | ||
45d6a902 AM |
1295 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; |
1296 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1297 | ||
1298 | /* See if the new flags lead us to realize that the symbol must | |
1299 | be dynamic. */ | |
1300 | if (! *dynsym) | |
1301 | { | |
1302 | if (! dynamic) | |
1303 | { | |
1304 | if (info->shared | |
1305 | || ((hi->elf_link_hash_flags | |
1306 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
1307 | *dynsym = TRUE; | |
1308 | } | |
1309 | else | |
1310 | { | |
1311 | if ((hi->elf_link_hash_flags | |
1312 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1313 | *dynsym = TRUE; | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | /* We also need to define an indirection from the nondefault version | |
1319 | of the symbol. */ | |
1320 | ||
1321 | nondefault: | |
1322 | len = strlen (name); | |
1323 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1324 | if (shortname == NULL) | |
1325 | return FALSE; | |
1326 | memcpy (shortname, name, shortlen); | |
1327 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1328 | ||
1329 | /* Once again, merge with any existing symbol. */ | |
1330 | type_change_ok = FALSE; | |
1331 | size_change_ok = FALSE; | |
1332 | sec = *psec; | |
1333 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1334 | &hi, &skip, &override, &type_change_ok, | |
0f8a2703 | 1335 | &size_change_ok)) |
45d6a902 AM |
1336 | return FALSE; |
1337 | ||
1338 | if (skip) | |
1339 | return TRUE; | |
1340 | ||
1341 | if (override) | |
1342 | { | |
1343 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1344 | the type of override we do in the case above unless it is | |
4cc11e76 | 1345 | overridden by a versioned definition. */ |
45d6a902 AM |
1346 | if (hi->root.type != bfd_link_hash_defined |
1347 | && hi->root.type != bfd_link_hash_defweak) | |
1348 | (*_bfd_error_handler) | |
1349 | (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"), | |
1350 | bfd_archive_filename (abfd), shortname); | |
1351 | } | |
1352 | else | |
1353 | { | |
1354 | bh = &hi->root; | |
1355 | if (! (_bfd_generic_link_add_one_symbol | |
1356 | (info, abfd, shortname, BSF_INDIRECT, | |
268b6b39 | 1357 | bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) |
45d6a902 AM |
1358 | return FALSE; |
1359 | hi = (struct elf_link_hash_entry *) bh; | |
1360 | ||
1361 | /* If there is a duplicate definition somewhere, then HI may not | |
1362 | point to an indirect symbol. We will have reported an error | |
1363 | to the user in that case. */ | |
1364 | ||
1365 | if (hi->root.type == bfd_link_hash_indirect) | |
1366 | { | |
45d6a902 AM |
1367 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); |
1368 | ||
1369 | /* See if the new flags lead us to realize that the symbol | |
1370 | must be dynamic. */ | |
1371 | if (! *dynsym) | |
1372 | { | |
1373 | if (! dynamic) | |
1374 | { | |
1375 | if (info->shared | |
1376 | || ((hi->elf_link_hash_flags | |
1377 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
1378 | *dynsym = TRUE; | |
1379 | } | |
1380 | else | |
1381 | { | |
1382 | if ((hi->elf_link_hash_flags | |
1383 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1384 | *dynsym = TRUE; | |
1385 | } | |
1386 | } | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | return TRUE; | |
1391 | } | |
1392 | \f | |
1393 | /* This routine is used to export all defined symbols into the dynamic | |
1394 | symbol table. It is called via elf_link_hash_traverse. */ | |
1395 | ||
1396 | bfd_boolean | |
268b6b39 | 1397 | _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 1398 | { |
268b6b39 | 1399 | struct elf_info_failed *eif = data; |
45d6a902 AM |
1400 | |
1401 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1402 | if (h->root.type == bfd_link_hash_indirect) | |
1403 | return TRUE; | |
1404 | ||
1405 | if (h->root.type == bfd_link_hash_warning) | |
1406 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1407 | ||
1408 | if (h->dynindx == -1 | |
1409 | && (h->elf_link_hash_flags | |
1410 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
1411 | { | |
1412 | struct bfd_elf_version_tree *t; | |
1413 | struct bfd_elf_version_expr *d; | |
1414 | ||
1415 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1416 | { | |
108ba305 | 1417 | if (t->globals.list != NULL) |
45d6a902 | 1418 | { |
108ba305 JJ |
1419 | d = (*t->match) (&t->globals, NULL, h->root.root.string); |
1420 | if (d != NULL) | |
1421 | goto doit; | |
45d6a902 AM |
1422 | } |
1423 | ||
108ba305 | 1424 | if (t->locals.list != NULL) |
45d6a902 | 1425 | { |
108ba305 JJ |
1426 | d = (*t->match) (&t->locals, NULL, h->root.root.string); |
1427 | if (d != NULL) | |
1428 | return TRUE; | |
45d6a902 AM |
1429 | } |
1430 | } | |
1431 | ||
1432 | if (!eif->verdefs) | |
1433 | { | |
1434 | doit: | |
1435 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
1436 | { | |
1437 | eif->failed = TRUE; | |
1438 | return FALSE; | |
1439 | } | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | return TRUE; | |
1444 | } | |
1445 | \f | |
1446 | /* Look through the symbols which are defined in other shared | |
1447 | libraries and referenced here. Update the list of version | |
1448 | dependencies. This will be put into the .gnu.version_r section. | |
1449 | This function is called via elf_link_hash_traverse. */ | |
1450 | ||
1451 | bfd_boolean | |
268b6b39 AM |
1452 | _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, |
1453 | void *data) | |
45d6a902 | 1454 | { |
268b6b39 | 1455 | struct elf_find_verdep_info *rinfo = data; |
45d6a902 AM |
1456 | Elf_Internal_Verneed *t; |
1457 | Elf_Internal_Vernaux *a; | |
1458 | bfd_size_type amt; | |
1459 | ||
1460 | if (h->root.type == bfd_link_hash_warning) | |
1461 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1462 | ||
1463 | /* We only care about symbols defined in shared objects with version | |
1464 | information. */ | |
1465 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
1466 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1467 | || h->dynindx == -1 | |
1468 | || h->verinfo.verdef == NULL) | |
1469 | return TRUE; | |
1470 | ||
1471 | /* See if we already know about this version. */ | |
1472 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1473 | { | |
1474 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1475 | continue; | |
1476 | ||
1477 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1478 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1479 | return TRUE; | |
1480 | ||
1481 | break; | |
1482 | } | |
1483 | ||
1484 | /* This is a new version. Add it to tree we are building. */ | |
1485 | ||
1486 | if (t == NULL) | |
1487 | { | |
1488 | amt = sizeof *t; | |
268b6b39 | 1489 | t = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1490 | if (t == NULL) |
1491 | { | |
1492 | rinfo->failed = TRUE; | |
1493 | return FALSE; | |
1494 | } | |
1495 | ||
1496 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1497 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1498 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1499 | } | |
1500 | ||
1501 | amt = sizeof *a; | |
268b6b39 | 1502 | a = bfd_zalloc (rinfo->output_bfd, amt); |
45d6a902 AM |
1503 | |
1504 | /* Note that we are copying a string pointer here, and testing it | |
1505 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1506 | discard the string data when low in memory, this will have to be | |
1507 | fixed. */ | |
1508 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1509 | ||
1510 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1511 | a->vna_nextptr = t->vn_auxptr; | |
1512 | ||
1513 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1514 | ++rinfo->vers; | |
1515 | ||
1516 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1517 | ||
1518 | t->vn_auxptr = a; | |
1519 | ||
1520 | return TRUE; | |
1521 | } | |
1522 | ||
1523 | /* Figure out appropriate versions for all the symbols. We may not | |
1524 | have the version number script until we have read all of the input | |
1525 | files, so until that point we don't know which symbols should be | |
1526 | local. This function is called via elf_link_hash_traverse. */ | |
1527 | ||
1528 | bfd_boolean | |
268b6b39 | 1529 | _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
1530 | { |
1531 | struct elf_assign_sym_version_info *sinfo; | |
1532 | struct bfd_link_info *info; | |
9c5bfbb7 | 1533 | const struct elf_backend_data *bed; |
45d6a902 AM |
1534 | struct elf_info_failed eif; |
1535 | char *p; | |
1536 | bfd_size_type amt; | |
1537 | ||
268b6b39 | 1538 | sinfo = data; |
45d6a902 AM |
1539 | info = sinfo->info; |
1540 | ||
1541 | if (h->root.type == bfd_link_hash_warning) | |
1542 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1543 | ||
1544 | /* Fix the symbol flags. */ | |
1545 | eif.failed = FALSE; | |
1546 | eif.info = info; | |
1547 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1548 | { | |
1549 | if (eif.failed) | |
1550 | sinfo->failed = TRUE; | |
1551 | return FALSE; | |
1552 | } | |
1553 | ||
1554 | /* We only need version numbers for symbols defined in regular | |
1555 | objects. */ | |
1556 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1557 | return TRUE; | |
1558 | ||
1559 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1560 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1561 | if (p != NULL && h->verinfo.vertree == NULL) | |
1562 | { | |
1563 | struct bfd_elf_version_tree *t; | |
1564 | bfd_boolean hidden; | |
1565 | ||
1566 | hidden = TRUE; | |
1567 | ||
1568 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1569 | not a hidden symbol. */ | |
1570 | ++p; | |
1571 | if (*p == ELF_VER_CHR) | |
1572 | { | |
1573 | hidden = FALSE; | |
1574 | ++p; | |
1575 | } | |
1576 | ||
1577 | /* If there is no version string, we can just return out. */ | |
1578 | if (*p == '\0') | |
1579 | { | |
1580 | if (hidden) | |
1581 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
1582 | return TRUE; | |
1583 | } | |
1584 | ||
1585 | /* Look for the version. If we find it, it is no longer weak. */ | |
1586 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1587 | { | |
1588 | if (strcmp (t->name, p) == 0) | |
1589 | { | |
1590 | size_t len; | |
1591 | char *alc; | |
1592 | struct bfd_elf_version_expr *d; | |
1593 | ||
1594 | len = p - h->root.root.string; | |
268b6b39 | 1595 | alc = bfd_malloc (len); |
45d6a902 AM |
1596 | if (alc == NULL) |
1597 | return FALSE; | |
1598 | memcpy (alc, h->root.root.string, len - 1); | |
1599 | alc[len - 1] = '\0'; | |
1600 | if (alc[len - 2] == ELF_VER_CHR) | |
1601 | alc[len - 2] = '\0'; | |
1602 | ||
1603 | h->verinfo.vertree = t; | |
1604 | t->used = TRUE; | |
1605 | d = NULL; | |
1606 | ||
108ba305 JJ |
1607 | if (t->globals.list != NULL) |
1608 | d = (*t->match) (&t->globals, NULL, alc); | |
45d6a902 AM |
1609 | |
1610 | /* See if there is anything to force this symbol to | |
1611 | local scope. */ | |
108ba305 | 1612 | if (d == NULL && t->locals.list != NULL) |
45d6a902 | 1613 | { |
108ba305 JJ |
1614 | d = (*t->match) (&t->locals, NULL, alc); |
1615 | if (d != NULL | |
1616 | && h->dynindx != -1 | |
1617 | && info->shared | |
1618 | && ! info->export_dynamic) | |
1619 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
45d6a902 AM |
1620 | } |
1621 | ||
1622 | free (alc); | |
1623 | break; | |
1624 | } | |
1625 | } | |
1626 | ||
1627 | /* If we are building an application, we need to create a | |
1628 | version node for this version. */ | |
36af4a4e | 1629 | if (t == NULL && info->executable) |
45d6a902 AM |
1630 | { |
1631 | struct bfd_elf_version_tree **pp; | |
1632 | int version_index; | |
1633 | ||
1634 | /* If we aren't going to export this symbol, we don't need | |
1635 | to worry about it. */ | |
1636 | if (h->dynindx == -1) | |
1637 | return TRUE; | |
1638 | ||
1639 | amt = sizeof *t; | |
108ba305 | 1640 | t = bfd_zalloc (sinfo->output_bfd, amt); |
45d6a902 AM |
1641 | if (t == NULL) |
1642 | { | |
1643 | sinfo->failed = TRUE; | |
1644 | return FALSE; | |
1645 | } | |
1646 | ||
45d6a902 | 1647 | t->name = p; |
45d6a902 AM |
1648 | t->name_indx = (unsigned int) -1; |
1649 | t->used = TRUE; | |
1650 | ||
1651 | version_index = 1; | |
1652 | /* Don't count anonymous version tag. */ | |
1653 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1654 | version_index = 0; | |
1655 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1656 | ++version_index; | |
1657 | t->vernum = version_index; | |
1658 | ||
1659 | *pp = t; | |
1660 | ||
1661 | h->verinfo.vertree = t; | |
1662 | } | |
1663 | else if (t == NULL) | |
1664 | { | |
1665 | /* We could not find the version for a symbol when | |
1666 | generating a shared archive. Return an error. */ | |
1667 | (*_bfd_error_handler) | |
1668 | (_("%s: undefined versioned symbol name %s"), | |
1669 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); | |
1670 | bfd_set_error (bfd_error_bad_value); | |
1671 | sinfo->failed = TRUE; | |
1672 | return FALSE; | |
1673 | } | |
1674 | ||
1675 | if (hidden) | |
1676 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
1677 | } | |
1678 | ||
1679 | /* If we don't have a version for this symbol, see if we can find | |
1680 | something. */ | |
1681 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1682 | { | |
1683 | struct bfd_elf_version_tree *t; | |
1684 | struct bfd_elf_version_tree *local_ver; | |
1685 | struct bfd_elf_version_expr *d; | |
1686 | ||
1687 | /* See if can find what version this symbol is in. If the | |
1688 | symbol is supposed to be local, then don't actually register | |
1689 | it. */ | |
1690 | local_ver = NULL; | |
1691 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1692 | { | |
108ba305 | 1693 | if (t->globals.list != NULL) |
45d6a902 AM |
1694 | { |
1695 | bfd_boolean matched; | |
1696 | ||
1697 | matched = FALSE; | |
108ba305 JJ |
1698 | d = NULL; |
1699 | while ((d = (*t->match) (&t->globals, d, | |
1700 | h->root.root.string)) != NULL) | |
1701 | if (d->symver) | |
1702 | matched = TRUE; | |
1703 | else | |
1704 | { | |
1705 | /* There is a version without definition. Make | |
1706 | the symbol the default definition for this | |
1707 | version. */ | |
1708 | h->verinfo.vertree = t; | |
1709 | local_ver = NULL; | |
1710 | d->script = 1; | |
1711 | break; | |
1712 | } | |
45d6a902 AM |
1713 | if (d != NULL) |
1714 | break; | |
1715 | else if (matched) | |
1716 | /* There is no undefined version for this symbol. Hide the | |
1717 | default one. */ | |
1718 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1719 | } | |
1720 | ||
108ba305 | 1721 | if (t->locals.list != NULL) |
45d6a902 | 1722 | { |
108ba305 JJ |
1723 | d = NULL; |
1724 | while ((d = (*t->match) (&t->locals, d, | |
1725 | h->root.root.string)) != NULL) | |
45d6a902 | 1726 | { |
108ba305 | 1727 | local_ver = t; |
45d6a902 | 1728 | /* If the match is "*", keep looking for a more |
108ba305 JJ |
1729 | explicit, perhaps even global, match. |
1730 | XXX: Shouldn't this be !d->wildcard instead? */ | |
1731 | if (d->pattern[0] != '*' || d->pattern[1] != '\0') | |
1732 | break; | |
45d6a902 AM |
1733 | } |
1734 | ||
1735 | if (d != NULL) | |
1736 | break; | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | if (local_ver != NULL) | |
1741 | { | |
1742 | h->verinfo.vertree = local_ver; | |
1743 | if (h->dynindx != -1 | |
1744 | && info->shared | |
1745 | && ! info->export_dynamic) | |
1746 | { | |
1747 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1748 | } | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | return TRUE; | |
1753 | } | |
1754 | \f | |
45d6a902 AM |
1755 | /* Read and swap the relocs from the section indicated by SHDR. This |
1756 | may be either a REL or a RELA section. The relocations are | |
1757 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1758 | which should have already been allocated to contain enough space. | |
1759 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1760 | relocations should be stored. | |
1761 | ||
1762 | Returns FALSE if something goes wrong. */ | |
1763 | ||
1764 | static bfd_boolean | |
268b6b39 | 1765 | elf_link_read_relocs_from_section (bfd *abfd, |
243ef1e0 | 1766 | asection *sec, |
268b6b39 AM |
1767 | Elf_Internal_Shdr *shdr, |
1768 | void *external_relocs, | |
1769 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 | 1770 | { |
9c5bfbb7 | 1771 | const struct elf_backend_data *bed; |
268b6b39 | 1772 | void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); |
45d6a902 AM |
1773 | const bfd_byte *erela; |
1774 | const bfd_byte *erelaend; | |
1775 | Elf_Internal_Rela *irela; | |
243ef1e0 L |
1776 | Elf_Internal_Shdr *symtab_hdr; |
1777 | size_t nsyms; | |
45d6a902 | 1778 | |
45d6a902 AM |
1779 | /* Position ourselves at the start of the section. */ |
1780 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1781 | return FALSE; | |
1782 | ||
1783 | /* Read the relocations. */ | |
1784 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1785 | return FALSE; | |
1786 | ||
243ef1e0 L |
1787 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1788 | nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
1789 | ||
45d6a902 AM |
1790 | bed = get_elf_backend_data (abfd); |
1791 | ||
1792 | /* Convert the external relocations to the internal format. */ | |
1793 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1794 | swap_in = bed->s->swap_reloc_in; | |
1795 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1796 | swap_in = bed->s->swap_reloca_in; | |
1797 | else | |
1798 | { | |
1799 | bfd_set_error (bfd_error_wrong_format); | |
1800 | return FALSE; | |
1801 | } | |
1802 | ||
1803 | erela = external_relocs; | |
51992aec | 1804 | erelaend = erela + shdr->sh_size; |
45d6a902 AM |
1805 | irela = internal_relocs; |
1806 | while (erela < erelaend) | |
1807 | { | |
243ef1e0 L |
1808 | bfd_vma r_symndx; |
1809 | ||
45d6a902 | 1810 | (*swap_in) (abfd, erela, irela); |
243ef1e0 L |
1811 | r_symndx = ELF32_R_SYM (irela->r_info); |
1812 | if (bed->s->arch_size == 64) | |
1813 | r_symndx >>= 24; | |
1814 | if ((size_t) r_symndx >= nsyms) | |
1815 | { | |
1816 | (*_bfd_error_handler) | |
1817 | (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"), | |
1818 | bfd_archive_filename (abfd), (unsigned long) r_symndx, | |
1819 | (unsigned long) nsyms, irela->r_offset, sec->name); | |
1820 | bfd_set_error (bfd_error_bad_value); | |
1821 | return FALSE; | |
1822 | } | |
45d6a902 AM |
1823 | irela += bed->s->int_rels_per_ext_rel; |
1824 | erela += shdr->sh_entsize; | |
1825 | } | |
1826 | ||
1827 | return TRUE; | |
1828 | } | |
1829 | ||
1830 | /* Read and swap the relocs for a section O. They may have been | |
1831 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1832 | not NULL, they are used as buffers to read into. They are known to | |
1833 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1834 | the return value is allocated using either malloc or bfd_alloc, | |
1835 | according to the KEEP_MEMORY argument. If O has two relocation | |
1836 | sections (both REL and RELA relocations), then the REL_HDR | |
1837 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1838 | REL_HDR2 relocations. */ | |
1839 | ||
1840 | Elf_Internal_Rela * | |
268b6b39 AM |
1841 | _bfd_elf_link_read_relocs (bfd *abfd, |
1842 | asection *o, | |
1843 | void *external_relocs, | |
1844 | Elf_Internal_Rela *internal_relocs, | |
1845 | bfd_boolean keep_memory) | |
45d6a902 AM |
1846 | { |
1847 | Elf_Internal_Shdr *rel_hdr; | |
268b6b39 | 1848 | void *alloc1 = NULL; |
45d6a902 | 1849 | Elf_Internal_Rela *alloc2 = NULL; |
9c5bfbb7 | 1850 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
45d6a902 AM |
1851 | |
1852 | if (elf_section_data (o)->relocs != NULL) | |
1853 | return elf_section_data (o)->relocs; | |
1854 | ||
1855 | if (o->reloc_count == 0) | |
1856 | return NULL; | |
1857 | ||
1858 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1859 | ||
1860 | if (internal_relocs == NULL) | |
1861 | { | |
1862 | bfd_size_type size; | |
1863 | ||
1864 | size = o->reloc_count; | |
1865 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
1866 | if (keep_memory) | |
268b6b39 | 1867 | internal_relocs = bfd_alloc (abfd, size); |
45d6a902 | 1868 | else |
268b6b39 | 1869 | internal_relocs = alloc2 = bfd_malloc (size); |
45d6a902 AM |
1870 | if (internal_relocs == NULL) |
1871 | goto error_return; | |
1872 | } | |
1873 | ||
1874 | if (external_relocs == NULL) | |
1875 | { | |
1876 | bfd_size_type size = rel_hdr->sh_size; | |
1877 | ||
1878 | if (elf_section_data (o)->rel_hdr2) | |
1879 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
268b6b39 | 1880 | alloc1 = bfd_malloc (size); |
45d6a902 AM |
1881 | if (alloc1 == NULL) |
1882 | goto error_return; | |
1883 | external_relocs = alloc1; | |
1884 | } | |
1885 | ||
243ef1e0 | 1886 | if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, |
45d6a902 AM |
1887 | external_relocs, |
1888 | internal_relocs)) | |
1889 | goto error_return; | |
51992aec AM |
1890 | if (elf_section_data (o)->rel_hdr2 |
1891 | && (!elf_link_read_relocs_from_section | |
1892 | (abfd, o, | |
1893 | elf_section_data (o)->rel_hdr2, | |
1894 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
1895 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
1896 | * bed->s->int_rels_per_ext_rel)))) | |
45d6a902 AM |
1897 | goto error_return; |
1898 | ||
1899 | /* Cache the results for next time, if we can. */ | |
1900 | if (keep_memory) | |
1901 | elf_section_data (o)->relocs = internal_relocs; | |
1902 | ||
1903 | if (alloc1 != NULL) | |
1904 | free (alloc1); | |
1905 | ||
1906 | /* Don't free alloc2, since if it was allocated we are passing it | |
1907 | back (under the name of internal_relocs). */ | |
1908 | ||
1909 | return internal_relocs; | |
1910 | ||
1911 | error_return: | |
1912 | if (alloc1 != NULL) | |
1913 | free (alloc1); | |
1914 | if (alloc2 != NULL) | |
1915 | free (alloc2); | |
1916 | return NULL; | |
1917 | } | |
1918 | ||
1919 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
1920 | section header for a section containing relocations for O. */ | |
1921 | ||
1922 | bfd_boolean | |
268b6b39 AM |
1923 | _bfd_elf_link_size_reloc_section (bfd *abfd, |
1924 | Elf_Internal_Shdr *rel_hdr, | |
1925 | asection *o) | |
45d6a902 AM |
1926 | { |
1927 | bfd_size_type reloc_count; | |
1928 | bfd_size_type num_rel_hashes; | |
1929 | ||
1930 | /* Figure out how many relocations there will be. */ | |
1931 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
1932 | reloc_count = elf_section_data (o)->rel_count; | |
1933 | else | |
1934 | reloc_count = elf_section_data (o)->rel_count2; | |
1935 | ||
1936 | num_rel_hashes = o->reloc_count; | |
1937 | if (num_rel_hashes < reloc_count) | |
1938 | num_rel_hashes = reloc_count; | |
1939 | ||
1940 | /* That allows us to calculate the size of the section. */ | |
1941 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
1942 | ||
1943 | /* The contents field must last into write_object_contents, so we | |
1944 | allocate it with bfd_alloc rather than malloc. Also since we | |
1945 | cannot be sure that the contents will actually be filled in, | |
1946 | we zero the allocated space. */ | |
268b6b39 | 1947 | rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); |
45d6a902 AM |
1948 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) |
1949 | return FALSE; | |
1950 | ||
1951 | /* We only allocate one set of hash entries, so we only do it the | |
1952 | first time we are called. */ | |
1953 | if (elf_section_data (o)->rel_hashes == NULL | |
1954 | && num_rel_hashes) | |
1955 | { | |
1956 | struct elf_link_hash_entry **p; | |
1957 | ||
268b6b39 | 1958 | p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); |
45d6a902 AM |
1959 | if (p == NULL) |
1960 | return FALSE; | |
1961 | ||
1962 | elf_section_data (o)->rel_hashes = p; | |
1963 | } | |
1964 | ||
1965 | return TRUE; | |
1966 | } | |
1967 | ||
1968 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
1969 | originated from the section given by INPUT_REL_HDR) to the | |
1970 | OUTPUT_BFD. */ | |
1971 | ||
1972 | bfd_boolean | |
268b6b39 AM |
1973 | _bfd_elf_link_output_relocs (bfd *output_bfd, |
1974 | asection *input_section, | |
1975 | Elf_Internal_Shdr *input_rel_hdr, | |
1976 | Elf_Internal_Rela *internal_relocs) | |
45d6a902 AM |
1977 | { |
1978 | Elf_Internal_Rela *irela; | |
1979 | Elf_Internal_Rela *irelaend; | |
1980 | bfd_byte *erel; | |
1981 | Elf_Internal_Shdr *output_rel_hdr; | |
1982 | asection *output_section; | |
1983 | unsigned int *rel_countp = NULL; | |
9c5bfbb7 | 1984 | const struct elf_backend_data *bed; |
268b6b39 | 1985 | void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); |
45d6a902 AM |
1986 | |
1987 | output_section = input_section->output_section; | |
1988 | output_rel_hdr = NULL; | |
1989 | ||
1990 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
1991 | == input_rel_hdr->sh_entsize) | |
1992 | { | |
1993 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
1994 | rel_countp = &elf_section_data (output_section)->rel_count; | |
1995 | } | |
1996 | else if (elf_section_data (output_section)->rel_hdr2 | |
1997 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
1998 | == input_rel_hdr->sh_entsize)) | |
1999 | { | |
2000 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2001 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2002 | } | |
2003 | else | |
2004 | { | |
2005 | (*_bfd_error_handler) | |
2006 | (_("%s: relocation size mismatch in %s section %s"), | |
2007 | bfd_get_filename (output_bfd), | |
2008 | bfd_archive_filename (input_section->owner), | |
2009 | input_section->name); | |
2010 | bfd_set_error (bfd_error_wrong_object_format); | |
2011 | return FALSE; | |
2012 | } | |
2013 | ||
2014 | bed = get_elf_backend_data (output_bfd); | |
2015 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2016 | swap_out = bed->s->swap_reloc_out; | |
2017 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2018 | swap_out = bed->s->swap_reloca_out; | |
2019 | else | |
2020 | abort (); | |
2021 | ||
2022 | erel = output_rel_hdr->contents; | |
2023 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2024 | irela = internal_relocs; | |
2025 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2026 | * bed->s->int_rels_per_ext_rel); | |
2027 | while (irela < irelaend) | |
2028 | { | |
2029 | (*swap_out) (output_bfd, irela, erel); | |
2030 | irela += bed->s->int_rels_per_ext_rel; | |
2031 | erel += input_rel_hdr->sh_entsize; | |
2032 | } | |
2033 | ||
2034 | /* Bump the counter, so that we know where to add the next set of | |
2035 | relocations. */ | |
2036 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2037 | ||
2038 | return TRUE; | |
2039 | } | |
2040 | \f | |
2041 | /* Fix up the flags for a symbol. This handles various cases which | |
2042 | can only be fixed after all the input files are seen. This is | |
2043 | currently called by both adjust_dynamic_symbol and | |
2044 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2045 | the face of future changes. */ | |
2046 | ||
2047 | bfd_boolean | |
268b6b39 AM |
2048 | _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, |
2049 | struct elf_info_failed *eif) | |
45d6a902 AM |
2050 | { |
2051 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2052 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2053 | permit a non-ELF file to correctly refer to a symbol defined in | |
2054 | an ELF dynamic object. */ | |
2055 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) | |
2056 | { | |
2057 | while (h->root.type == bfd_link_hash_indirect) | |
2058 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2059 | ||
2060 | if (h->root.type != bfd_link_hash_defined | |
2061 | && h->root.type != bfd_link_hash_defweak) | |
2062 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
2063 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
2064 | else | |
2065 | { | |
2066 | if (h->root.u.def.section->owner != NULL | |
2067 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2068 | == bfd_target_elf_flavour)) | |
2069 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
2070 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
2071 | else | |
2072 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2073 | } | |
2074 | ||
2075 | if (h->dynindx == -1 | |
2076 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2077 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
2078 | { | |
2079 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2080 | { | |
2081 | eif->failed = TRUE; | |
2082 | return FALSE; | |
2083 | } | |
2084 | } | |
2085 | } | |
2086 | else | |
2087 | { | |
2088 | /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol | |
2089 | was first seen in a non-ELF file. Fortunately, if the symbol | |
2090 | was first seen in an ELF file, we're probably OK unless the | |
2091 | symbol was defined in a non-ELF file. Catch that case here. | |
2092 | FIXME: We're still in trouble if the symbol was first seen in | |
2093 | a dynamic object, and then later in a non-ELF regular object. */ | |
2094 | if ((h->root.type == bfd_link_hash_defined | |
2095 | || h->root.type == bfd_link_hash_defweak) | |
2096 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2097 | && (h->root.u.def.section->owner != NULL | |
2098 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2099 | != bfd_target_elf_flavour) | |
2100 | : (bfd_is_abs_section (h->root.u.def.section) | |
2101 | && (h->elf_link_hash_flags | |
2102 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0))) | |
2103 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2104 | } | |
2105 | ||
2106 | /* If this is a final link, and the symbol was defined as a common | |
2107 | symbol in a regular object file, and there was no definition in | |
2108 | any dynamic object, then the linker will have allocated space for | |
2109 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR | |
2110 | flag will not have been set. */ | |
2111 | if (h->root.type == bfd_link_hash_defined | |
2112 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2113 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
2114 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2115 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
2116 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2117 | ||
2118 | /* If -Bsymbolic was used (which means to bind references to global | |
2119 | symbols to the definition within the shared object), and this | |
2120 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2121 | need a PLT entry. Likewise, if the symbol has non-default |
2122 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2123 | will force it local. */ |
45d6a902 AM |
2124 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 |
2125 | && eif->info->shared | |
0eddce27 | 2126 | && is_elf_hash_table (eif->info->hash) |
45d6a902 | 2127 | && (eif->info->symbolic |
c1be741f | 2128 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
45d6a902 AM |
2129 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
2130 | { | |
9c5bfbb7 | 2131 | const struct elf_backend_data *bed; |
45d6a902 AM |
2132 | bfd_boolean force_local; |
2133 | ||
2134 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2135 | ||
2136 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2137 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2138 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2139 | } | |
2140 | ||
2141 | /* If a weak undefined symbol has non-default visibility, we also | |
2142 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2143 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2144 | && h->root.type == bfd_link_hash_undefweak) |
2145 | { | |
9c5bfbb7 | 2146 | const struct elf_backend_data *bed; |
45d6a902 AM |
2147 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); |
2148 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2149 | } | |
2150 | ||
2151 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2152 | the real definition in the dynamic object, copy interesting flags | |
2153 | over to the real definition. */ | |
2154 | if (h->weakdef != NULL) | |
2155 | { | |
2156 | struct elf_link_hash_entry *weakdef; | |
2157 | ||
2158 | weakdef = h->weakdef; | |
2159 | if (h->root.type == bfd_link_hash_indirect) | |
2160 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2161 | ||
2162 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2163 | || h->root.type == bfd_link_hash_defweak); | |
2164 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2165 | || weakdef->root.type == bfd_link_hash_defweak); | |
2166 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
2167 | ||
2168 | /* If the real definition is defined by a regular object file, | |
2169 | don't do anything special. See the longer description in | |
2170 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
2171 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2172 | h->weakdef = NULL; | |
2173 | else | |
2174 | { | |
9c5bfbb7 | 2175 | const struct elf_backend_data *bed; |
45d6a902 AM |
2176 | |
2177 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2178 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2179 | } | |
2180 | } | |
2181 | ||
2182 | return TRUE; | |
2183 | } | |
2184 | ||
2185 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2186 | called via elf_link_hash_traverse, and also calls itself | |
2187 | recursively. */ | |
2188 | ||
2189 | bfd_boolean | |
268b6b39 | 2190 | _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) |
45d6a902 | 2191 | { |
268b6b39 | 2192 | struct elf_info_failed *eif = data; |
45d6a902 | 2193 | bfd *dynobj; |
9c5bfbb7 | 2194 | const struct elf_backend_data *bed; |
45d6a902 | 2195 | |
0eddce27 | 2196 | if (! is_elf_hash_table (eif->info->hash)) |
45d6a902 AM |
2197 | return FALSE; |
2198 | ||
2199 | if (h->root.type == bfd_link_hash_warning) | |
2200 | { | |
2201 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2202 | h->got = elf_hash_table (eif->info)->init_offset; | |
2203 | ||
2204 | /* When warning symbols are created, they **replace** the "real" | |
2205 | entry in the hash table, thus we never get to see the real | |
2206 | symbol in a hash traversal. So look at it now. */ | |
2207 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2208 | } | |
2209 | ||
2210 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2211 | if (h->root.type == bfd_link_hash_indirect) | |
2212 | return TRUE; | |
2213 | ||
2214 | /* Fix the symbol flags. */ | |
2215 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2216 | return FALSE; | |
2217 | ||
2218 | /* If this symbol does not require a PLT entry, and it is not | |
2219 | defined by a dynamic object, or is not referenced by a regular | |
2220 | object, ignore it. We do have to handle a weak defined symbol, | |
2221 | even if no regular object refers to it, if we decided to add it | |
2222 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2223 | about symbols which are defined by one dynamic object and | |
2224 | referenced by another one? */ | |
2225 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 | |
2226 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
2227 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2228 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 | |
2229 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) | |
2230 | { | |
2231 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2232 | return TRUE; | |
2233 | } | |
2234 | ||
2235 | /* If we've already adjusted this symbol, don't do it again. This | |
2236 | can happen via a recursive call. */ | |
2237 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
2238 | return TRUE; | |
2239 | ||
2240 | /* Don't look at this symbol again. Note that we must set this | |
2241 | after checking the above conditions, because we may look at a | |
2242 | symbol once, decide not to do anything, and then get called | |
2243 | recursively later after REF_REGULAR is set below. */ | |
2244 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
2245 | ||
2246 | /* If this is a weak definition, and we know a real definition, and | |
2247 | the real symbol is not itself defined by a regular object file, | |
2248 | then get a good value for the real definition. We handle the | |
2249 | real symbol first, for the convenience of the backend routine. | |
2250 | ||
2251 | Note that there is a confusing case here. If the real definition | |
2252 | is defined by a regular object file, we don't get the real symbol | |
2253 | from the dynamic object, but we do get the weak symbol. If the | |
2254 | processor backend uses a COPY reloc, then if some routine in the | |
2255 | dynamic object changes the real symbol, we will not see that | |
2256 | change in the corresponding weak symbol. This is the way other | |
2257 | ELF linkers work as well, and seems to be a result of the shared | |
2258 | library model. | |
2259 | ||
2260 | I will clarify this issue. Most SVR4 shared libraries define the | |
2261 | variable _timezone and define timezone as a weak synonym. The | |
2262 | tzset call changes _timezone. If you write | |
2263 | extern int timezone; | |
2264 | int _timezone = 5; | |
2265 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2266 | you might expect that, since timezone is a synonym for _timezone, | |
2267 | the same number will print both times. However, if the processor | |
2268 | backend uses a COPY reloc, then actually timezone will be copied | |
2269 | into your process image, and, since you define _timezone | |
2270 | yourself, _timezone will not. Thus timezone and _timezone will | |
2271 | wind up at different memory locations. The tzset call will set | |
2272 | _timezone, leaving timezone unchanged. */ | |
2273 | ||
2274 | if (h->weakdef != NULL) | |
2275 | { | |
2276 | /* If we get to this point, we know there is an implicit | |
2277 | reference by a regular object file via the weak symbol H. | |
2278 | FIXME: Is this really true? What if the traversal finds | |
2279 | H->WEAKDEF before it finds H? */ | |
2280 | h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2281 | ||
268b6b39 | 2282 | if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif)) |
45d6a902 AM |
2283 | return FALSE; |
2284 | } | |
2285 | ||
2286 | /* If a symbol has no type and no size and does not require a PLT | |
2287 | entry, then we are probably about to do the wrong thing here: we | |
2288 | are probably going to create a COPY reloc for an empty object. | |
2289 | This case can arise when a shared object is built with assembly | |
2290 | code, and the assembly code fails to set the symbol type. */ | |
2291 | if (h->size == 0 | |
2292 | && h->type == STT_NOTYPE | |
2293 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
2294 | (*_bfd_error_handler) | |
2295 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2296 | h->root.root.string); | |
2297 | ||
2298 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2299 | bed = get_elf_backend_data (dynobj); | |
2300 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2301 | { | |
2302 | eif->failed = TRUE; | |
2303 | return FALSE; | |
2304 | } | |
2305 | ||
2306 | return TRUE; | |
2307 | } | |
2308 | ||
2309 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2310 | to reflect the object merging within the sections. */ | |
2311 | ||
2312 | bfd_boolean | |
268b6b39 | 2313 | _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) |
45d6a902 AM |
2314 | { |
2315 | asection *sec; | |
2316 | ||
2317 | if (h->root.type == bfd_link_hash_warning) | |
2318 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2319 | ||
2320 | if ((h->root.type == bfd_link_hash_defined | |
2321 | || h->root.type == bfd_link_hash_defweak) | |
2322 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2323 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2324 | { | |
268b6b39 | 2325 | bfd *output_bfd = data; |
45d6a902 AM |
2326 | |
2327 | h->root.u.def.value = | |
2328 | _bfd_merged_section_offset (output_bfd, | |
2329 | &h->root.u.def.section, | |
2330 | elf_section_data (sec)->sec_info, | |
268b6b39 | 2331 | h->root.u.def.value, 0); |
45d6a902 AM |
2332 | } |
2333 | ||
2334 | return TRUE; | |
2335 | } | |
986a241f RH |
2336 | |
2337 | /* Returns false if the symbol referred to by H should be considered | |
2338 | to resolve local to the current module, and true if it should be | |
2339 | considered to bind dynamically. */ | |
2340 | ||
2341 | bfd_boolean | |
268b6b39 AM |
2342 | _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, |
2343 | struct bfd_link_info *info, | |
2344 | bfd_boolean ignore_protected) | |
986a241f RH |
2345 | { |
2346 | bfd_boolean binding_stays_local_p; | |
2347 | ||
2348 | if (h == NULL) | |
2349 | return FALSE; | |
2350 | ||
2351 | while (h->root.type == bfd_link_hash_indirect | |
2352 | || h->root.type == bfd_link_hash_warning) | |
2353 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2354 | ||
2355 | /* If it was forced local, then clearly it's not dynamic. */ | |
2356 | if (h->dynindx == -1) | |
2357 | return FALSE; | |
2358 | if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) | |
2359 | return FALSE; | |
2360 | ||
2361 | /* Identify the cases where name binding rules say that a | |
2362 | visible symbol resolves locally. */ | |
2363 | binding_stays_local_p = info->executable || info->symbolic; | |
2364 | ||
2365 | switch (ELF_ST_VISIBILITY (h->other)) | |
2366 | { | |
2367 | case STV_INTERNAL: | |
2368 | case STV_HIDDEN: | |
2369 | return FALSE; | |
2370 | ||
2371 | case STV_PROTECTED: | |
2372 | /* Proper resolution for function pointer equality may require | |
2373 | that these symbols perhaps be resolved dynamically, even though | |
2374 | we should be resolving them to the current module. */ | |
2375 | if (!ignore_protected) | |
2376 | binding_stays_local_p = TRUE; | |
2377 | break; | |
2378 | ||
2379 | default: | |
986a241f RH |
2380 | break; |
2381 | } | |
2382 | ||
aa37626c L |
2383 | /* If it isn't defined locally, then clearly it's dynamic. */ |
2384 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2385 | return TRUE; | |
2386 | ||
986a241f RH |
2387 | /* Otherwise, the symbol is dynamic if binding rules don't tell |
2388 | us that it remains local. */ | |
2389 | return !binding_stays_local_p; | |
2390 | } | |
f6c52c13 AM |
2391 | |
2392 | /* Return true if the symbol referred to by H should be considered | |
2393 | to resolve local to the current module, and false otherwise. Differs | |
2394 | from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of | |
2395 | undefined symbols and weak symbols. */ | |
2396 | ||
2397 | bfd_boolean | |
268b6b39 AM |
2398 | _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, |
2399 | struct bfd_link_info *info, | |
2400 | bfd_boolean local_protected) | |
f6c52c13 AM |
2401 | { |
2402 | /* If it's a local sym, of course we resolve locally. */ | |
2403 | if (h == NULL) | |
2404 | return TRUE; | |
2405 | ||
2406 | /* If we don't have a definition in a regular file, then we can't | |
2407 | resolve locally. The sym is either undefined or dynamic. */ | |
2408 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2409 | return FALSE; | |
2410 | ||
2411 | /* Forced local symbols resolve locally. */ | |
2412 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
2413 | return TRUE; | |
2414 | ||
2415 | /* As do non-dynamic symbols. */ | |
2416 | if (h->dynindx == -1) | |
2417 | return TRUE; | |
2418 | ||
2419 | /* At this point, we know the symbol is defined and dynamic. In an | |
2420 | executable it must resolve locally, likewise when building symbolic | |
2421 | shared libraries. */ | |
2422 | if (info->executable || info->symbolic) | |
2423 | return TRUE; | |
2424 | ||
2425 | /* Now deal with defined dynamic symbols in shared libraries. Ones | |
2426 | with default visibility might not resolve locally. */ | |
2427 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
2428 | return FALSE; | |
2429 | ||
2430 | /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ | |
2431 | if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) | |
2432 | return TRUE; | |
2433 | ||
2434 | /* Function pointer equality tests may require that STV_PROTECTED | |
2435 | symbols be treated as dynamic symbols, even when we know that the | |
2436 | dynamic linker will resolve them locally. */ | |
2437 | return local_protected; | |
2438 | } | |
e1918d23 AM |
2439 | |
2440 | /* Caches some TLS segment info, and ensures that the TLS segment vma is | |
2441 | aligned. Returns the first TLS output section. */ | |
2442 | ||
2443 | struct bfd_section * | |
2444 | _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) | |
2445 | { | |
2446 | struct bfd_section *sec, *tls; | |
2447 | unsigned int align = 0; | |
2448 | ||
2449 | for (sec = obfd->sections; sec != NULL; sec = sec->next) | |
2450 | if ((sec->flags & SEC_THREAD_LOCAL) != 0) | |
2451 | break; | |
2452 | tls = sec; | |
2453 | ||
2454 | for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) | |
2455 | if (sec->alignment_power > align) | |
2456 | align = sec->alignment_power; | |
2457 | ||
2458 | elf_hash_table (info)->tls_sec = tls; | |
2459 | ||
2460 | /* Ensure the alignment of the first section is the largest alignment, | |
2461 | so that the tls segment starts aligned. */ | |
2462 | if (tls != NULL) | |
2463 | tls->alignment_power = align; | |
2464 | ||
2465 | return tls; | |
2466 | } | |
0ad989f9 L |
2467 | |
2468 | /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ | |
2469 | static bfd_boolean | |
2470 | is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, | |
2471 | Elf_Internal_Sym *sym) | |
2472 | { | |
2473 | /* Local symbols do not count, but target specific ones might. */ | |
2474 | if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL | |
2475 | && ELF_ST_BIND (sym->st_info) < STB_LOOS) | |
2476 | return FALSE; | |
2477 | ||
2478 | /* Function symbols do not count. */ | |
2479 | if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) | |
2480 | return FALSE; | |
2481 | ||
2482 | /* If the section is undefined, then so is the symbol. */ | |
2483 | if (sym->st_shndx == SHN_UNDEF) | |
2484 | return FALSE; | |
2485 | ||
2486 | /* If the symbol is defined in the common section, then | |
2487 | it is a common definition and so does not count. */ | |
2488 | if (sym->st_shndx == SHN_COMMON) | |
2489 | return FALSE; | |
2490 | ||
2491 | /* If the symbol is in a target specific section then we | |
2492 | must rely upon the backend to tell us what it is. */ | |
2493 | if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) | |
2494 | /* FIXME - this function is not coded yet: | |
2495 | ||
2496 | return _bfd_is_global_symbol_definition (abfd, sym); | |
2497 | ||
2498 | Instead for now assume that the definition is not global, | |
2499 | Even if this is wrong, at least the linker will behave | |
2500 | in the same way that it used to do. */ | |
2501 | return FALSE; | |
2502 | ||
2503 | return TRUE; | |
2504 | } | |
2505 | ||
2506 | /* Search the symbol table of the archive element of the archive ABFD | |
2507 | whose archive map contains a mention of SYMDEF, and determine if | |
2508 | the symbol is defined in this element. */ | |
2509 | static bfd_boolean | |
2510 | elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) | |
2511 | { | |
2512 | Elf_Internal_Shdr * hdr; | |
2513 | bfd_size_type symcount; | |
2514 | bfd_size_type extsymcount; | |
2515 | bfd_size_type extsymoff; | |
2516 | Elf_Internal_Sym *isymbuf; | |
2517 | Elf_Internal_Sym *isym; | |
2518 | Elf_Internal_Sym *isymend; | |
2519 | bfd_boolean result; | |
2520 | ||
2521 | abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
2522 | if (abfd == NULL) | |
2523 | return FALSE; | |
2524 | ||
2525 | if (! bfd_check_format (abfd, bfd_object)) | |
2526 | return FALSE; | |
2527 | ||
2528 | /* If we have already included the element containing this symbol in the | |
2529 | link then we do not need to include it again. Just claim that any symbol | |
2530 | it contains is not a definition, so that our caller will not decide to | |
2531 | (re)include this element. */ | |
2532 | if (abfd->archive_pass) | |
2533 | return FALSE; | |
2534 | ||
2535 | /* Select the appropriate symbol table. */ | |
2536 | if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) | |
2537 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2538 | else | |
2539 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2540 | ||
2541 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2542 | ||
2543 | /* The sh_info field of the symtab header tells us where the | |
2544 | external symbols start. We don't care about the local symbols. */ | |
2545 | if (elf_bad_symtab (abfd)) | |
2546 | { | |
2547 | extsymcount = symcount; | |
2548 | extsymoff = 0; | |
2549 | } | |
2550 | else | |
2551 | { | |
2552 | extsymcount = symcount - hdr->sh_info; | |
2553 | extsymoff = hdr->sh_info; | |
2554 | } | |
2555 | ||
2556 | if (extsymcount == 0) | |
2557 | return FALSE; | |
2558 | ||
2559 | /* Read in the symbol table. */ | |
2560 | isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, | |
2561 | NULL, NULL, NULL); | |
2562 | if (isymbuf == NULL) | |
2563 | return FALSE; | |
2564 | ||
2565 | /* Scan the symbol table looking for SYMDEF. */ | |
2566 | result = FALSE; | |
2567 | for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) | |
2568 | { | |
2569 | const char *name; | |
2570 | ||
2571 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
2572 | isym->st_name); | |
2573 | if (name == NULL) | |
2574 | break; | |
2575 | ||
2576 | if (strcmp (name, symdef->name) == 0) | |
2577 | { | |
2578 | result = is_global_data_symbol_definition (abfd, isym); | |
2579 | break; | |
2580 | } | |
2581 | } | |
2582 | ||
2583 | free (isymbuf); | |
2584 | ||
2585 | return result; | |
2586 | } | |
2587 | \f | |
5a580b3a AM |
2588 | /* Add an entry to the .dynamic table. */ |
2589 | ||
2590 | bfd_boolean | |
2591 | _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, | |
2592 | bfd_vma tag, | |
2593 | bfd_vma val) | |
2594 | { | |
2595 | struct elf_link_hash_table *hash_table; | |
2596 | const struct elf_backend_data *bed; | |
2597 | asection *s; | |
2598 | bfd_size_type newsize; | |
2599 | bfd_byte *newcontents; | |
2600 | Elf_Internal_Dyn dyn; | |
2601 | ||
2602 | hash_table = elf_hash_table (info); | |
2603 | if (! is_elf_hash_table (hash_table)) | |
2604 | return FALSE; | |
2605 | ||
2606 | bed = get_elf_backend_data (hash_table->dynobj); | |
2607 | s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2608 | BFD_ASSERT (s != NULL); | |
2609 | ||
2610 | newsize = s->_raw_size + bed->s->sizeof_dyn; | |
2611 | newcontents = bfd_realloc (s->contents, newsize); | |
2612 | if (newcontents == NULL) | |
2613 | return FALSE; | |
2614 | ||
2615 | dyn.d_tag = tag; | |
2616 | dyn.d_un.d_val = val; | |
2617 | bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->_raw_size); | |
2618 | ||
2619 | s->_raw_size = newsize; | |
2620 | s->contents = newcontents; | |
2621 | ||
2622 | return TRUE; | |
2623 | } | |
2624 | ||
2625 | /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, | |
2626 | otherwise just check whether one already exists. Returns -1 on error, | |
2627 | 1 if a DT_NEEDED tag already exists, and 0 on success. */ | |
2628 | ||
2629 | int | |
2630 | _bfd_elf_add_dt_needed_tag (struct bfd_link_info *info, | |
2631 | const char *soname, | |
2632 | bfd_boolean do_it) | |
2633 | { | |
2634 | struct elf_link_hash_table *hash_table; | |
2635 | bfd_size_type oldsize; | |
2636 | bfd_size_type strindex; | |
2637 | ||
2638 | hash_table = elf_hash_table (info); | |
2639 | oldsize = _bfd_elf_strtab_size (hash_table->dynstr); | |
2640 | strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); | |
2641 | if (strindex == (bfd_size_type) -1) | |
2642 | return -1; | |
2643 | ||
2644 | if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) | |
2645 | { | |
2646 | asection *sdyn; | |
2647 | const struct elf_backend_data *bed; | |
2648 | bfd_byte *extdyn; | |
2649 | ||
2650 | bed = get_elf_backend_data (hash_table->dynobj); | |
2651 | sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); | |
2652 | BFD_ASSERT (sdyn != NULL); | |
2653 | ||
2654 | for (extdyn = sdyn->contents; | |
2655 | extdyn < sdyn->contents + sdyn->_raw_size; | |
2656 | extdyn += bed->s->sizeof_dyn) | |
2657 | { | |
2658 | Elf_Internal_Dyn dyn; | |
2659 | ||
2660 | bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); | |
2661 | if (dyn.d_tag == DT_NEEDED | |
2662 | && dyn.d_un.d_val == strindex) | |
2663 | { | |
2664 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2665 | return 1; | |
2666 | } | |
2667 | } | |
2668 | } | |
2669 | ||
2670 | if (do_it) | |
2671 | { | |
2672 | if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) | |
2673 | return -1; | |
2674 | } | |
2675 | else | |
2676 | /* We were just checking for existence of the tag. */ | |
2677 | _bfd_elf_strtab_delref (hash_table->dynstr, strindex); | |
2678 | ||
2679 | return 0; | |
2680 | } | |
2681 | ||
2682 | /* Sort symbol by value and section. */ | |
2683 | int | |
2684 | _bfd_elf_sort_symbol (const void *arg1, const void *arg2) | |
2685 | { | |
2686 | const struct elf_link_hash_entry *h1; | |
2687 | const struct elf_link_hash_entry *h2; | |
2688 | bfd_signed_vma vdiff; | |
2689 | ||
2690 | h1 = *(const struct elf_link_hash_entry **) arg1; | |
2691 | h2 = *(const struct elf_link_hash_entry **) arg2; | |
2692 | vdiff = h1->root.u.def.value - h2->root.u.def.value; | |
2693 | if (vdiff != 0) | |
2694 | return vdiff > 0 ? 1 : -1; | |
2695 | else | |
2696 | { | |
2697 | long sdiff = h1->root.u.def.section - h2->root.u.def.section; | |
2698 | if (sdiff != 0) | |
2699 | return sdiff > 0 ? 1 : -1; | |
2700 | } | |
2701 | return 0; | |
2702 | } | |
2703 | \f | |
2704 | /* This function is used to adjust offsets into .dynstr for | |
2705 | dynamic symbols. This is called via elf_link_hash_traverse. */ | |
2706 | ||
2707 | static bfd_boolean | |
2708 | elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) | |
2709 | { | |
2710 | struct elf_strtab_hash *dynstr = data; | |
2711 | ||
2712 | if (h->root.type == bfd_link_hash_warning) | |
2713 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2714 | ||
2715 | if (h->dynindx != -1) | |
2716 | h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); | |
2717 | return TRUE; | |
2718 | } | |
2719 | ||
2720 | /* Assign string offsets in .dynstr, update all structures referencing | |
2721 | them. */ | |
2722 | ||
2723 | bfd_boolean | |
2724 | _bfd_elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) | |
2725 | { | |
2726 | struct elf_link_hash_table *hash_table = elf_hash_table (info); | |
2727 | struct elf_link_local_dynamic_entry *entry; | |
2728 | struct elf_strtab_hash *dynstr = hash_table->dynstr; | |
2729 | bfd *dynobj = hash_table->dynobj; | |
2730 | asection *sdyn; | |
2731 | bfd_size_type size; | |
2732 | const struct elf_backend_data *bed; | |
2733 | bfd_byte *extdyn; | |
2734 | ||
2735 | _bfd_elf_strtab_finalize (dynstr); | |
2736 | size = _bfd_elf_strtab_size (dynstr); | |
2737 | ||
2738 | bed = get_elf_backend_data (dynobj); | |
2739 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
2740 | BFD_ASSERT (sdyn != NULL); | |
2741 | ||
2742 | /* Update all .dynamic entries referencing .dynstr strings. */ | |
2743 | for (extdyn = sdyn->contents; | |
2744 | extdyn < sdyn->contents + sdyn->_raw_size; | |
2745 | extdyn += bed->s->sizeof_dyn) | |
2746 | { | |
2747 | Elf_Internal_Dyn dyn; | |
2748 | ||
2749 | bed->s->swap_dyn_in (dynobj, extdyn, &dyn); | |
2750 | switch (dyn.d_tag) | |
2751 | { | |
2752 | case DT_STRSZ: | |
2753 | dyn.d_un.d_val = size; | |
2754 | break; | |
2755 | case DT_NEEDED: | |
2756 | case DT_SONAME: | |
2757 | case DT_RPATH: | |
2758 | case DT_RUNPATH: | |
2759 | case DT_FILTER: | |
2760 | case DT_AUXILIARY: | |
2761 | dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); | |
2762 | break; | |
2763 | default: | |
2764 | continue; | |
2765 | } | |
2766 | bed->s->swap_dyn_out (dynobj, &dyn, extdyn); | |
2767 | } | |
2768 | ||
2769 | /* Now update local dynamic symbols. */ | |
2770 | for (entry = hash_table->dynlocal; entry ; entry = entry->next) | |
2771 | entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, | |
2772 | entry->isym.st_name); | |
2773 | ||
2774 | /* And the rest of dynamic symbols. */ | |
2775 | elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); | |
2776 | ||
2777 | /* Adjust version definitions. */ | |
2778 | if (elf_tdata (output_bfd)->cverdefs) | |
2779 | { | |
2780 | asection *s; | |
2781 | bfd_byte *p; | |
2782 | bfd_size_type i; | |
2783 | Elf_Internal_Verdef def; | |
2784 | Elf_Internal_Verdaux defaux; | |
2785 | ||
2786 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2787 | p = s->contents; | |
2788 | do | |
2789 | { | |
2790 | _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, | |
2791 | &def); | |
2792 | p += sizeof (Elf_External_Verdef); | |
2793 | for (i = 0; i < def.vd_cnt; ++i) | |
2794 | { | |
2795 | _bfd_elf_swap_verdaux_in (output_bfd, | |
2796 | (Elf_External_Verdaux *) p, &defaux); | |
2797 | defaux.vda_name = _bfd_elf_strtab_offset (dynstr, | |
2798 | defaux.vda_name); | |
2799 | _bfd_elf_swap_verdaux_out (output_bfd, | |
2800 | &defaux, (Elf_External_Verdaux *) p); | |
2801 | p += sizeof (Elf_External_Verdaux); | |
2802 | } | |
2803 | } | |
2804 | while (def.vd_next); | |
2805 | } | |
2806 | ||
2807 | /* Adjust version references. */ | |
2808 | if (elf_tdata (output_bfd)->verref) | |
2809 | { | |
2810 | asection *s; | |
2811 | bfd_byte *p; | |
2812 | bfd_size_type i; | |
2813 | Elf_Internal_Verneed need; | |
2814 | Elf_Internal_Vernaux needaux; | |
2815 | ||
2816 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2817 | p = s->contents; | |
2818 | do | |
2819 | { | |
2820 | _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, | |
2821 | &need); | |
2822 | need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); | |
2823 | _bfd_elf_swap_verneed_out (output_bfd, &need, | |
2824 | (Elf_External_Verneed *) p); | |
2825 | p += sizeof (Elf_External_Verneed); | |
2826 | for (i = 0; i < need.vn_cnt; ++i) | |
2827 | { | |
2828 | _bfd_elf_swap_vernaux_in (output_bfd, | |
2829 | (Elf_External_Vernaux *) p, &needaux); | |
2830 | needaux.vna_name = _bfd_elf_strtab_offset (dynstr, | |
2831 | needaux.vna_name); | |
2832 | _bfd_elf_swap_vernaux_out (output_bfd, | |
2833 | &needaux, | |
2834 | (Elf_External_Vernaux *) p); | |
2835 | p += sizeof (Elf_External_Vernaux); | |
2836 | } | |
2837 | } | |
2838 | while (need.vn_next); | |
2839 | } | |
2840 | ||
2841 | return TRUE; | |
2842 | } | |
2843 | \f | |
0ad989f9 L |
2844 | /* Add symbols from an ELF archive file to the linker hash table. We |
2845 | don't use _bfd_generic_link_add_archive_symbols because of a | |
2846 | problem which arises on UnixWare. The UnixWare libc.so is an | |
2847 | archive which includes an entry libc.so.1 which defines a bunch of | |
2848 | symbols. The libc.so archive also includes a number of other | |
2849 | object files, which also define symbols, some of which are the same | |
2850 | as those defined in libc.so.1. Correct linking requires that we | |
2851 | consider each object file in turn, and include it if it defines any | |
2852 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
2853 | this; it looks through the list of undefined symbols, and includes | |
2854 | any object file which defines them. When this algorithm is used on | |
2855 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
2856 | bunch of symbols. This means that some of the other objects in the | |
2857 | archive are not included in the link, which is incorrect since they | |
2858 | precede libc.so.1 in the archive. | |
2859 | ||
2860 | Fortunately, ELF archive handling is simpler than that done by | |
2861 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
2862 | oddities. In ELF, if we find a symbol in the archive map, and the | |
2863 | symbol is currently undefined, we know that we must pull in that | |
2864 | object file. | |
2865 | ||
2866 | Unfortunately, we do have to make multiple passes over the symbol | |
2867 | table until nothing further is resolved. */ | |
2868 | ||
2869 | bfd_boolean | |
2870 | _bfd_elf_link_add_archive_symbols (bfd *abfd, | |
2871 | struct bfd_link_info *info) | |
2872 | { | |
2873 | symindex c; | |
2874 | bfd_boolean *defined = NULL; | |
2875 | bfd_boolean *included = NULL; | |
2876 | carsym *symdefs; | |
2877 | bfd_boolean loop; | |
2878 | bfd_size_type amt; | |
2879 | ||
2880 | if (! bfd_has_map (abfd)) | |
2881 | { | |
2882 | /* An empty archive is a special case. */ | |
2883 | if (bfd_openr_next_archived_file (abfd, NULL) == NULL) | |
2884 | return TRUE; | |
2885 | bfd_set_error (bfd_error_no_armap); | |
2886 | return FALSE; | |
2887 | } | |
2888 | ||
2889 | /* Keep track of all symbols we know to be already defined, and all | |
2890 | files we know to be already included. This is to speed up the | |
2891 | second and subsequent passes. */ | |
2892 | c = bfd_ardata (abfd)->symdef_count; | |
2893 | if (c == 0) | |
2894 | return TRUE; | |
2895 | amt = c; | |
2896 | amt *= sizeof (bfd_boolean); | |
2897 | defined = bfd_zmalloc (amt); | |
2898 | included = bfd_zmalloc (amt); | |
2899 | if (defined == NULL || included == NULL) | |
2900 | goto error_return; | |
2901 | ||
2902 | symdefs = bfd_ardata (abfd)->symdefs; | |
2903 | ||
2904 | do | |
2905 | { | |
2906 | file_ptr last; | |
2907 | symindex i; | |
2908 | carsym *symdef; | |
2909 | carsym *symdefend; | |
2910 | ||
2911 | loop = FALSE; | |
2912 | last = -1; | |
2913 | ||
2914 | symdef = symdefs; | |
2915 | symdefend = symdef + c; | |
2916 | for (i = 0; symdef < symdefend; symdef++, i++) | |
2917 | { | |
2918 | struct elf_link_hash_entry *h; | |
2919 | bfd *element; | |
2920 | struct bfd_link_hash_entry *undefs_tail; | |
2921 | symindex mark; | |
2922 | ||
2923 | if (defined[i] || included[i]) | |
2924 | continue; | |
2925 | if (symdef->file_offset == last) | |
2926 | { | |
2927 | included[i] = TRUE; | |
2928 | continue; | |
2929 | } | |
2930 | ||
2931 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, | |
2932 | FALSE, FALSE, FALSE); | |
2933 | ||
2934 | if (h == NULL) | |
2935 | { | |
2936 | char *p, *copy; | |
2937 | size_t len, first; | |
2938 | ||
2939 | /* If this is a default version (the name contains @@), | |
2940 | look up the symbol again with only one `@' as well | |
2941 | as without the version. The effect is that references | |
2942 | to the symbol with and without the version will be | |
2943 | matched by the default symbol in the archive. */ | |
2944 | ||
2945 | p = strchr (symdef->name, ELF_VER_CHR); | |
2946 | if (p == NULL || p[1] != ELF_VER_CHR) | |
2947 | continue; | |
2948 | ||
2949 | /* First check with only one `@'. */ | |
2950 | len = strlen (symdef->name); | |
2951 | copy = bfd_alloc (abfd, len); | |
2952 | if (copy == NULL) | |
2953 | goto error_return; | |
2954 | first = p - symdef->name + 1; | |
2955 | memcpy (copy, symdef->name, first); | |
2956 | memcpy (copy + first, symdef->name + first + 1, len - first); | |
2957 | ||
2958 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
2959 | FALSE, FALSE, FALSE); | |
2960 | ||
2961 | if (h == NULL) | |
2962 | { | |
2963 | /* We also need to check references to the symbol | |
2964 | without the version. */ | |
2965 | ||
2966 | copy[first - 1] = '\0'; | |
2967 | h = elf_link_hash_lookup (elf_hash_table (info), | |
2968 | copy, FALSE, FALSE, FALSE); | |
2969 | } | |
2970 | ||
2971 | bfd_release (abfd, copy); | |
2972 | } | |
2973 | ||
2974 | if (h == NULL) | |
2975 | continue; | |
2976 | ||
2977 | if (h->root.type == bfd_link_hash_common) | |
2978 | { | |
2979 | /* We currently have a common symbol. The archive map contains | |
2980 | a reference to this symbol, so we may want to include it. We | |
2981 | only want to include it however, if this archive element | |
2982 | contains a definition of the symbol, not just another common | |
2983 | declaration of it. | |
2984 | ||
2985 | Unfortunately some archivers (including GNU ar) will put | |
2986 | declarations of common symbols into their archive maps, as | |
2987 | well as real definitions, so we cannot just go by the archive | |
2988 | map alone. Instead we must read in the element's symbol | |
2989 | table and check that to see what kind of symbol definition | |
2990 | this is. */ | |
2991 | if (! elf_link_is_defined_archive_symbol (abfd, symdef)) | |
2992 | continue; | |
2993 | } | |
2994 | else if (h->root.type != bfd_link_hash_undefined) | |
2995 | { | |
2996 | if (h->root.type != bfd_link_hash_undefweak) | |
2997 | defined[i] = TRUE; | |
2998 | continue; | |
2999 | } | |
3000 | ||
3001 | /* We need to include this archive member. */ | |
3002 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3003 | if (element == NULL) | |
3004 | goto error_return; | |
3005 | ||
3006 | if (! bfd_check_format (element, bfd_object)) | |
3007 | goto error_return; | |
3008 | ||
3009 | /* Doublecheck that we have not included this object | |
3010 | already--it should be impossible, but there may be | |
3011 | something wrong with the archive. */ | |
3012 | if (element->archive_pass != 0) | |
3013 | { | |
3014 | bfd_set_error (bfd_error_bad_value); | |
3015 | goto error_return; | |
3016 | } | |
3017 | element->archive_pass = 1; | |
3018 | ||
3019 | undefs_tail = info->hash->undefs_tail; | |
3020 | ||
3021 | if (! (*info->callbacks->add_archive_element) (info, element, | |
3022 | symdef->name)) | |
3023 | goto error_return; | |
3024 | if (! bfd_link_add_symbols (element, info)) | |
3025 | goto error_return; | |
3026 | ||
3027 | /* If there are any new undefined symbols, we need to make | |
3028 | another pass through the archive in order to see whether | |
3029 | they can be defined. FIXME: This isn't perfect, because | |
3030 | common symbols wind up on undefs_tail and because an | |
3031 | undefined symbol which is defined later on in this pass | |
3032 | does not require another pass. This isn't a bug, but it | |
3033 | does make the code less efficient than it could be. */ | |
3034 | if (undefs_tail != info->hash->undefs_tail) | |
3035 | loop = TRUE; | |
3036 | ||
3037 | /* Look backward to mark all symbols from this object file | |
3038 | which we have already seen in this pass. */ | |
3039 | mark = i; | |
3040 | do | |
3041 | { | |
3042 | included[mark] = TRUE; | |
3043 | if (mark == 0) | |
3044 | break; | |
3045 | --mark; | |
3046 | } | |
3047 | while (symdefs[mark].file_offset == symdef->file_offset); | |
3048 | ||
3049 | /* We mark subsequent symbols from this object file as we go | |
3050 | on through the loop. */ | |
3051 | last = symdef->file_offset; | |
3052 | } | |
3053 | } | |
3054 | while (loop); | |
3055 | ||
3056 | free (defined); | |
3057 | free (included); | |
3058 | ||
3059 | return TRUE; | |
3060 | ||
3061 | error_return: | |
3062 | if (defined != NULL) | |
3063 | free (defined); | |
3064 | if (included != NULL) | |
3065 | free (included); | |
3066 | return FALSE; | |
3067 | } | |
5a580b3a AM |
3068 | \f |
3069 | /* This function will be called though elf_link_hash_traverse to store | |
3070 | all hash value of the exported symbols in an array. */ | |
3071 | ||
3072 | static bfd_boolean | |
3073 | elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) | |
3074 | { | |
3075 | unsigned long **valuep = data; | |
3076 | const char *name; | |
3077 | char *p; | |
3078 | unsigned long ha; | |
3079 | char *alc = NULL; | |
3080 | ||
3081 | if (h->root.type == bfd_link_hash_warning) | |
3082 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3083 | ||
3084 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
3085 | if (h->dynindx == -1) | |
3086 | return TRUE; | |
3087 | ||
3088 | name = h->root.root.string; | |
3089 | p = strchr (name, ELF_VER_CHR); | |
3090 | if (p != NULL) | |
3091 | { | |
3092 | alc = bfd_malloc (p - name + 1); | |
3093 | memcpy (alc, name, p - name); | |
3094 | alc[p - name] = '\0'; | |
3095 | name = alc; | |
3096 | } | |
3097 | ||
3098 | /* Compute the hash value. */ | |
3099 | ha = bfd_elf_hash (name); | |
3100 | ||
3101 | /* Store the found hash value in the array given as the argument. */ | |
3102 | *(*valuep)++ = ha; | |
3103 | ||
3104 | /* And store it in the struct so that we can put it in the hash table | |
3105 | later. */ | |
3106 | h->elf_hash_value = ha; | |
3107 | ||
3108 | if (alc != NULL) | |
3109 | free (alc); | |
3110 | ||
3111 | return TRUE; | |
3112 | } | |
3113 | ||
3114 | /* Array used to determine the number of hash table buckets to use | |
3115 | based on the number of symbols there are. If there are fewer than | |
3116 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
3117 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
3118 | than 32771 buckets. */ | |
3119 | ||
3120 | static const size_t elf_buckets[] = | |
3121 | { | |
3122 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
3123 | 16411, 32771, 0 | |
3124 | }; | |
3125 | ||
3126 | /* Compute bucket count for hashing table. We do not use a static set | |
3127 | of possible tables sizes anymore. Instead we determine for all | |
3128 | possible reasonable sizes of the table the outcome (i.e., the | |
3129 | number of collisions etc) and choose the best solution. The | |
3130 | weighting functions are not too simple to allow the table to grow | |
3131 | without bounds. Instead one of the weighting factors is the size. | |
3132 | Therefore the result is always a good payoff between few collisions | |
3133 | (= short chain lengths) and table size. */ | |
3134 | static size_t | |
3135 | compute_bucket_count (struct bfd_link_info *info) | |
3136 | { | |
3137 | size_t dynsymcount = elf_hash_table (info)->dynsymcount; | |
3138 | size_t best_size = 0; | |
3139 | unsigned long int *hashcodes; | |
3140 | unsigned long int *hashcodesp; | |
3141 | unsigned long int i; | |
3142 | bfd_size_type amt; | |
3143 | ||
3144 | /* Compute the hash values for all exported symbols. At the same | |
3145 | time store the values in an array so that we could use them for | |
3146 | optimizations. */ | |
3147 | amt = dynsymcount; | |
3148 | amt *= sizeof (unsigned long int); | |
3149 | hashcodes = bfd_malloc (amt); | |
3150 | if (hashcodes == NULL) | |
3151 | return 0; | |
3152 | hashcodesp = hashcodes; | |
3153 | ||
3154 | /* Put all hash values in HASHCODES. */ | |
3155 | elf_link_hash_traverse (elf_hash_table (info), | |
3156 | elf_collect_hash_codes, &hashcodesp); | |
3157 | ||
3158 | /* We have a problem here. The following code to optimize the table | |
3159 | size requires an integer type with more the 32 bits. If | |
3160 | BFD_HOST_U_64_BIT is set we know about such a type. */ | |
3161 | #ifdef BFD_HOST_U_64_BIT | |
3162 | if (info->optimize) | |
3163 | { | |
3164 | unsigned long int nsyms = hashcodesp - hashcodes; | |
3165 | size_t minsize; | |
3166 | size_t maxsize; | |
3167 | BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); | |
3168 | unsigned long int *counts ; | |
3169 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
3170 | const struct elf_backend_data *bed = get_elf_backend_data (dynobj); | |
3171 | ||
3172 | /* Possible optimization parameters: if we have NSYMS symbols we say | |
3173 | that the hashing table must at least have NSYMS/4 and at most | |
3174 | 2*NSYMS buckets. */ | |
3175 | minsize = nsyms / 4; | |
3176 | if (minsize == 0) | |
3177 | minsize = 1; | |
3178 | best_size = maxsize = nsyms * 2; | |
3179 | ||
3180 | /* Create array where we count the collisions in. We must use bfd_malloc | |
3181 | since the size could be large. */ | |
3182 | amt = maxsize; | |
3183 | amt *= sizeof (unsigned long int); | |
3184 | counts = bfd_malloc (amt); | |
3185 | if (counts == NULL) | |
3186 | { | |
3187 | free (hashcodes); | |
3188 | return 0; | |
3189 | } | |
3190 | ||
3191 | /* Compute the "optimal" size for the hash table. The criteria is a | |
3192 | minimal chain length. The minor criteria is (of course) the size | |
3193 | of the table. */ | |
3194 | for (i = minsize; i < maxsize; ++i) | |
3195 | { | |
3196 | /* Walk through the array of hashcodes and count the collisions. */ | |
3197 | BFD_HOST_U_64_BIT max; | |
3198 | unsigned long int j; | |
3199 | unsigned long int fact; | |
3200 | ||
3201 | memset (counts, '\0', i * sizeof (unsigned long int)); | |
3202 | ||
3203 | /* Determine how often each hash bucket is used. */ | |
3204 | for (j = 0; j < nsyms; ++j) | |
3205 | ++counts[hashcodes[j] % i]; | |
3206 | ||
3207 | /* For the weight function we need some information about the | |
3208 | pagesize on the target. This is information need not be 100% | |
3209 | accurate. Since this information is not available (so far) we | |
3210 | define it here to a reasonable default value. If it is crucial | |
3211 | to have a better value some day simply define this value. */ | |
3212 | # ifndef BFD_TARGET_PAGESIZE | |
3213 | # define BFD_TARGET_PAGESIZE (4096) | |
3214 | # endif | |
3215 | ||
3216 | /* We in any case need 2 + NSYMS entries for the size values and | |
3217 | the chains. */ | |
3218 | max = (2 + nsyms) * (bed->s->arch_size / 8); | |
3219 | ||
3220 | # if 1 | |
3221 | /* Variant 1: optimize for short chains. We add the squares | |
3222 | of all the chain lengths (which favors many small chain | |
3223 | over a few long chains). */ | |
3224 | for (j = 0; j < i; ++j) | |
3225 | max += counts[j] * counts[j]; | |
3226 | ||
3227 | /* This adds penalties for the overall size of the table. */ | |
3228 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
3229 | max *= fact * fact; | |
3230 | # else | |
3231 | /* Variant 2: Optimize a lot more for small table. Here we | |
3232 | also add squares of the size but we also add penalties for | |
3233 | empty slots (the +1 term). */ | |
3234 | for (j = 0; j < i; ++j) | |
3235 | max += (1 + counts[j]) * (1 + counts[j]); | |
3236 | ||
3237 | /* The overall size of the table is considered, but not as | |
3238 | strong as in variant 1, where it is squared. */ | |
3239 | fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; | |
3240 | max *= fact; | |
3241 | # endif | |
3242 | ||
3243 | /* Compare with current best results. */ | |
3244 | if (max < best_chlen) | |
3245 | { | |
3246 | best_chlen = max; | |
3247 | best_size = i; | |
3248 | } | |
3249 | } | |
3250 | ||
3251 | free (counts); | |
3252 | } | |
3253 | else | |
3254 | #endif /* defined (BFD_HOST_U_64_BIT) */ | |
3255 | { | |
3256 | /* This is the fallback solution if no 64bit type is available or if we | |
3257 | are not supposed to spend much time on optimizations. We select the | |
3258 | bucket count using a fixed set of numbers. */ | |
3259 | for (i = 0; elf_buckets[i] != 0; i++) | |
3260 | { | |
3261 | best_size = elf_buckets[i]; | |
3262 | if (dynsymcount < elf_buckets[i + 1]) | |
3263 | break; | |
3264 | } | |
3265 | } | |
3266 | ||
3267 | /* Free the arrays we needed. */ | |
3268 | free (hashcodes); | |
3269 | ||
3270 | return best_size; | |
3271 | } | |
3272 | ||
3273 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
3274 | called by the ELF linker emulation before_allocation routine. We | |
3275 | must set the sizes of the sections before the linker sets the | |
3276 | addresses of the various sections. */ | |
3277 | ||
3278 | bfd_boolean | |
3279 | bfd_elf_size_dynamic_sections (bfd *output_bfd, | |
3280 | const char *soname, | |
3281 | const char *rpath, | |
3282 | const char *filter_shlib, | |
3283 | const char * const *auxiliary_filters, | |
3284 | struct bfd_link_info *info, | |
3285 | asection **sinterpptr, | |
3286 | struct bfd_elf_version_tree *verdefs) | |
3287 | { | |
3288 | bfd_size_type soname_indx; | |
3289 | bfd *dynobj; | |
3290 | const struct elf_backend_data *bed; | |
3291 | struct elf_assign_sym_version_info asvinfo; | |
3292 | ||
3293 | *sinterpptr = NULL; | |
3294 | ||
3295 | soname_indx = (bfd_size_type) -1; | |
3296 | ||
3297 | if (!is_elf_hash_table (info->hash)) | |
3298 | return TRUE; | |
3299 | ||
3300 | if (info->execstack) | |
3301 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; | |
3302 | else if (info->noexecstack) | |
3303 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; | |
3304 | else | |
3305 | { | |
3306 | bfd *inputobj; | |
3307 | asection *notesec = NULL; | |
3308 | int exec = 0; | |
3309 | ||
3310 | for (inputobj = info->input_bfds; | |
3311 | inputobj; | |
3312 | inputobj = inputobj->link_next) | |
3313 | { | |
3314 | asection *s; | |
3315 | ||
3316 | if (inputobj->flags & DYNAMIC) | |
3317 | continue; | |
3318 | s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); | |
3319 | if (s) | |
3320 | { | |
3321 | if (s->flags & SEC_CODE) | |
3322 | exec = PF_X; | |
3323 | notesec = s; | |
3324 | } | |
3325 | else | |
3326 | exec = PF_X; | |
3327 | } | |
3328 | if (notesec) | |
3329 | { | |
3330 | elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; | |
3331 | if (exec && info->relocatable | |
3332 | && notesec->output_section != bfd_abs_section_ptr) | |
3333 | notesec->output_section->flags |= SEC_CODE; | |
3334 | } | |
3335 | } | |
3336 | ||
3337 | /* Any syms created from now on start with -1 in | |
3338 | got.refcount/offset and plt.refcount/offset. */ | |
3339 | elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset; | |
3340 | ||
3341 | /* The backend may have to create some sections regardless of whether | |
3342 | we're dynamic or not. */ | |
3343 | bed = get_elf_backend_data (output_bfd); | |
3344 | if (bed->elf_backend_always_size_sections | |
3345 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
3346 | return FALSE; | |
3347 | ||
3348 | dynobj = elf_hash_table (info)->dynobj; | |
3349 | ||
3350 | /* If there were no dynamic objects in the link, there is nothing to | |
3351 | do here. */ | |
3352 | if (dynobj == NULL) | |
3353 | return TRUE; | |
3354 | ||
3355 | if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) | |
3356 | return FALSE; | |
3357 | ||
3358 | if (elf_hash_table (info)->dynamic_sections_created) | |
3359 | { | |
3360 | struct elf_info_failed eif; | |
3361 | struct elf_link_hash_entry *h; | |
3362 | asection *dynstr; | |
3363 | struct bfd_elf_version_tree *t; | |
3364 | struct bfd_elf_version_expr *d; | |
3365 | bfd_boolean all_defined; | |
3366 | ||
3367 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
3368 | BFD_ASSERT (*sinterpptr != NULL || !info->executable); | |
3369 | ||
3370 | if (soname != NULL) | |
3371 | { | |
3372 | soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
3373 | soname, TRUE); | |
3374 | if (soname_indx == (bfd_size_type) -1 | |
3375 | || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
3376 | return FALSE; | |
3377 | } | |
3378 | ||
3379 | if (info->symbolic) | |
3380 | { | |
3381 | if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
3382 | return FALSE; | |
3383 | info->flags |= DF_SYMBOLIC; | |
3384 | } | |
3385 | ||
3386 | if (rpath != NULL) | |
3387 | { | |
3388 | bfd_size_type indx; | |
3389 | ||
3390 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, | |
3391 | TRUE); | |
3392 | if (indx == (bfd_size_type) -1 | |
3393 | || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
3394 | return FALSE; | |
3395 | ||
3396 | if (info->new_dtags) | |
3397 | { | |
3398 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); | |
3399 | if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) | |
3400 | return FALSE; | |
3401 | } | |
3402 | } | |
3403 | ||
3404 | if (filter_shlib != NULL) | |
3405 | { | |
3406 | bfd_size_type indx; | |
3407 | ||
3408 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
3409 | filter_shlib, TRUE); | |
3410 | if (indx == (bfd_size_type) -1 | |
3411 | || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
3412 | return FALSE; | |
3413 | } | |
3414 | ||
3415 | if (auxiliary_filters != NULL) | |
3416 | { | |
3417 | const char * const *p; | |
3418 | ||
3419 | for (p = auxiliary_filters; *p != NULL; p++) | |
3420 | { | |
3421 | bfd_size_type indx; | |
3422 | ||
3423 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
3424 | *p, TRUE); | |
3425 | if (indx == (bfd_size_type) -1 | |
3426 | || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
3427 | return FALSE; | |
3428 | } | |
3429 | } | |
3430 | ||
3431 | eif.info = info; | |
3432 | eif.verdefs = verdefs; | |
3433 | eif.failed = FALSE; | |
3434 | ||
3435 | /* If we are supposed to export all symbols into the dynamic symbol | |
3436 | table (this is not the normal case), then do so. */ | |
3437 | if (info->export_dynamic) | |
3438 | { | |
3439 | elf_link_hash_traverse (elf_hash_table (info), | |
3440 | _bfd_elf_export_symbol, | |
3441 | &eif); | |
3442 | if (eif.failed) | |
3443 | return FALSE; | |
3444 | } | |
3445 | ||
3446 | /* Make all global versions with definition. */ | |
3447 | for (t = verdefs; t != NULL; t = t->next) | |
3448 | for (d = t->globals.list; d != NULL; d = d->next) | |
3449 | if (!d->symver && d->symbol) | |
3450 | { | |
3451 | const char *verstr, *name; | |
3452 | size_t namelen, verlen, newlen; | |
3453 | char *newname, *p; | |
3454 | struct elf_link_hash_entry *newh; | |
3455 | ||
3456 | name = d->symbol; | |
3457 | namelen = strlen (name); | |
3458 | verstr = t->name; | |
3459 | verlen = strlen (verstr); | |
3460 | newlen = namelen + verlen + 3; | |
3461 | ||
3462 | newname = bfd_malloc (newlen); | |
3463 | if (newname == NULL) | |
3464 | return FALSE; | |
3465 | memcpy (newname, name, namelen); | |
3466 | ||
3467 | /* Check the hidden versioned definition. */ | |
3468 | p = newname + namelen; | |
3469 | *p++ = ELF_VER_CHR; | |
3470 | memcpy (p, verstr, verlen + 1); | |
3471 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
3472 | newname, FALSE, FALSE, | |
3473 | FALSE); | |
3474 | if (newh == NULL | |
3475 | || (newh->root.type != bfd_link_hash_defined | |
3476 | && newh->root.type != bfd_link_hash_defweak)) | |
3477 | { | |
3478 | /* Check the default versioned definition. */ | |
3479 | *p++ = ELF_VER_CHR; | |
3480 | memcpy (p, verstr, verlen + 1); | |
3481 | newh = elf_link_hash_lookup (elf_hash_table (info), | |
3482 | newname, FALSE, FALSE, | |
3483 | FALSE); | |
3484 | } | |
3485 | free (newname); | |
3486 | ||
3487 | /* Mark this version if there is a definition and it is | |
3488 | not defined in a shared object. */ | |
3489 | if (newh != NULL | |
3490 | && ((newh->elf_link_hash_flags | |
3491 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0) | |
3492 | && (newh->root.type == bfd_link_hash_defined | |
3493 | || newh->root.type == bfd_link_hash_defweak)) | |
3494 | d->symver = 1; | |
3495 | } | |
3496 | ||
3497 | /* Attach all the symbols to their version information. */ | |
3498 | asvinfo.output_bfd = output_bfd; | |
3499 | asvinfo.info = info; | |
3500 | asvinfo.verdefs = verdefs; | |
3501 | asvinfo.failed = FALSE; | |
3502 | ||
3503 | elf_link_hash_traverse (elf_hash_table (info), | |
3504 | _bfd_elf_link_assign_sym_version, | |
3505 | &asvinfo); | |
3506 | if (asvinfo.failed) | |
3507 | return FALSE; | |
3508 | ||
3509 | if (!info->allow_undefined_version) | |
3510 | { | |
3511 | /* Check if all global versions have a definition. */ | |
3512 | all_defined = TRUE; | |
3513 | for (t = verdefs; t != NULL; t = t->next) | |
3514 | for (d = t->globals.list; d != NULL; d = d->next) | |
3515 | if (!d->symver && !d->script) | |
3516 | { | |
3517 | (*_bfd_error_handler) | |
3518 | (_("%s: undefined version: %s"), | |
3519 | d->pattern, t->name); | |
3520 | all_defined = FALSE; | |
3521 | } | |
3522 | ||
3523 | if (!all_defined) | |
3524 | { | |
3525 | bfd_set_error (bfd_error_bad_value); | |
3526 | return FALSE; | |
3527 | } | |
3528 | } | |
3529 | ||
3530 | /* Find all symbols which were defined in a dynamic object and make | |
3531 | the backend pick a reasonable value for them. */ | |
3532 | elf_link_hash_traverse (elf_hash_table (info), | |
3533 | _bfd_elf_adjust_dynamic_symbol, | |
3534 | &eif); | |
3535 | if (eif.failed) | |
3536 | return FALSE; | |
3537 | ||
3538 | /* Add some entries to the .dynamic section. We fill in some of the | |
3539 | values later, in elf_bfd_final_link, but we must add the entries | |
3540 | now so that we know the final size of the .dynamic section. */ | |
3541 | ||
3542 | /* If there are initialization and/or finalization functions to | |
3543 | call then add the corresponding DT_INIT/DT_FINI entries. */ | |
3544 | h = (info->init_function | |
3545 | ? elf_link_hash_lookup (elf_hash_table (info), | |
3546 | info->init_function, FALSE, | |
3547 | FALSE, FALSE) | |
3548 | : NULL); | |
3549 | if (h != NULL | |
3550 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
3551 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
3552 | { | |
3553 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) | |
3554 | return FALSE; | |
3555 | } | |
3556 | h = (info->fini_function | |
3557 | ? elf_link_hash_lookup (elf_hash_table (info), | |
3558 | info->fini_function, FALSE, | |
3559 | FALSE, FALSE) | |
3560 | : NULL); | |
3561 | if (h != NULL | |
3562 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
3563 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
3564 | { | |
3565 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) | |
3566 | return FALSE; | |
3567 | } | |
3568 | ||
3569 | if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL) | |
3570 | { | |
3571 | /* DT_PREINIT_ARRAY is not allowed in shared library. */ | |
3572 | if (! info->executable) | |
3573 | { | |
3574 | bfd *sub; | |
3575 | asection *o; | |
3576 | ||
3577 | for (sub = info->input_bfds; sub != NULL; | |
3578 | sub = sub->link_next) | |
3579 | for (o = sub->sections; o != NULL; o = o->next) | |
3580 | if (elf_section_data (o)->this_hdr.sh_type | |
3581 | == SHT_PREINIT_ARRAY) | |
3582 | { | |
3583 | (*_bfd_error_handler) | |
3584 | (_("%s: .preinit_array section is not allowed in DSO"), | |
3585 | bfd_archive_filename (sub)); | |
3586 | break; | |
3587 | } | |
3588 | ||
3589 | bfd_set_error (bfd_error_nonrepresentable_section); | |
3590 | return FALSE; | |
3591 | } | |
3592 | ||
3593 | if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) | |
3594 | || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) | |
3595 | return FALSE; | |
3596 | } | |
3597 | if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL) | |
3598 | { | |
3599 | if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) | |
3600 | || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) | |
3601 | return FALSE; | |
3602 | } | |
3603 | if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL) | |
3604 | { | |
3605 | if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) | |
3606 | || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) | |
3607 | return FALSE; | |
3608 | } | |
3609 | ||
3610 | dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); | |
3611 | /* If .dynstr is excluded from the link, we don't want any of | |
3612 | these tags. Strictly, we should be checking each section | |
3613 | individually; This quick check covers for the case where | |
3614 | someone does a /DISCARD/ : { *(*) }. */ | |
3615 | if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) | |
3616 | { | |
3617 | bfd_size_type strsize; | |
3618 | ||
3619 | strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
3620 | if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) | |
3621 | || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
3622 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
3623 | || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
3624 | || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, | |
3625 | bed->s->sizeof_sym)) | |
3626 | return FALSE; | |
3627 | } | |
3628 | } | |
3629 | ||
3630 | /* The backend must work out the sizes of all the other dynamic | |
3631 | sections. */ | |
3632 | if (bed->elf_backend_size_dynamic_sections | |
3633 | && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
3634 | return FALSE; | |
3635 | ||
3636 | if (elf_hash_table (info)->dynamic_sections_created) | |
3637 | { | |
3638 | bfd_size_type dynsymcount; | |
3639 | asection *s; | |
3640 | size_t bucketcount = 0; | |
3641 | size_t hash_entry_size; | |
3642 | unsigned int dtagcount; | |
3643 | ||
3644 | /* Set up the version definition section. */ | |
3645 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
3646 | BFD_ASSERT (s != NULL); | |
3647 | ||
3648 | /* We may have created additional version definitions if we are | |
3649 | just linking a regular application. */ | |
3650 | verdefs = asvinfo.verdefs; | |
3651 | ||
3652 | /* Skip anonymous version tag. */ | |
3653 | if (verdefs != NULL && verdefs->vernum == 0) | |
3654 | verdefs = verdefs->next; | |
3655 | ||
3656 | if (verdefs == NULL) | |
3657 | _bfd_strip_section_from_output (info, s); | |
3658 | else | |
3659 | { | |
3660 | unsigned int cdefs; | |
3661 | bfd_size_type size; | |
3662 | struct bfd_elf_version_tree *t; | |
3663 | bfd_byte *p; | |
3664 | Elf_Internal_Verdef def; | |
3665 | Elf_Internal_Verdaux defaux; | |
3666 | ||
3667 | cdefs = 0; | |
3668 | size = 0; | |
3669 | ||
3670 | /* Make space for the base version. */ | |
3671 | size += sizeof (Elf_External_Verdef); | |
3672 | size += sizeof (Elf_External_Verdaux); | |
3673 | ++cdefs; | |
3674 | ||
3675 | for (t = verdefs; t != NULL; t = t->next) | |
3676 | { | |
3677 | struct bfd_elf_version_deps *n; | |
3678 | ||
3679 | size += sizeof (Elf_External_Verdef); | |
3680 | size += sizeof (Elf_External_Verdaux); | |
3681 | ++cdefs; | |
3682 | ||
3683 | for (n = t->deps; n != NULL; n = n->next) | |
3684 | size += sizeof (Elf_External_Verdaux); | |
3685 | } | |
3686 | ||
3687 | s->_raw_size = size; | |
3688 | s->contents = bfd_alloc (output_bfd, s->_raw_size); | |
3689 | if (s->contents == NULL && s->_raw_size != 0) | |
3690 | return FALSE; | |
3691 | ||
3692 | /* Fill in the version definition section. */ | |
3693 | ||
3694 | p = s->contents; | |
3695 | ||
3696 | def.vd_version = VER_DEF_CURRENT; | |
3697 | def.vd_flags = VER_FLG_BASE; | |
3698 | def.vd_ndx = 1; | |
3699 | def.vd_cnt = 1; | |
3700 | def.vd_aux = sizeof (Elf_External_Verdef); | |
3701 | def.vd_next = (sizeof (Elf_External_Verdef) | |
3702 | + sizeof (Elf_External_Verdaux)); | |
3703 | ||
3704 | if (soname_indx != (bfd_size_type) -1) | |
3705 | { | |
3706 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
3707 | soname_indx); | |
3708 | def.vd_hash = bfd_elf_hash (soname); | |
3709 | defaux.vda_name = soname_indx; | |
3710 | } | |
3711 | else | |
3712 | { | |
3713 | const char *name; | |
3714 | bfd_size_type indx; | |
3715 | ||
3716 | name = basename (output_bfd->filename); | |
3717 | def.vd_hash = bfd_elf_hash (name); | |
3718 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
3719 | name, FALSE); | |
3720 | if (indx == (bfd_size_type) -1) | |
3721 | return FALSE; | |
3722 | defaux.vda_name = indx; | |
3723 | } | |
3724 | defaux.vda_next = 0; | |
3725 | ||
3726 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
3727 | (Elf_External_Verdef *) p); | |
3728 | p += sizeof (Elf_External_Verdef); | |
3729 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
3730 | (Elf_External_Verdaux *) p); | |
3731 | p += sizeof (Elf_External_Verdaux); | |
3732 | ||
3733 | for (t = verdefs; t != NULL; t = t->next) | |
3734 | { | |
3735 | unsigned int cdeps; | |
3736 | struct bfd_elf_version_deps *n; | |
3737 | struct elf_link_hash_entry *h; | |
3738 | struct bfd_link_hash_entry *bh; | |
3739 | ||
3740 | cdeps = 0; | |
3741 | for (n = t->deps; n != NULL; n = n->next) | |
3742 | ++cdeps; | |
3743 | ||
3744 | /* Add a symbol representing this version. */ | |
3745 | bh = NULL; | |
3746 | if (! (_bfd_generic_link_add_one_symbol | |
3747 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
3748 | 0, NULL, FALSE, | |
3749 | get_elf_backend_data (dynobj)->collect, &bh))) | |
3750 | return FALSE; | |
3751 | h = (struct elf_link_hash_entry *) bh; | |
3752 | h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; | |
3753 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
3754 | h->type = STT_OBJECT; | |
3755 | h->verinfo.vertree = t; | |
3756 | ||
3757 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
3758 | return FALSE; | |
3759 | ||
3760 | def.vd_version = VER_DEF_CURRENT; | |
3761 | def.vd_flags = 0; | |
3762 | if (t->globals.list == NULL | |
3763 | && t->locals.list == NULL | |
3764 | && ! t->used) | |
3765 | def.vd_flags |= VER_FLG_WEAK; | |
3766 | def.vd_ndx = t->vernum + 1; | |
3767 | def.vd_cnt = cdeps + 1; | |
3768 | def.vd_hash = bfd_elf_hash (t->name); | |
3769 | def.vd_aux = sizeof (Elf_External_Verdef); | |
3770 | def.vd_next = 0; | |
3771 | if (t->next != NULL) | |
3772 | def.vd_next = (sizeof (Elf_External_Verdef) | |
3773 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
3774 | ||
3775 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
3776 | (Elf_External_Verdef *) p); | |
3777 | p += sizeof (Elf_External_Verdef); | |
3778 | ||
3779 | defaux.vda_name = h->dynstr_index; | |
3780 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
3781 | h->dynstr_index); | |
3782 | defaux.vda_next = 0; | |
3783 | if (t->deps != NULL) | |
3784 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
3785 | t->name_indx = defaux.vda_name; | |
3786 | ||
3787 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
3788 | (Elf_External_Verdaux *) p); | |
3789 | p += sizeof (Elf_External_Verdaux); | |
3790 | ||
3791 | for (n = t->deps; n != NULL; n = n->next) | |
3792 | { | |
3793 | if (n->version_needed == NULL) | |
3794 | { | |
3795 | /* This can happen if there was an error in the | |
3796 | version script. */ | |
3797 | defaux.vda_name = 0; | |
3798 | } | |
3799 | else | |
3800 | { | |
3801 | defaux.vda_name = n->version_needed->name_indx; | |
3802 | _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, | |
3803 | defaux.vda_name); | |
3804 | } | |
3805 | if (n->next == NULL) | |
3806 | defaux.vda_next = 0; | |
3807 | else | |
3808 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
3809 | ||
3810 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
3811 | (Elf_External_Verdaux *) p); | |
3812 | p += sizeof (Elf_External_Verdaux); | |
3813 | } | |
3814 | } | |
3815 | ||
3816 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
3817 | || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
3818 | return FALSE; | |
3819 | ||
3820 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
3821 | } | |
3822 | ||
3823 | if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) | |
3824 | { | |
3825 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) | |
3826 | return FALSE; | |
3827 | } | |
3828 | else if (info->flags & DF_BIND_NOW) | |
3829 | { | |
3830 | if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) | |
3831 | return FALSE; | |
3832 | } | |
3833 | ||
3834 | if (info->flags_1) | |
3835 | { | |
3836 | if (info->executable) | |
3837 | info->flags_1 &= ~ (DF_1_INITFIRST | |
3838 | | DF_1_NODELETE | |
3839 | | DF_1_NOOPEN); | |
3840 | if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) | |
3841 | return FALSE; | |
3842 | } | |
3843 | ||
3844 | /* Work out the size of the version reference section. */ | |
3845 | ||
3846 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
3847 | BFD_ASSERT (s != NULL); | |
3848 | { | |
3849 | struct elf_find_verdep_info sinfo; | |
3850 | ||
3851 | sinfo.output_bfd = output_bfd; | |
3852 | sinfo.info = info; | |
3853 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
3854 | if (sinfo.vers == 0) | |
3855 | sinfo.vers = 1; | |
3856 | sinfo.failed = FALSE; | |
3857 | ||
3858 | elf_link_hash_traverse (elf_hash_table (info), | |
3859 | _bfd_elf_link_find_version_dependencies, | |
3860 | &sinfo); | |
3861 | ||
3862 | if (elf_tdata (output_bfd)->verref == NULL) | |
3863 | _bfd_strip_section_from_output (info, s); | |
3864 | else | |
3865 | { | |
3866 | Elf_Internal_Verneed *t; | |
3867 | unsigned int size; | |
3868 | unsigned int crefs; | |
3869 | bfd_byte *p; | |
3870 | ||
3871 | /* Build the version definition section. */ | |
3872 | size = 0; | |
3873 | crefs = 0; | |
3874 | for (t = elf_tdata (output_bfd)->verref; | |
3875 | t != NULL; | |
3876 | t = t->vn_nextref) | |
3877 | { | |
3878 | Elf_Internal_Vernaux *a; | |
3879 | ||
3880 | size += sizeof (Elf_External_Verneed); | |
3881 | ++crefs; | |
3882 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3883 | size += sizeof (Elf_External_Vernaux); | |
3884 | } | |
3885 | ||
3886 | s->_raw_size = size; | |
3887 | s->contents = bfd_alloc (output_bfd, s->_raw_size); | |
3888 | if (s->contents == NULL) | |
3889 | return FALSE; | |
3890 | ||
3891 | p = s->contents; | |
3892 | for (t = elf_tdata (output_bfd)->verref; | |
3893 | t != NULL; | |
3894 | t = t->vn_nextref) | |
3895 | { | |
3896 | unsigned int caux; | |
3897 | Elf_Internal_Vernaux *a; | |
3898 | bfd_size_type indx; | |
3899 | ||
3900 | caux = 0; | |
3901 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3902 | ++caux; | |
3903 | ||
3904 | t->vn_version = VER_NEED_CURRENT; | |
3905 | t->vn_cnt = caux; | |
3906 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
3907 | elf_dt_name (t->vn_bfd) != NULL | |
3908 | ? elf_dt_name (t->vn_bfd) | |
3909 | : basename (t->vn_bfd->filename), | |
3910 | FALSE); | |
3911 | if (indx == (bfd_size_type) -1) | |
3912 | return FALSE; | |
3913 | t->vn_file = indx; | |
3914 | t->vn_aux = sizeof (Elf_External_Verneed); | |
3915 | if (t->vn_nextref == NULL) | |
3916 | t->vn_next = 0; | |
3917 | else | |
3918 | t->vn_next = (sizeof (Elf_External_Verneed) | |
3919 | + caux * sizeof (Elf_External_Vernaux)); | |
3920 | ||
3921 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
3922 | (Elf_External_Verneed *) p); | |
3923 | p += sizeof (Elf_External_Verneed); | |
3924 | ||
3925 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3926 | { | |
3927 | a->vna_hash = bfd_elf_hash (a->vna_nodename); | |
3928 | indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, | |
3929 | a->vna_nodename, FALSE); | |
3930 | if (indx == (bfd_size_type) -1) | |
3931 | return FALSE; | |
3932 | a->vna_name = indx; | |
3933 | if (a->vna_nextptr == NULL) | |
3934 | a->vna_next = 0; | |
3935 | else | |
3936 | a->vna_next = sizeof (Elf_External_Vernaux); | |
3937 | ||
3938 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
3939 | (Elf_External_Vernaux *) p); | |
3940 | p += sizeof (Elf_External_Vernaux); | |
3941 | } | |
3942 | } | |
3943 | ||
3944 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
3945 | || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
3946 | return FALSE; | |
3947 | ||
3948 | elf_tdata (output_bfd)->cverrefs = crefs; | |
3949 | } | |
3950 | } | |
3951 | ||
3952 | /* Assign dynsym indicies. In a shared library we generate a | |
3953 | section symbol for each output section, which come first. | |
3954 | Next come all of the back-end allocated local dynamic syms, | |
3955 | followed by the rest of the global symbols. */ | |
3956 | ||
3957 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
3958 | ||
3959 | /* Work out the size of the symbol version section. */ | |
3960 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
3961 | BFD_ASSERT (s != NULL); | |
3962 | if (dynsymcount == 0 | |
3963 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) | |
3964 | { | |
3965 | _bfd_strip_section_from_output (info, s); | |
3966 | /* The DYNSYMCOUNT might have changed if we were going to | |
3967 | output a dynamic symbol table entry for S. */ | |
3968 | dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info); | |
3969 | } | |
3970 | else | |
3971 | { | |
3972 | s->_raw_size = dynsymcount * sizeof (Elf_External_Versym); | |
3973 | s->contents = bfd_zalloc (output_bfd, s->_raw_size); | |
3974 | if (s->contents == NULL) | |
3975 | return FALSE; | |
3976 | ||
3977 | if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) | |
3978 | return FALSE; | |
3979 | } | |
3980 | ||
3981 | /* Set the size of the .dynsym and .hash sections. We counted | |
3982 | the number of dynamic symbols in elf_link_add_object_symbols. | |
3983 | We will build the contents of .dynsym and .hash when we build | |
3984 | the final symbol table, because until then we do not know the | |
3985 | correct value to give the symbols. We built the .dynstr | |
3986 | section as we went along in elf_link_add_object_symbols. */ | |
3987 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
3988 | BFD_ASSERT (s != NULL); | |
3989 | s->_raw_size = dynsymcount * bed->s->sizeof_sym; | |
3990 | s->contents = bfd_alloc (output_bfd, s->_raw_size); | |
3991 | if (s->contents == NULL && s->_raw_size != 0) | |
3992 | return FALSE; | |
3993 | ||
3994 | if (dynsymcount != 0) | |
3995 | { | |
3996 | Elf_Internal_Sym isym; | |
3997 | ||
3998 | /* The first entry in .dynsym is a dummy symbol. */ | |
3999 | isym.st_value = 0; | |
4000 | isym.st_size = 0; | |
4001 | isym.st_name = 0; | |
4002 | isym.st_info = 0; | |
4003 | isym.st_other = 0; | |
4004 | isym.st_shndx = 0; | |
4005 | bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0); | |
4006 | } | |
4007 | ||
4008 | /* Compute the size of the hashing table. As a side effect this | |
4009 | computes the hash values for all the names we export. */ | |
4010 | bucketcount = compute_bucket_count (info); | |
4011 | ||
4012 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
4013 | BFD_ASSERT (s != NULL); | |
4014 | hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; | |
4015 | s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size); | |
4016 | s->contents = bfd_zalloc (output_bfd, s->_raw_size); | |
4017 | if (s->contents == NULL) | |
4018 | return FALSE; | |
4019 | ||
4020 | bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); | |
4021 | bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, | |
4022 | s->contents + hash_entry_size); | |
4023 | ||
4024 | elf_hash_table (info)->bucketcount = bucketcount; | |
4025 | ||
4026 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
4027 | BFD_ASSERT (s != NULL); | |
4028 | ||
4029 | _bfd_elf_finalize_dynstr (output_bfd, info); | |
4030 | ||
4031 | s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); | |
4032 | ||
4033 | for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) | |
4034 | if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) | |
4035 | return FALSE; | |
4036 | } | |
4037 | ||
4038 | return TRUE; | |
4039 | } |