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