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