Commit | Line | Data |
---|---|---|
8afe83be | 1 | /* ELF linker support. |
56f3b62c | 2 | Copyright 1995, 1996, 1997 Free Software Foundation, Inc. |
8afe83be KR |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
943fbd5b | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
452a5efb | 19 | |
ede4eed4 KR |
20 | /* ELF linker code. */ |
21 | ||
22 | static boolean elf_link_add_object_symbols | |
23 | PARAMS ((bfd *, struct bfd_link_info *)); | |
24 | static boolean elf_link_add_archive_symbols | |
25 | PARAMS ((bfd *, struct bfd_link_info *)); | |
ede4eed4 KR |
26 | static boolean elf_export_symbol |
27 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
28 | static boolean elf_adjust_dynamic_symbol | |
29 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
d044b40a ILT |
30 | static boolean elf_link_find_version_dependencies |
31 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
32 | static boolean elf_link_find_version_dependencies | |
33 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
34 | static boolean elf_link_assign_sym_version | |
35 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
36 | static boolean elf_link_renumber_dynsyms | |
37 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
ede4eed4 KR |
38 | |
39 | /* This struct is used to pass information to routines called via | |
40 | elf_link_hash_traverse which must return failure. */ | |
41 | ||
42 | struct elf_info_failed | |
43 | { | |
44 | boolean failed; | |
45 | struct bfd_link_info *info; | |
ff12f303 | 46 | }; |
ede4eed4 KR |
47 | |
48 | /* Given an ELF BFD, add symbols to the global hash table as | |
49 | appropriate. */ | |
50 | ||
51 | boolean | |
52 | elf_bfd_link_add_symbols (abfd, info) | |
53 | bfd *abfd; | |
54 | struct bfd_link_info *info; | |
55 | { | |
ede4eed4 KR |
56 | switch (bfd_get_format (abfd)) |
57 | { | |
58 | case bfd_object: | |
59 | return elf_link_add_object_symbols (abfd, info); | |
60 | case bfd_archive: | |
ede4eed4 KR |
61 | return elf_link_add_archive_symbols (abfd, info); |
62 | default: | |
63 | bfd_set_error (bfd_error_wrong_format); | |
64 | return false; | |
65 | } | |
66 | } | |
3b3753b8 | 67 | \f |
ede4eed4 KR |
68 | |
69 | /* Add symbols from an ELF archive file to the linker hash table. We | |
70 | don't use _bfd_generic_link_add_archive_symbols because of a | |
71 | problem which arises on UnixWare. The UnixWare libc.so is an | |
72 | archive which includes an entry libc.so.1 which defines a bunch of | |
73 | symbols. The libc.so archive also includes a number of other | |
74 | object files, which also define symbols, some of which are the same | |
75 | as those defined in libc.so.1. Correct linking requires that we | |
76 | consider each object file in turn, and include it if it defines any | |
77 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
78 | this; it looks through the list of undefined symbols, and includes | |
79 | any object file which defines them. When this algorithm is used on | |
80 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
81 | bunch of symbols. This means that some of the other objects in the | |
82 | archive are not included in the link, which is incorrect since they | |
83 | precede libc.so.1 in the archive. | |
84 | ||
85 | Fortunately, ELF archive handling is simpler than that done by | |
86 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
87 | oddities. In ELF, if we find a symbol in the archive map, and the | |
88 | symbol is currently undefined, we know that we must pull in that | |
89 | object file. | |
90 | ||
91 | Unfortunately, we do have to make multiple passes over the symbol | |
92 | table until nothing further is resolved. */ | |
93 | ||
94 | static boolean | |
95 | elf_link_add_archive_symbols (abfd, info) | |
96 | bfd *abfd; | |
97 | struct bfd_link_info *info; | |
98 | { | |
99 | symindex c; | |
100 | boolean *defined = NULL; | |
101 | boolean *included = NULL; | |
102 | carsym *symdefs; | |
103 | boolean loop; | |
104 | ||
105 | if (! bfd_has_map (abfd)) | |
106 | { | |
107 | /* An empty archive is a special case. */ | |
108 | if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL) | |
109 | return true; | |
110 | bfd_set_error (bfd_error_no_armap); | |
111 | return false; | |
112 | } | |
113 | ||
114 | /* Keep track of all symbols we know to be already defined, and all | |
115 | files we know to be already included. This is to speed up the | |
116 | second and subsequent passes. */ | |
117 | c = bfd_ardata (abfd)->symdef_count; | |
118 | if (c == 0) | |
119 | return true; | |
58142f10 ILT |
120 | defined = (boolean *) bfd_malloc (c * sizeof (boolean)); |
121 | included = (boolean *) bfd_malloc (c * sizeof (boolean)); | |
ede4eed4 | 122 | if (defined == (boolean *) NULL || included == (boolean *) NULL) |
58142f10 | 123 | goto error_return; |
ede4eed4 KR |
124 | memset (defined, 0, c * sizeof (boolean)); |
125 | memset (included, 0, c * sizeof (boolean)); | |
126 | ||
127 | symdefs = bfd_ardata (abfd)->symdefs; | |
128 | ||
129 | do | |
130 | { | |
131 | file_ptr last; | |
132 | symindex i; | |
133 | carsym *symdef; | |
134 | carsym *symdefend; | |
135 | ||
136 | loop = false; | |
137 | last = -1; | |
138 | ||
139 | symdef = symdefs; | |
140 | symdefend = symdef + c; | |
141 | for (i = 0; symdef < symdefend; symdef++, i++) | |
142 | { | |
143 | struct elf_link_hash_entry *h; | |
144 | bfd *element; | |
145 | struct bfd_link_hash_entry *undefs_tail; | |
146 | symindex mark; | |
147 | ||
148 | if (defined[i] || included[i]) | |
149 | continue; | |
150 | if (symdef->file_offset == last) | |
151 | { | |
152 | included[i] = true; | |
153 | continue; | |
154 | } | |
155 | ||
156 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, | |
157 | false, false, false); | |
d044b40a ILT |
158 | |
159 | if (h == NULL) | |
160 | { | |
161 | char *p, *copy; | |
162 | ||
163 | /* If this is a default version (the name contains @@), | |
164 | look up the symbol again without the version. The | |
165 | effect is that references to the symbol without the | |
166 | version will be matched by the default symbol in the | |
167 | archive. */ | |
168 | ||
169 | p = strchr (symdef->name, ELF_VER_CHR); | |
170 | if (p == NULL || p[1] != ELF_VER_CHR) | |
171 | continue; | |
172 | ||
173 | copy = bfd_alloc (abfd, p - symdef->name + 1); | |
174 | if (copy == NULL) | |
175 | goto error_return; | |
176 | memcpy (copy, symdef->name, p - symdef->name); | |
177 | copy[p - symdef->name] = '\0'; | |
178 | ||
179 | h = elf_link_hash_lookup (elf_hash_table (info), copy, | |
180 | false, false, false); | |
181 | ||
182 | bfd_release (abfd, copy); | |
183 | } | |
184 | ||
185 | if (h == NULL) | |
ede4eed4 | 186 | continue; |
d044b40a | 187 | |
ede4eed4 KR |
188 | if (h->root.type != bfd_link_hash_undefined) |
189 | { | |
68807a39 ILT |
190 | if (h->root.type != bfd_link_hash_undefweak) |
191 | defined[i] = true; | |
ede4eed4 KR |
192 | continue; |
193 | } | |
194 | ||
195 | /* We need to include this archive member. */ | |
196 | ||
197 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
198 | if (element == (bfd *) NULL) | |
199 | goto error_return; | |
200 | ||
201 | if (! bfd_check_format (element, bfd_object)) | |
202 | goto error_return; | |
203 | ||
204 | /* Doublecheck that we have not included this object | |
205 | already--it should be impossible, but there may be | |
206 | something wrong with the archive. */ | |
207 | if (element->archive_pass != 0) | |
208 | { | |
209 | bfd_set_error (bfd_error_bad_value); | |
210 | goto error_return; | |
211 | } | |
212 | element->archive_pass = 1; | |
213 | ||
214 | undefs_tail = info->hash->undefs_tail; | |
215 | ||
216 | if (! (*info->callbacks->add_archive_element) (info, element, | |
217 | symdef->name)) | |
218 | goto error_return; | |
219 | if (! elf_link_add_object_symbols (element, info)) | |
220 | goto error_return; | |
221 | ||
222 | /* If there are any new undefined symbols, we need to make | |
223 | another pass through the archive in order to see whether | |
224 | they can be defined. FIXME: This isn't perfect, because | |
225 | common symbols wind up on undefs_tail and because an | |
226 | undefined symbol which is defined later on in this pass | |
227 | does not require another pass. This isn't a bug, but it | |
228 | does make the code less efficient than it could be. */ | |
229 | if (undefs_tail != info->hash->undefs_tail) | |
230 | loop = true; | |
231 | ||
232 | /* Look backward to mark all symbols from this object file | |
233 | which we have already seen in this pass. */ | |
234 | mark = i; | |
235 | do | |
236 | { | |
237 | included[mark] = true; | |
238 | if (mark == 0) | |
239 | break; | |
240 | --mark; | |
241 | } | |
242 | while (symdefs[mark].file_offset == symdef->file_offset); | |
243 | ||
244 | /* We mark subsequent symbols from this object file as we go | |
245 | on through the loop. */ | |
246 | last = symdef->file_offset; | |
247 | } | |
248 | } | |
249 | while (loop); | |
250 | ||
251 | free (defined); | |
252 | free (included); | |
253 | ||
254 | return true; | |
255 | ||
256 | error_return: | |
257 | if (defined != (boolean *) NULL) | |
258 | free (defined); | |
259 | if (included != (boolean *) NULL) | |
260 | free (included); | |
261 | return false; | |
262 | } | |
263 | ||
264 | /* Add symbols from an ELF object file to the linker hash table. */ | |
265 | ||
266 | static boolean | |
267 | elf_link_add_object_symbols (abfd, info) | |
268 | bfd *abfd; | |
269 | struct bfd_link_info *info; | |
270 | { | |
271 | boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *, | |
272 | const Elf_Internal_Sym *, | |
273 | const char **, flagword *, | |
274 | asection **, bfd_vma *)); | |
275 | boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *, | |
276 | asection *, const Elf_Internal_Rela *)); | |
277 | boolean collect; | |
278 | Elf_Internal_Shdr *hdr; | |
279 | size_t symcount; | |
280 | size_t extsymcount; | |
281 | size_t extsymoff; | |
282 | Elf_External_Sym *buf = NULL; | |
283 | struct elf_link_hash_entry **sym_hash; | |
284 | boolean dynamic; | |
d044b40a ILT |
285 | bfd_byte *dynver = NULL; |
286 | Elf_External_Versym *extversym = NULL; | |
287 | Elf_External_Versym *ever; | |
ede4eed4 KR |
288 | Elf_External_Dyn *dynbuf = NULL; |
289 | struct elf_link_hash_entry *weaks; | |
290 | Elf_External_Sym *esym; | |
291 | Elf_External_Sym *esymend; | |
292 | ||
293 | add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook; | |
294 | collect = get_elf_backend_data (abfd)->collect; | |
295 | ||
d044b40a ILT |
296 | if ((abfd->flags & DYNAMIC) == 0) |
297 | dynamic = false; | |
298 | else | |
299 | { | |
300 | dynamic = true; | |
301 | ||
302 | /* You can't use -r against a dynamic object. Also, there's no | |
303 | hope of using a dynamic object which does not exactly match | |
304 | the format of the output file. */ | |
305 | if (info->relocateable || info->hash->creator != abfd->xvec) | |
306 | { | |
307 | bfd_set_error (bfd_error_invalid_operation); | |
308 | goto error_return; | |
309 | } | |
310 | } | |
311 | ||
0cb70568 ILT |
312 | /* As a GNU extension, any input sections which are named |
313 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
314 | symbol. This differs from .gnu.warning sections, which generate | |
315 | warnings when they are included in an output file. */ | |
316 | if (! info->shared) | |
317 | { | |
318 | asection *s; | |
319 | ||
320 | for (s = abfd->sections; s != NULL; s = s->next) | |
321 | { | |
322 | const char *name; | |
323 | ||
324 | name = bfd_get_section_name (abfd, s); | |
325 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
326 | { | |
327 | char *msg; | |
328 | bfd_size_type sz; | |
329 | ||
54626f1a ILT |
330 | name += sizeof ".gnu.warning." - 1; |
331 | ||
332 | /* If this is a shared object, then look up the symbol | |
333 | in the hash table. If it is there, and it is already | |
334 | been defined, then we will not be using the entry | |
335 | from this shared object, so we don't need to warn. | |
336 | FIXME: If we see the definition in a regular object | |
337 | later on, we will warn, but we shouldn't. The only | |
338 | fix is to keep track of what warnings we are supposed | |
339 | to emit, and then handle them all at the end of the | |
340 | link. */ | |
d044b40a | 341 | if (dynamic && abfd->xvec == info->hash->creator) |
54626f1a ILT |
342 | { |
343 | struct elf_link_hash_entry *h; | |
344 | ||
345 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
346 | false, false, true); | |
347 | ||
348 | /* FIXME: What about bfd_link_hash_common? */ | |
349 | if (h != NULL | |
350 | && (h->root.type == bfd_link_hash_defined | |
351 | || h->root.type == bfd_link_hash_defweak)) | |
352 | { | |
353 | /* We don't want to issue this warning. Clobber | |
354 | the section size so that the warning does not | |
355 | get copied into the output file. */ | |
356 | s->_raw_size = 0; | |
357 | continue; | |
358 | } | |
359 | } | |
360 | ||
0cb70568 ILT |
361 | sz = bfd_section_size (abfd, s); |
362 | msg = (char *) bfd_alloc (abfd, sz); | |
363 | if (msg == NULL) | |
a9713b91 | 364 | goto error_return; |
0cb70568 ILT |
365 | |
366 | if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz)) | |
367 | goto error_return; | |
368 | ||
369 | if (! (_bfd_generic_link_add_one_symbol | |
54626f1a ILT |
370 | (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg, |
371 | false, collect, (struct bfd_link_hash_entry **) NULL))) | |
0cb70568 ILT |
372 | goto error_return; |
373 | ||
374 | if (! info->relocateable) | |
375 | { | |
376 | /* Clobber the section size so that the warning does | |
377 | not get copied into the output file. */ | |
378 | s->_raw_size = 0; | |
379 | } | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
d044b40a ILT |
384 | /* If this is a dynamic object, we always link against the .dynsym |
385 | symbol table, not the .symtab symbol table. The dynamic linker | |
386 | will only see the .dynsym symbol table, so there is no reason to | |
387 | look at .symtab for a dynamic object. */ | |
388 | ||
389 | if (! dynamic || elf_dynsymtab (abfd) == 0) | |
390 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
391 | else | |
392 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
393 | ||
394 | if (dynamic) | |
ede4eed4 | 395 | { |
d044b40a ILT |
396 | /* Read in any version definitions. */ |
397 | ||
398 | if (elf_dynverdef (abfd) != 0) | |
399 | { | |
400 | Elf_Internal_Shdr *verdefhdr; | |
401 | bfd_byte *dynver; | |
402 | int i; | |
403 | const Elf_External_Verdef *extverdef; | |
404 | Elf_Internal_Verdef *intverdef; | |
405 | ||
406 | verdefhdr = &elf_tdata (abfd)->dynverdef_hdr; | |
407 | elf_tdata (abfd)->verdef = | |
408 | ((Elf_Internal_Verdef *) | |
409 | bfd_zalloc (abfd, | |
410 | verdefhdr->sh_info * sizeof (Elf_Internal_Verdef))); | |
411 | if (elf_tdata (abfd)->verdef == NULL) | |
412 | goto error_return; | |
413 | ||
414 | dynver = (bfd_byte *) bfd_malloc (verdefhdr->sh_size); | |
415 | if (dynver == NULL) | |
416 | goto error_return; | |
417 | ||
418 | if (bfd_seek (abfd, verdefhdr->sh_offset, SEEK_SET) != 0 | |
d2f0374f UD |
419 | || (bfd_read ((PTR) dynver, 1, verdefhdr->sh_size, abfd) |
420 | != verdefhdr->sh_size)) | |
d044b40a ILT |
421 | goto error_return; |
422 | ||
423 | extverdef = (const Elf_External_Verdef *) dynver; | |
424 | intverdef = elf_tdata (abfd)->verdef; | |
425 | for (i = 0; i < verdefhdr->sh_info; i++, intverdef++) | |
426 | { | |
427 | const Elf_External_Verdaux *extverdaux; | |
428 | Elf_Internal_Verdaux intverdaux; | |
429 | ||
430 | _bfd_elf_swap_verdef_in (abfd, extverdef, intverdef); | |
431 | ||
432 | /* Pick up the name of the version. */ | |
433 | extverdaux = ((const Elf_External_Verdaux *) | |
d2f0374f | 434 | ((bfd_byte *) extverdef + intverdef->vd_aux)); |
d044b40a ILT |
435 | _bfd_elf_swap_verdaux_in (abfd, extverdaux, &intverdaux); |
436 | ||
437 | intverdef->vd_bfd = abfd; | |
438 | intverdef->vd_nodename = | |
439 | bfd_elf_string_from_elf_section (abfd, verdefhdr->sh_link, | |
440 | intverdaux.vda_name); | |
441 | ||
442 | extverdef = ((const Elf_External_Verdef *) | |
d2f0374f | 443 | ((bfd_byte *) extverdef + intverdef->vd_next)); |
d044b40a ILT |
444 | } |
445 | ||
446 | free (dynver); | |
447 | dynver = NULL; | |
448 | } | |
449 | ||
450 | /* Read in the symbol versions, but don't bother to convert them | |
451 | to internal format. */ | |
452 | if (elf_dynversym (abfd) != 0) | |
453 | { | |
454 | Elf_Internal_Shdr *versymhdr; | |
455 | ||
456 | versymhdr = &elf_tdata (abfd)->dynversym_hdr; | |
457 | extversym = (Elf_External_Versym *) bfd_malloc (hdr->sh_size); | |
458 | if (extversym == NULL) | |
459 | goto error_return; | |
460 | if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 | |
461 | || (bfd_read ((PTR) extversym, 1, versymhdr->sh_size, abfd) | |
462 | != versymhdr->sh_size)) | |
463 | goto error_return; | |
464 | } | |
ede4eed4 KR |
465 | } |
466 | ||
ede4eed4 KR |
467 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
468 | ||
469 | /* The sh_info field of the symtab header tells us where the | |
470 | external symbols start. We don't care about the local symbols at | |
471 | this point. */ | |
472 | if (elf_bad_symtab (abfd)) | |
473 | { | |
474 | extsymcount = symcount; | |
475 | extsymoff = 0; | |
476 | } | |
477 | else | |
478 | { | |
479 | extsymcount = symcount - hdr->sh_info; | |
480 | extsymoff = hdr->sh_info; | |
481 | } | |
482 | ||
58142f10 ILT |
483 | buf = ((Elf_External_Sym *) |
484 | bfd_malloc (extsymcount * sizeof (Elf_External_Sym))); | |
ede4eed4 | 485 | if (buf == NULL && extsymcount != 0) |
58142f10 | 486 | goto error_return; |
ede4eed4 KR |
487 | |
488 | /* We store a pointer to the hash table entry for each external | |
489 | symbol. */ | |
490 | sym_hash = ((struct elf_link_hash_entry **) | |
491 | bfd_alloc (abfd, | |
492 | extsymcount * sizeof (struct elf_link_hash_entry *))); | |
493 | if (sym_hash == NULL) | |
a9713b91 | 494 | goto error_return; |
ede4eed4 KR |
495 | elf_sym_hashes (abfd) = sym_hash; |
496 | ||
d044b40a | 497 | if (! dynamic) |
ede4eed4 | 498 | { |
ede4eed4 KR |
499 | /* If we are creating a shared library, create all the dynamic |
500 | sections immediately. We need to attach them to something, | |
501 | so we attach them to this BFD, provided it is the right | |
502 | format. FIXME: If there are no input BFD's of the same | |
503 | format as the output, we can't make a shared library. */ | |
504 | if (info->shared | |
505 | && ! elf_hash_table (info)->dynamic_sections_created | |
506 | && abfd->xvec == info->hash->creator) | |
507 | { | |
508 | if (! elf_link_create_dynamic_sections (abfd, info)) | |
509 | goto error_return; | |
510 | } | |
511 | } | |
512 | else | |
513 | { | |
514 | asection *s; | |
515 | boolean add_needed; | |
516 | const char *name; | |
517 | bfd_size_type oldsize; | |
518 | bfd_size_type strindex; | |
519 | ||
ede4eed4 KR |
520 | /* Find the name to use in a DT_NEEDED entry that refers to this |
521 | object. If the object has a DT_SONAME entry, we use it. | |
522 | Otherwise, if the generic linker stuck something in | |
60a49e7f ILT |
523 | elf_dt_name, we use that. Otherwise, we just use the file |
524 | name. If the generic linker put a null string into | |
525 | elf_dt_name, we don't make a DT_NEEDED entry at all, even if | |
526 | there is a DT_SONAME entry. */ | |
ede4eed4 KR |
527 | add_needed = true; |
528 | name = bfd_get_filename (abfd); | |
60a49e7f | 529 | if (elf_dt_name (abfd) != NULL) |
ede4eed4 | 530 | { |
60a49e7f | 531 | name = elf_dt_name (abfd); |
ede4eed4 KR |
532 | if (*name == '\0') |
533 | add_needed = false; | |
534 | } | |
535 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
536 | if (s != NULL) | |
537 | { | |
538 | Elf_External_Dyn *extdyn; | |
539 | Elf_External_Dyn *extdynend; | |
540 | int elfsec; | |
541 | unsigned long link; | |
542 | ||
58142f10 | 543 | dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size); |
ede4eed4 | 544 | if (dynbuf == NULL) |
58142f10 | 545 | goto error_return; |
ede4eed4 KR |
546 | |
547 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, | |
548 | (file_ptr) 0, s->_raw_size)) | |
549 | goto error_return; | |
550 | ||
551 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
552 | if (elfsec == -1) | |
553 | goto error_return; | |
554 | link = elf_elfsections (abfd)[elfsec]->sh_link; | |
555 | ||
556 | extdyn = dynbuf; | |
557 | extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn); | |
558 | for (; extdyn < extdynend; extdyn++) | |
559 | { | |
560 | Elf_Internal_Dyn dyn; | |
561 | ||
562 | elf_swap_dyn_in (abfd, extdyn, &dyn); | |
60a49e7f | 563 | if (dyn.d_tag == DT_SONAME) |
ede4eed4 KR |
564 | { |
565 | name = bfd_elf_string_from_elf_section (abfd, link, | |
566 | dyn.d_un.d_val); | |
567 | if (name == NULL) | |
568 | goto error_return; | |
569 | } | |
570 | if (dyn.d_tag == DT_NEEDED) | |
571 | { | |
54406786 | 572 | struct bfd_link_needed_list *n, **pn; |
ede4eed4 KR |
573 | char *fnm, *anm; |
574 | ||
54406786 ILT |
575 | n = ((struct bfd_link_needed_list *) |
576 | bfd_alloc (abfd, sizeof (struct bfd_link_needed_list))); | |
ede4eed4 KR |
577 | fnm = bfd_elf_string_from_elf_section (abfd, link, |
578 | dyn.d_un.d_val); | |
579 | if (n == NULL || fnm == NULL) | |
580 | goto error_return; | |
581 | anm = bfd_alloc (abfd, strlen (fnm) + 1); | |
582 | if (anm == NULL) | |
583 | goto error_return; | |
584 | strcpy (anm, fnm); | |
585 | n->name = anm; | |
586 | n->by = abfd; | |
587 | n->next = NULL; | |
588 | for (pn = &elf_hash_table (info)->needed; | |
589 | *pn != NULL; | |
590 | pn = &(*pn)->next) | |
591 | ; | |
592 | *pn = n; | |
593 | } | |
594 | } | |
595 | ||
596 | free (dynbuf); | |
597 | dynbuf = NULL; | |
598 | } | |
599 | ||
600 | /* We do not want to include any of the sections in a dynamic | |
601 | object in the output file. We hack by simply clobbering the | |
602 | list of sections in the BFD. This could be handled more | |
603 | cleanly by, say, a new section flag; the existing | |
604 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
605 | still implies that the section takes up space in the output | |
606 | file. */ | |
607 | abfd->sections = NULL; | |
010d9f2d | 608 | abfd->section_count = 0; |
ede4eed4 KR |
609 | |
610 | /* If this is the first dynamic object found in the link, create | |
611 | the special sections required for dynamic linking. */ | |
612 | if (! elf_hash_table (info)->dynamic_sections_created) | |
613 | { | |
614 | if (! elf_link_create_dynamic_sections (abfd, info)) | |
615 | goto error_return; | |
616 | } | |
617 | ||
618 | if (add_needed) | |
619 | { | |
620 | /* Add a DT_NEEDED entry for this dynamic object. */ | |
621 | oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
622 | strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name, | |
623 | true, false); | |
624 | if (strindex == (bfd_size_type) -1) | |
625 | goto error_return; | |
626 | ||
627 | if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr)) | |
628 | { | |
629 | asection *sdyn; | |
630 | Elf_External_Dyn *dyncon, *dynconend; | |
631 | ||
632 | /* The hash table size did not change, which means that | |
633 | the dynamic object name was already entered. If we | |
634 | have already included this dynamic object in the | |
635 | link, just ignore it. There is no reason to include | |
636 | a particular dynamic object more than once. */ | |
637 | sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, | |
638 | ".dynamic"); | |
639 | BFD_ASSERT (sdyn != NULL); | |
640 | ||
641 | dyncon = (Elf_External_Dyn *) sdyn->contents; | |
642 | dynconend = (Elf_External_Dyn *) (sdyn->contents + | |
643 | sdyn->_raw_size); | |
644 | for (; dyncon < dynconend; dyncon++) | |
645 | { | |
646 | Elf_Internal_Dyn dyn; | |
647 | ||
648 | elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, | |
649 | &dyn); | |
650 | if (dyn.d_tag == DT_NEEDED | |
651 | && dyn.d_un.d_val == strindex) | |
652 | { | |
653 | if (buf != NULL) | |
654 | free (buf); | |
d044b40a ILT |
655 | if (extversym != NULL) |
656 | free (extversym); | |
ede4eed4 KR |
657 | return true; |
658 | } | |
659 | } | |
660 | } | |
661 | ||
662 | if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex)) | |
663 | goto error_return; | |
664 | } | |
60a49e7f ILT |
665 | |
666 | /* Save the SONAME, if there is one, because sometimes the | |
667 | linker emulation code will need to know it. */ | |
668 | if (*name == '\0') | |
669 | name = bfd_get_filename (abfd); | |
670 | elf_dt_name (abfd) = name; | |
ede4eed4 KR |
671 | } |
672 | ||
673 | if (bfd_seek (abfd, | |
674 | hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym), | |
675 | SEEK_SET) != 0 | |
676 | || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd) | |
677 | != extsymcount * sizeof (Elf_External_Sym))) | |
678 | goto error_return; | |
679 | ||
680 | weaks = NULL; | |
681 | ||
e549b1d2 | 682 | ever = extversym != NULL ? extversym + extsymoff : NULL; |
ede4eed4 | 683 | esymend = buf + extsymcount; |
d044b40a ILT |
684 | for (esym = buf; |
685 | esym < esymend; | |
686 | esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) | |
ede4eed4 KR |
687 | { |
688 | Elf_Internal_Sym sym; | |
689 | int bind; | |
690 | bfd_vma value; | |
691 | asection *sec; | |
692 | flagword flags; | |
693 | const char *name; | |
0cb70568 | 694 | struct elf_link_hash_entry *h; |
ede4eed4 | 695 | boolean definition; |
ee9f09cd | 696 | boolean size_change_ok, type_change_ok; |
452a5efb | 697 | boolean new_weakdef; |
ede4eed4 KR |
698 | |
699 | elf_swap_symbol_in (abfd, esym, &sym); | |
700 | ||
701 | flags = BSF_NO_FLAGS; | |
702 | sec = NULL; | |
703 | value = sym.st_value; | |
704 | *sym_hash = NULL; | |
705 | ||
706 | bind = ELF_ST_BIND (sym.st_info); | |
707 | if (bind == STB_LOCAL) | |
708 | { | |
709 | /* This should be impossible, since ELF requires that all | |
710 | global symbols follow all local symbols, and that sh_info | |
711 | point to the first global symbol. Unfortunatealy, Irix 5 | |
712 | screws this up. */ | |
713 | continue; | |
714 | } | |
715 | else if (bind == STB_GLOBAL) | |
716 | { | |
717 | if (sym.st_shndx != SHN_UNDEF | |
718 | && sym.st_shndx != SHN_COMMON) | |
719 | flags = BSF_GLOBAL; | |
720 | else | |
721 | flags = 0; | |
722 | } | |
723 | else if (bind == STB_WEAK) | |
724 | flags = BSF_WEAK; | |
725 | else | |
726 | { | |
727 | /* Leave it up to the processor backend. */ | |
728 | } | |
729 | ||
730 | if (sym.st_shndx == SHN_UNDEF) | |
731 | sec = bfd_und_section_ptr; | |
732 | else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE) | |
733 | { | |
734 | sec = section_from_elf_index (abfd, sym.st_shndx); | |
735 | if (sec != NULL) | |
736 | value -= sec->vma; | |
737 | else | |
738 | sec = bfd_abs_section_ptr; | |
739 | } | |
740 | else if (sym.st_shndx == SHN_ABS) | |
741 | sec = bfd_abs_section_ptr; | |
742 | else if (sym.st_shndx == SHN_COMMON) | |
743 | { | |
744 | sec = bfd_com_section_ptr; | |
745 | /* What ELF calls the size we call the value. What ELF | |
746 | calls the value we call the alignment. */ | |
747 | value = sym.st_size; | |
748 | } | |
749 | else | |
750 | { | |
751 | /* Leave it up to the processor backend. */ | |
752 | } | |
753 | ||
754 | name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name); | |
755 | if (name == (const char *) NULL) | |
756 | goto error_return; | |
757 | ||
758 | if (add_symbol_hook) | |
759 | { | |
760 | if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec, | |
761 | &value)) | |
762 | goto error_return; | |
763 | ||
764 | /* The hook function sets the name to NULL if this symbol | |
765 | should be skipped for some reason. */ | |
766 | if (name == (const char *) NULL) | |
767 | continue; | |
768 | } | |
769 | ||
770 | /* Sanity check that all possibilities were handled. */ | |
771 | if (sec == (asection *) NULL) | |
772 | { | |
773 | bfd_set_error (bfd_error_bad_value); | |
774 | goto error_return; | |
775 | } | |
776 | ||
777 | if (bfd_is_und_section (sec) | |
778 | || bfd_is_com_section (sec)) | |
779 | definition = false; | |
780 | else | |
781 | definition = true; | |
782 | ||
ee9f09cd | 783 | size_change_ok = false; |
5b3b9ff6 | 784 | type_change_ok = get_elf_backend_data (abfd)->type_change_ok; |
ede4eed4 KR |
785 | if (info->hash->creator->flavour == bfd_target_elf_flavour) |
786 | { | |
d044b40a ILT |
787 | Elf_Internal_Versym iver; |
788 | int vernum; | |
789 | boolean override; | |
790 | ||
791 | if (ever != NULL) | |
792 | { | |
793 | _bfd_elf_swap_versym_in (abfd, ever, &iver); | |
794 | vernum = iver.vs_vers & VERSYM_VERSION; | |
795 | ||
796 | /* If this is a hidden symbol, or if it is not version | |
797 | 1, we append the version name to the symbol name. | |
798 | However, we do not modify a non-hidden absolute | |
799 | symbol, because it might be the version symbol | |
800 | itself. FIXME: What if it isn't? */ | |
801 | if ((iver.vs_vers & VERSYM_HIDDEN) != 0 | |
802 | || (vernum > 1 && ! bfd_is_abs_section (sec))) | |
803 | { | |
804 | const char *verstr; | |
805 | int namelen, newlen; | |
806 | char *newname, *p; | |
807 | ||
e549b1d2 ILT |
808 | if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info) |
809 | { | |
810 | (*_bfd_error_handler) | |
811 | ("%s: %s: invalid version %d (max %d)", | |
812 | abfd->filename, name, vernum, | |
813 | elf_tdata (abfd)->dynverdef_hdr.sh_info); | |
814 | bfd_set_error (bfd_error_bad_value); | |
815 | goto error_return; | |
816 | } | |
817 | else if (vernum > 1) | |
d044b40a ILT |
818 | verstr = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; |
819 | else | |
820 | verstr = ""; | |
821 | ||
822 | namelen = strlen (name); | |
823 | newlen = namelen + strlen (verstr) + 2; | |
824 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
825 | ++newlen; | |
826 | ||
827 | newname = (char *) bfd_alloc (abfd, newlen); | |
828 | if (newname == NULL) | |
829 | goto error_return; | |
830 | strcpy (newname, name); | |
831 | p = newname + namelen; | |
832 | *p++ = ELF_VER_CHR; | |
833 | if ((iver.vs_vers & VERSYM_HIDDEN) == 0) | |
834 | *p++ = ELF_VER_CHR; | |
835 | strcpy (p, verstr); | |
836 | ||
837 | name = newname; | |
838 | } | |
839 | } | |
840 | ||
ede4eed4 KR |
841 | /* We need to look up the symbol now in order to get some of |
842 | the dynamic object handling right. We pass the hash | |
843 | table entry in to _bfd_generic_link_add_one_symbol so | |
844 | that it does not have to look it up again. */ | |
8881b321 ILT |
845 | if (! bfd_is_und_section (sec)) |
846 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
847 | true, false, false); | |
848 | else | |
849 | h = ((struct elf_link_hash_entry *) | |
850 | bfd_wrapped_link_hash_lookup (abfd, info, name, true, | |
851 | false, false)); | |
ede4eed4 KR |
852 | if (h == NULL) |
853 | goto error_return; | |
854 | *sym_hash = h; | |
855 | ||
869b7d80 ILT |
856 | if (h->root.type == bfd_link_hash_new) |
857 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; | |
858 | ||
0cb70568 ILT |
859 | while (h->root.type == bfd_link_hash_indirect |
860 | || h->root.type == bfd_link_hash_warning) | |
861 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
862 | ||
ee9f09cd | 863 | /* It's OK to change the type if it used to be a weak |
37897db0 ILT |
864 | definition, or if the current definition is weak (and |
865 | hence might be ignored). */ | |
5b3b9ff6 | 866 | if (h->root.type == bfd_link_hash_defweak |
37897db0 ILT |
867 | || h->root.type == bfd_link_hash_undefweak |
868 | || bind == STB_WEAK) | |
5b3b9ff6 | 869 | type_change_ok = true; |
ee9f09cd ILT |
870 | |
871 | /* It's OK to change the size if it used to be a weak | |
872 | definition, or if it used to be undefined, or if we will | |
5b3b9ff6 ILT |
873 | be overriding an old definition. */ |
874 | if (type_change_ok | |
875 | || h->root.type == bfd_link_hash_undefined) | |
876 | size_change_ok = true; | |
8235c112 | 877 | |
d044b40a ILT |
878 | override = false; |
879 | ||
ede4eed4 KR |
880 | /* If we are looking at a dynamic object, and this is a |
881 | definition, we need to see if it has already been defined | |
882 | by some other object. If it has, we want to use the | |
883 | existing definition, and we do not want to report a | |
884 | multiple symbol definition error; we do this by | |
4596421b ILT |
885 | clobbering sec to be bfd_und_section_ptr. We treat a |
886 | common symbol as a definition if the symbol in the shared | |
887 | library is a function, since common symbols always | |
888 | represent variables; this can cause confusion in | |
889 | principle, but any such confusion would seem to indicate | |
d0c45fcb | 890 | an erroneous program or shared library. */ |
ede4eed4 KR |
891 | if (dynamic && definition) |
892 | { | |
893 | if (h->root.type == bfd_link_hash_defined | |
3d7c42c9 ILT |
894 | || h->root.type == bfd_link_hash_defweak |
895 | || (h->root.type == bfd_link_hash_common | |
d0c45fcb ILT |
896 | && (bind == STB_WEAK |
897 | || ELF_ST_TYPE (sym.st_info) == STT_FUNC))) | |
3d7c42c9 | 898 | { |
d044b40a | 899 | override = true; |
3d7c42c9 ILT |
900 | sec = bfd_und_section_ptr; |
901 | definition = false; | |
ee9f09cd | 902 | size_change_ok = true; |
4596421b ILT |
903 | if (h->root.type == bfd_link_hash_common) |
904 | type_change_ok = true; | |
3d7c42c9 | 905 | } |
ede4eed4 KR |
906 | } |
907 | ||
d0c45fcb ILT |
908 | /* If we already have a common symbol, and the symbol in the |
909 | shared library is in an uninitialized section, then treat | |
910 | the shared library symbol as a common symbol. This will | |
911 | not always be correct, but it should do little harm. */ | |
912 | if (dynamic | |
913 | && definition | |
914 | && h->root.type == bfd_link_hash_common | |
915 | && (sec->flags & SEC_ALLOC) != 0 | |
916 | && (sec->flags & SEC_LOAD) == 0) | |
917 | { | |
918 | if (! ((*info->callbacks->multiple_common) | |
919 | (info, h->root.root.string, | |
920 | h->root.u.c.p->section->owner, bfd_link_hash_common, | |
921 | h->root.u.c.size, abfd, bfd_link_hash_common, | |
922 | sym.st_size))) | |
923 | goto error_return; | |
924 | ||
925 | /* If the symbol in the shared library is smaller than | |
926 | the one we already have, then override it to stick | |
927 | with the larger symbol. Set SIZE_CHANGE_OK because | |
928 | we only want to warn if requested with --warn-common. */ | |
929 | if (sym.st_size < h->size) | |
930 | { | |
931 | override = true; | |
932 | sec = bfd_und_section_ptr; | |
933 | definition = false; | |
934 | size_change_ok = true; | |
935 | } | |
936 | } | |
937 | ||
ede4eed4 KR |
938 | /* Similarly, if we are not looking at a dynamic object, and |
939 | we have a definition, we want to override any definition | |
940 | we may have from a dynamic object. Symbols from regular | |
941 | files always take precedence over symbols from dynamic | |
942 | objects, even if they are defined after the dynamic | |
943 | object in the link. */ | |
944 | if (! dynamic | |
4596421b ILT |
945 | && (definition |
946 | || (bfd_is_com_section (sec) | |
947 | && (h->root.type == bfd_link_hash_defweak | |
948 | || h->type == STT_FUNC))) | |
ede4eed4 KR |
949 | && (h->root.type == bfd_link_hash_defined |
950 | || h->root.type == bfd_link_hash_defweak) | |
951 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
d044b40a | 952 | && (h->root.u.def.section->owner->flags & DYNAMIC) != 0) |
ede4eed4 | 953 | { |
d044b40a | 954 | override = true; |
ede4eed4 KR |
955 | /* Change the hash table entry to undefined, and let |
956 | _bfd_generic_link_add_one_symbol do the right thing | |
957 | with the new definition. */ | |
958 | h->root.type = bfd_link_hash_undefined; | |
959 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
ee9f09cd | 960 | size_change_ok = true; |
4596421b ILT |
961 | if (bfd_is_com_section (sec)) |
962 | type_change_ok = true; | |
d044b40a ILT |
963 | |
964 | /* This union may have been set to be non-NULL when this | |
965 | symbol was seen in a dynamic object. We must force | |
966 | the union to be NULL, so that it is correct for a | |
967 | regular symbol. */ | |
968 | h->verinfo.vertree = NULL; | |
ede4eed4 | 969 | } |
d044b40a ILT |
970 | |
971 | if (ever != NULL | |
972 | && ! override | |
973 | && vernum > 1 | |
974 | && (h->verinfo.verdef == NULL || definition)) | |
975 | h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; | |
ede4eed4 KR |
976 | } |
977 | ||
978 | if (! (_bfd_generic_link_add_one_symbol | |
979 | (info, abfd, name, flags, sec, value, (const char *) NULL, | |
980 | false, collect, (struct bfd_link_hash_entry **) sym_hash))) | |
981 | goto error_return; | |
982 | ||
0cb70568 ILT |
983 | h = *sym_hash; |
984 | while (h->root.type == bfd_link_hash_indirect | |
985 | || h->root.type == bfd_link_hash_warning) | |
986 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
987 | *sym_hash = h; | |
988 | ||
452a5efb | 989 | new_weakdef = false; |
ede4eed4 KR |
990 | if (dynamic |
991 | && definition | |
992 | && (flags & BSF_WEAK) != 0 | |
993 | && ELF_ST_TYPE (sym.st_info) != STT_FUNC | |
994 | && info->hash->creator->flavour == bfd_target_elf_flavour | |
0cb70568 | 995 | && h->weakdef == NULL) |
ede4eed4 KR |
996 | { |
997 | /* Keep a list of all weak defined non function symbols from | |
998 | a dynamic object, using the weakdef field. Later in this | |
999 | function we will set the weakdef field to the correct | |
1000 | value. We only put non-function symbols from dynamic | |
1001 | objects on this list, because that happens to be the only | |
1002 | time we need to know the normal symbol corresponding to a | |
1003 | weak symbol, and the information is time consuming to | |
1004 | figure out. If the weakdef field is not already NULL, | |
1005 | then this symbol was already defined by some previous | |
1006 | dynamic object, and we will be using that previous | |
1007 | definition anyhow. */ | |
1008 | ||
0cb70568 ILT |
1009 | h->weakdef = weaks; |
1010 | weaks = h; | |
452a5efb | 1011 | new_weakdef = true; |
ede4eed4 KR |
1012 | } |
1013 | ||
1014 | /* Get the alignment of a common symbol. */ | |
1015 | if (sym.st_shndx == SHN_COMMON | |
0cb70568 ILT |
1016 | && h->root.type == bfd_link_hash_common) |
1017 | h->root.u.c.p->alignment_power = bfd_log2 (sym.st_value); | |
ede4eed4 KR |
1018 | |
1019 | if (info->hash->creator->flavour == bfd_target_elf_flavour) | |
1020 | { | |
1021 | int old_flags; | |
1022 | boolean dynsym; | |
1023 | int new_flag; | |
1024 | ||
1025 | /* Remember the symbol size and type. */ | |
3d7c42c9 ILT |
1026 | if (sym.st_size != 0 |
1027 | && (definition || h->size == 0)) | |
ede4eed4 | 1028 | { |
ee9f09cd | 1029 | if (h->size != 0 && h->size != sym.st_size && ! size_change_ok) |
3d7c42c9 | 1030 | (*_bfd_error_handler) |
ba254dc5 ILT |
1031 | ("Warning: size of symbol `%s' changed from %lu to %lu in %s", |
1032 | name, (unsigned long) h->size, (unsigned long) sym.st_size, | |
1033 | bfd_get_filename (abfd)); | |
1034 | ||
1035 | h->size = sym.st_size; | |
ede4eed4 | 1036 | } |
3d7c42c9 ILT |
1037 | if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE |
1038 | && (definition || h->type == STT_NOTYPE)) | |
ede4eed4 | 1039 | { |
3d7c42c9 | 1040 | if (h->type != STT_NOTYPE |
8235c112 | 1041 | && h->type != ELF_ST_TYPE (sym.st_info) |
ee9f09cd | 1042 | && ! type_change_ok) |
3d7c42c9 ILT |
1043 | (*_bfd_error_handler) |
1044 | ("Warning: type of symbol `%s' changed from %d to %d in %s", | |
1045 | name, h->type, ELF_ST_TYPE (sym.st_info), | |
1046 | bfd_get_filename (abfd)); | |
1047 | ||
ede4eed4 KR |
1048 | h->type = ELF_ST_TYPE (sym.st_info); |
1049 | } | |
1050 | ||
6c02f1a0 ILT |
1051 | if (sym.st_other != 0 |
1052 | && (definition || h->other == 0)) | |
1053 | h->other = sym.st_other; | |
1054 | ||
ede4eed4 KR |
1055 | /* Set a flag in the hash table entry indicating the type of |
1056 | reference or definition we just found. Keep a count of | |
1057 | the number of dynamic symbols we find. A dynamic symbol | |
1058 | is one which is referenced or defined by both a regular | |
440f3914 | 1059 | object and a shared object. */ |
ede4eed4 KR |
1060 | old_flags = h->elf_link_hash_flags; |
1061 | dynsym = false; | |
1062 | if (! dynamic) | |
1063 | { | |
1064 | if (! definition) | |
1065 | new_flag = ELF_LINK_HASH_REF_REGULAR; | |
1066 | else | |
1067 | new_flag = ELF_LINK_HASH_DEF_REGULAR; | |
1068 | if (info->shared | |
1069 | || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
1070 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0) | |
1071 | dynsym = true; | |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | if (! definition) | |
1076 | new_flag = ELF_LINK_HASH_REF_DYNAMIC; | |
1077 | else | |
1078 | new_flag = ELF_LINK_HASH_DEF_DYNAMIC; | |
0db6249c | 1079 | if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR |
1c4794f5 ILT |
1080 | | ELF_LINK_HASH_REF_REGULAR)) != 0 |
1081 | || (h->weakdef != NULL | |
440f3914 ILT |
1082 | && ! new_weakdef |
1083 | && h->weakdef->dynindx != -1)) | |
ede4eed4 KR |
1084 | dynsym = true; |
1085 | } | |
1086 | ||
1087 | h->elf_link_hash_flags |= new_flag; | |
d044b40a ILT |
1088 | |
1089 | /* If this symbol has a version, and it is the default | |
1090 | version, we create an indirect symbol from the default | |
1091 | name to the fully decorated name. This will cause | |
1092 | external references which do not specify a version to be | |
1093 | bound to this version of the symbol. */ | |
1094 | if (definition) | |
1095 | { | |
1096 | char *p; | |
1097 | ||
1098 | p = strchr (name, ELF_VER_CHR); | |
1099 | if (p != NULL && p[1] == ELF_VER_CHR) | |
1100 | { | |
1101 | char *shortname; | |
52c92c7f | 1102 | struct elf_link_hash_entry *hold; |
d044b40a ILT |
1103 | |
1104 | shortname = bfd_hash_allocate (&info->hash->table, | |
1105 | p - name + 1); | |
1106 | if (shortname == NULL) | |
1107 | goto error_return; | |
1108 | strncpy (shortname, name, p - name); | |
1109 | shortname[p - name] = '\0'; | |
1110 | ||
52c92c7f ILT |
1111 | /* First look to see if we have an existing symbol |
1112 | with this name. */ | |
1113 | hold = elf_link_hash_lookup (elf_hash_table (info), | |
1114 | shortname, false, false, | |
1115 | false); | |
1116 | ||
1117 | /* If we are looking at a normal object, and the | |
1118 | symbol was seen in a shared object, clobber the | |
1119 | definition in the shared object. */ | |
1120 | if (hold != NULL | |
1121 | && ! dynamic | |
1122 | && (hold->root.type == bfd_link_hash_defined | |
1123 | || hold->root.type == bfd_link_hash_defweak) | |
1124 | && (hold->elf_link_hash_flags | |
1125 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1126 | && ((hold->root.u.def.section->owner->flags & DYNAMIC) | |
1127 | != 0)) | |
1128 | { | |
1129 | /* Change the hash table entry to undefined, so | |
1130 | that _bfd_generic_link_add_one_symbol will do | |
1131 | the right thing. */ | |
1132 | hold->root.type = bfd_link_hash_undefined; | |
1133 | hold->root.u.undef.abfd = | |
1134 | hold->root.u.def.section->owner; | |
1135 | hold->verinfo.vertree = NULL; | |
1136 | hold = NULL; | |
1137 | } | |
d044b40a | 1138 | |
52c92c7f ILT |
1139 | /* If we are looking at a shared object, and we have |
1140 | already seen this symbol defined elsewhere, then | |
1141 | don't try to define it again. */ | |
1142 | if (hold != NULL | |
1143 | && dynamic | |
1144 | && (hold->root.type == bfd_link_hash_defined | |
1145 | || hold->root.type == bfd_link_hash_defweak | |
1146 | || hold->root.type == bfd_link_hash_indirect | |
1147 | || (hold->root.type == bfd_link_hash_common | |
1148 | && (bind == STB_WEAK | |
1149 | || ELF_ST_TYPE (sym.st_info) == STT_FUNC)))) | |
1150 | { | |
1151 | /* Don't add an indirect symbol. */ | |
1152 | } | |
1153 | else | |
d6bfcdb5 | 1154 | { |
52c92c7f ILT |
1155 | struct elf_link_hash_entry *hi; |
1156 | ||
1157 | hi = NULL; | |
1158 | if (! (_bfd_generic_link_add_one_symbol | |
1159 | (info, abfd, shortname, BSF_INDIRECT, | |
1160 | bfd_ind_section_ptr, (bfd_vma) 0, name, false, | |
1161 | collect, (struct bfd_link_hash_entry **) &hi))) | |
1162 | goto error_return; | |
1163 | ||
1164 | /* If there is a duplicate definition somewhere, | |
1165 | then HI may not point to an indirect symbol. | |
1166 | We will have reported an error to the user in | |
1167 | that case. */ | |
1168 | ||
1169 | if (hi->root.type == bfd_link_hash_indirect) | |
1170 | { | |
1171 | hi->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; | |
e549b1d2 ILT |
1172 | |
1173 | /* If the symbol became indirect, then we | |
1174 | assume that we have not seen a definition | |
1175 | before. */ | |
1176 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1177 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1178 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1179 | == 0); | |
1180 | ||
1181 | /* Copy down any references that we may have | |
1182 | already seen to the symbol which just | |
1183 | became indirect. */ | |
1184 | h->elf_link_hash_flags |= | |
1185 | (hi->elf_link_hash_flags | |
1186 | & (ELF_LINK_HASH_REF_DYNAMIC | |
1187 | | ELF_LINK_HASH_REF_REGULAR)); | |
1188 | ||
1189 | /* Copy over the global table offset entry. | |
1190 | This may have been already set up by a | |
1191 | check_relocs routine. */ | |
1192 | if (h->got_offset == (bfd_vma) -1) | |
1193 | { | |
1194 | h->got_offset = hi->got_offset; | |
1195 | hi->got_offset = (bfd_vma) -1; | |
1196 | } | |
1197 | BFD_ASSERT (hi->got_offset == (bfd_vma) -1); | |
1198 | ||
1199 | if (h->dynindx == -1) | |
1200 | { | |
1201 | h->dynindx = hi->dynindx; | |
1202 | h->dynstr_index = hi->dynstr_index; | |
1203 | hi->dynindx = -1; | |
1204 | hi->dynstr_index = 0; | |
1205 | } | |
1206 | BFD_ASSERT (hi->dynindx == -1); | |
1207 | ||
1208 | /* FIXME: There may be other information to | |
1209 | copy over for particular targets. */ | |
1210 | ||
1211 | /* See if the new flags lead us to realize | |
1212 | that the symbol must be dynamic. */ | |
1213 | if (! dynsym) | |
1214 | { | |
1215 | if (! dynamic) | |
1216 | { | |
1217 | if (info->shared | |
1218 | || ((hi->elf_link_hash_flags | |
1219 | & ELF_LINK_HASH_REF_DYNAMIC) | |
1220 | != 0)) | |
1221 | dynsym = true; | |
1222 | } | |
1223 | else | |
1224 | { | |
1225 | if ((hi->elf_link_hash_flags | |
1226 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1227 | dynsym = true; | |
1228 | } | |
1229 | } | |
52c92c7f | 1230 | } |
d6bfcdb5 ILT |
1231 | } |
1232 | ||
1233 | /* We also need to define an indirection from the | |
1234 | nondefault version of the symbol. */ | |
1235 | ||
1236 | shortname = bfd_hash_allocate (&info->hash->table, | |
1237 | strlen (name)); | |
1238 | if (shortname == NULL) | |
1239 | goto error_return; | |
1240 | strncpy (shortname, name, p - name); | |
1241 | strcpy (shortname + (p - name), p + 1); | |
1242 | ||
52c92c7f ILT |
1243 | /* First look to see if we have an existing symbol |
1244 | with this name. */ | |
1245 | hold = elf_link_hash_lookup (elf_hash_table (info), | |
1246 | shortname, false, false, | |
1247 | false); | |
1248 | ||
1249 | /* If we are looking at a normal object, and the | |
1250 | symbol was seen in a shared object, clobber the | |
1251 | definition in the shared object. */ | |
1252 | if (hold != NULL | |
1253 | && ! dynamic | |
1254 | && (hold->root.type == bfd_link_hash_defined | |
1255 | || hold->root.type == bfd_link_hash_defweak) | |
1256 | && (hold->elf_link_hash_flags | |
1257 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1258 | && ((hold->root.u.def.section->owner->flags & DYNAMIC) | |
1259 | != 0)) | |
1260 | { | |
1261 | /* Change the hash table entry to undefined, so | |
1262 | that _bfd_generic_link_add_one_symbol will do | |
1263 | the right thing. */ | |
1264 | hold->root.type = bfd_link_hash_undefined; | |
1265 | hold->root.u.undef.abfd = | |
1266 | hold->root.u.def.section->owner; | |
1267 | hold->verinfo.vertree = NULL; | |
1268 | hold = NULL; | |
1269 | } | |
d6bfcdb5 | 1270 | |
52c92c7f ILT |
1271 | /* If we are looking at a shared object, and we have |
1272 | already seen this symbol defined elsewhere, then | |
1273 | don't try to define it again. */ | |
1274 | if (hold != NULL | |
1275 | && dynamic | |
1276 | && (hold->root.type == bfd_link_hash_defined | |
1277 | || hold->root.type == bfd_link_hash_defweak | |
1278 | || hold->root.type == bfd_link_hash_indirect | |
1279 | || (hold->root.type == bfd_link_hash_common | |
1280 | && (bind == STB_WEAK | |
1281 | || ELF_ST_TYPE (sym.st_info) == STT_FUNC)))) | |
d6bfcdb5 | 1282 | { |
52c92c7f ILT |
1283 | /* Don't add an indirect symbol. */ |
1284 | } | |
1285 | else | |
1286 | { | |
1287 | struct elf_link_hash_entry *hi; | |
1288 | ||
1289 | hi = NULL; | |
1290 | if (! (_bfd_generic_link_add_one_symbol | |
1291 | (info, abfd, shortname, BSF_INDIRECT, | |
1292 | bfd_ind_section_ptr, (bfd_vma) 0, name, false, | |
1293 | collect, (struct bfd_link_hash_entry **) &hi))) | |
1294 | goto error_return; | |
1295 | ||
1296 | /* If there is a duplicate definition somewhere, | |
1297 | then HI may not point to an indirect symbol. | |
1298 | We will have reported an error to the user in | |
1299 | that case. */ | |
1300 | ||
1301 | if (hi->root.type == bfd_link_hash_indirect) | |
1302 | { | |
1303 | hi->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; | |
e549b1d2 ILT |
1304 | |
1305 | /* If the symbol became indirect, then we | |
1306 | assume that we have not seen a definition | |
1307 | before. */ | |
1308 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1309 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1310 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1311 | == 0); | |
1312 | ||
1313 | /* Copy down any references that we may have | |
1314 | already seen to the symbol which just | |
1315 | became indirect. */ | |
1316 | h->elf_link_hash_flags |= | |
1317 | (hi->elf_link_hash_flags | |
1318 | & (ELF_LINK_HASH_REF_DYNAMIC | |
1319 | | ELF_LINK_HASH_REF_REGULAR)); | |
1320 | ||
1321 | /* Copy over the global table offset entry. | |
1322 | This may have been already set up by a | |
1323 | check_relocs routine. */ | |
1324 | if (h->got_offset == (bfd_vma) -1) | |
1325 | { | |
1326 | h->got_offset = hi->got_offset; | |
1327 | hi->got_offset = (bfd_vma) -1; | |
1328 | } | |
1329 | BFD_ASSERT (hi->got_offset == (bfd_vma) -1); | |
1330 | ||
1331 | if (h->dynindx == -1) | |
1332 | { | |
1333 | h->dynindx = hi->dynindx; | |
1334 | h->dynstr_index = hi->dynstr_index; | |
1335 | hi->dynindx = -1; | |
1336 | hi->dynstr_index = 0; | |
1337 | } | |
1338 | BFD_ASSERT (hi->dynindx == -1); | |
1339 | ||
1340 | /* FIXME: There may be other information to | |
1341 | copy over for particular targets. */ | |
1342 | ||
1343 | /* See if the new flags lead us to realize | |
1344 | that the symbol must be dynamic. */ | |
1345 | if (! dynsym) | |
1346 | { | |
1347 | if (! dynamic) | |
1348 | { | |
1349 | if (info->shared | |
1350 | || ((hi->elf_link_hash_flags | |
1351 | & ELF_LINK_HASH_REF_DYNAMIC) | |
1352 | != 0)) | |
1353 | dynsym = true; | |
1354 | } | |
1355 | else | |
1356 | { | |
1357 | if ((hi->elf_link_hash_flags | |
1358 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1359 | dynsym = true; | |
1360 | } | |
1361 | } | |
52c92c7f | 1362 | } |
d6bfcdb5 | 1363 | } |
d044b40a ILT |
1364 | } |
1365 | } | |
1366 | ||
ede4eed4 KR |
1367 | if (dynsym && h->dynindx == -1) |
1368 | { | |
1369 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1370 | goto error_return; | |
452a5efb ILT |
1371 | if (h->weakdef != NULL |
1372 | && ! new_weakdef | |
1373 | && h->weakdef->dynindx == -1) | |
1374 | { | |
1375 | if (! _bfd_elf_link_record_dynamic_symbol (info, | |
1376 | h->weakdef)) | |
1377 | goto error_return; | |
1378 | } | |
ede4eed4 KR |
1379 | } |
1380 | } | |
1381 | } | |
1382 | ||
1383 | /* Now set the weakdefs field correctly for all the weak defined | |
1384 | symbols we found. The only way to do this is to search all the | |
1385 | symbols. Since we only need the information for non functions in | |
1386 | dynamic objects, that's the only time we actually put anything on | |
1387 | the list WEAKS. We need this information so that if a regular | |
1388 | object refers to a symbol defined weakly in a dynamic object, the | |
1389 | real symbol in the dynamic object is also put in the dynamic | |
1390 | symbols; we also must arrange for both symbols to point to the | |
1391 | same memory location. We could handle the general case of symbol | |
1392 | aliasing, but a general symbol alias can only be generated in | |
1393 | assembler code, handling it correctly would be very time | |
1394 | consuming, and other ELF linkers don't handle general aliasing | |
1395 | either. */ | |
1396 | while (weaks != NULL) | |
1397 | { | |
1398 | struct elf_link_hash_entry *hlook; | |
1399 | asection *slook; | |
1400 | bfd_vma vlook; | |
1401 | struct elf_link_hash_entry **hpp; | |
1402 | struct elf_link_hash_entry **hppend; | |
1403 | ||
1404 | hlook = weaks; | |
1405 | weaks = hlook->weakdef; | |
1406 | hlook->weakdef = NULL; | |
1407 | ||
1408 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
1409 | || hlook->root.type == bfd_link_hash_defweak | |
1410 | || hlook->root.type == bfd_link_hash_common | |
1411 | || hlook->root.type == bfd_link_hash_indirect); | |
1412 | slook = hlook->root.u.def.section; | |
1413 | vlook = hlook->root.u.def.value; | |
1414 | ||
1415 | hpp = elf_sym_hashes (abfd); | |
1416 | hppend = hpp + extsymcount; | |
1417 | for (; hpp < hppend; hpp++) | |
1418 | { | |
1419 | struct elf_link_hash_entry *h; | |
1420 | ||
1421 | h = *hpp; | |
1422 | if (h != NULL && h != hlook | |
d2bb6c79 | 1423 | && h->root.type == bfd_link_hash_defined |
ede4eed4 KR |
1424 | && h->root.u.def.section == slook |
1425 | && h->root.u.def.value == vlook) | |
1426 | { | |
1427 | hlook->weakdef = h; | |
1428 | ||
1429 | /* If the weak definition is in the list of dynamic | |
1430 | symbols, make sure the real definition is put there | |
1431 | as well. */ | |
1432 | if (hlook->dynindx != -1 | |
1433 | && h->dynindx == -1) | |
1434 | { | |
1435 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1436 | goto error_return; | |
1437 | } | |
1438 | ||
440f3914 ILT |
1439 | /* If the real definition is in the list of dynamic |
1440 | symbols, make sure the weak definition is put there | |
1441 | as well. If we don't do this, then the dynamic | |
1442 | loader might not merge the entries for the real | |
1443 | definition and the weak definition. */ | |
1444 | if (h->dynindx != -1 | |
1445 | && hlook->dynindx == -1) | |
1446 | { | |
1447 | if (! _bfd_elf_link_record_dynamic_symbol (info, hlook)) | |
1448 | goto error_return; | |
1449 | } | |
1450 | ||
ede4eed4 KR |
1451 | break; |
1452 | } | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | if (buf != NULL) | |
1457 | { | |
1458 | free (buf); | |
1459 | buf = NULL; | |
1460 | } | |
1461 | ||
d044b40a ILT |
1462 | if (extversym != NULL) |
1463 | { | |
1464 | free (extversym); | |
1465 | extversym = NULL; | |
1466 | } | |
1467 | ||
ede4eed4 KR |
1468 | /* If this object is the same format as the output object, and it is |
1469 | not a shared library, then let the backend look through the | |
1470 | relocs. | |
1471 | ||
1472 | This is required to build global offset table entries and to | |
1473 | arrange for dynamic relocs. It is not required for the | |
1474 | particular common case of linking non PIC code, even when linking | |
1475 | against shared libraries, but unfortunately there is no way of | |
1476 | knowing whether an object file has been compiled PIC or not. | |
1477 | Looking through the relocs is not particularly time consuming. | |
1478 | The problem is that we must either (1) keep the relocs in memory, | |
1479 | which causes the linker to require additional runtime memory or | |
1480 | (2) read the relocs twice from the input file, which wastes time. | |
1481 | This would be a good case for using mmap. | |
1482 | ||
1483 | I have no idea how to handle linking PIC code into a file of a | |
1484 | different format. It probably can't be done. */ | |
1485 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
1486 | if (! dynamic | |
1487 | && abfd->xvec == info->hash->creator | |
1488 | && check_relocs != NULL) | |
1489 | { | |
1490 | asection *o; | |
1491 | ||
1492 | for (o = abfd->sections; o != NULL; o = o->next) | |
1493 | { | |
1494 | Elf_Internal_Rela *internal_relocs; | |
1495 | boolean ok; | |
1496 | ||
1497 | if ((o->flags & SEC_RELOC) == 0 | |
1498 | || o->reloc_count == 0) | |
ede4eed4 KR |
1499 | continue; |
1500 | ||
c86158e5 ILT |
1501 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
1502 | (abfd, o, (PTR) NULL, | |
1503 | (Elf_Internal_Rela *) NULL, | |
1504 | info->keep_memory)); | |
ede4eed4 KR |
1505 | if (internal_relocs == NULL) |
1506 | goto error_return; | |
1507 | ||
1508 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
1509 | ||
1510 | if (! info->keep_memory) | |
1511 | free (internal_relocs); | |
1512 | ||
1513 | if (! ok) | |
1514 | goto error_return; | |
1515 | } | |
1516 | } | |
1517 | ||
1726b8f0 ILT |
1518 | /* If this is a non-traditional, non-relocateable link, try to |
1519 | optimize the handling of the .stab/.stabstr sections. */ | |
1520 | if (! dynamic | |
1521 | && ! info->relocateable | |
1522 | && ! info->traditional_format | |
1523 | && info->hash->creator->flavour == bfd_target_elf_flavour | |
1524 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
1525 | { | |
1526 | asection *stab, *stabstr; | |
1527 | ||
1528 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
1529 | if (stab != NULL) | |
1530 | { | |
1531 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
1532 | ||
1533 | if (stabstr != NULL) | |
1534 | { | |
1535 | struct bfd_elf_section_data *secdata; | |
1536 | ||
1537 | secdata = elf_section_data (stab); | |
1538 | if (! _bfd_link_section_stabs (abfd, | |
1539 | &elf_hash_table (info)->stab_info, | |
1540 | stab, stabstr, | |
1541 | &secdata->stab_info)) | |
1542 | goto error_return; | |
1543 | } | |
1544 | } | |
1545 | } | |
1546 | ||
ede4eed4 KR |
1547 | return true; |
1548 | ||
1549 | error_return: | |
1550 | if (buf != NULL) | |
1551 | free (buf); | |
1552 | if (dynbuf != NULL) | |
1553 | free (dynbuf); | |
d044b40a ILT |
1554 | if (dynver != NULL) |
1555 | free (dynver); | |
1556 | if (extversym != NULL) | |
1557 | free (extversym); | |
ede4eed4 KR |
1558 | return false; |
1559 | } | |
1560 | ||
1561 | /* Create some sections which will be filled in with dynamic linking | |
1562 | information. ABFD is an input file which requires dynamic sections | |
1563 | to be created. The dynamic sections take up virtual memory space | |
1564 | when the final executable is run, so we need to create them before | |
1565 | addresses are assigned to the output sections. We work out the | |
1566 | actual contents and size of these sections later. */ | |
1567 | ||
1568 | boolean | |
1569 | elf_link_create_dynamic_sections (abfd, info) | |
1570 | bfd *abfd; | |
1571 | struct bfd_link_info *info; | |
1572 | { | |
1573 | flagword flags; | |
1574 | register asection *s; | |
1575 | struct elf_link_hash_entry *h; | |
1576 | struct elf_backend_data *bed; | |
1577 | ||
1578 | if (elf_hash_table (info)->dynamic_sections_created) | |
1579 | return true; | |
1580 | ||
1581 | /* Make sure that all dynamic sections use the same input BFD. */ | |
1582 | if (elf_hash_table (info)->dynobj == NULL) | |
1583 | elf_hash_table (info)->dynobj = abfd; | |
1584 | else | |
1585 | abfd = elf_hash_table (info)->dynobj; | |
1586 | ||
1587 | /* Note that we set the SEC_IN_MEMORY flag for all of these | |
1588 | sections. */ | |
ff12f303 ILT |
1589 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
1590 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
ede4eed4 KR |
1591 | |
1592 | /* A dynamically linked executable has a .interp section, but a | |
1593 | shared library does not. */ | |
1594 | if (! info->shared) | |
1595 | { | |
1596 | s = bfd_make_section (abfd, ".interp"); | |
1597 | if (s == NULL | |
1598 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
1599 | return false; | |
1600 | } | |
1601 | ||
d044b40a ILT |
1602 | /* Create sections to hold version informations. These are removed |
1603 | if they are not needed. */ | |
1604 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
1605 | if (s == NULL | |
1606 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1607 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
1608 | return false; | |
1609 | ||
1610 | s = bfd_make_section (abfd, ".gnu.version"); | |
1611 | if (s == NULL | |
1612 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1613 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
1614 | return false; | |
1615 | ||
1616 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
1617 | if (s == NULL | |
1618 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1619 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
1620 | return false; | |
1621 | ||
ede4eed4 KR |
1622 | s = bfd_make_section (abfd, ".dynsym"); |
1623 | if (s == NULL | |
1624 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1625 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
1626 | return false; | |
1627 | ||
1628 | s = bfd_make_section (abfd, ".dynstr"); | |
1629 | if (s == NULL | |
1630 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
1631 | return false; | |
1632 | ||
1633 | /* Create a strtab to hold the dynamic symbol names. */ | |
1634 | if (elf_hash_table (info)->dynstr == NULL) | |
1635 | { | |
1636 | elf_hash_table (info)->dynstr = elf_stringtab_init (); | |
1637 | if (elf_hash_table (info)->dynstr == NULL) | |
1638 | return false; | |
1639 | } | |
1640 | ||
1641 | s = bfd_make_section (abfd, ".dynamic"); | |
1642 | if (s == NULL | |
1643 | || ! bfd_set_section_flags (abfd, s, flags) | |
1644 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
1645 | return false; | |
1646 | ||
1647 | /* The special symbol _DYNAMIC is always set to the start of the | |
1648 | .dynamic section. This call occurs before we have processed the | |
1649 | symbols for any dynamic object, so we don't have to worry about | |
1650 | overriding a dynamic definition. We could set _DYNAMIC in a | |
1651 | linker script, but we only want to define it if we are, in fact, | |
1652 | creating a .dynamic section. We don't want to define it if there | |
1653 | is no .dynamic section, since on some ELF platforms the start up | |
1654 | code examines it to decide how to initialize the process. */ | |
1655 | h = NULL; | |
1656 | if (! (_bfd_generic_link_add_one_symbol | |
1657 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0, | |
1658 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
1659 | (struct bfd_link_hash_entry **) &h))) | |
1660 | return false; | |
1661 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
1662 | h->type = STT_OBJECT; | |
1663 | ||
1664 | if (info->shared | |
1665 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1666 | return false; | |
1667 | ||
1668 | s = bfd_make_section (abfd, ".hash"); | |
1669 | if (s == NULL | |
1670 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1671 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
1672 | return false; | |
1673 | ||
1674 | /* Let the backend create the rest of the sections. This lets the | |
1675 | backend set the right flags. The backend will normally create | |
1676 | the .got and .plt sections. */ | |
1677 | bed = get_elf_backend_data (abfd); | |
1678 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
1679 | return false; | |
1680 | ||
1681 | elf_hash_table (info)->dynamic_sections_created = true; | |
1682 | ||
1683 | return true; | |
1684 | } | |
1685 | ||
1686 | /* Add an entry to the .dynamic table. */ | |
1687 | ||
1688 | boolean | |
1689 | elf_add_dynamic_entry (info, tag, val) | |
1690 | struct bfd_link_info *info; | |
1691 | bfd_vma tag; | |
1692 | bfd_vma val; | |
1693 | { | |
1694 | Elf_Internal_Dyn dyn; | |
1695 | bfd *dynobj; | |
1696 | asection *s; | |
1697 | size_t newsize; | |
1698 | bfd_byte *newcontents; | |
1699 | ||
1700 | dynobj = elf_hash_table (info)->dynobj; | |
1701 | ||
1702 | s = bfd_get_section_by_name (dynobj, ".dynamic"); | |
1703 | BFD_ASSERT (s != NULL); | |
1704 | ||
1705 | newsize = s->_raw_size + sizeof (Elf_External_Dyn); | |
58142f10 | 1706 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
ede4eed4 | 1707 | if (newcontents == NULL) |
58142f10 | 1708 | return false; |
ede4eed4 KR |
1709 | |
1710 | dyn.d_tag = tag; | |
1711 | dyn.d_un.d_val = val; | |
1712 | elf_swap_dyn_out (dynobj, &dyn, | |
1713 | (Elf_External_Dyn *) (newcontents + s->_raw_size)); | |
1714 | ||
1715 | s->_raw_size = newsize; | |
1716 | s->contents = newcontents; | |
1717 | ||
1718 | return true; | |
1719 | } | |
3b3753b8 | 1720 | \f |
ede4eed4 KR |
1721 | |
1722 | /* Read and swap the relocs for a section. They may have been cached. | |
1723 | If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL, | |
1724 | they are used as buffers to read into. They are known to be large | |
1725 | enough. If the INTERNAL_RELOCS relocs argument is NULL, the return | |
1726 | value is allocated using either malloc or bfd_alloc, according to | |
1727 | the KEEP_MEMORY argument. */ | |
1728 | ||
c86158e5 ILT |
1729 | Elf_Internal_Rela * |
1730 | NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs, | |
1731 | keep_memory) | |
ede4eed4 KR |
1732 | bfd *abfd; |
1733 | asection *o; | |
1734 | PTR external_relocs; | |
1735 | Elf_Internal_Rela *internal_relocs; | |
1736 | boolean keep_memory; | |
1737 | { | |
1738 | Elf_Internal_Shdr *rel_hdr; | |
1739 | PTR alloc1 = NULL; | |
1740 | Elf_Internal_Rela *alloc2 = NULL; | |
1741 | ||
1742 | if (elf_section_data (o)->relocs != NULL) | |
1743 | return elf_section_data (o)->relocs; | |
1744 | ||
1745 | if (o->reloc_count == 0) | |
1746 | return NULL; | |
1747 | ||
1748 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1749 | ||
1750 | if (internal_relocs == NULL) | |
1751 | { | |
1752 | size_t size; | |
1753 | ||
1754 | size = o->reloc_count * sizeof (Elf_Internal_Rela); | |
1755 | if (keep_memory) | |
1756 | internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size); | |
1757 | else | |
58142f10 | 1758 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
ede4eed4 | 1759 | if (internal_relocs == NULL) |
58142f10 | 1760 | goto error_return; |
ede4eed4 KR |
1761 | } |
1762 | ||
1763 | if (external_relocs == NULL) | |
1764 | { | |
58142f10 | 1765 | alloc1 = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size); |
ede4eed4 | 1766 | if (alloc1 == NULL) |
58142f10 | 1767 | goto error_return; |
ede4eed4 KR |
1768 | external_relocs = alloc1; |
1769 | } | |
1770 | ||
1771 | if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0) | |
1772 | || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd) | |
1773 | != rel_hdr->sh_size)) | |
1774 | goto error_return; | |
1775 | ||
1776 | /* Swap in the relocs. For convenience, we always produce an | |
1777 | Elf_Internal_Rela array; if the relocs are Rel, we set the addend | |
1778 | to 0. */ | |
1779 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
1780 | { | |
1781 | Elf_External_Rel *erel; | |
1782 | Elf_External_Rel *erelend; | |
1783 | Elf_Internal_Rela *irela; | |
1784 | ||
1785 | erel = (Elf_External_Rel *) external_relocs; | |
1786 | erelend = erel + o->reloc_count; | |
1787 | irela = internal_relocs; | |
1788 | for (; erel < erelend; erel++, irela++) | |
1789 | { | |
1790 | Elf_Internal_Rel irel; | |
1791 | ||
1792 | elf_swap_reloc_in (abfd, erel, &irel); | |
1793 | irela->r_offset = irel.r_offset; | |
1794 | irela->r_info = irel.r_info; | |
1795 | irela->r_addend = 0; | |
1796 | } | |
1797 | } | |
1798 | else | |
1799 | { | |
1800 | Elf_External_Rela *erela; | |
1801 | Elf_External_Rela *erelaend; | |
1802 | Elf_Internal_Rela *irela; | |
1803 | ||
1804 | BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela)); | |
1805 | ||
1806 | erela = (Elf_External_Rela *) external_relocs; | |
1807 | erelaend = erela + o->reloc_count; | |
1808 | irela = internal_relocs; | |
1809 | for (; erela < erelaend; erela++, irela++) | |
1810 | elf_swap_reloca_in (abfd, erela, irela); | |
1811 | } | |
1812 | ||
1813 | /* Cache the results for next time, if we can. */ | |
1814 | if (keep_memory) | |
1815 | elf_section_data (o)->relocs = internal_relocs; | |
ff12f303 | 1816 | |
ede4eed4 KR |
1817 | if (alloc1 != NULL) |
1818 | free (alloc1); | |
1819 | ||
1820 | /* Don't free alloc2, since if it was allocated we are passing it | |
1821 | back (under the name of internal_relocs). */ | |
1822 | ||
1823 | return internal_relocs; | |
1824 | ||
1825 | error_return: | |
1826 | if (alloc1 != NULL) | |
1827 | free (alloc1); | |
1828 | if (alloc2 != NULL) | |
1829 | free (alloc2); | |
1830 | return NULL; | |
1831 | } | |
3b3753b8 | 1832 | \f |
ede4eed4 KR |
1833 | |
1834 | /* Record an assignment to a symbol made by a linker script. We need | |
1835 | this in case some dynamic object refers to this symbol. */ | |
1836 | ||
1837 | /*ARGSUSED*/ | |
1838 | boolean | |
1839 | NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide) | |
1840 | bfd *output_bfd; | |
1841 | struct bfd_link_info *info; | |
1842 | const char *name; | |
1843 | boolean provide; | |
1844 | { | |
1845 | struct elf_link_hash_entry *h; | |
1846 | ||
1847 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
1848 | return true; | |
1849 | ||
1850 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false); | |
1851 | if (h == NULL) | |
1852 | return false; | |
1853 | ||
869b7d80 ILT |
1854 | if (h->root.type == bfd_link_hash_new) |
1855 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; | |
1856 | ||
ede4eed4 KR |
1857 | /* If this symbol is being provided by the linker script, and it is |
1858 | currently defined by a dynamic object, but not by a regular | |
1859 | object, then mark it as undefined so that the generic linker will | |
1860 | force the correct value. */ | |
1861 | if (provide | |
1862 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1863 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1864 | h->root.type = bfd_link_hash_undefined; | |
1865 | ||
1866 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
1867 | h->type = STT_OBJECT; | |
1868 | ||
1869 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
1870 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
1871 | || info->shared) | |
1872 | && h->dynindx == -1) | |
1873 | { | |
1874 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1875 | return false; | |
1876 | ||
1877 | /* If this is a weak defined symbol, and we know a corresponding | |
1878 | real symbol from the same dynamic object, make sure the real | |
1879 | symbol is also made into a dynamic symbol. */ | |
1880 | if (h->weakdef != NULL | |
1881 | && h->weakdef->dynindx == -1) | |
1882 | { | |
1883 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) | |
1884 | return false; | |
1885 | } | |
1886 | } | |
1887 | ||
1888 | return true; | |
1889 | } | |
3b3753b8 | 1890 | \f |
d044b40a ILT |
1891 | /* This structure is used to pass information to |
1892 | elf_link_assign_sym_version. */ | |
1893 | ||
1894 | struct elf_assign_sym_version_info | |
1895 | { | |
1896 | /* Output BFD. */ | |
1897 | bfd *output_bfd; | |
1898 | /* General link information. */ | |
1899 | struct bfd_link_info *info; | |
1900 | /* Version tree. */ | |
1901 | struct bfd_elf_version_tree *verdefs; | |
1902 | /* Whether we are exporting all dynamic symbols. */ | |
1903 | boolean export_dynamic; | |
1904 | /* Whether we removed any symbols from the dynamic symbol table. */ | |
1905 | boolean removed_dynamic; | |
1906 | /* Whether we had a failure. */ | |
1907 | boolean failed; | |
1908 | }; | |
1909 | ||
1910 | /* This structure is used to pass information to | |
1911 | elf_link_find_version_dependencies. */ | |
1912 | ||
1913 | struct elf_find_verdep_info | |
1914 | { | |
1915 | /* Output BFD. */ | |
1916 | bfd *output_bfd; | |
1917 | /* General link information. */ | |
1918 | struct bfd_link_info *info; | |
1919 | /* The number of dependencies. */ | |
1920 | unsigned int vers; | |
1921 | /* Whether we had a failure. */ | |
1922 | boolean failed; | |
1923 | }; | |
ede4eed4 KR |
1924 | |
1925 | /* Array used to determine the number of hash table buckets to use | |
1926 | based on the number of symbols there are. If there are fewer than | |
1927 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
1928 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
6b8ec6f3 | 1929 | than 32771 buckets. */ |
ede4eed4 KR |
1930 | |
1931 | static const size_t elf_buckets[] = | |
1932 | { | |
6b8ec6f3 ILT |
1933 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, |
1934 | 16411, 32771, 0 | |
ede4eed4 KR |
1935 | }; |
1936 | ||
1937 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
1938 | called by the ELF linker emulation before_allocation routine. We | |
1939 | must set the sizes of the sections before the linker sets the | |
1940 | addresses of the various sections. */ | |
1941 | ||
1942 | boolean | |
1943 | NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, | |
148437ec | 1944 | export_dynamic, filter_shlib, |
d044b40a ILT |
1945 | auxiliary_filters, info, sinterpptr, |
1946 | verdefs) | |
ede4eed4 KR |
1947 | bfd *output_bfd; |
1948 | const char *soname; | |
1949 | const char *rpath; | |
1950 | boolean export_dynamic; | |
148437ec | 1951 | const char *filter_shlib; |
db109ca2 | 1952 | const char * const *auxiliary_filters; |
ede4eed4 KR |
1953 | struct bfd_link_info *info; |
1954 | asection **sinterpptr; | |
d044b40a | 1955 | struct bfd_elf_version_tree *verdefs; |
ede4eed4 | 1956 | { |
d044b40a | 1957 | bfd_size_type soname_indx; |
ede4eed4 KR |
1958 | bfd *dynobj; |
1959 | struct elf_backend_data *bed; | |
e549b1d2 | 1960 | bfd_size_type old_dynsymcount; |
ede4eed4 KR |
1961 | |
1962 | *sinterpptr = NULL; | |
1963 | ||
d044b40a ILT |
1964 | soname_indx = -1; |
1965 | ||
ede4eed4 KR |
1966 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
1967 | return true; | |
1968 | ||
ff12f303 ILT |
1969 | /* The backend may have to create some sections regardless of whether |
1970 | we're dynamic or not. */ | |
1971 | bed = get_elf_backend_data (output_bfd); | |
1972 | if (bed->elf_backend_always_size_sections | |
1973 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
1974 | return false; | |
1975 | ||
ede4eed4 KR |
1976 | dynobj = elf_hash_table (info)->dynobj; |
1977 | ||
1978 | /* If there were no dynamic objects in the link, there is nothing to | |
1979 | do here. */ | |
1980 | if (dynobj == NULL) | |
1981 | return true; | |
1982 | ||
1983 | /* If we are supposed to export all symbols into the dynamic symbol | |
1984 | table (this is not the normal case), then do so. */ | |
1985 | if (export_dynamic) | |
1986 | { | |
1987 | struct elf_info_failed eif; | |
1988 | ||
1989 | eif.failed = false; | |
1990 | eif.info = info; | |
1991 | elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol, | |
1992 | (PTR) &eif); | |
1993 | if (eif.failed) | |
1994 | return false; | |
1995 | } | |
1996 | ||
1997 | if (elf_hash_table (info)->dynamic_sections_created) | |
1998 | { | |
1999 | struct elf_info_failed eif; | |
73a68447 | 2000 | struct elf_link_hash_entry *h; |
ede4eed4 KR |
2001 | bfd_size_type strsize; |
2002 | ||
2003 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
2004 | BFD_ASSERT (*sinterpptr != NULL || info->shared); | |
2005 | ||
2006 | if (soname != NULL) | |
2007 | { | |
d044b40a ILT |
2008 | soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
2009 | soname, true, true); | |
2010 | if (soname_indx == (bfd_size_type) -1 | |
2011 | || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
ede4eed4 | 2012 | return false; |
ff12f303 | 2013 | } |
ede4eed4 | 2014 | |
951fe66d ILT |
2015 | if (info->symbolic) |
2016 | { | |
2017 | if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
2018 | return false; | |
2019 | } | |
2020 | ||
ede4eed4 KR |
2021 | if (rpath != NULL) |
2022 | { | |
2023 | bfd_size_type indx; | |
2024 | ||
2025 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath, | |
2026 | true, true); | |
2027 | if (indx == (bfd_size_type) -1 | |
2028 | || ! elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
2029 | return false; | |
2030 | } | |
2031 | ||
148437ec ILT |
2032 | if (filter_shlib != NULL) |
2033 | { | |
2034 | bfd_size_type indx; | |
2035 | ||
2036 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2037 | filter_shlib, true, true); | |
2038 | if (indx == (bfd_size_type) -1 | |
2039 | || ! elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
2040 | return false; | |
2041 | } | |
2042 | ||
db109ca2 | 2043 | if (auxiliary_filters != NULL) |
148437ec | 2044 | { |
db109ca2 | 2045 | const char * const *p; |
148437ec | 2046 | |
db109ca2 ILT |
2047 | for (p = auxiliary_filters; *p != NULL; p++) |
2048 | { | |
2049 | bfd_size_type indx; | |
2050 | ||
2051 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2052 | *p, true, true); | |
2053 | if (indx == (bfd_size_type) -1 | |
2054 | || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
2055 | return false; | |
2056 | } | |
148437ec ILT |
2057 | } |
2058 | ||
ede4eed4 KR |
2059 | /* Find all symbols which were defined in a dynamic object and make |
2060 | the backend pick a reasonable value for them. */ | |
2061 | eif.failed = false; | |
2062 | eif.info = info; | |
2063 | elf_link_hash_traverse (elf_hash_table (info), | |
2064 | elf_adjust_dynamic_symbol, | |
2065 | (PTR) &eif); | |
2066 | if (eif.failed) | |
2067 | return false; | |
2068 | ||
2069 | /* Add some entries to the .dynamic section. We fill in some of the | |
2070 | values later, in elf_bfd_final_link, but we must add the entries | |
2071 | now so that we know the final size of the .dynamic section. */ | |
73a68447 ILT |
2072 | h = elf_link_hash_lookup (elf_hash_table (info), "_init", false, |
2073 | false, false); | |
2074 | if (h != NULL | |
2075 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
2076 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
ede4eed4 KR |
2077 | { |
2078 | if (! elf_add_dynamic_entry (info, DT_INIT, 0)) | |
2079 | return false; | |
2080 | } | |
73a68447 ILT |
2081 | h = elf_link_hash_lookup (elf_hash_table (info), "_fini", false, |
2082 | false, false); | |
2083 | if (h != NULL | |
2084 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
2085 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
ede4eed4 KR |
2086 | { |
2087 | if (! elf_add_dynamic_entry (info, DT_FINI, 0)) | |
2088 | return false; | |
2089 | } | |
2090 | strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
2091 | if (! elf_add_dynamic_entry (info, DT_HASH, 0) | |
2092 | || ! elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
2093 | || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
2094 | || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
2095 | || ! elf_add_dynamic_entry (info, DT_SYMENT, | |
2096 | sizeof (Elf_External_Sym))) | |
2097 | return false; | |
2098 | } | |
2099 | ||
2100 | /* The backend must work out the sizes of all the other dynamic | |
2101 | sections. */ | |
e549b1d2 | 2102 | old_dynsymcount = elf_hash_table (info)->dynsymcount; |
ede4eed4 KR |
2103 | if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
2104 | return false; | |
2105 | ||
2106 | if (elf_hash_table (info)->dynamic_sections_created) | |
2107 | { | |
2108 | size_t dynsymcount; | |
2109 | asection *s; | |
2110 | size_t i; | |
2111 | size_t bucketcount = 0; | |
2112 | Elf_Internal_Sym isym; | |
d6bfcdb5 | 2113 | struct elf_assign_sym_version_info sinfo; |
ede4eed4 | 2114 | |
d044b40a ILT |
2115 | /* Set up the version definition section. */ |
2116 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2117 | BFD_ASSERT (s != NULL); | |
d6bfcdb5 ILT |
2118 | |
2119 | /* Attach all the symbols to their version information. This | |
2120 | may cause some symbols to be unexported. */ | |
2121 | sinfo.output_bfd = output_bfd; | |
2122 | sinfo.info = info; | |
2123 | sinfo.verdefs = verdefs; | |
2124 | sinfo.export_dynamic = export_dynamic; | |
2125 | sinfo.removed_dynamic = false; | |
2126 | sinfo.failed = false; | |
2127 | ||
2128 | elf_link_hash_traverse (elf_hash_table (info), | |
2129 | elf_link_assign_sym_version, | |
2130 | (PTR) &sinfo); | |
2131 | if (sinfo.failed) | |
2132 | return false; | |
2133 | ||
2134 | /* We may have created additional version definitions if we are | |
2135 | just linking a regular application. */ | |
2136 | verdefs = sinfo.verdefs; | |
2137 | ||
d044b40a ILT |
2138 | if (verdefs == NULL) |
2139 | { | |
d044b40a ILT |
2140 | asection **spp; |
2141 | ||
d044b40a ILT |
2142 | /* Don't include this section in the output file. */ |
2143 | for (spp = &output_bfd->sections; | |
2144 | *spp != s->output_section; | |
2145 | spp = &(*spp)->next) | |
2146 | ; | |
2147 | *spp = s->output_section->next; | |
2148 | --output_bfd->section_count; | |
2149 | } | |
2150 | else | |
2151 | { | |
d044b40a ILT |
2152 | unsigned int cdefs; |
2153 | bfd_size_type size; | |
2154 | struct bfd_elf_version_tree *t; | |
2155 | bfd_byte *p; | |
2156 | Elf_Internal_Verdef def; | |
2157 | Elf_Internal_Verdaux defaux; | |
2158 | ||
d044b40a ILT |
2159 | if (sinfo.removed_dynamic) |
2160 | { | |
2161 | /* Some dynamic symbols were changed to be local | |
e549b1d2 ILT |
2162 | symbols. In this case, we renumber all of the |
2163 | dynamic symbols, so that we don't have a hole. If | |
2164 | the backend changed dynsymcount, then assume that the | |
2165 | new symbols are at the start. This is the case on | |
2166 | the MIPS. FIXME: The names of the removed symbols | |
2167 | will still be in the dynamic string table, wasting | |
2168 | space. */ | |
2169 | elf_hash_table (info)->dynsymcount = | |
2170 | 1 + (elf_hash_table (info)->dynsymcount - old_dynsymcount); | |
d044b40a ILT |
2171 | elf_link_hash_traverse (elf_hash_table (info), |
2172 | elf_link_renumber_dynsyms, | |
2173 | (PTR) info); | |
2174 | } | |
2175 | ||
2176 | cdefs = 0; | |
2177 | size = 0; | |
2178 | ||
2179 | /* Make space for the base version. */ | |
2180 | size += sizeof (Elf_External_Verdef); | |
2181 | size += sizeof (Elf_External_Verdaux); | |
2182 | ++cdefs; | |
2183 | ||
2184 | for (t = verdefs; t != NULL; t = t->next) | |
2185 | { | |
2186 | struct bfd_elf_version_deps *n; | |
2187 | ||
2188 | size += sizeof (Elf_External_Verdef); | |
2189 | size += sizeof (Elf_External_Verdaux); | |
2190 | ++cdefs; | |
2191 | ||
2192 | for (n = t->deps; n != NULL; n = n->next) | |
2193 | size += sizeof (Elf_External_Verdaux); | |
2194 | } | |
2195 | ||
2196 | s->_raw_size = size; | |
2197 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
2198 | if (s->contents == NULL && s->_raw_size != 0) | |
2199 | return false; | |
2200 | ||
2201 | /* Fill in the version definition section. */ | |
2202 | ||
2203 | p = s->contents; | |
2204 | ||
2205 | def.vd_version = VER_DEF_CURRENT; | |
2206 | def.vd_flags = VER_FLG_BASE; | |
2207 | def.vd_ndx = 1; | |
2208 | def.vd_cnt = 1; | |
2209 | def.vd_aux = sizeof (Elf_External_Verdef); | |
2210 | def.vd_next = (sizeof (Elf_External_Verdef) | |
2211 | + sizeof (Elf_External_Verdaux)); | |
2212 | ||
2213 | if (soname_indx != -1) | |
2214 | { | |
2215 | def.vd_hash = bfd_elf_hash ((const unsigned char *) soname); | |
2216 | defaux.vda_name = soname_indx; | |
2217 | } | |
2218 | else | |
2219 | { | |
2220 | const char *name; | |
2221 | bfd_size_type indx; | |
2222 | ||
2223 | name = output_bfd->filename; | |
2224 | def.vd_hash = bfd_elf_hash ((const unsigned char *) name); | |
2225 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2226 | name, true, false); | |
2227 | if (indx == (bfd_size_type) -1) | |
2228 | return false; | |
2229 | defaux.vda_name = indx; | |
2230 | } | |
2231 | defaux.vda_next = 0; | |
2232 | ||
2233 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
2234 | (Elf_External_Verdef *)p); | |
2235 | p += sizeof (Elf_External_Verdef); | |
2236 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
2237 | (Elf_External_Verdaux *) p); | |
2238 | p += sizeof (Elf_External_Verdaux); | |
2239 | ||
2240 | for (t = verdefs; t != NULL; t = t->next) | |
2241 | { | |
2242 | unsigned int cdeps; | |
2243 | struct bfd_elf_version_deps *n; | |
2244 | struct elf_link_hash_entry *h; | |
2245 | ||
2246 | cdeps = 0; | |
2247 | for (n = t->deps; n != NULL; n = n->next) | |
2248 | ++cdeps; | |
2249 | ||
2250 | /* Add a symbol representing this version. */ | |
2251 | h = NULL; | |
2252 | if (! (_bfd_generic_link_add_one_symbol | |
2253 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
2254 | (bfd_vma) 0, (const char *) NULL, false, | |
2255 | get_elf_backend_data (dynobj)->collect, | |
2256 | (struct bfd_link_hash_entry **) &h))) | |
2257 | return false; | |
2258 | h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; | |
2259 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2260 | h->type = STT_OBJECT; | |
2261 | h->verinfo.vertree = t; | |
2262 | ||
d6bfcdb5 ILT |
2263 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) |
2264 | return false; | |
d044b40a ILT |
2265 | |
2266 | def.vd_version = VER_DEF_CURRENT; | |
2267 | def.vd_flags = 0; | |
2268 | if (t->globals == NULL && t->locals == NULL && ! t->used) | |
2269 | def.vd_flags |= VER_FLG_WEAK; | |
2270 | def.vd_ndx = t->vernum + 1; | |
2271 | def.vd_cnt = cdeps + 1; | |
2272 | def.vd_hash = bfd_elf_hash ((const unsigned char *) t->name); | |
2273 | def.vd_aux = sizeof (Elf_External_Verdef); | |
2274 | if (t->next != NULL) | |
2275 | def.vd_next = (sizeof (Elf_External_Verdef) | |
2276 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
2277 | else | |
2278 | def.vd_next = 0; | |
2279 | ||
2280 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
2281 | (Elf_External_Verdef *) p); | |
2282 | p += sizeof (Elf_External_Verdef); | |
2283 | ||
2284 | defaux.vda_name = h->dynstr_index; | |
2285 | if (t->deps == NULL) | |
2286 | defaux.vda_next = 0; | |
2287 | else | |
2288 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
2289 | t->name_indx = defaux.vda_name; | |
2290 | ||
2291 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
2292 | (Elf_External_Verdaux *) p); | |
2293 | p += sizeof (Elf_External_Verdaux); | |
2294 | ||
2295 | for (n = t->deps; n != NULL; n = n->next) | |
2296 | { | |
2297 | defaux.vda_name = n->version_needed->name_indx; | |
2298 | if (n->next == NULL) | |
2299 | defaux.vda_next = 0; | |
2300 | else | |
2301 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
2302 | ||
2303 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
2304 | (Elf_External_Verdaux *) p); | |
2305 | p += sizeof (Elf_External_Verdaux); | |
2306 | } | |
2307 | } | |
2308 | ||
2309 | if (! elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
2310 | || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
2311 | return false; | |
2312 | ||
2313 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
2314 | } | |
2315 | ||
2316 | /* Work out the size of the version reference section. */ | |
2317 | ||
2318 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2319 | BFD_ASSERT (s != NULL); | |
2320 | { | |
2321 | struct elf_find_verdep_info sinfo; | |
2322 | ||
2323 | sinfo.output_bfd = output_bfd; | |
2324 | sinfo.info = info; | |
2325 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
2326 | if (sinfo.vers == 0) | |
2327 | sinfo.vers = 1; | |
2328 | sinfo.failed = false; | |
2329 | ||
2330 | elf_link_hash_traverse (elf_hash_table (info), | |
2331 | elf_link_find_version_dependencies, | |
2332 | (PTR) &sinfo); | |
2333 | ||
2334 | if (elf_tdata (output_bfd)->verref == NULL) | |
2335 | { | |
2336 | asection **spp; | |
2337 | ||
2338 | /* We don't have any version definitions, so we can just | |
2339 | remove the section. */ | |
2340 | ||
2341 | for (spp = &output_bfd->sections; | |
2342 | *spp != s->output_section; | |
2343 | spp = &(*spp)->next) | |
2344 | ; | |
2345 | *spp = s->output_section->next; | |
2346 | --output_bfd->section_count; | |
2347 | } | |
2348 | else | |
2349 | { | |
2350 | Elf_Internal_Verneed *t; | |
2351 | unsigned int size; | |
2352 | unsigned int crefs; | |
2353 | bfd_byte *p; | |
2354 | ||
2355 | /* Build the version definition section. */ | |
d6bfcdb5 ILT |
2356 | size = 0; |
2357 | crefs = 0; | |
d044b40a ILT |
2358 | for (t = elf_tdata (output_bfd)->verref; |
2359 | t != NULL; | |
2360 | t = t->vn_nextref) | |
2361 | { | |
2362 | Elf_Internal_Vernaux *a; | |
2363 | ||
2364 | size += sizeof (Elf_External_Verneed); | |
2365 | ++crefs; | |
2366 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2367 | size += sizeof (Elf_External_Vernaux); | |
2368 | } | |
2369 | ||
2370 | s->_raw_size = size; | |
2371 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, size); | |
2372 | if (s->contents == NULL) | |
2373 | return false; | |
2374 | ||
2375 | p = s->contents; | |
2376 | for (t = elf_tdata (output_bfd)->verref; | |
2377 | t != NULL; | |
2378 | t = t->vn_nextref) | |
2379 | { | |
2380 | unsigned int caux; | |
2381 | Elf_Internal_Vernaux *a; | |
2382 | bfd_size_type indx; | |
2383 | ||
2384 | caux = 0; | |
2385 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2386 | ++caux; | |
2387 | ||
2388 | t->vn_version = VER_NEED_CURRENT; | |
2389 | t->vn_cnt = caux; | |
2390 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2391 | t->vn_bfd->filename, true, false); | |
2392 | if (indx == (bfd_size_type) -1) | |
2393 | return false; | |
2394 | t->vn_file = indx; | |
2395 | t->vn_aux = sizeof (Elf_External_Verneed); | |
2396 | if (t->vn_nextref == NULL) | |
2397 | t->vn_next = 0; | |
2398 | else | |
2399 | t->vn_next = (sizeof (Elf_External_Verneed) | |
2400 | + caux * sizeof (Elf_External_Vernaux)); | |
2401 | ||
2402 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
2403 | (Elf_External_Verneed *) p); | |
2404 | p += sizeof (Elf_External_Verneed); | |
2405 | ||
2406 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2407 | { | |
2408 | a->vna_hash = bfd_elf_hash ((const unsigned char *) | |
2409 | a->vna_nodename); | |
2410 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2411 | a->vna_nodename, true, false); | |
2412 | if (indx == (bfd_size_type) -1) | |
2413 | return false; | |
2414 | a->vna_name = indx; | |
2415 | if (a->vna_nextptr == NULL) | |
2416 | a->vna_next = 0; | |
2417 | else | |
2418 | a->vna_next = sizeof (Elf_External_Vernaux); | |
2419 | ||
2420 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
2421 | (Elf_External_Vernaux *) p); | |
2422 | p += sizeof (Elf_External_Vernaux); | |
2423 | } | |
2424 | } | |
2425 | ||
2426 | if (! elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
2427 | || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
2428 | return false; | |
2429 | ||
2430 | elf_tdata (output_bfd)->cverrefs = crefs; | |
2431 | } | |
2432 | } | |
2433 | ||
2434 | dynsymcount = elf_hash_table (info)->dynsymcount; | |
2435 | ||
2436 | /* Work out the size of the symbol version section. */ | |
2437 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
2438 | BFD_ASSERT (s != NULL); | |
2439 | if (dynsymcount == 0 | |
2440 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) | |
2441 | { | |
2442 | asection **spp; | |
2443 | ||
2444 | /* We don't need any symbol versions; just discard the | |
2445 | section. */ | |
2446 | for (spp = &output_bfd->sections; | |
2447 | *spp != s->output_section; | |
2448 | spp = &(*spp)->next) | |
2449 | ; | |
2450 | *spp = s->output_section->next; | |
2451 | --output_bfd->section_count; | |
2452 | } | |
2453 | else | |
2454 | { | |
d044b40a | 2455 | s->_raw_size = dynsymcount * sizeof (Elf_External_Versym); |
e549b1d2 | 2456 | s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size); |
d044b40a ILT |
2457 | if (s->contents == NULL) |
2458 | return false; | |
2459 | ||
d044b40a ILT |
2460 | if (! elf_add_dynamic_entry (info, DT_VERSYM, 0)) |
2461 | return false; | |
2462 | } | |
2463 | ||
ede4eed4 KR |
2464 | /* Set the size of the .dynsym and .hash sections. We counted |
2465 | the number of dynamic symbols in elf_link_add_object_symbols. | |
2466 | We will build the contents of .dynsym and .hash when we build | |
2467 | the final symbol table, because until then we do not know the | |
2468 | correct value to give the symbols. We built the .dynstr | |
2469 | section as we went along in elf_link_add_object_symbols. */ | |
ede4eed4 KR |
2470 | s = bfd_get_section_by_name (dynobj, ".dynsym"); |
2471 | BFD_ASSERT (s != NULL); | |
2472 | s->_raw_size = dynsymcount * sizeof (Elf_External_Sym); | |
2473 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
2474 | if (s->contents == NULL && s->_raw_size != 0) | |
a9713b91 | 2475 | return false; |
ede4eed4 KR |
2476 | |
2477 | /* The first entry in .dynsym is a dummy symbol. */ | |
2478 | isym.st_value = 0; | |
2479 | isym.st_size = 0; | |
2480 | isym.st_name = 0; | |
2481 | isym.st_info = 0; | |
2482 | isym.st_other = 0; | |
2483 | isym.st_shndx = 0; | |
2484 | elf_swap_symbol_out (output_bfd, &isym, | |
cf9fb9f2 | 2485 | (PTR) (Elf_External_Sym *) s->contents); |
ede4eed4 KR |
2486 | |
2487 | for (i = 0; elf_buckets[i] != 0; i++) | |
2488 | { | |
2489 | bucketcount = elf_buckets[i]; | |
2490 | if (dynsymcount < elf_buckets[i + 1]) | |
2491 | break; | |
2492 | } | |
2493 | ||
2494 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
2495 | BFD_ASSERT (s != NULL); | |
2496 | s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8); | |
2497 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
2498 | if (s->contents == NULL) | |
a9713b91 | 2499 | return false; |
3fe22b98 | 2500 | memset (s->contents, 0, (size_t) s->_raw_size); |
ede4eed4 KR |
2501 | |
2502 | put_word (output_bfd, bucketcount, s->contents); | |
2503 | put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8)); | |
2504 | ||
2505 | elf_hash_table (info)->bucketcount = bucketcount; | |
2506 | ||
2507 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
2508 | BFD_ASSERT (s != NULL); | |
2509 | s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
2510 | ||
2511 | if (! elf_add_dynamic_entry (info, DT_NULL, 0)) | |
2512 | return false; | |
2513 | } | |
2514 | ||
2515 | return true; | |
2516 | } | |
3b3753b8 | 2517 | \f |
ede4eed4 KR |
2518 | /* Make the backend pick a good value for a dynamic symbol. This is |
2519 | called via elf_link_hash_traverse, and also calls itself | |
2520 | recursively. */ | |
2521 | ||
2522 | static boolean | |
2523 | elf_adjust_dynamic_symbol (h, data) | |
2524 | struct elf_link_hash_entry *h; | |
2525 | PTR data; | |
2526 | { | |
2527 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
2528 | bfd *dynobj; | |
2529 | struct elf_backend_data *bed; | |
2530 | ||
e549b1d2 | 2531 | /* Ignore indirect symbols. These are added by the versioning code. */ |
d044b40a ILT |
2532 | if (h->root.type == bfd_link_hash_indirect) |
2533 | return true; | |
2534 | ||
869b7d80 ILT |
2535 | /* If this symbol was mentioned in a non-ELF file, try to set |
2536 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2537 | permit a non-ELF file to correctly refer to a symbol defined in | |
2538 | an ELF dynamic object. */ | |
2539 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) | |
2540 | { | |
2541 | if (h->root.type != bfd_link_hash_defined | |
2542 | && h->root.type != bfd_link_hash_defweak) | |
2543 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2544 | else | |
2545 | { | |
e303e2e3 ILT |
2546 | if (h->root.u.def.section->owner != NULL |
2547 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2548 | == bfd_target_elf_flavour)) | |
869b7d80 ILT |
2549 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; |
2550 | else | |
2551 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2552 | } | |
2553 | ||
2554 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2555 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
2556 | { | |
2557 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2558 | { | |
2559 | eif->failed = true; | |
2560 | return false; | |
2561 | } | |
2562 | } | |
2563 | } | |
2564 | ||
ce6a7731 ILT |
2565 | /* If this is a final link, and the symbol was defined as a common |
2566 | symbol in a regular object file, and there was no definition in | |
2567 | any dynamic object, then the linker will have allocated space for | |
2568 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR | |
2569 | flag will not have been set. */ | |
2570 | if (h->root.type == bfd_link_hash_defined | |
2571 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2572 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
2573 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2574 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
2575 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2576 | ||
951fe66d ILT |
2577 | /* If -Bsymbolic was used (which means to bind references to global |
2578 | symbols to the definition within the shared object), and this | |
2579 | symbol was defined in a regular object, then it actually doesn't | |
2580 | need a PLT entry. */ | |
2581 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 | |
2582 | && eif->info->shared | |
2583 | && eif->info->symbolic | |
2584 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2585 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; | |
2586 | ||
ede4eed4 KR |
2587 | /* If this symbol does not require a PLT entry, and it is not |
2588 | defined by a dynamic object, or is not referenced by a regular | |
452a5efb ILT |
2589 | object, ignore it. We do have to handle a weak defined symbol, |
2590 | even if no regular object refers to it, if we decided to add it | |
2591 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2592 | about symbols which are defined by one dynamic object and | |
2593 | referenced by another one? */ | |
ede4eed4 KR |
2594 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 |
2595 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
2596 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
452a5efb ILT |
2597 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 |
2598 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) | |
ede4eed4 KR |
2599 | return true; |
2600 | ||
2601 | /* If we've already adjusted this symbol, don't do it again. This | |
2602 | can happen via a recursive call. */ | |
2603 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
2604 | return true; | |
2605 | ||
2606 | /* Don't look at this symbol again. Note that we must set this | |
2607 | after checking the above conditions, because we may look at a | |
2608 | symbol once, decide not to do anything, and then get called | |
2609 | recursively later after REF_REGULAR is set below. */ | |
2610 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
2611 | ||
2612 | /* If this is a weak definition, and we know a real definition, and | |
2613 | the real symbol is not itself defined by a regular object file, | |
2614 | then get a good value for the real definition. We handle the | |
2615 | real symbol first, for the convenience of the backend routine. | |
2616 | ||
2617 | Note that there is a confusing case here. If the real definition | |
2618 | is defined by a regular object file, we don't get the real symbol | |
2619 | from the dynamic object, but we do get the weak symbol. If the | |
2620 | processor backend uses a COPY reloc, then if some routine in the | |
2621 | dynamic object changes the real symbol, we will not see that | |
2622 | change in the corresponding weak symbol. This is the way other | |
2623 | ELF linkers work as well, and seems to be a result of the shared | |
2624 | library model. | |
2625 | ||
2626 | I will clarify this issue. Most SVR4 shared libraries define the | |
2627 | variable _timezone and define timezone as a weak synonym. The | |
2628 | tzset call changes _timezone. If you write | |
2629 | extern int timezone; | |
2630 | int _timezone = 5; | |
2631 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2632 | you might expect that, since timezone is a synonym for _timezone, | |
2633 | the same number will print both times. However, if the processor | |
2634 | backend uses a COPY reloc, then actually timezone will be copied | |
2635 | into your process image, and, since you define _timezone | |
2636 | yourself, _timezone will not. Thus timezone and _timezone will | |
2637 | wind up at different memory locations. The tzset call will set | |
2638 | _timezone, leaving timezone unchanged. */ | |
2639 | ||
2640 | if (h->weakdef != NULL) | |
2641 | { | |
2642 | struct elf_link_hash_entry *weakdef; | |
2643 | ||
2644 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2645 | || h->root.type == bfd_link_hash_defweak); | |
2646 | weakdef = h->weakdef; | |
2647 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2648 | || weakdef->root.type == bfd_link_hash_defweak); | |
2649 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
2650 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2651 | { | |
2652 | /* This symbol is defined by a regular object file, so we | |
2653 | will not do anything special. Clear weakdef for the | |
2654 | convenience of the processor backend. */ | |
2655 | h->weakdef = NULL; | |
2656 | } | |
2657 | else | |
2658 | { | |
2659 | /* There is an implicit reference by a regular object file | |
2660 | via the weak symbol. */ | |
2661 | weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2662 | if (! elf_adjust_dynamic_symbol (weakdef, (PTR) eif)) | |
2663 | return false; | |
2664 | } | |
2665 | } | |
2666 | ||
2667 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2668 | bed = get_elf_backend_data (dynobj); | |
2669 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2670 | { | |
2671 | eif->failed = true; | |
2672 | return false; | |
2673 | } | |
2674 | ||
2675 | return true; | |
2676 | } | |
2677 | \f | |
d044b40a ILT |
2678 | /* This routine is used to export all defined symbols into the dynamic |
2679 | symbol table. It is called via elf_link_hash_traverse. */ | |
2680 | ||
2681 | static boolean | |
2682 | elf_export_symbol (h, data) | |
2683 | struct elf_link_hash_entry *h; | |
2684 | PTR data; | |
2685 | { | |
2686 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
2687 | ||
e549b1d2 ILT |
2688 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2689 | if (h->root.type == bfd_link_hash_indirect) | |
2690 | return true; | |
2691 | ||
d044b40a ILT |
2692 | if (h->dynindx == -1 |
2693 | && (h->elf_link_hash_flags | |
2694 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
2695 | { | |
2696 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2697 | { | |
2698 | eif->failed = true; | |
2699 | return false; | |
2700 | } | |
2701 | } | |
2702 | ||
2703 | return true; | |
2704 | } | |
2705 | \f | |
2706 | /* Look through the symbols which are defined in other shared | |
2707 | libraries and referenced here. Update the list of version | |
2708 | dependencies. This will be put into the .gnu.version_r section. | |
2709 | This function is called via elf_link_hash_traverse. */ | |
2710 | ||
2711 | static boolean | |
2712 | elf_link_find_version_dependencies (h, data) | |
2713 | struct elf_link_hash_entry *h; | |
2714 | PTR data; | |
2715 | { | |
2716 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; | |
2717 | Elf_Internal_Verneed *t; | |
2718 | Elf_Internal_Vernaux *a; | |
2719 | ||
2720 | /* We only care about symbols defined in shared objects with version | |
2721 | information. */ | |
2722 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
a48ef404 | 2723 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 |
d044b40a ILT |
2724 | || h->dynindx == -1 |
2725 | || h->verinfo.verdef == NULL) | |
2726 | return true; | |
2727 | ||
2728 | /* See if we already know about this version. */ | |
2729 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
2730 | { | |
2731 | if (t->vn_bfd == h->verinfo.verdef->vd_bfd) | |
2732 | continue; | |
2733 | ||
2734 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2735 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
2736 | return true; | |
2737 | ||
2738 | break; | |
2739 | } | |
2740 | ||
2741 | /* This is a new version. Add it to tree we are building. */ | |
2742 | ||
2743 | if (t == NULL) | |
2744 | { | |
2745 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t); | |
2746 | if (t == NULL) | |
2747 | { | |
2748 | rinfo->failed = true; | |
2749 | return false; | |
2750 | } | |
2751 | ||
2752 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
2753 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
2754 | elf_tdata (rinfo->output_bfd)->verref = t; | |
2755 | } | |
2756 | ||
2757 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a); | |
2758 | ||
2759 | /* Note that we are copying a string pointer here, and testing it | |
2760 | above. If bfd_elf_string_from_elf_section is ever changed to | |
2761 | discard the string data when low in memory, this will have to be | |
2762 | fixed. */ | |
2763 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
2764 | ||
2765 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
2766 | a->vna_nextptr = t->vn_auxptr; | |
2767 | ||
2768 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
2769 | ++rinfo->vers; | |
2770 | ||
2771 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
2772 | ||
2773 | t->vn_auxptr = a; | |
2774 | ||
2775 | return true; | |
2776 | } | |
2777 | ||
2778 | /* Figure out appropriate versions for all the symbols. We may not | |
2779 | have the version number script until we have read all of the input | |
2780 | files, so until that point we don't know which symbols should be | |
2781 | local. This function is called via elf_link_hash_traverse. */ | |
2782 | ||
2783 | static boolean | |
2784 | elf_link_assign_sym_version (h, data) | |
2785 | struct elf_link_hash_entry *h; | |
2786 | PTR data; | |
2787 | { | |
2788 | struct elf_assign_sym_version_info *sinfo = | |
2789 | (struct elf_assign_sym_version_info *) data; | |
2790 | struct bfd_link_info *info = sinfo->info; | |
2791 | char *p; | |
2792 | ||
2793 | /* We only need version numbers for symbols defined in regular | |
2794 | objects. */ | |
2795 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2796 | return true; | |
2797 | ||
2798 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
2799 | if (p != NULL && h->verinfo.vertree == NULL) | |
2800 | { | |
2801 | struct bfd_elf_version_tree *t; | |
2802 | boolean hidden; | |
2803 | ||
2804 | hidden = true; | |
2805 | ||
2806 | /* There are two consecutive ELF_VER_CHR characters if this is | |
2807 | not a hidden symbol. */ | |
2808 | ++p; | |
2809 | if (*p == ELF_VER_CHR) | |
2810 | { | |
2811 | hidden = false; | |
2812 | ++p; | |
2813 | } | |
2814 | ||
2815 | /* If there is no version string, we can just return out. */ | |
2816 | if (*p == '\0') | |
2817 | { | |
2818 | if (hidden) | |
2819 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
2820 | return true; | |
2821 | } | |
2822 | ||
2823 | /* Look for the version. If we find it, it is no longer weak. */ | |
2824 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
2825 | { | |
2826 | if (strcmp (t->name, p) == 0) | |
2827 | { | |
2828 | h->verinfo.vertree = t; | |
2829 | t->used = true; | |
d6bfcdb5 ILT |
2830 | |
2831 | /* See if there is anything to force this symbol to | |
2832 | local scope. */ | |
2833 | if (t->locals != NULL) | |
2834 | { | |
2835 | int len; | |
2836 | char *alc; | |
2837 | struct bfd_elf_version_expr *d; | |
2838 | ||
2839 | len = p - h->root.root.string; | |
2840 | alc = bfd_alloc (sinfo->output_bfd, len); | |
2841 | if (alc == NULL) | |
2842 | return false; | |
2843 | strncpy (alc, h->root.root.string, len - 1); | |
2844 | alc[len - 1] = '\0'; | |
2845 | if (alc[len - 2] == ELF_VER_CHR) | |
2846 | alc[len - 2] = '\0'; | |
2847 | ||
2848 | for (d = t->locals; d != NULL; d = d->next) | |
2849 | { | |
2850 | if ((d->match[0] == '*' && d->match[1] == '\0') | |
2851 | || fnmatch (d->match, alc, 0) == 0) | |
2852 | { | |
2853 | if (h->dynindx != -1 | |
2854 | && info->shared | |
2855 | && ! sinfo->export_dynamic | |
2856 | && (h->elf_link_hash_flags | |
2857 | & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
2858 | { | |
2859 | sinfo->removed_dynamic = true; | |
52c92c7f | 2860 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
d6bfcdb5 ILT |
2861 | h->dynindx = -1; |
2862 | /* FIXME: The name of the symbol has | |
2863 | already been recorded in the dynamic | |
2864 | string table section. */ | |
2865 | } | |
2866 | ||
2867 | break; | |
2868 | } | |
2869 | } | |
2870 | ||
2871 | bfd_release (sinfo->output_bfd, alc); | |
2872 | } | |
2873 | ||
d044b40a ILT |
2874 | break; |
2875 | } | |
2876 | } | |
2877 | ||
d6bfcdb5 ILT |
2878 | /* If we are building an application, we need to create a |
2879 | version node for this version. */ | |
2880 | if (t == NULL && ! info->shared) | |
2881 | { | |
2882 | struct bfd_elf_version_tree **pp; | |
2883 | int version_index; | |
2884 | ||
2885 | /* If we aren't going to export this symbol, we don't need | |
2886 | to worry about it. */ | |
2887 | if (h->dynindx == -1) | |
2888 | return true; | |
2889 | ||
2890 | t = ((struct bfd_elf_version_tree *) | |
2891 | bfd_alloc (sinfo->output_bfd, sizeof *t)); | |
2892 | if (t == NULL) | |
2893 | { | |
2894 | sinfo->failed = true; | |
2895 | return false; | |
2896 | } | |
2897 | ||
2898 | t->next = NULL; | |
2899 | t->name = p; | |
2900 | t->globals = NULL; | |
2901 | t->locals = NULL; | |
2902 | t->deps = NULL; | |
2903 | t->name_indx = (unsigned int) -1; | |
2904 | t->used = true; | |
2905 | ||
2906 | version_index = 1; | |
2907 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
2908 | ++version_index; | |
2909 | t->vernum = version_index; | |
2910 | ||
2911 | *pp = t; | |
2912 | ||
2913 | h->verinfo.vertree = t; | |
2914 | } | |
2915 | else if (t == NULL) | |
d044b40a | 2916 | { |
d6bfcdb5 ILT |
2917 | /* We could not find the version for a symbol when |
2918 | generating a shared archive. Return an error. */ | |
d044b40a | 2919 | (*_bfd_error_handler) |
52c92c7f ILT |
2920 | ("%s: undefined version name %s", |
2921 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); | |
d044b40a ILT |
2922 | bfd_set_error (bfd_error_bad_value); |
2923 | sinfo->failed = true; | |
2924 | return false; | |
2925 | } | |
2926 | ||
2927 | if (hidden) | |
2928 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
2929 | } | |
2930 | ||
2931 | /* If we don't have a version for this symbol, see if we can find | |
2932 | something. */ | |
2933 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
2934 | { | |
2935 | struct bfd_elf_version_tree *t; | |
2936 | struct bfd_elf_version_tree *deflt; | |
2937 | struct bfd_elf_version_expr *d; | |
2938 | ||
2939 | /* See if can find what version this symbol is in. If the | |
2940 | symbol is supposed to eb local, then don't actually register | |
2941 | it. */ | |
2942 | deflt = NULL; | |
2943 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
2944 | { | |
2945 | if (t->globals != NULL) | |
2946 | { | |
2947 | for (d = t->globals; d != NULL; d = d->next) | |
2948 | { | |
2949 | if (fnmatch (d->match, h->root.root.string, 0) == 0) | |
2950 | { | |
2951 | h->verinfo.vertree = t; | |
2952 | break; | |
2953 | } | |
2954 | } | |
2955 | ||
2956 | if (d != NULL) | |
2957 | break; | |
2958 | } | |
2959 | ||
2960 | if (t->locals != NULL) | |
2961 | { | |
2962 | for (d = t->locals; d != NULL; d = d->next) | |
2963 | { | |
2964 | if (d->match[0] == '*' && d->match[1] == '\0') | |
2965 | deflt = t; | |
2966 | else if (fnmatch (d->match, h->root.root.string, 0) == 0) | |
2967 | { | |
2968 | h->verinfo.vertree = t; | |
2969 | if (h->dynindx != -1 | |
2970 | && info->shared | |
2971 | && ! sinfo->export_dynamic | |
2972 | && (h->elf_link_hash_flags | |
2973 | & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
2974 | { | |
2975 | sinfo->removed_dynamic = true; | |
52c92c7f | 2976 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
d044b40a ILT |
2977 | h->dynindx = -1; |
2978 | /* FIXME: The name of the symbol has already | |
2979 | been recorded in the dynamic string table | |
2980 | section. */ | |
2981 | } | |
2982 | break; | |
2983 | } | |
2984 | } | |
2985 | ||
2986 | if (d != NULL) | |
2987 | break; | |
2988 | } | |
2989 | } | |
2990 | ||
2991 | if (deflt != NULL && h->verinfo.vertree == NULL) | |
2992 | { | |
2993 | h->verinfo.vertree = deflt; | |
2994 | if (h->dynindx != -1 | |
2995 | && info->shared | |
2996 | && ! sinfo->export_dynamic | |
2997 | && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0) | |
2998 | { | |
2999 | sinfo->removed_dynamic = true; | |
52c92c7f | 3000 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
d044b40a ILT |
3001 | h->dynindx = -1; |
3002 | /* FIXME: The name of the symbol has already been | |
3003 | recorded in the dynamic string table section. */ | |
3004 | } | |
3005 | } | |
3006 | } | |
3007 | ||
3008 | return true; | |
3009 | } | |
3010 | ||
3011 | /* This function is used to renumber the dynamic symbols, if some of | |
3012 | them are removed because they are marked as local. This is called | |
3013 | via elf_link_hash_traverse. */ | |
3014 | ||
3015 | static boolean | |
3016 | elf_link_renumber_dynsyms (h, data) | |
3017 | struct elf_link_hash_entry *h; | |
3018 | PTR data; | |
3019 | { | |
3020 | struct bfd_link_info *info = (struct bfd_link_info *) data; | |
3021 | ||
3022 | if (h->dynindx != -1) | |
3023 | { | |
3024 | h->dynindx = elf_hash_table (info)->dynsymcount; | |
3025 | ++elf_hash_table (info)->dynsymcount; | |
3026 | } | |
3027 | ||
3028 | return true; | |
3029 | } | |
3030 | \f | |
ede4eed4 KR |
3031 | /* Final phase of ELF linker. */ |
3032 | ||
3033 | /* A structure we use to avoid passing large numbers of arguments. */ | |
3034 | ||
3035 | struct elf_final_link_info | |
3036 | { | |
3037 | /* General link information. */ | |
3038 | struct bfd_link_info *info; | |
3039 | /* Output BFD. */ | |
3040 | bfd *output_bfd; | |
3041 | /* Symbol string table. */ | |
3042 | struct bfd_strtab_hash *symstrtab; | |
3043 | /* .dynsym section. */ | |
3044 | asection *dynsym_sec; | |
3045 | /* .hash section. */ | |
3046 | asection *hash_sec; | |
d044b40a ILT |
3047 | /* symbol version section (.gnu.version). */ |
3048 | asection *symver_sec; | |
ede4eed4 KR |
3049 | /* Buffer large enough to hold contents of any section. */ |
3050 | bfd_byte *contents; | |
3051 | /* Buffer large enough to hold external relocs of any section. */ | |
3052 | PTR external_relocs; | |
3053 | /* Buffer large enough to hold internal relocs of any section. */ | |
3054 | Elf_Internal_Rela *internal_relocs; | |
3055 | /* Buffer large enough to hold external local symbols of any input | |
3056 | BFD. */ | |
3057 | Elf_External_Sym *external_syms; | |
3058 | /* Buffer large enough to hold internal local symbols of any input | |
3059 | BFD. */ | |
3060 | Elf_Internal_Sym *internal_syms; | |
3061 | /* Array large enough to hold a symbol index for each local symbol | |
3062 | of any input BFD. */ | |
3063 | long *indices; | |
3064 | /* Array large enough to hold a section pointer for each local | |
3065 | symbol of any input BFD. */ | |
3066 | asection **sections; | |
3067 | /* Buffer to hold swapped out symbols. */ | |
3068 | Elf_External_Sym *symbuf; | |
3069 | /* Number of swapped out symbols in buffer. */ | |
3070 | size_t symbuf_count; | |
3071 | /* Number of symbols which fit in symbuf. */ | |
3072 | size_t symbuf_size; | |
3073 | }; | |
3074 | ||
3075 | static boolean elf_link_output_sym | |
3076 | PARAMS ((struct elf_final_link_info *, const char *, | |
3077 | Elf_Internal_Sym *, asection *)); | |
3078 | static boolean elf_link_flush_output_syms | |
3079 | PARAMS ((struct elf_final_link_info *)); | |
3080 | static boolean elf_link_output_extsym | |
3081 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
3082 | static boolean elf_link_input_bfd | |
3083 | PARAMS ((struct elf_final_link_info *, bfd *)); | |
3084 | static boolean elf_reloc_link_order | |
3085 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
3086 | struct bfd_link_order *)); | |
3087 | ||
52c92c7f | 3088 | /* This struct is used to pass information to elf_link_output_extsym. */ |
ede4eed4 | 3089 | |
52c92c7f | 3090 | struct elf_outext_info |
ede4eed4 KR |
3091 | { |
3092 | boolean failed; | |
52c92c7f | 3093 | boolean localsyms; |
ede4eed4 | 3094 | struct elf_final_link_info *finfo; |
ff12f303 | 3095 | }; |
ede4eed4 KR |
3096 | |
3097 | /* Do the final step of an ELF link. */ | |
3098 | ||
3099 | boolean | |
3100 | elf_bfd_final_link (abfd, info) | |
3101 | bfd *abfd; | |
3102 | struct bfd_link_info *info; | |
3103 | { | |
3104 | boolean dynamic; | |
3105 | bfd *dynobj; | |
3106 | struct elf_final_link_info finfo; | |
3107 | register asection *o; | |
3108 | register struct bfd_link_order *p; | |
3109 | register bfd *sub; | |
3110 | size_t max_contents_size; | |
3111 | size_t max_external_reloc_size; | |
3112 | size_t max_internal_reloc_count; | |
3113 | size_t max_sym_count; | |
3114 | file_ptr off; | |
3115 | Elf_Internal_Sym elfsym; | |
3116 | unsigned int i; | |
3117 | Elf_Internal_Shdr *symtab_hdr; | |
3118 | Elf_Internal_Shdr *symstrtab_hdr; | |
3119 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
52c92c7f | 3120 | struct elf_outext_info eoinfo; |
ede4eed4 KR |
3121 | |
3122 | if (info->shared) | |
3123 | abfd->flags |= DYNAMIC; | |
3124 | ||
3125 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
3126 | dynobj = elf_hash_table (info)->dynobj; | |
3127 | ||
3128 | finfo.info = info; | |
3129 | finfo.output_bfd = abfd; | |
3130 | finfo.symstrtab = elf_stringtab_init (); | |
3131 | if (finfo.symstrtab == NULL) | |
3132 | return false; | |
d044b40a | 3133 | |
ede4eed4 KR |
3134 | if (! dynamic) |
3135 | { | |
3136 | finfo.dynsym_sec = NULL; | |
3137 | finfo.hash_sec = NULL; | |
d044b40a | 3138 | finfo.symver_sec = NULL; |
ede4eed4 KR |
3139 | } |
3140 | else | |
3141 | { | |
3142 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
3143 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
3144 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
d044b40a ILT |
3145 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
3146 | /* Note that it is OK if symver_sec is NULL. */ | |
ede4eed4 | 3147 | } |
d044b40a | 3148 | |
ede4eed4 KR |
3149 | finfo.contents = NULL; |
3150 | finfo.external_relocs = NULL; | |
3151 | finfo.internal_relocs = NULL; | |
3152 | finfo.external_syms = NULL; | |
3153 | finfo.internal_syms = NULL; | |
3154 | finfo.indices = NULL; | |
3155 | finfo.sections = NULL; | |
3156 | finfo.symbuf = NULL; | |
3157 | finfo.symbuf_count = 0; | |
3158 | ||
3159 | /* Count up the number of relocations we will output for each output | |
3160 | section, so that we know the sizes of the reloc sections. We | |
3161 | also figure out some maximum sizes. */ | |
3162 | max_contents_size = 0; | |
3163 | max_external_reloc_size = 0; | |
3164 | max_internal_reloc_count = 0; | |
3165 | max_sym_count = 0; | |
3166 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
3167 | { | |
3168 | o->reloc_count = 0; | |
3169 | ||
3170 | for (p = o->link_order_head; p != NULL; p = p->next) | |
3171 | { | |
3172 | if (p->type == bfd_section_reloc_link_order | |
3173 | || p->type == bfd_symbol_reloc_link_order) | |
3174 | ++o->reloc_count; | |
3175 | else if (p->type == bfd_indirect_link_order) | |
3176 | { | |
3177 | asection *sec; | |
3178 | ||
3179 | sec = p->u.indirect.section; | |
3180 | ||
7ec49f91 ILT |
3181 | /* Mark all sections which are to be included in the |
3182 | link. This will normally be every section. We need | |
3183 | to do this so that we can identify any sections which | |
3184 | the linker has decided to not include. */ | |
ff0e4a93 | 3185 | sec->linker_mark = true; |
7ec49f91 | 3186 | |
ede4eed4 KR |
3187 | if (info->relocateable) |
3188 | o->reloc_count += sec->reloc_count; | |
3189 | ||
3190 | if (sec->_raw_size > max_contents_size) | |
3191 | max_contents_size = sec->_raw_size; | |
3192 | if (sec->_cooked_size > max_contents_size) | |
3193 | max_contents_size = sec->_cooked_size; | |
3194 | ||
3195 | /* We are interested in just local symbols, not all | |
3196 | symbols. */ | |
d044b40a ILT |
3197 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
3198 | && (sec->owner->flags & DYNAMIC) == 0) | |
ede4eed4 KR |
3199 | { |
3200 | size_t sym_count; | |
3201 | ||
3202 | if (elf_bad_symtab (sec->owner)) | |
3203 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
3204 | / sizeof (Elf_External_Sym)); | |
3205 | else | |
3206 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
3207 | ||
3208 | if (sym_count > max_sym_count) | |
3209 | max_sym_count = sym_count; | |
3210 | ||
3211 | if ((sec->flags & SEC_RELOC) != 0) | |
3212 | { | |
3213 | size_t ext_size; | |
3214 | ||
3215 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
3216 | if (ext_size > max_external_reloc_size) | |
3217 | max_external_reloc_size = ext_size; | |
3218 | if (sec->reloc_count > max_internal_reloc_count) | |
3219 | max_internal_reloc_count = sec->reloc_count; | |
3220 | } | |
3221 | } | |
3222 | } | |
3223 | } | |
3224 | ||
3225 | if (o->reloc_count > 0) | |
3226 | o->flags |= SEC_RELOC; | |
3227 | else | |
3228 | { | |
3229 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
3230 | set it (this is probably a bug) and if it is set | |
3231 | assign_section_numbers will create a reloc section. */ | |
3232 | o->flags &=~ SEC_RELOC; | |
3233 | } | |
3234 | ||
3235 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
3236 | zero. This is done in elf_fake_sections as well, but forcing | |
3237 | the VMA to 0 here will ensure that relocs against these | |
3238 | sections are handled correctly. */ | |
2e0567eb ILT |
3239 | if ((o->flags & SEC_ALLOC) == 0 |
3240 | && ! o->user_set_vma) | |
ede4eed4 KR |
3241 | o->vma = 0; |
3242 | } | |
3243 | ||
3244 | /* Figure out the file positions for everything but the symbol table | |
3245 | and the relocs. We set symcount to force assign_section_numbers | |
3246 | to create a symbol table. */ | |
3247 | abfd->symcount = info->strip == strip_all ? 0 : 1; | |
3248 | BFD_ASSERT (! abfd->output_has_begun); | |
3249 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
3250 | goto error_return; | |
3251 | ||
3252 | /* That created the reloc sections. Set their sizes, and assign | |
3253 | them file positions, and allocate some buffers. */ | |
3254 | for (o = abfd->sections; o != NULL; o = o->next) | |
3255 | { | |
3256 | if ((o->flags & SEC_RELOC) != 0) | |
3257 | { | |
3258 | Elf_Internal_Shdr *rel_hdr; | |
3259 | register struct elf_link_hash_entry **p, **pend; | |
3260 | ||
3261 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
3262 | ||
3263 | rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count; | |
3264 | ||
3265 | /* The contents field must last into write_object_contents, | |
3266 | so we allocate it with bfd_alloc rather than malloc. */ | |
3267 | rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size); | |
3268 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) | |
a9713b91 | 3269 | goto error_return; |
ede4eed4 KR |
3270 | |
3271 | p = ((struct elf_link_hash_entry **) | |
58142f10 ILT |
3272 | bfd_malloc (o->reloc_count |
3273 | * sizeof (struct elf_link_hash_entry *))); | |
ede4eed4 | 3274 | if (p == NULL && o->reloc_count != 0) |
58142f10 | 3275 | goto error_return; |
ede4eed4 KR |
3276 | elf_section_data (o)->rel_hashes = p; |
3277 | pend = p + o->reloc_count; | |
3278 | for (; p < pend; p++) | |
3279 | *p = NULL; | |
3280 | ||
3281 | /* Use the reloc_count field as an index when outputting the | |
3282 | relocs. */ | |
3283 | o->reloc_count = 0; | |
3284 | } | |
3285 | } | |
3286 | ||
3287 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
3288 | ||
3289 | /* We have now assigned file positions for all the sections except | |
3290 | .symtab and .strtab. We start the .symtab section at the current | |
3291 | file position, and write directly to it. We build the .strtab | |
ab276dfa | 3292 | section in memory. */ |
ede4eed4 KR |
3293 | abfd->symcount = 0; |
3294 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
3295 | /* sh_name is set in prep_headers. */ | |
3296 | symtab_hdr->sh_type = SHT_SYMTAB; | |
3297 | symtab_hdr->sh_flags = 0; | |
3298 | symtab_hdr->sh_addr = 0; | |
3299 | symtab_hdr->sh_size = 0; | |
3300 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
3301 | /* sh_link is set in assign_section_numbers. */ | |
3302 | /* sh_info is set below. */ | |
3303 | /* sh_offset is set just below. */ | |
3304 | symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */ | |
3305 | ||
3306 | off = elf_tdata (abfd)->next_file_pos; | |
3307 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true); | |
3308 | ||
3309 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
3310 | incorrect. We do not yet know the size of the .symtab section. | |
3311 | We correct next_file_pos below, after we do know the size. */ | |
3312 | ||
3313 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
3314 | continuously seeking to the right position in the file. */ | |
3315 | if (! info->keep_memory || max_sym_count < 20) | |
3316 | finfo.symbuf_size = 20; | |
3317 | else | |
3318 | finfo.symbuf_size = max_sym_count; | |
3319 | finfo.symbuf = ((Elf_External_Sym *) | |
58142f10 | 3320 | bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym))); |
ede4eed4 | 3321 | if (finfo.symbuf == NULL) |
58142f10 | 3322 | goto error_return; |
ede4eed4 KR |
3323 | |
3324 | /* Start writing out the symbol table. The first symbol is always a | |
3325 | dummy symbol. */ | |
28c16b55 ILT |
3326 | if (info->strip != strip_all || info->relocateable) |
3327 | { | |
3328 | elfsym.st_value = 0; | |
3329 | elfsym.st_size = 0; | |
3330 | elfsym.st_info = 0; | |
3331 | elfsym.st_other = 0; | |
3332 | elfsym.st_shndx = SHN_UNDEF; | |
3333 | if (! elf_link_output_sym (&finfo, (const char *) NULL, | |
3334 | &elfsym, bfd_und_section_ptr)) | |
3335 | goto error_return; | |
3336 | } | |
ede4eed4 KR |
3337 | |
3338 | #if 0 | |
3339 | /* Some standard ELF linkers do this, but we don't because it causes | |
3340 | bootstrap comparison failures. */ | |
3341 | /* Output a file symbol for the output file as the second symbol. | |
3342 | We output this even if we are discarding local symbols, although | |
3343 | I'm not sure if this is correct. */ | |
3344 | elfsym.st_value = 0; | |
3345 | elfsym.st_size = 0; | |
3346 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
3347 | elfsym.st_other = 0; | |
3348 | elfsym.st_shndx = SHN_ABS; | |
3349 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
3350 | &elfsym, bfd_abs_section_ptr)) | |
3351 | goto error_return; | |
3352 | #endif | |
3353 | ||
3354 | /* Output a symbol for each section. We output these even if we are | |
3355 | discarding local symbols, since they are used for relocs. These | |
3356 | symbols have no names. We store the index of each one in the | |
3357 | index field of the section, so that we can find it again when | |
3358 | outputting relocs. */ | |
28c16b55 | 3359 | if (info->strip != strip_all || info->relocateable) |
ede4eed4 | 3360 | { |
28c16b55 ILT |
3361 | elfsym.st_size = 0; |
3362 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
3363 | elfsym.st_other = 0; | |
3364 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) | |
3365 | { | |
3366 | o = section_from_elf_index (abfd, i); | |
3367 | if (o != NULL) | |
3368 | o->target_index = abfd->symcount; | |
3369 | elfsym.st_shndx = i; | |
34bc6ffc ILT |
3370 | if (info->relocateable || o == NULL) |
3371 | elfsym.st_value = 0; | |
3372 | else | |
3373 | elfsym.st_value = o->vma; | |
28c16b55 ILT |
3374 | if (! elf_link_output_sym (&finfo, (const char *) NULL, |
3375 | &elfsym, o)) | |
3376 | goto error_return; | |
3377 | } | |
ede4eed4 KR |
3378 | } |
3379 | ||
3380 | /* Allocate some memory to hold information read in from the input | |
3381 | files. */ | |
58142f10 ILT |
3382 | finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
3383 | finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size); | |
ede4eed4 | 3384 | finfo.internal_relocs = ((Elf_Internal_Rela *) |
58142f10 ILT |
3385 | bfd_malloc (max_internal_reloc_count |
3386 | * sizeof (Elf_Internal_Rela))); | |
ede4eed4 | 3387 | finfo.external_syms = ((Elf_External_Sym *) |
58142f10 ILT |
3388 | bfd_malloc (max_sym_count |
3389 | * sizeof (Elf_External_Sym))); | |
ede4eed4 | 3390 | finfo.internal_syms = ((Elf_Internal_Sym *) |
58142f10 ILT |
3391 | bfd_malloc (max_sym_count |
3392 | * sizeof (Elf_Internal_Sym))); | |
3393 | finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long)); | |
3394 | finfo.sections = ((asection **) | |
3395 | bfd_malloc (max_sym_count * sizeof (asection *))); | |
ede4eed4 KR |
3396 | if ((finfo.contents == NULL && max_contents_size != 0) |
3397 | || (finfo.external_relocs == NULL && max_external_reloc_size != 0) | |
3398 | || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0) | |
3399 | || (finfo.external_syms == NULL && max_sym_count != 0) | |
3400 | || (finfo.internal_syms == NULL && max_sym_count != 0) | |
3401 | || (finfo.indices == NULL && max_sym_count != 0) | |
3402 | || (finfo.sections == NULL && max_sym_count != 0)) | |
58142f10 | 3403 | goto error_return; |
ede4eed4 KR |
3404 | |
3405 | /* Since ELF permits relocations to be against local symbols, we | |
3406 | must have the local symbols available when we do the relocations. | |
3407 | Since we would rather only read the local symbols once, and we | |
3408 | would rather not keep them in memory, we handle all the | |
3409 | relocations for a single input file at the same time. | |
3410 | ||
3411 | Unfortunately, there is no way to know the total number of local | |
3412 | symbols until we have seen all of them, and the local symbol | |
3413 | indices precede the global symbol indices. This means that when | |
3414 | we are generating relocateable output, and we see a reloc against | |
3415 | a global symbol, we can not know the symbol index until we have | |
3416 | finished examining all the local symbols to see which ones we are | |
3417 | going to output. To deal with this, we keep the relocations in | |
3418 | memory, and don't output them until the end of the link. This is | |
3419 | an unfortunate waste of memory, but I don't see a good way around | |
3420 | it. Fortunately, it only happens when performing a relocateable | |
3421 | link, which is not the common case. FIXME: If keep_memory is set | |
3422 | we could write the relocs out and then read them again; I don't | |
3423 | know how bad the memory loss will be. */ | |
3424 | ||
3425 | for (sub = info->input_bfds; sub != NULL; sub = sub->next) | |
3426 | sub->output_has_begun = false; | |
3427 | for (o = abfd->sections; o != NULL; o = o->next) | |
3428 | { | |
3429 | for (p = o->link_order_head; p != NULL; p = p->next) | |
3430 | { | |
3431 | if (p->type == bfd_indirect_link_order | |
3432 | && (bfd_get_flavour (p->u.indirect.section->owner) | |
3433 | == bfd_target_elf_flavour)) | |
3434 | { | |
3435 | sub = p->u.indirect.section->owner; | |
3436 | if (! sub->output_has_begun) | |
3437 | { | |
3438 | if (! elf_link_input_bfd (&finfo, sub)) | |
3439 | goto error_return; | |
3440 | sub->output_has_begun = true; | |
3441 | } | |
3442 | } | |
3443 | else if (p->type == bfd_section_reloc_link_order | |
3444 | || p->type == bfd_symbol_reloc_link_order) | |
3445 | { | |
3446 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
3447 | goto error_return; | |
3448 | } | |
3449 | else | |
3450 | { | |
3451 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
3452 | goto error_return; | |
3453 | } | |
3454 | } | |
3455 | } | |
3456 | ||
3457 | /* That wrote out all the local symbols. Finish up the symbol table | |
3458 | with the global symbols. */ | |
3459 | ||
52c92c7f ILT |
3460 | if (info->strip != strip_all && info->shared) |
3461 | { | |
3462 | /* Output any global symbols that got converted to local in a | |
3463 | version script. We do this in a separate step since ELF | |
3464 | requires all local symbols to appear prior to any global | |
3465 | symbols. FIXME: We should only do this if some global | |
3466 | symbols were, in fact, converted to become local. FIXME: | |
3467 | Will this work correctly with the Irix 5 linker? */ | |
3468 | eoinfo.failed = false; | |
3469 | eoinfo.finfo = &finfo; | |
3470 | eoinfo.localsyms = true; | |
3471 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
3472 | (PTR) &eoinfo); | |
3473 | if (eoinfo.failed) | |
3474 | return false; | |
3475 | } | |
3476 | ||
ede4eed4 KR |
3477 | /* The sh_info field records the index of the first non local |
3478 | symbol. */ | |
3479 | symtab_hdr->sh_info = abfd->symcount; | |
3480 | if (dynamic) | |
3481 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1; | |
3482 | ||
3483 | /* We get the global symbols from the hash table. */ | |
52c92c7f ILT |
3484 | eoinfo.failed = false; |
3485 | eoinfo.localsyms = false; | |
3486 | eoinfo.finfo = &finfo; | |
ede4eed4 | 3487 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, |
52c92c7f ILT |
3488 | (PTR) &eoinfo); |
3489 | if (eoinfo.failed) | |
ede4eed4 KR |
3490 | return false; |
3491 | ||
3492 | /* Flush all symbols to the file. */ | |
3493 | if (! elf_link_flush_output_syms (&finfo)) | |
3494 | return false; | |
3495 | ||
3496 | /* Now we know the size of the symtab section. */ | |
3497 | off += symtab_hdr->sh_size; | |
3498 | ||
3499 | /* Finish up and write out the symbol string table (.strtab) | |
3500 | section. */ | |
3501 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
3502 | /* sh_name was set in prep_headers. */ | |
3503 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
3504 | symstrtab_hdr->sh_flags = 0; | |
3505 | symstrtab_hdr->sh_addr = 0; | |
3506 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
3507 | symstrtab_hdr->sh_entsize = 0; | |
3508 | symstrtab_hdr->sh_link = 0; | |
3509 | symstrtab_hdr->sh_info = 0; | |
3510 | /* sh_offset is set just below. */ | |
3511 | symstrtab_hdr->sh_addralign = 1; | |
3512 | ||
3513 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true); | |
3514 | elf_tdata (abfd)->next_file_pos = off; | |
3515 | ||
28c16b55 ILT |
3516 | if (abfd->symcount > 0) |
3517 | { | |
3518 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
3519 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
3520 | return false; | |
3521 | } | |
ede4eed4 KR |
3522 | |
3523 | /* Adjust the relocs to have the correct symbol indices. */ | |
3524 | for (o = abfd->sections; o != NULL; o = o->next) | |
3525 | { | |
3526 | struct elf_link_hash_entry **rel_hash; | |
3527 | Elf_Internal_Shdr *rel_hdr; | |
3528 | ||
3529 | if ((o->flags & SEC_RELOC) == 0) | |
3530 | continue; | |
3531 | ||
3532 | rel_hash = elf_section_data (o)->rel_hashes; | |
3533 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
3534 | for (i = 0; i < o->reloc_count; i++, rel_hash++) | |
3535 | { | |
3536 | if (*rel_hash == NULL) | |
3537 | continue; | |
ff12f303 | 3538 | |
ede4eed4 KR |
3539 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
3540 | ||
3541 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
3542 | { | |
3543 | Elf_External_Rel *erel; | |
3544 | Elf_Internal_Rel irel; | |
3545 | ||
3546 | erel = (Elf_External_Rel *) rel_hdr->contents + i; | |
3547 | elf_swap_reloc_in (abfd, erel, &irel); | |
3548 | irel.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
3549 | ELF_R_TYPE (irel.r_info)); | |
3550 | elf_swap_reloc_out (abfd, &irel, erel); | |
3551 | } | |
3552 | else | |
3553 | { | |
3554 | Elf_External_Rela *erela; | |
3555 | Elf_Internal_Rela irela; | |
3556 | ||
3557 | BFD_ASSERT (rel_hdr->sh_entsize | |
3558 | == sizeof (Elf_External_Rela)); | |
3559 | ||
3560 | erela = (Elf_External_Rela *) rel_hdr->contents + i; | |
3561 | elf_swap_reloca_in (abfd, erela, &irela); | |
3562 | irela.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
3563 | ELF_R_TYPE (irela.r_info)); | |
3564 | elf_swap_reloca_out (abfd, &irela, erela); | |
3565 | } | |
3566 | } | |
3567 | ||
3568 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
3569 | trying to swap the relocs out itself. */ | |
3570 | o->reloc_count = 0; | |
3571 | } | |
3572 | ||
3573 | /* If we are linking against a dynamic object, or generating a | |
3574 | shared library, finish up the dynamic linking information. */ | |
3575 | if (dynamic) | |
3576 | { | |
3577 | Elf_External_Dyn *dyncon, *dynconend; | |
3578 | ||
3579 | /* Fix up .dynamic entries. */ | |
3580 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3581 | BFD_ASSERT (o != NULL); | |
3582 | ||
3583 | dyncon = (Elf_External_Dyn *) o->contents; | |
3584 | dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size); | |
3585 | for (; dyncon < dynconend; dyncon++) | |
3586 | { | |
3587 | Elf_Internal_Dyn dyn; | |
3588 | const char *name; | |
3589 | unsigned int type; | |
3590 | ||
3591 | elf_swap_dyn_in (dynobj, dyncon, &dyn); | |
3592 | ||
3593 | switch (dyn.d_tag) | |
3594 | { | |
3595 | default: | |
3596 | break; | |
3597 | ||
3598 | /* SVR4 linkers seem to set DT_INIT and DT_FINI based on | |
3599 | magic _init and _fini symbols. This is pretty ugly, | |
3600 | but we are compatible. */ | |
3601 | case DT_INIT: | |
3602 | name = "_init"; | |
3603 | goto get_sym; | |
3604 | case DT_FINI: | |
3605 | name = "_fini"; | |
3606 | get_sym: | |
3607 | { | |
3608 | struct elf_link_hash_entry *h; | |
3609 | ||
3610 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
3611 | false, false, true); | |
d6f672b8 ILT |
3612 | if (h != NULL |
3613 | && (h->root.type == bfd_link_hash_defined | |
3614 | || h->root.type == bfd_link_hash_defweak)) | |
ede4eed4 KR |
3615 | { |
3616 | dyn.d_un.d_val = h->root.u.def.value; | |
3617 | o = h->root.u.def.section; | |
3618 | if (o->output_section != NULL) | |
3619 | dyn.d_un.d_val += (o->output_section->vma | |
3620 | + o->output_offset); | |
3621 | else | |
d6f672b8 ILT |
3622 | { |
3623 | /* The symbol is imported from another shared | |
3624 | library and does not apply to this one. */ | |
3625 | dyn.d_un.d_val = 0; | |
3626 | } | |
3627 | ||
3628 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
ede4eed4 | 3629 | } |
ede4eed4 KR |
3630 | } |
3631 | break; | |
3632 | ||
3633 | case DT_HASH: | |
3634 | name = ".hash"; | |
3635 | goto get_vma; | |
3636 | case DT_STRTAB: | |
3637 | name = ".dynstr"; | |
3638 | goto get_vma; | |
3639 | case DT_SYMTAB: | |
3640 | name = ".dynsym"; | |
d044b40a ILT |
3641 | goto get_vma; |
3642 | case DT_VERDEF: | |
3643 | name = ".gnu.version_d"; | |
3644 | goto get_vma; | |
3645 | case DT_VERNEED: | |
3646 | name = ".gnu.version_r"; | |
3647 | goto get_vma; | |
3648 | case DT_VERSYM: | |
3649 | name = ".gnu.version"; | |
ede4eed4 KR |
3650 | get_vma: |
3651 | o = bfd_get_section_by_name (abfd, name); | |
3652 | BFD_ASSERT (o != NULL); | |
3653 | dyn.d_un.d_ptr = o->vma; | |
3654 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
3655 | break; | |
3656 | ||
3657 | case DT_REL: | |
3658 | case DT_RELA: | |
3659 | case DT_RELSZ: | |
3660 | case DT_RELASZ: | |
3661 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
3662 | type = SHT_REL; | |
3663 | else | |
3664 | type = SHT_RELA; | |
3665 | dyn.d_un.d_val = 0; | |
3666 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) | |
3667 | { | |
3668 | Elf_Internal_Shdr *hdr; | |
3669 | ||
3670 | hdr = elf_elfsections (abfd)[i]; | |
3671 | if (hdr->sh_type == type | |
3672 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
3673 | { | |
3674 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
3675 | dyn.d_un.d_val += hdr->sh_size; | |
3676 | else | |
3677 | { | |
3678 | if (dyn.d_un.d_val == 0 | |
3679 | || hdr->sh_addr < dyn.d_un.d_val) | |
3680 | dyn.d_un.d_val = hdr->sh_addr; | |
3681 | } | |
3682 | } | |
3683 | } | |
3684 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
3685 | break; | |
3686 | } | |
3687 | } | |
3688 | } | |
3689 | ||
3690 | /* If we have created any dynamic sections, then output them. */ | |
3691 | if (dynobj != NULL) | |
3692 | { | |
3693 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
3694 | goto error_return; | |
3695 | ||
3696 | for (o = dynobj->sections; o != NULL; o = o->next) | |
3697 | { | |
3698 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
3699 | || o->_raw_size == 0) | |
3700 | continue; | |
ff12f303 | 3701 | if ((o->flags & SEC_LINKER_CREATED) == 0) |
ede4eed4 KR |
3702 | { |
3703 | /* At this point, we are only interested in sections | |
ff12f303 | 3704 | created by elf_link_create_dynamic_sections. */ |
ede4eed4 KR |
3705 | continue; |
3706 | } | |
3707 | if ((elf_section_data (o->output_section)->this_hdr.sh_type | |
3708 | != SHT_STRTAB) | |
3709 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
3710 | { | |
3711 | if (! bfd_set_section_contents (abfd, o->output_section, | |
3712 | o->contents, o->output_offset, | |
3713 | o->_raw_size)) | |
3714 | goto error_return; | |
3715 | } | |
3716 | else | |
3717 | { | |
3718 | file_ptr off; | |
3719 | ||
3720 | /* The contents of the .dynstr section are actually in a | |
3721 | stringtab. */ | |
3722 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
3723 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
3724 | || ! _bfd_stringtab_emit (abfd, | |
3725 | elf_hash_table (info)->dynstr)) | |
3726 | goto error_return; | |
3727 | } | |
3728 | } | |
3729 | } | |
3730 | ||
1726b8f0 ILT |
3731 | /* If we have optimized stabs strings, output them. */ |
3732 | if (elf_hash_table (info)->stab_info != NULL) | |
3733 | { | |
3734 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
3735 | goto error_return; | |
3736 | } | |
3737 | ||
ede4eed4 KR |
3738 | if (finfo.symstrtab != NULL) |
3739 | _bfd_stringtab_free (finfo.symstrtab); | |
3740 | if (finfo.contents != NULL) | |
3741 | free (finfo.contents); | |
3742 | if (finfo.external_relocs != NULL) | |
3743 | free (finfo.external_relocs); | |
3744 | if (finfo.internal_relocs != NULL) | |
3745 | free (finfo.internal_relocs); | |
3746 | if (finfo.external_syms != NULL) | |
3747 | free (finfo.external_syms); | |
3748 | if (finfo.internal_syms != NULL) | |
3749 | free (finfo.internal_syms); | |
3750 | if (finfo.indices != NULL) | |
3751 | free (finfo.indices); | |
3752 | if (finfo.sections != NULL) | |
3753 | free (finfo.sections); | |
3754 | if (finfo.symbuf != NULL) | |
3755 | free (finfo.symbuf); | |
3756 | for (o = abfd->sections; o != NULL; o = o->next) | |
3757 | { | |
3758 | if ((o->flags & SEC_RELOC) != 0 | |
3759 | && elf_section_data (o)->rel_hashes != NULL) | |
3760 | free (elf_section_data (o)->rel_hashes); | |
3761 | } | |
3762 | ||
3763 | elf_tdata (abfd)->linker = true; | |
3764 | ||
3765 | return true; | |
3766 | ||
3767 | error_return: | |
3768 | if (finfo.symstrtab != NULL) | |
3769 | _bfd_stringtab_free (finfo.symstrtab); | |
3770 | if (finfo.contents != NULL) | |
3771 | free (finfo.contents); | |
3772 | if (finfo.external_relocs != NULL) | |
3773 | free (finfo.external_relocs); | |
3774 | if (finfo.internal_relocs != NULL) | |
3775 | free (finfo.internal_relocs); | |
3776 | if (finfo.external_syms != NULL) | |
3777 | free (finfo.external_syms); | |
3778 | if (finfo.internal_syms != NULL) | |
3779 | free (finfo.internal_syms); | |
3780 | if (finfo.indices != NULL) | |
3781 | free (finfo.indices); | |
3782 | if (finfo.sections != NULL) | |
3783 | free (finfo.sections); | |
3784 | if (finfo.symbuf != NULL) | |
3785 | free (finfo.symbuf); | |
3786 | for (o = abfd->sections; o != NULL; o = o->next) | |
3787 | { | |
3788 | if ((o->flags & SEC_RELOC) != 0 | |
3789 | && elf_section_data (o)->rel_hashes != NULL) | |
3790 | free (elf_section_data (o)->rel_hashes); | |
3791 | } | |
3792 | ||
3793 | return false; | |
3794 | } | |
3795 | ||
3796 | /* Add a symbol to the output symbol table. */ | |
3797 | ||
3798 | static boolean | |
3799 | elf_link_output_sym (finfo, name, elfsym, input_sec) | |
3800 | struct elf_final_link_info *finfo; | |
3801 | const char *name; | |
3802 | Elf_Internal_Sym *elfsym; | |
3803 | asection *input_sec; | |
3804 | { | |
3805 | boolean (*output_symbol_hook) PARAMS ((bfd *, | |
3806 | struct bfd_link_info *info, | |
3807 | const char *, | |
3808 | Elf_Internal_Sym *, | |
3809 | asection *)); | |
3810 | ||
3811 | output_symbol_hook = get_elf_backend_data (finfo->output_bfd)-> | |
3812 | elf_backend_link_output_symbol_hook; | |
3813 | if (output_symbol_hook != NULL) | |
3814 | { | |
3815 | if (! ((*output_symbol_hook) | |
3816 | (finfo->output_bfd, finfo->info, name, elfsym, input_sec))) | |
3817 | return false; | |
3818 | } | |
3819 | ||
3820 | if (name == (const char *) NULL || *name == '\0') | |
3821 | elfsym->st_name = 0; | |
3822 | else | |
3823 | { | |
3824 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
3825 | name, true, | |
3826 | false); | |
3827 | if (elfsym->st_name == (unsigned long) -1) | |
3828 | return false; | |
3829 | } | |
3830 | ||
3831 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
3832 | { | |
3833 | if (! elf_link_flush_output_syms (finfo)) | |
3834 | return false; | |
3835 | } | |
3836 | ||
3837 | elf_swap_symbol_out (finfo->output_bfd, elfsym, | |
cf9fb9f2 | 3838 | (PTR) (finfo->symbuf + finfo->symbuf_count)); |
ede4eed4 KR |
3839 | ++finfo->symbuf_count; |
3840 | ||
3841 | ++finfo->output_bfd->symcount; | |
3842 | ||
3843 | return true; | |
3844 | } | |
3845 | ||
3846 | /* Flush the output symbols to the file. */ | |
3847 | ||
3848 | static boolean | |
3849 | elf_link_flush_output_syms (finfo) | |
3850 | struct elf_final_link_info *finfo; | |
3851 | { | |
28c16b55 ILT |
3852 | if (finfo->symbuf_count > 0) |
3853 | { | |
3854 | Elf_Internal_Shdr *symtab; | |
ede4eed4 | 3855 | |
28c16b55 | 3856 | symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr; |
ede4eed4 | 3857 | |
28c16b55 ILT |
3858 | if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size, |
3859 | SEEK_SET) != 0 | |
3860 | || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count, | |
3861 | sizeof (Elf_External_Sym), finfo->output_bfd) | |
3862 | != finfo->symbuf_count * sizeof (Elf_External_Sym))) | |
3863 | return false; | |
ede4eed4 | 3864 | |
28c16b55 | 3865 | symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym); |
ede4eed4 | 3866 | |
28c16b55 ILT |
3867 | finfo->symbuf_count = 0; |
3868 | } | |
ede4eed4 KR |
3869 | |
3870 | return true; | |
3871 | } | |
3872 | ||
3873 | /* Add an external symbol to the symbol table. This is called from | |
52c92c7f ILT |
3874 | the hash table traversal routine. When generating a shared object, |
3875 | we go through the symbol table twice. The first time we output | |
3876 | anything that might have been forced to local scope in a version | |
3877 | script. The second time we output the symbols that are still | |
3878 | global symbols. */ | |
ede4eed4 KR |
3879 | |
3880 | static boolean | |
3881 | elf_link_output_extsym (h, data) | |
3882 | struct elf_link_hash_entry *h; | |
3883 | PTR data; | |
3884 | { | |
52c92c7f ILT |
3885 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
3886 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
ede4eed4 KR |
3887 | boolean strip; |
3888 | Elf_Internal_Sym sym; | |
3889 | asection *input_sec; | |
3890 | ||
52c92c7f ILT |
3891 | /* Decide whether to output this symbol in this pass. */ |
3892 | if (eoinfo->localsyms) | |
3893 | { | |
3894 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
3895 | return true; | |
3896 | } | |
3897 | else | |
3898 | { | |
3899 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
3900 | return true; | |
3901 | } | |
3902 | ||
ede4eed4 KR |
3903 | /* If we are not creating a shared library, and this symbol is |
3904 | referenced by a shared library but is not defined anywhere, then | |
3905 | warn that it is undefined. If we do not do this, the runtime | |
3906 | linker will complain that the symbol is undefined when the | |
3907 | program is run. We don't have to worry about symbols that are | |
3908 | referenced by regular files, because we will already have issued | |
252239f8 | 3909 | warnings for them. */ |
ede4eed4 KR |
3910 | if (! finfo->info->relocateable |
3911 | && ! finfo->info->shared | |
3912 | && h->root.type == bfd_link_hash_undefined | |
3913 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0 | |
252239f8 | 3914 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) |
ede4eed4 KR |
3915 | { |
3916 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
3917 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
3918 | (asection *) NULL, 0))) | |
3919 | { | |
52c92c7f | 3920 | eoinfo->failed = true; |
ede4eed4 KR |
3921 | return false; |
3922 | } | |
3923 | } | |
3924 | ||
3925 | /* We don't want to output symbols that have never been mentioned by | |
3926 | a regular file, or that we have been told to strip. However, if | |
3927 | h->indx is set to -2, the symbol is used by a reloc and we must | |
3928 | output it. */ | |
3929 | if (h->indx == -2) | |
3930 | strip = false; | |
3931 | else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
3932 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
3933 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
3934 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
3935 | strip = true; | |
3936 | else if (finfo->info->strip == strip_all | |
3937 | || (finfo->info->strip == strip_some | |
3938 | && bfd_hash_lookup (finfo->info->keep_hash, | |
3939 | h->root.root.string, | |
3940 | false, false) == NULL)) | |
3941 | strip = true; | |
3942 | else | |
3943 | strip = false; | |
3944 | ||
3945 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
3946 | nothing else to do. */ | |
3947 | if (strip && h->dynindx == -1) | |
3948 | return true; | |
3949 | ||
3950 | sym.st_value = 0; | |
3951 | sym.st_size = h->size; | |
6c02f1a0 | 3952 | sym.st_other = h->other; |
52c92c7f ILT |
3953 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) |
3954 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); | |
3955 | else if (h->root.type == bfd_link_hash_undefweak | |
3956 | || h->root.type == bfd_link_hash_defweak) | |
ede4eed4 KR |
3957 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); |
3958 | else | |
3959 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
3960 | ||
3961 | switch (h->root.type) | |
3962 | { | |
3963 | default: | |
3964 | case bfd_link_hash_new: | |
3965 | abort (); | |
3966 | return false; | |
3967 | ||
3968 | case bfd_link_hash_undefined: | |
3969 | input_sec = bfd_und_section_ptr; | |
3970 | sym.st_shndx = SHN_UNDEF; | |
3971 | break; | |
3972 | ||
3973 | case bfd_link_hash_undefweak: | |
3974 | input_sec = bfd_und_section_ptr; | |
3975 | sym.st_shndx = SHN_UNDEF; | |
3976 | break; | |
3977 | ||
3978 | case bfd_link_hash_defined: | |
3979 | case bfd_link_hash_defweak: | |
3980 | { | |
3981 | input_sec = h->root.u.def.section; | |
3982 | if (input_sec->output_section != NULL) | |
3983 | { | |
3984 | sym.st_shndx = | |
3985 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
3986 | input_sec->output_section); | |
3987 | if (sym.st_shndx == (unsigned short) -1) | |
3988 | { | |
52c92c7f | 3989 | eoinfo->failed = true; |
ede4eed4 KR |
3990 | return false; |
3991 | } | |
3992 | ||
3993 | /* ELF symbols in relocateable files are section relative, | |
3994 | but in nonrelocateable files they are virtual | |
3995 | addresses. */ | |
3996 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
3997 | if (! finfo->info->relocateable) | |
3998 | sym.st_value += input_sec->output_section->vma; | |
3999 | } | |
4000 | else | |
4001 | { | |
e549b1d2 ILT |
4002 | BFD_ASSERT (input_sec->owner == NULL |
4003 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
ede4eed4 KR |
4004 | sym.st_shndx = SHN_UNDEF; |
4005 | input_sec = bfd_und_section_ptr; | |
4006 | } | |
4007 | } | |
4008 | break; | |
4009 | ||
4010 | case bfd_link_hash_common: | |
4011 | input_sec = bfd_com_section_ptr; | |
4012 | sym.st_shndx = SHN_COMMON; | |
4013 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
4014 | break; | |
4015 | ||
4016 | case bfd_link_hash_indirect: | |
d044b40a ILT |
4017 | /* These symbols are created by symbol versioning. They point |
4018 | to the decorated version of the name. For example, if the | |
4019 | symbol foo@@GNU_1.2 is the default, which should be used when | |
4020 | foo is used with no version, then we add an indirect symbol | |
d6bfcdb5 ILT |
4021 | foo which points to foo@@GNU_1.2. We ignore these symbols, |
4022 | since the indirected symbol is already in the hash table. If | |
4023 | the indirect symbol is non-ELF, fall through and output it. */ | |
4024 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) == 0) | |
d044b40a ILT |
4025 | return true; |
4026 | ||
4027 | /* Fall through. */ | |
ede4eed4 | 4028 | case bfd_link_hash_warning: |
d044b40a ILT |
4029 | /* We can't represent these symbols in ELF, although a warning |
4030 | symbol may have come from a .gnu.warning.SYMBOL section. We | |
1f4ae0d6 ILT |
4031 | just put the target symbol in the hash table. If the target |
4032 | symbol does not really exist, don't do anything. */ | |
4033 | if (h->root.u.i.link->type == bfd_link_hash_new) | |
4034 | return true; | |
0cb70568 ILT |
4035 | return (elf_link_output_extsym |
4036 | ((struct elf_link_hash_entry *) h->root.u.i.link, data)); | |
ede4eed4 KR |
4037 | } |
4038 | ||
4039 | /* If this symbol should be put in the .dynsym section, then put it | |
4040 | there now. We have already know the symbol index. We also fill | |
4041 | in the entry in the .hash section. */ | |
4042 | if (h->dynindx != -1 | |
4043 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
4044 | { | |
4045 | struct elf_backend_data *bed; | |
d044b40a ILT |
4046 | char *p, *copy; |
4047 | const char *name; | |
ede4eed4 KR |
4048 | size_t bucketcount; |
4049 | size_t bucket; | |
4050 | bfd_byte *bucketpos; | |
4051 | bfd_vma chain; | |
4052 | ||
4053 | sym.st_name = h->dynstr_index; | |
4054 | ||
4055 | /* Give the processor backend a chance to tweak the symbol | |
4056 | value, and also to finish up anything that needs to be done | |
4057 | for this symbol. */ | |
4058 | bed = get_elf_backend_data (finfo->output_bfd); | |
4059 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
4060 | (finfo->output_bfd, finfo->info, h, &sym))) | |
4061 | { | |
52c92c7f | 4062 | eoinfo->failed = true; |
ede4eed4 KR |
4063 | return false; |
4064 | } | |
4065 | ||
4066 | elf_swap_symbol_out (finfo->output_bfd, &sym, | |
cf9fb9f2 ILT |
4067 | (PTR) (((Elf_External_Sym *) |
4068 | finfo->dynsym_sec->contents) | |
4069 | + h->dynindx)); | |
ede4eed4 | 4070 | |
d044b40a ILT |
4071 | /* We didn't include the version string in the dynamic string |
4072 | table, so we must not consider it in the hash table. */ | |
4073 | name = h->root.root.string; | |
4074 | p = strchr (name, ELF_VER_CHR); | |
4075 | if (p == NULL) | |
4076 | copy = NULL; | |
4077 | else | |
4078 | { | |
4079 | copy = bfd_alloc (finfo->output_bfd, p - name + 1); | |
4080 | strncpy (copy, name, p - name); | |
4081 | copy[p - name] = '\0'; | |
4082 | name = copy; | |
4083 | } | |
4084 | ||
ede4eed4 | 4085 | bucketcount = elf_hash_table (finfo->info)->bucketcount; |
d044b40a | 4086 | bucket = bfd_elf_hash ((const unsigned char *) name) % bucketcount; |
ede4eed4 KR |
4087 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents |
4088 | + (bucket + 2) * (ARCH_SIZE / 8)); | |
4089 | chain = get_word (finfo->output_bfd, bucketpos); | |
4090 | put_word (finfo->output_bfd, h->dynindx, bucketpos); | |
4091 | put_word (finfo->output_bfd, chain, | |
4092 | ((bfd_byte *) finfo->hash_sec->contents | |
4093 | + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8))); | |
d044b40a ILT |
4094 | |
4095 | if (copy != NULL) | |
4096 | bfd_release (finfo->output_bfd, copy); | |
4097 | ||
4098 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
4099 | { | |
4100 | Elf_Internal_Versym iversym; | |
4101 | ||
4102 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
4103 | { | |
4104 | if (h->verinfo.verdef == NULL) | |
4105 | iversym.vs_vers = 0; | |
4106 | else | |
4107 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
4108 | } | |
4109 | else | |
4110 | { | |
4111 | if (h->verinfo.vertree == NULL) | |
4112 | iversym.vs_vers = 1; | |
4113 | else | |
4114 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
4115 | } | |
4116 | ||
4117 | if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0) | |
4118 | iversym.vs_vers |= VERSYM_HIDDEN; | |
4119 | ||
4120 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, | |
4121 | (((Elf_External_Versym *) | |
4122 | finfo->symver_sec->contents) | |
4123 | + h->dynindx)); | |
4124 | } | |
ede4eed4 KR |
4125 | } |
4126 | ||
4127 | /* If we're stripping it, then it was just a dynamic symbol, and | |
4128 | there's nothing else to do. */ | |
4129 | if (strip) | |
4130 | return true; | |
4131 | ||
4132 | h->indx = finfo->output_bfd->symcount; | |
4133 | ||
4134 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec)) | |
4135 | { | |
52c92c7f | 4136 | eoinfo->failed = true; |
ede4eed4 KR |
4137 | return false; |
4138 | } | |
4139 | ||
4140 | return true; | |
4141 | } | |
4142 | ||
4143 | /* Link an input file into the linker output file. This function | |
4144 | handles all the sections and relocations of the input file at once. | |
4145 | This is so that we only have to read the local symbols once, and | |
4146 | don't have to keep them in memory. */ | |
4147 | ||
4148 | static boolean | |
4149 | elf_link_input_bfd (finfo, input_bfd) | |
4150 | struct elf_final_link_info *finfo; | |
4151 | bfd *input_bfd; | |
4152 | { | |
4153 | boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *, | |
4154 | bfd *, asection *, bfd_byte *, | |
4155 | Elf_Internal_Rela *, | |
4156 | Elf_Internal_Sym *, asection **)); | |
4157 | bfd *output_bfd; | |
4158 | Elf_Internal_Shdr *symtab_hdr; | |
4159 | size_t locsymcount; | |
4160 | size_t extsymoff; | |
c86158e5 | 4161 | Elf_External_Sym *external_syms; |
ede4eed4 KR |
4162 | Elf_External_Sym *esym; |
4163 | Elf_External_Sym *esymend; | |
4164 | Elf_Internal_Sym *isym; | |
4165 | long *pindex; | |
4166 | asection **ppsection; | |
4167 | asection *o; | |
4168 | ||
4169 | output_bfd = finfo->output_bfd; | |
4170 | relocate_section = | |
4171 | get_elf_backend_data (output_bfd)->elf_backend_relocate_section; | |
4172 | ||
4173 | /* If this is a dynamic object, we don't want to do anything here: | |
4174 | we don't want the local symbols, and we don't want the section | |
4175 | contents. */ | |
d044b40a | 4176 | if ((input_bfd->flags & DYNAMIC) != 0) |
ede4eed4 KR |
4177 | return true; |
4178 | ||
4179 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
4180 | if (elf_bad_symtab (input_bfd)) | |
4181 | { | |
4182 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); | |
4183 | extsymoff = 0; | |
4184 | } | |
4185 | else | |
4186 | { | |
4187 | locsymcount = symtab_hdr->sh_info; | |
4188 | extsymoff = symtab_hdr->sh_info; | |
4189 | } | |
4190 | ||
4191 | /* Read the local symbols. */ | |
c86158e5 ILT |
4192 | if (symtab_hdr->contents != NULL) |
4193 | external_syms = (Elf_External_Sym *) symtab_hdr->contents; | |
4194 | else if (locsymcount == 0) | |
4195 | external_syms = NULL; | |
4196 | else | |
4197 | { | |
4198 | external_syms = finfo->external_syms; | |
4199 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
4200 | || (bfd_read (external_syms, sizeof (Elf_External_Sym), | |
ede4eed4 | 4201 | locsymcount, input_bfd) |
c86158e5 ILT |
4202 | != locsymcount * sizeof (Elf_External_Sym))) |
4203 | return false; | |
4204 | } | |
ede4eed4 KR |
4205 | |
4206 | /* Swap in the local symbols and write out the ones which we know | |
4207 | are going into the output file. */ | |
c86158e5 | 4208 | esym = external_syms; |
ede4eed4 KR |
4209 | esymend = esym + locsymcount; |
4210 | isym = finfo->internal_syms; | |
4211 | pindex = finfo->indices; | |
4212 | ppsection = finfo->sections; | |
4213 | for (; esym < esymend; esym++, isym++, pindex++, ppsection++) | |
4214 | { | |
4215 | asection *isec; | |
4216 | const char *name; | |
4217 | Elf_Internal_Sym osym; | |
4218 | ||
4219 | elf_swap_symbol_in (input_bfd, esym, isym); | |
4220 | *pindex = -1; | |
4221 | ||
4222 | if (elf_bad_symtab (input_bfd)) | |
4223 | { | |
4224 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
4225 | { | |
4226 | *ppsection = NULL; | |
4227 | continue; | |
4228 | } | |
4229 | } | |
4230 | ||
4231 | if (isym->st_shndx == SHN_UNDEF) | |
4232 | isec = bfd_und_section_ptr; | |
4233 | else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE) | |
4234 | isec = section_from_elf_index (input_bfd, isym->st_shndx); | |
4235 | else if (isym->st_shndx == SHN_ABS) | |
4236 | isec = bfd_abs_section_ptr; | |
4237 | else if (isym->st_shndx == SHN_COMMON) | |
4238 | isec = bfd_com_section_ptr; | |
4239 | else | |
4240 | { | |
4241 | /* Who knows? */ | |
4242 | isec = NULL; | |
4243 | } | |
4244 | ||
4245 | *ppsection = isec; | |
4246 | ||
4247 | /* Don't output the first, undefined, symbol. */ | |
c86158e5 | 4248 | if (esym == external_syms) |
ede4eed4 KR |
4249 | continue; |
4250 | ||
4251 | /* If we are stripping all symbols, we don't want to output this | |
4252 | one. */ | |
4253 | if (finfo->info->strip == strip_all) | |
4254 | continue; | |
4255 | ||
4256 | /* We never output section symbols. Instead, we use the section | |
4257 | symbol of the corresponding section in the output file. */ | |
4258 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
4259 | continue; | |
4260 | ||
4261 | /* If we are discarding all local symbols, we don't want to | |
4262 | output this one. If we are generating a relocateable output | |
4263 | file, then some of the local symbols may be required by | |
4264 | relocs; we output them below as we discover that they are | |
4265 | needed. */ | |
4266 | if (finfo->info->discard == discard_all) | |
4267 | continue; | |
4268 | ||
258b1f5d | 4269 | /* If this symbol is defined in a section which we are |
fa802cb0 ILT |
4270 | discarding, we don't need to keep it, but note that |
4271 | linker_mark is only reliable for sections that have contents. | |
4272 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
4273 | as well as linker_mark. */ | |
258b1f5d ILT |
4274 | if (isym->st_shndx > 0 |
4275 | && isym->st_shndx < SHN_LORESERVE | |
4276 | && isec != NULL | |
fa802cb0 | 4277 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) |
258b1f5d ILT |
4278 | || (! finfo->info->relocateable |
4279 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
4280 | continue; | |
4281 | ||
ede4eed4 KR |
4282 | /* Get the name of the symbol. */ |
4283 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
258b1f5d | 4284 | isym->st_name); |
ede4eed4 KR |
4285 | if (name == NULL) |
4286 | return false; | |
4287 | ||
4288 | /* See if we are discarding symbols with this name. */ | |
4289 | if ((finfo->info->strip == strip_some | |
4290 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false) | |
4291 | == NULL)) | |
4292 | || (finfo->info->discard == discard_l | |
e316f514 | 4293 | && bfd_is_local_label_name (input_bfd, name))) |
ede4eed4 KR |
4294 | continue; |
4295 | ||
4296 | /* If we get here, we are going to output this symbol. */ | |
4297 | ||
4298 | osym = *isym; | |
4299 | ||
4300 | /* Adjust the section index for the output file. */ | |
4301 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
4302 | isec->output_section); | |
4303 | if (osym.st_shndx == (unsigned short) -1) | |
4304 | return false; | |
4305 | ||
4306 | *pindex = output_bfd->symcount; | |
4307 | ||
4308 | /* ELF symbols in relocateable files are section relative, but | |
4309 | in executable files they are virtual addresses. Note that | |
4310 | this code assumes that all ELF sections have an associated | |
4311 | BFD section with a reasonable value for output_offset; below | |
4312 | we assume that they also have a reasonable value for | |
4313 | output_section. Any special sections must be set up to meet | |
4314 | these requirements. */ | |
4315 | osym.st_value += isec->output_offset; | |
4316 | if (! finfo->info->relocateable) | |
4317 | osym.st_value += isec->output_section->vma; | |
4318 | ||
4319 | if (! elf_link_output_sym (finfo, name, &osym, isec)) | |
4320 | return false; | |
4321 | } | |
4322 | ||
4323 | /* Relocate the contents of each section. */ | |
4324 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
4325 | { | |
c86158e5 ILT |
4326 | bfd_byte *contents; |
4327 | ||
ff0e4a93 | 4328 | if (! o->linker_mark) |
7ec49f91 ILT |
4329 | { |
4330 | /* This section was omitted from the link. */ | |
4331 | continue; | |
4332 | } | |
4333 | ||
1726b8f0 ILT |
4334 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
4335 | || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0)) | |
ede4eed4 KR |
4336 | continue; |
4337 | ||
ff12f303 | 4338 | if ((o->flags & SEC_LINKER_CREATED) != 0) |
ede4eed4 | 4339 | { |
ff12f303 ILT |
4340 | /* Section was created by elf_link_create_dynamic_sections |
4341 | or somesuch. */ | |
ede4eed4 KR |
4342 | continue; |
4343 | } | |
4344 | ||
c86158e5 ILT |
4345 | /* Get the contents of the section. They have been cached by a |
4346 | relaxation routine. Note that o is a section in an input | |
4347 | file, so the contents field will not have been set by any of | |
4348 | the routines which work on output files. */ | |
4349 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
4350 | contents = elf_section_data (o)->this_hdr.contents; | |
4351 | else | |
4352 | { | |
4353 | contents = finfo->contents; | |
4354 | if (! bfd_get_section_contents (input_bfd, o, contents, | |
4355 | (file_ptr) 0, o->_raw_size)) | |
4356 | return false; | |
4357 | } | |
ede4eed4 KR |
4358 | |
4359 | if ((o->flags & SEC_RELOC) != 0) | |
4360 | { | |
4361 | Elf_Internal_Rela *internal_relocs; | |
4362 | ||
4363 | /* Get the swapped relocs. */ | |
c86158e5 ILT |
4364 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
4365 | (input_bfd, o, finfo->external_relocs, | |
4366 | finfo->internal_relocs, false)); | |
ede4eed4 KR |
4367 | if (internal_relocs == NULL |
4368 | && o->reloc_count > 0) | |
4369 | return false; | |
4370 | ||
4371 | /* Relocate the section by invoking a back end routine. | |
4372 | ||
4373 | The back end routine is responsible for adjusting the | |
4374 | section contents as necessary, and (if using Rela relocs | |
4375 | and generating a relocateable output file) adjusting the | |
4376 | reloc addend as necessary. | |
4377 | ||
4378 | The back end routine does not have to worry about setting | |
4379 | the reloc address or the reloc symbol index. | |
4380 | ||
4381 | The back end routine is given a pointer to the swapped in | |
4382 | internal symbols, and can access the hash table entries | |
4383 | for the external symbols via elf_sym_hashes (input_bfd). | |
4384 | ||
4385 | When generating relocateable output, the back end routine | |
4386 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
4387 | output symbol is going to be a section symbol | |
4388 | corresponding to the output section, which will require | |
4389 | the addend to be adjusted. */ | |
4390 | ||
4391 | if (! (*relocate_section) (output_bfd, finfo->info, | |
c86158e5 | 4392 | input_bfd, o, contents, |
ede4eed4 KR |
4393 | internal_relocs, |
4394 | finfo->internal_syms, | |
4395 | finfo->sections)) | |
4396 | return false; | |
4397 | ||
4398 | if (finfo->info->relocateable) | |
4399 | { | |
4400 | Elf_Internal_Rela *irela; | |
4401 | Elf_Internal_Rela *irelaend; | |
4402 | struct elf_link_hash_entry **rel_hash; | |
4403 | Elf_Internal_Shdr *input_rel_hdr; | |
4404 | Elf_Internal_Shdr *output_rel_hdr; | |
4405 | ||
4406 | /* Adjust the reloc addresses and symbol indices. */ | |
4407 | ||
4408 | irela = internal_relocs; | |
4409 | irelaend = irela + o->reloc_count; | |
4410 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
4411 | + o->output_section->reloc_count); | |
4412 | for (; irela < irelaend; irela++, rel_hash++) | |
4413 | { | |
ae115e51 | 4414 | unsigned long r_symndx; |
ede4eed4 KR |
4415 | Elf_Internal_Sym *isym; |
4416 | asection *sec; | |
4417 | ||
4418 | irela->r_offset += o->output_offset; | |
4419 | ||
4420 | r_symndx = ELF_R_SYM (irela->r_info); | |
4421 | ||
4422 | if (r_symndx == 0) | |
4423 | continue; | |
4424 | ||
4425 | if (r_symndx >= locsymcount | |
4426 | || (elf_bad_symtab (input_bfd) | |
4427 | && finfo->sections[r_symndx] == NULL)) | |
4428 | { | |
4429 | long indx; | |
4430 | ||
4431 | /* This is a reloc against a global symbol. We | |
4432 | have not yet output all the local symbols, so | |
4433 | we do not know the symbol index of any global | |
4434 | symbol. We set the rel_hash entry for this | |
4435 | reloc to point to the global hash table entry | |
4436 | for this symbol. The symbol index is then | |
4437 | set at the end of elf_bfd_final_link. */ | |
4438 | indx = r_symndx - extsymoff; | |
4439 | *rel_hash = elf_sym_hashes (input_bfd)[indx]; | |
4440 | ||
4441 | /* Setting the index to -2 tells | |
4442 | elf_link_output_extsym that this symbol is | |
4443 | used by a reloc. */ | |
4444 | BFD_ASSERT ((*rel_hash)->indx < 0); | |
4445 | (*rel_hash)->indx = -2; | |
4446 | ||
4447 | continue; | |
4448 | } | |
4449 | ||
4450 | /* This is a reloc against a local symbol. */ | |
4451 | ||
4452 | *rel_hash = NULL; | |
4453 | isym = finfo->internal_syms + r_symndx; | |
4454 | sec = finfo->sections[r_symndx]; | |
4455 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
4456 | { | |
4457 | /* I suppose the backend ought to fill in the | |
4458 | section of any STT_SECTION symbol against a | |
ba4a4594 ILT |
4459 | processor specific section. If we have |
4460 | discarded a section, the output_section will | |
4461 | be the absolute section. */ | |
4462 | if (sec != NULL | |
4463 | && (bfd_is_abs_section (sec) | |
4464 | || (sec->output_section != NULL | |
4465 | && bfd_is_abs_section (sec->output_section)))) | |
ede4eed4 KR |
4466 | r_symndx = 0; |
4467 | else if (sec == NULL || sec->owner == NULL) | |
4468 | { | |
4469 | bfd_set_error (bfd_error_bad_value); | |
4470 | return false; | |
4471 | } | |
4472 | else | |
4473 | { | |
4474 | r_symndx = sec->output_section->target_index; | |
4475 | BFD_ASSERT (r_symndx != 0); | |
4476 | } | |
4477 | } | |
4478 | else | |
4479 | { | |
4480 | if (finfo->indices[r_symndx] == -1) | |
4481 | { | |
4482 | unsigned long link; | |
4483 | const char *name; | |
4484 | asection *osec; | |
4485 | ||
4486 | if (finfo->info->strip == strip_all) | |
4487 | { | |
4488 | /* You can't do ld -r -s. */ | |
4489 | bfd_set_error (bfd_error_invalid_operation); | |
4490 | return false; | |
4491 | } | |
4492 | ||
4493 | /* This symbol was skipped earlier, but | |
4494 | since it is needed by a reloc, we | |
4495 | must output it now. */ | |
4496 | link = symtab_hdr->sh_link; | |
4497 | name = bfd_elf_string_from_elf_section (input_bfd, | |
4498 | link, | |
4499 | isym->st_name); | |
4500 | if (name == NULL) | |
4501 | return false; | |
4502 | ||
4503 | osec = sec->output_section; | |
4504 | isym->st_shndx = | |
4505 | _bfd_elf_section_from_bfd_section (output_bfd, | |
4506 | osec); | |
4507 | if (isym->st_shndx == (unsigned short) -1) | |
4508 | return false; | |
4509 | ||
4510 | isym->st_value += sec->output_offset; | |
4511 | if (! finfo->info->relocateable) | |
4512 | isym->st_value += osec->vma; | |
4513 | ||
4514 | finfo->indices[r_symndx] = output_bfd->symcount; | |
4515 | ||
4516 | if (! elf_link_output_sym (finfo, name, isym, sec)) | |
4517 | return false; | |
4518 | } | |
4519 | ||
4520 | r_symndx = finfo->indices[r_symndx]; | |
4521 | } | |
4522 | ||
4523 | irela->r_info = ELF_R_INFO (r_symndx, | |
4524 | ELF_R_TYPE (irela->r_info)); | |
4525 | } | |
4526 | ||
4527 | /* Swap out the relocs. */ | |
4528 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
4529 | output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr; | |
4530 | BFD_ASSERT (output_rel_hdr->sh_entsize | |
4531 | == input_rel_hdr->sh_entsize); | |
4532 | irela = internal_relocs; | |
4533 | irelaend = irela + o->reloc_count; | |
4534 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
4535 | { | |
4536 | Elf_External_Rel *erel; | |
4537 | ||
4538 | erel = ((Elf_External_Rel *) output_rel_hdr->contents | |
4539 | + o->output_section->reloc_count); | |
4540 | for (; irela < irelaend; irela++, erel++) | |
4541 | { | |
4542 | Elf_Internal_Rel irel; | |
4543 | ||
4544 | irel.r_offset = irela->r_offset; | |
4545 | irel.r_info = irela->r_info; | |
4546 | BFD_ASSERT (irela->r_addend == 0); | |
4547 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
4548 | } | |
4549 | } | |
4550 | else | |
4551 | { | |
4552 | Elf_External_Rela *erela; | |
4553 | ||
4554 | BFD_ASSERT (input_rel_hdr->sh_entsize | |
4555 | == sizeof (Elf_External_Rela)); | |
4556 | erela = ((Elf_External_Rela *) output_rel_hdr->contents | |
4557 | + o->output_section->reloc_count); | |
4558 | for (; irela < irelaend; irela++, erela++) | |
4559 | elf_swap_reloca_out (output_bfd, irela, erela); | |
4560 | } | |
4561 | ||
4562 | o->output_section->reloc_count += o->reloc_count; | |
4563 | } | |
4564 | } | |
4565 | ||
4566 | /* Write out the modified section contents. */ | |
1726b8f0 ILT |
4567 | if (elf_section_data (o)->stab_info == NULL) |
4568 | { | |
4569 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
c86158e5 | 4570 | contents, o->output_offset, |
1726b8f0 ILT |
4571 | (o->_cooked_size != 0 |
4572 | ? o->_cooked_size | |
4573 | : o->_raw_size))) | |
4574 | return false; | |
4575 | } | |
4576 | else | |
4577 | { | |
3cd5cf3d ILT |
4578 | if (! (_bfd_write_section_stabs |
4579 | (output_bfd, &elf_hash_table (finfo->info)->stab_info, | |
4580 | o, &elf_section_data (o)->stab_info, contents))) | |
1726b8f0 ILT |
4581 | return false; |
4582 | } | |
ede4eed4 KR |
4583 | } |
4584 | ||
4585 | return true; | |
4586 | } | |
4587 | ||
4588 | /* Generate a reloc when linking an ELF file. This is a reloc | |
4589 | requested by the linker, and does come from any input file. This | |
4590 | is used to build constructor and destructor tables when linking | |
4591 | with -Ur. */ | |
4592 | ||
4593 | static boolean | |
4594 | elf_reloc_link_order (output_bfd, info, output_section, link_order) | |
4595 | bfd *output_bfd; | |
4596 | struct bfd_link_info *info; | |
4597 | asection *output_section; | |
4598 | struct bfd_link_order *link_order; | |
4599 | { | |
4600 | reloc_howto_type *howto; | |
4601 | long indx; | |
4602 | bfd_vma offset; | |
5b3b9ff6 | 4603 | bfd_vma addend; |
ede4eed4 KR |
4604 | struct elf_link_hash_entry **rel_hash_ptr; |
4605 | Elf_Internal_Shdr *rel_hdr; | |
4606 | ||
4607 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
4608 | if (howto == NULL) | |
4609 | { | |
4610 | bfd_set_error (bfd_error_bad_value); | |
4611 | return false; | |
4612 | } | |
4613 | ||
5b3b9ff6 ILT |
4614 | addend = link_order->u.reloc.p->addend; |
4615 | ||
4616 | /* Figure out the symbol index. */ | |
4617 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
4618 | + output_section->reloc_count); | |
4619 | if (link_order->type == bfd_section_reloc_link_order) | |
4620 | { | |
4621 | indx = link_order->u.reloc.p->u.section->target_index; | |
4622 | BFD_ASSERT (indx != 0); | |
4623 | *rel_hash_ptr = NULL; | |
4624 | } | |
4625 | else | |
4626 | { | |
4627 | struct elf_link_hash_entry *h; | |
4628 | ||
4629 | /* Treat a reloc against a defined symbol as though it were | |
4630 | actually against the section. */ | |
8881b321 ILT |
4631 | h = ((struct elf_link_hash_entry *) |
4632 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
4633 | link_order->u.reloc.p->u.name, | |
4634 | false, false, true)); | |
5b3b9ff6 ILT |
4635 | if (h != NULL |
4636 | && (h->root.type == bfd_link_hash_defined | |
4637 | || h->root.type == bfd_link_hash_defweak)) | |
4638 | { | |
4639 | asection *section; | |
4640 | ||
4641 | section = h->root.u.def.section; | |
4642 | indx = section->output_section->target_index; | |
4643 | *rel_hash_ptr = NULL; | |
4644 | /* It seems that we ought to add the symbol value to the | |
4645 | addend here, but in practice it has already been added | |
4646 | because it was passed to constructor_callback. */ | |
4647 | addend += section->output_section->vma + section->output_offset; | |
4648 | } | |
4649 | else if (h != NULL) | |
4650 | { | |
4651 | /* Setting the index to -2 tells elf_link_output_extsym that | |
4652 | this symbol is used by a reloc. */ | |
4653 | h->indx = -2; | |
4654 | *rel_hash_ptr = h; | |
4655 | indx = 0; | |
4656 | } | |
4657 | else | |
4658 | { | |
4659 | if (! ((*info->callbacks->unattached_reloc) | |
4660 | (info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
4661 | (asection *) NULL, (bfd_vma) 0))) | |
4662 | return false; | |
4663 | indx = 0; | |
4664 | } | |
4665 | } | |
4666 | ||
ede4eed4 KR |
4667 | /* If this is an inplace reloc, we must write the addend into the |
4668 | object file. */ | |
5b3b9ff6 | 4669 | if (howto->partial_inplace && addend != 0) |
ede4eed4 KR |
4670 | { |
4671 | bfd_size_type size; | |
4672 | bfd_reloc_status_type rstat; | |
4673 | bfd_byte *buf; | |
4674 | boolean ok; | |
4675 | ||
4676 | size = bfd_get_reloc_size (howto); | |
4677 | buf = (bfd_byte *) bfd_zmalloc (size); | |
4678 | if (buf == (bfd_byte *) NULL) | |
a9713b91 | 4679 | return false; |
5b3b9ff6 | 4680 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); |
ede4eed4 KR |
4681 | switch (rstat) |
4682 | { | |
4683 | case bfd_reloc_ok: | |
4684 | break; | |
4685 | default: | |
4686 | case bfd_reloc_outofrange: | |
4687 | abort (); | |
4688 | case bfd_reloc_overflow: | |
4689 | if (! ((*info->callbacks->reloc_overflow) | |
4690 | (info, | |
4691 | (link_order->type == bfd_section_reloc_link_order | |
4692 | ? bfd_section_name (output_bfd, | |
4693 | link_order->u.reloc.p->u.section) | |
4694 | : link_order->u.reloc.p->u.name), | |
5b3b9ff6 ILT |
4695 | howto->name, addend, (bfd *) NULL, (asection *) NULL, |
4696 | (bfd_vma) 0))) | |
ede4eed4 KR |
4697 | { |
4698 | free (buf); | |
4699 | return false; | |
4700 | } | |
4701 | break; | |
4702 | } | |
4703 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
4704 | (file_ptr) link_order->offset, size); | |
4705 | free (buf); | |
4706 | if (! ok) | |
4707 | return false; | |
4708 | } | |
4709 | ||
ede4eed4 KR |
4710 | /* The address of a reloc is relative to the section in a |
4711 | relocateable file, and is a virtual address in an executable | |
4712 | file. */ | |
4713 | offset = link_order->offset; | |
4714 | if (! info->relocateable) | |
4715 | offset += output_section->vma; | |
4716 | ||
4717 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
4718 | ||
4719 | if (rel_hdr->sh_type == SHT_REL) | |
4720 | { | |
4721 | Elf_Internal_Rel irel; | |
4722 | Elf_External_Rel *erel; | |
4723 | ||
4724 | irel.r_offset = offset; | |
4725 | irel.r_info = ELF_R_INFO (indx, howto->type); | |
4726 | erel = ((Elf_External_Rel *) rel_hdr->contents | |
4727 | + output_section->reloc_count); | |
4728 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
4729 | } | |
4730 | else | |
4731 | { | |
4732 | Elf_Internal_Rela irela; | |
4733 | Elf_External_Rela *erela; | |
4734 | ||
4735 | irela.r_offset = offset; | |
4736 | irela.r_info = ELF_R_INFO (indx, howto->type); | |
5b3b9ff6 | 4737 | irela.r_addend = addend; |
ede4eed4 KR |
4738 | erela = ((Elf_External_Rela *) rel_hdr->contents |
4739 | + output_section->reloc_count); | |
4740 | elf_swap_reloca_out (output_bfd, &irela, erela); | |
4741 | } | |
4742 | ||
4743 | ++output_section->reloc_count; | |
4744 | ||
4745 | return true; | |
4746 | } | |
4747 | ||
3b3753b8 MM |
4748 | \f |
4749 | /* Allocate a pointer to live in a linker created section. */ | |
4750 | ||
4751 | boolean | |
4752 | elf_create_pointer_linker_section (abfd, info, lsect, h, rel) | |
4753 | bfd *abfd; | |
4754 | struct bfd_link_info *info; | |
4755 | elf_linker_section_t *lsect; | |
4756 | struct elf_link_hash_entry *h; | |
4757 | const Elf_Internal_Rela *rel; | |
4758 | { | |
4759 | elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL; | |
4760 | elf_linker_section_pointers_t *linker_section_ptr; | |
4761 | unsigned long r_symndx = ELF_R_SYM (rel->r_info);; | |
4762 | ||
4763 | BFD_ASSERT (lsect != NULL); | |
4764 | ||
4765 | /* Is this a global symbol? */ | |
4766 | if (h != NULL) | |
4767 | { | |
4768 | /* Has this symbol already been allocated, if so, our work is done */ | |
4769 | if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer, | |
4770 | rel->r_addend, | |
4771 | lsect->which)) | |
4772 | return true; | |
4773 | ||
4774 | ptr_linker_section_ptr = &h->linker_section_pointer; | |
4775 | /* Make sure this symbol is output as a dynamic symbol. */ | |
4776 | if (h->dynindx == -1) | |
4777 | { | |
4778 | if (! elf_link_record_dynamic_symbol (info, h)) | |
4779 | return false; | |
4780 | } | |
4781 | ||
eb82bc60 MM |
4782 | if (lsect->rel_section) |
4783 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); | |
3b3753b8 MM |
4784 | } |
4785 | ||
4786 | else /* Allocation of a pointer to a local symbol */ | |
4787 | { | |
4788 | elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd); | |
4789 | ||
4790 | /* Allocate a table to hold the local symbols if first time */ | |
4791 | if (!ptr) | |
4792 | { | |
4793 | int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info; | |
4794 | register unsigned int i; | |
4795 | ||
4796 | ptr = (elf_linker_section_pointers_t **) | |
4797 | bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *)); | |
4798 | ||
4799 | if (!ptr) | |
4800 | return false; | |
4801 | ||
4802 | elf_local_ptr_offsets (abfd) = ptr; | |
4803 | for (i = 0; i < num_symbols; i++) | |
4804 | ptr[i] = (elf_linker_section_pointers_t *)0; | |
4805 | } | |
4806 | ||
4807 | /* Has this symbol already been allocated, if so, our work is done */ | |
4808 | if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx], | |
4809 | rel->r_addend, | |
4810 | lsect->which)) | |
4811 | return true; | |
4812 | ||
4813 | ptr_linker_section_ptr = &ptr[r_symndx]; | |
4814 | ||
4815 | if (info->shared) | |
4816 | { | |
4817 | /* If we are generating a shared object, we need to | |
05f927dd | 4818 | output a R_<xxx>_RELATIVE reloc so that the |
3b3753b8 MM |
4819 | dynamic linker can adjust this GOT entry. */ |
4820 | BFD_ASSERT (lsect->rel_section != NULL); | |
4821 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); | |
4822 | } | |
4823 | } | |
4824 | ||
4825 | /* Allocate space for a pointer in the linker section, and allocate a new pointer record | |
4826 | from internal memory. */ | |
4827 | BFD_ASSERT (ptr_linker_section_ptr != NULL); | |
4828 | linker_section_ptr = (elf_linker_section_pointers_t *) | |
4829 | bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t)); | |
4830 | ||
4831 | if (!linker_section_ptr) | |
4832 | return false; | |
4833 | ||
4834 | linker_section_ptr->next = *ptr_linker_section_ptr; | |
4835 | linker_section_ptr->addend = rel->r_addend; | |
4836 | linker_section_ptr->which = lsect->which; | |
4837 | linker_section_ptr->written_address_p = false; | |
4838 | *ptr_linker_section_ptr = linker_section_ptr; | |
4839 | ||
cb73f5d7 | 4840 | #if 0 |
3b3753b8 MM |
4841 | if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset) |
4842 | { | |
cb73f5d7 | 4843 | linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8); |
3b3753b8 MM |
4844 | lsect->hole_offset += ARCH_SIZE / 8; |
4845 | lsect->sym_offset += ARCH_SIZE / 8; | |
4846 | if (lsect->sym_hash) /* Bump up symbol value if needed */ | |
4a4953f5 MM |
4847 | { |
4848 | lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8; | |
4849 | #ifdef DEBUG | |
4850 | fprintf (stderr, "Bump up %s by %ld, current value = %ld\n", | |
4851 | lsect->sym_hash->root.root.string, | |
4852 | (long)ARCH_SIZE / 8, | |
4853 | (long)lsect->sym_hash->root.u.def.value); | |
4854 | #endif | |
4855 | } | |
3b3753b8 MM |
4856 | } |
4857 | else | |
cb73f5d7 | 4858 | #endif |
3b3753b8 MM |
4859 | linker_section_ptr->offset = lsect->section->_raw_size; |
4860 | ||
4861 | lsect->section->_raw_size += ARCH_SIZE / 8; | |
4862 | ||
4863 | #ifdef DEBUG | |
4864 | fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n", | |
4865 | lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size); | |
4866 | #endif | |
4867 | ||
4868 | return true; | |
4869 | } | |
4870 | ||
4871 | \f | |
4872 | #if ARCH_SIZE==64 | |
4873 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR) | |
4874 | #endif | |
4875 | #if ARCH_SIZE==32 | |
4876 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR) | |
4877 | #endif | |
4878 | ||
4879 | /* Fill in the address for a pointer generated in alinker section. */ | |
4880 | ||
4881 | bfd_vma | |
4882 | elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc) | |
4883 | bfd *output_bfd; | |
4884 | bfd *input_bfd; | |
4885 | struct bfd_link_info *info; | |
4886 | elf_linker_section_t *lsect; | |
4887 | struct elf_link_hash_entry *h; | |
4888 | bfd_vma relocation; | |
4889 | const Elf_Internal_Rela *rel; | |
4890 | int relative_reloc; | |
4891 | { | |
4892 | elf_linker_section_pointers_t *linker_section_ptr; | |
4893 | ||
4894 | BFD_ASSERT (lsect != NULL); | |
4895 | ||
3b3753b8 MM |
4896 | if (h != NULL) /* global symbol */ |
4897 | { | |
4898 | linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer, | |
4899 | rel->r_addend, | |
4900 | lsect->which); | |
4901 | ||
4902 | BFD_ASSERT (linker_section_ptr != NULL); | |
4903 | ||
4904 | if (! elf_hash_table (info)->dynamic_sections_created | |
4905 | || (info->shared | |
4906 | && info->symbolic | |
4907 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
4908 | { | |
4909 | /* This is actually a static link, or it is a | |
4910 | -Bsymbolic link and the symbol is defined | |
4911 | locally. We must initialize this entry in the | |
4912 | global section. | |
4913 | ||
4914 | When doing a dynamic link, we create a .rela.<xxx> | |
4915 | relocation entry to initialize the value. This | |
4916 | is done in the finish_dynamic_symbol routine. */ | |
4917 | if (!linker_section_ptr->written_address_p) | |
4918 | { | |
4919 | linker_section_ptr->written_address_p = true; | |
4920 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, | |
4921 | lsect->section->contents + linker_section_ptr->offset); | |
4922 | } | |
4923 | } | |
4924 | } | |
4925 | else /* local symbol */ | |
4926 | { | |
4927 | unsigned long r_symndx = ELF_R_SYM (rel->r_info); | |
4928 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL); | |
4929 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL); | |
4930 | linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx], | |
4931 | rel->r_addend, | |
4932 | lsect->which); | |
4933 | ||
4934 | BFD_ASSERT (linker_section_ptr != NULL); | |
4935 | ||
4936 | /* Write out pointer if it hasn't been rewritten out before */ | |
4937 | if (!linker_section_ptr->written_address_p) | |
4938 | { | |
4939 | linker_section_ptr->written_address_p = true; | |
4940 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, | |
4941 | lsect->section->contents + linker_section_ptr->offset); | |
4942 | ||
4943 | if (info->shared) | |
4944 | { | |
4945 | asection *srel = lsect->rel_section; | |
4946 | Elf_Internal_Rela outrel; | |
4947 | ||
4948 | /* We need to generate a relative reloc for the dynamic linker. */ | |
4949 | if (!srel) | |
4950 | lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj, | |
4951 | lsect->rel_name); | |
4952 | ||
4953 | BFD_ASSERT (srel != NULL); | |
4954 | ||
4955 | outrel.r_offset = (lsect->section->output_section->vma | |
4956 | + lsect->section->output_offset | |
4957 | + linker_section_ptr->offset); | |
4958 | outrel.r_info = ELF_R_INFO (0, relative_reloc); | |
4959 | outrel.r_addend = 0; | |
4960 | elf_swap_reloca_out (output_bfd, &outrel, | |
5a5bac64 | 4961 | (((Elf_External_Rela *) |
3b3753b8 MM |
4962 | lsect->section->contents) |
4963 | + lsect->section->reloc_count)); | |
4964 | ++lsect->section->reloc_count; | |
4965 | } | |
4966 | } | |
4967 | } | |
4968 | ||
4969 | relocation = (lsect->section->output_offset | |
4970 | + linker_section_ptr->offset | |
4971 | - lsect->hole_offset | |
4972 | - lsect->sym_offset); | |
4973 | ||
4974 | #ifdef DEBUG | |
4975 | fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n", | |
4976 | lsect->name, (long)relocation, (long)relocation); | |
4977 | #endif | |
4978 | ||
4979 | /* Subtract out the addend, because it will get added back in by the normal | |
4980 | processing. */ | |
4981 | return relocation - linker_section_ptr->addend; | |
4982 | } |