Commit | Line | Data |
---|---|---|
8afe83be | 1 | /* ELF linker support. |
56f3b62c | 2 | Copyright 1995, 1996, 1997 Free Software Foundation, Inc. |
8afe83be KR |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
943fbd5b | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
452a5efb | 19 | |
ede4eed4 KR |
20 | /* ELF linker code. */ |
21 | ||
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 ILT |
301 | *override = false; |
302 | *type_change_ok = false; | |
303 | *size_change_ok = false; | |
304 | ||
305 | sec = *psec; | |
306 | bind = ELF_ST_BIND (sym->st_info); | |
307 | ||
308 | if (! bfd_is_und_section (sec)) | |
309 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false); | |
310 | else | |
311 | h = ((struct elf_link_hash_entry *) | |
312 | bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false)); | |
313 | if (h == NULL) | |
314 | return false; | |
315 | *sym_hash = h; | |
316 | ||
3359a0bc ILT |
317 | /* This code is for coping with dynamic objects, and is only useful |
318 | if we are doing an ELF link. */ | |
319 | if (info->hash->creator != abfd->xvec) | |
320 | return true; | |
321 | ||
e9982ee5 ILT |
322 | /* For merging, we only care about real symbols. */ |
323 | ||
324 | while (h->root.type == bfd_link_hash_indirect | |
325 | || h->root.type == bfd_link_hash_warning) | |
326 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
327 | ||
044d7d49 ILT |
328 | /* If we just created the symbol, mark it as being an ELF symbol. |
329 | Other than that, there is nothing to do--there is no merge issue | |
330 | with a newly defined symbol--so we just return. */ | |
331 | ||
332 | if (h->root.type == bfd_link_hash_new) | |
333 | { | |
334 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; | |
335 | return true; | |
336 | } | |
337 | ||
044d7d49 ILT |
338 | /* OLDBFD is a BFD associated with the existing symbol. */ |
339 | ||
340 | switch (h->root.type) | |
341 | { | |
342 | default: | |
343 | oldbfd = NULL; | |
344 | break; | |
345 | ||
346 | case bfd_link_hash_undefined: | |
347 | case bfd_link_hash_undefweak: | |
348 | oldbfd = h->root.u.undef.abfd; | |
349 | break; | |
350 | ||
351 | case bfd_link_hash_defined: | |
352 | case bfd_link_hash_defweak: | |
353 | oldbfd = h->root.u.def.section->owner; | |
354 | break; | |
355 | ||
356 | case bfd_link_hash_common: | |
357 | oldbfd = h->root.u.c.p->section->owner; | |
358 | break; | |
359 | } | |
360 | ||
361 | /* NEWDYN and OLDDYN indicate whether the new or old symbol, | |
362 | respectively, is from a dynamic object. */ | |
363 | ||
364 | if ((abfd->flags & DYNAMIC) != 0) | |
365 | newdyn = true; | |
366 | else | |
367 | newdyn = false; | |
368 | ||
369 | if (oldbfd == NULL || (oldbfd->flags & DYNAMIC) == 0) | |
370 | olddyn = false; | |
371 | else | |
372 | olddyn = true; | |
373 | ||
374 | /* NEWDEF and OLDDEF indicate whether the new or old symbol, | |
375 | respectively, appear to be a definition rather than reference. */ | |
376 | ||
377 | if (bfd_is_und_section (sec) || bfd_is_com_section (sec)) | |
378 | newdef = false; | |
379 | else | |
380 | newdef = true; | |
381 | ||
382 | if (h->root.type == bfd_link_hash_undefined | |
383 | || h->root.type == bfd_link_hash_undefweak | |
384 | || h->root.type == bfd_link_hash_common) | |
385 | olddef = false; | |
386 | else | |
387 | olddef = true; | |
388 | ||
389 | /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old | |
390 | symbol, respectively, appears to be a common symbol in a dynamic | |
391 | object. If a symbol appears in an uninitialized section, and is | |
392 | not weak, and is not a function, then it may be a common symbol | |
393 | which was resolved when the dynamic object was created. We want | |
394 | to treat such symbols specially, because they raise special | |
395 | considerations when setting the symbol size: if the symbol | |
396 | appears as a common symbol in a regular object, and the size in | |
397 | the regular object is larger, we must make sure that we use the | |
398 | larger size. This problematic case can always be avoided in C, | |
399 | but it must be handled correctly when using Fortran shared | |
400 | libraries. | |
401 | ||
402 | Note that if NEWDYNCOMMON is set, NEWDEF will be set, and | |
403 | likewise for OLDDYNCOMMON and OLDDEF. | |
404 | ||
405 | Note that this test is just a heuristic, and that it is quite | |
406 | possible to have an uninitialized symbol in a shared object which | |
407 | is really a definition, rather than a common symbol. This could | |
408 | lead to some minor confusion when the symbol really is a common | |
409 | symbol in some regular object. However, I think it will be | |
410 | harmless. */ | |
411 | ||
412 | if (newdyn | |
413 | && newdef | |
414 | && (sec->flags & SEC_ALLOC) != 0 | |
415 | && (sec->flags & SEC_LOAD) == 0 | |
416 | && sym->st_size > 0 | |
417 | && bind != STB_WEAK | |
418 | && ELF_ST_TYPE (sym->st_info) != STT_FUNC) | |
419 | newdyncommon = true; | |
420 | else | |
421 | newdyncommon = false; | |
422 | ||
423 | if (olddyn | |
424 | && olddef | |
425 | && h->root.type == bfd_link_hash_defined | |
426 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
427 | && (h->root.u.def.section->flags & SEC_ALLOC) != 0 | |
428 | && (h->root.u.def.section->flags & SEC_LOAD) == 0 | |
429 | && h->size > 0 | |
430 | && h->type != STT_FUNC) | |
431 | olddyncommon = true; | |
432 | else | |
433 | olddyncommon = false; | |
434 | ||
435 | /* It's OK to change the type if either the existing symbol or the | |
436 | new symbol is weak. */ | |
437 | ||
438 | if (h->root.type == bfd_link_hash_defweak | |
439 | || h->root.type == bfd_link_hash_undefweak | |
440 | || bind == STB_WEAK) | |
441 | *type_change_ok = true; | |
442 | ||
443 | /* It's OK to change the size if either the existing symbol or the | |
444 | new symbol is weak, or if the old symbol is undefined. */ | |
445 | ||
446 | if (*type_change_ok | |
447 | || h->root.type == bfd_link_hash_undefined) | |
448 | *size_change_ok = true; | |
449 | ||
450 | /* If both the old and the new symbols look like common symbols in a | |
451 | dynamic object, set the size of the symbol to the larger of the | |
452 | two. */ | |
453 | ||
454 | if (olddyncommon | |
455 | && newdyncommon | |
456 | && sym->st_size != h->size) | |
457 | { | |
458 | /* Since we think we have two common symbols, issue a multiple | |
459 | common warning if desired. Note that we only warn if the | |
460 | size is different. If the size is the same, we simply let | |
461 | the old symbol override the new one as normally happens with | |
462 | symbols defined in dynamic objects. */ | |
463 | ||
464 | if (! ((*info->callbacks->multiple_common) | |
465 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
466 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
467 | return false; | |
468 | ||
469 | if (sym->st_size > h->size) | |
470 | h->size = sym->st_size; | |
471 | ||
472 | *size_change_ok = true; | |
473 | } | |
474 | ||
475 | /* If we are looking at a dynamic object, and we have found a | |
476 | definition, we need to see if the symbol was already defined by | |
477 | some other object. If so, we want to use the existing | |
478 | definition, and we do not want to report a multiple symbol | |
479 | definition error; we do this by clobbering *PSEC to be | |
480 | bfd_und_section_ptr. | |
481 | ||
482 | We treat a common symbol as a definition if the symbol in the | |
483 | shared library is a function, since common symbols always | |
484 | represent variables; this can cause confusion in principle, but | |
485 | any such confusion would seem to indicate an erroneous program or | |
486 | shared library. We also permit a common symbol in a regular | |
487 | object to override a weak symbol in a shared object. */ | |
488 | ||
489 | if (newdyn | |
490 | && newdef | |
491 | && (olddef | |
492 | || (h->root.type == bfd_link_hash_common | |
493 | && (bind == STB_WEAK | |
494 | || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) | |
495 | { | |
496 | *override = true; | |
497 | newdef = false; | |
498 | newdyncommon = false; | |
499 | ||
500 | *psec = sec = bfd_und_section_ptr; | |
501 | *size_change_ok = true; | |
502 | ||
503 | /* If we get here when the old symbol is a common symbol, then | |
504 | we are explicitly letting it override a weak symbol or | |
505 | function in a dynamic object, and we don't want to warn about | |
506 | a type change. If the old symbol is a defined symbol, a type | |
507 | change warning may still be appropriate. */ | |
508 | ||
509 | if (h->root.type == bfd_link_hash_common) | |
510 | *type_change_ok = true; | |
511 | } | |
512 | ||
513 | /* Handle the special case of an old common symbol merging with a | |
514 | new symbol which looks like a common symbol in a shared object. | |
515 | We change *PSEC and *PVALUE to make the new symbol look like a | |
516 | common symbol, and let _bfd_generic_link_add_one_symbol will do | |
517 | the right thing. */ | |
518 | ||
519 | if (newdyncommon | |
520 | && h->root.type == bfd_link_hash_common) | |
521 | { | |
522 | *override = true; | |
523 | newdef = false; | |
524 | newdyncommon = false; | |
525 | *pvalue = sym->st_size; | |
526 | *psec = sec = bfd_com_section_ptr; | |
527 | *size_change_ok = true; | |
528 | } | |
529 | ||
530 | /* If the old symbol is from a dynamic object, and the new symbol is | |
531 | a definition which is not from a dynamic object, then the new | |
532 | symbol overrides the old symbol. Symbols from regular files | |
533 | always take precedence over symbols from dynamic objects, even if | |
534 | they are defined after the dynamic object in the link. | |
535 | ||
536 | As above, we again permit a common symbol in a regular object to | |
537 | override a definition in a shared object if the shared object | |
538 | symbol is a function or is weak. */ | |
539 | ||
540 | if (! newdyn | |
541 | && (newdef | |
542 | || (bfd_is_com_section (sec) | |
543 | && (h->root.type == bfd_link_hash_defweak | |
544 | || h->type == STT_FUNC))) | |
545 | && olddyn | |
546 | && olddef | |
547 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0) | |
548 | { | |
549 | /* Change the hash table entry to undefined, and let | |
550 | _bfd_generic_link_add_one_symbol do the right thing with the | |
551 | new definition. */ | |
552 | ||
553 | h->root.type = bfd_link_hash_undefined; | |
554 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
555 | *size_change_ok = true; | |
556 | ||
557 | olddef = false; | |
558 | olddyncommon = false; | |
559 | ||
560 | /* We again permit a type change when a common symbol may be | |
561 | overriding a function. */ | |
562 | ||
563 | if (bfd_is_com_section (sec)) | |
564 | *type_change_ok = true; | |
565 | ||
566 | /* This union may have been set to be non-NULL when this symbol | |
567 | was seen in a dynamic object. We must force the union to be | |
568 | NULL, so that it is correct for a regular symbol. */ | |
569 | ||
570 | h->verinfo.vertree = NULL; | |
541a4b54 ILT |
571 | |
572 | /* In this special case, if H is the target of an indirection, | |
573 | we want the caller to frob with H rather than with the | |
574 | indirect symbol. That will permit the caller to redefine the | |
575 | target of the indirection, rather than the indirect symbol | |
862eaedc ILT |
576 | itself. FIXME: This will break the -y option if we store a |
577 | symbol with a different name. */ | |
541a4b54 | 578 | *sym_hash = h; |
044d7d49 ILT |
579 | } |
580 | ||
581 | /* Handle the special case of a new common symbol merging with an | |
582 | old symbol that looks like it might be a common symbol defined in | |
583 | a shared object. Note that we have already handled the case in | |
584 | which a new common symbol should simply override the definition | |
585 | in the shared library. */ | |
586 | ||
587 | if (! newdyn | |
588 | && bfd_is_com_section (sec) | |
589 | && olddyncommon) | |
590 | { | |
591 | /* It would be best if we could set the hash table entry to a | |
592 | common symbol, but we don't know what to use for the section | |
593 | or the alignment. */ | |
594 | if (! ((*info->callbacks->multiple_common) | |
595 | (info, h->root.root.string, oldbfd, bfd_link_hash_common, | |
596 | h->size, abfd, bfd_link_hash_common, sym->st_size))) | |
597 | return false; | |
598 | ||
599 | /* If the predumed common symbol in the dynamic object is | |
600 | larger, pretend that the new symbol has its size. */ | |
601 | ||
602 | if (h->size > *pvalue) | |
603 | *pvalue = h->size; | |
604 | ||
605 | /* FIXME: We no longer know the alignment required by the symbol | |
606 | in the dynamic object, so we just wind up using the one from | |
607 | the regular object. */ | |
608 | ||
609 | olddef = false; | |
610 | olddyncommon = false; | |
611 | ||
612 | h->root.type = bfd_link_hash_undefined; | |
613 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
614 | ||
615 | *size_change_ok = true; | |
616 | *type_change_ok = true; | |
617 | ||
618 | h->verinfo.vertree = NULL; | |
619 | } | |
620 | ||
621 | return true; | |
622 | } | |
623 | ||
ede4eed4 KR |
624 | /* Add symbols from an ELF object file to the linker hash table. */ |
625 | ||
626 | static boolean | |
627 | elf_link_add_object_symbols (abfd, info) | |
628 | bfd *abfd; | |
629 | struct bfd_link_info *info; | |
630 | { | |
631 | boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *, | |
632 | const Elf_Internal_Sym *, | |
633 | const char **, flagword *, | |
634 | asection **, bfd_vma *)); | |
635 | boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *, | |
636 | asection *, const Elf_Internal_Rela *)); | |
637 | boolean collect; | |
638 | Elf_Internal_Shdr *hdr; | |
639 | size_t symcount; | |
640 | size_t extsymcount; | |
641 | size_t extsymoff; | |
642 | Elf_External_Sym *buf = NULL; | |
643 | struct elf_link_hash_entry **sym_hash; | |
644 | boolean dynamic; | |
d044b40a ILT |
645 | bfd_byte *dynver = NULL; |
646 | Elf_External_Versym *extversym = NULL; | |
647 | Elf_External_Versym *ever; | |
ede4eed4 KR |
648 | Elf_External_Dyn *dynbuf = NULL; |
649 | struct elf_link_hash_entry *weaks; | |
650 | Elf_External_Sym *esym; | |
651 | Elf_External_Sym *esymend; | |
652 | ||
653 | add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook; | |
654 | collect = get_elf_backend_data (abfd)->collect; | |
655 | ||
d044b40a ILT |
656 | if ((abfd->flags & DYNAMIC) == 0) |
657 | dynamic = false; | |
658 | else | |
659 | { | |
660 | dynamic = true; | |
661 | ||
662 | /* You can't use -r against a dynamic object. Also, there's no | |
663 | hope of using a dynamic object which does not exactly match | |
664 | the format of the output file. */ | |
665 | if (info->relocateable || info->hash->creator != abfd->xvec) | |
666 | { | |
667 | bfd_set_error (bfd_error_invalid_operation); | |
668 | goto error_return; | |
669 | } | |
670 | } | |
671 | ||
0cb70568 ILT |
672 | /* As a GNU extension, any input sections which are named |
673 | .gnu.warning.SYMBOL are treated as warning symbols for the given | |
674 | symbol. This differs from .gnu.warning sections, which generate | |
675 | warnings when they are included in an output file. */ | |
676 | if (! info->shared) | |
677 | { | |
678 | asection *s; | |
679 | ||
680 | for (s = abfd->sections; s != NULL; s = s->next) | |
681 | { | |
682 | const char *name; | |
683 | ||
684 | name = bfd_get_section_name (abfd, s); | |
685 | if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) | |
686 | { | |
687 | char *msg; | |
688 | bfd_size_type sz; | |
689 | ||
54626f1a ILT |
690 | name += sizeof ".gnu.warning." - 1; |
691 | ||
692 | /* If this is a shared object, then look up the symbol | |
693 | in the hash table. If it is there, and it is already | |
694 | been defined, then we will not be using the entry | |
695 | from this shared object, so we don't need to warn. | |
696 | FIXME: If we see the definition in a regular object | |
697 | later on, we will warn, but we shouldn't. The only | |
698 | fix is to keep track of what warnings we are supposed | |
699 | to emit, and then handle them all at the end of the | |
700 | link. */ | |
d044b40a | 701 | if (dynamic && abfd->xvec == info->hash->creator) |
54626f1a ILT |
702 | { |
703 | struct elf_link_hash_entry *h; | |
704 | ||
705 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
706 | false, false, true); | |
707 | ||
708 | /* FIXME: What about bfd_link_hash_common? */ | |
709 | if (h != NULL | |
710 | && (h->root.type == bfd_link_hash_defined | |
711 | || h->root.type == bfd_link_hash_defweak)) | |
712 | { | |
713 | /* We don't want to issue this warning. Clobber | |
714 | the section size so that the warning does not | |
715 | get copied into the output file. */ | |
716 | s->_raw_size = 0; | |
717 | continue; | |
718 | } | |
719 | } | |
720 | ||
0cb70568 ILT |
721 | sz = bfd_section_size (abfd, s); |
722 | msg = (char *) bfd_alloc (abfd, sz); | |
723 | if (msg == NULL) | |
a9713b91 | 724 | goto error_return; |
0cb70568 ILT |
725 | |
726 | if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz)) | |
727 | goto error_return; | |
728 | ||
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 ILT |
1100 | Elf_Internal_Versym iver; |
1101 | int vernum; | |
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) | |
1126 | ("%s: %s: invalid version %d (max %d)", | |
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) | |
1167 | ("%s: %s: invalid needed version %d", | |
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) |
ba254dc5 ILT |
1280 | ("Warning: size of symbol `%s' changed from %lu to %lu in %s", |
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 ILT |
1301 | (*_bfd_error_handler) |
1302 | ("Warning: type of symbol `%s' changed from %d to %d in %s", | |
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. */ | |
1375 | if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec, | |
1376 | &value, &hi, &override, | |
1377 | &type_change_ok, &size_change_ok)) | |
1378 | goto error_return; | |
d044b40a | 1379 | |
044d7d49 | 1380 | if (! override) |
d6bfcdb5 | 1381 | { |
52c92c7f ILT |
1382 | if (! (_bfd_generic_link_add_one_symbol |
1383 | (info, abfd, shortname, BSF_INDIRECT, | |
1384 | bfd_ind_section_ptr, (bfd_vma) 0, name, false, | |
1385 | collect, (struct bfd_link_hash_entry **) &hi))) | |
1386 | goto error_return; | |
541a4b54 ILT |
1387 | } |
1388 | else | |
1389 | { | |
1390 | /* In this case the symbol named SHORTNAME is | |
1391 | overriding the indirect symbol we want to | |
1392 | add. We were planning on making SHORTNAME an | |
1393 | indirect symbol referring to NAME. SHORTNAME | |
1394 | is the name without a version. NAME is the | |
1395 | fully versioned name, and it is the default | |
1396 | version. | |
1397 | ||
1398 | Overriding means that we already saw a | |
1399 | definition for the symbol SHORTNAME in a | |
1400 | regular object, and it is overriding the | |
1401 | symbol defined in the dynamic object. | |
1402 | ||
1403 | When this happens, we actually want to change | |
1404 | NAME, the symbol we just added, to refer to | |
1405 | SHORTNAME. This will cause references to | |
1406 | NAME in the shared object to become | |
1407 | references to SHORTNAME in the regular | |
1408 | object. This is what we expect when we | |
1409 | override a function in a shared object: that | |
1410 | the references in the shared object will be | |
1411 | mapped to the definition in the regular | |
1412 | object. */ | |
1413 | ||
677525e9 ILT |
1414 | while (hi->root.type == bfd_link_hash_indirect |
1415 | || hi->root.type == bfd_link_hash_warning) | |
1416 | hi = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1417 | ||
541a4b54 ILT |
1418 | h->root.type = bfd_link_hash_indirect; |
1419 | h->root.u.i.link = (struct bfd_link_hash_entry *) hi; | |
1420 | if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) | |
1421 | { | |
1422 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC; | |
1423 | hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC; | |
1424 | if (! _bfd_elf_link_record_dynamic_symbol (info, hi)) | |
1425 | goto error_return; | |
1426 | } | |
52c92c7f | 1427 | |
541a4b54 ILT |
1428 | /* Now set HI to H, so that the following code |
1429 | will set the other fields correctly. */ | |
1430 | hi = h; | |
1431 | } | |
52c92c7f | 1432 | |
541a4b54 ILT |
1433 | /* If there is a duplicate definition somewhere, |
1434 | then HI may not point to an indirect symbol. We | |
1435 | will have reported an error to the user in that | |
1436 | case. */ | |
1437 | ||
1438 | if (hi->root.type == bfd_link_hash_indirect) | |
1439 | { | |
1440 | struct elf_link_hash_entry *ht; | |
1441 | ||
1442 | /* If the symbol became indirect, then we assume | |
1443 | that we have not seen a definition before. */ | |
1444 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1445 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1446 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1447 | == 0); | |
1448 | ||
1449 | ht = (struct elf_link_hash_entry *) hi->root.u.i.link; | |
1450 | ||
1451 | /* Copy down any references that we may have | |
1452 | already seen to the symbol which just became | |
1453 | indirect. */ | |
1454 | ht->elf_link_hash_flags |= | |
1455 | (hi->elf_link_hash_flags | |
1456 | & (ELF_LINK_HASH_REF_DYNAMIC | |
1457 | | ELF_LINK_HASH_REF_REGULAR)); | |
1458 | ||
1459 | /* Copy over the global table offset entry. | |
1460 | This may have been already set up by a | |
1461 | check_relocs routine. */ | |
1462 | if (ht->got_offset == (bfd_vma) -1) | |
52c92c7f | 1463 | { |
541a4b54 ILT |
1464 | ht->got_offset = hi->got_offset; |
1465 | hi->got_offset = (bfd_vma) -1; | |
1466 | } | |
1467 | BFD_ASSERT (hi->got_offset == (bfd_vma) -1); | |
e549b1d2 | 1468 | |
541a4b54 ILT |
1469 | if (ht->dynindx == -1) |
1470 | { | |
1471 | ht->dynindx = hi->dynindx; | |
1472 | ht->dynstr_index = hi->dynstr_index; | |
1473 | hi->dynindx = -1; | |
1474 | hi->dynstr_index = 0; | |
1475 | } | |
1476 | BFD_ASSERT (hi->dynindx == -1); | |
e549b1d2 | 1477 | |
541a4b54 ILT |
1478 | /* FIXME: There may be other information to copy |
1479 | over for particular targets. */ | |
1480 | ||
1481 | /* See if the new flags lead us to realize that | |
1482 | the symbol must be dynamic. */ | |
1483 | if (! dynsym) | |
1484 | { | |
1485 | if (! dynamic) | |
e549b1d2 | 1486 | { |
541a4b54 ILT |
1487 | if (info->shared |
1488 | || ((hi->elf_link_hash_flags | |
1489 | & ELF_LINK_HASH_REF_DYNAMIC) | |
1490 | != 0)) | |
1491 | dynsym = true; | |
e549b1d2 | 1492 | } |
541a4b54 | 1493 | else |
e549b1d2 | 1494 | { |
541a4b54 ILT |
1495 | if ((hi->elf_link_hash_flags |
1496 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1497 | dynsym = true; | |
e549b1d2 | 1498 | } |
52c92c7f | 1499 | } |
d6bfcdb5 ILT |
1500 | } |
1501 | ||
1502 | /* We also need to define an indirection from the | |
1503 | nondefault version of the symbol. */ | |
1504 | ||
1505 | shortname = bfd_hash_allocate (&info->hash->table, | |
1506 | strlen (name)); | |
1507 | if (shortname == NULL) | |
1508 | goto error_return; | |
1509 | strncpy (shortname, name, p - name); | |
1510 | strcpy (shortname + (p - name), p + 1); | |
1511 | ||
044d7d49 ILT |
1512 | /* Once again, merge with any existing symbol. */ |
1513 | if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec, | |
1514 | &value, &hi, &override, | |
1515 | &type_change_ok, &size_change_ok)) | |
1516 | goto error_return; | |
d6bfcdb5 | 1517 | |
541a4b54 ILT |
1518 | if (override) |
1519 | { | |
1520 | /* Here SHORTNAME is a versioned name, so we | |
1521 | don't expect to see the type of override we | |
1522 | do in the case above. */ | |
1523 | (*_bfd_error_handler) | |
1524 | ("%s: warning: unexpected redefinition of `%s'", | |
1525 | bfd_get_filename (abfd), shortname); | |
1526 | } | |
1527 | else | |
d6bfcdb5 | 1528 | { |
52c92c7f ILT |
1529 | if (! (_bfd_generic_link_add_one_symbol |
1530 | (info, abfd, shortname, BSF_INDIRECT, | |
1531 | bfd_ind_section_ptr, (bfd_vma) 0, name, false, | |
1532 | collect, (struct bfd_link_hash_entry **) &hi))) | |
1533 | goto error_return; | |
1534 | ||
1535 | /* If there is a duplicate definition somewhere, | |
1536 | then HI may not point to an indirect symbol. | |
1537 | We will have reported an error to the user in | |
1538 | that case. */ | |
1539 | ||
1540 | if (hi->root.type == bfd_link_hash_indirect) | |
1541 | { | |
e549b1d2 ILT |
1542 | /* If the symbol became indirect, then we |
1543 | assume that we have not seen a definition | |
1544 | before. */ | |
1545 | BFD_ASSERT ((hi->elf_link_hash_flags | |
1546 | & (ELF_LINK_HASH_DEF_DYNAMIC | |
1547 | | ELF_LINK_HASH_DEF_REGULAR)) | |
1548 | == 0); | |
1549 | ||
1550 | /* Copy down any references that we may have | |
1551 | already seen to the symbol which just | |
1552 | became indirect. */ | |
1553 | h->elf_link_hash_flags |= | |
1554 | (hi->elf_link_hash_flags | |
1555 | & (ELF_LINK_HASH_REF_DYNAMIC | |
1556 | | ELF_LINK_HASH_REF_REGULAR)); | |
1557 | ||
1558 | /* Copy over the global table offset entry. | |
1559 | This may have been already set up by a | |
1560 | check_relocs routine. */ | |
1561 | if (h->got_offset == (bfd_vma) -1) | |
1562 | { | |
1563 | h->got_offset = hi->got_offset; | |
1564 | hi->got_offset = (bfd_vma) -1; | |
1565 | } | |
1566 | BFD_ASSERT (hi->got_offset == (bfd_vma) -1); | |
1567 | ||
1568 | if (h->dynindx == -1) | |
1569 | { | |
1570 | h->dynindx = hi->dynindx; | |
1571 | h->dynstr_index = hi->dynstr_index; | |
1572 | hi->dynindx = -1; | |
1573 | hi->dynstr_index = 0; | |
1574 | } | |
1575 | BFD_ASSERT (hi->dynindx == -1); | |
1576 | ||
1577 | /* FIXME: There may be other information to | |
1578 | copy over for particular targets. */ | |
1579 | ||
1580 | /* See if the new flags lead us to realize | |
1581 | that the symbol must be dynamic. */ | |
1582 | if (! dynsym) | |
1583 | { | |
1584 | if (! dynamic) | |
1585 | { | |
1586 | if (info->shared | |
1587 | || ((hi->elf_link_hash_flags | |
1588 | & ELF_LINK_HASH_REF_DYNAMIC) | |
1589 | != 0)) | |
1590 | dynsym = true; | |
1591 | } | |
1592 | else | |
1593 | { | |
1594 | if ((hi->elf_link_hash_flags | |
1595 | & ELF_LINK_HASH_REF_REGULAR) != 0) | |
1596 | dynsym = true; | |
1597 | } | |
1598 | } | |
52c92c7f | 1599 | } |
d6bfcdb5 | 1600 | } |
d044b40a ILT |
1601 | } |
1602 | } | |
1603 | ||
ede4eed4 KR |
1604 | if (dynsym && h->dynindx == -1) |
1605 | { | |
1606 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1607 | goto error_return; | |
452a5efb ILT |
1608 | if (h->weakdef != NULL |
1609 | && ! new_weakdef | |
1610 | && h->weakdef->dynindx == -1) | |
1611 | { | |
1612 | if (! _bfd_elf_link_record_dynamic_symbol (info, | |
1613 | h->weakdef)) | |
1614 | goto error_return; | |
1615 | } | |
ede4eed4 KR |
1616 | } |
1617 | } | |
1618 | } | |
1619 | ||
1620 | /* Now set the weakdefs field correctly for all the weak defined | |
1621 | symbols we found. The only way to do this is to search all the | |
1622 | symbols. Since we only need the information for non functions in | |
1623 | dynamic objects, that's the only time we actually put anything on | |
1624 | the list WEAKS. We need this information so that if a regular | |
1625 | object refers to a symbol defined weakly in a dynamic object, the | |
1626 | real symbol in the dynamic object is also put in the dynamic | |
1627 | symbols; we also must arrange for both symbols to point to the | |
1628 | same memory location. We could handle the general case of symbol | |
1629 | aliasing, but a general symbol alias can only be generated in | |
1630 | assembler code, handling it correctly would be very time | |
1631 | consuming, and other ELF linkers don't handle general aliasing | |
1632 | either. */ | |
1633 | while (weaks != NULL) | |
1634 | { | |
1635 | struct elf_link_hash_entry *hlook; | |
1636 | asection *slook; | |
1637 | bfd_vma vlook; | |
1638 | struct elf_link_hash_entry **hpp; | |
1639 | struct elf_link_hash_entry **hppend; | |
1640 | ||
1641 | hlook = weaks; | |
1642 | weaks = hlook->weakdef; | |
1643 | hlook->weakdef = NULL; | |
1644 | ||
1645 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined | |
1646 | || hlook->root.type == bfd_link_hash_defweak | |
1647 | || hlook->root.type == bfd_link_hash_common | |
1648 | || hlook->root.type == bfd_link_hash_indirect); | |
1649 | slook = hlook->root.u.def.section; | |
1650 | vlook = hlook->root.u.def.value; | |
1651 | ||
1652 | hpp = elf_sym_hashes (abfd); | |
1653 | hppend = hpp + extsymcount; | |
1654 | for (; hpp < hppend; hpp++) | |
1655 | { | |
1656 | struct elf_link_hash_entry *h; | |
1657 | ||
1658 | h = *hpp; | |
1659 | if (h != NULL && h != hlook | |
d2bb6c79 | 1660 | && h->root.type == bfd_link_hash_defined |
ede4eed4 KR |
1661 | && h->root.u.def.section == slook |
1662 | && h->root.u.def.value == vlook) | |
1663 | { | |
1664 | hlook->weakdef = h; | |
1665 | ||
1666 | /* If the weak definition is in the list of dynamic | |
1667 | symbols, make sure the real definition is put there | |
1668 | as well. */ | |
1669 | if (hlook->dynindx != -1 | |
1670 | && h->dynindx == -1) | |
1671 | { | |
1672 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1673 | goto error_return; | |
1674 | } | |
1675 | ||
440f3914 ILT |
1676 | /* If the real definition is in the list of dynamic |
1677 | symbols, make sure the weak definition is put there | |
1678 | as well. If we don't do this, then the dynamic | |
1679 | loader might not merge the entries for the real | |
1680 | definition and the weak definition. */ | |
1681 | if (h->dynindx != -1 | |
1682 | && hlook->dynindx == -1) | |
1683 | { | |
1684 | if (! _bfd_elf_link_record_dynamic_symbol (info, hlook)) | |
1685 | goto error_return; | |
1686 | } | |
1687 | ||
ede4eed4 KR |
1688 | break; |
1689 | } | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | if (buf != NULL) | |
1694 | { | |
1695 | free (buf); | |
1696 | buf = NULL; | |
1697 | } | |
1698 | ||
d044b40a ILT |
1699 | if (extversym != NULL) |
1700 | { | |
1701 | free (extversym); | |
1702 | extversym = NULL; | |
1703 | } | |
1704 | ||
ede4eed4 KR |
1705 | /* If this object is the same format as the output object, and it is |
1706 | not a shared library, then let the backend look through the | |
1707 | relocs. | |
1708 | ||
1709 | This is required to build global offset table entries and to | |
1710 | arrange for dynamic relocs. It is not required for the | |
1711 | particular common case of linking non PIC code, even when linking | |
1712 | against shared libraries, but unfortunately there is no way of | |
1713 | knowing whether an object file has been compiled PIC or not. | |
1714 | Looking through the relocs is not particularly time consuming. | |
1715 | The problem is that we must either (1) keep the relocs in memory, | |
1716 | which causes the linker to require additional runtime memory or | |
1717 | (2) read the relocs twice from the input file, which wastes time. | |
1718 | This would be a good case for using mmap. | |
1719 | ||
1720 | I have no idea how to handle linking PIC code into a file of a | |
1721 | different format. It probably can't be done. */ | |
1722 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
1723 | if (! dynamic | |
1724 | && abfd->xvec == info->hash->creator | |
1725 | && check_relocs != NULL) | |
1726 | { | |
1727 | asection *o; | |
1728 | ||
1729 | for (o = abfd->sections; o != NULL; o = o->next) | |
1730 | { | |
1731 | Elf_Internal_Rela *internal_relocs; | |
1732 | boolean ok; | |
1733 | ||
1734 | if ((o->flags & SEC_RELOC) == 0 | |
a0c80726 ILT |
1735 | || o->reloc_count == 0 |
1736 | || ((info->strip == strip_all || info->strip == strip_debugger) | |
94e05b00 ILT |
1737 | && (o->flags & SEC_DEBUGGING) != 0) |
1738 | || bfd_is_abs_section (o->output_section)) | |
ede4eed4 KR |
1739 | continue; |
1740 | ||
c86158e5 ILT |
1741 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
1742 | (abfd, o, (PTR) NULL, | |
1743 | (Elf_Internal_Rela *) NULL, | |
1744 | info->keep_memory)); | |
ede4eed4 KR |
1745 | if (internal_relocs == NULL) |
1746 | goto error_return; | |
1747 | ||
1748 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
1749 | ||
1750 | if (! info->keep_memory) | |
1751 | free (internal_relocs); | |
1752 | ||
1753 | if (! ok) | |
1754 | goto error_return; | |
1755 | } | |
1756 | } | |
1757 | ||
1726b8f0 ILT |
1758 | /* If this is a non-traditional, non-relocateable link, try to |
1759 | optimize the handling of the .stab/.stabstr sections. */ | |
1760 | if (! dynamic | |
1761 | && ! info->relocateable | |
1762 | && ! info->traditional_format | |
1763 | && info->hash->creator->flavour == bfd_target_elf_flavour | |
1764 | && (info->strip != strip_all && info->strip != strip_debugger)) | |
1765 | { | |
1766 | asection *stab, *stabstr; | |
1767 | ||
1768 | stab = bfd_get_section_by_name (abfd, ".stab"); | |
1769 | if (stab != NULL) | |
1770 | { | |
1771 | stabstr = bfd_get_section_by_name (abfd, ".stabstr"); | |
1772 | ||
1773 | if (stabstr != NULL) | |
1774 | { | |
1775 | struct bfd_elf_section_data *secdata; | |
1776 | ||
1777 | secdata = elf_section_data (stab); | |
1778 | if (! _bfd_link_section_stabs (abfd, | |
1779 | &elf_hash_table (info)->stab_info, | |
1780 | stab, stabstr, | |
1781 | &secdata->stab_info)) | |
1782 | goto error_return; | |
1783 | } | |
1784 | } | |
1785 | } | |
1786 | ||
ede4eed4 KR |
1787 | return true; |
1788 | ||
1789 | error_return: | |
1790 | if (buf != NULL) | |
1791 | free (buf); | |
1792 | if (dynbuf != NULL) | |
1793 | free (dynbuf); | |
d044b40a ILT |
1794 | if (dynver != NULL) |
1795 | free (dynver); | |
1796 | if (extversym != NULL) | |
1797 | free (extversym); | |
ede4eed4 KR |
1798 | return false; |
1799 | } | |
1800 | ||
1801 | /* Create some sections which will be filled in with dynamic linking | |
1802 | information. ABFD is an input file which requires dynamic sections | |
1803 | to be created. The dynamic sections take up virtual memory space | |
1804 | when the final executable is run, so we need to create them before | |
1805 | addresses are assigned to the output sections. We work out the | |
1806 | actual contents and size of these sections later. */ | |
1807 | ||
1808 | boolean | |
1809 | elf_link_create_dynamic_sections (abfd, info) | |
1810 | bfd *abfd; | |
1811 | struct bfd_link_info *info; | |
1812 | { | |
1813 | flagword flags; | |
1814 | register asection *s; | |
1815 | struct elf_link_hash_entry *h; | |
1816 | struct elf_backend_data *bed; | |
1817 | ||
1818 | if (elf_hash_table (info)->dynamic_sections_created) | |
1819 | return true; | |
1820 | ||
1821 | /* Make sure that all dynamic sections use the same input BFD. */ | |
1822 | if (elf_hash_table (info)->dynobj == NULL) | |
1823 | elf_hash_table (info)->dynobj = abfd; | |
1824 | else | |
1825 | abfd = elf_hash_table (info)->dynobj; | |
1826 | ||
1827 | /* Note that we set the SEC_IN_MEMORY flag for all of these | |
1828 | sections. */ | |
ff12f303 ILT |
1829 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
1830 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
ede4eed4 KR |
1831 | |
1832 | /* A dynamically linked executable has a .interp section, but a | |
1833 | shared library does not. */ | |
1834 | if (! info->shared) | |
1835 | { | |
1836 | s = bfd_make_section (abfd, ".interp"); | |
1837 | if (s == NULL | |
1838 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
1839 | return false; | |
1840 | } | |
1841 | ||
d044b40a ILT |
1842 | /* Create sections to hold version informations. These are removed |
1843 | if they are not needed. */ | |
1844 | s = bfd_make_section (abfd, ".gnu.version_d"); | |
1845 | if (s == NULL | |
1846 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
65c2dd6e | 1847 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
d044b40a ILT |
1848 | return false; |
1849 | ||
1850 | s = bfd_make_section (abfd, ".gnu.version"); | |
1851 | if (s == NULL | |
1852 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1853 | || ! bfd_set_section_alignment (abfd, s, 1)) | |
1854 | return false; | |
1855 | ||
1856 | s = bfd_make_section (abfd, ".gnu.version_r"); | |
1857 | if (s == NULL | |
1858 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
65c2dd6e | 1859 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) |
d044b40a ILT |
1860 | return false; |
1861 | ||
ede4eed4 KR |
1862 | s = bfd_make_section (abfd, ".dynsym"); |
1863 | if (s == NULL | |
1864 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1865 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
1866 | return false; | |
1867 | ||
1868 | s = bfd_make_section (abfd, ".dynstr"); | |
1869 | if (s == NULL | |
1870 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
1871 | return false; | |
1872 | ||
1873 | /* Create a strtab to hold the dynamic symbol names. */ | |
1874 | if (elf_hash_table (info)->dynstr == NULL) | |
1875 | { | |
1876 | elf_hash_table (info)->dynstr = elf_stringtab_init (); | |
1877 | if (elf_hash_table (info)->dynstr == NULL) | |
1878 | return false; | |
1879 | } | |
1880 | ||
1881 | s = bfd_make_section (abfd, ".dynamic"); | |
1882 | if (s == NULL | |
1883 | || ! bfd_set_section_flags (abfd, s, flags) | |
1884 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
1885 | return false; | |
1886 | ||
1887 | /* The special symbol _DYNAMIC is always set to the start of the | |
1888 | .dynamic section. This call occurs before we have processed the | |
1889 | symbols for any dynamic object, so we don't have to worry about | |
1890 | overriding a dynamic definition. We could set _DYNAMIC in a | |
1891 | linker script, but we only want to define it if we are, in fact, | |
1892 | creating a .dynamic section. We don't want to define it if there | |
1893 | is no .dynamic section, since on some ELF platforms the start up | |
1894 | code examines it to decide how to initialize the process. */ | |
1895 | h = NULL; | |
1896 | if (! (_bfd_generic_link_add_one_symbol | |
1897 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0, | |
1898 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
1899 | (struct bfd_link_hash_entry **) &h))) | |
1900 | return false; | |
1901 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
1902 | h->type = STT_OBJECT; | |
1903 | ||
1904 | if (info->shared | |
1905 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
1906 | return false; | |
1907 | ||
1908 | s = bfd_make_section (abfd, ".hash"); | |
1909 | if (s == NULL | |
1910 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
1911 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
1912 | return false; | |
1913 | ||
1914 | /* Let the backend create the rest of the sections. This lets the | |
1915 | backend set the right flags. The backend will normally create | |
1916 | the .got and .plt sections. */ | |
1917 | bed = get_elf_backend_data (abfd); | |
1918 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) | |
1919 | return false; | |
1920 | ||
1921 | elf_hash_table (info)->dynamic_sections_created = true; | |
1922 | ||
1923 | return true; | |
1924 | } | |
1925 | ||
1926 | /* Add an entry to the .dynamic table. */ | |
1927 | ||
1928 | boolean | |
1929 | elf_add_dynamic_entry (info, tag, val) | |
1930 | struct bfd_link_info *info; | |
1931 | bfd_vma tag; | |
1932 | bfd_vma val; | |
1933 | { | |
1934 | Elf_Internal_Dyn dyn; | |
1935 | bfd *dynobj; | |
1936 | asection *s; | |
1937 | size_t newsize; | |
1938 | bfd_byte *newcontents; | |
1939 | ||
1940 | dynobj = elf_hash_table (info)->dynobj; | |
1941 | ||
1942 | s = bfd_get_section_by_name (dynobj, ".dynamic"); | |
1943 | BFD_ASSERT (s != NULL); | |
1944 | ||
1945 | newsize = s->_raw_size + sizeof (Elf_External_Dyn); | |
58142f10 | 1946 | newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); |
ede4eed4 | 1947 | if (newcontents == NULL) |
58142f10 | 1948 | return false; |
ede4eed4 KR |
1949 | |
1950 | dyn.d_tag = tag; | |
1951 | dyn.d_un.d_val = val; | |
1952 | elf_swap_dyn_out (dynobj, &dyn, | |
1953 | (Elf_External_Dyn *) (newcontents + s->_raw_size)); | |
1954 | ||
1955 | s->_raw_size = newsize; | |
1956 | s->contents = newcontents; | |
1957 | ||
1958 | return true; | |
1959 | } | |
3b3753b8 | 1960 | \f |
ede4eed4 KR |
1961 | |
1962 | /* Read and swap the relocs for a section. They may have been cached. | |
1963 | If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL, | |
1964 | they are used as buffers to read into. They are known to be large | |
1965 | enough. If the INTERNAL_RELOCS relocs argument is NULL, the return | |
1966 | value is allocated using either malloc or bfd_alloc, according to | |
1967 | the KEEP_MEMORY argument. */ | |
1968 | ||
c86158e5 ILT |
1969 | Elf_Internal_Rela * |
1970 | NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs, | |
1971 | keep_memory) | |
ede4eed4 KR |
1972 | bfd *abfd; |
1973 | asection *o; | |
1974 | PTR external_relocs; | |
1975 | Elf_Internal_Rela *internal_relocs; | |
1976 | boolean keep_memory; | |
1977 | { | |
1978 | Elf_Internal_Shdr *rel_hdr; | |
1979 | PTR alloc1 = NULL; | |
1980 | Elf_Internal_Rela *alloc2 = NULL; | |
1981 | ||
1982 | if (elf_section_data (o)->relocs != NULL) | |
1983 | return elf_section_data (o)->relocs; | |
1984 | ||
1985 | if (o->reloc_count == 0) | |
1986 | return NULL; | |
1987 | ||
1988 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
1989 | ||
1990 | if (internal_relocs == NULL) | |
1991 | { | |
1992 | size_t size; | |
1993 | ||
1994 | size = o->reloc_count * sizeof (Elf_Internal_Rela); | |
1995 | if (keep_memory) | |
1996 | internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size); | |
1997 | else | |
58142f10 | 1998 | internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); |
ede4eed4 | 1999 | if (internal_relocs == NULL) |
58142f10 | 2000 | goto error_return; |
ede4eed4 KR |
2001 | } |
2002 | ||
2003 | if (external_relocs == NULL) | |
2004 | { | |
58142f10 | 2005 | alloc1 = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size); |
ede4eed4 | 2006 | if (alloc1 == NULL) |
58142f10 | 2007 | goto error_return; |
ede4eed4 KR |
2008 | external_relocs = alloc1; |
2009 | } | |
2010 | ||
2011 | if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0) | |
2012 | || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd) | |
2013 | != rel_hdr->sh_size)) | |
2014 | goto error_return; | |
2015 | ||
2016 | /* Swap in the relocs. For convenience, we always produce an | |
2017 | Elf_Internal_Rela array; if the relocs are Rel, we set the addend | |
2018 | to 0. */ | |
2019 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
2020 | { | |
2021 | Elf_External_Rel *erel; | |
2022 | Elf_External_Rel *erelend; | |
2023 | Elf_Internal_Rela *irela; | |
2024 | ||
2025 | erel = (Elf_External_Rel *) external_relocs; | |
2026 | erelend = erel + o->reloc_count; | |
2027 | irela = internal_relocs; | |
2028 | for (; erel < erelend; erel++, irela++) | |
2029 | { | |
2030 | Elf_Internal_Rel irel; | |
2031 | ||
2032 | elf_swap_reloc_in (abfd, erel, &irel); | |
2033 | irela->r_offset = irel.r_offset; | |
2034 | irela->r_info = irel.r_info; | |
2035 | irela->r_addend = 0; | |
2036 | } | |
2037 | } | |
2038 | else | |
2039 | { | |
2040 | Elf_External_Rela *erela; | |
2041 | Elf_External_Rela *erelaend; | |
2042 | Elf_Internal_Rela *irela; | |
2043 | ||
2044 | BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela)); | |
2045 | ||
2046 | erela = (Elf_External_Rela *) external_relocs; | |
2047 | erelaend = erela + o->reloc_count; | |
2048 | irela = internal_relocs; | |
2049 | for (; erela < erelaend; erela++, irela++) | |
2050 | elf_swap_reloca_in (abfd, erela, irela); | |
2051 | } | |
2052 | ||
2053 | /* Cache the results for next time, if we can. */ | |
2054 | if (keep_memory) | |
2055 | elf_section_data (o)->relocs = internal_relocs; | |
ff12f303 | 2056 | |
ede4eed4 KR |
2057 | if (alloc1 != NULL) |
2058 | free (alloc1); | |
2059 | ||
2060 | /* Don't free alloc2, since if it was allocated we are passing it | |
2061 | back (under the name of internal_relocs). */ | |
2062 | ||
2063 | return internal_relocs; | |
2064 | ||
2065 | error_return: | |
2066 | if (alloc1 != NULL) | |
2067 | free (alloc1); | |
2068 | if (alloc2 != NULL) | |
2069 | free (alloc2); | |
2070 | return NULL; | |
2071 | } | |
3b3753b8 | 2072 | \f |
ede4eed4 KR |
2073 | |
2074 | /* Record an assignment to a symbol made by a linker script. We need | |
2075 | this in case some dynamic object refers to this symbol. */ | |
2076 | ||
2077 | /*ARGSUSED*/ | |
2078 | boolean | |
2079 | NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide) | |
2080 | bfd *output_bfd; | |
2081 | struct bfd_link_info *info; | |
2082 | const char *name; | |
2083 | boolean provide; | |
2084 | { | |
2085 | struct elf_link_hash_entry *h; | |
2086 | ||
2087 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
2088 | return true; | |
2089 | ||
2090 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false); | |
2091 | if (h == NULL) | |
2092 | return false; | |
2093 | ||
869b7d80 ILT |
2094 | if (h->root.type == bfd_link_hash_new) |
2095 | h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF; | |
2096 | ||
ede4eed4 KR |
2097 | /* If this symbol is being provided by the linker script, and it is |
2098 | currently defined by a dynamic object, but not by a regular | |
2099 | object, then mark it as undefined so that the generic linker will | |
2100 | force the correct value. */ | |
2101 | if (provide | |
2102 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2103 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2104 | h->root.type = bfd_link_hash_undefined; | |
2105 | ||
13eb6306 ILT |
2106 | /* If this symbol is not being provided by the linker script, and it is |
2107 | currently defined by a dynamic object, but not by a regular object, | |
2108 | then clear out any version information because the symbol will not be | |
2109 | associated with the dynamic object any more. */ | |
2110 | if (!provide | |
2111 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2112 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2113 | h->verinfo.verdef = NULL; | |
2114 | ||
ede4eed4 KR |
2115 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; |
2116 | h->type = STT_OBJECT; | |
2117 | ||
2118 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
2119 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
2120 | || info->shared) | |
2121 | && h->dynindx == -1) | |
2122 | { | |
2123 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
2124 | return false; | |
2125 | ||
2126 | /* If this is a weak defined symbol, and we know a corresponding | |
2127 | real symbol from the same dynamic object, make sure the real | |
2128 | symbol is also made into a dynamic symbol. */ | |
2129 | if (h->weakdef != NULL | |
2130 | && h->weakdef->dynindx == -1) | |
2131 | { | |
2132 | if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef)) | |
2133 | return false; | |
2134 | } | |
2135 | } | |
2136 | ||
2137 | return true; | |
2138 | } | |
3b3753b8 | 2139 | \f |
d044b40a ILT |
2140 | /* This structure is used to pass information to |
2141 | elf_link_assign_sym_version. */ | |
2142 | ||
2143 | struct elf_assign_sym_version_info | |
2144 | { | |
2145 | /* Output BFD. */ | |
2146 | bfd *output_bfd; | |
2147 | /* General link information. */ | |
2148 | struct bfd_link_info *info; | |
2149 | /* Version tree. */ | |
2150 | struct bfd_elf_version_tree *verdefs; | |
2151 | /* Whether we are exporting all dynamic symbols. */ | |
2152 | boolean export_dynamic; | |
2153 | /* Whether we removed any symbols from the dynamic symbol table. */ | |
2154 | boolean removed_dynamic; | |
2155 | /* Whether we had a failure. */ | |
2156 | boolean failed; | |
2157 | }; | |
2158 | ||
2159 | /* This structure is used to pass information to | |
2160 | elf_link_find_version_dependencies. */ | |
2161 | ||
2162 | struct elf_find_verdep_info | |
2163 | { | |
2164 | /* Output BFD. */ | |
2165 | bfd *output_bfd; | |
2166 | /* General link information. */ | |
2167 | struct bfd_link_info *info; | |
2168 | /* The number of dependencies. */ | |
2169 | unsigned int vers; | |
2170 | /* Whether we had a failure. */ | |
2171 | boolean failed; | |
2172 | }; | |
ede4eed4 KR |
2173 | |
2174 | /* Array used to determine the number of hash table buckets to use | |
2175 | based on the number of symbols there are. If there are fewer than | |
2176 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
2177 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
6b8ec6f3 | 2178 | than 32771 buckets. */ |
ede4eed4 KR |
2179 | |
2180 | static const size_t elf_buckets[] = | |
2181 | { | |
6b8ec6f3 ILT |
2182 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, |
2183 | 16411, 32771, 0 | |
ede4eed4 KR |
2184 | }; |
2185 | ||
2186 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
2187 | called by the ELF linker emulation before_allocation routine. We | |
2188 | must set the sizes of the sections before the linker sets the | |
2189 | addresses of the various sections. */ | |
2190 | ||
2191 | boolean | |
2192 | NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, | |
148437ec | 2193 | export_dynamic, filter_shlib, |
d044b40a ILT |
2194 | auxiliary_filters, info, sinterpptr, |
2195 | verdefs) | |
ede4eed4 KR |
2196 | bfd *output_bfd; |
2197 | const char *soname; | |
2198 | const char *rpath; | |
2199 | boolean export_dynamic; | |
148437ec | 2200 | const char *filter_shlib; |
db109ca2 | 2201 | const char * const *auxiliary_filters; |
ede4eed4 KR |
2202 | struct bfd_link_info *info; |
2203 | asection **sinterpptr; | |
d044b40a | 2204 | struct bfd_elf_version_tree *verdefs; |
ede4eed4 | 2205 | { |
d044b40a | 2206 | bfd_size_type soname_indx; |
ede4eed4 KR |
2207 | bfd *dynobj; |
2208 | struct elf_backend_data *bed; | |
e549b1d2 | 2209 | bfd_size_type old_dynsymcount; |
c19fbe0f | 2210 | struct elf_assign_sym_version_info asvinfo; |
ede4eed4 KR |
2211 | |
2212 | *sinterpptr = NULL; | |
2213 | ||
d044b40a ILT |
2214 | soname_indx = -1; |
2215 | ||
ede4eed4 KR |
2216 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
2217 | return true; | |
2218 | ||
ff12f303 ILT |
2219 | /* The backend may have to create some sections regardless of whether |
2220 | we're dynamic or not. */ | |
2221 | bed = get_elf_backend_data (output_bfd); | |
2222 | if (bed->elf_backend_always_size_sections | |
2223 | && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) | |
2224 | return false; | |
2225 | ||
ede4eed4 KR |
2226 | dynobj = elf_hash_table (info)->dynobj; |
2227 | ||
2228 | /* If there were no dynamic objects in the link, there is nothing to | |
2229 | do here. */ | |
2230 | if (dynobj == NULL) | |
2231 | return true; | |
2232 | ||
2233 | /* If we are supposed to export all symbols into the dynamic symbol | |
2234 | table (this is not the normal case), then do so. */ | |
2235 | if (export_dynamic) | |
2236 | { | |
2237 | struct elf_info_failed eif; | |
2238 | ||
2239 | eif.failed = false; | |
2240 | eif.info = info; | |
2241 | elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol, | |
2242 | (PTR) &eif); | |
2243 | if (eif.failed) | |
2244 | return false; | |
2245 | } | |
2246 | ||
2247 | if (elf_hash_table (info)->dynamic_sections_created) | |
2248 | { | |
2249 | struct elf_info_failed eif; | |
73a68447 | 2250 | struct elf_link_hash_entry *h; |
ede4eed4 KR |
2251 | bfd_size_type strsize; |
2252 | ||
2253 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
2254 | BFD_ASSERT (*sinterpptr != NULL || info->shared); | |
2255 | ||
2256 | if (soname != NULL) | |
2257 | { | |
d044b40a ILT |
2258 | soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, |
2259 | soname, true, true); | |
2260 | if (soname_indx == (bfd_size_type) -1 | |
2261 | || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) | |
ede4eed4 | 2262 | return false; |
ff12f303 | 2263 | } |
ede4eed4 | 2264 | |
951fe66d ILT |
2265 | if (info->symbolic) |
2266 | { | |
2267 | if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) | |
2268 | return false; | |
2269 | } | |
2270 | ||
ede4eed4 KR |
2271 | if (rpath != NULL) |
2272 | { | |
2273 | bfd_size_type indx; | |
2274 | ||
2275 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath, | |
2276 | true, true); | |
2277 | if (indx == (bfd_size_type) -1 | |
2278 | || ! elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
2279 | return false; | |
2280 | } | |
2281 | ||
148437ec ILT |
2282 | if (filter_shlib != NULL) |
2283 | { | |
2284 | bfd_size_type indx; | |
2285 | ||
2286 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2287 | filter_shlib, true, true); | |
2288 | if (indx == (bfd_size_type) -1 | |
2289 | || ! elf_add_dynamic_entry (info, DT_FILTER, indx)) | |
2290 | return false; | |
2291 | } | |
2292 | ||
db109ca2 | 2293 | if (auxiliary_filters != NULL) |
148437ec | 2294 | { |
db109ca2 | 2295 | const char * const *p; |
148437ec | 2296 | |
db109ca2 ILT |
2297 | for (p = auxiliary_filters; *p != NULL; p++) |
2298 | { | |
2299 | bfd_size_type indx; | |
2300 | ||
2301 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2302 | *p, true, true); | |
2303 | if (indx == (bfd_size_type) -1 | |
2304 | || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) | |
2305 | return false; | |
2306 | } | |
148437ec ILT |
2307 | } |
2308 | ||
c19fbe0f ILT |
2309 | /* Attach all the symbols to their version information. */ |
2310 | asvinfo.output_bfd = output_bfd; | |
2311 | asvinfo.info = info; | |
2312 | asvinfo.verdefs = verdefs; | |
2313 | asvinfo.export_dynamic = export_dynamic; | |
2314 | asvinfo.removed_dynamic = false; | |
2315 | asvinfo.failed = false; | |
2316 | ||
2317 | elf_link_hash_traverse (elf_hash_table (info), | |
2318 | elf_link_assign_sym_version, | |
2319 | (PTR) &asvinfo); | |
2320 | if (asvinfo.failed) | |
2321 | return false; | |
2322 | ||
ede4eed4 KR |
2323 | /* Find all symbols which were defined in a dynamic object and make |
2324 | the backend pick a reasonable value for them. */ | |
2325 | eif.failed = false; | |
2326 | eif.info = info; | |
2327 | elf_link_hash_traverse (elf_hash_table (info), | |
2328 | elf_adjust_dynamic_symbol, | |
2329 | (PTR) &eif); | |
2330 | if (eif.failed) | |
2331 | return false; | |
2332 | ||
2333 | /* Add some entries to the .dynamic section. We fill in some of the | |
2334 | values later, in elf_bfd_final_link, but we must add the entries | |
2335 | now so that we know the final size of the .dynamic section. */ | |
73a68447 ILT |
2336 | h = elf_link_hash_lookup (elf_hash_table (info), "_init", false, |
2337 | false, false); | |
2338 | if (h != NULL | |
2339 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
2340 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
ede4eed4 KR |
2341 | { |
2342 | if (! elf_add_dynamic_entry (info, DT_INIT, 0)) | |
2343 | return false; | |
2344 | } | |
73a68447 ILT |
2345 | h = elf_link_hash_lookup (elf_hash_table (info), "_fini", false, |
2346 | false, false); | |
2347 | if (h != NULL | |
2348 | && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR | |
2349 | | ELF_LINK_HASH_DEF_REGULAR)) != 0) | |
ede4eed4 KR |
2350 | { |
2351 | if (! elf_add_dynamic_entry (info, DT_FINI, 0)) | |
2352 | return false; | |
2353 | } | |
2354 | strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
2355 | if (! elf_add_dynamic_entry (info, DT_HASH, 0) | |
2356 | || ! elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
2357 | || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
2358 | || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
2359 | || ! elf_add_dynamic_entry (info, DT_SYMENT, | |
2360 | sizeof (Elf_External_Sym))) | |
2361 | return false; | |
2362 | } | |
2363 | ||
2364 | /* The backend must work out the sizes of all the other dynamic | |
2365 | sections. */ | |
e549b1d2 | 2366 | old_dynsymcount = elf_hash_table (info)->dynsymcount; |
ede4eed4 KR |
2367 | if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) |
2368 | return false; | |
2369 | ||
2370 | if (elf_hash_table (info)->dynamic_sections_created) | |
2371 | { | |
2372 | size_t dynsymcount; | |
2373 | asection *s; | |
2374 | size_t i; | |
2375 | size_t bucketcount = 0; | |
2376 | Elf_Internal_Sym isym; | |
2377 | ||
d044b40a ILT |
2378 | /* Set up the version definition section. */ |
2379 | s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); | |
2380 | BFD_ASSERT (s != NULL); | |
d6bfcdb5 | 2381 | |
d6bfcdb5 ILT |
2382 | /* We may have created additional version definitions if we are |
2383 | just linking a regular application. */ | |
c19fbe0f | 2384 | verdefs = asvinfo.verdefs; |
d6bfcdb5 | 2385 | |
d044b40a ILT |
2386 | if (verdefs == NULL) |
2387 | { | |
d044b40a ILT |
2388 | asection **spp; |
2389 | ||
d044b40a ILT |
2390 | /* Don't include this section in the output file. */ |
2391 | for (spp = &output_bfd->sections; | |
2392 | *spp != s->output_section; | |
2393 | spp = &(*spp)->next) | |
2394 | ; | |
2395 | *spp = s->output_section->next; | |
2396 | --output_bfd->section_count; | |
2397 | } | |
2398 | else | |
2399 | { | |
d044b40a ILT |
2400 | unsigned int cdefs; |
2401 | bfd_size_type size; | |
2402 | struct bfd_elf_version_tree *t; | |
2403 | bfd_byte *p; | |
2404 | Elf_Internal_Verdef def; | |
2405 | Elf_Internal_Verdaux defaux; | |
2406 | ||
c19fbe0f | 2407 | if (asvinfo.removed_dynamic) |
d044b40a ILT |
2408 | { |
2409 | /* Some dynamic symbols were changed to be local | |
e549b1d2 ILT |
2410 | symbols. In this case, we renumber all of the |
2411 | dynamic symbols, so that we don't have a hole. If | |
2412 | the backend changed dynsymcount, then assume that the | |
2413 | new symbols are at the start. This is the case on | |
2414 | the MIPS. FIXME: The names of the removed symbols | |
2415 | will still be in the dynamic string table, wasting | |
2416 | space. */ | |
2417 | elf_hash_table (info)->dynsymcount = | |
2418 | 1 + (elf_hash_table (info)->dynsymcount - old_dynsymcount); | |
d044b40a ILT |
2419 | elf_link_hash_traverse (elf_hash_table (info), |
2420 | elf_link_renumber_dynsyms, | |
2421 | (PTR) info); | |
2422 | } | |
2423 | ||
2424 | cdefs = 0; | |
2425 | size = 0; | |
2426 | ||
2427 | /* Make space for the base version. */ | |
2428 | size += sizeof (Elf_External_Verdef); | |
2429 | size += sizeof (Elf_External_Verdaux); | |
2430 | ++cdefs; | |
2431 | ||
2432 | for (t = verdefs; t != NULL; t = t->next) | |
2433 | { | |
2434 | struct bfd_elf_version_deps *n; | |
2435 | ||
2436 | size += sizeof (Elf_External_Verdef); | |
2437 | size += sizeof (Elf_External_Verdaux); | |
2438 | ++cdefs; | |
2439 | ||
2440 | for (n = t->deps; n != NULL; n = n->next) | |
2441 | size += sizeof (Elf_External_Verdaux); | |
2442 | } | |
2443 | ||
2444 | s->_raw_size = size; | |
2445 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
2446 | if (s->contents == NULL && s->_raw_size != 0) | |
2447 | return false; | |
2448 | ||
2449 | /* Fill in the version definition section. */ | |
2450 | ||
2451 | p = s->contents; | |
2452 | ||
2453 | def.vd_version = VER_DEF_CURRENT; | |
2454 | def.vd_flags = VER_FLG_BASE; | |
2455 | def.vd_ndx = 1; | |
2456 | def.vd_cnt = 1; | |
2457 | def.vd_aux = sizeof (Elf_External_Verdef); | |
2458 | def.vd_next = (sizeof (Elf_External_Verdef) | |
2459 | + sizeof (Elf_External_Verdaux)); | |
2460 | ||
2461 | if (soname_indx != -1) | |
2462 | { | |
2463 | def.vd_hash = bfd_elf_hash ((const unsigned char *) soname); | |
2464 | defaux.vda_name = soname_indx; | |
2465 | } | |
2466 | else | |
2467 | { | |
2468 | const char *name; | |
2469 | bfd_size_type indx; | |
2470 | ||
2471 | name = output_bfd->filename; | |
2472 | def.vd_hash = bfd_elf_hash ((const unsigned char *) name); | |
2473 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2474 | name, true, false); | |
2475 | if (indx == (bfd_size_type) -1) | |
2476 | return false; | |
2477 | defaux.vda_name = indx; | |
2478 | } | |
2479 | defaux.vda_next = 0; | |
2480 | ||
2481 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
2482 | (Elf_External_Verdef *)p); | |
2483 | p += sizeof (Elf_External_Verdef); | |
2484 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
2485 | (Elf_External_Verdaux *) p); | |
2486 | p += sizeof (Elf_External_Verdaux); | |
2487 | ||
2488 | for (t = verdefs; t != NULL; t = t->next) | |
2489 | { | |
2490 | unsigned int cdeps; | |
2491 | struct bfd_elf_version_deps *n; | |
2492 | struct elf_link_hash_entry *h; | |
2493 | ||
2494 | cdeps = 0; | |
2495 | for (n = t->deps; n != NULL; n = n->next) | |
2496 | ++cdeps; | |
2497 | ||
2498 | /* Add a symbol representing this version. */ | |
2499 | h = NULL; | |
2500 | if (! (_bfd_generic_link_add_one_symbol | |
2501 | (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, | |
2502 | (bfd_vma) 0, (const char *) NULL, false, | |
2503 | get_elf_backend_data (dynobj)->collect, | |
2504 | (struct bfd_link_hash_entry **) &h))) | |
2505 | return false; | |
2506 | h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF; | |
2507 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2508 | h->type = STT_OBJECT; | |
2509 | h->verinfo.vertree = t; | |
2510 | ||
d6bfcdb5 ILT |
2511 | if (! _bfd_elf_link_record_dynamic_symbol (info, h)) |
2512 | return false; | |
d044b40a ILT |
2513 | |
2514 | def.vd_version = VER_DEF_CURRENT; | |
2515 | def.vd_flags = 0; | |
2516 | if (t->globals == NULL && t->locals == NULL && ! t->used) | |
2517 | def.vd_flags |= VER_FLG_WEAK; | |
2518 | def.vd_ndx = t->vernum + 1; | |
2519 | def.vd_cnt = cdeps + 1; | |
2520 | def.vd_hash = bfd_elf_hash ((const unsigned char *) t->name); | |
2521 | def.vd_aux = sizeof (Elf_External_Verdef); | |
2522 | if (t->next != NULL) | |
2523 | def.vd_next = (sizeof (Elf_External_Verdef) | |
2524 | + (cdeps + 1) * sizeof (Elf_External_Verdaux)); | |
2525 | else | |
2526 | def.vd_next = 0; | |
2527 | ||
2528 | _bfd_elf_swap_verdef_out (output_bfd, &def, | |
2529 | (Elf_External_Verdef *) p); | |
2530 | p += sizeof (Elf_External_Verdef); | |
2531 | ||
2532 | defaux.vda_name = h->dynstr_index; | |
2533 | if (t->deps == NULL) | |
2534 | defaux.vda_next = 0; | |
2535 | else | |
2536 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
2537 | t->name_indx = defaux.vda_name; | |
2538 | ||
2539 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
2540 | (Elf_External_Verdaux *) p); | |
2541 | p += sizeof (Elf_External_Verdaux); | |
2542 | ||
2543 | for (n = t->deps; n != NULL; n = n->next) | |
2544 | { | |
9793236c ILT |
2545 | if (n->version_needed == NULL) |
2546 | { | |
2547 | /* This can happen if there was an error in the | |
2548 | version script. */ | |
2549 | defaux.vda_name = 0; | |
2550 | } | |
2551 | else | |
2552 | defaux.vda_name = n->version_needed->name_indx; | |
d044b40a ILT |
2553 | if (n->next == NULL) |
2554 | defaux.vda_next = 0; | |
2555 | else | |
2556 | defaux.vda_next = sizeof (Elf_External_Verdaux); | |
2557 | ||
2558 | _bfd_elf_swap_verdaux_out (output_bfd, &defaux, | |
2559 | (Elf_External_Verdaux *) p); | |
2560 | p += sizeof (Elf_External_Verdaux); | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | if (! elf_add_dynamic_entry (info, DT_VERDEF, 0) | |
2565 | || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) | |
2566 | return false; | |
2567 | ||
2568 | elf_tdata (output_bfd)->cverdefs = cdefs; | |
2569 | } | |
2570 | ||
2571 | /* Work out the size of the version reference section. */ | |
2572 | ||
2573 | s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); | |
2574 | BFD_ASSERT (s != NULL); | |
2575 | { | |
2576 | struct elf_find_verdep_info sinfo; | |
2577 | ||
2578 | sinfo.output_bfd = output_bfd; | |
2579 | sinfo.info = info; | |
2580 | sinfo.vers = elf_tdata (output_bfd)->cverdefs; | |
2581 | if (sinfo.vers == 0) | |
2582 | sinfo.vers = 1; | |
2583 | sinfo.failed = false; | |
2584 | ||
2585 | elf_link_hash_traverse (elf_hash_table (info), | |
2586 | elf_link_find_version_dependencies, | |
2587 | (PTR) &sinfo); | |
2588 | ||
2589 | if (elf_tdata (output_bfd)->verref == NULL) | |
2590 | { | |
2591 | asection **spp; | |
2592 | ||
2593 | /* We don't have any version definitions, so we can just | |
2594 | remove the section. */ | |
2595 | ||
2596 | for (spp = &output_bfd->sections; | |
2597 | *spp != s->output_section; | |
2598 | spp = &(*spp)->next) | |
2599 | ; | |
2600 | *spp = s->output_section->next; | |
2601 | --output_bfd->section_count; | |
2602 | } | |
2603 | else | |
2604 | { | |
2605 | Elf_Internal_Verneed *t; | |
2606 | unsigned int size; | |
2607 | unsigned int crefs; | |
2608 | bfd_byte *p; | |
2609 | ||
2610 | /* Build the version definition section. */ | |
d6bfcdb5 ILT |
2611 | size = 0; |
2612 | crefs = 0; | |
d044b40a ILT |
2613 | for (t = elf_tdata (output_bfd)->verref; |
2614 | t != NULL; | |
2615 | t = t->vn_nextref) | |
2616 | { | |
2617 | Elf_Internal_Vernaux *a; | |
2618 | ||
2619 | size += sizeof (Elf_External_Verneed); | |
2620 | ++crefs; | |
2621 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2622 | size += sizeof (Elf_External_Vernaux); | |
2623 | } | |
2624 | ||
2625 | s->_raw_size = size; | |
2626 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, size); | |
2627 | if (s->contents == NULL) | |
2628 | return false; | |
2629 | ||
2630 | p = s->contents; | |
2631 | for (t = elf_tdata (output_bfd)->verref; | |
2632 | t != NULL; | |
2633 | t = t->vn_nextref) | |
2634 | { | |
2635 | unsigned int caux; | |
2636 | Elf_Internal_Vernaux *a; | |
2637 | bfd_size_type indx; | |
2638 | ||
2639 | caux = 0; | |
2640 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2641 | ++caux; | |
2642 | ||
2643 | t->vn_version = VER_NEED_CURRENT; | |
2644 | t->vn_cnt = caux; | |
601acd61 UD |
2645 | if (elf_dt_name (t->vn_bfd) != NULL) |
2646 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2647 | elf_dt_name (t->vn_bfd), | |
2648 | true, false); | |
2649 | else | |
2650 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2651 | t->vn_bfd->filename, true, false); | |
d044b40a ILT |
2652 | if (indx == (bfd_size_type) -1) |
2653 | return false; | |
2654 | t->vn_file = indx; | |
2655 | t->vn_aux = sizeof (Elf_External_Verneed); | |
2656 | if (t->vn_nextref == NULL) | |
2657 | t->vn_next = 0; | |
2658 | else | |
2659 | t->vn_next = (sizeof (Elf_External_Verneed) | |
2660 | + caux * sizeof (Elf_External_Vernaux)); | |
2661 | ||
2662 | _bfd_elf_swap_verneed_out (output_bfd, t, | |
2663 | (Elf_External_Verneed *) p); | |
2664 | p += sizeof (Elf_External_Verneed); | |
2665 | ||
2666 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
2667 | { | |
2668 | a->vna_hash = bfd_elf_hash ((const unsigned char *) | |
2669 | a->vna_nodename); | |
2670 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, | |
2671 | a->vna_nodename, true, false); | |
2672 | if (indx == (bfd_size_type) -1) | |
2673 | return false; | |
2674 | a->vna_name = indx; | |
2675 | if (a->vna_nextptr == NULL) | |
2676 | a->vna_next = 0; | |
2677 | else | |
2678 | a->vna_next = sizeof (Elf_External_Vernaux); | |
2679 | ||
2680 | _bfd_elf_swap_vernaux_out (output_bfd, a, | |
2681 | (Elf_External_Vernaux *) p); | |
2682 | p += sizeof (Elf_External_Vernaux); | |
2683 | } | |
2684 | } | |
2685 | ||
2686 | if (! elf_add_dynamic_entry (info, DT_VERNEED, 0) | |
2687 | || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) | |
2688 | return false; | |
2689 | ||
2690 | elf_tdata (output_bfd)->cverrefs = crefs; | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | dynsymcount = elf_hash_table (info)->dynsymcount; | |
2695 | ||
2696 | /* Work out the size of the symbol version section. */ | |
2697 | s = bfd_get_section_by_name (dynobj, ".gnu.version"); | |
2698 | BFD_ASSERT (s != NULL); | |
2699 | if (dynsymcount == 0 | |
2700 | || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL)) | |
2701 | { | |
2702 | asection **spp; | |
2703 | ||
2704 | /* We don't need any symbol versions; just discard the | |
2705 | section. */ | |
2706 | for (spp = &output_bfd->sections; | |
2707 | *spp != s->output_section; | |
2708 | spp = &(*spp)->next) | |
2709 | ; | |
2710 | *spp = s->output_section->next; | |
2711 | --output_bfd->section_count; | |
2712 | } | |
2713 | else | |
2714 | { | |
d044b40a | 2715 | s->_raw_size = dynsymcount * sizeof (Elf_External_Versym); |
e549b1d2 | 2716 | s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size); |
d044b40a ILT |
2717 | if (s->contents == NULL) |
2718 | return false; | |
2719 | ||
d044b40a ILT |
2720 | if (! elf_add_dynamic_entry (info, DT_VERSYM, 0)) |
2721 | return false; | |
2722 | } | |
2723 | ||
ede4eed4 KR |
2724 | /* Set the size of the .dynsym and .hash sections. We counted |
2725 | the number of dynamic symbols in elf_link_add_object_symbols. | |
2726 | We will build the contents of .dynsym and .hash when we build | |
2727 | the final symbol table, because until then we do not know the | |
2728 | correct value to give the symbols. We built the .dynstr | |
2729 | section as we went along in elf_link_add_object_symbols. */ | |
ede4eed4 KR |
2730 | s = bfd_get_section_by_name (dynobj, ".dynsym"); |
2731 | BFD_ASSERT (s != NULL); | |
2732 | s->_raw_size = dynsymcount * sizeof (Elf_External_Sym); | |
2733 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
2734 | if (s->contents == NULL && s->_raw_size != 0) | |
a9713b91 | 2735 | return false; |
ede4eed4 KR |
2736 | |
2737 | /* The first entry in .dynsym is a dummy symbol. */ | |
2738 | isym.st_value = 0; | |
2739 | isym.st_size = 0; | |
2740 | isym.st_name = 0; | |
2741 | isym.st_info = 0; | |
2742 | isym.st_other = 0; | |
2743 | isym.st_shndx = 0; | |
2744 | elf_swap_symbol_out (output_bfd, &isym, | |
cf9fb9f2 | 2745 | (PTR) (Elf_External_Sym *) s->contents); |
ede4eed4 KR |
2746 | |
2747 | for (i = 0; elf_buckets[i] != 0; i++) | |
2748 | { | |
2749 | bucketcount = elf_buckets[i]; | |
2750 | if (dynsymcount < elf_buckets[i + 1]) | |
2751 | break; | |
2752 | } | |
2753 | ||
2754 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
2755 | BFD_ASSERT (s != NULL); | |
2756 | s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8); | |
2757 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
2758 | if (s->contents == NULL) | |
a9713b91 | 2759 | return false; |
3fe22b98 | 2760 | memset (s->contents, 0, (size_t) s->_raw_size); |
ede4eed4 KR |
2761 | |
2762 | put_word (output_bfd, bucketcount, s->contents); | |
2763 | put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8)); | |
2764 | ||
2765 | elf_hash_table (info)->bucketcount = bucketcount; | |
2766 | ||
2767 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
2768 | BFD_ASSERT (s != NULL); | |
2769 | s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
2770 | ||
2771 | if (! elf_add_dynamic_entry (info, DT_NULL, 0)) | |
2772 | return false; | |
2773 | } | |
2774 | ||
2775 | return true; | |
2776 | } | |
3b3753b8 | 2777 | \f |
c19fbe0f ILT |
2778 | /* Fix up the flags for a symbol. This handles various cases which |
2779 | can only be fixed after all the input files are seen. This is | |
2780 | currently called by both adjust_dynamic_symbol and | |
2781 | assign_sym_version, which is unnecessary but perhaps more robust in | |
2782 | the face of future changes. */ | |
ede4eed4 KR |
2783 | |
2784 | static boolean | |
c19fbe0f | 2785 | elf_fix_symbol_flags (h, eif) |
ede4eed4 | 2786 | struct elf_link_hash_entry *h; |
c19fbe0f | 2787 | struct elf_info_failed *eif; |
ede4eed4 | 2788 | { |
869b7d80 ILT |
2789 | /* If this symbol was mentioned in a non-ELF file, try to set |
2790 | DEF_REGULAR and REF_REGULAR correctly. This is the only way to | |
2791 | permit a non-ELF file to correctly refer to a symbol defined in | |
2792 | an ELF dynamic object. */ | |
2793 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0) | |
2794 | { | |
2795 | if (h->root.type != bfd_link_hash_defined | |
2796 | && h->root.type != bfd_link_hash_defweak) | |
2797 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2798 | else | |
2799 | { | |
e303e2e3 ILT |
2800 | if (h->root.u.def.section->owner != NULL |
2801 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
2802 | == bfd_target_elf_flavour)) | |
869b7d80 ILT |
2803 | h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; |
2804 | else | |
2805 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2806 | } | |
2807 | ||
c19fbe0f ILT |
2808 | if (h->dynindx == -1 |
2809 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2810 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)) | |
869b7d80 ILT |
2811 | { |
2812 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2813 | { | |
2814 | eif->failed = true; | |
2815 | return false; | |
2816 | } | |
2817 | } | |
2818 | } | |
2819 | ||
ce6a7731 ILT |
2820 | /* If this is a final link, and the symbol was defined as a common |
2821 | symbol in a regular object file, and there was no definition in | |
2822 | any dynamic object, then the linker will have allocated space for | |
2823 | the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR | |
2824 | flag will not have been set. */ | |
2825 | if (h->root.type == bfd_link_hash_defined | |
2826 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
2827 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
2828 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2829 | && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) | |
2830 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
2831 | ||
951fe66d ILT |
2832 | /* If -Bsymbolic was used (which means to bind references to global |
2833 | symbols to the definition within the shared object), and this | |
2834 | symbol was defined in a regular object, then it actually doesn't | |
2835 | need a PLT entry. */ | |
2836 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0 | |
2837 | && eif->info->shared | |
2838 | && eif->info->symbolic | |
2839 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2840 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; | |
2841 | ||
c19fbe0f ILT |
2842 | return true; |
2843 | } | |
2844 | ||
2845 | /* Make the backend pick a good value for a dynamic symbol. This is | |
2846 | called via elf_link_hash_traverse, and also calls itself | |
2847 | recursively. */ | |
2848 | ||
2849 | static boolean | |
2850 | elf_adjust_dynamic_symbol (h, data) | |
2851 | struct elf_link_hash_entry *h; | |
2852 | PTR data; | |
2853 | { | |
2854 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
2855 | bfd *dynobj; | |
2856 | struct elf_backend_data *bed; | |
2857 | ||
2858 | /* Ignore indirect symbols. These are added by the versioning code. */ | |
2859 | if (h->root.type == bfd_link_hash_indirect) | |
2860 | return true; | |
2861 | ||
2862 | /* Fix the symbol flags. */ | |
2863 | if (! elf_fix_symbol_flags (h, eif)) | |
2864 | return false; | |
2865 | ||
ede4eed4 KR |
2866 | /* If this symbol does not require a PLT entry, and it is not |
2867 | defined by a dynamic object, or is not referenced by a regular | |
452a5efb ILT |
2868 | object, ignore it. We do have to handle a weak defined symbol, |
2869 | even if no regular object refers to it, if we decided to add it | |
2870 | to the dynamic symbol table. FIXME: Do we normally need to worry | |
2871 | about symbols which are defined by one dynamic object and | |
2872 | referenced by another one? */ | |
ede4eed4 KR |
2873 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 |
2874 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
2875 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
452a5efb ILT |
2876 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0 |
2877 | && (h->weakdef == NULL || h->weakdef->dynindx == -1)))) | |
ede4eed4 KR |
2878 | return true; |
2879 | ||
2880 | /* If we've already adjusted this symbol, don't do it again. This | |
2881 | can happen via a recursive call. */ | |
2882 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
2883 | return true; | |
2884 | ||
2885 | /* Don't look at this symbol again. Note that we must set this | |
2886 | after checking the above conditions, because we may look at a | |
2887 | symbol once, decide not to do anything, and then get called | |
2888 | recursively later after REF_REGULAR is set below. */ | |
2889 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
2890 | ||
2891 | /* If this is a weak definition, and we know a real definition, and | |
2892 | the real symbol is not itself defined by a regular object file, | |
2893 | then get a good value for the real definition. We handle the | |
2894 | real symbol first, for the convenience of the backend routine. | |
2895 | ||
2896 | Note that there is a confusing case here. If the real definition | |
2897 | is defined by a regular object file, we don't get the real symbol | |
2898 | from the dynamic object, but we do get the weak symbol. If the | |
2899 | processor backend uses a COPY reloc, then if some routine in the | |
2900 | dynamic object changes the real symbol, we will not see that | |
2901 | change in the corresponding weak symbol. This is the way other | |
2902 | ELF linkers work as well, and seems to be a result of the shared | |
2903 | library model. | |
2904 | ||
2905 | I will clarify this issue. Most SVR4 shared libraries define the | |
2906 | variable _timezone and define timezone as a weak synonym. The | |
2907 | tzset call changes _timezone. If you write | |
2908 | extern int timezone; | |
2909 | int _timezone = 5; | |
2910 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
2911 | you might expect that, since timezone is a synonym for _timezone, | |
2912 | the same number will print both times. However, if the processor | |
2913 | backend uses a COPY reloc, then actually timezone will be copied | |
2914 | into your process image, and, since you define _timezone | |
2915 | yourself, _timezone will not. Thus timezone and _timezone will | |
2916 | wind up at different memory locations. The tzset call will set | |
2917 | _timezone, leaving timezone unchanged. */ | |
2918 | ||
2919 | if (h->weakdef != NULL) | |
2920 | { | |
2921 | struct elf_link_hash_entry *weakdef; | |
2922 | ||
2923 | BFD_ASSERT (h->root.type == bfd_link_hash_defined | |
2924 | || h->root.type == bfd_link_hash_defweak); | |
2925 | weakdef = h->weakdef; | |
2926 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined | |
2927 | || weakdef->root.type == bfd_link_hash_defweak); | |
2928 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
2929 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2930 | { | |
2931 | /* This symbol is defined by a regular object file, so we | |
2932 | will not do anything special. Clear weakdef for the | |
2933 | convenience of the processor backend. */ | |
2934 | h->weakdef = NULL; | |
2935 | } | |
2936 | else | |
2937 | { | |
2938 | /* There is an implicit reference by a regular object file | |
2939 | via the weak symbol. */ | |
2940 | weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
2941 | if (! elf_adjust_dynamic_symbol (weakdef, (PTR) eif)) | |
2942 | return false; | |
2943 | } | |
2944 | } | |
2945 | ||
2946 | dynobj = elf_hash_table (eif->info)->dynobj; | |
2947 | bed = get_elf_backend_data (dynobj); | |
2948 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) | |
2949 | { | |
2950 | eif->failed = true; | |
2951 | return false; | |
2952 | } | |
2953 | ||
2954 | return true; | |
2955 | } | |
2956 | \f | |
d044b40a ILT |
2957 | /* This routine is used to export all defined symbols into the dynamic |
2958 | symbol table. It is called via elf_link_hash_traverse. */ | |
2959 | ||
2960 | static boolean | |
2961 | elf_export_symbol (h, data) | |
2962 | struct elf_link_hash_entry *h; | |
2963 | PTR data; | |
2964 | { | |
2965 | struct elf_info_failed *eif = (struct elf_info_failed *) data; | |
2966 | ||
e549b1d2 ILT |
2967 | /* Ignore indirect symbols. These are added by the versioning code. */ |
2968 | if (h->root.type == bfd_link_hash_indirect) | |
2969 | return true; | |
2970 | ||
d044b40a ILT |
2971 | if (h->dynindx == -1 |
2972 | && (h->elf_link_hash_flags | |
2973 | & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
2974 | { | |
2975 | if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h)) | |
2976 | { | |
2977 | eif->failed = true; | |
2978 | return false; | |
2979 | } | |
2980 | } | |
2981 | ||
2982 | return true; | |
2983 | } | |
2984 | \f | |
2985 | /* Look through the symbols which are defined in other shared | |
2986 | libraries and referenced here. Update the list of version | |
2987 | dependencies. This will be put into the .gnu.version_r section. | |
2988 | This function is called via elf_link_hash_traverse. */ | |
2989 | ||
2990 | static boolean | |
2991 | elf_link_find_version_dependencies (h, data) | |
2992 | struct elf_link_hash_entry *h; | |
2993 | PTR data; | |
2994 | { | |
2995 | struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; | |
2996 | Elf_Internal_Verneed *t; | |
2997 | Elf_Internal_Vernaux *a; | |
2998 | ||
2999 | /* We only care about symbols defined in shared objects with version | |
3000 | information. */ | |
3001 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
a48ef404 | 3002 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 |
d044b40a ILT |
3003 | || h->dynindx == -1 |
3004 | || h->verinfo.verdef == NULL) | |
3005 | return true; | |
3006 | ||
3007 | /* See if we already know about this version. */ | |
3008 | for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) | |
3009 | { | |
cf2cd4cf | 3010 | if (t->vn_bfd != h->verinfo.verdef->vd_bfd) |
d044b40a ILT |
3011 | continue; |
3012 | ||
3013 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
3014 | if (a->vna_nodename == h->verinfo.verdef->vd_nodename) | |
3015 | return true; | |
3016 | ||
3017 | break; | |
3018 | } | |
3019 | ||
3020 | /* This is a new version. Add it to tree we are building. */ | |
3021 | ||
3022 | if (t == NULL) | |
3023 | { | |
3024 | t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t); | |
3025 | if (t == NULL) | |
3026 | { | |
3027 | rinfo->failed = true; | |
3028 | return false; | |
3029 | } | |
3030 | ||
3031 | t->vn_bfd = h->verinfo.verdef->vd_bfd; | |
3032 | t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; | |
3033 | elf_tdata (rinfo->output_bfd)->verref = t; | |
3034 | } | |
3035 | ||
3036 | a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a); | |
3037 | ||
3038 | /* Note that we are copying a string pointer here, and testing it | |
3039 | above. If bfd_elf_string_from_elf_section is ever changed to | |
3040 | discard the string data when low in memory, this will have to be | |
3041 | fixed. */ | |
3042 | a->vna_nodename = h->verinfo.verdef->vd_nodename; | |
3043 | ||
3044 | a->vna_flags = h->verinfo.verdef->vd_flags; | |
3045 | a->vna_nextptr = t->vn_auxptr; | |
3046 | ||
3047 | h->verinfo.verdef->vd_exp_refno = rinfo->vers; | |
3048 | ++rinfo->vers; | |
3049 | ||
3050 | a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; | |
3051 | ||
3052 | t->vn_auxptr = a; | |
3053 | ||
3054 | return true; | |
3055 | } | |
3056 | ||
3057 | /* Figure out appropriate versions for all the symbols. We may not | |
3058 | have the version number script until we have read all of the input | |
3059 | files, so until that point we don't know which symbols should be | |
3060 | local. This function is called via elf_link_hash_traverse. */ | |
3061 | ||
3062 | static boolean | |
3063 | elf_link_assign_sym_version (h, data) | |
3064 | struct elf_link_hash_entry *h; | |
3065 | PTR data; | |
3066 | { | |
3067 | struct elf_assign_sym_version_info *sinfo = | |
3068 | (struct elf_assign_sym_version_info *) data; | |
3069 | struct bfd_link_info *info = sinfo->info; | |
c19fbe0f | 3070 | struct elf_info_failed eif; |
d044b40a ILT |
3071 | char *p; |
3072 | ||
c19fbe0f ILT |
3073 | /* Fix the symbol flags. */ |
3074 | eif.failed = false; | |
3075 | eif.info = info; | |
3076 | if (! elf_fix_symbol_flags (h, &eif)) | |
3077 | { | |
3078 | if (eif.failed) | |
3079 | sinfo->failed = true; | |
3080 | return false; | |
3081 | } | |
3082 | ||
d044b40a ILT |
3083 | /* We only need version numbers for symbols defined in regular |
3084 | objects. */ | |
3085 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
3086 | return true; | |
3087 | ||
3088 | p = strchr (h->root.root.string, ELF_VER_CHR); | |
3089 | if (p != NULL && h->verinfo.vertree == NULL) | |
3090 | { | |
3091 | struct bfd_elf_version_tree *t; | |
3092 | boolean hidden; | |
3093 | ||
3094 | hidden = true; | |
3095 | ||
3096 | /* There are two consecutive ELF_VER_CHR characters if this is | |
3097 | not a hidden symbol. */ | |
3098 | ++p; | |
3099 | if (*p == ELF_VER_CHR) | |
3100 | { | |
3101 | hidden = false; | |
3102 | ++p; | |
3103 | } | |
3104 | ||
3105 | /* If there is no version string, we can just return out. */ | |
3106 | if (*p == '\0') | |
3107 | { | |
3108 | if (hidden) | |
3109 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
3110 | return true; | |
3111 | } | |
3112 | ||
3113 | /* Look for the version. If we find it, it is no longer weak. */ | |
3114 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
3115 | { | |
3116 | if (strcmp (t->name, p) == 0) | |
3117 | { | |
bccab630 RH |
3118 | int len; |
3119 | char *alc; | |
3120 | struct bfd_elf_version_expr *d; | |
3121 | ||
3122 | len = p - h->root.root.string; | |
3123 | alc = bfd_alloc (sinfo->output_bfd, len); | |
3124 | if (alc == NULL) | |
3125 | return false; | |
3126 | strncpy (alc, h->root.root.string, len - 1); | |
3127 | alc[len - 1] = '\0'; | |
3128 | if (alc[len - 2] == ELF_VER_CHR) | |
3129 | alc[len - 2] = '\0'; | |
3130 | ||
d044b40a ILT |
3131 | h->verinfo.vertree = t; |
3132 | t->used = true; | |
bccab630 RH |
3133 | d = NULL; |
3134 | ||
3135 | if (t->globals != NULL) | |
3136 | { | |
3137 | for (d = t->globals; d != NULL; d = d->next) | |
3138 | { | |
3139 | if ((d->match[0] == '*' && d->match[1] == '\0') | |
3140 | || fnmatch (d->match, alc, 0) == 0) | |
3141 | break; | |
3142 | } | |
3143 | } | |
d6bfcdb5 ILT |
3144 | |
3145 | /* See if there is anything to force this symbol to | |
3146 | local scope. */ | |
bccab630 | 3147 | if (d == NULL && t->locals != NULL) |
d6bfcdb5 | 3148 | { |
d6bfcdb5 ILT |
3149 | for (d = t->locals; d != NULL; d = d->next) |
3150 | { | |
3151 | if ((d->match[0] == '*' && d->match[1] == '\0') | |
3152 | || fnmatch (d->match, alc, 0) == 0) | |
3153 | { | |
3154 | if (h->dynindx != -1 | |
3155 | && info->shared | |
c19fbe0f | 3156 | && ! sinfo->export_dynamic) |
d6bfcdb5 ILT |
3157 | { |
3158 | sinfo->removed_dynamic = true; | |
52c92c7f | 3159 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
c19fbe0f ILT |
3160 | h->elf_link_hash_flags &=~ |
3161 | ELF_LINK_HASH_NEEDS_PLT; | |
d6bfcdb5 ILT |
3162 | h->dynindx = -1; |
3163 | /* FIXME: The name of the symbol has | |
3164 | already been recorded in the dynamic | |
3165 | string table section. */ | |
3166 | } | |
3167 | ||
3168 | break; | |
3169 | } | |
3170 | } | |
d6bfcdb5 ILT |
3171 | } |
3172 | ||
bccab630 | 3173 | bfd_release (sinfo->output_bfd, alc); |
d044b40a ILT |
3174 | break; |
3175 | } | |
3176 | } | |
3177 | ||
d6bfcdb5 ILT |
3178 | /* If we are building an application, we need to create a |
3179 | version node for this version. */ | |
3180 | if (t == NULL && ! info->shared) | |
3181 | { | |
3182 | struct bfd_elf_version_tree **pp; | |
3183 | int version_index; | |
3184 | ||
3185 | /* If we aren't going to export this symbol, we don't need | |
3186 | to worry about it. */ | |
3187 | if (h->dynindx == -1) | |
3188 | return true; | |
3189 | ||
3190 | t = ((struct bfd_elf_version_tree *) | |
3191 | bfd_alloc (sinfo->output_bfd, sizeof *t)); | |
3192 | if (t == NULL) | |
3193 | { | |
3194 | sinfo->failed = true; | |
3195 | return false; | |
3196 | } | |
3197 | ||
3198 | t->next = NULL; | |
3199 | t->name = p; | |
3200 | t->globals = NULL; | |
3201 | t->locals = NULL; | |
3202 | t->deps = NULL; | |
3203 | t->name_indx = (unsigned int) -1; | |
3204 | t->used = true; | |
3205 | ||
3206 | version_index = 1; | |
3207 | for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) | |
3208 | ++version_index; | |
3209 | t->vernum = version_index; | |
3210 | ||
3211 | *pp = t; | |
3212 | ||
3213 | h->verinfo.vertree = t; | |
3214 | } | |
3215 | else if (t == NULL) | |
d044b40a | 3216 | { |
d6bfcdb5 ILT |
3217 | /* We could not find the version for a symbol when |
3218 | generating a shared archive. Return an error. */ | |
d044b40a | 3219 | (*_bfd_error_handler) |
7e3d76b7 | 3220 | ("%s: undefined versioned symbol name %s", |
52c92c7f | 3221 | bfd_get_filename (sinfo->output_bfd), h->root.root.string); |
d044b40a ILT |
3222 | bfd_set_error (bfd_error_bad_value); |
3223 | sinfo->failed = true; | |
3224 | return false; | |
3225 | } | |
3226 | ||
3227 | if (hidden) | |
3228 | h->elf_link_hash_flags |= ELF_LINK_HIDDEN; | |
3229 | } | |
3230 | ||
3231 | /* If we don't have a version for this symbol, see if we can find | |
3232 | something. */ | |
3233 | if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) | |
3234 | { | |
3235 | struct bfd_elf_version_tree *t; | |
3236 | struct bfd_elf_version_tree *deflt; | |
3237 | struct bfd_elf_version_expr *d; | |
3238 | ||
3239 | /* See if can find what version this symbol is in. If the | |
c19fbe0f | 3240 | symbol is supposed to be local, then don't actually register |
d044b40a ILT |
3241 | it. */ |
3242 | deflt = NULL; | |
3243 | for (t = sinfo->verdefs; t != NULL; t = t->next) | |
3244 | { | |
3245 | if (t->globals != NULL) | |
3246 | { | |
3247 | for (d = t->globals; d != NULL; d = d->next) | |
3248 | { | |
3249 | if (fnmatch (d->match, h->root.root.string, 0) == 0) | |
3250 | { | |
3251 | h->verinfo.vertree = t; | |
3252 | break; | |
3253 | } | |
3254 | } | |
3255 | ||
3256 | if (d != NULL) | |
3257 | break; | |
3258 | } | |
3259 | ||
3260 | if (t->locals != NULL) | |
3261 | { | |
3262 | for (d = t->locals; d != NULL; d = d->next) | |
3263 | { | |
3264 | if (d->match[0] == '*' && d->match[1] == '\0') | |
3265 | deflt = t; | |
3266 | else if (fnmatch (d->match, h->root.root.string, 0) == 0) | |
3267 | { | |
3268 | h->verinfo.vertree = t; | |
3269 | if (h->dynindx != -1 | |
3270 | && info->shared | |
c19fbe0f | 3271 | && ! sinfo->export_dynamic) |
d044b40a ILT |
3272 | { |
3273 | sinfo->removed_dynamic = true; | |
52c92c7f | 3274 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
c19fbe0f | 3275 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; |
d044b40a ILT |
3276 | h->dynindx = -1; |
3277 | /* FIXME: The name of the symbol has already | |
3278 | been recorded in the dynamic string table | |
3279 | section. */ | |
3280 | } | |
3281 | break; | |
3282 | } | |
3283 | } | |
3284 | ||
3285 | if (d != NULL) | |
3286 | break; | |
3287 | } | |
3288 | } | |
3289 | ||
3290 | if (deflt != NULL && h->verinfo.vertree == NULL) | |
3291 | { | |
3292 | h->verinfo.vertree = deflt; | |
3293 | if (h->dynindx != -1 | |
3294 | && info->shared | |
c19fbe0f | 3295 | && ! sinfo->export_dynamic) |
d044b40a ILT |
3296 | { |
3297 | sinfo->removed_dynamic = true; | |
52c92c7f | 3298 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; |
c19fbe0f | 3299 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT; |
d044b40a ILT |
3300 | h->dynindx = -1; |
3301 | /* FIXME: The name of the symbol has already been | |
3302 | recorded in the dynamic string table section. */ | |
3303 | } | |
3304 | } | |
3305 | } | |
3306 | ||
3307 | return true; | |
3308 | } | |
3309 | ||
3310 | /* This function is used to renumber the dynamic symbols, if some of | |
3311 | them are removed because they are marked as local. This is called | |
3312 | via elf_link_hash_traverse. */ | |
3313 | ||
3314 | static boolean | |
3315 | elf_link_renumber_dynsyms (h, data) | |
3316 | struct elf_link_hash_entry *h; | |
3317 | PTR data; | |
3318 | { | |
3319 | struct bfd_link_info *info = (struct bfd_link_info *) data; | |
3320 | ||
3321 | if (h->dynindx != -1) | |
3322 | { | |
3323 | h->dynindx = elf_hash_table (info)->dynsymcount; | |
3324 | ++elf_hash_table (info)->dynsymcount; | |
3325 | } | |
3326 | ||
3327 | return true; | |
3328 | } | |
3329 | \f | |
ede4eed4 KR |
3330 | /* Final phase of ELF linker. */ |
3331 | ||
3332 | /* A structure we use to avoid passing large numbers of arguments. */ | |
3333 | ||
3334 | struct elf_final_link_info | |
3335 | { | |
3336 | /* General link information. */ | |
3337 | struct bfd_link_info *info; | |
3338 | /* Output BFD. */ | |
3339 | bfd *output_bfd; | |
3340 | /* Symbol string table. */ | |
3341 | struct bfd_strtab_hash *symstrtab; | |
3342 | /* .dynsym section. */ | |
3343 | asection *dynsym_sec; | |
3344 | /* .hash section. */ | |
3345 | asection *hash_sec; | |
d044b40a ILT |
3346 | /* symbol version section (.gnu.version). */ |
3347 | asection *symver_sec; | |
ede4eed4 KR |
3348 | /* Buffer large enough to hold contents of any section. */ |
3349 | bfd_byte *contents; | |
3350 | /* Buffer large enough to hold external relocs of any section. */ | |
3351 | PTR external_relocs; | |
3352 | /* Buffer large enough to hold internal relocs of any section. */ | |
3353 | Elf_Internal_Rela *internal_relocs; | |
3354 | /* Buffer large enough to hold external local symbols of any input | |
3355 | BFD. */ | |
3356 | Elf_External_Sym *external_syms; | |
3357 | /* Buffer large enough to hold internal local symbols of any input | |
3358 | BFD. */ | |
3359 | Elf_Internal_Sym *internal_syms; | |
3360 | /* Array large enough to hold a symbol index for each local symbol | |
3361 | of any input BFD. */ | |
3362 | long *indices; | |
3363 | /* Array large enough to hold a section pointer for each local | |
3364 | symbol of any input BFD. */ | |
3365 | asection **sections; | |
3366 | /* Buffer to hold swapped out symbols. */ | |
3367 | Elf_External_Sym *symbuf; | |
3368 | /* Number of swapped out symbols in buffer. */ | |
3369 | size_t symbuf_count; | |
3370 | /* Number of symbols which fit in symbuf. */ | |
3371 | size_t symbuf_size; | |
3372 | }; | |
3373 | ||
3374 | static boolean elf_link_output_sym | |
3375 | PARAMS ((struct elf_final_link_info *, const char *, | |
3376 | Elf_Internal_Sym *, asection *)); | |
3377 | static boolean elf_link_flush_output_syms | |
3378 | PARAMS ((struct elf_final_link_info *)); | |
3379 | static boolean elf_link_output_extsym | |
3380 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
3381 | static boolean elf_link_input_bfd | |
3382 | PARAMS ((struct elf_final_link_info *, bfd *)); | |
3383 | static boolean elf_reloc_link_order | |
3384 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
3385 | struct bfd_link_order *)); | |
3386 | ||
52c92c7f | 3387 | /* This struct is used to pass information to elf_link_output_extsym. */ |
ede4eed4 | 3388 | |
52c92c7f | 3389 | struct elf_outext_info |
ede4eed4 KR |
3390 | { |
3391 | boolean failed; | |
52c92c7f | 3392 | boolean localsyms; |
ede4eed4 | 3393 | struct elf_final_link_info *finfo; |
ff12f303 | 3394 | }; |
ede4eed4 KR |
3395 | |
3396 | /* Do the final step of an ELF link. */ | |
3397 | ||
3398 | boolean | |
3399 | elf_bfd_final_link (abfd, info) | |
3400 | bfd *abfd; | |
3401 | struct bfd_link_info *info; | |
3402 | { | |
3403 | boolean dynamic; | |
3404 | bfd *dynobj; | |
3405 | struct elf_final_link_info finfo; | |
3406 | register asection *o; | |
3407 | register struct bfd_link_order *p; | |
3408 | register bfd *sub; | |
3409 | size_t max_contents_size; | |
3410 | size_t max_external_reloc_size; | |
3411 | size_t max_internal_reloc_count; | |
3412 | size_t max_sym_count; | |
3413 | file_ptr off; | |
3414 | Elf_Internal_Sym elfsym; | |
3415 | unsigned int i; | |
3416 | Elf_Internal_Shdr *symtab_hdr; | |
3417 | Elf_Internal_Shdr *symstrtab_hdr; | |
3418 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
52c92c7f | 3419 | struct elf_outext_info eoinfo; |
ede4eed4 KR |
3420 | |
3421 | if (info->shared) | |
3422 | abfd->flags |= DYNAMIC; | |
3423 | ||
3424 | dynamic = elf_hash_table (info)->dynamic_sections_created; | |
3425 | dynobj = elf_hash_table (info)->dynobj; | |
3426 | ||
3427 | finfo.info = info; | |
3428 | finfo.output_bfd = abfd; | |
3429 | finfo.symstrtab = elf_stringtab_init (); | |
3430 | if (finfo.symstrtab == NULL) | |
3431 | return false; | |
d044b40a | 3432 | |
ede4eed4 KR |
3433 | if (! dynamic) |
3434 | { | |
3435 | finfo.dynsym_sec = NULL; | |
3436 | finfo.hash_sec = NULL; | |
d044b40a | 3437 | finfo.symver_sec = NULL; |
ede4eed4 KR |
3438 | } |
3439 | else | |
3440 | { | |
3441 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
3442 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
3443 | BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); | |
d044b40a ILT |
3444 | finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); |
3445 | /* Note that it is OK if symver_sec is NULL. */ | |
ede4eed4 | 3446 | } |
d044b40a | 3447 | |
ede4eed4 KR |
3448 | finfo.contents = NULL; |
3449 | finfo.external_relocs = NULL; | |
3450 | finfo.internal_relocs = NULL; | |
3451 | finfo.external_syms = NULL; | |
3452 | finfo.internal_syms = NULL; | |
3453 | finfo.indices = NULL; | |
3454 | finfo.sections = NULL; | |
3455 | finfo.symbuf = NULL; | |
3456 | finfo.symbuf_count = 0; | |
3457 | ||
3458 | /* Count up the number of relocations we will output for each output | |
3459 | section, so that we know the sizes of the reloc sections. We | |
3460 | also figure out some maximum sizes. */ | |
3461 | max_contents_size = 0; | |
3462 | max_external_reloc_size = 0; | |
3463 | max_internal_reloc_count = 0; | |
3464 | max_sym_count = 0; | |
3465 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
3466 | { | |
3467 | o->reloc_count = 0; | |
3468 | ||
3469 | for (p = o->link_order_head; p != NULL; p = p->next) | |
3470 | { | |
3471 | if (p->type == bfd_section_reloc_link_order | |
3472 | || p->type == bfd_symbol_reloc_link_order) | |
3473 | ++o->reloc_count; | |
3474 | else if (p->type == bfd_indirect_link_order) | |
3475 | { | |
3476 | asection *sec; | |
3477 | ||
3478 | sec = p->u.indirect.section; | |
3479 | ||
7ec49f91 ILT |
3480 | /* Mark all sections which are to be included in the |
3481 | link. This will normally be every section. We need | |
3482 | to do this so that we can identify any sections which | |
3483 | the linker has decided to not include. */ | |
ff0e4a93 | 3484 | sec->linker_mark = true; |
7ec49f91 | 3485 | |
ede4eed4 KR |
3486 | if (info->relocateable) |
3487 | o->reloc_count += sec->reloc_count; | |
3488 | ||
3489 | if (sec->_raw_size > max_contents_size) | |
3490 | max_contents_size = sec->_raw_size; | |
3491 | if (sec->_cooked_size > max_contents_size) | |
3492 | max_contents_size = sec->_cooked_size; | |
3493 | ||
3494 | /* We are interested in just local symbols, not all | |
3495 | symbols. */ | |
d044b40a ILT |
3496 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour |
3497 | && (sec->owner->flags & DYNAMIC) == 0) | |
ede4eed4 KR |
3498 | { |
3499 | size_t sym_count; | |
3500 | ||
3501 | if (elf_bad_symtab (sec->owner)) | |
3502 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
3503 | / sizeof (Elf_External_Sym)); | |
3504 | else | |
3505 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
3506 | ||
3507 | if (sym_count > max_sym_count) | |
3508 | max_sym_count = sym_count; | |
3509 | ||
3510 | if ((sec->flags & SEC_RELOC) != 0) | |
3511 | { | |
3512 | size_t ext_size; | |
3513 | ||
3514 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
3515 | if (ext_size > max_external_reloc_size) | |
3516 | max_external_reloc_size = ext_size; | |
3517 | if (sec->reloc_count > max_internal_reloc_count) | |
3518 | max_internal_reloc_count = sec->reloc_count; | |
3519 | } | |
3520 | } | |
3521 | } | |
3522 | } | |
3523 | ||
3524 | if (o->reloc_count > 0) | |
3525 | o->flags |= SEC_RELOC; | |
3526 | else | |
3527 | { | |
3528 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
3529 | set it (this is probably a bug) and if it is set | |
3530 | assign_section_numbers will create a reloc section. */ | |
3531 | o->flags &=~ SEC_RELOC; | |
3532 | } | |
3533 | ||
3534 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
3535 | zero. This is done in elf_fake_sections as well, but forcing | |
3536 | the VMA to 0 here will ensure that relocs against these | |
3537 | sections are handled correctly. */ | |
2e0567eb ILT |
3538 | if ((o->flags & SEC_ALLOC) == 0 |
3539 | && ! o->user_set_vma) | |
ede4eed4 KR |
3540 | o->vma = 0; |
3541 | } | |
3542 | ||
3543 | /* Figure out the file positions for everything but the symbol table | |
3544 | and the relocs. We set symcount to force assign_section_numbers | |
3545 | to create a symbol table. */ | |
3546 | abfd->symcount = info->strip == strip_all ? 0 : 1; | |
3547 | BFD_ASSERT (! abfd->output_has_begun); | |
3548 | if (! _bfd_elf_compute_section_file_positions (abfd, info)) | |
3549 | goto error_return; | |
3550 | ||
3551 | /* That created the reloc sections. Set their sizes, and assign | |
3552 | them file positions, and allocate some buffers. */ | |
3553 | for (o = abfd->sections; o != NULL; o = o->next) | |
3554 | { | |
3555 | if ((o->flags & SEC_RELOC) != 0) | |
3556 | { | |
3557 | Elf_Internal_Shdr *rel_hdr; | |
3558 | register struct elf_link_hash_entry **p, **pend; | |
3559 | ||
3560 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
3561 | ||
3562 | rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count; | |
3563 | ||
3564 | /* The contents field must last into write_object_contents, | |
3565 | so we allocate it with bfd_alloc rather than malloc. */ | |
3566 | rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size); | |
3567 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) | |
a9713b91 | 3568 | goto error_return; |
ede4eed4 KR |
3569 | |
3570 | p = ((struct elf_link_hash_entry **) | |
58142f10 ILT |
3571 | bfd_malloc (o->reloc_count |
3572 | * sizeof (struct elf_link_hash_entry *))); | |
ede4eed4 | 3573 | if (p == NULL && o->reloc_count != 0) |
58142f10 | 3574 | goto error_return; |
ede4eed4 KR |
3575 | elf_section_data (o)->rel_hashes = p; |
3576 | pend = p + o->reloc_count; | |
3577 | for (; p < pend; p++) | |
3578 | *p = NULL; | |
3579 | ||
3580 | /* Use the reloc_count field as an index when outputting the | |
3581 | relocs. */ | |
3582 | o->reloc_count = 0; | |
3583 | } | |
3584 | } | |
3585 | ||
3586 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
3587 | ||
3588 | /* We have now assigned file positions for all the sections except | |
3589 | .symtab and .strtab. We start the .symtab section at the current | |
3590 | file position, and write directly to it. We build the .strtab | |
ab276dfa | 3591 | section in memory. */ |
ede4eed4 KR |
3592 | abfd->symcount = 0; |
3593 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
3594 | /* sh_name is set in prep_headers. */ | |
3595 | symtab_hdr->sh_type = SHT_SYMTAB; | |
3596 | symtab_hdr->sh_flags = 0; | |
3597 | symtab_hdr->sh_addr = 0; | |
3598 | symtab_hdr->sh_size = 0; | |
3599 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
3600 | /* sh_link is set in assign_section_numbers. */ | |
3601 | /* sh_info is set below. */ | |
3602 | /* sh_offset is set just below. */ | |
3603 | symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */ | |
3604 | ||
3605 | off = elf_tdata (abfd)->next_file_pos; | |
3606 | off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true); | |
3607 | ||
3608 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
3609 | incorrect. We do not yet know the size of the .symtab section. | |
3610 | We correct next_file_pos below, after we do know the size. */ | |
3611 | ||
3612 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
3613 | continuously seeking to the right position in the file. */ | |
3614 | if (! info->keep_memory || max_sym_count < 20) | |
3615 | finfo.symbuf_size = 20; | |
3616 | else | |
3617 | finfo.symbuf_size = max_sym_count; | |
3618 | finfo.symbuf = ((Elf_External_Sym *) | |
58142f10 | 3619 | bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym))); |
ede4eed4 | 3620 | if (finfo.symbuf == NULL) |
58142f10 | 3621 | goto error_return; |
ede4eed4 KR |
3622 | |
3623 | /* Start writing out the symbol table. The first symbol is always a | |
3624 | dummy symbol. */ | |
28c16b55 ILT |
3625 | if (info->strip != strip_all || info->relocateable) |
3626 | { | |
3627 | elfsym.st_value = 0; | |
3628 | elfsym.st_size = 0; | |
3629 | elfsym.st_info = 0; | |
3630 | elfsym.st_other = 0; | |
3631 | elfsym.st_shndx = SHN_UNDEF; | |
3632 | if (! elf_link_output_sym (&finfo, (const char *) NULL, | |
3633 | &elfsym, bfd_und_section_ptr)) | |
3634 | goto error_return; | |
3635 | } | |
ede4eed4 KR |
3636 | |
3637 | #if 0 | |
3638 | /* Some standard ELF linkers do this, but we don't because it causes | |
3639 | bootstrap comparison failures. */ | |
3640 | /* Output a file symbol for the output file as the second symbol. | |
3641 | We output this even if we are discarding local symbols, although | |
3642 | I'm not sure if this is correct. */ | |
3643 | elfsym.st_value = 0; | |
3644 | elfsym.st_size = 0; | |
3645 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
3646 | elfsym.st_other = 0; | |
3647 | elfsym.st_shndx = SHN_ABS; | |
3648 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), | |
3649 | &elfsym, bfd_abs_section_ptr)) | |
3650 | goto error_return; | |
3651 | #endif | |
3652 | ||
3653 | /* Output a symbol for each section. We output these even if we are | |
3654 | discarding local symbols, since they are used for relocs. These | |
3655 | symbols have no names. We store the index of each one in the | |
3656 | index field of the section, so that we can find it again when | |
3657 | outputting relocs. */ | |
28c16b55 | 3658 | if (info->strip != strip_all || info->relocateable) |
ede4eed4 | 3659 | { |
28c16b55 ILT |
3660 | elfsym.st_size = 0; |
3661 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
3662 | elfsym.st_other = 0; | |
3663 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) | |
3664 | { | |
3665 | o = section_from_elf_index (abfd, i); | |
3666 | if (o != NULL) | |
3667 | o->target_index = abfd->symcount; | |
3668 | elfsym.st_shndx = i; | |
34bc6ffc ILT |
3669 | if (info->relocateable || o == NULL) |
3670 | elfsym.st_value = 0; | |
3671 | else | |
3672 | elfsym.st_value = o->vma; | |
28c16b55 ILT |
3673 | if (! elf_link_output_sym (&finfo, (const char *) NULL, |
3674 | &elfsym, o)) | |
3675 | goto error_return; | |
3676 | } | |
ede4eed4 KR |
3677 | } |
3678 | ||
3679 | /* Allocate some memory to hold information read in from the input | |
3680 | files. */ | |
58142f10 ILT |
3681 | finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); |
3682 | finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size); | |
ede4eed4 | 3683 | finfo.internal_relocs = ((Elf_Internal_Rela *) |
58142f10 ILT |
3684 | bfd_malloc (max_internal_reloc_count |
3685 | * sizeof (Elf_Internal_Rela))); | |
ede4eed4 | 3686 | finfo.external_syms = ((Elf_External_Sym *) |
58142f10 ILT |
3687 | bfd_malloc (max_sym_count |
3688 | * sizeof (Elf_External_Sym))); | |
ede4eed4 | 3689 | finfo.internal_syms = ((Elf_Internal_Sym *) |
58142f10 ILT |
3690 | bfd_malloc (max_sym_count |
3691 | * sizeof (Elf_Internal_Sym))); | |
3692 | finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long)); | |
3693 | finfo.sections = ((asection **) | |
3694 | bfd_malloc (max_sym_count * sizeof (asection *))); | |
ede4eed4 KR |
3695 | if ((finfo.contents == NULL && max_contents_size != 0) |
3696 | || (finfo.external_relocs == NULL && max_external_reloc_size != 0) | |
3697 | || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0) | |
3698 | || (finfo.external_syms == NULL && max_sym_count != 0) | |
3699 | || (finfo.internal_syms == NULL && max_sym_count != 0) | |
3700 | || (finfo.indices == NULL && max_sym_count != 0) | |
3701 | || (finfo.sections == NULL && max_sym_count != 0)) | |
58142f10 | 3702 | goto error_return; |
ede4eed4 KR |
3703 | |
3704 | /* Since ELF permits relocations to be against local symbols, we | |
3705 | must have the local symbols available when we do the relocations. | |
3706 | Since we would rather only read the local symbols once, and we | |
3707 | would rather not keep them in memory, we handle all the | |
3708 | relocations for a single input file at the same time. | |
3709 | ||
3710 | Unfortunately, there is no way to know the total number of local | |
3711 | symbols until we have seen all of them, and the local symbol | |
3712 | indices precede the global symbol indices. This means that when | |
3713 | we are generating relocateable output, and we see a reloc against | |
3714 | a global symbol, we can not know the symbol index until we have | |
3715 | finished examining all the local symbols to see which ones we are | |
3716 | going to output. To deal with this, we keep the relocations in | |
3717 | memory, and don't output them until the end of the link. This is | |
3718 | an unfortunate waste of memory, but I don't see a good way around | |
3719 | it. Fortunately, it only happens when performing a relocateable | |
3720 | link, which is not the common case. FIXME: If keep_memory is set | |
3721 | we could write the relocs out and then read them again; I don't | |
3722 | know how bad the memory loss will be. */ | |
3723 | ||
3724 | for (sub = info->input_bfds; sub != NULL; sub = sub->next) | |
3725 | sub->output_has_begun = false; | |
3726 | for (o = abfd->sections; o != NULL; o = o->next) | |
3727 | { | |
3728 | for (p = o->link_order_head; p != NULL; p = p->next) | |
3729 | { | |
3730 | if (p->type == bfd_indirect_link_order | |
3731 | && (bfd_get_flavour (p->u.indirect.section->owner) | |
3732 | == bfd_target_elf_flavour)) | |
3733 | { | |
3734 | sub = p->u.indirect.section->owner; | |
3735 | if (! sub->output_has_begun) | |
3736 | { | |
3737 | if (! elf_link_input_bfd (&finfo, sub)) | |
3738 | goto error_return; | |
3739 | sub->output_has_begun = true; | |
3740 | } | |
3741 | } | |
3742 | else if (p->type == bfd_section_reloc_link_order | |
3743 | || p->type == bfd_symbol_reloc_link_order) | |
3744 | { | |
3745 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
3746 | goto error_return; | |
3747 | } | |
3748 | else | |
3749 | { | |
3750 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
3751 | goto error_return; | |
3752 | } | |
3753 | } | |
3754 | } | |
3755 | ||
3756 | /* That wrote out all the local symbols. Finish up the symbol table | |
3757 | with the global symbols. */ | |
3758 | ||
52c92c7f ILT |
3759 | if (info->strip != strip_all && info->shared) |
3760 | { | |
3761 | /* Output any global symbols that got converted to local in a | |
3762 | version script. We do this in a separate step since ELF | |
3763 | requires all local symbols to appear prior to any global | |
3764 | symbols. FIXME: We should only do this if some global | |
3765 | symbols were, in fact, converted to become local. FIXME: | |
3766 | Will this work correctly with the Irix 5 linker? */ | |
3767 | eoinfo.failed = false; | |
3768 | eoinfo.finfo = &finfo; | |
3769 | eoinfo.localsyms = true; | |
3770 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
3771 | (PTR) &eoinfo); | |
3772 | if (eoinfo.failed) | |
3773 | return false; | |
3774 | } | |
3775 | ||
ede4eed4 KR |
3776 | /* The sh_info field records the index of the first non local |
3777 | symbol. */ | |
3778 | symtab_hdr->sh_info = abfd->symcount; | |
3779 | if (dynamic) | |
3780 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1; | |
3781 | ||
3782 | /* We get the global symbols from the hash table. */ | |
52c92c7f ILT |
3783 | eoinfo.failed = false; |
3784 | eoinfo.localsyms = false; | |
3785 | eoinfo.finfo = &finfo; | |
ede4eed4 | 3786 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, |
52c92c7f ILT |
3787 | (PTR) &eoinfo); |
3788 | if (eoinfo.failed) | |
ede4eed4 KR |
3789 | return false; |
3790 | ||
3791 | /* Flush all symbols to the file. */ | |
3792 | if (! elf_link_flush_output_syms (&finfo)) | |
3793 | return false; | |
3794 | ||
3795 | /* Now we know the size of the symtab section. */ | |
3796 | off += symtab_hdr->sh_size; | |
3797 | ||
3798 | /* Finish up and write out the symbol string table (.strtab) | |
3799 | section. */ | |
3800 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
3801 | /* sh_name was set in prep_headers. */ | |
3802 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
3803 | symstrtab_hdr->sh_flags = 0; | |
3804 | symstrtab_hdr->sh_addr = 0; | |
3805 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); | |
3806 | symstrtab_hdr->sh_entsize = 0; | |
3807 | symstrtab_hdr->sh_link = 0; | |
3808 | symstrtab_hdr->sh_info = 0; | |
3809 | /* sh_offset is set just below. */ | |
3810 | symstrtab_hdr->sh_addralign = 1; | |
3811 | ||
3812 | off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true); | |
3813 | elf_tdata (abfd)->next_file_pos = off; | |
3814 | ||
28c16b55 ILT |
3815 | if (abfd->symcount > 0) |
3816 | { | |
3817 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 | |
3818 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
3819 | return false; | |
3820 | } | |
ede4eed4 KR |
3821 | |
3822 | /* Adjust the relocs to have the correct symbol indices. */ | |
3823 | for (o = abfd->sections; o != NULL; o = o->next) | |
3824 | { | |
3825 | struct elf_link_hash_entry **rel_hash; | |
3826 | Elf_Internal_Shdr *rel_hdr; | |
3827 | ||
3828 | if ((o->flags & SEC_RELOC) == 0) | |
3829 | continue; | |
3830 | ||
3831 | rel_hash = elf_section_data (o)->rel_hashes; | |
3832 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
3833 | for (i = 0; i < o->reloc_count; i++, rel_hash++) | |
3834 | { | |
3835 | if (*rel_hash == NULL) | |
3836 | continue; | |
ff12f303 | 3837 | |
ede4eed4 KR |
3838 | BFD_ASSERT ((*rel_hash)->indx >= 0); |
3839 | ||
3840 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
3841 | { | |
3842 | Elf_External_Rel *erel; | |
3843 | Elf_Internal_Rel irel; | |
3844 | ||
3845 | erel = (Elf_External_Rel *) rel_hdr->contents + i; | |
3846 | elf_swap_reloc_in (abfd, erel, &irel); | |
3847 | irel.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
3848 | ELF_R_TYPE (irel.r_info)); | |
3849 | elf_swap_reloc_out (abfd, &irel, erel); | |
3850 | } | |
3851 | else | |
3852 | { | |
3853 | Elf_External_Rela *erela; | |
3854 | Elf_Internal_Rela irela; | |
3855 | ||
3856 | BFD_ASSERT (rel_hdr->sh_entsize | |
3857 | == sizeof (Elf_External_Rela)); | |
3858 | ||
3859 | erela = (Elf_External_Rela *) rel_hdr->contents + i; | |
3860 | elf_swap_reloca_in (abfd, erela, &irela); | |
3861 | irela.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
3862 | ELF_R_TYPE (irela.r_info)); | |
3863 | elf_swap_reloca_out (abfd, &irela, erela); | |
3864 | } | |
3865 | } | |
3866 | ||
3867 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
3868 | trying to swap the relocs out itself. */ | |
3869 | o->reloc_count = 0; | |
3870 | } | |
3871 | ||
3872 | /* If we are linking against a dynamic object, or generating a | |
3873 | shared library, finish up the dynamic linking information. */ | |
3874 | if (dynamic) | |
3875 | { | |
3876 | Elf_External_Dyn *dyncon, *dynconend; | |
3877 | ||
3878 | /* Fix up .dynamic entries. */ | |
3879 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
3880 | BFD_ASSERT (o != NULL); | |
3881 | ||
3882 | dyncon = (Elf_External_Dyn *) o->contents; | |
3883 | dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size); | |
3884 | for (; dyncon < dynconend; dyncon++) | |
3885 | { | |
3886 | Elf_Internal_Dyn dyn; | |
3887 | const char *name; | |
3888 | unsigned int type; | |
3889 | ||
3890 | elf_swap_dyn_in (dynobj, dyncon, &dyn); | |
3891 | ||
3892 | switch (dyn.d_tag) | |
3893 | { | |
3894 | default: | |
3895 | break; | |
3896 | ||
3897 | /* SVR4 linkers seem to set DT_INIT and DT_FINI based on | |
3898 | magic _init and _fini symbols. This is pretty ugly, | |
3899 | but we are compatible. */ | |
3900 | case DT_INIT: | |
3901 | name = "_init"; | |
3902 | goto get_sym; | |
3903 | case DT_FINI: | |
3904 | name = "_fini"; | |
3905 | get_sym: | |
3906 | { | |
3907 | struct elf_link_hash_entry *h; | |
3908 | ||
3909 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
3910 | false, false, true); | |
d6f672b8 ILT |
3911 | if (h != NULL |
3912 | && (h->root.type == bfd_link_hash_defined | |
3913 | || h->root.type == bfd_link_hash_defweak)) | |
ede4eed4 KR |
3914 | { |
3915 | dyn.d_un.d_val = h->root.u.def.value; | |
3916 | o = h->root.u.def.section; | |
3917 | if (o->output_section != NULL) | |
3918 | dyn.d_un.d_val += (o->output_section->vma | |
3919 | + o->output_offset); | |
3920 | else | |
d6f672b8 ILT |
3921 | { |
3922 | /* The symbol is imported from another shared | |
3923 | library and does not apply to this one. */ | |
3924 | dyn.d_un.d_val = 0; | |
3925 | } | |
3926 | ||
3927 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
ede4eed4 | 3928 | } |
ede4eed4 KR |
3929 | } |
3930 | break; | |
3931 | ||
3932 | case DT_HASH: | |
3933 | name = ".hash"; | |
3934 | goto get_vma; | |
3935 | case DT_STRTAB: | |
3936 | name = ".dynstr"; | |
3937 | goto get_vma; | |
3938 | case DT_SYMTAB: | |
3939 | name = ".dynsym"; | |
d044b40a ILT |
3940 | goto get_vma; |
3941 | case DT_VERDEF: | |
3942 | name = ".gnu.version_d"; | |
3943 | goto get_vma; | |
3944 | case DT_VERNEED: | |
3945 | name = ".gnu.version_r"; | |
3946 | goto get_vma; | |
3947 | case DT_VERSYM: | |
3948 | name = ".gnu.version"; | |
ede4eed4 KR |
3949 | get_vma: |
3950 | o = bfd_get_section_by_name (abfd, name); | |
3951 | BFD_ASSERT (o != NULL); | |
3952 | dyn.d_un.d_ptr = o->vma; | |
3953 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
3954 | break; | |
3955 | ||
3956 | case DT_REL: | |
3957 | case DT_RELA: | |
3958 | case DT_RELSZ: | |
3959 | case DT_RELASZ: | |
3960 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
3961 | type = SHT_REL; | |
3962 | else | |
3963 | type = SHT_RELA; | |
3964 | dyn.d_un.d_val = 0; | |
3965 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) | |
3966 | { | |
3967 | Elf_Internal_Shdr *hdr; | |
3968 | ||
3969 | hdr = elf_elfsections (abfd)[i]; | |
3970 | if (hdr->sh_type == type | |
3971 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
3972 | { | |
3973 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
3974 | dyn.d_un.d_val += hdr->sh_size; | |
3975 | else | |
3976 | { | |
3977 | if (dyn.d_un.d_val == 0 | |
3978 | || hdr->sh_addr < dyn.d_un.d_val) | |
3979 | dyn.d_un.d_val = hdr->sh_addr; | |
3980 | } | |
3981 | } | |
3982 | } | |
3983 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
3984 | break; | |
3985 | } | |
3986 | } | |
3987 | } | |
3988 | ||
3989 | /* If we have created any dynamic sections, then output them. */ | |
3990 | if (dynobj != NULL) | |
3991 | { | |
3992 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) | |
3993 | goto error_return; | |
3994 | ||
3995 | for (o = dynobj->sections; o != NULL; o = o->next) | |
3996 | { | |
3997 | if ((o->flags & SEC_HAS_CONTENTS) == 0 | |
3998 | || o->_raw_size == 0) | |
3999 | continue; | |
ff12f303 | 4000 | if ((o->flags & SEC_LINKER_CREATED) == 0) |
ede4eed4 KR |
4001 | { |
4002 | /* At this point, we are only interested in sections | |
ff12f303 | 4003 | created by elf_link_create_dynamic_sections. */ |
ede4eed4 KR |
4004 | continue; |
4005 | } | |
4006 | if ((elf_section_data (o->output_section)->this_hdr.sh_type | |
4007 | != SHT_STRTAB) | |
4008 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
4009 | { | |
4010 | if (! bfd_set_section_contents (abfd, o->output_section, | |
4011 | o->contents, o->output_offset, | |
4012 | o->_raw_size)) | |
4013 | goto error_return; | |
4014 | } | |
4015 | else | |
4016 | { | |
4017 | file_ptr off; | |
4018 | ||
4019 | /* The contents of the .dynstr section are actually in a | |
4020 | stringtab. */ | |
4021 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
4022 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
4023 | || ! _bfd_stringtab_emit (abfd, | |
4024 | elf_hash_table (info)->dynstr)) | |
4025 | goto error_return; | |
4026 | } | |
4027 | } | |
4028 | } | |
4029 | ||
1726b8f0 ILT |
4030 | /* If we have optimized stabs strings, output them. */ |
4031 | if (elf_hash_table (info)->stab_info != NULL) | |
4032 | { | |
4033 | if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) | |
4034 | goto error_return; | |
4035 | } | |
4036 | ||
ede4eed4 KR |
4037 | if (finfo.symstrtab != NULL) |
4038 | _bfd_stringtab_free (finfo.symstrtab); | |
4039 | if (finfo.contents != NULL) | |
4040 | free (finfo.contents); | |
4041 | if (finfo.external_relocs != NULL) | |
4042 | free (finfo.external_relocs); | |
4043 | if (finfo.internal_relocs != NULL) | |
4044 | free (finfo.internal_relocs); | |
4045 | if (finfo.external_syms != NULL) | |
4046 | free (finfo.external_syms); | |
4047 | if (finfo.internal_syms != NULL) | |
4048 | free (finfo.internal_syms); | |
4049 | if (finfo.indices != NULL) | |
4050 | free (finfo.indices); | |
4051 | if (finfo.sections != NULL) | |
4052 | free (finfo.sections); | |
4053 | if (finfo.symbuf != NULL) | |
4054 | free (finfo.symbuf); | |
4055 | for (o = abfd->sections; o != NULL; o = o->next) | |
4056 | { | |
4057 | if ((o->flags & SEC_RELOC) != 0 | |
4058 | && elf_section_data (o)->rel_hashes != NULL) | |
4059 | free (elf_section_data (o)->rel_hashes); | |
4060 | } | |
4061 | ||
4062 | elf_tdata (abfd)->linker = true; | |
4063 | ||
4064 | return true; | |
4065 | ||
4066 | error_return: | |
4067 | if (finfo.symstrtab != NULL) | |
4068 | _bfd_stringtab_free (finfo.symstrtab); | |
4069 | if (finfo.contents != NULL) | |
4070 | free (finfo.contents); | |
4071 | if (finfo.external_relocs != NULL) | |
4072 | free (finfo.external_relocs); | |
4073 | if (finfo.internal_relocs != NULL) | |
4074 | free (finfo.internal_relocs); | |
4075 | if (finfo.external_syms != NULL) | |
4076 | free (finfo.external_syms); | |
4077 | if (finfo.internal_syms != NULL) | |
4078 | free (finfo.internal_syms); | |
4079 | if (finfo.indices != NULL) | |
4080 | free (finfo.indices); | |
4081 | if (finfo.sections != NULL) | |
4082 | free (finfo.sections); | |
4083 | if (finfo.symbuf != NULL) | |
4084 | free (finfo.symbuf); | |
4085 | for (o = abfd->sections; o != NULL; o = o->next) | |
4086 | { | |
4087 | if ((o->flags & SEC_RELOC) != 0 | |
4088 | && elf_section_data (o)->rel_hashes != NULL) | |
4089 | free (elf_section_data (o)->rel_hashes); | |
4090 | } | |
4091 | ||
4092 | return false; | |
4093 | } | |
4094 | ||
4095 | /* Add a symbol to the output symbol table. */ | |
4096 | ||
4097 | static boolean | |
4098 | elf_link_output_sym (finfo, name, elfsym, input_sec) | |
4099 | struct elf_final_link_info *finfo; | |
4100 | const char *name; | |
4101 | Elf_Internal_Sym *elfsym; | |
4102 | asection *input_sec; | |
4103 | { | |
4104 | boolean (*output_symbol_hook) PARAMS ((bfd *, | |
4105 | struct bfd_link_info *info, | |
4106 | const char *, | |
4107 | Elf_Internal_Sym *, | |
4108 | asection *)); | |
4109 | ||
4110 | output_symbol_hook = get_elf_backend_data (finfo->output_bfd)-> | |
4111 | elf_backend_link_output_symbol_hook; | |
4112 | if (output_symbol_hook != NULL) | |
4113 | { | |
4114 | if (! ((*output_symbol_hook) | |
4115 | (finfo->output_bfd, finfo->info, name, elfsym, input_sec))) | |
4116 | return false; | |
4117 | } | |
4118 | ||
4119 | if (name == (const char *) NULL || *name == '\0') | |
4120 | elfsym->st_name = 0; | |
4121 | else | |
4122 | { | |
4123 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, | |
4124 | name, true, | |
4125 | false); | |
4126 | if (elfsym->st_name == (unsigned long) -1) | |
4127 | return false; | |
4128 | } | |
4129 | ||
4130 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
4131 | { | |
4132 | if (! elf_link_flush_output_syms (finfo)) | |
4133 | return false; | |
4134 | } | |
4135 | ||
4136 | elf_swap_symbol_out (finfo->output_bfd, elfsym, | |
cf9fb9f2 | 4137 | (PTR) (finfo->symbuf + finfo->symbuf_count)); |
ede4eed4 KR |
4138 | ++finfo->symbuf_count; |
4139 | ||
4140 | ++finfo->output_bfd->symcount; | |
4141 | ||
4142 | return true; | |
4143 | } | |
4144 | ||
4145 | /* Flush the output symbols to the file. */ | |
4146 | ||
4147 | static boolean | |
4148 | elf_link_flush_output_syms (finfo) | |
4149 | struct elf_final_link_info *finfo; | |
4150 | { | |
28c16b55 ILT |
4151 | if (finfo->symbuf_count > 0) |
4152 | { | |
4153 | Elf_Internal_Shdr *symtab; | |
ede4eed4 | 4154 | |
28c16b55 | 4155 | symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr; |
ede4eed4 | 4156 | |
28c16b55 ILT |
4157 | if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size, |
4158 | SEEK_SET) != 0 | |
4159 | || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count, | |
4160 | sizeof (Elf_External_Sym), finfo->output_bfd) | |
4161 | != finfo->symbuf_count * sizeof (Elf_External_Sym))) | |
4162 | return false; | |
ede4eed4 | 4163 | |
28c16b55 | 4164 | symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym); |
ede4eed4 | 4165 | |
28c16b55 ILT |
4166 | finfo->symbuf_count = 0; |
4167 | } | |
ede4eed4 KR |
4168 | |
4169 | return true; | |
4170 | } | |
4171 | ||
4172 | /* Add an external symbol to the symbol table. This is called from | |
52c92c7f ILT |
4173 | the hash table traversal routine. When generating a shared object, |
4174 | we go through the symbol table twice. The first time we output | |
4175 | anything that might have been forced to local scope in a version | |
4176 | script. The second time we output the symbols that are still | |
4177 | global symbols. */ | |
ede4eed4 KR |
4178 | |
4179 | static boolean | |
4180 | elf_link_output_extsym (h, data) | |
4181 | struct elf_link_hash_entry *h; | |
4182 | PTR data; | |
4183 | { | |
52c92c7f ILT |
4184 | struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; |
4185 | struct elf_final_link_info *finfo = eoinfo->finfo; | |
ede4eed4 KR |
4186 | boolean strip; |
4187 | Elf_Internal_Sym sym; | |
4188 | asection *input_sec; | |
4189 | ||
52c92c7f ILT |
4190 | /* Decide whether to output this symbol in this pass. */ |
4191 | if (eoinfo->localsyms) | |
4192 | { | |
4193 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) | |
4194 | return true; | |
4195 | } | |
4196 | else | |
4197 | { | |
4198 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
4199 | return true; | |
4200 | } | |
4201 | ||
ede4eed4 KR |
4202 | /* If we are not creating a shared library, and this symbol is |
4203 | referenced by a shared library but is not defined anywhere, then | |
4204 | warn that it is undefined. If we do not do this, the runtime | |
4205 | linker will complain that the symbol is undefined when the | |
4206 | program is run. We don't have to worry about symbols that are | |
4207 | referenced by regular files, because we will already have issued | |
252239f8 | 4208 | warnings for them. */ |
ede4eed4 KR |
4209 | if (! finfo->info->relocateable |
4210 | && ! finfo->info->shared | |
4211 | && h->root.type == bfd_link_hash_undefined | |
4212 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0 | |
252239f8 | 4213 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) |
ede4eed4 KR |
4214 | { |
4215 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
4216 | (finfo->info, h->root.root.string, h->root.u.undef.abfd, | |
4217 | (asection *) NULL, 0))) | |
4218 | { | |
52c92c7f | 4219 | eoinfo->failed = true; |
ede4eed4 KR |
4220 | return false; |
4221 | } | |
4222 | } | |
4223 | ||
4224 | /* We don't want to output symbols that have never been mentioned by | |
4225 | a regular file, or that we have been told to strip. However, if | |
4226 | h->indx is set to -2, the symbol is used by a reloc and we must | |
4227 | output it. */ | |
4228 | if (h->indx == -2) | |
4229 | strip = false; | |
4230 | else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
4231 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
4232 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
4233 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
4234 | strip = true; | |
4235 | else if (finfo->info->strip == strip_all | |
4236 | || (finfo->info->strip == strip_some | |
4237 | && bfd_hash_lookup (finfo->info->keep_hash, | |
4238 | h->root.root.string, | |
4239 | false, false) == NULL)) | |
4240 | strip = true; | |
4241 | else | |
4242 | strip = false; | |
4243 | ||
4244 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
4245 | nothing else to do. */ | |
4246 | if (strip && h->dynindx == -1) | |
4247 | return true; | |
4248 | ||
4249 | sym.st_value = 0; | |
4250 | sym.st_size = h->size; | |
6c02f1a0 | 4251 | sym.st_other = h->other; |
52c92c7f ILT |
4252 | if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) |
4253 | sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); | |
4254 | else if (h->root.type == bfd_link_hash_undefweak | |
4255 | || h->root.type == bfd_link_hash_defweak) | |
ede4eed4 KR |
4256 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); |
4257 | else | |
4258 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
4259 | ||
4260 | switch (h->root.type) | |
4261 | { | |
4262 | default: | |
4263 | case bfd_link_hash_new: | |
4264 | abort (); | |
4265 | return false; | |
4266 | ||
4267 | case bfd_link_hash_undefined: | |
4268 | input_sec = bfd_und_section_ptr; | |
4269 | sym.st_shndx = SHN_UNDEF; | |
4270 | break; | |
4271 | ||
4272 | case bfd_link_hash_undefweak: | |
4273 | input_sec = bfd_und_section_ptr; | |
4274 | sym.st_shndx = SHN_UNDEF; | |
4275 | break; | |
4276 | ||
4277 | case bfd_link_hash_defined: | |
4278 | case bfd_link_hash_defweak: | |
4279 | { | |
4280 | input_sec = h->root.u.def.section; | |
4281 | if (input_sec->output_section != NULL) | |
4282 | { | |
4283 | sym.st_shndx = | |
4284 | _bfd_elf_section_from_bfd_section (finfo->output_bfd, | |
4285 | input_sec->output_section); | |
4286 | if (sym.st_shndx == (unsigned short) -1) | |
4287 | { | |
52c92c7f | 4288 | eoinfo->failed = true; |
ede4eed4 KR |
4289 | return false; |
4290 | } | |
4291 | ||
4292 | /* ELF symbols in relocateable files are section relative, | |
4293 | but in nonrelocateable files they are virtual | |
4294 | addresses. */ | |
4295 | sym.st_value = h->root.u.def.value + input_sec->output_offset; | |
4296 | if (! finfo->info->relocateable) | |
4297 | sym.st_value += input_sec->output_section->vma; | |
4298 | } | |
4299 | else | |
4300 | { | |
e549b1d2 ILT |
4301 | BFD_ASSERT (input_sec->owner == NULL |
4302 | || (input_sec->owner->flags & DYNAMIC) != 0); | |
ede4eed4 KR |
4303 | sym.st_shndx = SHN_UNDEF; |
4304 | input_sec = bfd_und_section_ptr; | |
4305 | } | |
4306 | } | |
4307 | break; | |
4308 | ||
4309 | case bfd_link_hash_common: | |
8211c929 | 4310 | input_sec = h->root.u.c.p->section; |
ede4eed4 KR |
4311 | sym.st_shndx = SHN_COMMON; |
4312 | sym.st_value = 1 << h->root.u.c.p->alignment_power; | |
4313 | break; | |
4314 | ||
4315 | case bfd_link_hash_indirect: | |
d044b40a ILT |
4316 | /* These symbols are created by symbol versioning. They point |
4317 | to the decorated version of the name. For example, if the | |
4318 | symbol foo@@GNU_1.2 is the default, which should be used when | |
4319 | foo is used with no version, then we add an indirect symbol | |
d6bfcdb5 ILT |
4320 | foo which points to foo@@GNU_1.2. We ignore these symbols, |
4321 | since the indirected symbol is already in the hash table. If | |
4322 | the indirect symbol is non-ELF, fall through and output it. */ | |
4323 | if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) == 0) | |
d044b40a ILT |
4324 | return true; |
4325 | ||
4326 | /* Fall through. */ | |
ede4eed4 | 4327 | case bfd_link_hash_warning: |
d044b40a ILT |
4328 | /* We can't represent these symbols in ELF, although a warning |
4329 | symbol may have come from a .gnu.warning.SYMBOL section. We | |
1f4ae0d6 ILT |
4330 | just put the target symbol in the hash table. If the target |
4331 | symbol does not really exist, don't do anything. */ | |
4332 | if (h->root.u.i.link->type == bfd_link_hash_new) | |
4333 | return true; | |
0cb70568 ILT |
4334 | return (elf_link_output_extsym |
4335 | ((struct elf_link_hash_entry *) h->root.u.i.link, data)); | |
ede4eed4 KR |
4336 | } |
4337 | ||
8519ea21 ILT |
4338 | /* Give the processor backend a chance to tweak the symbol value, |
4339 | and also to finish up anything that needs to be done for this | |
4340 | symbol. */ | |
4341 | if ((h->dynindx != -1 | |
4342 | || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0) | |
4343 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
4344 | { | |
4345 | struct elf_backend_data *bed; | |
4346 | ||
4347 | bed = get_elf_backend_data (finfo->output_bfd); | |
4348 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
4349 | (finfo->output_bfd, finfo->info, h, &sym))) | |
4350 | { | |
4351 | eoinfo->failed = true; | |
4352 | return false; | |
4353 | } | |
4354 | } | |
4355 | ||
ede4eed4 KR |
4356 | /* If this symbol should be put in the .dynsym section, then put it |
4357 | there now. We have already know the symbol index. We also fill | |
4358 | in the entry in the .hash section. */ | |
4359 | if (h->dynindx != -1 | |
4360 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
4361 | { | |
d044b40a ILT |
4362 | char *p, *copy; |
4363 | const char *name; | |
ede4eed4 KR |
4364 | size_t bucketcount; |
4365 | size_t bucket; | |
4366 | bfd_byte *bucketpos; | |
4367 | bfd_vma chain; | |
4368 | ||
4369 | sym.st_name = h->dynstr_index; | |
4370 | ||
ede4eed4 | 4371 | elf_swap_symbol_out (finfo->output_bfd, &sym, |
cf9fb9f2 ILT |
4372 | (PTR) (((Elf_External_Sym *) |
4373 | finfo->dynsym_sec->contents) | |
4374 | + h->dynindx)); | |
ede4eed4 | 4375 | |
d044b40a ILT |
4376 | /* We didn't include the version string in the dynamic string |
4377 | table, so we must not consider it in the hash table. */ | |
4378 | name = h->root.root.string; | |
4379 | p = strchr (name, ELF_VER_CHR); | |
4380 | if (p == NULL) | |
4381 | copy = NULL; | |
4382 | else | |
4383 | { | |
4384 | copy = bfd_alloc (finfo->output_bfd, p - name + 1); | |
4385 | strncpy (copy, name, p - name); | |
4386 | copy[p - name] = '\0'; | |
4387 | name = copy; | |
4388 | } | |
4389 | ||
ede4eed4 | 4390 | bucketcount = elf_hash_table (finfo->info)->bucketcount; |
d044b40a | 4391 | bucket = bfd_elf_hash ((const unsigned char *) name) % bucketcount; |
ede4eed4 KR |
4392 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents |
4393 | + (bucket + 2) * (ARCH_SIZE / 8)); | |
4394 | chain = get_word (finfo->output_bfd, bucketpos); | |
4395 | put_word (finfo->output_bfd, h->dynindx, bucketpos); | |
4396 | put_word (finfo->output_bfd, chain, | |
4397 | ((bfd_byte *) finfo->hash_sec->contents | |
4398 | + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8))); | |
d044b40a ILT |
4399 | |
4400 | if (copy != NULL) | |
4401 | bfd_release (finfo->output_bfd, copy); | |
4402 | ||
4403 | if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) | |
4404 | { | |
4405 | Elf_Internal_Versym iversym; | |
4406 | ||
4407 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
4408 | { | |
4409 | if (h->verinfo.verdef == NULL) | |
4410 | iversym.vs_vers = 0; | |
4411 | else | |
4412 | iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; | |
4413 | } | |
4414 | else | |
4415 | { | |
4416 | if (h->verinfo.vertree == NULL) | |
4417 | iversym.vs_vers = 1; | |
4418 | else | |
4419 | iversym.vs_vers = h->verinfo.vertree->vernum + 1; | |
4420 | } | |
4421 | ||
4422 | if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0) | |
4423 | iversym.vs_vers |= VERSYM_HIDDEN; | |
4424 | ||
4425 | _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, | |
4426 | (((Elf_External_Versym *) | |
4427 | finfo->symver_sec->contents) | |
4428 | + h->dynindx)); | |
4429 | } | |
ede4eed4 KR |
4430 | } |
4431 | ||
4432 | /* If we're stripping it, then it was just a dynamic symbol, and | |
4433 | there's nothing else to do. */ | |
4434 | if (strip) | |
4435 | return true; | |
4436 | ||
4437 | h->indx = finfo->output_bfd->symcount; | |
4438 | ||
4439 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec)) | |
4440 | { | |
52c92c7f | 4441 | eoinfo->failed = true; |
ede4eed4 KR |
4442 | return false; |
4443 | } | |
4444 | ||
4445 | return true; | |
4446 | } | |
4447 | ||
4448 | /* Link an input file into the linker output file. This function | |
4449 | handles all the sections and relocations of the input file at once. | |
4450 | This is so that we only have to read the local symbols once, and | |
4451 | don't have to keep them in memory. */ | |
4452 | ||
4453 | static boolean | |
4454 | elf_link_input_bfd (finfo, input_bfd) | |
4455 | struct elf_final_link_info *finfo; | |
4456 | bfd *input_bfd; | |
4457 | { | |
4458 | boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *, | |
4459 | bfd *, asection *, bfd_byte *, | |
4460 | Elf_Internal_Rela *, | |
4461 | Elf_Internal_Sym *, asection **)); | |
4462 | bfd *output_bfd; | |
4463 | Elf_Internal_Shdr *symtab_hdr; | |
4464 | size_t locsymcount; | |
4465 | size_t extsymoff; | |
c86158e5 | 4466 | Elf_External_Sym *external_syms; |
ede4eed4 KR |
4467 | Elf_External_Sym *esym; |
4468 | Elf_External_Sym *esymend; | |
4469 | Elf_Internal_Sym *isym; | |
4470 | long *pindex; | |
4471 | asection **ppsection; | |
4472 | asection *o; | |
4473 | ||
4474 | output_bfd = finfo->output_bfd; | |
4475 | relocate_section = | |
4476 | get_elf_backend_data (output_bfd)->elf_backend_relocate_section; | |
4477 | ||
4478 | /* If this is a dynamic object, we don't want to do anything here: | |
4479 | we don't want the local symbols, and we don't want the section | |
4480 | contents. */ | |
d044b40a | 4481 | if ((input_bfd->flags & DYNAMIC) != 0) |
ede4eed4 KR |
4482 | return true; |
4483 | ||
4484 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
4485 | if (elf_bad_symtab (input_bfd)) | |
4486 | { | |
4487 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); | |
4488 | extsymoff = 0; | |
4489 | } | |
4490 | else | |
4491 | { | |
4492 | locsymcount = symtab_hdr->sh_info; | |
4493 | extsymoff = symtab_hdr->sh_info; | |
4494 | } | |
4495 | ||
4496 | /* Read the local symbols. */ | |
c86158e5 ILT |
4497 | if (symtab_hdr->contents != NULL) |
4498 | external_syms = (Elf_External_Sym *) symtab_hdr->contents; | |
4499 | else if (locsymcount == 0) | |
4500 | external_syms = NULL; | |
4501 | else | |
4502 | { | |
4503 | external_syms = finfo->external_syms; | |
4504 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
4505 | || (bfd_read (external_syms, sizeof (Elf_External_Sym), | |
ede4eed4 | 4506 | locsymcount, input_bfd) |
c86158e5 ILT |
4507 | != locsymcount * sizeof (Elf_External_Sym))) |
4508 | return false; | |
4509 | } | |
ede4eed4 KR |
4510 | |
4511 | /* Swap in the local symbols and write out the ones which we know | |
4512 | are going into the output file. */ | |
c86158e5 | 4513 | esym = external_syms; |
ede4eed4 KR |
4514 | esymend = esym + locsymcount; |
4515 | isym = finfo->internal_syms; | |
4516 | pindex = finfo->indices; | |
4517 | ppsection = finfo->sections; | |
4518 | for (; esym < esymend; esym++, isym++, pindex++, ppsection++) | |
4519 | { | |
4520 | asection *isec; | |
4521 | const char *name; | |
4522 | Elf_Internal_Sym osym; | |
4523 | ||
4524 | elf_swap_symbol_in (input_bfd, esym, isym); | |
4525 | *pindex = -1; | |
4526 | ||
4527 | if (elf_bad_symtab (input_bfd)) | |
4528 | { | |
4529 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
4530 | { | |
4531 | *ppsection = NULL; | |
4532 | continue; | |
4533 | } | |
4534 | } | |
4535 | ||
4536 | if (isym->st_shndx == SHN_UNDEF) | |
4537 | isec = bfd_und_section_ptr; | |
4538 | else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE) | |
4539 | isec = section_from_elf_index (input_bfd, isym->st_shndx); | |
4540 | else if (isym->st_shndx == SHN_ABS) | |
4541 | isec = bfd_abs_section_ptr; | |
4542 | else if (isym->st_shndx == SHN_COMMON) | |
4543 | isec = bfd_com_section_ptr; | |
4544 | else | |
4545 | { | |
4546 | /* Who knows? */ | |
4547 | isec = NULL; | |
4548 | } | |
4549 | ||
4550 | *ppsection = isec; | |
4551 | ||
4552 | /* Don't output the first, undefined, symbol. */ | |
c86158e5 | 4553 | if (esym == external_syms) |
ede4eed4 KR |
4554 | continue; |
4555 | ||
4556 | /* If we are stripping all symbols, we don't want to output this | |
4557 | one. */ | |
4558 | if (finfo->info->strip == strip_all) | |
4559 | continue; | |
4560 | ||
4561 | /* We never output section symbols. Instead, we use the section | |
4562 | symbol of the corresponding section in the output file. */ | |
4563 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
4564 | continue; | |
4565 | ||
4566 | /* If we are discarding all local symbols, we don't want to | |
4567 | output this one. If we are generating a relocateable output | |
4568 | file, then some of the local symbols may be required by | |
4569 | relocs; we output them below as we discover that they are | |
4570 | needed. */ | |
4571 | if (finfo->info->discard == discard_all) | |
4572 | continue; | |
4573 | ||
258b1f5d | 4574 | /* If this symbol is defined in a section which we are |
fa802cb0 ILT |
4575 | discarding, we don't need to keep it, but note that |
4576 | linker_mark is only reliable for sections that have contents. | |
4577 | For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE | |
4578 | as well as linker_mark. */ | |
258b1f5d ILT |
4579 | if (isym->st_shndx > 0 |
4580 | && isym->st_shndx < SHN_LORESERVE | |
4581 | && isec != NULL | |
fa802cb0 | 4582 | && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0) |
258b1f5d ILT |
4583 | || (! finfo->info->relocateable |
4584 | && (isec->flags & SEC_EXCLUDE) != 0))) | |
4585 | continue; | |
4586 | ||
ede4eed4 KR |
4587 | /* Get the name of the symbol. */ |
4588 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
258b1f5d | 4589 | isym->st_name); |
ede4eed4 KR |
4590 | if (name == NULL) |
4591 | return false; | |
4592 | ||
4593 | /* See if we are discarding symbols with this name. */ | |
4594 | if ((finfo->info->strip == strip_some | |
4595 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false) | |
4596 | == NULL)) | |
4597 | || (finfo->info->discard == discard_l | |
e316f514 | 4598 | && bfd_is_local_label_name (input_bfd, name))) |
ede4eed4 KR |
4599 | continue; |
4600 | ||
4601 | /* If we get here, we are going to output this symbol. */ | |
4602 | ||
4603 | osym = *isym; | |
4604 | ||
4605 | /* Adjust the section index for the output file. */ | |
4606 | osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, | |
4607 | isec->output_section); | |
4608 | if (osym.st_shndx == (unsigned short) -1) | |
4609 | return false; | |
4610 | ||
4611 | *pindex = output_bfd->symcount; | |
4612 | ||
4613 | /* ELF symbols in relocateable files are section relative, but | |
4614 | in executable files they are virtual addresses. Note that | |
4615 | this code assumes that all ELF sections have an associated | |
4616 | BFD section with a reasonable value for output_offset; below | |
4617 | we assume that they also have a reasonable value for | |
4618 | output_section. Any special sections must be set up to meet | |
4619 | these requirements. */ | |
4620 | osym.st_value += isec->output_offset; | |
4621 | if (! finfo->info->relocateable) | |
4622 | osym.st_value += isec->output_section->vma; | |
4623 | ||
4624 | if (! elf_link_output_sym (finfo, name, &osym, isec)) | |
4625 | return false; | |
4626 | } | |
4627 | ||
4628 | /* Relocate the contents of each section. */ | |
4629 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
4630 | { | |
c86158e5 ILT |
4631 | bfd_byte *contents; |
4632 | ||
ff0e4a93 | 4633 | if (! o->linker_mark) |
7ec49f91 ILT |
4634 | { |
4635 | /* This section was omitted from the link. */ | |
4636 | continue; | |
4637 | } | |
4638 | ||
1726b8f0 ILT |
4639 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
4640 | || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0)) | |
ede4eed4 KR |
4641 | continue; |
4642 | ||
ff12f303 | 4643 | if ((o->flags & SEC_LINKER_CREATED) != 0) |
ede4eed4 | 4644 | { |
ff12f303 ILT |
4645 | /* Section was created by elf_link_create_dynamic_sections |
4646 | or somesuch. */ | |
ede4eed4 KR |
4647 | continue; |
4648 | } | |
4649 | ||
c86158e5 ILT |
4650 | /* Get the contents of the section. They have been cached by a |
4651 | relaxation routine. Note that o is a section in an input | |
4652 | file, so the contents field will not have been set by any of | |
4653 | the routines which work on output files. */ | |
4654 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
4655 | contents = elf_section_data (o)->this_hdr.contents; | |
4656 | else | |
4657 | { | |
4658 | contents = finfo->contents; | |
4659 | if (! bfd_get_section_contents (input_bfd, o, contents, | |
4660 | (file_ptr) 0, o->_raw_size)) | |
4661 | return false; | |
4662 | } | |
ede4eed4 KR |
4663 | |
4664 | if ((o->flags & SEC_RELOC) != 0) | |
4665 | { | |
4666 | Elf_Internal_Rela *internal_relocs; | |
4667 | ||
4668 | /* Get the swapped relocs. */ | |
c86158e5 ILT |
4669 | internal_relocs = (NAME(_bfd_elf,link_read_relocs) |
4670 | (input_bfd, o, finfo->external_relocs, | |
4671 | finfo->internal_relocs, false)); | |
ede4eed4 KR |
4672 | if (internal_relocs == NULL |
4673 | && o->reloc_count > 0) | |
4674 | return false; | |
4675 | ||
4676 | /* Relocate the section by invoking a back end routine. | |
4677 | ||
4678 | The back end routine is responsible for adjusting the | |
4679 | section contents as necessary, and (if using Rela relocs | |
4680 | and generating a relocateable output file) adjusting the | |
4681 | reloc addend as necessary. | |
4682 | ||
4683 | The back end routine does not have to worry about setting | |
4684 | the reloc address or the reloc symbol index. | |
4685 | ||
4686 | The back end routine is given a pointer to the swapped in | |
4687 | internal symbols, and can access the hash table entries | |
4688 | for the external symbols via elf_sym_hashes (input_bfd). | |
4689 | ||
4690 | When generating relocateable output, the back end routine | |
4691 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
4692 | output symbol is going to be a section symbol | |
4693 | corresponding to the output section, which will require | |
4694 | the addend to be adjusted. */ | |
4695 | ||
4696 | if (! (*relocate_section) (output_bfd, finfo->info, | |
c86158e5 | 4697 | input_bfd, o, contents, |
ede4eed4 KR |
4698 | internal_relocs, |
4699 | finfo->internal_syms, | |
4700 | finfo->sections)) | |
4701 | return false; | |
4702 | ||
4703 | if (finfo->info->relocateable) | |
4704 | { | |
4705 | Elf_Internal_Rela *irela; | |
4706 | Elf_Internal_Rela *irelaend; | |
4707 | struct elf_link_hash_entry **rel_hash; | |
4708 | Elf_Internal_Shdr *input_rel_hdr; | |
4709 | Elf_Internal_Shdr *output_rel_hdr; | |
4710 | ||
4711 | /* Adjust the reloc addresses and symbol indices. */ | |
4712 | ||
4713 | irela = internal_relocs; | |
4714 | irelaend = irela + o->reloc_count; | |
4715 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
4716 | + o->output_section->reloc_count); | |
4717 | for (; irela < irelaend; irela++, rel_hash++) | |
4718 | { | |
ae115e51 | 4719 | unsigned long r_symndx; |
ede4eed4 KR |
4720 | Elf_Internal_Sym *isym; |
4721 | asection *sec; | |
4722 | ||
4723 | irela->r_offset += o->output_offset; | |
4724 | ||
4725 | r_symndx = ELF_R_SYM (irela->r_info); | |
4726 | ||
4727 | if (r_symndx == 0) | |
4728 | continue; | |
4729 | ||
4730 | if (r_symndx >= locsymcount | |
4731 | || (elf_bad_symtab (input_bfd) | |
4732 | && finfo->sections[r_symndx] == NULL)) | |
4733 | { | |
5ee8d932 | 4734 | struct elf_link_hash_entry *rh; |
ede4eed4 KR |
4735 | long indx; |
4736 | ||
4737 | /* This is a reloc against a global symbol. We | |
4738 | have not yet output all the local symbols, so | |
4739 | we do not know the symbol index of any global | |
4740 | symbol. We set the rel_hash entry for this | |
4741 | reloc to point to the global hash table entry | |
4742 | for this symbol. The symbol index is then | |
4743 | set at the end of elf_bfd_final_link. */ | |
4744 | indx = r_symndx - extsymoff; | |
5ee8d932 ILT |
4745 | rh = elf_sym_hashes (input_bfd)[indx]; |
4746 | while (rh->root.type == bfd_link_hash_indirect | |
4747 | || rh->root.type == bfd_link_hash_warning) | |
4748 | rh = (struct elf_link_hash_entry *) rh->root.u.i.link; | |
ede4eed4 KR |
4749 | |
4750 | /* Setting the index to -2 tells | |
4751 | elf_link_output_extsym that this symbol is | |
4752 | used by a reloc. */ | |
5ee8d932 ILT |
4753 | BFD_ASSERT (rh->indx < 0); |
4754 | rh->indx = -2; | |
4755 | ||
4756 | *rel_hash = rh; | |
ede4eed4 KR |
4757 | |
4758 | continue; | |
4759 | } | |
4760 | ||
4761 | /* This is a reloc against a local symbol. */ | |
4762 | ||
4763 | *rel_hash = NULL; | |
4764 | isym = finfo->internal_syms + r_symndx; | |
4765 | sec = finfo->sections[r_symndx]; | |
4766 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
4767 | { | |
4768 | /* I suppose the backend ought to fill in the | |
4769 | section of any STT_SECTION symbol against a | |
ba4a4594 ILT |
4770 | processor specific section. If we have |
4771 | discarded a section, the output_section will | |
4772 | be the absolute section. */ | |
4773 | if (sec != NULL | |
4774 | && (bfd_is_abs_section (sec) | |
4775 | || (sec->output_section != NULL | |
4776 | && bfd_is_abs_section (sec->output_section)))) | |
ede4eed4 KR |
4777 | r_symndx = 0; |
4778 | else if (sec == NULL || sec->owner == NULL) | |
4779 | { | |
4780 | bfd_set_error (bfd_error_bad_value); | |
4781 | return false; | |
4782 | } | |
4783 | else | |
4784 | { | |
4785 | r_symndx = sec->output_section->target_index; | |
4786 | BFD_ASSERT (r_symndx != 0); | |
4787 | } | |
4788 | } | |
4789 | else | |
4790 | { | |
4791 | if (finfo->indices[r_symndx] == -1) | |
4792 | { | |
4793 | unsigned long link; | |
4794 | const char *name; | |
4795 | asection *osec; | |
4796 | ||
4797 | if (finfo->info->strip == strip_all) | |
4798 | { | |
4799 | /* You can't do ld -r -s. */ | |
4800 | bfd_set_error (bfd_error_invalid_operation); | |
4801 | return false; | |
4802 | } | |
4803 | ||
4804 | /* This symbol was skipped earlier, but | |
4805 | since it is needed by a reloc, we | |
4806 | must output it now. */ | |
4807 | link = symtab_hdr->sh_link; | |
4808 | name = bfd_elf_string_from_elf_section (input_bfd, | |
4809 | link, | |
4810 | isym->st_name); | |
4811 | if (name == NULL) | |
4812 | return false; | |
4813 | ||
4814 | osec = sec->output_section; | |
4815 | isym->st_shndx = | |
4816 | _bfd_elf_section_from_bfd_section (output_bfd, | |
4817 | osec); | |
4818 | if (isym->st_shndx == (unsigned short) -1) | |
4819 | return false; | |
4820 | ||
4821 | isym->st_value += sec->output_offset; | |
4822 | if (! finfo->info->relocateable) | |
4823 | isym->st_value += osec->vma; | |
4824 | ||
4825 | finfo->indices[r_symndx] = output_bfd->symcount; | |
4826 | ||
4827 | if (! elf_link_output_sym (finfo, name, isym, sec)) | |
4828 | return false; | |
4829 | } | |
4830 | ||
4831 | r_symndx = finfo->indices[r_symndx]; | |
4832 | } | |
4833 | ||
4834 | irela->r_info = ELF_R_INFO (r_symndx, | |
4835 | ELF_R_TYPE (irela->r_info)); | |
4836 | } | |
4837 | ||
4838 | /* Swap out the relocs. */ | |
4839 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
4840 | output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr; | |
4841 | BFD_ASSERT (output_rel_hdr->sh_entsize | |
4842 | == input_rel_hdr->sh_entsize); | |
4843 | irela = internal_relocs; | |
4844 | irelaend = irela + o->reloc_count; | |
4845 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
4846 | { | |
4847 | Elf_External_Rel *erel; | |
4848 | ||
4849 | erel = ((Elf_External_Rel *) output_rel_hdr->contents | |
4850 | + o->output_section->reloc_count); | |
4851 | for (; irela < irelaend; irela++, erel++) | |
4852 | { | |
4853 | Elf_Internal_Rel irel; | |
4854 | ||
4855 | irel.r_offset = irela->r_offset; | |
4856 | irel.r_info = irela->r_info; | |
4857 | BFD_ASSERT (irela->r_addend == 0); | |
4858 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
4859 | } | |
4860 | } | |
4861 | else | |
4862 | { | |
4863 | Elf_External_Rela *erela; | |
4864 | ||
4865 | BFD_ASSERT (input_rel_hdr->sh_entsize | |
4866 | == sizeof (Elf_External_Rela)); | |
4867 | erela = ((Elf_External_Rela *) output_rel_hdr->contents | |
4868 | + o->output_section->reloc_count); | |
4869 | for (; irela < irelaend; irela++, erela++) | |
4870 | elf_swap_reloca_out (output_bfd, irela, erela); | |
4871 | } | |
4872 | ||
4873 | o->output_section->reloc_count += o->reloc_count; | |
4874 | } | |
4875 | } | |
4876 | ||
4877 | /* Write out the modified section contents. */ | |
1726b8f0 ILT |
4878 | if (elf_section_data (o)->stab_info == NULL) |
4879 | { | |
4880 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
c86158e5 | 4881 | contents, o->output_offset, |
1726b8f0 ILT |
4882 | (o->_cooked_size != 0 |
4883 | ? o->_cooked_size | |
4884 | : o->_raw_size))) | |
4885 | return false; | |
4886 | } | |
4887 | else | |
4888 | { | |
3cd5cf3d ILT |
4889 | if (! (_bfd_write_section_stabs |
4890 | (output_bfd, &elf_hash_table (finfo->info)->stab_info, | |
4891 | o, &elf_section_data (o)->stab_info, contents))) | |
1726b8f0 ILT |
4892 | return false; |
4893 | } | |
ede4eed4 KR |
4894 | } |
4895 | ||
4896 | return true; | |
4897 | } | |
4898 | ||
4899 | /* Generate a reloc when linking an ELF file. This is a reloc | |
4900 | requested by the linker, and does come from any input file. This | |
4901 | is used to build constructor and destructor tables when linking | |
4902 | with -Ur. */ | |
4903 | ||
4904 | static boolean | |
4905 | elf_reloc_link_order (output_bfd, info, output_section, link_order) | |
4906 | bfd *output_bfd; | |
4907 | struct bfd_link_info *info; | |
4908 | asection *output_section; | |
4909 | struct bfd_link_order *link_order; | |
4910 | { | |
4911 | reloc_howto_type *howto; | |
4912 | long indx; | |
4913 | bfd_vma offset; | |
5b3b9ff6 | 4914 | bfd_vma addend; |
ede4eed4 KR |
4915 | struct elf_link_hash_entry **rel_hash_ptr; |
4916 | Elf_Internal_Shdr *rel_hdr; | |
4917 | ||
4918 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
4919 | if (howto == NULL) | |
4920 | { | |
4921 | bfd_set_error (bfd_error_bad_value); | |
4922 | return false; | |
4923 | } | |
4924 | ||
5b3b9ff6 ILT |
4925 | addend = link_order->u.reloc.p->addend; |
4926 | ||
4927 | /* Figure out the symbol index. */ | |
4928 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
4929 | + output_section->reloc_count); | |
4930 | if (link_order->type == bfd_section_reloc_link_order) | |
4931 | { | |
4932 | indx = link_order->u.reloc.p->u.section->target_index; | |
4933 | BFD_ASSERT (indx != 0); | |
4934 | *rel_hash_ptr = NULL; | |
4935 | } | |
4936 | else | |
4937 | { | |
4938 | struct elf_link_hash_entry *h; | |
4939 | ||
4940 | /* Treat a reloc against a defined symbol as though it were | |
4941 | actually against the section. */ | |
8881b321 ILT |
4942 | h = ((struct elf_link_hash_entry *) |
4943 | bfd_wrapped_link_hash_lookup (output_bfd, info, | |
4944 | link_order->u.reloc.p->u.name, | |
4945 | false, false, true)); | |
5b3b9ff6 ILT |
4946 | if (h != NULL |
4947 | && (h->root.type == bfd_link_hash_defined | |
4948 | || h->root.type == bfd_link_hash_defweak)) | |
4949 | { | |
4950 | asection *section; | |
4951 | ||
4952 | section = h->root.u.def.section; | |
4953 | indx = section->output_section->target_index; | |
4954 | *rel_hash_ptr = NULL; | |
4955 | /* It seems that we ought to add the symbol value to the | |
4956 | addend here, but in practice it has already been added | |
4957 | because it was passed to constructor_callback. */ | |
4958 | addend += section->output_section->vma + section->output_offset; | |
4959 | } | |
4960 | else if (h != NULL) | |
4961 | { | |
4962 | /* Setting the index to -2 tells elf_link_output_extsym that | |
4963 | this symbol is used by a reloc. */ | |
4964 | h->indx = -2; | |
4965 | *rel_hash_ptr = h; | |
4966 | indx = 0; | |
4967 | } | |
4968 | else | |
4969 | { | |
4970 | if (! ((*info->callbacks->unattached_reloc) | |
4971 | (info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
4972 | (asection *) NULL, (bfd_vma) 0))) | |
4973 | return false; | |
4974 | indx = 0; | |
4975 | } | |
4976 | } | |
4977 | ||
ede4eed4 KR |
4978 | /* If this is an inplace reloc, we must write the addend into the |
4979 | object file. */ | |
5b3b9ff6 | 4980 | if (howto->partial_inplace && addend != 0) |
ede4eed4 KR |
4981 | { |
4982 | bfd_size_type size; | |
4983 | bfd_reloc_status_type rstat; | |
4984 | bfd_byte *buf; | |
4985 | boolean ok; | |
4986 | ||
4987 | size = bfd_get_reloc_size (howto); | |
4988 | buf = (bfd_byte *) bfd_zmalloc (size); | |
4989 | if (buf == (bfd_byte *) NULL) | |
a9713b91 | 4990 | return false; |
5b3b9ff6 | 4991 | rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); |
ede4eed4 KR |
4992 | switch (rstat) |
4993 | { | |
4994 | case bfd_reloc_ok: | |
4995 | break; | |
4996 | default: | |
4997 | case bfd_reloc_outofrange: | |
4998 | abort (); | |
4999 | case bfd_reloc_overflow: | |
5000 | if (! ((*info->callbacks->reloc_overflow) | |
5001 | (info, | |
5002 | (link_order->type == bfd_section_reloc_link_order | |
5003 | ? bfd_section_name (output_bfd, | |
5004 | link_order->u.reloc.p->u.section) | |
5005 | : link_order->u.reloc.p->u.name), | |
5b3b9ff6 ILT |
5006 | howto->name, addend, (bfd *) NULL, (asection *) NULL, |
5007 | (bfd_vma) 0))) | |
ede4eed4 KR |
5008 | { |
5009 | free (buf); | |
5010 | return false; | |
5011 | } | |
5012 | break; | |
5013 | } | |
5014 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
5015 | (file_ptr) link_order->offset, size); | |
5016 | free (buf); | |
5017 | if (! ok) | |
5018 | return false; | |
5019 | } | |
5020 | ||
ede4eed4 KR |
5021 | /* The address of a reloc is relative to the section in a |
5022 | relocateable file, and is a virtual address in an executable | |
5023 | file. */ | |
5024 | offset = link_order->offset; | |
5025 | if (! info->relocateable) | |
5026 | offset += output_section->vma; | |
5027 | ||
5028 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
5029 | ||
5030 | if (rel_hdr->sh_type == SHT_REL) | |
5031 | { | |
5032 | Elf_Internal_Rel irel; | |
5033 | Elf_External_Rel *erel; | |
5034 | ||
5035 | irel.r_offset = offset; | |
5036 | irel.r_info = ELF_R_INFO (indx, howto->type); | |
5037 | erel = ((Elf_External_Rel *) rel_hdr->contents | |
5038 | + output_section->reloc_count); | |
5039 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
5040 | } | |
5041 | else | |
5042 | { | |
5043 | Elf_Internal_Rela irela; | |
5044 | Elf_External_Rela *erela; | |
5045 | ||
5046 | irela.r_offset = offset; | |
5047 | irela.r_info = ELF_R_INFO (indx, howto->type); | |
5b3b9ff6 | 5048 | irela.r_addend = addend; |
ede4eed4 KR |
5049 | erela = ((Elf_External_Rela *) rel_hdr->contents |
5050 | + output_section->reloc_count); | |
5051 | elf_swap_reloca_out (output_bfd, &irela, erela); | |
5052 | } | |
5053 | ||
5054 | ++output_section->reloc_count; | |
5055 | ||
5056 | return true; | |
5057 | } | |
5058 | ||
3b3753b8 MM |
5059 | \f |
5060 | /* Allocate a pointer to live in a linker created section. */ | |
5061 | ||
5062 | boolean | |
5063 | elf_create_pointer_linker_section (abfd, info, lsect, h, rel) | |
5064 | bfd *abfd; | |
5065 | struct bfd_link_info *info; | |
5066 | elf_linker_section_t *lsect; | |
5067 | struct elf_link_hash_entry *h; | |
5068 | const Elf_Internal_Rela *rel; | |
5069 | { | |
5070 | elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL; | |
5071 | elf_linker_section_pointers_t *linker_section_ptr; | |
5072 | unsigned long r_symndx = ELF_R_SYM (rel->r_info);; | |
5073 | ||
5074 | BFD_ASSERT (lsect != NULL); | |
5075 | ||
5076 | /* Is this a global symbol? */ | |
5077 | if (h != NULL) | |
5078 | { | |
5079 | /* Has this symbol already been allocated, if so, our work is done */ | |
5080 | if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer, | |
5081 | rel->r_addend, | |
5082 | lsect->which)) | |
5083 | return true; | |
5084 | ||
5085 | ptr_linker_section_ptr = &h->linker_section_pointer; | |
5086 | /* Make sure this symbol is output as a dynamic symbol. */ | |
5087 | if (h->dynindx == -1) | |
5088 | { | |
5089 | if (! elf_link_record_dynamic_symbol (info, h)) | |
5090 | return false; | |
5091 | } | |
5092 | ||
eb82bc60 MM |
5093 | if (lsect->rel_section) |
5094 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); | |
3b3753b8 MM |
5095 | } |
5096 | ||
5097 | else /* Allocation of a pointer to a local symbol */ | |
5098 | { | |
5099 | elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd); | |
5100 | ||
5101 | /* Allocate a table to hold the local symbols if first time */ | |
5102 | if (!ptr) | |
5103 | { | |
5104 | int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info; | |
5105 | register unsigned int i; | |
5106 | ||
5107 | ptr = (elf_linker_section_pointers_t **) | |
5108 | bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *)); | |
5109 | ||
5110 | if (!ptr) | |
5111 | return false; | |
5112 | ||
5113 | elf_local_ptr_offsets (abfd) = ptr; | |
5114 | for (i = 0; i < num_symbols; i++) | |
5115 | ptr[i] = (elf_linker_section_pointers_t *)0; | |
5116 | } | |
5117 | ||
5118 | /* Has this symbol already been allocated, if so, our work is done */ | |
5119 | if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx], | |
5120 | rel->r_addend, | |
5121 | lsect->which)) | |
5122 | return true; | |
5123 | ||
5124 | ptr_linker_section_ptr = &ptr[r_symndx]; | |
5125 | ||
5126 | if (info->shared) | |
5127 | { | |
5128 | /* If we are generating a shared object, we need to | |
05f927dd | 5129 | output a R_<xxx>_RELATIVE reloc so that the |
3b3753b8 MM |
5130 | dynamic linker can adjust this GOT entry. */ |
5131 | BFD_ASSERT (lsect->rel_section != NULL); | |
5132 | lsect->rel_section->_raw_size += sizeof (Elf_External_Rela); | |
5133 | } | |
5134 | } | |
5135 | ||
5136 | /* Allocate space for a pointer in the linker section, and allocate a new pointer record | |
5137 | from internal memory. */ | |
5138 | BFD_ASSERT (ptr_linker_section_ptr != NULL); | |
5139 | linker_section_ptr = (elf_linker_section_pointers_t *) | |
5140 | bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t)); | |
5141 | ||
5142 | if (!linker_section_ptr) | |
5143 | return false; | |
5144 | ||
5145 | linker_section_ptr->next = *ptr_linker_section_ptr; | |
5146 | linker_section_ptr->addend = rel->r_addend; | |
5147 | linker_section_ptr->which = lsect->which; | |
5148 | linker_section_ptr->written_address_p = false; | |
5149 | *ptr_linker_section_ptr = linker_section_ptr; | |
5150 | ||
cb73f5d7 | 5151 | #if 0 |
3b3753b8 MM |
5152 | if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset) |
5153 | { | |
cb73f5d7 | 5154 | linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8); |
3b3753b8 MM |
5155 | lsect->hole_offset += ARCH_SIZE / 8; |
5156 | lsect->sym_offset += ARCH_SIZE / 8; | |
5157 | if (lsect->sym_hash) /* Bump up symbol value if needed */ | |
4a4953f5 MM |
5158 | { |
5159 | lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8; | |
5160 | #ifdef DEBUG | |
5161 | fprintf (stderr, "Bump up %s by %ld, current value = %ld\n", | |
5162 | lsect->sym_hash->root.root.string, | |
5163 | (long)ARCH_SIZE / 8, | |
5164 | (long)lsect->sym_hash->root.u.def.value); | |
5165 | #endif | |
5166 | } | |
3b3753b8 MM |
5167 | } |
5168 | else | |
cb73f5d7 | 5169 | #endif |
3b3753b8 MM |
5170 | linker_section_ptr->offset = lsect->section->_raw_size; |
5171 | ||
5172 | lsect->section->_raw_size += ARCH_SIZE / 8; | |
5173 | ||
5174 | #ifdef DEBUG | |
5175 | fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n", | |
5176 | lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size); | |
5177 | #endif | |
5178 | ||
5179 | return true; | |
5180 | } | |
5181 | ||
5182 | \f | |
5183 | #if ARCH_SIZE==64 | |
5184 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR) | |
5185 | #endif | |
5186 | #if ARCH_SIZE==32 | |
5187 | #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR) | |
5188 | #endif | |
5189 | ||
5190 | /* Fill in the address for a pointer generated in alinker section. */ | |
5191 | ||
5192 | bfd_vma | |
5193 | elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc) | |
5194 | bfd *output_bfd; | |
5195 | bfd *input_bfd; | |
5196 | struct bfd_link_info *info; | |
5197 | elf_linker_section_t *lsect; | |
5198 | struct elf_link_hash_entry *h; | |
5199 | bfd_vma relocation; | |
5200 | const Elf_Internal_Rela *rel; | |
5201 | int relative_reloc; | |
5202 | { | |
5203 | elf_linker_section_pointers_t *linker_section_ptr; | |
5204 | ||
5205 | BFD_ASSERT (lsect != NULL); | |
5206 | ||
3b3753b8 MM |
5207 | if (h != NULL) /* global symbol */ |
5208 | { | |
5209 | linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer, | |
5210 | rel->r_addend, | |
5211 | lsect->which); | |
5212 | ||
5213 | BFD_ASSERT (linker_section_ptr != NULL); | |
5214 | ||
5215 | if (! elf_hash_table (info)->dynamic_sections_created | |
5216 | || (info->shared | |
5217 | && info->symbolic | |
5218 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
5219 | { | |
5220 | /* This is actually a static link, or it is a | |
5221 | -Bsymbolic link and the symbol is defined | |
5222 | locally. We must initialize this entry in the | |
5223 | global section. | |
5224 | ||
5225 | When doing a dynamic link, we create a .rela.<xxx> | |
5226 | relocation entry to initialize the value. This | |
5227 | is done in the finish_dynamic_symbol routine. */ | |
5228 | if (!linker_section_ptr->written_address_p) | |
5229 | { | |
5230 | linker_section_ptr->written_address_p = true; | |
5231 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, | |
5232 | lsect->section->contents + linker_section_ptr->offset); | |
5233 | } | |
5234 | } | |
5235 | } | |
5236 | else /* local symbol */ | |
5237 | { | |
5238 | unsigned long r_symndx = ELF_R_SYM (rel->r_info); | |
5239 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL); | |
5240 | BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL); | |
5241 | linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx], | |
5242 | rel->r_addend, | |
5243 | lsect->which); | |
5244 | ||
5245 | BFD_ASSERT (linker_section_ptr != NULL); | |
5246 | ||
5247 | /* Write out pointer if it hasn't been rewritten out before */ | |
5248 | if (!linker_section_ptr->written_address_p) | |
5249 | { | |
5250 | linker_section_ptr->written_address_p = true; | |
5251 | bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend, | |
5252 | lsect->section->contents + linker_section_ptr->offset); | |
5253 | ||
5254 | if (info->shared) | |
5255 | { | |
5256 | asection *srel = lsect->rel_section; | |
5257 | Elf_Internal_Rela outrel; | |
5258 | ||
5259 | /* We need to generate a relative reloc for the dynamic linker. */ | |
5260 | if (!srel) | |
5261 | lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj, | |
5262 | lsect->rel_name); | |
5263 | ||
5264 | BFD_ASSERT (srel != NULL); | |
5265 | ||
5266 | outrel.r_offset = (lsect->section->output_section->vma | |
5267 | + lsect->section->output_offset | |
5268 | + linker_section_ptr->offset); | |
5269 | outrel.r_info = ELF_R_INFO (0, relative_reloc); | |
5270 | outrel.r_addend = 0; | |
5271 | elf_swap_reloca_out (output_bfd, &outrel, | |
5a5bac64 | 5272 | (((Elf_External_Rela *) |
3b3753b8 MM |
5273 | lsect->section->contents) |
5274 | + lsect->section->reloc_count)); | |
5275 | ++lsect->section->reloc_count; | |
5276 | } | |
5277 | } | |
5278 | } | |
5279 | ||
5280 | relocation = (lsect->section->output_offset | |
5281 | + linker_section_ptr->offset | |
5282 | - lsect->hole_offset | |
5283 | - lsect->sym_offset); | |
5284 | ||
5285 | #ifdef DEBUG | |
5286 | fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n", | |
5287 | lsect->name, (long)relocation, (long)relocation); | |
5288 | #endif | |
5289 | ||
5290 | /* Subtract out the addend, because it will get added back in by the normal | |
5291 | processing. */ | |
5292 | return relocation - linker_section_ptr->addend; | |
5293 | } |