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