elflink.c revision 1.13.12.3 1 1.1 skrll /* ELF linking support for BFD.
2 1.13.12.2 pgoyette Copyright (C) 1995-2018 Free Software Foundation, Inc.
3 1.1 skrll
4 1.1 skrll This file is part of BFD, the Binary File Descriptor library.
5 1.1 skrll
6 1.1 skrll This program is free software; you can redistribute it and/or modify
7 1.1 skrll it under the terms of the GNU General Public License as published by
8 1.1 skrll the Free Software Foundation; either version 3 of the License, or
9 1.1 skrll (at your option) any later version.
10 1.1 skrll
11 1.1 skrll This program is distributed in the hope that it will be useful,
12 1.1 skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 skrll GNU General Public License for more details.
15 1.1 skrll
16 1.1 skrll You should have received a copy of the GNU General Public License
17 1.1 skrll along with this program; if not, write to the Free Software
18 1.1 skrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 1.1 skrll MA 02110-1301, USA. */
20 1.1 skrll
21 1.1 skrll #include "sysdep.h"
22 1.1 skrll #include "bfd.h"
23 1.9 christos #include "bfd_stdint.h"
24 1.1 skrll #include "bfdlink.h"
25 1.1 skrll #include "libbfd.h"
26 1.1 skrll #define ARCH_SIZE 0
27 1.1 skrll #include "elf-bfd.h"
28 1.1 skrll #include "safe-ctype.h"
29 1.1 skrll #include "libiberty.h"
30 1.1 skrll #include "objalloc.h"
31 1.13 christos #if BFD_SUPPORTS_PLUGINS
32 1.13.12.2 pgoyette #include "plugin-api.h"
33 1.13 christos #include "plugin.h"
34 1.13 christos #endif
35 1.1 skrll
36 1.4 christos /* This struct is used to pass information to routines called via
37 1.4 christos elf_link_hash_traverse which must return failure. */
38 1.4 christos
39 1.4 christos struct elf_info_failed
40 1.4 christos {
41 1.4 christos struct bfd_link_info *info;
42 1.4 christos bfd_boolean failed;
43 1.4 christos };
44 1.4 christos
45 1.4 christos /* This structure is used to pass information to
46 1.4 christos _bfd_elf_link_find_version_dependencies. */
47 1.4 christos
48 1.4 christos struct elf_find_verdep_info
49 1.4 christos {
50 1.4 christos /* General link information. */
51 1.4 christos struct bfd_link_info *info;
52 1.4 christos /* The number of dependencies. */
53 1.4 christos unsigned int vers;
54 1.4 christos /* Whether we had a failure. */
55 1.4 christos bfd_boolean failed;
56 1.4 christos };
57 1.4 christos
58 1.4 christos static bfd_boolean _bfd_elf_fix_symbol_flags
59 1.4 christos (struct elf_link_hash_entry *, struct elf_info_failed *);
60 1.4 christos
61 1.9 christos asection *
62 1.9 christos _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 1.9 christos unsigned long r_symndx,
64 1.9 christos bfd_boolean discard)
65 1.9 christos {
66 1.9 christos if (r_symndx >= cookie->locsymcount
67 1.9 christos || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 1.9 christos {
69 1.9 christos struct elf_link_hash_entry *h;
70 1.9 christos
71 1.9 christos h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72 1.9 christos
73 1.9 christos while (h->root.type == bfd_link_hash_indirect
74 1.9 christos || h->root.type == bfd_link_hash_warning)
75 1.9 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
76 1.9 christos
77 1.9 christos if ((h->root.type == bfd_link_hash_defined
78 1.9 christos || h->root.type == bfd_link_hash_defweak)
79 1.9 christos && discarded_section (h->root.u.def.section))
80 1.13.12.2 pgoyette return h->root.u.def.section;
81 1.9 christos else
82 1.9 christos return NULL;
83 1.9 christos }
84 1.9 christos else
85 1.9 christos {
86 1.9 christos /* It's not a relocation against a global symbol,
87 1.9 christos but it could be a relocation against a local
88 1.9 christos symbol for a discarded section. */
89 1.9 christos asection *isec;
90 1.9 christos Elf_Internal_Sym *isym;
91 1.9 christos
92 1.9 christos /* Need to: get the symbol; get the section. */
93 1.9 christos isym = &cookie->locsyms[r_symndx];
94 1.9 christos isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 1.9 christos if (isec != NULL
96 1.9 christos && discard ? discarded_section (isec) : 1)
97 1.9 christos return isec;
98 1.9 christos }
99 1.9 christos return NULL;
100 1.9 christos }
101 1.9 christos
102 1.1 skrll /* Define a symbol in a dynamic linkage section. */
103 1.1 skrll
104 1.1 skrll struct elf_link_hash_entry *
105 1.1 skrll _bfd_elf_define_linkage_sym (bfd *abfd,
106 1.1 skrll struct bfd_link_info *info,
107 1.1 skrll asection *sec,
108 1.1 skrll const char *name)
109 1.1 skrll {
110 1.1 skrll struct elf_link_hash_entry *h;
111 1.1 skrll struct bfd_link_hash_entry *bh;
112 1.1 skrll const struct elf_backend_data *bed;
113 1.1 skrll
114 1.1 skrll h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 1.1 skrll if (h != NULL)
116 1.1 skrll {
117 1.1 skrll /* Zap symbol defined in an as-needed lib that wasn't linked.
118 1.1 skrll This is a symptom of a larger problem: Absolute symbols
119 1.1 skrll defined in shared libraries can't be overridden, because we
120 1.1 skrll lose the link to the bfd which is via the symbol section. */
121 1.1 skrll h->root.type = bfd_link_hash_new;
122 1.13.12.2 pgoyette bh = &h->root;
123 1.1 skrll }
124 1.13.12.2 pgoyette else
125 1.13.12.2 pgoyette bh = NULL;
126 1.1 skrll
127 1.9 christos bed = get_elf_backend_data (abfd);
128 1.1 skrll if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 1.9 christos sec, 0, NULL, FALSE, bed->collect,
130 1.1 skrll &bh))
131 1.1 skrll return NULL;
132 1.1 skrll h = (struct elf_link_hash_entry *) bh;
133 1.13.12.2 pgoyette BFD_ASSERT (h != NULL);
134 1.1 skrll h->def_regular = 1;
135 1.4 christos h->non_elf = 0;
136 1.9 christos h->root.linker_def = 1;
137 1.1 skrll h->type = STT_OBJECT;
138 1.9 christos if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 1.9 christos h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140 1.1 skrll
141 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 1.1 skrll return h;
143 1.1 skrll }
144 1.1 skrll
145 1.1 skrll bfd_boolean
146 1.1 skrll _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
147 1.1 skrll {
148 1.1 skrll flagword flags;
149 1.1 skrll asection *s;
150 1.1 skrll struct elf_link_hash_entry *h;
151 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 1.4 christos struct elf_link_hash_table *htab = elf_hash_table (info);
153 1.1 skrll
154 1.1 skrll /* This function may be called more than once. */
155 1.13.12.2 pgoyette if (htab->sgot != NULL)
156 1.1 skrll return TRUE;
157 1.1 skrll
158 1.4 christos flags = bed->dynamic_sec_flags;
159 1.1 skrll
160 1.7 christos s = bfd_make_section_anyway_with_flags (abfd,
161 1.7 christos (bed->rela_plts_and_copies_p
162 1.7 christos ? ".rela.got" : ".rel.got"),
163 1.7 christos (bed->dynamic_sec_flags
164 1.7 christos | SEC_READONLY));
165 1.4 christos if (s == NULL
166 1.4 christos || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 1.4 christos return FALSE;
168 1.4 christos htab->srelgot = s;
169 1.1 skrll
170 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 1.1 skrll if (s == NULL
172 1.4 christos || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 1.1 skrll return FALSE;
174 1.4 christos htab->sgot = s;
175 1.1 skrll
176 1.1 skrll if (bed->want_got_plt)
177 1.1 skrll {
178 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 1.1 skrll if (s == NULL
180 1.4 christos || !bfd_set_section_alignment (abfd, s,
181 1.4 christos bed->s->log_file_align))
182 1.1 skrll return FALSE;
183 1.4 christos htab->sgotplt = s;
184 1.1 skrll }
185 1.1 skrll
186 1.4 christos /* The first bit of the global offset table is the header. */
187 1.4 christos s->size += bed->got_header_size;
188 1.4 christos
189 1.1 skrll if (bed->want_got_sym)
190 1.1 skrll {
191 1.1 skrll /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 1.1 skrll (or .got.plt) section. We don't do this in the linker script
193 1.1 skrll because we don't want to define the symbol if we are not creating
194 1.1 skrll a global offset table. */
195 1.4 christos h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 1.4 christos "_GLOBAL_OFFSET_TABLE_");
197 1.1 skrll elf_hash_table (info)->hgot = h;
198 1.1 skrll if (h == NULL)
199 1.1 skrll return FALSE;
200 1.1 skrll }
201 1.1 skrll
202 1.1 skrll return TRUE;
203 1.1 skrll }
204 1.1 skrll
205 1.1 skrll /* Create a strtab to hold the dynamic symbol names. */
207 1.1 skrll static bfd_boolean
208 1.1 skrll _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 1.1 skrll {
210 1.1 skrll struct elf_link_hash_table *hash_table;
211 1.1 skrll
212 1.1 skrll hash_table = elf_hash_table (info);
213 1.13 christos if (hash_table->dynobj == NULL)
214 1.13 christos {
215 1.13 christos /* We may not set dynobj, an input file holding linker created
216 1.13 christos dynamic sections to abfd, which may be a dynamic object with
217 1.13 christos its own dynamic sections. We need to find a normal input file
218 1.13 christos to hold linker created sections if possible. */
219 1.13 christos if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
220 1.13 christos {
221 1.13.12.2 pgoyette bfd *ibfd;
222 1.13 christos asection *s;
223 1.13 christos for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 1.13.12.2 pgoyette if ((ibfd->flags
225 1.13.12.2 pgoyette & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
226 1.13.12.2 pgoyette && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
227 1.13.12.2 pgoyette && !((s = ibfd->sections) != NULL
228 1.13 christos && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
229 1.13 christos {
230 1.13 christos abfd = ibfd;
231 1.13 christos break;
232 1.13 christos }
233 1.13 christos }
234 1.13 christos hash_table->dynobj = abfd;
235 1.1 skrll }
236 1.1 skrll
237 1.1 skrll if (hash_table->dynstr == NULL)
238 1.1 skrll {
239 1.1 skrll hash_table->dynstr = _bfd_elf_strtab_init ();
240 1.1 skrll if (hash_table->dynstr == NULL)
241 1.1 skrll return FALSE;
242 1.1 skrll }
243 1.1 skrll return TRUE;
244 1.1 skrll }
245 1.1 skrll
246 1.1 skrll /* Create some sections which will be filled in with dynamic linking
247 1.1 skrll information. ABFD is an input file which requires dynamic sections
248 1.1 skrll to be created. The dynamic sections take up virtual memory space
249 1.1 skrll when the final executable is run, so we need to create them before
250 1.1 skrll addresses are assigned to the output sections. We work out the
251 1.1 skrll actual contents and size of these sections later. */
252 1.1 skrll
253 1.1 skrll bfd_boolean
254 1.1 skrll _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
255 1.1 skrll {
256 1.4 christos flagword flags;
257 1.1 skrll asection *s;
258 1.9 christos const struct elf_backend_data *bed;
259 1.1 skrll struct elf_link_hash_entry *h;
260 1.1 skrll
261 1.1 skrll if (! is_elf_hash_table (info->hash))
262 1.1 skrll return FALSE;
263 1.1 skrll
264 1.1 skrll if (elf_hash_table (info)->dynamic_sections_created)
265 1.1 skrll return TRUE;
266 1.1 skrll
267 1.1 skrll if (!_bfd_elf_link_create_dynstrtab (abfd, info))
268 1.1 skrll return FALSE;
269 1.1 skrll
270 1.1 skrll abfd = elf_hash_table (info)->dynobj;
271 1.1 skrll bed = get_elf_backend_data (abfd);
272 1.1 skrll
273 1.1 skrll flags = bed->dynamic_sec_flags;
274 1.1 skrll
275 1.1 skrll /* A dynamically linked executable has a .interp section, but a
276 1.9 christos shared library does not. */
277 1.1 skrll if (bfd_link_executable (info) && !info->nointerp)
278 1.7 christos {
279 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".interp",
280 1.1 skrll flags | SEC_READONLY);
281 1.1 skrll if (s == NULL)
282 1.1 skrll return FALSE;
283 1.1 skrll }
284 1.1 skrll
285 1.1 skrll /* Create sections to hold version informations. These are removed
286 1.7 christos if they are not needed. */
287 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
288 1.1 skrll flags | SEC_READONLY);
289 1.1 skrll if (s == NULL
290 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
291 1.1 skrll return FALSE;
292 1.7 christos
293 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
294 1.1 skrll flags | SEC_READONLY);
295 1.1 skrll if (s == NULL
296 1.1 skrll || ! bfd_set_section_alignment (abfd, s, 1))
297 1.1 skrll return FALSE;
298 1.7 christos
299 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
300 1.1 skrll flags | SEC_READONLY);
301 1.1 skrll if (s == NULL
302 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
303 1.1 skrll return FALSE;
304 1.7 christos
305 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
306 1.1 skrll flags | SEC_READONLY);
307 1.1 skrll if (s == NULL
308 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 1.9 christos return FALSE;
310 1.1 skrll elf_hash_table (info)->dynsym = s;
311 1.7 christos
312 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
313 1.1 skrll flags | SEC_READONLY);
314 1.1 skrll if (s == NULL)
315 1.1 skrll return FALSE;
316 1.7 christos
317 1.1 skrll s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 1.1 skrll if (s == NULL
319 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
320 1.1 skrll return FALSE;
321 1.1 skrll
322 1.1 skrll /* The special symbol _DYNAMIC is always set to the start of the
323 1.1 skrll .dynamic section. We could set _DYNAMIC in a linker script, but we
324 1.1 skrll only want to define it if we are, in fact, creating a .dynamic
325 1.1 skrll section. We don't want to define it if there is no .dynamic
326 1.1 skrll section, since on some ELF platforms the start up code examines it
327 1.9 christos to decide how to initialize the process. */
328 1.9 christos h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
329 1.9 christos elf_hash_table (info)->hdynamic = h;
330 1.1 skrll if (h == NULL)
331 1.1 skrll return FALSE;
332 1.1 skrll
333 1.1 skrll if (info->emit_hash)
334 1.7 christos {
335 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".hash",
336 1.1 skrll flags | SEC_READONLY);
337 1.1 skrll if (s == NULL
338 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 1.1 skrll return FALSE;
340 1.1 skrll elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
341 1.1 skrll }
342 1.1 skrll
343 1.1 skrll if (info->emit_gnu_hash)
344 1.7 christos {
345 1.7 christos s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
346 1.1 skrll flags | SEC_READONLY);
347 1.1 skrll if (s == NULL
348 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 1.1 skrll return FALSE;
350 1.1 skrll /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
351 1.1 skrll 4 32-bit words followed by variable count of 64-bit words, then
352 1.1 skrll variable count of 32-bit words. */
353 1.1 skrll if (bed->s->arch_size == 64)
354 1.1 skrll elf_section_data (s)->this_hdr.sh_entsize = 0;
355 1.1 skrll else
356 1.1 skrll elf_section_data (s)->this_hdr.sh_entsize = 4;
357 1.1 skrll }
358 1.1 skrll
359 1.1 skrll /* Let the backend create the rest of the sections. This lets the
360 1.1 skrll backend set the right flags. The backend will normally create
361 1.7 christos the .got and .plt sections. */
362 1.7 christos if (bed->elf_backend_create_dynamic_sections == NULL
363 1.1 skrll || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
364 1.1 skrll return FALSE;
365 1.1 skrll
366 1.1 skrll elf_hash_table (info)->dynamic_sections_created = TRUE;
367 1.1 skrll
368 1.1 skrll return TRUE;
369 1.1 skrll }
370 1.1 skrll
371 1.1 skrll /* Create dynamic sections when linking against a dynamic object. */
372 1.1 skrll
373 1.1 skrll bfd_boolean
374 1.1 skrll _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 1.1 skrll {
376 1.1 skrll flagword flags, pltflags;
377 1.1 skrll struct elf_link_hash_entry *h;
378 1.1 skrll asection *s;
379 1.4 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
380 1.1 skrll struct elf_link_hash_table *htab = elf_hash_table (info);
381 1.1 skrll
382 1.1 skrll /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
383 1.1 skrll .rel[a].bss sections. */
384 1.1 skrll flags = bed->dynamic_sec_flags;
385 1.1 skrll
386 1.1 skrll pltflags = flags;
387 1.1 skrll if (bed->plt_not_loaded)
388 1.1 skrll /* We do not clear SEC_ALLOC here because we still want the OS to
389 1.1 skrll allocate space for the section; it's just that there's nothing
390 1.1 skrll to read in from the object file. */
391 1.1 skrll pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 1.1 skrll else
393 1.1 skrll pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
394 1.1 skrll if (bed->plt_readonly)
395 1.1 skrll pltflags |= SEC_READONLY;
396 1.7 christos
397 1.1 skrll s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 1.1 skrll if (s == NULL
399 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
400 1.4 christos return FALSE;
401 1.1 skrll htab->splt = s;
402 1.1 skrll
403 1.1 skrll /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 1.1 skrll .plt section. */
405 1.1 skrll if (bed->want_plt_sym)
406 1.1 skrll {
407 1.1 skrll h = _bfd_elf_define_linkage_sym (abfd, info, s,
408 1.1 skrll "_PROCEDURE_LINKAGE_TABLE_");
409 1.1 skrll elf_hash_table (info)->hplt = h;
410 1.1 skrll if (h == NULL)
411 1.1 skrll return FALSE;
412 1.1 skrll }
413 1.7 christos
414 1.7 christos s = bfd_make_section_anyway_with_flags (abfd,
415 1.7 christos (bed->rela_plts_and_copies_p
416 1.7 christos ? ".rela.plt" : ".rel.plt"),
417 1.1 skrll flags | SEC_READONLY);
418 1.1 skrll if (s == NULL
419 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
420 1.4 christos return FALSE;
421 1.1 skrll htab->srelplt = s;
422 1.1 skrll
423 1.1 skrll if (! _bfd_elf_create_got_section (abfd, info))
424 1.1 skrll return FALSE;
425 1.1 skrll
426 1.1 skrll if (bed->want_dynbss)
427 1.1 skrll {
428 1.1 skrll /* The .dynbss section is a place to put symbols which are defined
429 1.1 skrll by dynamic objects, are referenced by regular objects, and are
430 1.1 skrll not functions. We must allocate space for them in the process
431 1.1 skrll image and use a R_*_COPY reloc to tell the dynamic linker to
432 1.1 skrll initialize them at run time. The linker script puts the .dynbss
433 1.7 christos section into the .bss section of the final image. */
434 1.13.12.2 pgoyette s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
435 1.1 skrll SEC_ALLOC | SEC_LINKER_CREATED);
436 1.1 skrll if (s == NULL)
437 1.13.12.2 pgoyette return FALSE;
438 1.13.12.2 pgoyette htab->sdynbss = s;
439 1.13.12.2 pgoyette
440 1.13.12.2 pgoyette if (bed->want_dynrelro)
441 1.13.12.2 pgoyette {
442 1.13.12.2 pgoyette /* Similarly, but for symbols that were originally in read-only
443 1.13.12.2 pgoyette sections. This section doesn't really need to have contents,
444 1.13.12.2 pgoyette but make it like other .data.rel.ro sections. */
445 1.13.12.2 pgoyette s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
446 1.13.12.2 pgoyette flags);
447 1.13.12.2 pgoyette if (s == NULL)
448 1.13.12.2 pgoyette return FALSE;
449 1.13.12.2 pgoyette htab->sdynrelro = s;
450 1.1 skrll }
451 1.1 skrll
452 1.1 skrll /* The .rel[a].bss section holds copy relocs. This section is not
453 1.1 skrll normally needed. We need to create it here, though, so that the
454 1.1 skrll linker will map it to an output section. We can't just create it
455 1.1 skrll only if we need it, because we will not know whether we need it
456 1.1 skrll until we have seen all the input files, and the first time the
457 1.1 skrll main linker code calls BFD after examining all the input files
458 1.1 skrll (size_dynamic_sections) the input sections have already been
459 1.1 skrll mapped to the output sections. If the section turns out not to
460 1.1 skrll be needed, we can discard it later. We will never need this
461 1.1 skrll section when generating a shared object, since they do not use
462 1.13.12.2 pgoyette copy relocs. */
463 1.1 skrll if (bfd_link_executable (info))
464 1.7 christos {
465 1.7 christos s = bfd_make_section_anyway_with_flags (abfd,
466 1.7 christos (bed->rela_plts_and_copies_p
467 1.7 christos ? ".rela.bss" : ".rel.bss"),
468 1.1 skrll flags | SEC_READONLY);
469 1.1 skrll if (s == NULL
470 1.1 skrll || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
471 1.13.12.2 pgoyette return FALSE;
472 1.13.12.2 pgoyette htab->srelbss = s;
473 1.13.12.2 pgoyette
474 1.13.12.2 pgoyette if (bed->want_dynrelro)
475 1.13.12.2 pgoyette {
476 1.13.12.2 pgoyette s = (bfd_make_section_anyway_with_flags
477 1.13.12.2 pgoyette (abfd, (bed->rela_plts_and_copies_p
478 1.13.12.2 pgoyette ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
479 1.13.12.2 pgoyette flags | SEC_READONLY));
480 1.13.12.2 pgoyette if (s == NULL
481 1.13.12.2 pgoyette || ! bfd_set_section_alignment (abfd, s,
482 1.13.12.2 pgoyette bed->s->log_file_align))
483 1.13.12.2 pgoyette return FALSE;
484 1.13.12.2 pgoyette htab->sreldynrelro = s;
485 1.1 skrll }
486 1.1 skrll }
487 1.1 skrll }
488 1.1 skrll
489 1.1 skrll return TRUE;
490 1.1 skrll }
491 1.1 skrll
492 1.1 skrll /* Record a new dynamic symbol. We record the dynamic symbols as we
494 1.1 skrll read the input files, since we need to have a list of all of them
495 1.1 skrll before we can determine the final sizes of the output sections.
496 1.1 skrll Note that we may actually call this function even though we are not
497 1.1 skrll going to output any dynamic symbols; in some cases we know that a
498 1.1 skrll symbol should be in the dynamic symbol table, but only if there is
499 1.1 skrll one. */
500 1.1 skrll
501 1.1 skrll bfd_boolean
502 1.1 skrll bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
503 1.1 skrll struct elf_link_hash_entry *h)
504 1.1 skrll {
505 1.1 skrll if (h->dynindx == -1)
506 1.1 skrll {
507 1.1 skrll struct elf_strtab_hash *dynstr;
508 1.13 christos char *p;
509 1.1 skrll const char *name;
510 1.1 skrll size_t indx;
511 1.1 skrll
512 1.1 skrll /* XXX: The ABI draft says the linker must turn hidden and
513 1.1 skrll internal symbols into STB_LOCAL symbols when producing the
514 1.1 skrll DSO. However, if ld.so honors st_other in the dynamic table,
515 1.1 skrll this would not be necessary. */
516 1.1 skrll switch (ELF_ST_VISIBILITY (h->other))
517 1.1 skrll {
518 1.1 skrll case STV_INTERNAL:
519 1.1 skrll case STV_HIDDEN:
520 1.1 skrll if (h->root.type != bfd_link_hash_undefined
521 1.1 skrll && h->root.type != bfd_link_hash_undefweak)
522 1.1 skrll {
523 1.1 skrll h->forced_local = 1;
524 1.1 skrll if (!elf_hash_table (info)->is_relocatable_executable)
525 1.1 skrll return TRUE;
526 1.1 skrll }
527 1.1 skrll
528 1.1 skrll default:
529 1.1 skrll break;
530 1.1 skrll }
531 1.1 skrll
532 1.1 skrll h->dynindx = elf_hash_table (info)->dynsymcount;
533 1.1 skrll ++elf_hash_table (info)->dynsymcount;
534 1.1 skrll
535 1.1 skrll dynstr = elf_hash_table (info)->dynstr;
536 1.1 skrll if (dynstr == NULL)
537 1.1 skrll {
538 1.1 skrll /* Create a strtab to hold the dynamic symbol names. */
539 1.1 skrll elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
540 1.1 skrll if (dynstr == NULL)
541 1.1 skrll return FALSE;
542 1.1 skrll }
543 1.1 skrll
544 1.1 skrll /* We don't put any version information in the dynamic string
545 1.1 skrll table. */
546 1.1 skrll name = h->root.root.string;
547 1.1 skrll p = strchr (name, ELF_VER_CHR);
548 1.1 skrll if (p != NULL)
549 1.1 skrll /* We know that the p points into writable memory. In fact,
550 1.1 skrll there are only a few symbols that have read-only names, being
551 1.1 skrll those like _GLOBAL_OFFSET_TABLE_ that are created specially
552 1.1 skrll by the backends. Most symbols will have names pointing into
553 1.1 skrll an ELF string table read from a file, or to objalloc memory. */
554 1.1 skrll *p = 0;
555 1.1 skrll
556 1.1 skrll indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
557 1.1 skrll
558 1.1 skrll if (p != NULL)
559 1.13 christos *p = ELF_VER_CHR;
560 1.1 skrll
561 1.1 skrll if (indx == (size_t) -1)
562 1.1 skrll return FALSE;
563 1.1 skrll h->dynstr_index = indx;
564 1.1 skrll }
565 1.1 skrll
566 1.1 skrll return TRUE;
567 1.1 skrll }
568 1.1 skrll
569 1.4 christos /* Mark a symbol dynamic. */
571 1.1 skrll
572 1.1 skrll static void
573 1.1 skrll bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
574 1.1 skrll struct elf_link_hash_entry *h,
575 1.1 skrll Elf_Internal_Sym *sym)
576 1.1 skrll {
577 1.9 christos struct bfd_elf_dynamic_list *d = info->dynamic_list;
578 1.1 skrll
579 1.1 skrll /* It may be called more than once on the same H. */
580 1.1 skrll if(h->dynamic || bfd_link_relocatable (info))
581 1.1 skrll return;
582 1.13 christos
583 1.1 skrll if ((info->dynamic_data
584 1.13 christos && (h->type == STT_OBJECT
585 1.13 christos || h->type == STT_COMMON
586 1.1 skrll || (sym != NULL
587 1.13.12.2 pgoyette && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
588 1.1 skrll || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
589 1.1 skrll || (d != NULL
590 1.1 skrll && h->non_elf
591 1.1 skrll && (*d->match) (&d->head, NULL, h->root.root.string)))
592 1.1 skrll h->dynamic = 1;
593 1.1 skrll }
594 1.1 skrll
595 1.1 skrll /* Record an assignment to a symbol made by a linker script. We need
596 1.1 skrll this in case some dynamic object refers to this symbol. */
597 1.1 skrll
598 1.1 skrll bfd_boolean
599 1.1 skrll bfd_elf_record_link_assignment (bfd *output_bfd,
600 1.1 skrll struct bfd_link_info *info,
601 1.1 skrll const char *name,
602 1.1 skrll bfd_boolean provide,
603 1.1 skrll bfd_boolean hidden)
604 1.1 skrll {
605 1.1 skrll struct elf_link_hash_entry *h, *hv;
606 1.1 skrll struct elf_link_hash_table *htab;
607 1.1 skrll const struct elf_backend_data *bed;
608 1.1 skrll
609 1.1 skrll if (!is_elf_hash_table (info->hash))
610 1.1 skrll return TRUE;
611 1.1 skrll
612 1.1 skrll htab = elf_hash_table (info);
613 1.1 skrll h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 1.13.12.2 pgoyette if (h == NULL)
615 1.13.12.2 pgoyette return provide;
616 1.13.12.2 pgoyette
617 1.12 christos if (h->root.type == bfd_link_hash_warning)
618 1.12 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
619 1.12 christos
620 1.12 christos if (h->versioned == unknown)
621 1.12 christos {
622 1.12 christos /* Set versioned if symbol version is unknown. */
623 1.12 christos char *version = strrchr (name, ELF_VER_CHR);
624 1.12 christos if (version)
625 1.12 christos {
626 1.12 christos if (version > name && version[-1] != ELF_VER_CHR)
627 1.12 christos h->versioned = versioned_hidden;
628 1.12 christos else
629 1.12 christos h->versioned = versioned;
630 1.13.12.2 pgoyette }
631 1.13.12.2 pgoyette }
632 1.13.12.2 pgoyette
633 1.13.12.2 pgoyette /* Symbols defined in a linker script but not referenced anywhere
634 1.13.12.2 pgoyette else will have non_elf set. */
635 1.13.12.2 pgoyette if (h->non_elf)
636 1.13.12.2 pgoyette {
637 1.13.12.2 pgoyette bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 1.1 skrll h->non_elf = 0;
639 1.1 skrll }
640 1.1 skrll
641 1.1 skrll switch (h->root.type)
642 1.1 skrll {
643 1.1 skrll case bfd_link_hash_defined:
644 1.1 skrll case bfd_link_hash_defweak:
645 1.1 skrll case bfd_link_hash_common:
646 1.1 skrll break;
647 1.1 skrll case bfd_link_hash_undefweak:
648 1.1 skrll case bfd_link_hash_undefined:
649 1.1 skrll /* Since we're defining the symbol, don't let it seem to have not
650 1.1 skrll been defined. record_dynamic_symbol and size_dynamic_sections
651 1.1 skrll may depend on this. */
652 1.1 skrll h->root.type = bfd_link_hash_new;
653 1.1 skrll if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 1.1 skrll bfd_link_repair_undef_list (&htab->root);
655 1.1 skrll break;
656 1.1 skrll case bfd_link_hash_new:
657 1.1 skrll break;
658 1.1 skrll case bfd_link_hash_indirect:
659 1.1 skrll /* We had a versioned symbol in a dynamic library. We make the
660 1.1 skrll the versioned symbol point to this one. */
661 1.1 skrll bed = get_elf_backend_data (output_bfd);
662 1.1 skrll hv = h;
663 1.1 skrll while (hv->root.type == bfd_link_hash_indirect
664 1.1 skrll || hv->root.type == bfd_link_hash_warning)
665 1.1 skrll hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 1.1 skrll /* We don't need to update h->root.u since linker will set them
667 1.1 skrll later. */
668 1.1 skrll h->root.type = bfd_link_hash_undefined;
669 1.1 skrll hv->root.type = bfd_link_hash_indirect;
670 1.13.12.2 pgoyette hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 1.13.12.2 pgoyette (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 1.13.12.2 pgoyette break;
673 1.1 skrll default:
674 1.1 skrll BFD_FAIL ();
675 1.1 skrll return FALSE;
676 1.1 skrll }
677 1.1 skrll
678 1.1 skrll /* If this symbol is being provided by the linker script, and it is
679 1.1 skrll currently defined by a dynamic object, but not by a regular
680 1.1 skrll object, then mark it as undefined so that the generic linker will
681 1.1 skrll force the correct value. */
682 1.1 skrll if (provide
683 1.1 skrll && h->def_dynamic
684 1.1 skrll && !h->def_regular)
685 1.1 skrll h->root.type = bfd_link_hash_undefined;
686 1.1 skrll
687 1.1 skrll /* If this symbol is not being provided by the linker script, and it is
688 1.1 skrll currently defined by a dynamic object, but not by a regular object,
689 1.1 skrll then clear out any version information because the symbol will not be
690 1.1 skrll associated with the dynamic object any more. */
691 1.1 skrll if (!provide
692 1.1 skrll && h->def_dynamic
693 1.13.12.2 pgoyette && !h->def_regular)
694 1.13.12.2 pgoyette h->verinfo.verdef = NULL;
695 1.13.12.2 pgoyette
696 1.6 matt /* Make sure this symbol is not garbage collected. */
697 1.1 skrll h->mark = 1;
698 1.7 christos
699 1.1 skrll h->def_regular = 1;
700 1.4 christos
701 1.9 christos if (hidden)
702 1.9 christos {
703 1.1 skrll bed = get_elf_backend_data (output_bfd);
704 1.1 skrll if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 1.1 skrll h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
706 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 1.1 skrll }
708 1.9 christos
709 1.1 skrll /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 1.1 skrll and executables. */
711 1.1 skrll if (!bfd_link_relocatable (info)
712 1.1 skrll && h->dynindx != -1
713 1.1 skrll && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 1.1 skrll || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 1.1 skrll h->forced_local = 1;
716 1.13 christos
717 1.13 christos if ((h->def_dynamic
718 1.1 skrll || h->ref_dynamic
719 1.1 skrll || bfd_link_dll (info)
720 1.1 skrll || elf_hash_table (info)->is_relocatable_executable)
721 1.1 skrll && h->dynindx == -1)
722 1.1 skrll {
723 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (info, h))
724 1.1 skrll return FALSE;
725 1.1 skrll
726 1.13.12.2 pgoyette /* If this is a weak defined symbol, and we know a corresponding
727 1.1 skrll real symbol from the same dynamic object, make sure the real
728 1.13.12.2 pgoyette symbol is also made into a dynamic symbol. */
729 1.13.12.2 pgoyette if (h->is_weakalias)
730 1.13.12.2 pgoyette {
731 1.13.12.2 pgoyette struct elf_link_hash_entry *def = weakdef (h);
732 1.1 skrll
733 1.1 skrll if (def->dynindx == -1
734 1.1 skrll && !bfd_elf_link_record_dynamic_symbol (info, def))
735 1.1 skrll return FALSE;
736 1.1 skrll }
737 1.1 skrll }
738 1.1 skrll
739 1.1 skrll return TRUE;
740 1.1 skrll }
741 1.1 skrll
742 1.1 skrll /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
743 1.1 skrll success, and 2 on a failure caused by attempting to record a symbol
744 1.1 skrll in a discarded section, eg. a discarded link-once section symbol. */
745 1.1 skrll
746 1.1 skrll int
747 1.1 skrll bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
748 1.1 skrll bfd *input_bfd,
749 1.1 skrll long input_indx)
750 1.1 skrll {
751 1.1 skrll bfd_size_type amt;
752 1.13 christos struct elf_link_local_dynamic_entry *entry;
753 1.1 skrll struct elf_link_hash_table *eht;
754 1.1 skrll struct elf_strtab_hash *dynstr;
755 1.1 skrll size_t dynstr_index;
756 1.1 skrll char *name;
757 1.1 skrll Elf_External_Sym_Shndx eshndx;
758 1.1 skrll char esym[sizeof (Elf64_External_Sym)];
759 1.1 skrll
760 1.1 skrll if (! is_elf_hash_table (info->hash))
761 1.1 skrll return 0;
762 1.1 skrll
763 1.1 skrll /* See if the entry exists already. */
764 1.1 skrll for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
765 1.1 skrll if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
766 1.4 christos return 1;
767 1.1 skrll
768 1.1 skrll amt = sizeof (*entry);
769 1.1 skrll entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
770 1.1 skrll if (entry == NULL)
771 1.1 skrll return 0;
772 1.1 skrll
773 1.1 skrll /* Go find the symbol, so that we can find it's name. */
774 1.1 skrll if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
775 1.1 skrll 1, input_indx, &entry->isym, esym, &eshndx))
776 1.1 skrll {
777 1.1 skrll bfd_release (input_bfd, entry);
778 1.1 skrll return 0;
779 1.1 skrll }
780 1.1 skrll
781 1.1 skrll if (entry->isym.st_shndx != SHN_UNDEF
782 1.1 skrll && entry->isym.st_shndx < SHN_LORESERVE)
783 1.1 skrll {
784 1.1 skrll asection *s;
785 1.1 skrll
786 1.1 skrll s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
787 1.1 skrll if (s == NULL || bfd_is_abs_section (s->output_section))
788 1.1 skrll {
789 1.1 skrll /* We can still bfd_release here as nothing has done another
790 1.1 skrll bfd_alloc. We can't do this later in this function. */
791 1.1 skrll bfd_release (input_bfd, entry);
792 1.1 skrll return 2;
793 1.1 skrll }
794 1.1 skrll }
795 1.1 skrll
796 1.1 skrll name = (bfd_elf_string_from_elf_section
797 1.1 skrll (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
798 1.1 skrll entry->isym.st_name));
799 1.1 skrll
800 1.1 skrll dynstr = elf_hash_table (info)->dynstr;
801 1.1 skrll if (dynstr == NULL)
802 1.1 skrll {
803 1.1 skrll /* Create a strtab to hold the dynamic symbol names. */
804 1.1 skrll elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
805 1.1 skrll if (dynstr == NULL)
806 1.1 skrll return 0;
807 1.13 christos }
808 1.1 skrll
809 1.1 skrll dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
810 1.1 skrll if (dynstr_index == (size_t) -1)
811 1.1 skrll return 0;
812 1.1 skrll entry->isym.st_name = dynstr_index;
813 1.1 skrll
814 1.1 skrll eht = elf_hash_table (info);
815 1.1 skrll
816 1.1 skrll entry->next = eht->dynlocal;
817 1.1 skrll eht->dynlocal = entry;
818 1.1 skrll entry->input_bfd = input_bfd;
819 1.1 skrll entry->input_indx = input_indx;
820 1.1 skrll eht->dynsymcount++;
821 1.1 skrll
822 1.1 skrll /* Whatever binding the symbol had before, it's now local. */
823 1.1 skrll entry->isym.st_info
824 1.1 skrll = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
825 1.1 skrll
826 1.1 skrll /* The dynindx will be set at the end of size_dynamic_sections. */
827 1.1 skrll
828 1.1 skrll return 1;
829 1.1 skrll }
830 1.1 skrll
831 1.1 skrll /* Return the dynindex of a local dynamic symbol. */
832 1.1 skrll
833 1.1 skrll long
834 1.1 skrll _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
835 1.1 skrll bfd *input_bfd,
836 1.1 skrll long input_indx)
837 1.1 skrll {
838 1.1 skrll struct elf_link_local_dynamic_entry *e;
839 1.1 skrll
840 1.1 skrll for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
841 1.1 skrll if (e->input_bfd == input_bfd && e->input_indx == input_indx)
842 1.1 skrll return e->dynindx;
843 1.1 skrll return -1;
844 1.1 skrll }
845 1.1 skrll
846 1.1 skrll /* This function is used to renumber the dynamic symbols, if some of
847 1.1 skrll them are removed because they are marked as local. This is called
848 1.1 skrll via elf_link_hash_traverse. */
849 1.1 skrll
850 1.1 skrll static bfd_boolean
851 1.4 christos elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
852 1.1 skrll void *data)
853 1.1 skrll {
854 1.1 skrll size_t *count = (size_t *) data;
855 1.1 skrll
856 1.1 skrll if (h->forced_local)
857 1.1 skrll return TRUE;
858 1.1 skrll
859 1.1 skrll if (h->dynindx != -1)
860 1.1 skrll h->dynindx = ++(*count);
861 1.1 skrll
862 1.1 skrll return TRUE;
863 1.1 skrll }
864 1.1 skrll
865 1.1 skrll
866 1.1 skrll /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
867 1.1 skrll STB_LOCAL binding. */
868 1.1 skrll
869 1.1 skrll static bfd_boolean
870 1.4 christos elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
871 1.1 skrll void *data)
872 1.1 skrll {
873 1.1 skrll size_t *count = (size_t *) data;
874 1.1 skrll
875 1.1 skrll if (!h->forced_local)
876 1.1 skrll return TRUE;
877 1.1 skrll
878 1.1 skrll if (h->dynindx != -1)
879 1.1 skrll h->dynindx = ++(*count);
880 1.1 skrll
881 1.1 skrll return TRUE;
882 1.1 skrll }
883 1.1 skrll
884 1.1 skrll /* Return true if the dynamic symbol for a given section should be
885 1.1 skrll omitted when creating a shared library. */
886 1.1 skrll bfd_boolean
887 1.1 skrll _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
888 1.1 skrll struct bfd_link_info *info,
889 1.9 christos asection *p)
890 1.1 skrll {
891 1.1 skrll struct elf_link_hash_table *htab;
892 1.1 skrll asection *ip;
893 1.1 skrll
894 1.1 skrll switch (elf_section_data (p)->this_hdr.sh_type)
895 1.1 skrll {
896 1.1 skrll case SHT_PROGBITS:
897 1.1 skrll case SHT_NOBITS:
898 1.1 skrll /* If sh_type is yet undecided, assume it could be
899 1.1 skrll SHT_PROGBITS/SHT_NOBITS. */
900 1.1 skrll case SHT_NULL:
901 1.1 skrll htab = elf_hash_table (info);
902 1.1 skrll if (p == htab->tls_sec)
903 1.1 skrll return FALSE;
904 1.1 skrll
905 1.9 christos if (htab->text_index_section != NULL)
906 1.7 christos return p != htab->text_index_section && p != htab->data_index_section;
907 1.9 christos
908 1.1 skrll return (htab->dynobj != NULL
909 1.1 skrll && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
910 1.1 skrll && ip->output_section == p);
911 1.1 skrll
912 1.1 skrll /* There shouldn't be section relative relocations
913 1.1 skrll against any other section. */
914 1.1 skrll default:
915 1.1 skrll return TRUE;
916 1.1 skrll }
917 1.1 skrll }
918 1.1 skrll
919 1.1 skrll /* Assign dynsym indices. In a shared library we generate a section
920 1.13.12.2 pgoyette symbol for each output section, which come first. Next come symbols
921 1.13.12.2 pgoyette which have been forced to local binding. Then all of the back-end
922 1.13.12.2 pgoyette allocated local dynamic syms, followed by the rest of the global
923 1.13.12.2 pgoyette symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
924 1.1 skrll (This prevents the early call before elf_backend_init_index_section
925 1.1 skrll and strip_excluded_output_sections setting dynindx for sections
926 1.1 skrll that are stripped.) */
927 1.1 skrll
928 1.1 skrll static unsigned long
929 1.1 skrll _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
930 1.1 skrll struct bfd_link_info *info,
931 1.13.12.2 pgoyette unsigned long *section_sym_count)
932 1.1 skrll {
933 1.9 christos unsigned long dynsymcount = 0;
934 1.9 christos bfd_boolean do_sec = section_sym_count != NULL;
935 1.1 skrll
936 1.1 skrll if (bfd_link_pic (info)
937 1.1 skrll || elf_hash_table (info)->is_relocatable_executable)
938 1.1 skrll {
939 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
940 1.1 skrll asection *p;
941 1.1 skrll for (p = output_bfd->sections; p ; p = p->next)
942 1.13.12.2 pgoyette if ((p->flags & SEC_EXCLUDE) == 0
943 1.13.12.2 pgoyette && (p->flags & SEC_ALLOC) != 0
944 1.13.12.2 pgoyette && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
945 1.13.12.2 pgoyette {
946 1.13.12.2 pgoyette ++dynsymcount;
947 1.13.12.2 pgoyette if (do_sec)
948 1.1 skrll elf_section_data (p)->dynindx = dynsymcount;
949 1.1 skrll }
950 1.13.12.2 pgoyette else if (do_sec)
951 1.13.12.2 pgoyette elf_section_data (p)->dynindx = 0;
952 1.1 skrll }
953 1.1 skrll if (do_sec)
954 1.1 skrll *section_sym_count = dynsymcount;
955 1.1 skrll
956 1.1 skrll elf_link_hash_traverse (elf_hash_table (info),
957 1.1 skrll elf_link_renumber_local_hash_table_dynsyms,
958 1.1 skrll &dynsymcount);
959 1.1 skrll
960 1.1 skrll if (elf_hash_table (info)->dynlocal)
961 1.1 skrll {
962 1.1 skrll struct elf_link_local_dynamic_entry *p;
963 1.13.12.2 pgoyette for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
964 1.1 skrll p->dynindx = ++dynsymcount;
965 1.1 skrll }
966 1.1 skrll elf_hash_table (info)->local_dynsymcount = dynsymcount;
967 1.1 skrll
968 1.1 skrll elf_link_hash_traverse (elf_hash_table (info),
969 1.13 christos elf_link_renumber_hash_table_dynsyms,
970 1.13 christos &dynsymcount);
971 1.13 christos
972 1.13 christos /* There is an unused NULL entry at the head of the table which we
973 1.13 christos must account for in our count even if the table is empty since it
974 1.1 skrll is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
975 1.1 skrll .dynamic section. */
976 1.1 skrll dynsymcount++;
977 1.1 skrll
978 1.1 skrll elf_hash_table (info)->dynsymcount = dynsymcount;
979 1.4 christos return dynsymcount;
980 1.4 christos }
981 1.4 christos
982 1.4 christos /* Merge st_other field. */
983 1.9 christos
984 1.9 christos static void
985 1.4 christos elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
986 1.4 christos const Elf_Internal_Sym *isym, asection *sec,
987 1.4 christos bfd_boolean definition, bfd_boolean dynamic)
988 1.4 christos {
989 1.9 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
990 1.4 christos
991 1.4 christos /* If st_other has a processor-specific meaning, specific
992 1.4 christos code might be needed here. */
993 1.4 christos if (bed->elf_backend_merge_symbol_attribute)
994 1.9 christos (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
995 1.9 christos dynamic);
996 1.9 christos
997 1.9 christos if (!dynamic)
998 1.4 christos {
999 1.9 christos unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1000 1.9 christos unsigned hvis = ELF_ST_VISIBILITY (h->other);
1001 1.9 christos
1002 1.9 christos /* Keep the most constraining visibility. Leave the remainder
1003 1.9 christos of the st_other field to elf_backend_merge_symbol_attribute. */
1004 1.9 christos if (symvis - 1 < hvis - 1)
1005 1.9 christos h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1006 1.9 christos }
1007 1.9 christos else if (definition
1008 1.9 christos && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1009 1.9 christos && (sec->flags & SEC_READONLY) == 0)
1010 1.9 christos h->protected_def = 1;
1011 1.9 christos }
1012 1.9 christos
1013 1.9 christos /* This function is called when we want to merge a new symbol with an
1014 1.9 christos existing symbol. It handles the various cases which arise when we
1015 1.9 christos find a definition in a dynamic object, or when there is already a
1016 1.9 christos definition in a dynamic object. The new symbol is described by
1017 1.9 christos NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1018 1.9 christos entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1019 1.9 christos if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1020 1.9 christos of an old common symbol. We set OVERRIDE if the old symbol is
1021 1.9 christos overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1022 1.1 skrll the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1023 1.9 christos to change. By OK to change, we mean that we shouldn't warn if the
1024 1.1 skrll type or size does change. */
1025 1.1 skrll
1026 1.1 skrll static bfd_boolean
1027 1.1 skrll _bfd_elf_merge_symbol (bfd *abfd,
1028 1.1 skrll struct bfd_link_info *info,
1029 1.1 skrll const char *name,
1030 1.9 christos Elf_Internal_Sym *sym,
1031 1.9 christos asection **psec,
1032 1.7 christos bfd_vma *pvalue,
1033 1.1 skrll struct elf_link_hash_entry **sym_hash,
1034 1.1 skrll bfd **poldbfd,
1035 1.1 skrll bfd_boolean *pold_weak,
1036 1.1 skrll unsigned int *pold_alignment,
1037 1.9 christos bfd_boolean *skip,
1038 1.9 christos bfd_boolean *override,
1039 1.1 skrll bfd_boolean *type_change_ok,
1040 1.1 skrll bfd_boolean *size_change_ok,
1041 1.1 skrll bfd_boolean *matched)
1042 1.7 christos {
1043 1.1 skrll asection *sec, *oldsec;
1044 1.1 skrll struct elf_link_hash_entry *h;
1045 1.1 skrll struct elf_link_hash_entry *hi;
1046 1.1 skrll struct elf_link_hash_entry *flip;
1047 1.4 christos int bind;
1048 1.1 skrll bfd *oldbfd;
1049 1.9 christos bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1050 1.13.12.2 pgoyette bfd_boolean newweak, oldweak, newfunc, oldfunc;
1051 1.1 skrll const struct elf_backend_data *bed;
1052 1.1 skrll char *new_version;
1053 1.1 skrll bfd_boolean default_sym = *matched;
1054 1.1 skrll
1055 1.1 skrll *skip = FALSE;
1056 1.1 skrll *override = FALSE;
1057 1.1 skrll
1058 1.1 skrll sec = *psec;
1059 1.1 skrll bind = ELF_ST_BIND (sym->st_info);
1060 1.1 skrll
1061 1.1 skrll if (! bfd_is_und_section (sec))
1062 1.1 skrll h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1063 1.1 skrll else
1064 1.1 skrll h = ((struct elf_link_hash_entry *)
1065 1.1 skrll bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1066 1.1 skrll if (h == NULL)
1067 1.1 skrll return FALSE;
1068 1.1 skrll *sym_hash = h;
1069 1.9 christos
1070 1.9 christos bed = get_elf_backend_data (abfd);
1071 1.9 christos
1072 1.9 christos /* NEW_VERSION is the symbol version of the new symbol. */
1073 1.9 christos if (h->versioned != unversioned)
1074 1.9 christos {
1075 1.9 christos /* Symbol version is unknown or versioned. */
1076 1.9 christos new_version = strrchr (name, ELF_VER_CHR);
1077 1.9 christos if (new_version)
1078 1.9 christos {
1079 1.9 christos if (h->versioned == unknown)
1080 1.9 christos {
1081 1.9 christos if (new_version > name && new_version[-1] != ELF_VER_CHR)
1082 1.9 christos h->versioned = versioned_hidden;
1083 1.9 christos else
1084 1.9 christos h->versioned = versioned;
1085 1.9 christos }
1086 1.9 christos new_version += 1;
1087 1.9 christos if (new_version[0] == '\0')
1088 1.9 christos new_version = NULL;
1089 1.9 christos }
1090 1.9 christos else
1091 1.9 christos h->versioned = unversioned;
1092 1.1 skrll }
1093 1.7 christos else
1094 1.7 christos new_version = NULL;
1095 1.7 christos
1096 1.1 skrll /* For merging, we only care about real symbols. But we need to make
1097 1.1 skrll sure that indirect symbol dynamic flags are updated. */
1098 1.1 skrll hi = h;
1099 1.1 skrll while (h->root.type == bfd_link_hash_indirect
1100 1.9 christos || h->root.type == bfd_link_hash_warning)
1101 1.9 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
1102 1.9 christos
1103 1.9 christos if (!*matched)
1104 1.9 christos {
1105 1.9 christos if (hi == h || h->root.type == bfd_link_hash_new)
1106 1.9 christos *matched = TRUE;
1107 1.9 christos else
1108 1.9 christos {
1109 1.9 christos /* OLD_HIDDEN is true if the existing symbol is only visible
1110 1.9 christos to the symbol with the same symbol version. NEW_HIDDEN is
1111 1.9 christos true if the new symbol is only visible to the symbol with
1112 1.9 christos the same symbol version. */
1113 1.9 christos bfd_boolean old_hidden = h->versioned == versioned_hidden;
1114 1.9 christos bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1115 1.9 christos if (!old_hidden && !new_hidden)
1116 1.9 christos /* The new symbol matches the existing symbol if both
1117 1.9 christos aren't hidden. */
1118 1.9 christos *matched = TRUE;
1119 1.9 christos else
1120 1.9 christos {
1121 1.9 christos /* OLD_VERSION is the symbol version of the existing
1122 1.9 christos symbol. */
1123 1.9 christos char *old_version;
1124 1.9 christos
1125 1.9 christos if (h->versioned >= versioned)
1126 1.9 christos old_version = strrchr (h->root.root.string,
1127 1.9 christos ELF_VER_CHR) + 1;
1128 1.9 christos else
1129 1.9 christos old_version = NULL;
1130 1.9 christos
1131 1.9 christos /* The new symbol matches the existing symbol if they
1132 1.9 christos have the same symbol version. */
1133 1.9 christos *matched = (old_version == new_version
1134 1.9 christos || (old_version != NULL
1135 1.9 christos && new_version != NULL
1136 1.9 christos && strcmp (old_version, new_version) == 0));
1137 1.9 christos }
1138 1.9 christos }
1139 1.9 christos }
1140 1.9 christos
1141 1.9 christos /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1142 1.9 christos existing symbol. */
1143 1.9 christos
1144 1.9 christos oldbfd = NULL;
1145 1.9 christos oldsec = NULL;
1146 1.9 christos switch (h->root.type)
1147 1.9 christos {
1148 1.9 christos default:
1149 1.9 christos break;
1150 1.9 christos
1151 1.9 christos case bfd_link_hash_undefined:
1152 1.9 christos case bfd_link_hash_undefweak:
1153 1.9 christos oldbfd = h->root.u.undef.abfd;
1154 1.9 christos break;
1155 1.9 christos
1156 1.9 christos case bfd_link_hash_defined:
1157 1.9 christos case bfd_link_hash_defweak:
1158 1.9 christos oldbfd = h->root.u.def.section->owner;
1159 1.9 christos oldsec = h->root.u.def.section;
1160 1.9 christos break;
1161 1.9 christos
1162 1.9 christos case bfd_link_hash_common:
1163 1.9 christos oldbfd = h->root.u.c.p->section->owner;
1164 1.9 christos oldsec = h->root.u.c.p->section;
1165 1.9 christos if (pold_alignment)
1166 1.9 christos *pold_alignment = h->root.u.c.p->alignment_power;
1167 1.9 christos break;
1168 1.9 christos }
1169 1.9 christos if (poldbfd && *poldbfd == NULL)
1170 1.9 christos *poldbfd = oldbfd;
1171 1.9 christos
1172 1.9 christos /* Differentiate strong and weak symbols. */
1173 1.9 christos newweak = bind == STB_WEAK;
1174 1.9 christos oldweak = (h->root.type == bfd_link_hash_defweak
1175 1.9 christos || h->root.type == bfd_link_hash_undefweak);
1176 1.1 skrll if (pold_weak)
1177 1.7 christos *pold_weak = oldweak;
1178 1.1 skrll
1179 1.1 skrll /* We have to check it for every instance since the first few may be
1180 1.1 skrll references and not all compilers emit symbol type for undefined
1181 1.7 christos symbols. */
1182 1.7 christos bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1183 1.7 christos
1184 1.7 christos /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1185 1.7 christos respectively, is from a dynamic object. */
1186 1.7 christos
1187 1.7 christos newdyn = (abfd->flags & DYNAMIC) != 0;
1188 1.7 christos
1189 1.7 christos /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1190 1.7 christos syms and defined syms in dynamic libraries respectively.
1191 1.7 christos ref_dynamic on the other hand can be set for a symbol defined in
1192 1.7 christos a dynamic library, and def_dynamic may not be set; When the
1193 1.7 christos definition in a dynamic lib is overridden by a definition in the
1194 1.7 christos executable use of the symbol in the dynamic lib becomes a
1195 1.7 christos reference to the executable symbol. */
1196 1.7 christos if (newdyn)
1197 1.7 christos {
1198 1.7 christos if (bfd_is_und_section (sec))
1199 1.7 christos {
1200 1.7 christos if (bind != STB_WEAK)
1201 1.7 christos {
1202 1.7 christos h->ref_dynamic_nonweak = 1;
1203 1.7 christos hi->ref_dynamic_nonweak = 1;
1204 1.7 christos }
1205 1.9 christos }
1206 1.9 christos else
1207 1.9 christos {
1208 1.7 christos /* Update the existing symbol only if they match. */
1209 1.7 christos if (*matched)
1210 1.7 christos h->dynamic_def = 1;
1211 1.7 christos hi->dynamic_def = 1;
1212 1.1 skrll }
1213 1.1 skrll }
1214 1.1 skrll
1215 1.1 skrll /* If we just created the symbol, mark it as being an ELF symbol.
1216 1.1 skrll Other than that, there is nothing to do--there is no merge issue
1217 1.1 skrll with a newly defined symbol--so we just return. */
1218 1.1 skrll
1219 1.1 skrll if (h->root.type == bfd_link_hash_new)
1220 1.1 skrll {
1221 1.1 skrll h->non_elf = 0;
1222 1.1 skrll return TRUE;
1223 1.1 skrll }
1224 1.1 skrll
1225 1.1 skrll /* In cases involving weak versioned symbols, we may wind up trying
1226 1.1 skrll to merge a symbol with itself. Catch that here, to avoid the
1227 1.1 skrll confusion that results if we try to override a symbol with
1228 1.1 skrll itself. The additional tests catch cases like
1229 1.4 christos _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1230 1.1 skrll dynamic object, which we do want to handle here. */
1231 1.1 skrll if (abfd == oldbfd
1232 1.1 skrll && (newweak || oldweak)
1233 1.1 skrll && ((abfd->flags & DYNAMIC) == 0
1234 1.1 skrll || !h->def_regular))
1235 1.1 skrll return TRUE;
1236 1.1 skrll
1237 1.1 skrll olddyn = FALSE;
1238 1.1 skrll if (oldbfd != NULL)
1239 1.1 skrll olddyn = (oldbfd->flags & DYNAMIC) != 0;
1240 1.1 skrll else if (oldsec != NULL)
1241 1.1 skrll {
1242 1.1 skrll /* This handles the special SHN_MIPS_{TEXT,DATA} section
1243 1.1 skrll indices used by MIPS ELF. */
1244 1.13.12.2 pgoyette olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1245 1.13.12.2 pgoyette }
1246 1.13.12.2 pgoyette
1247 1.13.12.2 pgoyette /* Handle a case where plugin_notice won't be called and thus won't
1248 1.13.12.2 pgoyette set the non_ir_ref flags on the first pass over symbols. */
1249 1.13.12.2 pgoyette if (oldbfd != NULL
1250 1.13.12.2 pgoyette && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1251 1.13.12.2 pgoyette && newdyn != olddyn)
1252 1.13.12.2 pgoyette {
1253 1.13.12.2 pgoyette h->root.non_ir_ref_dynamic = TRUE;
1254 1.1 skrll hi->root.non_ir_ref_dynamic = TRUE;
1255 1.1 skrll }
1256 1.1 skrll
1257 1.1 skrll /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1258 1.1 skrll respectively, appear to be a definition rather than reference. */
1259 1.1 skrll
1260 1.1 skrll newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1261 1.1 skrll
1262 1.1 skrll olddef = (h->root.type != bfd_link_hash_undefined
1263 1.4 christos && h->root.type != bfd_link_hash_undefweak
1264 1.4 christos && h->root.type != bfd_link_hash_common);
1265 1.4 christos
1266 1.4 christos /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1267 1.4 christos respectively, appear to be a function. */
1268 1.4 christos
1269 1.4 christos newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1270 1.4 christos && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1271 1.4 christos
1272 1.13.12.2 pgoyette oldfunc = (h->type != STT_NOTYPE
1273 1.12 christos && bed->is_function_type (h->type));
1274 1.12 christos
1275 1.12 christos if (!(newfunc && oldfunc)
1276 1.13.12.2 pgoyette && ELF_ST_TYPE (sym->st_info) != h->type
1277 1.13.12.2 pgoyette && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1278 1.1 skrll && h->type != STT_NOTYPE
1279 1.13.12.2 pgoyette && (newdef || bfd_is_com_section (sec))
1280 1.13.12.2 pgoyette && (olddef || h->root.type == bfd_link_hash_common))
1281 1.13.12.2 pgoyette {
1282 1.13.12.2 pgoyette /* If creating a default indirect symbol ("foo" or "foo@") from
1283 1.13.12.2 pgoyette a dynamic versioned definition ("foo@@") skip doing so if
1284 1.13.12.2 pgoyette there is an existing regular definition with a different
1285 1.13.12.2 pgoyette type. We don't want, for example, a "time" variable in the
1286 1.13.12.2 pgoyette executable overriding a "time" function in a shared library. */
1287 1.13.12.2 pgoyette if (newdyn
1288 1.13.12.2 pgoyette && !olddyn)
1289 1.13.12.2 pgoyette {
1290 1.13.12.2 pgoyette *skip = TRUE;
1291 1.13.12.2 pgoyette return TRUE;
1292 1.13.12.2 pgoyette }
1293 1.13.12.2 pgoyette
1294 1.13.12.2 pgoyette /* When adding a symbol from a regular object file after we have
1295 1.13.12.2 pgoyette created indirect symbols, undo the indirection and any
1296 1.13.12.2 pgoyette dynamic state. */
1297 1.13.12.2 pgoyette if (hi != h
1298 1.13.12.2 pgoyette && !newdyn
1299 1.13.12.2 pgoyette && olddyn)
1300 1.13.12.2 pgoyette {
1301 1.13.12.2 pgoyette h = hi;
1302 1.13.12.2 pgoyette (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1303 1.13.12.2 pgoyette h->forced_local = 0;
1304 1.13.12.2 pgoyette h->ref_dynamic = 0;
1305 1.13.12.2 pgoyette h->def_dynamic = 0;
1306 1.13.12.2 pgoyette h->dynamic_def = 0;
1307 1.13.12.2 pgoyette if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1308 1.13.12.2 pgoyette {
1309 1.13.12.2 pgoyette h->root.type = bfd_link_hash_undefined;
1310 1.13.12.2 pgoyette h->root.u.undef.abfd = abfd;
1311 1.13.12.2 pgoyette }
1312 1.13.12.2 pgoyette else
1313 1.13.12.2 pgoyette {
1314 1.13.12.2 pgoyette h->root.type = bfd_link_hash_new;
1315 1.13.12.2 pgoyette h->root.u.undef.abfd = NULL;
1316 1.1 skrll }
1317 1.1 skrll return TRUE;
1318 1.9 christos }
1319 1.9 christos }
1320 1.9 christos
1321 1.9 christos /* Check TLS symbols. We don't check undefined symbols introduced
1322 1.9 christos by "ld -u" which have no type (and oldbfd NULL), and we don't
1323 1.9 christos check symbols from plugins because they also have no type. */
1324 1.9 christos if (oldbfd != NULL
1325 1.9 christos && (oldbfd->flags & BFD_PLUGIN) == 0
1326 1.1 skrll && (abfd->flags & BFD_PLUGIN) == 0
1327 1.1 skrll && ELF_ST_TYPE (sym->st_info) != h->type
1328 1.1 skrll && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1329 1.1 skrll {
1330 1.1 skrll bfd *ntbfd, *tbfd;
1331 1.1 skrll bfd_boolean ntdef, tdef;
1332 1.1 skrll asection *ntsec, *tsec;
1333 1.1 skrll
1334 1.1 skrll if (h->type == STT_TLS)
1335 1.1 skrll {
1336 1.1 skrll ntbfd = abfd;
1337 1.1 skrll ntsec = sec;
1338 1.1 skrll ntdef = newdef;
1339 1.1 skrll tbfd = oldbfd;
1340 1.1 skrll tsec = oldsec;
1341 1.1 skrll tdef = olddef;
1342 1.1 skrll }
1343 1.1 skrll else
1344 1.1 skrll {
1345 1.1 skrll ntbfd = oldbfd;
1346 1.1 skrll ntsec = oldsec;
1347 1.1 skrll ntdef = olddef;
1348 1.1 skrll tbfd = abfd;
1349 1.1 skrll tsec = sec;
1350 1.1 skrll tdef = newdef;
1351 1.13.12.2 pgoyette }
1352 1.13.12.2 pgoyette
1353 1.9 christos if (tdef && ntdef)
1354 1.9 christos _bfd_error_handler
1355 1.13.12.2 pgoyette /* xgettext:c-format */
1356 1.1 skrll (_("%s: TLS definition in %B section %A "
1357 1.13.12.2 pgoyette "mismatches non-TLS definition in %B section %A"),
1358 1.13.12.2 pgoyette h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1359 1.9 christos else if (!tdef && !ntdef)
1360 1.9 christos _bfd_error_handler
1361 1.13.12.2 pgoyette /* xgettext:c-format */
1362 1.1 skrll (_("%s: TLS reference in %B "
1363 1.13.12.2 pgoyette "mismatches non-TLS reference in %B"),
1364 1.13.12.2 pgoyette h->root.root.string, tbfd, ntbfd);
1365 1.9 christos else if (tdef)
1366 1.9 christos _bfd_error_handler
1367 1.13.12.2 pgoyette /* xgettext:c-format */
1368 1.1 skrll (_("%s: TLS definition in %B section %A "
1369 1.13.12.2 pgoyette "mismatches non-TLS reference in %B"),
1370 1.13.12.2 pgoyette h->root.root.string, tbfd, tsec, ntbfd);
1371 1.9 christos else
1372 1.9 christos _bfd_error_handler
1373 1.13.12.2 pgoyette /* xgettext:c-format */
1374 1.1 skrll (_("%s: TLS reference in %B "
1375 1.1 skrll "mismatches non-TLS definition in %B section %A"),
1376 1.1 skrll h->root.root.string, tbfd, ntbfd, ntsec);
1377 1.1 skrll
1378 1.1 skrll bfd_set_error (bfd_error_bad_value);
1379 1.1 skrll return FALSE;
1380 1.1 skrll }
1381 1.1 skrll
1382 1.1 skrll /* If the old symbol has non-default visibility, we ignore the new
1383 1.1 skrll definition from a dynamic object. */
1384 1.1 skrll if (newdyn
1385 1.1 skrll && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1386 1.1 skrll && !bfd_is_und_section (sec))
1387 1.1 skrll {
1388 1.7 christos *skip = TRUE;
1389 1.1 skrll /* Make sure this symbol is dynamic. */
1390 1.1 skrll h->ref_dynamic = 1;
1391 1.1 skrll hi->ref_dynamic = 1;
1392 1.1 skrll /* A protected symbol has external availability. Make sure it is
1393 1.1 skrll recorded as dynamic.
1394 1.1 skrll
1395 1.1 skrll FIXME: Should we check type and size for protected symbol? */
1396 1.1 skrll if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1397 1.1 skrll return bfd_elf_link_record_dynamic_symbol (info, h);
1398 1.1 skrll else
1399 1.1 skrll return TRUE;
1400 1.1 skrll }
1401 1.1 skrll else if (!newdyn
1402 1.1 skrll && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1403 1.1 skrll && h->def_dynamic)
1404 1.1 skrll {
1405 1.9 christos /* If the new symbol with non-default visibility comes from a
1406 1.1 skrll relocatable file and the old definition comes from a dynamic
1407 1.1 skrll object, we remove the old definition. */
1408 1.1 skrll if (hi->root.type == bfd_link_hash_indirect)
1409 1.1 skrll {
1410 1.1 skrll /* Handle the case where the old dynamic definition is
1411 1.1 skrll default versioned. We need to copy the symbol info from
1412 1.1 skrll the symbol with default version to the normal one if it
1413 1.9 christos was referenced before. */
1414 1.1 skrll if (h->ref_regular)
1415 1.9 christos {
1416 1.7 christos hi->root.type = h->root.type;
1417 1.9 christos h->root.type = bfd_link_hash_indirect;
1418 1.7 christos (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1419 1.7 christos
1420 1.7 christos h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1421 1.7 christos if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1422 1.7 christos {
1423 1.7 christos /* If the new symbol is hidden or internal, completely undo
1424 1.7 christos any dynamic link state. */
1425 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1426 1.1 skrll h->forced_local = 0;
1427 1.7 christos h->ref_dynamic = 0;
1428 1.7 christos }
1429 1.7 christos else
1430 1.7 christos h->ref_dynamic = 1;
1431 1.7 christos
1432 1.7 christos h->def_dynamic = 0;
1433 1.7 christos /* FIXME: Should we check type and size for protected symbol? */
1434 1.9 christos h->size = 0;
1435 1.1 skrll h->type = 0;
1436 1.1 skrll
1437 1.9 christos h = hi;
1438 1.1 skrll }
1439 1.1 skrll else
1440 1.7 christos h = hi;
1441 1.7 christos }
1442 1.7 christos
1443 1.7 christos /* If the old symbol was undefined before, then it will still be
1444 1.7 christos on the undefs list. If the new symbol is undefined or
1445 1.7 christos common, we can't make it bfd_link_hash_new here, because new
1446 1.7 christos undefined or common symbols will be added to the undefs list
1447 1.7 christos by _bfd_generic_link_add_one_symbol. Symbols may not be
1448 1.1 skrll added twice to the undefs list. Also, if the new symbol is
1449 1.1 skrll undefweak then we don't want to lose the strong undef. */
1450 1.1 skrll if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1451 1.1 skrll {
1452 1.1 skrll h->root.type = bfd_link_hash_undefined;
1453 1.1 skrll h->root.u.undef.abfd = abfd;
1454 1.1 skrll }
1455 1.1 skrll else
1456 1.1 skrll {
1457 1.1 skrll h->root.type = bfd_link_hash_new;
1458 1.7 christos h->root.u.undef.abfd = NULL;
1459 1.1 skrll }
1460 1.7 christos
1461 1.7 christos if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1462 1.7 christos {
1463 1.7 christos /* If the new symbol is hidden or internal, completely undo
1464 1.7 christos any dynamic link state. */
1465 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1466 1.7 christos h->forced_local = 0;
1467 1.7 christos h->ref_dynamic = 0;
1468 1.7 christos }
1469 1.1 skrll else
1470 1.1 skrll h->ref_dynamic = 1;
1471 1.1 skrll h->def_dynamic = 0;
1472 1.1 skrll /* FIXME: Should we check type and size for protected symbol? */
1473 1.1 skrll h->size = 0;
1474 1.1 skrll h->type = 0;
1475 1.1 skrll return TRUE;
1476 1.1 skrll }
1477 1.1 skrll
1478 1.1 skrll /* If a new weak symbol definition comes from a regular file and the
1479 1.1 skrll old symbol comes from a dynamic library, we treat the new one as
1480 1.1 skrll strong. Similarly, an old weak symbol definition from a regular
1481 1.1 skrll file is treated as strong when the new symbol comes from a dynamic
1482 1.1 skrll library. Further, an old weak symbol from a dynamic library is
1483 1.13.12.2 pgoyette treated as strong if the new symbol is from a dynamic library.
1484 1.13.12.2 pgoyette This reflects the way glibc's ld.so works.
1485 1.13.12.2 pgoyette
1486 1.13.12.2 pgoyette Also allow a weak symbol to override a linker script symbol
1487 1.13.12.2 pgoyette defined by an early pass over the script. This is done so the
1488 1.1 skrll linker knows the symbol is defined in an object file, for the
1489 1.1 skrll DEFINED script function.
1490 1.1 skrll
1491 1.13.12.2 pgoyette Do this before setting *type_change_ok or *size_change_ok so that
1492 1.1 skrll we warn properly when dynamic library symbols are overridden. */
1493 1.1 skrll
1494 1.1 skrll if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1495 1.1 skrll newweak = FALSE;
1496 1.4 christos if (olddef && newdyn)
1497 1.4 christos oldweak = FALSE;
1498 1.1 skrll
1499 1.1 skrll /* Allow changes between different types of function symbol. */
1500 1.1 skrll if (newfunc && oldfunc)
1501 1.1 skrll *type_change_ok = TRUE;
1502 1.1 skrll
1503 1.1 skrll /* It's OK to change the type if either the existing symbol or the
1504 1.1 skrll new symbol is weak. A type change is also OK if the old symbol
1505 1.1 skrll is undefined and the new symbol is defined. */
1506 1.1 skrll
1507 1.1 skrll if (oldweak
1508 1.1 skrll || newweak
1509 1.1 skrll || (newdef
1510 1.1 skrll && h->root.type == bfd_link_hash_undefined))
1511 1.1 skrll *type_change_ok = TRUE;
1512 1.1 skrll
1513 1.1 skrll /* It's OK to change the size if either the existing symbol or the
1514 1.1 skrll new symbol is weak, or if the old symbol is undefined. */
1515 1.1 skrll
1516 1.1 skrll if (*type_change_ok
1517 1.1 skrll || h->root.type == bfd_link_hash_undefined)
1518 1.1 skrll *size_change_ok = TRUE;
1519 1.1 skrll
1520 1.1 skrll /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1521 1.1 skrll symbol, respectively, appears to be a common symbol in a dynamic
1522 1.1 skrll object. If a symbol appears in an uninitialized section, and is
1523 1.1 skrll not weak, and is not a function, then it may be a common symbol
1524 1.1 skrll which was resolved when the dynamic object was created. We want
1525 1.1 skrll to treat such symbols specially, because they raise special
1526 1.1 skrll considerations when setting the symbol size: if the symbol
1527 1.1 skrll appears as a common symbol in a regular object, and the size in
1528 1.1 skrll the regular object is larger, we must make sure that we use the
1529 1.1 skrll larger size. This problematic case can always be avoided in C,
1530 1.1 skrll but it must be handled correctly when using Fortran shared
1531 1.1 skrll libraries.
1532 1.1 skrll
1533 1.1 skrll Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1534 1.1 skrll likewise for OLDDYNCOMMON and OLDDEF.
1535 1.1 skrll
1536 1.1 skrll Note that this test is just a heuristic, and that it is quite
1537 1.1 skrll possible to have an uninitialized symbol in a shared object which
1538 1.1 skrll is really a definition, rather than a common symbol. This could
1539 1.1 skrll lead to some minor confusion when the symbol really is a common
1540 1.1 skrll symbol in some regular object. However, I think it will be
1541 1.1 skrll harmless. */
1542 1.1 skrll
1543 1.1 skrll if (newdyn
1544 1.1 skrll && newdef
1545 1.1 skrll && !newweak
1546 1.4 christos && (sec->flags & SEC_ALLOC) != 0
1547 1.1 skrll && (sec->flags & SEC_LOAD) == 0
1548 1.1 skrll && sym->st_size > 0
1549 1.1 skrll && !newfunc)
1550 1.1 skrll newdyncommon = TRUE;
1551 1.1 skrll else
1552 1.1 skrll newdyncommon = FALSE;
1553 1.1 skrll
1554 1.1 skrll if (olddyn
1555 1.1 skrll && olddef
1556 1.1 skrll && h->root.type == bfd_link_hash_defined
1557 1.1 skrll && h->def_dynamic
1558 1.4 christos && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1559 1.1 skrll && (h->root.u.def.section->flags & SEC_LOAD) == 0
1560 1.1 skrll && h->size > 0
1561 1.1 skrll && !oldfunc)
1562 1.1 skrll olddyncommon = TRUE;
1563 1.1 skrll else
1564 1.1 skrll olddyncommon = FALSE;
1565 1.9 christos
1566 1.9 christos /* We now know everything about the old and new symbols. We ask the
1567 1.9 christos backend to check if we can merge them. */
1568 1.9 christos if (bed->merge_symbol != NULL)
1569 1.9 christos {
1570 1.9 christos if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1571 1.1 skrll return FALSE;
1572 1.13.12.2 pgoyette sec = *psec;
1573 1.13.12.2 pgoyette }
1574 1.13.12.2 pgoyette
1575 1.13.12.2 pgoyette /* There are multiple definitions of a normal symbol. Skip the
1576 1.13.12.2 pgoyette default symbol as well as definition from an IR object. */
1577 1.13.12.2 pgoyette if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1578 1.13.12.2 pgoyette && !default_sym && h->def_regular
1579 1.13.12.2 pgoyette && !(oldbfd != NULL
1580 1.13.12.2 pgoyette && (oldbfd->flags & BFD_PLUGIN) != 0
1581 1.13.12.2 pgoyette && (abfd->flags & BFD_PLUGIN) == 0))
1582 1.13.12.2 pgoyette {
1583 1.13.12.2 pgoyette /* Handle a multiple definition. */
1584 1.13.12.2 pgoyette (*info->callbacks->multiple_definition) (info, &h->root,
1585 1.13.12.2 pgoyette abfd, sec, *pvalue);
1586 1.13.12.2 pgoyette *skip = TRUE;
1587 1.1 skrll return TRUE;
1588 1.1 skrll }
1589 1.1 skrll
1590 1.1 skrll /* If both the old and the new symbols look like common symbols in a
1591 1.1 skrll dynamic object, set the size of the symbol to the larger of the
1592 1.1 skrll two. */
1593 1.1 skrll
1594 1.1 skrll if (olddyncommon
1595 1.1 skrll && newdyncommon
1596 1.1 skrll && sym->st_size != h->size)
1597 1.1 skrll {
1598 1.1 skrll /* Since we think we have two common symbols, issue a multiple
1599 1.1 skrll common warning if desired. Note that we only warn if the
1600 1.1 skrll size is different. If the size is the same, we simply let
1601 1.13 christos the old symbol override the new one as normally happens with
1602 1.13 christos symbols defined in dynamic objects. */
1603 1.1 skrll
1604 1.1 skrll (*info->callbacks->multiple_common) (info, &h->root, abfd,
1605 1.1 skrll bfd_link_hash_common, sym->st_size);
1606 1.1 skrll if (sym->st_size > h->size)
1607 1.1 skrll h->size = sym->st_size;
1608 1.1 skrll
1609 1.1 skrll *size_change_ok = TRUE;
1610 1.1 skrll }
1611 1.1 skrll
1612 1.1 skrll /* If we are looking at a dynamic object, and we have found a
1613 1.1 skrll definition, we need to see if the symbol was already defined by
1614 1.1 skrll some other object. If so, we want to use the existing
1615 1.1 skrll definition, and we do not want to report a multiple symbol
1616 1.1 skrll definition error; we do this by clobbering *PSEC to be
1617 1.1 skrll bfd_und_section_ptr.
1618 1.1 skrll
1619 1.1 skrll We treat a common symbol as a definition if the symbol in the
1620 1.1 skrll shared library is a function, since common symbols always
1621 1.13.12.2 pgoyette represent variables; this can cause confusion in principle, but
1622 1.1 skrll any such confusion would seem to indicate an erroneous program or
1623 1.1 skrll shared library. We also permit a common symbol in a regular
1624 1.1 skrll object to override a weak symbol in a shared object. */
1625 1.1 skrll
1626 1.1 skrll if (newdyn
1627 1.13.12.2 pgoyette && newdef
1628 1.1 skrll && (olddef
1629 1.1 skrll || (h->root.type == bfd_link_hash_common
1630 1.1 skrll && (newweak || newfunc))))
1631 1.1 skrll {
1632 1.1 skrll *override = TRUE;
1633 1.1 skrll newdef = FALSE;
1634 1.1 skrll newdyncommon = FALSE;
1635 1.1 skrll
1636 1.1 skrll *psec = sec = bfd_und_section_ptr;
1637 1.1 skrll *size_change_ok = TRUE;
1638 1.1 skrll
1639 1.1 skrll /* If we get here when the old symbol is a common symbol, then
1640 1.1 skrll we are explicitly letting it override a weak symbol or
1641 1.1 skrll function in a dynamic object, and we don't want to warn about
1642 1.1 skrll a type change. If the old symbol is a defined symbol, a type
1643 1.1 skrll change warning may still be appropriate. */
1644 1.1 skrll
1645 1.1 skrll if (h->root.type == bfd_link_hash_common)
1646 1.1 skrll *type_change_ok = TRUE;
1647 1.1 skrll }
1648 1.1 skrll
1649 1.1 skrll /* Handle the special case of an old common symbol merging with a
1650 1.1 skrll new symbol which looks like a common symbol in a shared object.
1651 1.1 skrll We change *PSEC and *PVALUE to make the new symbol look like a
1652 1.1 skrll common symbol, and let _bfd_generic_link_add_one_symbol do the
1653 1.1 skrll right thing. */
1654 1.1 skrll
1655 1.1 skrll if (newdyncommon
1656 1.1 skrll && h->root.type == bfd_link_hash_common)
1657 1.1 skrll {
1658 1.1 skrll *override = TRUE;
1659 1.1 skrll newdef = FALSE;
1660 1.1 skrll newdyncommon = FALSE;
1661 1.1 skrll *pvalue = sym->st_size;
1662 1.1 skrll *psec = sec = bed->common_section (oldsec);
1663 1.1 skrll *size_change_ok = TRUE;
1664 1.1 skrll }
1665 1.4 christos
1666 1.4 christos /* Skip weak definitions of symbols that are already defined. */
1667 1.4 christos if (newdef && olddef && newweak)
1668 1.4 christos {
1669 1.4 christos /* Don't skip new non-IR weak syms. */
1670 1.8 skrll if (!(oldbfd != NULL
1671 1.8 skrll && (oldbfd->flags & BFD_PLUGIN) != 0
1672 1.8 skrll && (abfd->flags & BFD_PLUGIN) == 0))
1673 1.8 skrll {
1674 1.4 christos newdef = FALSE;
1675 1.4 christos *skip = TRUE;
1676 1.4 christos }
1677 1.4 christos
1678 1.9 christos /* Merge st_other. If the symbol already has a dynamic index,
1679 1.4 christos but visibility says it should not be visible, turn it into a
1680 1.4 christos local symbol. */
1681 1.4 christos elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1682 1.4 christos if (h->dynindx != -1)
1683 1.4 christos switch (ELF_ST_VISIBILITY (h->other))
1684 1.4 christos {
1685 1.4 christos case STV_INTERNAL:
1686 1.4 christos case STV_HIDDEN:
1687 1.4 christos (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1688 1.1 skrll break;
1689 1.1 skrll }
1690 1.1 skrll }
1691 1.1 skrll
1692 1.1 skrll /* If the old symbol is from a dynamic object, and the new symbol is
1693 1.1 skrll a definition which is not from a dynamic object, then the new
1694 1.1 skrll symbol overrides the old symbol. Symbols from regular files
1695 1.1 skrll always take precedence over symbols from dynamic objects, even if
1696 1.1 skrll they are defined after the dynamic object in the link.
1697 1.1 skrll
1698 1.1 skrll As above, we again permit a common symbol in a regular object to
1699 1.1 skrll override a definition in a shared object if the shared object
1700 1.1 skrll symbol is a function or is weak. */
1701 1.1 skrll
1702 1.1 skrll flip = NULL;
1703 1.4 christos if (!newdyn
1704 1.1 skrll && (newdef
1705 1.1 skrll || (bfd_is_com_section (sec)
1706 1.1 skrll && (oldweak || oldfunc)))
1707 1.1 skrll && olddyn
1708 1.1 skrll && olddef
1709 1.1 skrll && h->def_dynamic)
1710 1.1 skrll {
1711 1.1 skrll /* Change the hash table entry to undefined, and let
1712 1.1 skrll _bfd_generic_link_add_one_symbol do the right thing with the
1713 1.1 skrll new definition. */
1714 1.1 skrll
1715 1.1 skrll h->root.type = bfd_link_hash_undefined;
1716 1.1 skrll h->root.u.undef.abfd = h->root.u.def.section->owner;
1717 1.1 skrll *size_change_ok = TRUE;
1718 1.1 skrll
1719 1.1 skrll olddef = FALSE;
1720 1.1 skrll olddyncommon = FALSE;
1721 1.1 skrll
1722 1.1 skrll /* We again permit a type change when a common symbol may be
1723 1.4 christos overriding a function. */
1724 1.4 christos
1725 1.4 christos if (bfd_is_com_section (sec))
1726 1.4 christos {
1727 1.4 christos if (oldfunc)
1728 1.4 christos {
1729 1.4 christos /* If a common symbol overrides a function, make sure
1730 1.4 christos that it isn't defined dynamically nor has type
1731 1.4 christos function. */
1732 1.4 christos h->def_dynamic = 0;
1733 1.4 christos h->type = STT_NOTYPE;
1734 1.1 skrll }
1735 1.9 christos *type_change_ok = TRUE;
1736 1.9 christos }
1737 1.1 skrll
1738 1.1 skrll if (hi->root.type == bfd_link_hash_indirect)
1739 1.1 skrll flip = hi;
1740 1.1 skrll else
1741 1.1 skrll /* This union may have been set to be non-NULL when this symbol
1742 1.1 skrll was seen in a dynamic object. We must force the union to be
1743 1.1 skrll NULL, so that it is correct for a regular symbol. */
1744 1.1 skrll h->verinfo.vertree = NULL;
1745 1.1 skrll }
1746 1.1 skrll
1747 1.1 skrll /* Handle the special case of a new common symbol merging with an
1748 1.1 skrll old symbol that looks like it might be a common symbol defined in
1749 1.1 skrll a shared object. Note that we have already handled the case in
1750 1.1 skrll which a new common symbol should simply override the definition
1751 1.1 skrll in the shared library. */
1752 1.1 skrll
1753 1.1 skrll if (! newdyn
1754 1.1 skrll && bfd_is_com_section (sec)
1755 1.1 skrll && olddyncommon)
1756 1.1 skrll {
1757 1.13 christos /* It would be best if we could set the hash table entry to a
1758 1.13 christos common symbol, but we don't know what to use for the section
1759 1.1 skrll or the alignment. */
1760 1.1 skrll (*info->callbacks->multiple_common) (info, &h->root, abfd,
1761 1.1 skrll bfd_link_hash_common, sym->st_size);
1762 1.1 skrll
1763 1.1 skrll /* If the presumed common symbol in the dynamic object is
1764 1.1 skrll larger, pretend that the new symbol has its size. */
1765 1.1 skrll
1766 1.1 skrll if (h->size > *pvalue)
1767 1.1 skrll *pvalue = h->size;
1768 1.1 skrll
1769 1.1 skrll /* We need to remember the alignment required by the symbol
1770 1.1 skrll in the dynamic object. */
1771 1.1 skrll BFD_ASSERT (pold_alignment);
1772 1.1 skrll *pold_alignment = h->root.u.def.section->alignment_power;
1773 1.1 skrll
1774 1.1 skrll olddef = FALSE;
1775 1.1 skrll olddyncommon = FALSE;
1776 1.1 skrll
1777 1.1 skrll h->root.type = bfd_link_hash_undefined;
1778 1.1 skrll h->root.u.undef.abfd = h->root.u.def.section->owner;
1779 1.1 skrll
1780 1.9 christos *size_change_ok = TRUE;
1781 1.9 christos *type_change_ok = TRUE;
1782 1.1 skrll
1783 1.1 skrll if (hi->root.type == bfd_link_hash_indirect)
1784 1.1 skrll flip = hi;
1785 1.1 skrll else
1786 1.1 skrll h->verinfo.vertree = NULL;
1787 1.1 skrll }
1788 1.1 skrll
1789 1.1 skrll if (flip != NULL)
1790 1.1 skrll {
1791 1.1 skrll /* Handle the case where we had a versioned symbol in a dynamic
1792 1.1 skrll library and now find a definition in a normal object. In this
1793 1.1 skrll case, we make the versioned symbol point to the normal one. */
1794 1.1 skrll flip->root.type = h->root.type;
1795 1.1 skrll flip->root.u.undef.abfd = h->root.u.undef.abfd;
1796 1.1 skrll h->root.type = bfd_link_hash_indirect;
1797 1.1 skrll h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1798 1.1 skrll (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1799 1.1 skrll if (h->def_dynamic)
1800 1.1 skrll {
1801 1.1 skrll h->def_dynamic = 0;
1802 1.1 skrll flip->ref_dynamic = 1;
1803 1.1 skrll }
1804 1.1 skrll }
1805 1.1 skrll
1806 1.1 skrll return TRUE;
1807 1.1 skrll }
1808 1.9 christos
1809 1.1 skrll /* This function is called to create an indirect symbol from the
1810 1.1 skrll default for the symbol with the default version if needed. The
1811 1.4 christos symbol is described by H, NAME, SYM, SEC, and VALUE. We
1812 1.1 skrll set DYNSYM if the new indirect symbol is dynamic. */
1813 1.1 skrll
1814 1.1 skrll static bfd_boolean
1815 1.1 skrll _bfd_elf_add_default_symbol (bfd *abfd,
1816 1.1 skrll struct bfd_link_info *info,
1817 1.9 christos struct elf_link_hash_entry *h,
1818 1.9 christos const char *name,
1819 1.9 christos Elf_Internal_Sym *sym,
1820 1.9 christos asection *sec,
1821 1.1 skrll bfd_vma value,
1822 1.1 skrll bfd **poldbfd,
1823 1.1 skrll bfd_boolean *dynsym)
1824 1.1 skrll {
1825 1.1 skrll bfd_boolean type_change_ok;
1826 1.1 skrll bfd_boolean size_change_ok;
1827 1.1 skrll bfd_boolean skip;
1828 1.1 skrll char *shortname;
1829 1.1 skrll struct elf_link_hash_entry *hi;
1830 1.1 skrll struct bfd_link_hash_entry *bh;
1831 1.9 christos const struct elf_backend_data *bed;
1832 1.1 skrll bfd_boolean collect;
1833 1.1 skrll bfd_boolean dynamic;
1834 1.9 christos bfd_boolean override;
1835 1.9 christos char *p;
1836 1.9 christos size_t len, shortlen;
1837 1.9 christos asection *tmp_sec;
1838 1.9 christos bfd_boolean matched;
1839 1.1 skrll
1840 1.1 skrll if (h->versioned == unversioned || h->versioned == versioned_hidden)
1841 1.1 skrll return TRUE;
1842 1.1 skrll
1843 1.1 skrll /* If this symbol has a version, and it is the default version, we
1844 1.1 skrll create an indirect symbol from the default name to the fully
1845 1.9 christos decorated name. This will cause external references which do not
1846 1.1 skrll specify a version to be bound to this version of the symbol. */
1847 1.9 christos p = strchr (name, ELF_VER_CHR);
1848 1.9 christos if (h->versioned == unknown)
1849 1.9 christos {
1850 1.9 christos if (p == NULL)
1851 1.9 christos {
1852 1.9 christos h->versioned = unversioned;
1853 1.1 skrll return TRUE;
1854 1.9 christos }
1855 1.9 christos else
1856 1.9 christos {
1857 1.9 christos if (p[1] != ELF_VER_CHR)
1858 1.9 christos {
1859 1.9 christos h->versioned = versioned_hidden;
1860 1.9 christos return TRUE;
1861 1.1 skrll }
1862 1.1 skrll else
1863 1.9 christos h->versioned = versioned;
1864 1.9 christos }
1865 1.9 christos }
1866 1.9 christos else
1867 1.9 christos {
1868 1.9 christos /* PR ld/19073: We may see an unversioned definition after the
1869 1.9 christos default version. */
1870 1.1 skrll if (p == NULL)
1871 1.1 skrll return TRUE;
1872 1.1 skrll }
1873 1.1 skrll
1874 1.1 skrll bed = get_elf_backend_data (abfd);
1875 1.1 skrll collect = bed->collect;
1876 1.4 christos dynamic = (abfd->flags & DYNAMIC) != 0;
1877 1.1 skrll
1878 1.1 skrll shortlen = p - name;
1879 1.1 skrll shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1880 1.1 skrll if (shortname == NULL)
1881 1.1 skrll return FALSE;
1882 1.1 skrll memcpy (shortname, name, shortlen);
1883 1.1 skrll shortname[shortlen] = '\0';
1884 1.1 skrll
1885 1.1 skrll /* We are going to create a new symbol. Merge it with any existing
1886 1.1 skrll symbol with this name. For the purposes of the merge, act as
1887 1.1 skrll though we were defining the symbol we just defined, although we
1888 1.9 christos actually going to define an indirect symbol. */
1889 1.9 christos type_change_ok = FALSE;
1890 1.9 christos size_change_ok = FALSE;
1891 1.9 christos matched = TRUE;
1892 1.9 christos tmp_sec = sec;
1893 1.1 skrll if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1894 1.1 skrll &hi, poldbfd, NULL, NULL, &skip, &override,
1895 1.1 skrll &type_change_ok, &size_change_ok, &matched))
1896 1.1 skrll return FALSE;
1897 1.1 skrll
1898 1.12 christos if (skip)
1899 1.12 christos goto nondefault;
1900 1.12 christos
1901 1.12 christos if (hi->def_regular)
1902 1.12 christos {
1903 1.12 christos /* If the undecorated symbol will have a version added by a
1904 1.12 christos script different to H, then don't indirect to/from the
1905 1.12 christos undecorated symbol. This isn't ideal because we may not yet
1906 1.12 christos have seen symbol versions, if given by a script on the
1907 1.12 christos command line rather than via --version-script. */
1908 1.12 christos if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1909 1.12 christos {
1910 1.12 christos bfd_boolean hide;
1911 1.12 christos
1912 1.12 christos hi->verinfo.vertree
1913 1.12 christos = bfd_find_version_for_sym (info->version_info,
1914 1.12 christos hi->root.root.string, &hide);
1915 1.12 christos if (hi->verinfo.vertree != NULL && hide)
1916 1.12 christos {
1917 1.12 christos (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1918 1.12 christos goto nondefault;
1919 1.12 christos }
1920 1.12 christos }
1921 1.12 christos if (hi->verinfo.vertree != NULL
1922 1.12 christos && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1923 1.1 skrll goto nondefault;
1924 1.1 skrll }
1925 1.9 christos
1926 1.9 christos if (! override)
1927 1.9 christos {
1928 1.9 christos /* Add the default symbol if not performing a relocatable link. */
1929 1.9 christos if (! bfd_link_relocatable (info))
1930 1.9 christos {
1931 1.9 christos bh = &hi->root;
1932 1.9 christos if (! (_bfd_generic_link_add_one_symbol
1933 1.9 christos (info, abfd, shortname, BSF_INDIRECT,
1934 1.9 christos bfd_ind_section_ptr,
1935 1.9 christos 0, name, FALSE, collect, &bh)))
1936 1.1 skrll return FALSE;
1937 1.1 skrll hi = (struct elf_link_hash_entry *) bh;
1938 1.1 skrll }
1939 1.1 skrll }
1940 1.1 skrll else
1941 1.1 skrll {
1942 1.1 skrll /* In this case the symbol named SHORTNAME is overriding the
1943 1.1 skrll indirect symbol we want to add. We were planning on making
1944 1.1 skrll SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1945 1.1 skrll is the name without a version. NAME is the fully versioned
1946 1.1 skrll name, and it is the default version.
1947 1.1 skrll
1948 1.1 skrll Overriding means that we already saw a definition for the
1949 1.1 skrll symbol SHORTNAME in a regular object, and it is overriding
1950 1.1 skrll the symbol defined in the dynamic object.
1951 1.1 skrll
1952 1.1 skrll When this happens, we actually want to change NAME, the
1953 1.1 skrll symbol we just added, to refer to SHORTNAME. This will cause
1954 1.1 skrll references to NAME in the shared object to become references
1955 1.1 skrll to SHORTNAME in the regular object. This is what we expect
1956 1.1 skrll when we override a function in a shared object: that the
1957 1.1 skrll references in the shared object will be mapped to the
1958 1.1 skrll definition in the regular object. */
1959 1.1 skrll
1960 1.1 skrll while (hi->root.type == bfd_link_hash_indirect
1961 1.1 skrll || hi->root.type == bfd_link_hash_warning)
1962 1.1 skrll hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1963 1.1 skrll
1964 1.1 skrll h->root.type = bfd_link_hash_indirect;
1965 1.1 skrll h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1966 1.1 skrll if (h->def_dynamic)
1967 1.1 skrll {
1968 1.1 skrll h->def_dynamic = 0;
1969 1.1 skrll hi->ref_dynamic = 1;
1970 1.1 skrll if (hi->ref_regular
1971 1.1 skrll || hi->def_regular)
1972 1.1 skrll {
1973 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1974 1.1 skrll return FALSE;
1975 1.1 skrll }
1976 1.1 skrll }
1977 1.1 skrll
1978 1.1 skrll /* Now set HI to H, so that the following code will set the
1979 1.1 skrll other fields correctly. */
1980 1.1 skrll hi = h;
1981 1.1 skrll }
1982 1.1 skrll
1983 1.1 skrll /* Check if HI is a warning symbol. */
1984 1.1 skrll if (hi->root.type == bfd_link_hash_warning)
1985 1.1 skrll hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1986 1.1 skrll
1987 1.1 skrll /* If there is a duplicate definition somewhere, then HI may not
1988 1.1 skrll point to an indirect symbol. We will have reported an error to
1989 1.1 skrll the user in that case. */
1990 1.1 skrll
1991 1.1 skrll if (hi->root.type == bfd_link_hash_indirect)
1992 1.1 skrll {
1993 1.1 skrll struct elf_link_hash_entry *ht;
1994 1.1 skrll
1995 1.9 christos ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1996 1.9 christos (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1997 1.9 christos
1998 1.9 christos /* A reference to the SHORTNAME symbol from a dynamic library
1999 1.9 christos will be satisfied by the versioned symbol at runtime. In
2000 1.9 christos effect, we have a reference to the versioned symbol. */
2001 1.1 skrll ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2002 1.1 skrll hi->dynamic_def |= ht->dynamic_def;
2003 1.1 skrll
2004 1.1 skrll /* See if the new flags lead us to realize that the symbol must
2005 1.1 skrll be dynamic. */
2006 1.1 skrll if (! *dynsym)
2007 1.9 christos {
2008 1.7 christos if (! dynamic)
2009 1.1 skrll {
2010 1.1 skrll if (! bfd_link_executable (info)
2011 1.1 skrll || hi->def_dynamic
2012 1.1 skrll || hi->ref_dynamic)
2013 1.1 skrll *dynsym = TRUE;
2014 1.1 skrll }
2015 1.1 skrll else
2016 1.1 skrll {
2017 1.1 skrll if (hi->ref_regular)
2018 1.1 skrll *dynsym = TRUE;
2019 1.1 skrll }
2020 1.1 skrll }
2021 1.1 skrll }
2022 1.1 skrll
2023 1.1 skrll /* We also need to define an indirection from the nondefault version
2024 1.1 skrll of the symbol. */
2025 1.4 christos
2026 1.1 skrll nondefault:
2027 1.1 skrll len = strlen (name);
2028 1.1 skrll shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2029 1.1 skrll if (shortname == NULL)
2030 1.1 skrll return FALSE;
2031 1.1 skrll memcpy (shortname, name, shortlen);
2032 1.1 skrll memcpy (shortname + shortlen, p + 1, len - shortlen);
2033 1.1 skrll
2034 1.9 christos /* Once again, merge with any existing symbol. */
2035 1.9 christos type_change_ok = FALSE;
2036 1.9 christos size_change_ok = FALSE;
2037 1.9 christos tmp_sec = sec;
2038 1.1 skrll if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2039 1.1 skrll &hi, poldbfd, NULL, NULL, &skip, &override,
2040 1.1 skrll &type_change_ok, &size_change_ok, &matched))
2041 1.1 skrll return FALSE;
2042 1.1 skrll
2043 1.1 skrll if (skip)
2044 1.1 skrll return TRUE;
2045 1.1 skrll
2046 1.1 skrll if (override)
2047 1.1 skrll {
2048 1.1 skrll /* Here SHORTNAME is a versioned name, so we don't expect to see
2049 1.1 skrll the type of override we do in the case above unless it is
2050 1.13.12.2 pgoyette overridden by a versioned definition. */
2051 1.13.12.2 pgoyette if (hi->root.type != bfd_link_hash_defined
2052 1.1 skrll && hi->root.type != bfd_link_hash_defweak)
2053 1.1 skrll _bfd_error_handler
2054 1.1 skrll /* xgettext:c-format */
2055 1.1 skrll (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2056 1.1 skrll abfd, shortname);
2057 1.1 skrll }
2058 1.1 skrll else
2059 1.1 skrll {
2060 1.1 skrll bh = &hi->root;
2061 1.1 skrll if (! (_bfd_generic_link_add_one_symbol
2062 1.1 skrll (info, abfd, shortname, BSF_INDIRECT,
2063 1.1 skrll bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2064 1.1 skrll return FALSE;
2065 1.1 skrll hi = (struct elf_link_hash_entry *) bh;
2066 1.1 skrll
2067 1.1 skrll /* If there is a duplicate definition somewhere, then HI may not
2068 1.1 skrll point to an indirect symbol. We will have reported an error
2069 1.1 skrll to the user in that case. */
2070 1.1 skrll
2071 1.9 christos if (hi->root.type == bfd_link_hash_indirect)
2072 1.9 christos {
2073 1.1 skrll (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2074 1.1 skrll h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2075 1.1 skrll hi->dynamic_def |= h->dynamic_def;
2076 1.1 skrll
2077 1.1 skrll /* See if the new flags lead us to realize that the symbol
2078 1.1 skrll must be dynamic. */
2079 1.1 skrll if (! *dynsym)
2080 1.9 christos {
2081 1.1 skrll if (! dynamic)
2082 1.1 skrll {
2083 1.1 skrll if (! bfd_link_executable (info)
2084 1.1 skrll || hi->ref_dynamic)
2085 1.1 skrll *dynsym = TRUE;
2086 1.1 skrll }
2087 1.1 skrll else
2088 1.1 skrll {
2089 1.1 skrll if (hi->ref_regular)
2090 1.1 skrll *dynsym = TRUE;
2091 1.1 skrll }
2092 1.1 skrll }
2093 1.1 skrll }
2094 1.1 skrll }
2095 1.1 skrll
2096 1.1 skrll return TRUE;
2097 1.1 skrll }
2098 1.1 skrll
2099 1.4 christos /* This routine is used to export all defined symbols into the dynamic
2101 1.1 skrll symbol table. It is called via elf_link_hash_traverse. */
2102 1.4 christos
2103 1.1 skrll static bfd_boolean
2104 1.1 skrll _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2105 1.1 skrll {
2106 1.1 skrll struct elf_info_failed *eif = (struct elf_info_failed *) data;
2107 1.1 skrll
2108 1.7 christos /* Ignore indirect symbols. These are added by the versioning code. */
2109 1.7 christos if (h->root.type == bfd_link_hash_indirect)
2110 1.7 christos return TRUE;
2111 1.1 skrll
2112 1.1 skrll /* Ignore this if we won't export it. */
2113 1.7 christos if (!eif->info->export_dynamic && !h->dynamic)
2114 1.7 christos return TRUE;
2115 1.7 christos
2116 1.1 skrll if (h->dynindx == -1
2117 1.7 christos && (h->def_regular || h->ref_regular)
2118 1.1 skrll && ! bfd_hide_sym_by_version (eif->info->version_info,
2119 1.7 christos h->root.root.string))
2120 1.7 christos {
2121 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2122 1.1 skrll {
2123 1.1 skrll eif->failed = TRUE;
2124 1.1 skrll return FALSE;
2125 1.1 skrll }
2126 1.1 skrll }
2127 1.1 skrll
2128 1.1 skrll return TRUE;
2129 1.1 skrll }
2130 1.1 skrll
2131 1.1 skrll /* Look through the symbols which are defined in other shared
2133 1.1 skrll libraries and referenced here. Update the list of version
2134 1.1 skrll dependencies. This will be put into the .gnu.version_r section.
2135 1.1 skrll This function is called via elf_link_hash_traverse. */
2136 1.4 christos
2137 1.1 skrll static bfd_boolean
2138 1.1 skrll _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2139 1.1 skrll void *data)
2140 1.1 skrll {
2141 1.1 skrll struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2142 1.1 skrll Elf_Internal_Verneed *t;
2143 1.1 skrll Elf_Internal_Vernaux *a;
2144 1.1 skrll bfd_size_type amt;
2145 1.1 skrll
2146 1.9 christos /* We only care about symbols defined in shared objects with version
2147 1.9 christos information. */
2148 1.9 christos if (!h->def_dynamic
2149 1.1 skrll || h->def_regular
2150 1.1 skrll || h->dynindx == -1
2151 1.1 skrll || h->verinfo.verdef == NULL
2152 1.4 christos || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2153 1.4 christos & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2154 1.4 christos return TRUE;
2155 1.1 skrll
2156 1.1 skrll /* See if we already know about this version. */
2157 1.1 skrll for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2158 1.1 skrll t != NULL;
2159 1.1 skrll t = t->vn_nextref)
2160 1.1 skrll {
2161 1.1 skrll if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2162 1.1 skrll continue;
2163 1.1 skrll
2164 1.1 skrll for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2165 1.1 skrll if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2166 1.1 skrll return TRUE;
2167 1.1 skrll
2168 1.1 skrll break;
2169 1.1 skrll }
2170 1.1 skrll
2171 1.4 christos /* This is a new version. Add it to tree we are building. */
2172 1.1 skrll
2173 1.1 skrll if (t == NULL)
2174 1.1 skrll {
2175 1.1 skrll amt = sizeof *t;
2176 1.1 skrll t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2177 1.1 skrll if (t == NULL)
2178 1.1 skrll {
2179 1.4 christos rinfo->failed = TRUE;
2180 1.4 christos return FALSE;
2181 1.1 skrll }
2182 1.1 skrll
2183 1.1 skrll t->vn_bfd = h->verinfo.verdef->vd_bfd;
2184 1.4 christos t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2185 1.1 skrll elf_tdata (rinfo->info->output_bfd)->verref = t;
2186 1.1 skrll }
2187 1.1 skrll
2188 1.1 skrll amt = sizeof *a;
2189 1.1 skrll a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2190 1.1 skrll if (a == NULL)
2191 1.1 skrll {
2192 1.1 skrll rinfo->failed = TRUE;
2193 1.1 skrll return FALSE;
2194 1.1 skrll }
2195 1.1 skrll
2196 1.1 skrll /* Note that we are copying a string pointer here, and testing it
2197 1.1 skrll above. If bfd_elf_string_from_elf_section is ever changed to
2198 1.1 skrll discard the string data when low in memory, this will have to be
2199 1.1 skrll fixed. */
2200 1.1 skrll a->vna_nodename = h->verinfo.verdef->vd_nodename;
2201 1.1 skrll
2202 1.1 skrll a->vna_flags = h->verinfo.verdef->vd_flags;
2203 1.1 skrll a->vna_nextptr = t->vn_auxptr;
2204 1.1 skrll
2205 1.1 skrll h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2206 1.1 skrll ++rinfo->vers;
2207 1.1 skrll
2208 1.1 skrll a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2209 1.1 skrll
2210 1.1 skrll t->vn_auxptr = a;
2211 1.1 skrll
2212 1.1 skrll return TRUE;
2213 1.1 skrll }
2214 1.1 skrll
2215 1.4 christos /* Figure out appropriate versions for all the symbols. We may not
2216 1.1 skrll have the version number script until we have read all of the input
2217 1.1 skrll files, so until that point we don't know which symbols should be
2218 1.4 christos local. This function is called via elf_link_hash_traverse. */
2219 1.1 skrll
2220 1.1 skrll static bfd_boolean
2221 1.1 skrll _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2222 1.1 skrll {
2223 1.1 skrll struct elf_info_failed *sinfo;
2224 1.4 christos struct bfd_link_info *info;
2225 1.1 skrll const struct elf_backend_data *bed;
2226 1.1 skrll struct elf_info_failed eif;
2227 1.1 skrll char *p;
2228 1.1 skrll
2229 1.1 skrll sinfo = (struct elf_info_failed *) data;
2230 1.1 skrll info = sinfo->info;
2231 1.1 skrll
2232 1.1 skrll /* Fix the symbol flags. */
2233 1.1 skrll eif.failed = FALSE;
2234 1.1 skrll eif.info = info;
2235 1.1 skrll if (! _bfd_elf_fix_symbol_flags (h, &eif))
2236 1.1 skrll {
2237 1.1 skrll if (eif.failed)
2238 1.1 skrll sinfo->failed = TRUE;
2239 1.1 skrll return FALSE;
2240 1.1 skrll }
2241 1.1 skrll
2242 1.4 christos /* We only need version numbers for symbols defined in regular
2243 1.1 skrll objects. */
2244 1.1 skrll if (!h->def_regular)
2245 1.1 skrll return TRUE;
2246 1.1 skrll
2247 1.1 skrll bed = get_elf_backend_data (info->output_bfd);
2248 1.1 skrll p = strchr (h->root.root.string, ELF_VER_CHR);
2249 1.1 skrll if (p != NULL && h->verinfo.vertree == NULL)
2250 1.9 christos {
2251 1.1 skrll struct bfd_elf_version_tree *t;
2252 1.1 skrll
2253 1.1 skrll ++p;
2254 1.9 christos if (*p == ELF_VER_CHR)
2255 1.1 skrll ++p;
2256 1.1 skrll
2257 1.7 christos /* If there is no version string, we can just return out. */
2258 1.1 skrll if (*p == '\0')
2259 1.1 skrll return TRUE;
2260 1.1 skrll
2261 1.1 skrll /* Look for the version. If we find it, it is no longer weak. */
2262 1.1 skrll for (t = sinfo->info->version_info; t != NULL; t = t->next)
2263 1.1 skrll {
2264 1.1 skrll if (strcmp (t->name, p) == 0)
2265 1.1 skrll {
2266 1.4 christos size_t len;
2267 1.1 skrll char *alc;
2268 1.1 skrll struct bfd_elf_version_expr *d;
2269 1.1 skrll
2270 1.1 skrll len = p - h->root.root.string;
2271 1.1 skrll alc = (char *) bfd_malloc (len);
2272 1.1 skrll if (alc == NULL)
2273 1.1 skrll {
2274 1.1 skrll sinfo->failed = TRUE;
2275 1.1 skrll return FALSE;
2276 1.1 skrll }
2277 1.1 skrll memcpy (alc, h->root.root.string, len - 1);
2278 1.1 skrll alc[len - 1] = '\0';
2279 1.1 skrll if (alc[len - 2] == ELF_VER_CHR)
2280 1.1 skrll alc[len - 2] = '\0';
2281 1.1 skrll
2282 1.1 skrll h->verinfo.vertree = t;
2283 1.1 skrll t->used = TRUE;
2284 1.1 skrll d = NULL;
2285 1.1 skrll
2286 1.1 skrll if (t->globals.list != NULL)
2287 1.1 skrll d = (*t->match) (&t->globals, NULL, alc);
2288 1.1 skrll
2289 1.1 skrll /* See if there is anything to force this symbol to
2290 1.1 skrll local scope. */
2291 1.1 skrll if (d == NULL && t->locals.list != NULL)
2292 1.1 skrll {
2293 1.1 skrll d = (*t->match) (&t->locals, NULL, alc);
2294 1.1 skrll if (d != NULL
2295 1.1 skrll && h->dynindx != -1
2296 1.1 skrll && ! info->export_dynamic)
2297 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2298 1.1 skrll }
2299 1.1 skrll
2300 1.1 skrll free (alc);
2301 1.1 skrll break;
2302 1.9 christos }
2303 1.1 skrll }
2304 1.1 skrll
2305 1.1 skrll /* If we are building an application, we need to create a
2306 1.1 skrll version node for this version. */
2307 1.1 skrll if (t == NULL && bfd_link_executable (info))
2308 1.1 skrll {
2309 1.1 skrll struct bfd_elf_version_tree **pp;
2310 1.1 skrll int version_index;
2311 1.1 skrll
2312 1.13 christos /* If we aren't going to export this symbol, we don't need
2313 1.13 christos to worry about it. */
2314 1.1 skrll if (h->dynindx == -1)
2315 1.1 skrll return TRUE;
2316 1.1 skrll
2317 1.1 skrll t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2318 1.1 skrll sizeof *t);
2319 1.1 skrll if (t == NULL)
2320 1.1 skrll {
2321 1.1 skrll sinfo->failed = TRUE;
2322 1.1 skrll return FALSE;
2323 1.1 skrll }
2324 1.1 skrll
2325 1.1 skrll t->name = p;
2326 1.7 christos t->name_indx = (unsigned int) -1;
2327 1.7 christos t->used = TRUE;
2328 1.1 skrll
2329 1.7 christos version_index = 1;
2330 1.7 christos /* Don't count anonymous version tag. */
2331 1.7 christos if (sinfo->info->version_info != NULL
2332 1.1 skrll && sinfo->info->version_info->vernum == 0)
2333 1.1 skrll version_index = 0;
2334 1.1 skrll for (pp = &sinfo->info->version_info;
2335 1.1 skrll *pp != NULL;
2336 1.1 skrll pp = &(*pp)->next)
2337 1.1 skrll ++version_index;
2338 1.1 skrll t->vernum = version_index;
2339 1.1 skrll
2340 1.1 skrll *pp = t;
2341 1.1 skrll
2342 1.1 skrll h->verinfo.vertree = t;
2343 1.13.12.2 pgoyette }
2344 1.13.12.2 pgoyette else if (t == NULL)
2345 1.1 skrll {
2346 1.4 christos /* We could not find the version for a symbol when
2347 1.1 skrll generating a shared archive. Return an error. */
2348 1.1 skrll _bfd_error_handler
2349 1.1 skrll /* xgettext:c-format */
2350 1.1 skrll (_("%B: version node not found for symbol %s"),
2351 1.1 skrll info->output_bfd, h->root.root.string);
2352 1.1 skrll bfd_set_error (bfd_error_bad_value);
2353 1.1 skrll sinfo->failed = TRUE;
2354 1.1 skrll return FALSE;
2355 1.7 christos }
2356 1.1 skrll }
2357 1.4 christos
2358 1.1 skrll /* If we don't have a version for this symbol, see if we can find
2359 1.7 christos something. */
2360 1.7 christos if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2361 1.7 christos {
2362 1.4 christos bfd_boolean hide;
2363 1.4 christos
2364 1.1 skrll h->verinfo.vertree
2365 1.1 skrll = bfd_find_version_for_sym (sinfo->info->version_info,
2366 1.1 skrll h->root.root.string, &hide);
2367 1.1 skrll if (h->verinfo.vertree != NULL && hide)
2368 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2369 1.1 skrll }
2370 1.1 skrll
2371 1.1 skrll return TRUE;
2372 1.1 skrll }
2373 1.1 skrll
2374 1.1 skrll /* Read and swap the relocs from the section indicated by SHDR. This
2376 1.1 skrll may be either a REL or a RELA section. The relocations are
2377 1.1 skrll translated into RELA relocations and stored in INTERNAL_RELOCS,
2378 1.1 skrll which should have already been allocated to contain enough space.
2379 1.1 skrll The EXTERNAL_RELOCS are a buffer where the external form of the
2380 1.1 skrll relocations should be stored.
2381 1.1 skrll
2382 1.1 skrll Returns FALSE if something goes wrong. */
2383 1.1 skrll
2384 1.1 skrll static bfd_boolean
2385 1.1 skrll elf_link_read_relocs_from_section (bfd *abfd,
2386 1.1 skrll asection *sec,
2387 1.1 skrll Elf_Internal_Shdr *shdr,
2388 1.1 skrll void *external_relocs,
2389 1.1 skrll Elf_Internal_Rela *internal_relocs)
2390 1.1 skrll {
2391 1.1 skrll const struct elf_backend_data *bed;
2392 1.1 skrll void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2393 1.1 skrll const bfd_byte *erela;
2394 1.1 skrll const bfd_byte *erelaend;
2395 1.1 skrll Elf_Internal_Rela *irela;
2396 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
2397 1.1 skrll size_t nsyms;
2398 1.1 skrll
2399 1.1 skrll /* Position ourselves at the start of the section. */
2400 1.1 skrll if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2401 1.1 skrll return FALSE;
2402 1.4 christos
2403 1.1 skrll /* Read the relocations. */
2404 1.1 skrll if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2405 1.1 skrll return FALSE;
2406 1.1 skrll
2407 1.1 skrll symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2408 1.1 skrll nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2409 1.1 skrll
2410 1.1 skrll bed = get_elf_backend_data (abfd);
2411 1.1 skrll
2412 1.1 skrll /* Convert the external relocations to the internal format. */
2413 1.1 skrll if (shdr->sh_entsize == bed->s->sizeof_rel)
2414 1.1 skrll swap_in = bed->s->swap_reloc_in;
2415 1.1 skrll else if (shdr->sh_entsize == bed->s->sizeof_rela)
2416 1.1 skrll swap_in = bed->s->swap_reloca_in;
2417 1.4 christos else
2418 1.1 skrll {
2419 1.1 skrll bfd_set_error (bfd_error_wrong_format);
2420 1.1 skrll return FALSE;
2421 1.1 skrll }
2422 1.1 skrll
2423 1.1 skrll erela = (const bfd_byte *) external_relocs;
2424 1.1 skrll erelaend = erela + shdr->sh_size;
2425 1.1 skrll irela = internal_relocs;
2426 1.1 skrll while (erela < erelaend)
2427 1.1 skrll {
2428 1.4 christos bfd_vma r_symndx;
2429 1.4 christos
2430 1.4 christos (*swap_in) (abfd, erela, irela);
2431 1.4 christos r_symndx = ELF32_R_SYM (irela->r_info);
2432 1.13.12.2 pgoyette if (bed->s->arch_size == 64)
2433 1.13.12.2 pgoyette r_symndx >>= 24;
2434 1.13.12.2 pgoyette if (nsyms > 0)
2435 1.13.12.2 pgoyette {
2436 1.13.12.2 pgoyette if ((size_t) r_symndx >= nsyms)
2437 1.13.12.2 pgoyette {
2438 1.4 christos _bfd_error_handler
2439 1.4 christos /* xgettext:c-format */
2440 1.4 christos (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2441 1.4 christos " for offset %#Lx in section `%A'"),
2442 1.4 christos abfd, r_symndx, (unsigned long) nsyms,
2443 1.1 skrll irela->r_offset, sec);
2444 1.13.12.2 pgoyette bfd_set_error (bfd_error_bad_value);
2445 1.13.12.2 pgoyette return FALSE;
2446 1.13.12.2 pgoyette }
2447 1.13.12.2 pgoyette }
2448 1.4 christos else if (r_symndx != STN_UNDEF)
2449 1.13.12.2 pgoyette {
2450 1.13.12.2 pgoyette _bfd_error_handler
2451 1.1 skrll /* xgettext:c-format */
2452 1.1 skrll (_("%B: non-zero symbol index (%#Lx)"
2453 1.1 skrll " for offset %#Lx in section `%A'"
2454 1.1 skrll " when the object file has no symbol table"),
2455 1.1 skrll abfd, r_symndx,
2456 1.1 skrll irela->r_offset, sec);
2457 1.1 skrll bfd_set_error (bfd_error_bad_value);
2458 1.1 skrll return FALSE;
2459 1.1 skrll }
2460 1.1 skrll irela += bed->s->int_rels_per_ext_rel;
2461 1.1 skrll erela += shdr->sh_entsize;
2462 1.1 skrll }
2463 1.1 skrll
2464 1.1 skrll return TRUE;
2465 1.1 skrll }
2466 1.1 skrll
2467 1.1 skrll /* Read and swap the relocs for a section O. They may have been
2468 1.1 skrll cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2469 1.4 christos not NULL, they are used as buffers to read into. They are known to
2470 1.1 skrll be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2471 1.1 skrll the return value is allocated using either malloc or bfd_alloc,
2472 1.1 skrll according to the KEEP_MEMORY argument. If O has two relocation
2473 1.1 skrll sections (both REL and RELA relocations), then the REL_HDR
2474 1.1 skrll relocations will appear first in INTERNAL_RELOCS, followed by the
2475 1.1 skrll RELA_HDR relocations. */
2476 1.1 skrll
2477 1.1 skrll Elf_Internal_Rela *
2478 1.1 skrll _bfd_elf_link_read_relocs (bfd *abfd,
2479 1.1 skrll asection *o,
2480 1.1 skrll void *external_relocs,
2481 1.4 christos Elf_Internal_Rela *internal_relocs,
2482 1.4 christos bfd_boolean keep_memory)
2483 1.1 skrll {
2484 1.4 christos void *alloc1 = NULL;
2485 1.4 christos Elf_Internal_Rela *alloc2 = NULL;
2486 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2487 1.1 skrll struct bfd_elf_section_data *esdo = elf_section_data (o);
2488 1.1 skrll Elf_Internal_Rela *internal_rela_relocs;
2489 1.1 skrll
2490 1.1 skrll if (esdo->relocs != NULL)
2491 1.1 skrll return esdo->relocs;
2492 1.1 skrll
2493 1.1 skrll if (o->reloc_count == 0)
2494 1.13.12.2 pgoyette return NULL;
2495 1.1 skrll
2496 1.4 christos if (internal_relocs == NULL)
2497 1.1 skrll {
2498 1.4 christos bfd_size_type size;
2499 1.1 skrll
2500 1.1 skrll size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2501 1.1 skrll if (keep_memory)
2502 1.1 skrll internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2503 1.1 skrll else
2504 1.1 skrll internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2505 1.4 christos if (internal_relocs == NULL)
2506 1.4 christos goto error_return;
2507 1.4 christos }
2508 1.4 christos
2509 1.4 christos if (external_relocs == NULL)
2510 1.4 christos {
2511 1.1 skrll bfd_size_type size = 0;
2512 1.1 skrll
2513 1.1 skrll if (esdo->rel.hdr)
2514 1.1 skrll size += esdo->rel.hdr->sh_size;
2515 1.1 skrll if (esdo->rela.hdr)
2516 1.1 skrll size += esdo->rela.hdr->sh_size;
2517 1.1 skrll
2518 1.4 christos alloc1 = bfd_malloc (size);
2519 1.4 christos if (alloc1 == NULL)
2520 1.4 christos goto error_return;
2521 1.4 christos external_relocs = alloc1;
2522 1.4 christos }
2523 1.4 christos
2524 1.4 christos internal_rela_relocs = internal_relocs;
2525 1.4 christos if (esdo->rel.hdr)
2526 1.4 christos {
2527 1.4 christos if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2528 1.4 christos external_relocs,
2529 1.4 christos internal_relocs))
2530 1.4 christos goto error_return;
2531 1.4 christos external_relocs = (((bfd_byte *) external_relocs)
2532 1.4 christos + esdo->rel.hdr->sh_size);
2533 1.4 christos internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2534 1.4 christos * bed->s->int_rels_per_ext_rel);
2535 1.1 skrll }
2536 1.1 skrll
2537 1.1 skrll if (esdo->rela.hdr
2538 1.1 skrll && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2539 1.4 christos external_relocs,
2540 1.1 skrll internal_rela_relocs)))
2541 1.1 skrll goto error_return;
2542 1.1 skrll
2543 1.1 skrll /* Cache the results for next time, if we can. */
2544 1.1 skrll if (keep_memory)
2545 1.1 skrll esdo->relocs = internal_relocs;
2546 1.1 skrll
2547 1.1 skrll if (alloc1 != NULL)
2548 1.1 skrll free (alloc1);
2549 1.1 skrll
2550 1.1 skrll /* Don't free alloc2, since if it was allocated we are passing it
2551 1.1 skrll back (under the name of internal_relocs). */
2552 1.1 skrll
2553 1.1 skrll return internal_relocs;
2554 1.1 skrll
2555 1.1 skrll error_return:
2556 1.1 skrll if (alloc1 != NULL)
2557 1.1 skrll free (alloc1);
2558 1.1 skrll if (alloc2 != NULL)
2559 1.1 skrll {
2560 1.1 skrll if (keep_memory)
2561 1.1 skrll bfd_release (abfd, alloc2);
2562 1.1 skrll else
2563 1.1 skrll free (alloc2);
2564 1.1 skrll }
2565 1.4 christos return NULL;
2566 1.1 skrll }
2567 1.4 christos
2568 1.1 skrll /* Compute the size of, and allocate space for, REL_HDR which is the
2569 1.4 christos section header for a section containing relocations for O. */
2570 1.1 skrll
2571 1.1 skrll static bfd_boolean
2572 1.4 christos _bfd_elf_link_size_reloc_section (bfd *abfd,
2573 1.1 skrll struct bfd_elf_section_reloc_data *reldata)
2574 1.1 skrll {
2575 1.1 skrll Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2576 1.1 skrll
2577 1.1 skrll /* That allows us to calculate the size of the section. */
2578 1.4 christos rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2579 1.1 skrll
2580 1.1 skrll /* The contents field must last into write_object_contents, so we
2581 1.1 skrll allocate it with bfd_alloc rather than malloc. Also since we
2582 1.4 christos cannot be sure that the contents will actually be filled in,
2583 1.1 skrll we zero the allocated space. */
2584 1.1 skrll rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2585 1.1 skrll if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2586 1.9 christos return FALSE;
2587 1.9 christos
2588 1.1 skrll if (reldata->hashes == NULL && reldata->count)
2589 1.1 skrll {
2590 1.1 skrll struct elf_link_hash_entry **p;
2591 1.4 christos
2592 1.1 skrll p = ((struct elf_link_hash_entry **)
2593 1.1 skrll bfd_zmalloc (reldata->count * sizeof (*p)));
2594 1.1 skrll if (p == NULL)
2595 1.1 skrll return FALSE;
2596 1.1 skrll
2597 1.1 skrll reldata->hashes = p;
2598 1.1 skrll }
2599 1.1 skrll
2600 1.1 skrll return TRUE;
2601 1.1 skrll }
2602 1.1 skrll
2603 1.1 skrll /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2604 1.1 skrll originated from the section given by INPUT_REL_HDR) to the
2605 1.1 skrll OUTPUT_BFD. */
2606 1.1 skrll
2607 1.1 skrll bfd_boolean
2608 1.1 skrll _bfd_elf_link_output_relocs (bfd *output_bfd,
2609 1.1 skrll asection *input_section,
2610 1.1 skrll Elf_Internal_Shdr *input_rel_hdr,
2611 1.1 skrll Elf_Internal_Rela *internal_relocs,
2612 1.4 christos struct elf_link_hash_entry **rel_hash
2613 1.1 skrll ATTRIBUTE_UNUSED)
2614 1.1 skrll {
2615 1.1 skrll Elf_Internal_Rela *irela;
2616 1.4 christos Elf_Internal_Rela *irelaend;
2617 1.1 skrll bfd_byte *erel;
2618 1.1 skrll struct bfd_elf_section_reloc_data *output_reldata;
2619 1.1 skrll asection *output_section;
2620 1.4 christos const struct elf_backend_data *bed;
2621 1.4 christos void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2622 1.4 christos struct bfd_elf_section_data *esdo;
2623 1.1 skrll
2624 1.4 christos output_section = input_section->output_section;
2625 1.4 christos
2626 1.1 skrll bed = get_elf_backend_data (output_bfd);
2627 1.4 christos esdo = elf_section_data (output_section);
2628 1.4 christos if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2629 1.1 skrll {
2630 1.4 christos output_reldata = &esdo->rel;
2631 1.4 christos swap_out = bed->s->swap_reloc_out;
2632 1.1 skrll }
2633 1.1 skrll else if (esdo->rela.hdr
2634 1.1 skrll && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2635 1.13.12.2 pgoyette {
2636 1.13.12.2 pgoyette output_reldata = &esdo->rela;
2637 1.1 skrll swap_out = bed->s->swap_reloca_out;
2638 1.1 skrll }
2639 1.1 skrll else
2640 1.1 skrll {
2641 1.1 skrll _bfd_error_handler
2642 1.1 skrll /* xgettext:c-format */
2643 1.4 christos (_("%B: relocation size mismatch in %B section %A"),
2644 1.4 christos output_bfd, input_section->owner, input_section);
2645 1.1 skrll bfd_set_error (bfd_error_wrong_format);
2646 1.1 skrll return FALSE;
2647 1.1 skrll }
2648 1.1 skrll
2649 1.1 skrll erel = output_reldata->hdr->contents;
2650 1.1 skrll erel += output_reldata->count * input_rel_hdr->sh_entsize;
2651 1.1 skrll irela = internal_relocs;
2652 1.1 skrll irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2653 1.1 skrll * bed->s->int_rels_per_ext_rel);
2654 1.1 skrll while (irela < irelaend)
2655 1.1 skrll {
2656 1.1 skrll (*swap_out) (output_bfd, irela, erel);
2657 1.4 christos irela += bed->s->int_rels_per_ext_rel;
2658 1.1 skrll erel += input_rel_hdr->sh_entsize;
2659 1.1 skrll }
2660 1.1 skrll
2661 1.1 skrll /* Bump the counter, so that we know where to add the next set of
2662 1.1 skrll relocations. */
2663 1.1 skrll output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2664 1.1 skrll
2665 1.1 skrll return TRUE;
2666 1.1 skrll }
2667 1.1 skrll
2668 1.9 christos /* Make weak undefined symbols in PIE dynamic. */
2670 1.1 skrll
2671 1.1 skrll bfd_boolean
2672 1.1 skrll _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2673 1.1 skrll struct elf_link_hash_entry *h)
2674 1.1 skrll {
2675 1.1 skrll if (bfd_link_pie (info)
2676 1.1 skrll && h->dynindx == -1
2677 1.1 skrll && h->root.type == bfd_link_hash_undefweak)
2678 1.1 skrll return bfd_elf_link_record_dynamic_symbol (info, h);
2679 1.1 skrll
2680 1.1 skrll return TRUE;
2681 1.1 skrll }
2682 1.4 christos
2683 1.1 skrll /* Fix up the flags for a symbol. This handles various cases which
2684 1.1 skrll can only be fixed after all the input files are seen. This is
2685 1.1 skrll currently called by both adjust_dynamic_symbol and
2686 1.1 skrll assign_sym_version, which is unnecessary but perhaps more robust in
2687 1.1 skrll the face of future changes. */
2688 1.1 skrll
2689 1.1 skrll static bfd_boolean
2690 1.1 skrll _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2691 1.1 skrll struct elf_info_failed *eif)
2692 1.1 skrll {
2693 1.1 skrll const struct elf_backend_data *bed;
2694 1.1 skrll
2695 1.1 skrll /* If this symbol was mentioned in a non-ELF file, try to set
2696 1.1 skrll DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2697 1.1 skrll permit a non-ELF file to correctly refer to a symbol defined in
2698 1.1 skrll an ELF dynamic object. */
2699 1.1 skrll if (h->non_elf)
2700 1.1 skrll {
2701 1.1 skrll while (h->root.type == bfd_link_hash_indirect)
2702 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
2703 1.1 skrll
2704 1.1 skrll if (h->root.type != bfd_link_hash_defined
2705 1.1 skrll && h->root.type != bfd_link_hash_defweak)
2706 1.1 skrll {
2707 1.1 skrll h->ref_regular = 1;
2708 1.1 skrll h->ref_regular_nonweak = 1;
2709 1.1 skrll }
2710 1.1 skrll else
2711 1.1 skrll {
2712 1.1 skrll if (h->root.u.def.section->owner != NULL
2713 1.1 skrll && (bfd_get_flavour (h->root.u.def.section->owner)
2714 1.1 skrll == bfd_target_elf_flavour))
2715 1.1 skrll {
2716 1.1 skrll h->ref_regular = 1;
2717 1.1 skrll h->ref_regular_nonweak = 1;
2718 1.1 skrll }
2719 1.1 skrll else
2720 1.1 skrll h->def_regular = 1;
2721 1.1 skrll }
2722 1.1 skrll
2723 1.1 skrll if (h->dynindx == -1
2724 1.1 skrll && (h->def_dynamic
2725 1.1 skrll || h->ref_dynamic))
2726 1.1 skrll {
2727 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2728 1.1 skrll {
2729 1.1 skrll eif->failed = TRUE;
2730 1.1 skrll return FALSE;
2731 1.1 skrll }
2732 1.1 skrll }
2733 1.1 skrll }
2734 1.1 skrll else
2735 1.1 skrll {
2736 1.1 skrll /* Unfortunately, NON_ELF is only correct if the symbol
2737 1.1 skrll was first seen in a non-ELF file. Fortunately, if the symbol
2738 1.1 skrll was first seen in an ELF file, we're probably OK unless the
2739 1.1 skrll symbol was defined in a non-ELF file. Catch that case here.
2740 1.1 skrll FIXME: We're still in trouble if the symbol was first seen in
2741 1.1 skrll a dynamic object, and then later in a non-ELF regular object. */
2742 1.1 skrll if ((h->root.type == bfd_link_hash_defined
2743 1.1 skrll || h->root.type == bfd_link_hash_defweak)
2744 1.1 skrll && !h->def_regular
2745 1.1 skrll && (h->root.u.def.section->owner != NULL
2746 1.1 skrll ? (bfd_get_flavour (h->root.u.def.section->owner)
2747 1.1 skrll != bfd_target_elf_flavour)
2748 1.1 skrll : (bfd_is_abs_section (h->root.u.def.section)
2749 1.1 skrll && !h->def_dynamic)))
2750 1.1 skrll h->def_regular = 1;
2751 1.1 skrll }
2752 1.1 skrll
2753 1.1 skrll /* Backend specific symbol fixup. */
2754 1.1 skrll bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2755 1.1 skrll if (bed->elf_backend_fixup_symbol
2756 1.1 skrll && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2757 1.1 skrll return FALSE;
2758 1.1 skrll
2759 1.1 skrll /* If this is a final link, and the symbol was defined as a common
2760 1.1 skrll symbol in a regular object file, and there was no definition in
2761 1.9 christos any dynamic object, then the linker will have allocated space for
2762 1.1 skrll the symbol in a common section but the DEF_REGULAR
2763 1.1 skrll flag will not have been set. */
2764 1.13.12.2 pgoyette if (h->root.type == bfd_link_hash_defined
2765 1.13.12.2 pgoyette && !h->def_regular
2766 1.13.12.2 pgoyette && h->ref_regular
2767 1.13.12.2 pgoyette && !h->def_dynamic
2768 1.13.12.2 pgoyette && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2769 1.13.12.2 pgoyette h->def_regular = 1;
2770 1.13.12.2 pgoyette
2771 1.13.12.2 pgoyette /* If a weak undefined symbol has non-default visibility, we also
2772 1.13.12.2 pgoyette hide it from the dynamic linker. */
2773 1.13.12.2 pgoyette if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2774 1.13.12.2 pgoyette && h->root.type == bfd_link_hash_undefweak)
2775 1.13.12.2 pgoyette (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2776 1.13.12.2 pgoyette
2777 1.13.12.2 pgoyette /* A hidden versioned symbol in executable should be forced local if
2778 1.13.12.2 pgoyette it is is locally defined, not referenced by shared library and not
2779 1.13.12.2 pgoyette exported. */
2780 1.13.12.2 pgoyette else if (bfd_link_executable (eif->info)
2781 1.1 skrll && h->versioned == versioned_hidden
2782 1.1 skrll && !eif->info->export_dynamic
2783 1.1 skrll && !h->dynamic
2784 1.1 skrll && !h->ref_dynamic
2785 1.1 skrll && h->def_regular)
2786 1.1 skrll (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2787 1.13.12.2 pgoyette
2788 1.13.12.2 pgoyette /* If -Bsymbolic was used (which means to bind references to global
2789 1.13.12.2 pgoyette symbols to the definition within the shared object), and this
2790 1.13.12.2 pgoyette symbol was defined in a regular object, then it actually doesn't
2791 1.13.12.2 pgoyette need a PLT entry. Likewise, if the symbol has non-default
2792 1.13.12.2 pgoyette visibility. If the symbol has hidden or internal visibility, we
2793 1.1 skrll will force it local. */
2794 1.1 skrll else if (h->needs_plt
2795 1.1 skrll && bfd_link_pic (eif->info)
2796 1.1 skrll && is_elf_hash_table (eif->info->hash)
2797 1.1 skrll && (SYMBOLIC_BIND (eif->info, h)
2798 1.1 skrll || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2799 1.1 skrll && h->def_regular)
2800 1.1 skrll {
2801 1.1 skrll bfd_boolean force_local;
2802 1.1 skrll
2803 1.1 skrll force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2804 1.13.12.2 pgoyette || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2805 1.1 skrll (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2806 1.13.12.2 pgoyette }
2807 1.13.12.3 pgoyette
2808 1.13.12.3 pgoyette /* If this is a weak defined symbol in a dynamic object, and we know
2809 1.13.12.1 pgoyette the real definition in the dynamic object, copy interesting flags
2810 1.1 skrll over to the real definition. */
2811 1.1 skrll if (h->is_weakalias)
2812 1.1 skrll {
2813 1.13.12.2 pgoyette struct elf_link_hash_entry *def = weakdef (h);
2814 1.13.12.2 pgoyette while (def->root.type == bfd_link_hash_indirect)
2815 1.13.12.2 pgoyette def = (struct elf_link_hash_entry *) def->root.u.i.link;
2816 1.13.12.2 pgoyette
2817 1.13.12.2 pgoyette /* If the real definition is defined by a regular object file,
2818 1.13.12.2 pgoyette don't do anything special. See the longer description in
2819 1.1 skrll _bfd_elf_adjust_dynamic_symbol, below. */
2820 1.1 skrll if (def->def_regular)
2821 1.7 christos {
2822 1.7 christos h = def;
2823 1.7 christos while ((h = h->u.alias) != def)
2824 1.7 christos h->is_weakalias = 0;
2825 1.13.12.2 pgoyette }
2826 1.13.12.2 pgoyette else
2827 1.13.12.2 pgoyette {
2828 1.1 skrll while (h->root.type == bfd_link_hash_indirect)
2829 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
2830 1.1 skrll BFD_ASSERT (h->root.type == bfd_link_hash_defined
2831 1.1 skrll || h->root.type == bfd_link_hash_defweak);
2832 1.1 skrll BFD_ASSERT (def->def_dynamic);
2833 1.1 skrll BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2834 1.1 skrll (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2835 1.1 skrll }
2836 1.1 skrll }
2837 1.1 skrll
2838 1.4 christos return TRUE;
2839 1.1 skrll }
2840 1.1 skrll
2841 1.4 christos /* Make the backend pick a good value for a dynamic symbol. This is
2842 1.13.12.2 pgoyette called via elf_link_hash_traverse, and also calls itself
2843 1.1 skrll recursively. */
2844 1.1 skrll
2845 1.1 skrll static bfd_boolean
2846 1.1 skrll _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2847 1.1 skrll {
2848 1.1 skrll struct elf_info_failed *eif = (struct elf_info_failed *) data;
2849 1.1 skrll struct elf_link_hash_table *htab;
2850 1.1 skrll const struct elf_backend_data *bed;
2851 1.1 skrll
2852 1.1 skrll if (! is_elf_hash_table (eif->info->hash))
2853 1.1 skrll return FALSE;
2854 1.1 skrll
2855 1.1 skrll /* Ignore indirect symbols. These are added by the versioning code. */
2856 1.13.12.2 pgoyette if (h->root.type == bfd_link_hash_indirect)
2857 1.13.12.2 pgoyette return TRUE;
2858 1.13.12.2 pgoyette
2859 1.13.12.2 pgoyette /* Fix the symbol flags. */
2860 1.13.12.2 pgoyette if (! _bfd_elf_fix_symbol_flags (h, eif))
2861 1.13.12.2 pgoyette return FALSE;
2862 1.13.12.2 pgoyette
2863 1.13.12.2 pgoyette htab = elf_hash_table (eif->info);
2864 1.13.12.2 pgoyette bed = get_elf_backend_data (htab->dynobj);
2865 1.13.12.2 pgoyette
2866 1.13.12.2 pgoyette if (h->root.type == bfd_link_hash_undefweak)
2867 1.13.12.2 pgoyette {
2868 1.13.12.2 pgoyette if (eif->info->dynamic_undefined_weak == 0)
2869 1.13.12.2 pgoyette (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2870 1.13.12.2 pgoyette else if (eif->info->dynamic_undefined_weak > 0
2871 1.13.12.2 pgoyette && h->ref_regular
2872 1.13.12.2 pgoyette && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2873 1.13.12.2 pgoyette && !bfd_hide_sym_by_version (eif->info->version_info,
2874 1.13.12.2 pgoyette h->root.root.string))
2875 1.13.12.2 pgoyette {
2876 1.13.12.2 pgoyette if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2877 1.1 skrll {
2878 1.1 skrll eif->failed = TRUE;
2879 1.1 skrll return FALSE;
2880 1.1 skrll }
2881 1.1 skrll }
2882 1.1 skrll }
2883 1.1 skrll
2884 1.1 skrll /* If this symbol does not require a PLT entry, and it is not
2885 1.4 christos defined by a dynamic object, or is not referenced by a regular
2886 1.1 skrll object, ignore it. We do have to handle a weak defined symbol,
2887 1.1 skrll even if no regular object refers to it, if we decided to add it
2888 1.1 skrll to the dynamic symbol table. FIXME: Do we normally need to worry
2889 1.13.12.2 pgoyette about symbols which are defined by one dynamic object and
2890 1.1 skrll referenced by another one? */
2891 1.1 skrll if (!h->needs_plt
2892 1.1 skrll && h->type != STT_GNU_IFUNC
2893 1.1 skrll && (h->def_regular
2894 1.1 skrll || !h->def_dynamic
2895 1.1 skrll || (!h->ref_regular
2896 1.1 skrll && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2897 1.1 skrll {
2898 1.1 skrll h->plt = elf_hash_table (eif->info)->init_plt_offset;
2899 1.1 skrll return TRUE;
2900 1.1 skrll }
2901 1.1 skrll
2902 1.1 skrll /* If we've already adjusted this symbol, don't do it again. This
2903 1.1 skrll can happen via a recursive call. */
2904 1.1 skrll if (h->dynamic_adjusted)
2905 1.1 skrll return TRUE;
2906 1.1 skrll
2907 1.1 skrll /* Don't look at this symbol again. Note that we must set this
2908 1.1 skrll after checking the above conditions, because we may look at a
2909 1.1 skrll symbol once, decide not to do anything, and then get called
2910 1.1 skrll recursively later after REF_REGULAR is set below. */
2911 1.1 skrll h->dynamic_adjusted = 1;
2912 1.1 skrll
2913 1.1 skrll /* If this is a weak definition, and we know a real definition, and
2914 1.1 skrll the real symbol is not itself defined by a regular object file,
2915 1.1 skrll then get a good value for the real definition. We handle the
2916 1.1 skrll real symbol first, for the convenience of the backend routine.
2917 1.1 skrll
2918 1.1 skrll Note that there is a confusing case here. If the real definition
2919 1.1 skrll is defined by a regular object file, we don't get the real symbol
2920 1.1 skrll from the dynamic object, but we do get the weak symbol. If the
2921 1.1 skrll processor backend uses a COPY reloc, then if some routine in the
2922 1.1 skrll dynamic object changes the real symbol, we will not see that
2923 1.1 skrll change in the corresponding weak symbol. This is the way other
2924 1.1 skrll ELF linkers work as well, and seems to be a result of the shared
2925 1.1 skrll library model.
2926 1.1 skrll
2927 1.1 skrll I will clarify this issue. Most SVR4 shared libraries define the
2928 1.1 skrll variable _timezone and define timezone as a weak synonym. The
2929 1.1 skrll tzset call changes _timezone. If you write
2930 1.1 skrll extern int timezone;
2931 1.1 skrll int _timezone = 5;
2932 1.1 skrll int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2933 1.1 skrll you might expect that, since timezone is a synonym for _timezone,
2934 1.13.12.2 pgoyette the same number will print both times. However, if the processor
2935 1.1 skrll backend uses a COPY reloc, then actually timezone will be copied
2936 1.13.12.2 pgoyette into your process image, and, since you define _timezone
2937 1.13.12.2 pgoyette yourself, _timezone will not. Thus timezone and _timezone will
2938 1.7 christos wind up at different memory locations. The tzset call will set
2939 1.13.12.2 pgoyette _timezone, leaving timezone unchanged. */
2940 1.13.12.2 pgoyette
2941 1.1 skrll if (h->is_weakalias)
2942 1.7 christos {
2943 1.13.12.2 pgoyette struct elf_link_hash_entry *def = weakdef (h);
2944 1.13.12.2 pgoyette
2945 1.1 skrll /* If we get to this point, there is an implicit reference to
2946 1.1 skrll the alias by a regular object file via the weak symbol H. */
2947 1.1 skrll def->ref_regular = 1;
2948 1.1 skrll
2949 1.1 skrll /* Ensure that the backend adjust_dynamic_symbol function sees
2950 1.1 skrll the strong alias before H by recursively calling ourselves. */
2951 1.1 skrll if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2952 1.1 skrll return FALSE;
2953 1.1 skrll }
2954 1.1 skrll
2955 1.1 skrll /* If a symbol has no type and no size and does not require a PLT
2956 1.13.12.2 pgoyette entry, then we are probably about to do the wrong thing here: we
2957 1.1 skrll are probably going to create a COPY reloc for an empty object.
2958 1.1 skrll This case can arise when a shared object is built with assembly
2959 1.1 skrll code, and the assembly code fails to set the symbol type. */
2960 1.1 skrll if (h->size == 0
2961 1.1 skrll && h->type == STT_NOTYPE
2962 1.1 skrll && !h->needs_plt)
2963 1.1 skrll _bfd_error_handler
2964 1.1 skrll (_("warning: type and size of dynamic symbol `%s' are not defined"),
2965 1.1 skrll h->root.root.string);
2966 1.1 skrll
2967 1.1 skrll if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2968 1.1 skrll {
2969 1.1 skrll eif->failed = TRUE;
2970 1.1 skrll return FALSE;
2971 1.1 skrll }
2972 1.1 skrll
2973 1.9 christos return TRUE;
2974 1.9 christos }
2975 1.1 skrll
2976 1.1 skrll /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2977 1.1 skrll DYNBSS. */
2978 1.1 skrll
2979 1.1 skrll bfd_boolean
2980 1.1 skrll _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2981 1.13.12.2 pgoyette struct elf_link_hash_entry *h,
2982 1.1 skrll asection *dynbss)
2983 1.1 skrll {
2984 1.1 skrll unsigned int power_of_two;
2985 1.1 skrll bfd_vma mask;
2986 1.1 skrll asection *sec = h->root.u.def.section;
2987 1.1 skrll
2988 1.1 skrll /* The section alignment of the definition is the maximum alignment
2989 1.1 skrll requirement of symbols defined in the section. Since we don't
2990 1.1 skrll know the symbol alignment requirement, we start with the
2991 1.1 skrll maximum alignment and check low bits of the symbol address
2992 1.1 skrll for the minimum alignment. */
2993 1.1 skrll power_of_two = bfd_get_section_alignment (sec->owner, sec);
2994 1.1 skrll mask = ((bfd_vma) 1 << power_of_two) - 1;
2995 1.1 skrll while ((h->root.u.def.value & mask) != 0)
2996 1.1 skrll {
2997 1.1 skrll mask >>= 1;
2998 1.1 skrll --power_of_two;
2999 1.1 skrll }
3000 1.1 skrll
3001 1.1 skrll if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3002 1.1 skrll dynbss))
3003 1.1 skrll {
3004 1.1 skrll /* Adjust the section alignment if needed. */
3005 1.1 skrll if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3006 1.1 skrll power_of_two))
3007 1.1 skrll return FALSE;
3008 1.1 skrll }
3009 1.1 skrll
3010 1.1 skrll /* We make sure that the symbol will be aligned properly. */
3011 1.1 skrll dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3012 1.1 skrll
3013 1.9 christos /* Define the symbol as being at this point in DYNBSS. */
3014 1.9 christos h->root.u.def.section = dynbss;
3015 1.9 christos h->root.u.def.value = dynbss->size;
3016 1.9 christos
3017 1.9 christos /* Increment the size of DYNBSS to make room for the symbol. */
3018 1.9 christos dynbss->size += h->size;
3019 1.9 christos
3020 1.9 christos /* No error if extern_protected_data is true. */
3021 1.9 christos if (h->protected_def
3022 1.1 skrll && (!info->extern_protected_data
3023 1.1 skrll || (info->extern_protected_data < 0
3024 1.1 skrll && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3025 1.1 skrll info->callbacks->einfo
3026 1.1 skrll (_("%P: copy reloc against protected `%T' is dangerous\n"),
3027 1.1 skrll h->root.root.string);
3028 1.4 christos
3029 1.1 skrll return TRUE;
3030 1.1 skrll }
3031 1.1 skrll
3032 1.1 skrll /* Adjust all external symbols pointing into SEC_MERGE sections
3033 1.1 skrll to reflect the object merging within the sections. */
3034 1.1 skrll
3035 1.1 skrll static bfd_boolean
3036 1.7 christos _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3037 1.1 skrll {
3038 1.4 christos asection *sec;
3039 1.1 skrll
3040 1.1 skrll if ((h->root.type == bfd_link_hash_defined
3041 1.1 skrll || h->root.type == bfd_link_hash_defweak)
3042 1.1 skrll && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3043 1.1 skrll && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3044 1.1 skrll {
3045 1.1 skrll bfd *output_bfd = (bfd *) data;
3046 1.1 skrll
3047 1.1 skrll h->root.u.def.value =
3048 1.1 skrll _bfd_merged_section_offset (output_bfd,
3049 1.1 skrll &h->root.u.def.section,
3050 1.1 skrll elf_section_data (sec)->sec_info,
3051 1.1 skrll h->root.u.def.value);
3052 1.1 skrll }
3053 1.1 skrll
3054 1.1 skrll return TRUE;
3055 1.1 skrll }
3056 1.1 skrll
3057 1.4 christos /* Returns false if the symbol referred to by H should be considered
3058 1.1 skrll to resolve local to the current module, and true if it should be
3059 1.1 skrll considered to bind dynamically. */
3060 1.1 skrll
3061 1.1 skrll bfd_boolean
3062 1.1 skrll _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3063 1.1 skrll struct bfd_link_info *info,
3064 1.1 skrll bfd_boolean not_local_protected)
3065 1.1 skrll {
3066 1.1 skrll bfd_boolean binding_stays_local_p;
3067 1.1 skrll const struct elf_backend_data *bed;
3068 1.1 skrll struct elf_link_hash_table *hash_table;
3069 1.1 skrll
3070 1.1 skrll if (h == NULL)
3071 1.1 skrll return FALSE;
3072 1.1 skrll
3073 1.1 skrll while (h->root.type == bfd_link_hash_indirect
3074 1.1 skrll || h->root.type == bfd_link_hash_warning)
3075 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
3076 1.1 skrll
3077 1.1 skrll /* If it was forced local, then clearly it's not dynamic. */
3078 1.9 christos if (h->dynindx == -1)
3079 1.9 christos return FALSE;
3080 1.1 skrll if (h->forced_local)
3081 1.1 skrll return FALSE;
3082 1.1 skrll
3083 1.1 skrll /* Identify the cases where name binding rules say that a
3084 1.1 skrll visible symbol resolves locally. */
3085 1.1 skrll binding_stays_local_p = (bfd_link_executable (info)
3086 1.1 skrll || SYMBOLIC_BIND (info, h));
3087 1.1 skrll
3088 1.1 skrll switch (ELF_ST_VISIBILITY (h->other))
3089 1.1 skrll {
3090 1.1 skrll case STV_INTERNAL:
3091 1.1 skrll case STV_HIDDEN:
3092 1.1 skrll return FALSE;
3093 1.1 skrll
3094 1.1 skrll case STV_PROTECTED:
3095 1.1 skrll hash_table = elf_hash_table (info);
3096 1.1 skrll if (!is_elf_hash_table (hash_table))
3097 1.4 christos return FALSE;
3098 1.1 skrll
3099 1.1 skrll bed = get_elf_backend_data (hash_table->dynobj);
3100 1.1 skrll
3101 1.1 skrll /* Proper resolution for function pointer equality may require
3102 1.1 skrll that these symbols perhaps be resolved dynamically, even though
3103 1.1 skrll we should be resolving them to the current module. */
3104 1.1 skrll if (!not_local_protected || !bed->is_function_type (h->type))
3105 1.1 skrll binding_stays_local_p = TRUE;
3106 1.4 christos break;
3107 1.1 skrll
3108 1.1 skrll default:
3109 1.1 skrll break;
3110 1.1 skrll }
3111 1.1 skrll
3112 1.1 skrll /* If it isn't defined locally, then clearly it's dynamic. */
3113 1.1 skrll if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3114 1.1 skrll return TRUE;
3115 1.1 skrll
3116 1.1 skrll /* Otherwise, the symbol is dynamic if binding rules don't tell
3117 1.4 christos us that it remains local. */
3118 1.13.12.2 pgoyette return !binding_stays_local_p;
3119 1.13.12.2 pgoyette }
3120 1.13.12.2 pgoyette
3121 1.13.12.2 pgoyette /* Return true if the symbol referred to by H should be considered
3122 1.4 christos to resolve local to the current module, and false otherwise. Differs
3123 1.4 christos from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3124 1.4 christos undefined symbols. The two functions are virtually identical except
3125 1.4 christos for the place where dynindx == -1 is tested. If that test is true,
3126 1.1 skrll _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3127 1.1 skrll _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3128 1.1 skrll defined symbols.
3129 1.1 skrll It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3130 1.1 skrll !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3131 1.1 skrll treatment of undefined weak symbols. For those that do not make
3132 1.1 skrll undefined weak symbols dynamic, both functions may return false. */
3133 1.1 skrll
3134 1.1 skrll bfd_boolean
3135 1.1 skrll _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3136 1.1 skrll struct bfd_link_info *info,
3137 1.1 skrll bfd_boolean local_protected)
3138 1.1 skrll {
3139 1.1 skrll const struct elf_backend_data *bed;
3140 1.1 skrll struct elf_link_hash_table *hash_table;
3141 1.1 skrll
3142 1.1 skrll /* If it's a local sym, of course we resolve locally. */
3143 1.1 skrll if (h == NULL)
3144 1.13.12.2 pgoyette return TRUE;
3145 1.13.12.2 pgoyette
3146 1.13.12.2 pgoyette /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3147 1.13.12.2 pgoyette if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3148 1.1 skrll || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3149 1.1 skrll return TRUE;
3150 1.1 skrll
3151 1.1 skrll /* Forced local symbols resolve locally. */
3152 1.1 skrll if (h->forced_local)
3153 1.1 skrll return TRUE;
3154 1.1 skrll
3155 1.1 skrll /* Common symbols that become definitions don't get the DEF_REGULAR
3156 1.1 skrll flag set, so test it first, and don't bail out. */
3157 1.13.12.2 pgoyette if (ELF_COMMON_DEF_P (h))
3158 1.1 skrll /* Do nothing. */;
3159 1.1 skrll /* If we don't have a definition in a regular file, then we can't
3160 1.1 skrll resolve locally. The sym is either undefined or dynamic. */
3161 1.1 skrll else if (!h->def_regular)
3162 1.1 skrll return FALSE;
3163 1.1 skrll
3164 1.9 christos /* Non-dynamic symbols resolve locally. */
3165 1.1 skrll if (h->dynindx == -1)
3166 1.1 skrll return TRUE;
3167 1.1 skrll
3168 1.1 skrll /* At this point, we know the symbol is defined and dynamic. In an
3169 1.1 skrll executable it must resolve locally, likewise when building symbolic
3170 1.1 skrll shared libraries. */
3171 1.1 skrll if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3172 1.1 skrll return TRUE;
3173 1.1 skrll
3174 1.1 skrll /* Now deal with defined dynamic symbols in shared libraries. Ones
3175 1.1 skrll with default visibility might not resolve locally. */
3176 1.1 skrll if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3177 1.1 skrll return FALSE;
3178 1.9 christos
3179 1.9 christos hash_table = elf_hash_table (info);
3180 1.9 christos if (!is_elf_hash_table (hash_table))
3181 1.9 christos return TRUE;
3182 1.9 christos
3183 1.9 christos bed = get_elf_backend_data (hash_table->dynobj);
3184 1.1 skrll
3185 1.1 skrll /* If extern_protected_data is false, STV_PROTECTED non-function
3186 1.1 skrll symbols are local. */
3187 1.4 christos if ((!info->extern_protected_data
3188 1.4 christos || (info->extern_protected_data < 0
3189 1.4 christos && !bed->extern_protected_data))
3190 1.4 christos && !bed->is_function_type (h->type))
3191 1.1 skrll return TRUE;
3192 1.1 skrll
3193 1.1 skrll /* Function pointer equality tests may require that STV_PROTECTED
3194 1.1 skrll symbols be treated as dynamic symbols. If the address of a
3195 1.1 skrll function not defined in an executable is set to that function's
3196 1.1 skrll plt entry in the executable, then the address of the function in
3197 1.1 skrll a shared library must also be the plt entry in the executable. */
3198 1.1 skrll return local_protected;
3199 1.1 skrll }
3200 1.1 skrll
3201 1.1 skrll /* Caches some TLS segment info, and ensures that the TLS segment vma is
3202 1.1 skrll aligned. Returns the first TLS output section. */
3203 1.1 skrll
3204 1.1 skrll struct bfd_section *
3205 1.1 skrll _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3206 1.1 skrll {
3207 1.1 skrll struct bfd_section *sec, *tls;
3208 1.1 skrll unsigned int align = 0;
3209 1.1 skrll
3210 1.1 skrll for (sec = obfd->sections; sec != NULL; sec = sec->next)
3211 1.1 skrll if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3212 1.1 skrll break;
3213 1.1 skrll tls = sec;
3214 1.1 skrll
3215 1.1 skrll for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3216 1.1 skrll if (sec->alignment_power > align)
3217 1.1 skrll align = sec->alignment_power;
3218 1.1 skrll
3219 1.1 skrll elf_hash_table (info)->tls_sec = tls;
3220 1.1 skrll
3221 1.1 skrll /* Ensure the alignment of the first section is the largest alignment,
3222 1.1 skrll so that the tls segment starts aligned. */
3223 1.1 skrll if (tls != NULL)
3224 1.1 skrll tls->alignment_power = align;
3225 1.1 skrll
3226 1.1 skrll return tls;
3227 1.1 skrll }
3228 1.1 skrll
3229 1.1 skrll /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3230 1.1 skrll static bfd_boolean
3231 1.1 skrll is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3232 1.1 skrll Elf_Internal_Sym *sym)
3233 1.1 skrll {
3234 1.1 skrll const struct elf_backend_data *bed;
3235 1.1 skrll
3236 1.1 skrll /* Local symbols do not count, but target specific ones might. */
3237 1.1 skrll if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3238 1.1 skrll && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3239 1.1 skrll return FALSE;
3240 1.1 skrll
3241 1.1 skrll bed = get_elf_backend_data (abfd);
3242 1.1 skrll /* Function symbols do not count. */
3243 1.1 skrll if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3244 1.1 skrll return FALSE;
3245 1.1 skrll
3246 1.1 skrll /* If the section is undefined, then so is the symbol. */
3247 1.1 skrll if (sym->st_shndx == SHN_UNDEF)
3248 1.1 skrll return FALSE;
3249 1.1 skrll
3250 1.1 skrll /* If the symbol is defined in the common section, then
3251 1.1 skrll it is a common definition and so does not count. */
3252 1.1 skrll if (bed->common_definition (sym))
3253 1.1 skrll return FALSE;
3254 1.1 skrll
3255 1.1 skrll /* If the symbol is in a target specific section then we
3256 1.1 skrll must rely upon the backend to tell us what it is. */
3257 1.1 skrll if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3258 1.1 skrll /* FIXME - this function is not coded yet:
3259 1.1 skrll
3260 1.1 skrll return _bfd_is_global_symbol_definition (abfd, sym);
3261 1.1 skrll
3262 1.1 skrll Instead for now assume that the definition is not global,
3263 1.1 skrll Even if this is wrong, at least the linker will behave
3264 1.1 skrll in the same way that it used to do. */
3265 1.1 skrll return FALSE;
3266 1.1 skrll
3267 1.1 skrll return TRUE;
3268 1.1 skrll }
3269 1.1 skrll
3270 1.13 christos /* Search the symbol table of the archive element of the archive ABFD
3271 1.13 christos whose archive map contains a mention of SYMDEF, and determine if
3272 1.13 christos the symbol is defined in this element. */
3273 1.1 skrll static bfd_boolean
3274 1.1 skrll elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3275 1.1 skrll {
3276 1.1 skrll Elf_Internal_Shdr * hdr;
3277 1.1 skrll size_t symcount;
3278 1.1 skrll size_t extsymcount;
3279 1.1 skrll size_t extsymoff;
3280 1.1 skrll Elf_Internal_Sym *isymbuf;
3281 1.1 skrll Elf_Internal_Sym *isym;
3282 1.9 christos Elf_Internal_Sym *isymend;
3283 1.1 skrll bfd_boolean result;
3284 1.1 skrll
3285 1.13 christos abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3286 1.13 christos if (abfd == NULL)
3287 1.13 christos return FALSE;
3288 1.13 christos
3289 1.13 christos if (! bfd_check_format (abfd, bfd_object))
3290 1.13 christos return FALSE;
3291 1.13 christos
3292 1.13 christos /* Select the appropriate symbol table. If we don't know if the
3293 1.13 christos object file is an IR object, give linker LTO plugin a chance to
3294 1.13 christos get the correct symbol table. */
3295 1.13 christos if (abfd->plugin_format == bfd_plugin_yes
3296 1.13 christos #if BFD_SUPPORTS_PLUGINS
3297 1.13 christos || (abfd->plugin_format == bfd_plugin_unknown
3298 1.13 christos && bfd_link_plugin_object_p (abfd))
3299 1.13 christos #endif
3300 1.13 christos )
3301 1.1 skrll {
3302 1.1 skrll /* Use the IR symbol table if the object has been claimed by
3303 1.1 skrll plugin. */
3304 1.1 skrll abfd = abfd->plugin_dummy_bfd;
3305 1.1 skrll hdr = &elf_tdata (abfd)->symtab_hdr;
3306 1.1 skrll }
3307 1.1 skrll else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3308 1.1 skrll hdr = &elf_tdata (abfd)->symtab_hdr;
3309 1.1 skrll else
3310 1.1 skrll hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3311 1.1 skrll
3312 1.1 skrll symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3313 1.1 skrll
3314 1.1 skrll /* The sh_info field of the symtab header tells us where the
3315 1.1 skrll external symbols start. We don't care about the local symbols. */
3316 1.1 skrll if (elf_bad_symtab (abfd))
3317 1.1 skrll {
3318 1.1 skrll extsymcount = symcount;
3319 1.1 skrll extsymoff = 0;
3320 1.1 skrll }
3321 1.1 skrll else
3322 1.1 skrll {
3323 1.1 skrll extsymcount = symcount - hdr->sh_info;
3324 1.1 skrll extsymoff = hdr->sh_info;
3325 1.1 skrll }
3326 1.1 skrll
3327 1.1 skrll if (extsymcount == 0)
3328 1.1 skrll return FALSE;
3329 1.1 skrll
3330 1.1 skrll /* Read in the symbol table. */
3331 1.1 skrll isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3332 1.1 skrll NULL, NULL, NULL);
3333 1.1 skrll if (isymbuf == NULL)
3334 1.1 skrll return FALSE;
3335 1.1 skrll
3336 1.1 skrll /* Scan the symbol table looking for SYMDEF. */
3337 1.1 skrll result = FALSE;
3338 1.1 skrll for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3339 1.1 skrll {
3340 1.1 skrll const char *name;
3341 1.1 skrll
3342 1.1 skrll name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3343 1.1 skrll isym->st_name);
3344 1.1 skrll if (name == NULL)
3345 1.1 skrll break;
3346 1.1 skrll
3347 1.1 skrll if (strcmp (name, symdef->name) == 0)
3348 1.1 skrll {
3349 1.1 skrll result = is_global_data_symbol_definition (abfd, isym);
3350 1.1 skrll break;
3351 1.1 skrll }
3352 1.1 skrll }
3353 1.1 skrll
3354 1.1 skrll free (isymbuf);
3355 1.1 skrll
3356 1.1 skrll return result;
3357 1.1 skrll }
3358 1.1 skrll
3359 1.1 skrll /* Add an entry to the .dynamic table. */
3361 1.1 skrll
3362 1.1 skrll bfd_boolean
3363 1.1 skrll _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3364 1.1 skrll bfd_vma tag,
3365 1.1 skrll bfd_vma val)
3366 1.1 skrll {
3367 1.1 skrll struct elf_link_hash_table *hash_table;
3368 1.1 skrll const struct elf_backend_data *bed;
3369 1.1 skrll asection *s;
3370 1.1 skrll bfd_size_type newsize;
3371 1.7 christos bfd_byte *newcontents;
3372 1.1 skrll Elf_Internal_Dyn dyn;
3373 1.1 skrll
3374 1.1 skrll hash_table = elf_hash_table (info);
3375 1.4 christos if (! is_elf_hash_table (hash_table))
3376 1.1 skrll return FALSE;
3377 1.1 skrll
3378 1.1 skrll bed = get_elf_backend_data (hash_table->dynobj);
3379 1.1 skrll s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3380 1.1 skrll BFD_ASSERT (s != NULL);
3381 1.1 skrll
3382 1.1 skrll newsize = s->size + bed->s->sizeof_dyn;
3383 1.1 skrll newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3384 1.1 skrll if (newcontents == NULL)
3385 1.1 skrll return FALSE;
3386 1.1 skrll
3387 1.1 skrll dyn.d_tag = tag;
3388 1.1 skrll dyn.d_un.d_val = val;
3389 1.1 skrll bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3390 1.1 skrll
3391 1.1 skrll s->size = newsize;
3392 1.1 skrll s->contents = newcontents;
3393 1.1 skrll
3394 1.1 skrll return TRUE;
3395 1.1 skrll }
3396 1.1 skrll
3397 1.1 skrll /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3398 1.1 skrll otherwise just check whether one already exists. Returns -1 on error,
3399 1.1 skrll 1 if a DT_NEEDED tag already exists, and 0 on success. */
3400 1.13 christos
3401 1.1 skrll static int
3402 1.1 skrll elf_add_dt_needed_tag (bfd *abfd,
3403 1.1 skrll struct bfd_link_info *info,
3404 1.1 skrll const char *soname,
3405 1.1 skrll bfd_boolean do_it)
3406 1.1 skrll {
3407 1.13 christos struct elf_link_hash_table *hash_table;
3408 1.1 skrll size_t strindex;
3409 1.1 skrll
3410 1.7 christos if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3411 1.1 skrll return -1;
3412 1.1 skrll
3413 1.1 skrll hash_table = elf_hash_table (info);
3414 1.1 skrll strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3415 1.1 skrll if (strindex == (size_t) -1)
3416 1.1 skrll return -1;
3417 1.7 christos
3418 1.1 skrll if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3419 1.1 skrll {
3420 1.1 skrll asection *sdyn;
3421 1.1 skrll const struct elf_backend_data *bed;
3422 1.1 skrll bfd_byte *extdyn;
3423 1.1 skrll
3424 1.1 skrll bed = get_elf_backend_data (hash_table->dynobj);
3425 1.1 skrll sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3426 1.1 skrll if (sdyn != NULL)
3427 1.1 skrll for (extdyn = sdyn->contents;
3428 1.1 skrll extdyn < sdyn->contents + sdyn->size;
3429 1.1 skrll extdyn += bed->s->sizeof_dyn)
3430 1.1 skrll {
3431 1.1 skrll Elf_Internal_Dyn dyn;
3432 1.1 skrll
3433 1.1 skrll bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3434 1.1 skrll if (dyn.d_tag == DT_NEEDED
3435 1.1 skrll && dyn.d_un.d_val == strindex)
3436 1.1 skrll {
3437 1.1 skrll _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3438 1.1 skrll return 1;
3439 1.1 skrll }
3440 1.1 skrll }
3441 1.1 skrll }
3442 1.1 skrll
3443 1.1 skrll if (do_it)
3444 1.1 skrll {
3445 1.1 skrll if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3446 1.1 skrll return -1;
3447 1.1 skrll
3448 1.1 skrll if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3449 1.1 skrll return -1;
3450 1.13 christos }
3451 1.13 christos else
3452 1.13 christos /* We were just checking for existence of the tag. */
3453 1.13 christos _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3454 1.13 christos
3455 1.13 christos return 0;
3456 1.13 christos }
3457 1.13 christos
3458 1.13 christos /* Return true if SONAME is on the needed list between NEEDED and STOP
3459 1.13 christos (or the end of list if STOP is NULL), and needed by a library that
3460 1.13 christos will be loaded. */
3461 1.13 christos
3462 1.13 christos static bfd_boolean
3463 1.13 christos on_needed_list (const char *soname,
3464 1.13 christos struct bfd_link_needed_list *needed,
3465 1.13 christos struct bfd_link_needed_list *stop)
3466 1.13 christos {
3467 1.13 christos struct bfd_link_needed_list *look;
3468 1.13 christos for (look = needed; look != stop; look = look->next)
3469 1.13 christos if (strcmp (soname, look->name) == 0
3470 1.4 christos && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3471 1.4 christos /* If needed by a library that itself is not directly
3472 1.4 christos needed, recursively check whether that library is
3473 1.4 christos indirectly needed. Since we add DT_NEEDED entries to
3474 1.4 christos the end of the list, library dependencies appear after
3475 1.7 christos the library. Therefore search prior to the current
3476 1.1 skrll LOOK, preventing possible infinite recursion. */
3477 1.1 skrll || on_needed_list (elf_dt_name (look->by), needed, look)))
3478 1.1 skrll return TRUE;
3479 1.1 skrll
3480 1.1 skrll return FALSE;
3481 1.1 skrll }
3482 1.1 skrll
3483 1.1 skrll /* Sort symbol by value, section, and size. */
3484 1.1 skrll static int
3485 1.1 skrll elf_sort_symbol (const void *arg1, const void *arg2)
3486 1.1 skrll {
3487 1.1 skrll const struct elf_link_hash_entry *h1;
3488 1.1 skrll const struct elf_link_hash_entry *h2;
3489 1.1 skrll bfd_signed_vma vdiff;
3490 1.9 christos
3491 1.1 skrll h1 = *(const struct elf_link_hash_entry **) arg1;
3492 1.1 skrll h2 = *(const struct elf_link_hash_entry **) arg2;
3493 1.1 skrll vdiff = h1->root.u.def.value - h2->root.u.def.value;
3494 1.7 christos if (vdiff != 0)
3495 1.7 christos return vdiff > 0 ? 1 : -1;
3496 1.1 skrll else
3497 1.1 skrll {
3498 1.1 skrll int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3499 1.1 skrll if (sdiff != 0)
3500 1.1 skrll return sdiff > 0 ? 1 : -1;
3501 1.1 skrll }
3502 1.1 skrll vdiff = h1->size - h2->size;
3503 1.1 skrll return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3504 1.4 christos }
3505 1.1 skrll
3506 1.1 skrll /* This function is used to adjust offsets into .dynstr for
3507 1.1 skrll dynamic symbols. This is called via elf_link_hash_traverse. */
3508 1.1 skrll
3509 1.1 skrll static bfd_boolean
3510 1.1 skrll elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3511 1.1 skrll {
3512 1.1 skrll struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3513 1.1 skrll
3514 1.1 skrll if (h->dynindx != -1)
3515 1.1 skrll h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3516 1.1 skrll return TRUE;
3517 1.1 skrll }
3518 1.1 skrll
3519 1.1 skrll /* Assign string offsets in .dynstr, update all structures referencing
3520 1.1 skrll them. */
3521 1.1 skrll
3522 1.1 skrll static bfd_boolean
3523 1.1 skrll elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3524 1.1 skrll {
3525 1.1 skrll struct elf_link_hash_table *hash_table = elf_hash_table (info);
3526 1.1 skrll struct elf_link_local_dynamic_entry *entry;
3527 1.1 skrll struct elf_strtab_hash *dynstr = hash_table->dynstr;
3528 1.1 skrll bfd *dynobj = hash_table->dynobj;
3529 1.1 skrll asection *sdyn;
3530 1.7 christos bfd_size_type size;
3531 1.1 skrll const struct elf_backend_data *bed;
3532 1.1 skrll bfd_byte *extdyn;
3533 1.1 skrll
3534 1.1 skrll _bfd_elf_strtab_finalize (dynstr);
3535 1.1 skrll size = _bfd_elf_strtab_size (dynstr);
3536 1.1 skrll
3537 1.1 skrll bed = get_elf_backend_data (dynobj);
3538 1.1 skrll sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3539 1.1 skrll BFD_ASSERT (sdyn != NULL);
3540 1.1 skrll
3541 1.1 skrll /* Update all .dynamic entries referencing .dynstr strings. */
3542 1.1 skrll for (extdyn = sdyn->contents;
3543 1.1 skrll extdyn < sdyn->contents + sdyn->size;
3544 1.1 skrll extdyn += bed->s->sizeof_dyn)
3545 1.1 skrll {
3546 1.1 skrll Elf_Internal_Dyn dyn;
3547 1.1 skrll
3548 1.1 skrll bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3549 1.1 skrll switch (dyn.d_tag)
3550 1.1 skrll {
3551 1.1 skrll case DT_STRSZ:
3552 1.4 christos dyn.d_un.d_val = size;
3553 1.4 christos break;
3554 1.1 skrll case DT_NEEDED:
3555 1.1 skrll case DT_SONAME:
3556 1.1 skrll case DT_RPATH:
3557 1.1 skrll case DT_RUNPATH:
3558 1.1 skrll case DT_FILTER:
3559 1.1 skrll case DT_AUXILIARY:
3560 1.1 skrll case DT_AUDIT:
3561 1.1 skrll case DT_DEPAUDIT:
3562 1.1 skrll dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3563 1.1 skrll break;
3564 1.1 skrll default:
3565 1.1 skrll continue;
3566 1.1 skrll }
3567 1.1 skrll bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3568 1.1 skrll }
3569 1.1 skrll
3570 1.1 skrll /* Now update local dynamic symbols. */
3571 1.1 skrll for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3572 1.1 skrll entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3573 1.1 skrll entry->isym.st_name);
3574 1.1 skrll
3575 1.13 christos /* And the rest of dynamic symbols. */
3576 1.1 skrll elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3577 1.1 skrll
3578 1.1 skrll /* Adjust version definitions. */
3579 1.7 christos if (elf_tdata (output_bfd)->cverdefs)
3580 1.1 skrll {
3581 1.1 skrll asection *s;
3582 1.1 skrll bfd_byte *p;
3583 1.1 skrll size_t i;
3584 1.1 skrll Elf_Internal_Verdef def;
3585 1.1 skrll Elf_Internal_Verdaux defaux;
3586 1.1 skrll
3587 1.1 skrll s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3588 1.1 skrll p = s->contents;
3589 1.1 skrll do
3590 1.1 skrll {
3591 1.1 skrll _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3592 1.1 skrll &def);
3593 1.1 skrll p += sizeof (Elf_External_Verdef);
3594 1.1 skrll if (def.vd_aux != sizeof (Elf_External_Verdef))
3595 1.1 skrll continue;
3596 1.1 skrll for (i = 0; i < def.vd_cnt; ++i)
3597 1.1 skrll {
3598 1.1 skrll _bfd_elf_swap_verdaux_in (output_bfd,
3599 1.1 skrll (Elf_External_Verdaux *) p, &defaux);
3600 1.1 skrll defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3601 1.1 skrll defaux.vda_name);
3602 1.1 skrll _bfd_elf_swap_verdaux_out (output_bfd,
3603 1.1 skrll &defaux, (Elf_External_Verdaux *) p);
3604 1.1 skrll p += sizeof (Elf_External_Verdaux);
3605 1.1 skrll }
3606 1.1 skrll }
3607 1.13 christos while (def.vd_next);
3608 1.1 skrll }
3609 1.1 skrll
3610 1.1 skrll /* Adjust version references. */
3611 1.7 christos if (elf_tdata (output_bfd)->verref)
3612 1.1 skrll {
3613 1.1 skrll asection *s;
3614 1.1 skrll bfd_byte *p;
3615 1.1 skrll size_t i;
3616 1.1 skrll Elf_Internal_Verneed need;
3617 1.1 skrll Elf_Internal_Vernaux needaux;
3618 1.1 skrll
3619 1.1 skrll s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3620 1.1 skrll p = s->contents;
3621 1.1 skrll do
3622 1.1 skrll {
3623 1.1 skrll _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3624 1.1 skrll &need);
3625 1.1 skrll need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3626 1.1 skrll _bfd_elf_swap_verneed_out (output_bfd, &need,
3627 1.1 skrll (Elf_External_Verneed *) p);
3628 1.1 skrll p += sizeof (Elf_External_Verneed);
3629 1.1 skrll for (i = 0; i < need.vn_cnt; ++i)
3630 1.1 skrll {
3631 1.1 skrll _bfd_elf_swap_vernaux_in (output_bfd,
3632 1.1 skrll (Elf_External_Vernaux *) p, &needaux);
3633 1.1 skrll needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3634 1.1 skrll needaux.vna_name);
3635 1.1 skrll _bfd_elf_swap_vernaux_out (output_bfd,
3636 1.1 skrll &needaux,
3637 1.1 skrll (Elf_External_Vernaux *) p);
3638 1.1 skrll p += sizeof (Elf_External_Vernaux);
3639 1.1 skrll }
3640 1.1 skrll }
3641 1.1 skrll while (need.vn_next);
3642 1.1 skrll }
3643 1.1 skrll
3644 1.1 skrll return TRUE;
3645 1.1 skrll }
3646 1.1 skrll
3647 1.1 skrll /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3649 1.1 skrll The default is to only match when the INPUT and OUTPUT are exactly
3650 1.1 skrll the same target. */
3651 1.1 skrll
3652 1.1 skrll bfd_boolean
3653 1.1 skrll _bfd_elf_default_relocs_compatible (const bfd_target *input,
3654 1.1 skrll const bfd_target *output)
3655 1.1 skrll {
3656 1.1 skrll return input == output;
3657 1.1 skrll }
3658 1.1 skrll
3659 1.1 skrll /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3660 1.1 skrll This version is used when different targets for the same architecture
3661 1.1 skrll are virtually identical. */
3662 1.1 skrll
3663 1.1 skrll bfd_boolean
3664 1.1 skrll _bfd_elf_relocs_compatible (const bfd_target *input,
3665 1.1 skrll const bfd_target *output)
3666 1.1 skrll {
3667 1.1 skrll const struct elf_backend_data *obed, *ibed;
3668 1.1 skrll
3669 1.1 skrll if (input == output)
3670 1.1 skrll return TRUE;
3671 1.1 skrll
3672 1.1 skrll ibed = xvec_get_elf_backend_data (input);
3673 1.9 christos obed = xvec_get_elf_backend_data (output);
3674 1.9 christos
3675 1.9 christos if (ibed->arch != obed->arch)
3676 1.9 christos return FALSE;
3677 1.9 christos
3678 1.9 christos /* If both backends are using this function, deem them compatible. */
3679 1.9 christos return ibed->relocs_compatible == obed->relocs_compatible;
3680 1.9 christos }
3681 1.9 christos
3682 1.9 christos /* Make a special call to the linker "notice" function to tell it that
3683 1.9 christos we are about to handle an as-needed lib, or have finished
3684 1.9 christos processing the lib. */
3685 1.13 christos
3686 1.13 christos bfd_boolean
3687 1.13 christos _bfd_elf_notice_as_needed (bfd *ibfd,
3688 1.13 christos struct bfd_link_info *info,
3689 1.13 christos enum notice_asneeded_action act)
3690 1.13 christos {
3691 1.13 christos return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3692 1.13 christos }
3693 1.13 christos
3694 1.13 christos /* Check relocations an ELF object file. */
3695 1.13 christos
3696 1.13 christos bfd_boolean
3697 1.13 christos _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3698 1.13 christos {
3699 1.13 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3700 1.13 christos struct elf_link_hash_table *htab = elf_hash_table (info);
3701 1.13 christos
3702 1.13 christos /* If this object is the same format as the output object, and it is
3703 1.13 christos not a shared library, then let the backend look through the
3704 1.13 christos relocs.
3705 1.13 christos
3706 1.13 christos This is required to build global offset table entries and to
3707 1.13 christos arrange for dynamic relocs. It is not required for the
3708 1.13 christos particular common case of linking non PIC code, even when linking
3709 1.13 christos against shared libraries, but unfortunately there is no way of
3710 1.13 christos knowing whether an object file has been compiled PIC or not.
3711 1.13 christos Looking through the relocs is not particularly time consuming.
3712 1.13 christos The problem is that we must either (1) keep the relocs in memory,
3713 1.13 christos which causes the linker to require additional runtime memory or
3714 1.13 christos (2) read the relocs twice from the input file, which wastes time.
3715 1.13 christos This would be a good case for using mmap.
3716 1.13 christos
3717 1.13 christos I have no idea how to handle linking PIC code into a file of a
3718 1.13 christos different format. It probably can't be done. */
3719 1.13 christos if ((abfd->flags & DYNAMIC) == 0
3720 1.13 christos && is_elf_hash_table (htab)
3721 1.13 christos && bed->check_relocs != NULL
3722 1.13 christos && elf_object_id (abfd) == elf_hash_table_id (htab)
3723 1.13 christos && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3724 1.13 christos {
3725 1.13 christos asection *o;
3726 1.13 christos
3727 1.13 christos for (o = abfd->sections; o != NULL; o = o->next)
3728 1.13 christos {
3729 1.13 christos Elf_Internal_Rela *internal_relocs;
3730 1.13 christos bfd_boolean ok;
3731 1.13 christos
3732 1.13 christos /* Don't check relocations in excluded sections. */
3733 1.13 christos if ((o->flags & SEC_RELOC) == 0
3734 1.13 christos || (o->flags & SEC_EXCLUDE) != 0
3735 1.13 christos || o->reloc_count == 0
3736 1.13 christos || ((info->strip == strip_all || info->strip == strip_debugger)
3737 1.13 christos && (o->flags & SEC_DEBUGGING) != 0)
3738 1.13 christos || bfd_is_abs_section (o->output_section))
3739 1.13 christos continue;
3740 1.13 christos
3741 1.13 christos internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3742 1.13 christos info->keep_memory);
3743 1.13 christos if (internal_relocs == NULL)
3744 1.13 christos return FALSE;
3745 1.13 christos
3746 1.13 christos ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3747 1.13 christos
3748 1.13 christos if (elf_section_data (o)->relocs != internal_relocs)
3749 1.13 christos free (internal_relocs);
3750 1.1 skrll
3751 1.1 skrll if (! ok)
3752 1.1 skrll return FALSE;
3753 1.1 skrll }
3754 1.1 skrll }
3755 1.4 christos
3756 1.1 skrll return TRUE;
3757 1.13 christos }
3758 1.13 christos
3759 1.13 christos /* Add symbols from an ELF object file to the linker hash table. */
3760 1.1 skrll
3761 1.1 skrll static bfd_boolean
3762 1.1 skrll elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3763 1.1 skrll {
3764 1.1 skrll Elf_Internal_Ehdr *ehdr;
3765 1.1 skrll Elf_Internal_Shdr *hdr;
3766 1.13 christos size_t symcount;
3767 1.1 skrll size_t extsymcount;
3768 1.1 skrll size_t extsymoff;
3769 1.1 skrll struct elf_link_hash_entry **sym_hash;
3770 1.1 skrll bfd_boolean dynamic;
3771 1.1 skrll Elf_External_Versym *extversym = NULL;
3772 1.1 skrll Elf_External_Versym *ever;
3773 1.1 skrll struct elf_link_hash_entry *weaks;
3774 1.1 skrll struct elf_link_hash_entry **nondeflt_vers = NULL;
3775 1.1 skrll size_t nondeflt_vers_cnt = 0;
3776 1.1 skrll Elf_Internal_Sym *isymbuf = NULL;
3777 1.1 skrll Elf_Internal_Sym *isym;
3778 1.1 skrll Elf_Internal_Sym *isymend;
3779 1.1 skrll const struct elf_backend_data *bed;
3780 1.1 skrll bfd_boolean add_needed;
3781 1.1 skrll struct elf_link_hash_table *htab;
3782 1.12 christos bfd_size_type amt;
3783 1.1 skrll void *alloc_mark = NULL;
3784 1.9 christos struct bfd_hash_entry **old_table = NULL;
3785 1.9 christos unsigned int old_size = 0;
3786 1.1 skrll unsigned int old_count = 0;
3787 1.1 skrll void *old_tab = NULL;
3788 1.1 skrll void *old_ent;
3789 1.1 skrll struct bfd_link_hash_entry *old_undefs = NULL;
3790 1.1 skrll struct bfd_link_hash_entry *old_undefs_tail = NULL;
3791 1.1 skrll void *old_strtab = NULL;
3792 1.1 skrll size_t tabsize = 0;
3793 1.1 skrll asection *s;
3794 1.1 skrll bfd_boolean just_syms;
3795 1.1 skrll
3796 1.1 skrll htab = elf_hash_table (info);
3797 1.1 skrll bed = get_elf_backend_data (abfd);
3798 1.1 skrll
3799 1.9 christos if ((abfd->flags & DYNAMIC) == 0)
3800 1.1 skrll dynamic = FALSE;
3801 1.1 skrll else
3802 1.1 skrll {
3803 1.9 christos dynamic = TRUE;
3804 1.1 skrll
3805 1.1 skrll /* You can't use -r against a dynamic object. Also, there's no
3806 1.1 skrll hope of using a dynamic object which does not exactly match
3807 1.1 skrll the format of the output file. */
3808 1.1 skrll if (bfd_link_relocatable (info)
3809 1.1 skrll || !is_elf_hash_table (htab)
3810 1.1 skrll || info->output_bfd->xvec != abfd->xvec)
3811 1.4 christos {
3812 1.4 christos if (bfd_link_relocatable (info))
3813 1.4 christos bfd_set_error (bfd_error_invalid_operation);
3814 1.4 christos else
3815 1.4 christos bfd_set_error (bfd_error_wrong_format);
3816 1.4 christos goto error_return;
3817 1.4 christos }
3818 1.4 christos }
3819 1.13.12.2 pgoyette
3820 1.4 christos ehdr = elf_elfheader (abfd);
3821 1.4 christos if (info->warn_alternate_em
3822 1.4 christos && bed->elf_machine_code != ehdr->e_machine
3823 1.1 skrll && ((bed->elf_machine_alt1 != 0
3824 1.1 skrll && ehdr->e_machine == bed->elf_machine_alt1)
3825 1.1 skrll || (bed->elf_machine_alt2 != 0
3826 1.1 skrll && ehdr->e_machine == bed->elf_machine_alt2)))
3827 1.7 christos info->callbacks->einfo
3828 1.9 christos /* xgettext:c-format */
3829 1.1 skrll (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3830 1.9 christos ehdr->e_machine, abfd, bed->elf_machine_code);
3831 1.1 skrll
3832 1.9 christos /* As a GNU extension, any input sections which are named
3833 1.9 christos .gnu.warning.SYMBOL are treated as warning symbols for the given
3834 1.1 skrll symbol. This differs from .gnu.warning sections, which generate
3835 1.9 christos warnings when they are included in an output file. */
3836 1.9 christos /* PR 12761: Also generate this warning when building shared libraries. */
3837 1.9 christos for (s = abfd->sections; s != NULL; s = s->next)
3838 1.9 christos {
3839 1.1 skrll const char *name;
3840 1.9 christos
3841 1.9 christos name = bfd_get_section_name (abfd, s);
3842 1.9 christos if (CONST_STRNEQ (name, ".gnu.warning."))
3843 1.9 christos {
3844 1.9 christos char *msg;
3845 1.9 christos bfd_size_type sz;
3846 1.9 christos
3847 1.9 christos name += sizeof ".gnu.warning." - 1;
3848 1.9 christos
3849 1.9 christos /* If this is a shared object, then look up the symbol
3850 1.1 skrll in the hash table. If it is there, and it is already
3851 1.9 christos been defined, then we will not be using the entry
3852 1.1 skrll from this shared object, so we don't need to warn.
3853 1.9 christos FIXME: If we see the definition in a regular object
3854 1.1 skrll later on, we will warn, but we shouldn't. The only
3855 1.9 christos fix is to keep track of what warnings we are supposed
3856 1.9 christos to emit, and then handle them all at the end of the
3857 1.9 christos link. */
3858 1.9 christos if (dynamic)
3859 1.9 christos {
3860 1.9 christos struct elf_link_hash_entry *h;
3861 1.1 skrll
3862 1.9 christos h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3863 1.9 christos
3864 1.9 christos /* FIXME: What about bfd_link_hash_common? */
3865 1.9 christos if (h != NULL
3866 1.1 skrll && (h->root.type == bfd_link_hash_defined
3867 1.9 christos || h->root.type == bfd_link_hash_defweak))
3868 1.9 christos continue;
3869 1.1 skrll }
3870 1.9 christos
3871 1.1 skrll sz = s->size;
3872 1.9 christos msg = (char *) bfd_alloc (abfd, sz + 1);
3873 1.9 christos if (msg == NULL)
3874 1.9 christos goto error_return;
3875 1.9 christos
3876 1.1 skrll if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3877 1.9 christos goto error_return;
3878 1.9 christos
3879 1.9 christos msg[sz] = '\0';
3880 1.9 christos
3881 1.9 christos if (! (_bfd_generic_link_add_one_symbol
3882 1.1 skrll (info, abfd, name, BSF_WARNING, s, 0, msg,
3883 1.9 christos FALSE, bed->collect, NULL)))
3884 1.9 christos goto error_return;
3885 1.9 christos
3886 1.1 skrll if (bfd_link_executable (info))
3887 1.1 skrll {
3888 1.1 skrll /* Clobber the section size so that the warning does
3889 1.1 skrll not get copied into the output file. */
3890 1.9 christos s->size = 0;
3891 1.9 christos
3892 1.9 christos /* Also set SEC_EXCLUDE, so that symbols defined in
3893 1.1 skrll the warning section don't get copied to the output. */
3894 1.1 skrll s->flags |= SEC_EXCLUDE;
3895 1.1 skrll }
3896 1.1 skrll }
3897 1.1 skrll }
3898 1.1 skrll
3899 1.13 christos just_syms = ((s = abfd->sections) != NULL
3900 1.13 christos && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3901 1.9 christos
3902 1.9 christos add_needed = TRUE;
3903 1.9 christos if (! dynamic)
3904 1.13 christos {
3905 1.13 christos /* If we are creating a shared library, create all the dynamic
3906 1.13.12.2 pgoyette sections immediately. We need to attach them to something,
3907 1.13 christos so we attach them to this BFD, provided it is the right
3908 1.1 skrll format and is not from ld --just-symbols. Always create the
3909 1.1 skrll dynamic sections for -E/--dynamic-list. FIXME: If there
3910 1.1 skrll are no input BFD's of the same format as the output, we can't
3911 1.1 skrll make a shared library. */
3912 1.1 skrll if (!just_syms
3913 1.1 skrll && (bfd_link_pic (info)
3914 1.1 skrll || (!bfd_link_relocatable (info)
3915 1.1 skrll && info->nointerp
3916 1.1 skrll && (info->export_dynamic || info->dynamic)))
3917 1.1 skrll && is_elf_hash_table (htab)
3918 1.1 skrll && info->output_bfd->xvec == abfd->xvec
3919 1.1 skrll && !htab->dynamic_sections_created)
3920 1.1 skrll {
3921 1.4 christos if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3922 1.1 skrll goto error_return;
3923 1.13.12.2 pgoyette }
3924 1.1 skrll }
3925 1.1 skrll else if (!is_elf_hash_table (htab))
3926 1.1 skrll goto error_return;
3927 1.1 skrll else
3928 1.9 christos {
3929 1.1 skrll const char *soname = NULL;
3930 1.1 skrll char *audit = NULL;
3931 1.1 skrll struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3932 1.1 skrll const Elf_Internal_Phdr *phdr;
3933 1.1 skrll int ret;
3934 1.1 skrll
3935 1.1 skrll /* ld --just-symbols and dynamic objects don't mix very well.
3936 1.1 skrll ld shouldn't allow it. */
3937 1.1 skrll if (just_syms)
3938 1.1 skrll abort ();
3939 1.1 skrll
3940 1.1 skrll /* If this dynamic lib was specified on the command line with
3941 1.1 skrll --as-needed in effect, then we don't want to add a DT_NEEDED
3942 1.1 skrll tag unless the lib is actually used. Similary for libs brought
3943 1.1 skrll in by another lib's DT_NEEDED. When --no-add-needed is used
3944 1.1 skrll on a dynamic lib, we don't want to add a DT_NEEDED entry for
3945 1.1 skrll any dynamic library in DT_NEEDED tags in the dynamic lib at
3946 1.1 skrll all. */
3947 1.1 skrll add_needed = (elf_dyn_lib_class (abfd)
3948 1.1 skrll & (DYN_AS_NEEDED | DYN_DT_NEEDED
3949 1.1 skrll | DYN_NO_NEEDED)) == 0;
3950 1.1 skrll
3951 1.4 christos s = bfd_get_section_by_name (abfd, ".dynamic");
3952 1.4 christos if (s != NULL)
3953 1.4 christos {
3954 1.4 christos bfd_byte *dynbuf;
3955 1.4 christos bfd_byte *extdyn;
3956 1.1 skrll unsigned int elfsec;
3957 1.1 skrll unsigned long shlink;
3958 1.1 skrll
3959 1.1 skrll if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3960 1.1 skrll {
3961 1.1 skrll error_free_dyn:
3962 1.1 skrll free (dynbuf);
3963 1.1 skrll goto error_return;
3964 1.1 skrll }
3965 1.1 skrll
3966 1.1 skrll elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3967 1.1 skrll if (elfsec == SHN_BAD)
3968 1.1 skrll goto error_free_dyn;
3969 1.1 skrll shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3970 1.1 skrll
3971 1.1 skrll for (extdyn = dynbuf;
3972 1.1 skrll extdyn < dynbuf + s->size;
3973 1.1 skrll extdyn += bed->s->sizeof_dyn)
3974 1.1 skrll {
3975 1.1 skrll Elf_Internal_Dyn dyn;
3976 1.1 skrll
3977 1.1 skrll bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3978 1.1 skrll if (dyn.d_tag == DT_SONAME)
3979 1.1 skrll {
3980 1.1 skrll unsigned int tagv = dyn.d_un.d_val;
3981 1.1 skrll soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3982 1.1 skrll if (soname == NULL)
3983 1.4 christos goto error_free_dyn;
3984 1.1 skrll }
3985 1.1 skrll if (dyn.d_tag == DT_NEEDED)
3986 1.1 skrll {
3987 1.1 skrll struct bfd_link_needed_list *n, **pn;
3988 1.4 christos char *fnm, *anm;
3989 1.1 skrll unsigned int tagv = dyn.d_un.d_val;
3990 1.1 skrll
3991 1.1 skrll amt = sizeof (struct bfd_link_needed_list);
3992 1.1 skrll n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3993 1.1 skrll fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3994 1.1 skrll if (n == NULL || fnm == NULL)
3995 1.1 skrll goto error_free_dyn;
3996 1.1 skrll amt = strlen (fnm) + 1;
3997 1.1 skrll anm = (char *) bfd_alloc (abfd, amt);
3998 1.1 skrll if (anm == NULL)
3999 1.1 skrll goto error_free_dyn;
4000 1.1 skrll memcpy (anm, fnm, amt);
4001 1.1 skrll n->name = anm;
4002 1.1 skrll n->by = abfd;
4003 1.1 skrll n->next = NULL;
4004 1.1 skrll for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4005 1.1 skrll ;
4006 1.4 christos *pn = n;
4007 1.1 skrll }
4008 1.1 skrll if (dyn.d_tag == DT_RUNPATH)
4009 1.1 skrll {
4010 1.1 skrll struct bfd_link_needed_list *n, **pn;
4011 1.4 christos char *fnm, *anm;
4012 1.1 skrll unsigned int tagv = dyn.d_un.d_val;
4013 1.1 skrll
4014 1.1 skrll amt = sizeof (struct bfd_link_needed_list);
4015 1.1 skrll n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4016 1.1 skrll fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4017 1.1 skrll if (n == NULL || fnm == NULL)
4018 1.1 skrll goto error_free_dyn;
4019 1.1 skrll amt = strlen (fnm) + 1;
4020 1.1 skrll anm = (char *) bfd_alloc (abfd, amt);
4021 1.1 skrll if (anm == NULL)
4022 1.1 skrll goto error_free_dyn;
4023 1.1 skrll memcpy (anm, fnm, amt);
4024 1.1 skrll n->name = anm;
4025 1.1 skrll n->by = abfd;
4026 1.1 skrll n->next = NULL;
4027 1.1 skrll for (pn = & runpath;
4028 1.1 skrll *pn != NULL;
4029 1.1 skrll pn = &(*pn)->next)
4030 1.1 skrll ;
4031 1.1 skrll *pn = n;
4032 1.4 christos }
4033 1.1 skrll /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4034 1.1 skrll if (!runpath && dyn.d_tag == DT_RPATH)
4035 1.1 skrll {
4036 1.1 skrll struct bfd_link_needed_list *n, **pn;
4037 1.4 christos char *fnm, *anm;
4038 1.1 skrll unsigned int tagv = dyn.d_un.d_val;
4039 1.4 christos
4040 1.1 skrll amt = sizeof (struct bfd_link_needed_list);
4041 1.1 skrll n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4042 1.1 skrll fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4043 1.1 skrll if (n == NULL || fnm == NULL)
4044 1.1 skrll goto error_free_dyn;
4045 1.1 skrll amt = strlen (fnm) + 1;
4046 1.1 skrll anm = (char *) bfd_alloc (abfd, amt);
4047 1.1 skrll if (anm == NULL)
4048 1.1 skrll goto error_free_dyn;
4049 1.1 skrll memcpy (anm, fnm, amt);
4050 1.4 christos n->name = anm;
4051 1.4 christos n->by = abfd;
4052 1.4 christos n->next = NULL;
4053 1.4 christos for (pn = & rpath;
4054 1.4 christos *pn != NULL;
4055 1.1 skrll pn = &(*pn)->next)
4056 1.1 skrll ;
4057 1.1 skrll *pn = n;
4058 1.1 skrll }
4059 1.1 skrll if (dyn.d_tag == DT_AUDIT)
4060 1.1 skrll {
4061 1.1 skrll unsigned int tagv = dyn.d_un.d_val;
4062 1.1 skrll audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4063 1.1 skrll }
4064 1.1 skrll }
4065 1.1 skrll
4066 1.1 skrll free (dynbuf);
4067 1.1 skrll }
4068 1.1 skrll
4069 1.1 skrll /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4070 1.1 skrll frees all more recently bfd_alloc'd blocks as well. */
4071 1.1 skrll if (runpath)
4072 1.1 skrll rpath = runpath;
4073 1.13.12.2 pgoyette
4074 1.13.12.2 pgoyette if (rpath)
4075 1.13.12.2 pgoyette {
4076 1.13.12.2 pgoyette struct bfd_link_needed_list **pn;
4077 1.13.12.2 pgoyette for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4078 1.13.12.2 pgoyette ;
4079 1.13.12.2 pgoyette *pn = rpath;
4080 1.13.12.2 pgoyette }
4081 1.13.12.2 pgoyette
4082 1.13.12.2 pgoyette /* If we have a PT_GNU_RELRO program header, mark as read-only
4083 1.13.12.2 pgoyette all sections contained fully therein. This makes relro
4084 1.13.12.2 pgoyette shared library sections appear as they will at run-time. */
4085 1.13.12.2 pgoyette phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4086 1.13.12.2 pgoyette while (--phdr >= elf_tdata (abfd)->phdr)
4087 1.13.12.2 pgoyette if (phdr->p_type == PT_GNU_RELRO)
4088 1.1 skrll {
4089 1.1 skrll for (s = abfd->sections; s != NULL; s = s->next)
4090 1.1 skrll if ((s->flags & SEC_ALLOC) != 0
4091 1.1 skrll && s->vma >= phdr->p_vaddr
4092 1.1 skrll && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4093 1.1 skrll s->flags |= SEC_READONLY;
4094 1.1 skrll break;
4095 1.1 skrll }
4096 1.1 skrll
4097 1.1 skrll /* We do not want to include any of the sections in a dynamic
4098 1.1 skrll object in the output file. We hack by simply clobbering the
4099 1.1 skrll list of sections in the BFD. This could be handled more
4100 1.1 skrll cleanly by, say, a new section flag; the existing
4101 1.1 skrll SEC_NEVER_LOAD flag is not the one we want, because that one
4102 1.1 skrll still implies that the section takes up space in the output
4103 1.1 skrll file. */
4104 1.1 skrll bfd_section_list_clear (abfd);
4105 1.1 skrll
4106 1.1 skrll /* Find the name to use in a DT_NEEDED entry that refers to this
4107 1.1 skrll object. If the object has a DT_SONAME entry, we use it.
4108 1.1 skrll Otherwise, if the generic linker stuck something in
4109 1.1 skrll elf_dt_name, we use that. Otherwise, we just use the file
4110 1.1 skrll name. */
4111 1.1 skrll if (soname == NULL || *soname == '\0')
4112 1.1 skrll {
4113 1.1 skrll soname = elf_dt_name (abfd);
4114 1.1 skrll if (soname == NULL || *soname == '\0')
4115 1.1 skrll soname = bfd_get_filename (abfd);
4116 1.1 skrll }
4117 1.1 skrll
4118 1.1 skrll /* Save the SONAME because sometimes the linker emulation code
4119 1.1 skrll will need to know it. */
4120 1.1 skrll elf_dt_name (abfd) = soname;
4121 1.1 skrll
4122 1.4 christos ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4123 1.4 christos if (ret < 0)
4124 1.9 christos goto error_return;
4125 1.1 skrll
4126 1.1 skrll /* If we have already included this dynamic object in the
4127 1.1 skrll link, just ignore it. There is no reason to include a
4128 1.1 skrll particular dynamic object more than once. */
4129 1.1 skrll if (ret > 0)
4130 1.1 skrll return TRUE;
4131 1.1 skrll
4132 1.1 skrll /* Save the DT_AUDIT entry for the linker emulation code. */
4133 1.1 skrll elf_dt_audit (abfd) = audit;
4134 1.1 skrll }
4135 1.1 skrll
4136 1.1 skrll /* If this is a dynamic object, we always link against the .dynsym
4137 1.1 skrll symbol table, not the .symtab symbol table. The dynamic linker
4138 1.1 skrll will only see the .dynsym symbol table, so there is no reason to
4139 1.1 skrll look at .symtab for a dynamic object. */
4140 1.1 skrll
4141 1.1 skrll if (! dynamic || elf_dynsymtab (abfd) == 0)
4142 1.1 skrll hdr = &elf_tdata (abfd)->symtab_hdr;
4143 1.1 skrll else
4144 1.1 skrll hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4145 1.1 skrll
4146 1.1 skrll symcount = hdr->sh_size / bed->s->sizeof_sym;
4147 1.1 skrll
4148 1.1 skrll /* The sh_info field of the symtab header tells us where the
4149 1.1 skrll external symbols start. We don't care about the local symbols at
4150 1.1 skrll this point. */
4151 1.1 skrll if (elf_bad_symtab (abfd))
4152 1.1 skrll {
4153 1.9 christos extsymcount = symcount;
4154 1.1 skrll extsymoff = 0;
4155 1.1 skrll }
4156 1.1 skrll else
4157 1.1 skrll {
4158 1.1 skrll extsymcount = symcount - hdr->sh_info;
4159 1.1 skrll extsymoff = hdr->sh_info;
4160 1.1 skrll }
4161 1.1 skrll
4162 1.9 christos sym_hash = elf_sym_hashes (abfd);
4163 1.9 christos if (extsymcount != 0)
4164 1.9 christos {
4165 1.13 christos isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4166 1.13 christos NULL, NULL, NULL);
4167 1.9 christos if (isymbuf == NULL)
4168 1.9 christos goto error_return;
4169 1.9 christos
4170 1.9 christos if (sym_hash == NULL)
4171 1.9 christos {
4172 1.1 skrll /* We store a pointer to the hash table entry for each
4173 1.1 skrll external symbol. */
4174 1.1 skrll amt = extsymcount;
4175 1.1 skrll amt *= sizeof (struct elf_link_hash_entry *);
4176 1.1 skrll sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4177 1.1 skrll if (sym_hash == NULL)
4178 1.1 skrll goto error_free_sym;
4179 1.1 skrll elf_sym_hashes (abfd) = sym_hash;
4180 1.1 skrll }
4181 1.1 skrll }
4182 1.1 skrll
4183 1.1 skrll if (dynamic)
4184 1.1 skrll {
4185 1.1 skrll /* Read in any version definitions. */
4186 1.1 skrll if (!_bfd_elf_slurp_version_tables (abfd,
4187 1.1 skrll info->default_imported_symver))
4188 1.4 christos goto error_free_sym;
4189 1.1 skrll
4190 1.1 skrll /* Read in the symbol versions, but don't bother to convert them
4191 1.1 skrll to internal format. */
4192 1.1 skrll if (elf_dynversym (abfd) != 0)
4193 1.1 skrll {
4194 1.1 skrll Elf_Internal_Shdr *versymhdr;
4195 1.1 skrll
4196 1.1 skrll versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4197 1.1 skrll extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4198 1.1 skrll if (extversym == NULL)
4199 1.1 skrll goto error_free_sym;
4200 1.1 skrll amt = versymhdr->sh_size;
4201 1.1 skrll if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4202 1.1 skrll || bfd_bread (extversym, amt, abfd) != amt)
4203 1.1 skrll goto error_free_vers;
4204 1.1 skrll }
4205 1.1 skrll }
4206 1.1 skrll
4207 1.1 skrll /* If we are loading an as-needed shared lib, save the symbol table
4208 1.1 skrll state before we start adding symbols. If the lib turns out
4209 1.1 skrll to be unneeded, restore the state. */
4210 1.1 skrll if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4211 1.1 skrll {
4212 1.1 skrll unsigned int i;
4213 1.1 skrll size_t entsize;
4214 1.1 skrll
4215 1.1 skrll for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4216 1.1 skrll {
4217 1.1 skrll struct bfd_hash_entry *p;
4218 1.1 skrll struct elf_link_hash_entry *h;
4219 1.1 skrll
4220 1.1 skrll for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4221 1.9 christos {
4222 1.1 skrll h = (struct elf_link_hash_entry *) p;
4223 1.1 skrll entsize += htab->root.table.entsize;
4224 1.1 skrll if (h->root.type == bfd_link_hash_warning)
4225 1.1 skrll entsize += htab->root.table.entsize;
4226 1.1 skrll }
4227 1.1 skrll }
4228 1.1 skrll
4229 1.1 skrll tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4230 1.1 skrll old_tab = bfd_malloc (tabsize + entsize);
4231 1.1 skrll if (old_tab == NULL)
4232 1.1 skrll goto error_free_vers;
4233 1.9 christos
4234 1.1 skrll /* Remember the current objalloc pointer, so that all mem for
4235 1.1 skrll symbols added can later be reclaimed. */
4236 1.9 christos alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4237 1.9 christos if (alloc_mark == NULL)
4238 1.9 christos goto error_free_vers;
4239 1.1 skrll
4240 1.1 skrll /* Make a special call to the linker "notice" function to
4241 1.1 skrll tell it that we are about to handle an as-needed lib. */
4242 1.1 skrll if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4243 1.1 skrll goto error_free_vers;
4244 1.1 skrll
4245 1.12 christos /* Clone the symbol table. Remember some pointers into the
4246 1.12 christos symbol table, and dynamic symbol count. */
4247 1.12 christos old_ent = (char *) old_tab + tabsize;
4248 1.1 skrll memcpy (old_tab, htab->root.table.table, tabsize);
4249 1.1 skrll old_undefs = htab->root.undefs;
4250 1.1 skrll old_undefs_tail = htab->root.undefs_tail;
4251 1.1 skrll old_table = htab->root.table.table;
4252 1.1 skrll old_size = htab->root.table.size;
4253 1.1 skrll old_count = htab->root.table.count;
4254 1.1 skrll old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4255 1.1 skrll if (old_strtab == NULL)
4256 1.1 skrll goto error_free_vers;
4257 1.1 skrll
4258 1.1 skrll for (i = 0; i < htab->root.table.size; i++)
4259 1.1 skrll {
4260 1.1 skrll struct bfd_hash_entry *p;
4261 1.1 skrll struct elf_link_hash_entry *h;
4262 1.1 skrll
4263 1.1 skrll for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4264 1.1 skrll {
4265 1.1 skrll memcpy (old_ent, p, htab->root.table.entsize);
4266 1.1 skrll old_ent = (char *) old_ent + htab->root.table.entsize;
4267 1.1 skrll h = (struct elf_link_hash_entry *) p;
4268 1.1 skrll if (h->root.type == bfd_link_hash_warning)
4269 1.1 skrll {
4270 1.1 skrll memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4271 1.1 skrll old_ent = (char *) old_ent + htab->root.table.entsize;
4272 1.1 skrll }
4273 1.1 skrll }
4274 1.1 skrll }
4275 1.1 skrll }
4276 1.1 skrll
4277 1.1 skrll weaks = NULL;
4278 1.1 skrll ever = extversym != NULL ? extversym + extsymoff : NULL;
4279 1.1 skrll for (isym = isymbuf, isymend = isymbuf + extsymcount;
4280 1.7 christos isym < isymend;
4281 1.1 skrll isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4282 1.1 skrll {
4283 1.1 skrll int bind;
4284 1.7 christos bfd_vma value;
4285 1.7 christos asection *sec, *new_sec;
4286 1.1 skrll flagword flags;
4287 1.1 skrll const char *name;
4288 1.13 christos struct elf_link_hash_entry *h;
4289 1.1 skrll struct elf_link_hash_entry *hi;
4290 1.1 skrll bfd_boolean definition;
4291 1.9 christos bfd_boolean size_change_ok;
4292 1.1 skrll bfd_boolean type_change_ok;
4293 1.1 skrll bfd_boolean new_weak;
4294 1.1 skrll bfd_boolean old_weak;
4295 1.1 skrll bfd_boolean override;
4296 1.1 skrll bfd_boolean common;
4297 1.1 skrll bfd_boolean discarded;
4298 1.1 skrll unsigned int old_alignment;
4299 1.13.12.2 pgoyette bfd *old_bfd;
4300 1.13.12.2 pgoyette bfd_boolean matched;
4301 1.13.12.2 pgoyette
4302 1.13.12.2 pgoyette override = FALSE;
4303 1.13.12.2 pgoyette
4304 1.13.12.2 pgoyette flags = BSF_NO_FLAGS;
4305 1.13 christos sec = NULL;
4306 1.1 skrll value = isym->st_value;
4307 1.1 skrll common = bed->common_definition (isym);
4308 1.4 christos if (common && info->inhibit_common_definition)
4309 1.1 skrll {
4310 1.4 christos /* Treat common symbol as undefined for --no-define-common. */
4311 1.1 skrll isym->st_shndx = SHN_UNDEF;
4312 1.1 skrll common = FALSE;
4313 1.1 skrll }
4314 1.1 skrll discarded = FALSE;
4315 1.1 skrll
4316 1.4 christos bind = ELF_ST_BIND (isym->st_info);
4317 1.4 christos switch (bind)
4318 1.1 skrll {
4319 1.1 skrll case STB_LOCAL:
4320 1.4 christos /* This should be impossible, since ELF requires that all
4321 1.4 christos global symbols follow all local symbols, and that sh_info
4322 1.4 christos point to the first global symbol. Unfortunately, Irix 5
4323 1.4 christos screws this up. */
4324 1.4 christos continue;
4325 1.4 christos
4326 1.4 christos case STB_GLOBAL:
4327 1.4 christos if (isym->st_shndx != SHN_UNDEF && !common)
4328 1.4 christos flags = BSF_GLOBAL;
4329 1.4 christos break;
4330 1.4 christos
4331 1.1 skrll case STB_WEAK:
4332 1.4 christos flags = BSF_WEAK;
4333 1.1 skrll break;
4334 1.1 skrll
4335 1.1 skrll case STB_GNU_UNIQUE:
4336 1.1 skrll flags = BSF_GNU_UNIQUE;
4337 1.1 skrll break;
4338 1.1 skrll
4339 1.1 skrll default:
4340 1.1 skrll /* Leave it up to the processor backend. */
4341 1.1 skrll break;
4342 1.1 skrll }
4343 1.1 skrll
4344 1.1 skrll if (isym->st_shndx == SHN_UNDEF)
4345 1.1 skrll sec = bfd_und_section_ptr;
4346 1.1 skrll else if (isym->st_shndx == SHN_ABS)
4347 1.1 skrll sec = bfd_abs_section_ptr;
4348 1.1 skrll else if (isym->st_shndx == SHN_COMMON)
4349 1.1 skrll {
4350 1.1 skrll sec = bfd_com_section_ptr;
4351 1.7 christos /* What ELF calls the size we call the value. What ELF
4352 1.1 skrll calls the value we call the alignment. */
4353 1.1 skrll value = isym->st_size;
4354 1.1 skrll }
4355 1.1 skrll else
4356 1.13 christos {
4357 1.1 skrll sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4358 1.1 skrll if (sec == NULL)
4359 1.1 skrll sec = bfd_abs_section_ptr;
4360 1.1 skrll else if (discarded_section (sec))
4361 1.1 skrll {
4362 1.1 skrll /* Symbols from discarded section are undefined. We keep
4363 1.1 skrll its visibility. */
4364 1.1 skrll sec = bfd_und_section_ptr;
4365 1.1 skrll discarded = TRUE;
4366 1.1 skrll isym->st_shndx = SHN_UNDEF;
4367 1.1 skrll }
4368 1.1 skrll else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4369 1.4 christos value -= sec->vma;
4370 1.4 christos }
4371 1.4 christos
4372 1.4 christos name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4373 1.4 christos isym->st_name);
4374 1.4 christos if (name == NULL)
4375 1.4 christos goto error_free_vers;
4376 1.4 christos
4377 1.4 christos if (isym->st_shndx == SHN_COMMON
4378 1.4 christos && (abfd->flags & BFD_PLUGIN) != 0)
4379 1.4 christos {
4380 1.4 christos asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4381 1.4 christos
4382 1.4 christos if (xc == NULL)
4383 1.4 christos {
4384 1.4 christos flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4385 1.9 christos | SEC_EXCLUDE);
4386 1.1 skrll xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4387 1.1 skrll if (xc == NULL)
4388 1.1 skrll goto error_free_vers;
4389 1.1 skrll }
4390 1.1 skrll sec = xc;
4391 1.4 christos }
4392 1.4 christos else if (isym->st_shndx == SHN_COMMON
4393 1.4 christos && ELF_ST_TYPE (isym->st_info) == STT_TLS
4394 1.1 skrll && !bfd_link_relocatable (info))
4395 1.1 skrll {
4396 1.1 skrll asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4397 1.1 skrll
4398 1.1 skrll if (tcomm == NULL)
4399 1.1 skrll {
4400 1.1 skrll flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4401 1.1 skrll | SEC_LINKER_CREATED);
4402 1.1 skrll tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4403 1.1 skrll if (tcomm == NULL)
4404 1.1 skrll goto error_free_vers;
4405 1.1 skrll }
4406 1.1 skrll sec = tcomm;
4407 1.1 skrll }
4408 1.1 skrll else if (bed->elf_add_symbol_hook)
4409 1.1 skrll {
4410 1.1 skrll if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4411 1.1 skrll &sec, &value))
4412 1.1 skrll goto error_free_vers;
4413 1.1 skrll
4414 1.1 skrll /* The hook function sets the name to NULL if this symbol
4415 1.1 skrll should be skipped for some reason. */
4416 1.1 skrll if (name == NULL)
4417 1.1 skrll continue;
4418 1.9 christos }
4419 1.9 christos
4420 1.9 christos /* Sanity check that all possibilities were handled. */
4421 1.9 christos if (sec == NULL)
4422 1.9 christos {
4423 1.9 christos bfd_set_error (bfd_error_bad_value);
4424 1.9 christos goto error_free_vers;
4425 1.1 skrll }
4426 1.1 skrll
4427 1.1 skrll /* Silently discard TLS symbols from --just-syms. There's
4428 1.1 skrll no way to combine a static TLS block with a new TLS block
4429 1.1 skrll for this executable. */
4430 1.1 skrll if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4431 1.1 skrll && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4432 1.1 skrll continue;
4433 1.7 christos
4434 1.9 christos if (bfd_is_und_section (sec)
4435 1.1 skrll || bfd_is_com_section (sec))
4436 1.1 skrll definition = FALSE;
4437 1.1 skrll else
4438 1.1 skrll definition = TRUE;
4439 1.1 skrll
4440 1.1 skrll size_change_ok = FALSE;
4441 1.1 skrll type_change_ok = bed->type_change_ok;
4442 1.1 skrll old_weak = FALSE;
4443 1.1 skrll matched = FALSE;
4444 1.1 skrll old_alignment = 0;
4445 1.1 skrll old_bfd = NULL;
4446 1.1 skrll new_sec = sec;
4447 1.1 skrll
4448 1.1 skrll if (is_elf_hash_table (htab))
4449 1.1 skrll {
4450 1.1 skrll Elf_Internal_Versym iver;
4451 1.1 skrll unsigned int vernum = 0;
4452 1.1 skrll bfd_boolean skip;
4453 1.1 skrll
4454 1.1 skrll if (ever == NULL)
4455 1.1 skrll {
4456 1.1 skrll if (info->default_imported_symver)
4457 1.1 skrll /* Use the default symbol version created earlier. */
4458 1.1 skrll iver.vs_vers = elf_tdata (abfd)->cverdefs;
4459 1.1 skrll else
4460 1.1 skrll iver.vs_vers = 0;
4461 1.1 skrll }
4462 1.1 skrll else
4463 1.1 skrll _bfd_elf_swap_versym_in (abfd, ever, &iver);
4464 1.1 skrll
4465 1.1 skrll vernum = iver.vs_vers & VERSYM_VERSION;
4466 1.1 skrll
4467 1.1 skrll /* If this is a hidden symbol, or if it is not version
4468 1.1 skrll 1, we append the version name to the symbol name.
4469 1.1 skrll However, we do not modify a non-hidden absolute symbol
4470 1.1 skrll if it is not a function, because it might be the version
4471 1.1 skrll symbol itself. FIXME: What if it isn't? */
4472 1.1 skrll if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4473 1.1 skrll || (vernum > 1
4474 1.1 skrll && (!bfd_is_abs_section (sec)
4475 1.1 skrll || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4476 1.1 skrll {
4477 1.1 skrll const char *verstr;
4478 1.1 skrll size_t namelen, verlen, newlen;
4479 1.1 skrll char *newname, *p;
4480 1.1 skrll
4481 1.1 skrll if (isym->st_shndx != SHN_UNDEF)
4482 1.1 skrll {
4483 1.1 skrll if (vernum > elf_tdata (abfd)->cverdefs)
4484 1.13.12.2 pgoyette verstr = NULL;
4485 1.13.12.2 pgoyette else if (vernum > 1)
4486 1.1 skrll verstr =
4487 1.1 skrll elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4488 1.1 skrll else
4489 1.1 skrll verstr = "";
4490 1.1 skrll
4491 1.1 skrll if (verstr == NULL)
4492 1.1 skrll {
4493 1.1 skrll _bfd_error_handler
4494 1.1 skrll /* xgettext:c-format */
4495 1.1 skrll (_("%B: %s: invalid version %u (max %d)"),
4496 1.1 skrll abfd, name, vernum,
4497 1.1 skrll elf_tdata (abfd)->cverdefs);
4498 1.1 skrll bfd_set_error (bfd_error_bad_value);
4499 1.1 skrll goto error_free_vers;
4500 1.1 skrll }
4501 1.1 skrll }
4502 1.1 skrll else
4503 1.1 skrll {
4504 1.1 skrll /* We cannot simply test for the number of
4505 1.1 skrll entries in the VERNEED section since the
4506 1.1 skrll numbers for the needed versions do not start
4507 1.1 skrll at 0. */
4508 1.1 skrll Elf_Internal_Verneed *t;
4509 1.1 skrll
4510 1.1 skrll verstr = NULL;
4511 1.1 skrll for (t = elf_tdata (abfd)->verref;
4512 1.1 skrll t != NULL;
4513 1.1 skrll t = t->vn_nextref)
4514 1.1 skrll {
4515 1.1 skrll Elf_Internal_Vernaux *a;
4516 1.1 skrll
4517 1.1 skrll for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4518 1.1 skrll {
4519 1.1 skrll if (a->vna_other == vernum)
4520 1.1 skrll {
4521 1.13.12.2 pgoyette verstr = a->vna_nodename;
4522 1.13.12.2 pgoyette break;
4523 1.1 skrll }
4524 1.1 skrll }
4525 1.1 skrll if (a != NULL)
4526 1.1 skrll break;
4527 1.1 skrll }
4528 1.1 skrll if (verstr == NULL)
4529 1.1 skrll {
4530 1.1 skrll _bfd_error_handler
4531 1.1 skrll /* xgettext:c-format */
4532 1.1 skrll (_("%B: %s: invalid needed version %d"),
4533 1.1 skrll abfd, name, vernum);
4534 1.1 skrll bfd_set_error (bfd_error_bad_value);
4535 1.1 skrll goto error_free_vers;
4536 1.1 skrll }
4537 1.4 christos }
4538 1.1 skrll
4539 1.1 skrll namelen = strlen (name);
4540 1.1 skrll verlen = strlen (verstr);
4541 1.1 skrll newlen = namelen + verlen + 2;
4542 1.1 skrll if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4543 1.1 skrll && isym->st_shndx != SHN_UNDEF)
4544 1.1 skrll ++newlen;
4545 1.1 skrll
4546 1.1 skrll newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4547 1.1 skrll if (newname == NULL)
4548 1.1 skrll goto error_free_vers;
4549 1.1 skrll memcpy (newname, name, namelen);
4550 1.1 skrll p = newname + namelen;
4551 1.1 skrll *p++ = ELF_VER_CHR;
4552 1.1 skrll /* If this is a defined non-hidden version symbol,
4553 1.1 skrll we add another @ to the name. This indicates the
4554 1.9 christos default version of the symbol. */
4555 1.9 christos if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4556 1.9 christos && isym->st_shndx != SHN_UNDEF)
4557 1.9 christos *p++ = ELF_VER_CHR;
4558 1.9 christos memcpy (p, verstr, verlen + 1);
4559 1.9 christos
4560 1.9 christos name = newname;
4561 1.9 christos }
4562 1.9 christos
4563 1.9 christos /* If this symbol has default visibility and the user has
4564 1.9 christos requested we not re-export it, then mark it as hidden. */
4565 1.9 christos if (!bfd_is_und_section (sec)
4566 1.9 christos && !dynamic
4567 1.9 christos && abfd->no_export
4568 1.1 skrll && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4569 1.1 skrll isym->st_other = (STV_HIDDEN
4570 1.1 skrll | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4571 1.1 skrll
4572 1.1 skrll if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4573 1.9 christos sym_hash, &old_bfd, &old_weak,
4574 1.9 christos &old_alignment, &skip, &override,
4575 1.9 christos &type_change_ok, &size_change_ok,
4576 1.1 skrll &matched))
4577 1.1 skrll goto error_free_vers;
4578 1.1 skrll
4579 1.1 skrll if (skip)
4580 1.1 skrll continue;
4581 1.1 skrll
4582 1.1 skrll /* Override a definition only if the new symbol matches the
4583 1.1 skrll existing one. */
4584 1.1 skrll if (override && matched)
4585 1.1 skrll definition = FALSE;
4586 1.1 skrll
4587 1.1 skrll h = *sym_hash;
4588 1.1 skrll while (h->root.type == bfd_link_hash_indirect
4589 1.1 skrll || h->root.type == bfd_link_hash_warning)
4590 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
4591 1.1 skrll
4592 1.1 skrll if (elf_tdata (abfd)->verdef != NULL
4593 1.1 skrll && vernum > 1
4594 1.13 christos && definition)
4595 1.13 christos h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4596 1.13 christos }
4597 1.13 christos
4598 1.13 christos if (! (_bfd_generic_link_add_one_symbol
4599 1.1 skrll (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4600 1.7 christos (struct bfd_link_hash_entry **) sym_hash)))
4601 1.7 christos goto error_free_vers;
4602 1.7 christos
4603 1.1 skrll if ((flags & BSF_GNU_UNIQUE)
4604 1.1 skrll && (abfd->flags & DYNAMIC) == 0
4605 1.1 skrll && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4606 1.4 christos elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4607 1.13 christos
4608 1.13 christos h = *sym_hash;
4609 1.13 christos /* We need to make sure that indirect symbol dynamic flags are
4610 1.13 christos updated. */
4611 1.13 christos hi = h;
4612 1.1 skrll while (h->root.type == bfd_link_hash_indirect
4613 1.1 skrll || h->root.type == bfd_link_hash_warning)
4614 1.7 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
4615 1.1 skrll
4616 1.1 skrll /* Setting the index to -3 tells elf_link_output_extsym that
4617 1.7 christos this symbol is defined in a discarded section. */
4618 1.1 skrll if (discarded)
4619 1.1 skrll h->indx = -3;
4620 1.13.12.2 pgoyette
4621 1.1 skrll *sym_hash = h;
4622 1.1 skrll
4623 1.13.12.2 pgoyette new_weak = (flags & BSF_WEAK) != 0;
4624 1.13.12.2 pgoyette if (dynamic
4625 1.1 skrll && definition
4626 1.1 skrll && new_weak
4627 1.1 skrll && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4628 1.1 skrll && is_elf_hash_table (htab)
4629 1.13.12.2 pgoyette && h->u.alias == NULL)
4630 1.1 skrll {
4631 1.1 skrll /* Keep a list of all weak defined non function symbols from
4632 1.1 skrll a dynamic object, using the alias field. Later in this
4633 1.1 skrll function we will set the alias field to the correct
4634 1.13.12.2 pgoyette value. We only put non-function symbols from dynamic
4635 1.1 skrll objects on this list, because that happens to be the only
4636 1.1 skrll time we need to know the normal symbol corresponding to a
4637 1.1 skrll weak symbol, and the information is time consuming to
4638 1.1 skrll figure out. If the alias field is not already NULL,
4639 1.1 skrll then this symbol was already defined by some previous
4640 1.1 skrll dynamic object, and we will be using that previous
4641 1.1 skrll definition anyhow. */
4642 1.1 skrll
4643 1.1 skrll h->u.alias = weaks;
4644 1.1 skrll weaks = h;
4645 1.1 skrll }
4646 1.1 skrll
4647 1.1 skrll /* Set the alignment of a common symbol. */
4648 1.1 skrll if ((common || bfd_is_com_section (sec))
4649 1.1 skrll && h->root.type == bfd_link_hash_common)
4650 1.1 skrll {
4651 1.1 skrll unsigned int align;
4652 1.4 christos
4653 1.1 skrll if (common)
4654 1.1 skrll align = bfd_log2 (isym->st_value);
4655 1.1 skrll else
4656 1.1 skrll {
4657 1.1 skrll /* The new symbol is a common symbol in a shared object.
4658 1.1 skrll We need to get the alignment from the section. */
4659 1.1 skrll align = new_sec->alignment_power;
4660 1.9 christos }
4661 1.9 christos if (align > old_alignment)
4662 1.9 christos h->root.u.c.p->alignment_power = align;
4663 1.9 christos else
4664 1.9 christos h->root.u.c.p->alignment_power = old_alignment;
4665 1.9 christos }
4666 1.9 christos
4667 1.9 christos if (is_elf_hash_table (htab))
4668 1.9 christos {
4669 1.9 christos /* Set a flag in the hash table entry indicating the type of
4670 1.9 christos reference or definition we just found. A dynamic symbol
4671 1.9 christos is one which is referenced or defined by both a regular
4672 1.9 christos object and a shared object. */
4673 1.9 christos bfd_boolean dynsym = FALSE;
4674 1.9 christos
4675 1.9 christos /* Plugin symbols aren't normal. Don't set def_regular or
4676 1.9 christos ref_regular for them, or make them dynamic. */
4677 1.9 christos if ((abfd->flags & BFD_PLUGIN) != 0)
4678 1.9 christos ;
4679 1.9 christos else if (! dynamic)
4680 1.9 christos {
4681 1.9 christos if (! definition)
4682 1.9 christos {
4683 1.9 christos h->ref_regular = 1;
4684 1.9 christos if (bind != STB_WEAK)
4685 1.9 christos h->ref_regular_nonweak = 1;
4686 1.9 christos }
4687 1.9 christos else
4688 1.9 christos {
4689 1.9 christos h->def_regular = 1;
4690 1.9 christos if (h->def_dynamic)
4691 1.9 christos {
4692 1.9 christos h->def_dynamic = 0;
4693 1.9 christos h->ref_dynamic = 1;
4694 1.9 christos }
4695 1.9 christos }
4696 1.9 christos
4697 1.9 christos /* If the indirect symbol has been forced local, don't
4698 1.9 christos make the real symbol dynamic. */
4699 1.9 christos if ((h == hi || !hi->forced_local)
4700 1.9 christos && (bfd_link_dll (info)
4701 1.9 christos || h->def_dynamic
4702 1.9 christos || h->ref_dynamic))
4703 1.9 christos dynsym = TRUE;
4704 1.9 christos }
4705 1.9 christos else
4706 1.9 christos {
4707 1.9 christos if (! definition)
4708 1.9 christos {
4709 1.9 christos h->ref_dynamic = 1;
4710 1.9 christos hi->ref_dynamic = 1;
4711 1.9 christos }
4712 1.9 christos else
4713 1.9 christos {
4714 1.13.12.2 pgoyette h->def_dynamic = 1;
4715 1.13.12.2 pgoyette hi->def_dynamic = 1;
4716 1.9 christos }
4717 1.9 christos
4718 1.9 christos /* If the indirect symbol has been forced local, don't
4719 1.9 christos make the real symbol dynamic. */
4720 1.9 christos if ((h == hi || !hi->forced_local)
4721 1.9 christos && (h->def_regular
4722 1.9 christos || h->ref_regular
4723 1.9 christos || (h->is_weakalias
4724 1.9 christos && weakdef (h)->dynindx != -1)))
4725 1.9 christos dynsym = TRUE;
4726 1.1 skrll }
4727 1.1 skrll
4728 1.1 skrll /* Check to see if we need to add an indirect symbol for
4729 1.1 skrll the default name. */
4730 1.1 skrll if (definition
4731 1.1 skrll || (!override && h->root.type == bfd_link_hash_common))
4732 1.1 skrll if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4733 1.1 skrll sec, value, &old_bfd, &dynsym))
4734 1.1 skrll goto error_free_vers;
4735 1.1 skrll
4736 1.1 skrll /* Check the alignment when a common symbol is involved. This
4737 1.1 skrll can change when a common symbol is overridden by a normal
4738 1.1 skrll definition or a common symbol is ignored due to the old
4739 1.1 skrll normal definition. We need to make sure the maximum
4740 1.1 skrll alignment is maintained. */
4741 1.9 christos if ((old_alignment || common)
4742 1.9 christos && h->root.type != bfd_link_hash_common)
4743 1.9 christos {
4744 1.1 skrll unsigned int common_align;
4745 1.1 skrll unsigned int normal_align;
4746 1.13 christos unsigned int symbol_align;
4747 1.13 christos bfd *normal_bfd;
4748 1.1 skrll bfd *common_bfd;
4749 1.1 skrll
4750 1.1 skrll BFD_ASSERT (h->root.type == bfd_link_hash_defined
4751 1.1 skrll || h->root.type == bfd_link_hash_defweak);
4752 1.1 skrll
4753 1.1 skrll symbol_align = ffs (h->root.u.def.value) - 1;
4754 1.1 skrll if (h->root.u.def.section->owner != NULL
4755 1.1 skrll && (h->root.u.def.section->owner->flags
4756 1.1 skrll & (DYNAMIC | BFD_PLUGIN)) == 0)
4757 1.1 skrll {
4758 1.1 skrll normal_align = h->root.u.def.section->alignment_power;
4759 1.1 skrll if (normal_align > symbol_align)
4760 1.1 skrll normal_align = symbol_align;
4761 1.1 skrll }
4762 1.1 skrll else
4763 1.1 skrll normal_align = symbol_align;
4764 1.1 skrll
4765 1.1 skrll if (old_alignment)
4766 1.1 skrll {
4767 1.1 skrll common_align = old_alignment;
4768 1.1 skrll common_bfd = old_bfd;
4769 1.1 skrll normal_bfd = abfd;
4770 1.1 skrll }
4771 1.1 skrll else
4772 1.1 skrll {
4773 1.13.12.2 pgoyette common_align = bfd_log2 (isym->st_value);
4774 1.13.12.2 pgoyette common_bfd = abfd;
4775 1.9 christos normal_bfd = old_bfd;
4776 1.9 christos }
4777 1.13.12.2 pgoyette
4778 1.13.12.2 pgoyette if (normal_align < common_align)
4779 1.1 skrll {
4780 1.13.12.2 pgoyette /* PR binutils/2735 */
4781 1.13.12.2 pgoyette if (normal_bfd == NULL)
4782 1.1 skrll _bfd_error_handler
4783 1.1 skrll /* xgettext:c-format */
4784 1.13.12.2 pgoyette (_("Warning: alignment %u of common symbol `%s' in %B is"
4785 1.13.12.2 pgoyette " greater than the alignment (%u) of its section %A"),
4786 1.1 skrll 1 << common_align, name, common_bfd,
4787 1.1 skrll 1 << normal_align, h->root.u.def.section);
4788 1.1 skrll else
4789 1.1 skrll _bfd_error_handler
4790 1.9 christos /* xgettext:c-format */
4791 1.9 christos (_("Warning: alignment %u of symbol `%s' in %B"
4792 1.1 skrll " is smaller than %u in %B"),
4793 1.1 skrll 1 << normal_align, name, normal_bfd,
4794 1.1 skrll 1 << common_align, common_bfd);
4795 1.1 skrll }
4796 1.1 skrll }
4797 1.13.12.2 pgoyette
4798 1.13.12.2 pgoyette /* Remember the symbol size if it isn't undefined. */
4799 1.1 skrll if (isym->st_size != 0
4800 1.13.12.2 pgoyette && isym->st_shndx != SHN_UNDEF
4801 1.13.12.2 pgoyette && (definition || h->size == 0))
4802 1.1 skrll {
4803 1.1 skrll if (h->size != 0
4804 1.1 skrll && h->size != isym->st_size
4805 1.1 skrll && ! size_change_ok)
4806 1.1 skrll _bfd_error_handler
4807 1.1 skrll /* xgettext:c-format */
4808 1.1 skrll (_("Warning: size of symbol `%s' changed"
4809 1.1 skrll " from %Lu in %B to %Lu in %B"),
4810 1.9 christos name, h->size, old_bfd, isym->st_size, abfd);
4811 1.1 skrll
4812 1.1 skrll h->size = isym->st_size;
4813 1.1 skrll }
4814 1.1 skrll
4815 1.1 skrll /* If this is a common symbol, then we always want H->SIZE
4816 1.7 christos to be the size of the common symbol. The code just above
4817 1.7 christos won't fix the size if a common symbol becomes larger. We
4818 1.7 christos don't warn about a size change here, because that is
4819 1.1 skrll covered by --warn-common. Allow changes between different
4820 1.4 christos function types. */
4821 1.4 christos if (h->root.type == bfd_link_hash_common)
4822 1.4 christos h->size = h->root.u.c.size;
4823 1.4 christos
4824 1.4 christos if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4825 1.4 christos && ((definition && !new_weak)
4826 1.4 christos || (old_weak && h->root.type == bfd_link_hash_common)
4827 1.4 christos || h->type == STT_NOTYPE))
4828 1.4 christos {
4829 1.4 christos unsigned int type = ELF_ST_TYPE (isym->st_info);
4830 1.9 christos
4831 1.13.12.2 pgoyette /* Turn an IFUNC symbol from a DSO into a normal FUNC
4832 1.13.12.2 pgoyette symbol. */
4833 1.9 christos if (type == STT_GNU_IFUNC
4834 1.9 christos && (abfd->flags & DYNAMIC) != 0)
4835 1.13.12.2 pgoyette type = STT_FUNC;
4836 1.9 christos
4837 1.9 christos if (h->type != type)
4838 1.7 christos {
4839 1.9 christos if (h->type != STT_NOTYPE && ! type_change_ok)
4840 1.7 christos /* xgettext:c-format */
4841 1.9 christos _bfd_error_handler
4842 1.9 christos (_("Warning: type of symbol `%s' changed"
4843 1.1 skrll " from %d to %d in %B"),
4844 1.7 christos name, h->type, type, abfd);
4845 1.9 christos
4846 1.9 christos h->type = type;
4847 1.9 christos }
4848 1.7 christos }
4849 1.7 christos
4850 1.7 christos /* Merge st_other field. */
4851 1.7 christos elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4852 1.7 christos
4853 1.7 christos /* We don't want to make debug symbol dynamic. */
4854 1.7 christos if (definition
4855 1.9 christos && (sec->flags & SEC_DEBUGGING)
4856 1.9 christos && !bfd_link_relocatable (info))
4857 1.9 christos dynsym = FALSE;
4858 1.9 christos
4859 1.1 skrll /* Nor should we make plugin symbols dynamic. */
4860 1.1 skrll if ((abfd->flags & BFD_PLUGIN) != 0)
4861 1.1 skrll dynsym = FALSE;
4862 1.1 skrll
4863 1.1 skrll if (definition)
4864 1.1 skrll {
4865 1.1 skrll h->target_internal = isym->st_target_internal;
4866 1.1 skrll h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4867 1.1 skrll }
4868 1.1 skrll
4869 1.1 skrll if (definition && !dynamic)
4870 1.1 skrll {
4871 1.9 christos char *p = strchr (name, ELF_VER_CHR);
4872 1.9 christos if (p != NULL && p[1] != ELF_VER_CHR)
4873 1.1 skrll {
4874 1.1 skrll /* Queue non-default versions so that .symver x, x@FOO
4875 1.1 skrll aliases can be checked. */
4876 1.1 skrll if (!nondeflt_vers)
4877 1.1 skrll {
4878 1.1 skrll amt = ((isymend - isym + 1)
4879 1.1 skrll * sizeof (struct elf_link_hash_entry *));
4880 1.1 skrll nondeflt_vers
4881 1.1 skrll = (struct elf_link_hash_entry **) bfd_malloc (amt);
4882 1.1 skrll if (!nondeflt_vers)
4883 1.1 skrll goto error_free_vers;
4884 1.13.12.2 pgoyette }
4885 1.13.12.2 pgoyette nondeflt_vers[nondeflt_vers_cnt++] = h;
4886 1.1 skrll }
4887 1.13.12.2 pgoyette }
4888 1.1 skrll
4889 1.1 skrll if (dynsym && h->dynindx == -1)
4890 1.1 skrll {
4891 1.13 christos if (! bfd_elf_link_record_dynamic_symbol (info, h))
4892 1.1 skrll goto error_free_vers;
4893 1.1 skrll if (h->is_weakalias
4894 1.1 skrll && weakdef (h)->dynindx == -1)
4895 1.1 skrll {
4896 1.1 skrll if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4897 1.1 skrll goto error_free_vers;
4898 1.1 skrll }
4899 1.1 skrll }
4900 1.1 skrll else if (h->dynindx != -1)
4901 1.1 skrll /* If the symbol already has a dynamic index, but
4902 1.1 skrll visibility says it should not be visible, turn it into
4903 1.1 skrll a local symbol. */
4904 1.12 christos switch (ELF_ST_VISIBILITY (h->other))
4905 1.12 christos {
4906 1.1 skrll case STV_INTERNAL:
4907 1.12 christos case STV_HIDDEN:
4908 1.1 skrll (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4909 1.4 christos dynsym = FALSE;
4910 1.9 christos break;
4911 1.9 christos }
4912 1.9 christos
4913 1.9 christos /* Don't add DT_NEEDED for references from the dummy bfd nor
4914 1.4 christos for unmatched symbol. */
4915 1.13 christos if (!add_needed
4916 1.13 christos && matched
4917 1.1 skrll && definition
4918 1.1 skrll && ((dynsym
4919 1.1 skrll && h->ref_regular_nonweak
4920 1.1 skrll && (old_bfd == NULL
4921 1.9 christos || (old_bfd->flags & BFD_PLUGIN) == 0))
4922 1.9 christos || (h->ref_dynamic_nonweak
4923 1.9 christos && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4924 1.1 skrll && !on_needed_list (elf_dt_name (abfd),
4925 1.1 skrll htab->needed, NULL))))
4926 1.1 skrll {
4927 1.4 christos int ret;
4928 1.4 christos const char *soname = elf_dt_name (abfd);
4929 1.9 christos
4930 1.4 christos info->callbacks->minfo ("%!", soname, old_bfd,
4931 1.1 skrll h->root.root.string);
4932 1.13.12.2 pgoyette
4933 1.13.12.2 pgoyette /* A symbol from a library loaded via DT_NEEDED of some
4934 1.4 christos other library is referenced by a regular object.
4935 1.9 christos Add a DT_NEEDED entry for it. Issue an error if
4936 1.9 christos --no-add-needed is used and the reference was not
4937 1.1 skrll a weak one. */
4938 1.1 skrll if (old_bfd != NULL
4939 1.1 skrll && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4940 1.4 christos {
4941 1.9 christos _bfd_error_handler
4942 1.1 skrll /* xgettext:c-format */
4943 1.1 skrll (_("%B: undefined reference to symbol '%s'"),
4944 1.1 skrll old_bfd, name);
4945 1.1 skrll bfd_set_error (bfd_error_missing_dso);
4946 1.1 skrll goto error_free_vers;
4947 1.1 skrll }
4948 1.1 skrll
4949 1.1 skrll elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4950 1.1 skrll (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4951 1.1 skrll
4952 1.1 skrll add_needed = TRUE;
4953 1.13.12.2 pgoyette ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4954 1.13.12.2 pgoyette if (ret < 0)
4955 1.13.12.2 pgoyette goto error_free_vers;
4956 1.13.12.2 pgoyette
4957 1.13.12.2 pgoyette BFD_ASSERT (ret == 0);
4958 1.13.12.2 pgoyette }
4959 1.13.12.2 pgoyette }
4960 1.13.12.2 pgoyette }
4961 1.13.12.2 pgoyette
4962 1.13.12.2 pgoyette if (info->lto_plugin_active
4963 1.13.12.2 pgoyette && !bfd_link_relocatable (info)
4964 1.13.12.2 pgoyette && (abfd->flags & BFD_PLUGIN) == 0
4965 1.13.12.2 pgoyette && !just_syms
4966 1.13.12.2 pgoyette && extsymcount)
4967 1.13.12.2 pgoyette {
4968 1.13.12.2 pgoyette int r_sym_shift;
4969 1.13.12.2 pgoyette
4970 1.13.12.2 pgoyette if (bed->s->arch_size == 32)
4971 1.13.12.2 pgoyette r_sym_shift = 8;
4972 1.13.12.2 pgoyette else
4973 1.13.12.2 pgoyette r_sym_shift = 32;
4974 1.13.12.2 pgoyette
4975 1.13.12.2 pgoyette /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4976 1.13.12.2 pgoyette referenced in regular objects so that linker plugin will get
4977 1.13.12.2 pgoyette the correct symbol resolution. */
4978 1.13.12.2 pgoyette
4979 1.13.12.2 pgoyette sym_hash = elf_sym_hashes (abfd);
4980 1.13.12.2 pgoyette for (s = abfd->sections; s != NULL; s = s->next)
4981 1.13.12.2 pgoyette {
4982 1.13.12.2 pgoyette Elf_Internal_Rela *internal_relocs;
4983 1.13.12.2 pgoyette Elf_Internal_Rela *rel, *relend;
4984 1.13.12.2 pgoyette
4985 1.13.12.2 pgoyette /* Don't check relocations in excluded sections. */
4986 1.13.12.2 pgoyette if ((s->flags & SEC_RELOC) == 0
4987 1.13.12.2 pgoyette || s->reloc_count == 0
4988 1.13.12.2 pgoyette || (s->flags & SEC_EXCLUDE) != 0
4989 1.13.12.2 pgoyette || ((info->strip == strip_all
4990 1.13.12.2 pgoyette || info->strip == strip_debugger)
4991 1.13.12.2 pgoyette && (s->flags & SEC_DEBUGGING) != 0))
4992 1.13.12.2 pgoyette continue;
4993 1.13.12.2 pgoyette
4994 1.13.12.2 pgoyette internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4995 1.13.12.2 pgoyette NULL,
4996 1.13.12.2 pgoyette info->keep_memory);
4997 1.13.12.2 pgoyette if (internal_relocs == NULL)
4998 1.13.12.2 pgoyette goto error_free_vers;
4999 1.13.12.2 pgoyette
5000 1.13.12.2 pgoyette rel = internal_relocs;
5001 1.13.12.2 pgoyette relend = rel + s->reloc_count;
5002 1.13.12.2 pgoyette for ( ; rel < relend; rel++)
5003 1.13.12.2 pgoyette {
5004 1.13.12.2 pgoyette unsigned long r_symndx = rel->r_info >> r_sym_shift;
5005 1.13.12.2 pgoyette struct elf_link_hash_entry *h;
5006 1.13.12.2 pgoyette
5007 1.13.12.2 pgoyette /* Skip local symbols. */
5008 1.13.12.2 pgoyette if (r_symndx < extsymoff)
5009 1.13.12.2 pgoyette continue;
5010 1.13.12.2 pgoyette
5011 1.13.12.2 pgoyette h = sym_hash[r_symndx - extsymoff];
5012 1.1 skrll if (h != NULL)
5013 1.1 skrll h->root.non_ir_ref_regular = 1;
5014 1.1 skrll }
5015 1.1 skrll
5016 1.1 skrll if (elf_section_data (s)->relocs != internal_relocs)
5017 1.1 skrll free (internal_relocs);
5018 1.1 skrll }
5019 1.1 skrll }
5020 1.1 skrll
5021 1.1 skrll if (extversym != NULL)
5022 1.1 skrll {
5023 1.1 skrll free (extversym);
5024 1.1 skrll extversym = NULL;
5025 1.1 skrll }
5026 1.1 skrll
5027 1.1 skrll if (isymbuf != NULL)
5028 1.1 skrll {
5029 1.9 christos free (isymbuf);
5030 1.9 christos isymbuf = NULL;
5031 1.9 christos }
5032 1.1 skrll
5033 1.1 skrll if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5034 1.1 skrll {
5035 1.1 skrll unsigned int i;
5036 1.1 skrll
5037 1.1 skrll /* Restore the symbol table. */
5038 1.12 christos old_ent = (char *) old_tab + tabsize;
5039 1.12 christos memset (elf_sym_hashes (abfd), 0,
5040 1.12 christos extsymcount * sizeof (struct elf_link_hash_entry *));
5041 1.1 skrll htab->root.table.table = old_table;
5042 1.1 skrll htab->root.table.size = old_size;
5043 1.1 skrll htab->root.table.count = old_count;
5044 1.1 skrll memcpy (htab->root.table.table, old_tab, tabsize);
5045 1.7 christos htab->root.undefs = old_undefs;
5046 1.7 christos htab->root.undefs_tail = old_undefs_tail;
5047 1.13.12.2 pgoyette _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5048 1.1 skrll free (old_strtab);
5049 1.1 skrll old_strtab = NULL;
5050 1.1 skrll for (i = 0; i < htab->root.table.size; i++)
5051 1.1 skrll {
5052 1.1 skrll struct bfd_hash_entry *p;
5053 1.1 skrll struct elf_link_hash_entry *h;
5054 1.1 skrll bfd_size_type size;
5055 1.7 christos unsigned int alignment_power;
5056 1.7 christos unsigned int non_ir_ref_dynamic;
5057 1.7 christos
5058 1.7 christos for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5059 1.7 christos {
5060 1.7 christos h = (struct elf_link_hash_entry *) p;
5061 1.7 christos if (h->root.type == bfd_link_hash_warning)
5062 1.7 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
5063 1.7 christos
5064 1.7 christos /* Preserve the maximum alignment and size for common
5065 1.7 christos symbols even if this dynamic lib isn't on DT_NEEDED
5066 1.7 christos since it can still be loaded at run time by another
5067 1.7 christos dynamic lib. */
5068 1.7 christos if (h->root.type == bfd_link_hash_common)
5069 1.13.12.2 pgoyette {
5070 1.13.12.2 pgoyette size = h->root.u.c.size;
5071 1.13.12.2 pgoyette alignment_power = h->root.u.c.p->alignment_power;
5072 1.13.12.2 pgoyette }
5073 1.1 skrll else
5074 1.1 skrll {
5075 1.1 skrll size = 0;
5076 1.1 skrll alignment_power = 0;
5077 1.1 skrll }
5078 1.1 skrll /* Preserve non_ir_ref_dynamic so that this symbol
5079 1.1 skrll will be exported when the dynamic lib becomes needed
5080 1.7 christos in the second pass. */
5081 1.7 christos non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5082 1.7 christos memcpy (p, old_ent, htab->root.table.entsize);
5083 1.7 christos old_ent = (char *) old_ent + htab->root.table.entsize;
5084 1.7 christos h = (struct elf_link_hash_entry *) p;
5085 1.7 christos if (h->root.type == bfd_link_hash_warning)
5086 1.7 christos {
5087 1.7 christos memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5088 1.1 skrll old_ent = (char *) old_ent + htab->root.table.entsize;
5089 1.13.12.2 pgoyette h = (struct elf_link_hash_entry *) h->root.u.i.link;
5090 1.1 skrll }
5091 1.1 skrll if (h->root.type == bfd_link_hash_common)
5092 1.1 skrll {
5093 1.1 skrll if (size > h->root.u.c.size)
5094 1.1 skrll h->root.u.c.size = size;
5095 1.9 christos if (alignment_power > h->root.u.c.p->alignment_power)
5096 1.1 skrll h->root.u.c.p->alignment_power = alignment_power;
5097 1.1 skrll }
5098 1.1 skrll h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5099 1.1 skrll }
5100 1.1 skrll }
5101 1.1 skrll
5102 1.1 skrll /* Make a special call to the linker "notice" function to
5103 1.1 skrll tell it that symbols added for crefs may need to be removed. */
5104 1.1 skrll if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5105 1.1 skrll goto error_free_vers;
5106 1.1 skrll
5107 1.1 skrll free (old_tab);
5108 1.9 christos objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5109 1.1 skrll alloc_mark);
5110 1.1 skrll if (nondeflt_vers != NULL)
5111 1.1 skrll free (nondeflt_vers);
5112 1.1 skrll return TRUE;
5113 1.1 skrll }
5114 1.9 christos
5115 1.9 christos if (old_tab != NULL)
5116 1.9 christos {
5117 1.9 christos if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5118 1.1 skrll goto error_free_vers;
5119 1.13 christos free (old_tab);
5120 1.1 skrll old_tab = NULL;
5121 1.1 skrll }
5122 1.1 skrll
5123 1.1 skrll /* Now that all the symbols from this input file are created, if
5124 1.1 skrll not performing a relocatable link, handle .symver foo, foo@BAR
5125 1.1 skrll such that any relocs against foo become foo@BAR. */
5126 1.1 skrll if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5127 1.1 skrll {
5128 1.1 skrll size_t cnt, symidx;
5129 1.1 skrll
5130 1.1 skrll for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5131 1.1 skrll {
5132 1.1 skrll struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5133 1.4 christos char *shortname, *p;
5134 1.1 skrll
5135 1.1 skrll p = strchr (h->root.root.string, ELF_VER_CHR);
5136 1.1 skrll if (p == NULL
5137 1.1 skrll || (h->root.type != bfd_link_hash_defined
5138 1.1 skrll && h->root.type != bfd_link_hash_defweak))
5139 1.1 skrll continue;
5140 1.1 skrll
5141 1.1 skrll amt = p - h->root.root.string;
5142 1.1 skrll shortname = (char *) bfd_malloc (amt + 1);
5143 1.1 skrll if (!shortname)
5144 1.1 skrll goto error_free_vers;
5145 1.1 skrll memcpy (shortname, h->root.root.string, amt);
5146 1.1 skrll shortname[amt] = '\0';
5147 1.1 skrll
5148 1.1 skrll hi = (struct elf_link_hash_entry *)
5149 1.1 skrll bfd_link_hash_lookup (&htab->root, shortname,
5150 1.1 skrll FALSE, FALSE, FALSE);
5151 1.1 skrll if (hi != NULL
5152 1.1 skrll && hi->root.type == h->root.type
5153 1.1 skrll && hi->root.u.def.value == h->root.u.def.value
5154 1.1 skrll && hi->root.u.def.section == h->root.u.def.section)
5155 1.1 skrll {
5156 1.1 skrll (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5157 1.1 skrll hi->root.type = bfd_link_hash_indirect;
5158 1.1 skrll hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5159 1.1 skrll (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5160 1.1 skrll sym_hash = elf_sym_hashes (abfd);
5161 1.1 skrll if (sym_hash)
5162 1.1 skrll for (symidx = 0; symidx < extsymcount; ++symidx)
5163 1.1 skrll if (sym_hash[symidx] == hi)
5164 1.1 skrll {
5165 1.1 skrll sym_hash[symidx] = h;
5166 1.13.12.2 pgoyette break;
5167 1.1 skrll }
5168 1.1 skrll }
5169 1.1 skrll free (shortname);
5170 1.1 skrll }
5171 1.1 skrll free (nondeflt_vers);
5172 1.1 skrll nondeflt_vers = NULL;
5173 1.1 skrll }
5174 1.1 skrll
5175 1.1 skrll /* Now set the alias field correctly for all the weak defined
5176 1.1 skrll symbols we found. The only way to do this is to search all the
5177 1.1 skrll symbols. Since we only need the information for non functions in
5178 1.1 skrll dynamic objects, that's the only time we actually put anything on
5179 1.1 skrll the list WEAKS. We need this information so that if a regular
5180 1.1 skrll object refers to a symbol defined weakly in a dynamic object, the
5181 1.1 skrll real symbol in the dynamic object is also put in the dynamic
5182 1.1 skrll symbols; we also must arrange for both symbols to point to the
5183 1.1 skrll same memory location. We could handle the general case of symbol
5184 1.1 skrll aliasing, but a general symbol alias can only be generated in
5185 1.1 skrll assembler code, handling it correctly would be very time
5186 1.1 skrll consuming, and other ELF linkers don't handle general aliasing
5187 1.1 skrll either. */
5188 1.1 skrll if (weaks != NULL)
5189 1.1 skrll {
5190 1.13 christos struct elf_link_hash_entry **hpp;
5191 1.13 christos struct elf_link_hash_entry **hppend;
5192 1.4 christos struct elf_link_hash_entry **sorted_sym_hash;
5193 1.1 skrll struct elf_link_hash_entry *h;
5194 1.1 skrll size_t sym_count;
5195 1.1 skrll
5196 1.1 skrll /* Since we have to search the whole symbol list for each weak
5197 1.1 skrll defined symbol, search time for N weak defined symbols will be
5198 1.1 skrll O(N^2). Binary search will cut it down to O(NlogN). */
5199 1.1 skrll amt = extsymcount;
5200 1.1 skrll amt *= sizeof (struct elf_link_hash_entry *);
5201 1.1 skrll sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5202 1.1 skrll if (sorted_sym_hash == NULL)
5203 1.1 skrll goto error_return;
5204 1.1 skrll sym_hash = sorted_sym_hash;
5205 1.1 skrll hpp = elf_sym_hashes (abfd);
5206 1.1 skrll hppend = hpp + extsymcount;
5207 1.1 skrll sym_count = 0;
5208 1.1 skrll for (; hpp < hppend; hpp++)
5209 1.1 skrll {
5210 1.1 skrll h = *hpp;
5211 1.1 skrll if (h != NULL
5212 1.1 skrll && h->root.type == bfd_link_hash_defined
5213 1.1 skrll && !bed->is_function_type (h->type))
5214 1.1 skrll {
5215 1.1 skrll *sym_hash = h;
5216 1.1 skrll sym_hash++;
5217 1.1 skrll sym_count++;
5218 1.1 skrll }
5219 1.1 skrll }
5220 1.1 skrll
5221 1.9 christos qsort (sorted_sym_hash, sym_count,
5222 1.1 skrll sizeof (struct elf_link_hash_entry *),
5223 1.1 skrll elf_sort_symbol);
5224 1.13.12.2 pgoyette
5225 1.13.12.2 pgoyette while (weaks != NULL)
5226 1.13.12.2 pgoyette {
5227 1.13.12.2 pgoyette struct elf_link_hash_entry *hlook;
5228 1.13.12.2 pgoyette asection *slook;
5229 1.13.12.2 pgoyette bfd_vma vlook;
5230 1.1 skrll size_t i, j, idx = 0;
5231 1.1 skrll
5232 1.1 skrll hlook = weaks;
5233 1.1 skrll weaks = hlook->u.alias;
5234 1.1 skrll hlook->u.alias = NULL;
5235 1.1 skrll
5236 1.7 christos if (hlook->root.type != bfd_link_hash_defined
5237 1.1 skrll && hlook->root.type != bfd_link_hash_defweak)
5238 1.1 skrll continue;
5239 1.1 skrll
5240 1.7 christos slook = hlook->root.u.def.section;
5241 1.1 skrll vlook = hlook->root.u.def.value;
5242 1.1 skrll
5243 1.1 skrll i = 0;
5244 1.1 skrll j = sym_count;
5245 1.1 skrll while (i != j)
5246 1.1 skrll {
5247 1.1 skrll bfd_signed_vma vdiff;
5248 1.9 christos idx = (i + j) / 2;
5249 1.1 skrll h = sorted_sym_hash[idx];
5250 1.1 skrll vdiff = vlook - h->root.u.def.value;
5251 1.1 skrll if (vdiff < 0)
5252 1.1 skrll j = idx;
5253 1.1 skrll else if (vdiff > 0)
5254 1.7 christos i = idx + 1;
5255 1.1 skrll else
5256 1.1 skrll {
5257 1.1 skrll int sdiff = slook->id - h->root.u.def.section->id;
5258 1.1 skrll if (sdiff < 0)
5259 1.7 christos j = idx;
5260 1.1 skrll else if (sdiff > 0)
5261 1.1 skrll i = idx + 1;
5262 1.7 christos else
5263 1.7 christos break;
5264 1.7 christos }
5265 1.7 christos }
5266 1.7 christos
5267 1.7 christos /* We didn't find a value/section match. */
5268 1.7 christos if (i == j)
5269 1.7 christos continue;
5270 1.7 christos
5271 1.7 christos /* With multiple aliases, or when the weak symbol is already
5272 1.7 christos strongly defined, we have multiple matching symbols and
5273 1.7 christos the binary search above may land on any of them. Step
5274 1.7 christos one past the matching symbol(s). */
5275 1.7 christos while (++idx != j)
5276 1.7 christos {
5277 1.7 christos h = sorted_sym_hash[idx];
5278 1.1 skrll if (h->root.u.def.section != slook
5279 1.7 christos || h->root.u.def.value != vlook)
5280 1.1 skrll break;
5281 1.1 skrll }
5282 1.7 christos
5283 1.7 christos /* Now look back over the aliases. Since we sorted by size
5284 1.1 skrll as well as value and section, we'll choose the one with
5285 1.1 skrll the largest size. */
5286 1.1 skrll while (idx-- != i)
5287 1.13.12.2 pgoyette {
5288 1.13.12.2 pgoyette h = sorted_sym_hash[idx];
5289 1.13.12.2 pgoyette
5290 1.13.12.2 pgoyette /* Stop if value or section doesn't match. */
5291 1.13.12.2 pgoyette if (h->root.u.def.section != slook
5292 1.13.12.2 pgoyette || h->root.u.def.value != vlook)
5293 1.13.12.2 pgoyette break;
5294 1.13.12.2 pgoyette else if (h != hlook)
5295 1.13.12.2 pgoyette {
5296 1.1 skrll struct elf_link_hash_entry *t;
5297 1.1 skrll
5298 1.1 skrll hlook->u.alias = h;
5299 1.1 skrll hlook->is_weakalias = 1;
5300 1.1 skrll t = h;
5301 1.1 skrll if (t->u.alias != NULL)
5302 1.1 skrll while (t->u.alias != h)
5303 1.1 skrll t = t->u.alias;
5304 1.1 skrll t->u.alias = hlook;
5305 1.1 skrll
5306 1.1 skrll /* If the weak definition is in the list of dynamic
5307 1.1 skrll symbols, make sure the real definition is put
5308 1.1 skrll there as well. */
5309 1.1 skrll if (hlook->dynindx != -1 && h->dynindx == -1)
5310 1.1 skrll {
5311 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (info, h))
5312 1.1 skrll {
5313 1.1 skrll err_free_sym_hash:
5314 1.1 skrll free (sorted_sym_hash);
5315 1.1 skrll goto error_return;
5316 1.1 skrll }
5317 1.1 skrll }
5318 1.1 skrll
5319 1.1 skrll /* If the real definition is in the list of dynamic
5320 1.1 skrll symbols, make sure the weak definition is put
5321 1.1 skrll there as well. If we don't do this, then the
5322 1.1 skrll dynamic loader might not merge the entries for the
5323 1.1 skrll real definition and the weak definition. */
5324 1.1 skrll if (h->dynindx != -1 && hlook->dynindx == -1)
5325 1.1 skrll {
5326 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5327 1.1 skrll goto err_free_sym_hash;
5328 1.4 christos }
5329 1.4 christos break;
5330 1.4 christos }
5331 1.1 skrll }
5332 1.1 skrll }
5333 1.1 skrll
5334 1.1 skrll free (sorted_sym_hash);
5335 1.1 skrll }
5336 1.1 skrll
5337 1.1 skrll if (bed->check_directives
5338 1.1 skrll && !(*bed->check_directives) (abfd, info))
5339 1.1 skrll return FALSE;
5340 1.1 skrll
5341 1.1 skrll /* If this is a non-traditional link, try to optimize the handling
5342 1.1 skrll of the .stab/.stabstr sections. */
5343 1.1 skrll if (! dynamic
5344 1.1 skrll && ! info->traditional_format
5345 1.1 skrll && is_elf_hash_table (htab)
5346 1.1 skrll && (info->strip != strip_all && info->strip != strip_debugger))
5347 1.1 skrll {
5348 1.1 skrll asection *stabstr;
5349 1.1 skrll
5350 1.1 skrll stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5351 1.1 skrll if (stabstr != NULL)
5352 1.1 skrll {
5353 1.1 skrll bfd_size_type string_offset = 0;
5354 1.1 skrll asection *stab;
5355 1.1 skrll
5356 1.1 skrll for (stab = abfd->sections; stab; stab = stab->next)
5357 1.1 skrll if (CONST_STRNEQ (stab->name, ".stab")
5358 1.1 skrll && (!stab->name[5] ||
5359 1.1 skrll (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5360 1.1 skrll && (stab->flags & SEC_MERGE) == 0
5361 1.1 skrll && !bfd_is_abs_section (stab->output_section))
5362 1.7 christos {
5363 1.1 skrll struct bfd_elf_section_data *secdata;
5364 1.1 skrll
5365 1.1 skrll secdata = elf_section_data (stab);
5366 1.1 skrll if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5367 1.1 skrll stabstr, &secdata->sec_info,
5368 1.1 skrll &string_offset))
5369 1.1 skrll goto error_return;
5370 1.1 skrll if (secdata->sec_info)
5371 1.1 skrll stab->sec_info_type = SEC_INFO_TYPE_STABS;
5372 1.9 christos }
5373 1.1 skrll }
5374 1.1 skrll }
5375 1.1 skrll
5376 1.1 skrll if (is_elf_hash_table (htab) && add_needed)
5377 1.1 skrll {
5378 1.1 skrll /* Add this bfd to the loaded list. */
5379 1.1 skrll struct elf_link_loaded_list *n;
5380 1.1 skrll
5381 1.1 skrll n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5382 1.1 skrll if (n == NULL)
5383 1.1 skrll goto error_return;
5384 1.1 skrll n->abfd = abfd;
5385 1.12 christos n->next = htab->loaded;
5386 1.12 christos htab->loaded = n;
5387 1.1 skrll }
5388 1.1 skrll
5389 1.1 skrll return TRUE;
5390 1.1 skrll
5391 1.1 skrll error_free_vers:
5392 1.1 skrll if (old_tab != NULL)
5393 1.1 skrll free (old_tab);
5394 1.1 skrll if (old_strtab != NULL)
5395 1.1 skrll free (old_strtab);
5396 1.1 skrll if (nondeflt_vers != NULL)
5397 1.1 skrll free (nondeflt_vers);
5398 1.1 skrll if (extversym != NULL)
5399 1.1 skrll free (extversym);
5400 1.1 skrll error_free_sym:
5401 1.1 skrll if (isymbuf != NULL)
5402 1.1 skrll free (isymbuf);
5403 1.1 skrll error_return:
5404 1.1 skrll return FALSE;
5405 1.1 skrll }
5406 1.1 skrll
5407 1.1 skrll /* Return the linker hash table entry of a symbol that might be
5408 1.1 skrll satisfied by an archive symbol. Return -1 on error. */
5409 1.1 skrll
5410 1.7 christos struct elf_link_hash_entry *
5411 1.1 skrll _bfd_elf_archive_symbol_lookup (bfd *abfd,
5412 1.1 skrll struct bfd_link_info *info,
5413 1.1 skrll const char *name)
5414 1.1 skrll {
5415 1.1 skrll struct elf_link_hash_entry *h;
5416 1.1 skrll char *p, *copy;
5417 1.1 skrll size_t len, first;
5418 1.1 skrll
5419 1.1 skrll h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5420 1.1 skrll if (h != NULL)
5421 1.1 skrll return h;
5422 1.1 skrll
5423 1.1 skrll /* If this is a default version (the name contains @@), look up the
5424 1.1 skrll symbol again with only one `@' as well as without the version.
5425 1.4 christos The effect is that references to the symbol with and without the
5426 1.1 skrll version will be matched by the default symbol in the archive. */
5427 1.1 skrll
5428 1.1 skrll p = strchr (name, ELF_VER_CHR);
5429 1.1 skrll if (p == NULL || p[1] != ELF_VER_CHR)
5430 1.1 skrll return h;
5431 1.1 skrll
5432 1.1 skrll /* First check with only one `@'. */
5433 1.7 christos len = strlen (name);
5434 1.1 skrll copy = (char *) bfd_alloc (abfd, len);
5435 1.1 skrll if (copy == NULL)
5436 1.1 skrll return (struct elf_link_hash_entry *) 0 - 1;
5437 1.1 skrll
5438 1.1 skrll first = p - name + 1;
5439 1.1 skrll memcpy (copy, name, first);
5440 1.7 christos memcpy (copy + first, name + first + 1, len - first);
5441 1.1 skrll
5442 1.1 skrll h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5443 1.1 skrll if (h == NULL)
5444 1.1 skrll {
5445 1.1 skrll /* We also need to check references to the symbol without the
5446 1.1 skrll version. */
5447 1.1 skrll copy[first - 1] = '\0';
5448 1.9 christos h = elf_link_hash_lookup (elf_hash_table (info), copy,
5449 1.9 christos FALSE, FALSE, TRUE);
5450 1.1 skrll }
5451 1.1 skrll
5452 1.1 skrll bfd_release (abfd, copy);
5453 1.1 skrll return h;
5454 1.1 skrll }
5455 1.1 skrll
5456 1.1 skrll /* Add symbols from an ELF archive file to the linker hash table. We
5457 1.1 skrll don't use _bfd_generic_link_add_archive_symbols because we need to
5458 1.1 skrll handle versioned symbols.
5459 1.1 skrll
5460 1.1 skrll Fortunately, ELF archive handling is simpler than that done by
5461 1.1 skrll _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5462 1.1 skrll oddities. In ELF, if we find a symbol in the archive map, and the
5463 1.1 skrll symbol is currently undefined, we know that we must pull in that
5464 1.9 christos object file.
5465 1.1 skrll
5466 1.1 skrll Unfortunately, we do have to make multiple passes over the symbol
5467 1.1 skrll table until nothing further is resolved. */
5468 1.1 skrll
5469 1.1 skrll static bfd_boolean
5470 1.1 skrll elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5471 1.1 skrll {
5472 1.1 skrll symindex c;
5473 1.1 skrll unsigned char *included = NULL;
5474 1.1 skrll carsym *symdefs;
5475 1.1 skrll bfd_boolean loop;
5476 1.1 skrll bfd_size_type amt;
5477 1.1 skrll const struct elf_backend_data *bed;
5478 1.1 skrll struct elf_link_hash_entry * (*archive_symbol_lookup)
5479 1.1 skrll (bfd *, struct bfd_link_info *, const char *);
5480 1.1 skrll
5481 1.1 skrll if (! bfd_has_map (abfd))
5482 1.1 skrll {
5483 1.1 skrll /* An empty archive is a special case. */
5484 1.1 skrll if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5485 1.1 skrll return TRUE;
5486 1.1 skrll bfd_set_error (bfd_error_no_armap);
5487 1.1 skrll return FALSE;
5488 1.9 christos }
5489 1.9 christos
5490 1.9 christos /* Keep track of all symbols we know to be already defined, and all
5491 1.9 christos files we know to be already included. This is to speed up the
5492 1.1 skrll second and subsequent passes. */
5493 1.1 skrll c = bfd_ardata (abfd)->symdef_count;
5494 1.1 skrll if (c == 0)
5495 1.1 skrll return TRUE;
5496 1.1 skrll amt = c;
5497 1.1 skrll amt *= sizeof (*included);
5498 1.1 skrll included = (unsigned char *) bfd_zmalloc (amt);
5499 1.1 skrll if (included == NULL)
5500 1.1 skrll return FALSE;
5501 1.1 skrll
5502 1.1 skrll symdefs = bfd_ardata (abfd)->symdefs;
5503 1.1 skrll bed = get_elf_backend_data (abfd);
5504 1.1 skrll archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5505 1.1 skrll
5506 1.1 skrll do
5507 1.1 skrll {
5508 1.1 skrll file_ptr last;
5509 1.1 skrll symindex i;
5510 1.1 skrll carsym *symdef;
5511 1.1 skrll carsym *symdefend;
5512 1.1 skrll
5513 1.1 skrll loop = FALSE;
5514 1.1 skrll last = -1;
5515 1.1 skrll
5516 1.9 christos symdef = symdefs;
5517 1.1 skrll symdefend = symdef + c;
5518 1.1 skrll for (i = 0; symdef < symdefend; symdef++, i++)
5519 1.1 skrll {
5520 1.1 skrll struct elf_link_hash_entry *h;
5521 1.1 skrll bfd *element;
5522 1.1 skrll struct bfd_link_hash_entry *undefs_tail;
5523 1.1 skrll symindex mark;
5524 1.1 skrll
5525 1.1 skrll if (included[i])
5526 1.1 skrll continue;
5527 1.1 skrll if (symdef->file_offset == last)
5528 1.1 skrll {
5529 1.1 skrll included[i] = TRUE;
5530 1.1 skrll continue;
5531 1.1 skrll }
5532 1.1 skrll
5533 1.1 skrll h = archive_symbol_lookup (abfd, info, symdef->name);
5534 1.1 skrll if (h == (struct elf_link_hash_entry *) 0 - 1)
5535 1.1 skrll goto error_return;
5536 1.1 skrll
5537 1.1 skrll if (h == NULL)
5538 1.1 skrll continue;
5539 1.1 skrll
5540 1.1 skrll if (h->root.type == bfd_link_hash_common)
5541 1.1 skrll {
5542 1.1 skrll /* We currently have a common symbol. The archive map contains
5543 1.1 skrll a reference to this symbol, so we may want to include it. We
5544 1.1 skrll only want to include it however, if this archive element
5545 1.1 skrll contains a definition of the symbol, not just another common
5546 1.1 skrll declaration of it.
5547 1.1 skrll
5548 1.1 skrll Unfortunately some archivers (including GNU ar) will put
5549 1.1 skrll declarations of common symbols into their archive maps, as
5550 1.1 skrll well as real definitions, so we cannot just go by the archive
5551 1.9 christos map alone. Instead we must read in the element's symbol
5552 1.9 christos table and check that to see what kind of symbol definition
5553 1.1 skrll this is. */
5554 1.1 skrll if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5555 1.1 skrll continue;
5556 1.1 skrll }
5557 1.1 skrll else if (h->root.type != bfd_link_hash_undefined)
5558 1.1 skrll {
5559 1.1 skrll if (h->root.type != bfd_link_hash_undefweak)
5560 1.1 skrll /* Symbol must be defined. Don't check it again. */
5561 1.1 skrll included[i] = TRUE;
5562 1.1 skrll continue;
5563 1.1 skrll }
5564 1.1 skrll
5565 1.1 skrll /* We need to include this archive member. */
5566 1.4 christos element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5567 1.4 christos if (element == NULL)
5568 1.13 christos goto error_return;
5569 1.4 christos
5570 1.1 skrll if (! bfd_check_format (element, bfd_object))
5571 1.1 skrll goto error_return;
5572 1.1 skrll
5573 1.1 skrll undefs_tail = info->hash->undefs_tail;
5574 1.1 skrll
5575 1.1 skrll if (!(*info->callbacks
5576 1.1 skrll ->add_archive_element) (info, element, symdef->name, &element))
5577 1.1 skrll continue;
5578 1.1 skrll if (!bfd_link_add_symbols (element, info))
5579 1.1 skrll goto error_return;
5580 1.1 skrll
5581 1.1 skrll /* If there are any new undefined symbols, we need to make
5582 1.1 skrll another pass through the archive in order to see whether
5583 1.1 skrll they can be defined. FIXME: This isn't perfect, because
5584 1.1 skrll common symbols wind up on undefs_tail and because an
5585 1.1 skrll undefined symbol which is defined later on in this pass
5586 1.1 skrll does not require another pass. This isn't a bug, but it
5587 1.1 skrll does make the code less efficient than it could be. */
5588 1.1 skrll if (undefs_tail != info->hash->undefs_tail)
5589 1.1 skrll loop = TRUE;
5590 1.1 skrll
5591 1.1 skrll /* Look backward to mark all symbols from this object file
5592 1.1 skrll which we have already seen in this pass. */
5593 1.1 skrll mark = i;
5594 1.1 skrll do
5595 1.1 skrll {
5596 1.1 skrll included[mark] = TRUE;
5597 1.1 skrll if (mark == 0)
5598 1.1 skrll break;
5599 1.1 skrll --mark;
5600 1.1 skrll }
5601 1.1 skrll while (symdefs[mark].file_offset == symdef->file_offset);
5602 1.1 skrll
5603 1.1 skrll /* We mark subsequent symbols from this object file as we go
5604 1.1 skrll on through the loop. */
5605 1.1 skrll last = symdef->file_offset;
5606 1.1 skrll }
5607 1.1 skrll }
5608 1.1 skrll while (loop);
5609 1.1 skrll
5610 1.1 skrll free (included);
5611 1.1 skrll
5612 1.1 skrll return TRUE;
5613 1.1 skrll
5614 1.1 skrll error_return:
5615 1.1 skrll if (included != NULL)
5616 1.1 skrll free (included);
5617 1.1 skrll return FALSE;
5618 1.1 skrll }
5619 1.1 skrll
5620 1.1 skrll /* Given an ELF BFD, add symbols to the global hash table as
5621 1.1 skrll appropriate. */
5622 1.1 skrll
5623 1.1 skrll bfd_boolean
5624 1.1 skrll bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5625 1.1 skrll {
5626 1.1 skrll switch (bfd_get_format (abfd))
5627 1.1 skrll {
5628 1.1 skrll case bfd_object:
5629 1.1 skrll return elf_link_add_object_symbols (abfd, info);
5630 1.1 skrll case bfd_archive:
5631 1.1 skrll return elf_link_add_archive_symbols (abfd, info);
5632 1.1 skrll default:
5633 1.1 skrll bfd_set_error (bfd_error_wrong_format);
5634 1.1 skrll return FALSE;
5635 1.1 skrll }
5636 1.1 skrll }
5637 1.1 skrll
5638 1.1 skrll struct hash_codes_info
5640 1.1 skrll {
5641 1.4 christos unsigned long *hashcodes;
5642 1.1 skrll bfd_boolean error;
5643 1.1 skrll };
5644 1.1 skrll
5645 1.1 skrll /* This function will be called though elf_link_hash_traverse to store
5646 1.1 skrll all hash value of the exported symbols in an array. */
5647 1.1 skrll
5648 1.1 skrll static bfd_boolean
5649 1.1 skrll elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5650 1.1 skrll {
5651 1.9 christos struct hash_codes_info *inf = (struct hash_codes_info *) data;
5652 1.1 skrll const char *name;
5653 1.9 christos unsigned long ha;
5654 1.9 christos char *alc = NULL;
5655 1.1 skrll
5656 1.9 christos /* Ignore indirect symbols. These are added by the versioning code. */
5657 1.9 christos if (h->dynindx == -1)
5658 1.9 christos return TRUE;
5659 1.9 christos
5660 1.9 christos name = h->root.root.string;
5661 1.9 christos if (h->versioned >= versioned)
5662 1.9 christos {
5663 1.9 christos char *p = strchr (name, ELF_VER_CHR);
5664 1.9 christos if (p != NULL)
5665 1.1 skrll {
5666 1.1 skrll alc = (char *) bfd_malloc (p - name + 1);
5667 1.1 skrll if (alc == NULL)
5668 1.1 skrll {
5669 1.1 skrll inf->error = TRUE;
5670 1.1 skrll return FALSE;
5671 1.1 skrll }
5672 1.1 skrll memcpy (alc, name, p - name);
5673 1.1 skrll alc[p - name] = '\0';
5674 1.1 skrll name = alc;
5675 1.1 skrll }
5676 1.1 skrll }
5677 1.1 skrll
5678 1.1 skrll /* Compute the hash value. */
5679 1.1 skrll ha = bfd_elf_hash (name);
5680 1.1 skrll
5681 1.1 skrll /* Store the found hash value in the array given as the argument. */
5682 1.1 skrll *(inf->hashcodes)++ = ha;
5683 1.1 skrll
5684 1.1 skrll /* And store it in the struct so that we can put it in the hash table
5685 1.1 skrll later. */
5686 1.1 skrll h->u.elf_hash_value = ha;
5687 1.1 skrll
5688 1.1 skrll if (alc != NULL)
5689 1.1 skrll free (alc);
5690 1.1 skrll
5691 1.1 skrll return TRUE;
5692 1.1 skrll }
5693 1.1 skrll
5694 1.1 skrll struct collect_gnu_hash_codes
5695 1.1 skrll {
5696 1.1 skrll bfd *output_bfd;
5697 1.1 skrll const struct elf_backend_data *bed;
5698 1.1 skrll unsigned long int nsyms;
5699 1.1 skrll unsigned long int maskbits;
5700 1.1 skrll unsigned long int *hashcodes;
5701 1.1 skrll unsigned long int *hashval;
5702 1.1 skrll unsigned long int *indx;
5703 1.1 skrll unsigned long int *counts;
5704 1.1 skrll bfd_vma *bitmask;
5705 1.1 skrll bfd_byte *contents;
5706 1.1 skrll long int min_dynindx;
5707 1.1 skrll unsigned long int bucketcount;
5708 1.1 skrll unsigned long int symindx;
5709 1.1 skrll long int local_indx;
5710 1.1 skrll long int shift1, shift2;
5711 1.4 christos unsigned long int mask;
5712 1.1 skrll bfd_boolean error;
5713 1.1 skrll };
5714 1.1 skrll
5715 1.1 skrll /* This function will be called though elf_link_hash_traverse to store
5716 1.1 skrll all hash value of the exported symbols in an array. */
5717 1.1 skrll
5718 1.1 skrll static bfd_boolean
5719 1.1 skrll elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5720 1.1 skrll {
5721 1.1 skrll struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5722 1.1 skrll const char *name;
5723 1.1 skrll unsigned long ha;
5724 1.1 skrll char *alc = NULL;
5725 1.9 christos
5726 1.1 skrll /* Ignore indirect symbols. These are added by the versioning code. */
5727 1.9 christos if (h->dynindx == -1)
5728 1.9 christos return TRUE;
5729 1.1 skrll
5730 1.9 christos /* Ignore also local symbols and undefined symbols. */
5731 1.9 christos if (! (*s->bed->elf_hash_symbol) (h))
5732 1.9 christos return TRUE;
5733 1.9 christos
5734 1.9 christos name = h->root.root.string;
5735 1.9 christos if (h->versioned >= versioned)
5736 1.9 christos {
5737 1.9 christos char *p = strchr (name, ELF_VER_CHR);
5738 1.9 christos if (p != NULL)
5739 1.1 skrll {
5740 1.1 skrll alc = (char *) bfd_malloc (p - name + 1);
5741 1.1 skrll if (alc == NULL)
5742 1.1 skrll {
5743 1.1 skrll s->error = TRUE;
5744 1.1 skrll return FALSE;
5745 1.1 skrll }
5746 1.1 skrll memcpy (alc, name, p - name);
5747 1.1 skrll alc[p - name] = '\0';
5748 1.1 skrll name = alc;
5749 1.1 skrll }
5750 1.1 skrll }
5751 1.1 skrll
5752 1.1 skrll /* Compute the hash value. */
5753 1.1 skrll ha = bfd_elf_gnu_hash (name);
5754 1.1 skrll
5755 1.1 skrll /* Store the found hash value in the array for compute_bucket_count,
5756 1.1 skrll and also for .dynsym reordering purposes. */
5757 1.1 skrll s->hashcodes[s->nsyms] = ha;
5758 1.1 skrll s->hashval[h->dynindx] = ha;
5759 1.1 skrll ++s->nsyms;
5760 1.1 skrll if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5761 1.1 skrll s->min_dynindx = h->dynindx;
5762 1.1 skrll
5763 1.1 skrll if (alc != NULL)
5764 1.1 skrll free (alc);
5765 1.4 christos
5766 1.1 skrll return TRUE;
5767 1.1 skrll }
5768 1.1 skrll
5769 1.1 skrll /* This function will be called though elf_link_hash_traverse to do
5770 1.1 skrll final dynaminc symbol renumbering. */
5771 1.1 skrll
5772 1.1 skrll static bfd_boolean
5773 1.1 skrll elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5774 1.1 skrll {
5775 1.1 skrll struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5776 1.1 skrll unsigned long int bucket;
5777 1.1 skrll unsigned long int val;
5778 1.1 skrll
5779 1.1 skrll /* Ignore indirect symbols. */
5780 1.1 skrll if (h->dynindx == -1)
5781 1.1 skrll return TRUE;
5782 1.1 skrll
5783 1.1 skrll /* Ignore also local symbols and undefined symbols. */
5784 1.1 skrll if (! (*s->bed->elf_hash_symbol) (h))
5785 1.1 skrll {
5786 1.1 skrll if (h->dynindx >= s->min_dynindx)
5787 1.1 skrll h->dynindx = s->local_indx++;
5788 1.1 skrll return TRUE;
5789 1.1 skrll }
5790 1.1 skrll
5791 1.1 skrll bucket = s->hashval[h->dynindx] % s->bucketcount;
5792 1.1 skrll val = (s->hashval[h->dynindx] >> s->shift1)
5793 1.1 skrll & ((s->maskbits >> s->shift1) - 1);
5794 1.1 skrll s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5795 1.1 skrll s->bitmask[val]
5796 1.1 skrll |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5797 1.1 skrll val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5798 1.1 skrll if (s->counts[bucket] == 1)
5799 1.1 skrll /* Last element terminates the chain. */
5800 1.1 skrll val |= 1;
5801 1.1 skrll bfd_put_32 (s->output_bfd, val,
5802 1.1 skrll s->contents + (s->indx[bucket] - s->symindx) * 4);
5803 1.1 skrll --s->counts[bucket];
5804 1.1 skrll h->dynindx = s->indx[bucket]++;
5805 1.1 skrll return TRUE;
5806 1.1 skrll }
5807 1.1 skrll
5808 1.1 skrll /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5809 1.1 skrll
5810 1.1 skrll bfd_boolean
5811 1.1 skrll _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5812 1.1 skrll {
5813 1.1 skrll return !(h->forced_local
5814 1.1 skrll || h->root.type == bfd_link_hash_undefined
5815 1.1 skrll || h->root.type == bfd_link_hash_undefweak
5816 1.1 skrll || ((h->root.type == bfd_link_hash_defined
5817 1.1 skrll || h->root.type == bfd_link_hash_defweak)
5818 1.1 skrll && h->root.u.def.section->output_section == NULL));
5819 1.1 skrll }
5820 1.1 skrll
5821 1.1 skrll /* Array used to determine the number of hash table buckets to use
5822 1.1 skrll based on the number of symbols there are. If there are fewer than
5823 1.1 skrll 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5824 1.1 skrll fewer than 37 we use 17 buckets, and so forth. We never use more
5825 1.1 skrll than 32771 buckets. */
5826 1.1 skrll
5827 1.1 skrll static const size_t elf_buckets[] =
5828 1.1 skrll {
5829 1.1 skrll 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5830 1.1 skrll 16411, 32771, 0
5831 1.1 skrll };
5832 1.4 christos
5833 1.1 skrll /* Compute bucket count for hashing table. We do not use a static set
5834 1.1 skrll of possible tables sizes anymore. Instead we determine for all
5835 1.1 skrll possible reasonable sizes of the table the outcome (i.e., the
5836 1.1 skrll number of collisions etc) and choose the best solution. The
5837 1.1 skrll weighting functions are not too simple to allow the table to grow
5838 1.1 skrll without bounds. Instead one of the weighting factors is the size.
5839 1.1 skrll Therefore the result is always a good payoff between few collisions
5840 1.1 skrll (= short chain lengths) and table size. */
5841 1.1 skrll static size_t
5842 1.1 skrll compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5843 1.1 skrll unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5844 1.1 skrll unsigned long int nsyms,
5845 1.1 skrll int gnu_hash)
5846 1.1 skrll {
5847 1.1 skrll size_t best_size = 0;
5848 1.1 skrll unsigned long int i;
5849 1.1 skrll
5850 1.1 skrll /* We have a problem here. The following code to optimize the table
5851 1.1 skrll size requires an integer type with more the 32 bits. If
5852 1.1 skrll BFD_HOST_U_64_BIT is set we know about such a type. */
5853 1.1 skrll #ifdef BFD_HOST_U_64_BIT
5854 1.4 christos if (info->optimize)
5855 1.1 skrll {
5856 1.1 skrll size_t minsize;
5857 1.1 skrll size_t maxsize;
5858 1.1 skrll BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5859 1.1 skrll bfd *dynobj = elf_hash_table (info)->dynobj;
5860 1.1 skrll size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5861 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5862 1.1 skrll unsigned long int *counts;
5863 1.1 skrll bfd_size_type amt;
5864 1.1 skrll unsigned int no_improvement_count = 0;
5865 1.1 skrll
5866 1.1 skrll /* Possible optimization parameters: if we have NSYMS symbols we say
5867 1.1 skrll that the hashing table must at least have NSYMS/4 and at most
5868 1.1 skrll 2*NSYMS buckets. */
5869 1.1 skrll minsize = nsyms / 4;
5870 1.1 skrll if (minsize == 0)
5871 1.1 skrll minsize = 1;
5872 1.1 skrll best_size = maxsize = nsyms * 2;
5873 1.1 skrll if (gnu_hash)
5874 1.1 skrll {
5875 1.4 christos if (minsize < 2)
5876 1.1 skrll minsize = 2;
5877 1.1 skrll if ((best_size & 31) == 0)
5878 1.1 skrll ++best_size;
5879 1.1 skrll }
5880 1.1 skrll
5881 1.1 skrll /* Create array where we count the collisions in. We must use bfd_malloc
5882 1.1 skrll since the size could be large. */
5883 1.1 skrll amt = maxsize;
5884 1.1 skrll amt *= sizeof (unsigned long int);
5885 1.1 skrll counts = (unsigned long int *) bfd_malloc (amt);
5886 1.1 skrll if (counts == NULL)
5887 1.1 skrll return 0;
5888 1.1 skrll
5889 1.1 skrll /* Compute the "optimal" size for the hash table. The criteria is a
5890 1.1 skrll minimal chain length. The minor criteria is (of course) the size
5891 1.1 skrll of the table. */
5892 1.1 skrll for (i = minsize; i < maxsize; ++i)
5893 1.1 skrll {
5894 1.1 skrll /* Walk through the array of hashcodes and count the collisions. */
5895 1.1 skrll BFD_HOST_U_64_BIT max;
5896 1.1 skrll unsigned long int j;
5897 1.1 skrll unsigned long int fact;
5898 1.1 skrll
5899 1.1 skrll if (gnu_hash && (i & 31) == 0)
5900 1.1 skrll continue;
5901 1.1 skrll
5902 1.1 skrll memset (counts, '\0', i * sizeof (unsigned long int));
5903 1.1 skrll
5904 1.1 skrll /* Determine how often each hash bucket is used. */
5905 1.1 skrll for (j = 0; j < nsyms; ++j)
5906 1.1 skrll ++counts[hashcodes[j] % i];
5907 1.1 skrll
5908 1.1 skrll /* For the weight function we need some information about the
5909 1.1 skrll pagesize on the target. This is information need not be 100%
5910 1.1 skrll accurate. Since this information is not available (so far) we
5911 1.1 skrll define it here to a reasonable default value. If it is crucial
5912 1.1 skrll to have a better value some day simply define this value. */
5913 1.1 skrll # ifndef BFD_TARGET_PAGESIZE
5914 1.1 skrll # define BFD_TARGET_PAGESIZE (4096)
5915 1.1 skrll # endif
5916 1.1 skrll
5917 1.1 skrll /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5918 1.1 skrll and the chains. */
5919 1.1 skrll max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5920 1.1 skrll
5921 1.1 skrll # if 1
5922 1.1 skrll /* Variant 1: optimize for short chains. We add the squares
5923 1.1 skrll of all the chain lengths (which favors many small chain
5924 1.1 skrll over a few long chains). */
5925 1.1 skrll for (j = 0; j < i; ++j)
5926 1.1 skrll max += counts[j] * counts[j];
5927 1.1 skrll
5928 1.1 skrll /* This adds penalties for the overall size of the table. */
5929 1.1 skrll fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5930 1.1 skrll max *= fact * fact;
5931 1.1 skrll # else
5932 1.1 skrll /* Variant 2: Optimize a lot more for small table. Here we
5933 1.1 skrll also add squares of the size but we also add penalties for
5934 1.1 skrll empty slots (the +1 term). */
5935 1.1 skrll for (j = 0; j < i; ++j)
5936 1.1 skrll max += (1 + counts[j]) * (1 + counts[j]);
5937 1.1 skrll
5938 1.1 skrll /* The overall size of the table is considered, but not as
5939 1.9 christos strong as in variant 1, where it is squared. */
5940 1.1 skrll fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5941 1.4 christos max *= fact;
5942 1.4 christos # endif
5943 1.4 christos
5944 1.4 christos /* Compare with current best results. */
5945 1.1 skrll if (max < best_chlen)
5946 1.1 skrll {
5947 1.1 skrll best_chlen = max;
5948 1.1 skrll best_size = i;
5949 1.1 skrll no_improvement_count = 0;
5950 1.1 skrll }
5951 1.1 skrll /* PR 11843: Avoid futile long searches for the best bucket size
5952 1.1 skrll when there are a large number of symbols. */
5953 1.1 skrll else if (++no_improvement_count == 100)
5954 1.1 skrll break;
5955 1.1 skrll }
5956 1.1 skrll
5957 1.1 skrll free (counts);
5958 1.1 skrll }
5959 1.1 skrll else
5960 1.1 skrll #endif /* defined (BFD_HOST_U_64_BIT) */
5961 1.1 skrll {
5962 1.1 skrll /* This is the fallback solution if no 64bit type is available or if we
5963 1.1 skrll are not supposed to spend much time on optimizations. We select the
5964 1.1 skrll bucket count using a fixed set of numbers. */
5965 1.1 skrll for (i = 0; elf_buckets[i] != 0; i++)
5966 1.1 skrll {
5967 1.1 skrll best_size = elf_buckets[i];
5968 1.4 christos if (nsyms < elf_buckets[i + 1])
5969 1.4 christos break;
5970 1.4 christos }
5971 1.4 christos if (gnu_hash && best_size < 2)
5972 1.4 christos best_size = 2;
5973 1.4 christos }
5974 1.13.12.2 pgoyette
5975 1.4 christos return best_size;
5976 1.9 christos }
5977 1.4 christos
5978 1.13.12.2 pgoyette /* Size any SHT_GROUP section for ld -r. */
5979 1.13.12.2 pgoyette
5980 1.4 christos bfd_boolean
5981 1.4 christos _bfd_elf_size_group_sections (struct bfd_link_info *info)
5982 1.4 christos {
5983 1.4 christos bfd *ibfd;
5984 1.4 christos asection *s;
5985 1.9 christos
5986 1.9 christos for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5987 1.9 christos if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5988 1.9 christos && (s = ibfd->sections) != NULL
5989 1.9 christos && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5990 1.9 christos && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5991 1.9 christos return FALSE;
5992 1.9 christos return TRUE;
5993 1.9 christos }
5994 1.9 christos
5995 1.9 christos /* Set a default stack segment size. The value in INFO wins. If it
5996 1.9 christos is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5997 1.9 christos undefined it is initialized. */
5998 1.9 christos
5999 1.9 christos bfd_boolean
6000 1.9 christos bfd_elf_stack_segment_size (bfd *output_bfd,
6001 1.9 christos struct bfd_link_info *info,
6002 1.9 christos const char *legacy_symbol,
6003 1.9 christos bfd_vma default_size)
6004 1.9 christos {
6005 1.9 christos struct elf_link_hash_entry *h = NULL;
6006 1.9 christos
6007 1.9 christos /* Look for legacy symbol. */
6008 1.9 christos if (legacy_symbol)
6009 1.13.12.2 pgoyette h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6010 1.13.12.2 pgoyette FALSE, FALSE, FALSE);
6011 1.13.12.2 pgoyette if (h && (h->root.type == bfd_link_hash_defined
6012 1.9 christos || h->root.type == bfd_link_hash_defweak)
6013 1.13.12.2 pgoyette && h->def_regular
6014 1.13.12.2 pgoyette && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6015 1.13.12.2 pgoyette {
6016 1.9 christos /* The symbol has no type if specified on the command line. */
6017 1.9 christos h->type = STT_OBJECT;
6018 1.9 christos if (info->stacksize)
6019 1.9 christos /* xgettext:c-format */
6020 1.9 christos _bfd_error_handler (_("%B: stack size specified and %s set"),
6021 1.9 christos output_bfd, legacy_symbol);
6022 1.9 christos else if (h->root.u.def.section != bfd_abs_section_ptr)
6023 1.9 christos /* xgettext:c-format */
6024 1.9 christos _bfd_error_handler (_("%B: %s not absolute"),
6025 1.9 christos output_bfd, legacy_symbol);
6026 1.9 christos else
6027 1.9 christos info->stacksize = h->root.u.def.value;
6028 1.9 christos }
6029 1.9 christos
6030 1.9 christos if (!info->stacksize)
6031 1.9 christos /* If the user didn't set a size, or explicitly inhibit the
6032 1.9 christos size, set it now. */
6033 1.9 christos info->stacksize = default_size;
6034 1.9 christos
6035 1.9 christos /* Provide the legacy symbol, if it is referenced. */
6036 1.9 christos if (h && (h->root.type == bfd_link_hash_undefined
6037 1.9 christos || h->root.type == bfd_link_hash_undefweak))
6038 1.9 christos {
6039 1.9 christos struct bfd_link_hash_entry *bh = NULL;
6040 1.9 christos
6041 1.9 christos if (!(_bfd_generic_link_add_one_symbol
6042 1.9 christos (info, output_bfd, legacy_symbol,
6043 1.9 christos BSF_GLOBAL, bfd_abs_section_ptr,
6044 1.9 christos info->stacksize >= 0 ? info->stacksize : 0,
6045 1.9 christos NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6046 1.13.12.2 pgoyette return FALSE;
6047 1.13.12.2 pgoyette
6048 1.13.12.2 pgoyette h = (struct elf_link_hash_entry *) bh;
6049 1.13.12.2 pgoyette h->def_regular = 1;
6050 1.13.12.2 pgoyette h->type = STT_OBJECT;
6051 1.13.12.2 pgoyette }
6052 1.13.12.2 pgoyette
6053 1.13.12.2 pgoyette return TRUE;
6054 1.13.12.2 pgoyette }
6055 1.13.12.2 pgoyette
6056 1.13.12.2 pgoyette /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6057 1.13.12.2 pgoyette
6058 1.13.12.2 pgoyette struct elf_gc_sweep_symbol_info
6059 1.13.12.2 pgoyette {
6060 1.13.12.2 pgoyette struct bfd_link_info *info;
6061 1.13.12.2 pgoyette void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6062 1.13.12.2 pgoyette bfd_boolean);
6063 1.13.12.2 pgoyette };
6064 1.13.12.2 pgoyette
6065 1.13.12.2 pgoyette static bfd_boolean
6066 1.13.12.2 pgoyette elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6067 1.13.12.2 pgoyette {
6068 1.13.12.2 pgoyette if (!h->mark
6069 1.13.12.2 pgoyette && (((h->root.type == bfd_link_hash_defined
6070 1.13.12.2 pgoyette || h->root.type == bfd_link_hash_defweak)
6071 1.13.12.2 pgoyette && !((h->def_regular || ELF_COMMON_DEF_P (h))
6072 1.13.12.2 pgoyette && h->root.u.def.section->gc_mark))
6073 1.13.12.2 pgoyette || h->root.type == bfd_link_hash_undefined
6074 1.13.12.2 pgoyette || h->root.type == bfd_link_hash_undefweak))
6075 1.13.12.2 pgoyette {
6076 1.13.12.2 pgoyette struct elf_gc_sweep_symbol_info *inf;
6077 1.13.12.2 pgoyette
6078 1.1 skrll inf = (struct elf_gc_sweep_symbol_info *) data;
6079 1.1 skrll (*inf->hide_symbol) (inf->info, h, TRUE);
6080 1.1 skrll h->def_regular = 0;
6081 1.1 skrll h->ref_regular = 0;
6082 1.1 skrll h->ref_regular_nonweak = 0;
6083 1.1 skrll }
6084 1.1 skrll
6085 1.1 skrll return TRUE;
6086 1.1 skrll }
6087 1.1 skrll
6088 1.4 christos /* Set up the sizes and contents of the ELF dynamic sections. This is
6089 1.4 christos called by the ELF linker emulation before_allocation routine. We
6090 1.1 skrll must set the sizes of the sections before the linker sets the
6091 1.1 skrll addresses of the various sections. */
6092 1.7 christos
6093 1.1 skrll bfd_boolean
6094 1.1 skrll bfd_elf_size_dynamic_sections (bfd *output_bfd,
6095 1.1 skrll const char *soname,
6096 1.1 skrll const char *rpath,
6097 1.1 skrll const char *filter_shlib,
6098 1.1 skrll const char *audit,
6099 1.1 skrll const char *depaudit,
6100 1.1 skrll const char * const *auxiliary_filters,
6101 1.1 skrll struct bfd_link_info *info,
6102 1.1 skrll asection **sinterpptr)
6103 1.1 skrll {
6104 1.7 christos bfd *dynobj;
6105 1.1 skrll const struct elf_backend_data *bed;
6106 1.13.12.2 pgoyette
6107 1.13.12.2 pgoyette *sinterpptr = NULL;
6108 1.1 skrll
6109 1.1 skrll if (!is_elf_hash_table (info->hash))
6110 1.1 skrll return TRUE;
6111 1.13.12.2 pgoyette
6112 1.1 skrll dynobj = elf_hash_table (info)->dynobj;
6113 1.13.12.2 pgoyette
6114 1.13.12.2 pgoyette if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6115 1.13.12.2 pgoyette {
6116 1.13.12.2 pgoyette struct bfd_elf_version_tree *verdefs;
6117 1.1 skrll struct elf_info_failed asvinfo;
6118 1.13.12.2 pgoyette struct bfd_elf_version_tree *t;
6119 1.4 christos struct bfd_elf_version_expr *d;
6120 1.13.12.2 pgoyette asection *s;
6121 1.13.12.2 pgoyette size_t soname_indx;
6122 1.1 skrll
6123 1.1 skrll /* If we are supposed to export all symbols into the dynamic symbol
6124 1.1 skrll table (this is not the normal case), then do so. */
6125 1.1 skrll if (info->export_dynamic
6126 1.1 skrll || (bfd_link_executable (info) && info->dynamic))
6127 1.1 skrll {
6128 1.1 skrll struct elf_info_failed eif;
6129 1.13.12.2 pgoyette
6130 1.13.12.2 pgoyette eif.info = info;
6131 1.13.12.2 pgoyette eif.failed = FALSE;
6132 1.13.12.2 pgoyette elf_link_hash_traverse (elf_hash_table (info),
6133 1.13.12.2 pgoyette _bfd_elf_export_symbol,
6134 1.13.12.2 pgoyette &eif);
6135 1.13.12.2 pgoyette if (eif.failed)
6136 1.13.12.2 pgoyette return FALSE;
6137 1.13.12.2 pgoyette }
6138 1.13.12.2 pgoyette
6139 1.13.12.2 pgoyette if (soname != NULL)
6140 1.1 skrll {
6141 1.7 christos soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6142 1.1 skrll soname, TRUE);
6143 1.4 christos if (soname_indx == (size_t) -1
6144 1.1 skrll || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6145 1.1 skrll return FALSE;
6146 1.1 skrll }
6147 1.4 christos else
6148 1.1 skrll soname_indx = (size_t) -1;
6149 1.1 skrll
6150 1.4 christos /* Make all global versions with definition. */
6151 1.4 christos for (t = info->version_info; t != NULL; t = t->next)
6152 1.4 christos for (d = t->globals.list; d != NULL; d = d->next)
6153 1.1 skrll if (!d->symver && d->literal)
6154 1.1 skrll {
6155 1.1 skrll const char *verstr, *name;
6156 1.1 skrll size_t namelen, verlen, newlen;
6157 1.4 christos char *newname, *p, leading_char;
6158 1.1 skrll struct elf_link_hash_entry *newh;
6159 1.1 skrll
6160 1.4 christos leading_char = bfd_get_symbol_leading_char (output_bfd);
6161 1.4 christos name = d->pattern;
6162 1.1 skrll namelen = strlen (name) + (leading_char != '\0');
6163 1.1 skrll verstr = t->name;
6164 1.1 skrll verlen = strlen (verstr);
6165 1.1 skrll newlen = namelen + verlen + 3;
6166 1.1 skrll
6167 1.1 skrll newname = (char *) bfd_malloc (newlen);
6168 1.1 skrll if (newname == NULL)
6169 1.1 skrll return FALSE;
6170 1.1 skrll newname[0] = leading_char;
6171 1.1 skrll memcpy (newname + (leading_char != '\0'), name, namelen);
6172 1.1 skrll
6173 1.1 skrll /* Check the hidden versioned definition. */
6174 1.1 skrll p = newname + namelen;
6175 1.1 skrll *p++ = ELF_VER_CHR;
6176 1.1 skrll memcpy (p, verstr, verlen + 1);
6177 1.1 skrll newh = elf_link_hash_lookup (elf_hash_table (info),
6178 1.1 skrll newname, FALSE, FALSE,
6179 1.1 skrll FALSE);
6180 1.1 skrll if (newh == NULL
6181 1.1 skrll || (newh->root.type != bfd_link_hash_defined
6182 1.1 skrll && newh->root.type != bfd_link_hash_defweak))
6183 1.1 skrll {
6184 1.1 skrll /* Check the default versioned definition. */
6185 1.1 skrll *p++ = ELF_VER_CHR;
6186 1.1 skrll memcpy (p, verstr, verlen + 1);
6187 1.1 skrll newh = elf_link_hash_lookup (elf_hash_table (info),
6188 1.1 skrll newname, FALSE, FALSE,
6189 1.1 skrll FALSE);
6190 1.1 skrll }
6191 1.1 skrll free (newname);
6192 1.1 skrll
6193 1.1 skrll /* Mark this version if there is a definition and it is
6194 1.1 skrll not defined in a shared object. */
6195 1.1 skrll if (newh != NULL
6196 1.1 skrll && !newh->def_dynamic
6197 1.1 skrll && (newh->root.type == bfd_link_hash_defined
6198 1.1 skrll || newh->root.type == bfd_link_hash_defweak))
6199 1.1 skrll d->symver = 1;
6200 1.1 skrll }
6201 1.1 skrll
6202 1.1 skrll /* Attach all the symbols to their version information. */
6203 1.1 skrll asvinfo.info = info;
6204 1.1 skrll asvinfo.failed = FALSE;
6205 1.13.12.2 pgoyette
6206 1.7 christos elf_link_hash_traverse (elf_hash_table (info),
6207 1.1 skrll _bfd_elf_link_assign_sym_version,
6208 1.4 christos &asvinfo);
6209 1.1 skrll if (asvinfo.failed)
6210 1.13.12.2 pgoyette return FALSE;
6211 1.1 skrll
6212 1.1 skrll if (!info->allow_undefined_version)
6213 1.1 skrll {
6214 1.1 skrll /* Check if all global versions have a definition. */
6215 1.1 skrll bfd_boolean all_defined = TRUE;
6216 1.1 skrll for (t = info->version_info; t != NULL; t = t->next)
6217 1.1 skrll for (d = t->globals.list; d != NULL; d = d->next)
6218 1.1 skrll if (d->literal && !d->symver && !d->script)
6219 1.1 skrll {
6220 1.1 skrll _bfd_error_handler
6221 1.1 skrll (_("%s: undefined version: %s"),
6222 1.1 skrll d->pattern, t->name);
6223 1.1 skrll all_defined = FALSE;
6224 1.7 christos }
6225 1.1 skrll
6226 1.1 skrll if (!all_defined)
6227 1.1 skrll {
6228 1.1 skrll bfd_set_error (bfd_error_bad_value);
6229 1.7 christos return FALSE;
6230 1.1 skrll }
6231 1.1 skrll }
6232 1.1 skrll
6233 1.1 skrll /* Set up the version definition section. */
6234 1.1 skrll s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6235 1.1 skrll BFD_ASSERT (s != NULL);
6236 1.1 skrll
6237 1.1 skrll /* We may have created additional version definitions if we are
6238 1.1 skrll just linking a regular application. */
6239 1.1 skrll verdefs = info->version_info;
6240 1.1 skrll
6241 1.1 skrll /* Skip anonymous version tag. */
6242 1.1 skrll if (verdefs != NULL && verdefs->vernum == 0)
6243 1.1 skrll verdefs = verdefs->next;
6244 1.1 skrll
6245 1.1 skrll if (verdefs == NULL && !info->create_default_symver)
6246 1.1 skrll s->flags |= SEC_EXCLUDE;
6247 1.1 skrll else
6248 1.1 skrll {
6249 1.1 skrll unsigned int cdefs;
6250 1.1 skrll bfd_size_type size;
6251 1.1 skrll bfd_byte *p;
6252 1.1 skrll Elf_Internal_Verdef def;
6253 1.1 skrll Elf_Internal_Verdaux defaux;
6254 1.1 skrll struct bfd_link_hash_entry *bh;
6255 1.1 skrll struct elf_link_hash_entry *h;
6256 1.1 skrll const char *name;
6257 1.1 skrll
6258 1.1 skrll cdefs = 0;
6259 1.1 skrll size = 0;
6260 1.1 skrll
6261 1.1 skrll /* Make space for the base version. */
6262 1.1 skrll size += sizeof (Elf_External_Verdef);
6263 1.1 skrll size += sizeof (Elf_External_Verdaux);
6264 1.1 skrll ++cdefs;
6265 1.1 skrll
6266 1.1 skrll /* Make space for the default version. */
6267 1.4 christos if (info->create_default_symver)
6268 1.4 christos {
6269 1.4 christos size += sizeof (Elf_External_Verdef);
6270 1.4 christos ++cdefs;
6271 1.1 skrll }
6272 1.1 skrll
6273 1.1 skrll for (t = verdefs; t != NULL; t = t->next)
6274 1.1 skrll {
6275 1.1 skrll struct bfd_elf_version_deps *n;
6276 1.1 skrll
6277 1.1 skrll /* Don't emit base version twice. */
6278 1.1 skrll if (t->vernum == 0)
6279 1.1 skrll continue;
6280 1.4 christos
6281 1.1 skrll size += sizeof (Elf_External_Verdef);
6282 1.1 skrll size += sizeof (Elf_External_Verdaux);
6283 1.1 skrll ++cdefs;
6284 1.1 skrll
6285 1.1 skrll for (n = t->deps; n != NULL; n = n->next)
6286 1.1 skrll size += sizeof (Elf_External_Verdaux);
6287 1.1 skrll }
6288 1.1 skrll
6289 1.1 skrll s->size = size;
6290 1.1 skrll s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6291 1.1 skrll if (s->contents == NULL && s->size != 0)
6292 1.1 skrll return FALSE;
6293 1.1 skrll
6294 1.1 skrll /* Fill in the version definition section. */
6295 1.1 skrll
6296 1.1 skrll p = s->contents;
6297 1.1 skrll
6298 1.1 skrll def.vd_version = VER_DEF_CURRENT;
6299 1.1 skrll def.vd_flags = VER_FLG_BASE;
6300 1.1 skrll def.vd_ndx = 1;
6301 1.1 skrll def.vd_cnt = 1;
6302 1.1 skrll if (info->create_default_symver)
6303 1.1 skrll {
6304 1.13 christos def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6305 1.1 skrll def.vd_next = sizeof (Elf_External_Verdef);
6306 1.1 skrll }
6307 1.1 skrll else
6308 1.1 skrll {
6309 1.1 skrll def.vd_aux = sizeof (Elf_External_Verdef);
6310 1.1 skrll def.vd_next = (sizeof (Elf_External_Verdef)
6311 1.1 skrll + sizeof (Elf_External_Verdaux));
6312 1.1 skrll }
6313 1.1 skrll
6314 1.13 christos if (soname_indx != (size_t) -1)
6315 1.1 skrll {
6316 1.1 skrll _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6317 1.1 skrll soname_indx);
6318 1.1 skrll def.vd_hash = bfd_elf_hash (soname);
6319 1.1 skrll defaux.vda_name = soname_indx;
6320 1.13 christos name = soname;
6321 1.1 skrll }
6322 1.1 skrll else
6323 1.1 skrll {
6324 1.1 skrll size_t indx;
6325 1.1 skrll
6326 1.1 skrll name = lbasename (output_bfd->filename);
6327 1.1 skrll def.vd_hash = bfd_elf_hash (name);
6328 1.1 skrll indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6329 1.1 skrll name, FALSE);
6330 1.1 skrll if (indx == (size_t) -1)
6331 1.1 skrll return FALSE;
6332 1.1 skrll defaux.vda_name = indx;
6333 1.1 skrll }
6334 1.1 skrll defaux.vda_next = 0;
6335 1.1 skrll
6336 1.1 skrll _bfd_elf_swap_verdef_out (output_bfd, &def,
6337 1.1 skrll (Elf_External_Verdef *) p);
6338 1.1 skrll p += sizeof (Elf_External_Verdef);
6339 1.1 skrll if (info->create_default_symver)
6340 1.1 skrll {
6341 1.1 skrll /* Add a symbol representing this version. */
6342 1.1 skrll bh = NULL;
6343 1.1 skrll if (! (_bfd_generic_link_add_one_symbol
6344 1.1 skrll (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6345 1.1 skrll 0, NULL, FALSE,
6346 1.1 skrll get_elf_backend_data (dynobj)->collect, &bh)))
6347 1.1 skrll return FALSE;
6348 1.1 skrll h = (struct elf_link_hash_entry *) bh;
6349 1.1 skrll h->non_elf = 0;
6350 1.1 skrll h->def_regular = 1;
6351 1.1 skrll h->type = STT_OBJECT;
6352 1.1 skrll h->verinfo.vertree = NULL;
6353 1.1 skrll
6354 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (info, h))
6355 1.1 skrll return FALSE;
6356 1.1 skrll
6357 1.1 skrll /* Create a duplicate of the base version with the same
6358 1.1 skrll aux block, but different flags. */
6359 1.1 skrll def.vd_flags = 0;
6360 1.1 skrll def.vd_ndx = 2;
6361 1.1 skrll def.vd_aux = sizeof (Elf_External_Verdef);
6362 1.1 skrll if (verdefs)
6363 1.1 skrll def.vd_next = (sizeof (Elf_External_Verdef)
6364 1.1 skrll + sizeof (Elf_External_Verdaux));
6365 1.1 skrll else
6366 1.1 skrll def.vd_next = 0;
6367 1.1 skrll _bfd_elf_swap_verdef_out (output_bfd, &def,
6368 1.1 skrll (Elf_External_Verdef *) p);
6369 1.1 skrll p += sizeof (Elf_External_Verdef);
6370 1.4 christos }
6371 1.4 christos _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6372 1.4 christos (Elf_External_Verdaux *) p);
6373 1.4 christos p += sizeof (Elf_External_Verdaux);
6374 1.1 skrll
6375 1.1 skrll for (t = verdefs; t != NULL; t = t->next)
6376 1.1 skrll {
6377 1.1 skrll unsigned int cdeps;
6378 1.1 skrll struct bfd_elf_version_deps *n;
6379 1.1 skrll
6380 1.1 skrll /* Don't emit the base version twice. */
6381 1.1 skrll if (t->vernum == 0)
6382 1.1 skrll continue;
6383 1.1 skrll
6384 1.1 skrll cdeps = 0;
6385 1.1 skrll for (n = t->deps; n != NULL; n = n->next)
6386 1.1 skrll ++cdeps;
6387 1.1 skrll
6388 1.1 skrll /* Add a symbol representing this version. */
6389 1.1 skrll bh = NULL;
6390 1.1 skrll if (! (_bfd_generic_link_add_one_symbol
6391 1.1 skrll (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6392 1.1 skrll 0, NULL, FALSE,
6393 1.1 skrll get_elf_backend_data (dynobj)->collect, &bh)))
6394 1.1 skrll return FALSE;
6395 1.1 skrll h = (struct elf_link_hash_entry *) bh;
6396 1.1 skrll h->non_elf = 0;
6397 1.1 skrll h->def_regular = 1;
6398 1.1 skrll h->type = STT_OBJECT;
6399 1.1 skrll h->verinfo.vertree = t;
6400 1.1 skrll
6401 1.1 skrll if (! bfd_elf_link_record_dynamic_symbol (info, h))
6402 1.1 skrll return FALSE;
6403 1.1 skrll
6404 1.1 skrll def.vd_version = VER_DEF_CURRENT;
6405 1.4 christos def.vd_flags = 0;
6406 1.4 christos if (t->globals.list == NULL
6407 1.4 christos && t->locals.list == NULL
6408 1.4 christos && ! t->used)
6409 1.4 christos def.vd_flags |= VER_FLG_WEAK;
6410 1.4 christos def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6411 1.4 christos def.vd_cnt = cdeps + 1;
6412 1.1 skrll def.vd_hash = bfd_elf_hash (t->name);
6413 1.1 skrll def.vd_aux = sizeof (Elf_External_Verdef);
6414 1.1 skrll def.vd_next = 0;
6415 1.1 skrll
6416 1.1 skrll /* If a basever node is next, it *must* be the last node in
6417 1.1 skrll the chain, otherwise Verdef construction breaks. */
6418 1.1 skrll if (t->next != NULL && t->next->vernum == 0)
6419 1.1 skrll BFD_ASSERT (t->next->next == NULL);
6420 1.1 skrll
6421 1.1 skrll if (t->next != NULL && t->next->vernum != 0)
6422 1.1 skrll def.vd_next = (sizeof (Elf_External_Verdef)
6423 1.1 skrll + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6424 1.1 skrll
6425 1.1 skrll _bfd_elf_swap_verdef_out (output_bfd, &def,
6426 1.1 skrll (Elf_External_Verdef *) p);
6427 1.1 skrll p += sizeof (Elf_External_Verdef);
6428 1.1 skrll
6429 1.1 skrll defaux.vda_name = h->dynstr_index;
6430 1.1 skrll _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6431 1.1 skrll h->dynstr_index);
6432 1.1 skrll defaux.vda_next = 0;
6433 1.1 skrll if (t->deps != NULL)
6434 1.1 skrll defaux.vda_next = sizeof (Elf_External_Verdaux);
6435 1.1 skrll t->name_indx = defaux.vda_name;
6436 1.1 skrll
6437 1.1 skrll _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6438 1.1 skrll (Elf_External_Verdaux *) p);
6439 1.1 skrll p += sizeof (Elf_External_Verdaux);
6440 1.1 skrll
6441 1.1 skrll for (n = t->deps; n != NULL; n = n->next)
6442 1.1 skrll {
6443 1.1 skrll if (n->version_needed == NULL)
6444 1.1 skrll {
6445 1.1 skrll /* This can happen if there was an error in the
6446 1.1 skrll version script. */
6447 1.1 skrll defaux.vda_name = 0;
6448 1.1 skrll }
6449 1.1 skrll else
6450 1.13.12.2 pgoyette {
6451 1.13.12.2 pgoyette defaux.vda_name = n->version_needed->name_indx;
6452 1.13.12.2 pgoyette _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6453 1.13.12.2 pgoyette defaux.vda_name);
6454 1.13.12.2 pgoyette }
6455 1.13.12.2 pgoyette if (n->next == NULL)
6456 1.13.12.2 pgoyette defaux.vda_next = 0;
6457 1.13.12.2 pgoyette else
6458 1.13.12.2 pgoyette defaux.vda_next = sizeof (Elf_External_Verdaux);
6459 1.13.12.2 pgoyette
6460 1.13.12.2 pgoyette _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6461 1.13.12.2 pgoyette (Elf_External_Verdaux *) p);
6462 1.13.12.2 pgoyette p += sizeof (Elf_External_Verdaux);
6463 1.13.12.2 pgoyette }
6464 1.13.12.2 pgoyette }
6465 1.13.12.2 pgoyette
6466 1.13.12.2 pgoyette elf_tdata (output_bfd)->cverdefs = cdefs;
6467 1.13.12.2 pgoyette }
6468 1.13.12.2 pgoyette }
6469 1.13.12.2 pgoyette
6470 1.13.12.2 pgoyette bed = get_elf_backend_data (output_bfd);
6471 1.13.12.2 pgoyette
6472 1.13.12.2 pgoyette if (info->gc_sections && bed->can_gc_sections)
6473 1.13.12.2 pgoyette {
6474 1.13.12.2 pgoyette struct elf_gc_sweep_symbol_info sweep_info;
6475 1.13.12.2 pgoyette
6476 1.13.12.2 pgoyette /* Remove the symbols that were in the swept sections from the
6477 1.13.12.2 pgoyette dynamic symbol table. */
6478 1.13.12.2 pgoyette sweep_info.info = info;
6479 1.13.12.2 pgoyette sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6480 1.13.12.2 pgoyette elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6481 1.13.12.2 pgoyette &sweep_info);
6482 1.13.12.2 pgoyette }
6483 1.13.12.2 pgoyette
6484 1.13.12.2 pgoyette if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6485 1.13.12.2 pgoyette {
6486 1.13.12.2 pgoyette asection *s;
6487 1.13.12.2 pgoyette struct elf_find_verdep_info sinfo;
6488 1.13.12.2 pgoyette
6489 1.13.12.2 pgoyette /* Work out the size of the version reference section. */
6490 1.13.12.2 pgoyette
6491 1.13.12.2 pgoyette s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6492 1.13.12.2 pgoyette BFD_ASSERT (s != NULL);
6493 1.13.12.2 pgoyette
6494 1.13.12.2 pgoyette sinfo.info = info;
6495 1.13.12.2 pgoyette sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6496 1.13.12.2 pgoyette if (sinfo.vers == 0)
6497 1.13.12.2 pgoyette sinfo.vers = 1;
6498 1.13.12.2 pgoyette sinfo.failed = FALSE;
6499 1.13.12.2 pgoyette
6500 1.13.12.2 pgoyette elf_link_hash_traverse (elf_hash_table (info),
6501 1.13.12.2 pgoyette _bfd_elf_link_find_version_dependencies,
6502 1.13.12.2 pgoyette &sinfo);
6503 1.13.12.2 pgoyette if (sinfo.failed)
6504 1.13.12.2 pgoyette return FALSE;
6505 1.13.12.2 pgoyette
6506 1.13.12.2 pgoyette if (elf_tdata (output_bfd)->verref == NULL)
6507 1.13.12.2 pgoyette s->flags |= SEC_EXCLUDE;
6508 1.13.12.2 pgoyette else
6509 1.13.12.2 pgoyette {
6510 1.13.12.2 pgoyette Elf_Internal_Verneed *vn;
6511 1.13.12.2 pgoyette unsigned int size;
6512 1.13.12.2 pgoyette unsigned int crefs;
6513 1.13.12.2 pgoyette bfd_byte *p;
6514 1.13.12.2 pgoyette
6515 1.13.12.2 pgoyette /* Build the version dependency section. */
6516 1.13.12.2 pgoyette size = 0;
6517 1.13.12.2 pgoyette crefs = 0;
6518 1.13.12.2 pgoyette for (vn = elf_tdata (output_bfd)->verref;
6519 1.13.12.2 pgoyette vn != NULL;
6520 1.13.12.2 pgoyette vn = vn->vn_nextref)
6521 1.13.12.2 pgoyette {
6522 1.13.12.2 pgoyette Elf_Internal_Vernaux *a;
6523 1.13.12.2 pgoyette
6524 1.13.12.2 pgoyette size += sizeof (Elf_External_Verneed);
6525 1.13.12.2 pgoyette ++crefs;
6526 1.13.12.2 pgoyette for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6527 1.13.12.2 pgoyette size += sizeof (Elf_External_Vernaux);
6528 1.13.12.2 pgoyette }
6529 1.13.12.2 pgoyette
6530 1.13.12.2 pgoyette s->size = size;
6531 1.13.12.2 pgoyette s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6532 1.13.12.2 pgoyette if (s->contents == NULL)
6533 1.13.12.2 pgoyette return FALSE;
6534 1.13.12.2 pgoyette
6535 1.13.12.2 pgoyette p = s->contents;
6536 1.13.12.2 pgoyette for (vn = elf_tdata (output_bfd)->verref;
6537 1.13.12.2 pgoyette vn != NULL;
6538 1.13.12.2 pgoyette vn = vn->vn_nextref)
6539 1.13.12.2 pgoyette {
6540 1.13.12.2 pgoyette unsigned int caux;
6541 1.13.12.2 pgoyette Elf_Internal_Vernaux *a;
6542 1.13.12.2 pgoyette size_t indx;
6543 1.13.12.2 pgoyette
6544 1.13.12.2 pgoyette caux = 0;
6545 1.13.12.2 pgoyette for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6546 1.13.12.2 pgoyette ++caux;
6547 1.13.12.2 pgoyette
6548 1.13.12.2 pgoyette vn->vn_version = VER_NEED_CURRENT;
6549 1.13.12.2 pgoyette vn->vn_cnt = caux;
6550 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6551 1.13.12.2 pgoyette elf_dt_name (vn->vn_bfd) != NULL
6552 1.13.12.2 pgoyette ? elf_dt_name (vn->vn_bfd)
6553 1.13.12.2 pgoyette : lbasename (vn->vn_bfd->filename),
6554 1.13.12.2 pgoyette FALSE);
6555 1.13.12.2 pgoyette if (indx == (size_t) -1)
6556 1.13.12.2 pgoyette return FALSE;
6557 1.13.12.2 pgoyette vn->vn_file = indx;
6558 1.13.12.2 pgoyette vn->vn_aux = sizeof (Elf_External_Verneed);
6559 1.13.12.2 pgoyette if (vn->vn_nextref == NULL)
6560 1.13.12.2 pgoyette vn->vn_next = 0;
6561 1.13.12.2 pgoyette else
6562 1.13.12.2 pgoyette vn->vn_next = (sizeof (Elf_External_Verneed)
6563 1.13.12.2 pgoyette + caux * sizeof (Elf_External_Vernaux));
6564 1.13.12.2 pgoyette
6565 1.13.12.2 pgoyette _bfd_elf_swap_verneed_out (output_bfd, vn,
6566 1.13.12.2 pgoyette (Elf_External_Verneed *) p);
6567 1.13.12.2 pgoyette p += sizeof (Elf_External_Verneed);
6568 1.13.12.2 pgoyette
6569 1.13.12.2 pgoyette for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6570 1.13.12.2 pgoyette {
6571 1.13.12.2 pgoyette a->vna_hash = bfd_elf_hash (a->vna_nodename);
6572 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6573 1.13.12.2 pgoyette a->vna_nodename, FALSE);
6574 1.13.12.2 pgoyette if (indx == (size_t) -1)
6575 1.13.12.2 pgoyette return FALSE;
6576 1.13.12.2 pgoyette a->vna_name = indx;
6577 1.13.12.2 pgoyette if (a->vna_nextptr == NULL)
6578 1.13.12.2 pgoyette a->vna_next = 0;
6579 1.13.12.2 pgoyette else
6580 1.13.12.2 pgoyette a->vna_next = sizeof (Elf_External_Vernaux);
6581 1.13.12.2 pgoyette
6582 1.13.12.2 pgoyette _bfd_elf_swap_vernaux_out (output_bfd, a,
6583 1.13.12.2 pgoyette (Elf_External_Vernaux *) p);
6584 1.13.12.2 pgoyette p += sizeof (Elf_External_Vernaux);
6585 1.13.12.2 pgoyette }
6586 1.13.12.2 pgoyette }
6587 1.13.12.2 pgoyette
6588 1.13.12.2 pgoyette elf_tdata (output_bfd)->cverrefs = crefs;
6589 1.13.12.2 pgoyette }
6590 1.13.12.2 pgoyette }
6591 1.13.12.2 pgoyette
6592 1.13.12.2 pgoyette /* Any syms created from now on start with -1 in
6593 1.13.12.2 pgoyette got.refcount/offset and plt.refcount/offset. */
6594 1.13.12.2 pgoyette elf_hash_table (info)->init_got_refcount
6595 1.13.12.2 pgoyette = elf_hash_table (info)->init_got_offset;
6596 1.13.12.2 pgoyette elf_hash_table (info)->init_plt_refcount
6597 1.13.12.2 pgoyette = elf_hash_table (info)->init_plt_offset;
6598 1.13.12.2 pgoyette
6599 1.13.12.2 pgoyette if (bfd_link_relocatable (info)
6600 1.13.12.2 pgoyette && !_bfd_elf_size_group_sections (info))
6601 1.13.12.2 pgoyette return FALSE;
6602 1.13.12.2 pgoyette
6603 1.13.12.2 pgoyette /* The backend may have to create some sections regardless of whether
6604 1.13.12.2 pgoyette we're dynamic or not. */
6605 1.13.12.2 pgoyette if (bed->elf_backend_always_size_sections
6606 1.13.12.2 pgoyette && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6607 1.13.12.2 pgoyette return FALSE;
6608 1.13.12.2 pgoyette
6609 1.13.12.2 pgoyette /* Determine any GNU_STACK segment requirements, after the backend
6610 1.13.12.2 pgoyette has had a chance to set a default segment size. */
6611 1.13.12.2 pgoyette if (info->execstack)
6612 1.13.12.2 pgoyette elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6613 1.13.12.2 pgoyette else if (info->noexecstack)
6614 1.13.12.2 pgoyette elf_stack_flags (output_bfd) = PF_R | PF_W;
6615 1.13.12.2 pgoyette else
6616 1.13.12.2 pgoyette {
6617 1.13.12.2 pgoyette bfd *inputobj;
6618 1.13.12.2 pgoyette asection *notesec = NULL;
6619 1.13.12.2 pgoyette int exec = 0;
6620 1.13.12.2 pgoyette
6621 1.13.12.2 pgoyette for (inputobj = info->input_bfds;
6622 1.13.12.2 pgoyette inputobj;
6623 1.13.12.2 pgoyette inputobj = inputobj->link.next)
6624 1.13.12.2 pgoyette {
6625 1.13.12.2 pgoyette asection *s;
6626 1.13.12.2 pgoyette
6627 1.13.12.2 pgoyette if (inputobj->flags
6628 1.13.12.2 pgoyette & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6629 1.13.12.2 pgoyette continue;
6630 1.13.12.2 pgoyette s = inputobj->sections;
6631 1.13.12.2 pgoyette if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6632 1.13.12.2 pgoyette continue;
6633 1.13.12.2 pgoyette
6634 1.13.12.2 pgoyette s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6635 1.13.12.2 pgoyette if (s)
6636 1.13.12.2 pgoyette {
6637 1.13.12.2 pgoyette if (s->flags & SEC_CODE)
6638 1.13.12.2 pgoyette exec = PF_X;
6639 1.13.12.2 pgoyette notesec = s;
6640 1.13.12.2 pgoyette }
6641 1.13.12.2 pgoyette else if (bed->default_execstack)
6642 1.13.12.2 pgoyette exec = PF_X;
6643 1.13.12.2 pgoyette }
6644 1.13.12.2 pgoyette if (notesec || info->stacksize > 0)
6645 1.13.12.2 pgoyette elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6646 1.13.12.2 pgoyette if (notesec && exec && bfd_link_relocatable (info)
6647 1.13.12.2 pgoyette && notesec->output_section != bfd_abs_section_ptr)
6648 1.13.12.2 pgoyette notesec->output_section->flags |= SEC_CODE;
6649 1.13.12.2 pgoyette }
6650 1.13.12.2 pgoyette
6651 1.13.12.2 pgoyette if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6652 1.13.12.2 pgoyette {
6653 1.13.12.2 pgoyette struct elf_info_failed eif;
6654 1.13.12.2 pgoyette struct elf_link_hash_entry *h;
6655 1.13.12.2 pgoyette asection *dynstr;
6656 1.13.12.2 pgoyette asection *s;
6657 1.13.12.2 pgoyette
6658 1.13.12.2 pgoyette *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6659 1.13.12.2 pgoyette BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6660 1.13.12.2 pgoyette
6661 1.13.12.2 pgoyette if (info->symbolic)
6662 1.13.12.2 pgoyette {
6663 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6664 1.13.12.2 pgoyette return FALSE;
6665 1.13.12.2 pgoyette info->flags |= DF_SYMBOLIC;
6666 1.13.12.2 pgoyette }
6667 1.13.12.2 pgoyette
6668 1.13.12.2 pgoyette if (rpath != NULL)
6669 1.13.12.2 pgoyette {
6670 1.13.12.2 pgoyette size_t indx;
6671 1.13.12.2 pgoyette bfd_vma tag;
6672 1.13.12.2 pgoyette
6673 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6674 1.13.12.2 pgoyette TRUE);
6675 1.13.12.2 pgoyette if (indx == (size_t) -1)
6676 1.13.12.2 pgoyette return FALSE;
6677 1.13.12.2 pgoyette
6678 1.13.12.2 pgoyette tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6679 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6680 1.13.12.2 pgoyette return FALSE;
6681 1.13.12.2 pgoyette }
6682 1.13.12.2 pgoyette
6683 1.13.12.2 pgoyette if (filter_shlib != NULL)
6684 1.13.12.2 pgoyette {
6685 1.13.12.2 pgoyette size_t indx;
6686 1.13.12.2 pgoyette
6687 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6688 1.13.12.2 pgoyette filter_shlib, TRUE);
6689 1.13.12.2 pgoyette if (indx == (size_t) -1
6690 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6691 1.13.12.2 pgoyette return FALSE;
6692 1.13.12.2 pgoyette }
6693 1.13.12.2 pgoyette
6694 1.13.12.2 pgoyette if (auxiliary_filters != NULL)
6695 1.13.12.2 pgoyette {
6696 1.13.12.2 pgoyette const char * const *p;
6697 1.13.12.2 pgoyette
6698 1.13.12.2 pgoyette for (p = auxiliary_filters; *p != NULL; p++)
6699 1.13.12.2 pgoyette {
6700 1.13.12.2 pgoyette size_t indx;
6701 1.13.12.2 pgoyette
6702 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6703 1.13.12.2 pgoyette *p, TRUE);
6704 1.13.12.2 pgoyette if (indx == (size_t) -1
6705 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6706 1.13.12.2 pgoyette return FALSE;
6707 1.13.12.2 pgoyette }
6708 1.13.12.2 pgoyette }
6709 1.13.12.2 pgoyette
6710 1.13.12.2 pgoyette if (audit != NULL)
6711 1.13.12.2 pgoyette {
6712 1.13.12.2 pgoyette size_t indx;
6713 1.13.12.2 pgoyette
6714 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6715 1.13.12.2 pgoyette TRUE);
6716 1.13.12.2 pgoyette if (indx == (size_t) -1
6717 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6718 1.13.12.2 pgoyette return FALSE;
6719 1.13.12.2 pgoyette }
6720 1.13.12.2 pgoyette
6721 1.13.12.2 pgoyette if (depaudit != NULL)
6722 1.13.12.2 pgoyette {
6723 1.13.12.2 pgoyette size_t indx;
6724 1.13.12.2 pgoyette
6725 1.13.12.2 pgoyette indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6726 1.13.12.2 pgoyette TRUE);
6727 1.13.12.2 pgoyette if (indx == (size_t) -1
6728 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6729 1.13.12.2 pgoyette return FALSE;
6730 1.13.12.2 pgoyette }
6731 1.13.12.2 pgoyette
6732 1.13.12.2 pgoyette eif.info = info;
6733 1.13.12.2 pgoyette eif.failed = FALSE;
6734 1.13.12.2 pgoyette
6735 1.13.12.2 pgoyette /* Find all symbols which were defined in a dynamic object and make
6736 1.13.12.2 pgoyette the backend pick a reasonable value for them. */
6737 1.13.12.2 pgoyette elf_link_hash_traverse (elf_hash_table (info),
6738 1.13.12.2 pgoyette _bfd_elf_adjust_dynamic_symbol,
6739 1.13.12.2 pgoyette &eif);
6740 1.13.12.2 pgoyette if (eif.failed)
6741 1.13.12.2 pgoyette return FALSE;
6742 1.13.12.2 pgoyette
6743 1.13.12.2 pgoyette /* Add some entries to the .dynamic section. We fill in some of the
6744 1.13.12.2 pgoyette values later, in bfd_elf_final_link, but we must add the entries
6745 1.13.12.2 pgoyette now so that we know the final size of the .dynamic section. */
6746 1.13.12.2 pgoyette
6747 1.13.12.2 pgoyette /* If there are initialization and/or finalization functions to
6748 1.13.12.2 pgoyette call then add the corresponding DT_INIT/DT_FINI entries. */
6749 1.13.12.2 pgoyette h = (info->init_function
6750 1.13.12.2 pgoyette ? elf_link_hash_lookup (elf_hash_table (info),
6751 1.13.12.2 pgoyette info->init_function, FALSE,
6752 1.13.12.2 pgoyette FALSE, FALSE)
6753 1.13.12.2 pgoyette : NULL);
6754 1.13.12.2 pgoyette if (h != NULL
6755 1.13.12.2 pgoyette && (h->ref_regular
6756 1.13.12.2 pgoyette || h->def_regular))
6757 1.13.12.2 pgoyette {
6758 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6759 1.13.12.2 pgoyette return FALSE;
6760 1.13.12.2 pgoyette }
6761 1.13.12.2 pgoyette h = (info->fini_function
6762 1.13.12.2 pgoyette ? elf_link_hash_lookup (elf_hash_table (info),
6763 1.13.12.2 pgoyette info->fini_function, FALSE,
6764 1.13.12.2 pgoyette FALSE, FALSE)
6765 1.13.12.2 pgoyette : NULL);
6766 1.13.12.2 pgoyette if (h != NULL
6767 1.13.12.2 pgoyette && (h->ref_regular
6768 1.13.12.2 pgoyette || h->def_regular))
6769 1.13.12.2 pgoyette {
6770 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6771 1.13.12.2 pgoyette return FALSE;
6772 1.13.12.2 pgoyette }
6773 1.13.12.2 pgoyette
6774 1.13.12.2 pgoyette s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6775 1.13.12.2 pgoyette if (s != NULL && s->linker_has_input)
6776 1.13.12.2 pgoyette {
6777 1.13.12.2 pgoyette /* DT_PREINIT_ARRAY is not allowed in shared library. */
6778 1.13.12.2 pgoyette if (! bfd_link_executable (info))
6779 1.13.12.2 pgoyette {
6780 1.13.12.2 pgoyette bfd *sub;
6781 1.13.12.2 pgoyette asection *o;
6782 1.13.12.2 pgoyette
6783 1.13.12.2 pgoyette for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6784 1.13.12.2 pgoyette if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6785 1.13.12.2 pgoyette && (o = sub->sections) != NULL
6786 1.13.12.2 pgoyette && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6787 1.13.12.2 pgoyette for (o = sub->sections; o != NULL; o = o->next)
6788 1.13.12.2 pgoyette if (elf_section_data (o)->this_hdr.sh_type
6789 1.13.12.2 pgoyette == SHT_PREINIT_ARRAY)
6790 1.13.12.2 pgoyette {
6791 1.13.12.2 pgoyette _bfd_error_handler
6792 1.13.12.2 pgoyette (_("%B: .preinit_array section is not allowed in DSO"),
6793 1.13.12.2 pgoyette sub);
6794 1.13.12.2 pgoyette break;
6795 1.13.12.2 pgoyette }
6796 1.13.12.2 pgoyette
6797 1.13.12.2 pgoyette bfd_set_error (bfd_error_nonrepresentable_section);
6798 1.13.12.2 pgoyette return FALSE;
6799 1.13.12.2 pgoyette }
6800 1.13.12.2 pgoyette
6801 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6802 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6803 1.13.12.2 pgoyette return FALSE;
6804 1.13.12.2 pgoyette }
6805 1.13.12.2 pgoyette s = bfd_get_section_by_name (output_bfd, ".init_array");
6806 1.13.12.2 pgoyette if (s != NULL && s->linker_has_input)
6807 1.13.12.2 pgoyette {
6808 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6809 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6810 1.13.12.2 pgoyette return FALSE;
6811 1.13.12.2 pgoyette }
6812 1.13.12.2 pgoyette s = bfd_get_section_by_name (output_bfd, ".fini_array");
6813 1.13.12.2 pgoyette if (s != NULL && s->linker_has_input)
6814 1.13.12.2 pgoyette {
6815 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6816 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6817 1.13.12.2 pgoyette return FALSE;
6818 1.13.12.2 pgoyette }
6819 1.13.12.2 pgoyette
6820 1.13.12.2 pgoyette dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6821 1.13.12.2 pgoyette /* If .dynstr is excluded from the link, we don't want any of
6822 1.13.12.2 pgoyette these tags. Strictly, we should be checking each section
6823 1.13.12.2 pgoyette individually; This quick check covers for the case where
6824 1.13.12.2 pgoyette someone does a /DISCARD/ : { *(*) }. */
6825 1.13.12.2 pgoyette if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6826 1.13.12.2 pgoyette {
6827 1.13.12.2 pgoyette bfd_size_type strsize;
6828 1.13.12.2 pgoyette
6829 1.13.12.2 pgoyette strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6830 1.13.12.2 pgoyette if ((info->emit_hash
6831 1.13.12.2 pgoyette && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6832 1.13.12.2 pgoyette || (info->emit_gnu_hash
6833 1.13.12.2 pgoyette && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6834 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6835 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6836 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6837 1.13.12.2 pgoyette || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6838 1.13.12.2 pgoyette bed->s->sizeof_sym))
6839 1.13.12.2 pgoyette return FALSE;
6840 1.13.12.2 pgoyette }
6841 1.13.12.2 pgoyette }
6842 1.13.12.2 pgoyette
6843 1.13.12.2 pgoyette if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6844 1.13.12.2 pgoyette return FALSE;
6845 1.13.12.2 pgoyette
6846 1.13.12.2 pgoyette /* The backend must work out the sizes of all the other dynamic
6847 1.13.12.2 pgoyette sections. */
6848 1.1 skrll if (dynobj != NULL
6849 1.1 skrll && bed->elf_backend_size_dynamic_sections != NULL
6850 1.13.12.2 pgoyette && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6851 1.1 skrll return FALSE;
6852 1.1 skrll
6853 1.1 skrll if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6854 1.1 skrll {
6855 1.1 skrll if (elf_tdata (output_bfd)->cverdefs)
6856 1.1 skrll {
6857 1.1 skrll unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6858 1.1 skrll
6859 1.1 skrll if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6860 1.1 skrll || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6861 1.1 skrll return FALSE;
6862 1.1 skrll }
6863 1.1 skrll
6864 1.1 skrll if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6865 1.1 skrll {
6866 1.1 skrll if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6867 1.9 christos return FALSE;
6868 1.1 skrll }
6869 1.1 skrll else if (info->flags & DF_BIND_NOW)
6870 1.1 skrll {
6871 1.1 skrll if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6872 1.1 skrll return FALSE;
6873 1.1 skrll }
6874 1.1 skrll
6875 1.13.12.2 pgoyette if (info->flags_1)
6876 1.13.12.2 pgoyette {
6877 1.13.12.2 pgoyette if (bfd_link_executable (info))
6878 1.1 skrll info->flags_1 &= ~ (DF_1_INITFIRST
6879 1.13.12.2 pgoyette | DF_1_NODELETE
6880 1.13.12.2 pgoyette | DF_1_NOOPEN);
6881 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6882 1.13.12.2 pgoyette return FALSE;
6883 1.1 skrll }
6884 1.1 skrll
6885 1.1 skrll if (elf_tdata (output_bfd)->cverrefs)
6886 1.13.12.2 pgoyette {
6887 1.1 skrll unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6888 1.13.12.2 pgoyette
6889 1.13.12.2 pgoyette if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6890 1.7 christos || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6891 1.1 skrll return FALSE;
6892 1.1 skrll }
6893 1.1 skrll
6894 1.1 skrll if ((elf_tdata (output_bfd)->cverrefs == 0
6895 1.1 skrll && elf_tdata (output_bfd)->cverdefs == 0)
6896 1.1 skrll || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6897 1.1 skrll {
6898 1.1 skrll asection *s;
6899 1.1 skrll
6900 1.1 skrll s = bfd_get_linker_section (dynobj, ".gnu.version");
6901 1.1 skrll s->flags |= SEC_EXCLUDE;
6902 1.1 skrll }
6903 1.1 skrll }
6904 1.1 skrll return TRUE;
6905 1.1 skrll }
6906 1.1 skrll
6907 1.1 skrll /* Find the first non-excluded output section. We'll use its
6908 1.1 skrll section symbol for some emitted relocs. */
6909 1.1 skrll void
6910 1.1 skrll _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6911 1.1 skrll {
6912 1.1 skrll asection *s;
6913 1.1 skrll
6914 1.1 skrll for (s = output_bfd->sections; s != NULL; s = s->next)
6915 1.1 skrll if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6916 1.1 skrll && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6917 1.1 skrll {
6918 1.1 skrll elf_hash_table (info)->text_index_section = s;
6919 1.1 skrll break;
6920 1.1 skrll }
6921 1.1 skrll }
6922 1.1 skrll
6923 1.1 skrll /* Find two non-excluded output sections, one for code, one for data.
6924 1.1 skrll We'll use their section symbols for some emitted relocs. */
6925 1.1 skrll void
6926 1.1 skrll _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6927 1.1 skrll {
6928 1.1 skrll asection *s;
6929 1.1 skrll
6930 1.1 skrll /* Data first, since setting text_index_section changes
6931 1.1 skrll _bfd_elf_link_omit_section_dynsym. */
6932 1.1 skrll for (s = output_bfd->sections; s != NULL; s = s->next)
6933 1.1 skrll if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6934 1.1 skrll && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6935 1.1 skrll {
6936 1.1 skrll elf_hash_table (info)->data_index_section = s;
6937 1.1 skrll break;
6938 1.1 skrll }
6939 1.1 skrll
6940 1.1 skrll for (s = output_bfd->sections; s != NULL; s = s->next)
6941 1.1 skrll if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6942 1.1 skrll == (SEC_ALLOC | SEC_READONLY))
6943 1.1 skrll && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6944 1.1 skrll {
6945 1.1 skrll elf_hash_table (info)->text_index_section = s;
6946 1.1 skrll break;
6947 1.1 skrll }
6948 1.13.12.2 pgoyette
6949 1.13.12.2 pgoyette if (elf_hash_table (info)->text_index_section == NULL)
6950 1.1 skrll elf_hash_table (info)->text_index_section
6951 1.1 skrll = elf_hash_table (info)->data_index_section;
6952 1.1 skrll }
6953 1.1 skrll
6954 1.1 skrll bfd_boolean
6955 1.1 skrll bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6956 1.1 skrll {
6957 1.13.12.2 pgoyette const struct elf_backend_data *bed;
6958 1.13.12.2 pgoyette unsigned long section_sym_count;
6959 1.13.12.2 pgoyette bfd_size_type dynsymcount = 0;
6960 1.13.12.2 pgoyette
6961 1.13.12.2 pgoyette if (!is_elf_hash_table (info->hash))
6962 1.13.12.2 pgoyette return TRUE;
6963 1.13.12.2 pgoyette
6964 1.13.12.2 pgoyette bed = get_elf_backend_data (output_bfd);
6965 1.13.12.2 pgoyette (*bed->elf_backend_init_index_section) (output_bfd, info);
6966 1.13.12.2 pgoyette
6967 1.13.12.2 pgoyette /* Assign dynsym indices. In a shared library we generate a section
6968 1.13.12.2 pgoyette symbol for each output section, which come first. Next come all
6969 1.13.12.2 pgoyette of the back-end allocated local dynamic syms, followed by the rest
6970 1.13.12.2 pgoyette of the global symbols.
6971 1.13.12.2 pgoyette
6972 1.13.12.2 pgoyette This is usually not needed for static binaries, however backends
6973 1.1 skrll can request to always do it, e.g. the MIPS backend uses dynamic
6974 1.1 skrll symbol counts to lay out GOT, which will be produced in the
6975 1.1 skrll presence of GOT relocations even in static binaries (holding fixed
6976 1.1 skrll data in that case, to satisfy those relocations). */
6977 1.1 skrll
6978 1.1 skrll if (elf_hash_table (info)->dynamic_sections_created
6979 1.1 skrll || bed->always_renumber_dynsyms)
6980 1.1 skrll dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6981 1.1 skrll §ion_sym_count);
6982 1.7 christos
6983 1.1 skrll if (elf_hash_table (info)->dynamic_sections_created)
6984 1.13 christos {
6985 1.1 skrll bfd *dynobj;
6986 1.1 skrll asection *s;
6987 1.4 christos unsigned int dtagcount;
6988 1.1 skrll
6989 1.1 skrll dynobj = elf_hash_table (info)->dynobj;
6990 1.1 skrll
6991 1.1 skrll /* Work out the size of the symbol version section. */
6992 1.1 skrll s = bfd_get_linker_section (dynobj, ".gnu.version");
6993 1.1 skrll BFD_ASSERT (s != NULL);
6994 1.1 skrll if ((s->flags & SEC_EXCLUDE) == 0)
6995 1.1 skrll {
6996 1.1 skrll s->size = dynsymcount * sizeof (Elf_External_Versym);
6997 1.1 skrll s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6998 1.1 skrll if (s->contents == NULL)
6999 1.1 skrll return FALSE;
7000 1.1 skrll
7001 1.9 christos if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7002 1.1 skrll return FALSE;
7003 1.1 skrll }
7004 1.1 skrll
7005 1.13 christos /* Set the size of the .dynsym and .hash sections. We counted
7006 1.13 christos the number of dynamic symbols in elf_link_add_object_symbols.
7007 1.13 christos We will build the contents of .dynsym and .hash when we build
7008 1.1 skrll the final symbol table, because until then we do not know the
7009 1.13 christos correct value to give the symbols. We built the .dynstr
7010 1.13 christos section as we went along in elf_link_add_object_symbols. */
7011 1.13 christos s = elf_hash_table (info)->dynsym;
7012 1.13 christos BFD_ASSERT (s != NULL);
7013 1.1 skrll s->size = dynsymcount * bed->s->sizeof_sym;
7014 1.1 skrll
7015 1.1 skrll s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7016 1.1 skrll if (s->contents == NULL)
7017 1.1 skrll return FALSE;
7018 1.1 skrll
7019 1.1 skrll /* The first entry in .dynsym is a dummy symbol. Clear all the
7020 1.1 skrll section syms, in case we don't output them all. */
7021 1.1 skrll ++section_sym_count;
7022 1.1 skrll memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7023 1.1 skrll
7024 1.1 skrll elf_hash_table (info)->bucketcount = 0;
7025 1.1 skrll
7026 1.1 skrll /* Compute the size of the hashing table. As a side effect this
7027 1.1 skrll computes the hash values for all the names we export. */
7028 1.1 skrll if (info->emit_hash)
7029 1.1 skrll {
7030 1.1 skrll unsigned long int *hashcodes;
7031 1.4 christos struct hash_codes_info hashinf;
7032 1.1 skrll bfd_size_type amt;
7033 1.1 skrll unsigned long int nsyms;
7034 1.1 skrll size_t bucketcount;
7035 1.1 skrll size_t hash_entry_size;
7036 1.1 skrll
7037 1.1 skrll /* Compute the hash values for all exported symbols. At the same
7038 1.1 skrll time store the values in an array so that we could use them for
7039 1.1 skrll optimizations. */
7040 1.1 skrll amt = dynsymcount * sizeof (unsigned long int);
7041 1.1 skrll hashcodes = (unsigned long int *) bfd_malloc (amt);
7042 1.1 skrll if (hashcodes == NULL)
7043 1.1 skrll return FALSE;
7044 1.1 skrll hashinf.hashcodes = hashcodes;
7045 1.1 skrll hashinf.error = FALSE;
7046 1.1 skrll
7047 1.1 skrll /* Put all hash values in HASHCODES. */
7048 1.1 skrll elf_link_hash_traverse (elf_hash_table (info),
7049 1.1 skrll elf_collect_hash_codes, &hashinf);
7050 1.1 skrll if (hashinf.error)
7051 1.13.12.2 pgoyette {
7052 1.1 skrll free (hashcodes);
7053 1.1 skrll return FALSE;
7054 1.1 skrll }
7055 1.1 skrll
7056 1.7 christos nsyms = hashinf.hashcodes - hashcodes;
7057 1.1 skrll bucketcount
7058 1.1 skrll = compute_bucket_count (info, hashcodes, nsyms, 0);
7059 1.1 skrll free (hashcodes);
7060 1.4 christos
7061 1.1 skrll if (bucketcount == 0 && nsyms > 0)
7062 1.1 skrll return FALSE;
7063 1.1 skrll
7064 1.1 skrll elf_hash_table (info)->bucketcount = bucketcount;
7065 1.1 skrll
7066 1.1 skrll s = bfd_get_linker_section (dynobj, ".hash");
7067 1.1 skrll BFD_ASSERT (s != NULL);
7068 1.1 skrll hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7069 1.1 skrll s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7070 1.1 skrll s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7071 1.1 skrll if (s->contents == NULL)
7072 1.1 skrll return FALSE;
7073 1.1 skrll
7074 1.1 skrll bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7075 1.1 skrll bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7076 1.1 skrll s->contents + hash_entry_size);
7077 1.1 skrll }
7078 1.1 skrll
7079 1.1 skrll if (info->emit_gnu_hash)
7080 1.1 skrll {
7081 1.1 skrll size_t i, cnt;
7082 1.1 skrll unsigned char *contents;
7083 1.4 christos struct collect_gnu_hash_codes cinfo;
7084 1.1 skrll bfd_size_type amt;
7085 1.1 skrll size_t bucketcount;
7086 1.1 skrll
7087 1.1 skrll memset (&cinfo, 0, sizeof (cinfo));
7088 1.1 skrll
7089 1.1 skrll /* Compute the hash values for all exported symbols. At the same
7090 1.1 skrll time store the values in an array so that we could use them for
7091 1.1 skrll optimizations. */
7092 1.1 skrll amt = dynsymcount * 2 * sizeof (unsigned long int);
7093 1.1 skrll cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7094 1.1 skrll if (cinfo.hashcodes == NULL)
7095 1.1 skrll return FALSE;
7096 1.1 skrll
7097 1.1 skrll cinfo.hashval = cinfo.hashcodes + dynsymcount;
7098 1.1 skrll cinfo.min_dynindx = -1;
7099 1.1 skrll cinfo.output_bfd = output_bfd;
7100 1.1 skrll cinfo.bed = bed;
7101 1.1 skrll
7102 1.1 skrll /* Put all hash values in HASHCODES. */
7103 1.1 skrll elf_link_hash_traverse (elf_hash_table (info),
7104 1.1 skrll elf_collect_gnu_hash_codes, &cinfo);
7105 1.1 skrll if (cinfo.error)
7106 1.1 skrll {
7107 1.1 skrll free (cinfo.hashcodes);
7108 1.1 skrll return FALSE;
7109 1.1 skrll }
7110 1.7 christos
7111 1.1 skrll bucketcount
7112 1.1 skrll = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7113 1.1 skrll
7114 1.1 skrll if (bucketcount == 0)
7115 1.1 skrll {
7116 1.1 skrll free (cinfo.hashcodes);
7117 1.1 skrll return FALSE;
7118 1.1 skrll }
7119 1.4 christos
7120 1.1 skrll s = bfd_get_linker_section (dynobj, ".gnu.hash");
7121 1.1 skrll BFD_ASSERT (s != NULL);
7122 1.1 skrll
7123 1.1 skrll if (cinfo.nsyms == 0)
7124 1.1 skrll {
7125 1.1 skrll /* Empty .gnu.hash section is special. */
7126 1.1 skrll BFD_ASSERT (cinfo.min_dynindx == -1);
7127 1.1 skrll free (cinfo.hashcodes);
7128 1.1 skrll s->size = 5 * 4 + bed->s->arch_size / 8;
7129 1.1 skrll contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7130 1.1 skrll if (contents == NULL)
7131 1.1 skrll return FALSE;
7132 1.1 skrll s->contents = contents;
7133 1.1 skrll /* 1 empty bucket. */
7134 1.1 skrll bfd_put_32 (output_bfd, 1, contents);
7135 1.1 skrll /* SYMIDX above the special symbol 0. */
7136 1.1 skrll bfd_put_32 (output_bfd, 1, contents + 4);
7137 1.1 skrll /* Just one word for bitmask. */
7138 1.1 skrll bfd_put_32 (output_bfd, 1, contents + 8);
7139 1.4 christos /* Only hash fn bloom filter. */
7140 1.1 skrll bfd_put_32 (output_bfd, 0, contents + 12);
7141 1.1 skrll /* No hashes are valid - empty bitmask. */
7142 1.4 christos bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7143 1.4 christos /* No hashes in the only bucket. */
7144 1.4 christos bfd_put_32 (output_bfd, 0,
7145 1.4 christos contents + 16 + bed->s->arch_size / 8);
7146 1.1 skrll }
7147 1.1 skrll else
7148 1.1 skrll {
7149 1.1 skrll unsigned long int maskwords, maskbitslog2, x;
7150 1.1 skrll BFD_ASSERT (cinfo.min_dynindx != -1);
7151 1.1 skrll
7152 1.1 skrll x = cinfo.nsyms;
7153 1.1 skrll maskbitslog2 = 1;
7154 1.1 skrll while ((x >>= 1) != 0)
7155 1.1 skrll ++maskbitslog2;
7156 1.1 skrll if (maskbitslog2 < 3)
7157 1.1 skrll maskbitslog2 = 5;
7158 1.1 skrll else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7159 1.1 skrll maskbitslog2 = maskbitslog2 + 3;
7160 1.1 skrll else
7161 1.1 skrll maskbitslog2 = maskbitslog2 + 2;
7162 1.1 skrll if (bed->s->arch_size == 64)
7163 1.1 skrll {
7164 1.1 skrll if (maskbitslog2 == 5)
7165 1.1 skrll maskbitslog2 = 6;
7166 1.4 christos cinfo.shift1 = 6;
7167 1.1 skrll }
7168 1.1 skrll else
7169 1.1 skrll cinfo.shift1 = 5;
7170 1.1 skrll cinfo.mask = (1 << cinfo.shift1) - 1;
7171 1.1 skrll cinfo.shift2 = maskbitslog2;
7172 1.1 skrll cinfo.maskbits = 1 << maskbitslog2;
7173 1.4 christos maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7174 1.1 skrll amt = bucketcount * sizeof (unsigned long int) * 2;
7175 1.1 skrll amt += maskwords * sizeof (bfd_vma);
7176 1.1 skrll cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7177 1.1 skrll if (cinfo.bitmask == NULL)
7178 1.1 skrll {
7179 1.1 skrll free (cinfo.hashcodes);
7180 1.1 skrll return FALSE;
7181 1.1 skrll }
7182 1.1 skrll
7183 1.1 skrll cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7184 1.1 skrll cinfo.indx = cinfo.counts + bucketcount;
7185 1.1 skrll cinfo.symindx = dynsymcount - cinfo.nsyms;
7186 1.1 skrll memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7187 1.1 skrll
7188 1.1 skrll /* Determine how often each hash bucket is used. */
7189 1.1 skrll memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7190 1.1 skrll for (i = 0; i < cinfo.nsyms; ++i)
7191 1.1 skrll ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7192 1.1 skrll
7193 1.1 skrll for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7194 1.1 skrll if (cinfo.counts[i] != 0)
7195 1.4 christos {
7196 1.1 skrll cinfo.indx[i] = cnt;
7197 1.1 skrll cnt += cinfo.counts[i];
7198 1.1 skrll }
7199 1.1 skrll BFD_ASSERT (cnt == dynsymcount);
7200 1.1 skrll cinfo.bucketcount = bucketcount;
7201 1.1 skrll cinfo.local_indx = cinfo.min_dynindx;
7202 1.1 skrll
7203 1.1 skrll s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7204 1.1 skrll s->size += cinfo.maskbits / 8;
7205 1.1 skrll contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7206 1.1 skrll if (contents == NULL)
7207 1.1 skrll {
7208 1.1 skrll free (cinfo.bitmask);
7209 1.1 skrll free (cinfo.hashcodes);
7210 1.1 skrll return FALSE;
7211 1.1 skrll }
7212 1.1 skrll
7213 1.1 skrll s->contents = contents;
7214 1.1 skrll bfd_put_32 (output_bfd, bucketcount, contents);
7215 1.1 skrll bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7216 1.1 skrll bfd_put_32 (output_bfd, maskwords, contents + 8);
7217 1.1 skrll bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7218 1.1 skrll contents += 16 + cinfo.maskbits / 8;
7219 1.1 skrll
7220 1.1 skrll for (i = 0; i < bucketcount; ++i)
7221 1.1 skrll {
7222 1.1 skrll if (cinfo.counts[i] == 0)
7223 1.1 skrll bfd_put_32 (output_bfd, 0, contents);
7224 1.1 skrll else
7225 1.1 skrll bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7226 1.1 skrll contents += 4;
7227 1.1 skrll }
7228 1.1 skrll
7229 1.1 skrll cinfo.contents = contents;
7230 1.1 skrll
7231 1.1 skrll /* Renumber dynamic symbols, populate .gnu.hash section. */
7232 1.1 skrll elf_link_hash_traverse (elf_hash_table (info),
7233 1.1 skrll elf_renumber_gnu_hash_syms, &cinfo);
7234 1.1 skrll
7235 1.1 skrll contents = s->contents + 16;
7236 1.1 skrll for (i = 0; i < maskwords; ++i)
7237 1.1 skrll {
7238 1.7 christos bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7239 1.1 skrll contents);
7240 1.1 skrll contents += bed->s->arch_size / 8;
7241 1.1 skrll }
7242 1.1 skrll
7243 1.1 skrll free (cinfo.bitmask);
7244 1.1 skrll free (cinfo.hashcodes);
7245 1.1 skrll }
7246 1.1 skrll }
7247 1.1 skrll
7248 1.1 skrll s = bfd_get_linker_section (dynobj, ".dynstr");
7249 1.1 skrll BFD_ASSERT (s != NULL);
7250 1.1 skrll
7251 1.1 skrll elf_finalize_dynstr (output_bfd, info);
7252 1.1 skrll
7253 1.1 skrll s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7254 1.1 skrll
7255 1.1 skrll for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7256 1.1 skrll if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7257 1.1 skrll return FALSE;
7258 1.1 skrll }
7259 1.7 christos
7260 1.7 christos return TRUE;
7261 1.1 skrll }
7262 1.1 skrll
7263 1.1 skrll /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7265 1.1 skrll
7266 1.9 christos static void
7267 1.1 skrll merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7268 1.1 skrll asection *sec)
7269 1.1 skrll {
7270 1.1 skrll BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7271 1.1 skrll sec->sec_info_type = SEC_INFO_TYPE_NONE;
7272 1.1 skrll }
7273 1.1 skrll
7274 1.9 christos /* Finish SHF_MERGE section merging. */
7275 1.9 christos
7276 1.9 christos bfd_boolean
7277 1.9 christos _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7278 1.9 christos {
7279 1.1 skrll bfd *ibfd;
7280 1.1 skrll asection *sec;
7281 1.1 skrll
7282 1.1 skrll if (!is_elf_hash_table (info->hash))
7283 1.1 skrll return FALSE;
7284 1.1 skrll
7285 1.1 skrll for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7286 1.9 christos if ((ibfd->flags & DYNAMIC) == 0
7287 1.1 skrll && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7288 1.1 skrll && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7289 1.1 skrll == get_elf_backend_data (obfd)->s->elfclass))
7290 1.1 skrll for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7291 1.7 christos if ((sec->flags & SEC_MERGE) != 0
7292 1.1 skrll && !bfd_is_abs_section (sec->output_section))
7293 1.1 skrll {
7294 1.1 skrll struct bfd_elf_section_data *secdata;
7295 1.9 christos
7296 1.1 skrll secdata = elf_section_data (sec);
7297 1.1 skrll if (! _bfd_add_merge_section (obfd,
7298 1.1 skrll &elf_hash_table (info)->merge_info,
7299 1.1 skrll sec, &secdata->sec_info))
7300 1.1 skrll return FALSE;
7301 1.1 skrll else if (secdata->sec_info)
7302 1.1 skrll sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7303 1.1 skrll }
7304 1.1 skrll
7305 1.1 skrll if (elf_hash_table (info)->merge_info != NULL)
7306 1.1 skrll _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7307 1.1 skrll merge_sections_remove_hook);
7308 1.1 skrll return TRUE;
7309 1.1 skrll }
7310 1.1 skrll
7311 1.4 christos /* Create an entry in an ELF linker hash table. */
7312 1.9 christos
7313 1.1 skrll struct bfd_hash_entry *
7314 1.1 skrll _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7315 1.1 skrll struct bfd_hash_table *table,
7316 1.1 skrll const char *string)
7317 1.1 skrll {
7318 1.1 skrll /* Allocate the structure if it has not already been allocated by a
7319 1.1 skrll subclass. */
7320 1.1 skrll if (entry == NULL)
7321 1.1 skrll {
7322 1.1 skrll entry = (struct bfd_hash_entry *)
7323 1.1 skrll bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7324 1.1 skrll if (entry == NULL)
7325 1.1 skrll return entry;
7326 1.1 skrll }
7327 1.1 skrll
7328 1.1 skrll /* Call the allocation method of the superclass. */
7329 1.1 skrll entry = _bfd_link_hash_newfunc (entry, table, string);
7330 1.1 skrll if (entry != NULL)
7331 1.1 skrll {
7332 1.1 skrll struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7333 1.1 skrll struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7334 1.1 skrll
7335 1.1 skrll /* Set local fields. */
7336 1.1 skrll ret->indx = -1;
7337 1.1 skrll ret->dynindx = -1;
7338 1.1 skrll ret->got = htab->init_got_refcount;
7339 1.1 skrll ret->plt = htab->init_plt_refcount;
7340 1.1 skrll memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7341 1.1 skrll - offsetof (struct elf_link_hash_entry, size)));
7342 1.1 skrll /* Assume that we have been called by a non-ELF symbol reader.
7343 1.1 skrll This flag is then reset by the code which reads an ELF input
7344 1.1 skrll file. This ensures that a symbol created by a non-ELF symbol
7345 1.1 skrll reader will have the flag set correctly. */
7346 1.1 skrll ret->non_elf = 1;
7347 1.1 skrll }
7348 1.1 skrll
7349 1.1 skrll return entry;
7350 1.1 skrll }
7351 1.1 skrll
7352 1.13.12.2 pgoyette /* Copy data from an indirect symbol to its direct symbol, hiding the
7353 1.1 skrll old indirect symbol. Also used for copying flags to a weakdef. */
7354 1.9 christos
7355 1.13.12.2 pgoyette void
7356 1.13.12.2 pgoyette _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7357 1.13.12.2 pgoyette struct elf_link_hash_entry *dir,
7358 1.13.12.2 pgoyette struct elf_link_hash_entry *ind)
7359 1.13.12.2 pgoyette {
7360 1.13.12.2 pgoyette struct elf_link_hash_table *htab;
7361 1.1 skrll
7362 1.1 skrll /* Copy down any references that we may have already seen to the
7363 1.1 skrll symbol which just became indirect. */
7364 1.1 skrll
7365 1.1 skrll if (dir->versioned != versioned_hidden)
7366 1.1 skrll dir->ref_dynamic |= ind->ref_dynamic;
7367 1.1 skrll dir->ref_regular |= ind->ref_regular;
7368 1.1 skrll dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7369 1.1 skrll dir->non_got_ref |= ind->non_got_ref;
7370 1.1 skrll dir->needs_plt |= ind->needs_plt;
7371 1.1 skrll dir->pointer_equality_needed |= ind->pointer_equality_needed;
7372 1.1 skrll
7373 1.1 skrll if (ind->root.type != bfd_link_hash_indirect)
7374 1.1 skrll return;
7375 1.1 skrll
7376 1.1 skrll /* Copy over the global and procedure linkage table refcount entries.
7377 1.1 skrll These may have been already set up by a check_relocs routine. */
7378 1.1 skrll htab = elf_hash_table (info);
7379 1.1 skrll if (ind->got.refcount > htab->init_got_refcount.refcount)
7380 1.1 skrll {
7381 1.1 skrll if (dir->got.refcount < 0)
7382 1.1 skrll dir->got.refcount = 0;
7383 1.1 skrll dir->got.refcount += ind->got.refcount;
7384 1.1 skrll ind->got.refcount = htab->init_got_refcount.refcount;
7385 1.1 skrll }
7386 1.1 skrll
7387 1.1 skrll if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7388 1.1 skrll {
7389 1.1 skrll if (dir->plt.refcount < 0)
7390 1.1 skrll dir->plt.refcount = 0;
7391 1.1 skrll dir->plt.refcount += ind->plt.refcount;
7392 1.1 skrll ind->plt.refcount = htab->init_plt_refcount.refcount;
7393 1.1 skrll }
7394 1.1 skrll
7395 1.1 skrll if (ind->dynindx != -1)
7396 1.1 skrll {
7397 1.1 skrll if (dir->dynindx != -1)
7398 1.1 skrll _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7399 1.1 skrll dir->dynindx = ind->dynindx;
7400 1.4 christos dir->dynstr_index = ind->dynstr_index;
7401 1.4 christos ind->dynindx = -1;
7402 1.4 christos ind->dynstr_index = 0;
7403 1.4 christos }
7404 1.4 christos }
7405 1.4 christos
7406 1.1 skrll void
7407 1.1 skrll _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7408 1.1 skrll struct elf_link_hash_entry *h,
7409 1.1 skrll bfd_boolean force_local)
7410 1.1 skrll {
7411 1.1 skrll /* STT_GNU_IFUNC symbol must go through PLT. */
7412 1.1 skrll if (h->type != STT_GNU_IFUNC)
7413 1.13.12.2 pgoyette {
7414 1.13.12.2 pgoyette h->plt = elf_hash_table (info)->init_plt_offset;
7415 1.1 skrll h->needs_plt = 0;
7416 1.1 skrll }
7417 1.1 skrll if (force_local)
7418 1.1 skrll {
7419 1.9 christos h->forced_local = 1;
7420 1.9 christos if (h->dynindx != -1)
7421 1.1 skrll {
7422 1.1 skrll _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7423 1.1 skrll h->dynstr_index);
7424 1.1 skrll h->dynindx = -1;
7425 1.1 skrll h->dynstr_index = 0;
7426 1.1 skrll }
7427 1.1 skrll }
7428 1.1 skrll }
7429 1.4 christos
7430 1.4 christos /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7431 1.1 skrll caller. */
7432 1.1 skrll
7433 1.1 skrll bfd_boolean
7434 1.1 skrll _bfd_elf_link_hash_table_init
7435 1.1 skrll (struct elf_link_hash_table *table,
7436 1.1 skrll bfd *abfd,
7437 1.1 skrll struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7438 1.1 skrll struct bfd_hash_table *,
7439 1.1 skrll const char *),
7440 1.1 skrll unsigned int entsize,
7441 1.1 skrll enum elf_target_id target_id)
7442 1.1 skrll {
7443 1.4 christos bfd_boolean ret;
7444 1.1 skrll int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7445 1.4 christos
7446 1.1 skrll table->init_got_refcount.refcount = can_refcount - 1;
7447 1.1 skrll table->init_plt_refcount.refcount = can_refcount - 1;
7448 1.1 skrll table->init_got_offset.offset = -(bfd_vma) 1;
7449 1.1 skrll table->init_plt_offset.offset = -(bfd_vma) 1;
7450 1.1 skrll /* The first dynamic symbol is a dummy. */
7451 1.1 skrll table->dynsymcount = 1;
7452 1.1 skrll
7453 1.1 skrll ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7454 1.1 skrll
7455 1.1 skrll table->root.type = bfd_link_elf_hash_table;
7456 1.1 skrll table->hash_table_id = target_id;
7457 1.1 skrll
7458 1.9 christos return ret;
7459 1.1 skrll }
7460 1.1 skrll
7461 1.1 skrll /* Create an ELF linker hash table. */
7462 1.1 skrll
7463 1.4 christos struct bfd_link_hash_table *
7464 1.4 christos _bfd_elf_link_hash_table_create (bfd *abfd)
7465 1.1 skrll {
7466 1.1 skrll struct elf_link_hash_table *ret;
7467 1.1 skrll bfd_size_type amt = sizeof (struct elf_link_hash_table);
7468 1.1 skrll
7469 1.9 christos ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7470 1.1 skrll if (ret == NULL)
7471 1.1 skrll return NULL;
7472 1.1 skrll
7473 1.1 skrll if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7474 1.9 christos sizeof (struct elf_link_hash_entry),
7475 1.9 christos GENERIC_ELF_DATA))
7476 1.9 christos {
7477 1.9 christos free (ret);
7478 1.9 christos return NULL;
7479 1.9 christos }
7480 1.9 christos ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7481 1.9 christos
7482 1.9 christos return &ret->root;
7483 1.9 christos }
7484 1.9 christos
7485 1.9 christos /* Destroy an ELF linker hash table. */
7486 1.9 christos
7487 1.9 christos void
7488 1.1 skrll _bfd_elf_link_hash_table_free (bfd *obfd)
7489 1.1 skrll {
7490 1.1 skrll struct elf_link_hash_table *htab;
7491 1.1 skrll
7492 1.1 skrll htab = (struct elf_link_hash_table *) obfd->link.hash;
7493 1.1 skrll if (htab->dynstr != NULL)
7494 1.1 skrll _bfd_elf_strtab_free (htab->dynstr);
7495 1.1 skrll _bfd_merge_sections_free (htab->merge_info);
7496 1.1 skrll _bfd_generic_link_hash_table_free (obfd);
7497 1.1 skrll }
7498 1.1 skrll
7499 1.1 skrll /* This is a hook for the ELF emulation code in the generic linker to
7500 1.1 skrll tell the backend linker what file name to use for the DT_NEEDED
7501 1.1 skrll entry for a dynamic object. */
7502 1.1 skrll
7503 1.1 skrll void
7504 1.1 skrll bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7505 1.1 skrll {
7506 1.1 skrll if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7507 1.1 skrll && bfd_get_format (abfd) == bfd_object)
7508 1.1 skrll elf_dt_name (abfd) = name;
7509 1.1 skrll }
7510 1.1 skrll
7511 1.1 skrll int
7512 1.1 skrll bfd_elf_get_dyn_lib_class (bfd *abfd)
7513 1.1 skrll {
7514 1.1 skrll int lib_class;
7515 1.1 skrll if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7516 1.1 skrll && bfd_get_format (abfd) == bfd_object)
7517 1.1 skrll lib_class = elf_dyn_lib_class (abfd);
7518 1.1 skrll else
7519 1.1 skrll lib_class = 0;
7520 1.1 skrll return lib_class;
7521 1.1 skrll }
7522 1.1 skrll
7523 1.1 skrll void
7524 1.1 skrll bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7525 1.1 skrll {
7526 1.1 skrll if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7527 1.1 skrll && bfd_get_format (abfd) == bfd_object)
7528 1.1 skrll elf_dyn_lib_class (abfd) = lib_class;
7529 1.1 skrll }
7530 1.1 skrll
7531 1.1 skrll /* Get the list of DT_NEEDED entries for a link. This is a hook for
7532 1.1 skrll the linker ELF emulation code. */
7533 1.1 skrll
7534 1.1 skrll struct bfd_link_needed_list *
7535 1.1 skrll bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7536 1.1 skrll struct bfd_link_info *info)
7537 1.1 skrll {
7538 1.1 skrll if (! is_elf_hash_table (info->hash))
7539 1.1 skrll return NULL;
7540 1.1 skrll return elf_hash_table (info)->needed;
7541 1.1 skrll }
7542 1.1 skrll
7543 1.1 skrll /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7544 1.1 skrll hook for the linker ELF emulation code. */
7545 1.1 skrll
7546 1.1 skrll struct bfd_link_needed_list *
7547 1.1 skrll bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7548 1.1 skrll struct bfd_link_info *info)
7549 1.1 skrll {
7550 1.1 skrll if (! is_elf_hash_table (info->hash))
7551 1.1 skrll return NULL;
7552 1.1 skrll return elf_hash_table (info)->runpath;
7553 1.1 skrll }
7554 1.1 skrll
7555 1.1 skrll /* Get the name actually used for a dynamic object for a link. This
7556 1.1 skrll is the SONAME entry if there is one. Otherwise, it is the string
7557 1.1 skrll passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7558 1.1 skrll
7559 1.1 skrll const char *
7560 1.1 skrll bfd_elf_get_dt_soname (bfd *abfd)
7561 1.1 skrll {
7562 1.1 skrll if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7563 1.1 skrll && bfd_get_format (abfd) == bfd_object)
7564 1.1 skrll return elf_dt_name (abfd);
7565 1.1 skrll return NULL;
7566 1.1 skrll }
7567 1.1 skrll
7568 1.1 skrll /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7569 1.1 skrll the ELF linker emulation code. */
7570 1.1 skrll
7571 1.1 skrll bfd_boolean
7572 1.1 skrll bfd_elf_get_bfd_needed_list (bfd *abfd,
7573 1.1 skrll struct bfd_link_needed_list **pneeded)
7574 1.1 skrll {
7575 1.1 skrll asection *s;
7576 1.1 skrll bfd_byte *dynbuf = NULL;
7577 1.1 skrll unsigned int elfsec;
7578 1.1 skrll unsigned long shlink;
7579 1.1 skrll bfd_byte *extdyn, *extdynend;
7580 1.1 skrll size_t extdynsize;
7581 1.1 skrll void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7582 1.1 skrll
7583 1.1 skrll *pneeded = NULL;
7584 1.1 skrll
7585 1.1 skrll if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7586 1.1 skrll || bfd_get_format (abfd) != bfd_object)
7587 1.1 skrll return TRUE;
7588 1.1 skrll
7589 1.1 skrll s = bfd_get_section_by_name (abfd, ".dynamic");
7590 1.1 skrll if (s == NULL || s->size == 0)
7591 1.1 skrll return TRUE;
7592 1.1 skrll
7593 1.1 skrll if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7594 1.1 skrll goto error_return;
7595 1.1 skrll
7596 1.1 skrll elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7597 1.1 skrll if (elfsec == SHN_BAD)
7598 1.1 skrll goto error_return;
7599 1.1 skrll
7600 1.1 skrll shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7601 1.1 skrll
7602 1.1 skrll extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7603 1.1 skrll swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7604 1.1 skrll
7605 1.1 skrll extdyn = dynbuf;
7606 1.1 skrll extdynend = extdyn + s->size;
7607 1.1 skrll for (; extdyn < extdynend; extdyn += extdynsize)
7608 1.1 skrll {
7609 1.1 skrll Elf_Internal_Dyn dyn;
7610 1.1 skrll
7611 1.1 skrll (*swap_dyn_in) (abfd, extdyn, &dyn);
7612 1.1 skrll
7613 1.1 skrll if (dyn.d_tag == DT_NULL)
7614 1.1 skrll break;
7615 1.1 skrll
7616 1.1 skrll if (dyn.d_tag == DT_NEEDED)
7617 1.4 christos {
7618 1.1 skrll const char *string;
7619 1.1 skrll struct bfd_link_needed_list *l;
7620 1.1 skrll unsigned int tagv = dyn.d_un.d_val;
7621 1.1 skrll bfd_size_type amt;
7622 1.1 skrll
7623 1.1 skrll string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7624 1.1 skrll if (string == NULL)
7625 1.1 skrll goto error_return;
7626 1.1 skrll
7627 1.1 skrll amt = sizeof *l;
7628 1.1 skrll l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7629 1.1 skrll if (l == NULL)
7630 1.1 skrll goto error_return;
7631 1.1 skrll
7632 1.1 skrll l->by = abfd;
7633 1.1 skrll l->name = string;
7634 1.1 skrll l->next = *pneeded;
7635 1.1 skrll *pneeded = l;
7636 1.1 skrll }
7637 1.1 skrll }
7638 1.1 skrll
7639 1.1 skrll free (dynbuf);
7640 1.1 skrll
7641 1.1 skrll return TRUE;
7642 1.1 skrll
7643 1.1 skrll error_return:
7644 1.1 skrll if (dynbuf != NULL)
7645 1.1 skrll free (dynbuf);
7646 1.1 skrll return FALSE;
7647 1.1 skrll }
7648 1.13 christos
7649 1.1 skrll struct elf_symbuf_symbol
7650 1.1 skrll {
7651 1.1 skrll unsigned long st_name; /* Symbol name, index in string tbl */
7652 1.1 skrll unsigned char st_info; /* Type and binding attributes */
7653 1.1 skrll unsigned char st_other; /* Visibilty, and target specific */
7654 1.1 skrll };
7655 1.1 skrll
7656 1.1 skrll struct elf_symbuf_head
7657 1.1 skrll {
7658 1.1 skrll struct elf_symbuf_symbol *ssym;
7659 1.1 skrll size_t count;
7660 1.1 skrll unsigned int st_shndx;
7661 1.1 skrll };
7662 1.1 skrll
7663 1.1 skrll struct elf_symbol
7664 1.1 skrll {
7665 1.1 skrll union
7666 1.1 skrll {
7667 1.1 skrll Elf_Internal_Sym *isym;
7668 1.1 skrll struct elf_symbuf_symbol *ssym;
7669 1.1 skrll } u;
7670 1.1 skrll const char *name;
7671 1.1 skrll };
7672 1.1 skrll
7673 1.1 skrll /* Sort references to symbols by ascending section number. */
7674 1.1 skrll
7675 1.1 skrll static int
7676 1.1 skrll elf_sort_elf_symbol (const void *arg1, const void *arg2)
7677 1.1 skrll {
7678 1.1 skrll const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7679 1.1 skrll const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7680 1.1 skrll
7681 1.1 skrll return s1->st_shndx - s2->st_shndx;
7682 1.13 christos }
7683 1.1 skrll
7684 1.1 skrll static int
7685 1.1 skrll elf_sym_name_compare (const void *arg1, const void *arg2)
7686 1.1 skrll {
7687 1.13 christos const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7688 1.1 skrll const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7689 1.4 christos return strcmp (s1->name, s2->name);
7690 1.1 skrll }
7691 1.1 skrll
7692 1.1 skrll static struct elf_symbuf_head *
7693 1.1 skrll elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7694 1.1 skrll {
7695 1.1 skrll Elf_Internal_Sym **ind, **indbufend, **indbuf;
7696 1.1 skrll struct elf_symbuf_symbol *ssym;
7697 1.1 skrll struct elf_symbuf_head *ssymbuf, *ssymhead;
7698 1.1 skrll size_t i, shndx_count, total_size;
7699 1.1 skrll
7700 1.1 skrll indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7701 1.1 skrll if (indbuf == NULL)
7702 1.1 skrll return NULL;
7703 1.1 skrll
7704 1.1 skrll for (ind = indbuf, i = 0; i < symcount; i++)
7705 1.1 skrll if (isymbuf[i].st_shndx != SHN_UNDEF)
7706 1.1 skrll *ind++ = &isymbuf[i];
7707 1.1 skrll indbufend = ind;
7708 1.1 skrll
7709 1.4 christos qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7710 1.1 skrll elf_sort_elf_symbol);
7711 1.1 skrll
7712 1.1 skrll shndx_count = 0;
7713 1.1 skrll if (indbufend > indbuf)
7714 1.1 skrll for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7715 1.1 skrll if (ind[0]->st_shndx != ind[1]->st_shndx)
7716 1.1 skrll shndx_count++;
7717 1.1 skrll
7718 1.1 skrll total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7719 1.1 skrll + (indbufend - indbuf) * sizeof (*ssym));
7720 1.1 skrll ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7721 1.1 skrll if (ssymbuf == NULL)
7722 1.1 skrll {
7723 1.1 skrll free (indbuf);
7724 1.1 skrll return NULL;
7725 1.1 skrll }
7726 1.1 skrll
7727 1.1 skrll ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7728 1.1 skrll ssymbuf->ssym = NULL;
7729 1.1 skrll ssymbuf->count = shndx_count;
7730 1.1 skrll ssymbuf->st_shndx = 0;
7731 1.1 skrll for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7732 1.1 skrll {
7733 1.1 skrll if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7734 1.13 christos {
7735 1.1 skrll ssymhead++;
7736 1.1 skrll ssymhead->ssym = ssym;
7737 1.1 skrll ssymhead->count = 0;
7738 1.1 skrll ssymhead->st_shndx = (*ind)->st_shndx;
7739 1.1 skrll }
7740 1.1 skrll ssym->st_name = (*ind)->st_name;
7741 1.1 skrll ssym->st_info = (*ind)->st_info;
7742 1.1 skrll ssym->st_other = (*ind)->st_other;
7743 1.1 skrll ssymhead->count++;
7744 1.1 skrll }
7745 1.1 skrll BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7746 1.1 skrll && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7747 1.1 skrll == total_size));
7748 1.1 skrll
7749 1.1 skrll free (indbuf);
7750 1.1 skrll return ssymbuf;
7751 1.1 skrll }
7752 1.13 christos
7753 1.1 skrll /* Check if 2 sections define the same set of local and global
7754 1.1 skrll symbols. */
7755 1.1 skrll
7756 1.1 skrll static bfd_boolean
7757 1.13 christos bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7758 1.1 skrll struct bfd_link_info *info)
7759 1.1 skrll {
7760 1.1 skrll bfd *bfd1, *bfd2;
7761 1.1 skrll const struct elf_backend_data *bed1, *bed2;
7762 1.1 skrll Elf_Internal_Shdr *hdr1, *hdr2;
7763 1.1 skrll size_t symcount1, symcount2;
7764 1.1 skrll Elf_Internal_Sym *isymbuf1, *isymbuf2;
7765 1.1 skrll struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7766 1.1 skrll Elf_Internal_Sym *isym, *isymend;
7767 1.1 skrll struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7768 1.1 skrll size_t count1, count2, i;
7769 1.1 skrll unsigned int shndx1, shndx2;
7770 1.1 skrll bfd_boolean result;
7771 1.1 skrll
7772 1.1 skrll bfd1 = sec1->owner;
7773 1.1 skrll bfd2 = sec2->owner;
7774 1.1 skrll
7775 1.1 skrll /* Both sections have to be in ELF. */
7776 1.1 skrll if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7777 1.1 skrll || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7778 1.1 skrll return FALSE;
7779 1.1 skrll
7780 1.1 skrll if (elf_section_type (sec1) != elf_section_type (sec2))
7781 1.1 skrll return FALSE;
7782 1.1 skrll
7783 1.1 skrll shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7784 1.1 skrll shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7785 1.1 skrll if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7786 1.1 skrll return FALSE;
7787 1.1 skrll
7788 1.1 skrll bed1 = get_elf_backend_data (bfd1);
7789 1.1 skrll bed2 = get_elf_backend_data (bfd2);
7790 1.4 christos hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7791 1.4 christos symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7792 1.1 skrll hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7793 1.1 skrll symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7794 1.1 skrll
7795 1.1 skrll if (symcount1 == 0 || symcount2 == 0)
7796 1.1 skrll return FALSE;
7797 1.1 skrll
7798 1.1 skrll result = FALSE;
7799 1.1 skrll isymbuf1 = NULL;
7800 1.1 skrll isymbuf2 = NULL;
7801 1.1 skrll ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7802 1.1 skrll ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7803 1.1 skrll
7804 1.1 skrll if (ssymbuf1 == NULL)
7805 1.1 skrll {
7806 1.1 skrll isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7807 1.1 skrll NULL, NULL, NULL);
7808 1.1 skrll if (isymbuf1 == NULL)
7809 1.1 skrll goto done;
7810 1.1 skrll
7811 1.1 skrll if (!info->reduce_memory_overheads)
7812 1.1 skrll elf_tdata (bfd1)->symbuf = ssymbuf1
7813 1.1 skrll = elf_create_symbuf (symcount1, isymbuf1);
7814 1.1 skrll }
7815 1.1 skrll
7816 1.1 skrll if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7817 1.1 skrll {
7818 1.1 skrll isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7819 1.1 skrll NULL, NULL, NULL);
7820 1.13 christos if (isymbuf2 == NULL)
7821 1.1 skrll goto done;
7822 1.1 skrll
7823 1.1 skrll if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7824 1.1 skrll elf_tdata (bfd2)->symbuf = ssymbuf2
7825 1.1 skrll = elf_create_symbuf (symcount2, isymbuf2);
7826 1.1 skrll }
7827 1.1 skrll
7828 1.1 skrll if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7829 1.1 skrll {
7830 1.1 skrll /* Optimized faster version. */
7831 1.1 skrll size_t lo, hi, mid;
7832 1.1 skrll struct elf_symbol *symp;
7833 1.1 skrll struct elf_symbuf_symbol *ssym, *ssymend;
7834 1.1 skrll
7835 1.1 skrll lo = 0;
7836 1.1 skrll hi = ssymbuf1->count;
7837 1.1 skrll ssymbuf1++;
7838 1.1 skrll count1 = 0;
7839 1.1 skrll while (lo < hi)
7840 1.1 skrll {
7841 1.1 skrll mid = (lo + hi) / 2;
7842 1.1 skrll if (shndx1 < ssymbuf1[mid].st_shndx)
7843 1.1 skrll hi = mid;
7844 1.1 skrll else if (shndx1 > ssymbuf1[mid].st_shndx)
7845 1.1 skrll lo = mid + 1;
7846 1.1 skrll else
7847 1.1 skrll {
7848 1.1 skrll count1 = ssymbuf1[mid].count;
7849 1.1 skrll ssymbuf1 += mid;
7850 1.1 skrll break;
7851 1.1 skrll }
7852 1.1 skrll }
7853 1.1 skrll
7854 1.1 skrll lo = 0;
7855 1.1 skrll hi = ssymbuf2->count;
7856 1.1 skrll ssymbuf2++;
7857 1.1 skrll count2 = 0;
7858 1.1 skrll while (lo < hi)
7859 1.1 skrll {
7860 1.1 skrll mid = (lo + hi) / 2;
7861 1.1 skrll if (shndx2 < ssymbuf2[mid].st_shndx)
7862 1.1 skrll hi = mid;
7863 1.1 skrll else if (shndx2 > ssymbuf2[mid].st_shndx)
7864 1.1 skrll lo = mid + 1;
7865 1.9 christos else
7866 1.9 christos {
7867 1.9 christos count2 = ssymbuf2[mid].count;
7868 1.9 christos ssymbuf2 += mid;
7869 1.1 skrll break;
7870 1.1 skrll }
7871 1.1 skrll }
7872 1.1 skrll
7873 1.1 skrll if (count1 == 0 || count2 == 0 || count1 != count2)
7874 1.1 skrll goto done;
7875 1.1 skrll
7876 1.1 skrll symtable1
7877 1.1 skrll = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7878 1.1 skrll symtable2
7879 1.1 skrll = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7880 1.1 skrll if (symtable1 == NULL || symtable2 == NULL)
7881 1.1 skrll goto done;
7882 1.1 skrll
7883 1.1 skrll symp = symtable1;
7884 1.1 skrll for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7885 1.1 skrll ssym < ssymend; ssym++, symp++)
7886 1.1 skrll {
7887 1.1 skrll symp->u.ssym = ssym;
7888 1.1 skrll symp->name = bfd_elf_string_from_elf_section (bfd1,
7889 1.1 skrll hdr1->sh_link,
7890 1.1 skrll ssym->st_name);
7891 1.1 skrll }
7892 1.1 skrll
7893 1.1 skrll symp = symtable2;
7894 1.1 skrll for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7895 1.1 skrll ssym < ssymend; ssym++, symp++)
7896 1.1 skrll {
7897 1.1 skrll symp->u.ssym = ssym;
7898 1.1 skrll symp->name = bfd_elf_string_from_elf_section (bfd2,
7899 1.1 skrll hdr2->sh_link,
7900 1.1 skrll ssym->st_name);
7901 1.1 skrll }
7902 1.1 skrll
7903 1.1 skrll /* Sort symbol by name. */
7904 1.1 skrll qsort (symtable1, count1, sizeof (struct elf_symbol),
7905 1.1 skrll elf_sym_name_compare);
7906 1.1 skrll qsort (symtable2, count1, sizeof (struct elf_symbol),
7907 1.1 skrll elf_sym_name_compare);
7908 1.1 skrll
7909 1.4 christos for (i = 0; i < count1; i++)
7910 1.4 christos /* Two symbols must have the same binding, type and name. */
7911 1.4 christos if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7912 1.4 christos || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7913 1.1 skrll || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7914 1.1 skrll goto done;
7915 1.1 skrll
7916 1.1 skrll result = TRUE;
7917 1.1 skrll goto done;
7918 1.1 skrll }
7919 1.1 skrll
7920 1.1 skrll symtable1 = (struct elf_symbol *)
7921 1.1 skrll bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7922 1.1 skrll symtable2 = (struct elf_symbol *)
7923 1.1 skrll bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7924 1.1 skrll if (symtable1 == NULL || symtable2 == NULL)
7925 1.1 skrll goto done;
7926 1.1 skrll
7927 1.1 skrll /* Count definitions in the section. */
7928 1.1 skrll count1 = 0;
7929 1.1 skrll for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7930 1.1 skrll if (isym->st_shndx == shndx1)
7931 1.1 skrll symtable1[count1++].u.isym = isym;
7932 1.1 skrll
7933 1.1 skrll count2 = 0;
7934 1.1 skrll for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7935 1.1 skrll if (isym->st_shndx == shndx2)
7936 1.1 skrll symtable2[count2++].u.isym = isym;
7937 1.1 skrll
7938 1.1 skrll if (count1 == 0 || count2 == 0 || count1 != count2)
7939 1.1 skrll goto done;
7940 1.1 skrll
7941 1.1 skrll for (i = 0; i < count1; i++)
7942 1.1 skrll symtable1[i].name
7943 1.1 skrll = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7944 1.1 skrll symtable1[i].u.isym->st_name);
7945 1.1 skrll
7946 1.1 skrll for (i = 0; i < count2; i++)
7947 1.1 skrll symtable2[i].name
7948 1.1 skrll = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7949 1.1 skrll symtable2[i].u.isym->st_name);
7950 1.1 skrll
7951 1.1 skrll /* Sort symbol by name. */
7952 1.1 skrll qsort (symtable1, count1, sizeof (struct elf_symbol),
7953 1.1 skrll elf_sym_name_compare);
7954 1.1 skrll qsort (symtable2, count1, sizeof (struct elf_symbol),
7955 1.1 skrll elf_sym_name_compare);
7956 1.1 skrll
7957 1.1 skrll for (i = 0; i < count1; i++)
7958 1.1 skrll /* Two symbols must have the same binding, type and name. */
7959 1.1 skrll if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7960 1.1 skrll || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7961 1.1 skrll || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7962 1.1 skrll goto done;
7963 1.1 skrll
7964 1.1 skrll result = TRUE;
7965 1.1 skrll
7966 1.1 skrll done:
7967 1.1 skrll if (symtable1)
7968 1.1 skrll free (symtable1);
7969 1.1 skrll if (symtable2)
7970 1.1 skrll free (symtable2);
7971 1.1 skrll if (isymbuf1)
7972 1.1 skrll free (isymbuf1);
7973 1.1 skrll if (isymbuf2)
7974 1.1 skrll free (isymbuf2);
7975 1.1 skrll
7976 1.1 skrll return result;
7977 1.1 skrll }
7978 1.1 skrll
7979 1.1 skrll /* Return TRUE if 2 section types are compatible. */
7980 1.1 skrll
7981 1.1 skrll bfd_boolean
7982 1.1 skrll _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7983 1.1 skrll bfd *bbfd, const asection *bsec)
7984 1.1 skrll {
7985 1.1 skrll if (asec == NULL
7986 1.1 skrll || bsec == NULL
7987 1.1 skrll || abfd->xvec->flavour != bfd_target_elf_flavour
7988 1.1 skrll || bbfd->xvec->flavour != bfd_target_elf_flavour)
7989 1.1 skrll return TRUE;
7990 1.1 skrll
7991 1.1 skrll return elf_section_type (asec) == elf_section_type (bsec);
7992 1.1 skrll }
7993 1.1 skrll
7994 1.9 christos /* Final phase of ELF linker. */
7996 1.1 skrll
7997 1.1 skrll /* A structure we use to avoid passing large numbers of arguments. */
7998 1.1 skrll
7999 1.1 skrll struct elf_final_link_info
8000 1.1 skrll {
8001 1.1 skrll /* General link information. */
8002 1.1 skrll struct bfd_link_info *info;
8003 1.1 skrll /* Output BFD. */
8004 1.1 skrll bfd *output_bfd;
8005 1.1 skrll /* Symbol string table. */
8006 1.1 skrll struct elf_strtab_hash *symstrtab;
8007 1.1 skrll /* .hash section. */
8008 1.1 skrll asection *hash_sec;
8009 1.1 skrll /* symbol version section (.gnu.version). */
8010 1.1 skrll asection *symver_sec;
8011 1.1 skrll /* Buffer large enough to hold contents of any section. */
8012 1.1 skrll bfd_byte *contents;
8013 1.1 skrll /* Buffer large enough to hold external relocs of any section. */
8014 1.1 skrll void *external_relocs;
8015 1.1 skrll /* Buffer large enough to hold internal relocs of any section. */
8016 1.1 skrll Elf_Internal_Rela *internal_relocs;
8017 1.1 skrll /* Buffer large enough to hold external local symbols of any input
8018 1.1 skrll BFD. */
8019 1.9 christos bfd_byte *external_syms;
8020 1.1 skrll /* And a buffer for symbol section indices. */
8021 1.7 christos Elf_External_Sym_Shndx *locsym_shndx;
8022 1.7 christos /* Buffer large enough to hold internal local symbols of any input
8023 1.1 skrll BFD. */
8024 1.1 skrll Elf_Internal_Sym *internal_syms;
8025 1.1 skrll /* Array large enough to hold a symbol index for each local symbol
8026 1.1 skrll of any input BFD. */
8027 1.1 skrll long *indices;
8028 1.1 skrll /* Array large enough to hold a section pointer for each local
8029 1.1 skrll symbol of any input BFD. */
8030 1.1 skrll asection **sections;
8031 1.9 christos /* Buffer for SHT_SYMTAB_SHNDX section. */
8032 1.7 christos Elf_External_Sym_Shndx *symshndxbuf;
8033 1.1 skrll /* Number of STT_FILE syms seen. */
8034 1.1 skrll size_t filesym_count;
8035 1.1 skrll };
8036 1.1 skrll
8037 1.1 skrll /* This struct is used to pass information to elf_link_output_extsym. */
8038 1.1 skrll
8039 1.1 skrll struct elf_outext_info
8040 1.1 skrll {
8041 1.1 skrll bfd_boolean failed;
8042 1.1 skrll bfd_boolean localsyms;
8043 1.1 skrll bfd_boolean file_sym_done;
8044 1.1 skrll struct elf_final_link_info *flinfo;
8045 1.1 skrll };
8046 1.1 skrll
8047 1.1 skrll
8048 1.1 skrll /* Support for evaluating a complex relocation.
8049 1.1 skrll
8050 1.1 skrll Complex relocations are generalized, self-describing relocations. The
8051 1.1 skrll implementation of them consists of two parts: complex symbols, and the
8052 1.1 skrll relocations themselves.
8053 1.13.12.2 pgoyette
8054 1.13.12.2 pgoyette The relocations are use a reserved elf-wide relocation type code (R_RELC
8055 1.1 skrll external / BFD_RELOC_RELC internal) and an encoding of relocation field
8056 1.1 skrll information (start bit, end bit, word width, etc) into the addend. This
8057 1.1 skrll information is extracted from CGEN-generated operand tables within gas.
8058 1.13.12.2 pgoyette
8059 1.1 skrll Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8060 1.1 skrll internal) representing prefix-notation expressions, including but not
8061 1.1 skrll limited to those sorts of expressions normally encoded as addends in the
8062 1.1 skrll addend field. The symbol mangling format is:
8063 1.1 skrll
8064 1.1 skrll <node> := <literal>
8065 1.1 skrll | <unary-operator> ':' <node>
8066 1.1 skrll | <binary-operator> ':' <node> ':' <node>
8067 1.1 skrll ;
8068 1.1 skrll
8069 1.1 skrll <literal> := 's' <digits=N> ':' <N character symbol name>
8070 1.1 skrll | 'S' <digits=N> ':' <N character section name>
8071 1.1 skrll | '#' <hexdigits>
8072 1.1 skrll ;
8073 1.1 skrll
8074 1.1 skrll <binary-operator> := as in C
8075 1.1 skrll <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8076 1.1 skrll
8077 1.1 skrll static void
8078 1.1 skrll set_symbol_value (bfd *bfd_with_globals,
8079 1.1 skrll Elf_Internal_Sym *isymbuf,
8080 1.1 skrll size_t locsymcount,
8081 1.1 skrll size_t symidx,
8082 1.1 skrll bfd_vma val)
8083 1.1 skrll {
8084 1.1 skrll struct elf_link_hash_entry **sym_hashes;
8085 1.1 skrll struct elf_link_hash_entry *h;
8086 1.1 skrll size_t extsymoff = locsymcount;
8087 1.1 skrll
8088 1.1 skrll if (symidx < locsymcount)
8089 1.1 skrll {
8090 1.1 skrll Elf_Internal_Sym *sym;
8091 1.1 skrll
8092 1.1 skrll sym = isymbuf + symidx;
8093 1.1 skrll if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8094 1.1 skrll {
8095 1.1 skrll /* It is a local symbol: move it to the
8096 1.1 skrll "absolute" section and give it a value. */
8097 1.1 skrll sym->st_shndx = SHN_ABS;
8098 1.1 skrll sym->st_value = val;
8099 1.1 skrll return;
8100 1.1 skrll }
8101 1.1 skrll BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8102 1.1 skrll extsymoff = 0;
8103 1.1 skrll }
8104 1.1 skrll
8105 1.1 skrll /* It is a global symbol: set its link type
8106 1.1 skrll to "defined" and give it a value. */
8107 1.1 skrll
8108 1.1 skrll sym_hashes = elf_sym_hashes (bfd_with_globals);
8109 1.7 christos h = sym_hashes [symidx - extsymoff];
8110 1.1 skrll while (h->root.type == bfd_link_hash_indirect
8111 1.1 skrll || h->root.type == bfd_link_hash_warning)
8112 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
8113 1.1 skrll h->root.type = bfd_link_hash_defined;
8114 1.1 skrll h->root.u.def.value = val;
8115 1.1 skrll h->root.u.def.section = bfd_abs_section_ptr;
8116 1.1 skrll }
8117 1.1 skrll
8118 1.1 skrll static bfd_boolean
8119 1.1 skrll resolve_symbol (const char *name,
8120 1.1 skrll bfd *input_bfd,
8121 1.1 skrll struct elf_final_link_info *flinfo,
8122 1.1 skrll bfd_vma *result,
8123 1.1 skrll Elf_Internal_Sym *isymbuf,
8124 1.1 skrll size_t locsymcount)
8125 1.1 skrll {
8126 1.1 skrll Elf_Internal_Sym *sym;
8127 1.1 skrll struct bfd_link_hash_entry *global_entry;
8128 1.1 skrll const char *candidate = NULL;
8129 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
8130 1.1 skrll size_t i;
8131 1.1 skrll
8132 1.1 skrll symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8133 1.1 skrll
8134 1.1 skrll for (i = 0; i < locsymcount; ++ i)
8135 1.1 skrll {
8136 1.1 skrll sym = isymbuf + i;
8137 1.1 skrll
8138 1.7 christos if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8139 1.1 skrll continue;
8140 1.1 skrll
8141 1.1 skrll candidate = bfd_elf_string_from_elf_section (input_bfd,
8142 1.1 skrll symtab_hdr->sh_link,
8143 1.1 skrll sym->st_name);
8144 1.1 skrll #ifdef DEBUG
8145 1.1 skrll printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8146 1.1 skrll name, candidate, (unsigned long) sym->st_value);
8147 1.1 skrll #endif
8148 1.1 skrll if (candidate && strcmp (candidate, name) == 0)
8149 1.1 skrll {
8150 1.1 skrll asection *sec = flinfo->sections [i];
8151 1.7 christos
8152 1.1 skrll *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8153 1.1 skrll *result += sec->output_offset + sec->output_section->vma;
8154 1.1 skrll #ifdef DEBUG
8155 1.1 skrll printf ("Found symbol with value %8.8lx\n",
8156 1.1 skrll (unsigned long) *result);
8157 1.1 skrll #endif
8158 1.1 skrll return TRUE;
8159 1.1 skrll }
8160 1.1 skrll }
8161 1.1 skrll
8162 1.1 skrll /* Hmm, haven't found it yet. perhaps it is a global. */
8163 1.1 skrll global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8164 1.1 skrll FALSE, FALSE, TRUE);
8165 1.1 skrll if (!global_entry)
8166 1.1 skrll return FALSE;
8167 1.1 skrll
8168 1.1 skrll if (global_entry->type == bfd_link_hash_defined
8169 1.1 skrll || global_entry->type == bfd_link_hash_defweak)
8170 1.1 skrll {
8171 1.1 skrll *result = (global_entry->u.def.value
8172 1.13 christos + global_entry->u.def.section->output_section->vma
8173 1.13 christos + global_entry->u.def.section->output_offset);
8174 1.13 christos #ifdef DEBUG
8175 1.13.12.2 pgoyette printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8176 1.1 skrll global_entry->root.string, (unsigned long) *result);
8177 1.1 skrll #endif
8178 1.1 skrll return TRUE;
8179 1.13 christos }
8180 1.13 christos
8181 1.1 skrll return FALSE;
8182 1.1 skrll }
8183 1.1 skrll
8184 1.1 skrll /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8185 1.1 skrll bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8186 1.1 skrll names like "foo.end" which is the end address of section "foo". */
8187 1.1 skrll
8188 1.1 skrll static bfd_boolean
8189 1.1 skrll resolve_section (const char *name,
8190 1.1 skrll asection *sections,
8191 1.1 skrll bfd_vma *result,
8192 1.1 skrll bfd * abfd)
8193 1.13 christos {
8194 1.1 skrll asection *curr;
8195 1.1 skrll unsigned int len;
8196 1.1 skrll
8197 1.1 skrll for (curr = sections; curr; curr = curr->next)
8198 1.1 skrll if (strcmp (curr->name, name) == 0)
8199 1.1 skrll {
8200 1.1 skrll *result = curr->vma;
8201 1.1 skrll return TRUE;
8202 1.1 skrll }
8203 1.1 skrll
8204 1.13 christos /* Hmm. still haven't found it. try pseudo-section names. */
8205 1.1 skrll /* FIXME: This could be coded more efficiently... */
8206 1.1 skrll for (curr = sections; curr; curr = curr->next)
8207 1.1 skrll {
8208 1.1 skrll len = strlen (curr->name);
8209 1.1 skrll if (len > strlen (name))
8210 1.1 skrll continue;
8211 1.1 skrll
8212 1.1 skrll if (strncmp (curr->name, name, len) == 0)
8213 1.1 skrll {
8214 1.1 skrll if (strncmp (".end", name + len, 4) == 0)
8215 1.1 skrll {
8216 1.1 skrll *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8217 1.1 skrll return TRUE;
8218 1.13.12.2 pgoyette }
8219 1.1 skrll
8220 1.1 skrll /* Insert more pseudo-section names here, if you like. */
8221 1.1 skrll }
8222 1.1 skrll }
8223 1.1 skrll
8224 1.1 skrll return FALSE;
8225 1.1 skrll }
8226 1.1 skrll
8227 1.7 christos static void
8228 1.1 skrll undefined_reference (const char *reftype, const char *name)
8229 1.1 skrll {
8230 1.1 skrll /* xgettext:c-format */
8231 1.1 skrll _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8232 1.1 skrll reftype, name);
8233 1.1 skrll }
8234 1.1 skrll
8235 1.1 skrll static bfd_boolean
8236 1.1 skrll eval_symbol (bfd_vma *result,
8237 1.1 skrll const char **symp,
8238 1.1 skrll bfd *input_bfd,
8239 1.1 skrll struct elf_final_link_info *flinfo,
8240 1.1 skrll bfd_vma dot,
8241 1.1 skrll Elf_Internal_Sym *isymbuf,
8242 1.1 skrll size_t locsymcount,
8243 1.1 skrll int signed_p)
8244 1.1 skrll {
8245 1.1 skrll size_t len;
8246 1.1 skrll size_t symlen;
8247 1.1 skrll bfd_vma a;
8248 1.1 skrll bfd_vma b;
8249 1.1 skrll char symbuf[4096];
8250 1.1 skrll const char *sym = *symp;
8251 1.1 skrll const char *symend;
8252 1.1 skrll bfd_boolean symbol_is_section = FALSE;
8253 1.1 skrll
8254 1.1 skrll len = strlen (sym);
8255 1.1 skrll symend = sym + len;
8256 1.1 skrll
8257 1.1 skrll if (len < 1 || len > sizeof (symbuf))
8258 1.1 skrll {
8259 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
8260 1.1 skrll return FALSE;
8261 1.1 skrll }
8262 1.1 skrll
8263 1.1 skrll switch (* sym)
8264 1.1 skrll {
8265 1.13.12.2 pgoyette case '.':
8266 1.1 skrll *result = dot;
8267 1.1 skrll *symp = sym + 1;
8268 1.1 skrll return TRUE;
8269 1.1 skrll
8270 1.1 skrll case '#':
8271 1.1 skrll ++sym;
8272 1.1 skrll *result = strtoul (sym, (char **) symp, 16);
8273 1.1 skrll return TRUE;
8274 1.1 skrll
8275 1.1 skrll case 'S':
8276 1.1 skrll symbol_is_section = TRUE;
8277 1.1 skrll /* Fall through. */
8278 1.1 skrll case 's':
8279 1.1 skrll ++sym;
8280 1.1 skrll symlen = strtol (sym, (char **) symp, 10);
8281 1.1 skrll sym = *symp + 1; /* Skip the trailing ':'. */
8282 1.1 skrll
8283 1.1 skrll if (symend < sym || symlen + 1 > sizeof (symbuf))
8284 1.1 skrll {
8285 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
8286 1.1 skrll return FALSE;
8287 1.1 skrll }
8288 1.13 christos
8289 1.7 christos memcpy (symbuf, sym, symlen);
8290 1.1 skrll symbuf[symlen] = '\0';
8291 1.1 skrll *symp = sym + symlen;
8292 1.1 skrll
8293 1.1 skrll /* Is it always possible, with complex symbols, that gas "mis-guessed"
8294 1.1 skrll the symbol as a section, or vice-versa. so we're pretty liberal in our
8295 1.1 skrll interpretation here; section means "try section first", not "must be a
8296 1.1 skrll section", and likewise with symbol. */
8297 1.1 skrll
8298 1.7 christos if (symbol_is_section)
8299 1.1 skrll {
8300 1.7 christos if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8301 1.13 christos && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8302 1.1 skrll isymbuf, locsymcount))
8303 1.1 skrll {
8304 1.1 skrll undefined_reference ("section", symbuf);
8305 1.1 skrll return FALSE;
8306 1.1 skrll }
8307 1.1 skrll }
8308 1.1 skrll else
8309 1.1 skrll {
8310 1.1 skrll if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8311 1.1 skrll isymbuf, locsymcount)
8312 1.1 skrll && !resolve_section (symbuf, flinfo->output_bfd->sections,
8313 1.1 skrll result, input_bfd))
8314 1.1 skrll {
8315 1.1 skrll undefined_reference ("symbol", symbuf);
8316 1.1 skrll return FALSE;
8317 1.1 skrll }
8318 1.1 skrll }
8319 1.7 christos
8320 1.1 skrll return TRUE;
8321 1.1 skrll
8322 1.1 skrll /* All that remains are operators. */
8323 1.1 skrll
8324 1.1 skrll #define UNARY_OP(op) \
8325 1.1 skrll if (strncmp (sym, #op, strlen (#op)) == 0) \
8326 1.1 skrll { \
8327 1.1 skrll sym += strlen (#op); \
8328 1.1 skrll if (*sym == ':') \
8329 1.1 skrll ++sym; \
8330 1.1 skrll *symp = sym; \
8331 1.1 skrll if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8332 1.1 skrll isymbuf, locsymcount, signed_p)) \
8333 1.1 skrll return FALSE; \
8334 1.1 skrll if (signed_p) \
8335 1.1 skrll *result = op ((bfd_signed_vma) a); \
8336 1.7 christos else \
8337 1.1 skrll *result = op a; \
8338 1.1 skrll return TRUE; \
8339 1.1 skrll }
8340 1.7 christos
8341 1.1 skrll #define BINARY_OP(op) \
8342 1.1 skrll if (strncmp (sym, #op, strlen (#op)) == 0) \
8343 1.1 skrll { \
8344 1.1 skrll sym += strlen (#op); \
8345 1.1 skrll if (*sym == ':') \
8346 1.1 skrll ++sym; \
8347 1.1 skrll *symp = sym; \
8348 1.1 skrll if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8349 1.1 skrll isymbuf, locsymcount, signed_p)) \
8350 1.1 skrll return FALSE; \
8351 1.1 skrll ++*symp; \
8352 1.1 skrll if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8353 1.1 skrll isymbuf, locsymcount, signed_p)) \
8354 1.1 skrll return FALSE; \
8355 1.1 skrll if (signed_p) \
8356 1.1 skrll *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8357 1.1 skrll else \
8358 1.1 skrll *result = a op b; \
8359 1.1 skrll return TRUE; \
8360 1.1 skrll }
8361 1.1 skrll
8362 1.1 skrll default:
8363 1.1 skrll UNARY_OP (0-);
8364 1.1 skrll BINARY_OP (<<);
8365 1.1 skrll BINARY_OP (>>);
8366 1.1 skrll BINARY_OP (==);
8367 1.1 skrll BINARY_OP (!=);
8368 1.1 skrll BINARY_OP (<=);
8369 1.1 skrll BINARY_OP (>=);
8370 1.1 skrll BINARY_OP (&&);
8371 1.1 skrll BINARY_OP (||);
8372 1.1 skrll UNARY_OP (~);
8373 1.1 skrll UNARY_OP (!);
8374 1.1 skrll BINARY_OP (*);
8375 1.1 skrll BINARY_OP (/);
8376 1.1 skrll BINARY_OP (%);
8377 1.1 skrll BINARY_OP (^);
8378 1.1 skrll BINARY_OP (|);
8379 1.1 skrll BINARY_OP (&);
8380 1.1 skrll BINARY_OP (+);
8381 1.1 skrll BINARY_OP (-);
8382 1.1 skrll BINARY_OP (<);
8383 1.1 skrll BINARY_OP (>);
8384 1.1 skrll #undef UNARY_OP
8385 1.1 skrll #undef BINARY_OP
8386 1.1 skrll _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8387 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
8388 1.1 skrll return FALSE;
8389 1.9 christos }
8390 1.1 skrll }
8391 1.1 skrll
8392 1.1 skrll static void
8393 1.1 skrll put_value (bfd_vma size,
8394 1.1 skrll unsigned long chunksz,
8395 1.9 christos bfd *input_bfd,
8396 1.1 skrll bfd_vma x,
8397 1.1 skrll bfd_byte *location)
8398 1.1 skrll {
8399 1.9 christos location += (size - chunksz);
8400 1.1 skrll
8401 1.1 skrll for (; size; size -= chunksz, location -= chunksz)
8402 1.1 skrll {
8403 1.9 christos switch (chunksz)
8404 1.9 christos {
8405 1.9 christos case 1:
8406 1.1 skrll bfd_put_8 (input_bfd, x, location);
8407 1.9 christos x >>= 8;
8408 1.1 skrll break;
8409 1.1 skrll case 2:
8410 1.9 christos bfd_put_16 (input_bfd, x, location);
8411 1.9 christos x >>= 16;
8412 1.9 christos break;
8413 1.9 christos case 4:
8414 1.9 christos bfd_put_32 (input_bfd, x, location);
8415 1.9 christos /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8416 1.1 skrll x >>= 16;
8417 1.1 skrll x >>= 16;
8418 1.1 skrll break;
8419 1.1 skrll #ifdef BFD64
8420 1.1 skrll case 8:
8421 1.1 skrll bfd_put_64 (input_bfd, x, location);
8422 1.1 skrll /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8423 1.1 skrll x >>= 32;
8424 1.1 skrll x >>= 32;
8425 1.1 skrll break;
8426 1.1 skrll #endif
8427 1.1 skrll default:
8428 1.9 christos abort ();
8429 1.1 skrll break;
8430 1.1 skrll }
8431 1.9 christos }
8432 1.9 christos }
8433 1.9 christos
8434 1.9 christos static bfd_vma
8435 1.9 christos get_value (bfd_vma size,
8436 1.9 christos unsigned long chunksz,
8437 1.9 christos bfd *input_bfd,
8438 1.9 christos bfd_byte *location)
8439 1.9 christos {
8440 1.9 christos int shift;
8441 1.9 christos bfd_vma x = 0;
8442 1.9 christos
8443 1.9 christos /* Sanity checks. */
8444 1.9 christos BFD_ASSERT (chunksz <= sizeof (x)
8445 1.9 christos && size >= chunksz
8446 1.9 christos && chunksz != 0
8447 1.9 christos && (size % chunksz) == 0
8448 1.9 christos && input_bfd != NULL
8449 1.9 christos && location != NULL);
8450 1.9 christos
8451 1.1 skrll if (chunksz == sizeof (x))
8452 1.1 skrll {
8453 1.1 skrll BFD_ASSERT (size == chunksz);
8454 1.1 skrll
8455 1.1 skrll /* Make sure that we do not perform an undefined shift operation.
8456 1.9 christos We know that size == chunksz so there will only be one iteration
8457 1.1 skrll of the loop below. */
8458 1.1 skrll shift = 0;
8459 1.9 christos }
8460 1.1 skrll else
8461 1.1 skrll shift = 8 * chunksz;
8462 1.9 christos
8463 1.1 skrll for (; size; size -= chunksz, location += chunksz)
8464 1.9 christos {
8465 1.1 skrll switch (chunksz)
8466 1.9 christos {
8467 1.9 christos case 1:
8468 1.9 christos x = (x << shift) | bfd_get_8 (input_bfd, location);
8469 1.9 christos break;
8470 1.1 skrll case 2:
8471 1.1 skrll x = (x << shift) | bfd_get_16 (input_bfd, location);
8472 1.1 skrll break;
8473 1.1 skrll case 4:
8474 1.1 skrll x = (x << shift) | bfd_get_32 (input_bfd, location);
8475 1.1 skrll break;
8476 1.1 skrll #ifdef BFD64
8477 1.1 skrll case 8:
8478 1.1 skrll x = (x << shift) | bfd_get_64 (input_bfd, location);
8479 1.1 skrll break;
8480 1.1 skrll #endif
8481 1.1 skrll default:
8482 1.1 skrll abort ();
8483 1.1 skrll }
8484 1.1 skrll }
8485 1.1 skrll return x;
8486 1.1 skrll }
8487 1.13.12.2 pgoyette
8488 1.13.12.2 pgoyette static void
8489 1.1 skrll decode_complex_addend (unsigned long *start, /* in bits */
8490 1.1 skrll unsigned long *oplen, /* in bits */
8491 1.1 skrll unsigned long *len, /* in bits */
8492 1.1 skrll unsigned long *wordsz, /* in bytes */
8493 1.1 skrll unsigned long *chunksz, /* in bytes */
8494 1.1 skrll unsigned long *lsb0_p,
8495 1.1 skrll unsigned long *signed_p,
8496 1.1 skrll unsigned long *trunc_p,
8497 1.1 skrll unsigned long encoded)
8498 1.1 skrll {
8499 1.1 skrll * start = encoded & 0x3F;
8500 1.1 skrll * len = (encoded >> 6) & 0x3F;
8501 1.1 skrll * oplen = (encoded >> 12) & 0x3F;
8502 1.1 skrll * wordsz = (encoded >> 18) & 0xF;
8503 1.1 skrll * chunksz = (encoded >> 22) & 0xF;
8504 1.1 skrll * lsb0_p = (encoded >> 27) & 1;
8505 1.1 skrll * signed_p = (encoded >> 28) & 1;
8506 1.1 skrll * trunc_p = (encoded >> 29) & 1;
8507 1.1 skrll }
8508 1.1 skrll
8509 1.1 skrll bfd_reloc_status_type
8510 1.1 skrll bfd_elf_perform_complex_relocation (bfd *input_bfd,
8511 1.1 skrll asection *input_section ATTRIBUTE_UNUSED,
8512 1.1 skrll bfd_byte *contents,
8513 1.1 skrll Elf_Internal_Rela *rel,
8514 1.1 skrll bfd_vma relocation)
8515 1.1 skrll {
8516 1.1 skrll bfd_vma shift, x, mask;
8517 1.1 skrll unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8518 1.1 skrll bfd_reloc_status_type r;
8519 1.1 skrll
8520 1.1 skrll /* Perform this reloc, since it is complex.
8521 1.1 skrll (this is not to say that it necessarily refers to a complex
8522 1.1 skrll symbol; merely that it is a self-describing CGEN based reloc.
8523 1.1 skrll i.e. the addend has the complete reloc information (bit start, end,
8524 1.1 skrll word size, etc) encoded within it.). */
8525 1.13 christos
8526 1.13 christos decode_complex_addend (&start, &oplen, &len, &wordsz,
8527 1.1 skrll &chunksz, &lsb0_p, &signed_p,
8528 1.1 skrll &trunc_p, rel->r_addend);
8529 1.1 skrll
8530 1.1 skrll mask = (((1L << (len - 1)) - 1) << 1) | 1;
8531 1.1 skrll
8532 1.1 skrll if (lsb0_p)
8533 1.1 skrll shift = (start + 1) - len;
8534 1.4 christos else
8535 1.4 christos shift = (8 * wordsz) - (start + len);
8536 1.1 skrll
8537 1.1 skrll x = get_value (wordsz, chunksz, input_bfd,
8538 1.1 skrll contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8539 1.1 skrll
8540 1.1 skrll #ifdef DEBUG
8541 1.1 skrll printf ("Doing complex reloc: "
8542 1.1 skrll "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8543 1.1 skrll "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8544 1.1 skrll " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8545 1.1 skrll lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8546 1.1 skrll oplen, (unsigned long) x, (unsigned long) mask,
8547 1.1 skrll (unsigned long) relocation);
8548 1.1 skrll #endif
8549 1.1 skrll
8550 1.1 skrll r = bfd_reloc_ok;
8551 1.1 skrll if (! trunc_p)
8552 1.1 skrll /* Now do an overflow check. */
8553 1.1 skrll r = bfd_check_overflow ((signed_p
8554 1.1 skrll ? complain_overflow_signed
8555 1.4 christos : complain_overflow_unsigned),
8556 1.4 christos len, 0, (8 * wordsz),
8557 1.1 skrll relocation);
8558 1.13 christos
8559 1.13 christos /* Do the deed. */
8560 1.1 skrll x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8561 1.1 skrll
8562 1.1 skrll #ifdef DEBUG
8563 1.9 christos printf (" relocation: %8.8lx\n"
8564 1.9 christos " shifted mask: %8.8lx\n"
8565 1.9 christos " shifted/masked reloc: %8.8lx\n"
8566 1.9 christos " result: %8.8lx\n",
8567 1.9 christos (unsigned long) relocation, (unsigned long) (mask << shift),
8568 1.9 christos (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8569 1.9 christos #endif
8570 1.9 christos put_value (wordsz, chunksz, input_bfd, x,
8571 1.9 christos contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8572 1.9 christos return r;
8573 1.9 christos }
8574 1.9 christos
8575 1.9 christos /* Functions to read r_offset from external (target order) reloc
8576 1.9 christos entry. Faster than bfd_getl32 et al, because we let the compiler
8577 1.9 christos know the value is aligned. */
8578 1.9 christos
8579 1.9 christos static bfd_vma
8580 1.9 christos ext32l_r_offset (const void *p)
8581 1.9 christos {
8582 1.9 christos union aligned32
8583 1.9 christos {
8584 1.9 christos uint32_t v;
8585 1.9 christos unsigned char c[4];
8586 1.9 christos };
8587 1.9 christos const union aligned32 *a
8588 1.9 christos = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8589 1.9 christos
8590 1.9 christos uint32_t aval = ( (uint32_t) a->c[0]
8591 1.9 christos | (uint32_t) a->c[1] << 8
8592 1.9 christos | (uint32_t) a->c[2] << 16
8593 1.9 christos | (uint32_t) a->c[3] << 24);
8594 1.9 christos return aval;
8595 1.9 christos }
8596 1.9 christos
8597 1.9 christos static bfd_vma
8598 1.9 christos ext32b_r_offset (const void *p)
8599 1.9 christos {
8600 1.9 christos union aligned32
8601 1.9 christos {
8602 1.9 christos uint32_t v;
8603 1.9 christos unsigned char c[4];
8604 1.9 christos };
8605 1.9 christos const union aligned32 *a
8606 1.9 christos = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8607 1.9 christos
8608 1.9 christos uint32_t aval = ( (uint32_t) a->c[0] << 24
8609 1.9 christos | (uint32_t) a->c[1] << 16
8610 1.9 christos | (uint32_t) a->c[2] << 8
8611 1.9 christos | (uint32_t) a->c[3]);
8612 1.9 christos return aval;
8613 1.9 christos }
8614 1.9 christos
8615 1.9 christos #ifdef BFD_HOST_64_BIT
8616 1.9 christos static bfd_vma
8617 1.9 christos ext64l_r_offset (const void *p)
8618 1.9 christos {
8619 1.9 christos union aligned64
8620 1.9 christos {
8621 1.9 christos uint64_t v;
8622 1.9 christos unsigned char c[8];
8623 1.9 christos };
8624 1.9 christos const union aligned64 *a
8625 1.9 christos = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8626 1.9 christos
8627 1.9 christos uint64_t aval = ( (uint64_t) a->c[0]
8628 1.9 christos | (uint64_t) a->c[1] << 8
8629 1.9 christos | (uint64_t) a->c[2] << 16
8630 1.9 christos | (uint64_t) a->c[3] << 24
8631 1.9 christos | (uint64_t) a->c[4] << 32
8632 1.9 christos | (uint64_t) a->c[5] << 40
8633 1.9 christos | (uint64_t) a->c[6] << 48
8634 1.9 christos | (uint64_t) a->c[7] << 56);
8635 1.9 christos return aval;
8636 1.9 christos }
8637 1.9 christos
8638 1.9 christos static bfd_vma
8639 1.9 christos ext64b_r_offset (const void *p)
8640 1.9 christos {
8641 1.9 christos union aligned64
8642 1.9 christos {
8643 1.9 christos uint64_t v;
8644 1.9 christos unsigned char c[8];
8645 1.9 christos };
8646 1.9 christos const union aligned64 *a
8647 1.9 christos = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8648 1.9 christos
8649 1.1 skrll uint64_t aval = ( (uint64_t) a->c[0] << 56
8650 1.1 skrll | (uint64_t) a->c[1] << 48
8651 1.4 christos | (uint64_t) a->c[2] << 40
8652 1.4 christos | (uint64_t) a->c[3] << 32
8653 1.1 skrll | (uint64_t) a->c[4] << 24
8654 1.9 christos | (uint64_t) a->c[5] << 16
8655 1.1 skrll | (uint64_t) a->c[6] << 8
8656 1.13.12.2 pgoyette | (uint64_t) a->c[7]);
8657 1.9 christos return aval;
8658 1.13.12.2 pgoyette }
8659 1.13.12.2 pgoyette #endif
8660 1.1 skrll
8661 1.1 skrll /* When performing a relocatable link, the input relocations are
8662 1.1 skrll preserved. But, if they reference global symbols, the indices
8663 1.1 skrll referenced must be updated. Update all the relocations found in
8664 1.1 skrll RELDATA. */
8665 1.1 skrll
8666 1.1 skrll static bfd_boolean
8667 1.1 skrll elf_link_adjust_relocs (bfd *abfd,
8668 1.4 christos asection *sec,
8669 1.4 christos struct bfd_elf_section_reloc_data *reldata,
8670 1.1 skrll bfd_boolean sort,
8671 1.4 christos struct bfd_link_info *info)
8672 1.1 skrll {
8673 1.1 skrll unsigned int i;
8674 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8675 1.1 skrll bfd_byte *erela;
8676 1.4 christos void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8677 1.1 skrll void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8678 1.1 skrll bfd_vma r_type_mask;
8679 1.1 skrll int r_sym_shift;
8680 1.1 skrll unsigned int count = reldata->count;
8681 1.1 skrll struct elf_link_hash_entry **rel_hash = reldata->hashes;
8682 1.1 skrll
8683 1.1 skrll if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8684 1.1 skrll {
8685 1.1 skrll swap_in = bed->s->swap_reloc_in;
8686 1.1 skrll swap_out = bed->s->swap_reloc_out;
8687 1.1 skrll }
8688 1.1 skrll else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8689 1.1 skrll {
8690 1.1 skrll swap_in = bed->s->swap_reloca_in;
8691 1.1 skrll swap_out = bed->s->swap_reloca_out;
8692 1.1 skrll }
8693 1.1 skrll else
8694 1.1 skrll abort ();
8695 1.1 skrll
8696 1.1 skrll if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8697 1.1 skrll abort ();
8698 1.4 christos
8699 1.4 christos if (bed->s->arch_size == 32)
8700 1.1 skrll {
8701 1.1 skrll r_type_mask = 0xff;
8702 1.1 skrll r_sym_shift = 8;
8703 1.1 skrll }
8704 1.1 skrll else
8705 1.1 skrll {
8706 1.1 skrll r_type_mask = 0xffffffff;
8707 1.13.12.2 pgoyette r_sym_shift = 32;
8708 1.13.12.2 pgoyette }
8709 1.13.12.2 pgoyette
8710 1.13.12.2 pgoyette erela = reldata->hdr->contents;
8711 1.13.12.2 pgoyette for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8712 1.13.12.2 pgoyette {
8713 1.13.12.2 pgoyette Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8714 1.13.12.2 pgoyette unsigned int j;
8715 1.13.12.2 pgoyette
8716 1.13.12.2 pgoyette if (*rel_hash == NULL)
8717 1.13.12.2 pgoyette continue;
8718 1.13.12.2 pgoyette
8719 1.13.12.2 pgoyette if ((*rel_hash)->indx == -2
8720 1.1 skrll && info->gc_sections
8721 1.1 skrll && ! info->gc_keep_exported)
8722 1.1 skrll {
8723 1.1 skrll /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8724 1.1 skrll _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8725 1.1 skrll abfd, sec,
8726 1.1 skrll (*rel_hash)->root.root.string);
8727 1.1 skrll _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8728 1.9 christos abfd, sec);
8729 1.13.12.2 pgoyette bfd_set_error (bfd_error_invalid_operation);
8730 1.13.12.2 pgoyette return FALSE;
8731 1.13.12.2 pgoyette }
8732 1.9 christos BFD_ASSERT ((*rel_hash)->indx >= 0);
8733 1.9 christos
8734 1.9 christos (*swap_in) (abfd, erela, irela);
8735 1.9 christos for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8736 1.9 christos irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8737 1.9 christos | (irela[j].r_info & r_type_mask));
8738 1.9 christos (*swap_out) (abfd, irela, erela);
8739 1.9 christos }
8740 1.9 christos
8741 1.9 christos if (bed->elf_backend_update_relocs)
8742 1.9 christos (*bed->elf_backend_update_relocs) (sec, reldata);
8743 1.9 christos
8744 1.9 christos if (sort && count != 0)
8745 1.9 christos {
8746 1.9 christos bfd_vma (*ext_r_off) (const void *);
8747 1.9 christos bfd_vma r_off;
8748 1.9 christos size_t elt_size;
8749 1.9 christos bfd_byte *base, *end, *p, *loc;
8750 1.9 christos bfd_byte *buf = NULL;
8751 1.9 christos
8752 1.9 christos if (bed->s->arch_size == 32)
8753 1.9 christos {
8754 1.9 christos if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8755 1.9 christos ext_r_off = ext32l_r_offset;
8756 1.9 christos else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8757 1.9 christos ext_r_off = ext32b_r_offset;
8758 1.9 christos else
8759 1.9 christos abort ();
8760 1.9 christos }
8761 1.9 christos else
8762 1.9 christos {
8763 1.9 christos #ifdef BFD_HOST_64_BIT
8764 1.9 christos if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8765 1.9 christos ext_r_off = ext64l_r_offset;
8766 1.9 christos else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8767 1.9 christos ext_r_off = ext64b_r_offset;
8768 1.9 christos else
8769 1.9 christos #endif
8770 1.9 christos abort ();
8771 1.9 christos }
8772 1.9 christos
8773 1.9 christos /* Must use a stable sort here. A modified insertion sort,
8774 1.9 christos since the relocs are mostly sorted already. */
8775 1.9 christos elt_size = reldata->hdr->sh_entsize;
8776 1.9 christos base = reldata->hdr->contents;
8777 1.9 christos end = base + count * elt_size;
8778 1.9 christos if (elt_size > sizeof (Elf64_External_Rela))
8779 1.9 christos abort ();
8780 1.9 christos
8781 1.9 christos /* Ensure the first element is lowest. This acts as a sentinel,
8782 1.9 christos speeding the main loop below. */
8783 1.9 christos r_off = (*ext_r_off) (base);
8784 1.9 christos for (p = loc = base; (p += elt_size) < end; )
8785 1.9 christos {
8786 1.9 christos bfd_vma r_off2 = (*ext_r_off) (p);
8787 1.9 christos if (r_off > r_off2)
8788 1.9 christos {
8789 1.9 christos r_off = r_off2;
8790 1.9 christos loc = p;
8791 1.9 christos }
8792 1.9 christos }
8793 1.9 christos if (loc != base)
8794 1.9 christos {
8795 1.9 christos /* Don't just swap *base and *loc as that changes the order
8796 1.9 christos of the original base[0] and base[1] if they happen to
8797 1.9 christos have the same r_offset. */
8798 1.9 christos bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8799 1.9 christos memcpy (onebuf, loc, elt_size);
8800 1.9 christos memmove (base + elt_size, base, loc - base);
8801 1.9 christos memcpy (base, onebuf, elt_size);
8802 1.9 christos }
8803 1.9 christos
8804 1.9 christos for (p = base + elt_size; (p += elt_size) < end; )
8805 1.9 christos {
8806 1.9 christos /* base to p is sorted, *p is next to insert. */
8807 1.9 christos r_off = (*ext_r_off) (p);
8808 1.9 christos /* Search the sorted region for location to insert. */
8809 1.9 christos loc = p - elt_size;
8810 1.9 christos while (r_off < (*ext_r_off) (loc))
8811 1.9 christos loc -= elt_size;
8812 1.9 christos loc += elt_size;
8813 1.9 christos if (loc != p)
8814 1.9 christos {
8815 1.9 christos /* Chances are there is a run of relocs to insert here,
8816 1.9 christos from one of more input files. Files are not always
8817 1.9 christos linked in order due to the way elf_link_input_bfd is
8818 1.9 christos called. See pr17666. */
8819 1.9 christos size_t sortlen = p - loc;
8820 1.9 christos bfd_vma r_off2 = (*ext_r_off) (loc);
8821 1.9 christos size_t runlen = elt_size;
8822 1.9 christos size_t buf_size = 96 * 1024;
8823 1.9 christos while (p + runlen < end
8824 1.9 christos && (sortlen <= buf_size
8825 1.9 christos || runlen + elt_size <= buf_size)
8826 1.9 christos && r_off2 > (*ext_r_off) (p + runlen))
8827 1.9 christos runlen += elt_size;
8828 1.9 christos if (buf == NULL)
8829 1.9 christos {
8830 1.9 christos buf = bfd_malloc (buf_size);
8831 1.9 christos if (buf == NULL)
8832 1.9 christos return FALSE;
8833 1.9 christos }
8834 1.9 christos if (runlen < sortlen)
8835 1.9 christos {
8836 1.9 christos memcpy (buf, p, runlen);
8837 1.9 christos memmove (loc + runlen, loc, sortlen);
8838 1.9 christos memcpy (loc, buf, runlen);
8839 1.9 christos }
8840 1.9 christos else
8841 1.9 christos {
8842 1.9 christos memcpy (buf, loc, sortlen);
8843 1.1 skrll memmove (loc, p, runlen);
8844 1.1 skrll memcpy (loc + runlen, buf, sortlen);
8845 1.1 skrll }
8846 1.1 skrll p += runlen - elt_size;
8847 1.1 skrll }
8848 1.1 skrll }
8849 1.1 skrll /* Hashes are no longer valid. */
8850 1.1 skrll free (reldata->hashes);
8851 1.1 skrll reldata->hashes = NULL;
8852 1.1 skrll free (buf);
8853 1.1 skrll }
8854 1.1 skrll return TRUE;
8855 1.1 skrll }
8856 1.1 skrll
8857 1.1 skrll struct elf_link_sort_rela
8858 1.1 skrll {
8859 1.4 christos union {
8860 1.4 christos bfd_vma offset;
8861 1.1 skrll bfd_vma sym_mask;
8862 1.1 skrll } u;
8863 1.1 skrll enum elf_reloc_type_class type;
8864 1.1 skrll /* We use this as an array of size int_rels_per_ext_rel. */
8865 1.1 skrll Elf_Internal_Rela rela[1];
8866 1.1 skrll };
8867 1.1 skrll
8868 1.1 skrll static int
8869 1.1 skrll elf_link_sort_cmp1 (const void *A, const void *B)
8870 1.1 skrll {
8871 1.1 skrll const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8872 1.1 skrll const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8873 1.1 skrll int relativea, relativeb;
8874 1.1 skrll
8875 1.1 skrll relativea = a->type == reloc_class_relative;
8876 1.1 skrll relativeb = b->type == reloc_class_relative;
8877 1.1 skrll
8878 1.1 skrll if (relativea < relativeb)
8879 1.1 skrll return 1;
8880 1.1 skrll if (relativea > relativeb)
8881 1.1 skrll return -1;
8882 1.1 skrll if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8883 1.1 skrll return -1;
8884 1.4 christos if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8885 1.4 christos return 1;
8886 1.1 skrll if (a->rela->r_offset < b->rela->r_offset)
8887 1.9 christos return -1;
8888 1.9 christos if (a->rela->r_offset > b->rela->r_offset)
8889 1.9 christos return 1;
8890 1.9 christos return 0;
8891 1.1 skrll }
8892 1.1 skrll
8893 1.1 skrll static int
8894 1.1 skrll elf_link_sort_cmp2 (const void *A, const void *B)
8895 1.1 skrll {
8896 1.1 skrll const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8897 1.1 skrll const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8898 1.1 skrll
8899 1.1 skrll if (a->type < b->type)
8900 1.1 skrll return -1;
8901 1.1 skrll if (a->type > b->type)
8902 1.1 skrll return 1;
8903 1.1 skrll if (a->u.offset < b->u.offset)
8904 1.1 skrll return -1;
8905 1.1 skrll if (a->u.offset > b->u.offset)
8906 1.1 skrll return 1;
8907 1.1 skrll if (a->rela->r_offset < b->rela->r_offset)
8908 1.1 skrll return -1;
8909 1.1 skrll if (a->rela->r_offset > b->rela->r_offset)
8910 1.1 skrll return 1;
8911 1.1 skrll return 0;
8912 1.1 skrll }
8913 1.1 skrll
8914 1.13 christos static size_t
8915 1.1 skrll elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8916 1.1 skrll {
8917 1.1 skrll asection *dynamic_relocs;
8918 1.1 skrll asection *rela_dyn;
8919 1.1 skrll asection *rel_dyn;
8920 1.1 skrll bfd_size_type count, size;
8921 1.1 skrll size_t i, ret, sort_elt, ext_size;
8922 1.1 skrll bfd_byte *sort, *s_non_relative, *p;
8923 1.1 skrll struct elf_link_sort_rela *sq;
8924 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8925 1.1 skrll int i2e = bed->s->int_rels_per_ext_rel;
8926 1.1 skrll unsigned int opb = bfd_octets_per_byte (abfd);
8927 1.1 skrll void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8928 1.1 skrll void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8929 1.1 skrll struct bfd_link_order *lo;
8930 1.13 christos bfd_vma r_sym_mask;
8931 1.1 skrll bfd_boolean use_rela;
8932 1.1 skrll
8933 1.1 skrll /* Find a dynamic reloc section. */
8934 1.1 skrll rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8935 1.1 skrll rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8936 1.1 skrll if (rela_dyn != NULL && rela_dyn->size > 0
8937 1.1 skrll && rel_dyn != NULL && rel_dyn->size > 0)
8938 1.1 skrll {
8939 1.1 skrll bfd_boolean use_rela_initialised = FALSE;
8940 1.1 skrll
8941 1.1 skrll /* This is just here to stop gcc from complaining.
8942 1.1 skrll Its initialization checking code is not perfect. */
8943 1.1 skrll use_rela = TRUE;
8944 1.1 skrll
8945 1.1 skrll /* Both sections are present. Examine the sizes
8946 1.1 skrll of the indirect sections to help us choose. */
8947 1.1 skrll for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8948 1.1 skrll if (lo->type == bfd_indirect_link_order)
8949 1.13.12.2 pgoyette {
8950 1.1 skrll asection *o = lo->u.indirect.section;
8951 1.13 christos
8952 1.13 christos if ((o->size % bed->s->sizeof_rela) == 0)
8953 1.13 christos {
8954 1.1 skrll if ((o->size % bed->s->sizeof_rel) == 0)
8955 1.1 skrll /* Section size is divisible by both rel and rela sizes.
8956 1.1 skrll It is of no help to us. */
8957 1.1 skrll ;
8958 1.1 skrll else
8959 1.1 skrll {
8960 1.1 skrll /* Section size is only divisible by rela. */
8961 1.1 skrll if (use_rela_initialised && !use_rela)
8962 1.1 skrll {
8963 1.1 skrll _bfd_error_handler (_("%B: Unable to sort relocs - "
8964 1.1 skrll "they are in more than one size"),
8965 1.1 skrll abfd);
8966 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
8967 1.13.12.2 pgoyette return 0;
8968 1.1 skrll }
8969 1.13 christos else
8970 1.13 christos {
8971 1.13 christos use_rela = TRUE;
8972 1.1 skrll use_rela_initialised = TRUE;
8973 1.1 skrll }
8974 1.1 skrll }
8975 1.1 skrll }
8976 1.1 skrll else if ((o->size % bed->s->sizeof_rel) == 0)
8977 1.1 skrll {
8978 1.1 skrll /* Section size is only divisible by rel. */
8979 1.1 skrll if (use_rela_initialised && use_rela)
8980 1.1 skrll {
8981 1.1 skrll _bfd_error_handler (_("%B: Unable to sort relocs - "
8982 1.1 skrll "they are in more than one size"),
8983 1.13 christos abfd);
8984 1.13 christos bfd_set_error (bfd_error_invalid_operation);
8985 1.13 christos return 0;
8986 1.13 christos }
8987 1.1 skrll else
8988 1.1 skrll {
8989 1.1 skrll use_rela = FALSE;
8990 1.1 skrll use_rela_initialised = TRUE;
8991 1.1 skrll }
8992 1.1 skrll }
8993 1.1 skrll else
8994 1.1 skrll {
8995 1.1 skrll /* The section size is not divisible by either -
8996 1.1 skrll something is wrong. */
8997 1.1 skrll _bfd_error_handler (_("%B: Unable to sort relocs - "
8998 1.1 skrll "they are of an unknown size"), abfd);
8999 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
9000 1.1 skrll return 0;
9001 1.1 skrll }
9002 1.1 skrll }
9003 1.1 skrll
9004 1.1 skrll for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9005 1.1 skrll if (lo->type == bfd_indirect_link_order)
9006 1.13.12.2 pgoyette {
9007 1.1 skrll asection *o = lo->u.indirect.section;
9008 1.13 christos
9009 1.13 christos if ((o->size % bed->s->sizeof_rela) == 0)
9010 1.13 christos {
9011 1.1 skrll if ((o->size % bed->s->sizeof_rel) == 0)
9012 1.1 skrll /* Section size is divisible by both rel and rela sizes.
9013 1.1 skrll It is of no help to us. */
9014 1.1 skrll ;
9015 1.1 skrll else
9016 1.1 skrll {
9017 1.1 skrll /* Section size is only divisible by rela. */
9018 1.1 skrll if (use_rela_initialised && !use_rela)
9019 1.1 skrll {
9020 1.1 skrll _bfd_error_handler (_("%B: Unable to sort relocs - "
9021 1.1 skrll "they are in more than one size"),
9022 1.1 skrll abfd);
9023 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
9024 1.13.12.2 pgoyette return 0;
9025 1.1 skrll }
9026 1.13 christos else
9027 1.13 christos {
9028 1.13 christos use_rela = TRUE;
9029 1.1 skrll use_rela_initialised = TRUE;
9030 1.1 skrll }
9031 1.1 skrll }
9032 1.1 skrll }
9033 1.1 skrll else if ((o->size % bed->s->sizeof_rel) == 0)
9034 1.1 skrll {
9035 1.1 skrll /* Section size is only divisible by rel. */
9036 1.1 skrll if (use_rela_initialised && use_rela)
9037 1.1 skrll {
9038 1.1 skrll _bfd_error_handler (_("%B: Unable to sort relocs - "
9039 1.1 skrll "they are in more than one size"),
9040 1.13 christos abfd);
9041 1.13 christos bfd_set_error (bfd_error_invalid_operation);
9042 1.13 christos return 0;
9043 1.13 christos }
9044 1.1 skrll else
9045 1.1 skrll {
9046 1.1 skrll use_rela = FALSE;
9047 1.1 skrll use_rela_initialised = TRUE;
9048 1.1 skrll }
9049 1.1 skrll }
9050 1.1 skrll else
9051 1.1 skrll {
9052 1.1 skrll /* The section size is not divisible by either -
9053 1.1 skrll something is wrong. */
9054 1.1 skrll _bfd_error_handler (_("%B: Unable to sort relocs - "
9055 1.1 skrll "they are of an unknown size"), abfd);
9056 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
9057 1.1 skrll return 0;
9058 1.1 skrll }
9059 1.1 skrll }
9060 1.1 skrll
9061 1.1 skrll if (! use_rela_initialised)
9062 1.1 skrll /* Make a guess. */
9063 1.1 skrll use_rela = TRUE;
9064 1.1 skrll }
9065 1.1 skrll else if (rela_dyn != NULL && rela_dyn->size > 0)
9066 1.1 skrll use_rela = TRUE;
9067 1.1 skrll else if (rel_dyn != NULL && rel_dyn->size > 0)
9068 1.1 skrll use_rela = FALSE;
9069 1.1 skrll else
9070 1.1 skrll return 0;
9071 1.1 skrll
9072 1.1 skrll if (use_rela)
9073 1.1 skrll {
9074 1.1 skrll dynamic_relocs = rela_dyn;
9075 1.1 skrll ext_size = bed->s->sizeof_rela;
9076 1.1 skrll swap_in = bed->s->swap_reloca_in;
9077 1.1 skrll swap_out = bed->s->swap_reloca_out;
9078 1.1 skrll }
9079 1.1 skrll else
9080 1.1 skrll {
9081 1.1 skrll dynamic_relocs = rel_dyn;
9082 1.1 skrll ext_size = bed->s->sizeof_rel;
9083 1.1 skrll swap_in = bed->s->swap_reloc_in;
9084 1.1 skrll swap_out = bed->s->swap_reloc_out;
9085 1.1 skrll }
9086 1.1 skrll
9087 1.4 christos size = 0;
9088 1.4 christos for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9089 1.4 christos if (lo->type == bfd_indirect_link_order)
9090 1.1 skrll size += lo->u.indirect.section->size;
9091 1.1 skrll
9092 1.1 skrll if (size != dynamic_relocs->size)
9093 1.1 skrll return 0;
9094 1.1 skrll
9095 1.1 skrll sort_elt = (sizeof (struct elf_link_sort_rela)
9096 1.1 skrll + (i2e - 1) * sizeof (Elf_Internal_Rela));
9097 1.1 skrll
9098 1.1 skrll count = dynamic_relocs->size / ext_size;
9099 1.1 skrll if (count == 0)
9100 1.1 skrll return 0;
9101 1.1 skrll sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9102 1.1 skrll
9103 1.1 skrll if (sort == NULL)
9104 1.1 skrll {
9105 1.1 skrll (*info->callbacks->warning)
9106 1.1 skrll (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9107 1.1 skrll return 0;
9108 1.1 skrll }
9109 1.1 skrll
9110 1.1 skrll if (bed->s->arch_size == 32)
9111 1.1 skrll r_sym_mask = ~(bfd_vma) 0xff;
9112 1.1 skrll else
9113 1.1 skrll r_sym_mask = ~(bfd_vma) 0xffffffff;
9114 1.1 skrll
9115 1.1 skrll for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9116 1.1 skrll if (lo->type == bfd_indirect_link_order)
9117 1.1 skrll {
9118 1.1 skrll bfd_byte *erel, *erelend;
9119 1.13 christos asection *o = lo->u.indirect.section;
9120 1.1 skrll
9121 1.1 skrll if (o->contents == NULL && o->size != 0)
9122 1.1 skrll {
9123 1.1 skrll /* This is a reloc section that is being handled as a normal
9124 1.1 skrll section. See bfd_section_from_shdr. We can't combine
9125 1.1 skrll relocs in this case. */
9126 1.9 christos free (sort);
9127 1.1 skrll return 0;
9128 1.1 skrll }
9129 1.1 skrll erel = o->contents;
9130 1.1 skrll erelend = o->contents + o->size;
9131 1.1 skrll p = sort + o->output_offset * opb / ext_size * sort_elt;
9132 1.1 skrll
9133 1.1 skrll while (erel < erelend)
9134 1.1 skrll {
9135 1.1 skrll struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9136 1.1 skrll
9137 1.1 skrll (*swap_in) (abfd, erel, s->rela);
9138 1.1 skrll s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9139 1.1 skrll s->u.sym_mask = r_sym_mask;
9140 1.1 skrll p += sort_elt;
9141 1.1 skrll erel += ext_size;
9142 1.1 skrll }
9143 1.1 skrll }
9144 1.1 skrll
9145 1.1 skrll qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9146 1.1 skrll
9147 1.1 skrll for (i = 0, p = sort; i < count; i++, p += sort_elt)
9148 1.1 skrll {
9149 1.1 skrll struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9150 1.1 skrll if (s->type != reloc_class_relative)
9151 1.1 skrll break;
9152 1.1 skrll }
9153 1.1 skrll ret = i;
9154 1.1 skrll s_non_relative = p;
9155 1.13 christos
9156 1.13 christos sq = (struct elf_link_sort_rela *) s_non_relative;
9157 1.13 christos for (; i < count; i++, p += sort_elt)
9158 1.13 christos {
9159 1.13 christos struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9160 1.13 christos if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9161 1.13 christos sq = sp;
9162 1.13 christos sp->u.offset = sq->rela->r_offset;
9163 1.13 christos }
9164 1.13 christos
9165 1.13 christos qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9166 1.13 christos
9167 1.13 christos struct elf_link_hash_table *htab = elf_hash_table (info);
9168 1.13 christos if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9169 1.13 christos {
9170 1.13 christos /* We have plt relocs in .rela.dyn. */
9171 1.13 christos sq = (struct elf_link_sort_rela *) sort;
9172 1.13 christos for (i = 0; i < count; i++)
9173 1.13 christos if (sq[count - i - 1].type != reloc_class_plt)
9174 1.13 christos break;
9175 1.13 christos if (i != 0 && htab->srelplt->size == i * ext_size)
9176 1.13 christos {
9177 1.13 christos struct bfd_link_order **plo;
9178 1.13 christos /* Put srelplt link_order last. This is so the output_offset
9179 1.13 christos set in the next loop is correct for DT_JMPREL. */
9180 1.13 christos for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9181 1.13 christos if ((*plo)->type == bfd_indirect_link_order
9182 1.13 christos && (*plo)->u.indirect.section == htab->srelplt)
9183 1.13 christos {
9184 1.1 skrll lo = *plo;
9185 1.1 skrll *plo = lo->next;
9186 1.1 skrll }
9187 1.1 skrll else
9188 1.1 skrll plo = &(*plo)->next;
9189 1.1 skrll *plo = lo;
9190 1.1 skrll lo->next = NULL;
9191 1.1 skrll dynamic_relocs->map_tail.link_order = lo;
9192 1.13 christos }
9193 1.1 skrll }
9194 1.1 skrll
9195 1.1 skrll p = sort;
9196 1.1 skrll for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9197 1.1 skrll if (lo->type == bfd_indirect_link_order)
9198 1.1 skrll {
9199 1.1 skrll bfd_byte *erel, *erelend;
9200 1.1 skrll asection *o = lo->u.indirect.section;
9201 1.1 skrll
9202 1.1 skrll erel = o->contents;
9203 1.1 skrll erelend = o->contents + o->size;
9204 1.1 skrll o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9205 1.1 skrll while (erel < erelend)
9206 1.1 skrll {
9207 1.9 christos struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9208 1.1 skrll (*swap_out) (abfd, s->rela, erel);
9209 1.4 christos p += sort_elt;
9210 1.9 christos erel += ext_size;
9211 1.9 christos }
9212 1.9 christos }
9213 1.9 christos
9214 1.9 christos free (sort);
9215 1.1 skrll *psec = dynamic_relocs;
9216 1.4 christos return ret;
9217 1.1 skrll }
9218 1.1 skrll
9219 1.9 christos /* Add a symbol to the output symbol string table. */
9220 1.1 skrll
9221 1.9 christos static int
9222 1.9 christos elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9223 1.9 christos const char *name,
9224 1.1 skrll Elf_Internal_Sym *elfsym,
9225 1.7 christos asection *input_sec,
9226 1.1 skrll struct elf_link_hash_entry *h)
9227 1.1 skrll {
9228 1.1 skrll int (*output_symbol_hook)
9229 1.7 christos (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9230 1.4 christos struct elf_link_hash_entry *);
9231 1.4 christos struct elf_link_hash_table *hash_table;
9232 1.1 skrll const struct elf_backend_data *bed;
9233 1.1 skrll bfd_size_type strtabsize;
9234 1.9 christos
9235 1.9 christos BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9236 1.9 christos
9237 1.9 christos bed = get_elf_backend_data (flinfo->output_bfd);
9238 1.1 skrll output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9239 1.1 skrll if (output_symbol_hook != NULL)
9240 1.9 christos {
9241 1.9 christos int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9242 1.9 christos if (ret != 1)
9243 1.9 christos return ret;
9244 1.9 christos }
9245 1.1 skrll
9246 1.4 christos if (name == NULL
9247 1.1 skrll || *name == '\0'
9248 1.1 skrll || (input_sec->flags & SEC_EXCLUDE))
9249 1.9 christos elfsym->st_name = (unsigned long) -1;
9250 1.9 christos else
9251 1.9 christos {
9252 1.9 christos /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9253 1.9 christos to get the final offset for st_name. */
9254 1.9 christos elfsym->st_name
9255 1.9 christos = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9256 1.9 christos name, FALSE);
9257 1.9 christos if (elfsym->st_name == (unsigned long) -1)
9258 1.9 christos return 0;
9259 1.9 christos }
9260 1.4 christos
9261 1.1 skrll hash_table = elf_hash_table (flinfo->info);
9262 1.9 christos strtabsize = hash_table->strtabsize;
9263 1.9 christos if (strtabsize <= hash_table->strtabcount)
9264 1.9 christos {
9265 1.9 christos strtabsize += strtabsize;
9266 1.9 christos hash_table->strtabsize = strtabsize;
9267 1.9 christos strtabsize *= sizeof (*hash_table->strtab);
9268 1.9 christos hash_table->strtab
9269 1.9 christos = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9270 1.9 christos strtabsize);
9271 1.9 christos if (hash_table->strtab == NULL)
9272 1.9 christos return 0;
9273 1.1 skrll }
9274 1.9 christos hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9275 1.9 christos hash_table->strtab[hash_table->strtabcount].dest_index
9276 1.9 christos = hash_table->strtabcount;
9277 1.9 christos hash_table->strtab[hash_table->strtabcount].destshndx_index
9278 1.9 christos = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9279 1.9 christos
9280 1.9 christos bfd_get_symcount (flinfo->output_bfd) += 1;
9281 1.13 christos hash_table->strtabcount += 1;
9282 1.13 christos
9283 1.9 christos return 1;
9284 1.9 christos }
9285 1.9 christos
9286 1.9 christos /* Swap symbols out to the symbol table and flush the output symbols to
9287 1.9 christos the file. */
9288 1.9 christos
9289 1.9 christos static bfd_boolean
9290 1.9 christos elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9291 1.9 christos {
9292 1.9 christos struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9293 1.9 christos bfd_size_type amt;
9294 1.9 christos size_t i;
9295 1.9 christos const struct elf_backend_data *bed;
9296 1.9 christos bfd_byte *symbuf;
9297 1.9 christos Elf_Internal_Shdr *hdr;
9298 1.9 christos file_ptr pos;
9299 1.9 christos bfd_boolean ret;
9300 1.9 christos
9301 1.9 christos if (!hash_table->strtabcount)
9302 1.1 skrll return TRUE;
9303 1.13 christos
9304 1.13 christos BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9305 1.9 christos
9306 1.9 christos bed = get_elf_backend_data (flinfo->output_bfd);
9307 1.1 skrll
9308 1.9 christos amt = bed->s->sizeof_sym * hash_table->strtabcount;
9309 1.9 christos symbuf = (bfd_byte *) bfd_malloc (amt);
9310 1.9 christos if (symbuf == NULL)
9311 1.9 christos return FALSE;
9312 1.1 skrll
9313 1.9 christos if (flinfo->symshndxbuf)
9314 1.9 christos {
9315 1.9 christos amt = sizeof (Elf_External_Sym_Shndx);
9316 1.9 christos amt *= bfd_get_symcount (flinfo->output_bfd);
9317 1.9 christos flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9318 1.9 christos if (flinfo->symshndxbuf == NULL)
9319 1.9 christos {
9320 1.9 christos free (symbuf);
9321 1.9 christos return FALSE;
9322 1.9 christos }
9323 1.9 christos }
9324 1.9 christos
9325 1.9 christos for (i = 0; i < hash_table->strtabcount; i++)
9326 1.9 christos {
9327 1.9 christos struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9328 1.9 christos if (elfsym->sym.st_name == (unsigned long) -1)
9329 1.9 christos elfsym->sym.st_name = 0;
9330 1.9 christos else
9331 1.9 christos elfsym->sym.st_name
9332 1.9 christos = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9333 1.9 christos elfsym->sym.st_name);
9334 1.9 christos bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9335 1.9 christos ((bfd_byte *) symbuf
9336 1.9 christos + (elfsym->dest_index
9337 1.9 christos * bed->s->sizeof_sym)),
9338 1.1 skrll (flinfo->symshndxbuf
9339 1.9 christos + elfsym->destshndx_index));
9340 1.9 christos }
9341 1.1 skrll
9342 1.9 christos hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9343 1.9 christos pos = hdr->sh_offset + hdr->sh_size;
9344 1.9 christos amt = hash_table->strtabcount * bed->s->sizeof_sym;
9345 1.9 christos if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9346 1.1 skrll && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9347 1.9 christos {
9348 1.1 skrll hdr->sh_size += amt;
9349 1.1 skrll ret = TRUE;
9350 1.1 skrll }
9351 1.1 skrll else
9352 1.1 skrll ret = FALSE;
9353 1.1 skrll
9354 1.1 skrll free (symbuf);
9355 1.1 skrll
9356 1.1 skrll free (hash_table->strtab);
9357 1.1 skrll hash_table->strtab = NULL;
9358 1.1 skrll
9359 1.1 skrll return ret;
9360 1.13.12.2 pgoyette }
9361 1.13.12.2 pgoyette
9362 1.1 skrll /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9363 1.1 skrll
9364 1.1 skrll static bfd_boolean
9365 1.1 skrll check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9366 1.1 skrll {
9367 1.1 skrll if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9368 1.1 skrll && sym->st_shndx < SHN_LORESERVE)
9369 1.1 skrll {
9370 1.1 skrll /* The gABI doesn't support dynamic symbols in output sections
9371 1.1 skrll beyond 64k. */
9372 1.1 skrll _bfd_error_handler
9373 1.1 skrll /* xgettext:c-format */
9374 1.1 skrll (_("%B: Too many sections: %d (>= %d)"),
9375 1.1 skrll abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9376 1.1 skrll bfd_set_error (bfd_error_nonrepresentable_section);
9377 1.1 skrll return FALSE;
9378 1.1 skrll }
9379 1.1 skrll return TRUE;
9380 1.1 skrll }
9381 1.1 skrll
9382 1.1 skrll /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9383 1.1 skrll allowing an unsatisfied unversioned symbol in the DSO to match a
9384 1.1 skrll versioned symbol that would normally require an explicit version.
9385 1.1 skrll We also handle the case that a DSO references a hidden symbol
9386 1.1 skrll which may be satisfied by a versioned symbol in another DSO. */
9387 1.7 christos
9388 1.7 christos static bfd_boolean
9389 1.7 christos elf_link_check_versioned_symbol (struct bfd_link_info *info,
9390 1.7 christos const struct elf_backend_data *bed,
9391 1.1 skrll struct elf_link_hash_entry *h)
9392 1.1 skrll {
9393 1.1 skrll bfd *abfd;
9394 1.1 skrll struct elf_link_loaded_list *loaded;
9395 1.1 skrll
9396 1.1 skrll if (!is_elf_hash_table (info->hash))
9397 1.1 skrll return FALSE;
9398 1.1 skrll
9399 1.1 skrll /* Check indirect symbol. */
9400 1.13 christos while (h->root.type == bfd_link_hash_indirect)
9401 1.13 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
9402 1.1 skrll
9403 1.1 skrll switch (h->root.type)
9404 1.1 skrll {
9405 1.1 skrll default:
9406 1.1 skrll abfd = NULL;
9407 1.1 skrll break;
9408 1.1 skrll
9409 1.1 skrll case bfd_link_hash_undefined:
9410 1.1 skrll case bfd_link_hash_undefweak:
9411 1.1 skrll abfd = h->root.u.undef.abfd;
9412 1.1 skrll if (abfd == NULL
9413 1.1 skrll || (abfd->flags & DYNAMIC) == 0
9414 1.1 skrll || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9415 1.1 skrll return FALSE;
9416 1.1 skrll break;
9417 1.1 skrll
9418 1.1 skrll case bfd_link_hash_defined:
9419 1.1 skrll case bfd_link_hash_defweak:
9420 1.1 skrll abfd = h->root.u.def.section->owner;
9421 1.1 skrll break;
9422 1.1 skrll
9423 1.13 christos case bfd_link_hash_common:
9424 1.13 christos abfd = h->root.u.c.p->section->owner;
9425 1.13 christos break;
9426 1.1 skrll }
9427 1.1 skrll BFD_ASSERT (abfd != NULL);
9428 1.1 skrll
9429 1.1 skrll for (loaded = elf_hash_table (info)->loaded;
9430 1.1 skrll loaded != NULL;
9431 1.1 skrll loaded = loaded->next)
9432 1.1 skrll {
9433 1.1 skrll bfd *input;
9434 1.1 skrll Elf_Internal_Shdr *hdr;
9435 1.1 skrll size_t symcount;
9436 1.1 skrll size_t extsymcount;
9437 1.1 skrll size_t extsymoff;
9438 1.1 skrll Elf_Internal_Shdr *versymhdr;
9439 1.1 skrll Elf_Internal_Sym *isym;
9440 1.1 skrll Elf_Internal_Sym *isymend;
9441 1.1 skrll Elf_Internal_Sym *isymbuf;
9442 1.1 skrll Elf_External_Versym *ever;
9443 1.1 skrll Elf_External_Versym *extversym;
9444 1.1 skrll
9445 1.1 skrll input = loaded->abfd;
9446 1.1 skrll
9447 1.1 skrll /* We check each DSO for a possible hidden versioned definition. */
9448 1.1 skrll if (input == abfd
9449 1.1 skrll || (input->flags & DYNAMIC) == 0
9450 1.1 skrll || elf_dynversym (input) == 0)
9451 1.1 skrll continue;
9452 1.1 skrll
9453 1.1 skrll hdr = &elf_tdata (input)->dynsymtab_hdr;
9454 1.1 skrll
9455 1.1 skrll symcount = hdr->sh_size / bed->s->sizeof_sym;
9456 1.1 skrll if (elf_bad_symtab (input))
9457 1.1 skrll {
9458 1.1 skrll extsymcount = symcount;
9459 1.1 skrll extsymoff = 0;
9460 1.1 skrll }
9461 1.1 skrll else
9462 1.1 skrll {
9463 1.1 skrll extsymcount = symcount - hdr->sh_info;
9464 1.1 skrll extsymoff = hdr->sh_info;
9465 1.4 christos }
9466 1.1 skrll
9467 1.1 skrll if (extsymcount == 0)
9468 1.1 skrll continue;
9469 1.1 skrll
9470 1.1 skrll isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9471 1.1 skrll NULL, NULL, NULL);
9472 1.1 skrll if (isymbuf == NULL)
9473 1.1 skrll return FALSE;
9474 1.1 skrll
9475 1.1 skrll /* Read in any version definitions. */
9476 1.1 skrll versymhdr = &elf_tdata (input)->dynversym_hdr;
9477 1.1 skrll extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9478 1.1 skrll if (extversym == NULL)
9479 1.1 skrll goto error_ret;
9480 1.1 skrll
9481 1.1 skrll if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9482 1.1 skrll || (bfd_bread (extversym, versymhdr->sh_size, input)
9483 1.1 skrll != versymhdr->sh_size))
9484 1.1 skrll {
9485 1.1 skrll free (extversym);
9486 1.1 skrll error_ret:
9487 1.1 skrll free (isymbuf);
9488 1.1 skrll return FALSE;
9489 1.1 skrll }
9490 1.1 skrll
9491 1.1 skrll ever = extversym + extsymoff;
9492 1.1 skrll isymend = isymbuf + extsymcount;
9493 1.1 skrll for (isym = isymbuf; isym < isymend; isym++, ever++)
9494 1.1 skrll {
9495 1.1 skrll const char *name;
9496 1.1 skrll Elf_Internal_Versym iver;
9497 1.1 skrll unsigned short version_index;
9498 1.1 skrll
9499 1.4 christos if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9500 1.4 christos || isym->st_shndx == SHN_UNDEF)
9501 1.4 christos continue;
9502 1.1 skrll
9503 1.1 skrll name = bfd_elf_string_from_elf_section (input,
9504 1.4 christos hdr->sh_link,
9505 1.4 christos isym->st_name);
9506 1.4 christos if (strcmp (name, h->root.root.string) != 0)
9507 1.1 skrll continue;
9508 1.1 skrll
9509 1.1 skrll _bfd_elf_swap_versym_in (input, ever, &iver);
9510 1.1 skrll
9511 1.1 skrll if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9512 1.1 skrll && !(h->def_regular
9513 1.1 skrll && h->forced_local))
9514 1.1 skrll {
9515 1.1 skrll /* If we have a non-hidden versioned sym, then it should
9516 1.1 skrll have provided a definition for the undefined sym unless
9517 1.1 skrll it is defined in a non-shared object and forced local.
9518 1.1 skrll */
9519 1.1 skrll abort ();
9520 1.1 skrll }
9521 1.1 skrll
9522 1.1 skrll version_index = iver.vs_vers & VERSYM_VERSION;
9523 1.1 skrll if (version_index == 1 || version_index == 2)
9524 1.1 skrll {
9525 1.1 skrll /* This is the base or first version. We can use it. */
9526 1.1 skrll free (extversym);
9527 1.13 christos free (isymbuf);
9528 1.13 christos return TRUE;
9529 1.13 christos }
9530 1.13 christos }
9531 1.13 christos
9532 1.13 christos free (extversym);
9533 1.13 christos free (isymbuf);
9534 1.13 christos }
9535 1.13 christos
9536 1.13 christos return FALSE;
9537 1.13 christos }
9538 1.13 christos
9539 1.13 christos /* Convert ELF common symbol TYPE. */
9540 1.13 christos
9541 1.13 christos static int
9542 1.13 christos elf_link_convert_common_type (struct bfd_link_info *info, int type)
9543 1.13 christos {
9544 1.13 christos /* Commom symbol can only appear in relocatable link. */
9545 1.13 christos if (!bfd_link_relocatable (info))
9546 1.13 christos abort ();
9547 1.13 christos switch (info->elf_stt_common)
9548 1.13 christos {
9549 1.1 skrll case unchanged:
9550 1.1 skrll break;
9551 1.1 skrll case elf_stt_common:
9552 1.1 skrll type = STT_COMMON;
9553 1.1 skrll break;
9554 1.1 skrll case no_elf_stt_common:
9555 1.1 skrll type = STT_OBJECT;
9556 1.1 skrll break;
9557 1.7 christos }
9558 1.1 skrll return type;
9559 1.7 christos }
9560 1.4 christos
9561 1.7 christos /* Add an external symbol to the symbol table. This is called from
9562 1.1 skrll the hash table traversal routine. When generating a shared object,
9563 1.1 skrll we go through the symbol table twice. The first time we output
9564 1.1 skrll anything that might have been forced to local scope in a version
9565 1.1 skrll script. The second time we output the symbols that are still
9566 1.4 christos global symbols. */
9567 1.4 christos
9568 1.13 christos static bfd_boolean
9569 1.1 skrll elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9570 1.1 skrll {
9571 1.1 skrll struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9572 1.1 skrll struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9573 1.1 skrll struct elf_final_link_info *flinfo = eoinfo->flinfo;
9574 1.1 skrll bfd_boolean strip;
9575 1.1 skrll Elf_Internal_Sym sym;
9576 1.1 skrll asection *input_sec;
9577 1.1 skrll const struct elf_backend_data *bed;
9578 1.1 skrll long indx;
9579 1.1 skrll int ret;
9580 1.13.12.2 pgoyette unsigned int type;
9581 1.7 christos
9582 1.1 skrll if (h->root.type == bfd_link_hash_warning)
9583 1.1 skrll {
9584 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
9585 1.13.12.2 pgoyette if (h->root.type == bfd_link_hash_new)
9586 1.1 skrll return TRUE;
9587 1.1 skrll }
9588 1.1 skrll
9589 1.7 christos /* Decide whether to output this symbol in this pass. */
9590 1.1 skrll if (eoinfo->localsyms)
9591 1.1 skrll {
9592 1.1 skrll if (!h->forced_local)
9593 1.1 skrll return TRUE;
9594 1.1 skrll }
9595 1.4 christos else
9596 1.4 christos {
9597 1.4 christos if (h->forced_local)
9598 1.1 skrll return TRUE;
9599 1.1 skrll }
9600 1.1 skrll
9601 1.1 skrll bed = get_elf_backend_data (flinfo->output_bfd);
9602 1.1 skrll
9603 1.1 skrll if (h->root.type == bfd_link_hash_undefined)
9604 1.1 skrll {
9605 1.1 skrll /* If we have an undefined symbol reference here then it must have
9606 1.4 christos come from a shared library that is being linked in. (Undefined
9607 1.1 skrll references in regular files have already been handled unless
9608 1.7 christos they are in unreferenced sections which are removed by garbage
9609 1.7 christos collection). */
9610 1.7 christos bfd_boolean ignore_undef = FALSE;
9611 1.13 christos
9612 1.13 christos /* Some symbols may be special in that the fact that they're
9613 1.13 christos undefined can be safely ignored - let backend determine that. */
9614 1.13 christos if (bed->elf_backend_ignore_undef_symbol)
9615 1.13 christos ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9616 1.13 christos
9617 1.13 christos /* If we are reporting errors for this situation then do so now. */
9618 1.13 christos if (!ignore_undef
9619 1.13 christos && h->ref_dynamic
9620 1.1 skrll && (!h->ref_regular || flinfo->info->gc_sections)
9621 1.1 skrll && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9622 1.1 skrll && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9623 1.1 skrll (*flinfo->info->callbacks->undefined_symbol)
9624 1.9 christos (flinfo->info, h->root.root.string,
9625 1.1 skrll h->ref_regular ? NULL : h->root.u.undef.abfd,
9626 1.1 skrll NULL, 0,
9627 1.7 christos flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9628 1.1 skrll
9629 1.7 christos /* Strip a global symbol defined in a discarded section. */
9630 1.7 christos if (h->indx == -3)
9631 1.1 skrll return TRUE;
9632 1.4 christos }
9633 1.4 christos
9634 1.7 christos /* We should also warn if a forced local symbol is referenced from
9635 1.7 christos shared libraries. */
9636 1.7 christos if (bfd_link_executable (flinfo->info)
9637 1.7 christos && h->forced_local
9638 1.7 christos && h->ref_dynamic
9639 1.4 christos && h->def_regular
9640 1.4 christos && !h->dynamic_def
9641 1.13.12.2 pgoyette && h->ref_dynamic_nonweak
9642 1.4 christos && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9643 1.4 christos {
9644 1.13.12.2 pgoyette bfd *def_bfd;
9645 1.4 christos const char *msg;
9646 1.4 christos struct elf_link_hash_entry *hi = h;
9647 1.13.12.2 pgoyette
9648 1.4 christos /* Check indirect symbol. */
9649 1.7 christos while (hi->root.type == bfd_link_hash_indirect)
9650 1.7 christos hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9651 1.7 christos
9652 1.13.12.2 pgoyette if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9653 1.13.12.2 pgoyette /* xgettext:c-format */
9654 1.4 christos msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9655 1.1 skrll else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9656 1.1 skrll /* xgettext:c-format */
9657 1.1 skrll msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9658 1.1 skrll else
9659 1.1 skrll /* xgettext:c-format */
9660 1.1 skrll msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9661 1.1 skrll def_bfd = flinfo->output_bfd;
9662 1.1 skrll if (hi->root.u.def.section != bfd_abs_section_ptr)
9663 1.13 christos def_bfd = hi->root.u.def.section->owner;
9664 1.1 skrll _bfd_error_handler (msg, flinfo->output_bfd,
9665 1.13 christos h->root.root.string, def_bfd);
9666 1.1 skrll bfd_set_error (bfd_error_bad_value);
9667 1.1 skrll eoinfo->failed = TRUE;
9668 1.1 skrll return FALSE;
9669 1.1 skrll }
9670 1.1 skrll
9671 1.1 skrll /* We don't want to output symbols that have never been mentioned by
9672 1.7 christos a regular file, or that we have been told to strip. However, if
9673 1.1 skrll h->indx is set to -2, the symbol is used by a reloc and we must
9674 1.7 christos output it. */
9675 1.7 christos strip = FALSE;
9676 1.1 skrll if (h->indx == -2)
9677 1.1 skrll ;
9678 1.7 christos else if ((h->def_dynamic
9679 1.7 christos || h->ref_dynamic
9680 1.7 christos || h->root.type == bfd_link_hash_new)
9681 1.7 christos && !h->def_regular
9682 1.9 christos && !h->ref_regular)
9683 1.9 christos strip = TRUE;
9684 1.7 christos else if (flinfo->info->strip == strip_all)
9685 1.1 skrll strip = TRUE;
9686 1.4 christos else if (flinfo->info->strip == strip_some
9687 1.4 christos && bfd_hash_lookup (flinfo->info->keep_hash,
9688 1.4 christos h->root.root.string, FALSE, FALSE) == NULL)
9689 1.4 christos strip = TRUE;
9690 1.4 christos else if ((h->root.type == bfd_link_hash_defined
9691 1.13 christos || h->root.type == bfd_link_hash_defweak)
9692 1.13 christos && ((flinfo->info->strip_discarded
9693 1.1 skrll && discarded_section (h->root.u.def.section))
9694 1.1 skrll || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9695 1.13 christos && h->root.u.def.section->owner != NULL
9696 1.13 christos && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9697 1.13 christos strip = TRUE;
9698 1.1 skrll else if ((h->root.type == bfd_link_hash_undefined
9699 1.1 skrll || h->root.type == bfd_link_hash_undefweak)
9700 1.13 christos && h->root.u.undef.abfd != NULL
9701 1.1 skrll && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9702 1.1 skrll strip = TRUE;
9703 1.1 skrll
9704 1.1 skrll type = h->type;
9705 1.1 skrll
9706 1.1 skrll /* If we're stripping it, and it's not a dynamic symbol, there's
9707 1.1 skrll nothing else to do. However, if it is a forced local symbol or
9708 1.1 skrll an ifunc symbol we need to give the backend finish_dynamic_symbol
9709 1.1 skrll function a chance to make it dynamic. */
9710 1.1 skrll if (strip
9711 1.1 skrll && h->dynindx == -1
9712 1.1 skrll && type != STT_GNU_IFUNC
9713 1.1 skrll && !h->forced_local)
9714 1.1 skrll return TRUE;
9715 1.1 skrll
9716 1.1 skrll sym.st_value = 0;
9717 1.1 skrll sym.st_size = h->size;
9718 1.1 skrll sym.st_other = h->other;
9719 1.1 skrll switch (h->root.type)
9720 1.1 skrll {
9721 1.1 skrll default:
9722 1.1 skrll case bfd_link_hash_new:
9723 1.1 skrll case bfd_link_hash_warning:
9724 1.1 skrll abort ();
9725 1.1 skrll return FALSE;
9726 1.1 skrll
9727 1.1 skrll case bfd_link_hash_undefined:
9728 1.7 christos case bfd_link_hash_undefweak:
9729 1.1 skrll input_sec = bfd_und_section_ptr;
9730 1.1 skrll sym.st_shndx = SHN_UNDEF;
9731 1.1 skrll break;
9732 1.13.12.2 pgoyette
9733 1.13.12.2 pgoyette case bfd_link_hash_defined:
9734 1.1 skrll case bfd_link_hash_defweak:
9735 1.7 christos {
9736 1.4 christos input_sec = h->root.u.def.section;
9737 1.1 skrll if (input_sec->output_section != NULL)
9738 1.1 skrll {
9739 1.1 skrll sym.st_shndx =
9740 1.1 skrll _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9741 1.1 skrll input_sec->output_section);
9742 1.1 skrll if (sym.st_shndx == SHN_BAD)
9743 1.1 skrll {
9744 1.1 skrll _bfd_error_handler
9745 1.9 christos /* xgettext:c-format */
9746 1.1 skrll (_("%B: could not find output section %A for input section %A"),
9747 1.1 skrll flinfo->output_bfd, input_sec->output_section, input_sec);
9748 1.1 skrll bfd_set_error (bfd_error_nonrepresentable_section);
9749 1.1 skrll eoinfo->failed = TRUE;
9750 1.7 christos return FALSE;
9751 1.1 skrll }
9752 1.1 skrll
9753 1.1 skrll /* ELF symbols in relocatable files are section relative,
9754 1.1 skrll but in nonrelocatable files they are virtual
9755 1.1 skrll addresses. */
9756 1.1 skrll sym.st_value = h->root.u.def.value + input_sec->output_offset;
9757 1.1 skrll if (!bfd_link_relocatable (flinfo->info))
9758 1.1 skrll {
9759 1.1 skrll sym.st_value += input_sec->output_section->vma;
9760 1.1 skrll if (h->type == STT_TLS)
9761 1.1 skrll {
9762 1.1 skrll asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9763 1.1 skrll if (tls_sec != NULL)
9764 1.1 skrll sym.st_value -= tls_sec->vma;
9765 1.1 skrll }
9766 1.1 skrll }
9767 1.1 skrll }
9768 1.1 skrll else
9769 1.1 skrll {
9770 1.1 skrll BFD_ASSERT (input_sec->owner == NULL
9771 1.1 skrll || (input_sec->owner->flags & DYNAMIC) != 0);
9772 1.1 skrll sym.st_shndx = SHN_UNDEF;
9773 1.1 skrll input_sec = bfd_und_section_ptr;
9774 1.1 skrll }
9775 1.1 skrll }
9776 1.1 skrll break;
9777 1.1 skrll
9778 1.1 skrll case bfd_link_hash_common:
9779 1.1 skrll input_sec = h->root.u.c.p->section;
9780 1.1 skrll sym.st_shndx = bed->common_section_index (input_sec);
9781 1.1 skrll sym.st_value = 1 << h->root.u.c.p->alignment_power;
9782 1.13 christos break;
9783 1.13 christos
9784 1.13 christos case bfd_link_hash_indirect:
9785 1.13 christos /* These symbols are created by symbol versioning. They point
9786 1.13 christos to the decorated version of the name. For example, if the
9787 1.13 christos symbol foo@@GNU_1.2 is the default, which should be used when
9788 1.13 christos foo is used with no version, then we add an indirect symbol
9789 1.13 christos foo which points to foo@@GNU_1.2. We ignore these symbols,
9790 1.13 christos since the indirected symbol is already in the hash table. */
9791 1.13 christos return TRUE;
9792 1.13 christos }
9793 1.13 christos
9794 1.13 christos if (type == STT_COMMON || type == STT_OBJECT)
9795 1.13 christos switch (h->root.type)
9796 1.13 christos {
9797 1.13 christos case bfd_link_hash_common:
9798 1.13 christos type = elf_link_convert_common_type (flinfo->info, type);
9799 1.13 christos break;
9800 1.13 christos case bfd_link_hash_defined:
9801 1.13 christos case bfd_link_hash_defweak:
9802 1.13.12.2 pgoyette if (bed->common_definition (&sym))
9803 1.13 christos type = elf_link_convert_common_type (flinfo->info, type);
9804 1.13 christos else
9805 1.13 christos type = STT_OBJECT;
9806 1.13 christos break;
9807 1.13 christos case bfd_link_hash_undefined:
9808 1.13 christos case bfd_link_hash_undefweak:
9809 1.13 christos break;
9810 1.13 christos default:
9811 1.13 christos abort ();
9812 1.13 christos }
9813 1.13 christos
9814 1.13 christos if (h->forced_local)
9815 1.13 christos {
9816 1.13 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9817 1.13 christos /* Turn off visibility on local symbol. */
9818 1.1 skrll sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9819 1.1 skrll }
9820 1.1 skrll /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9821 1.4 christos else if (h->unique_global && h->def_regular)
9822 1.4 christos sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9823 1.4 christos else if (h->root.type == bfd_link_hash_undefweak
9824 1.4 christos || h->root.type == bfd_link_hash_defweak)
9825 1.9 christos sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9826 1.4 christos else
9827 1.4 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9828 1.9 christos sym.st_target_internal = h->target_internal;
9829 1.4 christos
9830 1.4 christos /* Give the processor backend a chance to tweak the symbol value,
9831 1.4 christos and also to finish up anything that needs to be done for this
9832 1.7 christos symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9833 1.1 skrll forced local syms when non-shared is due to a historical quirk.
9834 1.1 skrll STT_GNU_IFUNC symbol must go through PLT. */
9835 1.7 christos if ((h->type == STT_GNU_IFUNC
9836 1.1 skrll && h->def_regular
9837 1.1 skrll && !bfd_link_relocatable (flinfo->info))
9838 1.1 skrll || ((h->dynindx != -1
9839 1.1 skrll || h->forced_local)
9840 1.1 skrll && ((bfd_link_pic (flinfo->info)
9841 1.1 skrll && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9842 1.1 skrll || h->root.type != bfd_link_hash_undefweak))
9843 1.1 skrll || !h->forced_local)
9844 1.1 skrll && elf_hash_table (flinfo->info)->dynamic_sections_created))
9845 1.1 skrll {
9846 1.1 skrll if (! ((*bed->elf_backend_finish_dynamic_symbol)
9847 1.1 skrll (flinfo->output_bfd, flinfo->info, h, &sym)))
9848 1.1 skrll {
9849 1.1 skrll eoinfo->failed = TRUE;
9850 1.1 skrll return FALSE;
9851 1.1 skrll }
9852 1.1 skrll }
9853 1.1 skrll
9854 1.13 christos /* If we are marking the symbol as undefined, and there are no
9855 1.4 christos non-weak references to this symbol from a regular object, then
9856 1.4 christos mark the symbol as weak undefined; if there are non-weak
9857 1.4 christos references, mark the symbol as strong. We can't do this earlier,
9858 1.4 christos because it might not be marked as undefined until the
9859 1.1 skrll finish_dynamic_symbol routine gets through with it. */
9860 1.1 skrll if (sym.st_shndx == SHN_UNDEF
9861 1.1 skrll && h->ref_regular
9862 1.1 skrll && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9863 1.1 skrll || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9864 1.4 christos {
9865 1.1 skrll int bindtype;
9866 1.1 skrll type = ELF_ST_TYPE (sym.st_info);
9867 1.1 skrll
9868 1.1 skrll /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9869 1.1 skrll if (type == STT_GNU_IFUNC)
9870 1.1 skrll type = STT_FUNC;
9871 1.1 skrll
9872 1.1 skrll if (h->ref_regular_nonweak)
9873 1.1 skrll bindtype = STB_GLOBAL;
9874 1.1 skrll else
9875 1.1 skrll bindtype = STB_WEAK;
9876 1.11 joerg sym.st_info = ELF_ST_INFO (bindtype, type);
9877 1.1 skrll }
9878 1.9 christos
9879 1.1 skrll /* If this is a symbol defined in a dynamic library, don't use the
9880 1.1 skrll symbol size from the dynamic library. Relinking an executable
9881 1.1 skrll against a new library may introduce gratuitous changes in the
9882 1.1 skrll executable's symbols if we keep the size. */
9883 1.1 skrll if (sym.st_shndx == SHN_UNDEF
9884 1.4 christos && !h->def_regular
9885 1.4 christos && h->def_dynamic)
9886 1.4 christos sym.st_size = 0;
9887 1.13.12.2 pgoyette
9888 1.4 christos /* If a non-weak symbol with non-default visibility is not defined
9889 1.4 christos locally, it is a fatal error. */
9890 1.13.12.2 pgoyette if (!bfd_link_relocatable (flinfo->info)
9891 1.4 christos && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9892 1.4 christos && ELF_ST_BIND (sym.st_info) != STB_WEAK
9893 1.13.12.2 pgoyette && h->root.type == bfd_link_hash_undefined
9894 1.4 christos && !h->def_regular)
9895 1.13.12.2 pgoyette {
9896 1.4 christos const char *msg;
9897 1.1 skrll
9898 1.1 skrll if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9899 1.1 skrll /* xgettext:c-format */
9900 1.1 skrll msg = _("%B: protected symbol `%s' isn't defined");
9901 1.1 skrll else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9902 1.1 skrll /* xgettext:c-format */
9903 1.1 skrll msg = _("%B: internal symbol `%s' isn't defined");
9904 1.9 christos else
9905 1.7 christos /* xgettext:c-format */
9906 1.7 christos msg = _("%B: hidden symbol `%s' isn't defined");
9907 1.1 skrll _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9908 1.1 skrll bfd_set_error (bfd_error_bad_value);
9909 1.1 skrll eoinfo->failed = TRUE;
9910 1.7 christos return FALSE;
9911 1.7 christos }
9912 1.9 christos
9913 1.13.12.2 pgoyette /* If this symbol should be put in the .dynsym section, then put it
9914 1.9 christos there now. We already know the symbol index. We also fill in
9915 1.9 christos the entry in the .hash section. */
9916 1.9 christos if (elf_hash_table (flinfo->info)->dynsym != NULL
9917 1.9 christos && h->dynindx != -1
9918 1.7 christos && elf_hash_table (flinfo->info)->dynamic_sections_created)
9919 1.7 christos {
9920 1.7 christos bfd_byte *esym;
9921 1.7 christos
9922 1.7 christos /* Since there is no version information in the dynamic string,
9923 1.13.12.2 pgoyette if there is no version info in symbol version section, we will
9924 1.13.12.2 pgoyette have a run-time problem if not linking executable, referenced
9925 1.7 christos by shared library, or not bound locally. */
9926 1.7 christos if (h->verinfo.verdef == NULL
9927 1.7 christos && (!bfd_link_executable (flinfo->info)
9928 1.7 christos || h->ref_dynamic
9929 1.7 christos || !h->def_regular))
9930 1.7 christos {
9931 1.7 christos char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9932 1.1 skrll
9933 1.9 christos if (p && p [1] != '\0')
9934 1.9 christos {
9935 1.7 christos _bfd_error_handler
9936 1.1 skrll /* xgettext:c-format */
9937 1.1 skrll (_("%B: No symbol version section for versioned symbol `%s'"),
9938 1.1 skrll flinfo->output_bfd, h->root.root.string);
9939 1.1 skrll eoinfo->failed = TRUE;
9940 1.7 christos return FALSE;
9941 1.1 skrll }
9942 1.7 christos }
9943 1.1 skrll
9944 1.1 skrll sym.st_name = h->dynstr_index;
9945 1.1 skrll esym = (elf_hash_table (flinfo->info)->dynsym->contents
9946 1.1 skrll + h->dynindx * bed->s->sizeof_sym);
9947 1.1 skrll if (!check_dynsym (flinfo->output_bfd, &sym))
9948 1.1 skrll {
9949 1.1 skrll eoinfo->failed = TRUE;
9950 1.7 christos return FALSE;
9951 1.1 skrll }
9952 1.1 skrll bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9953 1.1 skrll
9954 1.7 christos if (flinfo->hash_sec != NULL)
9955 1.7 christos {
9956 1.1 skrll size_t hash_entry_size;
9957 1.7 christos bfd_byte *bucketpos;
9958 1.7 christos bfd_vma chain;
9959 1.7 christos size_t bucketcount;
9960 1.7 christos size_t bucket;
9961 1.7 christos
9962 1.1 skrll bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9963 1.1 skrll bucket = h->u.elf_hash_value % bucketcount;
9964 1.1 skrll
9965 1.7 christos hash_entry_size
9966 1.1 skrll = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9967 1.1 skrll bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9968 1.1 skrll + (bucket + 2) * hash_entry_size);
9969 1.1 skrll chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9970 1.1 skrll bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9971 1.1 skrll bucketpos);
9972 1.9 christos bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9973 1.9 christos ((bfd_byte *) flinfo->hash_sec->contents
9974 1.9 christos + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9975 1.1 skrll }
9976 1.1 skrll
9977 1.1 skrll if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9978 1.1 skrll {
9979 1.1 skrll Elf_Internal_Versym iversym;
9980 1.1 skrll Elf_External_Versym *eversym;
9981 1.1 skrll
9982 1.1 skrll if (!h->def_regular)
9983 1.1 skrll {
9984 1.1 skrll if (h->verinfo.verdef == NULL
9985 1.7 christos || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9986 1.1 skrll & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9987 1.1 skrll iversym.vs_vers = 0;
9988 1.1 skrll else
9989 1.9 christos iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9990 1.9 christos }
9991 1.9 christos else
9992 1.1 skrll {
9993 1.1 skrll if (h->verinfo.vertree == NULL)
9994 1.7 christos iversym.vs_vers = 1;
9995 1.1 skrll else
9996 1.7 christos iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9997 1.1 skrll if (flinfo->info->create_default_symver)
9998 1.1 skrll iversym.vs_vers++;
9999 1.1 skrll }
10000 1.13 christos
10001 1.13 christos /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10002 1.13 christos defined locally. */
10003 1.13 christos if (h->versioned == versioned_hidden && h->def_regular)
10004 1.13 christos iversym.vs_vers |= VERSYM_HIDDEN;
10005 1.13.12.2 pgoyette
10006 1.13.12.2 pgoyette eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10007 1.13 christos eversym += h->dynindx;
10008 1.13 christos _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10009 1.13.12.2 pgoyette }
10010 1.13 christos }
10011 1.13 christos
10012 1.13 christos /* If the symbol is undefined, and we didn't output it to .dynsym,
10013 1.13 christos strip it from .symtab too. Obviously we can't do this for
10014 1.13 christos relocatable output or when needed for --emit-relocs. */
10015 1.1 skrll else if (input_sec == bfd_und_section_ptr
10016 1.1 skrll && h->indx != -2
10017 1.9 christos /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10018 1.9 christos && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10019 1.9 christos && !bfd_link_relocatable (flinfo->info))
10020 1.9 christos return TRUE;
10021 1.9 christos
10022 1.9 christos /* Also strip others that we couldn't earlier due to dynamic symbol
10023 1.9 christos processing. */
10024 1.9 christos if (strip)
10025 1.9 christos return TRUE;
10026 1.9 christos if ((input_sec->flags & SEC_EXCLUDE) != 0)
10027 1.9 christos return TRUE;
10028 1.9 christos
10029 1.9 christos /* Output a FILE symbol so that following locals are not associated
10030 1.9 christos with the wrong input file. We need one for forced local symbols
10031 1.9 christos if we've seen more than one FILE symbol or when we have exactly
10032 1.9 christos one FILE symbol but global symbols are present in a file other
10033 1.9 christos than the one with the FILE symbol. We also need one if linker
10034 1.9 christos defined symbols are present. In practice these conditions are
10035 1.9 christos always met, so just emit the FILE symbol unconditionally. */
10036 1.9 christos if (eoinfo->localsyms
10037 1.9 christos && !eoinfo->file_sym_done
10038 1.9 christos && eoinfo->flinfo->filesym_count != 0)
10039 1.9 christos {
10040 1.7 christos Elf_Internal_Sym fsym;
10041 1.9 christos
10042 1.9 christos memset (&fsym, 0, sizeof (fsym));
10043 1.4 christos fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10044 1.1 skrll fsym.st_shndx = SHN_ABS;
10045 1.1 skrll if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10046 1.1 skrll bfd_und_section_ptr, NULL))
10047 1.1 skrll return FALSE;
10048 1.4 christos
10049 1.4 christos eoinfo->file_sym_done = TRUE;
10050 1.4 christos }
10051 1.4 christos
10052 1.1 skrll indx = bfd_get_symcount (flinfo->output_bfd);
10053 1.1 skrll ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10054 1.1 skrll input_sec, h);
10055 1.1 skrll if (ret == 0)
10056 1.1 skrll {
10057 1.1 skrll eoinfo->failed = TRUE;
10058 1.1 skrll return FALSE;
10059 1.1 skrll }
10060 1.1 skrll else if (ret == 1)
10061 1.1 skrll h->indx = indx;
10062 1.1 skrll else if (h->indx == -2)
10063 1.1 skrll abort();
10064 1.1 skrll
10065 1.1 skrll return TRUE;
10066 1.7 christos }
10067 1.7 christos
10068 1.9 christos /* Return TRUE if special handling is done for relocs in SEC against
10069 1.1 skrll symbols defined in discarded sections. */
10070 1.1 skrll
10071 1.1 skrll static bfd_boolean
10072 1.1 skrll elf_section_ignore_discarded_relocs (asection *sec)
10073 1.1 skrll {
10074 1.1 skrll const struct elf_backend_data *bed;
10075 1.1 skrll
10076 1.1 skrll switch (sec->sec_info_type)
10077 1.1 skrll {
10078 1.1 skrll case SEC_INFO_TYPE_STABS:
10079 1.1 skrll case SEC_INFO_TYPE_EH_FRAME:
10080 1.1 skrll case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10081 1.1 skrll return TRUE;
10082 1.1 skrll default:
10083 1.1 skrll break;
10084 1.1 skrll }
10085 1.1 skrll
10086 1.1 skrll bed = get_elf_backend_data (sec->owner);
10087 1.1 skrll if (bed->elf_backend_ignore_discarded_relocs != NULL
10088 1.1 skrll && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10089 1.1 skrll return TRUE;
10090 1.1 skrll
10091 1.1 skrll return FALSE;
10092 1.1 skrll }
10093 1.1 skrll
10094 1.1 skrll /* Return a mask saying how ld should treat relocations in SEC against
10095 1.1 skrll symbols defined in discarded sections. If this function returns
10096 1.1 skrll COMPLAIN set, ld will issue a warning message. If this function
10097 1.1 skrll returns PRETEND set, and the discarded section was link-once and the
10098 1.1 skrll same size as the kept link-once section, ld will pretend that the
10099 1.1 skrll symbol was actually defined in the kept section. Otherwise ld will
10100 1.1 skrll zero the reloc (at least that is the intent, but some cooperation by
10101 1.1 skrll the target dependent code is needed, particularly for REL targets). */
10102 1.1 skrll
10103 1.1 skrll unsigned int
10104 1.1 skrll _bfd_elf_default_action_discarded (asection *sec)
10105 1.1 skrll {
10106 1.1 skrll if (sec->flags & SEC_DEBUGGING)
10107 1.1 skrll return PRETEND;
10108 1.1 skrll
10109 1.1 skrll if (strcmp (".eh_frame", sec->name) == 0)
10110 1.1 skrll return 0;
10111 1.1 skrll
10112 1.1 skrll if (strcmp (".gcc_except_table", sec->name) == 0)
10113 1.1 skrll return 0;
10114 1.1 skrll
10115 1.1 skrll return COMPLAIN | PRETEND;
10116 1.1 skrll }
10117 1.1 skrll
10118 1.1 skrll /* Find a match between a section and a member of a section group. */
10119 1.1 skrll
10120 1.1 skrll static asection *
10121 1.1 skrll match_group_member (asection *sec, asection *group,
10122 1.1 skrll struct bfd_link_info *info)
10123 1.1 skrll {
10124 1.1 skrll asection *first = elf_next_in_group (group);
10125 1.1 skrll asection *s = first;
10126 1.1 skrll
10127 1.1 skrll while (s != NULL)
10128 1.1 skrll {
10129 1.1 skrll if (bfd_elf_match_symbols_in_sections (s, sec, info))
10130 1.1 skrll return s;
10131 1.1 skrll
10132 1.1 skrll s = elf_next_in_group (s);
10133 1.1 skrll if (s == first)
10134 1.1 skrll break;
10135 1.1 skrll }
10136 1.1 skrll
10137 1.1 skrll return NULL;
10138 1.1 skrll }
10139 1.1 skrll
10140 1.1 skrll /* Check if the kept section of a discarded section SEC can be used
10141 1.1 skrll to replace it. Return the replacement if it is OK. Otherwise return
10142 1.1 skrll NULL. */
10143 1.1 skrll
10144 1.1 skrll asection *
10145 1.1 skrll _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10146 1.1 skrll {
10147 1.1 skrll asection *kept;
10148 1.1 skrll
10149 1.1 skrll kept = sec->kept_section;
10150 1.1 skrll if (kept != NULL)
10151 1.1 skrll {
10152 1.1 skrll if ((kept->flags & SEC_GROUP) != 0)
10153 1.1 skrll kept = match_group_member (sec, kept, info);
10154 1.1 skrll if (kept != NULL
10155 1.1 skrll && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10156 1.1 skrll != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10157 1.7 christos kept = NULL;
10158 1.1 skrll sec->kept_section = kept;
10159 1.1 skrll }
10160 1.1 skrll return kept;
10161 1.1 skrll }
10162 1.1 skrll
10163 1.1 skrll /* Link an input file into the linker output file. This function
10164 1.1 skrll handles all the sections and relocations of the input file at once.
10165 1.1 skrll This is so that we only have to read the local symbols once, and
10166 1.1 skrll don't have to keep them in memory. */
10167 1.1 skrll
10168 1.1 skrll static bfd_boolean
10169 1.1 skrll elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10170 1.1 skrll {
10171 1.1 skrll int (*relocate_section)
10172 1.1 skrll (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10173 1.1 skrll Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10174 1.7 christos bfd *output_bfd;
10175 1.7 christos Elf_Internal_Shdr *symtab_hdr;
10176 1.7 christos size_t locsymcount;
10177 1.7 christos size_t extsymoff;
10178 1.1 skrll Elf_Internal_Sym *isymbuf;
10179 1.7 christos Elf_Internal_Sym *isym;
10180 1.1 skrll Elf_Internal_Sym *isymend;
10181 1.1 skrll long *pindex;
10182 1.1 skrll asection **ppsection;
10183 1.1 skrll asection *o;
10184 1.1 skrll const struct elf_backend_data *bed;
10185 1.1 skrll struct elf_link_hash_entry **sym_hashes;
10186 1.1 skrll bfd_size_type address_size;
10187 1.1 skrll bfd_vma r_type_mask;
10188 1.1 skrll int r_sym_shift;
10189 1.1 skrll bfd_boolean have_file_sym = FALSE;
10190 1.1 skrll
10191 1.1 skrll output_bfd = flinfo->output_bfd;
10192 1.1 skrll bed = get_elf_backend_data (output_bfd);
10193 1.1 skrll relocate_section = bed->elf_backend_relocate_section;
10194 1.1 skrll
10195 1.1 skrll /* If this is a dynamic object, we don't want to do anything here:
10196 1.1 skrll we don't want the local symbols, and we don't want the section
10197 1.1 skrll contents. */
10198 1.1 skrll if ((input_bfd->flags & DYNAMIC) != 0)
10199 1.1 skrll return TRUE;
10200 1.1 skrll
10201 1.1 skrll symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10202 1.1 skrll if (elf_bad_symtab (input_bfd))
10203 1.1 skrll {
10204 1.1 skrll locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10205 1.1 skrll extsymoff = 0;
10206 1.7 christos }
10207 1.7 christos else
10208 1.7 christos {
10209 1.1 skrll locsymcount = symtab_hdr->sh_info;
10210 1.1 skrll extsymoff = symtab_hdr->sh_info;
10211 1.1 skrll }
10212 1.1 skrll
10213 1.1 skrll /* Read the local symbols. */
10214 1.1 skrll isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10215 1.1 skrll if (isymbuf == NULL && locsymcount != 0)
10216 1.1 skrll {
10217 1.7 christos isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10218 1.1 skrll flinfo->internal_syms,
10219 1.1 skrll flinfo->external_syms,
10220 1.1 skrll flinfo->locsym_shndx);
10221 1.1 skrll if (isymbuf == NULL)
10222 1.1 skrll return FALSE;
10223 1.1 skrll }
10224 1.4 christos
10225 1.4 christos /* Find local symbol sections and adjust values of symbols in
10226 1.1 skrll SEC_MERGE sections. Write out those local symbols we know are
10227 1.1 skrll going into the output file. */
10228 1.1 skrll isymend = isymbuf + locsymcount;
10229 1.1 skrll for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10230 1.1 skrll isym < isymend;
10231 1.1 skrll isym++, pindex++, ppsection++)
10232 1.1 skrll {
10233 1.1 skrll asection *isec;
10234 1.1 skrll const char *name;
10235 1.1 skrll Elf_Internal_Sym osym;
10236 1.1 skrll long indx;
10237 1.1 skrll int ret;
10238 1.1 skrll
10239 1.1 skrll *pindex = -1;
10240 1.1 skrll
10241 1.1 skrll if (elf_bad_symtab (input_bfd))
10242 1.1 skrll {
10243 1.1 skrll if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10244 1.1 skrll {
10245 1.1 skrll *ppsection = NULL;
10246 1.1 skrll continue;
10247 1.1 skrll }
10248 1.1 skrll }
10249 1.1 skrll
10250 1.1 skrll if (isym->st_shndx == SHN_UNDEF)
10251 1.1 skrll isec = bfd_und_section_ptr;
10252 1.1 skrll else if (isym->st_shndx == SHN_ABS)
10253 1.1 skrll isec = bfd_abs_section_ptr;
10254 1.7 christos else if (isym->st_shndx == SHN_COMMON)
10255 1.1 skrll isec = bfd_com_section_ptr;
10256 1.1 skrll else
10257 1.1 skrll {
10258 1.1 skrll isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10259 1.1 skrll if (isec == NULL)
10260 1.1 skrll {
10261 1.1 skrll /* Don't attempt to output symbols with st_shnx in the
10262 1.1 skrll reserved range other than SHN_ABS and SHN_COMMON. */
10263 1.1 skrll *ppsection = NULL;
10264 1.13 christos continue;
10265 1.13 christos }
10266 1.13 christos else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10267 1.1 skrll && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10268 1.1 skrll isym->st_value =
10269 1.1 skrll _bfd_merged_section_offset (output_bfd, &isec,
10270 1.1 skrll elf_section_data (isec)->sec_info,
10271 1.1 skrll isym->st_value);
10272 1.1 skrll }
10273 1.1 skrll
10274 1.1 skrll *ppsection = isec;
10275 1.1 skrll
10276 1.1 skrll /* Don't output the first, undefined, symbol. In fact, don't
10277 1.1 skrll output any undefined local symbol. */
10278 1.1 skrll if (isec == bfd_und_section_ptr)
10279 1.7 christos continue;
10280 1.1 skrll
10281 1.1 skrll if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10282 1.1 skrll {
10283 1.1 skrll /* We never output section symbols. Instead, we use the
10284 1.1 skrll section symbol of the corresponding section in the output
10285 1.1 skrll file. */
10286 1.1 skrll continue;
10287 1.7 christos }
10288 1.1 skrll
10289 1.1 skrll /* If we are stripping all symbols, we don't want to output this
10290 1.1 skrll one. */
10291 1.1 skrll if (flinfo->info->strip == strip_all)
10292 1.1 skrll continue;
10293 1.1 skrll
10294 1.1 skrll /* If we are discarding all local symbols, we don't want to
10295 1.1 skrll output this one. If we are generating a relocatable output
10296 1.1 skrll file, then some of the local symbols may be required by
10297 1.1 skrll relocs; we output them below as we discover that they are
10298 1.1 skrll needed. */
10299 1.1 skrll if (flinfo->info->discard == discard_all)
10300 1.1 skrll continue;
10301 1.1 skrll
10302 1.1 skrll /* If this symbol is defined in a section which we are
10303 1.1 skrll discarding, we don't need to keep it. */
10304 1.1 skrll if (isym->st_shndx != SHN_UNDEF
10305 1.7 christos && isym->st_shndx < SHN_LORESERVE
10306 1.7 christos && bfd_section_removed_from_list (output_bfd,
10307 1.1 skrll isec->output_section))
10308 1.7 christos continue;
10309 1.9 christos
10310 1.9 christos /* Get the name of the symbol. */
10311 1.7 christos name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10312 1.1 skrll isym->st_name);
10313 1.1 skrll if (name == NULL)
10314 1.1 skrll return FALSE;
10315 1.7 christos
10316 1.7 christos /* See if we are discarding symbols with this name. */
10317 1.9 christos if ((flinfo->info->strip == strip_some
10318 1.9 christos && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10319 1.9 christos == NULL))
10320 1.9 christos || (((flinfo->info->discard == discard_sec_merge
10321 1.7 christos && (isec->flags & SEC_MERGE)
10322 1.7 christos && !bfd_link_relocatable (flinfo->info))
10323 1.7 christos || flinfo->info->discard == discard_l)
10324 1.7 christos && bfd_is_local_label_name (input_bfd, name)))
10325 1.7 christos continue;
10326 1.7 christos
10327 1.7 christos if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10328 1.7 christos {
10329 1.7 christos if (input_bfd->lto_output)
10330 1.7 christos /* -flto puts a temp file name here. This means builds
10331 1.7 christos are not reproducible. Discard the symbol. */
10332 1.7 christos continue;
10333 1.7 christos have_file_sym = TRUE;
10334 1.7 christos flinfo->filesym_count += 1;
10335 1.7 christos }
10336 1.7 christos if (!have_file_sym)
10337 1.9 christos {
10338 1.9 christos /* In the absence of debug info, bfd_find_nearest_line uses
10339 1.9 christos FILE symbols to determine the source file for local
10340 1.9 christos function symbols. Provide a FILE symbol here if input
10341 1.9 christos files lack such, so that their symbols won't be
10342 1.7 christos associated with a previous input file. It's not the
10343 1.7 christos source file, but the best we can do. */
10344 1.7 christos have_file_sym = TRUE;
10345 1.1 skrll flinfo->filesym_count += 1;
10346 1.1 skrll memset (&osym, 0, sizeof (osym));
10347 1.1 skrll osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10348 1.1 skrll osym.st_shndx = SHN_ABS;
10349 1.1 skrll if (!elf_link_output_symstrtab (flinfo,
10350 1.1 skrll (input_bfd->lto_output ? NULL
10351 1.1 skrll : input_bfd->filename),
10352 1.1 skrll &osym, bfd_abs_section_ptr,
10353 1.1 skrll NULL))
10354 1.1 skrll return FALSE;
10355 1.1 skrll }
10356 1.1 skrll
10357 1.1 skrll osym = *isym;
10358 1.1 skrll
10359 1.1 skrll /* Adjust the section index for the output file. */
10360 1.1 skrll osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10361 1.9 christos isec->output_section);
10362 1.1 skrll if (osym.st_shndx == SHN_BAD)
10363 1.1 skrll return FALSE;
10364 1.1 skrll
10365 1.1 skrll /* ELF symbols in relocatable files are section relative, but
10366 1.1 skrll in executable files they are virtual addresses. Note that
10367 1.7 christos this code assumes that all ELF sections have an associated
10368 1.7 christos BFD section with a reasonable value for output_offset; below
10369 1.1 skrll we assume that they also have a reasonable value for
10370 1.1 skrll output_section. Any special sections must be set up to meet
10371 1.1 skrll these requirements. */
10372 1.4 christos osym.st_value += isec->output_offset;
10373 1.9 christos if (!bfd_link_relocatable (flinfo->info))
10374 1.4 christos {
10375 1.1 skrll osym.st_value += isec->output_section->vma;
10376 1.4 christos if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10377 1.4 christos {
10378 1.1 skrll /* STT_TLS symbols are relative to PT_TLS segment base. */
10379 1.1 skrll BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10380 1.7 christos osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10381 1.7 christos }
10382 1.7 christos }
10383 1.7 christos
10384 1.7 christos indx = bfd_get_symcount (output_bfd);
10385 1.7 christos ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10386 1.7 christos if (ret == 0)
10387 1.7 christos return FALSE;
10388 1.7 christos else if (ret == 1)
10389 1.7 christos *pindex = indx;
10390 1.7 christos }
10391 1.7 christos
10392 1.7 christos if (bed->s->arch_size == 32)
10393 1.1 skrll {
10394 1.1 skrll r_type_mask = 0xff;
10395 1.1 skrll r_sym_shift = 8;
10396 1.1 skrll address_size = 4;
10397 1.1 skrll }
10398 1.1 skrll else
10399 1.1 skrll {
10400 1.1 skrll r_type_mask = 0xffffffff;
10401 1.1 skrll r_sym_shift = 32;
10402 1.1 skrll address_size = 8;
10403 1.1 skrll }
10404 1.1 skrll
10405 1.13.12.2 pgoyette /* Relocate the contents of each section. */
10406 1.3 skrll sym_hashes = elf_sym_hashes (input_bfd);
10407 1.3 skrll for (o = input_bfd->sections; o != NULL; o = o->next)
10408 1.3 skrll {
10409 1.3 skrll bfd_byte *contents;
10410 1.3 skrll
10411 1.3 skrll if (! o->linker_mark)
10412 1.3 skrll {
10413 1.13.12.2 pgoyette /* This section was omitted from the link. */
10414 1.3 skrll continue;
10415 1.3 skrll }
10416 1.7 christos
10417 1.3 skrll if (!flinfo->info->resolve_section_groups
10418 1.3 skrll && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10419 1.3 skrll {
10420 1.3 skrll /* Deal with the group signature symbol. */
10421 1.3 skrll struct bfd_elf_section_data *sec_data = elf_section_data (o);
10422 1.3 skrll unsigned long symndx = sec_data->this_hdr.sh_info;
10423 1.3 skrll asection *osec = o->output_section;
10424 1.3 skrll
10425 1.3 skrll BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10426 1.3 skrll if (symndx >= locsymcount
10427 1.3 skrll || (elf_bad_symtab (input_bfd)
10428 1.3 skrll && flinfo->sections[symndx] == NULL))
10429 1.7 christos {
10430 1.3 skrll struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10431 1.3 skrll while (h->root.type == bfd_link_hash_indirect
10432 1.3 skrll || h->root.type == bfd_link_hash_warning)
10433 1.3 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
10434 1.7 christos /* Arrange for symbol to be output. */
10435 1.3 skrll h->indx = -2;
10436 1.3 skrll elf_section_data (osec)->this_hdr.sh_info = -2;
10437 1.3 skrll }
10438 1.7 christos else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10439 1.3 skrll {
10440 1.4 christos /* We'll use the output section target_index. */
10441 1.4 christos asection *sec = flinfo->sections[symndx]->output_section;
10442 1.3 skrll elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10443 1.3 skrll }
10444 1.3 skrll else
10445 1.3 skrll {
10446 1.3 skrll if (flinfo->indices[symndx] == -1)
10447 1.3 skrll {
10448 1.3 skrll /* Otherwise output the local symbol now. */
10449 1.3 skrll Elf_Internal_Sym sym = isymbuf[symndx];
10450 1.3 skrll asection *sec = flinfo->sections[symndx]->output_section;
10451 1.3 skrll const char *name;
10452 1.3 skrll long indx;
10453 1.3 skrll int ret;
10454 1.3 skrll
10455 1.3 skrll name = bfd_elf_string_from_elf_section (input_bfd,
10456 1.4 christos symtab_hdr->sh_link,
10457 1.9 christos sym.st_name);
10458 1.9 christos if (name == NULL)
10459 1.4 christos return FALSE;
10460 1.3 skrll
10461 1.4 christos sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10462 1.7 christos sec);
10463 1.4 christos if (sym.st_shndx == SHN_BAD)
10464 1.4 christos return FALSE;
10465 1.3 skrll
10466 1.3 skrll sym.st_value += o->output_offset;
10467 1.7 christos
10468 1.3 skrll indx = bfd_get_symcount (output_bfd);
10469 1.3 skrll ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10470 1.3 skrll NULL);
10471 1.1 skrll if (ret == 0)
10472 1.1 skrll return FALSE;
10473 1.1 skrll else if (ret == 1)
10474 1.1 skrll flinfo->indices[symndx] = indx;
10475 1.1 skrll else
10476 1.1 skrll abort ();
10477 1.1 skrll }
10478 1.1 skrll elf_section_data (osec)->this_hdr.sh_info
10479 1.1 skrll = flinfo->indices[symndx];
10480 1.1 skrll }
10481 1.1 skrll }
10482 1.1 skrll
10483 1.1 skrll if ((o->flags & SEC_HAS_CONTENTS) == 0
10484 1.1 skrll || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10485 1.1 skrll continue;
10486 1.1 skrll
10487 1.9 christos if ((o->flags & SEC_LINKER_CREATED) != 0)
10488 1.9 christos {
10489 1.9 christos /* Section was created by _bfd_elf_link_create_dynamic_sections
10490 1.9 christos or somesuch. */
10491 1.9 christos continue;
10492 1.9 christos }
10493 1.9 christos
10494 1.9 christos /* Get the contents of the section. They have been cached by a
10495 1.9 christos relaxation routine. Note that o is a section in an input
10496 1.9 christos file, so the contents field will not have been set by any of
10497 1.1 skrll the routines which work on output files. */
10498 1.1 skrll if (elf_section_data (o)->this_hdr.contents != NULL)
10499 1.7 christos {
10500 1.4 christos contents = elf_section_data (o)->this_hdr.contents;
10501 1.1 skrll if (bed->caches_rawsize
10502 1.1 skrll && o->rawsize != 0
10503 1.1 skrll && o->rawsize < o->size)
10504 1.1 skrll {
10505 1.1 skrll memcpy (flinfo->contents, contents, o->rawsize);
10506 1.1 skrll contents = flinfo->contents;
10507 1.1 skrll }
10508 1.1 skrll }
10509 1.1 skrll else
10510 1.1 skrll {
10511 1.1 skrll contents = flinfo->contents;
10512 1.1 skrll if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10513 1.7 christos return FALSE;
10514 1.7 christos }
10515 1.1 skrll
10516 1.1 skrll if ((o->flags & SEC_RELOC) != 0)
10517 1.1 skrll {
10518 1.1 skrll Elf_Internal_Rela *internal_relocs;
10519 1.7 christos Elf_Internal_Rela *rel, *relend;
10520 1.7 christos int action_discarded;
10521 1.7 christos int ret;
10522 1.7 christos
10523 1.7 christos /* Get the swapped relocs. */
10524 1.7 christos internal_relocs
10525 1.7 christos = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10526 1.7 christos flinfo->internal_relocs, FALSE);
10527 1.7 christos if (internal_relocs == NULL
10528 1.7 christos && o->reloc_count > 0)
10529 1.1 skrll return FALSE;
10530 1.13.12.2 pgoyette
10531 1.13.12.2 pgoyette /* We need to reverse-copy input .ctors/.dtors sections if
10532 1.7 christos they are placed in .init_array/.finit_array for output. */
10533 1.13.12.2 pgoyette if (o->size > address_size
10534 1.13.12.2 pgoyette && ((strncmp (o->name, ".ctors", 6) == 0
10535 1.7 christos && strcmp (o->output_section->name,
10536 1.7 christos ".init_array") == 0)
10537 1.7 christos || (strncmp (o->name, ".dtors", 6) == 0
10538 1.13.12.2 pgoyette && strcmp (o->output_section->name,
10539 1.7 christos ".fini_array") == 0))
10540 1.7 christos && (o->name[6] == 0 || o->name[6] == '.'))
10541 1.7 christos {
10542 1.1 skrll if (o->size * bed->s->int_rels_per_ext_rel
10543 1.1 skrll != o->reloc_count * address_size)
10544 1.1 skrll {
10545 1.1 skrll _bfd_error_handler
10546 1.1 skrll /* xgettext:c-format */
10547 1.1 skrll (_("error: %B: size of section %A is not "
10548 1.1 skrll "multiple of address size"),
10549 1.1 skrll input_bfd, o);
10550 1.1 skrll bfd_set_error (bfd_error_bad_value);
10551 1.1 skrll return FALSE;
10552 1.1 skrll }
10553 1.1 skrll o->flags |= SEC_ELF_REVERSE_COPY;
10554 1.1 skrll }
10555 1.13.12.2 pgoyette
10556 1.1 skrll action_discarded = -1;
10557 1.1 skrll if (!elf_section_ignore_discarded_relocs (o))
10558 1.1 skrll action_discarded = (*bed->action_discarded) (o);
10559 1.1 skrll
10560 1.1 skrll /* Run through the relocs evaluating complex reloc symbols and
10561 1.1 skrll looking for relocs against symbols from discarded sections
10562 1.1 skrll or section symbols from removed link-once sections.
10563 1.1 skrll Complain about relocs against discarded sections. Zero
10564 1.1 skrll relocs against removed link-once sections. */
10565 1.1 skrll
10566 1.1 skrll rel = internal_relocs;
10567 1.1 skrll relend = rel + o->reloc_count;
10568 1.1 skrll for ( ; rel < relend; rel++)
10569 1.7 christos {
10570 1.1 skrll unsigned long r_symndx = rel->r_info >> r_sym_shift;
10571 1.1 skrll unsigned int s_type;
10572 1.1 skrll asection **ps, *sec;
10573 1.1 skrll struct elf_link_hash_entry *h = NULL;
10574 1.1 skrll const char *sym_name;
10575 1.1 skrll
10576 1.1 skrll if (r_symndx == STN_UNDEF)
10577 1.1 skrll continue;
10578 1.13.12.2 pgoyette
10579 1.13.12.2 pgoyette if (r_symndx >= locsymcount
10580 1.13.12.2 pgoyette || (elf_bad_symtab (input_bfd)
10581 1.1 skrll && flinfo->sections[r_symndx] == NULL))
10582 1.13.12.2 pgoyette {
10583 1.1 skrll h = sym_hashes[r_symndx - extsymoff];
10584 1.1 skrll
10585 1.1 skrll /* Badly formatted input files can contain relocs that
10586 1.1 skrll reference non-existant symbols. Check here so that
10587 1.1 skrll we do not seg fault. */
10588 1.1 skrll if (h == NULL)
10589 1.1 skrll {
10590 1.1 skrll _bfd_error_handler
10591 1.1 skrll /* xgettext:c-format */
10592 1.1 skrll (_("error: %B contains a reloc (%#Lx) for section %A "
10593 1.9 christos "that references a non-existent global symbol"),
10594 1.9 christos input_bfd, rel->r_info, o);
10595 1.9 christos bfd_set_error (bfd_error_bad_value);
10596 1.9 christos return FALSE;
10597 1.9 christos }
10598 1.13.12.2 pgoyette
10599 1.13.12.2 pgoyette while (h->root.type == bfd_link_hash_indirect
10600 1.9 christos || h->root.type == bfd_link_hash_warning)
10601 1.9 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
10602 1.9 christos
10603 1.9 christos s_type = h->type;
10604 1.9 christos
10605 1.9 christos /* If a plugin symbol is referenced from a non-IR file,
10606 1.9 christos mark the symbol as undefined. Note that the
10607 1.9 christos linker may attach linker created dynamic sections
10608 1.9 christos to the plugin bfd. Symbols defined in linker
10609 1.9 christos created sections are not plugin symbols. */
10610 1.9 christos if ((h->root.non_ir_ref_regular
10611 1.9 christos || h->root.non_ir_ref_dynamic)
10612 1.1 skrll && (h->root.type == bfd_link_hash_defined
10613 1.1 skrll || h->root.type == bfd_link_hash_defweak)
10614 1.1 skrll && (h->root.u.def.section->flags
10615 1.1 skrll & SEC_LINKER_CREATED) == 0
10616 1.1 skrll && h->root.u.def.section->owner != NULL
10617 1.1 skrll && (h->root.u.def.section->owner->flags
10618 1.1 skrll & BFD_PLUGIN) != 0)
10619 1.1 skrll {
10620 1.1 skrll h->root.type = bfd_link_hash_undefined;
10621 1.1 skrll h->root.u.undef.abfd = h->root.u.def.section->owner;
10622 1.1 skrll }
10623 1.1 skrll
10624 1.7 christos ps = NULL;
10625 1.1 skrll if (h->root.type == bfd_link_hash_defined
10626 1.1 skrll || h->root.type == bfd_link_hash_defweak)
10627 1.1 skrll ps = &h->root.u.def.section;
10628 1.1 skrll
10629 1.4 christos sym_name = h->root.root.string;
10630 1.9 christos }
10631 1.1 skrll else
10632 1.1 skrll {
10633 1.1 skrll Elf_Internal_Sym *sym = isymbuf + r_symndx;
10634 1.1 skrll
10635 1.1 skrll s_type = ELF_ST_TYPE (sym->st_info);
10636 1.1 skrll ps = &flinfo->sections[r_symndx];
10637 1.1 skrll sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10638 1.4 christos sym, *ps);
10639 1.4 christos }
10640 1.1 skrll
10641 1.1 skrll if ((s_type == STT_RELC || s_type == STT_SRELC)
10642 1.1 skrll && !bfd_link_relocatable (flinfo->info))
10643 1.1 skrll {
10644 1.1 skrll bfd_vma val;
10645 1.1 skrll bfd_vma dot = (rel->r_offset
10646 1.7 christos + o->output_offset + o->output_section->vma);
10647 1.1 skrll #ifdef DEBUG
10648 1.1 skrll printf ("Encountered a complex symbol!");
10649 1.1 skrll printf (" (input_bfd %s, section %s, reloc %ld\n",
10650 1.1 skrll input_bfd->filename, o->name,
10651 1.1 skrll (long) (rel - internal_relocs));
10652 1.1 skrll printf (" symbol: idx %8.8lx, name %s\n",
10653 1.1 skrll r_symndx, sym_name);
10654 1.1 skrll printf (" reloc : info %8.8lx, addr %8.8lx\n",
10655 1.1 skrll (unsigned long) rel->r_info,
10656 1.1 skrll (unsigned long) rel->r_offset);
10657 1.1 skrll #endif
10658 1.1 skrll if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10659 1.1 skrll isymbuf, locsymcount, s_type == STT_SRELC))
10660 1.7 christos return FALSE;
10661 1.1 skrll
10662 1.4 christos /* Symbol evaluated OK. Update to absolute value. */
10663 1.1 skrll set_symbol_value (input_bfd, isymbuf, locsymcount,
10664 1.7 christos r_symndx, val);
10665 1.13.12.2 pgoyette continue;
10666 1.1 skrll }
10667 1.1 skrll
10668 1.1 skrll if (action_discarded != -1 && ps != NULL)
10669 1.1 skrll {
10670 1.1 skrll /* Complain if the definition comes from a
10671 1.1 skrll discarded section. */
10672 1.1 skrll if ((sec = *ps) != NULL && discarded_section (sec))
10673 1.1 skrll {
10674 1.1 skrll BFD_ASSERT (r_symndx != STN_UNDEF);
10675 1.1 skrll if (action_discarded & COMPLAIN)
10676 1.1 skrll (*flinfo->info->callbacks->einfo)
10677 1.1 skrll /* xgettext:c-format */
10678 1.1 skrll (_("%X`%s' referenced in section `%A' of %B: "
10679 1.1 skrll "defined in discarded section `%A' of %B\n"),
10680 1.1 skrll sym_name, o, input_bfd, sec, sec->owner);
10681 1.7 christos
10682 1.1 skrll /* Try to do the best we can to support buggy old
10683 1.1 skrll versions of gcc. Pretend that the symbol is
10684 1.1 skrll really defined in the kept linkonce section.
10685 1.1 skrll FIXME: This is quite broken. Modifying the
10686 1.1 skrll symbol here means we will be changing all later
10687 1.1 skrll uses of the symbol, not just in this section. */
10688 1.1 skrll if (action_discarded & PRETEND)
10689 1.1 skrll {
10690 1.1 skrll asection *kept;
10691 1.1 skrll
10692 1.1 skrll kept = _bfd_elf_check_kept_section (sec,
10693 1.1 skrll flinfo->info);
10694 1.1 skrll if (kept != NULL)
10695 1.1 skrll {
10696 1.1 skrll *ps = kept;
10697 1.1 skrll continue;
10698 1.1 skrll }
10699 1.1 skrll }
10700 1.1 skrll }
10701 1.1 skrll }
10702 1.1 skrll }
10703 1.1 skrll
10704 1.1 skrll /* Relocate the section by invoking a back end routine.
10705 1.1 skrll
10706 1.1 skrll The back end routine is responsible for adjusting the
10707 1.1 skrll section contents as necessary, and (if using Rela relocs
10708 1.1 skrll and generating a relocatable output file) adjusting the
10709 1.1 skrll reloc addend as necessary.
10710 1.1 skrll
10711 1.1 skrll The back end routine does not have to worry about setting
10712 1.7 christos the reloc address or the reloc symbol index.
10713 1.1 skrll
10714 1.1 skrll The back end routine is given a pointer to the swapped in
10715 1.1 skrll internal symbols, and can access the hash table entries
10716 1.7 christos for the external symbols via elf_sym_hashes (input_bfd).
10717 1.1 skrll
10718 1.1 skrll When generating relocatable output, the back end routine
10719 1.1 skrll must handle STB_LOCAL/STT_SECTION symbols specially. The
10720 1.1 skrll output symbol is going to be a section symbol
10721 1.9 christos corresponding to the output section, which will require
10722 1.7 christos the addend to be adjusted. */
10723 1.1 skrll
10724 1.1 skrll ret = (*relocate_section) (output_bfd, flinfo->info,
10725 1.4 christos input_bfd, o, contents,
10726 1.1 skrll internal_relocs,
10727 1.1 skrll isymbuf,
10728 1.4 christos flinfo->sections);
10729 1.4 christos if (!ret)
10730 1.1 skrll return FALSE;
10731 1.1 skrll
10732 1.4 christos if (ret == 2
10733 1.1 skrll || bfd_link_relocatable (flinfo->info)
10734 1.4 christos || flinfo->info->emitrelocations)
10735 1.4 christos {
10736 1.4 christos Elf_Internal_Rela *irela;
10737 1.1 skrll Elf_Internal_Rela *irelaend, *irelamid;
10738 1.1 skrll bfd_vma last_offset;
10739 1.1 skrll struct elf_link_hash_entry **rel_hash;
10740 1.1 skrll struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10741 1.13.12.2 pgoyette Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10742 1.4 christos unsigned int next_erel;
10743 1.4 christos bfd_boolean rela_normal;
10744 1.4 christos struct bfd_elf_section_data *esdi, *esdo;
10745 1.4 christos
10746 1.4 christos esdi = elf_section_data (o);
10747 1.4 christos esdo = elf_section_data (o->output_section);
10748 1.4 christos rela_normal = FALSE;
10749 1.1 skrll
10750 1.4 christos /* Adjust the reloc addresses and symbol indices. */
10751 1.1 skrll
10752 1.9 christos irela = internal_relocs;
10753 1.1 skrll irelaend = irela + o->reloc_count;
10754 1.1 skrll rel_hash = esdo->rel.hashes + esdo->rel.count;
10755 1.1 skrll /* We start processing the REL relocs, if any. When we reach
10756 1.1 skrll IRELAMID in the loop, we switch to the RELA relocs. */
10757 1.1 skrll irelamid = irela;
10758 1.1 skrll if (esdi->rel.hdr != NULL)
10759 1.1 skrll irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10760 1.1 skrll * bed->s->int_rels_per_ext_rel);
10761 1.1 skrll rel_hash_list = rel_hash;
10762 1.1 skrll rela_hash_list = NULL;
10763 1.1 skrll last_offset = o->output_offset;
10764 1.1 skrll if (!bfd_link_relocatable (flinfo->info))
10765 1.1 skrll last_offset += o->output_section->vma;
10766 1.4 christos for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10767 1.4 christos {
10768 1.4 christos unsigned long r_symndx;
10769 1.4 christos asection *sec;
10770 1.4 christos Elf_Internal_Sym sym;
10771 1.4 christos
10772 1.4 christos if (next_erel == bed->s->int_rels_per_ext_rel)
10773 1.1 skrll {
10774 1.7 christos rel_hash++;
10775 1.1 skrll next_erel = 0;
10776 1.1 skrll }
10777 1.1 skrll
10778 1.1 skrll if (irela == irelamid)
10779 1.1 skrll {
10780 1.1 skrll rel_hash = esdo->rela.hashes + esdo->rela.count;
10781 1.1 skrll rela_hash_list = rel_hash;
10782 1.1 skrll rela_normal = bed->rela_normal;
10783 1.1 skrll }
10784 1.1 skrll
10785 1.1 skrll irela->r_offset = _bfd_elf_section_offset (output_bfd,
10786 1.1 skrll flinfo->info, o,
10787 1.1 skrll irela->r_offset);
10788 1.1 skrll if (irela->r_offset >= (bfd_vma) -2)
10789 1.1 skrll {
10790 1.1 skrll /* This is a reloc for a deleted entry or somesuch.
10791 1.1 skrll Turn it into an R_*_NONE reloc, at the same
10792 1.9 christos offset as the last reloc. elf_eh_frame.c and
10793 1.1 skrll bfd_elf_discard_info rely on reloc offsets
10794 1.1 skrll being ordered. */
10795 1.1 skrll irela->r_offset = last_offset;
10796 1.1 skrll irela->r_info = 0;
10797 1.1 skrll irela->r_addend = 0;
10798 1.1 skrll continue;
10799 1.1 skrll }
10800 1.1 skrll
10801 1.1 skrll irela->r_offset += o->output_offset;
10802 1.1 skrll
10803 1.7 christos /* Relocs in an executable have to be virtual addresses. */
10804 1.1 skrll if (!bfd_link_relocatable (flinfo->info))
10805 1.1 skrll irela->r_offset += o->output_section->vma;
10806 1.1 skrll
10807 1.1 skrll last_offset = irela->r_offset;
10808 1.1 skrll
10809 1.1 skrll r_symndx = irela->r_info >> r_sym_shift;
10810 1.1 skrll if (r_symndx == STN_UNDEF)
10811 1.1 skrll continue;
10812 1.1 skrll
10813 1.1 skrll if (r_symndx >= locsymcount
10814 1.1 skrll || (elf_bad_symtab (input_bfd)
10815 1.1 skrll && flinfo->sections[r_symndx] == NULL))
10816 1.1 skrll {
10817 1.1 skrll struct elf_link_hash_entry *rh;
10818 1.1 skrll unsigned long indx;
10819 1.1 skrll
10820 1.1 skrll /* This is a reloc against a global symbol. We
10821 1.1 skrll have not yet output all the local symbols, so
10822 1.1 skrll we do not know the symbol index of any global
10823 1.1 skrll symbol. We set the rel_hash entry for this
10824 1.1 skrll reloc to point to the global hash table entry
10825 1.1 skrll for this symbol. The symbol index is then
10826 1.1 skrll set at the end of bfd_elf_final_link. */
10827 1.1 skrll indx = r_symndx - extsymoff;
10828 1.1 skrll rh = elf_sym_hashes (input_bfd)[indx];
10829 1.1 skrll while (rh->root.type == bfd_link_hash_indirect
10830 1.1 skrll || rh->root.type == bfd_link_hash_warning)
10831 1.1 skrll rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10832 1.1 skrll
10833 1.1 skrll /* Setting the index to -2 tells
10834 1.1 skrll elf_link_output_extsym that this symbol is
10835 1.7 christos used by a reloc. */
10836 1.1 skrll BFD_ASSERT (rh->indx < 0);
10837 1.1 skrll rh->indx = -2;
10838 1.1 skrll *rel_hash = rh;
10839 1.1 skrll
10840 1.1 skrll continue;
10841 1.4 christos }
10842 1.1 skrll
10843 1.1 skrll /* This is a reloc against a local symbol. */
10844 1.1 skrll
10845 1.1 skrll *rel_hash = NULL;
10846 1.1 skrll sym = isymbuf[r_symndx];
10847 1.1 skrll sec = flinfo->sections[r_symndx];
10848 1.1 skrll if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10849 1.1 skrll {
10850 1.1 skrll /* I suppose the backend ought to fill in the
10851 1.1 skrll section of any STT_SECTION symbol against a
10852 1.1 skrll processor specific section. */
10853 1.1 skrll r_symndx = STN_UNDEF;
10854 1.1 skrll if (bfd_is_abs_section (sec))
10855 1.1 skrll ;
10856 1.1 skrll else if (sec == NULL || sec->owner == NULL)
10857 1.1 skrll {
10858 1.1 skrll bfd_set_error (bfd_error_bad_value);
10859 1.1 skrll return FALSE;
10860 1.1 skrll }
10861 1.1 skrll else
10862 1.1 skrll {
10863 1.1 skrll asection *osec = sec->output_section;
10864 1.1 skrll
10865 1.1 skrll /* If we have discarded a section, the output
10866 1.1 skrll section will be the absolute section. In
10867 1.1 skrll case of discarded SEC_MERGE sections, use
10868 1.1 skrll the kept section. relocate_section should
10869 1.1 skrll have already handled discarded linkonce
10870 1.4 christos sections. */
10871 1.1 skrll if (bfd_is_abs_section (osec)
10872 1.7 christos && sec->kept_section != NULL
10873 1.7 christos && sec->kept_section->output_section != NULL)
10874 1.7 christos {
10875 1.7 christos osec = sec->kept_section->output_section;
10876 1.7 christos irela->r_addend -= osec->vma;
10877 1.1 skrll }
10878 1.1 skrll
10879 1.1 skrll if (!bfd_is_abs_section (osec))
10880 1.1 skrll {
10881 1.1 skrll r_symndx = osec->target_index;
10882 1.1 skrll if (r_symndx == STN_UNDEF)
10883 1.1 skrll {
10884 1.1 skrll irela->r_addend += osec->vma;
10885 1.1 skrll osec = _bfd_nearby_section (output_bfd, osec,
10886 1.1 skrll osec->vma);
10887 1.1 skrll irela->r_addend -= osec->vma;
10888 1.7 christos r_symndx = osec->target_index;
10889 1.1 skrll }
10890 1.1 skrll }
10891 1.1 skrll }
10892 1.1 skrll
10893 1.4 christos /* Adjust the addend according to where the
10894 1.1 skrll section winds up in the output section. */
10895 1.7 christos if (rela_normal)
10896 1.1 skrll irela->r_addend += sec->output_offset;
10897 1.1 skrll }
10898 1.1 skrll else
10899 1.1 skrll {
10900 1.1 skrll if (flinfo->indices[r_symndx] == -1)
10901 1.1 skrll {
10902 1.1 skrll unsigned long shlink;
10903 1.1 skrll const char *name;
10904 1.1 skrll asection *osec;
10905 1.1 skrll long indx;
10906 1.1 skrll
10907 1.1 skrll if (flinfo->info->strip == strip_all)
10908 1.1 skrll {
10909 1.1 skrll /* You can't do ld -r -s. */
10910 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
10911 1.1 skrll return FALSE;
10912 1.1 skrll }
10913 1.1 skrll
10914 1.1 skrll /* This symbol was skipped earlier, but
10915 1.1 skrll since it is needed by a reloc, we
10916 1.1 skrll must output it now. */
10917 1.1 skrll shlink = symtab_hdr->sh_link;
10918 1.1 skrll name = (bfd_elf_string_from_elf_section
10919 1.9 christos (input_bfd, shlink, sym.st_name));
10920 1.1 skrll if (name == NULL)
10921 1.1 skrll return FALSE;
10922 1.1 skrll
10923 1.1 skrll osec = sec->output_section;
10924 1.1 skrll sym.st_shndx =
10925 1.1 skrll _bfd_elf_section_from_bfd_section (output_bfd,
10926 1.7 christos osec);
10927 1.1 skrll if (sym.st_shndx == SHN_BAD)
10928 1.7 christos return FALSE;
10929 1.1 skrll
10930 1.1 skrll sym.st_value += sec->output_offset;
10931 1.1 skrll if (!bfd_link_relocatable (flinfo->info))
10932 1.1 skrll {
10933 1.4 christos sym.st_value += osec->vma;
10934 1.9 christos if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10935 1.9 christos {
10936 1.9 christos /* STT_TLS symbols are relative to PT_TLS
10937 1.4 christos segment base. */
10938 1.1 skrll BFD_ASSERT (elf_hash_table (flinfo->info)
10939 1.4 christos ->tls_sec != NULL);
10940 1.7 christos sym.st_value -= (elf_hash_table (flinfo->info)
10941 1.4 christos ->tls_sec->vma);
10942 1.4 christos }
10943 1.1 skrll }
10944 1.1 skrll
10945 1.7 christos indx = bfd_get_symcount (output_bfd);
10946 1.1 skrll ret = elf_link_output_symstrtab (flinfo, name,
10947 1.1 skrll &sym, sec,
10948 1.1 skrll NULL);
10949 1.1 skrll if (ret == 0)
10950 1.1 skrll return FALSE;
10951 1.1 skrll else if (ret == 1)
10952 1.1 skrll flinfo->indices[r_symndx] = indx;
10953 1.4 christos else
10954 1.4 christos abort ();
10955 1.1 skrll }
10956 1.4 christos
10957 1.4 christos r_symndx = flinfo->indices[r_symndx];
10958 1.4 christos }
10959 1.4 christos
10960 1.4 christos irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10961 1.1 skrll | (irela->r_info & r_type_mask));
10962 1.1 skrll }
10963 1.1 skrll
10964 1.4 christos /* Swap out the relocs. */
10965 1.4 christos input_rel_hdr = esdi->rel.hdr;
10966 1.4 christos if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10967 1.4 christos {
10968 1.4 christos if (!bed->elf_backend_emit_relocs (output_bfd, o,
10969 1.1 skrll input_rel_hdr,
10970 1.4 christos internal_relocs,
10971 1.1 skrll rel_hash_list))
10972 1.4 christos return FALSE;
10973 1.1 skrll internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10974 1.1 skrll * bed->s->int_rels_per_ext_rel);
10975 1.1 skrll rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10976 1.1 skrll }
10977 1.1 skrll
10978 1.1 skrll input_rela_hdr = esdi->rela.hdr;
10979 1.1 skrll if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10980 1.7 christos {
10981 1.1 skrll if (!bed->elf_backend_emit_relocs (output_bfd, o,
10982 1.1 skrll input_rela_hdr,
10983 1.1 skrll internal_relocs,
10984 1.1 skrll rela_hash_list))
10985 1.1 skrll return FALSE;
10986 1.1 skrll }
10987 1.7 christos }
10988 1.1 skrll }
10989 1.1 skrll
10990 1.7 christos /* Write out the modified section contents. */
10991 1.1 skrll if (bed->elf_backend_write_section
10992 1.1 skrll && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10993 1.1 skrll contents))
10994 1.7 christos {
10995 1.1 skrll /* Section written out. */
10996 1.1 skrll }
10997 1.1 skrll else switch (o->sec_info_type)
10998 1.1 skrll {
10999 1.7 christos case SEC_INFO_TYPE_STABS:
11000 1.1 skrll if (! (_bfd_write_section_stabs
11001 1.7 christos (output_bfd,
11002 1.1 skrll &elf_hash_table (flinfo->info)->stab_info,
11003 1.1 skrll o, &elf_section_data (o)->sec_info, contents)))
11004 1.1 skrll return FALSE;
11005 1.1 skrll break;
11006 1.9 christos case SEC_INFO_TYPE_MERGE:
11007 1.9 christos if (! _bfd_write_merged_section (output_bfd, o,
11008 1.9 christos elf_section_data (o)->sec_info))
11009 1.9 christos return FALSE;
11010 1.9 christos break;
11011 1.9 christos case SEC_INFO_TYPE_EH_FRAME:
11012 1.9 christos {
11013 1.9 christos if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11014 1.1 skrll o, contents))
11015 1.1 skrll return FALSE;
11016 1.7 christos }
11017 1.7 christos break;
11018 1.7 christos case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11019 1.7 christos {
11020 1.13 christos if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11021 1.13 christos flinfo->info,
11022 1.13 christos o, contents))
11023 1.7 christos return FALSE;
11024 1.7 christos }
11025 1.7 christos break;
11026 1.7 christos default:
11027 1.7 christos {
11028 1.7 christos if (! (o->flags & SEC_EXCLUDE))
11029 1.7 christos {
11030 1.7 christos file_ptr offset = (file_ptr) o->output_offset;
11031 1.7 christos bfd_size_type todo = o->size;
11032 1.7 christos
11033 1.7 christos offset *= bfd_octets_per_byte (output_bfd);
11034 1.7 christos
11035 1.7 christos if ((o->flags & SEC_ELF_REVERSE_COPY))
11036 1.7 christos {
11037 1.7 christos /* Reverse-copy input section to output. */
11038 1.7 christos do
11039 1.7 christos {
11040 1.7 christos todo -= address_size;
11041 1.7 christos if (! bfd_set_section_contents (output_bfd,
11042 1.7 christos o->output_section,
11043 1.7 christos contents + todo,
11044 1.7 christos offset,
11045 1.7 christos address_size))
11046 1.7 christos return FALSE;
11047 1.1 skrll if (todo == 0)
11048 1.1 skrll break;
11049 1.1 skrll offset += address_size;
11050 1.1 skrll }
11051 1.1 skrll while (1);
11052 1.1 skrll }
11053 1.1 skrll else if (! bfd_set_section_contents (output_bfd,
11054 1.1 skrll o->output_section,
11055 1.1 skrll contents,
11056 1.1 skrll offset, todo))
11057 1.1 skrll return FALSE;
11058 1.1 skrll }
11059 1.1 skrll }
11060 1.1 skrll break;
11061 1.1 skrll }
11062 1.1 skrll }
11063 1.1 skrll
11064 1.1 skrll return TRUE;
11065 1.1 skrll }
11066 1.1 skrll
11067 1.1 skrll /* Generate a reloc when linking an ELF file. This is a reloc
11068 1.1 skrll requested by the linker, and does not come from any input file. This
11069 1.1 skrll is used to build constructor and destructor tables when linking
11070 1.4 christos with -Ur. */
11071 1.1 skrll
11072 1.1 skrll static bfd_boolean
11073 1.1 skrll elf_reloc_link_order (bfd *output_bfd,
11074 1.1 skrll struct bfd_link_info *info,
11075 1.1 skrll asection *output_section,
11076 1.1 skrll struct bfd_link_order *link_order)
11077 1.4 christos {
11078 1.1 skrll reloc_howto_type *howto;
11079 1.1 skrll long indx;
11080 1.1 skrll bfd_vma offset;
11081 1.1 skrll bfd_vma addend;
11082 1.1 skrll struct bfd_elf_section_reloc_data *reldata;
11083 1.1 skrll struct elf_link_hash_entry **rel_hash_ptr;
11084 1.1 skrll Elf_Internal_Shdr *rel_hdr;
11085 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11086 1.1 skrll Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11087 1.1 skrll bfd_byte *erel;
11088 1.4 christos unsigned int i;
11089 1.4 christos struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11090 1.4 christos
11091 1.4 christos howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11092 1.4 christos if (howto == NULL)
11093 1.4 christos {
11094 1.4 christos bfd_set_error (bfd_error_bad_value);
11095 1.4 christos return FALSE;
11096 1.4 christos }
11097 1.4 christos
11098 1.1 skrll addend = link_order->u.reloc.p->addend;
11099 1.4 christos
11100 1.1 skrll if (esdo->rel.hdr)
11101 1.1 skrll reldata = &esdo->rel;
11102 1.1 skrll else if (esdo->rela.hdr)
11103 1.1 skrll reldata = &esdo->rela;
11104 1.1 skrll else
11105 1.1 skrll {
11106 1.1 skrll reldata = NULL;
11107 1.1 skrll BFD_ASSERT (0);
11108 1.1 skrll }
11109 1.1 skrll
11110 1.1 skrll /* Figure out the symbol index. */
11111 1.1 skrll rel_hash_ptr = reldata->hashes + reldata->count;
11112 1.1 skrll if (link_order->type == bfd_section_reloc_link_order)
11113 1.1 skrll {
11114 1.1 skrll indx = link_order->u.reloc.p->u.section->target_index;
11115 1.1 skrll BFD_ASSERT (indx != 0);
11116 1.1 skrll *rel_hash_ptr = NULL;
11117 1.1 skrll }
11118 1.1 skrll else
11119 1.1 skrll {
11120 1.1 skrll struct elf_link_hash_entry *h;
11121 1.1 skrll
11122 1.1 skrll /* Treat a reloc against a defined symbol as though it were
11123 1.1 skrll actually against the section. */
11124 1.1 skrll h = ((struct elf_link_hash_entry *)
11125 1.1 skrll bfd_wrapped_link_hash_lookup (output_bfd, info,
11126 1.1 skrll link_order->u.reloc.p->u.name,
11127 1.1 skrll FALSE, FALSE, TRUE));
11128 1.1 skrll if (h != NULL
11129 1.1 skrll && (h->root.type == bfd_link_hash_defined
11130 1.1 skrll || h->root.type == bfd_link_hash_defweak))
11131 1.1 skrll {
11132 1.1 skrll asection *section;
11133 1.1 skrll
11134 1.1 skrll section = h->root.u.def.section;
11135 1.1 skrll indx = section->output_section->target_index;
11136 1.1 skrll *rel_hash_ptr = NULL;
11137 1.1 skrll /* It seems that we ought to add the symbol value to the
11138 1.1 skrll addend here, but in practice it has already been added
11139 1.1 skrll because it was passed to constructor_callback. */
11140 1.13 christos addend += section->output_section->vma + section->output_offset;
11141 1.13 christos }
11142 1.1 skrll else if (h != NULL)
11143 1.1 skrll {
11144 1.1 skrll /* Setting the index to -2 tells elf_link_output_extsym that
11145 1.1 skrll this symbol is used by a reloc. */
11146 1.1 skrll h->indx = -2;
11147 1.1 skrll *rel_hash_ptr = h;
11148 1.1 skrll indx = 0;
11149 1.1 skrll }
11150 1.1 skrll else
11151 1.1 skrll {
11152 1.1 skrll (*info->callbacks->unattached_reloc)
11153 1.1 skrll (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11154 1.1 skrll indx = 0;
11155 1.1 skrll }
11156 1.4 christos }
11157 1.4 christos
11158 1.9 christos /* If this is an inplace reloc, we must write the addend into the
11159 1.1 skrll object file. */
11160 1.1 skrll if (howto->partial_inplace && addend != 0)
11161 1.1 skrll {
11162 1.1 skrll bfd_size_type size;
11163 1.1 skrll bfd_reloc_status_type rstat;
11164 1.1 skrll bfd_byte *buf;
11165 1.1 skrll bfd_boolean ok;
11166 1.1 skrll const char *sym_name;
11167 1.1 skrll
11168 1.1 skrll size = (bfd_size_type) bfd_get_reloc_size (howto);
11169 1.1 skrll buf = (bfd_byte *) bfd_zmalloc (size);
11170 1.1 skrll if (buf == NULL && size != 0)
11171 1.1 skrll return FALSE;
11172 1.1 skrll rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11173 1.1 skrll switch (rstat)
11174 1.1 skrll {
11175 1.1 skrll case bfd_reloc_ok:
11176 1.13 christos break;
11177 1.13 christos
11178 1.13 christos default:
11179 1.1 skrll case bfd_reloc_outofrange:
11180 1.1 skrll abort ();
11181 1.13 christos
11182 1.1 skrll case bfd_reloc_overflow:
11183 1.13 christos if (link_order->type == bfd_section_reloc_link_order)
11184 1.13 christos sym_name = bfd_section_name (output_bfd,
11185 1.13 christos link_order->u.reloc.p->u.section);
11186 1.1 skrll else
11187 1.1 skrll sym_name = link_order->u.reloc.p->u.name;
11188 1.1 skrll (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11189 1.1 skrll howto->name, addend, NULL, NULL,
11190 1.1 skrll (bfd_vma) 0);
11191 1.1 skrll break;
11192 1.1 skrll }
11193 1.1 skrll
11194 1.1 skrll ok = bfd_set_section_contents (output_bfd, output_section, buf,
11195 1.9 christos link_order->offset
11196 1.1 skrll * bfd_octets_per_byte (output_bfd),
11197 1.1 skrll size);
11198 1.1 skrll free (buf);
11199 1.1 skrll if (! ok)
11200 1.1 skrll return FALSE;
11201 1.1 skrll }
11202 1.1 skrll
11203 1.1 skrll /* The address of a reloc is relative to the section in a
11204 1.1 skrll relocatable file, and is a virtual address in an executable
11205 1.1 skrll file. */
11206 1.1 skrll offset = link_order->offset;
11207 1.2 skrll if (! bfd_link_relocatable (info))
11208 1.2 skrll offset += output_section->vma;
11209 1.2 skrll
11210 1.2 skrll for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11211 1.2 skrll {
11212 1.2 skrll irel[i].r_offset = offset;
11213 1.2 skrll irel[i].r_info = 0;
11214 1.2 skrll irel[i].r_addend = 0;
11215 1.1 skrll }
11216 1.4 christos if (bed->s->arch_size == 32)
11217 1.1 skrll irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11218 1.1 skrll else
11219 1.1 skrll #ifdef BFD64
11220 1.4 christos {
11221 1.1 skrll bfd_uint64_t indx64 = indx;
11222 1.1 skrll irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
11223 1.1 skrll }
11224 1.1 skrll #else
11225 1.1 skrll BFD_FAIL();
11226 1.4 christos #endif
11227 1.1 skrll
11228 1.1 skrll rel_hdr = reldata->hdr;
11229 1.1 skrll erel = rel_hdr->contents;
11230 1.4 christos if (rel_hdr->sh_type == SHT_REL)
11231 1.1 skrll {
11232 1.1 skrll erel += reldata->count * bed->s->sizeof_rel;
11233 1.1 skrll (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11234 1.1 skrll }
11235 1.1 skrll else
11236 1.1 skrll {
11237 1.1 skrll irel[0].r_addend = addend;
11238 1.1 skrll erel += reldata->count * bed->s->sizeof_rela;
11239 1.1 skrll (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11240 1.1 skrll }
11241 1.1 skrll
11242 1.1 skrll ++reldata->count;
11243 1.1 skrll
11244 1.1 skrll return TRUE;
11245 1.1 skrll }
11246 1.1 skrll
11247 1.1 skrll
11248 1.1 skrll /* Get the output vma of the section pointed to by the sh_link field. */
11249 1.1 skrll
11250 1.1 skrll static bfd_vma
11251 1.1 skrll elf_get_linked_section_vma (struct bfd_link_order *p)
11252 1.1 skrll {
11253 1.1 skrll Elf_Internal_Shdr **elf_shdrp;
11254 1.1 skrll asection *s;
11255 1.1 skrll int elfsec;
11256 1.1 skrll
11257 1.1 skrll s = p->u.indirect.section;
11258 1.1 skrll elf_shdrp = elf_elfsections (s->owner);
11259 1.1 skrll elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11260 1.13.12.2 pgoyette elfsec = elf_shdrp[elfsec]->sh_link;
11261 1.1 skrll /* PR 290:
11262 1.1 skrll The Intel C compiler generates SHT_IA_64_UNWIND with
11263 1.1 skrll SHF_LINK_ORDER. But it doesn't set the sh_link or
11264 1.1 skrll sh_info fields. Hence we could get the situation
11265 1.1 skrll where elfsec is 0. */
11266 1.1 skrll if (elfsec == 0)
11267 1.1 skrll {
11268 1.1 skrll const struct elf_backend_data *bed
11269 1.1 skrll = get_elf_backend_data (s->owner);
11270 1.1 skrll if (bed->link_order_error_handler)
11271 1.1 skrll bed->link_order_error_handler
11272 1.1 skrll /* xgettext:c-format */
11273 1.1 skrll (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11274 1.1 skrll return 0;
11275 1.1 skrll }
11276 1.1 skrll else
11277 1.1 skrll {
11278 1.1 skrll s = elf_shdrp[elfsec]->bfd_section;
11279 1.1 skrll return s->output_section->vma + s->output_offset;
11280 1.1 skrll }
11281 1.1 skrll }
11282 1.1 skrll
11283 1.1 skrll
11284 1.1 skrll /* Compare two sections based on the locations of the sections they are
11285 1.1 skrll linked to. Used by elf_fixup_link_order. */
11286 1.1 skrll
11287 1.1 skrll static int
11288 1.1 skrll compare_link_order (const void * a, const void * b)
11289 1.1 skrll {
11290 1.1 skrll bfd_vma apos;
11291 1.1 skrll bfd_vma bpos;
11292 1.1 skrll
11293 1.1 skrll apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11294 1.1 skrll bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11295 1.1 skrll if (apos < bpos)
11296 1.1 skrll return -1;
11297 1.1 skrll return apos > bpos;
11298 1.1 skrll }
11299 1.1 skrll
11300 1.1 skrll
11301 1.1 skrll /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11302 1.1 skrll order as their linked sections. Returns false if this could not be done
11303 1.1 skrll because an output section includes both ordered and unordered
11304 1.1 skrll sections. Ideally we'd do this in the linker proper. */
11305 1.1 skrll
11306 1.1 skrll static bfd_boolean
11307 1.1 skrll elf_fixup_link_order (bfd *abfd, asection *o)
11308 1.1 skrll {
11309 1.1 skrll int seen_linkorder;
11310 1.1 skrll int seen_other;
11311 1.1 skrll int n;
11312 1.1 skrll struct bfd_link_order *p;
11313 1.1 skrll bfd *sub;
11314 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11315 1.1 skrll unsigned elfsec;
11316 1.1 skrll struct bfd_link_order **sections;
11317 1.1 skrll asection *s, *other_sec, *linkorder_sec;
11318 1.1 skrll bfd_vma offset;
11319 1.1 skrll
11320 1.1 skrll other_sec = NULL;
11321 1.1 skrll linkorder_sec = NULL;
11322 1.1 skrll seen_other = 0;
11323 1.1 skrll seen_linkorder = 0;
11324 1.1 skrll for (p = o->map_head.link_order; p != NULL; p = p->next)
11325 1.1 skrll {
11326 1.1 skrll if (p->type == bfd_indirect_link_order)
11327 1.1 skrll {
11328 1.1 skrll s = p->u.indirect.section;
11329 1.1 skrll sub = s->owner;
11330 1.1 skrll if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11331 1.1 skrll && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11332 1.1 skrll && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11333 1.1 skrll && elfsec < elf_numsections (sub)
11334 1.1 skrll && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11335 1.1 skrll && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11336 1.1 skrll {
11337 1.1 skrll seen_linkorder++;
11338 1.1 skrll linkorder_sec = s;
11339 1.1 skrll }
11340 1.13.12.2 pgoyette else
11341 1.13.12.2 pgoyette {
11342 1.13.12.2 pgoyette seen_other++;
11343 1.13.12.2 pgoyette other_sec = s;
11344 1.13.12.2 pgoyette }
11345 1.13.12.2 pgoyette }
11346 1.1 skrll else
11347 1.13.12.2 pgoyette seen_other++;
11348 1.13.12.2 pgoyette
11349 1.1 skrll if (seen_other && seen_linkorder)
11350 1.1 skrll {
11351 1.1 skrll if (other_sec && linkorder_sec)
11352 1.1 skrll _bfd_error_handler
11353 1.1 skrll /* xgettext:c-format */
11354 1.1 skrll (_("%A has both ordered [`%A' in %B] "
11355 1.1 skrll "and unordered [`%A' in %B] sections"),
11356 1.1 skrll o, linkorder_sec, linkorder_sec->owner,
11357 1.1 skrll other_sec, other_sec->owner);
11358 1.1 skrll else
11359 1.1 skrll _bfd_error_handler
11360 1.1 skrll (_("%A has both ordered and unordered sections"), o);
11361 1.1 skrll bfd_set_error (bfd_error_bad_value);
11362 1.1 skrll return FALSE;
11363 1.1 skrll }
11364 1.1 skrll }
11365 1.1 skrll
11366 1.1 skrll if (!seen_linkorder)
11367 1.1 skrll return TRUE;
11368 1.1 skrll
11369 1.1 skrll sections = (struct bfd_link_order **)
11370 1.1 skrll bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11371 1.1 skrll if (sections == NULL)
11372 1.1 skrll return FALSE;
11373 1.1 skrll seen_linkorder = 0;
11374 1.1 skrll
11375 1.1 skrll for (p = o->map_head.link_order; p != NULL; p = p->next)
11376 1.1 skrll {
11377 1.13 christos sections[seen_linkorder++] = p;
11378 1.1 skrll }
11379 1.1 skrll /* Sort the input sections in the order of their linked section. */
11380 1.1 skrll qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11381 1.1 skrll compare_link_order);
11382 1.1 skrll
11383 1.1 skrll /* Change the offsets of the sections. */
11384 1.1 skrll offset = 0;
11385 1.1 skrll for (n = 0; n < seen_linkorder; n++)
11386 1.13.12.2 pgoyette {
11387 1.13.12.2 pgoyette s = sections[n]->u.indirect.section;
11388 1.13.12.2 pgoyette offset &= ~(bfd_vma) 0 << s->alignment_power;
11389 1.13.12.2 pgoyette s->output_offset = offset / bfd_octets_per_byte (abfd);
11390 1.13.12.2 pgoyette sections[n]->offset = offset;
11391 1.13.12.2 pgoyette offset += sections[n]->size;
11392 1.13.12.2 pgoyette }
11393 1.13.12.2 pgoyette
11394 1.13.12.2 pgoyette free (sections);
11395 1.13.12.2 pgoyette return TRUE;
11396 1.13.12.2 pgoyette }
11397 1.13.12.2 pgoyette
11398 1.13.12.2 pgoyette /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11399 1.13.12.2 pgoyette Returns TRUE upon success, FALSE otherwise. */
11400 1.13.12.2 pgoyette
11401 1.13.12.2 pgoyette static bfd_boolean
11402 1.13.12.2 pgoyette elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11403 1.13.12.2 pgoyette {
11404 1.13.12.2 pgoyette bfd_boolean ret = FALSE;
11405 1.13.12.2 pgoyette bfd *implib_bfd;
11406 1.13.12.2 pgoyette const struct elf_backend_data *bed;
11407 1.13.12.2 pgoyette flagword flags;
11408 1.13.12.2 pgoyette enum bfd_architecture arch;
11409 1.13.12.2 pgoyette unsigned int mach;
11410 1.13.12.2 pgoyette asymbol **sympp = NULL;
11411 1.13.12.2 pgoyette long symsize;
11412 1.13.12.2 pgoyette long symcount;
11413 1.13.12.2 pgoyette long src_count;
11414 1.13.12.2 pgoyette elf_symbol_type *osymbuf;
11415 1.13.12.2 pgoyette
11416 1.13.12.2 pgoyette implib_bfd = info->out_implib_bfd;
11417 1.13.12.2 pgoyette bed = get_elf_backend_data (abfd);
11418 1.13.12.2 pgoyette
11419 1.13.12.2 pgoyette if (!bfd_set_format (implib_bfd, bfd_object))
11420 1.13.12.2 pgoyette return FALSE;
11421 1.13.12.2 pgoyette
11422 1.13.12.2 pgoyette /* Use flag from executable but make it a relocatable object. */
11423 1.13.12.2 pgoyette flags = bfd_get_file_flags (abfd);
11424 1.13.12.2 pgoyette flags &= ~HAS_RELOC;
11425 1.13.12.2 pgoyette if (!bfd_set_start_address (implib_bfd, 0)
11426 1.13.12.2 pgoyette || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11427 1.13.12.2 pgoyette return FALSE;
11428 1.13.12.2 pgoyette
11429 1.13.12.2 pgoyette /* Copy architecture of output file to import library file. */
11430 1.13.12.2 pgoyette arch = bfd_get_arch (abfd);
11431 1.13.12.2 pgoyette mach = bfd_get_mach (abfd);
11432 1.13.12.2 pgoyette if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11433 1.13.12.2 pgoyette && (abfd->target_defaulted
11434 1.13.12.2 pgoyette || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11435 1.13.12.2 pgoyette return FALSE;
11436 1.13.12.2 pgoyette
11437 1.13.12.2 pgoyette /* Get symbol table size. */
11438 1.13.12.2 pgoyette symsize = bfd_get_symtab_upper_bound (abfd);
11439 1.13.12.2 pgoyette if (symsize < 0)
11440 1.13.12.2 pgoyette return FALSE;
11441 1.13.12.2 pgoyette
11442 1.13.12.2 pgoyette /* Read in the symbol table. */
11443 1.13.12.2 pgoyette sympp = (asymbol **) xmalloc (symsize);
11444 1.13.12.2 pgoyette symcount = bfd_canonicalize_symtab (abfd, sympp);
11445 1.13.12.2 pgoyette if (symcount < 0)
11446 1.13.12.2 pgoyette goto free_sym_buf;
11447 1.13.12.2 pgoyette
11448 1.13.12.2 pgoyette /* Allow the BFD backend to copy any private header data it
11449 1.13.12.2 pgoyette understands from the output BFD to the import library BFD. */
11450 1.13.12.2 pgoyette if (! bfd_copy_private_header_data (abfd, implib_bfd))
11451 1.13.12.2 pgoyette goto free_sym_buf;
11452 1.13.12.2 pgoyette
11453 1.13.12.2 pgoyette /* Filter symbols to appear in the import library. */
11454 1.13.12.2 pgoyette if (bed->elf_backend_filter_implib_symbols)
11455 1.13.12.2 pgoyette symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11456 1.13.12.2 pgoyette symcount);
11457 1.13.12.2 pgoyette else
11458 1.13.12.2 pgoyette symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11459 1.13.12.2 pgoyette if (symcount == 0)
11460 1.13.12.2 pgoyette {
11461 1.13.12.2 pgoyette bfd_set_error (bfd_error_no_symbols);
11462 1.13.12.2 pgoyette _bfd_error_handler (_("%B: no symbol found for import library"),
11463 1.13.12.2 pgoyette implib_bfd);
11464 1.13.12.2 pgoyette goto free_sym_buf;
11465 1.13.12.2 pgoyette }
11466 1.13.12.2 pgoyette
11467 1.13.12.2 pgoyette
11468 1.13.12.2 pgoyette /* Make symbols absolute. */
11469 1.13.12.2 pgoyette osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11470 1.13.12.2 pgoyette sizeof (*osymbuf));
11471 1.13.12.2 pgoyette for (src_count = 0; src_count < symcount; src_count++)
11472 1.13.12.2 pgoyette {
11473 1.13.12.2 pgoyette memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11474 1.13.12.2 pgoyette sizeof (*osymbuf));
11475 1.13.12.2 pgoyette osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11476 1.13.12.2 pgoyette osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11477 1.13.12.2 pgoyette osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11478 1.13.12.2 pgoyette osymbuf[src_count].internal_elf_sym.st_value =
11479 1.13.12.2 pgoyette osymbuf[src_count].symbol.value;
11480 1.13.12.2 pgoyette sympp[src_count] = &osymbuf[src_count].symbol;
11481 1.13.12.2 pgoyette }
11482 1.13.12.2 pgoyette
11483 1.13.12.2 pgoyette bfd_set_symtab (implib_bfd, sympp, symcount);
11484 1.13.12.2 pgoyette
11485 1.13.12.2 pgoyette /* Allow the BFD backend to copy any private data it understands
11486 1.13.12.2 pgoyette from the output BFD to the import library BFD. This is done last
11487 1.13.12.2 pgoyette to permit the routine to look at the filtered symbol table. */
11488 1.13.12.2 pgoyette if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11489 1.9 christos goto free_sym_buf;
11490 1.9 christos
11491 1.9 christos if (!bfd_close (implib_bfd))
11492 1.9 christos goto free_sym_buf;
11493 1.9 christos
11494 1.9 christos ret = TRUE;
11495 1.9 christos
11496 1.9 christos free_sym_buf:
11497 1.9 christos free (sympp);
11498 1.9 christos return ret;
11499 1.9 christos }
11500 1.9 christos
11501 1.9 christos static void
11502 1.9 christos elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11503 1.9 christos {
11504 1.9 christos asection *o;
11505 1.9 christos
11506 1.9 christos if (flinfo->symstrtab != NULL)
11507 1.9 christos _bfd_elf_strtab_free (flinfo->symstrtab);
11508 1.9 christos if (flinfo->contents != NULL)
11509 1.9 christos free (flinfo->contents);
11510 1.9 christos if (flinfo->external_relocs != NULL)
11511 1.9 christos free (flinfo->external_relocs);
11512 1.9 christos if (flinfo->internal_relocs != NULL)
11513 1.9 christos free (flinfo->internal_relocs);
11514 1.9 christos if (flinfo->external_syms != NULL)
11515 1.9 christos free (flinfo->external_syms);
11516 1.9 christos if (flinfo->locsym_shndx != NULL)
11517 1.9 christos free (flinfo->locsym_shndx);
11518 1.9 christos if (flinfo->internal_syms != NULL)
11519 1.9 christos free (flinfo->internal_syms);
11520 1.9 christos if (flinfo->indices != NULL)
11521 1.9 christos free (flinfo->indices);
11522 1.9 christos if (flinfo->sections != NULL)
11523 1.1 skrll free (flinfo->sections);
11524 1.1 skrll if (flinfo->symshndxbuf != NULL)
11525 1.1 skrll free (flinfo->symshndxbuf);
11526 1.1 skrll for (o = obfd->sections; o != NULL; o = o->next)
11527 1.1 skrll {
11528 1.1 skrll struct bfd_elf_section_data *esdo = elf_section_data (o);
11529 1.1 skrll if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11530 1.1 skrll free (esdo->rel.hashes);
11531 1.1 skrll if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11532 1.7 christos free (esdo->rela.hashes);
11533 1.4 christos }
11534 1.4 christos }
11535 1.4 christos
11536 1.1 skrll /* Do the final step of an ELF link. */
11537 1.1 skrll
11538 1.1 skrll bfd_boolean
11539 1.1 skrll bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11540 1.1 skrll {
11541 1.1 skrll bfd_boolean dynamic;
11542 1.1 skrll bfd_boolean emit_relocs;
11543 1.1 skrll bfd *dynobj;
11544 1.1 skrll struct elf_final_link_info flinfo;
11545 1.1 skrll asection *o;
11546 1.1 skrll struct bfd_link_order *p;
11547 1.1 skrll bfd *sub;
11548 1.1 skrll bfd_size_type max_contents_size;
11549 1.1 skrll bfd_size_type max_external_reloc_size;
11550 1.1 skrll bfd_size_type max_internal_reloc_count;
11551 1.1 skrll bfd_size_type max_sym_count;
11552 1.1 skrll bfd_size_type max_sym_shndx_count;
11553 1.1 skrll Elf_Internal_Sym elfsym;
11554 1.13.12.2 pgoyette unsigned int i;
11555 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
11556 1.13.12.2 pgoyette Elf_Internal_Shdr *symtab_shndx_hdr;
11557 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11558 1.1 skrll struct elf_outext_info eoinfo;
11559 1.9 christos bfd_boolean merged;
11560 1.1 skrll size_t relativecount = 0;
11561 1.1 skrll asection *reldyn = 0;
11562 1.13.12.2 pgoyette bfd_size_type amt;
11563 1.13.12.2 pgoyette asection *attr_section = NULL;
11564 1.1 skrll bfd_vma attr_size = 0;
11565 1.9 christos const char *std_attrs_section;
11566 1.1 skrll struct elf_link_hash_table *htab = elf_hash_table (info);
11567 1.1 skrll
11568 1.7 christos if (!is_elf_hash_table (htab))
11569 1.7 christos return FALSE;
11570 1.9 christos
11571 1.7 christos if (bfd_link_pic (info))
11572 1.1 skrll abfd->flags |= DYNAMIC;
11573 1.1 skrll
11574 1.1 skrll dynamic = htab->dynamic_sections_created;
11575 1.1 skrll dynobj = htab->dynobj;
11576 1.7 christos
11577 1.7 christos emit_relocs = (bfd_link_relocatable (info)
11578 1.1 skrll || info->emitrelocations);
11579 1.1 skrll
11580 1.1 skrll flinfo.info = info;
11581 1.7 christos flinfo.output_bfd = abfd;
11582 1.7 christos flinfo.symstrtab = _bfd_elf_strtab_init ();
11583 1.7 christos if (flinfo.symstrtab == NULL)
11584 1.1 skrll return FALSE;
11585 1.1 skrll
11586 1.1 skrll if (! dynamic)
11587 1.7 christos {
11588 1.7 christos flinfo.hash_sec = NULL;
11589 1.7 christos flinfo.symver_sec = NULL;
11590 1.7 christos }
11591 1.7 christos else
11592 1.7 christos {
11593 1.7 christos flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11594 1.7 christos /* Note that dynsym_sec can be NULL (on VMS). */
11595 1.7 christos flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11596 1.7 christos /* Note that it is OK if symver_sec is NULL. */
11597 1.1 skrll }
11598 1.1 skrll
11599 1.1 skrll flinfo.contents = NULL;
11600 1.1 skrll flinfo.external_relocs = NULL;
11601 1.1 skrll flinfo.internal_relocs = NULL;
11602 1.1 skrll flinfo.external_syms = NULL;
11603 1.1 skrll flinfo.locsym_shndx = NULL;
11604 1.1 skrll flinfo.internal_syms = NULL;
11605 1.1 skrll flinfo.indices = NULL;
11606 1.1 skrll flinfo.sections = NULL;
11607 1.1 skrll flinfo.symshndxbuf = NULL;
11608 1.1 skrll flinfo.filesym_count = 0;
11609 1.1 skrll
11610 1.1 skrll /* The object attributes have been merged. Remove the input
11611 1.1 skrll sections from the link, and set the contents of the output
11612 1.1 skrll secton. */
11613 1.1 skrll std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11614 1.1 skrll for (o = abfd->sections; o != NULL; o = o->next)
11615 1.1 skrll {
11616 1.1 skrll if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11617 1.1 skrll || strcmp (o->name, ".gnu.attributes") == 0)
11618 1.1 skrll {
11619 1.1 skrll for (p = o->map_head.link_order; p != NULL; p = p->next)
11620 1.1 skrll {
11621 1.1 skrll asection *input_section;
11622 1.1 skrll
11623 1.1 skrll if (p->type != bfd_indirect_link_order)
11624 1.1 skrll continue;
11625 1.1 skrll input_section = p->u.indirect.section;
11626 1.1 skrll /* Hack: reset the SEC_HAS_CONTENTS flag so that
11627 1.1 skrll elf_link_input_bfd ignores this section. */
11628 1.1 skrll input_section->flags &= ~SEC_HAS_CONTENTS;
11629 1.1 skrll }
11630 1.1 skrll
11631 1.1 skrll attr_size = bfd_elf_obj_attr_size (abfd);
11632 1.1 skrll if (attr_size)
11633 1.1 skrll {
11634 1.1 skrll bfd_set_section_size (abfd, o, attr_size);
11635 1.1 skrll attr_section = o;
11636 1.1 skrll /* Skip this section later on. */
11637 1.1 skrll o->map_head.link_order = NULL;
11638 1.1 skrll }
11639 1.1 skrll else
11640 1.1 skrll o->flags |= SEC_EXCLUDE;
11641 1.1 skrll }
11642 1.1 skrll }
11643 1.1 skrll
11644 1.1 skrll /* Count up the number of relocations we will output for each output
11645 1.1 skrll section, so that we know the sizes of the reloc sections. We
11646 1.1 skrll also figure out some maximum sizes. */
11647 1.1 skrll max_contents_size = 0;
11648 1.1 skrll max_external_reloc_size = 0;
11649 1.13 christos max_internal_reloc_count = 0;
11650 1.1 skrll max_sym_count = 0;
11651 1.1 skrll max_sym_shndx_count = 0;
11652 1.1 skrll merged = FALSE;
11653 1.1 skrll for (o = abfd->sections; o != NULL; o = o->next)
11654 1.1 skrll {
11655 1.1 skrll struct bfd_elf_section_data *esdo = elf_section_data (o);
11656 1.1 skrll o->reloc_count = 0;
11657 1.1 skrll
11658 1.1 skrll for (p = o->map_head.link_order; p != NULL; p = p->next)
11659 1.1 skrll {
11660 1.1 skrll unsigned int reloc_count = 0;
11661 1.1 skrll unsigned int additional_reloc_count = 0;
11662 1.1 skrll struct bfd_elf_section_data *esdi = NULL;
11663 1.1 skrll
11664 1.1 skrll if (p->type == bfd_section_reloc_link_order
11665 1.1 skrll || p->type == bfd_symbol_reloc_link_order)
11666 1.1 skrll reloc_count = 1;
11667 1.1 skrll else if (p->type == bfd_indirect_link_order)
11668 1.1 skrll {
11669 1.1 skrll asection *sec;
11670 1.1 skrll
11671 1.1 skrll sec = p->u.indirect.section;
11672 1.1 skrll
11673 1.1 skrll /* Mark all sections which are to be included in the
11674 1.1 skrll link. This will normally be every section. We need
11675 1.1 skrll to do this so that we can identify any sections which
11676 1.1 skrll the linker has decided to not include. */
11677 1.1 skrll sec->linker_mark = TRUE;
11678 1.1 skrll
11679 1.1 skrll if (sec->flags & SEC_MERGE)
11680 1.13.12.2 pgoyette merged = TRUE;
11681 1.13.12.2 pgoyette
11682 1.1 skrll if (sec->rawsize > max_contents_size)
11683 1.1 skrll max_contents_size = sec->rawsize;
11684 1.1 skrll if (sec->size > max_contents_size)
11685 1.1 skrll max_contents_size = sec->size;
11686 1.1 skrll
11687 1.1 skrll if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11688 1.1 skrll && (sec->owner->flags & DYNAMIC) == 0)
11689 1.1 skrll {
11690 1.1 skrll size_t sym_count;
11691 1.1 skrll
11692 1.9 christos /* We are interested in just local symbols, not all
11693 1.1 skrll symbols. */
11694 1.1 skrll if (elf_bad_symtab (sec->owner))
11695 1.13.12.2 pgoyette sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11696 1.13.12.2 pgoyette / bed->s->sizeof_sym);
11697 1.13.12.2 pgoyette else
11698 1.13.12.2 pgoyette sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11699 1.13.12.2 pgoyette
11700 1.13.12.2 pgoyette if (sym_count > max_sym_count)
11701 1.13.12.2 pgoyette max_sym_count = sym_count;
11702 1.13.12.2 pgoyette
11703 1.13.12.2 pgoyette if (sym_count > max_sym_shndx_count
11704 1.13.12.2 pgoyette && elf_symtab_shndx_list (sec->owner) != NULL)
11705 1.13.12.2 pgoyette max_sym_shndx_count = sym_count;
11706 1.13.12.2 pgoyette
11707 1.13.12.2 pgoyette if (esdo->this_hdr.sh_type == SHT_REL
11708 1.13.12.2 pgoyette || esdo->this_hdr.sh_type == SHT_RELA)
11709 1.13.12.2 pgoyette /* Some backends use reloc_count in relocation sections
11710 1.13.12.2 pgoyette to count particular types of relocs. Of course,
11711 1.13.12.2 pgoyette reloc sections themselves can't have relocations. */
11712 1.13.12.2 pgoyette ;
11713 1.13.12.2 pgoyette else if (emit_relocs)
11714 1.13.12.2 pgoyette {
11715 1.13.12.2 pgoyette reloc_count = sec->reloc_count;
11716 1.1 skrll if (bed->elf_backend_count_additional_relocs)
11717 1.1 skrll {
11718 1.4 christos int c;
11719 1.4 christos c = (*bed->elf_backend_count_additional_relocs) (sec);
11720 1.4 christos additional_reloc_count += c;
11721 1.4 christos }
11722 1.4 christos }
11723 1.4 christos else if (bed->elf_backend_count_relocs)
11724 1.1 skrll reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11725 1.1 skrll
11726 1.1 skrll esdi = elf_section_data (sec);
11727 1.1 skrll
11728 1.1 skrll if ((sec->flags & SEC_RELOC) != 0)
11729 1.1 skrll {
11730 1.1 skrll size_t ext_size = 0;
11731 1.1 skrll
11732 1.1 skrll if (esdi->rel.hdr != NULL)
11733 1.1 skrll ext_size = esdi->rel.hdr->sh_size;
11734 1.1 skrll if (esdi->rela.hdr != NULL)
11735 1.1 skrll ext_size += esdi->rela.hdr->sh_size;
11736 1.13 christos
11737 1.1 skrll if (ext_size > max_external_reloc_size)
11738 1.1 skrll max_external_reloc_size = ext_size;
11739 1.9 christos if (sec->reloc_count > max_internal_reloc_count)
11740 1.4 christos max_internal_reloc_count = sec->reloc_count;
11741 1.4 christos }
11742 1.13 christos }
11743 1.13 christos }
11744 1.13 christos
11745 1.13 christos if (reloc_count == 0)
11746 1.4 christos continue;
11747 1.13 christos
11748 1.13 christos reloc_count += additional_reloc_count;
11749 1.13 christos o->reloc_count += reloc_count;
11750 1.13 christos
11751 1.4 christos if (p->type == bfd_indirect_link_order && emit_relocs)
11752 1.4 christos {
11753 1.4 christos if (esdi->rel.hdr)
11754 1.4 christos {
11755 1.4 christos esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11756 1.4 christos esdo->rel.count += additional_reloc_count;
11757 1.4 christos }
11758 1.1 skrll if (esdi->rela.hdr)
11759 1.1 skrll {
11760 1.1 skrll esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11761 1.1 skrll esdo->rela.count += additional_reloc_count;
11762 1.1 skrll }
11763 1.1 skrll }
11764 1.1 skrll else
11765 1.1 skrll {
11766 1.1 skrll if (o->use_rela_p)
11767 1.1 skrll esdo->rela.count += reloc_count;
11768 1.1 skrll else
11769 1.1 skrll esdo->rel.count += reloc_count;
11770 1.1 skrll }
11771 1.1 skrll }
11772 1.1 skrll
11773 1.1 skrll if (o->reloc_count > 0)
11774 1.1 skrll o->flags |= SEC_RELOC;
11775 1.1 skrll else
11776 1.1 skrll {
11777 1.1 skrll /* Explicitly clear the SEC_RELOC flag. The linker tends to
11778 1.1 skrll set it (this is probably a bug) and if it is set
11779 1.1 skrll assign_section_numbers will create a reloc section. */
11780 1.9 christos o->flags &=~ SEC_RELOC;
11781 1.13.12.2 pgoyette }
11782 1.1 skrll
11783 1.1 skrll /* If the SEC_ALLOC flag is not set, force the section VMA to
11784 1.1 skrll zero. This is done in elf_fake_sections as well, but forcing
11785 1.1 skrll the VMA to 0 here will ensure that relocs against these
11786 1.9 christos sections are handled correctly. */
11787 1.1 skrll if ((o->flags & SEC_ALLOC) == 0
11788 1.1 skrll && ! o->user_set_vma)
11789 1.1 skrll o->vma = 0;
11790 1.1 skrll }
11791 1.1 skrll
11792 1.1 skrll if (! bfd_link_relocatable (info) && merged)
11793 1.1 skrll elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11794 1.4 christos
11795 1.1 skrll /* Figure out the file positions for everything but the symbol table
11796 1.1 skrll and the relocs. We set symcount to force assign_section_numbers
11797 1.4 christos to create a symbol table. */
11798 1.4 christos bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11799 1.1 skrll BFD_ASSERT (! abfd->output_has_begun);
11800 1.1 skrll if (! _bfd_elf_compute_section_file_positions (abfd, info))
11801 1.4 christos goto error_return;
11802 1.4 christos
11803 1.1 skrll /* Set sizes, and assign file positions for reloc sections. */
11804 1.1 skrll for (o = abfd->sections; o != NULL; o = o->next)
11805 1.1 skrll {
11806 1.1 skrll struct bfd_elf_section_data *esdo = elf_section_data (o);
11807 1.1 skrll if ((o->flags & SEC_RELOC) != 0)
11808 1.4 christos {
11809 1.4 christos if (esdo->rel.hdr
11810 1.9 christos && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11811 1.9 christos goto error_return;
11812 1.9 christos
11813 1.9 christos if (esdo->rela.hdr
11814 1.9 christos && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11815 1.9 christos goto error_return;
11816 1.9 christos }
11817 1.9 christos
11818 1.9 christos /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11819 1.9 christos to count upwards while actually outputting the relocations. */
11820 1.9 christos esdo->rel.count = 0;
11821 1.9 christos esdo->rela.count = 0;
11822 1.9 christos
11823 1.9 christos if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11824 1.9 christos {
11825 1.1 skrll /* Cache the section contents so that they can be compressed
11826 1.1 skrll later. Use bfd_malloc since it will be freed by
11827 1.1 skrll bfd_compress_section_contents. */
11828 1.9 christos unsigned char *contents = esdo->this_hdr.contents;
11829 1.9 christos if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11830 1.9 christos abort ();
11831 1.1 skrll contents
11832 1.1 skrll = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11833 1.1 skrll if (contents == NULL)
11834 1.1 skrll goto error_return;
11835 1.1 skrll esdo->this_hdr.contents = contents;
11836 1.1 skrll }
11837 1.1 skrll }
11838 1.1 skrll
11839 1.1 skrll /* We have now assigned file positions for all the sections except
11840 1.1 skrll .symtab, .strtab, and non-loaded reloc sections. We start the
11841 1.1 skrll .symtab section at the current file position, and write directly
11842 1.9 christos to it. We build the .strtab section in memory. */
11843 1.9 christos bfd_get_symcount (abfd) = 0;
11844 1.13.12.2 pgoyette symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11845 1.9 christos /* sh_name is set in prep_headers. */
11846 1.13.12.2 pgoyette symtab_hdr->sh_type = SHT_SYMTAB;
11847 1.13.12.2 pgoyette /* sh_flags, sh_addr and sh_size all start off zero. */
11848 1.9 christos symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11849 1.9 christos /* sh_link is set in assign_section_numbers. */
11850 1.9 christos /* sh_info is set below. */
11851 1.9 christos /* sh_offset is set just below. */
11852 1.9 christos symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11853 1.1 skrll
11854 1.9 christos if (max_sym_count < 20)
11855 1.1 skrll max_sym_count = 20;
11856 1.9 christos htab->strtabsize = max_sym_count;
11857 1.9 christos amt = max_sym_count * sizeof (struct elf_sym_strtab);
11858 1.9 christos htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11859 1.9 christos if (htab->strtab == NULL)
11860 1.9 christos goto error_return;
11861 1.9 christos /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11862 1.9 christos flinfo.symshndxbuf
11863 1.1 skrll = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11864 1.9 christos ? (Elf_External_Sym_Shndx *) -1 : NULL);
11865 1.9 christos
11866 1.1 skrll if (info->strip != strip_all || emit_relocs)
11867 1.1 skrll {
11868 1.1 skrll file_ptr off = elf_next_file_pos (abfd);
11869 1.1 skrll
11870 1.1 skrll _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11871 1.7 christos
11872 1.9 christos /* Note that at this point elf_next_file_pos (abfd) is
11873 1.9 christos incorrect. We do not yet know the size of the .symtab section.
11874 1.1 skrll We correct next_file_pos below, after we do know the size. */
11875 1.1 skrll
11876 1.9 christos /* Start writing out the symbol table. The first symbol is always a
11877 1.9 christos dummy symbol. */
11878 1.9 christos elfsym.st_value = 0;
11879 1.9 christos elfsym.st_size = 0;
11880 1.9 christos elfsym.st_info = 0;
11881 1.9 christos elfsym.st_other = 0;
11882 1.1 skrll elfsym.st_shndx = SHN_UNDEF;
11883 1.1 skrll elfsym.st_target_internal = 0;
11884 1.1 skrll if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11885 1.1 skrll bfd_und_section_ptr, NULL) != 1)
11886 1.7 christos goto error_return;
11887 1.1 skrll
11888 1.1 skrll /* Output a symbol for each section. We output these even if we are
11889 1.1 skrll discarding local symbols, since they are used for relocs. These
11890 1.1 skrll symbols have no names. We store the index of each one in the
11891 1.1 skrll index field of the section, so that we can find it again when
11892 1.1 skrll outputting relocs. */
11893 1.1 skrll
11894 1.9 christos elfsym.st_size = 0;
11895 1.1 skrll elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11896 1.9 christos elfsym.st_other = 0;
11897 1.9 christos elfsym.st_value = 0;
11898 1.1 skrll elfsym.st_target_internal = 0;
11899 1.1 skrll for (i = 1; i < elf_numsections (abfd); i++)
11900 1.1 skrll {
11901 1.1 skrll o = bfd_section_from_elf_index (abfd, i);
11902 1.1 skrll if (o != NULL)
11903 1.1 skrll {
11904 1.1 skrll o->target_index = bfd_get_symcount (abfd);
11905 1.1 skrll elfsym.st_shndx = i;
11906 1.1 skrll if (!bfd_link_relocatable (info))
11907 1.7 christos elfsym.st_value = o->vma;
11908 1.7 christos if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11909 1.1 skrll NULL) != 1)
11910 1.1 skrll goto error_return;
11911 1.1 skrll }
11912 1.1 skrll }
11913 1.1 skrll }
11914 1.7 christos
11915 1.7 christos /* Allocate some memory to hold information read in from the input
11916 1.1 skrll files. */
11917 1.1 skrll if (max_contents_size != 0)
11918 1.1 skrll {
11919 1.1 skrll flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11920 1.1 skrll if (flinfo.contents == NULL)
11921 1.13.12.2 pgoyette goto error_return;
11922 1.7 christos }
11923 1.7 christos
11924 1.1 skrll if (max_external_reloc_size != 0)
11925 1.1 skrll {
11926 1.1 skrll flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11927 1.1 skrll if (flinfo.external_relocs == NULL)
11928 1.1 skrll goto error_return;
11929 1.1 skrll }
11930 1.7 christos
11931 1.7 christos if (max_internal_reloc_count != 0)
11932 1.1 skrll {
11933 1.1 skrll amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11934 1.1 skrll flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11935 1.7 christos if (flinfo.internal_relocs == NULL)
11936 1.7 christos goto error_return;
11937 1.1 skrll }
11938 1.1 skrll
11939 1.1 skrll if (max_sym_count != 0)
11940 1.7 christos {
11941 1.7 christos amt = max_sym_count * bed->s->sizeof_sym;
11942 1.1 skrll flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11943 1.1 skrll if (flinfo.external_syms == NULL)
11944 1.1 skrll goto error_return;
11945 1.7 christos
11946 1.7 christos amt = max_sym_count * sizeof (Elf_Internal_Sym);
11947 1.1 skrll flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11948 1.1 skrll if (flinfo.internal_syms == NULL)
11949 1.1 skrll goto error_return;
11950 1.1 skrll
11951 1.1 skrll amt = max_sym_count * sizeof (long);
11952 1.1 skrll flinfo.indices = (long int *) bfd_malloc (amt);
11953 1.7 christos if (flinfo.indices == NULL)
11954 1.7 christos goto error_return;
11955 1.1 skrll
11956 1.1 skrll amt = max_sym_count * sizeof (asection *);
11957 1.1 skrll flinfo.sections = (asection **) bfd_malloc (amt);
11958 1.13.12.2 pgoyette if (flinfo.sections == NULL)
11959 1.1 skrll goto error_return;
11960 1.1 skrll }
11961 1.1 skrll
11962 1.1 skrll if (max_sym_shndx_count != 0)
11963 1.13.12.2 pgoyette {
11964 1.1 skrll amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11965 1.1 skrll flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11966 1.1 skrll if (flinfo.locsym_shndx == NULL)
11967 1.1 skrll goto error_return;
11968 1.1 skrll }
11969 1.1 skrll
11970 1.1 skrll if (htab->tls_sec)
11971 1.1 skrll {
11972 1.4 christos bfd_vma base, end = 0;
11973 1.4 christos asection *sec;
11974 1.4 christos
11975 1.4 christos for (sec = htab->tls_sec;
11976 1.1 skrll sec && (sec->flags & SEC_THREAD_LOCAL);
11977 1.1 skrll sec = sec->next)
11978 1.1 skrll {
11979 1.13.12.2 pgoyette bfd_size_type size = sec->size;
11980 1.4 christos
11981 1.4 christos if (size == 0
11982 1.4 christos && (sec->flags & SEC_HAS_CONTENTS) == 0)
11983 1.13.12.2 pgoyette {
11984 1.13.12.2 pgoyette struct bfd_link_order *ord = sec->map_tail.link_order;
11985 1.1 skrll
11986 1.1 skrll if (ord != NULL)
11987 1.1 skrll size = ord->offset + ord->size;
11988 1.1 skrll }
11989 1.1 skrll end = sec->vma + size;
11990 1.1 skrll }
11991 1.1 skrll base = htab->tls_sec->vma;
11992 1.1 skrll /* Only align end of TLS section if static TLS doesn't have special
11993 1.1 skrll alignment requirements. */
11994 1.9 christos if (bed->static_tls_alignment == 1)
11995 1.9 christos end = align_power (end, htab->tls_sec->alignment_power);
11996 1.9 christos htab->tls_size = end - base;
11997 1.1 skrll }
11998 1.1 skrll
11999 1.1 skrll /* Reorder SHF_LINK_ORDER sections. */
12000 1.1 skrll for (o = abfd->sections; o != NULL; o = o->next)
12001 1.1 skrll {
12002 1.1 skrll if (!elf_fixup_link_order (abfd, o))
12003 1.1 skrll return FALSE;
12004 1.1 skrll }
12005 1.1 skrll
12006 1.1 skrll if (!_bfd_elf_fixup_eh_frame_hdr (info))
12007 1.1 skrll return FALSE;
12008 1.1 skrll
12009 1.1 skrll /* Since ELF permits relocations to be against local symbols, we
12010 1.1 skrll must have the local symbols available when we do the relocations.
12011 1.1 skrll Since we would rather only read the local symbols once, and we
12012 1.1 skrll would rather not keep them in memory, we handle all the
12013 1.1 skrll relocations for a single input file at the same time.
12014 1.1 skrll
12015 1.1 skrll Unfortunately, there is no way to know the total number of local
12016 1.1 skrll symbols until we have seen all of them, and the local symbol
12017 1.9 christos indices precede the global symbol indices. This means that when
12018 1.1 skrll we are generating relocatable output, and we see a reloc against
12019 1.1 skrll a global symbol, we can not know the symbol index until we have
12020 1.1 skrll finished examining all the local symbols to see which ones we are
12021 1.1 skrll going to output. To deal with this, we keep the relocations in
12022 1.1 skrll memory, and don't output them until the end of the link. This is
12023 1.1 skrll an unfortunate waste of memory, but I don't see a good way around
12024 1.1 skrll it. Fortunately, it only happens when performing a relocatable
12025 1.1 skrll link, which is not the common case. FIXME: If keep_memory is set
12026 1.1 skrll we could write the relocs out and then read them again; I don't
12027 1.1 skrll know how bad the memory loss will be. */
12028 1.1 skrll
12029 1.1 skrll for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12030 1.7 christos sub->output_has_begun = FALSE;
12031 1.1 skrll for (o = abfd->sections; o != NULL; o = o->next)
12032 1.1 skrll {
12033 1.1 skrll for (p = o->map_head.link_order; p != NULL; p = p->next)
12034 1.1 skrll {
12035 1.1 skrll if (p->type == bfd_indirect_link_order
12036 1.1 skrll && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12037 1.1 skrll == bfd_target_elf_flavour)
12038 1.1 skrll && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12039 1.1 skrll {
12040 1.1 skrll if (! sub->output_has_begun)
12041 1.1 skrll {
12042 1.1 skrll if (! elf_link_input_bfd (&flinfo, sub))
12043 1.1 skrll goto error_return;
12044 1.7 christos sub->output_has_begun = TRUE;
12045 1.7 christos }
12046 1.7 christos }
12047 1.7 christos else if (p->type == bfd_section_reloc_link_order
12048 1.7 christos || p->type == bfd_symbol_reloc_link_order)
12049 1.7 christos {
12050 1.7 christos if (! elf_reloc_link_order (abfd, info, o, p))
12051 1.7 christos goto error_return;
12052 1.7 christos }
12053 1.13 christos else
12054 1.7 christos {
12055 1.13 christos if (! _bfd_default_link_order (abfd, info, o, p))
12056 1.13 christos {
12057 1.13 christos if (p->type == bfd_indirect_link_order
12058 1.13 christos && (bfd_get_flavour (sub)
12059 1.7 christos == bfd_target_elf_flavour)
12060 1.13 christos && (elf_elfheader (sub)->e_ident[EI_CLASS]
12061 1.13 christos != bed->s->elfclass))
12062 1.7 christos {
12063 1.13 christos const char *iclass, *oclass;
12064 1.13 christos
12065 1.13 christos switch (bed->s->elfclass)
12066 1.13 christos {
12067 1.7 christos case ELFCLASS64: oclass = "ELFCLASS64"; break;
12068 1.7 christos case ELFCLASS32: oclass = "ELFCLASS32"; break;
12069 1.7 christos case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12070 1.13.12.2 pgoyette default: abort ();
12071 1.13.12.2 pgoyette }
12072 1.7 christos
12073 1.7 christos switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12074 1.7 christos {
12075 1.7 christos case ELFCLASS64: iclass = "ELFCLASS64"; break;
12076 1.7 christos case ELFCLASS32: iclass = "ELFCLASS32"; break;
12077 1.7 christos case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12078 1.1 skrll default: abort ();
12079 1.1 skrll }
12080 1.1 skrll
12081 1.1 skrll bfd_set_error (bfd_error_wrong_format);
12082 1.1 skrll _bfd_error_handler
12083 1.1 skrll /* xgettext:c-format */
12084 1.1 skrll (_("%B: file class %s incompatible with %s"),
12085 1.9 christos sub, iclass, oclass);
12086 1.1 skrll }
12087 1.1 skrll
12088 1.1 skrll goto error_return;
12089 1.1 skrll }
12090 1.1 skrll }
12091 1.1 skrll }
12092 1.1 skrll }
12093 1.1 skrll
12094 1.1 skrll /* Free symbol buffer if needed. */
12095 1.1 skrll if (!info->reduce_memory_overheads)
12096 1.1 skrll {
12097 1.1 skrll for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12098 1.1 skrll if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12099 1.1 skrll && elf_tdata (sub)->symbuf)
12100 1.1 skrll {
12101 1.7 christos free (elf_tdata (sub)->symbuf);
12102 1.1 skrll elf_tdata (sub)->symbuf = NULL;
12103 1.9 christos }
12104 1.7 christos }
12105 1.1 skrll
12106 1.1 skrll /* Output any global symbols that got converted to local in a
12107 1.1 skrll version script or due to symbol visibility. We do this in a
12108 1.1 skrll separate step since ELF requires all local symbols to appear
12109 1.1 skrll prior to any global symbols. FIXME: We should only do this if
12110 1.9 christos some global symbols were, in fact, converted to become local.
12111 1.9 christos FIXME: Will this work correctly with the Irix 5 linker? */
12112 1.1 skrll eoinfo.failed = FALSE;
12113 1.4 christos eoinfo.flinfo = &flinfo;
12114 1.1 skrll eoinfo.localsyms = TRUE;
12115 1.1 skrll eoinfo.file_sym_done = FALSE;
12116 1.1 skrll bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12117 1.1 skrll if (eoinfo.failed)
12118 1.9 christos return FALSE;
12119 1.9 christos
12120 1.1 skrll /* If backend needs to output some local symbols not present in the hash
12121 1.1 skrll table, do it now. */
12122 1.1 skrll if (bed->elf_backend_output_arch_local_syms
12123 1.1 skrll && (info->strip != strip_all || emit_relocs))
12124 1.1 skrll {
12125 1.1 skrll typedef int (*out_sym_func)
12126 1.1 skrll (void *, const char *, Elf_Internal_Sym *, asection *,
12127 1.1 skrll struct elf_link_hash_entry *);
12128 1.1 skrll
12129 1.1 skrll if (! ((*bed->elf_backend_output_arch_local_syms)
12130 1.1 skrll (abfd, info, &flinfo,
12131 1.1 skrll (out_sym_func) elf_link_output_symstrtab)))
12132 1.13.12.2 pgoyette return FALSE;
12133 1.13.12.2 pgoyette }
12134 1.1 skrll
12135 1.1 skrll /* That wrote out all the local symbols. Finish up the symbol table
12136 1.13.12.2 pgoyette with the global symbols. Even if we want to strip everything we
12137 1.13.12.2 pgoyette can, we still need to deal with those global symbols that got
12138 1.13.12.2 pgoyette converted to local in a version script. */
12139 1.13.12.2 pgoyette
12140 1.1 skrll /* The sh_info field records the index of the first non local symbol. */
12141 1.1 skrll symtab_hdr->sh_info = bfd_get_symcount (abfd);
12142 1.9 christos
12143 1.13.12.2 pgoyette if (dynamic
12144 1.1 skrll && htab->dynsym != NULL
12145 1.1 skrll && htab->dynsym->output_section != bfd_abs_section_ptr)
12146 1.1 skrll {
12147 1.1 skrll Elf_Internal_Sym sym;
12148 1.1 skrll bfd_byte *dynsym = htab->dynsym->contents;
12149 1.1 skrll
12150 1.1 skrll o = htab->dynsym->output_section;
12151 1.7 christos elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12152 1.1 skrll
12153 1.1 skrll /* Write out the section symbols for the output sections. */
12154 1.1 skrll if (bfd_link_pic (info)
12155 1.1 skrll || htab->is_relocatable_executable)
12156 1.1 skrll {
12157 1.1 skrll asection *s;
12158 1.1 skrll
12159 1.1 skrll sym.st_size = 0;
12160 1.1 skrll sym.st_name = 0;
12161 1.1 skrll sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12162 1.1 skrll sym.st_other = 0;
12163 1.1 skrll sym.st_target_internal = 0;
12164 1.1 skrll
12165 1.1 skrll for (s = abfd->sections; s != NULL; s = s->next)
12166 1.1 skrll {
12167 1.1 skrll int indx;
12168 1.1 skrll bfd_byte *dest;
12169 1.1 skrll long dynindx;
12170 1.1 skrll
12171 1.1 skrll dynindx = elf_section_data (s)->dynindx;
12172 1.1 skrll if (dynindx <= 0)
12173 1.1 skrll continue;
12174 1.13.12.2 pgoyette indx = elf_section_data (s)->this_idx;
12175 1.1 skrll BFD_ASSERT (indx > 0);
12176 1.1 skrll sym.st_shndx = indx;
12177 1.13.12.2 pgoyette if (! check_dynsym (abfd, &sym))
12178 1.1 skrll return FALSE;
12179 1.1 skrll sym.st_value = s->vma;
12180 1.1 skrll dest = dynsym + dynindx * bed->s->sizeof_sym;
12181 1.1 skrll bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12182 1.4 christos }
12183 1.1 skrll }
12184 1.1 skrll
12185 1.1 skrll /* Write out the local dynsyms. */
12186 1.4 christos if (htab->dynlocal)
12187 1.1 skrll {
12188 1.1 skrll struct elf_link_local_dynamic_entry *e;
12189 1.1 skrll for (e = htab->dynlocal; e ; e = e->next)
12190 1.1 skrll {
12191 1.1 skrll asection *s;
12192 1.1 skrll bfd_byte *dest;
12193 1.1 skrll
12194 1.1 skrll /* Copy the internal symbol and turn off visibility.
12195 1.1 skrll Note that we saved a word of storage and overwrote
12196 1.1 skrll the original st_name with the dynstr_index. */
12197 1.1 skrll sym = e->isym;
12198 1.1 skrll sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12199 1.1 skrll
12200 1.1 skrll s = bfd_section_from_elf_index (e->input_bfd,
12201 1.1 skrll e->isym.st_shndx);
12202 1.1 skrll if (s != NULL)
12203 1.1 skrll {
12204 1.1 skrll sym.st_shndx =
12205 1.1 skrll elf_section_data (s->output_section)->this_idx;
12206 1.1 skrll if (! check_dynsym (abfd, &sym))
12207 1.1 skrll return FALSE;
12208 1.1 skrll sym.st_value = (s->output_section->vma
12209 1.1 skrll + s->output_offset
12210 1.7 christos + e->isym.st_value);
12211 1.7 christos }
12212 1.1 skrll
12213 1.1 skrll dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12214 1.1 skrll bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12215 1.1 skrll }
12216 1.1 skrll }
12217 1.9 christos }
12218 1.9 christos
12219 1.1 skrll /* We get the global symbols from the hash table. */
12220 1.4 christos eoinfo.failed = FALSE;
12221 1.1 skrll eoinfo.localsyms = FALSE;
12222 1.1 skrll eoinfo.flinfo = &flinfo;
12223 1.1 skrll bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12224 1.1 skrll if (eoinfo.failed)
12225 1.9 christos return FALSE;
12226 1.9 christos
12227 1.1 skrll /* If backend needs to output some symbols not present in the hash
12228 1.1 skrll table, do it now. */
12229 1.1 skrll if (bed->elf_backend_output_arch_syms
12230 1.9 christos && (info->strip != strip_all || emit_relocs))
12231 1.9 christos {
12232 1.9 christos typedef int (*out_sym_func)
12233 1.9 christos (void *, const char *, Elf_Internal_Sym *, asection *,
12234 1.9 christos struct elf_link_hash_entry *);
12235 1.1 skrll
12236 1.1 skrll if (! ((*bed->elf_backend_output_arch_syms)
12237 1.1 skrll (abfd, info, &flinfo,
12238 1.9 christos (out_sym_func) elf_link_output_symstrtab)))
12239 1.1 skrll return FALSE;
12240 1.9 christos }
12241 1.9 christos
12242 1.13.12.2 pgoyette /* Finalize the .strtab section. */
12243 1.9 christos _bfd_elf_strtab_finalize (flinfo.symstrtab);
12244 1.9 christos
12245 1.13.12.2 pgoyette /* Swap out the .strtab section. */
12246 1.9 christos if (!elf_link_swap_symbols_out (&flinfo))
12247 1.13.12.2 pgoyette return FALSE;
12248 1.13.12.2 pgoyette
12249 1.13.12.2 pgoyette /* Now we know the size of the symtab section. */
12250 1.13.12.2 pgoyette if (bfd_get_symcount (abfd) > 0)
12251 1.13.12.2 pgoyette {
12252 1.13.12.2 pgoyette /* Finish up and write out the symbol string table (.strtab)
12253 1.13.12.2 pgoyette section. */
12254 1.13.12.2 pgoyette Elf_Internal_Shdr *symstrtab_hdr = NULL;
12255 1.13.12.2 pgoyette file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12256 1.1 skrll
12257 1.13.12.2 pgoyette if (elf_symtab_shndx_list (abfd))
12258 1.13.12.2 pgoyette {
12259 1.1 skrll symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12260 1.13.12.2 pgoyette
12261 1.13.12.2 pgoyette if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12262 1.13.12.2 pgoyette {
12263 1.13.12.2 pgoyette symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12264 1.9 christos symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12265 1.1 skrll symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12266 1.9 christos amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12267 1.9 christos symtab_shndx_hdr->sh_size = amt;
12268 1.9 christos
12269 1.13 christos off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12270 1.9 christos off, TRUE);
12271 1.9 christos
12272 1.9 christos if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12273 1.9 christos || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12274 1.9 christos return FALSE;
12275 1.9 christos }
12276 1.9 christos }
12277 1.1 skrll
12278 1.9 christos symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12279 1.9 christos /* sh_name was set in prep_headers. */
12280 1.9 christos symstrtab_hdr->sh_type = SHT_STRTAB;
12281 1.1 skrll symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12282 1.1 skrll symstrtab_hdr->sh_addr = 0;
12283 1.9 christos symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12284 1.1 skrll symstrtab_hdr->sh_entsize = 0;
12285 1.1 skrll symstrtab_hdr->sh_link = 0;
12286 1.1 skrll symstrtab_hdr->sh_info = 0;
12287 1.13.12.2 pgoyette /* sh_offset is set just below. */
12288 1.13.12.2 pgoyette symstrtab_hdr->sh_addralign = 1;
12289 1.13.12.2 pgoyette
12290 1.13.12.2 pgoyette off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12291 1.13.12.2 pgoyette off, TRUE);
12292 1.13.12.2 pgoyette elf_next_file_pos (abfd) = off;
12293 1.13.12.2 pgoyette
12294 1.1 skrll if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12295 1.1 skrll || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12296 1.1 skrll return FALSE;
12297 1.4 christos }
12298 1.9 christos
12299 1.13.12.2 pgoyette if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12300 1.1 skrll {
12301 1.1 skrll _bfd_error_handler (_("%B: failed to generate import library"),
12302 1.1 skrll info->out_implib_bfd);
12303 1.9 christos return FALSE;
12304 1.9 christos }
12305 1.13.12.2 pgoyette
12306 1.9 christos /* Adjust the relocs to have the correct symbol indices. */
12307 1.9 christos for (o = abfd->sections; o != NULL; o = o->next)
12308 1.13.12.2 pgoyette {
12309 1.9 christos struct bfd_elf_section_data *esdo = elf_section_data (o);
12310 1.1 skrll bfd_boolean sort;
12311 1.1 skrll
12312 1.1 skrll if ((o->flags & SEC_RELOC) == 0)
12313 1.1 skrll continue;
12314 1.1 skrll
12315 1.1 skrll sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12316 1.1 skrll if (esdo->rel.hdr != NULL
12317 1.1 skrll && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12318 1.1 skrll return FALSE;
12319 1.1 skrll if (esdo->rela.hdr != NULL
12320 1.1 skrll && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12321 1.1 skrll return FALSE;
12322 1.1 skrll
12323 1.1 skrll /* Set the reloc_count field to 0 to prevent write_relocs from
12324 1.1 skrll trying to swap the relocs out itself. */
12325 1.1 skrll o->reloc_count = 0;
12326 1.7 christos }
12327 1.1 skrll
12328 1.1 skrll if (dynamic && info->combreloc && dynobj != NULL)
12329 1.1 skrll relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12330 1.1 skrll
12331 1.1 skrll /* If we are linking against a dynamic object, or generating a
12332 1.1 skrll shared library, finish up the dynamic linking information. */
12333 1.1 skrll if (dynamic)
12334 1.1 skrll {
12335 1.1 skrll bfd_byte *dyncon, *dynconend;
12336 1.13.12.2 pgoyette
12337 1.13.12.2 pgoyette /* Fix up .dynamic entries. */
12338 1.1 skrll o = bfd_get_linker_section (dynobj, ".dynamic");
12339 1.1 skrll BFD_ASSERT (o != NULL);
12340 1.1 skrll
12341 1.1 skrll dyncon = o->contents;
12342 1.1 skrll dynconend = o->contents + o->size;
12343 1.1 skrll for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12344 1.1 skrll {
12345 1.1 skrll Elf_Internal_Dyn dyn;
12346 1.1 skrll const char *name;
12347 1.1 skrll unsigned int type;
12348 1.1 skrll bfd_size_type sh_size;
12349 1.1 skrll bfd_vma sh_addr;
12350 1.1 skrll
12351 1.1 skrll bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12352 1.1 skrll
12353 1.1 skrll switch (dyn.d_tag)
12354 1.1 skrll {
12355 1.1 skrll default:
12356 1.1 skrll continue;
12357 1.1 skrll case DT_NULL:
12358 1.1 skrll if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12359 1.1 skrll {
12360 1.1 skrll switch (elf_section_data (reldyn)->this_hdr.sh_type)
12361 1.1 skrll {
12362 1.1 skrll case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12363 1.1 skrll case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12364 1.1 skrll default: continue;
12365 1.1 skrll }
12366 1.1 skrll dyn.d_un.d_val = relativecount;
12367 1.1 skrll relativecount = 0;
12368 1.1 skrll break;
12369 1.13.12.2 pgoyette }
12370 1.1 skrll continue;
12371 1.1 skrll
12372 1.1 skrll case DT_INIT:
12373 1.1 skrll name = info->init_function;
12374 1.4 christos goto get_sym;
12375 1.1 skrll case DT_FINI:
12376 1.1 skrll name = info->fini_function;
12377 1.4 christos get_sym:
12378 1.1 skrll {
12379 1.1 skrll struct elf_link_hash_entry *h;
12380 1.1 skrll
12381 1.1 skrll h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12382 1.1 skrll if (h != NULL
12383 1.4 christos && (h->root.type == bfd_link_hash_defined
12384 1.1 skrll || h->root.type == bfd_link_hash_defweak))
12385 1.1 skrll {
12386 1.1 skrll dyn.d_un.d_ptr = h->root.u.def.value;
12387 1.1 skrll o = h->root.u.def.section;
12388 1.1 skrll if (o->output_section != NULL)
12389 1.1 skrll dyn.d_un.d_ptr += (o->output_section->vma
12390 1.1 skrll + o->output_offset);
12391 1.1 skrll else
12392 1.13 christos {
12393 1.1 skrll /* The symbol is imported from another shared
12394 1.1 skrll library and does not apply to this one. */
12395 1.13 christos dyn.d_un.d_ptr = 0;
12396 1.1 skrll }
12397 1.1 skrll break;
12398 1.13 christos }
12399 1.1 skrll }
12400 1.1 skrll continue;
12401 1.1 skrll
12402 1.13.12.2 pgoyette case DT_PREINIT_ARRAYSZ:
12403 1.13 christos name = ".preinit_array";
12404 1.1 skrll goto get_out_size;
12405 1.1 skrll case DT_INIT_ARRAYSZ:
12406 1.1 skrll name = ".init_array";
12407 1.13.12.2 pgoyette goto get_out_size;
12408 1.1 skrll case DT_FINI_ARRAYSZ:
12409 1.1 skrll name = ".fini_array";
12410 1.1 skrll get_out_size:
12411 1.1 skrll o = bfd_get_section_by_name (abfd, name);
12412 1.1 skrll if (o == NULL)
12413 1.1 skrll {
12414 1.13 christos _bfd_error_handler
12415 1.1 skrll (_("could not find section %s"), name);
12416 1.1 skrll goto error_return;
12417 1.13 christos }
12418 1.1 skrll if (o->size == 0)
12419 1.1 skrll _bfd_error_handler
12420 1.13 christos (_("warning: %s section has zero size"), name);
12421 1.13 christos dyn.d_un.d_val = o->size;
12422 1.13 christos break;
12423 1.1 skrll
12424 1.1 skrll case DT_PREINIT_ARRAY:
12425 1.1 skrll name = ".preinit_array";
12426 1.1 skrll goto get_out_vma;
12427 1.1 skrll case DT_INIT_ARRAY:
12428 1.1 skrll name = ".init_array";
12429 1.1 skrll goto get_out_vma;
12430 1.1 skrll case DT_FINI_ARRAY:
12431 1.1 skrll name = ".fini_array";
12432 1.1 skrll get_out_vma:
12433 1.1 skrll o = bfd_get_section_by_name (abfd, name);
12434 1.1 skrll goto do_vma;
12435 1.1 skrll
12436 1.1 skrll case DT_HASH:
12437 1.1 skrll name = ".hash";
12438 1.1 skrll goto get_vma;
12439 1.1 skrll case DT_GNU_HASH:
12440 1.1 skrll name = ".gnu.hash";
12441 1.1 skrll goto get_vma;
12442 1.1 skrll case DT_STRTAB:
12443 1.1 skrll name = ".dynstr";
12444 1.1 skrll goto get_vma;
12445 1.13 christos case DT_SYMTAB:
12446 1.13 christos name = ".dynsym";
12447 1.13.12.2 pgoyette goto get_vma;
12448 1.1 skrll case DT_VERDEF:
12449 1.13.12.2 pgoyette name = ".gnu.version_d";
12450 1.13 christos goto get_vma;
12451 1.1 skrll case DT_VERNEED:
12452 1.1 skrll name = ".gnu.version_r";
12453 1.7 christos goto get_vma;
12454 1.7 christos case DT_VERSYM:
12455 1.13.12.2 pgoyette name = ".gnu.version";
12456 1.7 christos get_vma:
12457 1.7 christos o = bfd_get_linker_section (dynobj, name);
12458 1.7 christos do_vma:
12459 1.7 christos if (o == NULL || bfd_is_abs_section (o->output_section))
12460 1.13 christos {
12461 1.1 skrll _bfd_error_handler
12462 1.1 skrll (_("could not find section %s"), name);
12463 1.1 skrll goto error_return;
12464 1.1 skrll }
12465 1.1 skrll if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12466 1.1 skrll {
12467 1.1 skrll _bfd_error_handler
12468 1.1 skrll (_("warning: section '%s' is being made into a note"), name);
12469 1.1 skrll bfd_set_error (bfd_error_nonrepresentable_section);
12470 1.1 skrll goto error_return;
12471 1.13.12.2 pgoyette }
12472 1.13.12.2 pgoyette dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12473 1.1 skrll break;
12474 1.1 skrll
12475 1.1 skrll case DT_REL:
12476 1.1 skrll case DT_RELA:
12477 1.1 skrll case DT_RELSZ:
12478 1.1 skrll case DT_RELASZ:
12479 1.1 skrll if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12480 1.1 skrll type = SHT_REL;
12481 1.13.12.2 pgoyette else
12482 1.13.12.2 pgoyette type = SHT_RELA;
12483 1.13.12.2 pgoyette sh_size = 0;
12484 1.13.12.2 pgoyette sh_addr = 0;
12485 1.1 skrll for (i = 1; i < elf_numsections (abfd); i++)
12486 1.1 skrll {
12487 1.13.12.2 pgoyette Elf_Internal_Shdr *hdr;
12488 1.13.12.2 pgoyette
12489 1.13.12.2 pgoyette hdr = elf_elfsections (abfd)[i];
12490 1.13.12.2 pgoyette if (hdr->sh_type == type
12491 1.13.12.2 pgoyette && (hdr->sh_flags & SHF_ALLOC) != 0)
12492 1.13.12.2 pgoyette {
12493 1.13.12.2 pgoyette sh_size += hdr->sh_size;
12494 1.13.12.2 pgoyette if (sh_addr == 0
12495 1.13.12.2 pgoyette || sh_addr > hdr->sh_addr)
12496 1.13.12.2 pgoyette sh_addr = hdr->sh_addr;
12497 1.13.12.2 pgoyette }
12498 1.13.12.2 pgoyette }
12499 1.13.12.2 pgoyette
12500 1.13.12.2 pgoyette if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12501 1.13.12.2 pgoyette {
12502 1.13.12.2 pgoyette /* Don't count procedure linkage table relocs in the
12503 1.13.12.2 pgoyette overall reloc count. */
12504 1.13.12.2 pgoyette sh_size -= htab->srelplt->size;
12505 1.13.12.2 pgoyette if (sh_size == 0)
12506 1.13.12.2 pgoyette /* If the size is zero, make the address zero too.
12507 1.13.12.2 pgoyette This is to avoid a glibc bug. If the backend
12508 1.13.12.2 pgoyette emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12509 1.13.12.2 pgoyette zero, then we'll put DT_RELA at the end of
12510 1.13.12.2 pgoyette DT_JMPREL. glibc will interpret the end of
12511 1.13.12.2 pgoyette DT_RELA matching the end of DT_JMPREL as the
12512 1.13.12.2 pgoyette case where DT_RELA includes DT_JMPREL, and for
12513 1.13.12.2 pgoyette LD_BIND_NOW will decide that processing DT_RELA
12514 1.13.12.2 pgoyette will process the PLT relocs too. Net result:
12515 1.13.12.2 pgoyette No PLT relocs applied. */
12516 1.13.12.2 pgoyette sh_addr = 0;
12517 1.1 skrll
12518 1.1 skrll /* If .rela.plt is the first .rela section, exclude
12519 1.1 skrll it from DT_RELA. */
12520 1.1 skrll else if (sh_addr == (htab->srelplt->output_section->vma
12521 1.1 skrll + htab->srelplt->output_offset))
12522 1.1 skrll sh_addr += htab->srelplt->size;
12523 1.1 skrll }
12524 1.1 skrll
12525 1.1 skrll if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12526 1.1 skrll dyn.d_un.d_val = sh_size;
12527 1.1 skrll else
12528 1.1 skrll dyn.d_un.d_ptr = sh_addr;
12529 1.1 skrll break;
12530 1.9 christos }
12531 1.7 christos bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12532 1.7 christos }
12533 1.1 skrll }
12534 1.1 skrll
12535 1.1 skrll /* If we have created any dynamic sections, then output them. */
12536 1.1 skrll if (dynobj != NULL)
12537 1.1 skrll {
12538 1.1 skrll if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12539 1.1 skrll goto error_return;
12540 1.1 skrll
12541 1.1 skrll /* Check for DT_TEXTREL (late, in case the backend removes it). */
12542 1.1 skrll if (((info->warn_shared_textrel && bfd_link_pic (info))
12543 1.1 skrll || info->error_textrel)
12544 1.1 skrll && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12545 1.1 skrll {
12546 1.7 christos bfd_byte *dyncon, *dynconend;
12547 1.7 christos
12548 1.7 christos dyncon = o->contents;
12549 1.7 christos dynconend = o->contents + o->size;
12550 1.7 christos for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12551 1.7 christos {
12552 1.1 skrll Elf_Internal_Dyn dyn;
12553 1.1 skrll
12554 1.1 skrll bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12555 1.1 skrll
12556 1.1 skrll if (dyn.d_tag == DT_TEXTREL)
12557 1.1 skrll {
12558 1.1 skrll if (info->error_textrel)
12559 1.1 skrll info->callbacks->einfo
12560 1.1 skrll (_("%P%X: read-only segment has dynamic relocations.\n"));
12561 1.1 skrll else
12562 1.1 skrll info->callbacks->einfo
12563 1.1 skrll (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12564 1.1 skrll break;
12565 1.1 skrll }
12566 1.1 skrll }
12567 1.1 skrll }
12568 1.1 skrll
12569 1.13.12.2 pgoyette for (o = dynobj->sections; o != NULL; o = o->next)
12570 1.1 skrll {
12571 1.13.12.2 pgoyette if ((o->flags & SEC_HAS_CONTENTS) == 0
12572 1.1 skrll || o->size == 0
12573 1.7 christos || o->output_section == bfd_abs_section_ptr)
12574 1.1 skrll continue;
12575 1.1 skrll if ((o->flags & SEC_LINKER_CREATED) == 0)
12576 1.1 skrll {
12577 1.13 christos /* At this point, we are only interested in sections
12578 1.13 christos created by _bfd_elf_link_create_dynamic_sections. */
12579 1.1 skrll continue;
12580 1.1 skrll }
12581 1.1 skrll if (htab->stab_info.stabstr == o)
12582 1.1 skrll continue;
12583 1.1 skrll if (htab->eh_info.hdr_sec == o)
12584 1.1 skrll continue;
12585 1.1 skrll if (strcmp (o->name, ".dynstr") != 0)
12586 1.9 christos {
12587 1.9 christos if (! bfd_set_section_contents (abfd, o->output_section,
12588 1.1 skrll o->contents,
12589 1.1 skrll (file_ptr) o->output_offset
12590 1.13.12.2 pgoyette * bfd_octets_per_byte (abfd),
12591 1.1 skrll o->size))
12592 1.1 skrll goto error_return;
12593 1.1 skrll }
12594 1.1 skrll else
12595 1.1 skrll {
12596 1.13.12.2 pgoyette /* The contents of the .dynstr section are actually in a
12597 1.1 skrll stringtab. */
12598 1.1 skrll file_ptr off;
12599 1.1 skrll
12600 1.13.12.2 pgoyette off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12601 1.1 skrll if (bfd_seek (abfd, off, SEEK_SET) != 0
12602 1.1 skrll || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12603 1.1 skrll goto error_return;
12604 1.1 skrll }
12605 1.1 skrll }
12606 1.1 skrll }
12607 1.13.12.2 pgoyette
12608 1.1 skrll if (!info->resolve_section_groups)
12609 1.13.12.2 pgoyette {
12610 1.1 skrll bfd_boolean failed = FALSE;
12611 1.1 skrll
12612 1.1 skrll BFD_ASSERT (bfd_link_relocatable (info));
12613 1.9 christos bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12614 1.9 christos if (failed)
12615 1.1 skrll goto error_return;
12616 1.9 christos }
12617 1.1 skrll
12618 1.9 christos /* If we have optimized stabs strings, output them. */
12619 1.1 skrll if (htab->stab_info.stabstr != NULL)
12620 1.1 skrll {
12621 1.1 skrll if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12622 1.4 christos goto error_return;
12623 1.1 skrll }
12624 1.1 skrll
12625 1.1 skrll if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12626 1.1 skrll goto error_return;
12627 1.1 skrll
12628 1.1 skrll elf_final_link_free (abfd, &flinfo);
12629 1.1 skrll
12630 1.1 skrll elf_linker (abfd) = TRUE;
12631 1.1 skrll
12632 1.1 skrll if (attr_section)
12633 1.9 christos {
12634 1.1 skrll bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12635 1.1 skrll if (contents == NULL)
12636 1.1 skrll return FALSE; /* Bail out and fail. */
12637 1.1 skrll bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12638 1.1 skrll bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12639 1.1 skrll free (contents);
12640 1.1 skrll }
12641 1.1 skrll
12642 1.1 skrll return TRUE;
12643 1.1 skrll
12644 1.1 skrll error_return:
12645 1.1 skrll elf_final_link_free (abfd, &flinfo);
12646 1.1 skrll return FALSE;
12647 1.1 skrll }
12648 1.1 skrll
12649 1.1 skrll /* Initialize COOKIE for input bfd ABFD. */
12651 1.1 skrll
12652 1.1 skrll static bfd_boolean
12653 1.1 skrll init_reloc_cookie (struct elf_reloc_cookie *cookie,
12654 1.1 skrll struct bfd_link_info *info, bfd *abfd)
12655 1.1 skrll {
12656 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
12657 1.1 skrll const struct elf_backend_data *bed;
12658 1.1 skrll
12659 1.1 skrll bed = get_elf_backend_data (abfd);
12660 1.1 skrll symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12661 1.1 skrll
12662 1.1 skrll cookie->abfd = abfd;
12663 1.1 skrll cookie->sym_hashes = elf_sym_hashes (abfd);
12664 1.1 skrll cookie->bad_symtab = elf_bad_symtab (abfd);
12665 1.1 skrll if (cookie->bad_symtab)
12666 1.1 skrll {
12667 1.1 skrll cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12668 1.1 skrll cookie->extsymoff = 0;
12669 1.1 skrll }
12670 1.1 skrll else
12671 1.1 skrll {
12672 1.1 skrll cookie->locsymcount = symtab_hdr->sh_info;
12673 1.1 skrll cookie->extsymoff = symtab_hdr->sh_info;
12674 1.1 skrll }
12675 1.1 skrll
12676 1.1 skrll if (bed->s->arch_size == 32)
12677 1.1 skrll cookie->r_sym_shift = 8;
12678 1.1 skrll else
12679 1.1 skrll cookie->r_sym_shift = 32;
12680 1.1 skrll
12681 1.1 skrll cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12682 1.1 skrll if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12683 1.1 skrll {
12684 1.1 skrll cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12685 1.1 skrll cookie->locsymcount, 0,
12686 1.1 skrll NULL, NULL, NULL);
12687 1.1 skrll if (cookie->locsyms == NULL)
12688 1.1 skrll {
12689 1.1 skrll info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12690 1.1 skrll return FALSE;
12691 1.1 skrll }
12692 1.1 skrll if (info->keep_memory)
12693 1.1 skrll symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12694 1.1 skrll }
12695 1.1 skrll return TRUE;
12696 1.1 skrll }
12697 1.1 skrll
12698 1.1 skrll /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12699 1.1 skrll
12700 1.1 skrll static void
12701 1.1 skrll fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12702 1.1 skrll {
12703 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
12704 1.1 skrll
12705 1.1 skrll symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12706 1.1 skrll if (cookie->locsyms != NULL
12707 1.1 skrll && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12708 1.1 skrll free (cookie->locsyms);
12709 1.1 skrll }
12710 1.1 skrll
12711 1.1 skrll /* Initialize the relocation information in COOKIE for input section SEC
12712 1.1 skrll of input bfd ABFD. */
12713 1.1 skrll
12714 1.1 skrll static bfd_boolean
12715 1.1 skrll init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12716 1.1 skrll struct bfd_link_info *info, bfd *abfd,
12717 1.1 skrll asection *sec)
12718 1.13.12.2 pgoyette {
12719 1.1 skrll if (sec->reloc_count == 0)
12720 1.1 skrll {
12721 1.1 skrll cookie->rels = NULL;
12722 1.1 skrll cookie->relend = NULL;
12723 1.1 skrll }
12724 1.1 skrll else
12725 1.1 skrll {
12726 1.1 skrll cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12727 1.1 skrll info->keep_memory);
12728 1.1 skrll if (cookie->rels == NULL)
12729 1.1 skrll return FALSE;
12730 1.1 skrll cookie->rel = cookie->rels;
12731 1.1 skrll cookie->relend = cookie->rels + sec->reloc_count;
12732 1.1 skrll }
12733 1.1 skrll cookie->rel = cookie->rels;
12734 1.1 skrll return TRUE;
12735 1.1 skrll }
12736 1.1 skrll
12737 1.1 skrll /* Free the memory allocated by init_reloc_cookie_rels,
12738 1.1 skrll if appropriate. */
12739 1.1 skrll
12740 1.1 skrll static void
12741 1.1 skrll fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12742 1.1 skrll asection *sec)
12743 1.1 skrll {
12744 1.1 skrll if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12745 1.1 skrll free (cookie->rels);
12746 1.1 skrll }
12747 1.1 skrll
12748 1.1 skrll /* Initialize the whole of COOKIE for input section SEC. */
12749 1.1 skrll
12750 1.1 skrll static bfd_boolean
12751 1.1 skrll init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12752 1.1 skrll struct bfd_link_info *info,
12753 1.1 skrll asection *sec)
12754 1.1 skrll {
12755 1.1 skrll if (!init_reloc_cookie (cookie, info, sec->owner))
12756 1.1 skrll goto error1;
12757 1.1 skrll if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12758 1.1 skrll goto error2;
12759 1.1 skrll return TRUE;
12760 1.1 skrll
12761 1.1 skrll error2:
12762 1.1 skrll fini_reloc_cookie (cookie, sec->owner);
12763 1.1 skrll error1:
12764 1.1 skrll return FALSE;
12765 1.1 skrll }
12766 1.1 skrll
12767 1.1 skrll /* Free the memory allocated by init_reloc_cookie_for_section,
12768 1.1 skrll if appropriate. */
12769 1.1 skrll
12770 1.1 skrll static void
12771 1.1 skrll fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12772 1.1 skrll asection *sec)
12773 1.1 skrll {
12774 1.1 skrll fini_reloc_cookie_rels (cookie, sec);
12775 1.1 skrll fini_reloc_cookie (cookie, sec->owner);
12776 1.1 skrll }
12777 1.1 skrll
12778 1.1 skrll /* Garbage collect unused sections. */
12780 1.1 skrll
12781 1.1 skrll /* Default gc_mark_hook. */
12782 1.1 skrll
12783 1.1 skrll asection *
12784 1.1 skrll _bfd_elf_gc_mark_hook (asection *sec,
12785 1.1 skrll struct bfd_link_info *info ATTRIBUTE_UNUSED,
12786 1.1 skrll Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12787 1.1 skrll struct elf_link_hash_entry *h,
12788 1.1 skrll Elf_Internal_Sym *sym)
12789 1.1 skrll {
12790 1.1 skrll if (h != NULL)
12791 1.1 skrll {
12792 1.1 skrll switch (h->root.type)
12793 1.1 skrll {
12794 1.1 skrll case bfd_link_hash_defined:
12795 1.1 skrll case bfd_link_hash_defweak:
12796 1.1 skrll return h->root.u.def.section;
12797 1.13.12.2 pgoyette
12798 1.13 christos case bfd_link_hash_common:
12799 1.13.12.2 pgoyette return h->root.u.c.p->section;
12800 1.13.12.2 pgoyette
12801 1.13.12.2 pgoyette default:
12802 1.13.12.2 pgoyette break;
12803 1.13.12.2 pgoyette }
12804 1.13.12.2 pgoyette }
12805 1.13 christos else
12806 1.13.12.2 pgoyette return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12807 1.13.12.2 pgoyette
12808 1.13.12.2 pgoyette return NULL;
12809 1.13.12.2 pgoyette }
12810 1.13.12.2 pgoyette
12811 1.13 christos /* Return the global debug definition section. */
12812 1.13.12.2 pgoyette
12813 1.13 christos static asection *
12814 1.13 christos elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12815 1.1 skrll struct bfd_link_info *info ATTRIBUTE_UNUSED,
12816 1.1 skrll Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12817 1.1 skrll struct elf_link_hash_entry *h,
12818 1.1 skrll Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12819 1.1 skrll {
12820 1.1 skrll if (h != NULL
12821 1.1 skrll && (h->root.type == bfd_link_hash_defined
12822 1.9 christos || h->root.type == bfd_link_hash_defweak)
12823 1.9 christos && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12824 1.1 skrll return h->root.u.def.section;
12825 1.1 skrll
12826 1.1 skrll return NULL;
12827 1.1 skrll }
12828 1.1 skrll
12829 1.4 christos /* COOKIE->rel describes a relocation against section SEC, which is
12830 1.1 skrll a section we've decided to keep. Return the section that contains
12831 1.1 skrll the relocation symbol, or NULL if no section contains it. */
12832 1.1 skrll
12833 1.1 skrll asection *
12834 1.1 skrll _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12835 1.1 skrll elf_gc_mark_hook_fn gc_mark_hook,
12836 1.9 christos struct elf_reloc_cookie *cookie,
12837 1.9 christos bfd_boolean *start_stop)
12838 1.9 christos {
12839 1.9 christos unsigned long r_symndx;
12840 1.9 christos struct elf_link_hash_entry *h;
12841 1.9 christos
12842 1.1 skrll r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12843 1.1 skrll if (r_symndx == STN_UNDEF)
12844 1.1 skrll return NULL;
12845 1.7 christos
12846 1.7 christos if (r_symndx >= cookie->locsymcount
12847 1.7 christos || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12848 1.7 christos {
12849 1.7 christos h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12850 1.13.12.2 pgoyette if (h == NULL)
12851 1.13.12.2 pgoyette {
12852 1.9 christos info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12853 1.13 christos sec->owner);
12854 1.9 christos return NULL;
12855 1.13.12.2 pgoyette }
12856 1.13.12.2 pgoyette while (h->root.type == bfd_link_hash_indirect
12857 1.13.12.2 pgoyette || h->root.type == bfd_link_hash_warning)
12858 1.13.12.2 pgoyette h = (struct elf_link_hash_entry *) h->root.u.i.link;
12859 1.9 christos h->mark = 1;
12860 1.13.12.2 pgoyette /* If this symbol is weak and there is a non-weak definition, we
12861 1.13 christos keep the non-weak definition because many backends put
12862 1.13 christos dynamic reloc info on the non-weak definition for code
12863 1.9 christos handling copy relocs. */
12864 1.9 christos if (h->is_weakalias)
12865 1.9 christos weakdef (h)->mark = 1;
12866 1.1 skrll
12867 1.1 skrll if (start_stop != NULL)
12868 1.1 skrll {
12869 1.1 skrll /* To work around a glibc bug, mark XXX input sections
12870 1.1 skrll when there is a reference to __start_XXX or __stop_XXX
12871 1.1 skrll symbols. */
12872 1.1 skrll if (h->start_stop)
12873 1.1 skrll {
12874 1.1 skrll asection *s = h->u2.start_stop_section;
12875 1.1 skrll *start_stop = !s->gc_mark;
12876 1.1 skrll return s;
12877 1.1 skrll }
12878 1.1 skrll }
12879 1.1 skrll
12880 1.1 skrll return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12881 1.1 skrll }
12882 1.1 skrll
12883 1.1 skrll return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12884 1.9 christos &cookie->locsyms[r_symndx]);
12885 1.1 skrll }
12886 1.9 christos
12887 1.9 christos /* COOKIE->rel describes a relocation against section SEC, which is
12888 1.1 skrll a section we've decided to keep. Mark the section that contains
12889 1.9 christos the relocation symbol. */
12890 1.9 christos
12891 1.9 christos bfd_boolean
12892 1.9 christos _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12893 1.9 christos asection *sec,
12894 1.9 christos elf_gc_mark_hook_fn gc_mark_hook,
12895 1.9 christos struct elf_reloc_cookie *cookie)
12896 1.9 christos {
12897 1.9 christos asection *rsec;
12898 1.9 christos bfd_boolean start_stop = FALSE;
12899 1.9 christos
12900 1.1 skrll rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12901 1.1 skrll while (rsec != NULL)
12902 1.1 skrll {
12903 1.1 skrll if (!rsec->gc_mark)
12904 1.1 skrll {
12905 1.1 skrll if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12906 1.1 skrll || (rsec->owner->flags & DYNAMIC) != 0)
12907 1.1 skrll rsec->gc_mark = 1;
12908 1.1 skrll else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12909 1.1 skrll return FALSE;
12910 1.1 skrll }
12911 1.1 skrll if (!start_stop)
12912 1.1 skrll break;
12913 1.1 skrll rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12914 1.1 skrll }
12915 1.1 skrll return TRUE;
12916 1.1 skrll }
12917 1.1 skrll
12918 1.1 skrll /* The mark phase of garbage collection. For a given section, mark
12919 1.1 skrll it and any sections in this section's group, and all the sections
12920 1.1 skrll which define symbols to which it refers. */
12921 1.1 skrll
12922 1.1 skrll bfd_boolean
12923 1.1 skrll _bfd_elf_gc_mark (struct bfd_link_info *info,
12924 1.1 skrll asection *sec,
12925 1.1 skrll elf_gc_mark_hook_fn gc_mark_hook)
12926 1.1 skrll {
12927 1.1 skrll bfd_boolean ret;
12928 1.1 skrll asection *group_sec, *eh_frame;
12929 1.1 skrll
12930 1.1 skrll sec->gc_mark = 1;
12931 1.1 skrll
12932 1.1 skrll /* Mark all the sections in the group. */
12933 1.1 skrll group_sec = elf_section_data (sec)->next_in_group;
12934 1.1 skrll if (group_sec && !group_sec->gc_mark)
12935 1.1 skrll if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12936 1.1 skrll return FALSE;
12937 1.1 skrll
12938 1.1 skrll /* Look through the section relocs. */
12939 1.1 skrll ret = TRUE;
12940 1.1 skrll eh_frame = elf_eh_frame_section (sec->owner);
12941 1.1 skrll if ((sec->flags & SEC_RELOC) != 0
12942 1.1 skrll && sec->reloc_count > 0
12943 1.1 skrll && sec != eh_frame)
12944 1.1 skrll {
12945 1.1 skrll struct elf_reloc_cookie cookie;
12946 1.1 skrll
12947 1.1 skrll if (!init_reloc_cookie_for_section (&cookie, info, sec))
12948 1.1 skrll ret = FALSE;
12949 1.1 skrll else
12950 1.1 skrll {
12951 1.1 skrll for (; cookie.rel < cookie.relend; cookie.rel++)
12952 1.1 skrll if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12953 1.1 skrll {
12954 1.1 skrll ret = FALSE;
12955 1.1 skrll break;
12956 1.1 skrll }
12957 1.1 skrll fini_reloc_cookie_for_section (&cookie, sec);
12958 1.1 skrll }
12959 1.1 skrll }
12960 1.1 skrll
12961 1.1 skrll if (ret && eh_frame && elf_fde_list (sec))
12962 1.9 christos {
12963 1.9 christos struct elf_reloc_cookie cookie;
12964 1.9 christos
12965 1.9 christos if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12966 1.9 christos ret = FALSE;
12967 1.1 skrll else
12968 1.1 skrll {
12969 1.1 skrll if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12970 1.9 christos gc_mark_hook, &cookie))
12971 1.9 christos ret = FALSE;
12972 1.9 christos fini_reloc_cookie_for_section (&cookie, eh_frame);
12973 1.9 christos }
12974 1.9 christos }
12975 1.9 christos
12976 1.9 christos eh_frame = elf_section_eh_frame_entry (sec);
12977 1.9 christos if (ret && eh_frame && !eh_frame->gc_mark)
12978 1.9 christos if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12979 1.9 christos ret = FALSE;
12980 1.9 christos
12981 1.9 christos return ret;
12982 1.9 christos }
12983 1.9 christos
12984 1.9 christos /* Scan and mark sections in a special or debug section group. */
12985 1.9 christos
12986 1.9 christos static void
12987 1.9 christos _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12988 1.9 christos {
12989 1.9 christos /* Point to first section of section group. */
12990 1.9 christos asection *ssec;
12991 1.9 christos /* Used to iterate the section group. */
12992 1.9 christos asection *msec;
12993 1.9 christos
12994 1.9 christos bfd_boolean is_special_grp = TRUE;
12995 1.9 christos bfd_boolean is_debug_grp = TRUE;
12996 1.9 christos
12997 1.9 christos /* First scan to see if group contains any section other than debug
12998 1.9 christos and special section. */
12999 1.9 christos ssec = msec = elf_next_in_group (grp);
13000 1.9 christos do
13001 1.9 christos {
13002 1.9 christos if ((msec->flags & SEC_DEBUGGING) == 0)
13003 1.9 christos is_debug_grp = FALSE;
13004 1.9 christos
13005 1.9 christos if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13006 1.9 christos is_special_grp = FALSE;
13007 1.9 christos
13008 1.9 christos msec = elf_next_in_group (msec);
13009 1.9 christos }
13010 1.9 christos while (msec != ssec);
13011 1.7 christos
13012 1.7 christos /* If this is a pure debug section group or pure special section group,
13013 1.7 christos keep all sections in this group. */
13014 1.7 christos if (is_debug_grp || is_special_grp)
13015 1.7 christos {
13016 1.7 christos do
13017 1.7 christos {
13018 1.7 christos msec->gc_mark = 1;
13019 1.9 christos msec = elf_next_in_group (msec);
13020 1.7 christos }
13021 1.7 christos while (msec != ssec);
13022 1.7 christos }
13023 1.9 christos }
13024 1.13.12.2 pgoyette
13025 1.7 christos /* Keep debug and special sections. */
13026 1.7 christos
13027 1.7 christos bfd_boolean
13028 1.13.12.2 pgoyette _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13029 1.13.12.2 pgoyette elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13030 1.13.12.2 pgoyette {
13031 1.7 christos bfd *ibfd;
13032 1.9 christos
13033 1.9 christos for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13034 1.9 christos {
13035 1.13.12.2 pgoyette asection *isec;
13036 1.7 christos bfd_boolean some_kept;
13037 1.7 christos bfd_boolean debug_frag_seen;
13038 1.7 christos bfd_boolean has_kept_debug_info;
13039 1.7 christos
13040 1.13.12.2 pgoyette if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13041 1.13.12.2 pgoyette continue;
13042 1.13.12.2 pgoyette isec = ibfd->sections;
13043 1.7 christos if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13044 1.9 christos continue;
13045 1.13.12.2 pgoyette
13046 1.9 christos /* Ensure all linker created sections are kept,
13047 1.9 christos see if any other section is already marked,
13048 1.9 christos and note if we have any fragmented debug sections. */
13049 1.7 christos debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13050 1.7 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13051 1.13.12.2 pgoyette {
13052 1.13.12.2 pgoyette if ((isec->flags & SEC_LINKER_CREATED) != 0)
13053 1.7 christos isec->gc_mark = 1;
13054 1.7 christos else if (isec->gc_mark
13055 1.7 christos && (isec->flags & SEC_ALLOC) != 0
13056 1.7 christos && elf_section_type (isec) != SHT_NOTE)
13057 1.9 christos some_kept = TRUE;
13058 1.9 christos
13059 1.9 christos if (!debug_frag_seen
13060 1.9 christos && (isec->flags & SEC_DEBUGGING)
13061 1.9 christos && CONST_STRNEQ (isec->name, ".debug_line."))
13062 1.9 christos debug_frag_seen = TRUE;
13063 1.9 christos }
13064 1.9 christos
13065 1.9 christos /* If no non-note alloc section in this file will be kept, then
13066 1.9 christos we can toss out the debug and special sections. */
13067 1.13.12.2 pgoyette if (!some_kept)
13068 1.13.12.2 pgoyette continue;
13069 1.9 christos
13070 1.9 christos /* Keep debug and special sections like .comment when they are
13071 1.9 christos not part of a group. Also keep section groups that contain
13072 1.9 christos just debug sections or special sections. */
13073 1.9 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13074 1.13.12.2 pgoyette {
13075 1.13.12.2 pgoyette if ((isec->flags & SEC_GROUP) != 0)
13076 1.13.12.2 pgoyette _bfd_elf_gc_mark_debug_special_section_group (isec);
13077 1.13.12.2 pgoyette else if (((isec->flags & SEC_DEBUGGING) != 0
13078 1.13.12.2 pgoyette || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13079 1.13.12.2 pgoyette && elf_next_in_group (isec) == NULL)
13080 1.13.12.2 pgoyette isec->gc_mark = 1;
13081 1.13.12.2 pgoyette if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13082 1.13.12.2 pgoyette has_kept_debug_info = TRUE;
13083 1.13.12.2 pgoyette }
13084 1.13.12.2 pgoyette
13085 1.13.12.2 pgoyette /* Look for CODE sections which are going to be discarded,
13086 1.13.12.2 pgoyette and find and discard any fragmented debug sections which
13087 1.13.12.2 pgoyette are associated with that code section. */
13088 1.13.12.2 pgoyette if (debug_frag_seen)
13089 1.13.12.2 pgoyette for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13090 1.13.12.2 pgoyette if ((isec->flags & SEC_CODE) != 0
13091 1.9 christos && isec->gc_mark == 0)
13092 1.13.12.2 pgoyette {
13093 1.13.12.2 pgoyette unsigned int ilen;
13094 1.13.12.2 pgoyette asection *dsec;
13095 1.9 christos
13096 1.13.12.2 pgoyette ilen = strlen (isec->name);
13097 1.9 christos
13098 1.13.12.2 pgoyette /* Association is determined by the name of the debug
13099 1.13.12.2 pgoyette section containing the name of the code section as
13100 1.13.12.2 pgoyette a suffix. For example .debug_line.text.foo is a
13101 1.9 christos debug section associated with .text.foo. */
13102 1.13.12.2 pgoyette for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13103 1.9 christos {
13104 1.1 skrll unsigned int dlen;
13105 1.13.12.2 pgoyette
13106 1.13.12.2 pgoyette if (dsec->gc_mark == 0
13107 1.13.12.2 pgoyette || (dsec->flags & SEC_DEBUGGING) == 0)
13108 1.13.12.2 pgoyette continue;
13109 1.13.12.2 pgoyette
13110 1.13.12.2 pgoyette dlen = strlen (dsec->name);
13111 1.13.12.2 pgoyette
13112 1.13.12.2 pgoyette if (dlen > ilen
13113 1.1 skrll && strncmp (dsec->name + (dlen - ilen),
13114 1.1 skrll isec->name, ilen) == 0)
13115 1.1 skrll dsec->gc_mark = 0;
13116 1.1 skrll }
13117 1.1 skrll }
13118 1.1 skrll
13119 1.1 skrll /* Mark debug sections referenced by kept debug sections. */
13120 1.1 skrll if (has_kept_debug_info)
13121 1.1 skrll for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13122 1.1 skrll if (isec->gc_mark
13123 1.9 christos && (isec->flags & SEC_DEBUGGING) != 0)
13124 1.1 skrll if (!_bfd_elf_gc_mark (info, isec,
13125 1.1 skrll elf_gc_mark_debug_section))
13126 1.1 skrll return FALSE;
13127 1.9 christos }
13128 1.13.12.2 pgoyette return TRUE;
13129 1.9 christos }
13130 1.1 skrll
13131 1.13.12.2 pgoyette static bfd_boolean
13132 1.13.12.2 pgoyette elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13133 1.13.12.2 pgoyette {
13134 1.1 skrll bfd *sub;
13135 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13136 1.1 skrll
13137 1.4 christos for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13138 1.4 christos {
13139 1.4 christos asection *o;
13140 1.4 christos
13141 1.4 christos if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13142 1.4 christos || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13143 1.4 christos || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13144 1.4 christos continue;
13145 1.4 christos o = sub->sections;
13146 1.1 skrll if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13147 1.1 skrll continue;
13148 1.1 skrll
13149 1.1 skrll for (o = sub->sections; o != NULL; o = o->next)
13150 1.1 skrll {
13151 1.1 skrll /* When any section in a section group is kept, we keep all
13152 1.1 skrll sections in the section group. If the first member of
13153 1.1 skrll the section group is excluded, we will also exclude the
13154 1.1 skrll group section. */
13155 1.1 skrll if (o->flags & SEC_GROUP)
13156 1.1 skrll {
13157 1.1 skrll asection *first = elf_next_in_group (o);
13158 1.1 skrll o->gc_mark = first->gc_mark;
13159 1.13.12.2 pgoyette }
13160 1.13.12.2 pgoyette
13161 1.13.12.2 pgoyette if (o->gc_mark)
13162 1.1 skrll continue;
13163 1.1 skrll
13164 1.1 skrll /* Skip sweeping sections already excluded. */
13165 1.1 skrll if (o->flags & SEC_EXCLUDE)
13166 1.1 skrll continue;
13167 1.1 skrll
13168 1.1 skrll /* Since this is early in the link process, it is simple
13169 1.1 skrll to remove a section from the output. */
13170 1.1 skrll o->flags |= SEC_EXCLUDE;
13171 1.1 skrll
13172 1.1 skrll if (info->print_gc_sections && o->size != 0)
13173 1.1 skrll /* xgettext:c-format */
13174 1.1 skrll _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13175 1.13.12.2 pgoyette o, sub);
13176 1.13.12.2 pgoyette }
13177 1.13.12.2 pgoyette }
13178 1.1 skrll
13179 1.1 skrll return TRUE;
13180 1.1 skrll }
13181 1.13.12.2 pgoyette
13182 1.1 skrll /* Propagate collected vtable information. This is called through
13183 1.1 skrll elf_link_hash_traverse. */
13184 1.1 skrll
13185 1.13.12.2 pgoyette static bfd_boolean
13186 1.1 skrll elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13187 1.1 skrll {
13188 1.1 skrll /* Those that are not vtables. */
13189 1.13.12.2 pgoyette if (h->start_stop
13190 1.1 skrll || h->u2.vtable == NULL
13191 1.13.12.2 pgoyette || h->u2.vtable->parent == NULL)
13192 1.1 skrll return TRUE;
13193 1.1 skrll
13194 1.1 skrll /* Those vtables that do not have parents, we cannot merge. */
13195 1.13.12.2 pgoyette if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13196 1.13.12.2 pgoyette return TRUE;
13197 1.1 skrll
13198 1.1 skrll /* If we've already been done, exit. */
13199 1.1 skrll if (h->u2.vtable->used && h->u2.vtable->used[-1])
13200 1.1 skrll return TRUE;
13201 1.1 skrll
13202 1.1 skrll /* Make sure the parent's table is up to date. */
13203 1.1 skrll elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13204 1.13.12.2 pgoyette
13205 1.1 skrll if (h->u2.vtable->used == NULL)
13206 1.13.12.2 pgoyette {
13207 1.1 skrll /* None of this table's entries were referenced. Re-use the
13208 1.1 skrll parent's table. */
13209 1.1 skrll h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13210 1.1 skrll h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13211 1.1 skrll }
13212 1.1 skrll else
13213 1.1 skrll {
13214 1.13.12.2 pgoyette size_t n;
13215 1.1 skrll bfd_boolean *cu, *pu;
13216 1.1 skrll
13217 1.1 skrll /* Or the parent's entries into ours. */
13218 1.1 skrll cu = h->u2.vtable->used;
13219 1.1 skrll cu[-1] = TRUE;
13220 1.1 skrll pu = h->u2.vtable->parent->u2.vtable->used;
13221 1.1 skrll if (pu != NULL)
13222 1.1 skrll {
13223 1.1 skrll const struct elf_backend_data *bed;
13224 1.1 skrll unsigned int log_file_align;
13225 1.1 skrll
13226 1.1 skrll bed = get_elf_backend_data (h->root.u.def.section->owner);
13227 1.1 skrll log_file_align = bed->s->log_file_align;
13228 1.1 skrll n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13229 1.1 skrll while (n--)
13230 1.1 skrll {
13231 1.1 skrll if (*pu)
13232 1.1 skrll *cu = TRUE;
13233 1.1 skrll pu++;
13234 1.1 skrll cu++;
13235 1.1 skrll }
13236 1.1 skrll }
13237 1.1 skrll }
13238 1.1 skrll
13239 1.13.12.2 pgoyette return TRUE;
13240 1.13.12.2 pgoyette }
13241 1.13.12.2 pgoyette
13242 1.1 skrll static bfd_boolean
13243 1.1 skrll elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13244 1.1 skrll {
13245 1.1 skrll asection *sec;
13246 1.1 skrll bfd_vma hstart, hend;
13247 1.1 skrll Elf_Internal_Rela *relstart, *relend, *rel;
13248 1.1 skrll const struct elf_backend_data *bed;
13249 1.1 skrll unsigned int log_file_align;
13250 1.1 skrll
13251 1.1 skrll /* Take care of both those symbols that do not describe vtables as
13252 1.1 skrll well as those that are not loaded. */
13253 1.1 skrll if (h->start_stop
13254 1.1 skrll || h->u2.vtable == NULL
13255 1.1 skrll || h->u2.vtable->parent == NULL)
13256 1.1 skrll return TRUE;
13257 1.13.12.2 pgoyette
13258 1.1 skrll BFD_ASSERT (h->root.type == bfd_link_hash_defined
13259 1.1 skrll || h->root.type == bfd_link_hash_defweak);
13260 1.1 skrll
13261 1.1 skrll sec = h->root.u.def.section;
13262 1.1 skrll hstart = h->root.u.def.value;
13263 1.13.12.2 pgoyette hend = hstart + h->size;
13264 1.13.12.2 pgoyette
13265 1.1 skrll relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13266 1.1 skrll if (!relstart)
13267 1.13.12.2 pgoyette return *(bfd_boolean *) okp = FALSE;
13268 1.1 skrll bed = get_elf_backend_data (sec->owner);
13269 1.1 skrll log_file_align = bed->s->log_file_align;
13270 1.1 skrll
13271 1.1 skrll relend = relstart + sec->reloc_count;
13272 1.1 skrll
13273 1.1 skrll for (rel = relstart; rel < relend; ++rel)
13274 1.1 skrll if (rel->r_offset >= hstart && rel->r_offset < hend)
13275 1.1 skrll {
13276 1.1 skrll /* If the entry is in use, do nothing. */
13277 1.1 skrll if (h->u2.vtable->used
13278 1.1 skrll && (rel->r_offset - hstart) < h->u2.vtable->size)
13279 1.1 skrll {
13280 1.1 skrll bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13281 1.1 skrll if (h->u2.vtable->used[entry])
13282 1.1 skrll continue;
13283 1.1 skrll }
13284 1.1 skrll /* Otherwise, kill it. */
13285 1.9 christos rel->r_offset = rel->r_info = rel->r_addend = 0;
13286 1.1 skrll }
13287 1.1 skrll
13288 1.1 skrll return TRUE;
13289 1.13.12.2 pgoyette }
13290 1.9 christos
13291 1.1 skrll /* Mark sections containing dynamically referenced symbols. When
13292 1.7 christos building shared libraries, we must assume that any visible symbol is
13293 1.9 christos referenced. */
13294 1.13.12.2 pgoyette
13295 1.9 christos bfd_boolean
13296 1.9 christos bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13297 1.9 christos {
13298 1.9 christos struct bfd_link_info *info = (struct bfd_link_info *) inf;
13299 1.9 christos struct bfd_elf_dynamic_list *d = info->dynamic_list;
13300 1.7 christos
13301 1.7 christos if ((h->root.type == bfd_link_hash_defined
13302 1.1 skrll || h->root.type == bfd_link_hash_defweak)
13303 1.1 skrll && ((h->ref_dynamic && !h->forced_local)
13304 1.1 skrll || ((h->def_regular || ELF_COMMON_DEF_P (h))
13305 1.1 skrll && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13306 1.1 skrll && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13307 1.1 skrll && (!bfd_link_executable (info)
13308 1.1 skrll || info->gc_keep_exported
13309 1.1 skrll || info->export_dynamic
13310 1.1 skrll || (h->dynamic
13311 1.1 skrll && d != NULL
13312 1.1 skrll && (*d->match) (&d->head, NULL, h->root.root.string)))
13313 1.1 skrll && (h->versioned >= versioned
13314 1.1 skrll || !bfd_hide_sym_by_version (info->version_info,
13315 1.1 skrll h->root.root.string)))))
13316 1.1 skrll h->root.u.def.section->flags |= SEC_KEEP;
13317 1.1 skrll
13318 1.1 skrll return TRUE;
13319 1.1 skrll }
13320 1.1 skrll
13321 1.1 skrll /* Keep all sections containing symbols undefined on the command-line,
13322 1.1 skrll and the section containing the entry symbol. */
13323 1.1 skrll
13324 1.1 skrll void
13325 1.13.12.2 pgoyette _bfd_elf_gc_keep (struct bfd_link_info *info)
13326 1.13.12.2 pgoyette {
13327 1.1 skrll struct bfd_sym_chain *sym;
13328 1.1 skrll
13329 1.1 skrll for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13330 1.1 skrll {
13331 1.9 christos struct elf_link_hash_entry *h;
13332 1.9 christos
13333 1.9 christos h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13334 1.9 christos FALSE, FALSE, FALSE);
13335 1.9 christos
13336 1.9 christos if (h != NULL
13337 1.9 christos && (h->root.type == bfd_link_hash_defined
13338 1.9 christos || h->root.type == bfd_link_hash_defweak)
13339 1.9 christos && !bfd_is_abs_section (h->root.u.def.section)
13340 1.9 christos && !bfd_is_und_section (h->root.u.def.section))
13341 1.9 christos h->root.u.def.section->flags |= SEC_KEEP;
13342 1.9 christos }
13343 1.9 christos }
13344 1.13.12.2 pgoyette
13345 1.13.12.2 pgoyette bfd_boolean
13346 1.13.12.2 pgoyette bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13347 1.9 christos struct bfd_link_info *info)
13348 1.9 christos {
13349 1.9 christos bfd *ibfd = info->input_bfds;
13350 1.9 christos
13351 1.9 christos for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13352 1.9 christos {
13353 1.9 christos asection *sec;
13354 1.9 christos struct elf_reloc_cookie cookie;
13355 1.9 christos
13356 1.9 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13357 1.9 christos continue;
13358 1.9 christos sec = ibfd->sections;
13359 1.9 christos if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13360 1.9 christos continue;
13361 1.9 christos
13362 1.9 christos if (!init_reloc_cookie (&cookie, info, ibfd))
13363 1.9 christos return FALSE;
13364 1.1 skrll
13365 1.1 skrll for (sec = ibfd->sections; sec; sec = sec->next)
13366 1.1 skrll {
13367 1.1 skrll if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13368 1.1 skrll && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13369 1.1 skrll {
13370 1.1 skrll _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13371 1.1 skrll fini_reloc_cookie_rels (&cookie, sec);
13372 1.1 skrll }
13373 1.9 christos }
13374 1.1 skrll }
13375 1.1 skrll return TRUE;
13376 1.1 skrll }
13377 1.1 skrll
13378 1.13.12.2 pgoyette /* Do mark and sweep of unused sections. */
13379 1.1 skrll
13380 1.1 skrll bfd_boolean
13381 1.1 skrll bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13382 1.1 skrll {
13383 1.9 christos bfd_boolean ok = TRUE;
13384 1.1 skrll bfd *sub;
13385 1.1 skrll elf_gc_mark_hook_fn gc_mark_hook;
13386 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13387 1.9 christos struct elf_link_hash_table *htab;
13388 1.9 christos
13389 1.9 christos if (!bed->can_gc_sections
13390 1.1 skrll || !is_elf_hash_table (info->hash))
13391 1.1 skrll {
13392 1.1 skrll _bfd_error_handler(_("Warning: gc-sections option ignored"));
13393 1.1 skrll return TRUE;
13394 1.13.12.2 pgoyette }
13395 1.13.12.2 pgoyette
13396 1.13.12.2 pgoyette bed->gc_keep (info);
13397 1.1 skrll htab = elf_hash_table (info);
13398 1.7 christos
13399 1.1 skrll /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13400 1.1 skrll at the .eh_frame section if we can mark the FDEs individually. */
13401 1.7 christos for (sub = info->input_bfds;
13402 1.7 christos info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13403 1.1 skrll sub = sub->link.next)
13404 1.1 skrll {
13405 1.9 christos asection *sec;
13406 1.1 skrll struct elf_reloc_cookie cookie;
13407 1.1 skrll
13408 1.1 skrll sec = sub->sections;
13409 1.1 skrll if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13410 1.9 christos continue;
13411 1.1 skrll sec = bfd_get_section_by_name (sub, ".eh_frame");
13412 1.1 skrll while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13413 1.1 skrll {
13414 1.1 skrll _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13415 1.9 christos if (elf_section_data (sec)->sec_info
13416 1.1 skrll && (sec->flags & SEC_LINKER_CREATED) == 0)
13417 1.1 skrll elf_eh_frame_section (sub) = sec;
13418 1.1 skrll fini_reloc_cookie_for_section (&cookie, sec);
13419 1.1 skrll sec = bfd_get_next_section_by_name (NULL, sec);
13420 1.13.12.2 pgoyette }
13421 1.9 christos }
13422 1.1 skrll
13423 1.1 skrll /* Apply transitive closure to the vtable entry usage info. */
13424 1.1 skrll elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13425 1.9 christos if (!ok)
13426 1.1 skrll return FALSE;
13427 1.1 skrll
13428 1.1 skrll /* Kill the vtable relocations that were not used. */
13429 1.9 christos elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13430 1.13.12.2 pgoyette if (!ok)
13431 1.9 christos return FALSE;
13432 1.1 skrll
13433 1.1 skrll /* Mark dynamically referenced symbols. */
13434 1.13.12.2 pgoyette if (htab->dynamic_sections_created || info->gc_keep_exported)
13435 1.13.12.2 pgoyette elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13436 1.13.12.2 pgoyette
13437 1.13.12.2 pgoyette /* Grovel through relocs to find out who stays ... */
13438 1.7 christos gc_mark_hook = bed->gc_mark_hook;
13439 1.7 christos for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13440 1.13.12.2 pgoyette {
13441 1.13.12.2 pgoyette asection *o;
13442 1.1 skrll
13443 1.7 christos if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13444 1.7 christos || elf_object_id (sub) != elf_hash_table_id (htab)
13445 1.7 christos || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13446 1.13.12.2 pgoyette continue;
13447 1.13.12.2 pgoyette
13448 1.13.12.2 pgoyette o = sub->sections;
13449 1.13.12.2 pgoyette if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13450 1.13.12.2 pgoyette continue;
13451 1.13.12.2 pgoyette
13452 1.13.12.2 pgoyette /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13453 1.7 christos Also treat note sections as a root, if the section is not part
13454 1.7 christos of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13455 1.7 christos well as FINI_ARRAY sections for ld -r. */
13456 1.7 christos for (o = sub->sections; o != NULL; o = o->next)
13457 1.7 christos if (!o->gc_mark
13458 1.7 christos && (o->flags & SEC_EXCLUDE) == 0
13459 1.1 skrll && ((o->flags & SEC_KEEP) != 0
13460 1.1 skrll || (bfd_link_relocatable (info)
13461 1.1 skrll && ((elf_section_data (o)->this_hdr.sh_type
13462 1.7 christos == SHT_PREINIT_ARRAY)
13463 1.1 skrll || (elf_section_data (o)->this_hdr.sh_type
13464 1.1 skrll == SHT_INIT_ARRAY)
13465 1.1 skrll || (elf_section_data (o)->this_hdr.sh_type
13466 1.1 skrll == SHT_FINI_ARRAY)))
13467 1.1 skrll || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13468 1.1 skrll && elf_next_in_group (o) == NULL )))
13469 1.1 skrll {
13470 1.1 skrll if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13471 1.1 skrll return FALSE;
13472 1.1 skrll }
13473 1.1 skrll }
13474 1.1 skrll
13475 1.1 skrll /* Allow the backend to mark additional target specific sections. */
13476 1.1 skrll bed->gc_mark_extra_sections (info, gc_mark_hook);
13477 1.1 skrll
13478 1.13 christos /* ... and mark SEC_EXCLUDE for those that go. */
13479 1.1 skrll return elf_gc_sweep (abfd, info);
13480 1.1 skrll }
13481 1.1 skrll
13482 1.1 skrll /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13484 1.1 skrll
13485 1.1 skrll bfd_boolean
13486 1.1 skrll bfd_elf_gc_record_vtinherit (bfd *abfd,
13487 1.1 skrll asection *sec,
13488 1.1 skrll struct elf_link_hash_entry *h,
13489 1.1 skrll bfd_vma offset)
13490 1.1 skrll {
13491 1.1 skrll struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13492 1.1 skrll struct elf_link_hash_entry **search, *child;
13493 1.1 skrll size_t extsymcount;
13494 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13495 1.1 skrll
13496 1.1 skrll /* The sh_info field of the symtab header tells us where the
13497 1.1 skrll external symbols start. We don't care about the local symbols at
13498 1.1 skrll this point. */
13499 1.1 skrll extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13500 1.1 skrll if (!elf_bad_symtab (abfd))
13501 1.1 skrll extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13502 1.1 skrll
13503 1.13.12.2 pgoyette sym_hashes = elf_sym_hashes (abfd);
13504 1.13.12.2 pgoyette sym_hashes_end = sym_hashes + extsymcount;
13505 1.13.12.2 pgoyette
13506 1.1 skrll /* Hunt down the child symbol, which is in this section at the same
13507 1.1 skrll offset as the relocation. */
13508 1.1 skrll for (search = sym_hashes; search != sym_hashes_end; ++search)
13509 1.1 skrll {
13510 1.13.12.2 pgoyette if ((child = *search) != NULL
13511 1.1 skrll && (child->root.type == bfd_link_hash_defined
13512 1.13.12.2 pgoyette || child->root.type == bfd_link_hash_defweak)
13513 1.13.12.2 pgoyette && child->root.u.def.section == sec
13514 1.13.12.2 pgoyette && child->root.u.def.value == offset)
13515 1.1 skrll goto win;
13516 1.1 skrll }
13517 1.1 skrll
13518 1.1 skrll /* xgettext:c-format */
13519 1.1 skrll _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13520 1.1 skrll abfd, sec, offset);
13521 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
13522 1.1 skrll return FALSE;
13523 1.1 skrll
13524 1.13.12.2 pgoyette win:
13525 1.1 skrll if (!child->u2.vtable)
13526 1.1 skrll {
13527 1.13.12.2 pgoyette child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13528 1.1 skrll bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13529 1.1 skrll if (!child->u2.vtable)
13530 1.1 skrll return FALSE;
13531 1.1 skrll }
13532 1.1 skrll if (!h)
13533 1.1 skrll {
13534 1.1 skrll /* This *should* only be the absolute section. It could potentially
13535 1.1 skrll be that someone has defined a non-global vtable though, which
13536 1.1 skrll would be bad. It isn't worth paging in the local symbols to be
13537 1.1 skrll sure though; that case should simply be handled by the assembler. */
13538 1.1 skrll
13539 1.1 skrll child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13540 1.1 skrll }
13541 1.1 skrll else
13542 1.1 skrll child->u2.vtable->parent = h;
13543 1.13.12.2 pgoyette
13544 1.1 skrll return TRUE;
13545 1.13.12.2 pgoyette }
13546 1.13.12.2 pgoyette
13547 1.13.12.2 pgoyette /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13548 1.1 skrll
13549 1.1 skrll bfd_boolean
13550 1.1 skrll bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13551 1.13.12.2 pgoyette asection *sec ATTRIBUTE_UNUSED,
13552 1.1 skrll struct elf_link_hash_entry *h,
13553 1.1 skrll bfd_vma addend)
13554 1.13.12.2 pgoyette {
13555 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13556 1.1 skrll unsigned int log_file_align = bed->s->log_file_align;
13557 1.1 skrll
13558 1.1 skrll if (!h->u2.vtable)
13559 1.1 skrll {
13560 1.1 skrll h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13561 1.1 skrll bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13562 1.1 skrll if (!h->u2.vtable)
13563 1.1 skrll return FALSE;
13564 1.1 skrll }
13565 1.1 skrll
13566 1.1 skrll if (addend >= h->u2.vtable->size)
13567 1.1 skrll {
13568 1.1 skrll size_t size, bytes, file_align;
13569 1.1 skrll bfd_boolean *ptr = h->u2.vtable->used;
13570 1.1 skrll
13571 1.1 skrll /* While the symbol is undefined, we have to be prepared to handle
13572 1.1 skrll a zero size. */
13573 1.1 skrll file_align = 1 << log_file_align;
13574 1.1 skrll if (h->root.type == bfd_link_hash_undefined)
13575 1.1 skrll size = addend + file_align;
13576 1.1 skrll else
13577 1.1 skrll {
13578 1.1 skrll size = h->size;
13579 1.4 christos if (addend >= size)
13580 1.1 skrll {
13581 1.1 skrll /* Oops! We've got a reference past the defined end of
13582 1.1 skrll the table. This is probably a bug -- shall we warn? */
13583 1.1 skrll size = addend + file_align;
13584 1.1 skrll }
13585 1.13.12.2 pgoyette }
13586 1.1 skrll size = (size + file_align - 1) & -file_align;
13587 1.1 skrll
13588 1.1 skrll /* Allocate one extra entry for use as a "done" flag for the
13589 1.1 skrll consolidation pass. */
13590 1.1 skrll bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13591 1.4 christos
13592 1.1 skrll if (ptr)
13593 1.1 skrll {
13594 1.1 skrll ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13595 1.1 skrll
13596 1.1 skrll if (ptr != NULL)
13597 1.13.12.2 pgoyette {
13598 1.13.12.2 pgoyette size_t oldbytes;
13599 1.1 skrll
13600 1.1 skrll oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13601 1.13.12.2 pgoyette * sizeof (bfd_boolean));
13602 1.1 skrll memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13603 1.1 skrll }
13604 1.1 skrll }
13605 1.1 skrll else
13606 1.7 christos ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13607 1.7 christos
13608 1.7 christos if (ptr == NULL)
13609 1.7 christos return FALSE;
13610 1.7 christos
13611 1.7 christos /* And arrange for that done flag to be at index -1. */
13612 1.7 christos h->u2.vtable->used = ptr + 1;
13613 1.7 christos h->u2.vtable->size = size;
13614 1.7 christos }
13615 1.7 christos
13616 1.7 christos h->u2.vtable->used[addend >> log_file_align] = TRUE;
13617 1.7 christos
13618 1.7 christos return TRUE;
13619 1.7 christos }
13620 1.7 christos
13621 1.7 christos /* Map an ELF section header flag to its corresponding string. */
13622 1.7 christos typedef struct
13623 1.7 christos {
13624 1.7 christos char *flag_name;
13625 1.7 christos flagword flag_value;
13626 1.7 christos } elf_flags_to_name_table;
13627 1.7 christos
13628 1.7 christos static elf_flags_to_name_table elf_flags_to_names [] =
13629 1.7 christos {
13630 1.7 christos { "SHF_WRITE", SHF_WRITE },
13631 1.7 christos { "SHF_ALLOC", SHF_ALLOC },
13632 1.7 christos { "SHF_EXECINSTR", SHF_EXECINSTR },
13633 1.7 christos { "SHF_MERGE", SHF_MERGE },
13634 1.7 christos { "SHF_STRINGS", SHF_STRINGS },
13635 1.7 christos { "SHF_INFO_LINK", SHF_INFO_LINK},
13636 1.7 christos { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13637 1.7 christos { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13638 1.7 christos { "SHF_GROUP", SHF_GROUP },
13639 1.7 christos { "SHF_TLS", SHF_TLS },
13640 1.7 christos { "SHF_MASKOS", SHF_MASKOS },
13641 1.7 christos { "SHF_EXCLUDE", SHF_EXCLUDE },
13642 1.7 christos };
13643 1.7 christos
13644 1.7 christos /* Returns TRUE if the section is to be included, otherwise FALSE. */
13645 1.7 christos bfd_boolean
13646 1.7 christos bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13647 1.7 christos struct flag_info *flaginfo,
13648 1.7 christos asection *section)
13649 1.7 christos {
13650 1.7 christos const bfd_vma sh_flags = elf_section_flags (section);
13651 1.7 christos
13652 1.7 christos if (!flaginfo->flags_initialized)
13653 1.7 christos {
13654 1.7 christos bfd *obfd = info->output_bfd;
13655 1.7 christos const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13656 1.7 christos struct flag_info_list *tf = flaginfo->flag_list;
13657 1.7 christos int with_hex = 0;
13658 1.7 christos int without_hex = 0;
13659 1.7 christos
13660 1.7 christos for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13661 1.7 christos {
13662 1.7 christos unsigned i;
13663 1.7 christos flagword (*lookup) (char *);
13664 1.7 christos
13665 1.7 christos lookup = bed->elf_backend_lookup_section_flags_hook;
13666 1.7 christos if (lookup != NULL)
13667 1.7 christos {
13668 1.7 christos flagword hexval = (*lookup) ((char *) tf->name);
13669 1.7 christos
13670 1.7 christos if (hexval != 0)
13671 1.7 christos {
13672 1.7 christos if (tf->with == with_flags)
13673 1.7 christos with_hex |= hexval;
13674 1.7 christos else if (tf->with == without_flags)
13675 1.7 christos without_hex |= hexval;
13676 1.7 christos tf->valid = TRUE;
13677 1.7 christos continue;
13678 1.7 christos }
13679 1.9 christos }
13680 1.7 christos for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13681 1.7 christos {
13682 1.7 christos if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13683 1.7 christos {
13684 1.7 christos if (tf->with == with_flags)
13685 1.7 christos with_hex |= elf_flags_to_names[i].flag_value;
13686 1.7 christos else if (tf->with == without_flags)
13687 1.7 christos without_hex |= elf_flags_to_names[i].flag_value;
13688 1.7 christos tf->valid = TRUE;
13689 1.7 christos break;
13690 1.7 christos }
13691 1.7 christos }
13692 1.7 christos if (!tf->valid)
13693 1.7 christos {
13694 1.7 christos info->callbacks->einfo
13695 1.7 christos (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13696 1.7 christos return FALSE;
13697 1.7 christos }
13698 1.1 skrll }
13699 1.1 skrll flaginfo->flags_initialized = TRUE;
13700 1.4 christos flaginfo->only_with_flags |= with_hex;
13701 1.1 skrll flaginfo->not_with_flags |= without_hex;
13702 1.1 skrll }
13703 1.1 skrll
13704 1.1 skrll if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13705 1.1 skrll return FALSE;
13706 1.1 skrll
13707 1.1 skrll if ((flaginfo->not_with_flags & sh_flags) != 0)
13708 1.1 skrll return FALSE;
13709 1.4 christos
13710 1.4 christos return TRUE;
13711 1.4 christos }
13712 1.1 skrll
13713 1.1 skrll struct alloc_got_off_arg {
13714 1.1 skrll bfd_vma gotoff;
13715 1.1 skrll struct bfd_link_info *info;
13716 1.4 christos };
13717 1.1 skrll
13718 1.1 skrll /* We need a special top-level link routine to convert got reference counts
13719 1.1 skrll to real got offsets. */
13720 1.1 skrll
13721 1.1 skrll static bfd_boolean
13722 1.1 skrll elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13723 1.1 skrll {
13724 1.1 skrll struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13725 1.1 skrll bfd *obfd = gofarg->info->output_bfd;
13726 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13727 1.1 skrll
13728 1.1 skrll if (h->got.refcount > 0)
13729 1.1 skrll {
13730 1.1 skrll h->got.offset = gofarg->gotoff;
13731 1.1 skrll gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13732 1.1 skrll }
13733 1.1 skrll else
13734 1.1 skrll h->got.offset = (bfd_vma) -1;
13735 1.1 skrll
13736 1.4 christos return TRUE;
13737 1.4 christos }
13738 1.1 skrll
13739 1.1 skrll /* And an accompanying bit to work out final got entry offsets once
13740 1.1 skrll we're done. Should be called from final_link. */
13741 1.1 skrll
13742 1.1 skrll bfd_boolean
13743 1.1 skrll bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13744 1.1 skrll struct bfd_link_info *info)
13745 1.1 skrll {
13746 1.1 skrll bfd *i;
13747 1.1 skrll const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13748 1.1 skrll bfd_vma gotoff;
13749 1.9 christos struct alloc_got_off_arg gofarg;
13750 1.1 skrll
13751 1.1 skrll BFD_ASSERT (abfd == info->output_bfd);
13752 1.13 christos
13753 1.1 skrll if (! is_elf_hash_table (info->hash))
13754 1.1 skrll return FALSE;
13755 1.1 skrll
13756 1.1 skrll /* The GOT offset is relative to the .got section, but the GOT header is
13757 1.1 skrll put into the .got.plt section, if the backend uses it. */
13758 1.1 skrll if (bed->want_got_plt)
13759 1.1 skrll gotoff = 0;
13760 1.1 skrll else
13761 1.1 skrll gotoff = bed->got_header_size;
13762 1.1 skrll
13763 1.1 skrll /* Do the local .got entries first. */
13764 1.1 skrll for (i = info->input_bfds; i; i = i->link.next)
13765 1.1 skrll {
13766 1.1 skrll bfd_signed_vma *local_got;
13767 1.1 skrll size_t j, locsymcount;
13768 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
13769 1.1 skrll
13770 1.1 skrll if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13771 1.1 skrll continue;
13772 1.1 skrll
13773 1.4 christos local_got = elf_local_got_refcounts (i);
13774 1.1 skrll if (!local_got)
13775 1.1 skrll continue;
13776 1.1 skrll
13777 1.1 skrll symtab_hdr = &elf_tdata (i)->symtab_hdr;
13778 1.1 skrll if (elf_bad_symtab (i))
13779 1.1 skrll locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13780 1.1 skrll else
13781 1.1 skrll locsymcount = symtab_hdr->sh_info;
13782 1.1 skrll
13783 1.4 christos for (j = 0; j < locsymcount; ++j)
13784 1.1 skrll {
13785 1.1 skrll if (local_got[j] > 0)
13786 1.1 skrll {
13787 1.1 skrll local_got[j] = gotoff;
13788 1.1 skrll gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13789 1.1 skrll }
13790 1.1 skrll else
13791 1.1 skrll local_got[j] = (bfd_vma) -1;
13792 1.1 skrll }
13793 1.1 skrll }
13794 1.1 skrll
13795 1.1 skrll /* Then the global .got entries. .plt refcounts are handled by
13796 1.1 skrll adjust_dynamic_symbol */
13797 1.1 skrll gofarg.gotoff = gotoff;
13798 1.1 skrll gofarg.info = info;
13799 1.1 skrll elf_link_hash_traverse (elf_hash_table (info),
13800 1.1 skrll elf_gc_allocate_got_offsets,
13801 1.1 skrll &gofarg);
13802 1.1 skrll return TRUE;
13803 1.1 skrll }
13804 1.1 skrll
13805 1.1 skrll /* Many folk need no more in the way of final link than this, once
13806 1.4 christos got entry reference counting is enabled. */
13807 1.1 skrll
13808 1.1 skrll bfd_boolean
13809 1.1 skrll bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13810 1.1 skrll {
13811 1.1 skrll if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13812 1.1 skrll return FALSE;
13813 1.1 skrll
13814 1.1 skrll /* Invoke the regular ELF backend linker to do all the work. */
13815 1.1 skrll return bfd_elf_final_link (abfd, info);
13816 1.1 skrll }
13817 1.1 skrll
13818 1.1 skrll bfd_boolean
13819 1.1 skrll bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13820 1.1 skrll {
13821 1.1 skrll struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13822 1.4 christos
13823 1.1 skrll if (rcookie->bad_symtab)
13824 1.1 skrll rcookie->rel = rcookie->rels;
13825 1.1 skrll
13826 1.1 skrll for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13827 1.1 skrll {
13828 1.1 skrll unsigned long r_symndx;
13829 1.1 skrll
13830 1.1 skrll if (! rcookie->bad_symtab)
13831 1.1 skrll if (rcookie->rel->r_offset > offset)
13832 1.1 skrll return FALSE;
13833 1.1 skrll if (rcookie->rel->r_offset != offset)
13834 1.1 skrll continue;
13835 1.1 skrll
13836 1.1 skrll r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13837 1.1 skrll if (r_symndx == STN_UNDEF)
13838 1.9 christos return TRUE;
13839 1.9 christos
13840 1.9 christos if (r_symndx >= rcookie->locsymcount
13841 1.1 skrll || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13842 1.1 skrll {
13843 1.1 skrll struct elf_link_hash_entry *h;
13844 1.1 skrll
13845 1.1 skrll h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13846 1.1 skrll
13847 1.1 skrll while (h->root.type == bfd_link_hash_indirect
13848 1.1 skrll || h->root.type == bfd_link_hash_warning)
13849 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
13850 1.1 skrll
13851 1.1 skrll if ((h->root.type == bfd_link_hash_defined
13852 1.1 skrll || h->root.type == bfd_link_hash_defweak)
13853 1.1 skrll && (h->root.u.def.section->owner != rcookie->abfd
13854 1.9 christos || h->root.u.def.section->kept_section != NULL
13855 1.9 christos || discarded_section (h->root.u.def.section)))
13856 1.9 christos return TRUE;
13857 1.1 skrll }
13858 1.1 skrll else
13859 1.1 skrll {
13860 1.1 skrll /* It's not a relocation against a global symbol,
13861 1.1 skrll but it could be a relocation against a local
13862 1.1 skrll symbol for a discarded section. */
13863 1.1 skrll asection *isec;
13864 1.1 skrll Elf_Internal_Sym *isym;
13865 1.9 christos
13866 1.9 christos /* Need to: get the symbol; get the section. */
13867 1.9 christos isym = &rcookie->locsyms[r_symndx];
13868 1.1 skrll isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13869 1.9 christos if (isec != NULL
13870 1.1 skrll && (isec->kept_section != NULL
13871 1.1 skrll || discarded_section (isec)))
13872 1.1 skrll return TRUE;
13873 1.9 christos }
13874 1.1 skrll return FALSE;
13875 1.9 christos }
13876 1.1 skrll return FALSE;
13877 1.1 skrll }
13878 1.1 skrll
13879 1.9 christos /* Discard unneeded references to discarded sections.
13880 1.1 skrll Returns -1 on error, 1 if any section's size was changed, 0 if
13881 1.9 christos nothing changed. This function assumes that the relocations are in
13882 1.9 christos sorted order, which is true for all known assemblers. */
13883 1.1 skrll
13884 1.9 christos int
13885 1.1 skrll bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13886 1.9 christos {
13887 1.9 christos struct elf_reloc_cookie cookie;
13888 1.9 christos asection *o;
13889 1.9 christos bfd *abfd;
13890 1.9 christos int changed = 0;
13891 1.9 christos
13892 1.1 skrll if (info->traditional_format
13893 1.9 christos || !is_elf_hash_table (info->hash))
13894 1.9 christos return 0;
13895 1.9 christos
13896 1.1 skrll o = bfd_get_section_by_name (output_bfd, ".stab");
13897 1.9 christos if (o != NULL)
13898 1.9 christos {
13899 1.1 skrll asection *i;
13900 1.9 christos
13901 1.9 christos for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13902 1.1 skrll {
13903 1.1 skrll if (i->size == 0
13904 1.9 christos || i->reloc_count == 0
13905 1.9 christos || i->sec_info_type != SEC_INFO_TYPE_STABS)
13906 1.9 christos continue;
13907 1.1 skrll
13908 1.9 christos abfd = i->owner;
13909 1.1 skrll if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13910 1.9 christos continue;
13911 1.9 christos
13912 1.9 christos if (!init_reloc_cookie_for_section (&cookie, info, i))
13913 1.9 christos return -1;
13914 1.9 christos
13915 1.9 christos if (_bfd_discard_section_stabs (abfd, i,
13916 1.13.12.2 pgoyette elf_section_data (i)->sec_info,
13917 1.13.12.2 pgoyette bfd_elf_reloc_symbol_deleted_p,
13918 1.9 christos &cookie))
13919 1.9 christos changed = 1;
13920 1.1 skrll
13921 1.9 christos fini_reloc_cookie_for_section (&cookie, i);
13922 1.9 christos }
13923 1.9 christos }
13924 1.9 christos
13925 1.9 christos o = NULL;
13926 1.9 christos if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13927 1.9 christos o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13928 1.9 christos if (o != NULL)
13929 1.9 christos {
13930 1.9 christos asection *i;
13931 1.9 christos int eh_changed = 0;
13932 1.9 christos unsigned int eh_alignment;
13933 1.1 skrll
13934 1.1 skrll for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13935 1.13.12.2 pgoyette {
13936 1.13.12.2 pgoyette if (i->size == 0)
13937 1.13.12.2 pgoyette continue;
13938 1.13.12.2 pgoyette
13939 1.13.12.2 pgoyette abfd = i->owner;
13940 1.9 christos if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13941 1.9 christos continue;
13942 1.1 skrll
13943 1.13.12.2 pgoyette if (!init_reloc_cookie_for_section (&cookie, info, i))
13944 1.13.12.2 pgoyette return -1;
13945 1.13.12.2 pgoyette
13946 1.13.12.2 pgoyette _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13947 1.13.12.2 pgoyette if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13948 1.13.12.2 pgoyette bfd_elf_reloc_symbol_deleted_p,
13949 1.13.12.2 pgoyette &cookie))
13950 1.13.12.2 pgoyette {
13951 1.13.12.2 pgoyette eh_changed = 1;
13952 1.13.12.2 pgoyette if (i->size != i->rawsize)
13953 1.13.12.2 pgoyette changed = 1;
13954 1.13.12.2 pgoyette }
13955 1.13.12.2 pgoyette
13956 1.13.12.2 pgoyette fini_reloc_cookie_for_section (&cookie, i);
13957 1.13.12.2 pgoyette }
13958 1.13.12.2 pgoyette
13959 1.13.12.2 pgoyette eh_alignment = 1 << o->alignment_power;
13960 1.13.12.2 pgoyette /* Skip over zero terminator, and prevent empty sections from
13961 1.13.12.2 pgoyette adding alignment padding at the end. */
13962 1.13.12.2 pgoyette for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13963 1.13.12.2 pgoyette if (i->size == 0)
13964 1.13.12.2 pgoyette i->flags |= SEC_EXCLUDE;
13965 1.13.12.2 pgoyette else if (i->size > 4)
13966 1.13.12.2 pgoyette break;
13967 1.13.12.2 pgoyette /* The last non-empty eh_frame section doesn't need padding. */
13968 1.13.12.2 pgoyette if (i != NULL)
13969 1.13.12.2 pgoyette i = i->map_tail.s;
13970 1.13.12.2 pgoyette /* Any prior sections must pad the last FDE out to the output
13971 1.13.12.2 pgoyette section alignment. Otherwise we might have zero padding
13972 1.13.12.2 pgoyette between sections, which would be seen as a terminator. */
13973 1.13.12.2 pgoyette for (; i != NULL; i = i->map_tail.s)
13974 1.13.12.2 pgoyette if (i->size == 4)
13975 1.13.12.2 pgoyette /* All but the last zero terminator should have been removed. */
13976 1.9 christos BFD_FAIL ();
13977 1.9 christos else
13978 1.9 christos {
13979 1.9 christos bfd_size_type size
13980 1.9 christos = (i->size + eh_alignment - 1) & -eh_alignment;
13981 1.13.12.2 pgoyette if (i->size != size)
13982 1.9 christos {
13983 1.9 christos i->size = size;
13984 1.9 christos changed = 1;
13985 1.13.12.2 pgoyette eh_changed = 1;
13986 1.13.12.2 pgoyette }
13987 1.13.12.2 pgoyette }
13988 1.9 christos if (eh_changed)
13989 1.9 christos elf_link_hash_traverse (elf_hash_table (info),
13990 1.9 christos _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13991 1.9 christos }
13992 1.9 christos
13993 1.9 christos for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13994 1.9 christos {
13995 1.1 skrll const struct elf_backend_data *bed;
13996 1.9 christos asection *s;
13997 1.9 christos
13998 1.1 skrll if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13999 1.9 christos continue;
14000 1.9 christos s = abfd->sections;
14001 1.1 skrll if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14002 1.1 skrll continue;
14003 1.9 christos
14004 1.9 christos bed = get_elf_backend_data (abfd);
14005 1.9 christos
14006 1.9 christos if (bed->elf_backend_discard_info != NULL)
14007 1.9 christos {
14008 1.1 skrll if (!init_reloc_cookie (&cookie, info, abfd))
14009 1.9 christos return -1;
14010 1.1 skrll
14011 1.9 christos if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14012 1.1 skrll changed = 1;
14013 1.1 skrll
14014 1.7 christos fini_reloc_cookie (&cookie, abfd);
14015 1.7 christos }
14016 1.7 christos }
14017 1.1 skrll
14018 1.1 skrll if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14019 1.1 skrll _bfd_elf_end_eh_frame_parsing (info);
14020 1.7 christos
14021 1.1 skrll if (info->eh_frame_hdr_type
14022 1.1 skrll && !bfd_link_relocatable (info)
14023 1.1 skrll && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14024 1.1 skrll changed = 1;
14025 1.7 christos
14026 1.1 skrll return changed;
14027 1.1 skrll }
14028 1.1 skrll
14029 1.1 skrll bfd_boolean
14030 1.1 skrll _bfd_elf_section_already_linked (bfd *abfd,
14031 1.1 skrll asection *sec,
14032 1.7 christos struct bfd_link_info *info)
14033 1.1 skrll {
14034 1.1 skrll flagword flags;
14035 1.1 skrll const char *name, *key;
14036 1.1 skrll struct bfd_section_already_linked *l;
14037 1.7 christos struct bfd_section_already_linked_hash_entry *already_linked_list;
14038 1.1 skrll
14039 1.7 christos if (sec->output_section == bfd_abs_section_ptr)
14040 1.7 christos return FALSE;
14041 1.7 christos
14042 1.7 christos flags = sec->flags;
14043 1.7 christos
14044 1.7 christos /* Return if it isn't a linkonce section. A comdat group section
14045 1.1 skrll also has SEC_LINK_ONCE set. */
14046 1.7 christos if ((flags & SEC_LINK_ONCE) == 0)
14047 1.7 christos return FALSE;
14048 1.7 christos
14049 1.7 christos /* Don't put group member sections on our list of already linked
14050 1.7 christos sections. They are handled as a group via their group section. */
14051 1.7 christos if (elf_sec_group (sec) != NULL)
14052 1.7 christos return FALSE;
14053 1.7 christos
14054 1.7 christos /* For a SHT_GROUP section, use the group signature as the key. */
14055 1.7 christos name = sec->name;
14056 1.7 christos if ((flags & SEC_GROUP) != 0
14057 1.1 skrll && elf_next_in_group (sec) != NULL
14058 1.7 christos && elf_group_name (elf_next_in_group (sec)) != NULL)
14059 1.1 skrll key = elf_group_name (elf_next_in_group (sec));
14060 1.1 skrll else
14061 1.1 skrll {
14062 1.1 skrll /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14063 1.7 christos if (CONST_STRNEQ (name, ".gnu.linkonce.")
14064 1.7 christos && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14065 1.7 christos key++;
14066 1.7 christos else
14067 1.7 christos /* Must be a user linkonce section that doesn't follow gcc's
14068 1.7 christos naming convention. In this case we won't be matching
14069 1.7 christos single member groups. */
14070 1.7 christos key = name;
14071 1.7 christos }
14072 1.1 skrll
14073 1.1 skrll already_linked_list = bfd_section_already_linked_table_lookup (key);
14074 1.1 skrll
14075 1.7 christos for (l = already_linked_list->entry; l != NULL; l = l->next)
14076 1.7 christos {
14077 1.1 skrll /* We may have 2 different types of sections on the list: group
14078 1.1 skrll sections with a signature of <key> (<key> is some string),
14079 1.1 skrll and linkonce sections named .gnu.linkonce.<type>.<key>.
14080 1.1 skrll Match like sections. LTO plugin sections are an exception.
14081 1.1 skrll They are always named .gnu.linkonce.t.<key> and match either
14082 1.1 skrll type of section. */
14083 1.1 skrll if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14084 1.1 skrll && ((flags & SEC_GROUP) != 0
14085 1.1 skrll || strcmp (name, l->sec->name) == 0))
14086 1.1 skrll || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14087 1.1 skrll {
14088 1.1 skrll /* The section has already been linked. See if we should
14089 1.1 skrll issue a warning. */
14090 1.1 skrll if (!_bfd_handle_already_linked (sec, l, info))
14091 1.1 skrll return FALSE;
14092 1.1 skrll
14093 1.1 skrll if (flags & SEC_GROUP)
14094 1.1 skrll {
14095 1.7 christos asection *first = elf_next_in_group (sec);
14096 1.1 skrll asection *s = first;
14097 1.1 skrll
14098 1.1 skrll while (s != NULL)
14099 1.1 skrll {
14100 1.1 skrll s->output_section = bfd_abs_section_ptr;
14101 1.1 skrll /* Record which group discards it. */
14102 1.1 skrll s->kept_section = l->sec;
14103 1.1 skrll s = elf_next_in_group (s);
14104 1.1 skrll /* These lists are circular. */
14105 1.1 skrll if (s == first)
14106 1.1 skrll break;
14107 1.1 skrll }
14108 1.1 skrll }
14109 1.1 skrll
14110 1.1 skrll return TRUE;
14111 1.1 skrll }
14112 1.1 skrll }
14113 1.1 skrll
14114 1.1 skrll /* A single member comdat group section may be discarded by a
14115 1.1 skrll linkonce section and vice versa. */
14116 1.1 skrll if ((flags & SEC_GROUP) != 0)
14117 1.1 skrll {
14118 1.1 skrll asection *first = elf_next_in_group (sec);
14119 1.1 skrll
14120 1.1 skrll if (first != NULL && elf_next_in_group (first) == first)
14121 1.1 skrll /* Check this single member group against linkonce sections. */
14122 1.1 skrll for (l = already_linked_list->entry; l != NULL; l = l->next)
14123 1.1 skrll if ((l->sec->flags & SEC_GROUP) == 0
14124 1.1 skrll && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14125 1.1 skrll {
14126 1.1 skrll first->output_section = bfd_abs_section_ptr;
14127 1.1 skrll first->kept_section = l->sec;
14128 1.1 skrll sec->output_section = bfd_abs_section_ptr;
14129 1.1 skrll break;
14130 1.1 skrll }
14131 1.1 skrll }
14132 1.1 skrll else
14133 1.1 skrll /* Check this linkonce section against single member groups. */
14134 1.3 skrll for (l = already_linked_list->entry; l != NULL; l = l->next)
14135 1.3 skrll if (l->sec->flags & SEC_GROUP)
14136 1.3 skrll {
14137 1.3 skrll asection *first = elf_next_in_group (l->sec);
14138 1.3 skrll
14139 1.3 skrll if (first != NULL
14140 1.3 skrll && elf_next_in_group (first) == first
14141 1.3 skrll && bfd_elf_match_symbols_in_sections (first, sec, info))
14142 1.3 skrll {
14143 1.3 skrll sec->output_section = bfd_abs_section_ptr;
14144 1.3 skrll sec->kept_section = first;
14145 1.3 skrll break;
14146 1.3 skrll }
14147 1.3 skrll }
14148 1.3 skrll
14149 1.3 skrll /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14150 1.3 skrll referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14151 1.3 skrll specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14152 1.3 skrll prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14153 1.3 skrll matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14154 1.3 skrll but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14155 1.3 skrll `.gnu.linkonce.t.F' section from a different bfd not requiring any
14156 1.1 skrll `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14157 1.7 christos The reverse order cannot happen as there is never a bfd with only the
14158 1.4 christos `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14159 1.7 christos matter as here were are looking only for cross-bfd sections. */
14160 1.1 skrll
14161 1.1 skrll if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14162 1.1 skrll for (l = already_linked_list->entry; l != NULL; l = l->next)
14163 1.1 skrll if ((l->sec->flags & SEC_GROUP) == 0
14164 1.1 skrll && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14165 1.1 skrll {
14166 1.1 skrll if (abfd != l->sec->owner)
14167 1.1 skrll sec->output_section = bfd_abs_section_ptr;
14168 1.1 skrll break;
14169 1.1 skrll }
14170 1.1 skrll
14171 1.1 skrll /* This is the first section with this name. Record it. */
14172 1.1 skrll if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14173 1.1 skrll info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14174 1.1 skrll return sec->output_section == bfd_abs_section_ptr;
14175 1.1 skrll }
14176 1.1 skrll
14177 1.1 skrll bfd_boolean
14178 1.1 skrll _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14179 1.4 christos {
14180 1.4 christos return sym->st_shndx == SHN_COMMON;
14181 1.4 christos }
14182 1.4 christos
14183 1.4 christos unsigned int
14184 1.4 christos _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14185 1.4 christos {
14186 1.4 christos return SHN_COMMON;
14187 1.4 christos }
14188 1.4 christos
14189 1.4 christos asection *
14190 1.4 christos _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14191 1.4 christos {
14192 1.4 christos return bfd_com_section_ptr;
14193 1.4 christos }
14194 1.4 christos
14195 1.4 christos bfd_vma
14196 1.4 christos _bfd_elf_default_got_elt_size (bfd *abfd,
14197 1.4 christos struct bfd_link_info *info ATTRIBUTE_UNUSED,
14198 1.4 christos struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14199 1.4 christos bfd *ibfd ATTRIBUTE_UNUSED,
14200 1.7 christos unsigned long symndx ATTRIBUTE_UNUSED)
14201 1.7 christos {
14202 1.7 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14203 1.4 christos return bed->s->arch_size / 8;
14204 1.7 christos }
14205 1.4 christos
14206 1.4 christos /* Routines to support the creation of dynamic relocs. */
14207 1.7 christos
14208 1.9 christos /* Returns the name of the dynamic reloc section associated with SEC. */
14209 1.4 christos
14210 1.4 christos static const char *
14211 1.4 christos get_dynamic_reloc_section_name (bfd * abfd,
14212 1.4 christos asection * sec,
14213 1.4 christos bfd_boolean is_rela)
14214 1.4 christos {
14215 1.4 christos char *name;
14216 1.4 christos const char *old_name = bfd_get_section_name (NULL, sec);
14217 1.4 christos const char *prefix = is_rela ? ".rela" : ".rel";
14218 1.4 christos
14219 1.4 christos if (old_name == NULL)
14220 1.4 christos return NULL;
14221 1.4 christos
14222 1.4 christos name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14223 1.4 christos sprintf (name, "%s%s", prefix, old_name);
14224 1.4 christos
14225 1.4 christos return name;
14226 1.4 christos }
14227 1.4 christos
14228 1.4 christos /* Returns the dynamic reloc section associated with SEC.
14229 1.4 christos If necessary compute the name of the dynamic reloc section based
14230 1.4 christos on SEC's name (looked up in ABFD's string table) and the setting
14231 1.7 christos of IS_RELA. */
14232 1.4 christos
14233 1.4 christos asection *
14234 1.4 christos _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14235 1.4 christos asection * sec,
14236 1.4 christos bfd_boolean is_rela)
14237 1.4 christos {
14238 1.4 christos asection * reloc_sec = elf_section_data (sec)->sreloc;
14239 1.4 christos
14240 1.4 christos if (reloc_sec == NULL)
14241 1.4 christos {
14242 1.4 christos const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14243 1.4 christos
14244 1.4 christos if (name != NULL)
14245 1.4 christos {
14246 1.4 christos reloc_sec = bfd_get_linker_section (abfd, name);
14247 1.4 christos
14248 1.4 christos if (reloc_sec != NULL)
14249 1.4 christos elf_section_data (sec)->sreloc = reloc_sec;
14250 1.4 christos }
14251 1.4 christos }
14252 1.9 christos
14253 1.9 christos return reloc_sec;
14254 1.9 christos }
14255 1.9 christos
14256 1.9 christos /* Returns the dynamic reloc section associated with SEC. If the
14257 1.4 christos section does not exist it is created and attached to the DYNOBJ
14258 1.4 christos bfd and stored in the SRELOC field of SEC's elf_section_data
14259 1.4 christos structure.
14260 1.4 christos
14261 1.4 christos ALIGNMENT is the alignment for the newly created section and
14262 1.4 christos IS_RELA defines whether the name should be .rela.<SEC's name>
14263 1.4 christos or .rel.<SEC's name>. The section name is looked up in the
14264 1.4 christos string table associated with ABFD. */
14265 1.4 christos
14266 1.4 christos asection *
14267 1.7 christos _bfd_elf_make_dynamic_reloc_section (asection *sec,
14268 1.4 christos bfd *dynobj,
14269 1.4 christos unsigned int alignment,
14270 1.4 christos bfd *abfd,
14271 1.7 christos bfd_boolean is_rela)
14272 1.7 christos {
14273 1.4 christos asection * reloc_sec = elf_section_data (sec)->sreloc;
14274 1.4 christos
14275 1.4 christos if (reloc_sec == NULL)
14276 1.7 christos {
14277 1.4 christos const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14278 1.4 christos
14279 1.9 christos if (name == NULL)
14280 1.9 christos return NULL;
14281 1.9 christos
14282 1.9 christos reloc_sec = bfd_get_linker_section (dynobj, name);
14283 1.9 christos
14284 1.4 christos if (reloc_sec == NULL)
14285 1.4 christos {
14286 1.4 christos flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14287 1.4 christos | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14288 1.4 christos if ((sec->flags & SEC_ALLOC) != 0)
14289 1.4 christos flags |= SEC_ALLOC | SEC_LOAD;
14290 1.4 christos
14291 1.4 christos reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14292 1.4 christos if (reloc_sec != NULL)
14293 1.4 christos {
14294 1.4 christos /* _bfd_elf_get_sec_type_attr chooses a section type by
14295 1.9 christos name. Override as it may be wrong, eg. for a user
14296 1.9 christos section named "auto" we'll get ".relauto" which is
14297 1.9 christos seen to be a .rela section. */
14298 1.9 christos elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14299 1.4 christos if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14300 1.9 christos reloc_sec = NULL;
14301 1.9 christos }
14302 1.9 christos }
14303 1.9 christos
14304 1.9 christos elf_section_data (sec)->sreloc = reloc_sec;
14305 1.9 christos }
14306 1.9 christos
14307 1.4 christos return reloc_sec;
14308 1.4 christos }
14309 1.7 christos
14310 1.9 christos /* Copy the ELF symbol type and other attributes for a linker script
14311 1.9 christos assignment from HSRC to HDEST. Generally this should be treated as
14312 1.9 christos if we found a strong non-dynamic definition for HDEST (except that
14313 1.7 christos ld ignores multiple definition errors). */
14314 1.7 christos void
14315 1.7 christos _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14316 1.7 christos struct bfd_link_hash_entry *hdest,
14317 1.7 christos struct bfd_link_hash_entry *hsrc)
14318 1.7 christos {
14319 1.7 christos struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14320 1.7 christos struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14321 1.7 christos Elf_Internal_Sym isym;
14322 1.7 christos
14323 1.7 christos ehdest->type = ehsrc->type;
14324 1.7 christos ehdest->target_internal = ehsrc->target_internal;
14325 1.7 christos
14326 1.7 christos isym.st_other = ehsrc->other;
14327 1.7 christos elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14328 1.7 christos }
14329 1.7 christos
14330 1.7 christos /* Append a RELA relocation REL to section S in BFD. */
14331 1.7 christos
14332 1.7 christos void
14333 1.7 christos elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14334 1.7 christos {
14335 1.4 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14336 1.13.12.2 pgoyette bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14337 1.13.12.2 pgoyette BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14338 1.13.12.2 pgoyette bed->s->swap_reloca_out (abfd, rel, loc);
14339 1.13.12.2 pgoyette }
14340 1.13.12.2 pgoyette
14341 1.13.12.2 pgoyette /* Append a REL relocation REL to section S in BFD. */
14342 1.13.12.2 pgoyette
14343 1.13.12.2 pgoyette void
14344 1.13.12.2 pgoyette elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14345 1.13.12.2 pgoyette {
14346 1.13.12.2 pgoyette const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14347 1.13.12.2 pgoyette bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14348 1.13.12.2 pgoyette BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14349 1.13.12.2 pgoyette bed->s->swap_reloc_out (abfd, rel, loc);
14350 1.13.12.2 pgoyette }
14351 1.13.12.2 pgoyette
14352 1.13.12.2 pgoyette /* Define __start, __stop, .startof. or .sizeof. symbol. */
14353 1.13.12.2 pgoyette
14354 1.13.12.2 pgoyette struct bfd_link_hash_entry *
14355 1.13.12.2 pgoyette bfd_elf_define_start_stop (struct bfd_link_info *info,
14356 1.13.12.2 pgoyette const char *symbol, asection *sec)
14357 1.13.12.2 pgoyette {
14358 1.13.12.2 pgoyette struct elf_link_hash_entry *h;
14359 1.13.12.2 pgoyette
14360 1.13.12.2 pgoyette h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14361 1.13.12.2 pgoyette FALSE, FALSE, TRUE);
14362 1.13.12.2 pgoyette if (h != NULL
14363 1.13.12.2 pgoyette && (h->root.type == bfd_link_hash_undefined
14364 1.13.12.2 pgoyette || h->root.type == bfd_link_hash_undefweak
14365 1.13.12.2 pgoyette || (h->ref_regular && !h->def_regular)))
14366 1.13.12.2 pgoyette {
14367 1.13.12.2 pgoyette h->root.type = bfd_link_hash_defined;
14368 1.13.12.2 pgoyette h->root.u.def.section = sec;
14369 1.13.12.2 pgoyette h->root.u.def.value = 0;
14370 1.13.12.2 pgoyette h->def_regular = 1;
14371 1.13.12.2 pgoyette h->def_dynamic = 0;
14372 h->start_stop = 1;
14373 h->u2.start_stop_section = sec;
14374 if (symbol[0] == '.')
14375 {
14376 /* .startof. and .sizeof. symbols are local. */
14377 const struct elf_backend_data *bed;
14378 bed = get_elf_backend_data (info->output_bfd);
14379 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14380 }
14381 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14382 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14383 return &h->root;
14384 }
14385 return NULL;
14386 }
14387