]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
aad5d350 | 2 | Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 |
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 | ||
45d6a902 AM |
28 | static bfd_boolean elf_link_read_relocs_from_section |
29 | PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *)); | |
30 | ||
b34976b6 | 31 | bfd_boolean |
252b5132 RH |
32 | _bfd_elf_create_got_section (abfd, info) |
33 | bfd *abfd; | |
34 | struct bfd_link_info *info; | |
35 | { | |
36 | flagword flags; | |
aad5d350 | 37 | asection *s; |
252b5132 | 38 | struct elf_link_hash_entry *h; |
14a793b2 | 39 | struct bfd_link_hash_entry *bh; |
252b5132 RH |
40 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
41 | int ptralign; | |
42 | ||
43 | /* This function may be called more than once. */ | |
aad5d350 AM |
44 | s = bfd_get_section_by_name (abfd, ".got"); |
45 | if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) | |
b34976b6 | 46 | return TRUE; |
252b5132 RH |
47 | |
48 | switch (bed->s->arch_size) | |
49 | { | |
bb0deeff AO |
50 | case 32: |
51 | ptralign = 2; | |
52 | break; | |
53 | ||
54 | case 64: | |
55 | ptralign = 3; | |
56 | break; | |
57 | ||
58 | default: | |
59 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 60 | return FALSE; |
252b5132 RH |
61 | } |
62 | ||
63 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
64 | | SEC_LINKER_CREATED); | |
65 | ||
66 | s = bfd_make_section (abfd, ".got"); | |
67 | if (s == NULL | |
68 | || !bfd_set_section_flags (abfd, s, flags) | |
69 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 70 | return FALSE; |
252b5132 RH |
71 | |
72 | if (bed->want_got_plt) | |
73 | { | |
74 | s = bfd_make_section (abfd, ".got.plt"); | |
75 | if (s == NULL | |
76 | || !bfd_set_section_flags (abfd, s, flags) | |
77 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
b34976b6 | 78 | return FALSE; |
252b5132 RH |
79 | } |
80 | ||
2517a57f AM |
81 | if (bed->want_got_sym) |
82 | { | |
83 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
84 | (or .got.plt) section. We don't do this in the linker script | |
85 | because we don't want to define the symbol if we are not creating | |
86 | a global offset table. */ | |
14a793b2 | 87 | bh = NULL; |
2517a57f AM |
88 | if (!(_bfd_generic_link_add_one_symbol |
89 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
b34976b6 | 90 | bed->got_symbol_offset, (const char *) NULL, FALSE, |
14a793b2 | 91 | bed->collect, &bh))) |
b34976b6 | 92 | return FALSE; |
14a793b2 | 93 | h = (struct elf_link_hash_entry *) bh; |
2517a57f AM |
94 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
95 | h->type = STT_OBJECT; | |
252b5132 | 96 | |
36af4a4e | 97 | if (! info->executable |
2517a57f | 98 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 99 | return FALSE; |
252b5132 | 100 | |
2517a57f AM |
101 | elf_hash_table (info)->hgot = h; |
102 | } | |
252b5132 RH |
103 | |
104 | /* The first bit of the global offset table is the header. */ | |
105 | s->_raw_size += bed->got_header_size + bed->got_symbol_offset; | |
106 | ||
b34976b6 | 107 | return TRUE; |
252b5132 RH |
108 | } |
109 | \f | |
45d6a902 AM |
110 | /* Create some sections which will be filled in with dynamic linking |
111 | information. ABFD is an input file which requires dynamic sections | |
112 | to be created. The dynamic sections take up virtual memory space | |
113 | when the final executable is run, so we need to create them before | |
114 | addresses are assigned to the output sections. We work out the | |
115 | actual contents and size of these sections later. */ | |
252b5132 | 116 | |
b34976b6 | 117 | bfd_boolean |
45d6a902 | 118 | _bfd_elf_link_create_dynamic_sections (abfd, info) |
252b5132 RH |
119 | bfd *abfd; |
120 | struct bfd_link_info *info; | |
121 | { | |
45d6a902 AM |
122 | flagword flags; |
123 | register asection *s; | |
124 | struct elf_link_hash_entry *h; | |
125 | struct bfd_link_hash_entry *bh; | |
126 | struct elf_backend_data *bed; | |
252b5132 | 127 | |
45d6a902 AM |
128 | if (! is_elf_hash_table (info)) |
129 | return FALSE; | |
130 | ||
131 | if (elf_hash_table (info)->dynamic_sections_created) | |
132 | return TRUE; | |
133 | ||
134 | /* Make sure that all dynamic sections use the same input BFD. */ | |
135 | if (elf_hash_table (info)->dynobj == NULL) | |
136 | elf_hash_table (info)->dynobj = abfd; | |
137 | else | |
138 | abfd = elf_hash_table (info)->dynobj; | |
139 | ||
140 | /* Note that we set the SEC_IN_MEMORY flag for all of these | |
141 | sections. */ | |
142 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | |
143 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
144 | ||
145 | /* A dynamically linked executable has a .interp section, but a | |
146 | shared library does not. */ | |
36af4a4e | 147 | if (info->executable) |
252b5132 | 148 | { |
45d6a902 AM |
149 | s = bfd_make_section (abfd, ".interp"); |
150 | if (s == NULL | |
151 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
152 | return FALSE; | |
153 | } | |
bb0deeff | 154 | |
45d6a902 AM |
155 | if (! info->traditional_format |
156 | && info->hash->creator->flavour == bfd_target_elf_flavour) | |
157 | { | |
158 | s = bfd_make_section (abfd, ".eh_frame_hdr"); | |
159 | if (s == NULL | |
160 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
161 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
162 | return FALSE; | |
163 | elf_hash_table (info)->eh_info.hdr_sec = s; | |
164 | } | |
bb0deeff | 165 | |
45d6a902 AM |
166 | bed = get_elf_backend_data (abfd); |
167 | ||
168 | /* Create sections to hold version informations. These are removed | |
169 | if they are not needed. */ | |
170 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
171 | if (s == NULL | |
172 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
173 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
174 | return FALSE; | |
175 | ||
176 | s = bfd_make_section (abfd, ".gnu.version"); | |
177 | if (s == NULL | |
178 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
179 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
180 | return FALSE; | |
181 | ||
182 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
183 | if (s == NULL | |
184 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
185 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
186 | return FALSE; | |
187 | ||
188 | s = bfd_make_section (abfd, ".dynsym"); | |
189 | if (s == NULL | |
190 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
191 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
192 | return FALSE; | |
193 | ||
194 | s = bfd_make_section (abfd, ".dynstr"); | |
195 | if (s == NULL | |
196 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
197 | return FALSE; | |
198 | ||
199 | /* Create a strtab to hold the dynamic symbol names. */ | |
200 | if (elf_hash_table (info)->dynstr == NULL) | |
201 | { | |
202 | elf_hash_table (info)->dynstr = _bfd_elf_strtab_init (); | |
203 | if (elf_hash_table (info)->dynstr == NULL) | |
204 | return FALSE; | |
252b5132 RH |
205 | } |
206 | ||
45d6a902 AM |
207 | s = bfd_make_section (abfd, ".dynamic"); |
208 | if (s == NULL | |
209 | || ! bfd_set_section_flags (abfd, s, flags) | |
210 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
211 | return FALSE; | |
212 | ||
213 | /* The special symbol _DYNAMIC is always set to the start of the | |
214 | .dynamic section. This call occurs before we have processed the | |
215 | symbols for any dynamic object, so we don't have to worry about | |
216 | overriding a dynamic definition. We could set _DYNAMIC in a | |
217 | linker script, but we only want to define it if we are, in fact, | |
218 | creating a .dynamic section. We don't want to define it if there | |
219 | is no .dynamic section, since on some ELF platforms the start up | |
220 | code examines it to decide how to initialize the process. */ | |
221 | bh = NULL; | |
222 | if (! (_bfd_generic_link_add_one_symbol | |
223 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0, | |
224 | (const char *) 0, FALSE, get_elf_backend_data (abfd)->collect, &bh))) | |
225 | return FALSE; | |
226 | h = (struct elf_link_hash_entry *) bh; | |
227 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
228 | h->type = STT_OBJECT; | |
229 | ||
36af4a4e | 230 | if (! info->executable |
45d6a902 AM |
231 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
232 | return FALSE; | |
233 | ||
234 | s = bfd_make_section (abfd, ".hash"); | |
235 | if (s == NULL | |
236 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
237 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) | |
238 | return FALSE; | |
239 | elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; | |
240 | ||
241 | /* Let the backend create the rest of the sections. This lets the | |
242 | backend set the right flags. The backend will normally create | |
243 | the .got and .plt sections. */ | |
244 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
245 | return FALSE; | |
246 | ||
247 | elf_hash_table (info)->dynamic_sections_created = TRUE; | |
248 | ||
249 | return TRUE; | |
250 | } | |
251 | ||
252 | /* Create dynamic sections when linking against a dynamic object. */ | |
253 | ||
254 | bfd_boolean | |
255 | _bfd_elf_create_dynamic_sections (abfd, info) | |
256 | bfd *abfd; | |
257 | struct bfd_link_info *info; | |
258 | { | |
259 | flagword flags, pltflags; | |
260 | asection *s; | |
261 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
262 | ||
252b5132 RH |
263 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and |
264 | .rel[a].bss sections. */ | |
265 | ||
266 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
267 | | SEC_LINKER_CREATED); | |
268 | ||
269 | pltflags = flags; | |
270 | pltflags |= SEC_CODE; | |
271 | if (bed->plt_not_loaded) | |
5d1634d7 | 272 | pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); |
252b5132 RH |
273 | if (bed->plt_readonly) |
274 | pltflags |= SEC_READONLY; | |
275 | ||
276 | s = bfd_make_section (abfd, ".plt"); | |
277 | if (s == NULL | |
278 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
279 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
b34976b6 | 280 | return FALSE; |
252b5132 RH |
281 | |
282 | if (bed->want_plt_sym) | |
283 | { | |
284 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
285 | .plt section. */ | |
14a793b2 AM |
286 | struct elf_link_hash_entry *h; |
287 | struct bfd_link_hash_entry *bh = NULL; | |
288 | ||
252b5132 RH |
289 | if (! (_bfd_generic_link_add_one_symbol |
290 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, | |
b34976b6 | 291 | (bfd_vma) 0, (const char *) NULL, FALSE, |
14a793b2 | 292 | get_elf_backend_data (abfd)->collect, &bh))) |
b34976b6 | 293 | return FALSE; |
14a793b2 | 294 | h = (struct elf_link_hash_entry *) bh; |
252b5132 RH |
295 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
296 | h->type = STT_OBJECT; | |
297 | ||
36af4a4e | 298 | if (! info->executable |
252b5132 | 299 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 300 | return FALSE; |
252b5132 RH |
301 | } |
302 | ||
3e932841 | 303 | s = bfd_make_section (abfd, |
bf572ba0 | 304 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); |
252b5132 RH |
305 | if (s == NULL |
306 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 307 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 308 | return FALSE; |
252b5132 RH |
309 | |
310 | if (! _bfd_elf_create_got_section (abfd, info)) | |
b34976b6 | 311 | return FALSE; |
252b5132 | 312 | |
3018b441 RH |
313 | if (bed->want_dynbss) |
314 | { | |
315 | /* The .dynbss section is a place to put symbols which are defined | |
316 | by dynamic objects, are referenced by regular objects, and are | |
317 | not functions. We must allocate space for them in the process | |
318 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
319 | initialize them at run time. The linker script puts the .dynbss | |
320 | section into the .bss section of the final image. */ | |
321 | s = bfd_make_section (abfd, ".dynbss"); | |
322 | if (s == NULL | |
323 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
b34976b6 | 324 | return FALSE; |
252b5132 | 325 | |
3018b441 | 326 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
327 | normally needed. We need to create it here, though, so that the |
328 | linker will map it to an output section. We can't just create it | |
329 | only if we need it, because we will not know whether we need it | |
330 | until we have seen all the input files, and the first time the | |
331 | main linker code calls BFD after examining all the input files | |
332 | (size_dynamic_sections) the input sections have already been | |
333 | mapped to the output sections. If the section turns out not to | |
334 | be needed, we can discard it later. We will never need this | |
335 | section when generating a shared object, since they do not use | |
336 | copy relocs. */ | |
3018b441 RH |
337 | if (! info->shared) |
338 | { | |
3e932841 KH |
339 | s = bfd_make_section (abfd, |
340 | (bed->default_use_rela_p | |
341 | ? ".rela.bss" : ".rel.bss")); | |
3018b441 RH |
342 | if (s == NULL |
343 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
45d6a902 | 344 | || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) |
b34976b6 | 345 | return FALSE; |
3018b441 | 346 | } |
252b5132 RH |
347 | } |
348 | ||
b34976b6 | 349 | return TRUE; |
252b5132 RH |
350 | } |
351 | \f | |
252b5132 RH |
352 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
353 | read the input files, since we need to have a list of all of them | |
354 | before we can determine the final sizes of the output sections. | |
355 | Note that we may actually call this function even though we are not | |
356 | going to output any dynamic symbols; in some cases we know that a | |
357 | symbol should be in the dynamic symbol table, but only if there is | |
358 | one. */ | |
359 | ||
b34976b6 | 360 | bfd_boolean |
252b5132 RH |
361 | _bfd_elf_link_record_dynamic_symbol (info, h) |
362 | struct bfd_link_info *info; | |
363 | struct elf_link_hash_entry *h; | |
364 | { | |
365 | if (h->dynindx == -1) | |
366 | { | |
2b0f7ef9 | 367 | struct elf_strtab_hash *dynstr; |
252b5132 RH |
368 | char *p, *alc; |
369 | const char *name; | |
b34976b6 | 370 | bfd_boolean copy; |
252b5132 RH |
371 | bfd_size_type indx; |
372 | ||
7a13edea NC |
373 | /* XXX: The ABI draft says the linker must turn hidden and |
374 | internal symbols into STB_LOCAL symbols when producing the | |
375 | DSO. However, if ld.so honors st_other in the dynamic table, | |
376 | this would not be necessary. */ | |
377 | switch (ELF_ST_VISIBILITY (h->other)) | |
378 | { | |
379 | case STV_INTERNAL: | |
380 | case STV_HIDDEN: | |
9d6eee78 L |
381 | if (h->root.type != bfd_link_hash_undefined |
382 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 L |
383 | { |
384 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; | |
b34976b6 | 385 | return TRUE; |
7a13edea | 386 | } |
0444bdd4 | 387 | |
7a13edea NC |
388 | default: |
389 | break; | |
390 | } | |
391 | ||
252b5132 RH |
392 | h->dynindx = elf_hash_table (info)->dynsymcount; |
393 | ++elf_hash_table (info)->dynsymcount; | |
394 | ||
395 | dynstr = elf_hash_table (info)->dynstr; | |
396 | if (dynstr == NULL) | |
397 | { | |
398 | /* Create a strtab to hold the dynamic symbol names. */ | |
2b0f7ef9 | 399 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); |
252b5132 | 400 | if (dynstr == NULL) |
b34976b6 | 401 | return FALSE; |
252b5132 RH |
402 | } |
403 | ||
404 | /* We don't put any version information in the dynamic string | |
aad5d350 | 405 | table. */ |
252b5132 RH |
406 | name = h->root.root.string; |
407 | p = strchr (name, ELF_VER_CHR); | |
408 | if (p == NULL) | |
409 | { | |
410 | alc = NULL; | |
b34976b6 | 411 | copy = FALSE; |
252b5132 RH |
412 | } |
413 | else | |
414 | { | |
d4c88bbb AM |
415 | size_t len = p - name + 1; |
416 | ||
417 | alc = bfd_malloc ((bfd_size_type) len); | |
252b5132 | 418 | if (alc == NULL) |
b34976b6 | 419 | return FALSE; |
d4c88bbb AM |
420 | memcpy (alc, name, len - 1); |
421 | alc[len - 1] = '\0'; | |
252b5132 | 422 | name = alc; |
b34976b6 | 423 | copy = TRUE; |
252b5132 RH |
424 | } |
425 | ||
2b0f7ef9 | 426 | indx = _bfd_elf_strtab_add (dynstr, name, copy); |
252b5132 RH |
427 | |
428 | if (alc != NULL) | |
429 | free (alc); | |
430 | ||
431 | if (indx == (bfd_size_type) -1) | |
b34976b6 | 432 | return FALSE; |
252b5132 RH |
433 | h->dynstr_index = indx; |
434 | } | |
435 | ||
b34976b6 | 436 | return TRUE; |
252b5132 | 437 | } |
45d6a902 AM |
438 | \f |
439 | /* Record an assignment to a symbol made by a linker script. We need | |
440 | this in case some dynamic object refers to this symbol. */ | |
441 | ||
442 | bfd_boolean | |
443 | bfd_elf_record_link_assignment (output_bfd, info, name, provide) | |
444 | bfd *output_bfd ATTRIBUTE_UNUSED; | |
445 | struct bfd_link_info *info; | |
446 | const char *name; | |
447 | bfd_boolean provide; | |
448 | { | |
449 | struct elf_link_hash_entry *h; | |
450 | ||
451 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
452 | return TRUE; | |
453 | ||
454 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE); | |
455 | if (h == NULL) | |
456 | return FALSE; | |
457 | ||
458 | if (h->root.type == bfd_link_hash_new) | |
459 | h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF; | |
460 | ||
461 | /* If this symbol is being provided by the linker script, and it is | |
462 | currently defined by a dynamic object, but not by a regular | |
463 | object, then mark it as undefined so that the generic linker will | |
464 | force the correct value. */ | |
465 | if (provide | |
466 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
467 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
468 | h->root.type = bfd_link_hash_undefined; | |
469 | ||
470 | /* If this symbol is not being provided by the linker script, and it is | |
471 | currently defined by a dynamic object, but not by a regular object, | |
472 | then clear out any version information because the symbol will not be | |
473 | associated with the dynamic object any more. */ | |
474 | if (!provide | |
475 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
476 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
477 | h->verinfo.verdef = NULL; | |
478 | ||
479 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
480 | ||
481 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
482 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
483 | || info->shared) | |
484 | && h->dynindx == -1) | |
485 | { | |
486 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
487 | return FALSE; | |
488 | ||
489 | /* If this is a weak defined symbol, and we know a corresponding | |
490 | real symbol from the same dynamic object, make sure the real | |
491 | symbol is also made into a dynamic symbol. */ | |
492 | if (h->weakdef != NULL | |
493 | && h->weakdef->dynindx == -1) | |
494 | { | |
495 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) | |
496 | return FALSE; | |
497 | } | |
498 | } | |
499 | ||
500 | return TRUE; | |
501 | } | |
42751cf3 | 502 | |
8c58d23b AM |
503 | /* Record a new local dynamic symbol. Returns 0 on failure, 1 on |
504 | success, and 2 on a failure caused by attempting to record a symbol | |
505 | in a discarded section, eg. a discarded link-once section symbol. */ | |
506 | ||
507 | int | |
508 | elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx) | |
509 | struct bfd_link_info *info; | |
510 | bfd *input_bfd; | |
511 | long input_indx; | |
512 | { | |
513 | bfd_size_type amt; | |
514 | struct elf_link_local_dynamic_entry *entry; | |
515 | struct elf_link_hash_table *eht; | |
516 | struct elf_strtab_hash *dynstr; | |
517 | unsigned long dynstr_index; | |
518 | char *name; | |
519 | Elf_External_Sym_Shndx eshndx; | |
520 | char esym[sizeof (Elf64_External_Sym)]; | |
521 | ||
522 | if (! is_elf_hash_table (info)) | |
523 | return 0; | |
524 | ||
525 | /* See if the entry exists already. */ | |
526 | for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) | |
527 | if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) | |
528 | return 1; | |
529 | ||
530 | amt = sizeof (*entry); | |
531 | entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); | |
532 | if (entry == NULL) | |
533 | return 0; | |
534 | ||
535 | /* Go find the symbol, so that we can find it's name. */ | |
536 | if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, | |
537 | (size_t) 1, (size_t) input_indx, | |
538 | &entry->isym, esym, &eshndx)) | |
539 | { | |
540 | bfd_release (input_bfd, entry); | |
541 | return 0; | |
542 | } | |
543 | ||
544 | if (entry->isym.st_shndx != SHN_UNDEF | |
545 | && (entry->isym.st_shndx < SHN_LORESERVE | |
546 | || entry->isym.st_shndx > SHN_HIRESERVE)) | |
547 | { | |
548 | asection *s; | |
549 | ||
550 | s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); | |
551 | if (s == NULL || bfd_is_abs_section (s->output_section)) | |
552 | { | |
553 | /* We can still bfd_release here as nothing has done another | |
554 | bfd_alloc. We can't do this later in this function. */ | |
555 | bfd_release (input_bfd, entry); | |
556 | return 2; | |
557 | } | |
558 | } | |
559 | ||
560 | name = (bfd_elf_string_from_elf_section | |
561 | (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, | |
562 | entry->isym.st_name)); | |
563 | ||
564 | dynstr = elf_hash_table (info)->dynstr; | |
565 | if (dynstr == NULL) | |
566 | { | |
567 | /* Create a strtab to hold the dynamic symbol names. */ | |
568 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); | |
569 | if (dynstr == NULL) | |
570 | return 0; | |
571 | } | |
572 | ||
b34976b6 | 573 | dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); |
8c58d23b AM |
574 | if (dynstr_index == (unsigned long) -1) |
575 | return 0; | |
576 | entry->isym.st_name = dynstr_index; | |
577 | ||
578 | eht = elf_hash_table (info); | |
579 | ||
580 | entry->next = eht->dynlocal; | |
581 | eht->dynlocal = entry; | |
582 | entry->input_bfd = input_bfd; | |
583 | entry->input_indx = input_indx; | |
584 | eht->dynsymcount++; | |
585 | ||
586 | /* Whatever binding the symbol had before, it's now local. */ | |
587 | entry->isym.st_info | |
588 | = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); | |
589 | ||
590 | /* The dynindx will be set at the end of size_dynamic_sections. */ | |
591 | ||
592 | return 1; | |
593 | } | |
594 | ||
30b30c21 | 595 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 596 | |
30b30c21 RH |
597 | long |
598 | _bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx) | |
599 | struct bfd_link_info *info; | |
600 | bfd *input_bfd; | |
601 | long input_indx; | |
602 | { | |
603 | struct elf_link_local_dynamic_entry *e; | |
604 | ||
605 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
606 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
607 | return e->dynindx; | |
608 | return -1; | |
609 | } | |
610 | ||
611 | /* This function is used to renumber the dynamic symbols, if some of | |
612 | them are removed because they are marked as local. This is called | |
613 | via elf_link_hash_traverse. */ | |
614 | ||
b34976b6 | 615 | static bfd_boolean elf_link_renumber_hash_table_dynsyms |
30b30c21 RH |
616 | PARAMS ((struct elf_link_hash_entry *, PTR)); |
617 | ||
b34976b6 | 618 | static bfd_boolean |
30b30c21 | 619 | elf_link_renumber_hash_table_dynsyms (h, data) |
42751cf3 | 620 | struct elf_link_hash_entry *h; |
30b30c21 | 621 | PTR data; |
42751cf3 | 622 | { |
30b30c21 RH |
623 | size_t *count = (size_t *) data; |
624 | ||
e92d460e AM |
625 | if (h->root.type == bfd_link_hash_warning) |
626 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
627 | ||
42751cf3 | 628 | if (h->dynindx != -1) |
30b30c21 RH |
629 | h->dynindx = ++(*count); |
630 | ||
b34976b6 | 631 | return TRUE; |
42751cf3 | 632 | } |
30b30c21 | 633 | |
062e2358 | 634 | /* Assign dynsym indices. In a shared library we generate a section |
30b30c21 RH |
635 | symbol for each output section, which come first. Next come all of |
636 | the back-end allocated local dynamic syms, followed by the rest of | |
637 | the global symbols. */ | |
638 | ||
639 | unsigned long | |
640 | _bfd_elf_link_renumber_dynsyms (output_bfd, info) | |
641 | bfd *output_bfd; | |
642 | struct bfd_link_info *info; | |
643 | { | |
644 | unsigned long dynsymcount = 0; | |
645 | ||
646 | if (info->shared) | |
647 | { | |
648 | asection *p; | |
649 | for (p = output_bfd->sections; p ; p = p->next) | |
bc0ba537 AM |
650 | if ((p->flags & SEC_EXCLUDE) == 0) |
651 | elf_section_data (p)->dynindx = ++dynsymcount; | |
30b30c21 RH |
652 | } |
653 | ||
654 | if (elf_hash_table (info)->dynlocal) | |
655 | { | |
656 | struct elf_link_local_dynamic_entry *p; | |
657 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
658 | p->dynindx = ++dynsymcount; | |
659 | } | |
660 | ||
661 | elf_link_hash_traverse (elf_hash_table (info), | |
662 | elf_link_renumber_hash_table_dynsyms, | |
663 | &dynsymcount); | |
664 | ||
665 | /* There is an unused NULL entry at the head of the table which | |
666 | we must account for in our count. Unless there weren't any | |
667 | symbols, which means we'll have no table at all. */ | |
668 | if (dynsymcount != 0) | |
669 | ++dynsymcount; | |
670 | ||
671 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
672 | } | |
252b5132 | 673 | |
45d6a902 AM |
674 | /* This function is called when we want to define a new symbol. It |
675 | handles the various cases which arise when we find a definition in | |
676 | a dynamic object, or when there is already a definition in a | |
677 | dynamic object. The new symbol is described by NAME, SYM, PSEC, | |
678 | and PVALUE. We set SYM_HASH to the hash table entry. We set | |
679 | OVERRIDE if the old symbol is overriding a new definition. We set | |
680 | TYPE_CHANGE_OK if it is OK for the type to change. We set | |
681 | SIZE_CHANGE_OK if it is OK for the size to change. By OK to | |
682 | change, we mean that we shouldn't warn if the type or size does | |
683 | change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of | |
684 | a shared object. */ | |
685 | ||
686 | bfd_boolean | |
687 | _bfd_elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash, skip, | |
688 | override, type_change_ok, size_change_ok, dt_needed) | |
252b5132 RH |
689 | bfd *abfd; |
690 | struct bfd_link_info *info; | |
45d6a902 AM |
691 | const char *name; |
692 | Elf_Internal_Sym *sym; | |
693 | asection **psec; | |
694 | bfd_vma *pvalue; | |
695 | struct elf_link_hash_entry **sym_hash; | |
696 | bfd_boolean *skip; | |
697 | bfd_boolean *override; | |
698 | bfd_boolean *type_change_ok; | |
699 | bfd_boolean *size_change_ok; | |
700 | bfd_boolean dt_needed; | |
252b5132 | 701 | { |
45d6a902 AM |
702 | asection *sec; |
703 | struct elf_link_hash_entry *h; | |
704 | struct elf_link_hash_entry *flip; | |
705 | int bind; | |
706 | bfd *oldbfd; | |
707 | bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; | |
708 | bfd_boolean newweakdef, oldweakdef, newweakundef, oldweakundef; | |
709 | ||
710 | *skip = FALSE; | |
711 | *override = FALSE; | |
712 | ||
713 | sec = *psec; | |
714 | bind = ELF_ST_BIND (sym->st_info); | |
715 | ||
716 | if (! bfd_is_und_section (sec)) | |
717 | h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); | |
718 | else | |
719 | h = ((struct elf_link_hash_entry *) | |
720 | bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); | |
721 | if (h == NULL) | |
722 | return FALSE; | |
723 | *sym_hash = h; | |
252b5132 | 724 | |
45d6a902 AM |
725 | /* This code is for coping with dynamic objects, and is only useful |
726 | if we are doing an ELF link. */ | |
727 | if (info->hash->creator != abfd->xvec) | |
728 | return TRUE; | |
252b5132 | 729 | |
45d6a902 AM |
730 | /* For merging, we only care about real symbols. */ |
731 | ||
732 | while (h->root.type == bfd_link_hash_indirect | |
733 | || h->root.type == bfd_link_hash_warning) | |
734 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
735 | ||
736 | /* If we just created the symbol, mark it as being an ELF symbol. | |
737 | Other than that, there is nothing to do--there is no merge issue | |
738 | with a newly defined symbol--so we just return. */ | |
739 | ||
740 | if (h->root.type == bfd_link_hash_new) | |
252b5132 | 741 | { |
45d6a902 AM |
742 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; |
743 | return TRUE; | |
744 | } | |
252b5132 | 745 | |
45d6a902 | 746 | /* OLDBFD is a BFD associated with the existing symbol. */ |
252b5132 | 747 | |
45d6a902 AM |
748 | switch (h->root.type) |
749 | { | |
750 | default: | |
751 | oldbfd = NULL; | |
752 | break; | |
252b5132 | 753 | |
45d6a902 AM |
754 | case bfd_link_hash_undefined: |
755 | case bfd_link_hash_undefweak: | |
756 | oldbfd = h->root.u.undef.abfd; | |
757 | break; | |
758 | ||
759 | case bfd_link_hash_defined: | |
760 | case bfd_link_hash_defweak: | |
761 | oldbfd = h->root.u.def.section->owner; | |
762 | break; | |
763 | ||
764 | case bfd_link_hash_common: | |
765 | oldbfd = h->root.u.c.p->section->owner; | |
766 | break; | |
767 | } | |
768 | ||
769 | /* In cases involving weak versioned symbols, we may wind up trying | |
770 | to merge a symbol with itself. Catch that here, to avoid the | |
771 | confusion that results if we try to override a symbol with | |
772 | itself. The additional tests catch cases like | |
773 | _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a | |
774 | dynamic object, which we do want to handle here. */ | |
775 | if (abfd == oldbfd | |
776 | && ((abfd->flags & DYNAMIC) == 0 | |
777 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
778 | return TRUE; | |
779 | ||
780 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
781 | respectively, is from a dynamic object. */ | |
782 | ||
783 | if ((abfd->flags & DYNAMIC) != 0) | |
784 | newdyn = TRUE; | |
785 | else | |
786 | newdyn = FALSE; | |
787 | ||
788 | if (oldbfd != NULL) | |
789 | olddyn = (oldbfd->flags & DYNAMIC) != 0; | |
790 | else | |
791 | { | |
792 | asection *hsec; | |
793 | ||
794 | /* This code handles the special SHN_MIPS_{TEXT,DATA} section | |
795 | indices used by MIPS ELF. */ | |
796 | switch (h->root.type) | |
252b5132 | 797 | { |
45d6a902 AM |
798 | default: |
799 | hsec = NULL; | |
800 | break; | |
252b5132 | 801 | |
45d6a902 AM |
802 | case bfd_link_hash_defined: |
803 | case bfd_link_hash_defweak: | |
804 | hsec = h->root.u.def.section; | |
805 | break; | |
252b5132 | 806 | |
45d6a902 AM |
807 | case bfd_link_hash_common: |
808 | hsec = h->root.u.c.p->section; | |
809 | break; | |
252b5132 | 810 | } |
252b5132 | 811 | |
45d6a902 AM |
812 | if (hsec == NULL) |
813 | olddyn = FALSE; | |
814 | else | |
815 | olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0; | |
816 | } | |
252b5132 | 817 | |
45d6a902 AM |
818 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, |
819 | respectively, appear to be a definition rather than reference. */ | |
820 | ||
821 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
822 | newdef = FALSE; | |
823 | else | |
824 | newdef = TRUE; | |
825 | ||
826 | if (h->root.type == bfd_link_hash_undefined | |
827 | || h->root.type == bfd_link_hash_undefweak | |
828 | || h->root.type == bfd_link_hash_common) | |
829 | olddef = FALSE; | |
830 | else | |
831 | olddef = TRUE; | |
832 | ||
833 | /* We need to rememeber if a symbol has a definition in a dynamic | |
834 | object or is weak in all dynamic objects. Internal and hidden | |
835 | visibility will make it unavailable to dynamic objects. */ | |
836 | if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0) | |
837 | { | |
838 | if (!bfd_is_und_section (sec)) | |
839 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF; | |
840 | else | |
252b5132 | 841 | { |
45d6a902 AM |
842 | /* Check if this symbol is weak in all dynamic objects. If it |
843 | is the first time we see it in a dynamic object, we mark | |
844 | if it is weak. Otherwise, we clear it. */ | |
845 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) | |
846 | { | |
847 | if (bind == STB_WEAK) | |
848 | h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK; | |
252b5132 | 849 | } |
45d6a902 AM |
850 | else if (bind != STB_WEAK) |
851 | h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK; | |
252b5132 | 852 | } |
45d6a902 | 853 | } |
252b5132 | 854 | |
45d6a902 AM |
855 | /* If the old symbol has non-default visibility, we ignore the new |
856 | definition from a dynamic object. */ | |
857 | if (newdyn | |
9c7a29a3 | 858 | && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
859 | && !bfd_is_und_section (sec)) |
860 | { | |
861 | *skip = TRUE; | |
862 | /* Make sure this symbol is dynamic. */ | |
863 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
864 | /* A protected symbol has external availability. Make sure it is | |
865 | recorded as dynamic. | |
866 | ||
867 | FIXME: Should we check type and size for protected symbol? */ | |
868 | if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
869 | return _bfd_elf_link_record_dynamic_symbol (info, h); | |
870 | else | |
871 | return TRUE; | |
872 | } | |
873 | else if (!newdyn | |
9c7a29a3 | 874 | && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT |
45d6a902 AM |
875 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) |
876 | { | |
877 | /* If the new symbol with non-default visibility comes from a | |
878 | relocatable file and the old definition comes from a dynamic | |
879 | object, we remove the old definition. */ | |
880 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
881 | h = *sym_hash; | |
882 | h->root.type = bfd_link_hash_new; | |
883 | h->root.u.undef.abfd = NULL; | |
884 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
252b5132 | 885 | { |
45d6a902 | 886 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; |
22d5e339 L |
887 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC |
888 | | ELF_LINK_DYNAMIC_DEF); | |
45d6a902 AM |
889 | } |
890 | /* FIXME: Should we check type and size for protected symbol? */ | |
891 | h->size = 0; | |
892 | h->type = 0; | |
893 | return TRUE; | |
894 | } | |
14a793b2 | 895 | |
45d6a902 AM |
896 | /* We need to treat weak definiton right, depending on if there is a |
897 | definition from a dynamic object. */ | |
898 | if (bind == STB_WEAK) | |
899 | { | |
900 | if (olddef) | |
901 | { | |
902 | newweakdef = TRUE; | |
903 | newweakundef = FALSE; | |
904 | } | |
905 | else | |
906 | { | |
907 | newweakdef = FALSE; | |
908 | newweakundef = TRUE; | |
909 | } | |
910 | } | |
911 | else | |
912 | newweakdef = newweakundef = FALSE; | |
14a793b2 | 913 | |
45d6a902 AM |
914 | /* If the new weak definition comes from a relocatable file and the |
915 | old symbol comes from a dynamic object, we treat the new one as | |
916 | strong. */ | |
917 | if (newweakdef && !newdyn && olddyn) | |
918 | newweakdef = FALSE; | |
252b5132 | 919 | |
45d6a902 AM |
920 | if (h->root.type == bfd_link_hash_defweak) |
921 | { | |
922 | oldweakdef = TRUE; | |
923 | oldweakundef = FALSE; | |
924 | } | |
925 | else if (h->root.type == bfd_link_hash_undefweak) | |
926 | { | |
927 | oldweakdef = FALSE; | |
928 | oldweakundef = TRUE; | |
929 | } | |
930 | else | |
931 | oldweakdef = oldweakundef = FALSE; | |
932 | ||
933 | /* If the old weak definition comes from a relocatable file and the | |
934 | new symbol comes from a dynamic object, we treat the old one as | |
935 | strong. */ | |
936 | if (oldweakdef && !olddyn && newdyn) | |
937 | oldweakdef = FALSE; | |
938 | ||
939 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old | |
940 | symbol, respectively, appears to be a common symbol in a dynamic | |
941 | object. If a symbol appears in an uninitialized section, and is | |
942 | not weak, and is not a function, then it may be a common symbol | |
943 | which was resolved when the dynamic object was created. We want | |
944 | to treat such symbols specially, because they raise special | |
945 | considerations when setting the symbol size: if the symbol | |
946 | appears as a common symbol in a regular object, and the size in | |
947 | the regular object is larger, we must make sure that we use the | |
948 | larger size. This problematic case can always be avoided in C, | |
949 | but it must be handled correctly when using Fortran shared | |
950 | libraries. | |
951 | ||
952 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
953 | likewise for OLDDYNCOMMON and OLDDEF. | |
954 | ||
955 | Note that this test is just a heuristic, and that it is quite | |
956 | possible to have an uninitialized symbol in a shared object which | |
957 | is really a definition, rather than a common symbol. This could | |
958 | lead to some minor confusion when the symbol really is a common | |
959 | symbol in some regular object. However, I think it will be | |
960 | harmless. */ | |
961 | ||
962 | if (newdyn | |
963 | && newdef | |
964 | && (sec->flags & SEC_ALLOC) != 0 | |
965 | && (sec->flags & SEC_LOAD) == 0 | |
966 | && sym->st_size > 0 | |
967 | && !newweakdef | |
968 | && !newweakundef | |
969 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) | |
970 | newdyncommon = TRUE; | |
971 | else | |
972 | newdyncommon = FALSE; | |
973 | ||
974 | if (olddyn | |
975 | && olddef | |
976 | && h->root.type == bfd_link_hash_defined | |
977 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
978 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 | |
979 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
980 | && h->size > 0 | |
981 | && h->type != STT_FUNC) | |
982 | olddyncommon = TRUE; | |
983 | else | |
984 | olddyncommon = FALSE; | |
985 | ||
986 | /* It's OK to change the type if either the existing symbol or the | |
987 | new symbol is weak unless it comes from a DT_NEEDED entry of | |
988 | a shared object, in which case, the DT_NEEDED entry may not be | |
989 | required at the run time. */ | |
990 | ||
991 | if ((! dt_needed && oldweakdef) | |
992 | || oldweakundef | |
993 | || newweakdef | |
994 | || newweakundef) | |
995 | *type_change_ok = TRUE; | |
996 | ||
997 | /* It's OK to change the size if either the existing symbol or the | |
998 | new symbol is weak, or if the old symbol is undefined. */ | |
999 | ||
1000 | if (*type_change_ok | |
1001 | || h->root.type == bfd_link_hash_undefined) | |
1002 | *size_change_ok = TRUE; | |
1003 | ||
1004 | /* If both the old and the new symbols look like common symbols in a | |
1005 | dynamic object, set the size of the symbol to the larger of the | |
1006 | two. */ | |
1007 | ||
1008 | if (olddyncommon | |
1009 | && newdyncommon | |
1010 | && sym->st_size != h->size) | |
1011 | { | |
1012 | /* Since we think we have two common symbols, issue a multiple | |
1013 | common warning if desired. Note that we only warn if the | |
1014 | size is different. If the size is the same, we simply let | |
1015 | the old symbol override the new one as normally happens with | |
1016 | symbols defined in dynamic objects. */ | |
1017 | ||
1018 | if (! ((*info->callbacks->multiple_common) | |
1019 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1020 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1021 | return FALSE; | |
252b5132 | 1022 | |
45d6a902 AM |
1023 | if (sym->st_size > h->size) |
1024 | h->size = sym->st_size; | |
252b5132 | 1025 | |
45d6a902 | 1026 | *size_change_ok = TRUE; |
252b5132 RH |
1027 | } |
1028 | ||
45d6a902 AM |
1029 | /* If we are looking at a dynamic object, and we have found a |
1030 | definition, we need to see if the symbol was already defined by | |
1031 | some other object. If so, we want to use the existing | |
1032 | definition, and we do not want to report a multiple symbol | |
1033 | definition error; we do this by clobbering *PSEC to be | |
1034 | bfd_und_section_ptr. | |
1035 | ||
1036 | We treat a common symbol as a definition if the symbol in the | |
1037 | shared library is a function, since common symbols always | |
1038 | represent variables; this can cause confusion in principle, but | |
1039 | any such confusion would seem to indicate an erroneous program or | |
1040 | shared library. We also permit a common symbol in a regular | |
1041 | object to override a weak symbol in a shared object. | |
1042 | ||
1043 | We prefer a non-weak definition in a shared library to a weak | |
1044 | definition in the executable unless it comes from a DT_NEEDED | |
1045 | entry of a shared object, in which case, the DT_NEEDED entry | |
1046 | may not be required at the run time. */ | |
1047 | ||
1048 | if (newdyn | |
1049 | && newdef | |
1050 | && (olddef | |
1051 | || (h->root.type == bfd_link_hash_common | |
1052 | && (newweakdef | |
1053 | || newweakundef | |
1054 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC))) | |
1055 | && (!oldweakdef | |
1056 | || dt_needed | |
1057 | || newweakdef | |
1058 | || newweakundef)) | |
1059 | { | |
1060 | *override = TRUE; | |
1061 | newdef = FALSE; | |
1062 | newdyncommon = FALSE; | |
252b5132 | 1063 | |
45d6a902 AM |
1064 | *psec = sec = bfd_und_section_ptr; |
1065 | *size_change_ok = TRUE; | |
252b5132 | 1066 | |
45d6a902 AM |
1067 | /* If we get here when the old symbol is a common symbol, then |
1068 | we are explicitly letting it override a weak symbol or | |
1069 | function in a dynamic object, and we don't want to warn about | |
1070 | a type change. If the old symbol is a defined symbol, a type | |
1071 | change warning may still be appropriate. */ | |
252b5132 | 1072 | |
45d6a902 AM |
1073 | if (h->root.type == bfd_link_hash_common) |
1074 | *type_change_ok = TRUE; | |
1075 | } | |
1076 | ||
1077 | /* Handle the special case of an old common symbol merging with a | |
1078 | new symbol which looks like a common symbol in a shared object. | |
1079 | We change *PSEC and *PVALUE to make the new symbol look like a | |
1080 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
1081 | the right thing. */ | |
1082 | ||
1083 | if (newdyncommon | |
1084 | && h->root.type == bfd_link_hash_common) | |
1085 | { | |
1086 | *override = TRUE; | |
1087 | newdef = FALSE; | |
1088 | newdyncommon = FALSE; | |
1089 | *pvalue = sym->st_size; | |
1090 | *psec = sec = bfd_com_section_ptr; | |
1091 | *size_change_ok = TRUE; | |
1092 | } | |
1093 | ||
1094 | /* If the old symbol is from a dynamic object, and the new symbol is | |
1095 | a definition which is not from a dynamic object, then the new | |
1096 | symbol overrides the old symbol. Symbols from regular files | |
1097 | always take precedence over symbols from dynamic objects, even if | |
1098 | they are defined after the dynamic object in the link. | |
1099 | ||
1100 | As above, we again permit a common symbol in a regular object to | |
1101 | override a definition in a shared object if the shared object | |
1102 | symbol is a function or is weak. | |
1103 | ||
1104 | As above, we permit a non-weak definition in a shared object to | |
1105 | override a weak definition in a regular object. */ | |
1106 | ||
1107 | flip = NULL; | |
1108 | if (! newdyn | |
1109 | && (newdef | |
1110 | || (bfd_is_com_section (sec) | |
1111 | && (oldweakdef || h->type == STT_FUNC))) | |
1112 | && olddyn | |
1113 | && olddef | |
1114 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1115 | && ((!newweakdef && !newweakundef) || oldweakdef)) | |
1116 | { | |
1117 | /* Change the hash table entry to undefined, and let | |
1118 | _bfd_generic_link_add_one_symbol do the right thing with the | |
1119 | new definition. */ | |
1120 | ||
1121 | h->root.type = bfd_link_hash_undefined; | |
1122 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1123 | *size_change_ok = TRUE; | |
1124 | ||
1125 | olddef = FALSE; | |
1126 | olddyncommon = FALSE; | |
1127 | ||
1128 | /* We again permit a type change when a common symbol may be | |
1129 | overriding a function. */ | |
1130 | ||
1131 | if (bfd_is_com_section (sec)) | |
1132 | *type_change_ok = TRUE; | |
1133 | ||
1134 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1135 | flip = *sym_hash; | |
1136 | else | |
1137 | /* This union may have been set to be non-NULL when this symbol | |
1138 | was seen in a dynamic object. We must force the union to be | |
1139 | NULL, so that it is correct for a regular symbol. */ | |
1140 | h->verinfo.vertree = NULL; | |
1141 | } | |
1142 | ||
1143 | /* Handle the special case of a new common symbol merging with an | |
1144 | old symbol that looks like it might be a common symbol defined in | |
1145 | a shared object. Note that we have already handled the case in | |
1146 | which a new common symbol should simply override the definition | |
1147 | in the shared library. */ | |
1148 | ||
1149 | if (! newdyn | |
1150 | && bfd_is_com_section (sec) | |
1151 | && olddyncommon) | |
1152 | { | |
1153 | /* It would be best if we could set the hash table entry to a | |
1154 | common symbol, but we don't know what to use for the section | |
1155 | or the alignment. */ | |
1156 | if (! ((*info->callbacks->multiple_common) | |
1157 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
1158 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
1159 | return FALSE; | |
1160 | ||
1161 | /* If the predumed common symbol in the dynamic object is | |
1162 | larger, pretend that the new symbol has its size. */ | |
1163 | ||
1164 | if (h->size > *pvalue) | |
1165 | *pvalue = h->size; | |
1166 | ||
1167 | /* FIXME: We no longer know the alignment required by the symbol | |
1168 | in the dynamic object, so we just wind up using the one from | |
1169 | the regular object. */ | |
1170 | ||
1171 | olddef = FALSE; | |
1172 | olddyncommon = FALSE; | |
1173 | ||
1174 | h->root.type = bfd_link_hash_undefined; | |
1175 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
1176 | ||
1177 | *size_change_ok = TRUE; | |
1178 | *type_change_ok = TRUE; | |
1179 | ||
1180 | if ((*sym_hash)->root.type == bfd_link_hash_indirect) | |
1181 | flip = *sym_hash; | |
1182 | else | |
1183 | h->verinfo.vertree = NULL; | |
1184 | } | |
1185 | ||
1186 | if (flip != NULL) | |
1187 | { | |
1188 | /* Handle the case where we had a versioned symbol in a dynamic | |
1189 | library and now find a definition in a normal object. In this | |
1190 | case, we make the versioned symbol point to the normal one. */ | |
1191 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1192 | flip->root.type = h->root.type; | |
1193 | h->root.type = bfd_link_hash_indirect; | |
1194 | h->root.u.i.link = (struct bfd_link_hash_entry *) flip; | |
1195 | (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h); | |
1196 | flip->root.u.undef.abfd = h->root.u.undef.abfd; | |
1197 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1198 | { | |
1199 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC; | |
1200 | flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | /* Handle the special case of a weak definition in a regular object | |
1205 | followed by a non-weak definition in a shared object. In this | |
1206 | case, we prefer the definition in the shared object unless it | |
1207 | comes from a DT_NEEDED entry of a shared object, in which case, | |
1208 | the DT_NEEDED entry may not be required at the run time. */ | |
1209 | if (olddef | |
1210 | && ! dt_needed | |
1211 | && oldweakdef | |
1212 | && newdef | |
1213 | && newdyn | |
1214 | && !newweakdef | |
1215 | && !newweakundef) | |
1216 | { | |
1217 | /* To make this work we have to frob the flags so that the rest | |
1218 | of the code does not think we are using the regular | |
1219 | definition. */ | |
1220 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
1221 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
1222 | else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) | |
1223 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1224 | h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR | |
1225 | | ELF_LINK_HASH_DEF_DYNAMIC); | |
1226 | ||
1227 | /* If H is the target of an indirection, we want the caller to | |
1228 | use H rather than the indirect symbol. Otherwise if we are | |
1229 | defining a new indirect symbol we will wind up attaching it | |
1230 | to the entry we are overriding. */ | |
1231 | *sym_hash = h; | |
1232 | } | |
1233 | ||
1234 | /* Handle the special case of a non-weak definition in a shared | |
1235 | object followed by a weak definition in a regular object. In | |
1236 | this case we prefer the definition in the shared object. To make | |
1237 | this work we have to tell the caller to not treat the new symbol | |
1238 | as a definition. */ | |
1239 | if (olddef | |
1240 | && olddyn | |
1241 | && !oldweakdef | |
1242 | && newdef | |
1243 | && ! newdyn | |
1244 | && (newweakdef || newweakundef)) | |
1245 | *override = TRUE; | |
1246 | ||
1247 | return TRUE; | |
1248 | } | |
1249 | ||
1250 | /* This function is called to create an indirect symbol from the | |
1251 | default for the symbol with the default version if needed. The | |
1252 | symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We | |
1253 | set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED | |
1254 | indicates if it comes from a DT_NEEDED entry of a shared object. */ | |
1255 | ||
1256 | bfd_boolean | |
1257 | _bfd_elf_add_default_symbol (abfd, info, h, name, sym, psec, value, | |
1258 | dynsym, override, dt_needed) | |
1259 | bfd *abfd; | |
1260 | struct bfd_link_info *info; | |
1261 | struct elf_link_hash_entry *h; | |
1262 | const char *name; | |
1263 | Elf_Internal_Sym *sym; | |
1264 | asection **psec; | |
1265 | bfd_vma *value; | |
1266 | bfd_boolean *dynsym; | |
1267 | bfd_boolean override; | |
1268 | bfd_boolean dt_needed; | |
1269 | { | |
1270 | bfd_boolean type_change_ok; | |
1271 | bfd_boolean size_change_ok; | |
1272 | bfd_boolean skip; | |
1273 | char *shortname; | |
1274 | struct elf_link_hash_entry *hi; | |
1275 | struct bfd_link_hash_entry *bh; | |
1276 | struct elf_backend_data *bed; | |
1277 | bfd_boolean collect; | |
1278 | bfd_boolean dynamic; | |
1279 | char *p; | |
1280 | size_t len, shortlen; | |
1281 | asection *sec; | |
1282 | ||
1283 | /* If this symbol has a version, and it is the default version, we | |
1284 | create an indirect symbol from the default name to the fully | |
1285 | decorated name. This will cause external references which do not | |
1286 | specify a version to be bound to this version of the symbol. */ | |
1287 | p = strchr (name, ELF_VER_CHR); | |
1288 | if (p == NULL || p[1] != ELF_VER_CHR) | |
1289 | return TRUE; | |
1290 | ||
1291 | if (override) | |
1292 | { | |
1293 | /* We are overridden by an old defition. We need to check if we | |
1294 | need to create the indirect symbol from the default name. */ | |
1295 | hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, | |
1296 | FALSE, FALSE); | |
1297 | BFD_ASSERT (hi != NULL); | |
1298 | if (hi == h) | |
1299 | return TRUE; | |
1300 | while (hi->root.type == bfd_link_hash_indirect | |
1301 | || hi->root.type == bfd_link_hash_warning) | |
1302 | { | |
1303 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1304 | if (hi == h) | |
1305 | return TRUE; | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | bed = get_elf_backend_data (abfd); | |
1310 | collect = bed->collect; | |
1311 | dynamic = (abfd->flags & DYNAMIC) != 0; | |
1312 | ||
1313 | shortlen = p - name; | |
1314 | shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); | |
1315 | if (shortname == NULL) | |
1316 | return FALSE; | |
1317 | memcpy (shortname, name, shortlen); | |
1318 | shortname[shortlen] = '\0'; | |
1319 | ||
1320 | /* We are going to create a new symbol. Merge it with any existing | |
1321 | symbol with this name. For the purposes of the merge, act as | |
1322 | though we were defining the symbol we just defined, although we | |
1323 | actually going to define an indirect symbol. */ | |
1324 | type_change_ok = FALSE; | |
1325 | size_change_ok = FALSE; | |
1326 | sec = *psec; | |
1327 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1328 | &hi, &skip, &override, &type_change_ok, | |
1329 | &size_change_ok, dt_needed)) | |
1330 | return FALSE; | |
1331 | ||
1332 | if (skip) | |
1333 | goto nondefault; | |
1334 | ||
1335 | if (! override) | |
1336 | { | |
1337 | bh = &hi->root; | |
1338 | if (! (_bfd_generic_link_add_one_symbol | |
1339 | (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, | |
1340 | (bfd_vma) 0, name, FALSE, collect, &bh))) | |
1341 | return FALSE; | |
1342 | hi = (struct elf_link_hash_entry *) bh; | |
1343 | } | |
1344 | else | |
1345 | { | |
1346 | /* In this case the symbol named SHORTNAME is overriding the | |
1347 | indirect symbol we want to add. We were planning on making | |
1348 | SHORTNAME an indirect symbol referring to NAME. SHORTNAME | |
1349 | is the name without a version. NAME is the fully versioned | |
1350 | name, and it is the default version. | |
1351 | ||
1352 | Overriding means that we already saw a definition for the | |
1353 | symbol SHORTNAME in a regular object, and it is overriding | |
1354 | the symbol defined in the dynamic object. | |
1355 | ||
1356 | When this happens, we actually want to change NAME, the | |
1357 | symbol we just added, to refer to SHORTNAME. This will cause | |
1358 | references to NAME in the shared object to become references | |
1359 | to SHORTNAME in the regular object. This is what we expect | |
1360 | when we override a function in a shared object: that the | |
1361 | references in the shared object will be mapped to the | |
1362 | definition in the regular object. */ | |
1363 | ||
1364 | while (hi->root.type == bfd_link_hash_indirect | |
1365 | || hi->root.type == bfd_link_hash_warning) | |
1366 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1367 | ||
1368 | h->root.type = bfd_link_hash_indirect; | |
1369 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
1370 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1371 | { | |
1372 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC; | |
1373 | hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1374 | if (hi->elf_link_hash_flags | |
1375 | & (ELF_LINK_HASH_REF_REGULAR | |
1376 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1377 | { | |
1378 | if (! _bfd_elf_link_record_dynamic_symbol (info, hi)) | |
1379 | return FALSE; | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | /* Now set HI to H, so that the following code will set the | |
1384 | other fields correctly. */ | |
1385 | hi = h; | |
1386 | } | |
1387 | ||
1388 | /* If there is a duplicate definition somewhere, then HI may not | |
1389 | point to an indirect symbol. We will have reported an error to | |
1390 | the user in that case. */ | |
1391 | ||
1392 | if (hi->root.type == bfd_link_hash_indirect) | |
1393 | { | |
1394 | struct elf_link_hash_entry *ht; | |
1395 | ||
1396 | /* If the symbol became indirect, then we assume that we have | |
1397 | not seen a definition before. */ | |
1398 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1399 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1400 | | ELF_LINK_HASH_DEF_REGULAR)) == 0); | |
1401 | ||
1402 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1403 | (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi); | |
1404 | ||
1405 | /* See if the new flags lead us to realize that the symbol must | |
1406 | be dynamic. */ | |
1407 | if (! *dynsym) | |
1408 | { | |
1409 | if (! dynamic) | |
1410 | { | |
1411 | if (info->shared | |
1412 | || ((hi->elf_link_hash_flags | |
1413 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
1414 | *dynsym = TRUE; | |
1415 | } | |
1416 | else | |
1417 | { | |
1418 | if ((hi->elf_link_hash_flags | |
1419 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1420 | *dynsym = TRUE; | |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | /* We also need to define an indirection from the nondefault version | |
1426 | of the symbol. */ | |
1427 | ||
1428 | nondefault: | |
1429 | len = strlen (name); | |
1430 | shortname = bfd_hash_allocate (&info->hash->table, len); | |
1431 | if (shortname == NULL) | |
1432 | return FALSE; | |
1433 | memcpy (shortname, name, shortlen); | |
1434 | memcpy (shortname + shortlen, p + 1, len - shortlen); | |
1435 | ||
1436 | /* Once again, merge with any existing symbol. */ | |
1437 | type_change_ok = FALSE; | |
1438 | size_change_ok = FALSE; | |
1439 | sec = *psec; | |
1440 | if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, | |
1441 | &hi, &skip, &override, &type_change_ok, | |
1442 | &size_change_ok, dt_needed)) | |
1443 | return FALSE; | |
1444 | ||
1445 | if (skip) | |
1446 | return TRUE; | |
1447 | ||
1448 | if (override) | |
1449 | { | |
1450 | /* Here SHORTNAME is a versioned name, so we don't expect to see | |
1451 | the type of override we do in the case above unless it is | |
1452 | overridden by a versioned definiton. */ | |
1453 | if (hi->root.type != bfd_link_hash_defined | |
1454 | && hi->root.type != bfd_link_hash_defweak) | |
1455 | (*_bfd_error_handler) | |
1456 | (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"), | |
1457 | bfd_archive_filename (abfd), shortname); | |
1458 | } | |
1459 | else | |
1460 | { | |
1461 | bh = &hi->root; | |
1462 | if (! (_bfd_generic_link_add_one_symbol | |
1463 | (info, abfd, shortname, BSF_INDIRECT, | |
1464 | bfd_ind_section_ptr, (bfd_vma) 0, name, FALSE, collect, &bh))) | |
1465 | return FALSE; | |
1466 | hi = (struct elf_link_hash_entry *) bh; | |
1467 | ||
1468 | /* If there is a duplicate definition somewhere, then HI may not | |
1469 | point to an indirect symbol. We will have reported an error | |
1470 | to the user in that case. */ | |
1471 | ||
1472 | if (hi->root.type == bfd_link_hash_indirect) | |
1473 | { | |
1474 | /* If the symbol became indirect, then we assume that we have | |
1475 | not seen a definition before. */ | |
1476 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1477 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1478 | | ELF_LINK_HASH_DEF_REGULAR)) == 0); | |
1479 | ||
1480 | (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi); | |
1481 | ||
1482 | /* See if the new flags lead us to realize that the symbol | |
1483 | must be dynamic. */ | |
1484 | if (! *dynsym) | |
1485 | { | |
1486 | if (! dynamic) | |
1487 | { | |
1488 | if (info->shared | |
1489 | || ((hi->elf_link_hash_flags | |
1490 | & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
1491 | *dynsym = TRUE; | |
1492 | } | |
1493 | else | |
1494 | { | |
1495 | if ((hi->elf_link_hash_flags | |
1496 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1497 | *dynsym = TRUE; | |
1498 | } | |
1499 | } | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | return TRUE; | |
1504 | } | |
1505 | \f | |
1506 | /* This routine is used to export all defined symbols into the dynamic | |
1507 | symbol table. It is called via elf_link_hash_traverse. */ | |
1508 | ||
1509 | bfd_boolean | |
1510 | _bfd_elf_export_symbol (h, data) | |
1511 | struct elf_link_hash_entry *h; | |
1512 | PTR data; | |
1513 | { | |
1514 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
1515 | ||
1516 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
1517 | if (h->root.type == bfd_link_hash_indirect) | |
1518 | return TRUE; | |
1519 | ||
1520 | if (h->root.type == bfd_link_hash_warning) | |
1521 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1522 | ||
1523 | if (h->dynindx == -1 | |
1524 | && (h->elf_link_hash_flags | |
1525 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
1526 | { | |
1527 | struct bfd_elf_version_tree *t; | |
1528 | struct bfd_elf_version_expr *d; | |
1529 | ||
1530 | for (t = eif->verdefs; t != NULL; t = t->next) | |
1531 | { | |
1532 | if (t->globals != NULL) | |
1533 | { | |
1534 | for (d = t->globals; d != NULL; d = d->next) | |
1535 | { | |
1536 | if ((*d->match) (d, h->root.root.string)) | |
1537 | goto doit; | |
1538 | } | |
1539 | } | |
1540 | ||
1541 | if (t->locals != NULL) | |
1542 | { | |
1543 | for (d = t->locals ; d != NULL; d = d->next) | |
1544 | { | |
1545 | if ((*d->match) (d, h->root.root.string)) | |
1546 | return TRUE; | |
1547 | } | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | if (!eif->verdefs) | |
1552 | { | |
1553 | doit: | |
1554 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
1555 | { | |
1556 | eif->failed = TRUE; | |
1557 | return FALSE; | |
1558 | } | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | return TRUE; | |
1563 | } | |
1564 | \f | |
1565 | /* Look through the symbols which are defined in other shared | |
1566 | libraries and referenced here. Update the list of version | |
1567 | dependencies. This will be put into the .gnu.version_r section. | |
1568 | This function is called via elf_link_hash_traverse. */ | |
1569 | ||
1570 | bfd_boolean | |
1571 | _bfd_elf_link_find_version_dependencies (h, data) | |
1572 | struct elf_link_hash_entry *h; | |
1573 | PTR data; | |
1574 | { | |
1575 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; | |
1576 | Elf_Internal_Verneed *t; | |
1577 | Elf_Internal_Vernaux *a; | |
1578 | bfd_size_type amt; | |
1579 | ||
1580 | if (h->root.type == bfd_link_hash_warning) | |
1581 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1582 | ||
1583 | /* We only care about symbols defined in shared objects with version | |
1584 | information. */ | |
1585 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
1586 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1587 | || h->dynindx == -1 | |
1588 | || h->verinfo.verdef == NULL) | |
1589 | return TRUE; | |
1590 | ||
1591 | /* See if we already know about this version. */ | |
1592 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
1593 | { | |
1594 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) | |
1595 | continue; | |
1596 | ||
1597 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1598 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
1599 | return TRUE; | |
1600 | ||
1601 | break; | |
1602 | } | |
1603 | ||
1604 | /* This is a new version. Add it to tree we are building. */ | |
1605 | ||
1606 | if (t == NULL) | |
1607 | { | |
1608 | amt = sizeof *t; | |
1609 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt); | |
1610 | if (t == NULL) | |
1611 | { | |
1612 | rinfo->failed = TRUE; | |
1613 | return FALSE; | |
1614 | } | |
1615 | ||
1616 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
1617 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
1618 | elf_tdata (rinfo->output_bfd)->verref = t; | |
1619 | } | |
1620 | ||
1621 | amt = sizeof *a; | |
1622 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt); | |
1623 | ||
1624 | /* Note that we are copying a string pointer here, and testing it | |
1625 | above. If bfd_elf_string_from_elf_section is ever changed to | |
1626 | discard the string data when low in memory, this will have to be | |
1627 | fixed. */ | |
1628 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
1629 | ||
1630 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
1631 | a->vna_nextptr = t->vn_auxptr; | |
1632 | ||
1633 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
1634 | ++rinfo->vers; | |
1635 | ||
1636 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
1637 | ||
1638 | t->vn_auxptr = a; | |
1639 | ||
1640 | return TRUE; | |
1641 | } | |
1642 | ||
1643 | /* Figure out appropriate versions for all the symbols. We may not | |
1644 | have the version number script until we have read all of the input | |
1645 | files, so until that point we don't know which symbols should be | |
1646 | local. This function is called via elf_link_hash_traverse. */ | |
1647 | ||
1648 | bfd_boolean | |
1649 | _bfd_elf_link_assign_sym_version (h, data) | |
1650 | struct elf_link_hash_entry *h; | |
1651 | PTR data; | |
1652 | { | |
1653 | struct elf_assign_sym_version_info *sinfo; | |
1654 | struct bfd_link_info *info; | |
1655 | struct elf_backend_data *bed; | |
1656 | struct elf_info_failed eif; | |
1657 | char *p; | |
1658 | bfd_size_type amt; | |
1659 | ||
1660 | sinfo = (struct elf_assign_sym_version_info *) data; | |
1661 | info = sinfo->info; | |
1662 | ||
1663 | if (h->root.type == bfd_link_hash_warning) | |
1664 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1665 | ||
1666 | /* Fix the symbol flags. */ | |
1667 | eif.failed = FALSE; | |
1668 | eif.info = info; | |
1669 | if (! _bfd_elf_fix_symbol_flags (h, &eif)) | |
1670 | { | |
1671 | if (eif.failed) | |
1672 | sinfo->failed = TRUE; | |
1673 | return FALSE; | |
1674 | } | |
1675 | ||
1676 | /* We only need version numbers for symbols defined in regular | |
1677 | objects. */ | |
1678 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1679 | return TRUE; | |
1680 | ||
1681 | bed = get_elf_backend_data (sinfo->output_bfd); | |
1682 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
1683 | if (p != NULL && h->verinfo.vertree == NULL) | |
1684 | { | |
1685 | struct bfd_elf_version_tree *t; | |
1686 | bfd_boolean hidden; | |
1687 | ||
1688 | hidden = TRUE; | |
1689 | ||
1690 | /* There are two consecutive ELF_VER_CHR characters if this is | |
1691 | not a hidden symbol. */ | |
1692 | ++p; | |
1693 | if (*p == ELF_VER_CHR) | |
1694 | { | |
1695 | hidden = FALSE; | |
1696 | ++p; | |
1697 | } | |
1698 | ||
1699 | /* If there is no version string, we can just return out. */ | |
1700 | if (*p == '\0') | |
1701 | { | |
1702 | if (hidden) | |
1703 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
1704 | return TRUE; | |
1705 | } | |
1706 | ||
1707 | /* Look for the version. If we find it, it is no longer weak. */ | |
1708 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1709 | { | |
1710 | if (strcmp (t->name, p) == 0) | |
1711 | { | |
1712 | size_t len; | |
1713 | char *alc; | |
1714 | struct bfd_elf_version_expr *d; | |
1715 | ||
1716 | len = p - h->root.root.string; | |
1717 | alc = bfd_malloc ((bfd_size_type) len); | |
1718 | if (alc == NULL) | |
1719 | return FALSE; | |
1720 | memcpy (alc, h->root.root.string, len - 1); | |
1721 | alc[len - 1] = '\0'; | |
1722 | if (alc[len - 2] == ELF_VER_CHR) | |
1723 | alc[len - 2] = '\0'; | |
1724 | ||
1725 | h->verinfo.vertree = t; | |
1726 | t->used = TRUE; | |
1727 | d = NULL; | |
1728 | ||
1729 | if (t->globals != NULL) | |
1730 | { | |
1731 | for (d = t->globals; d != NULL; d = d->next) | |
1732 | if ((*d->match) (d, alc)) | |
1733 | break; | |
1734 | } | |
1735 | ||
1736 | /* See if there is anything to force this symbol to | |
1737 | local scope. */ | |
1738 | if (d == NULL && t->locals != NULL) | |
1739 | { | |
1740 | for (d = t->locals; d != NULL; d = d->next) | |
1741 | { | |
1742 | if ((*d->match) (d, alc)) | |
1743 | { | |
1744 | if (h->dynindx != -1 | |
1745 | && info->shared | |
1746 | && ! info->export_dynamic) | |
1747 | { | |
1748 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1749 | } | |
1750 | ||
1751 | break; | |
1752 | } | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | free (alc); | |
1757 | break; | |
1758 | } | |
1759 | } | |
1760 | ||
1761 | /* If we are building an application, we need to create a | |
1762 | version node for this version. */ | |
36af4a4e | 1763 | if (t == NULL && info->executable) |
45d6a902 AM |
1764 | { |
1765 | struct bfd_elf_version_tree **pp; | |
1766 | int version_index; | |
1767 | ||
1768 | /* If we aren't going to export this symbol, we don't need | |
1769 | to worry about it. */ | |
1770 | if (h->dynindx == -1) | |
1771 | return TRUE; | |
1772 | ||
1773 | amt = sizeof *t; | |
1774 | t = ((struct bfd_elf_version_tree *) | |
1775 | bfd_alloc (sinfo->output_bfd, amt)); | |
1776 | if (t == NULL) | |
1777 | { | |
1778 | sinfo->failed = TRUE; | |
1779 | return FALSE; | |
1780 | } | |
1781 | ||
1782 | t->next = NULL; | |
1783 | t->name = p; | |
1784 | t->globals = NULL; | |
1785 | t->locals = NULL; | |
1786 | t->deps = NULL; | |
1787 | t->name_indx = (unsigned int) -1; | |
1788 | t->used = TRUE; | |
1789 | ||
1790 | version_index = 1; | |
1791 | /* Don't count anonymous version tag. */ | |
1792 | if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) | |
1793 | version_index = 0; | |
1794 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
1795 | ++version_index; | |
1796 | t->vernum = version_index; | |
1797 | ||
1798 | *pp = t; | |
1799 | ||
1800 | h->verinfo.vertree = t; | |
1801 | } | |
1802 | else if (t == NULL) | |
1803 | { | |
1804 | /* We could not find the version for a symbol when | |
1805 | generating a shared archive. Return an error. */ | |
1806 | (*_bfd_error_handler) | |
1807 | (_("%s: undefined versioned symbol name %s"), | |
1808 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); | |
1809 | bfd_set_error (bfd_error_bad_value); | |
1810 | sinfo->failed = TRUE; | |
1811 | return FALSE; | |
1812 | } | |
1813 | ||
1814 | if (hidden) | |
1815 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
1816 | } | |
1817 | ||
1818 | /* If we don't have a version for this symbol, see if we can find | |
1819 | something. */ | |
1820 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
1821 | { | |
1822 | struct bfd_elf_version_tree *t; | |
1823 | struct bfd_elf_version_tree *local_ver; | |
1824 | struct bfd_elf_version_expr *d; | |
1825 | ||
1826 | /* See if can find what version this symbol is in. If the | |
1827 | symbol is supposed to be local, then don't actually register | |
1828 | it. */ | |
1829 | local_ver = NULL; | |
1830 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
1831 | { | |
1832 | if (t->globals != NULL) | |
1833 | { | |
1834 | bfd_boolean matched; | |
1835 | ||
1836 | matched = FALSE; | |
1837 | for (d = t->globals; d != NULL; d = d->next) | |
1838 | { | |
1839 | if ((*d->match) (d, h->root.root.string)) | |
1840 | { | |
1841 | if (d->symver) | |
1842 | matched = TRUE; | |
1843 | else | |
1844 | { | |
1845 | /* There is a version without definition. Make | |
1846 | the symbol the default definition for this | |
1847 | version. */ | |
1848 | h->verinfo.vertree = t; | |
1849 | local_ver = NULL; | |
1850 | d->script = 1; | |
1851 | break; | |
1852 | } | |
1853 | } | |
1854 | } | |
1855 | ||
1856 | if (d != NULL) | |
1857 | break; | |
1858 | else if (matched) | |
1859 | /* There is no undefined version for this symbol. Hide the | |
1860 | default one. */ | |
1861 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1862 | } | |
1863 | ||
1864 | if (t->locals != NULL) | |
1865 | { | |
1866 | for (d = t->locals; d != NULL; d = d->next) | |
1867 | { | |
1868 | /* If the match is "*", keep looking for a more | |
1869 | explicit, perhaps even global, match. */ | |
1870 | if (d->pattern[0] == '*' && d->pattern[1] == '\0') | |
1871 | local_ver = t; | |
1872 | else if ((*d->match) (d, h->root.root.string)) | |
1873 | { | |
1874 | local_ver = t; | |
1875 | break; | |
1876 | } | |
1877 | } | |
1878 | ||
1879 | if (d != NULL) | |
1880 | break; | |
1881 | } | |
1882 | } | |
1883 | ||
1884 | if (local_ver != NULL) | |
1885 | { | |
1886 | h->verinfo.vertree = local_ver; | |
1887 | if (h->dynindx != -1 | |
1888 | && info->shared | |
1889 | && ! info->export_dynamic) | |
1890 | { | |
1891 | (*bed->elf_backend_hide_symbol) (info, h, TRUE); | |
1892 | } | |
1893 | } | |
1894 | } | |
1895 | ||
1896 | return TRUE; | |
1897 | } | |
1898 | \f | |
45d6a902 AM |
1899 | /* Read and swap the relocs from the section indicated by SHDR. This |
1900 | may be either a REL or a RELA section. The relocations are | |
1901 | translated into RELA relocations and stored in INTERNAL_RELOCS, | |
1902 | which should have already been allocated to contain enough space. | |
1903 | The EXTERNAL_RELOCS are a buffer where the external form of the | |
1904 | relocations should be stored. | |
1905 | ||
1906 | Returns FALSE if something goes wrong. */ | |
1907 | ||
1908 | static bfd_boolean | |
1909 | elf_link_read_relocs_from_section (abfd, shdr, external_relocs, | |
1910 | internal_relocs) | |
1911 | bfd *abfd; | |
1912 | Elf_Internal_Shdr *shdr; | |
1913 | PTR external_relocs; | |
1914 | Elf_Internal_Rela *internal_relocs; | |
1915 | { | |
1916 | struct elf_backend_data *bed; | |
1917 | void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *)); | |
1918 | const bfd_byte *erela; | |
1919 | const bfd_byte *erelaend; | |
1920 | Elf_Internal_Rela *irela; | |
1921 | ||
1922 | /* If there aren't any relocations, that's OK. */ | |
1923 | if (!shdr) | |
1924 | return TRUE; | |
1925 | ||
1926 | /* Position ourselves at the start of the section. */ | |
1927 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) | |
1928 | return FALSE; | |
1929 | ||
1930 | /* Read the relocations. */ | |
1931 | if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) | |
1932 | return FALSE; | |
1933 | ||
1934 | bed = get_elf_backend_data (abfd); | |
1935 | ||
1936 | /* Convert the external relocations to the internal format. */ | |
1937 | if (shdr->sh_entsize == bed->s->sizeof_rel) | |
1938 | swap_in = bed->s->swap_reloc_in; | |
1939 | else if (shdr->sh_entsize == bed->s->sizeof_rela) | |
1940 | swap_in = bed->s->swap_reloca_in; | |
1941 | else | |
1942 | { | |
1943 | bfd_set_error (bfd_error_wrong_format); | |
1944 | return FALSE; | |
1945 | } | |
1946 | ||
1947 | erela = external_relocs; | |
1948 | erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize; | |
1949 | irela = internal_relocs; | |
1950 | while (erela < erelaend) | |
1951 | { | |
1952 | (*swap_in) (abfd, erela, irela); | |
1953 | irela += bed->s->int_rels_per_ext_rel; | |
1954 | erela += shdr->sh_entsize; | |
1955 | } | |
1956 | ||
1957 | return TRUE; | |
1958 | } | |
1959 | ||
1960 | /* Read and swap the relocs for a section O. They may have been | |
1961 | cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are | |
1962 | not NULL, they are used as buffers to read into. They are known to | |
1963 | be large enough. If the INTERNAL_RELOCS relocs argument is NULL, | |
1964 | the return value is allocated using either malloc or bfd_alloc, | |
1965 | according to the KEEP_MEMORY argument. If O has two relocation | |
1966 | sections (both REL and RELA relocations), then the REL_HDR | |
1967 | relocations will appear first in INTERNAL_RELOCS, followed by the | |
1968 | REL_HDR2 relocations. */ | |
1969 | ||
1970 | Elf_Internal_Rela * | |
1971 | _bfd_elf_link_read_relocs (abfd, o, external_relocs, internal_relocs, | |
1972 | keep_memory) | |
1973 | bfd *abfd; | |
1974 | asection *o; | |
1975 | PTR external_relocs; | |
1976 | Elf_Internal_Rela *internal_relocs; | |
1977 | bfd_boolean keep_memory; | |
1978 | { | |
1979 | Elf_Internal_Shdr *rel_hdr; | |
1980 | PTR alloc1 = NULL; | |
1981 | Elf_Internal_Rela *alloc2 = NULL; | |
1982 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1983 | ||
1984 | if (elf_section_data (o)->relocs != NULL) | |
1985 | return elf_section_data (o)->relocs; | |
1986 | ||
1987 | if (o->reloc_count == 0) | |
1988 | return NULL; | |
1989 | ||
1990 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1991 | ||
1992 | if (internal_relocs == NULL) | |
1993 | { | |
1994 | bfd_size_type size; | |
1995 | ||
1996 | size = o->reloc_count; | |
1997 | size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); | |
1998 | if (keep_memory) | |
1999 | internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size); | |
2000 | else | |
2001 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); | |
2002 | if (internal_relocs == NULL) | |
2003 | goto error_return; | |
2004 | } | |
2005 | ||
2006 | if (external_relocs == NULL) | |
2007 | { | |
2008 | bfd_size_type size = rel_hdr->sh_size; | |
2009 | ||
2010 | if (elf_section_data (o)->rel_hdr2) | |
2011 | size += elf_section_data (o)->rel_hdr2->sh_size; | |
2012 | alloc1 = (PTR) bfd_malloc (size); | |
2013 | if (alloc1 == NULL) | |
2014 | goto error_return; | |
2015 | external_relocs = alloc1; | |
2016 | } | |
2017 | ||
2018 | if (!elf_link_read_relocs_from_section (abfd, rel_hdr, | |
2019 | external_relocs, | |
2020 | internal_relocs)) | |
2021 | goto error_return; | |
2022 | if (!elf_link_read_relocs_from_section | |
2023 | (abfd, | |
2024 | elf_section_data (o)->rel_hdr2, | |
2025 | ((bfd_byte *) external_relocs) + rel_hdr->sh_size, | |
2026 | internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) | |
2027 | * bed->s->int_rels_per_ext_rel))) | |
2028 | goto error_return; | |
2029 | ||
2030 | /* Cache the results for next time, if we can. */ | |
2031 | if (keep_memory) | |
2032 | elf_section_data (o)->relocs = internal_relocs; | |
2033 | ||
2034 | if (alloc1 != NULL) | |
2035 | free (alloc1); | |
2036 | ||
2037 | /* Don't free alloc2, since if it was allocated we are passing it | |
2038 | back (under the name of internal_relocs). */ | |
2039 | ||
2040 | return internal_relocs; | |
2041 | ||
2042 | error_return: | |
2043 | if (alloc1 != NULL) | |
2044 | free (alloc1); | |
2045 | if (alloc2 != NULL) | |
2046 | free (alloc2); | |
2047 | return NULL; | |
2048 | } | |
2049 | ||
2050 | /* Compute the size of, and allocate space for, REL_HDR which is the | |
2051 | section header for a section containing relocations for O. */ | |
2052 | ||
2053 | bfd_boolean | |
2054 | _bfd_elf_link_size_reloc_section (abfd, rel_hdr, o) | |
2055 | bfd *abfd; | |
2056 | Elf_Internal_Shdr *rel_hdr; | |
2057 | asection *o; | |
2058 | { | |
2059 | bfd_size_type reloc_count; | |
2060 | bfd_size_type num_rel_hashes; | |
2061 | ||
2062 | /* Figure out how many relocations there will be. */ | |
2063 | if (rel_hdr == &elf_section_data (o)->rel_hdr) | |
2064 | reloc_count = elf_section_data (o)->rel_count; | |
2065 | else | |
2066 | reloc_count = elf_section_data (o)->rel_count2; | |
2067 | ||
2068 | num_rel_hashes = o->reloc_count; | |
2069 | if (num_rel_hashes < reloc_count) | |
2070 | num_rel_hashes = reloc_count; | |
2071 | ||
2072 | /* That allows us to calculate the size of the section. */ | |
2073 | rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; | |
2074 | ||
2075 | /* The contents field must last into write_object_contents, so we | |
2076 | allocate it with bfd_alloc rather than malloc. Also since we | |
2077 | cannot be sure that the contents will actually be filled in, | |
2078 | we zero the allocated space. */ | |
2079 | rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size); | |
2080 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) | |
2081 | return FALSE; | |
2082 | ||
2083 | /* We only allocate one set of hash entries, so we only do it the | |
2084 | first time we are called. */ | |
2085 | if (elf_section_data (o)->rel_hashes == NULL | |
2086 | && num_rel_hashes) | |
2087 | { | |
2088 | struct elf_link_hash_entry **p; | |
2089 | ||
2090 | p = ((struct elf_link_hash_entry **) | |
2091 | bfd_zmalloc (num_rel_hashes | |
2092 | * sizeof (struct elf_link_hash_entry *))); | |
2093 | if (p == NULL) | |
2094 | return FALSE; | |
2095 | ||
2096 | elf_section_data (o)->rel_hashes = p; | |
2097 | } | |
2098 | ||
2099 | return TRUE; | |
2100 | } | |
2101 | ||
2102 | /* Copy the relocations indicated by the INTERNAL_RELOCS (which | |
2103 | originated from the section given by INPUT_REL_HDR) to the | |
2104 | OUTPUT_BFD. */ | |
2105 | ||
2106 | bfd_boolean | |
2107 | _bfd_elf_link_output_relocs (output_bfd, input_section, input_rel_hdr, | |
2108 | internal_relocs) | |
2109 | bfd *output_bfd; | |
2110 | asection *input_section; | |
2111 | Elf_Internal_Shdr *input_rel_hdr; | |
2112 | Elf_Internal_Rela *internal_relocs; | |
2113 | { | |
2114 | Elf_Internal_Rela *irela; | |
2115 | Elf_Internal_Rela *irelaend; | |
2116 | bfd_byte *erel; | |
2117 | Elf_Internal_Shdr *output_rel_hdr; | |
2118 | asection *output_section; | |
2119 | unsigned int *rel_countp = NULL; | |
2120 | struct elf_backend_data *bed; | |
2121 | void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *)); | |
2122 | ||
2123 | output_section = input_section->output_section; | |
2124 | output_rel_hdr = NULL; | |
2125 | ||
2126 | if (elf_section_data (output_section)->rel_hdr.sh_entsize | |
2127 | == input_rel_hdr->sh_entsize) | |
2128 | { | |
2129 | output_rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
2130 | rel_countp = &elf_section_data (output_section)->rel_count; | |
2131 | } | |
2132 | else if (elf_section_data (output_section)->rel_hdr2 | |
2133 | && (elf_section_data (output_section)->rel_hdr2->sh_entsize | |
2134 | == input_rel_hdr->sh_entsize)) | |
2135 | { | |
2136 | output_rel_hdr = elf_section_data (output_section)->rel_hdr2; | |
2137 | rel_countp = &elf_section_data (output_section)->rel_count2; | |
2138 | } | |
2139 | else | |
2140 | { | |
2141 | (*_bfd_error_handler) | |
2142 | (_("%s: relocation size mismatch in %s section %s"), | |
2143 | bfd_get_filename (output_bfd), | |
2144 | bfd_archive_filename (input_section->owner), | |
2145 | input_section->name); | |
2146 | bfd_set_error (bfd_error_wrong_object_format); | |
2147 | return FALSE; | |
2148 | } | |
2149 | ||
2150 | bed = get_elf_backend_data (output_bfd); | |
2151 | if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) | |
2152 | swap_out = bed->s->swap_reloc_out; | |
2153 | else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) | |
2154 | swap_out = bed->s->swap_reloca_out; | |
2155 | else | |
2156 | abort (); | |
2157 | ||
2158 | erel = output_rel_hdr->contents; | |
2159 | erel += *rel_countp * input_rel_hdr->sh_entsize; | |
2160 | irela = internal_relocs; | |
2161 | irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) | |
2162 | * bed->s->int_rels_per_ext_rel); | |
2163 | while (irela < irelaend) | |
2164 | { | |
2165 | (*swap_out) (output_bfd, irela, erel); | |
2166 | irela += bed->s->int_rels_per_ext_rel; | |
2167 | erel += input_rel_hdr->sh_entsize; | |
2168 | } | |
2169 | ||
2170 | /* Bump the counter, so that we know where to add the next set of | |
2171 | relocations. */ | |
2172 | *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); | |
2173 | ||
2174 | return TRUE; | |
2175 | } | |
2176 | \f | |
2177 | /* Fix up the flags for a symbol. This handles various cases which | |
2178 | can only be fixed after all the input files are seen. This is | |
2179 | currently called by both adjust_dynamic_symbol and | |
2180 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2181 | the face of future changes. */ | |
2182 | ||
2183 | bfd_boolean | |
2184 | _bfd_elf_fix_symbol_flags (h, eif) | |
2185 | struct elf_link_hash_entry *h; | |
2186 | struct elf_info_failed *eif; | |
2187 | { | |
2188 | /* If this symbol was mentioned in a non-ELF file, try to set | |
2189 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2190 | permit a non-ELF file to correctly refer to a symbol defined in | |
2191 | an ELF dynamic object. */ | |
2192 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) | |
2193 | { | |
2194 | while (h->root.type == bfd_link_hash_indirect) | |
2195 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2196 | ||
2197 | if (h->root.type != bfd_link_hash_defined | |
2198 | && h->root.type != bfd_link_hash_defweak) | |
2199 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
2200 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
2201 | else | |
2202 | { | |
2203 | if (h->root.u.def.section->owner != NULL | |
2204 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2205 | == bfd_target_elf_flavour)) | |
2206 | h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR | |
2207 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK); | |
2208 | else | |
2209 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2210 | } | |
2211 | ||
2212 | if (h->dynindx == -1 | |
2213 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2214 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
2215 | { | |
2216 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2217 | { | |
2218 | eif->failed = TRUE; | |
2219 | return FALSE; | |
2220 | } | |
2221 | } | |
2222 | } | |
2223 | else | |
2224 | { | |
2225 | /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol | |
2226 | was first seen in a non-ELF file. Fortunately, if the symbol | |
2227 | was first seen in an ELF file, we're probably OK unless the | |
2228 | symbol was defined in a non-ELF file. Catch that case here. | |
2229 | FIXME: We're still in trouble if the symbol was first seen in | |
2230 | a dynamic object, and then later in a non-ELF regular object. */ | |
2231 | if ((h->root.type == bfd_link_hash_defined | |
2232 | || h->root.type == bfd_link_hash_defweak) | |
2233 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2234 | && (h->root.u.def.section->owner != NULL | |
2235 | ? (bfd_get_flavour (h->root.u.def.section->owner) | |
2236 | != bfd_target_elf_flavour) | |
2237 | : (bfd_is_abs_section (h->root.u.def.section) | |
2238 | && (h->elf_link_hash_flags | |
2239 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0))) | |
2240 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2241 | } | |
2242 | ||
2243 | /* If this is a final link, and the symbol was defined as a common | |
2244 | symbol in a regular object file, and there was no definition in | |
2245 | any dynamic object, then the linker will have allocated space for | |
2246 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR | |
2247 | flag will not have been set. */ | |
2248 | if (h->root.type == bfd_link_hash_defined | |
2249 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2250 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
2251 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2252 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
2253 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2254 | ||
2255 | /* If -Bsymbolic was used (which means to bind references to global | |
2256 | symbols to the definition within the shared object), and this | |
2257 | symbol was defined in a regular object, then it actually doesn't | |
9c7a29a3 AM |
2258 | need a PLT entry. Likewise, if the symbol has non-default |
2259 | visibility. If the symbol has hidden or internal visibility, we | |
c1be741f | 2260 | will force it local. */ |
45d6a902 AM |
2261 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 |
2262 | && eif->info->shared | |
2263 | && is_elf_hash_table (eif->info) | |
2264 | && (eif->info->symbolic | |
c1be741f | 2265 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
45d6a902 AM |
2266 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
2267 | { | |
2268 | struct elf_backend_data *bed; | |
2269 | bfd_boolean force_local; | |
2270 | ||
2271 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2272 | ||
2273 | force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL | |
2274 | || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); | |
2275 | (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); | |
2276 | } | |
2277 | ||
2278 | /* If a weak undefined symbol has non-default visibility, we also | |
2279 | hide it from the dynamic linker. */ | |
9c7a29a3 | 2280 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT |
45d6a902 AM |
2281 | && h->root.type == bfd_link_hash_undefweak) |
2282 | { | |
2283 | struct elf_backend_data *bed; | |
2284 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2285 | (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); | |
2286 | } | |
2287 | ||
2288 | /* If this is a weak defined symbol in a dynamic object, and we know | |
2289 | the real definition in the dynamic object, copy interesting flags | |
2290 | over to the real definition. */ | |
2291 | if (h->weakdef != NULL) | |
2292 | { | |
2293 | struct elf_link_hash_entry *weakdef; | |
2294 | ||
2295 | weakdef = h->weakdef; | |
2296 | if (h->root.type == bfd_link_hash_indirect) | |
2297 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2298 | ||
2299 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2300 | || h->root.type == bfd_link_hash_defweak); | |
2301 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2302 | || weakdef->root.type == bfd_link_hash_defweak); | |
2303 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
2304 | ||
2305 | /* If the real definition is defined by a regular object file, | |
2306 | don't do anything special. See the longer description in | |
2307 | _bfd_elf_adjust_dynamic_symbol, below. */ | |
2308 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2309 | h->weakdef = NULL; | |
2310 | else | |
2311 | { | |
2312 | struct elf_backend_data *bed; | |
2313 | ||
2314 | bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); | |
2315 | (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h); | |
2316 | } | |
2317 | } | |
2318 | ||
2319 | return TRUE; | |
2320 | } | |
2321 | ||
2322 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2323 | called via elf_link_hash_traverse, and also calls itself | |
2324 | recursively. */ | |
2325 | ||
2326 | bfd_boolean | |
2327 | _bfd_elf_adjust_dynamic_symbol (h, data) | |
2328 | struct elf_link_hash_entry *h; | |
2329 | PTR data; | |
2330 | { | |
2331 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
2332 | bfd *dynobj; | |
2333 | struct elf_backend_data *bed; | |
2334 | ||
2335 | if (! is_elf_hash_table (eif->info)) | |
2336 | return FALSE; | |
2337 | ||
2338 | if (h->root.type == bfd_link_hash_warning) | |
2339 | { | |
2340 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2341 | h->got = elf_hash_table (eif->info)->init_offset; | |
2342 | ||
2343 | /* When warning symbols are created, they **replace** the "real" | |
2344 | entry in the hash table, thus we never get to see the real | |
2345 | symbol in a hash traversal. So look at it now. */ | |
2346 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2347 | } | |
2348 | ||
2349 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2350 | if (h->root.type == bfd_link_hash_indirect) | |
2351 | return TRUE; | |
2352 | ||
2353 | /* Fix the symbol flags. */ | |
2354 | if (! _bfd_elf_fix_symbol_flags (h, eif)) | |
2355 | return FALSE; | |
2356 | ||
2357 | /* If this symbol does not require a PLT entry, and it is not | |
2358 | defined by a dynamic object, or is not referenced by a regular | |
2359 | object, ignore it. We do have to handle a weak defined symbol, | |
2360 | even if no regular object refers to it, if we decided to add it | |
2361 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2362 | about symbols which are defined by one dynamic object and | |
2363 | referenced by another one? */ | |
2364 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 | |
2365 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
2366 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2367 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 | |
2368 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) | |
2369 | { | |
2370 | h->plt = elf_hash_table (eif->info)->init_offset; | |
2371 | return TRUE; | |
2372 | } | |
2373 | ||
2374 | /* If we've already adjusted this symbol, don't do it again. This | |
2375 | can happen via a recursive call. */ | |
2376 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
2377 | return TRUE; | |
2378 | ||
2379 | /* Don't look at this symbol again. Note that we must set this | |
2380 | after checking the above conditions, because we may look at a | |
2381 | symbol once, decide not to do anything, and then get called | |
2382 | recursively later after REF_REGULAR is set below. */ | |
2383 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
2384 | ||
2385 | /* If this is a weak definition, and we know a real definition, and | |
2386 | the real symbol is not itself defined by a regular object file, | |
2387 | then get a good value for the real definition. We handle the | |
2388 | real symbol first, for the convenience of the backend routine. | |
2389 | ||
2390 | Note that there is a confusing case here. If the real definition | |
2391 | is defined by a regular object file, we don't get the real symbol | |
2392 | from the dynamic object, but we do get the weak symbol. If the | |
2393 | processor backend uses a COPY reloc, then if some routine in the | |
2394 | dynamic object changes the real symbol, we will not see that | |
2395 | change in the corresponding weak symbol. This is the way other | |
2396 | ELF linkers work as well, and seems to be a result of the shared | |
2397 | library model. | |
2398 | ||
2399 | I will clarify this issue. Most SVR4 shared libraries define the | |
2400 | variable _timezone and define timezone as a weak synonym. The | |
2401 | tzset call changes _timezone. If you write | |
2402 | extern int timezone; | |
2403 | int _timezone = 5; | |
2404 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2405 | you might expect that, since timezone is a synonym for _timezone, | |
2406 | the same number will print both times. However, if the processor | |
2407 | backend uses a COPY reloc, then actually timezone will be copied | |
2408 | into your process image, and, since you define _timezone | |
2409 | yourself, _timezone will not. Thus timezone and _timezone will | |
2410 | wind up at different memory locations. The tzset call will set | |
2411 | _timezone, leaving timezone unchanged. */ | |
2412 | ||
2413 | if (h->weakdef != NULL) | |
2414 | { | |
2415 | /* If we get to this point, we know there is an implicit | |
2416 | reference by a regular object file via the weak symbol H. | |
2417 | FIXME: Is this really true? What if the traversal finds | |
2418 | H->WEAKDEF before it finds H? */ | |
2419 | h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2420 | ||
2421 | if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif)) | |
2422 | return FALSE; | |
2423 | } | |
2424 | ||
2425 | /* If a symbol has no type and no size and does not require a PLT | |
2426 | entry, then we are probably about to do the wrong thing here: we | |
2427 | are probably going to create a COPY reloc for an empty object. | |
2428 | This case can arise when a shared object is built with assembly | |
2429 | code, and the assembly code fails to set the symbol type. */ | |
2430 | if (h->size == 0 | |
2431 | && h->type == STT_NOTYPE | |
2432 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
2433 | (*_bfd_error_handler) | |
2434 | (_("warning: type and size of dynamic symbol `%s' are not defined"), | |
2435 | h->root.root.string); | |
2436 | ||
2437 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2438 | bed = get_elf_backend_data (dynobj); | |
2439 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2440 | { | |
2441 | eif->failed = TRUE; | |
2442 | return FALSE; | |
2443 | } | |
2444 | ||
2445 | return TRUE; | |
2446 | } | |
2447 | ||
2448 | /* Adjust all external symbols pointing into SEC_MERGE sections | |
2449 | to reflect the object merging within the sections. */ | |
2450 | ||
2451 | bfd_boolean | |
2452 | _bfd_elf_link_sec_merge_syms (h, data) | |
2453 | struct elf_link_hash_entry *h; | |
2454 | PTR data; | |
2455 | { | |
2456 | asection *sec; | |
2457 | ||
2458 | if (h->root.type == bfd_link_hash_warning) | |
2459 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2460 | ||
2461 | if ((h->root.type == bfd_link_hash_defined | |
2462 | || h->root.type == bfd_link_hash_defweak) | |
2463 | && ((sec = h->root.u.def.section)->flags & SEC_MERGE) | |
2464 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) | |
2465 | { | |
2466 | bfd *output_bfd = (bfd *) data; | |
2467 | ||
2468 | h->root.u.def.value = | |
2469 | _bfd_merged_section_offset (output_bfd, | |
2470 | &h->root.u.def.section, | |
2471 | elf_section_data (sec)->sec_info, | |
2472 | h->root.u.def.value, (bfd_vma) 0); | |
2473 | } | |
2474 | ||
2475 | return TRUE; | |
2476 | } |