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