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