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