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