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