elf.c revision 1.18 1 1.1 christos /* ELF executable support for BFD.
2 1.1 christos
3 1.17 christos Copyright (C) 1993-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of BFD, the Binary File Descriptor library.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software
19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 1.1 christos MA 02110-1301, USA. */
21 1.1 christos
22 1.1 christos
23 1.1 christos /*
24 1.1 christos SECTION
25 1.1 christos ELF backends
26 1.1 christos
27 1.1 christos BFD support for ELF formats is being worked on.
28 1.1 christos Currently, the best supported back ends are for sparc and i386
29 1.1 christos (running svr4 or Solaris 2).
30 1.1 christos
31 1.1 christos Documentation of the internals of the support code still needs
32 1.1 christos to be written. The code is changing quickly enough that we
33 1.1 christos haven't bothered yet. */
34 1.1 christos
35 1.1 christos /* For sparc64-cross-sparc32. */
36 1.1 christos #define _SYSCALL32
37 1.1 christos #include "sysdep.h"
38 1.11 christos #include <limits.h>
39 1.1 christos #include "bfd.h"
40 1.1 christos #include "bfdlink.h"
41 1.1 christos #include "libbfd.h"
42 1.1 christos #define ARCH_SIZE 0
43 1.1 christos #include "elf-bfd.h"
44 1.1 christos #include "libiberty.h"
45 1.1 christos #include "safe-ctype.h"
46 1.8 christos #include "elf-linux-core.h"
47 1.1 christos
48 1.1 christos #ifdef CORE_HEADER
49 1.1 christos #include CORE_HEADER
50 1.1 christos #endif
51 1.1 christos
52 1.1 christos static int elf_sort_sections (const void *, const void *);
53 1.14 christos static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 1.14 christos static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 1.14 christos struct bfd_link_info *);
56 1.14 christos static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 1.14 christos file_ptr offset, size_t align);
58 1.1 christos
59 1.1 christos /* Swap version information in and out. The version information is
60 1.1 christos currently size independent. If that ever changes, this code will
61 1.1 christos need to move into elfcode.h. */
62 1.1 christos
63 1.1 christos /* Swap in a Verdef structure. */
64 1.1 christos
65 1.1 christos void
66 1.1 christos _bfd_elf_swap_verdef_in (bfd *abfd,
67 1.1 christos const Elf_External_Verdef *src,
68 1.1 christos Elf_Internal_Verdef *dst)
69 1.1 christos {
70 1.1 christos dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 1.1 christos dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 1.1 christos dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 1.1 christos dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 1.1 christos dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 1.1 christos dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 1.1 christos dst->vd_next = H_GET_32 (abfd, src->vd_next);
77 1.1 christos }
78 1.1 christos
79 1.1 christos /* Swap out a Verdef structure. */
80 1.1 christos
81 1.1 christos void
82 1.1 christos _bfd_elf_swap_verdef_out (bfd *abfd,
83 1.1 christos const Elf_Internal_Verdef *src,
84 1.1 christos Elf_External_Verdef *dst)
85 1.1 christos {
86 1.1 christos H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 1.1 christos H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 1.1 christos H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 1.1 christos H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 1.1 christos H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 1.1 christos H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 1.1 christos H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93 1.1 christos }
94 1.1 christos
95 1.1 christos /* Swap in a Verdaux structure. */
96 1.1 christos
97 1.1 christos void
98 1.1 christos _bfd_elf_swap_verdaux_in (bfd *abfd,
99 1.1 christos const Elf_External_Verdaux *src,
100 1.1 christos Elf_Internal_Verdaux *dst)
101 1.1 christos {
102 1.1 christos dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 1.1 christos dst->vda_next = H_GET_32 (abfd, src->vda_next);
104 1.1 christos }
105 1.1 christos
106 1.1 christos /* Swap out a Verdaux structure. */
107 1.1 christos
108 1.1 christos void
109 1.1 christos _bfd_elf_swap_verdaux_out (bfd *abfd,
110 1.1 christos const Elf_Internal_Verdaux *src,
111 1.1 christos Elf_External_Verdaux *dst)
112 1.1 christos {
113 1.1 christos H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 1.1 christos H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115 1.1 christos }
116 1.1 christos
117 1.1 christos /* Swap in a Verneed structure. */
118 1.1 christos
119 1.1 christos void
120 1.1 christos _bfd_elf_swap_verneed_in (bfd *abfd,
121 1.1 christos const Elf_External_Verneed *src,
122 1.1 christos Elf_Internal_Verneed *dst)
123 1.1 christos {
124 1.1 christos dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 1.1 christos dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 1.1 christos dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 1.1 christos dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 1.1 christos dst->vn_next = H_GET_32 (abfd, src->vn_next);
129 1.1 christos }
130 1.1 christos
131 1.1 christos /* Swap out a Verneed structure. */
132 1.1 christos
133 1.1 christos void
134 1.1 christos _bfd_elf_swap_verneed_out (bfd *abfd,
135 1.1 christos const Elf_Internal_Verneed *src,
136 1.1 christos Elf_External_Verneed *dst)
137 1.1 christos {
138 1.1 christos H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 1.1 christos H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 1.1 christos H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 1.1 christos H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 1.1 christos H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143 1.1 christos }
144 1.1 christos
145 1.1 christos /* Swap in a Vernaux structure. */
146 1.1 christos
147 1.1 christos void
148 1.1 christos _bfd_elf_swap_vernaux_in (bfd *abfd,
149 1.1 christos const Elf_External_Vernaux *src,
150 1.1 christos Elf_Internal_Vernaux *dst)
151 1.1 christos {
152 1.1 christos dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 1.1 christos dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 1.1 christos dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 1.1 christos dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 1.1 christos dst->vna_next = H_GET_32 (abfd, src->vna_next);
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Swap out a Vernaux structure. */
160 1.1 christos
161 1.1 christos void
162 1.1 christos _bfd_elf_swap_vernaux_out (bfd *abfd,
163 1.1 christos const Elf_Internal_Vernaux *src,
164 1.1 christos Elf_External_Vernaux *dst)
165 1.1 christos {
166 1.1 christos H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 1.1 christos H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 1.1 christos H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 1.1 christos H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 1.1 christos H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171 1.1 christos }
172 1.1 christos
173 1.1 christos /* Swap in a Versym structure. */
174 1.1 christos
175 1.1 christos void
176 1.1 christos _bfd_elf_swap_versym_in (bfd *abfd,
177 1.1 christos const Elf_External_Versym *src,
178 1.1 christos Elf_Internal_Versym *dst)
179 1.1 christos {
180 1.1 christos dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181 1.1 christos }
182 1.1 christos
183 1.1 christos /* Swap out a Versym structure. */
184 1.1 christos
185 1.1 christos void
186 1.1 christos _bfd_elf_swap_versym_out (bfd *abfd,
187 1.1 christos const Elf_Internal_Versym *src,
188 1.1 christos Elf_External_Versym *dst)
189 1.1 christos {
190 1.1 christos H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191 1.1 christos }
192 1.1 christos
193 1.1 christos /* Standard ELF hash function. Do not change this function; you will
194 1.1 christos cause invalid hash tables to be generated. */
195 1.1 christos
196 1.1 christos unsigned long
197 1.1 christos bfd_elf_hash (const char *namearg)
198 1.1 christos {
199 1.17 christos uint32_t h = 0;
200 1.1 christos
201 1.17 christos for (const unsigned char *name = (const unsigned char *) namearg;
202 1.17 christos *name; name++)
203 1.1 christos {
204 1.17 christos h = (h << 4) + *name;
205 1.17 christos h ^= (h >> 24) & 0xf0;
206 1.1 christos }
207 1.17 christos return h & 0x0fffffff;
208 1.1 christos }
209 1.1 christos
210 1.1 christos /* DT_GNU_HASH hash function. Do not change this function; you will
211 1.1 christos cause invalid hash tables to be generated. */
212 1.1 christos
213 1.1 christos unsigned long
214 1.1 christos bfd_elf_gnu_hash (const char *namearg)
215 1.1 christos {
216 1.17 christos uint32_t h = 5381;
217 1.17 christos
218 1.17 christos for (const unsigned char *name = (const unsigned char *) namearg;
219 1.17 christos *name; name++)
220 1.17 christos h = (h << 5) + h + *name;
221 1.17 christos return h;
222 1.1 christos }
223 1.1 christos
224 1.1 christos /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 1.18 christos the object_id field of an elf_obj_tdata field set. */
226 1.14 christos bool
227 1.1 christos bfd_elf_allocate_object (bfd *abfd,
228 1.18 christos size_t object_size)
229 1.1 christos {
230 1.1 christos BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
231 1.1 christos abfd->tdata.any = bfd_zalloc (abfd, object_size);
232 1.1 christos if (abfd->tdata.any == NULL)
233 1.14 christos return false;
234 1.1 christos
235 1.18 christos elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
236 1.3 christos if (abfd->direction != read_direction)
237 1.3 christos {
238 1.3 christos struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
239 1.3 christos if (o == NULL)
240 1.14 christos return false;
241 1.3 christos elf_tdata (abfd)->o = o;
242 1.3 christos elf_program_header_size (abfd) = (bfd_size_type) -1;
243 1.3 christos }
244 1.14 christos return true;
245 1.1 christos }
246 1.1 christos
247 1.1 christos
248 1.14 christos bool
249 1.1 christos bfd_elf_make_object (bfd *abfd)
250 1.1 christos {
251 1.18 christos return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
252 1.1 christos }
253 1.1 christos
254 1.14 christos bool
255 1.1 christos bfd_elf_mkcorefile (bfd *abfd)
256 1.1 christos {
257 1.1 christos /* I think this can be done just like an object file. */
258 1.3 christos if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
259 1.14 christos return false;
260 1.3 christos elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
261 1.3 christos return elf_tdata (abfd)->core != NULL;
262 1.1 christos }
263 1.1 christos
264 1.12 christos char *
265 1.1 christos bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
266 1.1 christos {
267 1.1 christos Elf_Internal_Shdr **i_shdrp;
268 1.1 christos bfd_byte *shstrtab = NULL;
269 1.1 christos file_ptr offset;
270 1.1 christos bfd_size_type shstrtabsize;
271 1.1 christos
272 1.1 christos i_shdrp = elf_elfsections (abfd);
273 1.1 christos if (i_shdrp == 0
274 1.1 christos || shindex >= elf_numsections (abfd)
275 1.1 christos || i_shdrp[shindex] == 0)
276 1.1 christos return NULL;
277 1.1 christos
278 1.1 christos shstrtab = i_shdrp[shindex]->contents;
279 1.1 christos if (shstrtab == NULL)
280 1.1 christos {
281 1.1 christos /* No cached one, attempt to read, and cache what we read. */
282 1.1 christos offset = i_shdrp[shindex]->sh_offset;
283 1.1 christos shstrtabsize = i_shdrp[shindex]->sh_size;
284 1.1 christos
285 1.18 christos if (shstrtabsize == 0
286 1.5 christos || bfd_seek (abfd, offset, SEEK_SET) != 0
287 1.18 christos || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
288 1.1 christos {
289 1.1 christos /* Once we've failed to read it, make sure we don't keep
290 1.1 christos trying. Otherwise, we'll keep allocating space for
291 1.1 christos the string table over and over. */
292 1.1 christos i_shdrp[shindex]->sh_size = 0;
293 1.1 christos }
294 1.18 christos else if (shstrtab[shstrtabsize - 1] != 0)
295 1.17 christos {
296 1.17 christos /* It is an error if a string table isn't terminated. */
297 1.17 christos _bfd_error_handler
298 1.17 christos /* xgettext:c-format */
299 1.18 christos (_("%pB: string table [%u] is corrupt"), abfd, shindex);
300 1.18 christos shstrtab[shstrtabsize - 1] = 0;
301 1.17 christos }
302 1.1 christos i_shdrp[shindex]->contents = shstrtab;
303 1.1 christos }
304 1.1 christos return (char *) shstrtab;
305 1.1 christos }
306 1.1 christos
307 1.1 christos char *
308 1.1 christos bfd_elf_string_from_elf_section (bfd *abfd,
309 1.1 christos unsigned int shindex,
310 1.1 christos unsigned int strindex)
311 1.1 christos {
312 1.1 christos Elf_Internal_Shdr *hdr;
313 1.1 christos
314 1.1 christos if (strindex == 0)
315 1.1 christos return "";
316 1.1 christos
317 1.1 christos if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 1.1 christos return NULL;
319 1.1 christos
320 1.1 christos hdr = elf_elfsections (abfd)[shindex];
321 1.1 christos
322 1.5 christos if (hdr->contents == NULL)
323 1.5 christos {
324 1.5 christos if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325 1.5 christos {
326 1.5 christos /* PR 17512: file: f057ec89. */
327 1.9 christos /* xgettext:c-format */
328 1.11 christos _bfd_error_handler (_("%pB: attempt to load strings from"
329 1.9 christos " a non-string section (number %d)"),
330 1.5 christos abfd, shindex);
331 1.5 christos return NULL;
332 1.5 christos }
333 1.6 christos
334 1.5 christos if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335 1.5 christos return NULL;
336 1.5 christos }
337 1.12 christos else
338 1.12 christos {
339 1.12 christos /* PR 24273: The string section's contents may have already
340 1.12 christos been loaded elsewhere, eg because a corrupt file has the
341 1.12 christos string section index in the ELF header pointing at a group
342 1.12 christos section. So be paranoid, and test that the last byte of
343 1.12 christos the section is zero. */
344 1.12 christos if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345 1.12 christos return NULL;
346 1.12 christos }
347 1.1 christos
348 1.1 christos if (strindex >= hdr->sh_size)
349 1.1 christos {
350 1.1 christos unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351 1.9 christos _bfd_error_handler
352 1.9 christos /* xgettext:c-format */
353 1.11 christos (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354 1.11 christos abfd, strindex, (uint64_t) hdr->sh_size,
355 1.1 christos (shindex == shstrndx && strindex == hdr->sh_name
356 1.1 christos ? ".shstrtab"
357 1.1 christos : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358 1.1 christos return NULL;
359 1.1 christos }
360 1.1 christos
361 1.1 christos return ((char *) hdr->contents) + strindex;
362 1.1 christos }
363 1.1 christos
364 1.1 christos /* Read and convert symbols to internal format.
365 1.1 christos SYMCOUNT specifies the number of symbols to read, starting from
366 1.1 christos symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 1.1 christos are non-NULL, they are used to store the internal symbols, external
368 1.1 christos symbols, and symbol section index extensions, respectively.
369 1.1 christos Returns a pointer to the internal symbol buffer (malloced if necessary)
370 1.1 christos or NULL if there were no symbols or some kind of problem. */
371 1.1 christos
372 1.1 christos Elf_Internal_Sym *
373 1.1 christos bfd_elf_get_elf_syms (bfd *ibfd,
374 1.1 christos Elf_Internal_Shdr *symtab_hdr,
375 1.1 christos size_t symcount,
376 1.1 christos size_t symoffset,
377 1.1 christos Elf_Internal_Sym *intsym_buf,
378 1.1 christos void *extsym_buf,
379 1.1 christos Elf_External_Sym_Shndx *extshndx_buf)
380 1.1 christos {
381 1.1 christos Elf_Internal_Shdr *shndx_hdr;
382 1.1 christos void *alloc_ext;
383 1.1 christos const bfd_byte *esym;
384 1.1 christos Elf_External_Sym_Shndx *alloc_extshndx;
385 1.1 christos Elf_External_Sym_Shndx *shndx;
386 1.1 christos Elf_Internal_Sym *alloc_intsym;
387 1.1 christos Elf_Internal_Sym *isym;
388 1.1 christos Elf_Internal_Sym *isymend;
389 1.1 christos const struct elf_backend_data *bed;
390 1.1 christos size_t extsym_size;
391 1.12 christos size_t amt;
392 1.1 christos file_ptr pos;
393 1.1 christos
394 1.1 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395 1.1 christos abort ();
396 1.1 christos
397 1.1 christos if (symcount == 0)
398 1.1 christos return intsym_buf;
399 1.1 christos
400 1.17 christos if (elf_use_dt_symtab_p (ibfd))
401 1.17 christos {
402 1.17 christos /* Use dynamic symbol table. */
403 1.17 christos if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
404 1.17 christos {
405 1.17 christos bfd_set_error (bfd_error_invalid_operation);
406 1.17 christos return NULL;
407 1.17 christos }
408 1.17 christos return elf_tdata (ibfd)->dt_symtab + symoffset;
409 1.17 christos }
410 1.17 christos
411 1.1 christos /* Normal syms might have section extension entries. */
412 1.1 christos shndx_hdr = NULL;
413 1.8 christos if (elf_symtab_shndx_list (ibfd) != NULL)
414 1.8 christos {
415 1.8 christos elf_section_list * entry;
416 1.8 christos Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417 1.8 christos
418 1.8 christos /* Find an index section that is linked to this symtab section. */
419 1.8 christos for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420 1.8 christos {
421 1.8 christos /* PR 20063. */
422 1.8 christos if (entry->hdr.sh_link >= elf_numsections (ibfd))
423 1.8 christos continue;
424 1.8 christos
425 1.8 christos if (sections[entry->hdr.sh_link] == symtab_hdr)
426 1.8 christos {
427 1.8 christos shndx_hdr = & entry->hdr;
428 1.8 christos break;
429 1.8 christos };
430 1.8 christos }
431 1.8 christos
432 1.8 christos if (shndx_hdr == NULL)
433 1.8 christos {
434 1.14 christos if (symtab_hdr == &elf_symtab_hdr (ibfd))
435 1.14 christos /* Not really accurate, but this was how the old code used
436 1.14 christos to work. */
437 1.14 christos shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
438 1.8 christos /* Otherwise we do nothing. The assumption is that
439 1.8 christos the index table will not be needed. */
440 1.8 christos }
441 1.8 christos }
442 1.1 christos
443 1.1 christos /* Read the symbols. */
444 1.1 christos alloc_ext = NULL;
445 1.1 christos alloc_extshndx = NULL;
446 1.1 christos alloc_intsym = NULL;
447 1.1 christos bed = get_elf_backend_data (ibfd);
448 1.1 christos extsym_size = bed->s->sizeof_sym;
449 1.12 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt))
450 1.12 christos {
451 1.12 christos bfd_set_error (bfd_error_file_too_big);
452 1.17 christos return NULL;
453 1.12 christos }
454 1.1 christos pos = symtab_hdr->sh_offset + symoffset * extsym_size;
455 1.17 christos size_t alloc_ext_size = amt;
456 1.17 christos if (bfd_seek (ibfd, pos, SEEK_SET) != 0
457 1.17 christos || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
458 1.17 christos &alloc_ext, ibfd, false))
459 1.1 christos {
460 1.1 christos intsym_buf = NULL;
461 1.17 christos goto out2;
462 1.1 christos }
463 1.1 christos
464 1.17 christos size_t alloc_extshndx_size = 0;
465 1.1 christos if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466 1.1 christos extshndx_buf = NULL;
467 1.1 christos else
468 1.1 christos {
469 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
470 1.12 christos {
471 1.12 christos bfd_set_error (bfd_error_file_too_big);
472 1.12 christos intsym_buf = NULL;
473 1.17 christos goto out1;
474 1.12 christos }
475 1.17 christos alloc_extshndx_size = amt;
476 1.1 christos pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 1.17 christos if (bfd_seek (ibfd, pos, SEEK_SET) != 0
478 1.17 christos || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
479 1.17 christos &alloc_extshndx_size,
480 1.17 christos (void **) &alloc_extshndx,
481 1.17 christos ibfd, false))
482 1.1 christos {
483 1.1 christos intsym_buf = NULL;
484 1.17 christos goto out1;
485 1.1 christos }
486 1.1 christos }
487 1.1 christos
488 1.1 christos if (intsym_buf == NULL)
489 1.1 christos {
490 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
491 1.12 christos {
492 1.12 christos bfd_set_error (bfd_error_file_too_big);
493 1.17 christos goto out1;
494 1.12 christos }
495 1.12 christos alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
496 1.1 christos intsym_buf = alloc_intsym;
497 1.1 christos if (intsym_buf == NULL)
498 1.17 christos goto out1;
499 1.1 christos }
500 1.1 christos
501 1.1 christos /* Convert the symbols to internal form. */
502 1.1 christos isymend = intsym_buf + symcount;
503 1.1 christos for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
504 1.11 christos shndx = extshndx_buf;
505 1.1 christos isym < isymend;
506 1.1 christos esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
507 1.1 christos if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
508 1.1 christos {
509 1.1 christos symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
510 1.9 christos /* xgettext:c-format */
511 1.11 christos _bfd_error_handler (_("%pB symbol number %lu references"
512 1.9 christos " nonexistent SHT_SYMTAB_SHNDX section"),
513 1.9 christos ibfd, (unsigned long) symoffset);
514 1.12 christos free (alloc_intsym);
515 1.1 christos intsym_buf = NULL;
516 1.17 christos goto out1;
517 1.1 christos }
518 1.1 christos
519 1.17 christos out1:
520 1.18 christos _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
521 1.17 christos out2:
522 1.18 christos _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
523 1.1 christos
524 1.1 christos return intsym_buf;
525 1.1 christos }
526 1.1 christos
527 1.1 christos /* Look up a symbol name. */
528 1.18 christos static const char *
529 1.18 christos bfd_elf_sym_name_raw (bfd *abfd,
530 1.18 christos Elf_Internal_Shdr *symtab_hdr,
531 1.18 christos Elf_Internal_Sym *isym)
532 1.1 christos {
533 1.1 christos unsigned int iname = isym->st_name;
534 1.1 christos unsigned int shindex = symtab_hdr->sh_link;
535 1.1 christos
536 1.1 christos if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
537 1.1 christos /* Check for a bogus st_shndx to avoid crashing. */
538 1.1 christos && isym->st_shndx < elf_numsections (abfd))
539 1.1 christos {
540 1.1 christos iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
541 1.1 christos shindex = elf_elfheader (abfd)->e_shstrndx;
542 1.1 christos }
543 1.1 christos
544 1.18 christos return bfd_elf_string_from_elf_section (abfd, shindex, iname);
545 1.18 christos }
546 1.18 christos
547 1.18 christos const char *
548 1.18 christos bfd_elf_sym_name (bfd *abfd,
549 1.18 christos Elf_Internal_Shdr *symtab_hdr,
550 1.18 christos Elf_Internal_Sym *isym,
551 1.18 christos asection *sym_sec)
552 1.18 christos {
553 1.18 christos const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
554 1.1 christos if (name == NULL)
555 1.18 christos name = bfd_symbol_error_name;
556 1.1 christos else if (sym_sec && *name == '\0')
557 1.12 christos name = bfd_section_name (sym_sec);
558 1.1 christos
559 1.1 christos return name;
560 1.1 christos }
561 1.1 christos
562 1.1 christos /* Return the name of the group signature symbol. Why isn't the
563 1.1 christos signature just a string? */
564 1.1 christos
565 1.1 christos static const char *
566 1.1 christos group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
567 1.1 christos {
568 1.1 christos Elf_Internal_Shdr *hdr;
569 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)];
570 1.1 christos Elf_External_Sym_Shndx eshndx;
571 1.1 christos Elf_Internal_Sym isym;
572 1.1 christos
573 1.1 christos /* First we need to ensure the symbol table is available. Make sure
574 1.1 christos that it is a symbol table section. */
575 1.1 christos if (ghdr->sh_link >= elf_numsections (abfd))
576 1.1 christos return NULL;
577 1.1 christos hdr = elf_elfsections (abfd) [ghdr->sh_link];
578 1.1 christos if (hdr->sh_type != SHT_SYMTAB
579 1.1 christos || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
580 1.1 christos return NULL;
581 1.1 christos
582 1.1 christos /* Go read the symbol. */
583 1.1 christos hdr = &elf_tdata (abfd)->symtab_hdr;
584 1.1 christos if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
585 1.1 christos &isym, esym, &eshndx) == NULL)
586 1.1 christos return NULL;
587 1.1 christos
588 1.18 christos return bfd_elf_sym_name_raw (abfd, hdr, &isym);
589 1.1 christos }
590 1.1 christos
591 1.14 christos static bool
592 1.18 christos is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
593 1.1 christos {
594 1.18 christos return (shdr->sh_size >= minsize
595 1.18 christos && shdr->sh_entsize == GRP_ENTRY_SIZE
596 1.18 christos && shdr->sh_size % GRP_ENTRY_SIZE == 0
597 1.18 christos && shdr->bfd_section != NULL);
598 1.18 christos }
599 1.1 christos
600 1.1 christos
601 1.18 christos /* Set next_in_group, sec_group list pointers, and group names. */
602 1.1 christos
603 1.18 christos static bool
604 1.18 christos process_sht_group_entries (bfd *abfd,
605 1.18 christos Elf_Internal_Shdr *ghdr, unsigned int gidx)
606 1.18 christos {
607 1.18 christos unsigned char *contents;
608 1.1 christos
609 1.18 christos /* Read the raw contents. */
610 1.18 christos if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
611 1.18 christos {
612 1.18 christos _bfd_error_handler
613 1.18 christos /* xgettext:c-format */
614 1.18 christos (_("%pB: could not read contents of group [%u]"), abfd, gidx);
615 1.18 christos return false;
616 1.18 christos }
617 1.1 christos
618 1.18 christos asection *last_elt = NULL;
619 1.18 christos const char *gname = NULL;
620 1.18 christos unsigned char *p = contents + ghdr->sh_size;
621 1.18 christos while (1)
622 1.18 christos {
623 1.18 christos unsigned int idx;
624 1.18 christos Elf_Internal_Shdr *shdr;
625 1.18 christos asection *elt;
626 1.18 christos
627 1.18 christos p -= 4;
628 1.18 christos idx = H_GET_32 (abfd, p);
629 1.18 christos if (p == contents)
630 1.18 christos {
631 1.18 christos if ((idx & GRP_COMDAT) != 0)
632 1.18 christos ghdr->bfd_section->flags
633 1.18 christos |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
634 1.18 christos break;
635 1.1 christos }
636 1.1 christos
637 1.18 christos if (idx == 0
638 1.18 christos || idx >= elf_numsections (abfd)
639 1.18 christos || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
640 1.18 christos || ((elt = shdr->bfd_section) != NULL
641 1.18 christos && elf_sec_group (elt) != NULL
642 1.18 christos && elf_sec_group (elt) != ghdr->bfd_section))
643 1.1 christos {
644 1.18 christos _bfd_error_handler
645 1.18 christos (_("%pB: invalid entry (%#x) in group [%u]"),
646 1.18 christos abfd, idx, gidx);
647 1.18 christos continue;
648 1.1 christos }
649 1.1 christos
650 1.18 christos /* PR binutils/23199: According to the ELF gABI all sections in
651 1.18 christos a group must be marked with SHF_GROUP, but some tools
652 1.18 christos generate broken objects. Fix them up here. */
653 1.18 christos shdr->sh_flags |= SHF_GROUP;
654 1.1 christos
655 1.18 christos if (elt == NULL)
656 1.18 christos {
657 1.18 christos if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
658 1.1 christos {
659 1.18 christos const char *name = bfd_elf_string_from_elf_section
660 1.18 christos (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
661 1.1 christos
662 1.18 christos _bfd_error_handler
663 1.18 christos /* xgettext:c-format */
664 1.18 christos (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
665 1.18 christos abfd, shdr->sh_type, name, gidx);
666 1.5 christos }
667 1.18 christos continue;
668 1.1 christos }
669 1.1 christos
670 1.18 christos /* Don't try to add a section to elf_next_in_group list twice. */
671 1.18 christos if (elf_sec_group (elt) != NULL)
672 1.18 christos continue;
673 1.1 christos
674 1.18 christos if (last_elt == NULL)
675 1.1 christos {
676 1.18 christos /* Start a circular list with one element.
677 1.18 christos It will be in reverse order to match what gas does. */
678 1.18 christos elf_next_in_group (elt) = elt;
679 1.18 christos /* Point the group section to it. */
680 1.18 christos elf_next_in_group (ghdr->bfd_section) = elt;
681 1.18 christos gname = group_signature (abfd, ghdr);
682 1.18 christos if (gname == NULL)
683 1.11 christos {
684 1.18 christos free (contents);
685 1.14 christos return false;
686 1.11 christos }
687 1.1 christos }
688 1.18 christos else
689 1.18 christos {
690 1.18 christos elf_next_in_group (elt) = elf_next_in_group (last_elt);
691 1.18 christos elf_next_in_group (last_elt) = elt;
692 1.18 christos }
693 1.18 christos last_elt = elt;
694 1.18 christos elf_group_name (elt) = gname;
695 1.18 christos elf_sec_group (elt) = ghdr->bfd_section;
696 1.1 christos }
697 1.1 christos
698 1.18 christos free (contents);
699 1.14 christos return true;
700 1.1 christos }
701 1.1 christos
702 1.14 christos bool
703 1.1 christos _bfd_elf_setup_sections (bfd *abfd)
704 1.1 christos {
705 1.14 christos bool result = true;
706 1.1 christos
707 1.1 christos /* Process SHF_LINK_ORDER. */
708 1.18 christos for (asection *s = abfd->sections; s != NULL; s = s->next)
709 1.1 christos {
710 1.1 christos Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
711 1.1 christos if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
712 1.1 christos {
713 1.1 christos unsigned int elfsec = this_hdr->sh_link;
714 1.14 christos /* An sh_link value of 0 is now allowed. It indicates that linked
715 1.14 christos to section has already been discarded, but that the current
716 1.14 christos section has been retained for some other reason. This linking
717 1.14 christos section is still a candidate for later garbage collection
718 1.14 christos however. */
719 1.1 christos if (elfsec == 0)
720 1.1 christos {
721 1.14 christos elf_linked_to_section (s) = NULL;
722 1.1 christos }
723 1.1 christos else
724 1.1 christos {
725 1.1 christos asection *linksec = NULL;
726 1.1 christos
727 1.1 christos if (elfsec < elf_numsections (abfd))
728 1.1 christos {
729 1.1 christos this_hdr = elf_elfsections (abfd)[elfsec];
730 1.1 christos linksec = this_hdr->bfd_section;
731 1.1 christos }
732 1.1 christos
733 1.1 christos /* PR 1991, 2008:
734 1.1 christos Some strip/objcopy may leave an incorrect value in
735 1.1 christos sh_link. We don't want to proceed. */
736 1.1 christos if (linksec == NULL)
737 1.1 christos {
738 1.9 christos _bfd_error_handler
739 1.9 christos /* xgettext:c-format */
740 1.11 christos (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
741 1.9 christos s->owner, elfsec, s);
742 1.14 christos result = false;
743 1.1 christos }
744 1.1 christos
745 1.1 christos elf_linked_to_section (s) = linksec;
746 1.1 christos }
747 1.1 christos }
748 1.1 christos }
749 1.1 christos
750 1.1 christos /* Process section groups. */
751 1.18 christos for (unsigned int i = 1; i < elf_numsections (abfd); i++)
752 1.1 christos {
753 1.18 christos Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
754 1.1 christos
755 1.18 christos if (shdr && shdr->sh_type == SHT_GROUP)
756 1.11 christos {
757 1.18 christos if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
758 1.18 christos {
759 1.18 christos if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
760 1.18 christos && !process_sht_group_entries (abfd, shdr, i))
761 1.18 christos result = false;
762 1.18 christos }
763 1.18 christos else
764 1.11 christos {
765 1.18 christos /* PR binutils/18758: Beware of corrupt binaries with
766 1.18 christos invalid group data. */
767 1.11 christos _bfd_error_handler
768 1.11 christos /* xgettext:c-format */
769 1.18 christos (_("%pB: section group entry number %u is corrupt"), abfd, i);
770 1.14 christos result = false;
771 1.11 christos }
772 1.11 christos }
773 1.1 christos }
774 1.11 christos
775 1.1 christos return result;
776 1.1 christos }
777 1.1 christos
778 1.14 christos bool
779 1.1 christos bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
780 1.1 christos {
781 1.1 christos return elf_next_in_group (sec) != NULL;
782 1.1 christos }
783 1.1 christos
784 1.12 christos const char *
785 1.12 christos bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
786 1.12 christos {
787 1.12 christos if (elf_sec_group (sec) != NULL)
788 1.12 christos return elf_group_name (sec);
789 1.12 christos return NULL;
790 1.12 christos }
791 1.12 christos
792 1.1 christos /* Make a BFD section from an ELF section. We store a pointer to the
793 1.1 christos BFD section in the bfd_section field of the header. */
794 1.1 christos
795 1.14 christos bool
796 1.1 christos _bfd_elf_make_section_from_shdr (bfd *abfd,
797 1.1 christos Elf_Internal_Shdr *hdr,
798 1.1 christos const char *name,
799 1.1 christos int shindex)
800 1.1 christos {
801 1.1 christos asection *newsect;
802 1.1 christos flagword flags;
803 1.1 christos const struct elf_backend_data *bed;
804 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
805 1.1 christos
806 1.1 christos if (hdr->bfd_section != NULL)
807 1.14 christos return true;
808 1.1 christos
809 1.1 christos newsect = bfd_make_section_anyway (abfd, name);
810 1.1 christos if (newsect == NULL)
811 1.14 christos return false;
812 1.1 christos
813 1.1 christos hdr->bfd_section = newsect;
814 1.1 christos elf_section_data (newsect)->this_hdr = *hdr;
815 1.1 christos elf_section_data (newsect)->this_idx = shindex;
816 1.1 christos
817 1.1 christos /* Always use the real type/flags. */
818 1.1 christos elf_section_type (newsect) = hdr->sh_type;
819 1.1 christos elf_section_flags (newsect) = hdr->sh_flags;
820 1.1 christos
821 1.1 christos newsect->filepos = hdr->sh_offset;
822 1.1 christos
823 1.1 christos flags = SEC_NO_FLAGS;
824 1.1 christos if (hdr->sh_type != SHT_NOBITS)
825 1.1 christos flags |= SEC_HAS_CONTENTS;
826 1.1 christos if (hdr->sh_type == SHT_GROUP)
827 1.11 christos flags |= SEC_GROUP;
828 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0)
829 1.1 christos {
830 1.1 christos flags |= SEC_ALLOC;
831 1.1 christos if (hdr->sh_type != SHT_NOBITS)
832 1.1 christos flags |= SEC_LOAD;
833 1.1 christos }
834 1.1 christos if ((hdr->sh_flags & SHF_WRITE) == 0)
835 1.1 christos flags |= SEC_READONLY;
836 1.1 christos if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
837 1.1 christos flags |= SEC_CODE;
838 1.1 christos else if ((flags & SEC_LOAD) != 0)
839 1.1 christos flags |= SEC_DATA;
840 1.1 christos if ((hdr->sh_flags & SHF_MERGE) != 0)
841 1.1 christos {
842 1.1 christos flags |= SEC_MERGE;
843 1.1 christos newsect->entsize = hdr->sh_entsize;
844 1.1 christos }
845 1.8 christos if ((hdr->sh_flags & SHF_STRINGS) != 0)
846 1.18 christos {
847 1.18 christos flags |= SEC_STRINGS;
848 1.18 christos newsect->entsize = hdr->sh_entsize;
849 1.18 christos }
850 1.1 christos if ((hdr->sh_flags & SHF_TLS) != 0)
851 1.1 christos flags |= SEC_THREAD_LOCAL;
852 1.1 christos if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
853 1.1 christos flags |= SEC_EXCLUDE;
854 1.1 christos
855 1.12 christos switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
856 1.12 christos {
857 1.12 christos /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
858 1.12 christos but binutils as of 2019-07-23 did not set the EI_OSABI header
859 1.12 christos byte. */
860 1.12 christos case ELFOSABI_GNU:
861 1.12 christos case ELFOSABI_FREEBSD:
862 1.14 christos if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
863 1.14 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
864 1.14 christos /* Fall through */
865 1.14 christos case ELFOSABI_NONE:
866 1.12 christos if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
867 1.12 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
868 1.12 christos break;
869 1.12 christos }
870 1.12 christos
871 1.1 christos if ((flags & SEC_ALLOC) == 0)
872 1.1 christos {
873 1.1 christos /* The debugging sections appear to be recognized only by name,
874 1.1 christos not any sort of flag. Their SEC_ALLOC bits are cleared. */
875 1.1 christos if (name [0] == '.')
876 1.1 christos {
877 1.14 christos if (startswith (name, ".debug")
878 1.14 christos || startswith (name, ".gnu.debuglto_.debug_")
879 1.14 christos || startswith (name, ".gnu.linkonce.wi.")
880 1.14 christos || startswith (name, ".zdebug"))
881 1.12 christos flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
882 1.14 christos else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
883 1.14 christos || startswith (name, ".note.gnu"))
884 1.12 christos {
885 1.12 christos flags |= SEC_ELF_OCTETS;
886 1.12 christos opb = 1;
887 1.12 christos }
888 1.14 christos else if (startswith (name, ".line")
889 1.14 christos || startswith (name, ".stab")
890 1.12 christos || strcmp (name, ".gdb_index") == 0)
891 1.1 christos flags |= SEC_DEBUGGING;
892 1.1 christos }
893 1.1 christos }
894 1.1 christos
895 1.12 christos if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
896 1.12 christos || !bfd_set_section_size (newsect, hdr->sh_size)
897 1.14 christos || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
898 1.14 christos & -hdr->sh_addralign)))
899 1.14 christos return false;
900 1.12 christos
901 1.1 christos /* As a GNU extension, if the name begins with .gnu.linkonce, we
902 1.1 christos only link a single copy of the section. This is used to support
903 1.1 christos g++. g++ will emit each template expansion in its own section.
904 1.1 christos The symbols will be defined as weak, so that multiple definitions
905 1.1 christos are permitted. The GNU linker extension is to actually discard
906 1.1 christos all but one of the sections. */
907 1.14 christos if (startswith (name, ".gnu.linkonce")
908 1.1 christos && elf_next_in_group (newsect) == NULL)
909 1.1 christos flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
910 1.1 christos
911 1.12 christos if (!bfd_set_section_flags (newsect, flags))
912 1.14 christos return false;
913 1.12 christos
914 1.1 christos bed = get_elf_backend_data (abfd);
915 1.1 christos if (bed->elf_backend_section_flags)
916 1.12 christos if (!bed->elf_backend_section_flags (hdr))
917 1.14 christos return false;
918 1.1 christos
919 1.1 christos /* We do not parse the PT_NOTE segments as we are interested even in the
920 1.1 christos separate debug info files which may have the segments offsets corrupted.
921 1.1 christos PT_NOTEs from the core files are currently not parsed using BFD. */
922 1.14 christos if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
923 1.1 christos {
924 1.1 christos bfd_byte *contents;
925 1.1 christos
926 1.17 christos if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
927 1.14 christos return false;
928 1.1 christos
929 1.11 christos elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
930 1.11 christos hdr->sh_offset, hdr->sh_addralign);
931 1.17 christos _bfd_elf_munmap_section_contents (newsect, contents);
932 1.1 christos }
933 1.1 christos
934 1.12 christos if ((newsect->flags & SEC_ALLOC) != 0)
935 1.1 christos {
936 1.1 christos Elf_Internal_Phdr *phdr;
937 1.1 christos unsigned int i, nload;
938 1.1 christos
939 1.1 christos /* Some ELF linkers produce binaries with all the program header
940 1.1 christos p_paddr fields zero. If we have such a binary with more than
941 1.1 christos one PT_LOAD header, then leave the section lma equal to vma
942 1.1 christos so that we don't create sections with overlapping lma. */
943 1.1 christos phdr = elf_tdata (abfd)->phdr;
944 1.1 christos for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
945 1.1 christos if (phdr->p_paddr != 0)
946 1.1 christos break;
947 1.1 christos else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
948 1.1 christos ++nload;
949 1.1 christos if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
950 1.14 christos return true;
951 1.1 christos
952 1.1 christos phdr = elf_tdata (abfd)->phdr;
953 1.1 christos for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
954 1.1 christos {
955 1.3 christos if (((phdr->p_type == PT_LOAD
956 1.3 christos && (hdr->sh_flags & SHF_TLS) == 0)
957 1.3 christos || phdr->p_type == PT_TLS)
958 1.1 christos && ELF_SECTION_IN_SEGMENT (hdr, phdr))
959 1.1 christos {
960 1.12 christos if ((newsect->flags & SEC_LOAD) == 0)
961 1.1 christos newsect->lma = (phdr->p_paddr
962 1.12 christos + hdr->sh_addr - phdr->p_vaddr) / opb;
963 1.1 christos else
964 1.1 christos /* We used to use the same adjustment for SEC_LOAD
965 1.1 christos sections, but that doesn't work if the segment
966 1.1 christos is packed with code from multiple VMAs.
967 1.1 christos Instead we calculate the section LMA based on
968 1.1 christos the segment LMA. It is assumed that the
969 1.1 christos segment will contain sections with contiguous
970 1.1 christos LMAs, even if the VMAs are not. */
971 1.1 christos newsect->lma = (phdr->p_paddr
972 1.12 christos + hdr->sh_offset - phdr->p_offset) / opb;
973 1.1 christos
974 1.1 christos /* With contiguous segments, we can't tell from file
975 1.1 christos offsets whether a section with zero size should
976 1.1 christos be placed at the end of one segment or the
977 1.1 christos beginning of the next. Decide based on vaddr. */
978 1.1 christos if (hdr->sh_addr >= phdr->p_vaddr
979 1.1 christos && (hdr->sh_addr + hdr->sh_size
980 1.1 christos <= phdr->p_vaddr + phdr->p_memsz))
981 1.1 christos break;
982 1.1 christos }
983 1.1 christos }
984 1.1 christos }
985 1.1 christos
986 1.14 christos /* Compress/decompress DWARF debug sections with names: .debug_*,
987 1.14 christos .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
988 1.14 christos if ((newsect->flags & SEC_DEBUGGING) != 0
989 1.14 christos && (newsect->flags & SEC_HAS_CONTENTS) != 0
990 1.14 christos && (newsect->flags & SEC_ELF_OCTETS) != 0)
991 1.1 christos {
992 1.1 christos enum { nothing, compress, decompress } action = nothing;
993 1.6 christos int compression_header_size;
994 1.6 christos bfd_size_type uncompressed_size;
995 1.11 christos unsigned int uncompressed_align_power;
996 1.14 christos enum compression_type ch_type = ch_none;
997 1.14 christos bool compressed
998 1.14 christos = bfd_is_section_compressed_info (abfd, newsect,
999 1.14 christos &compression_header_size,
1000 1.14 christos &uncompressed_size,
1001 1.14 christos &uncompressed_align_power,
1002 1.14 christos &ch_type);
1003 1.14 christos
1004 1.14 christos /* Should we decompress? */
1005 1.14 christos if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1006 1.14 christos action = decompress;
1007 1.14 christos
1008 1.14 christos /* Should we compress? Or convert to a different compression? */
1009 1.14 christos else if ((abfd->flags & BFD_COMPRESS) != 0
1010 1.14 christos && newsect->size != 0
1011 1.14 christos && compression_header_size >= 0
1012 1.14 christos && uncompressed_size > 0)
1013 1.14 christos {
1014 1.14 christos if (!compressed)
1015 1.1 christos action = compress;
1016 1.6 christos else
1017 1.14 christos {
1018 1.14 christos enum compression_type new_ch_type = ch_none;
1019 1.14 christos if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1020 1.14 christos new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1021 1.14 christos ? ch_compress_zstd : ch_compress_zlib);
1022 1.14 christos if (new_ch_type != ch_type)
1023 1.14 christos action = compress;
1024 1.14 christos }
1025 1.1 christos }
1026 1.1 christos
1027 1.6 christos if (action == compress)
1028 1.1 christos {
1029 1.1 christos if (!bfd_init_section_compress_status (abfd, newsect))
1030 1.1 christos {
1031 1.9 christos _bfd_error_handler
1032 1.9 christos /* xgettext:c-format */
1033 1.14 christos (_("%pB: unable to compress section %s"), abfd, name);
1034 1.14 christos return false;
1035 1.1 christos }
1036 1.6 christos }
1037 1.14 christos else if (action == decompress)
1038 1.6 christos {
1039 1.1 christos if (!bfd_init_section_decompress_status (abfd, newsect))
1040 1.1 christos {
1041 1.9 christos _bfd_error_handler
1042 1.9 christos /* xgettext:c-format */
1043 1.14 christos (_("%pB: unable to decompress section %s"), abfd, name);
1044 1.14 christos return false;
1045 1.1 christos }
1046 1.14 christos #ifndef HAVE_ZSTD
1047 1.14 christos if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1048 1.6 christos {
1049 1.14 christos _bfd_error_handler
1050 1.14 christos /* xgettext:c-format */
1051 1.14 christos (_ ("%pB: section %s is compressed with zstd, but BFD "
1052 1.14 christos "is not built with zstd support"),
1053 1.14 christos abfd, name);
1054 1.14 christos newsect->compress_status = COMPRESS_SECTION_NONE;
1055 1.14 christos return false;
1056 1.14 christos }
1057 1.14 christos #endif
1058 1.14 christos if (abfd->is_linker_input
1059 1.14 christos && name[1] == 'z')
1060 1.14 christos {
1061 1.14 christos /* Rename section from .zdebug_* to .debug_* so that ld
1062 1.14 christos scripts will see this section as a debug section. */
1063 1.14 christos char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1064 1.1 christos if (new_name == NULL)
1065 1.14 christos return false;
1066 1.12 christos bfd_rename_section (newsect, new_name);
1067 1.1 christos }
1068 1.1 christos }
1069 1.1 christos }
1070 1.1 christos
1071 1.14 christos return true;
1072 1.1 christos }
1073 1.1 christos
1074 1.8 christos const char *const bfd_elf_section_type_names[] =
1075 1.8 christos {
1076 1.1 christos "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1077 1.1 christos "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1078 1.1 christos "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1079 1.1 christos };
1080 1.1 christos
1081 1.1 christos /* ELF relocs are against symbols. If we are producing relocatable
1082 1.1 christos output, and the reloc is against an external symbol, and nothing
1083 1.1 christos has given us any additional addend, the resulting reloc will also
1084 1.1 christos be against the same symbol. In such a case, we don't want to
1085 1.1 christos change anything about the way the reloc is handled, since it will
1086 1.1 christos all be done at final link time. Rather than put special case code
1087 1.1 christos into bfd_perform_relocation, all the reloc types use this howto
1088 1.14 christos function, or should call this function for relocatable output. */
1089 1.1 christos
1090 1.1 christos bfd_reloc_status_type
1091 1.1 christos bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1092 1.1 christos arelent *reloc_entry,
1093 1.1 christos asymbol *symbol,
1094 1.1 christos void *data ATTRIBUTE_UNUSED,
1095 1.1 christos asection *input_section,
1096 1.1 christos bfd *output_bfd,
1097 1.1 christos char **error_message ATTRIBUTE_UNUSED)
1098 1.1 christos {
1099 1.1 christos if (output_bfd != NULL
1100 1.1 christos && (symbol->flags & BSF_SECTION_SYM) == 0
1101 1.1 christos && (! reloc_entry->howto->partial_inplace
1102 1.1 christos || reloc_entry->addend == 0))
1103 1.1 christos {
1104 1.1 christos reloc_entry->address += input_section->output_offset;
1105 1.1 christos return bfd_reloc_ok;
1106 1.1 christos }
1107 1.1 christos
1108 1.14 christos /* In some cases the relocation should be treated as output section
1109 1.14 christos relative, as when linking ELF DWARF into PE COFF. Many ELF
1110 1.14 christos targets lack section relative relocations and instead use
1111 1.14 christos ordinary absolute relocations for references between DWARF
1112 1.14 christos sections. That is arguably a bug in those targets but it happens
1113 1.14 christos to work for the usual case of linking to non-loaded ELF debug
1114 1.14 christos sections with VMAs forced to zero. PE COFF on the other hand
1115 1.14 christos doesn't allow a section VMA of zero. */
1116 1.14 christos if (output_bfd == NULL
1117 1.14 christos && !reloc_entry->howto->pc_relative
1118 1.14 christos && (symbol->section->flags & SEC_DEBUGGING) != 0
1119 1.14 christos && (input_section->flags & SEC_DEBUGGING) != 0)
1120 1.14 christos reloc_entry->addend -= symbol->section->output_section->vma;
1121 1.14 christos
1122 1.1 christos return bfd_reloc_continue;
1123 1.1 christos }
1124 1.1 christos
1125 1.8 christos /* Returns TRUE if section A matches section B.
1127 1.8 christos Names, addresses and links may be different, but everything else
1128 1.8 christos should be the same. */
1129 1.14 christos
1130 1.8 christos static bool
1131 1.8 christos section_match (const Elf_Internal_Shdr * a,
1132 1.8 christos const Elf_Internal_Shdr * b)
1133 1.11 christos {
1134 1.11 christos if (a->sh_type != b->sh_type
1135 1.11 christos || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1136 1.11 christos || a->sh_addralign != b->sh_addralign
1137 1.14 christos || a->sh_entsize != b->sh_entsize)
1138 1.11 christos return false;
1139 1.11 christos if (a->sh_type == SHT_SYMTAB
1140 1.14 christos || a->sh_type == SHT_STRTAB)
1141 1.11 christos return true;
1142 1.8 christos return a->sh_size == b->sh_size;
1143 1.8 christos }
1144 1.8 christos
1145 1.8 christos /* Find a section in OBFD that has the same characteristics
1146 1.8 christos as IHEADER. Return the index of this section or SHN_UNDEF if
1147 1.8 christos none can be found. Check's section HINT first, as this is likely
1148 1.8 christos to be the correct section. */
1149 1.8 christos
1150 1.11 christos static unsigned int
1151 1.11 christos find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1152 1.8 christos const unsigned int hint)
1153 1.8 christos {
1154 1.8 christos Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1155 1.8 christos unsigned int i;
1156 1.9 christos
1157 1.9 christos BFD_ASSERT (iheader != NULL);
1158 1.9 christos
1159 1.11 christos /* See PR 20922 for a reproducer of the NULL test. */
1160 1.11 christos if (hint < elf_numsections (obfd)
1161 1.9 christos && oheaders[hint] != NULL
1162 1.8 christos && section_match (oheaders[hint], iheader))
1163 1.8 christos return hint;
1164 1.8 christos
1165 1.8 christos for (i = 1; i < elf_numsections (obfd); i++)
1166 1.8 christos {
1167 1.8 christos Elf_Internal_Shdr * oheader = oheaders[i];
1168 1.9 christos
1169 1.9 christos if (oheader == NULL)
1170 1.8 christos continue;
1171 1.8 christos if (section_match (oheader, iheader))
1172 1.8 christos /* FIXME: Do we care if there is a potential for
1173 1.8 christos multiple matches ? */
1174 1.8 christos return i;
1175 1.8 christos }
1176 1.8 christos
1177 1.8 christos return SHN_UNDEF;
1178 1.8 christos }
1179 1.8 christos
1180 1.8 christos /* PR 19938: Attempt to set the ELF section header fields of an OS or
1181 1.8 christos Processor specific section, based upon a matching input section.
1182 1.11 christos Returns TRUE upon success, FALSE otherwise. */
1183 1.14 christos
1184 1.8 christos static bool
1185 1.8 christos copy_special_section_fields (const bfd *ibfd,
1186 1.8 christos bfd *obfd,
1187 1.8 christos const Elf_Internal_Shdr *iheader,
1188 1.8 christos Elf_Internal_Shdr *oheader,
1189 1.8 christos const unsigned int secnum)
1190 1.8 christos {
1191 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1192 1.14 christos const Elf_Internal_Shdr **iheaders
1193 1.14 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1194 1.8 christos bool changed = false;
1195 1.8 christos unsigned int sh_link;
1196 1.8 christos
1197 1.8 christos if (oheader->sh_type == SHT_NOBITS)
1198 1.8 christos {
1199 1.8 christos /* This is a feature for objcopy --only-keep-debug:
1200 1.8 christos When a section's type is changed to NOBITS, we preserve
1201 1.8 christos the sh_link and sh_info fields so that they can be
1202 1.8 christos matched up with the original.
1203 1.8 christos
1204 1.8 christos Note: Strictly speaking these assignments are wrong.
1205 1.8 christos The sh_link and sh_info fields should point to the
1206 1.8 christos relevent sections in the output BFD, which may not be in
1207 1.8 christos the same location as they were in the input BFD. But
1208 1.8 christos the whole point of this action is to preserve the
1209 1.8 christos original values of the sh_link and sh_info fields, so
1210 1.8 christos that they can be matched up with the section headers in
1211 1.8 christos the original file. So strictly speaking we may be
1212 1.8 christos creating an invalid ELF file, but it is only for a file
1213 1.8 christos that just contains debug info and only for sections
1214 1.8 christos without any contents. */
1215 1.8 christos if (oheader->sh_link == 0)
1216 1.8 christos oheader->sh_link = iheader->sh_link;
1217 1.8 christos if (oheader->sh_info == 0)
1218 1.14 christos oheader->sh_info = iheader->sh_info;
1219 1.8 christos return true;
1220 1.8 christos }
1221 1.8 christos
1222 1.12 christos /* Allow the target a chance to decide how these fields should be set. */
1223 1.12 christos if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1224 1.14 christos iheader, oheader))
1225 1.8 christos return true;
1226 1.8 christos
1227 1.8 christos /* We have an iheader which might match oheader, and which has non-zero
1228 1.8 christos sh_info and/or sh_link fields. Attempt to follow those links and find
1229 1.8 christos the section in the output bfd which corresponds to the linked section
1230 1.8 christos in the input bfd. */
1231 1.8 christos if (iheader->sh_link != SHN_UNDEF)
1232 1.9 christos {
1233 1.9 christos /* See PR 20931 for a reproducer. */
1234 1.9 christos if (iheader->sh_link >= elf_numsections (ibfd))
1235 1.11 christos {
1236 1.9 christos _bfd_error_handler
1237 1.11 christos /* xgettext:c-format */
1238 1.9 christos (_("%pB: invalid sh_link field (%d) in section number %d"),
1239 1.14 christos ibfd, iheader->sh_link, secnum);
1240 1.9 christos return false;
1241 1.9 christos }
1242 1.8 christos
1243 1.8 christos sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1244 1.8 christos if (sh_link != SHN_UNDEF)
1245 1.8 christos {
1246 1.14 christos oheader->sh_link = sh_link;
1247 1.8 christos changed = true;
1248 1.8 christos }
1249 1.8 christos else
1250 1.8 christos /* FIXME: Should we install iheader->sh_link
1251 1.11 christos if we could not find a match ? */
1252 1.9 christos _bfd_error_handler
1253 1.11 christos /* xgettext:c-format */
1254 1.8 christos (_("%pB: failed to find link section for section %d"), obfd, secnum);
1255 1.8 christos }
1256 1.8 christos
1257 1.8 christos if (iheader->sh_info)
1258 1.8 christos {
1259 1.8 christos /* The sh_info field can hold arbitrary information, but if the
1260 1.8 christos SHF_LINK_INFO flag is set then it should be interpreted as a
1261 1.8 christos section index. */
1262 1.8 christos if (iheader->sh_flags & SHF_INFO_LINK)
1263 1.8 christos {
1264 1.8 christos sh_link = find_link (obfd, iheaders[iheader->sh_info],
1265 1.8 christos iheader->sh_info);
1266 1.8 christos if (sh_link != SHN_UNDEF)
1267 1.8 christos oheader->sh_flags |= SHF_INFO_LINK;
1268 1.8 christos }
1269 1.8 christos else
1270 1.8 christos /* No idea what it means - just copy it. */
1271 1.8 christos sh_link = iheader->sh_info;
1272 1.8 christos
1273 1.8 christos if (sh_link != SHN_UNDEF)
1274 1.8 christos {
1275 1.14 christos oheader->sh_info = sh_link;
1276 1.8 christos changed = true;
1277 1.8 christos }
1278 1.11 christos else
1279 1.9 christos _bfd_error_handler
1280 1.11 christos /* xgettext:c-format */
1281 1.8 christos (_("%pB: failed to find info section for section %d"), obfd, secnum);
1282 1.8 christos }
1283 1.8 christos
1284 1.8 christos return changed;
1285 1.11 christos }
1286 1.1 christos
1287 1.1 christos /* Copy the program header and other data from one object module to
1288 1.1 christos another. */
1289 1.14 christos
1290 1.1 christos bool
1291 1.1 christos _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1292 1.14 christos {
1293 1.14 christos const Elf_Internal_Shdr **iheaders
1294 1.8 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1295 1.8 christos Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1296 1.8 christos const struct elf_backend_data *bed;
1297 1.8 christos unsigned int i;
1298 1.1 christos
1299 1.8 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1300 1.14 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1301 1.1 christos return true;
1302 1.4 christos
1303 1.4 christos if (!elf_flags_init (obfd))
1304 1.4 christos {
1305 1.14 christos elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1306 1.4 christos elf_flags_init (obfd) = true;
1307 1.1 christos }
1308 1.1 christos
1309 1.4 christos elf_gp (obfd) = elf_gp (ibfd);
1310 1.4 christos
1311 1.4 christos /* Also copy the EI_OSABI field. */
1312 1.4 christos elf_elfheader (obfd)->e_ident[EI_OSABI] =
1313 1.1 christos elf_elfheader (ibfd)->e_ident[EI_OSABI];
1314 1.8 christos
1315 1.8 christos /* If set, copy the EI_ABIVERSION field. */
1316 1.8 christos if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1317 1.8 christos elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1318 1.11 christos = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1319 1.1 christos
1320 1.1 christos /* Copy object attributes. */
1321 1.8 christos _bfd_elf_copy_obj_attributes (ibfd, obfd);
1322 1.8 christos
1323 1.14 christos if (iheaders == NULL || oheaders == NULL)
1324 1.8 christos return true;
1325 1.8 christos
1326 1.8 christos bed = get_elf_backend_data (obfd);
1327 1.8 christos
1328 1.8 christos /* Possibly copy other fields in the section header. */
1329 1.8 christos for (i = 1; i < elf_numsections (obfd); i++)
1330 1.8 christos {
1331 1.8 christos unsigned int j;
1332 1.8 christos Elf_Internal_Shdr * oheader = oheaders[i];
1333 1.8 christos
1334 1.8 christos /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1335 1.8 christos because of a special case need for generating separate debug info
1336 1.8 christos files. See below for more details. */
1337 1.8 christos if (oheader == NULL
1338 1.8 christos || (oheader->sh_type != SHT_NOBITS
1339 1.8 christos && oheader->sh_type < SHT_LOOS))
1340 1.8 christos continue;
1341 1.8 christos
1342 1.8 christos /* Ignore empty sections, and sections whose
1343 1.8 christos fields have already been initialised. */
1344 1.8 christos if (oheader->sh_size == 0
1345 1.8 christos || (oheader->sh_info != 0 && oheader->sh_link != 0))
1346 1.8 christos continue;
1347 1.8 christos
1348 1.14 christos /* Scan for the matching section in the input bfd.
1349 1.14 christos First we try for a direct mapping between the input and
1350 1.8 christos output sections. */
1351 1.8 christos for (j = 1; j < elf_numsections (ibfd); j++)
1352 1.8 christos {
1353 1.8 christos const Elf_Internal_Shdr * iheader = iheaders[j];
1354 1.8 christos
1355 1.8 christos if (iheader == NULL)
1356 1.8 christos continue;
1357 1.8 christos
1358 1.8 christos if (oheader->bfd_section != NULL
1359 1.8 christos && iheader->bfd_section != NULL
1360 1.8 christos && iheader->bfd_section->output_section != NULL
1361 1.8 christos && iheader->bfd_section->output_section == oheader->bfd_section)
1362 1.14 christos {
1363 1.14 christos /* We have found a connection from the input section to
1364 1.14 christos the output section. Attempt to copy the header fields.
1365 1.14 christos If this fails then do not try any further sections -
1366 1.14 christos there should only be a one-to-one mapping between
1367 1.14 christos input and output. */
1368 1.14 christos if (!copy_special_section_fields (ibfd, obfd,
1369 1.8 christos iheader, oheader, i))
1370 1.8 christos j = elf_numsections (ibfd);
1371 1.8 christos break;
1372 1.8 christos }
1373 1.8 christos }
1374 1.8 christos
1375 1.8 christos if (j < elf_numsections (ibfd))
1376 1.8 christos continue;
1377 1.8 christos
1378 1.8 christos /* That failed. So try to deduce the corresponding input section.
1379 1.8 christos Unfortunately we cannot compare names as the output string table
1380 1.8 christos is empty, so instead we check size, address and type. */
1381 1.8 christos for (j = 1; j < elf_numsections (ibfd); j++)
1382 1.8 christos {
1383 1.8 christos const Elf_Internal_Shdr * iheader = iheaders[j];
1384 1.8 christos
1385 1.8 christos if (iheader == NULL)
1386 1.8 christos continue;
1387 1.8 christos
1388 1.8 christos /* Try matching fields in the input section's header.
1389 1.8 christos Since --only-keep-debug turns all non-debug sections into
1390 1.8 christos SHT_NOBITS sections, the output SHT_NOBITS type matches any
1391 1.8 christos input type. */
1392 1.8 christos if ((oheader->sh_type == SHT_NOBITS
1393 1.8 christos || iheader->sh_type == oheader->sh_type)
1394 1.8 christos && (iheader->sh_flags & ~ SHF_INFO_LINK)
1395 1.8 christos == (oheader->sh_flags & ~ SHF_INFO_LINK)
1396 1.8 christos && iheader->sh_addralign == oheader->sh_addralign
1397 1.8 christos && iheader->sh_entsize == oheader->sh_entsize
1398 1.8 christos && iheader->sh_size == oheader->sh_size
1399 1.8 christos && iheader->sh_addr == oheader->sh_addr
1400 1.8 christos && (iheader->sh_info != oheader->sh_info
1401 1.8 christos || iheader->sh_link != oheader->sh_link))
1402 1.8 christos {
1403 1.8 christos if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1404 1.8 christos break;
1405 1.8 christos }
1406 1.8 christos }
1407 1.8 christos
1408 1.8 christos if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1409 1.8 christos {
1410 1.8 christos /* Final attempt. Call the backend copy function
1411 1.12 christos with a NULL input section. */
1412 1.12 christos (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1413 1.8 christos NULL, oheader);
1414 1.8 christos }
1415 1.8 christos }
1416 1.14 christos
1417 1.1 christos return true;
1418 1.1 christos }
1419 1.1 christos
1420 1.1 christos static const char *
1421 1.1 christos get_segment_type (unsigned int p_type)
1422 1.1 christos {
1423 1.1 christos const char *pt;
1424 1.1 christos switch (p_type)
1425 1.1 christos {
1426 1.1 christos case PT_NULL: pt = "NULL"; break;
1427 1.1 christos case PT_LOAD: pt = "LOAD"; break;
1428 1.1 christos case PT_DYNAMIC: pt = "DYNAMIC"; break;
1429 1.1 christos case PT_INTERP: pt = "INTERP"; break;
1430 1.1 christos case PT_NOTE: pt = "NOTE"; break;
1431 1.1 christos case PT_SHLIB: pt = "SHLIB"; break;
1432 1.1 christos case PT_PHDR: pt = "PHDR"; break;
1433 1.1 christos case PT_TLS: pt = "TLS"; break;
1434 1.1 christos case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1435 1.1 christos case PT_GNU_STACK: pt = "STACK"; break;
1436 1.14 christos case PT_GNU_RELRO: pt = "RELRO"; break;
1437 1.1 christos case PT_GNU_SFRAME: pt = "SFRAME"; break;
1438 1.1 christos default: pt = NULL; break;
1439 1.1 christos }
1440 1.1 christos return pt;
1441 1.1 christos }
1442 1.1 christos
1443 1.1 christos /* Print out the program headers. */
1444 1.14 christos
1445 1.1 christos bool
1446 1.1 christos _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1447 1.1 christos {
1448 1.1 christos FILE *f = (FILE *) farg;
1449 1.1 christos Elf_Internal_Phdr *p;
1450 1.1 christos asection *s;
1451 1.1 christos bfd_byte *dynbuf = NULL;
1452 1.1 christos
1453 1.1 christos p = elf_tdata (abfd)->phdr;
1454 1.1 christos if (p != NULL)
1455 1.1 christos {
1456 1.1 christos unsigned int i, c;
1457 1.1 christos
1458 1.1 christos fprintf (f, _("\nProgram Header:\n"));
1459 1.1 christos c = elf_elfheader (abfd)->e_phnum;
1460 1.1 christos for (i = 0; i < c; i++, p++)
1461 1.1 christos {
1462 1.1 christos const char *pt = get_segment_type (p->p_type);
1463 1.1 christos char buf[20];
1464 1.1 christos
1465 1.1 christos if (pt == NULL)
1466 1.1 christos {
1467 1.1 christos sprintf (buf, "0x%lx", p->p_type);
1468 1.1 christos pt = buf;
1469 1.1 christos }
1470 1.1 christos fprintf (f, "%8s off 0x", pt);
1471 1.1 christos bfd_fprintf_vma (abfd, f, p->p_offset);
1472 1.1 christos fprintf (f, " vaddr 0x");
1473 1.1 christos bfd_fprintf_vma (abfd, f, p->p_vaddr);
1474 1.1 christos fprintf (f, " paddr 0x");
1475 1.1 christos bfd_fprintf_vma (abfd, f, p->p_paddr);
1476 1.1 christos fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1477 1.1 christos fprintf (f, " filesz 0x");
1478 1.1 christos bfd_fprintf_vma (abfd, f, p->p_filesz);
1479 1.1 christos fprintf (f, " memsz 0x");
1480 1.1 christos bfd_fprintf_vma (abfd, f, p->p_memsz);
1481 1.1 christos fprintf (f, " flags %c%c%c",
1482 1.1 christos (p->p_flags & PF_R) != 0 ? 'r' : '-',
1483 1.1 christos (p->p_flags & PF_W) != 0 ? 'w' : '-',
1484 1.1 christos (p->p_flags & PF_X) != 0 ? 'x' : '-');
1485 1.1 christos if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1486 1.1 christos fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1487 1.1 christos fprintf (f, "\n");
1488 1.1 christos }
1489 1.1 christos }
1490 1.1 christos
1491 1.17 christos s = bfd_get_section_by_name (abfd, ".dynamic");
1492 1.1 christos if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1493 1.1 christos {
1494 1.1 christos unsigned int elfsec;
1495 1.1 christos unsigned long shlink;
1496 1.1 christos bfd_byte *extdyn, *extdynend;
1497 1.1 christos size_t extdynsize;
1498 1.1 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1499 1.1 christos
1500 1.1 christos fprintf (f, _("\nDynamic Section:\n"));
1501 1.17 christos
1502 1.1 christos if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1503 1.1 christos goto error_return;
1504 1.1 christos
1505 1.1 christos elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1506 1.1 christos if (elfsec == SHN_BAD)
1507 1.1 christos goto error_return;
1508 1.1 christos shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1509 1.1 christos
1510 1.1 christos extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1511 1.1 christos swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1512 1.14 christos
1513 1.14 christos for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1514 1.14 christos (size_t) (extdynend - extdyn) >= extdynsize;
1515 1.1 christos extdyn += extdynsize)
1516 1.1 christos {
1517 1.1 christos Elf_Internal_Dyn dyn;
1518 1.1 christos const char *name = "";
1519 1.14 christos char ab[20];
1520 1.1 christos bool stringp;
1521 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1522 1.1 christos
1523 1.1 christos (*swap_dyn_in) (abfd, extdyn, &dyn);
1524 1.1 christos
1525 1.1 christos if (dyn.d_tag == DT_NULL)
1526 1.1 christos break;
1527 1.14 christos
1528 1.1 christos stringp = false;
1529 1.1 christos switch (dyn.d_tag)
1530 1.1 christos {
1531 1.1 christos default:
1532 1.1 christos if (bed->elf_backend_get_target_dtag)
1533 1.1 christos name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1534 1.1 christos
1535 1.1 christos if (!strcmp (name, ""))
1536 1.14 christos {
1537 1.1 christos sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1538 1.1 christos name = ab;
1539 1.1 christos }
1540 1.1 christos break;
1541 1.14 christos
1542 1.1 christos case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1543 1.1 christos case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1544 1.1 christos case DT_PLTGOT: name = "PLTGOT"; break;
1545 1.1 christos case DT_HASH: name = "HASH"; break;
1546 1.1 christos case DT_STRTAB: name = "STRTAB"; break;
1547 1.1 christos case DT_SYMTAB: name = "SYMTAB"; break;
1548 1.1 christos case DT_RELA: name = "RELA"; break;
1549 1.1 christos case DT_RELASZ: name = "RELASZ"; break;
1550 1.1 christos case DT_RELAENT: name = "RELAENT"; break;
1551 1.1 christos case DT_STRSZ: name = "STRSZ"; break;
1552 1.1 christos case DT_SYMENT: name = "SYMENT"; break;
1553 1.1 christos case DT_INIT: name = "INIT"; break;
1554 1.14 christos case DT_FINI: name = "FINI"; break;
1555 1.14 christos case DT_SONAME: name = "SONAME"; stringp = true; break;
1556 1.1 christos case DT_RPATH: name = "RPATH"; stringp = true; break;
1557 1.1 christos case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1558 1.1 christos case DT_REL: name = "REL"; break;
1559 1.1 christos case DT_RELSZ: name = "RELSZ"; break;
1560 1.14 christos case DT_RELENT: name = "RELENT"; break;
1561 1.14 christos case DT_RELR: name = "RELR"; break;
1562 1.14 christos case DT_RELRSZ: name = "RELRSZ"; break;
1563 1.1 christos case DT_RELRENT: name = "RELRENT"; break;
1564 1.1 christos case DT_PLTREL: name = "PLTREL"; break;
1565 1.1 christos case DT_DEBUG: name = "DEBUG"; break;
1566 1.1 christos case DT_TEXTREL: name = "TEXTREL"; break;
1567 1.1 christos case DT_JMPREL: name = "JMPREL"; break;
1568 1.1 christos case DT_BIND_NOW: name = "BIND_NOW"; break;
1569 1.1 christos case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1570 1.1 christos case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1571 1.1 christos case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1572 1.14 christos case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1573 1.1 christos case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1574 1.1 christos case DT_FLAGS: name = "FLAGS"; break;
1575 1.1 christos case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1576 1.1 christos case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1577 1.1 christos case DT_CHECKSUM: name = "CHECKSUM"; break;
1578 1.1 christos case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1579 1.1 christos case DT_MOVEENT: name = "MOVEENT"; break;
1580 1.1 christos case DT_MOVESZ: name = "MOVESZ"; break;
1581 1.1 christos case DT_FEATURE: name = "FEATURE"; break;
1582 1.1 christos case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1583 1.1 christos case DT_SYMINSZ: name = "SYMINSZ"; break;
1584 1.14 christos case DT_SYMINENT: name = "SYMINENT"; break;
1585 1.14 christos case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1586 1.14 christos case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1587 1.1 christos case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1588 1.1 christos case DT_PLTPAD: name = "PLTPAD"; break;
1589 1.1 christos case DT_MOVETAB: name = "MOVETAB"; break;
1590 1.1 christos case DT_SYMINFO: name = "SYMINFO"; break;
1591 1.1 christos case DT_RELACOUNT: name = "RELACOUNT"; break;
1592 1.1 christos case DT_RELCOUNT: name = "RELCOUNT"; break;
1593 1.1 christos case DT_FLAGS_1: name = "FLAGS_1"; break;
1594 1.1 christos case DT_VERSYM: name = "VERSYM"; break;
1595 1.1 christos case DT_VERDEF: name = "VERDEF"; break;
1596 1.1 christos case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1597 1.1 christos case DT_VERNEED: name = "VERNEED"; break;
1598 1.14 christos case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1599 1.1 christos case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1600 1.14 christos case DT_USED: name = "USED"; break;
1601 1.1 christos case DT_FILTER: name = "FILTER"; stringp = true; break;
1602 1.1 christos case DT_GNU_HASH: name = "GNU_HASH"; break;
1603 1.1 christos }
1604 1.1 christos
1605 1.1 christos fprintf (f, " %-20s ", name);
1606 1.1 christos if (! stringp)
1607 1.1 christos {
1608 1.1 christos fprintf (f, "0x");
1609 1.1 christos bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1610 1.1 christos }
1611 1.1 christos else
1612 1.1 christos {
1613 1.1 christos const char *string;
1614 1.1 christos unsigned int tagv = dyn.d_un.d_val;
1615 1.1 christos
1616 1.1 christos string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1617 1.1 christos if (string == NULL)
1618 1.1 christos goto error_return;
1619 1.1 christos fprintf (f, "%s", string);
1620 1.1 christos }
1621 1.1 christos fprintf (f, "\n");
1622 1.1 christos }
1623 1.17 christos
1624 1.1 christos _bfd_elf_munmap_section_contents (s, dynbuf);
1625 1.1 christos dynbuf = NULL;
1626 1.1 christos }
1627 1.1 christos
1628 1.1 christos if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1629 1.1 christos || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1630 1.14 christos {
1631 1.14 christos if (! _bfd_elf_slurp_version_tables (abfd, false))
1632 1.1 christos return false;
1633 1.1 christos }
1634 1.1 christos
1635 1.1 christos if (elf_dynverdef (abfd) != 0)
1636 1.1 christos {
1637 1.1 christos Elf_Internal_Verdef *t;
1638 1.1 christos
1639 1.1 christos fprintf (f, _("\nVersion definitions:\n"));
1640 1.1 christos for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1641 1.1 christos {
1642 1.1 christos fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1643 1.1 christos t->vd_flags, t->vd_hash,
1644 1.1 christos t->vd_nodename ? t->vd_nodename : "<corrupt>");
1645 1.1 christos if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1646 1.1 christos {
1647 1.1 christos Elf_Internal_Verdaux *a;
1648 1.1 christos
1649 1.1 christos fprintf (f, "\t");
1650 1.1 christos for (a = t->vd_auxptr->vda_nextptr;
1651 1.1 christos a != NULL;
1652 1.1 christos a = a->vda_nextptr)
1653 1.1 christos fprintf (f, "%s ",
1654 1.1 christos a->vda_nodename ? a->vda_nodename : "<corrupt>");
1655 1.1 christos fprintf (f, "\n");
1656 1.1 christos }
1657 1.1 christos }
1658 1.1 christos }
1659 1.1 christos
1660 1.1 christos if (elf_dynverref (abfd) != 0)
1661 1.1 christos {
1662 1.1 christos Elf_Internal_Verneed *t;
1663 1.1 christos
1664 1.1 christos fprintf (f, _("\nVersion References:\n"));
1665 1.1 christos for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1666 1.1 christos {
1667 1.1 christos Elf_Internal_Vernaux *a;
1668 1.1 christos
1669 1.1 christos fprintf (f, _(" required from %s:\n"),
1670 1.1 christos t->vn_filename ? t->vn_filename : "<corrupt>");
1671 1.1 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1672 1.1 christos fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1673 1.1 christos a->vna_flags, a->vna_other,
1674 1.1 christos a->vna_nodename ? a->vna_nodename : "<corrupt>");
1675 1.1 christos }
1676 1.1 christos }
1677 1.14 christos
1678 1.1 christos return true;
1679 1.1 christos
1680 1.17 christos error_return:
1681 1.14 christos _bfd_elf_munmap_section_contents (s, dynbuf);
1682 1.1 christos return false;
1683 1.1 christos }
1684 1.17 christos
1685 1.17 christos /* Find the file offset corresponding to VMA by using the program
1686 1.17 christos headers. */
1687 1.17 christos
1688 1.17 christos static file_ptr
1689 1.17 christos offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1690 1.17 christos size_t size, size_t *max_size_p)
1691 1.17 christos {
1692 1.17 christos Elf_Internal_Phdr *seg;
1693 1.17 christos size_t i;
1694 1.17 christos
1695 1.17 christos for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1696 1.17 christos if (seg->p_type == PT_LOAD
1697 1.17 christos && vma >= (seg->p_vaddr & -seg->p_align)
1698 1.17 christos && vma + size <= seg->p_vaddr + seg->p_filesz)
1699 1.17 christos {
1700 1.17 christos if (max_size_p)
1701 1.17 christos *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1702 1.17 christos return vma - seg->p_vaddr + seg->p_offset;
1703 1.17 christos }
1704 1.17 christos
1705 1.17 christos if (max_size_p)
1706 1.17 christos *max_size_p = 0;
1707 1.17 christos bfd_set_error (bfd_error_invalid_operation);
1708 1.17 christos return (file_ptr) -1;
1709 1.17 christos }
1710 1.17 christos
1711 1.17 christos /* Convert hash table to internal form. */
1712 1.17 christos
1713 1.17 christos static bfd_vma *
1714 1.17 christos get_hash_table_data (bfd *abfd, bfd_size_type number,
1715 1.17 christos unsigned int ent_size, bfd_size_type filesize)
1716 1.17 christos {
1717 1.17 christos unsigned char *e_data = NULL;
1718 1.17 christos bfd_vma *i_data = NULL;
1719 1.17 christos bfd_size_type size;
1720 1.17 christos void *e_data_addr;
1721 1.17 christos size_t e_data_size ATTRIBUTE_UNUSED;
1722 1.17 christos
1723 1.17 christos if (ent_size != 4 && ent_size != 8)
1724 1.17 christos return NULL;
1725 1.17 christos
1726 1.17 christos if ((size_t) number != number)
1727 1.17 christos {
1728 1.17 christos bfd_set_error (bfd_error_file_too_big);
1729 1.17 christos return NULL;
1730 1.17 christos }
1731 1.17 christos
1732 1.17 christos size = ent_size * number;
1733 1.17 christos /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1734 1.17 christos attempting to allocate memory when the read is bound to fail. */
1735 1.17 christos if (size > filesize
1736 1.17 christos || number >= ~(size_t) 0 / ent_size
1737 1.17 christos || number >= ~(size_t) 0 / sizeof (*i_data))
1738 1.17 christos {
1739 1.17 christos bfd_set_error (bfd_error_file_too_big);
1740 1.17 christos return NULL;
1741 1.17 christos }
1742 1.18 christos
1743 1.17 christos e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1744 1.17 christos if (e_data == NULL)
1745 1.17 christos return NULL;
1746 1.17 christos
1747 1.17 christos i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1748 1.17 christos if (i_data == NULL)
1749 1.17 christos {
1750 1.17 christos free (e_data);
1751 1.17 christos return NULL;
1752 1.17 christos }
1753 1.17 christos
1754 1.17 christos if (ent_size == 4)
1755 1.17 christos while (number--)
1756 1.17 christos i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1757 1.17 christos else
1758 1.17 christos while (number--)
1759 1.17 christos i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1760 1.18 christos
1761 1.17 christos _bfd_munmap_temporary (e_data_addr, e_data_size);
1762 1.17 christos return i_data;
1763 1.17 christos }
1764 1.17 christos
1765 1.17 christos /* Address of .MIPS.xhash section. FIXME: What is the best way to
1766 1.17 christos support DT_MIPS_XHASH? */
1767 1.17 christos #define DT_MIPS_XHASH 0x70000036
1768 1.17 christos
1769 1.17 christos /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1770 1.17 christos
1771 1.17 christos bool
1772 1.17 christos _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1773 1.17 christos Elf_Internal_Phdr *phdrs, size_t phnum,
1774 1.17 christos bfd_size_type filesize)
1775 1.17 christos {
1776 1.17 christos bfd_byte *extdyn, *extdynend;
1777 1.17 christos size_t extdynsize;
1778 1.17 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1779 1.17 christos bool (*swap_symbol_in) (bfd *, const void *, const void *,
1780 1.17 christos Elf_Internal_Sym *);
1781 1.17 christos Elf_Internal_Dyn dyn;
1782 1.17 christos bfd_vma dt_hash = 0;
1783 1.17 christos bfd_vma dt_gnu_hash = 0;
1784 1.17 christos bfd_vma dt_mips_xhash = 0;
1785 1.17 christos bfd_vma dt_strtab = 0;
1786 1.17 christos bfd_vma dt_symtab = 0;
1787 1.17 christos size_t dt_strsz = 0;
1788 1.17 christos bfd_vma dt_versym = 0;
1789 1.17 christos bfd_vma dt_verdef = 0;
1790 1.17 christos bfd_vma dt_verneed = 0;
1791 1.17 christos bfd_byte *dynbuf = NULL;
1792 1.17 christos char *strbuf = NULL;
1793 1.17 christos bfd_vma *gnubuckets = NULL;
1794 1.17 christos bfd_vma *gnuchains = NULL;
1795 1.17 christos bfd_vma *mipsxlat = NULL;
1796 1.17 christos file_ptr saved_filepos, filepos;
1797 1.17 christos bool res = false;
1798 1.17 christos size_t amt;
1799 1.17 christos bfd_byte *esymbuf = NULL, *esym;
1800 1.17 christos bfd_size_type symcount;
1801 1.17 christos Elf_Internal_Sym *isymbuf = NULL;
1802 1.17 christos Elf_Internal_Sym *isym, *isymend;
1803 1.17 christos bfd_byte *versym = NULL;
1804 1.17 christos bfd_byte *verdef = NULL;
1805 1.17 christos bfd_byte *verneed = NULL;
1806 1.17 christos size_t verdef_size = 0;
1807 1.17 christos size_t verneed_size = 0;
1808 1.17 christos size_t extsym_size;
1809 1.17 christos const struct elf_backend_data *bed;
1810 1.17 christos void *dynbuf_addr = NULL;
1811 1.17 christos void *esymbuf_addr = NULL;
1812 1.17 christos size_t dynbuf_size = 0;
1813 1.17 christos size_t esymbuf_size = 0;
1814 1.17 christos
1815 1.17 christos /* Return TRUE if symbol table is bad. */
1816 1.17 christos if (elf_bad_symtab (abfd))
1817 1.17 christos return true;
1818 1.17 christos
1819 1.17 christos /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
1820 1.17 christos if (elf_tdata (abfd)->dt_strtab != NULL)
1821 1.17 christos return true;
1822 1.17 christos
1823 1.17 christos bed = get_elf_backend_data (abfd);
1824 1.17 christos
1825 1.17 christos /* Save file position for elf_object_p. */
1826 1.17 christos saved_filepos = bfd_tell (abfd);
1827 1.17 christos
1828 1.17 christos if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1829 1.17 christos goto error_return;
1830 1.17 christos
1831 1.18 christos dynbuf_size = phdr->p_filesz;
1832 1.17 christos dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1833 1.17 christos if (dynbuf == NULL)
1834 1.17 christos goto error_return;
1835 1.17 christos
1836 1.17 christos extsym_size = bed->s->sizeof_sym;
1837 1.17 christos extdynsize = bed->s->sizeof_dyn;
1838 1.17 christos swap_dyn_in = bed->s->swap_dyn_in;
1839 1.17 christos
1840 1.17 christos extdyn = dynbuf;
1841 1.17 christos if (phdr->p_filesz < extdynsize)
1842 1.17 christos goto error_return;
1843 1.17 christos extdynend = extdyn + phdr->p_filesz;
1844 1.17 christos for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1845 1.17 christos {
1846 1.17 christos swap_dyn_in (abfd, extdyn, &dyn);
1847 1.17 christos
1848 1.17 christos if (dyn.d_tag == DT_NULL)
1849 1.17 christos break;
1850 1.17 christos
1851 1.17 christos switch (dyn.d_tag)
1852 1.17 christos {
1853 1.17 christos case DT_HASH:
1854 1.17 christos dt_hash = dyn.d_un.d_val;
1855 1.17 christos break;
1856 1.17 christos case DT_GNU_HASH:
1857 1.17 christos if (bed->elf_machine_code != EM_MIPS
1858 1.17 christos && bed->elf_machine_code != EM_MIPS_RS3_LE)
1859 1.17 christos dt_gnu_hash = dyn.d_un.d_val;
1860 1.17 christos break;
1861 1.17 christos case DT_STRTAB:
1862 1.17 christos dt_strtab = dyn.d_un.d_val;
1863 1.17 christos break;
1864 1.17 christos case DT_SYMTAB:
1865 1.17 christos dt_symtab = dyn.d_un.d_val;
1866 1.17 christos break;
1867 1.17 christos case DT_STRSZ:
1868 1.17 christos dt_strsz = dyn.d_un.d_val;
1869 1.17 christos break;
1870 1.17 christos case DT_SYMENT:
1871 1.17 christos if (dyn.d_un.d_val != extsym_size)
1872 1.17 christos goto error_return;
1873 1.17 christos break;
1874 1.17 christos case DT_VERSYM:
1875 1.17 christos dt_versym = dyn.d_un.d_val;
1876 1.17 christos break;
1877 1.17 christos case DT_VERDEF:
1878 1.17 christos dt_verdef = dyn.d_un.d_val;
1879 1.17 christos break;
1880 1.17 christos case DT_VERNEED:
1881 1.17 christos dt_verneed = dyn.d_un.d_val;
1882 1.17 christos break;
1883 1.17 christos default:
1884 1.17 christos if (dyn.d_tag == DT_MIPS_XHASH
1885 1.17 christos && (bed->elf_machine_code == EM_MIPS
1886 1.17 christos || bed->elf_machine_code == EM_MIPS_RS3_LE))
1887 1.17 christos {
1888 1.17 christos dt_gnu_hash = dyn.d_un.d_val;
1889 1.17 christos dt_mips_xhash = dyn.d_un.d_val;
1890 1.17 christos }
1891 1.17 christos break;
1892 1.17 christos }
1893 1.17 christos }
1894 1.17 christos
1895 1.17 christos /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1896 1.17 christos segment. */
1897 1.17 christos if ((!dt_hash && !dt_gnu_hash)
1898 1.17 christos || !dt_strtab
1899 1.17 christos || !dt_symtab
1900 1.17 christos || !dt_strsz)
1901 1.17 christos goto error_return;
1902 1.17 christos
1903 1.17 christos /* Get dynamic string table. */
1904 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1905 1.17 christos if (filepos == (file_ptr) -1
1906 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1907 1.17 christos goto error_return;
1908 1.17 christos
1909 1.18 christos /* Dynamic string table must be valid until ABFD is closed. */
1910 1.17 christos strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
1911 1.17 christos if (strbuf == NULL)
1912 1.17 christos goto error_return;
1913 1.17 christos if (strbuf[dt_strsz - 1] != 0)
1914 1.17 christos {
1915 1.17 christos /* It is an error if a string table is't terminated. */
1916 1.17 christos _bfd_error_handler
1917 1.17 christos /* xgettext:c-format */
1918 1.18 christos (_("%pB: DT_STRTAB table is corrupt"), abfd);
1919 1.17 christos strbuf[dt_strsz - 1] = 0;
1920 1.17 christos }
1921 1.17 christos
1922 1.17 christos /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
1923 1.17 christos DT_HASH since it is simpler than DT_GNU_HASH. */
1924 1.17 christos if (dt_hash)
1925 1.17 christos {
1926 1.17 christos unsigned char nb[16];
1927 1.17 christos unsigned int hash_ent_size;
1928 1.17 christos
1929 1.17 christos switch (bed->elf_machine_code)
1930 1.17 christos {
1931 1.17 christos case EM_ALPHA:
1932 1.17 christos case EM_S390:
1933 1.17 christos case EM_S390_OLD:
1934 1.17 christos if (bed->s->elfclass == ELFCLASS64)
1935 1.17 christos {
1936 1.17 christos hash_ent_size = 8;
1937 1.17 christos break;
1938 1.17 christos }
1939 1.17 christos /* FALLTHROUGH */
1940 1.17 christos default:
1941 1.17 christos hash_ent_size = 4;
1942 1.17 christos break;
1943 1.17 christos }
1944 1.17 christos
1945 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
1946 1.17 christos NULL);
1947 1.17 christos if (filepos == (file_ptr) -1
1948 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0
1949 1.17 christos || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
1950 1.17 christos goto error_return;
1951 1.17 christos
1952 1.17 christos /* The number of dynamic symbol table entries equals the number
1953 1.17 christos of chains. */
1954 1.17 christos if (hash_ent_size == 8)
1955 1.17 christos symcount = bfd_get_64 (abfd, nb + hash_ent_size);
1956 1.17 christos else
1957 1.17 christos symcount = bfd_get_32 (abfd, nb + hash_ent_size);
1958 1.17 christos }
1959 1.17 christos else
1960 1.17 christos {
1961 1.17 christos /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
1962 1.17 christos bindings are in hash table. Since in dynamic symbol table,
1963 1.17 christos all symbols with STB_LOCAL binding are placed before symbols
1964 1.17 christos with other bindings and all undefined symbols are placed
1965 1.17 christos before defined ones, the highest symbol index in DT_GNU_HASH
1966 1.17 christos is the highest dynamic symbol table index. */
1967 1.17 christos unsigned char nb[16];
1968 1.17 christos bfd_vma ngnubuckets;
1969 1.17 christos bfd_vma gnusymidx;
1970 1.17 christos size_t i, ngnuchains;
1971 1.17 christos bfd_vma maxchain = 0xffffffff, bitmaskwords;
1972 1.17 christos bfd_vma buckets_vma;
1973 1.17 christos
1974 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
1975 1.17 christos sizeof (nb), NULL);
1976 1.17 christos if (filepos == (file_ptr) -1
1977 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0
1978 1.17 christos || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
1979 1.17 christos goto error_return;
1980 1.17 christos
1981 1.17 christos ngnubuckets = bfd_get_32 (abfd, nb);
1982 1.17 christos gnusymidx = bfd_get_32 (abfd, nb + 4);
1983 1.17 christos bitmaskwords = bfd_get_32 (abfd, nb + 8);
1984 1.17 christos buckets_vma = dt_gnu_hash + 16;
1985 1.17 christos if (bed->s->elfclass == ELFCLASS32)
1986 1.17 christos buckets_vma += bitmaskwords * 4;
1987 1.17 christos else
1988 1.17 christos buckets_vma += bitmaskwords * 8;
1989 1.17 christos filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
1990 1.17 christos if (filepos == (file_ptr) -1
1991 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1992 1.17 christos goto error_return;
1993 1.17 christos
1994 1.17 christos gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
1995 1.17 christos if (gnubuckets == NULL)
1996 1.17 christos goto error_return;
1997 1.17 christos
1998 1.17 christos for (i = 0; i < ngnubuckets; i++)
1999 1.17 christos if (gnubuckets[i] != 0)
2000 1.17 christos {
2001 1.17 christos if (gnubuckets[i] < gnusymidx)
2002 1.17 christos goto error_return;
2003 1.17 christos
2004 1.17 christos if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2005 1.17 christos maxchain = gnubuckets[i];
2006 1.17 christos }
2007 1.17 christos
2008 1.17 christos if (maxchain == 0xffffffff)
2009 1.17 christos {
2010 1.17 christos symcount = 0;
2011 1.17 christos goto empty_gnu_hash;
2012 1.17 christos }
2013 1.17 christos
2014 1.17 christos maxchain -= gnusymidx;
2015 1.17 christos filepos = offset_from_vma (phdrs, phnum,
2016 1.17 christos (buckets_vma +
2017 1.17 christos 4 * (ngnubuckets + maxchain)),
2018 1.17 christos 4, NULL);
2019 1.17 christos if (filepos == (file_ptr) -1
2020 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2021 1.17 christos goto error_return;
2022 1.17 christos
2023 1.17 christos do
2024 1.17 christos {
2025 1.17 christos if (bfd_read (nb, 4, abfd) != 4)
2026 1.17 christos goto error_return;
2027 1.17 christos ++maxchain;
2028 1.17 christos if (maxchain == 0)
2029 1.17 christos goto error_return;
2030 1.17 christos }
2031 1.17 christos while ((bfd_get_32 (abfd, nb) & 1) == 0);
2032 1.17 christos
2033 1.17 christos filepos = offset_from_vma (phdrs, phnum,
2034 1.17 christos (buckets_vma + 4 * ngnubuckets),
2035 1.17 christos 4, NULL);
2036 1.17 christos if (filepos == (file_ptr) -1
2037 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2038 1.17 christos goto error_return;
2039 1.17 christos
2040 1.17 christos gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2041 1.17 christos if (gnuchains == NULL)
2042 1.17 christos goto error_return;
2043 1.17 christos ngnuchains = maxchain;
2044 1.17 christos
2045 1.17 christos if (dt_mips_xhash)
2046 1.17 christos {
2047 1.17 christos filepos = offset_from_vma (phdrs, phnum,
2048 1.17 christos (buckets_vma
2049 1.17 christos + 4 * (ngnubuckets + maxchain)),
2050 1.17 christos 4, NULL);
2051 1.17 christos if (filepos == (file_ptr) -1
2052 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2053 1.17 christos goto error_return;
2054 1.17 christos
2055 1.17 christos mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2056 1.17 christos if (mipsxlat == NULL)
2057 1.17 christos goto error_return;
2058 1.17 christos }
2059 1.17 christos
2060 1.17 christos symcount = 0;
2061 1.17 christos for (i = 0; i < ngnubuckets; ++i)
2062 1.17 christos if (gnubuckets[i] != 0)
2063 1.17 christos {
2064 1.17 christos bfd_vma si = gnubuckets[i];
2065 1.17 christos bfd_vma off = si - gnusymidx;
2066 1.17 christos do
2067 1.17 christos {
2068 1.17 christos if (mipsxlat)
2069 1.17 christos {
2070 1.17 christos if (mipsxlat[off] >= symcount)
2071 1.17 christos symcount = mipsxlat[off] + 1;
2072 1.17 christos }
2073 1.17 christos else
2074 1.17 christos {
2075 1.17 christos if (si >= symcount)
2076 1.17 christos symcount = si + 1;
2077 1.17 christos }
2078 1.17 christos si++;
2079 1.17 christos }
2080 1.17 christos while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2081 1.17 christos }
2082 1.17 christos }
2083 1.17 christos
2084 1.17 christos /* Swap in dynamic symbol table. */
2085 1.17 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2086 1.17 christos {
2087 1.17 christos bfd_set_error (bfd_error_file_too_big);
2088 1.17 christos goto error_return;
2089 1.17 christos }
2090 1.17 christos
2091 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2092 1.17 christos if (filepos == (file_ptr) -1
2093 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2094 1.17 christos goto error_return;
2095 1.18 christos esymbuf_size = amt;
2096 1.18 christos esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2097 1.17 christos &esymbuf_addr, &esymbuf_size);
2098 1.17 christos if (esymbuf == NULL)
2099 1.17 christos goto error_return;
2100 1.17 christos
2101 1.17 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2102 1.17 christos {
2103 1.17 christos bfd_set_error (bfd_error_file_too_big);
2104 1.17 christos goto error_return;
2105 1.17 christos }
2106 1.17 christos
2107 1.17 christos /* Dynamic symbol table must be valid until ABFD is closed. */
2108 1.17 christos isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2109 1.17 christos if (isymbuf == NULL)
2110 1.17 christos goto error_return;
2111 1.17 christos
2112 1.17 christos swap_symbol_in = bed->s->swap_symbol_in;
2113 1.17 christos
2114 1.17 christos /* Convert the symbols to internal form. */
2115 1.17 christos isymend = isymbuf + symcount;
2116 1.17 christos for (esym = esymbuf, isym = isymbuf;
2117 1.17 christos isym < isymend;
2118 1.17 christos esym += extsym_size, isym++)
2119 1.17 christos if (!swap_symbol_in (abfd, esym, NULL, isym)
2120 1.17 christos || isym->st_name >= dt_strsz)
2121 1.17 christos {
2122 1.17 christos bfd_set_error (bfd_error_invalid_operation);
2123 1.17 christos goto error_return;
2124 1.17 christos }
2125 1.17 christos
2126 1.17 christos if (dt_versym)
2127 1.17 christos {
2128 1.17 christos /* Swap in DT_VERSYM. */
2129 1.17 christos if (_bfd_mul_overflow (symcount, 2, &amt))
2130 1.17 christos {
2131 1.17 christos bfd_set_error (bfd_error_file_too_big);
2132 1.17 christos goto error_return;
2133 1.17 christos }
2134 1.17 christos
2135 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2136 1.17 christos if (filepos == (file_ptr) -1
2137 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2138 1.17 christos goto error_return;
2139 1.17 christos
2140 1.18 christos /* DT_VERSYM info must be valid until ABFD is closed. */
2141 1.17 christos versym = _bfd_mmap_persistent (abfd, amt);
2142 1.17 christos
2143 1.17 christos if (dt_verdef)
2144 1.17 christos {
2145 1.17 christos /* Read in DT_VERDEF. */
2146 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2147 1.17 christos 0, &verdef_size);
2148 1.17 christos if (filepos == (file_ptr) -1
2149 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2150 1.17 christos goto error_return;
2151 1.17 christos
2152 1.18 christos /* DT_VERDEF info must be valid until ABFD is closed. */
2153 1.17 christos verdef = _bfd_mmap_persistent (abfd, verdef_size);
2154 1.17 christos }
2155 1.17 christos
2156 1.17 christos if (dt_verneed)
2157 1.17 christos {
2158 1.17 christos /* Read in DT_VERNEED. */
2159 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2160 1.17 christos 0, &verneed_size);
2161 1.17 christos if (filepos == (file_ptr) -1
2162 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2163 1.17 christos goto error_return;
2164 1.17 christos
2165 1.18 christos /* DT_VERNEED info must be valid until ABFD is closed. */
2166 1.17 christos verneed = _bfd_mmap_persistent (abfd, verneed_size);
2167 1.17 christos }
2168 1.17 christos }
2169 1.17 christos
2170 1.17 christos empty_gnu_hash:
2171 1.17 christos elf_tdata (abfd)->dt_strtab = strbuf;
2172 1.17 christos elf_tdata (abfd)->dt_strsz = dt_strsz;
2173 1.17 christos elf_tdata (abfd)->dt_symtab = isymbuf;
2174 1.17 christos elf_tdata (abfd)->dt_symtab_count = symcount;
2175 1.17 christos elf_tdata (abfd)->dt_versym = versym;
2176 1.17 christos elf_tdata (abfd)->dt_verdef = verdef;
2177 1.17 christos elf_tdata (abfd)->dt_verneed = verneed;
2178 1.17 christos elf_tdata (abfd)->dt_verdef_count
2179 1.17 christos = verdef_size / sizeof (Elf_External_Verdef);
2180 1.17 christos elf_tdata (abfd)->dt_verneed_count
2181 1.17 christos = verneed_size / sizeof (Elf_External_Verneed);
2182 1.17 christos
2183 1.17 christos res = true;
2184 1.17 christos
2185 1.17 christos error_return:
2186 1.17 christos /* Restore file position for elf_object_p. */
2187 1.17 christos if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2188 1.18 christos res = false;
2189 1.18 christos _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2190 1.17 christos _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2191 1.17 christos free (gnubuckets);
2192 1.17 christos free (gnuchains);
2193 1.17 christos free (mipsxlat);
2194 1.17 christos return res;
2195 1.17 christos }
2196 1.17 christos
2197 1.17 christos /* Reconstruct section from dynamic symbol. */
2198 1.17 christos
2199 1.17 christos asection *
2200 1.17 christos _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2201 1.17 christos Elf_Internal_Sym *isym)
2202 1.17 christos {
2203 1.17 christos asection *sec;
2204 1.17 christos flagword flags;
2205 1.17 christos
2206 1.17 christos if (!elf_use_dt_symtab_p (abfd))
2207 1.17 christos return NULL;
2208 1.17 christos
2209 1.17 christos flags = SEC_ALLOC | SEC_LOAD;
2210 1.17 christos switch (ELF_ST_TYPE (isym->st_info))
2211 1.17 christos {
2212 1.17 christos case STT_FUNC:
2213 1.17 christos case STT_GNU_IFUNC:
2214 1.17 christos sec = bfd_get_section_by_name (abfd, ".text");
2215 1.17 christos if (sec == NULL)
2216 1.17 christos sec = bfd_make_section_with_flags (abfd,
2217 1.17 christos ".text",
2218 1.17 christos flags | SEC_CODE);
2219 1.17 christos break;
2220 1.17 christos case STT_COMMON:
2221 1.17 christos sec = bfd_com_section_ptr;
2222 1.17 christos break;
2223 1.17 christos case STT_OBJECT:
2224 1.17 christos sec = bfd_get_section_by_name (abfd, ".data");
2225 1.17 christos if (sec == NULL)
2226 1.17 christos sec = bfd_make_section_with_flags (abfd,
2227 1.17 christos ".data",
2228 1.17 christos flags | SEC_DATA);
2229 1.17 christos break;
2230 1.17 christos case STT_TLS:
2231 1.17 christos sec = bfd_get_section_by_name (abfd, ".tdata");
2232 1.17 christos if (sec == NULL)
2233 1.17 christos sec = bfd_make_section_with_flags (abfd,
2234 1.17 christos ".tdata",
2235 1.17 christos (flags
2236 1.17 christos | SEC_DATA
2237 1.17 christos | SEC_THREAD_LOCAL));
2238 1.17 christos break;
2239 1.17 christos default:
2240 1.17 christos sec = bfd_abs_section_ptr;
2241 1.17 christos break;
2242 1.17 christos }
2243 1.17 christos
2244 1.17 christos return sec;
2245 1.17 christos }
2246 1.12 christos
2247 1.12 christos /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2248 1.5 christos and return symbol version for symbol version itself. */
2249 1.5 christos
2250 1.5 christos const char *
2251 1.14 christos _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2252 1.14 christos bool base_p,
2253 1.5 christos bool *hidden)
2254 1.5 christos {
2255 1.17 christos const char *version_string = NULL;
2256 1.17 christos if ((elf_dynversym (abfd) != 0
2257 1.17 christos && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2258 1.17 christos || (elf_tdata (abfd)->dt_versym != NULL
2259 1.17 christos && (elf_tdata (abfd)->dt_verdef != NULL
2260 1.5 christos || elf_tdata (abfd)->dt_verneed != NULL)))
2261 1.5 christos {
2262 1.5 christos unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2263 1.5 christos
2264 1.5 christos *hidden = (vernum & VERSYM_HIDDEN) != 0;
2265 1.5 christos vernum &= VERSYM_VERSION;
2266 1.5 christos
2267 1.5 christos if (vernum == 0)
2268 1.11 christos version_string = "";
2269 1.11 christos else if (vernum == 1
2270 1.11 christos && (vernum > elf_tdata (abfd)->cverdefs
2271 1.11 christos || (elf_tdata (abfd)->verdef[0].vd_flags
2272 1.12 christos == VER_FLG_BASE)))
2273 1.5 christos version_string = base_p ? "Base" : "";
2274 1.12 christos else if (vernum <= elf_tdata (abfd)->cverdefs)
2275 1.12 christos {
2276 1.12 christos const char *nodename
2277 1.12 christos = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2278 1.12 christos version_string = "";
2279 1.12 christos if (base_p
2280 1.12 christos || nodename == NULL
2281 1.12 christos || symbol->name == NULL
2282 1.12 christos || strcmp (symbol->name, nodename) != 0)
2283 1.12 christos version_string = nodename;
2284 1.5 christos }
2285 1.5 christos else
2286 1.5 christos {
2287 1.5 christos Elf_Internal_Verneed *t;
2288 1.11 christos
2289 1.5 christos version_string = _("<corrupt>");
2290 1.5 christos for (t = elf_tdata (abfd)->verref;
2291 1.5 christos t != NULL;
2292 1.5 christos t = t->vn_nextref)
2293 1.5 christos {
2294 1.5 christos Elf_Internal_Vernaux *a;
2295 1.5 christos
2296 1.5 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2297 1.5 christos {
2298 1.5 christos if (a->vna_other == vernum)
2299 1.14 christos {
2300 1.5 christos *hidden = true;
2301 1.5 christos version_string = a->vna_nodename;
2302 1.5 christos break;
2303 1.5 christos }
2304 1.5 christos }
2305 1.5 christos }
2306 1.5 christos }
2307 1.5 christos }
2308 1.5 christos return version_string;
2309 1.5 christos }
2310 1.1 christos
2311 1.1 christos /* Display ELF-specific fields of a symbol. */
2312 1.1 christos
2313 1.1 christos void
2314 1.1 christos bfd_elf_print_symbol (bfd *abfd,
2315 1.1 christos void *filep,
2316 1.1 christos asymbol *symbol,
2317 1.1 christos bfd_print_symbol_type how)
2318 1.1 christos {
2319 1.18 christos FILE *file = (FILE *) filep;
2320 1.18 christos const char *symname = (symbol->name != bfd_symbol_error_name
2321 1.18 christos ? symbol->name : _("<corrupt>"));
2322 1.1 christos
2323 1.1 christos switch (how)
2324 1.1 christos {
2325 1.18 christos case bfd_print_symbol_name:
2326 1.1 christos fprintf (file, "%s", symname);
2327 1.1 christos break;
2328 1.1 christos case bfd_print_symbol_more:
2329 1.1 christos fprintf (file, "elf ");
2330 1.11 christos bfd_fprintf_vma (abfd, file, symbol->value);
2331 1.1 christos fprintf (file, " %x", symbol->flags);
2332 1.1 christos break;
2333 1.1 christos case bfd_print_symbol_all:
2334 1.1 christos {
2335 1.1 christos const char *section_name;
2336 1.1 christos const char *name = NULL;
2337 1.1 christos const struct elf_backend_data *bed;
2338 1.1 christos unsigned char st_other;
2339 1.5 christos bfd_vma val;
2340 1.14 christos const char *version_string;
2341 1.1 christos bool hidden;
2342 1.1 christos
2343 1.1 christos section_name = symbol->section ? symbol->section->name : "(*none*)";
2344 1.1 christos
2345 1.1 christos bed = get_elf_backend_data (abfd);
2346 1.1 christos if (bed->elf_backend_print_symbol_all)
2347 1.1 christos name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2348 1.18 christos
2349 1.18 christos if (name != NULL)
2350 1.18 christos symname = name;
2351 1.18 christos else
2352 1.1 christos bfd_print_symbol_vandf (abfd, file, symbol);
2353 1.1 christos
2354 1.1 christos fprintf (file, " %s\t", section_name);
2355 1.1 christos /* Print the "other" value for a symbol. For common symbols,
2356 1.1 christos we've already printed the size; now print the alignment.
2357 1.1 christos For other symbols, we have no specified alignment, and
2358 1.1 christos we've printed the address; now print the size. */
2359 1.1 christos if (symbol->section && bfd_is_com_section (symbol->section))
2360 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2361 1.1 christos else
2362 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2363 1.1 christos bfd_fprintf_vma (abfd, file, val);
2364 1.1 christos
2365 1.5 christos /* If we have version information, print it. */
2366 1.5 christos version_string = _bfd_elf_get_symbol_version_string (abfd,
2367 1.14 christos symbol,
2368 1.5 christos true,
2369 1.5 christos &hidden);
2370 1.1 christos if (version_string)
2371 1.5 christos {
2372 1.1 christos if (!hidden)
2373 1.1 christos fprintf (file, " %-11s", version_string);
2374 1.1 christos else
2375 1.1 christos {
2376 1.1 christos int i;
2377 1.1 christos
2378 1.1 christos fprintf (file, " (%s)", version_string);
2379 1.1 christos for (i = 10 - strlen (version_string); i > 0; --i)
2380 1.1 christos putc (' ', file);
2381 1.1 christos }
2382 1.1 christos }
2383 1.1 christos
2384 1.1 christos /* If the st_other field is not zero, print it. */
2385 1.1 christos st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2386 1.1 christos
2387 1.1 christos switch (st_other)
2388 1.1 christos {
2389 1.1 christos case 0: break;
2390 1.1 christos case STV_INTERNAL: fprintf (file, " .internal"); break;
2391 1.1 christos case STV_HIDDEN: fprintf (file, " .hidden"); break;
2392 1.1 christos case STV_PROTECTED: fprintf (file, " .protected"); break;
2393 1.1 christos default:
2394 1.1 christos /* Some other non-defined flags are also present, so print
2395 1.1 christos everything hex. */
2396 1.1 christos fprintf (file, " 0x%02x", (unsigned int) st_other);
2397 1.1 christos }
2398 1.18 christos
2399 1.1 christos fprintf (file, " %s", symname);
2400 1.1 christos }
2401 1.1 christos break;
2402 1.1 christos }
2403 1.1 christos }
2404 1.1 christos
2405 1.1 christos /* ELF .o/exec file reading */
2407 1.1 christos
2408 1.14 christos /* Create a new bfd section from an ELF section header. */
2409 1.1 christos
2410 1.1 christos bool
2411 1.1 christos bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2412 1.1 christos {
2413 1.1 christos Elf_Internal_Shdr *hdr;
2414 1.1 christos Elf_Internal_Ehdr *ehdr;
2415 1.14 christos const struct elf_backend_data *bed;
2416 1.1 christos const char *name;
2417 1.1 christos bool ret = true;
2418 1.14 christos
2419 1.1 christos if (shindex >= elf_numsections (abfd))
2420 1.14 christos return false;
2421 1.14 christos
2422 1.14 christos /* PR17512: A corrupt ELF binary might contain a loop of sections via
2423 1.14 christos sh_link or sh_info. Detect this here, by refusing to load a
2424 1.5 christos section that we are already in the process of loading. */
2425 1.14 christos if (elf_tdata (abfd)->being_created[shindex])
2426 1.14 christos {
2427 1.14 christos _bfd_error_handler
2428 1.5 christos (_("%pB: warning: loop in section dependencies detected"), abfd);
2429 1.14 christos return false;
2430 1.5 christos }
2431 1.1 christos elf_tdata (abfd)->being_created[shindex] = true;
2432 1.1 christos
2433 1.1 christos hdr = elf_elfsections (abfd)[shindex];
2434 1.1 christos ehdr = elf_elfheader (abfd);
2435 1.1 christos name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2436 1.5 christos hdr->sh_name);
2437 1.1 christos if (name == NULL)
2438 1.1 christos goto fail;
2439 1.1 christos
2440 1.1 christos bed = get_elf_backend_data (abfd);
2441 1.1 christos switch (hdr->sh_type)
2442 1.1 christos {
2443 1.5 christos case SHT_NULL:
2444 1.1 christos /* Inactive section. Throw it away. */
2445 1.5 christos goto success;
2446 1.5 christos
2447 1.5 christos case SHT_PROGBITS: /* Normal section with contents. */
2448 1.5 christos case SHT_NOBITS: /* .bss section. */
2449 1.1 christos case SHT_HASH: /* .hash section. */
2450 1.1 christos case SHT_NOTE: /* .note section. */
2451 1.1 christos case SHT_INIT_ARRAY: /* .init_array section. */
2452 1.1 christos case SHT_FINI_ARRAY: /* .fini_array section. */
2453 1.1 christos case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2454 1.5 christos case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2455 1.5 christos case SHT_GNU_HASH: /* .gnu.hash section. */
2456 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2457 1.1 christos goto success;
2458 1.1 christos
2459 1.5 christos case SHT_DYNAMIC: /* Dynamic linking information. */
2460 1.5 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2461 1.1 christos goto fail;
2462 1.1 christos
2463 1.14 christos if (hdr->sh_link > elf_numsections (abfd))
2464 1.14 christos {
2465 1.1 christos /* PR 10478: Accept Solaris binaries with a sh_link field
2466 1.1 christos set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2467 1.1 christos switch (bfd_get_arch (abfd))
2468 1.1 christos {
2469 1.14 christos case bfd_arch_i386:
2470 1.14 christos case bfd_arch_sparc:
2471 1.1 christos if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2472 1.1 christos || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2473 1.1 christos break;
2474 1.5 christos /* Otherwise fall through. */
2475 1.1 christos default:
2476 1.1 christos goto fail;
2477 1.1 christos }
2478 1.5 christos }
2479 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2480 1.1 christos goto fail;
2481 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2482 1.1 christos {
2483 1.1 christos Elf_Internal_Shdr *dynsymhdr;
2484 1.1 christos
2485 1.1 christos /* The shared libraries distributed with hpux11 have a bogus
2486 1.1 christos sh_link field for the ".dynamic" section. Find the
2487 1.1 christos string table for the ".dynsym" section instead. */
2488 1.1 christos if (elf_dynsymtab (abfd) != 0)
2489 1.1 christos {
2490 1.1 christos dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2491 1.1 christos hdr->sh_link = dynsymhdr->sh_link;
2492 1.1 christos }
2493 1.1 christos else
2494 1.1 christos {
2495 1.1 christos unsigned int i, num_sec;
2496 1.1 christos
2497 1.1 christos num_sec = elf_numsections (abfd);
2498 1.1 christos for (i = 1; i < num_sec; i++)
2499 1.1 christos {
2500 1.1 christos dynsymhdr = elf_elfsections (abfd)[i];
2501 1.1 christos if (dynsymhdr->sh_type == SHT_DYNSYM)
2502 1.1 christos {
2503 1.1 christos hdr->sh_link = dynsymhdr->sh_link;
2504 1.1 christos break;
2505 1.1 christos }
2506 1.1 christos }
2507 1.5 christos }
2508 1.1 christos }
2509 1.5 christos goto success;
2510 1.1 christos
2511 1.5 christos case SHT_SYMTAB: /* A symbol table. */
2512 1.1 christos if (elf_onesymtab (abfd) == shindex)
2513 1.1 christos goto success;
2514 1.5 christos
2515 1.5 christos if (hdr->sh_entsize != bed->s->sizeof_sym)
2516 1.1 christos goto fail;
2517 1.3 christos
2518 1.3 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2519 1.5 christos {
2520 1.3 christos if (hdr->sh_size != 0)
2521 1.3 christos goto fail;
2522 1.3 christos /* Some assemblers erroneously set sh_info to one with a
2523 1.3 christos zero sh_size. ld sees this as a global symbol count
2524 1.5 christos of (unsigned) -1. Fix it here. */
2525 1.3 christos hdr->sh_info = 0;
2526 1.5 christos goto success;
2527 1.8 christos }
2528 1.8 christos
2529 1.8 christos /* PR 18854: A binary might contain more than one symbol table.
2530 1.8 christos Unusual, but possible. Warn, but continue. */
2531 1.9 christos if (elf_onesymtab (abfd) != 0)
2532 1.9 christos {
2533 1.11 christos _bfd_error_handler
2534 1.9 christos /* xgettext:c-format */
2535 1.8 christos (_("%pB: warning: multiple symbol tables detected"
2536 1.8 christos " - ignoring the table in section %u"),
2537 1.8 christos abfd, shindex);
2538 1.1 christos goto success;
2539 1.8 christos }
2540 1.8 christos elf_onesymtab (abfd) = shindex;
2541 1.1 christos elf_symtab_hdr (abfd) = *hdr;
2542 1.1 christos elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2543 1.1 christos abfd->flags |= HAS_SYMS;
2544 1.1 christos
2545 1.1 christos /* Sometimes a shared object will map in the symbol table. If
2546 1.1 christos SHF_ALLOC is set, and this is a shared object, then we also
2547 1.1 christos treat this section as a BFD section. We can not base the
2548 1.1 christos decision purely on SHF_ALLOC, because that flag is sometimes
2549 1.1 christos set in a relocatable object file, which would confuse the
2550 1.1 christos linker. */
2551 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0
2552 1.1 christos && (abfd->flags & DYNAMIC) != 0
2553 1.5 christos && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2554 1.1 christos shindex))
2555 1.1 christos goto fail;
2556 1.1 christos
2557 1.1 christos /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2558 1.8 christos can't read symbols without that section loaded as well. It
2559 1.8 christos is most likely specified by the next section header. */
2560 1.8 christos {
2561 1.8 christos elf_section_list * entry;
2562 1.14 christos unsigned int i, num_sec;
2563 1.8 christos
2564 1.8 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2565 1.8 christos if (entry->hdr.sh_link == shindex)
2566 1.8 christos goto success;
2567 1.8 christos
2568 1.8 christos num_sec = elf_numsections (abfd);
2569 1.8 christos for (i = shindex + 1; i < num_sec; i++)
2570 1.8 christos {
2571 1.8 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2572 1.8 christos
2573 1.8 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2574 1.8 christos && hdr2->sh_link == shindex)
2575 1.1 christos break;
2576 1.8 christos }
2577 1.8 christos
2578 1.1 christos if (i == num_sec)
2579 1.1 christos for (i = 1; i < shindex; i++)
2580 1.8 christos {
2581 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2582 1.1 christos
2583 1.1 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2584 1.1 christos && hdr2->sh_link == shindex)
2585 1.8 christos break;
2586 1.8 christos }
2587 1.8 christos
2588 1.14 christos if (i != shindex)
2589 1.14 christos ret = bfd_section_from_shdr (abfd, i);
2590 1.8 christos /* else FIXME: we have failed to find the symbol table.
2591 1.8 christos Should we issue an error? */
2592 1.1 christos goto success;
2593 1.5 christos }
2594 1.1 christos
2595 1.5 christos case SHT_DYNSYM: /* A dynamic symbol table. */
2596 1.1 christos if (elf_dynsymtab (abfd) == shindex)
2597 1.1 christos goto success;
2598 1.5 christos
2599 1.5 christos if (hdr->sh_entsize != bed->s->sizeof_sym)
2600 1.3 christos goto fail;
2601 1.3 christos
2602 1.3 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2603 1.5 christos {
2604 1.5 christos if (hdr->sh_size != 0)
2605 1.3 christos goto fail;
2606 1.3 christos
2607 1.3 christos /* Some linkers erroneously set sh_info to one with a
2608 1.3 christos zero sh_size. ld sees this as a global symbol count
2609 1.5 christos of (unsigned) -1. Fix it here. */
2610 1.3 christos hdr->sh_info = 0;
2611 1.5 christos goto success;
2612 1.8 christos }
2613 1.8 christos
2614 1.8 christos /* PR 18854: A binary might contain more than one dynamic symbol table.
2615 1.8 christos Unusual, but possible. Warn, but continue. */
2616 1.9 christos if (elf_dynsymtab (abfd) != 0)
2617 1.9 christos {
2618 1.11 christos _bfd_error_handler
2619 1.9 christos /* xgettext:c-format */
2620 1.8 christos (_("%pB: warning: multiple dynamic symbol tables detected"
2621 1.8 christos " - ignoring the table in section %u"),
2622 1.8 christos abfd, shindex);
2623 1.1 christos goto success;
2624 1.1 christos }
2625 1.1 christos elf_dynsymtab (abfd) = shindex;
2626 1.1 christos elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2627 1.1 christos elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2628 1.1 christos abfd->flags |= HAS_SYMS;
2629 1.1 christos
2630 1.5 christos /* Besides being a symbol table, we also treat this as a regular
2631 1.5 christos section, so that objcopy can handle it. */
2632 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2633 1.14 christos goto success;
2634 1.8 christos
2635 1.8 christos case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2636 1.8 christos {
2637 1.14 christos elf_section_list * entry;
2638 1.8 christos
2639 1.8 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2640 1.11 christos if (entry->ndx == shindex)
2641 1.12 christos goto success;
2642 1.8 christos
2643 1.8 christos entry = bfd_alloc (abfd, sizeof (*entry));
2644 1.8 christos if (entry == NULL)
2645 1.8 christos goto fail;
2646 1.8 christos entry->ndx = shindex;
2647 1.8 christos entry->hdr = * hdr;
2648 1.8 christos entry->next = elf_symtab_shndx_list (abfd);
2649 1.5 christos elf_symtab_shndx_list (abfd) = entry;
2650 1.8 christos elf_elfsections (abfd)[shindex] = & entry->hdr;
2651 1.1 christos goto success;
2652 1.5 christos }
2653 1.1 christos
2654 1.5 christos case SHT_STRTAB: /* A string table. */
2655 1.5 christos if (hdr->bfd_section != NULL)
2656 1.1 christos goto success;
2657 1.1 christos
2658 1.1 christos if (ehdr->e_shstrndx == shindex)
2659 1.1 christos {
2660 1.5 christos elf_tdata (abfd)->shstrtab_hdr = *hdr;
2661 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2662 1.5 christos goto success;
2663 1.1 christos }
2664 1.1 christos
2665 1.1 christos if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2666 1.1 christos {
2667 1.1 christos symtab_strtab:
2668 1.5 christos elf_tdata (abfd)->strtab_hdr = *hdr;
2669 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2670 1.5 christos goto success;
2671 1.1 christos }
2672 1.1 christos
2673 1.1 christos if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2674 1.1 christos {
2675 1.1 christos dynsymtab_strtab:
2676 1.1 christos elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2677 1.1 christos hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2678 1.1 christos elf_elfsections (abfd)[shindex] = hdr;
2679 1.5 christos /* We also treat this as a regular section, so that objcopy
2680 1.5 christos can handle it. */
2681 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2682 1.1 christos shindex);
2683 1.1 christos goto success;
2684 1.1 christos }
2685 1.1 christos
2686 1.1 christos /* If the string table isn't one of the above, then treat it as a
2687 1.1 christos regular section. We need to scan all the headers to be sure,
2688 1.1 christos just in case this strtab section appeared before the above. */
2689 1.1 christos if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2690 1.1 christos {
2691 1.1 christos unsigned int i, num_sec;
2692 1.1 christos
2693 1.1 christos num_sec = elf_numsections (abfd);
2694 1.1 christos for (i = 1; i < num_sec; i++)
2695 1.1 christos {
2696 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2697 1.1 christos if (hdr2->sh_link == shindex)
2698 1.1 christos {
2699 1.5 christos /* Prevent endless recursion on broken objects. */
2700 1.1 christos if (i == shindex)
2701 1.5 christos goto fail;
2702 1.1 christos if (! bfd_section_from_shdr (abfd, i))
2703 1.1 christos goto fail;
2704 1.1 christos if (elf_onesymtab (abfd) == i)
2705 1.1 christos goto symtab_strtab;
2706 1.1 christos if (elf_dynsymtab (abfd) == i)
2707 1.1 christos goto dynsymtab_strtab;
2708 1.1 christos }
2709 1.5 christos }
2710 1.5 christos }
2711 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2712 1.1 christos goto success;
2713 1.1 christos
2714 1.14 christos case SHT_REL:
2715 1.1 christos case SHT_RELA:
2716 1.1 christos case SHT_RELR:
2717 1.1 christos /* *These* do a lot of work -- but build no sections! */
2718 1.1 christos {
2719 1.1 christos asection *target_sect;
2720 1.1 christos Elf_Internal_Shdr *hdr2, **p_hdr;
2721 1.14 christos unsigned int num_sec = elf_numsections (abfd);
2722 1.1 christos struct bfd_elf_section_data *esdt;
2723 1.14 christos bfd_size_type size;
2724 1.14 christos
2725 1.14 christos if (hdr->sh_type == SHT_REL)
2726 1.14 christos size = bed->s->sizeof_rel;
2727 1.14 christos else if (hdr->sh_type == SHT_RELA)
2728 1.14 christos size = bed->s->sizeof_rela;
2729 1.14 christos else
2730 1.5 christos size = bed->s->arch_size / 8;
2731 1.1 christos if (hdr->sh_entsize != size)
2732 1.1 christos goto fail;
2733 1.1 christos
2734 1.1 christos /* Check for a bogus link to avoid crashing. */
2735 1.9 christos if (hdr->sh_link >= num_sec)
2736 1.9 christos {
2737 1.11 christos _bfd_error_handler
2738 1.9 christos /* xgettext:c-format */
2739 1.14 christos (_("%pB: invalid link %u for reloc section %s (index %u)"),
2740 1.5 christos abfd, hdr->sh_link, name, shindex);
2741 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2742 1.1 christos goto success;
2743 1.1 christos }
2744 1.1 christos
2745 1.1 christos /* Get the symbol table. */
2746 1.1 christos if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2747 1.5 christos || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2748 1.1 christos && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2749 1.11 christos goto fail;
2750 1.11 christos
2751 1.11 christos /* If this is an alloc section in an executable or shared
2752 1.11 christos library, or the reloc section does not use the main symbol
2753 1.11 christos table we don't treat it as a reloc section. BFD can't
2754 1.11 christos adequately represent such a section, so at least for now,
2755 1.11 christos we don't try. We just present it as a normal section. We
2756 1.11 christos also can't use it as a reloc section if it points to the
2757 1.11 christos null section, an invalid section, another reloc section, or
2758 1.11 christos its sh_link points to the null section. */
2759 1.17 christos if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2760 1.14 christos && (hdr->sh_flags & SHF_ALLOC) != 0)
2761 1.1 christos || (hdr->sh_flags & SHF_COMPRESSED) != 0
2762 1.11 christos || hdr->sh_type == SHT_RELR
2763 1.1 christos || hdr->sh_link == SHN_UNDEF
2764 1.1 christos || hdr->sh_link != elf_onesymtab (abfd)
2765 1.1 christos || hdr->sh_info == SHN_UNDEF
2766 1.1 christos || hdr->sh_info >= num_sec
2767 1.5 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2768 1.14 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2769 1.5 christos {
2770 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2771 1.1 christos goto success;
2772 1.1 christos }
2773 1.5 christos
2774 1.5 christos if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2775 1.1 christos goto fail;
2776 1.1 christos
2777 1.5 christos target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2778 1.1 christos if (target_sect == NULL)
2779 1.1 christos goto fail;
2780 1.1 christos
2781 1.1 christos esdt = elf_section_data (target_sect);
2782 1.1 christos if (hdr->sh_type == SHT_RELA)
2783 1.1 christos p_hdr = &esdt->rela.hdr;
2784 1.1 christos else
2785 1.12 christos p_hdr = &esdt->rel.hdr;
2786 1.12 christos
2787 1.12 christos /* PR 17512: file: 0b4f81b7.
2788 1.5 christos Also see PR 24456, for a file which deliberately has two reloc
2789 1.12 christos sections. */
2790 1.12 christos if (*p_hdr != NULL)
2791 1.12 christos {
2792 1.12 christos if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2793 1.12 christos {
2794 1.12 christos _bfd_error_handler
2795 1.12 christos /* xgettext:c-format */
2796 1.12 christos (_("%pB: warning: secondary relocation section '%s' "
2797 1.12 christos "for section %pA found - ignoring"),
2798 1.14 christos abfd, name, target_sect);
2799 1.14 christos }
2800 1.12 christos else
2801 1.12 christos esdt->has_secondary_relocs = true;
2802 1.12 christos goto success;
2803 1.8 christos }
2804 1.1 christos
2805 1.5 christos hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2806 1.1 christos if (hdr2 == NULL)
2807 1.1 christos goto fail;
2808 1.1 christos *hdr2 = *hdr;
2809 1.11 christos *p_hdr = hdr2;
2810 1.11 christos elf_elfsections (abfd)[shindex] = hdr2;
2811 1.1 christos target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2812 1.1 christos * bed->s->int_rels_per_ext_rel);
2813 1.1 christos target_sect->flags |= SEC_RELOC;
2814 1.1 christos target_sect->relocation = NULL;
2815 1.1 christos target_sect->rel_filepos = hdr->sh_offset;
2816 1.1 christos /* In the section to which the relocations apply, mark whether
2817 1.1 christos its relocations are of the REL or RELA variety. */
2818 1.1 christos if (hdr->sh_size != 0)
2819 1.1 christos {
2820 1.1 christos if (hdr->sh_type == SHT_RELA)
2821 1.1 christos target_sect->use_rela_p = 1;
2822 1.5 christos }
2823 1.1 christos abfd->flags |= HAS_RELOC;
2824 1.1 christos goto success;
2825 1.1 christos }
2826 1.14 christos
2827 1.14 christos case SHT_GNU_verdef:
2828 1.1 christos if (hdr->sh_info != 0)
2829 1.5 christos elf_dynverdef (abfd) = shindex;
2830 1.5 christos elf_tdata (abfd)->dynverdef_hdr = *hdr;
2831 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2832 1.1 christos goto success;
2833 1.1 christos
2834 1.5 christos case SHT_GNU_versym:
2835 1.5 christos if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2836 1.1 christos goto fail;
2837 1.1 christos
2838 1.5 christos elf_dynversym (abfd) = shindex;
2839 1.5 christos elf_tdata (abfd)->dynversym_hdr = *hdr;
2840 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2841 1.1 christos goto success;
2842 1.14 christos
2843 1.14 christos case SHT_GNU_verneed:
2844 1.1 christos if (hdr->sh_info != 0)
2845 1.5 christos elf_dynverref (abfd) = shindex;
2846 1.5 christos elf_tdata (abfd)->dynverref_hdr = *hdr;
2847 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2848 1.1 christos goto success;
2849 1.5 christos
2850 1.1 christos case SHT_SHLIB:
2851 1.1 christos goto success;
2852 1.1 christos
2853 1.5 christos case SHT_GROUP:
2854 1.5 christos if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2855 1.5 christos goto fail;
2856 1.1 christos
2857 1.1 christos goto success;
2858 1.1 christos
2859 1.1 christos default:
2860 1.1 christos /* Possibly an attributes section. */
2861 1.1 christos if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2862 1.1 christos || hdr->sh_type == bed->obj_attrs_section_type)
2863 1.5 christos {
2864 1.1 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2865 1.5 christos goto fail;
2866 1.1 christos _bfd_elf_parse_attributes (abfd, hdr);
2867 1.1 christos goto success;
2868 1.1 christos }
2869 1.1 christos
2870 1.5 christos /* Check for any processor-specific section types. */
2871 1.1 christos if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2872 1.1 christos goto success;
2873 1.1 christos
2874 1.1 christos if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2875 1.1 christos {
2876 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0)
2877 1.9 christos /* FIXME: How to properly handle allocated section reserved
2878 1.9 christos for applications? */
2879 1.11 christos _bfd_error_handler
2880 1.11 christos /* xgettext:c-format */
2881 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2882 1.5 christos abfd, hdr->sh_type, name);
2883 1.5 christos else
2884 1.14 christos {
2885 1.5 christos /* Allow sections reserved for applications. */
2886 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2887 1.1 christos goto success;
2888 1.1 christos }
2889 1.1 christos }
2890 1.1 christos else if (hdr->sh_type >= SHT_LOPROC
2891 1.9 christos && hdr->sh_type <= SHT_HIPROC)
2892 1.9 christos /* FIXME: We should handle this section. */
2893 1.11 christos _bfd_error_handler
2894 1.11 christos /* xgettext:c-format */
2895 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2896 1.1 christos abfd, hdr->sh_type, name);
2897 1.1 christos else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2898 1.1 christos {
2899 1.1 christos /* Unrecognised OS-specific sections. */
2900 1.1 christos if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2901 1.1 christos /* SHF_OS_NONCONFORMING indicates that special knowledge is
2902 1.9 christos required to correctly process the section and the file should
2903 1.9 christos be rejected with an error message. */
2904 1.11 christos _bfd_error_handler
2905 1.11 christos /* xgettext:c-format */
2906 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2907 1.5 christos abfd, hdr->sh_type, name);
2908 1.5 christos else
2909 1.5 christos {
2910 1.5 christos /* Otherwise it should be processed. */
2911 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2912 1.1 christos goto success;
2913 1.1 christos }
2914 1.1 christos }
2915 1.9 christos else
2916 1.9 christos /* FIXME: We should handle this section. */
2917 1.11 christos _bfd_error_handler
2918 1.11 christos /* xgettext:c-format */
2919 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2920 1.5 christos abfd, hdr->sh_type, name);
2921 1.1 christos
2922 1.1 christos goto fail;
2923 1.5 christos }
2924 1.14 christos
2925 1.5 christos fail:
2926 1.14 christos ret = false;
2927 1.5 christos success:
2928 1.1 christos elf_tdata (abfd)->being_created[shindex] = false;
2929 1.1 christos return ret;
2930 1.1 christos }
2931 1.1 christos
2932 1.1 christos /* Return the local symbol specified by ABFD, R_SYMNDX. */
2933 1.1 christos
2934 1.1 christos Elf_Internal_Sym *
2935 1.1 christos bfd_sym_from_r_symndx (struct sym_cache *cache,
2936 1.1 christos bfd *abfd,
2937 1.1 christos unsigned long r_symndx)
2938 1.1 christos {
2939 1.1 christos unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2940 1.1 christos
2941 1.1 christos if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2942 1.1 christos {
2943 1.1 christos Elf_Internal_Shdr *symtab_hdr;
2944 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)];
2945 1.1 christos Elf_External_Sym_Shndx eshndx;
2946 1.1 christos
2947 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2948 1.1 christos if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2949 1.1 christos &cache->sym[ent], esym, &eshndx) == NULL)
2950 1.1 christos return NULL;
2951 1.1 christos
2952 1.1 christos if (cache->abfd != abfd)
2953 1.1 christos {
2954 1.1 christos memset (cache->indx, -1, sizeof (cache->indx));
2955 1.1 christos cache->abfd = abfd;
2956 1.1 christos }
2957 1.1 christos cache->indx[ent] = r_symndx;
2958 1.1 christos }
2959 1.1 christos
2960 1.1 christos return &cache->sym[ent];
2961 1.1 christos }
2962 1.1 christos
2963 1.1 christos /* Given an ELF section number, retrieve the corresponding BFD
2964 1.1 christos section. */
2965 1.1 christos
2966 1.1 christos asection *
2967 1.1 christos bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2968 1.1 christos {
2969 1.1 christos if (sec_index >= elf_numsections (abfd))
2970 1.1 christos return NULL;
2971 1.1 christos return elf_elfsections (abfd)[sec_index]->bfd_section;
2972 1.1 christos }
2973 1.1 christos
2974 1.1 christos static const struct bfd_elf_special_section special_sections_b[] =
2975 1.11 christos {
2976 1.1 christos { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2977 1.1 christos { NULL, 0, 0, 0, 0 }
2978 1.1 christos };
2979 1.1 christos
2980 1.1 christos static const struct bfd_elf_special_section special_sections_c[] =
2981 1.12 christos {
2982 1.11 christos { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2983 1.1 christos { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2984 1.1 christos { NULL, 0, 0, 0, 0 }
2985 1.1 christos };
2986 1.1 christos
2987 1.11 christos static const struct bfd_elf_special_section special_sections_d[] =
2988 1.11 christos {
2989 1.3 christos { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2990 1.3 christos { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2991 1.3 christos /* There are more DWARF sections than these, but they needn't be added here
2992 1.11 christos unless you have to cope with broken compilers that don't emit section
2993 1.11 christos attributes or you want to help the user writing assembler. */
2994 1.11 christos { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2995 1.11 christos { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2996 1.1 christos { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2997 1.11 christos { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2998 1.11 christos { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2999 1.11 christos { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3000 1.11 christos { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3001 1.1 christos { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3002 1.1 christos { NULL, 0, 0, 0, 0 }
3003 1.1 christos };
3004 1.1 christos
3005 1.11 christos static const struct bfd_elf_special_section special_sections_f[] =
3006 1.9 christos {
3007 1.11 christos { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3008 1.1 christos { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3009 1.1 christos { NULL, 0 , 0, 0, 0 }
3010 1.1 christos };
3011 1.1 christos
3012 1.1 christos static const struct bfd_elf_special_section special_sections_g[] =
3013 1.14 christos {
3014 1.14 christos { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3015 1.11 christos { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3016 1.11 christos { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3017 1.11 christos { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3018 1.1 christos { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3019 1.1 christos { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3020 1.11 christos { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3021 1.11 christos { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3022 1.11 christos { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3023 1.11 christos { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3024 1.1 christos { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3025 1.1 christos { NULL, 0, 0, 0, 0 }
3026 1.1 christos };
3027 1.1 christos
3028 1.11 christos static const struct bfd_elf_special_section special_sections_h[] =
3029 1.11 christos {
3030 1.1 christos { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3031 1.1 christos { NULL, 0, 0, 0, 0 }
3032 1.1 christos };
3033 1.1 christos
3034 1.11 christos static const struct bfd_elf_special_section special_sections_i[] =
3035 1.9 christos {
3036 1.11 christos { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3037 1.11 christos { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3038 1.1 christos { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3039 1.1 christos { NULL, 0, 0, 0, 0 }
3040 1.1 christos };
3041 1.1 christos
3042 1.1 christos static const struct bfd_elf_special_section special_sections_l[] =
3043 1.11 christos {
3044 1.1 christos { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3045 1.1 christos { NULL, 0, 0, 0, 0 }
3046 1.1 christos };
3047 1.1 christos
3048 1.14 christos static const struct bfd_elf_special_section special_sections_n[] =
3049 1.1 christos {
3050 1.11 christos { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3051 1.11 christos { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3052 1.1 christos { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3053 1.1 christos { NULL, 0, 0, 0, 0 }
3054 1.1 christos };
3055 1.1 christos
3056 1.14 christos static const struct bfd_elf_special_section special_sections_p[] =
3057 1.14 christos {
3058 1.9 christos { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3059 1.11 christos { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3060 1.11 christos { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3061 1.1 christos { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3062 1.1 christos { NULL, 0, 0, 0, 0 }
3063 1.1 christos };
3064 1.1 christos
3065 1.1 christos static const struct bfd_elf_special_section special_sections_r[] =
3066 1.1 christos {
3067 1.14 christos { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3068 1.11 christos { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3069 1.18 christos { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3070 1.18 christos { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3071 1.18 christos /* .relro_padding is generated by lld. It should not be confused with a
3072 1.18 christos reloc containing section, because otherwise elf_fake_sections() will
3073 1.18 christos set the entsize to 8, which may not be an actual multiple of the
3074 1.18 christos section's size.
3075 1.11 christos Note - this entry must appear before the ".rel" entry below. */
3076 1.11 christos { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3077 1.1 christos { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3078 1.1 christos { NULL, 0, 0, 0, 0 }
3079 1.1 christos };
3080 1.1 christos
3081 1.1 christos static const struct bfd_elf_special_section special_sections_s[] =
3082 1.1 christos {
3083 1.1 christos { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3084 1.1 christos { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3085 1.1 christos { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3086 1.1 christos /* See struct bfd_elf_special_section declaration for the semantics of
3087 1.11 christos this special case where .prefix_length != strlen (.prefix). */
3088 1.1 christos { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3089 1.1 christos { NULL, 0, 0, 0, 0 }
3090 1.1 christos };
3091 1.1 christos
3092 1.11 christos static const struct bfd_elf_special_section special_sections_t[] =
3093 1.11 christos {
3094 1.1 christos { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3095 1.11 christos { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3096 1.1 christos { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3097 1.1 christos { NULL, 0, 0, 0, 0 }
3098 1.1 christos };
3099 1.1 christos
3100 1.11 christos static const struct bfd_elf_special_section special_sections_z[] =
3101 1.11 christos {
3102 1.1 christos { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3103 1.1 christos { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3104 1.11 christos { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3105 1.1 christos { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3106 1.1 christos { NULL, 0, 0, 0, 0 }
3107 1.3 christos };
3108 1.1 christos
3109 1.1 christos static const struct bfd_elf_special_section * const special_sections[] =
3110 1.1 christos {
3111 1.1 christos special_sections_b, /* 'b' */
3112 1.1 christos special_sections_c, /* 'c' */
3113 1.1 christos special_sections_d, /* 'd' */
3114 1.1 christos NULL, /* 'e' */
3115 1.1 christos special_sections_f, /* 'f' */
3116 1.1 christos special_sections_g, /* 'g' */
3117 1.1 christos special_sections_h, /* 'h' */
3118 1.1 christos special_sections_i, /* 'i' */
3119 1.1 christos NULL, /* 'j' */
3120 1.1 christos NULL, /* 'k' */
3121 1.1 christos special_sections_l, /* 'l' */
3122 1.1 christos NULL, /* 'm' */
3123 1.1 christos special_sections_n, /* 'n' */
3124 1.1 christos NULL, /* 'o' */
3125 1.1 christos special_sections_p, /* 'p' */
3126 1.1 christos NULL, /* 'q' */
3127 1.1 christos special_sections_r, /* 'r' */
3128 1.1 christos special_sections_s, /* 's' */
3129 1.1 christos special_sections_t, /* 't' */
3130 1.1 christos NULL, /* 'u' */
3131 1.1 christos NULL, /* 'v' */
3132 1.1 christos NULL, /* 'w' */
3133 1.1 christos NULL, /* 'x' */
3134 1.1 christos NULL, /* 'y' */
3135 1.1 christos special_sections_z /* 'z' */
3136 1.1 christos };
3137 1.1 christos
3138 1.1 christos const struct bfd_elf_special_section *
3139 1.1 christos _bfd_elf_get_special_section (const char *name,
3140 1.1 christos const struct bfd_elf_special_section *spec,
3141 1.1 christos unsigned int rela)
3142 1.1 christos {
3143 1.1 christos int i;
3144 1.1 christos int len;
3145 1.1 christos
3146 1.1 christos len = strlen (name);
3147 1.1 christos
3148 1.1 christos for (i = 0; spec[i].prefix != NULL; i++)
3149 1.1 christos {
3150 1.1 christos int suffix_len;
3151 1.1 christos int prefix_len = spec[i].prefix_length;
3152 1.1 christos
3153 1.1 christos if (len < prefix_len)
3154 1.1 christos continue;
3155 1.1 christos if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3156 1.1 christos continue;
3157 1.1 christos
3158 1.1 christos suffix_len = spec[i].suffix_length;
3159 1.1 christos if (suffix_len <= 0)
3160 1.1 christos {
3161 1.1 christos if (name[prefix_len] != 0)
3162 1.1 christos {
3163 1.1 christos if (suffix_len == 0)
3164 1.1 christos continue;
3165 1.1 christos if (name[prefix_len] != '.'
3166 1.1 christos && (suffix_len == -2
3167 1.1 christos || (rela && spec[i].type == SHT_REL)))
3168 1.1 christos continue;
3169 1.1 christos }
3170 1.1 christos }
3171 1.1 christos else
3172 1.1 christos {
3173 1.1 christos if (len < prefix_len + suffix_len)
3174 1.1 christos continue;
3175 1.1 christos if (memcmp (name + len - suffix_len,
3176 1.1 christos spec[i].prefix + prefix_len,
3177 1.1 christos suffix_len) != 0)
3178 1.1 christos continue;
3179 1.1 christos }
3180 1.1 christos return &spec[i];
3181 1.1 christos }
3182 1.1 christos
3183 1.1 christos return NULL;
3184 1.1 christos }
3185 1.1 christos
3186 1.1 christos const struct bfd_elf_special_section *
3187 1.1 christos _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3188 1.1 christos {
3189 1.1 christos int i;
3190 1.1 christos const struct bfd_elf_special_section *spec;
3191 1.1 christos const struct elf_backend_data *bed;
3192 1.1 christos
3193 1.1 christos /* See if this is one of the special sections. */
3194 1.1 christos if (sec->name == NULL)
3195 1.1 christos return NULL;
3196 1.1 christos
3197 1.1 christos bed = get_elf_backend_data (abfd);
3198 1.1 christos spec = bed->special_sections;
3199 1.1 christos if (spec)
3200 1.1 christos {
3201 1.1 christos spec = _bfd_elf_get_special_section (sec->name,
3202 1.1 christos bed->special_sections,
3203 1.1 christos sec->use_rela_p);
3204 1.1 christos if (spec != NULL)
3205 1.1 christos return spec;
3206 1.1 christos }
3207 1.1 christos
3208 1.1 christos if (sec->name[0] != '.')
3209 1.1 christos return NULL;
3210 1.1 christos
3211 1.1 christos i = sec->name[1] - 'b';
3212 1.1 christos if (i < 0 || i > 'z' - 'b')
3213 1.1 christos return NULL;
3214 1.1 christos
3215 1.1 christos spec = special_sections[i];
3216 1.1 christos
3217 1.1 christos if (spec == NULL)
3218 1.1 christos return NULL;
3219 1.1 christos
3220 1.1 christos return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3221 1.14 christos }
3222 1.1 christos
3223 1.1 christos bool
3224 1.1 christos _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3225 1.1 christos {
3226 1.1 christos struct bfd_elf_section_data *sdata;
3227 1.1 christos const struct elf_backend_data *bed;
3228 1.1 christos const struct bfd_elf_special_section *ssect;
3229 1.1 christos
3230 1.1 christos sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3231 1.1 christos if (sdata == NULL)
3232 1.11 christos {
3233 1.1 christos sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3234 1.14 christos sizeof (*sdata));
3235 1.1 christos if (sdata == NULL)
3236 1.1 christos return false;
3237 1.1 christos sec->used_by_bfd = sdata;
3238 1.1 christos }
3239 1.1 christos
3240 1.1 christos /* Indicate whether or not this section should use RELA relocations. */
3241 1.1 christos bed = get_elf_backend_data (abfd);
3242 1.12 christos sec->use_rela_p = bed->default_use_rela_p;
3243 1.12 christos
3244 1.12 christos /* Set up ELF section type and flags for newly created sections, if
3245 1.12 christos there is an ABI mandated section. */
3246 1.12 christos ssect = (*bed->get_sec_type_attr) (abfd, sec);
3247 1.12 christos if (ssect != NULL)
3248 1.12 christos {
3249 1.1 christos elf_section_type (sec) = ssect->type;
3250 1.1 christos elf_section_flags (sec) = ssect->attr;
3251 1.1 christos }
3252 1.1 christos
3253 1.1 christos return _bfd_generic_new_section_hook (abfd, sec);
3254 1.1 christos }
3255 1.1 christos
3256 1.1 christos /* Create a new bfd section from an ELF program header.
3257 1.1 christos
3258 1.1 christos Since program segments have no names, we generate a synthetic name
3259 1.1 christos of the form segment<NUM>, where NUM is generally the index in the
3260 1.1 christos program header table. For segments that are split (see below) we
3261 1.1 christos generate the names segment<NUM>a and segment<NUM>b.
3262 1.1 christos
3263 1.1 christos Note that some program segments may have a file size that is different than
3264 1.1 christos (less than) the memory size. All this means is that at execution the
3265 1.1 christos system must allocate the amount of memory specified by the memory size,
3266 1.1 christos but only initialize it with the first "file size" bytes read from the
3267 1.1 christos file. This would occur for example, with program segments consisting
3268 1.1 christos of combined data+bss.
3269 1.1 christos
3270 1.1 christos To handle the above situation, this routine generates TWO bfd sections
3271 1.1 christos for the single program segment. The first has the length specified by
3272 1.14 christos the file size of the segment, and the second has the length specified
3273 1.1 christos by the difference between the two sizes. In effect, the segment is split
3274 1.14 christos into its initialized and uninitialized parts. */
3275 1.1 christos
3276 1.1 christos bool
3277 1.1 christos _bfd_elf_make_section_from_phdr (bfd *abfd,
3278 1.1 christos Elf_Internal_Phdr *hdr,
3279 1.1 christos int hdr_index,
3280 1.1 christos const char *type_name)
3281 1.1 christos {
3282 1.1 christos asection *newsect;
3283 1.1 christos char *name;
3284 1.1 christos char namebuf[64];
3285 1.12 christos size_t len;
3286 1.1 christos int split;
3287 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3288 1.1 christos
3289 1.1 christos split = ((hdr->p_memsz > 0)
3290 1.1 christos && (hdr->p_filesz > 0)
3291 1.1 christos && (hdr->p_memsz > hdr->p_filesz));
3292 1.1 christos
3293 1.1 christos if (hdr->p_filesz > 0)
3294 1.1 christos {
3295 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3296 1.1 christos len = strlen (namebuf) + 1;
3297 1.14 christos name = (char *) bfd_alloc (abfd, len);
3298 1.1 christos if (!name)
3299 1.1 christos return false;
3300 1.1 christos memcpy (name, namebuf, len);
3301 1.14 christos newsect = bfd_make_section (abfd, name);
3302 1.12 christos if (newsect == NULL)
3303 1.12 christos return false;
3304 1.1 christos newsect->vma = hdr->p_vaddr / opb;
3305 1.1 christos newsect->lma = hdr->p_paddr / opb;
3306 1.1 christos newsect->size = hdr->p_filesz;
3307 1.1 christos newsect->filepos = hdr->p_offset;
3308 1.1 christos newsect->flags |= SEC_HAS_CONTENTS;
3309 1.1 christos newsect->alignment_power = bfd_log2 (hdr->p_align);
3310 1.1 christos if (hdr->p_type == PT_LOAD)
3311 1.1 christos {
3312 1.1 christos newsect->flags |= SEC_ALLOC;
3313 1.1 christos newsect->flags |= SEC_LOAD;
3314 1.1 christos if (hdr->p_flags & PF_X)
3315 1.1 christos {
3316 1.1 christos /* FIXME: all we known is that it has execute PERMISSION,
3317 1.1 christos may be data. */
3318 1.1 christos newsect->flags |= SEC_CODE;
3319 1.1 christos }
3320 1.1 christos }
3321 1.1 christos if (!(hdr->p_flags & PF_W))
3322 1.1 christos {
3323 1.1 christos newsect->flags |= SEC_READONLY;
3324 1.1 christos }
3325 1.1 christos }
3326 1.1 christos
3327 1.1 christos if (hdr->p_memsz > hdr->p_filesz)
3328 1.1 christos {
3329 1.1 christos bfd_vma align;
3330 1.1 christos
3331 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3332 1.1 christos len = strlen (namebuf) + 1;
3333 1.14 christos name = (char *) bfd_alloc (abfd, len);
3334 1.1 christos if (!name)
3335 1.1 christos return false;
3336 1.1 christos memcpy (name, namebuf, len);
3337 1.14 christos newsect = bfd_make_section (abfd, name);
3338 1.12 christos if (newsect == NULL)
3339 1.12 christos return false;
3340 1.1 christos newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3341 1.1 christos newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3342 1.1 christos newsect->size = hdr->p_memsz - hdr->p_filesz;
3343 1.1 christos newsect->filepos = hdr->p_offset + hdr->p_filesz;
3344 1.1 christos align = newsect->vma & -newsect->vma;
3345 1.1 christos if (align == 0 || align > hdr->p_align)
3346 1.1 christos align = hdr->p_align;
3347 1.1 christos newsect->alignment_power = bfd_log2 (align);
3348 1.1 christos if (hdr->p_type == PT_LOAD)
3349 1.1 christos {
3350 1.1 christos newsect->flags |= SEC_ALLOC;
3351 1.1 christos if (hdr->p_flags & PF_X)
3352 1.1 christos newsect->flags |= SEC_CODE;
3353 1.1 christos }
3354 1.1 christos if (!(hdr->p_flags & PF_W))
3355 1.1 christos newsect->flags |= SEC_READONLY;
3356 1.14 christos }
3357 1.1 christos
3358 1.1 christos return true;
3359 1.14 christos }
3360 1.12 christos
3361 1.12 christos static bool
3362 1.12 christos _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3363 1.12 christos {
3364 1.12 christos /* The return value is ignored. Build-ids are considered optional. */
3365 1.12 christos if (templ->xvec->flavour == bfd_target_elf_flavour)
3366 1.14 christos return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3367 1.12 christos (templ, offset);
3368 1.12 christos return false;
3369 1.14 christos }
3370 1.1 christos
3371 1.1 christos bool
3372 1.1 christos bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3373 1.1 christos {
3374 1.1 christos const struct elf_backend_data *bed;
3375 1.1 christos
3376 1.1 christos switch (hdr->p_type)
3377 1.1 christos {
3378 1.1 christos case PT_NULL:
3379 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3380 1.12 christos
3381 1.14 christos case PT_LOAD:
3382 1.12 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3383 1.12 christos return false;
3384 1.14 christos if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3385 1.1 christos _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3386 1.1 christos return true;
3387 1.1 christos
3388 1.1 christos case PT_DYNAMIC:
3389 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3390 1.1 christos
3391 1.1 christos case PT_INTERP:
3392 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3393 1.1 christos
3394 1.14 christos case PT_NOTE:
3395 1.11 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3396 1.11 christos return false;
3397 1.14 christos if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3398 1.14 christos hdr->p_align))
3399 1.1 christos return false;
3400 1.1 christos return true;
3401 1.1 christos
3402 1.1 christos case PT_SHLIB:
3403 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3404 1.1 christos
3405 1.1 christos case PT_PHDR:
3406 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3407 1.1 christos
3408 1.1 christos case PT_GNU_EH_FRAME:
3409 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3410 1.1 christos "eh_frame_hdr");
3411 1.1 christos
3412 1.1 christos case PT_GNU_STACK:
3413 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3414 1.1 christos
3415 1.1 christos case PT_GNU_RELRO:
3416 1.14 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3417 1.14 christos
3418 1.14 christos case PT_GNU_SFRAME:
3419 1.14 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3420 1.1 christos "sframe");
3421 1.1 christos
3422 1.1 christos default:
3423 1.1 christos /* Check for any processor-specific program segment types. */
3424 1.1 christos bed = get_elf_backend_data (abfd);
3425 1.1 christos return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3426 1.1 christos }
3427 1.1 christos }
3428 1.1 christos
3429 1.1 christos /* Return the REL_HDR for SEC, assuming there is only a single one, either
3430 1.1 christos REL or RELA. */
3431 1.1 christos
3432 1.1 christos Elf_Internal_Shdr *
3433 1.1 christos _bfd_elf_single_rel_hdr (asection *sec)
3434 1.1 christos {
3435 1.1 christos if (elf_section_data (sec)->rel.hdr)
3436 1.1 christos {
3437 1.1 christos BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3438 1.1 christos return elf_section_data (sec)->rel.hdr;
3439 1.1 christos }
3440 1.1 christos else
3441 1.1 christos return elf_section_data (sec)->rela.hdr;
3442 1.14 christos }
3443 1.6 christos
3444 1.6 christos static bool
3445 1.6 christos _bfd_elf_set_reloc_sh_name (bfd *abfd,
3446 1.14 christos Elf_Internal_Shdr *rel_hdr,
3447 1.6 christos const char *sec_name,
3448 1.6 christos bool use_rela_p)
3449 1.6 christos {
3450 1.6 christos char *name = (char *) bfd_alloc (abfd,
3451 1.14 christos sizeof ".rela" + strlen (sec_name));
3452 1.6 christos if (name == NULL)
3453 1.6 christos return false;
3454 1.6 christos
3455 1.6 christos sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3456 1.14 christos rel_hdr->sh_name =
3457 1.6 christos (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3458 1.14 christos false);
3459 1.6 christos if (rel_hdr->sh_name == (unsigned int) -1)
3460 1.14 christos return false;
3461 1.6 christos
3462 1.6 christos return true;
3463 1.1 christos }
3464 1.1 christos
3465 1.1 christos /* Allocate and initialize a section-header for a new reloc section,
3466 1.1 christos containing relocations against ASECT. It is stored in RELDATA. If
3467 1.1 christos USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3468 1.14 christos relocations. */
3469 1.1 christos
3470 1.1 christos static bool
3471 1.6 christos _bfd_elf_init_reloc_shdr (bfd *abfd,
3472 1.14 christos struct bfd_elf_section_reloc_data *reldata,
3473 1.14 christos const char *sec_name,
3474 1.1 christos bool use_rela_p,
3475 1.1 christos bool delay_st_name_p)
3476 1.1 christos {
3477 1.1 christos Elf_Internal_Shdr *rel_hdr;
3478 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3479 1.8 christos
3480 1.17 christos BFD_ASSERT (reldata->hdr == NULL);
3481 1.17 christos rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3482 1.1 christos if (rel_hdr == NULL)
3483 1.1 christos return false;
3484 1.6 christos reldata->hdr = rel_hdr;
3485 1.6 christos
3486 1.6 christos if (delay_st_name_p)
3487 1.6 christos rel_hdr->sh_name = (unsigned int) -1;
3488 1.14 christos else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3489 1.1 christos use_rela_p))
3490 1.1 christos return false;
3491 1.1 christos rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3492 1.1 christos rel_hdr->sh_entsize = (use_rela_p
3493 1.1 christos ? bed->s->sizeof_rela
3494 1.1 christos : bed->s->sizeof_rel);
3495 1.1 christos rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3496 1.1 christos rel_hdr->sh_flags = 0;
3497 1.1 christos rel_hdr->sh_addr = 0;
3498 1.1 christos rel_hdr->sh_size = 0;
3499 1.14 christos rel_hdr->sh_offset = 0;
3500 1.1 christos
3501 1.1 christos return true;
3502 1.1 christos }
3503 1.1 christos
3504 1.1 christos /* Return the default section type based on the passed in section flags. */
3505 1.1 christos
3506 1.1 christos int
3507 1.11 christos bfd_elf_get_default_section_type (flagword flags)
3508 1.1 christos {
3509 1.1 christos if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3510 1.1 christos && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3511 1.1 christos return SHT_NOBITS;
3512 1.1 christos return SHT_PROGBITS;
3513 1.1 christos }
3514 1.1 christos
3515 1.1 christos struct fake_section_arg
3516 1.14 christos {
3517 1.1 christos struct bfd_link_info *link_info;
3518 1.1 christos bool failed;
3519 1.1 christos };
3520 1.1 christos
3521 1.1 christos /* Set up an ELF internal section header for a section. */
3522 1.1 christos
3523 1.1 christos static void
3524 1.1 christos elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3525 1.1 christos {
3526 1.1 christos struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3527 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3528 1.1 christos struct bfd_elf_section_data *esd = elf_section_data (asect);
3529 1.6 christos Elf_Internal_Shdr *this_hdr;
3530 1.14 christos unsigned int sh_type;
3531 1.12 christos const char *name = asect->name;
3532 1.1 christos bool delay_st_name_p = false;
3533 1.1 christos bfd_vma mask;
3534 1.1 christos
3535 1.1 christos if (arg->failed)
3536 1.1 christos {
3537 1.1 christos /* We already failed; just get out of the bfd_map_over_sections
3538 1.1 christos loop. */
3539 1.1 christos return;
3540 1.1 christos }
3541 1.1 christos
3542 1.14 christos this_hdr = &esd->this_hdr;
3543 1.14 christos
3544 1.14 christos /* ld: compress DWARF debug sections with names: .debug_*. */
3545 1.14 christos if (arg->link_info
3546 1.14 christos && (abfd->flags & BFD_COMPRESS) != 0
3547 1.14 christos && (asect->flags & SEC_DEBUGGING) != 0
3548 1.14 christos && name[1] == 'd'
3549 1.14 christos && name[6] == '_')
3550 1.14 christos {
3551 1.14 christos /* If this section will be compressed, delay adding section
3552 1.14 christos name to section name section after it is compressed in
3553 1.6 christos _bfd_elf_assign_file_positions_for_non_load. */
3554 1.6 christos delay_st_name_p = true;
3555 1.6 christos }
3556 1.6 christos
3557 1.6 christos if (delay_st_name_p)
3558 1.1 christos this_hdr->sh_name = (unsigned int) -1;
3559 1.6 christos else
3560 1.6 christos {
3561 1.14 christos this_hdr->sh_name
3562 1.6 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3563 1.6 christos name, false);
3564 1.14 christos if (this_hdr->sh_name == (unsigned int) -1)
3565 1.6 christos {
3566 1.6 christos arg->failed = true;
3567 1.1 christos return;
3568 1.1 christos }
3569 1.1 christos }
3570 1.1 christos
3571 1.1 christos /* Don't clear sh_flags. Assembler may set additional bits. */
3572 1.1 christos
3573 1.12 christos if ((asect->flags & SEC_ALLOC) != 0
3574 1.1 christos || asect->user_set_vma)
3575 1.1 christos this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3576 1.1 christos else
3577 1.1 christos this_hdr->sh_addr = 0;
3578 1.1 christos
3579 1.1 christos this_hdr->sh_offset = 0;
3580 1.6 christos this_hdr->sh_size = asect->size;
3581 1.6 christos this_hdr->sh_link = 0;
3582 1.6 christos /* PR 17512: file: 0eb809fe, 8b0535ee. */
3583 1.9 christos if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3584 1.9 christos {
3585 1.11 christos _bfd_error_handler
3586 1.9 christos /* xgettext:c-format */
3587 1.14 christos (_("%pB: error: alignment power %d of section `%pA' is too big"),
3588 1.6 christos abfd, asect->alignment_power, asect);
3589 1.6 christos arg->failed = true;
3590 1.12 christos return;
3591 1.12 christos }
3592 1.12 christos /* Set sh_addralign to the highest power of two given by alignment
3593 1.12 christos consistent with the section VMA. Linker scripts can force VMA. */
3594 1.1 christos mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3595 1.1 christos this_hdr->sh_addralign = mask & -mask;
3596 1.1 christos /* The sh_entsize and sh_info fields may have been set already by
3597 1.1 christos copy_private_section_data. */
3598 1.1 christos
3599 1.1 christos this_hdr->bfd_section = asect;
3600 1.1 christos this_hdr->contents = NULL;
3601 1.1 christos
3602 1.14 christos /* If the section type is unspecified, we set it based on
3603 1.14 christos asect->flags. */
3604 1.14 christos if (asect->type != 0)
3605 1.1 christos sh_type = asect->type;
3606 1.1 christos else if ((asect->flags & SEC_GROUP) != 0)
3607 1.1 christos sh_type = SHT_GROUP;
3608 1.1 christos else
3609 1.1 christos sh_type = bfd_elf_get_default_section_type (asect->flags);
3610 1.1 christos
3611 1.1 christos if (this_hdr->sh_type == SHT_NULL)
3612 1.1 christos this_hdr->sh_type = sh_type;
3613 1.1 christos else if (this_hdr->sh_type == SHT_NOBITS
3614 1.1 christos && sh_type == SHT_PROGBITS
3615 1.1 christos && (asect->flags & SEC_ALLOC) != 0)
3616 1.1 christos {
3617 1.1 christos /* Warn if we are changing a NOBITS section to PROGBITS, but
3618 1.1 christos allow the link to proceed. This can happen when users link
3619 1.9 christos non-bss input sections to bss output sections, or emit data
3620 1.11 christos to a bss output section via a linker script. */
3621 1.1 christos _bfd_error_handler
3622 1.1 christos (_("warning: section `%pA' type changed to PROGBITS"), asect);
3623 1.1 christos this_hdr->sh_type = sh_type;
3624 1.1 christos }
3625 1.1 christos
3626 1.1 christos switch (this_hdr->sh_type)
3627 1.1 christos {
3628 1.1 christos default:
3629 1.1 christos break;
3630 1.8 christos
3631 1.8 christos case SHT_STRTAB:
3632 1.8 christos case SHT_NOTE:
3633 1.8 christos case SHT_NOBITS:
3634 1.8 christos case SHT_PROGBITS:
3635 1.1 christos break;
3636 1.1 christos
3637 1.1 christos case SHT_INIT_ARRAY:
3638 1.8 christos case SHT_FINI_ARRAY:
3639 1.1 christos case SHT_PREINIT_ARRAY:
3640 1.1 christos this_hdr->sh_entsize = bed->s->arch_size / 8;
3641 1.1 christos break;
3642 1.1 christos
3643 1.1 christos case SHT_HASH:
3644 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3645 1.1 christos break;
3646 1.1 christos
3647 1.1 christos case SHT_DYNSYM:
3648 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_sym;
3649 1.1 christos break;
3650 1.1 christos
3651 1.1 christos case SHT_DYNAMIC:
3652 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_dyn;
3653 1.1 christos break;
3654 1.1 christos
3655 1.1 christos case SHT_RELA:
3656 1.1 christos if (get_elf_backend_data (abfd)->may_use_rela_p)
3657 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rela;
3658 1.1 christos break;
3659 1.1 christos
3660 1.1 christos case SHT_REL:
3661 1.1 christos if (get_elf_backend_data (abfd)->may_use_rel_p)
3662 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rel;
3663 1.1 christos break;
3664 1.1 christos
3665 1.1 christos case SHT_GNU_versym:
3666 1.1 christos this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3667 1.1 christos break;
3668 1.1 christos
3669 1.1 christos case SHT_GNU_verdef:
3670 1.1 christos this_hdr->sh_entsize = 0;
3671 1.1 christos /* objcopy or strip will copy over sh_info, but may not set
3672 1.1 christos cverdefs. The linker will set cverdefs, but sh_info will be
3673 1.1 christos zero. */
3674 1.1 christos if (this_hdr->sh_info == 0)
3675 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3676 1.1 christos else
3677 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3678 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3679 1.1 christos break;
3680 1.1 christos
3681 1.1 christos case SHT_GNU_verneed:
3682 1.1 christos this_hdr->sh_entsize = 0;
3683 1.1 christos /* objcopy or strip will copy over sh_info, but may not set
3684 1.1 christos cverrefs. The linker will set cverrefs, but sh_info will be
3685 1.1 christos zero. */
3686 1.1 christos if (this_hdr->sh_info == 0)
3687 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3688 1.1 christos else
3689 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3690 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3691 1.1 christos break;
3692 1.1 christos
3693 1.1 christos case SHT_GROUP:
3694 1.1 christos this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3695 1.1 christos break;
3696 1.1 christos
3697 1.1 christos case SHT_GNU_HASH:
3698 1.1 christos this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3699 1.1 christos break;
3700 1.1 christos }
3701 1.1 christos
3702 1.1 christos if ((asect->flags & SEC_ALLOC) != 0)
3703 1.1 christos this_hdr->sh_flags |= SHF_ALLOC;
3704 1.1 christos if ((asect->flags & SEC_READONLY) == 0)
3705 1.1 christos this_hdr->sh_flags |= SHF_WRITE;
3706 1.1 christos if ((asect->flags & SEC_CODE) != 0)
3707 1.1 christos this_hdr->sh_flags |= SHF_EXECINSTR;
3708 1.1 christos if ((asect->flags & SEC_MERGE) != 0)
3709 1.1 christos {
3710 1.1 christos this_hdr->sh_flags |= SHF_MERGE;
3711 1.8 christos this_hdr->sh_entsize = asect->entsize;
3712 1.18 christos }
3713 1.18 christos if ((asect->flags & SEC_STRINGS) != 0)
3714 1.18 christos {
3715 1.18 christos this_hdr->sh_flags |= SHF_STRINGS;
3716 1.1 christos this_hdr->sh_entsize = asect->entsize;
3717 1.1 christos }
3718 1.1 christos if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3719 1.1 christos this_hdr->sh_flags |= SHF_GROUP;
3720 1.1 christos if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3721 1.1 christos {
3722 1.1 christos this_hdr->sh_flags |= SHF_TLS;
3723 1.1 christos if (asect->size == 0
3724 1.1 christos && (asect->flags & SEC_HAS_CONTENTS) == 0)
3725 1.1 christos {
3726 1.1 christos struct bfd_link_order *o = asect->map_tail.link_order;
3727 1.1 christos
3728 1.1 christos this_hdr->sh_size = 0;
3729 1.1 christos if (o != NULL)
3730 1.1 christos {
3731 1.1 christos this_hdr->sh_size = o->offset + o->size;
3732 1.1 christos if (this_hdr->sh_size != 0)
3733 1.1 christos this_hdr->sh_type = SHT_NOBITS;
3734 1.1 christos }
3735 1.1 christos }
3736 1.1 christos }
3737 1.1 christos if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3738 1.1 christos this_hdr->sh_flags |= SHF_EXCLUDE;
3739 1.1 christos
3740 1.1 christos /* If the section has relocs, set up a section header for the
3741 1.1 christos SHT_REL[A] section. If two relocation sections are required for
3742 1.1 christos this section, it is up to the processor-specific back-end to
3743 1.1 christos create the other. */
3744 1.1 christos if ((asect->flags & SEC_RELOC) != 0)
3745 1.1 christos {
3746 1.1 christos /* When doing a relocatable link, create both REL and RELA sections if
3747 1.1 christos needed. */
3748 1.1 christos if (arg->link_info
3749 1.8 christos /* Do the normal setup if we wouldn't create any sections here. */
3750 1.8 christos && esd->rel.count + esd->rela.count > 0
3751 1.1 christos && (bfd_link_relocatable (arg->link_info)
3752 1.1 christos || arg->link_info->emitrelocations))
3753 1.11 christos {
3754 1.14 christos if (esd->rel.count && esd->rel.hdr == NULL
3755 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3756 1.14 christos false, delay_st_name_p))
3757 1.1 christos {
3758 1.1 christos arg->failed = true;
3759 1.1 christos return;
3760 1.11 christos }
3761 1.14 christos if (esd->rela.count && esd->rela.hdr == NULL
3762 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3763 1.14 christos true, delay_st_name_p))
3764 1.1 christos {
3765 1.1 christos arg->failed = true;
3766 1.1 christos return;
3767 1.1 christos }
3768 1.1 christos }
3769 1.1 christos else if (!_bfd_elf_init_reloc_shdr (abfd,
3770 1.6 christos (asect->use_rela_p
3771 1.6 christos ? &esd->rela : &esd->rel),
3772 1.6 christos name,
3773 1.11 christos asect->use_rela_p,
3774 1.14 christos delay_st_name_p))
3775 1.11 christos {
3776 1.11 christos arg->failed = true;
3777 1.1 christos return;
3778 1.1 christos }
3779 1.1 christos }
3780 1.1 christos
3781 1.1 christos /* Check for processor-specific section types. */
3782 1.1 christos sh_type = this_hdr->sh_type;
3783 1.11 christos if (bed->elf_backend_fake_sections
3784 1.14 christos && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3785 1.11 christos {
3786 1.11 christos arg->failed = true;
3787 1.1 christos return;
3788 1.1 christos }
3789 1.1 christos
3790 1.1 christos if (sh_type == SHT_NOBITS && asect->size != 0)
3791 1.1 christos {
3792 1.1 christos /* Don't change the header type from NOBITS if we are being
3793 1.1 christos called for objcopy --only-keep-debug. */
3794 1.1 christos this_hdr->sh_type = sh_type;
3795 1.1 christos }
3796 1.1 christos }
3797 1.1 christos
3798 1.1 christos /* Fill in the contents of a SHT_GROUP section. Called from
3799 1.1 christos _bfd_elf_compute_section_file_positions for gas, objcopy, and
3800 1.1 christos when ELF targets use the generic linker, ld. Called for ld -r
3801 1.1 christos from bfd_elf_final_link. */
3802 1.1 christos
3803 1.1 christos void
3804 1.14 christos bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3805 1.1 christos {
3806 1.1 christos bool *failedptr = (bool *) failedptrarg;
3807 1.14 christos asection *elt, *first;
3808 1.1 christos unsigned char *loc;
3809 1.1 christos bool gas;
3810 1.1 christos
3811 1.12 christos /* Ignore linker created group section. See elfNN_ia64_object_p in
3812 1.12 christos elfxx-ia64.c. */
3813 1.1 christos if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3814 1.1 christos || sec->size == 0
3815 1.1 christos || *failedptr)
3816 1.1 christos return;
3817 1.1 christos
3818 1.1 christos if (elf_section_data (sec)->this_hdr.sh_info == 0)
3819 1.1 christos {
3820 1.1 christos unsigned long symindx = 0;
3821 1.1 christos
3822 1.1 christos /* elf_group_id will have been set up by objcopy and the
3823 1.1 christos generic linker. */
3824 1.1 christos if (elf_group_id (sec) != NULL)
3825 1.1 christos symindx = elf_group_id (sec)->udata.i;
3826 1.1 christos
3827 1.1 christos if (symindx == 0)
3828 1.12 christos {
3829 1.12 christos /* If called from the assembler, swap_out_syms will have set up
3830 1.14 christos elf_section_syms.
3831 1.14 christos PR 25699: A corrupt input file could contain bogus group info. */
3832 1.12 christos if (sec->index >= elf_num_section_syms (abfd)
3833 1.14 christos || elf_section_syms (abfd)[sec->index] == NULL)
3834 1.12 christos {
3835 1.12 christos *failedptr = true;
3836 1.1 christos return;
3837 1.1 christos }
3838 1.1 christos symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3839 1.1 christos }
3840 1.1 christos elf_section_data (sec)->this_hdr.sh_info = symindx;
3841 1.1 christos }
3842 1.1 christos else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3843 1.1 christos {
3844 1.1 christos /* The ELF backend linker sets sh_info to -2 when the group
3845 1.9 christos signature symbol is global, and thus the index can't be
3846 1.9 christos set until all local symbols are output. */
3847 1.9 christos asection *igroup;
3848 1.9 christos struct bfd_elf_section_data *sec_data;
3849 1.1 christos unsigned long symndx;
3850 1.1 christos unsigned long extsymoff;
3851 1.9 christos struct elf_link_hash_entry *h;
3852 1.9 christos
3853 1.9 christos /* The point of this little dance to the first SHF_GROUP section
3854 1.9 christos then back to the SHT_GROUP section is that this gets us to
3855 1.9 christos the SHT_GROUP in the input object. */
3856 1.9 christos igroup = elf_sec_group (elf_next_in_group (sec));
3857 1.9 christos sec_data = elf_section_data (igroup);
3858 1.1 christos symndx = sec_data->this_hdr.sh_info;
3859 1.1 christos extsymoff = 0;
3860 1.1 christos if (!elf_bad_symtab (igroup->owner))
3861 1.1 christos {
3862 1.1 christos Elf_Internal_Shdr *symtab_hdr;
3863 1.1 christos
3864 1.1 christos symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3865 1.1 christos extsymoff = symtab_hdr->sh_info;
3866 1.1 christos }
3867 1.1 christos h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3868 1.1 christos while (h->root.type == bfd_link_hash_indirect
3869 1.1 christos || h->root.type == bfd_link_hash_warning)
3870 1.1 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
3871 1.1 christos
3872 1.1 christos elf_section_data (sec)->this_hdr.sh_info = h->indx;
3873 1.1 christos }
3874 1.14 christos
3875 1.1 christos /* The contents won't be allocated for "ld -r" or objcopy. */
3876 1.1 christos gas = true;
3877 1.14 christos if (sec->contents == NULL)
3878 1.1 christos {
3879 1.1 christos gas = false;
3880 1.1 christos sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3881 1.1 christos
3882 1.1 christos /* Arrange for the section to be written out. */
3883 1.1 christos elf_section_data (sec)->this_hdr.contents = sec->contents;
3884 1.14 christos if (sec->contents == NULL)
3885 1.1 christos {
3886 1.1 christos *failedptr = true;
3887 1.1 christos return;
3888 1.1 christos }
3889 1.1 christos }
3890 1.1 christos
3891 1.1 christos loc = sec->contents + sec->size;
3892 1.1 christos
3893 1.1 christos /* Get the pointer to the first section in the group that gas
3894 1.1 christos squirreled away here. objcopy arranges for this to be set to the
3895 1.1 christos start of the input section group. */
3896 1.1 christos first = elt = elf_next_in_group (sec);
3897 1.1 christos
3898 1.1 christos /* First element is a flag word. Rest of section is elf section
3899 1.1 christos indices for all the sections of the group. Write them backwards
3900 1.1 christos just to keep the group in the same order as given in .section
3901 1.1 christos directives, not that it matters. */
3902 1.1 christos while (elt != NULL)
3903 1.1 christos {
3904 1.1 christos asection *s;
3905 1.1 christos
3906 1.1 christos s = elt;
3907 1.1 christos if (!gas)
3908 1.1 christos s = s->output_section;
3909 1.1 christos if (s != NULL
3910 1.11 christos && !bfd_is_abs_section (s))
3911 1.11 christos {
3912 1.1 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3913 1.11 christos struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3914 1.11 christos
3915 1.11 christos if (elf_sec->rel.hdr != NULL
3916 1.11 christos && (gas
3917 1.11 christos || (input_elf_sec->rel.hdr != NULL
3918 1.11 christos && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3919 1.11 christos {
3920 1.14 christos elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3921 1.14 christos loc -= 4;
3922 1.11 christos if (loc == sec->contents)
3923 1.11 christos break;
3924 1.11 christos H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3925 1.11 christos }
3926 1.11 christos if (elf_sec->rela.hdr != NULL
3927 1.11 christos && (gas
3928 1.11 christos || (input_elf_sec->rela.hdr != NULL
3929 1.11 christos && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3930 1.11 christos {
3931 1.14 christos elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3932 1.14 christos loc -= 4;
3933 1.11 christos if (loc == sec->contents)
3934 1.11 christos break;
3935 1.1 christos H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3936 1.14 christos }
3937 1.14 christos loc -= 4;
3938 1.11 christos if (loc == sec->contents)
3939 1.1 christos break;
3940 1.1 christos H_PUT_32 (abfd, elf_sec->this_idx, loc);
3941 1.1 christos }
3942 1.1 christos elt = elf_next_in_group (elt);
3943 1.1 christos if (elt == first)
3944 1.1 christos break;
3945 1.14 christos }
3946 1.14 christos
3947 1.14 christos /* We should always get here with loc == sec->contents + 4, but it is
3948 1.14 christos possible to craft bogus SHT_GROUP sections that will cause segfaults
3949 1.14 christos in objcopy without checking loc here and in the loop above. */
3950 1.14 christos if (loc == sec->contents)
3951 1.14 christos BFD_ASSERT (0);
3952 1.14 christos else
3953 1.14 christos {
3954 1.14 christos loc -= 4;
3955 1.14 christos if (loc != sec->contents)
3956 1.14 christos {
3957 1.14 christos BFD_ASSERT (0);
3958 1.14 christos memset (sec->contents + 4, 0, loc - sec->contents);
3959 1.14 christos loc = sec->contents;
3960 1.1 christos }
3961 1.1 christos }
3962 1.1 christos
3963 1.1 christos H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3964 1.11 christos }
3965 1.11 christos
3966 1.11 christos /* Given NAME, the name of a relocation section stripped of its
3967 1.6 christos .rel/.rela prefix, return the section in ABFD to which the
3968 1.6 christos relocations apply. */
3969 1.11 christos
3970 1.11 christos asection *
3971 1.11 christos _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3972 1.11 christos {
3973 1.11 christos /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3974 1.11 christos section likely apply to .got.plt or .got section. */
3975 1.11 christos if (get_elf_backend_data (abfd)->want_got_plt
3976 1.11 christos && strcmp (name, ".plt") == 0)
3977 1.11 christos {
3978 1.11 christos asection *sec;
3979 1.11 christos
3980 1.11 christos name = ".got.plt";
3981 1.11 christos sec = bfd_get_section_by_name (abfd, name);
3982 1.11 christos if (sec != NULL)
3983 1.11 christos return sec;
3984 1.11 christos name = ".got";
3985 1.11 christos }
3986 1.11 christos
3987 1.11 christos return bfd_get_section_by_name (abfd, name);
3988 1.11 christos }
3989 1.11 christos
3990 1.11 christos /* Return the section to which RELOC_SEC applies. */
3991 1.11 christos
3992 1.6 christos static asection *
3993 1.6 christos elf_get_reloc_section (asection *reloc_sec)
3994 1.6 christos {
3995 1.6 christos const char *name;
3996 1.11 christos unsigned int type;
3997 1.6 christos bfd *abfd;
3998 1.6 christos const struct elf_backend_data *bed;
3999 1.6 christos
4000 1.6 christos type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4001 1.6 christos if (type != SHT_REL && type != SHT_RELA)
4002 1.6 christos return NULL;
4003 1.6 christos
4004 1.14 christos /* We look up the section the relocs apply to by name. */
4005 1.11 christos name = reloc_sec->name;
4006 1.11 christos if (!startswith (name, ".rel"))
4007 1.11 christos return NULL;
4008 1.11 christos name += 4;
4009 1.6 christos if (type == SHT_RELA && *name++ != 'a')
4010 1.6 christos return NULL;
4011 1.11 christos
4012 1.11 christos abfd = reloc_sec->owner;
4013 1.6 christos bed = get_elf_backend_data (abfd);
4014 1.6 christos return bed->get_reloc_section (abfd, name);
4015 1.1 christos }
4016 1.1 christos
4017 1.12 christos /* Assign all ELF section numbers. The dummy first section is handled here
4018 1.14 christos too. The link/info pointers for the standard section types are filled
4019 1.1 christos in here too, while we're at it. LINK_INFO will be 0 when arriving
4020 1.14 christos here for gas, objcopy, and when using the generic ELF linker. */
4021 1.1 christos
4022 1.1 christos static bool
4023 1.1 christos assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4024 1.1 christos {
4025 1.6 christos struct elf_obj_tdata *t = elf_tdata (abfd);
4026 1.1 christos asection *sec;
4027 1.1 christos unsigned int section_number;
4028 1.14 christos Elf_Internal_Shdr **i_shdrp;
4029 1.12 christos struct bfd_elf_section_data *d;
4030 1.1 christos bool need_symtab;
4031 1.1 christos size_t amt;
4032 1.1 christos
4033 1.1 christos section_number = 1;
4034 1.1 christos
4035 1.1 christos _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4036 1.11 christos
4037 1.1 christos /* SHT_GROUP sections are in relocatable files only. */
4038 1.8 christos if (link_info == NULL || !link_info->resolve_section_groups)
4039 1.8 christos {
4040 1.1 christos size_t reloc_count = 0;
4041 1.1 christos
4042 1.1 christos /* Put SHT_GROUP sections first. */
4043 1.1 christos for (sec = abfd->sections; sec != NULL; sec = sec->next)
4044 1.1 christos {
4045 1.1 christos d = elf_section_data (sec);
4046 1.1 christos
4047 1.1 christos if (d->this_hdr.sh_type == SHT_GROUP)
4048 1.1 christos {
4049 1.1 christos if (sec->flags & SEC_LINKER_CREATED)
4050 1.1 christos {
4051 1.1 christos /* Remove the linker created SHT_GROUP sections. */
4052 1.1 christos bfd_section_list_remove (abfd, sec);
4053 1.1 christos abfd->section_count--;
4054 1.1 christos }
4055 1.1 christos else
4056 1.8 christos d->this_idx = section_number++;
4057 1.8 christos }
4058 1.8 christos
4059 1.1 christos /* Count relocations. */
4060 1.8 christos reloc_count += sec->reloc_count;
4061 1.14 christos }
4062 1.8 christos
4063 1.8 christos /* Set/clear HAS_RELOC depending on whether there are relocations. */
4064 1.14 christos if (reloc_count == 0)
4065 1.14 christos abfd->flags &= ~HAS_RELOC;
4066 1.1 christos else
4067 1.1 christos abfd->flags |= HAS_RELOC;
4068 1.1 christos }
4069 1.1 christos
4070 1.1 christos for (sec = abfd->sections; sec; sec = sec->next)
4071 1.1 christos {
4072 1.1 christos d = elf_section_data (sec);
4073 1.1 christos
4074 1.6 christos if (d->this_hdr.sh_type != SHT_GROUP)
4075 1.6 christos d->this_idx = section_number++;
4076 1.1 christos if (d->this_hdr.sh_name != (unsigned int) -1)
4077 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4078 1.1 christos if (d->rel.hdr)
4079 1.6 christos {
4080 1.6 christos d->rel.idx = section_number++;
4081 1.1 christos if (d->rel.hdr->sh_name != (unsigned int) -1)
4082 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4083 1.1 christos }
4084 1.1 christos else
4085 1.1 christos d->rel.idx = 0;
4086 1.1 christos
4087 1.1 christos if (d->rela.hdr)
4088 1.6 christos {
4089 1.6 christos d->rela.idx = section_number++;
4090 1.1 christos if (d->rela.hdr->sh_name != (unsigned int) -1)
4091 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4092 1.1 christos }
4093 1.1 christos else
4094 1.1 christos d->rela.idx = 0;
4095 1.1 christos }
4096 1.14 christos
4097 1.14 christos need_symtab = (bfd_get_symcount (abfd) > 0
4098 1.14 christos || (link_info == NULL
4099 1.1 christos && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4100 1.1 christos == HAS_RELOC)));
4101 1.3 christos if (need_symtab)
4102 1.1 christos {
4103 1.1 christos elf_onesymtab (abfd) = section_number++;
4104 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4105 1.12 christos if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4106 1.8 christos {
4107 1.8 christos elf_section_list *entry;
4108 1.8 christos
4109 1.12 christos BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4110 1.8 christos
4111 1.8 christos entry = bfd_zalloc (abfd, sizeof (*entry));
4112 1.8 christos entry->ndx = section_number++;
4113 1.1 christos elf_symtab_shndx_list (abfd) = entry;
4114 1.14 christos entry->hdr.sh_name
4115 1.8 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4116 1.14 christos ".symtab_shndx", false);
4117 1.1 christos if (entry->hdr.sh_name == (unsigned int) -1)
4118 1.3 christos return false;
4119 1.1 christos }
4120 1.1 christos elf_strtab_sec (abfd) = section_number++;
4121 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4122 1.9 christos }
4123 1.9 christos
4124 1.9 christos elf_shstrtab_sec (abfd) = section_number++;
4125 1.9 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4126 1.3 christos elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4127 1.3 christos
4128 1.9 christos if (section_number >= SHN_LORESERVE)
4129 1.11 christos {
4130 1.3 christos /* xgettext:c-format */
4131 1.14 christos _bfd_error_handler (_("%pB: too many sections: %u"),
4132 1.3 christos abfd, section_number);
4133 1.3 christos return false;
4134 1.1 christos }
4135 1.1 christos
4136 1.1 christos elf_numsections (abfd) = section_number;
4137 1.1 christos elf_elfheader (abfd)->e_shnum = section_number;
4138 1.1 christos
4139 1.12 christos /* Set up the list of section header pointers, in agreement with the
4140 1.12 christos indices. */
4141 1.1 christos amt = section_number * sizeof (Elf_Internal_Shdr *);
4142 1.14 christos i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4143 1.1 christos if (i_shdrp == NULL)
4144 1.1 christos return false;
4145 1.11 christos
4146 1.1 christos i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4147 1.1 christos sizeof (Elf_Internal_Shdr));
4148 1.1 christos if (i_shdrp[0] == NULL)
4149 1.14 christos {
4150 1.1 christos bfd_release (abfd, i_shdrp);
4151 1.1 christos return false;
4152 1.1 christos }
4153 1.1 christos
4154 1.3 christos elf_elfsections (abfd) = i_shdrp;
4155 1.1 christos
4156 1.1 christos i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4157 1.3 christos if (need_symtab)
4158 1.1 christos {
4159 1.1 christos i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4160 1.8 christos if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4161 1.8 christos {
4162 1.8 christos elf_section_list * entry = elf_symtab_shndx_list (abfd);
4163 1.8 christos BFD_ASSERT (entry != NULL);
4164 1.1 christos i_shdrp[entry->ndx] = & entry->hdr;
4165 1.3 christos entry->hdr.sh_link = elf_onesymtab (abfd);
4166 1.3 christos }
4167 1.1 christos i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4168 1.1 christos t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4169 1.1 christos }
4170 1.1 christos
4171 1.1 christos for (sec = abfd->sections; sec; sec = sec->next)
4172 1.1 christos {
4173 1.1 christos asection *s;
4174 1.1 christos
4175 1.1 christos d = elf_section_data (sec);
4176 1.1 christos
4177 1.1 christos i_shdrp[d->this_idx] = &d->this_hdr;
4178 1.1 christos if (d->rel.idx != 0)
4179 1.1 christos i_shdrp[d->rel.idx] = d->rel.hdr;
4180 1.1 christos if (d->rela.idx != 0)
4181 1.1 christos i_shdrp[d->rela.idx] = d->rela.hdr;
4182 1.1 christos
4183 1.1 christos /* Fill in the sh_link and sh_info fields while we're at it. */
4184 1.1 christos
4185 1.1 christos /* sh_link of a reloc section is the section index of the symbol
4186 1.1 christos table. sh_info is the section index of the section to which
4187 1.1 christos the relocation entries apply. */
4188 1.3 christos if (d->rel.idx != 0)
4189 1.1 christos {
4190 1.4 christos d->rel.hdr->sh_link = elf_onesymtab (abfd);
4191 1.1 christos d->rel.hdr->sh_info = d->this_idx;
4192 1.1 christos d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4193 1.1 christos }
4194 1.3 christos if (d->rela.idx != 0)
4195 1.1 christos {
4196 1.4 christos d->rela.hdr->sh_link = elf_onesymtab (abfd);
4197 1.1 christos d->rela.hdr->sh_info = d->this_idx;
4198 1.1 christos d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4199 1.1 christos }
4200 1.1 christos
4201 1.1 christos /* We need to set up sh_link for SHF_LINK_ORDER. */
4202 1.1 christos if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4203 1.14 christos {
4204 1.14 christos s = elf_linked_to_section (sec);
4205 1.14 christos /* We can now have a NULL linked section pointer.
4206 1.14 christos This happens when the sh_link field is 0, which is done
4207 1.1 christos when a linked to section is discarded but the linking
4208 1.1 christos section has been retained for some reason. */
4209 1.12 christos if (s)
4210 1.12 christos {
4211 1.1 christos /* Check discarded linkonce section. */
4212 1.12 christos if (discarded_section (s))
4213 1.12 christos {
4214 1.12 christos asection *kept;
4215 1.12 christos _bfd_error_handler
4216 1.12 christos /* xgettext:c-format */
4217 1.12 christos (_("%pB: sh_link of section `%pA' points to"
4218 1.12 christos " discarded section `%pA' of `%pB'"),
4219 1.12 christos abfd, d->this_hdr.bfd_section, s, s->owner);
4220 1.12 christos /* Point to the kept section if it has the same
4221 1.12 christos size as the discarded one. */
4222 1.1 christos kept = _bfd_elf_check_kept_section (s, link_info);
4223 1.12 christos if (kept == NULL)
4224 1.14 christos {
4225 1.1 christos bfd_set_error (bfd_error_bad_value);
4226 1.12 christos return false;
4227 1.1 christos }
4228 1.12 christos s = kept;
4229 1.12 christos }
4230 1.1 christos /* Handle objcopy. */
4231 1.12 christos else if (s->output_section == NULL)
4232 1.12 christos {
4233 1.12 christos _bfd_error_handler
4234 1.12 christos /* xgettext:c-format */
4235 1.12 christos (_("%pB: sh_link of section `%pA' points to"
4236 1.12 christos " removed section `%pA' of `%pB'"),
4237 1.14 christos abfd, d->this_hdr.bfd_section, s, s->owner);
4238 1.1 christos bfd_set_error (bfd_error_bad_value);
4239 1.12 christos return false;
4240 1.1 christos }
4241 1.1 christos s = s->output_section;
4242 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4243 1.1 christos }
4244 1.1 christos }
4245 1.1 christos
4246 1.1 christos switch (d->this_hdr.sh_type)
4247 1.1 christos {
4248 1.17 christos case SHT_REL:
4249 1.17 christos case SHT_RELA:
4250 1.17 christos /* sh_link is the section index of the symbol table.
4251 1.17 christos sh_info is the section index of the section to which the
4252 1.14 christos relocation entries apply. */
4253 1.17 christos if (d->this_hdr.sh_link == 0)
4254 1.17 christos {
4255 1.17 christos /* FIXME maybe: If this is a reloc section which we are
4256 1.17 christos treating as a normal section then we likely should
4257 1.17 christos not be assuming its sh_link is .dynsym or .symtab. */
4258 1.17 christos if ((sec->flags & SEC_ALLOC) != 0)
4259 1.17 christos {
4260 1.17 christos s = bfd_get_section_by_name (abfd, ".dynsym");
4261 1.17 christos if (s != NULL)
4262 1.17 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4263 1.17 christos }
4264 1.14 christos else
4265 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd);
4266 1.11 christos }
4267 1.1 christos
4268 1.4 christos s = elf_get_reloc_section (sec);
4269 1.4 christos if (s != NULL)
4270 1.4 christos {
4271 1.4 christos d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4272 1.1 christos d->this_hdr.sh_flags |= SHF_INFO_LINK;
4273 1.1 christos }
4274 1.1 christos break;
4275 1.1 christos
4276 1.1 christos case SHT_STRTAB:
4277 1.1 christos /* We assume that a section named .stab*str is a stabs
4278 1.1 christos string section. We look for a section with the same name
4279 1.14 christos but without the trailing ``str'', and set its sh_link
4280 1.1 christos field to point to this section. */
4281 1.1 christos if (startswith (sec->name, ".stab")
4282 1.1 christos && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4283 1.1 christos {
4284 1.1 christos size_t len;
4285 1.1 christos char *alc;
4286 1.1 christos
4287 1.1 christos len = strlen (sec->name);
4288 1.14 christos alc = (char *) bfd_malloc (len - 2);
4289 1.1 christos if (alc == NULL)
4290 1.1 christos return false;
4291 1.1 christos memcpy (alc, sec->name, len - 3);
4292 1.1 christos alc[len - 3] = '\0';
4293 1.1 christos s = bfd_get_section_by_name (abfd, alc);
4294 1.1 christos free (alc);
4295 1.1 christos if (s != NULL)
4296 1.1 christos {
4297 1.1 christos elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4298 1.12 christos
4299 1.1 christos /* This is a .stab section. */
4300 1.1 christos elf_section_data (s)->this_hdr.sh_entsize = 12;
4301 1.1 christos }
4302 1.1 christos }
4303 1.1 christos break;
4304 1.1 christos
4305 1.1 christos case SHT_DYNAMIC:
4306 1.1 christos case SHT_DYNSYM:
4307 1.1 christos case SHT_GNU_verneed:
4308 1.1 christos case SHT_GNU_verdef:
4309 1.1 christos /* sh_link is the section header index of the string table
4310 1.1 christos used for the dynamic entries, or the symbol table, or the
4311 1.1 christos version strings. */
4312 1.1 christos s = bfd_get_section_by_name (abfd, ".dynstr");
4313 1.1 christos if (s != NULL)
4314 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4315 1.1 christos break;
4316 1.1 christos
4317 1.1 christos case SHT_GNU_LIBLIST:
4318 1.1 christos /* sh_link is the section header index of the prelink library
4319 1.14 christos list used for the dynamic entries, or the symbol table, or
4320 1.14 christos the version strings. */
4321 1.1 christos s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4322 1.1 christos ? ".dynstr" : ".gnu.libstr"));
4323 1.1 christos if (s != NULL)
4324 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4325 1.1 christos break;
4326 1.1 christos
4327 1.1 christos case SHT_HASH:
4328 1.1 christos case SHT_GNU_HASH:
4329 1.1 christos case SHT_GNU_versym:
4330 1.1 christos /* sh_link is the section header index of the symbol table
4331 1.1 christos this hash table or version table is for. */
4332 1.1 christos s = bfd_get_section_by_name (abfd, ".dynsym");
4333 1.1 christos if (s != NULL)
4334 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4335 1.1 christos break;
4336 1.3 christos
4337 1.1 christos case SHT_GROUP:
4338 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd);
4339 1.1 christos }
4340 1.6 christos }
4341 1.6 christos
4342 1.6 christos /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4343 1.6 christos _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4344 1.14 christos debug section name from .debug_* to .zdebug_* if needed. */
4345 1.1 christos
4346 1.1 christos return true;
4347 1.14 christos }
4348 1.1 christos
4349 1.1 christos static bool
4350 1.1 christos sym_is_global (bfd *abfd, asymbol *sym)
4351 1.1 christos {
4352 1.1 christos /* If the backend has a special mapping, use it. */
4353 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4354 1.1 christos if (bed->elf_backend_sym_is_global)
4355 1.1 christos return (*bed->elf_backend_sym_is_global) (abfd, sym);
4356 1.12 christos
4357 1.12 christos return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4358 1.1 christos || bfd_is_und_section (bfd_asymbol_section (sym))
4359 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym)));
4360 1.8 christos }
4361 1.8 christos
4362 1.8 christos /* Filter global symbols of ABFD to include in the import library. All
4363 1.8 christos SYMCOUNT symbols of ABFD can be examined from their pointers in
4364 1.8 christos SYMS. Pointers of symbols to keep should be stored contiguously at
4365 1.8 christos the beginning of that array.
4366 1.8 christos
4367 1.8 christos Returns the number of symbols to keep. */
4368 1.8 christos
4369 1.8 christos unsigned int
4370 1.8 christos _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4371 1.8 christos asymbol **syms, long symcount)
4372 1.8 christos {
4373 1.8 christos long src_count, dst_count = 0;
4374 1.8 christos
4375 1.8 christos for (src_count = 0; src_count < symcount; src_count++)
4376 1.8 christos {
4377 1.8 christos asymbol *sym = syms[src_count];
4378 1.8 christos char *name = (char *) bfd_asymbol_name (sym);
4379 1.8 christos struct bfd_link_hash_entry *h;
4380 1.8 christos
4381 1.8 christos if (!sym_is_global (abfd, sym))
4382 1.14 christos continue;
4383 1.8 christos
4384 1.8 christos h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4385 1.8 christos if (h == NULL)
4386 1.8 christos continue;
4387 1.8 christos if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4388 1.8 christos continue;
4389 1.8 christos if (h->linker_def || h->ldscript_def)
4390 1.8 christos continue;
4391 1.8 christos
4392 1.8 christos syms[dst_count++] = sym;
4393 1.8 christos }
4394 1.8 christos
4395 1.8 christos syms[dst_count] = NULL;
4396 1.8 christos
4397 1.8 christos return dst_count;
4398 1.17 christos }
4399 1.17 christos
4400 1.1 christos /* Don't output symbols for sections that are not going to be output,
4401 1.14 christos that are duplicates or there is no BFD section. */
4402 1.17 christos
4403 1.1 christos static bool
4404 1.11 christos ignore_sym (asymbol *sym)
4405 1.14 christos {
4406 1.11 christos if (sym == NULL)
4407 1.17 christos return false;
4408 1.14 christos
4409 1.3 christos if (sym->section == NULL)
4410 1.17 christos return true;
4411 1.17 christos
4412 1.17 christos if ((sym->flags & BSF_SECTION_SYM) != 0)
4413 1.17 christos {
4414 1.17 christos if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4415 1.17 christos return true;
4416 1.17 christos /* With ld -r on generic elf targets it is possible to have
4417 1.17 christos multiple section symbols in the output for a given section.
4418 1.17 christos We'd like to get rid of all but the first one. This drops
4419 1.17 christos them if the first input section is non-zero size, but fails
4420 1.17 christos to do so if the first input section is zero sized. */
4421 1.17 christos if (sym->section->output_offset != 0)
4422 1.11 christos return true;
4423 1.17 christos }
4424 1.1 christos
4425 1.1 christos return discarded_section (sym->section);
4426 1.3 christos }
4427 1.3 christos
4428 1.3 christos /* Map symbol from it's internal number to the external number, moving
4429 1.14 christos all local symbols to be at the head of the list. */
4430 1.3 christos
4431 1.1 christos static bool
4432 1.1 christos elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4433 1.1 christos {
4434 1.1 christos unsigned int symcount = bfd_get_symcount (abfd);
4435 1.1 christos asymbol **syms = bfd_get_outsymbols (abfd);
4436 1.1 christos asymbol **sect_syms;
4437 1.8 christos unsigned int num_locals = 0;
4438 1.1 christos unsigned int num_globals = 0;
4439 1.1 christos unsigned int max_index = 0;
4440 1.1 christos unsigned int idx;
4441 1.12 christos asection *asect;
4442 1.1 christos asymbol **new_syms;
4443 1.1 christos size_t amt;
4444 1.1 christos
4445 1.1 christos #ifdef DEBUG
4446 1.1 christos fprintf (stderr, "elf_map_symbols\n");
4447 1.1 christos fflush (stderr);
4448 1.1 christos #endif
4449 1.1 christos
4450 1.1 christos for (asect = abfd->sections; asect; asect = asect->next)
4451 1.1 christos {
4452 1.1 christos if (max_index < asect->index)
4453 1.1 christos max_index = asect->index;
4454 1.1 christos }
4455 1.12 christos
4456 1.12 christos max_index++;
4457 1.1 christos amt = max_index * sizeof (asymbol *);
4458 1.14 christos sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4459 1.1 christos if (sect_syms == NULL)
4460 1.1 christos return false;
4461 1.1 christos elf_section_syms (abfd) = sect_syms;
4462 1.1 christos elf_num_section_syms (abfd) = max_index;
4463 1.1 christos
4464 1.1 christos /* Init sect_syms entries for any section symbols we have already
4465 1.1 christos decided to output. */
4466 1.1 christos for (idx = 0; idx < symcount; idx++)
4467 1.1 christos {
4468 1.1 christos asymbol *sym = syms[idx];
4469 1.1 christos
4470 1.17 christos if ((sym->flags & BSF_SECTION_SYM) != 0
4471 1.3 christos && sym->value == 0
4472 1.1 christos && !ignore_sym (sym)
4473 1.1 christos && !bfd_is_abs_section (sym->section))
4474 1.1 christos {
4475 1.1 christos asection *sec = sym->section;
4476 1.1 christos
4477 1.1 christos if (sec->owner != abfd)
4478 1.1 christos sec = sec->output_section;
4479 1.1 christos
4480 1.1 christos sect_syms[sec->index] = syms[idx];
4481 1.1 christos }
4482 1.1 christos }
4483 1.1 christos
4484 1.1 christos /* Classify all of the symbols. */
4485 1.17 christos for (idx = 0; idx < symcount; idx++)
4486 1.17 christos {
4487 1.3 christos if (ignore_sym (syms[idx]))
4488 1.3 christos continue;
4489 1.17 christos if (sym_is_global (abfd, syms[idx]))
4490 1.1 christos num_globals++;
4491 1.1 christos else
4492 1.1 christos num_locals++;
4493 1.1 christos }
4494 1.1 christos
4495 1.1 christos /* We will be adding a section symbol for each normal BFD section. Most
4496 1.1 christos sections will already have a section symbol in outsymbols, but
4497 1.1 christos eg. SHT_GROUP sections will not, and we need the section symbol mapped
4498 1.1 christos at least in that case. */
4499 1.14 christos for (asect = abfd->sections; asect; asect = asect->next)
4500 1.14 christos {
4501 1.17 christos asymbol *sym = asect->symbol;
4502 1.14 christos /* Don't include ignored section symbols. */
4503 1.1 christos if (!ignore_sym (sym)
4504 1.17 christos && sect_syms[asect->index] == NULL)
4505 1.17 christos {
4506 1.17 christos if (sym_is_global (abfd, asect->symbol))
4507 1.1 christos num_globals++;
4508 1.1 christos else
4509 1.1 christos num_locals++;
4510 1.1 christos }
4511 1.1 christos }
4512 1.12 christos
4513 1.12 christos /* Now sort the symbols so the local symbols are first. */
4514 1.1 christos amt = (num_locals + num_globals) * sizeof (asymbol *);
4515 1.14 christos new_syms = (asymbol **) bfd_alloc (abfd, amt);
4516 1.1 christos if (new_syms == NULL)
4517 1.17 christos return false;
4518 1.17 christos
4519 1.1 christos unsigned int num_globals2 = 0;
4520 1.1 christos unsigned int num_locals2 = 0;
4521 1.1 christos for (idx = 0; idx < symcount; idx++)
4522 1.1 christos {
4523 1.1 christos asymbol *sym = syms[idx];
4524 1.17 christos unsigned int i;
4525 1.17 christos
4526 1.17 christos if (ignore_sym (sym))
4527 1.3 christos continue;
4528 1.3 christos
4529 1.17 christos if (sym_is_global (abfd, sym))
4530 1.1 christos i = num_locals + num_globals2++;
4531 1.1 christos else
4532 1.1 christos i = num_locals2++;
4533 1.1 christos new_syms[i] = sym;
4534 1.1 christos sym->udata.i = i + 1;
4535 1.1 christos }
4536 1.14 christos for (asect = abfd->sections; asect; asect = asect->next)
4537 1.17 christos {
4538 1.14 christos asymbol *sym = asect->symbol;
4539 1.1 christos if (!ignore_sym (sym)
4540 1.1 christos && sect_syms[asect->index] == NULL)
4541 1.1 christos {
4542 1.1 christos unsigned int i;
4543 1.17 christos
4544 1.17 christos sect_syms[asect->index] = sym;
4545 1.17 christos if (sym_is_global (abfd, sym))
4546 1.1 christos i = num_locals + num_globals2++;
4547 1.1 christos else
4548 1.1 christos i = num_locals2++;
4549 1.1 christos new_syms[i] = sym;
4550 1.1 christos sym->udata.i = i + 1;
4551 1.1 christos }
4552 1.1 christos }
4553 1.1 christos
4554 1.3 christos bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4555 1.14 christos
4556 1.1 christos *pnum_locals = num_locals;
4557 1.1 christos return true;
4558 1.1 christos }
4559 1.1 christos
4560 1.1 christos /* Assign a file position to a section, optionally aligning to the
4561 1.1 christos required section alignment. */
4562 1.1 christos
4563 1.1 christos file_ptr
4564 1.18 christos _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4565 1.18 christos file_ptr offset,
4566 1.1 christos bool align,
4567 1.18 christos unsigned char log_file_align)
4568 1.18 christos {
4569 1.18 christos if (i_shdrp->sh_addralign > 1)
4570 1.18 christos {
4571 1.18 christos file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4572 1.18 christos
4573 1.18 christos if (align)
4574 1.18 christos offset = BFD_ALIGN (offset, salign);
4575 1.18 christos else if (log_file_align)
4576 1.18 christos {
4577 1.18 christos /* Heuristic: Cap alignment at log_file_align. */
4578 1.18 christos file_ptr falign = 1u << log_file_align;
4579 1.18 christos
4580 1.18 christos offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4581 1.1 christos }
4582 1.1 christos }
4583 1.1 christos i_shdrp->sh_offset = offset;
4584 1.1 christos if (i_shdrp->bfd_section != NULL)
4585 1.1 christos i_shdrp->bfd_section->filepos = offset;
4586 1.1 christos if (i_shdrp->sh_type != SHT_NOBITS)
4587 1.1 christos offset += i_shdrp->sh_size;
4588 1.1 christos return offset;
4589 1.1 christos }
4590 1.1 christos
4591 1.1 christos /* Compute the file positions we are going to put the sections at, and
4592 1.1 christos otherwise prepare to begin writing out the ELF file. If LINK_INFO
4593 1.14 christos is not NULL, this is being called by the ELF backend linker. */
4594 1.1 christos
4595 1.1 christos bool
4596 1.1 christos _bfd_elf_compute_section_file_positions (bfd *abfd,
4597 1.1 christos struct bfd_link_info *link_info)
4598 1.1 christos {
4599 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4600 1.6 christos struct fake_section_arg fsargs;
4601 1.1 christos bool failed;
4602 1.14 christos struct elf_strtab_hash *strtab = NULL;
4603 1.1 christos Elf_Internal_Shdr *shstrtab_hdr;
4604 1.1 christos bool need_symtab;
4605 1.14 christos
4606 1.1 christos if (abfd->output_has_begun)
4607 1.1 christos return true;
4608 1.1 christos
4609 1.1 christos /* Do any elf backend specific processing first. */
4610 1.1 christos if (bed->elf_backend_begin_write_processing)
4611 1.12 christos (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4612 1.14 christos
4613 1.1 christos if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4614 1.14 christos return false;
4615 1.1 christos
4616 1.1 christos fsargs.failed = false;
4617 1.1 christos fsargs.link_info = link_info;
4618 1.14 christos bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4619 1.1 christos if (fsargs.failed)
4620 1.1 christos return false;
4621 1.14 christos
4622 1.1 christos if (!assign_section_numbers (abfd, link_info))
4623 1.1 christos return false;
4624 1.1 christos
4625 1.1 christos /* The backend linker builds symbol table information itself. */
4626 1.1 christos need_symtab = (link_info == NULL
4627 1.1 christos && (bfd_get_symcount (abfd) > 0
4628 1.1 christos || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4629 1.1 christos == HAS_RELOC)));
4630 1.1 christos if (need_symtab)
4631 1.1 christos {
4632 1.1 christos /* Non-zero if doing a relocatable link. */
4633 1.14 christos int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4634 1.14 christos
4635 1.1 christos if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4636 1.1 christos return false;
4637 1.14 christos }
4638 1.1 christos
4639 1.1 christos failed = false;
4640 1.1 christos if (link_info == NULL)
4641 1.1 christos {
4642 1.17 christos bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4643 1.1 christos if (failed)
4644 1.1 christos goto err_free_strtab;
4645 1.1 christos }
4646 1.12 christos
4647 1.1 christos shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4648 1.8 christos /* sh_name was set in init_file_header. */
4649 1.1 christos shstrtab_hdr->sh_type = SHT_STRTAB;
4650 1.6 christos shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4651 1.1 christos shstrtab_hdr->sh_addr = 0;
4652 1.1 christos /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4653 1.1 christos shstrtab_hdr->sh_entsize = 0;
4654 1.6 christos shstrtab_hdr->sh_link = 0;
4655 1.1 christos shstrtab_hdr->sh_info = 0;
4656 1.1 christos /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4657 1.1 christos shstrtab_hdr->sh_addralign = 1;
4658 1.17 christos
4659 1.1 christos if (!assign_file_positions_except_relocs (abfd, link_info))
4660 1.17 christos goto err_free_strtab;
4661 1.1 christos
4662 1.1 christos if (strtab != NULL)
4663 1.1 christos {
4664 1.1 christos file_ptr off;
4665 1.3 christos Elf_Internal_Shdr *hdr;
4666 1.1 christos
4667 1.8 christos off = elf_next_file_pos (abfd);
4668 1.18 christos
4669 1.1 christos hdr = & elf_symtab_hdr (abfd);
4670 1.8 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4671 1.8 christos
4672 1.8 christos if (elf_symtab_shndx_list (abfd) != NULL)
4673 1.8 christos {
4674 1.18 christos hdr = & elf_symtab_shndx_list (abfd)->hdr;
4675 1.8 christos if (hdr->sh_size != 0)
4676 1.8 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4677 1.1 christos /* FIXME: What about other symtab_shndx sections in the list ? */
4678 1.1 christos }
4679 1.18 christos
4680 1.1 christos hdr = &elf_tdata (abfd)->strtab_hdr;
4681 1.3 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4682 1.1 christos
4683 1.1 christos elf_next_file_pos (abfd) = off;
4684 1.1 christos
4685 1.1 christos /* Now that we know where the .strtab section goes, write it
4686 1.6 christos out. */
4687 1.17 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4688 1.6 christos || ! _bfd_elf_strtab_emit (abfd, strtab))
4689 1.1 christos goto err_free_strtab;
4690 1.1 christos _bfd_elf_strtab_free (strtab);
4691 1.14 christos }
4692 1.17 christos
4693 1.1 christos abfd->output_has_begun = true;
4694 1.17 christos return true;
4695 1.17 christos
4696 1.17 christos err_free_strtab:
4697 1.17 christos if (strtab != NULL)
4698 1.14 christos _bfd_elf_strtab_free (strtab);
4699 1.14 christos return false;
4700 1.14 christos }
4701 1.14 christos
4702 1.14 christos /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4703 1.14 christos function effectively returns whether --eh-frame-hdr is given on the
4704 1.14 christos command line. After size_dynamic_sections the result reflects
4705 1.14 christos whether .eh_frame_hdr will actually be output (sizing isn't done
4706 1.14 christos until ldemul_after_allocation). */
4707 1.14 christos
4708 1.14 christos static asection *
4709 1.14 christos elf_eh_frame_hdr (const struct bfd_link_info *info)
4710 1.14 christos {
4711 1.14 christos if (info != NULL && is_elf_hash_table (info->hash))
4712 1.1 christos return elf_hash_table (info)->eh_info.hdr_sec;
4713 1.1 christos return NULL;
4714 1.1 christos }
4715 1.1 christos
4716 1.1 christos /* Make an initial estimate of the size of the program header. If we
4717 1.1 christos get the number wrong here, we'll redo section placement. */
4718 1.1 christos
4719 1.1 christos static bfd_size_type
4720 1.1 christos get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4721 1.1 christos {
4722 1.1 christos size_t segs;
4723 1.1 christos asection *s;
4724 1.1 christos const struct elf_backend_data *bed;
4725 1.1 christos
4726 1.1 christos /* Assume we will need exactly two PT_LOAD segments: one for text
4727 1.1 christos and one for data. */
4728 1.1 christos segs = 2;
4729 1.11 christos
4730 1.1 christos s = bfd_get_section_by_name (abfd, ".interp");
4731 1.1 christos if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4732 1.1 christos {
4733 1.1 christos /* If we have a loadable interpreter section, we need a
4734 1.1 christos PT_INTERP segment. In this case, assume we also need a
4735 1.1 christos PT_PHDR segment, although that may not be true for all
4736 1.1 christos targets. */
4737 1.1 christos segs += 2;
4738 1.1 christos }
4739 1.1 christos
4740 1.1 christos if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4741 1.1 christos {
4742 1.1 christos /* We need a PT_DYNAMIC segment. */
4743 1.1 christos ++segs;
4744 1.1 christos }
4745 1.1 christos
4746 1.1 christos if (info != NULL && info->relro)
4747 1.1 christos {
4748 1.1 christos /* We need a PT_GNU_RELRO segment. */
4749 1.1 christos ++segs;
4750 1.14 christos }
4751 1.1 christos
4752 1.1 christos if (elf_eh_frame_hdr (info))
4753 1.1 christos {
4754 1.1 christos /* We need a PT_GNU_EH_FRAME segment. */
4755 1.1 christos ++segs;
4756 1.3 christos }
4757 1.1 christos
4758 1.1 christos if (elf_stack_flags (abfd))
4759 1.1 christos {
4760 1.1 christos /* We need a PT_GNU_STACK segment. */
4761 1.1 christos ++segs;
4762 1.14 christos }
4763 1.14 christos
4764 1.14 christos if (elf_sframe (abfd))
4765 1.14 christos {
4766 1.14 christos /* We need a PT_GNU_SFRAME segment. */
4767 1.14 christos ++segs;
4768 1.11 christos }
4769 1.11 christos
4770 1.11 christos s = bfd_get_section_by_name (abfd,
4771 1.11 christos NOTE_GNU_PROPERTY_SECTION_NAME);
4772 1.11 christos if (s != NULL && s->size != 0)
4773 1.11 christos {
4774 1.11 christos /* We need a PT_GNU_PROPERTY segment. */
4775 1.11 christos ++segs;
4776 1.1 christos }
4777 1.1 christos
4778 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
4779 1.11 christos {
4780 1.1 christos if ((s->flags & SEC_LOAD) != 0
4781 1.11 christos && elf_section_type (s) == SHT_NOTE)
4782 1.1 christos {
4783 1.1 christos unsigned int alignment_power;
4784 1.11 christos /* We need a PT_NOTE segment. */
4785 1.11 christos ++segs;
4786 1.11 christos /* Try to create just one PT_NOTE segment for all adjacent
4787 1.11 christos loadable SHT_NOTE sections. gABI requires that within a
4788 1.11 christos PT_NOTE segment (and also inside of each SHT_NOTE section)
4789 1.11 christos each note should have the same alignment. So we check
4790 1.11 christos whether the sections are correctly aligned. */
4791 1.11 christos alignment_power = s->alignment_power;
4792 1.11 christos while (s->next != NULL
4793 1.11 christos && s->next->alignment_power == alignment_power
4794 1.11 christos && (s->next->flags & SEC_LOAD) != 0
4795 1.1 christos && elf_section_type (s->next) == SHT_NOTE)
4796 1.1 christos s = s->next;
4797 1.1 christos }
4798 1.1 christos }
4799 1.1 christos
4800 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
4801 1.1 christos {
4802 1.1 christos if (s->flags & SEC_THREAD_LOCAL)
4803 1.1 christos {
4804 1.1 christos /* We need a PT_TLS segment. */
4805 1.1 christos ++segs;
4806 1.1 christos break;
4807 1.1 christos }
4808 1.1 christos }
4809 1.9 christos
4810 1.12 christos bed = get_elf_backend_data (abfd);
4811 1.12 christos
4812 1.12 christos if ((abfd->flags & D_PAGED) != 0
4813 1.12 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4814 1.14 christos {
4815 1.14 christos /* Add a PT_GNU_MBIND segment for each mbind section. */
4816 1.14 christos bfd_vma commonpagesize;
4817 1.14 christos unsigned int page_align_power;
4818 1.14 christos
4819 1.14 christos if (info != NULL)
4820 1.14 christos commonpagesize = info->commonpagesize;
4821 1.14 christos else
4822 1.12 christos commonpagesize = bed->commonpagesize;
4823 1.12 christos page_align_power = bfd_log2 (commonpagesize);
4824 1.12 christos for (s = abfd->sections; s != NULL; s = s->next)
4825 1.12 christos if (elf_section_flags (s) & SHF_GNU_MBIND)
4826 1.12 christos {
4827 1.12 christos if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4828 1.12 christos {
4829 1.12 christos _bfd_error_handler
4830 1.12 christos /* xgettext:c-format */
4831 1.12 christos (_("%pB: GNU_MBIND section `%pA' has invalid "
4832 1.12 christos "sh_info field: %d"),
4833 1.12 christos abfd, s, elf_section_data (s)->this_hdr.sh_info);
4834 1.12 christos continue;
4835 1.12 christos }
4836 1.12 christos /* Align mbind section to page size. */
4837 1.12 christos if (s->alignment_power < page_align_power)
4838 1.12 christos s->alignment_power = page_align_power;
4839 1.12 christos segs ++;
4840 1.9 christos }
4841 1.12 christos }
4842 1.12 christos
4843 1.1 christos /* Let the backend count up any program headers it might need. */
4844 1.1 christos if (bed->elf_backend_additional_program_headers)
4845 1.1 christos {
4846 1.1 christos int a;
4847 1.1 christos
4848 1.1 christos a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4849 1.1 christos if (a == -1)
4850 1.1 christos abort ();
4851 1.1 christos segs += a;
4852 1.1 christos }
4853 1.1 christos
4854 1.1 christos return segs * bed->s->sizeof_phdr;
4855 1.1 christos }
4856 1.1 christos
4857 1.1 christos /* Find the segment that contains the output_section of section. */
4858 1.1 christos
4859 1.1 christos Elf_Internal_Phdr *
4860 1.1 christos _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4861 1.1 christos {
4862 1.1 christos struct elf_segment_map *m;
4863 1.3 christos Elf_Internal_Phdr *p;
4864 1.1 christos
4865 1.1 christos for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4866 1.1 christos m != NULL;
4867 1.1 christos m = m->next, p++)
4868 1.1 christos {
4869 1.1 christos int i;
4870 1.1 christos
4871 1.1 christos for (i = m->count - 1; i >= 0; i--)
4872 1.1 christos if (m->sections[i] == section)
4873 1.1 christos return p;
4874 1.1 christos }
4875 1.1 christos
4876 1.1 christos return NULL;
4877 1.1 christos }
4878 1.1 christos
4879 1.1 christos /* Create a mapping from a set of sections to a program segment. */
4880 1.1 christos
4881 1.1 christos static struct elf_segment_map *
4882 1.1 christos make_mapping (bfd *abfd,
4883 1.1 christos asection **sections,
4884 1.14 christos unsigned int from,
4885 1.1 christos unsigned int to,
4886 1.1 christos bool phdr)
4887 1.1 christos {
4888 1.1 christos struct elf_segment_map *m;
4889 1.12 christos unsigned int i;
4890 1.1 christos asection **hdrpp;
4891 1.11 christos size_t amt;
4892 1.11 christos
4893 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4894 1.1 christos amt += (to - from) * sizeof (asection *);
4895 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4896 1.1 christos if (m == NULL)
4897 1.1 christos return NULL;
4898 1.1 christos m->next = NULL;
4899 1.1 christos m->p_type = PT_LOAD;
4900 1.1 christos for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4901 1.1 christos m->sections[i - from] = *hdrpp;
4902 1.1 christos m->count = to - from;
4903 1.1 christos
4904 1.1 christos if (from == 0 && phdr)
4905 1.1 christos {
4906 1.1 christos /* Include the headers in the first PT_LOAD segment. */
4907 1.1 christos m->includes_filehdr = 1;
4908 1.1 christos m->includes_phdrs = 1;
4909 1.1 christos }
4910 1.1 christos
4911 1.1 christos return m;
4912 1.1 christos }
4913 1.1 christos
4914 1.1 christos /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4915 1.1 christos on failure. */
4916 1.1 christos
4917 1.1 christos struct elf_segment_map *
4918 1.1 christos _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4919 1.1 christos {
4920 1.1 christos struct elf_segment_map *m;
4921 1.11 christos
4922 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd,
4923 1.1 christos sizeof (struct elf_segment_map));
4924 1.1 christos if (m == NULL)
4925 1.1 christos return NULL;
4926 1.1 christos m->next = NULL;
4927 1.1 christos m->p_type = PT_DYNAMIC;
4928 1.1 christos m->count = 1;
4929 1.1 christos m->sections[0] = dynsec;
4930 1.1 christos
4931 1.1 christos return m;
4932 1.1 christos }
4933 1.1 christos
4934 1.14 christos /* Possibly add or remove segments from the segment map. */
4935 1.1 christos
4936 1.1 christos static bool
4937 1.14 christos elf_modify_segment_map (bfd *abfd,
4938 1.1 christos struct bfd_link_info *info,
4939 1.1 christos bool remove_empty_load)
4940 1.1 christos {
4941 1.1 christos struct elf_segment_map **m;
4942 1.1 christos const struct elf_backend_data *bed;
4943 1.1 christos
4944 1.1 christos /* The placement algorithm assumes that non allocated sections are
4945 1.1 christos not in PT_LOAD segments. We ensure this here by removing such
4946 1.1 christos sections from the segment map. We also remove excluded
4947 1.3 christos sections. Finally, any PT_LOAD segment without sections is
4948 1.1 christos removed. */
4949 1.1 christos m = &elf_seg_map (abfd);
4950 1.1 christos while (*m)
4951 1.1 christos {
4952 1.1 christos unsigned int i, new_count;
4953 1.1 christos
4954 1.1 christos for (new_count = 0, i = 0; i < (*m)->count; i++)
4955 1.1 christos {
4956 1.1 christos if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4957 1.1 christos && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4958 1.1 christos || (*m)->p_type != PT_LOAD))
4959 1.1 christos {
4960 1.1 christos (*m)->sections[new_count] = (*m)->sections[i];
4961 1.1 christos new_count++;
4962 1.1 christos }
4963 1.1 christos }
4964 1.9 christos (*m)->count = new_count;
4965 1.9 christos
4966 1.9 christos if (remove_empty_load
4967 1.9 christos && (*m)->p_type == PT_LOAD
4968 1.1 christos && (*m)->count == 0
4969 1.1 christos && !(*m)->includes_phdrs)
4970 1.1 christos *m = (*m)->next;
4971 1.1 christos else
4972 1.1 christos m = &(*m)->next;
4973 1.1 christos }
4974 1.1 christos
4975 1.1 christos bed = get_elf_backend_data (abfd);
4976 1.1 christos if (bed->elf_backend_modify_segment_map != NULL)
4977 1.14 christos {
4978 1.1 christos if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4979 1.1 christos return false;
4980 1.14 christos }
4981 1.1 christos
4982 1.1 christos return true;
4983 1.11 christos }
4984 1.11 christos
4985 1.11 christos #define IS_TBSS(s) \
4986 1.14 christos ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4987 1.14 christos
4988 1.1 christos /* Set up a mapping from BFD sections to program segments. Update
4989 1.14 christos NEED_LAYOUT if the section layout is changed. */
4990 1.14 christos
4991 1.14 christos bool
4992 1.14 christos _bfd_elf_map_sections_to_segments (bfd *abfd,
4993 1.1 christos struct bfd_link_info *info,
4994 1.1 christos bool *need_layout)
4995 1.1 christos {
4996 1.1 christos unsigned int count;
4997 1.1 christos struct elf_segment_map *m;
4998 1.14 christos asection **sections = NULL;
4999 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5000 1.3 christos bool no_user_phdrs;
5001 1.3 christos
5002 1.3 christos no_user_phdrs = elf_seg_map (abfd) == NULL;
5003 1.14 christos
5004 1.14 christos if (info != NULL)
5005 1.14 christos {
5006 1.14 christos info->user_phdrs = !no_user_phdrs;
5007 1.14 christos
5008 1.14 christos /* Size the relative relocations if DT_RELR is enabled. */
5009 1.14 christos if (info->enable_dt_relr
5010 1.14 christos && need_layout != NULL
5011 1.14 christos && bed->size_relative_relocs
5012 1.14 christos && !bed->size_relative_relocs (info, need_layout))
5013 1.14 christos info->callbacks->einfo
5014 1.3 christos (_("%F%P: failed to size relative relocations\n"));
5015 1.1 christos }
5016 1.1 christos
5017 1.1 christos if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5018 1.1 christos {
5019 1.1 christos asection *s;
5020 1.1 christos unsigned int i;
5021 1.1 christos struct elf_segment_map *mfirst;
5022 1.1 christos struct elf_segment_map **pm;
5023 1.11 christos asection *last_hdr;
5024 1.1 christos bfd_vma last_size;
5025 1.1 christos unsigned int hdr_index;
5026 1.14 christos bfd_vma maxpagesize;
5027 1.14 christos asection **hdrpp;
5028 1.14 christos bool phdr_in_segment;
5029 1.12 christos bool writable;
5030 1.1 christos bool executable;
5031 1.9 christos unsigned int tls_count = 0;
5032 1.1 christos asection *first_tls = NULL;
5033 1.14 christos asection *first_mbind = NULL;
5034 1.12 christos asection *dynsec, *eh_frame_hdr;
5035 1.12 christos asection *sframe;
5036 1.12 christos size_t amt;
5037 1.12 christos bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5038 1.1 christos bfd_size_type phdr_size; /* Octets/bytes. */
5039 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5040 1.1 christos
5041 1.12 christos /* Select the allocated sections, and sort them. */
5042 1.12 christos
5043 1.1 christos amt = bfd_count_sections (abfd) * sizeof (asection *);
5044 1.1 christos sections = (asection **) bfd_malloc (amt);
5045 1.1 christos if (sections == NULL)
5046 1.1 christos goto error_return;
5047 1.1 christos
5048 1.1 christos /* Calculate top address, avoiding undefined behaviour of shift
5049 1.1 christos left operator when shift count is equal to size of type
5050 1.1 christos being shifted. */
5051 1.1 christos addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5052 1.1 christos addr_mask = (addr_mask << 1) + 1;
5053 1.1 christos
5054 1.1 christos i = 0;
5055 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
5056 1.1 christos {
5057 1.12 christos if ((s->flags & SEC_ALLOC) != 0)
5058 1.12 christos {
5059 1.12 christos /* target_index is unused until bfd_elf_final_link
5060 1.12 christos starts output of section symbols. Use it to make
5061 1.1 christos qsort stable. */
5062 1.1 christos s->target_index = i;
5063 1.1 christos sections[i] = s;
5064 1.12 christos ++i;
5065 1.12 christos /* A wrapping section potentially clashes with header. */
5066 1.1 christos if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5067 1.1 christos wrap_to = (s->lma + s->size / opb) & addr_mask;
5068 1.1 christos }
5069 1.1 christos }
5070 1.1 christos BFD_ASSERT (i <= bfd_count_sections (abfd));
5071 1.1 christos count = i;
5072 1.1 christos
5073 1.11 christos qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5074 1.11 christos
5075 1.11 christos phdr_size = elf_program_header_size (abfd);
5076 1.11 christos if (phdr_size == (bfd_size_type) -1)
5077 1.12 christos phdr_size = get_program_header_size (abfd, info);
5078 1.12 christos phdr_size += bed->s->sizeof_ehdr;
5079 1.14 christos /* phdr_size is compared to LMA values which are in bytes. */
5080 1.14 christos phdr_size /= opb;
5081 1.14 christos if (info != NULL)
5082 1.14 christos maxpagesize = info->maxpagesize;
5083 1.11 christos else
5084 1.11 christos maxpagesize = bed->maxpagesize;
5085 1.11 christos if (maxpagesize == 0)
5086 1.11 christos maxpagesize = 1;
5087 1.11 christos phdr_in_segment = info != NULL && info->load_phdrs;
5088 1.11 christos if (count != 0
5089 1.11 christos && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5090 1.11 christos >= (phdr_size & (maxpagesize - 1))))
5091 1.11 christos /* For compatibility with old scripts that may not be using
5092 1.14 christos SIZEOF_HEADERS, add headers when it looks like space has
5093 1.11 christos been left for them. */
5094 1.1 christos phdr_in_segment = true;
5095 1.1 christos
5096 1.1 christos /* Build the mapping. */
5097 1.1 christos mfirst = NULL;
5098 1.1 christos pm = &mfirst;
5099 1.1 christos
5100 1.1 christos /* If we have a .interp section, then create a PT_PHDR segment for
5101 1.1 christos the program headers and a PT_INTERP segment for the .interp
5102 1.11 christos section. */
5103 1.1 christos s = bfd_get_section_by_name (abfd, ".interp");
5104 1.1 christos if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5105 1.1 christos {
5106 1.1 christos amt = sizeof (struct elf_segment_map);
5107 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5108 1.1 christos if (m == NULL)
5109 1.1 christos goto error_return;
5110 1.11 christos m->next = NULL;
5111 1.1 christos m->p_type = PT_PHDR;
5112 1.1 christos m->p_flags = PF_R;
5113 1.14 christos m->p_flags_valid = 1;
5114 1.1 christos m->includes_phdrs = 1;
5115 1.1 christos phdr_in_segment = true;
5116 1.1 christos *pm = m;
5117 1.1 christos pm = &m->next;
5118 1.1 christos
5119 1.1 christos amt = sizeof (struct elf_segment_map);
5120 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5121 1.1 christos if (m == NULL)
5122 1.1 christos goto error_return;
5123 1.1 christos m->next = NULL;
5124 1.1 christos m->p_type = PT_INTERP;
5125 1.1 christos m->count = 1;
5126 1.1 christos m->sections[0] = s;
5127 1.1 christos
5128 1.1 christos *pm = m;
5129 1.1 christos pm = &m->next;
5130 1.1 christos }
5131 1.1 christos
5132 1.1 christos /* Look through the sections. We put sections in the same program
5133 1.1 christos segment when the start of the second section can be placed within
5134 1.1 christos a few bytes of the end of the first section. */
5135 1.11 christos last_hdr = NULL;
5136 1.14 christos last_size = 0;
5137 1.14 christos hdr_index = 0;
5138 1.1 christos writable = false;
5139 1.1 christos executable = false;
5140 1.1 christos dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5141 1.1 christos if (dynsec != NULL
5142 1.1 christos && (dynsec->flags & SEC_LOAD) == 0)
5143 1.11 christos dynsec = NULL;
5144 1.14 christos
5145 1.11 christos if ((abfd->flags & D_PAGED) == 0)
5146 1.1 christos phdr_in_segment = false;
5147 1.1 christos
5148 1.1 christos /* Deal with -Ttext or something similar such that the first section
5149 1.1 christos is not adjacent to the program headers. This is an
5150 1.11 christos approximation, since at this point we don't know exactly how many
5151 1.1 christos program headers we will need. */
5152 1.12 christos if (phdr_in_segment && count > 0)
5153 1.14 christos {
5154 1.1 christos bfd_vma phdr_lma; /* Bytes. */
5155 1.11 christos bool separate_phdr = false;
5156 1.11 christos
5157 1.11 christos phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5158 1.11 christos if (info != NULL
5159 1.11 christos && info->separate_code
5160 1.11 christos && (sections[0]->flags & SEC_CODE) != 0)
5161 1.11 christos {
5162 1.11 christos /* If data sections should be separate from code and
5163 1.11 christos thus not executable, and the first section is
5164 1.18 christos executable then put the file and program headers in
5165 1.18 christos their own PT_LOAD. */
5166 1.18 christos if (!info->one_rosegment)
5167 1.11 christos separate_phdr = true;
5168 1.11 christos
5169 1.11 christos if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5170 1.11 christos == (sections[0]->lma & addr_mask & -maxpagesize)))
5171 1.11 christos {
5172 1.11 christos /* The file and program headers are currently on the
5173 1.11 christos same page as the first section. Put them on the
5174 1.11 christos previous page if we can. */
5175 1.11 christos if (phdr_lma >= maxpagesize)
5176 1.14 christos phdr_lma -= maxpagesize;
5177 1.11 christos else
5178 1.11 christos separate_phdr = false;
5179 1.11 christos }
5180 1.11 christos }
5181 1.11 christos if ((sections[0]->lma & addr_mask) < phdr_lma
5182 1.11 christos || (sections[0]->lma & addr_mask) < phdr_size)
5183 1.14 christos /* If file and program headers would be placed at the end
5184 1.11 christos of memory then it's probably better to omit them. */
5185 1.11 christos phdr_in_segment = false;
5186 1.11 christos else if (phdr_lma < wrap_to)
5187 1.11 christos /* If a section wraps around to where we'll be placing
5188 1.14 christos file and program headers, then the headers will be
5189 1.11 christos overwritten. */
5190 1.11 christos phdr_in_segment = false;
5191 1.11 christos else if (separate_phdr)
5192 1.11 christos {
5193 1.11 christos m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5194 1.12 christos if (m == NULL)
5195 1.11 christos goto error_return;
5196 1.11 christos m->p_paddr = phdr_lma * opb;
5197 1.11 christos m->p_vaddr_offset
5198 1.11 christos = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5199 1.11 christos m->p_paddr_valid = 1;
5200 1.14 christos *pm = m;
5201 1.9 christos pm = &m->next;
5202 1.1 christos phdr_in_segment = false;
5203 1.1 christos }
5204 1.1 christos }
5205 1.1 christos
5206 1.1 christos for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5207 1.14 christos {
5208 1.1 christos asection *hdr;
5209 1.1 christos bool new_segment;
5210 1.1 christos
5211 1.1 christos hdr = *hdrpp;
5212 1.1 christos
5213 1.1 christos /* See if this section and the last one will fit in the same
5214 1.1 christos segment. */
5215 1.1 christos
5216 1.1 christos if (last_hdr == NULL)
5217 1.1 christos {
5218 1.14 christos /* If we don't have a segment yet, then we don't need a new
5219 1.1 christos one (we build the last one after this loop). */
5220 1.1 christos new_segment = false;
5221 1.1 christos }
5222 1.1 christos else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5223 1.1 christos {
5224 1.1 christos /* If this section has a different relation between the
5225 1.14 christos virtual address and the load address, then we need a new
5226 1.1 christos segment. */
5227 1.1 christos new_segment = true;
5228 1.1 christos }
5229 1.1 christos else if (hdr->lma < last_hdr->lma + last_size
5230 1.1 christos || last_hdr->lma + last_size < last_hdr->lma)
5231 1.1 christos {
5232 1.14 christos /* If this section has a load address that makes it overlap
5233 1.1 christos the previous section, then we need a new segment. */
5234 1.11 christos new_segment = true;
5235 1.11 christos }
5236 1.11 christos else if ((abfd->flags & D_PAGED) != 0
5237 1.11 christos && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5238 1.11 christos == (hdr->lma & -maxpagesize)))
5239 1.11 christos {
5240 1.14 christos /* If we are demand paged then we can't map two disk
5241 1.11 christos pages onto the same memory page. */
5242 1.1 christos new_segment = false;
5243 1.1 christos }
5244 1.1 christos /* In the next test we have to be careful when last_hdr->lma is close
5245 1.1 christos to the end of the address space. If the aligned address wraps
5246 1.1 christos around to the start of the address space, then there are no more
5247 1.11 christos pages left in memory and it is OK to assume that the current
5248 1.11 christos section can be included in the current segment. */
5249 1.11 christos else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5250 1.11 christos + maxpagesize > last_hdr->lma)
5251 1.1 christos && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5252 1.1 christos + maxpagesize <= hdr->lma))
5253 1.1 christos {
5254 1.14 christos /* If putting this section in this segment would force us to
5255 1.1 christos skip a page in the segment, then we need a new segment. */
5256 1.1 christos new_segment = true;
5257 1.11 christos }
5258 1.8 christos else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5259 1.8 christos && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5260 1.8 christos {
5261 1.8 christos /* We don't want to put a loaded section after a
5262 1.11 christos nonloaded (ie. bss style) section in the same segment
5263 1.14 christos as that will force the non-loaded section to be loaded.
5264 1.1 christos Consider .tbss sections as loaded for this purpose. */
5265 1.1 christos new_segment = true;
5266 1.1 christos }
5267 1.1 christos else if ((abfd->flags & D_PAGED) == 0)
5268 1.1 christos {
5269 1.1 christos /* If the file is not demand paged, which means that we
5270 1.14 christos don't require the sections to be correctly aligned in the
5271 1.1 christos file, then there is no other reason for a new segment. */
5272 1.11 christos new_segment = false;
5273 1.11 christos }
5274 1.11 christos else if (info != NULL
5275 1.11 christos && info->separate_code
5276 1.14 christos && executable != ((hdr->flags & SEC_CODE) != 0))
5277 1.11 christos {
5278 1.1 christos new_segment = true;
5279 1.11 christos }
5280 1.1 christos else if (! writable
5281 1.1 christos && (hdr->flags & SEC_READONLY) == 0)
5282 1.11 christos {
5283 1.14 christos /* We don't want to put a writable section in a read only
5284 1.1 christos segment. */
5285 1.1 christos new_segment = true;
5286 1.1 christos }
5287 1.1 christos else
5288 1.14 christos {
5289 1.1 christos /* Otherwise, we can use the same segment. */
5290 1.1 christos new_segment = false;
5291 1.1 christos }
5292 1.1 christos
5293 1.1 christos /* Allow interested parties a chance to override our decision. */
5294 1.1 christos if (last_hdr != NULL
5295 1.1 christos && info != NULL
5296 1.1 christos && info->callbacks->override_segment_assignment != NULL)
5297 1.1 christos new_segment
5298 1.1 christos = info->callbacks->override_segment_assignment (info, abfd, hdr,
5299 1.1 christos last_hdr,
5300 1.1 christos new_segment);
5301 1.1 christos
5302 1.1 christos if (! new_segment)
5303 1.14 christos {
5304 1.11 christos if ((hdr->flags & SEC_READONLY) == 0)
5305 1.14 christos writable = true;
5306 1.1 christos if ((hdr->flags & SEC_CODE) != 0)
5307 1.1 christos executable = true;
5308 1.12 christos last_hdr = hdr;
5309 1.1 christos /* .tbss sections effectively have zero size. */
5310 1.1 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5311 1.1 christos continue;
5312 1.1 christos }
5313 1.11 christos
5314 1.1 christos /* We need a new program segment. We must create a new program
5315 1.11 christos header holding all the sections from hdr_index until hdr. */
5316 1.1 christos
5317 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5318 1.1 christos if (m == NULL)
5319 1.1 christos goto error_return;
5320 1.1 christos
5321 1.1 christos *pm = m;
5322 1.1 christos pm = &m->next;
5323 1.14 christos
5324 1.1 christos if ((hdr->flags & SEC_READONLY) == 0)
5325 1.14 christos writable = true;
5326 1.1 christos else
5327 1.11 christos writable = false;
5328 1.14 christos
5329 1.11 christos if ((hdr->flags & SEC_CODE) == 0)
5330 1.14 christos executable = false;
5331 1.11 christos else
5332 1.1 christos executable = true;
5333 1.1 christos
5334 1.12 christos last_hdr = hdr;
5335 1.11 christos /* .tbss sections effectively have zero size. */
5336 1.14 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5337 1.1 christos hdr_index = i;
5338 1.1 christos phdr_in_segment = false;
5339 1.3 christos }
5340 1.3 christos
5341 1.3 christos /* Create a final PT_LOAD program segment, but not if it's just
5342 1.11 christos for .tbss. */
5343 1.11 christos if (last_hdr != NULL
5344 1.1 christos && (i - hdr_index != 1
5345 1.11 christos || !IS_TBSS (last_hdr)))
5346 1.1 christos {
5347 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5348 1.1 christos if (m == NULL)
5349 1.1 christos goto error_return;
5350 1.1 christos
5351 1.1 christos *pm = m;
5352 1.1 christos pm = &m->next;
5353 1.1 christos }
5354 1.1 christos
5355 1.1 christos /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5356 1.1 christos if (dynsec != NULL)
5357 1.1 christos {
5358 1.1 christos m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5359 1.1 christos if (m == NULL)
5360 1.1 christos goto error_return;
5361 1.1 christos *pm = m;
5362 1.1 christos pm = &m->next;
5363 1.11 christos }
5364 1.1 christos
5365 1.1 christos /* For each batch of consecutive loadable SHT_NOTE sections,
5366 1.1 christos add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5367 1.11 christos because if we link together nonloadable .note sections and
5368 1.1 christos loadable .note sections, we will generate two .note sections
5369 1.1 christos in the output file. */
5370 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
5371 1.11 christos {
5372 1.1 christos if ((s->flags & SEC_LOAD) != 0
5373 1.1 christos && elf_section_type (s) == SHT_NOTE)
5374 1.11 christos {
5375 1.1 christos asection *s2;
5376 1.1 christos unsigned int alignment_power = s->alignment_power;
5377 1.11 christos
5378 1.11 christos count = 1;
5379 1.11 christos for (s2 = s; s2->next != NULL; s2 = s2->next)
5380 1.11 christos {
5381 1.11 christos if (s2->next->alignment_power == alignment_power
5382 1.12 christos && (s2->next->flags & SEC_LOAD) != 0
5383 1.11 christos && elf_section_type (s2->next) == SHT_NOTE
5384 1.11 christos && align_power (s2->lma + s2->size / opb,
5385 1.11 christos alignment_power)
5386 1.11 christos == s2->next->lma)
5387 1.11 christos count++;
5388 1.11 christos else
5389 1.11 christos break;
5390 1.11 christos }
5391 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5392 1.1 christos amt += count * sizeof (asection *);
5393 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5394 1.1 christos if (m == NULL)
5395 1.1 christos goto error_return;
5396 1.1 christos m->next = NULL;
5397 1.1 christos m->p_type = PT_NOTE;
5398 1.1 christos m->count = count;
5399 1.1 christos while (count > 1)
5400 1.1 christos {
5401 1.1 christos m->sections[m->count - count--] = s;
5402 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5403 1.1 christos s = s->next;
5404 1.1 christos }
5405 1.1 christos m->sections[m->count - 1] = s;
5406 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5407 1.1 christos *pm = m;
5408 1.1 christos pm = &m->next;
5409 1.1 christos }
5410 1.1 christos if (s->flags & SEC_THREAD_LOCAL)
5411 1.1 christos {
5412 1.1 christos if (! tls_count)
5413 1.1 christos first_tls = s;
5414 1.9 christos tls_count++;
5415 1.9 christos }
5416 1.9 christos if (first_mbind == NULL
5417 1.1 christos && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5418 1.1 christos first_mbind = s;
5419 1.1 christos }
5420 1.1 christos
5421 1.1 christos /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5422 1.11 christos if (tls_count > 0)
5423 1.11 christos {
5424 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5425 1.1 christos amt += tls_count * sizeof (asection *);
5426 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5427 1.1 christos if (m == NULL)
5428 1.1 christos goto error_return;
5429 1.1 christos m->next = NULL;
5430 1.1 christos m->p_type = PT_TLS;
5431 1.1 christos m->count = tls_count;
5432 1.1 christos /* Mandated PF_R. */
5433 1.5 christos m->p_flags = PF_R;
5434 1.12 christos m->p_flags_valid = 1;
5435 1.1 christos s = first_tls;
5436 1.5 christos for (i = 0; i < tls_count; ++i)
5437 1.5 christos {
5438 1.5 christos if ((s->flags & SEC_THREAD_LOCAL) == 0)
5439 1.11 christos {
5440 1.5 christos _bfd_error_handler
5441 1.5 christos (_("%pB: TLS sections are not adjacent:"), abfd);
5442 1.12 christos s = first_tls;
5443 1.5 christos i = 0;
5444 1.5 christos while (i < tls_count)
5445 1.5 christos {
5446 1.11 christos if ((s->flags & SEC_THREAD_LOCAL) != 0)
5447 1.5 christos {
5448 1.5 christos _bfd_error_handler (_(" TLS: %pA"), s);
5449 1.5 christos i++;
5450 1.11 christos }
5451 1.5 christos else
5452 1.5 christos _bfd_error_handler (_(" non-TLS: %pA"), s);
5453 1.5 christos s = s->next;
5454 1.5 christos }
5455 1.5 christos bfd_set_error (bfd_error_bad_value);
5456 1.5 christos goto error_return;
5457 1.5 christos }
5458 1.1 christos m->sections[i] = s;
5459 1.1 christos s = s->next;
5460 1.1 christos }
5461 1.1 christos
5462 1.1 christos *pm = m;
5463 1.1 christos pm = &m->next;
5464 1.12 christos }
5465 1.12 christos
5466 1.12 christos if (first_mbind
5467 1.9 christos && (abfd->flags & D_PAGED) != 0
5468 1.9 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5469 1.12 christos for (s = first_mbind; s != NULL; s = s->next)
5470 1.9 christos if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5471 1.9 christos && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5472 1.9 christos {
5473 1.9 christos /* Mandated PF_R. */
5474 1.9 christos unsigned long p_flags = PF_R;
5475 1.9 christos if ((s->flags & SEC_READONLY) == 0)
5476 1.9 christos p_flags |= PF_W;
5477 1.9 christos if ((s->flags & SEC_CODE) != 0)
5478 1.9 christos p_flags |= PF_X;
5479 1.9 christos
5480 1.9 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5481 1.9 christos m = bfd_zalloc (abfd, amt);
5482 1.9 christos if (m == NULL)
5483 1.9 christos goto error_return;
5484 1.9 christos m->next = NULL;
5485 1.9 christos m->p_type = (PT_GNU_MBIND_LO
5486 1.9 christos + elf_section_data (s)->this_hdr.sh_info);
5487 1.9 christos m->count = 1;
5488 1.9 christos m->p_flags_valid = 1;
5489 1.9 christos m->sections[0] = s;
5490 1.9 christos m->p_flags = p_flags;
5491 1.9 christos
5492 1.9 christos *pm = m;
5493 1.9 christos pm = &m->next;
5494 1.11 christos }
5495 1.11 christos
5496 1.11 christos s = bfd_get_section_by_name (abfd,
5497 1.11 christos NOTE_GNU_PROPERTY_SECTION_NAME);
5498 1.11 christos if (s != NULL && s->size != 0)
5499 1.11 christos {
5500 1.11 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5501 1.11 christos m = bfd_zalloc (abfd, amt);
5502 1.11 christos if (m == NULL)
5503 1.11 christos goto error_return;
5504 1.11 christos m->next = NULL;
5505 1.11 christos m->p_type = PT_GNU_PROPERTY;
5506 1.11 christos m->count = 1;
5507 1.11 christos m->p_flags_valid = 1;
5508 1.11 christos m->sections[0] = s;
5509 1.11 christos m->p_flags = PF_R;
5510 1.11 christos *pm = m;
5511 1.11 christos pm = &m->next;
5512 1.1 christos }
5513 1.1 christos
5514 1.14 christos /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5515 1.1 christos segment. */
5516 1.1 christos eh_frame_hdr = elf_eh_frame_hdr (info);
5517 1.1 christos if (eh_frame_hdr != NULL
5518 1.1 christos && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5519 1.1 christos {
5520 1.1 christos amt = sizeof (struct elf_segment_map);
5521 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5522 1.1 christos if (m == NULL)
5523 1.1 christos goto error_return;
5524 1.1 christos m->next = NULL;
5525 1.1 christos m->p_type = PT_GNU_EH_FRAME;
5526 1.1 christos m->count = 1;
5527 1.1 christos m->sections[0] = eh_frame_hdr->output_section;
5528 1.1 christos
5529 1.1 christos *pm = m;
5530 1.1 christos pm = &m->next;
5531 1.14 christos }
5532 1.14 christos
5533 1.14 christos /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5534 1.14 christos segment. */
5535 1.14 christos sframe = elf_sframe (abfd);
5536 1.14 christos if (sframe != NULL
5537 1.14 christos && (sframe->output_section->flags & SEC_LOAD) != 0
5538 1.14 christos && sframe->size != 0)
5539 1.14 christos {
5540 1.14 christos amt = sizeof (struct elf_segment_map);
5541 1.14 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5542 1.14 christos if (m == NULL)
5543 1.14 christos goto error_return;
5544 1.14 christos m->next = NULL;
5545 1.14 christos m->p_type = PT_GNU_SFRAME;
5546 1.14 christos m->count = 1;
5547 1.14 christos m->sections[0] = sframe->output_section;
5548 1.14 christos
5549 1.14 christos *pm = m;
5550 1.14 christos pm = &m->next;
5551 1.3 christos }
5552 1.1 christos
5553 1.1 christos if (elf_stack_flags (abfd))
5554 1.1 christos {
5555 1.1 christos amt = sizeof (struct elf_segment_map);
5556 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5557 1.1 christos if (m == NULL)
5558 1.1 christos goto error_return;
5559 1.3 christos m->next = NULL;
5560 1.3 christos m->p_type = PT_GNU_STACK;
5561 1.1 christos m->p_flags = elf_stack_flags (abfd);
5562 1.3 christos m->p_align = bed->stack_align;
5563 1.3 christos m->p_flags_valid = 1;
5564 1.3 christos m->p_align_valid = m->p_align != 0;
5565 1.3 christos if (info->stacksize > 0)
5566 1.3 christos {
5567 1.3 christos m->p_size = info->stacksize;
5568 1.1 christos m->p_size_valid = 1;
5569 1.1 christos }
5570 1.1 christos
5571 1.1 christos *pm = m;
5572 1.1 christos pm = &m->next;
5573 1.1 christos }
5574 1.1 christos
5575 1.1 christos if (info != NULL && info->relro)
5576 1.1 christos {
5577 1.3 christos for (m = mfirst; m != NULL; m = m->next)
5578 1.3 christos {
5579 1.3 christos if (m->p_type == PT_LOAD
5580 1.3 christos && m->count != 0
5581 1.1 christos && m->sections[0]->vma >= info->relro_start
5582 1.3 christos && m->sections[0]->vma < info->relro_end)
5583 1.3 christos {
5584 1.12 christos i = m->count;
5585 1.12 christos while (--i != (unsigned) -1)
5586 1.14 christos {
5587 1.14 christos if (m->sections[i]->size > 0
5588 1.12 christos && (m->sections[i]->flags & SEC_LOAD) != 0
5589 1.12 christos && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5590 1.3 christos break;
5591 1.5 christos }
5592 1.1 christos
5593 1.1 christos if (i != (unsigned) -1)
5594 1.3 christos break;
5595 1.1 christos }
5596 1.1 christos }
5597 1.1 christos
5598 1.1 christos /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5599 1.1 christos if (m != NULL)
5600 1.1 christos {
5601 1.1 christos amt = sizeof (struct elf_segment_map);
5602 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5603 1.1 christos if (m == NULL)
5604 1.1 christos goto error_return;
5605 1.1 christos m->next = NULL;
5606 1.1 christos m->p_type = PT_GNU_RELRO;
5607 1.1 christos *pm = m;
5608 1.1 christos pm = &m->next;
5609 1.1 christos }
5610 1.1 christos }
5611 1.3 christos
5612 1.1 christos free (sections);
5613 1.1 christos elf_seg_map (abfd) = mfirst;
5614 1.17 christos }
5615 1.14 christos
5616 1.1 christos if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5617 1.3 christos return false;
5618 1.1 christos
5619 1.3 christos for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5620 1.1 christos ++count;
5621 1.14 christos elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5622 1.1 christos
5623 1.1 christos return true;
5624 1.12 christos
5625 1.14 christos error_return:
5626 1.1 christos free (sections);
5627 1.1 christos return false;
5628 1.1 christos }
5629 1.1 christos
5630 1.1 christos /* Sort sections by address. */
5631 1.1 christos
5632 1.1 christos static int
5633 1.1 christos elf_sort_sections (const void *arg1, const void *arg2)
5634 1.1 christos {
5635 1.1 christos const asection *sec1 = *(const asection **) arg1;
5636 1.1 christos const asection *sec2 = *(const asection **) arg2;
5637 1.1 christos bfd_size_type size1, size2;
5638 1.1 christos
5639 1.1 christos /* Sort by LMA first, since this is the address used to
5640 1.1 christos place the section into a segment. */
5641 1.1 christos if (sec1->lma < sec2->lma)
5642 1.1 christos return -1;
5643 1.1 christos else if (sec1->lma > sec2->lma)
5644 1.1 christos return 1;
5645 1.1 christos
5646 1.1 christos /* Then sort by VMA. Normally the LMA and the VMA will be
5647 1.1 christos the same, and this will do nothing. */
5648 1.1 christos if (sec1->vma < sec2->vma)
5649 1.1 christos return -1;
5650 1.1 christos else if (sec1->vma > sec2->vma)
5651 1.1 christos return 1;
5652 1.1 christos
5653 1.14 christos /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5654 1.14 christos
5655 1.1 christos #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5656 1.1 christos && (x)->size != 0)
5657 1.1 christos
5658 1.12 christos if (TOEND (sec1))
5659 1.1 christos {
5660 1.1 christos if (!TOEND (sec2))
5661 1.1 christos return 1;
5662 1.1 christos }
5663 1.1 christos else if (TOEND (sec2))
5664 1.1 christos return -1;
5665 1.1 christos
5666 1.1 christos #undef TOEND
5667 1.1 christos
5668 1.1 christos /* Sort by size, to put zero sized sections
5669 1.1 christos before others at the same address. */
5670 1.1 christos
5671 1.1 christos size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5672 1.1 christos size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5673 1.1 christos
5674 1.1 christos if (size1 < size2)
5675 1.1 christos return -1;
5676 1.1 christos if (size1 > size2)
5677 1.1 christos return 1;
5678 1.1 christos
5679 1.1 christos return sec1->target_index - sec2->target_index;
5680 1.12 christos }
5681 1.12 christos
5682 1.12 christos /* This qsort comparison functions sorts PT_LOAD segments first and
5683 1.12 christos by p_paddr, for assign_file_positions_for_load_sections. */
5684 1.12 christos
5685 1.12 christos static int
5686 1.12 christos elf_sort_segments (const void *arg1, const void *arg2)
5687 1.12 christos {
5688 1.12 christos const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5689 1.12 christos const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5690 1.12 christos
5691 1.12 christos if (m1->p_type != m2->p_type)
5692 1.12 christos {
5693 1.12 christos if (m1->p_type == PT_NULL)
5694 1.12 christos return 1;
5695 1.12 christos if (m2->p_type == PT_NULL)
5696 1.12 christos return -1;
5697 1.12 christos return m1->p_type < m2->p_type ? -1 : 1;
5698 1.12 christos }
5699 1.12 christos if (m1->includes_filehdr != m2->includes_filehdr)
5700 1.12 christos return m1->includes_filehdr ? -1 : 1;
5701 1.12 christos if (m1->no_sort_lma != m2->no_sort_lma)
5702 1.12 christos return m1->no_sort_lma ? -1 : 1;
5703 1.12 christos if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5704 1.12 christos {
5705 1.12 christos bfd_vma lma1, lma2; /* Octets. */
5706 1.12 christos lma1 = 0;
5707 1.12 christos if (m1->p_paddr_valid)
5708 1.12 christos lma1 = m1->p_paddr;
5709 1.12 christos else if (m1->count != 0)
5710 1.12 christos {
5711 1.12 christos unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5712 1.12 christos m1->sections[0]);
5713 1.12 christos lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5714 1.12 christos }
5715 1.12 christos lma2 = 0;
5716 1.12 christos if (m2->p_paddr_valid)
5717 1.12 christos lma2 = m2->p_paddr;
5718 1.12 christos else if (m2->count != 0)
5719 1.12 christos {
5720 1.12 christos unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5721 1.12 christos m2->sections[0]);
5722 1.12 christos lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5723 1.12 christos }
5724 1.12 christos if (lma1 != lma2)
5725 1.12 christos return lma1 < lma2 ? -1 : 1;
5726 1.12 christos }
5727 1.12 christos if (m1->idx != m2->idx)
5728 1.12 christos return m1->idx < m2->idx ? -1 : 1;
5729 1.12 christos return 0;
5730 1.1 christos }
5731 1.1 christos
5732 1.1 christos /* Ian Lance Taylor writes:
5733 1.1 christos
5734 1.1 christos We shouldn't be using % with a negative signed number. That's just
5735 1.1 christos not good. We have to make sure either that the number is not
5736 1.1 christos negative, or that the number has an unsigned type. When the types
5737 1.1 christos are all the same size they wind up as unsigned. When file_ptr is a
5738 1.1 christos larger signed type, the arithmetic winds up as signed long long,
5739 1.1 christos which is wrong.
5740 1.1 christos
5741 1.1 christos What we're trying to say here is something like ``increase OFF by
5742 1.1 christos the least amount that will cause it to be equal to the VMA modulo
5743 1.1 christos the page size.'' */
5744 1.1 christos /* In other words, something like:
5745 1.1 christos
5746 1.1 christos vma_offset = m->sections[0]->vma % bed->maxpagesize;
5747 1.1 christos off_offset = off % bed->maxpagesize;
5748 1.1 christos if (vma_offset < off_offset)
5749 1.1 christos adjustment = vma_offset + bed->maxpagesize - off_offset;
5750 1.1 christos else
5751 1.11 christos adjustment = vma_offset - off_offset;
5752 1.1 christos
5753 1.1 christos which can be collapsed into the expression below. */
5754 1.1 christos
5755 1.1 christos static file_ptr
5756 1.4 christos vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5757 1.4 christos {
5758 1.4 christos /* PR binutils/16199: Handle an alignment of zero. */
5759 1.1 christos if (maxpagesize == 0)
5760 1.1 christos maxpagesize = 1;
5761 1.1 christos return ((vma - off) % maxpagesize);
5762 1.1 christos }
5763 1.1 christos
5764 1.1 christos static void
5765 1.1 christos print_segment_map (const struct elf_segment_map *m)
5766 1.1 christos {
5767 1.1 christos unsigned int j;
5768 1.1 christos const char *pt = get_segment_type (m->p_type);
5769 1.1 christos char buf[32];
5770 1.1 christos
5771 1.1 christos if (pt == NULL)
5772 1.1 christos {
5773 1.1 christos if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5774 1.1 christos sprintf (buf, "LOPROC+%7.7x",
5775 1.1 christos (unsigned int) (m->p_type - PT_LOPROC));
5776 1.1 christos else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5777 1.1 christos sprintf (buf, "LOOS+%7.7x",
5778 1.1 christos (unsigned int) (m->p_type - PT_LOOS));
5779 1.1 christos else
5780 1.1 christos snprintf (buf, sizeof (buf), "%8.8x",
5781 1.1 christos (unsigned int) m->p_type);
5782 1.1 christos pt = buf;
5783 1.1 christos }
5784 1.1 christos fflush (stdout);
5785 1.1 christos fprintf (stderr, "%s:", pt);
5786 1.1 christos for (j = 0; j < m->count; j++)
5787 1.1 christos fprintf (stderr, " %s", m->sections [j]->name);
5788 1.1 christos putc ('\n',stderr);
5789 1.1 christos fflush (stderr);
5790 1.1 christos }
5791 1.1 christos
5792 1.1 christos /* Assign file positions to the sections based on the mapping from
5793 1.1 christos sections to segments. This function also sets up some fields in
5794 1.14 christos the file header. */
5795 1.1 christos
5796 1.1 christos static bool
5797 1.1 christos assign_file_positions_for_load_sections (bfd *abfd,
5798 1.1 christos struct bfd_link_info *link_info)
5799 1.1 christos {
5800 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5801 1.1 christos struct elf_segment_map *m;
5802 1.1 christos struct elf_segment_map *phdr_load_seg;
5803 1.12 christos Elf_Internal_Phdr *phdrs;
5804 1.1 christos Elf_Internal_Phdr *p;
5805 1.12 christos file_ptr off; /* Octets. */
5806 1.1 christos bfd_size_type maxpagesize;
5807 1.12 christos unsigned int alloc, actual;
5808 1.12 christos unsigned int i, j;
5809 1.1 christos struct elf_segment_map **sorted_seg_map;
5810 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5811 1.14 christos
5812 1.14 christos if (link_info == NULL
5813 1.1 christos && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5814 1.1 christos return false;
5815 1.3 christos
5816 1.12 christos alloc = 0;
5817 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5818 1.1 christos m->idx = alloc++;
5819 1.1 christos
5820 1.1 christos if (alloc)
5821 1.1 christos {
5822 1.1 christos elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5823 1.1 christos elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5824 1.1 christos }
5825 1.1 christos else
5826 1.1 christos {
5827 1.1 christos /* PR binutils/12467. */
5828 1.1 christos elf_elfheader (abfd)->e_phoff = 0;
5829 1.3 christos elf_elfheader (abfd)->e_phentsize = 0;
5830 1.1 christos }
5831 1.1 christos
5832 1.3 christos elf_elfheader (abfd)->e_phnum = alloc;
5833 1.12 christos
5834 1.12 christos if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5835 1.12 christos {
5836 1.12 christos actual = alloc;
5837 1.1 christos elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5838 1.12 christos }
5839 1.12 christos else
5840 1.12 christos {
5841 1.12 christos actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5842 1.12 christos BFD_ASSERT (elf_program_header_size (abfd)
5843 1.12 christos == actual * bed->s->sizeof_phdr);
5844 1.1 christos BFD_ASSERT (actual >= alloc);
5845 1.1 christos }
5846 1.1 christos
5847 1.3 christos if (alloc == 0)
5848 1.14 christos {
5849 1.1 christos elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5850 1.1 christos return true;
5851 1.3 christos }
5852 1.1 christos
5853 1.1 christos /* We're writing the size in elf_program_header_size (abfd),
5854 1.3 christos see assign_file_positions_except_relocs, so make sure we have
5855 1.3 christos that amount allocated, with trailing space cleared.
5856 1.1 christos The variable alloc contains the computed need, while
5857 1.1 christos elf_program_header_size (abfd) contains the size used for the
5858 1.1 christos layout.
5859 1.1 christos See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5860 1.12 christos where the layout is forced to according to a larger size in the
5861 1.12 christos last iterations for the testcase ld-elf/header. */
5862 1.12 christos phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5863 1.1 christos + alloc * sizeof (*sorted_seg_map)));
5864 1.1 christos sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5865 1.14 christos elf_tdata (abfd)->phdr = phdrs;
5866 1.1 christos if (phdrs == NULL)
5867 1.12 christos return false;
5868 1.12 christos
5869 1.12 christos for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5870 1.12 christos {
5871 1.12 christos sorted_seg_map[j] = m;
5872 1.12 christos /* If elf_segment_map is not from map_sections_to_segments, the
5873 1.12 christos sections may not be correctly ordered. NOTE: sorting should
5874 1.12 christos not be done to the PT_NOTE section of a corefile, which may
5875 1.12 christos contain several pseudo-sections artificially created by bfd.
5876 1.12 christos Sorting these pseudo-sections breaks things badly. */
5877 1.12 christos if (m->count > 1
5878 1.12 christos && !(elf_elfheader (abfd)->e_type == ET_CORE
5879 1.12 christos && m->p_type == PT_NOTE))
5880 1.12 christos {
5881 1.12 christos for (i = 0; i < m->count; i++)
5882 1.12 christos m->sections[i]->target_index = i;
5883 1.12 christos qsort (m->sections, (size_t) m->count, sizeof (asection *),
5884 1.12 christos elf_sort_sections);
5885 1.12 christos }
5886 1.12 christos }
5887 1.12 christos if (alloc > 1)
5888 1.12 christos qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5889 1.1 christos elf_sort_segments);
5890 1.1 christos
5891 1.14 christos maxpagesize = 1;
5892 1.14 christos if ((abfd->flags & D_PAGED) != 0)
5893 1.14 christos {
5894 1.14 christos if (link_info != NULL)
5895 1.14 christos maxpagesize = link_info->maxpagesize;
5896 1.14 christos else
5897 1.1 christos maxpagesize = bed->maxpagesize;
5898 1.12 christos }
5899 1.1 christos
5900 1.12 christos /* Sections must map to file offsets past the ELF file header. */
5901 1.12 christos off = bed->s->sizeof_ehdr;
5902 1.12 christos /* And if one of the PT_LOAD headers doesn't include the program
5903 1.12 christos headers then we'll be mapping program headers in the usual
5904 1.12 christos position after the ELF file header. */
5905 1.12 christos phdr_load_seg = NULL;
5906 1.12 christos for (j = 0; j < alloc; j++)
5907 1.12 christos {
5908 1.12 christos m = sorted_seg_map[j];
5909 1.12 christos if (m->p_type != PT_LOAD)
5910 1.12 christos break;
5911 1.12 christos if (m->includes_phdrs)
5912 1.12 christos {
5913 1.12 christos phdr_load_seg = m;
5914 1.12 christos break;
5915 1.12 christos }
5916 1.12 christos }
5917 1.1 christos if (phdr_load_seg == NULL)
5918 1.12 christos off += actual * bed->s->sizeof_phdr;
5919 1.1 christos
5920 1.1 christos for (j = 0; j < alloc; j++)
5921 1.12 christos {
5922 1.14 christos asection **secpp;
5923 1.14 christos bfd_vma off_adjust; /* Octets. */
5924 1.14 christos bool no_contents;
5925 1.1 christos bfd_size_type p_align;
5926 1.1 christos bool p_align_p;
5927 1.1 christos
5928 1.1 christos /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5929 1.1 christos number of sections with contents contributing to both p_filesz
5930 1.1 christos and p_memsz, followed by a number of sections with no contents
5931 1.12 christos that just contribute to p_memsz. In this loop, OFF tracks next
5932 1.12 christos available file offset for PT_LOAD and PT_NOTE segments. */
5933 1.1 christos m = sorted_seg_map[j];
5934 1.1 christos p = phdrs + m->idx;
5935 1.14 christos p->p_type = m->p_type;
5936 1.14 christos p->p_flags = m->p_flags;
5937 1.1 christos p_align = bed->p_align;
5938 1.1 christos p_align_p = false;
5939 1.12 christos
5940 1.1 christos if (m->count == 0)
5941 1.12 christos p->p_vaddr = m->p_vaddr_offset * opb;
5942 1.1 christos else
5943 1.1 christos p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5944 1.1 christos
5945 1.1 christos if (m->p_paddr_valid)
5946 1.1 christos p->p_paddr = m->p_paddr;
5947 1.1 christos else if (m->count == 0)
5948 1.12 christos p->p_paddr = 0;
5949 1.1 christos else
5950 1.1 christos p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5951 1.1 christos
5952 1.1 christos if (p->p_type == PT_LOAD
5953 1.1 christos && (abfd->flags & D_PAGED) != 0)
5954 1.1 christos {
5955 1.1 christos /* p_align in demand paged PT_LOAD segments effectively stores
5956 1.1 christos the maximum page size. When copying an executable with
5957 1.1 christos objcopy, we set m->p_align from the input file. Use this
5958 1.1 christos value for maxpagesize rather than bed->maxpagesize, which
5959 1.1 christos may be different. Note that we use maxpagesize for PT_TLS
5960 1.1 christos segment alignment later in this function, so we are relying
5961 1.1 christos on at least one PT_LOAD segment appearing before a PT_TLS
5962 1.1 christos segment. */
5963 1.14 christos if (m->p_align_valid)
5964 1.14 christos maxpagesize = m->p_align;
5965 1.14 christos else if (p_align != 0
5966 1.14 christos && (link_info == NULL
5967 1.14 christos || !link_info->maxpagesize_is_set))
5968 1.14 christos /* Set p_align to the default p_align value while laying
5969 1.14 christos out segments aligning to the maximum page size or the
5970 1.14 christos largest section alignment. The run-time loader can
5971 1.14 christos align segments to the default p_align value or the
5972 1.1 christos maximum page size, depending on system page size. */
5973 1.1 christos p_align_p = true;
5974 1.1 christos
5975 1.1 christos p->p_align = maxpagesize;
5976 1.1 christos }
5977 1.1 christos else if (m->p_align_valid)
5978 1.1 christos p->p_align = m->p_align;
5979 1.12 christos else if (m->count == 0)
5980 1.12 christos p->p_align = 1 << bed->s->log_file_align;
5981 1.18 christos
5982 1.1 christos if (m == phdr_load_seg)
5983 1.14 christos off += actual * bed->s->sizeof_phdr;
5984 1.1 christos
5985 1.1 christos no_contents = false;
5986 1.1 christos off_adjust = 0;
5987 1.1 christos if (p->p_type == PT_LOAD
5988 1.12 christos && m->count > 0)
5989 1.1 christos {
5990 1.1 christos bfd_size_type align; /* Bytes. */
5991 1.1 christos unsigned int align_power = 0;
5992 1.1 christos
5993 1.1 christos if (m->p_align_valid)
5994 1.1 christos align = p->p_align;
5995 1.1 christos else
5996 1.1 christos {
5997 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5998 1.1 christos {
5999 1.12 christos unsigned int secalign;
6000 1.1 christos
6001 1.1 christos secalign = bfd_section_alignment (*secpp);
6002 1.1 christos if (secalign > align_power)
6003 1.1 christos align_power = secalign;
6004 1.1 christos }
6005 1.14 christos align = (bfd_size_type) 1 << align_power;
6006 1.14 christos if (align < maxpagesize)
6007 1.14 christos {
6008 1.14 christos /* If a section requires alignment higher than the
6009 1.14 christos default p_align value, don't set p_align to the
6010 1.14 christos default p_align value. */
6011 1.14 christos if (align > p_align)
6012 1.14 christos p_align_p = false;
6013 1.14 christos align = maxpagesize;
6014 1.14 christos }
6015 1.14 christos else
6016 1.14 christos {
6017 1.14 christos /* If a section requires alignment higher than the
6018 1.14 christos maximum page size, set p_align to the section
6019 1.14 christos alignment. */
6020 1.14 christos p_align_p = true;
6021 1.1 christos p_align = align;
6022 1.1 christos }
6023 1.1 christos }
6024 1.1 christos
6025 1.1 christos for (i = 0; i < m->count; i++)
6026 1.1 christos if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6027 1.1 christos /* If we aren't making room for this section, then
6028 1.1 christos it must be SHT_NOBITS regardless of what we've
6029 1.1 christos set via struct bfd_elf_special_section. */
6030 1.1 christos elf_section_type (m->sections[i]) = SHT_NOBITS;
6031 1.1 christos
6032 1.14 christos /* Find out whether this segment contains any loadable
6033 1.1 christos sections. */
6034 1.1 christos no_contents = true;
6035 1.1 christos for (i = 0; i < m->count; i++)
6036 1.14 christos if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6037 1.1 christos {
6038 1.1 christos no_contents = false;
6039 1.1 christos break;
6040 1.12 christos }
6041 1.9 christos
6042 1.9 christos off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6043 1.9 christos
6044 1.9 christos /* Broken hardware and/or kernel require that files do not
6045 1.12 christos map the same page with different permissions on some hppa
6046 1.12 christos processors. */
6047 1.9 christos if (j != 0
6048 1.9 christos && (abfd->flags & D_PAGED) != 0
6049 1.12 christos && bed->no_page_alias
6050 1.12 christos && (off & (maxpagesize - 1)) != 0
6051 1.9 christos && ((off & -maxpagesize)
6052 1.1 christos == ((off + off_adjust) & -maxpagesize)))
6053 1.1 christos off_adjust += maxpagesize;
6054 1.1 christos off += off_adjust;
6055 1.1 christos if (no_contents)
6056 1.1 christos {
6057 1.1 christos /* We shouldn't need to align the segment on disk since
6058 1.1 christos the segment doesn't need file space, but the gABI
6059 1.1 christos arguably requires the alignment and glibc ld.so
6060 1.1 christos checks it. So to comply with the alignment
6061 1.1 christos requirement but not waste file space, we adjust
6062 1.1 christos p_offset for just this segment. (OFF_ADJUST is
6063 1.1 christos subtracted from OFF later.) This may put p_offset
6064 1.1 christos past the end of file, but that shouldn't matter. */
6065 1.1 christos }
6066 1.1 christos else
6067 1.1 christos off_adjust = 0;
6068 1.1 christos }
6069 1.1 christos /* Make sure the .dynamic section is the first section in the
6070 1.1 christos PT_DYNAMIC segment. */
6071 1.1 christos else if (p->p_type == PT_DYNAMIC
6072 1.1 christos && m->count > 1
6073 1.1 christos && strcmp (m->sections[0]->name, ".dynamic") != 0)
6074 1.11 christos {
6075 1.9 christos _bfd_error_handler
6076 1.1 christos (_("%pB: The first section in the PT_DYNAMIC segment"
6077 1.1 christos " is not the .dynamic section"),
6078 1.14 christos abfd);
6079 1.1 christos bfd_set_error (bfd_error_bad_value);
6080 1.1 christos return false;
6081 1.1 christos }
6082 1.1 christos /* Set the note section type to SHT_NOTE. */
6083 1.1 christos else if (p->p_type == PT_NOTE)
6084 1.1 christos for (i = 0; i < m->count; i++)
6085 1.1 christos elf_section_type (m->sections[i]) = SHT_NOTE;
6086 1.1 christos
6087 1.1 christos if (m->includes_filehdr)
6088 1.1 christos {
6089 1.1 christos if (!m->p_flags_valid)
6090 1.1 christos p->p_flags |= PF_R;
6091 1.12 christos p->p_filesz = bed->s->sizeof_ehdr;
6092 1.1 christos p->p_memsz = bed->s->sizeof_ehdr;
6093 1.12 christos if (p->p_type == PT_LOAD)
6094 1.1 christos {
6095 1.12 christos if (m->count > 0)
6096 1.12 christos {
6097 1.12 christos if (p->p_vaddr < (bfd_vma) off
6098 1.12 christos || (!m->p_paddr_valid
6099 1.12 christos && p->p_paddr < (bfd_vma) off))
6100 1.12 christos {
6101 1.12 christos _bfd_error_handler
6102 1.12 christos (_("%pB: not enough room for program headers,"
6103 1.12 christos " try linking with -N"),
6104 1.14 christos abfd);
6105 1.12 christos bfd_set_error (bfd_error_bad_value);
6106 1.12 christos return false;
6107 1.12 christos }
6108 1.12 christos p->p_vaddr -= off;
6109 1.1 christos if (!m->p_paddr_valid)
6110 1.12 christos p->p_paddr -= off;
6111 1.12 christos }
6112 1.12 christos }
6113 1.12 christos else if (sorted_seg_map[0]->includes_filehdr)
6114 1.12 christos {
6115 1.1 christos Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6116 1.12 christos p->p_vaddr = filehdr->p_vaddr;
6117 1.1 christos if (!m->p_paddr_valid)
6118 1.1 christos p->p_paddr = filehdr->p_paddr;
6119 1.1 christos }
6120 1.1 christos }
6121 1.1 christos
6122 1.1 christos if (m->includes_phdrs)
6123 1.1 christos {
6124 1.12 christos if (!m->p_flags_valid)
6125 1.12 christos p->p_flags |= PF_R;
6126 1.1 christos p->p_filesz += actual * bed->s->sizeof_phdr;
6127 1.1 christos p->p_memsz += actual * bed->s->sizeof_phdr;
6128 1.12 christos if (!m->includes_filehdr)
6129 1.12 christos {
6130 1.18 christos if (p->p_type == PT_LOAD)
6131 1.12 christos {
6132 1.12 christos p->p_offset = off - actual * bed->s->sizeof_phdr;
6133 1.12 christos elf_elfheader (abfd)->e_phoff = p->p_offset;
6134 1.12 christos if (m->count > 0)
6135 1.12 christos {
6136 1.12 christos p->p_vaddr -= off - p->p_offset;
6137 1.12 christos if (!m->p_paddr_valid)
6138 1.12 christos p->p_paddr -= off - p->p_offset;
6139 1.12 christos }
6140 1.1 christos }
6141 1.18 christos else if (phdr_load_seg != NULL)
6142 1.18 christos {
6143 1.18 christos /* Also set PT_PHDR to match phdr_load_seg. We've
6144 1.12 christos sorted segments so that phdr_load_seg will
6145 1.12 christos already be set by the code immediately above. */
6146 1.12 christos Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6147 1.12 christos bfd_vma phdr_off = 0; /* Octets. */
6148 1.12 christos if (phdr_load_seg->includes_filehdr)
6149 1.1 christos phdr_off = bed->s->sizeof_ehdr;
6150 1.12 christos p->p_vaddr = phdr->p_vaddr + phdr_off;
6151 1.12 christos if (!m->p_paddr_valid)
6152 1.1 christos p->p_paddr = phdr->p_paddr + phdr_off;
6153 1.12 christos p->p_offset = phdr->p_offset + phdr_off;
6154 1.12 christos }
6155 1.1 christos else
6156 1.1 christos p->p_offset = bed->s->sizeof_ehdr;
6157 1.1 christos }
6158 1.1 christos }
6159 1.1 christos
6160 1.1 christos if (p->p_type == PT_LOAD
6161 1.1 christos || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6162 1.12 christos {
6163 1.12 christos if (!m->includes_filehdr && !m->includes_phdrs)
6164 1.12 christos {
6165 1.12 christos p->p_offset = off;
6166 1.12 christos if (no_contents)
6167 1.12 christos {
6168 1.12 christos /* Put meaningless p_offset for PT_LOAD segments
6169 1.12 christos without file contents somewhere within the first
6170 1.12 christos page, in an attempt to not point past EOF. */
6171 1.12 christos bfd_size_type align = maxpagesize;
6172 1.12 christos if (align < p->p_align)
6173 1.12 christos align = p->p_align;
6174 1.12 christos if (align < 1)
6175 1.12 christos align = 1;
6176 1.12 christos p->p_offset = off % align;
6177 1.1 christos }
6178 1.1 christos }
6179 1.12 christos else
6180 1.1 christos {
6181 1.1 christos file_ptr adjust; /* Octets. */
6182 1.1 christos
6183 1.1 christos adjust = off - (p->p_offset + p->p_filesz);
6184 1.1 christos if (!no_contents)
6185 1.1 christos p->p_filesz += adjust;
6186 1.1 christos p->p_memsz += adjust;
6187 1.1 christos }
6188 1.1 christos }
6189 1.1 christos
6190 1.1 christos /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6191 1.1 christos maps. Set filepos for sections in PT_LOAD segments, and in
6192 1.1 christos core files, for sections in PT_NOTE segments.
6193 1.1 christos assign_file_positions_for_non_load_sections will set filepos
6194 1.1 christos for other sections and update p_filesz for other segments. */
6195 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6196 1.1 christos {
6197 1.1 christos asection *sec;
6198 1.1 christos bfd_size_type align;
6199 1.1 christos Elf_Internal_Shdr *this_hdr;
6200 1.1 christos
6201 1.12 christos sec = *secpp;
6202 1.1 christos this_hdr = &elf_section_data (sec)->this_hdr;
6203 1.1 christos align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6204 1.1 christos
6205 1.1 christos if ((p->p_type == PT_LOAD
6206 1.1 christos || p->p_type == PT_TLS)
6207 1.1 christos && (this_hdr->sh_type != SHT_NOBITS
6208 1.1 christos || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6209 1.1 christos && ((this_hdr->sh_flags & SHF_TLS) == 0
6210 1.14 christos || p->p_type == PT_TLS))))
6211 1.14 christos {
6212 1.14 christos bfd_vma p_start = p->p_paddr; /* Octets. */
6213 1.14 christos bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6214 1.1 christos bfd_vma s_start = sec->lma * opb; /* Octets. */
6215 1.1 christos bfd_vma adjust = s_start - p_end; /* Octets. */
6216 1.1 christos
6217 1.1 christos if (adjust != 0
6218 1.1 christos && (s_start < p_end
6219 1.9 christos || p_end < p_start))
6220 1.9 christos {
6221 1.14 christos _bfd_error_handler
6222 1.14 christos /* xgettext:c-format */
6223 1.12 christos (_("%pB: section %pA lma %#" PRIx64
6224 1.12 christos " adjusted to %#" PRIx64),
6225 1.1 christos abfd, sec, (uint64_t) s_start / opb,
6226 1.12 christos (uint64_t) p_end / opb);
6227 1.1 christos adjust = 0;
6228 1.1 christos sec->lma = p_end / opb;
6229 1.1 christos }
6230 1.12 christos p->p_memsz += adjust;
6231 1.1 christos
6232 1.12 christos if (p->p_type == PT_LOAD)
6233 1.12 christos {
6234 1.12 christos if (this_hdr->sh_type != SHT_NOBITS)
6235 1.12 christos {
6236 1.12 christos off_adjust = 0;
6237 1.12 christos if (p->p_filesz + adjust < p->p_memsz)
6238 1.17 christos {
6239 1.17 christos /* We have a PROGBITS section following NOBITS ones.
6240 1.17 christos Allocate file space for the NOBITS section(s).
6241 1.17 christos We don't need to write out the zeros, posix
6242 1.17 christos fseek past the end of data already written
6243 1.17 christos followed by a write at that location is
6244 1.12 christos guaranteed to result in zeros being read
6245 1.12 christos from the gap. */
6246 1.12 christos adjust = p->p_memsz - p->p_filesz;
6247 1.12 christos }
6248 1.12 christos }
6249 1.12 christos /* We only adjust sh_offset in SHT_NOBITS sections
6250 1.12 christos as would seem proper for their address when the
6251 1.12 christos section is first in the segment. sh_offset
6252 1.12 christos doesn't really have any significance for
6253 1.12 christos SHT_NOBITS anyway, apart from a notional position
6254 1.12 christos relative to other sections. Historically we
6255 1.12 christos didn't bother with adjusting sh_offset and some
6256 1.12 christos programs depend on it not being adjusted. See
6257 1.1 christos pr12921 and pr25662. */
6258 1.12 christos if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6259 1.12 christos {
6260 1.12 christos off += adjust;
6261 1.1 christos if (this_hdr->sh_type == SHT_NOBITS)
6262 1.1 christos off_adjust += adjust;
6263 1.12 christos }
6264 1.12 christos }
6265 1.1 christos if (this_hdr->sh_type != SHT_NOBITS)
6266 1.1 christos p->p_filesz += adjust;
6267 1.1 christos }
6268 1.1 christos
6269 1.1 christos if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6270 1.1 christos {
6271 1.1 christos /* The section at i == 0 is the one that actually contains
6272 1.1 christos everything. */
6273 1.1 christos if (i == 0)
6274 1.1 christos {
6275 1.1 christos this_hdr->sh_offset = sec->filepos = off;
6276 1.1 christos off += this_hdr->sh_size;
6277 1.1 christos p->p_filesz = this_hdr->sh_size;
6278 1.1 christos p->p_memsz = 0;
6279 1.1 christos p->p_align = 1;
6280 1.1 christos }
6281 1.1 christos else
6282 1.1 christos {
6283 1.1 christos /* The rest are fake sections that shouldn't be written. */
6284 1.1 christos sec->filepos = 0;
6285 1.1 christos sec->size = 0;
6286 1.1 christos sec->flags = 0;
6287 1.1 christos continue;
6288 1.1 christos }
6289 1.1 christos }
6290 1.1 christos else
6291 1.1 christos {
6292 1.1 christos if (p->p_type == PT_LOAD)
6293 1.1 christos {
6294 1.1 christos this_hdr->sh_offset = sec->filepos = off;
6295 1.1 christos if (this_hdr->sh_type != SHT_NOBITS)
6296 1.3 christos off += this_hdr->sh_size;
6297 1.3 christos }
6298 1.3 christos else if (this_hdr->sh_type == SHT_NOBITS
6299 1.3 christos && (this_hdr->sh_flags & SHF_TLS) != 0
6300 1.3 christos && this_hdr->sh_offset == 0)
6301 1.3 christos {
6302 1.3 christos /* This is a .tbss section that didn't get a PT_LOAD.
6303 1.3 christos (See _bfd_elf_map_sections_to_segments "Create a
6304 1.3 christos final PT_LOAD".) Set sh_offset to the value it
6305 1.3 christos would have if we had created a zero p_filesz and
6306 1.3 christos p_memsz PT_LOAD header for the section. This
6307 1.3 christos also makes the PT_TLS header have the same
6308 1.3 christos p_offset value. */
6309 1.3 christos bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6310 1.3 christos off, align);
6311 1.1 christos this_hdr->sh_offset = sec->filepos = off + adjust;
6312 1.1 christos }
6313 1.1 christos
6314 1.1 christos if (this_hdr->sh_type != SHT_NOBITS)
6315 1.1 christos {
6316 1.1 christos p->p_filesz += this_hdr->sh_size;
6317 1.1 christos /* A load section without SHF_ALLOC is something like
6318 1.1 christos a note section in a PT_NOTE segment. These take
6319 1.1 christos file space but are not loaded into memory. */
6320 1.1 christos if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6321 1.1 christos p->p_memsz += this_hdr->sh_size;
6322 1.1 christos }
6323 1.1 christos else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6324 1.1 christos {
6325 1.1 christos if (p->p_type == PT_TLS)
6326 1.1 christos p->p_memsz += this_hdr->sh_size;
6327 1.1 christos
6328 1.1 christos /* .tbss is special. It doesn't contribute to p_memsz of
6329 1.1 christos normal segments. */
6330 1.1 christos else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6331 1.1 christos p->p_memsz += this_hdr->sh_size;
6332 1.1 christos }
6333 1.1 christos
6334 1.1 christos if (align > p->p_align
6335 1.1 christos && !m->p_align_valid
6336 1.1 christos && (p->p_type != PT_LOAD
6337 1.1 christos || (abfd->flags & D_PAGED) == 0))
6338 1.1 christos p->p_align = align;
6339 1.1 christos }
6340 1.1 christos
6341 1.1 christos if (!m->p_flags_valid)
6342 1.1 christos {
6343 1.1 christos p->p_flags |= PF_R;
6344 1.1 christos if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6345 1.1 christos p->p_flags |= PF_X;
6346 1.1 christos if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6347 1.1 christos p->p_flags |= PF_W;
6348 1.5 christos }
6349 1.1 christos }
6350 1.1 christos
6351 1.12 christos off -= off_adjust;
6352 1.12 christos
6353 1.12 christos /* PR ld/20815 - Check that the program header segment, if
6354 1.12 christos present, will be loaded into memory. */
6355 1.12 christos if (p->p_type == PT_PHDR
6356 1.12 christos && phdr_load_seg == NULL
6357 1.12 christos && !(bed->elf_backend_allow_non_load_phdr != NULL
6358 1.12 christos && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6359 1.12 christos {
6360 1.12 christos /* The fix for this error is usually to edit the linker script being
6361 1.12 christos used and set up the program headers manually. Either that or
6362 1.12 christos leave room for the headers at the start of the SECTIONS. */
6363 1.12 christos _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6364 1.12 christos " by LOAD segment"),
6365 1.14 christos abfd);
6366 1.12 christos if (link_info == NULL)
6367 1.12 christos return false;
6368 1.12 christos /* Arrange for the linker to exit with an error, deleting
6369 1.12 christos the output file unless --noinhibit-exec is given. */
6370 1.12 christos link_info->callbacks->info ("%X");
6371 1.1 christos }
6372 1.1 christos
6373 1.1 christos /* Check that all sections are in a PT_LOAD segment.
6374 1.1 christos Don't check funky gdb generated core files. */
6375 1.14 christos if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6376 1.1 christos {
6377 1.1 christos bool check_vma = true;
6378 1.1 christos
6379 1.1 christos for (i = 1; i < m->count; i++)
6380 1.1 christos if (m->sections[i]->vma == m->sections[i - 1]->vma
6381 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6382 1.1 christos ->this_hdr), p) != 0
6383 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6384 1.1 christos ->this_hdr), p) != 0)
6385 1.14 christos {
6386 1.1 christos /* Looks like we have overlays packed into the segment. */
6387 1.1 christos check_vma = false;
6388 1.1 christos break;
6389 1.1 christos }
6390 1.1 christos
6391 1.1 christos for (i = 0; i < m->count; i++)
6392 1.1 christos {
6393 1.1 christos Elf_Internal_Shdr *this_hdr;
6394 1.1 christos asection *sec;
6395 1.1 christos
6396 1.3 christos sec = m->sections[i];
6397 1.3 christos this_hdr = &(elf_section_data(sec)->this_hdr);
6398 1.1 christos if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6399 1.9 christos && !ELF_TBSS_SPECIAL (this_hdr, p))
6400 1.9 christos {
6401 1.18 christos _bfd_error_handler
6402 1.18 christos /* xgettext:c-format */
6403 1.1 christos (_("%pB: section `%pA' can't be allocated in segment %u"),
6404 1.1 christos abfd, sec, m->idx);
6405 1.1 christos print_segment_map (m);
6406 1.14 christos }
6407 1.14 christos }
6408 1.14 christos
6409 1.1 christos if (p_align_p)
6410 1.1 christos p->p_align = p_align;
6411 1.1 christos }
6412 1.3 christos }
6413 1.1 christos
6414 1.12 christos elf_next_file_pos (abfd) = off;
6415 1.12 christos
6416 1.12 christos if (link_info != NULL
6417 1.1 christos && phdr_load_seg != NULL
6418 1.12 christos && phdr_load_seg->includes_filehdr)
6419 1.12 christos {
6420 1.12 christos /* There is a segment that contains both the file headers and the
6421 1.12 christos program headers, so provide a symbol __ehdr_start pointing there.
6422 1.12 christos A program can use this to examine itself robustly. */
6423 1.12 christos
6424 1.14 christos struct elf_link_hash_entry *hash
6425 1.12 christos = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6426 1.12 christos false, false, true);
6427 1.12 christos /* If the symbol was referenced and not defined, define it. */
6428 1.12 christos if (hash != NULL
6429 1.12 christos && (hash->root.type == bfd_link_hash_new
6430 1.12 christos || hash->root.type == bfd_link_hash_undefined
6431 1.12 christos || hash->root.type == bfd_link_hash_undefweak
6432 1.12 christos || hash->root.type == bfd_link_hash_common))
6433 1.12 christos {
6434 1.12 christos asection *s = NULL;
6435 1.12 christos bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6436 1.12 christos
6437 1.12 christos if (phdr_load_seg->count != 0)
6438 1.12 christos /* The segment contains sections, so use the first one. */
6439 1.12 christos s = phdr_load_seg->sections[0];
6440 1.12 christos else
6441 1.12 christos /* Use the first (i.e. lowest-addressed) section in any segment. */
6442 1.12 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6443 1.12 christos if (m->p_type == PT_LOAD && m->count != 0)
6444 1.12 christos {
6445 1.12 christos s = m->sections[0];
6446 1.12 christos break;
6447 1.12 christos }
6448 1.12 christos
6449 1.12 christos if (s != NULL)
6450 1.12 christos {
6451 1.12 christos hash->root.u.def.value = filehdr_vaddr - s->vma;
6452 1.12 christos hash->root.u.def.section = s;
6453 1.12 christos }
6454 1.12 christos else
6455 1.12 christos {
6456 1.12 christos hash->root.u.def.value = filehdr_vaddr;
6457 1.12 christos hash->root.u.def.section = bfd_abs_section_ptr;
6458 1.12 christos }
6459 1.12 christos
6460 1.12 christos hash->root.type = bfd_link_hash_defined;
6461 1.12 christos hash->def_regular = 1;
6462 1.12 christos hash->non_elf = 0;
6463 1.12 christos }
6464 1.14 christos }
6465 1.12 christos
6466 1.12 christos return true;
6467 1.12 christos }
6468 1.12 christos
6469 1.12 christos /* Determine if a bfd is a debuginfo file. Unfortunately there
6470 1.12 christos is no defined method for detecting such files, so we have to
6471 1.14 christos use heuristics instead. */
6472 1.12 christos
6473 1.12 christos bool
6474 1.12 christos is_debuginfo_file (bfd *abfd)
6475 1.14 christos {
6476 1.12 christos if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6477 1.12 christos return false;
6478 1.12 christos
6479 1.12 christos Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6480 1.12 christos Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6481 1.12 christos Elf_Internal_Shdr **headerp;
6482 1.12 christos
6483 1.12 christos for (headerp = start_headers; headerp < end_headers; headerp ++)
6484 1.12 christos {
6485 1.12 christos Elf_Internal_Shdr *header = * headerp;
6486 1.12 christos
6487 1.12 christos /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6488 1.12 christos The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6489 1.12 christos if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6490 1.14 christos && header->sh_type != SHT_NOBITS
6491 1.12 christos && header->sh_type != SHT_NOTE)
6492 1.12 christos return false;
6493 1.14 christos }
6494 1.12 christos
6495 1.12 christos return true;
6496 1.14 christos }
6497 1.14 christos
6498 1.12 christos /* Assign file positions for other sections, except for compressed debug
6499 1.14 christos and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6500 1.12 christos
6501 1.12 christos static bool
6502 1.12 christos assign_file_positions_for_non_load_sections (bfd *abfd,
6503 1.12 christos struct bfd_link_info *link_info)
6504 1.12 christos {
6505 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6506 1.12 christos Elf_Internal_Shdr **i_shdrpp;
6507 1.12 christos Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6508 1.12 christos Elf_Internal_Phdr *phdrs;
6509 1.12 christos Elf_Internal_Phdr *p;
6510 1.12 christos struct elf_segment_map *m;
6511 1.14 christos file_ptr off;
6512 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6513 1.14 christos bfd_vma maxpagesize;
6514 1.14 christos
6515 1.14 christos if (link_info != NULL)
6516 1.14 christos maxpagesize = link_info->maxpagesize;
6517 1.12 christos else
6518 1.12 christos maxpagesize = bed->maxpagesize;
6519 1.12 christos i_shdrpp = elf_elfsections (abfd);
6520 1.12 christos end_hdrpp = i_shdrpp + elf_numsections (abfd);
6521 1.12 christos off = elf_next_file_pos (abfd);
6522 1.12 christos for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6523 1.14 christos {
6524 1.1 christos Elf_Internal_Shdr *hdr;
6525 1.1 christos bfd_vma align;
6526 1.1 christos
6527 1.1 christos hdr = *hdrpp;
6528 1.1 christos if (hdr->bfd_section != NULL
6529 1.1 christos && (hdr->bfd_section->filepos != 0
6530 1.1 christos || (hdr->sh_type == SHT_NOBITS
6531 1.1 christos && hdr->contents == NULL)))
6532 1.1 christos BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6533 1.12 christos else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6534 1.12 christos {
6535 1.12 christos if (hdr->sh_size != 0
6536 1.12 christos /* PR 24717 - debuginfo files are known to be not strictly
6537 1.12 christos compliant with the ELF standard. In particular they often
6538 1.12 christos have .note.gnu.property sections that are outside of any
6539 1.12 christos loadable segment. This is not a problem for such files,
6540 1.9 christos so do not warn about them. */
6541 1.9 christos && ! is_debuginfo_file (abfd))
6542 1.11 christos _bfd_error_handler
6543 1.3 christos /* xgettext:c-format */
6544 1.3 christos (_("%pB: warning: allocated section `%s' not in segment"),
6545 1.3 christos abfd,
6546 1.3 christos (hdr->bfd_section == NULL
6547 1.1 christos ? "*unknown*"
6548 1.1 christos : hdr->bfd_section->name));
6549 1.14 christos /* We don't need to page align empty sections. */
6550 1.1 christos if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6551 1.14 christos align = maxpagesize;
6552 1.14 christos else
6553 1.18 christos align = hdr->sh_addralign & -hdr->sh_addralign;
6554 1.18 christos off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6555 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6556 1.1 christos bed->s->log_file_align);
6557 1.1 christos }
6558 1.14 christos else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6559 1.14 christos && hdr->bfd_section == NULL)
6560 1.14 christos /* We don't know the offset of these sections yet:
6561 1.14 christos their size has not been decided. */
6562 1.14 christos || (abfd->is_linker_output
6563 1.14 christos && hdr->bfd_section != NULL
6564 1.3 christos && (hdr->sh_name == -1u
6565 1.8 christos || bfd_section_is_ctf (hdr->bfd_section)))
6566 1.8 christos || hdr == i_shdrpp[elf_onesymtab (abfd)]
6567 1.6 christos || (elf_symtab_shndx_list (abfd) != NULL
6568 1.6 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6569 1.1 christos || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6570 1.1 christos || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6571 1.18 christos hdr->sh_offset = -1;
6572 1.1 christos else
6573 1.12 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6574 1.1 christos }
6575 1.1 christos elf_next_file_pos (abfd) = off;
6576 1.1 christos
6577 1.1 christos /* Now that we have set the section file positions, we can set up
6578 1.3 christos the file positions for the non PT_LOAD segments. */
6579 1.1 christos phdrs = elf_tdata (abfd)->phdr;
6580 1.1 christos for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6581 1.1 christos {
6582 1.12 christos if (p->p_type == PT_GNU_RELRO)
6583 1.14 christos {
6584 1.1 christos bfd_vma start, end; /* Bytes. */
6585 1.1 christos bool ok;
6586 1.1 christos
6587 1.1 christos if (link_info != NULL)
6588 1.11 christos {
6589 1.11 christos /* During linking the range of the RELRO segment is passed
6590 1.11 christos in link_info. Note that there may be padding between
6591 1.11 christos relro_start and the first RELRO section. */
6592 1.11 christos start = link_info->relro_start;
6593 1.11 christos end = link_info->relro_end;
6594 1.11 christos }
6595 1.11 christos else if (m->count != 0)
6596 1.11 christos {
6597 1.11 christos if (!m->p_size_valid)
6598 1.12 christos abort ();
6599 1.11 christos start = m->sections[0]->vma;
6600 1.11 christos end = start + m->p_size / opb;
6601 1.11 christos }
6602 1.11 christos else
6603 1.11 christos {
6604 1.11 christos start = 0;
6605 1.11 christos end = 0;
6606 1.14 christos }
6607 1.11 christos
6608 1.11 christos ok = false;
6609 1.11 christos if (start < end)
6610 1.11 christos {
6611 1.11 christos struct elf_segment_map *lm;
6612 1.11 christos const Elf_Internal_Phdr *lp;
6613 1.11 christos unsigned int i;
6614 1.11 christos
6615 1.3 christos /* Find a LOAD segment containing a section in the RELRO
6616 1.3 christos segment. */
6617 1.3 christos for (lm = elf_seg_map (abfd), lp = phdrs;
6618 1.1 christos lm != NULL;
6619 1.1 christos lm = lm->next, lp++)
6620 1.3 christos {
6621 1.11 christos if (lp->p_type == PT_LOAD
6622 1.11 christos && lm->count != 0
6623 1.12 christos && (lm->sections[lm->count - 1]->vma
6624 1.11 christos + (!IS_TBSS (lm->sections[lm->count - 1])
6625 1.11 christos ? lm->sections[lm->count - 1]->size / opb
6626 1.1 christos : 0)) > start
6627 1.1 christos && lm->sections[0]->vma < end)
6628 1.3 christos break;
6629 1.11 christos }
6630 1.1 christos
6631 1.11 christos if (lm != NULL)
6632 1.11 christos {
6633 1.11 christos /* Find the section starting the RELRO segment. */
6634 1.11 christos for (i = 0; i < lm->count; i++)
6635 1.11 christos {
6636 1.11 christos asection *s = lm->sections[i];
6637 1.11 christos if (s->vma >= start
6638 1.11 christos && s->vma < end
6639 1.11 christos && s->size != 0)
6640 1.11 christos break;
6641 1.11 christos }
6642 1.11 christos
6643 1.12 christos if (i < lm->count)
6644 1.12 christos {
6645 1.11 christos p->p_vaddr = lm->sections[i]->vma * opb;
6646 1.12 christos p->p_paddr = lm->sections[i]->lma * opb;
6647 1.11 christos p->p_offset = lm->sections[i]->filepos;
6648 1.11 christos p->p_memsz = end * opb - p->p_vaddr;
6649 1.11 christos p->p_filesz = p->p_memsz;
6650 1.11 christos
6651 1.11 christos /* The RELRO segment typically ends a few bytes
6652 1.11 christos into .got.plt but other layouts are possible.
6653 1.11 christos In cases where the end does not match any
6654 1.11 christos loaded section (for instance is in file
6655 1.11 christos padding), trim p_filesz back to correspond to
6656 1.11 christos the end of loaded section contents. */
6657 1.11 christos if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6658 1.11 christos p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6659 1.11 christos
6660 1.11 christos /* Preserve the alignment and flags if they are
6661 1.11 christos valid. The gold linker generates RW/4 for
6662 1.11 christos the PT_GNU_RELRO section. It is better for
6663 1.11 christos objcopy/strip to honor these attributes
6664 1.11 christos otherwise gdb will choke when using separate
6665 1.11 christos debug files. */
6666 1.11 christos if (!m->p_align_valid)
6667 1.11 christos p->p_align = 1;
6668 1.14 christos if (!m->p_flags_valid)
6669 1.11 christos p->p_flags = PF_R;
6670 1.1 christos ok = true;
6671 1.1 christos }
6672 1.14 christos }
6673 1.11 christos }
6674 1.14 christos
6675 1.14 christos if (!ok)
6676 1.14 christos {
6677 1.14 christos if (link_info != NULL)
6678 1.14 christos _bfd_error_handler
6679 1.14 christos (_("%pB: warning: unable to allocate any sections"
6680 1.14 christos " to PT_GNU_RELRO segment"),
6681 1.14 christos abfd);
6682 1.1 christos memset (p, 0, sizeof *p);
6683 1.3 christos }
6684 1.3 christos }
6685 1.3 christos else if (p->p_type == PT_GNU_STACK)
6686 1.3 christos {
6687 1.3 christos if (m->p_size_valid)
6688 1.1 christos p->p_memsz = m->p_size;
6689 1.1 christos }
6690 1.6 christos else if (m->count != 0)
6691 1.9 christos {
6692 1.1 christos unsigned int i;
6693 1.1 christos
6694 1.1 christos if (p->p_type != PT_LOAD
6695 1.1 christos && (p->p_type != PT_NOTE
6696 1.9 christos || bfd_get_format (abfd) != bfd_core))
6697 1.9 christos {
6698 1.9 christos /* A user specified segment layout may include a PHDR
6699 1.9 christos segment that overlaps with a LOAD segment... */
6700 1.9 christos if (p->p_type == PT_PHDR)
6701 1.9 christos {
6702 1.9 christos m->count = 0;
6703 1.9 christos continue;
6704 1.6 christos }
6705 1.6 christos
6706 1.6 christos if (m->includes_filehdr || m->includes_phdrs)
6707 1.9 christos {
6708 1.11 christos /* PR 17512: file: 2195325e. */
6709 1.11 christos _bfd_error_handler
6710 1.11 christos (_("%pB: error: non-load segment %d includes file header "
6711 1.14 christos "and/or program header"),
6712 1.6 christos abfd, (int) (p - phdrs));
6713 1.1 christos return false;
6714 1.3 christos }
6715 1.1 christos
6716 1.3 christos p->p_filesz = 0;
6717 1.3 christos p->p_offset = m->sections[0]->filepos;
6718 1.3 christos for (i = m->count; i-- != 0;)
6719 1.3 christos {
6720 1.3 christos asection *sect = m->sections[i];
6721 1.3 christos Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6722 1.14 christos if (hdr->sh_type != SHT_NOBITS)
6723 1.14 christos {
6724 1.14 christos p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6725 1.14 christos /* NB: p_memsz of the loadable PT_NOTE segment
6726 1.14 christos should be the same as p_filesz. */
6727 1.14 christos if (p->p_type == PT_NOTE
6728 1.3 christos && (hdr->sh_flags & SHF_ALLOC) != 0)
6729 1.3 christos p->p_memsz = p->p_filesz;
6730 1.3 christos break;
6731 1.1 christos }
6732 1.1 christos }
6733 1.1 christos }
6734 1.1 christos }
6735 1.14 christos }
6736 1.1 christos
6737 1.1 christos return true;
6738 1.8 christos }
6739 1.8 christos
6740 1.8 christos static elf_section_list *
6741 1.8 christos find_section_in_list (unsigned int i, elf_section_list * list)
6742 1.8 christos {
6743 1.8 christos for (;list != NULL; list = list->next)
6744 1.8 christos if (list->ndx == i)
6745 1.8 christos break;
6746 1.8 christos return list;
6747 1.1 christos }
6748 1.1 christos
6749 1.1 christos /* Work out the file positions of all the sections. This is called by
6750 1.1 christos _bfd_elf_compute_section_file_positions. All the section sizes and
6751 1.1 christos VMAs must be known before this is called.
6752 1.14 christos
6753 1.14 christos Reloc sections come in two flavours: Those processed specially as
6754 1.14 christos "side-channel" data attached to a section to which they apply, and
6755 1.14 christos those that bfd doesn't process as relocations. The latter sort are
6756 1.14 christos stored in a normal bfd section by bfd_section_from_shdr. We don't
6757 1.14 christos consider the former sort here, unless they form part of the loadable
6758 1.14 christos image. Reloc sections not assigned here (and compressed debugging
6759 1.1 christos sections and CTF sections which nothing else in the file can rely
6760 1.1 christos upon) will be handled later by assign_file_positions_for_relocs.
6761 1.1 christos
6762 1.14 christos We also don't set the positions of the .symtab and .strtab here. */
6763 1.1 christos
6764 1.1 christos static bool
6765 1.1 christos assign_file_positions_except_relocs (bfd *abfd,
6766 1.1 christos struct bfd_link_info *link_info)
6767 1.1 christos {
6768 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
6769 1.12 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6770 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6771 1.1 christos unsigned int alloc;
6772 1.1 christos
6773 1.1 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6774 1.1 christos && bfd_get_format (abfd) != bfd_core)
6775 1.1 christos {
6776 1.1 christos Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6777 1.1 christos unsigned int num_sec = elf_numsections (abfd);
6778 1.5 christos Elf_Internal_Shdr **hdrpp;
6779 1.1 christos unsigned int i;
6780 1.1 christos file_ptr off;
6781 1.1 christos
6782 1.1 christos /* Start after the ELF header. */
6783 1.1 christos off = i_ehdrp->e_ehsize;
6784 1.1 christos
6785 1.1 christos /* We are not creating an executable, which means that we are
6786 1.1 christos not creating a program header, and that the actual order of
6787 1.1 christos the sections in the file is unimportant. */
6788 1.1 christos for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6789 1.1 christos {
6790 1.1 christos Elf_Internal_Shdr *hdr;
6791 1.1 christos
6792 1.1 christos hdr = *hdrpp;
6793 1.12 christos if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6794 1.12 christos && hdr->bfd_section == NULL)
6795 1.14 christos /* Do not assign offsets for these sections yet: we don't know
6796 1.14 christos their sizes. */
6797 1.14 christos || (abfd->is_linker_output
6798 1.14 christos && hdr->bfd_section != NULL
6799 1.3 christos && (hdr->sh_name == -1u
6800 1.8 christos || bfd_section_is_ctf (hdr->bfd_section)))
6801 1.8 christos || i == elf_onesymtab (abfd)
6802 1.6 christos || (elf_symtab_shndx_list (abfd) != NULL
6803 1.6 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6804 1.1 christos || i == elf_strtab_sec (abfd)
6805 1.1 christos || i == elf_shstrtab_sec (abfd))
6806 1.1 christos {
6807 1.1 christos hdr->sh_offset = -1;
6808 1.18 christos }
6809 1.18 christos else
6810 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6811 1.5 christos 0);
6812 1.5 christos }
6813 1.12 christos
6814 1.1 christos elf_next_file_pos (abfd) = off;
6815 1.1 christos elf_program_header_size (abfd) = 0;
6816 1.1 christos }
6817 1.1 christos else
6818 1.1 christos {
6819 1.1 christos /* Assign file positions for the loaded sections based on the
6820 1.14 christos assignment of sections to segments. */
6821 1.1 christos if (!assign_file_positions_for_load_sections (abfd, link_info))
6822 1.1 christos return false;
6823 1.1 christos
6824 1.14 christos /* And for non-load sections. */
6825 1.12 christos if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6826 1.1 christos return false;
6827 1.12 christos }
6828 1.14 christos
6829 1.1 christos if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6830 1.12 christos return false;
6831 1.12 christos
6832 1.12 christos /* Write out the program headers. */
6833 1.12 christos alloc = i_ehdrp->e_phnum;
6834 1.14 christos if (alloc != 0)
6835 1.14 christos {
6836 1.17 christos if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6837 1.17 christos {
6838 1.17 christos bool warned_tls = false;
6839 1.14 christos bool warned_rwx = false;
6840 1.14 christos
6841 1.14 christos /* Memory resident segments with non-zero size and RWX
6842 1.14 christos permissions are a security risk, so we generate a warning
6843 1.14 christos here if we are creating any. */
6844 1.14 christos unsigned int i;
6845 1.14 christos
6846 1.14 christos for (i = 0; i < alloc; i++)
6847 1.14 christos {
6848 1.14 christos const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6849 1.14 christos
6850 1.14 christos if (phdr->p_memsz == 0)
6851 1.17 christos continue;
6852 1.17 christos
6853 1.17 christos if (! warned_tls
6854 1.17 christos && phdr->p_type == PT_TLS
6855 1.17 christos && (phdr->p_flags & PF_X))
6856 1.17 christos {
6857 1.17 christos if (link_info->warn_is_error_for_rwx_segments)
6858 1.17 christos {
6859 1.17 christos _bfd_error_handler (_("\
6860 1.17 christos error: %pB has a TLS segment with execute permission"),
6861 1.17 christos abfd);
6862 1.17 christos return false;
6863 1.17 christos }
6864 1.17 christos
6865 1.17 christos _bfd_error_handler (_("\
6866 1.17 christos warning: %pB has a TLS segment with execute permission"),
6867 1.17 christos abfd);
6868 1.17 christos if (warned_rwx)
6869 1.17 christos break;
6870 1.17 christos
6871 1.17 christos warned_tls = true;
6872 1.17 christos }
6873 1.14 christos else if (! warned_rwx
6874 1.14 christos && phdr->p_type == PT_LOAD
6875 1.17 christos && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6876 1.17 christos == (PF_R | PF_W | PF_X)))
6877 1.17 christos {
6878 1.17 christos if (link_info->warn_is_error_for_rwx_segments)
6879 1.17 christos {
6880 1.17 christos _bfd_error_handler (_("\
6881 1.17 christos error: %pB has a LOAD segment with RWX permissions"),
6882 1.17 christos abfd);
6883 1.17 christos return false;
6884 1.17 christos }
6885 1.17 christos
6886 1.17 christos _bfd_error_handler (_("\
6887 1.17 christos warning: %pB has a LOAD segment with RWX permissions"),
6888 1.17 christos abfd);
6889 1.17 christos if (warned_tls)
6890 1.17 christos break;
6891 1.17 christos
6892 1.14 christos warned_rwx = true;
6893 1.14 christos }
6894 1.14 christos }
6895 1.12 christos }
6896 1.1 christos
6897 1.14 christos if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6898 1.1 christos || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6899 1.1 christos return false;
6900 1.14 christos }
6901 1.1 christos
6902 1.1 christos return true;
6903 1.14 christos }
6904 1.12 christos
6905 1.12 christos bool
6906 1.1 christos _bfd_elf_init_file_header (bfd *abfd,
6907 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED)
6908 1.1 christos {
6909 1.1 christos Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6910 1.1 christos struct elf_strtab_hash *shstrtab;
6911 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6912 1.1 christos
6913 1.1 christos i_ehdrp = elf_elfheader (abfd);
6914 1.1 christos
6915 1.14 christos shstrtab = _bfd_elf_strtab_init ();
6916 1.1 christos if (shstrtab == NULL)
6917 1.1 christos return false;
6918 1.1 christos
6919 1.1 christos elf_shstrtab (abfd) = shstrtab;
6920 1.1 christos
6921 1.1 christos i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6922 1.1 christos i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6923 1.1 christos i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6924 1.1 christos i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6925 1.1 christos
6926 1.1 christos i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6927 1.1 christos i_ehdrp->e_ident[EI_DATA] =
6928 1.1 christos bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6929 1.1 christos i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6930 1.1 christos
6931 1.1 christos if ((abfd->flags & DYNAMIC) != 0)
6932 1.1 christos i_ehdrp->e_type = ET_DYN;
6933 1.1 christos else if ((abfd->flags & EXEC_P) != 0)
6934 1.1 christos i_ehdrp->e_type = ET_EXEC;
6935 1.1 christos else if (bfd_get_format (abfd) == bfd_core)
6936 1.1 christos i_ehdrp->e_type = ET_CORE;
6937 1.1 christos else
6938 1.1 christos i_ehdrp->e_type = ET_REL;
6939 1.1 christos
6940 1.1 christos switch (bfd_get_arch (abfd))
6941 1.1 christos {
6942 1.1 christos case bfd_arch_unknown:
6943 1.1 christos i_ehdrp->e_machine = EM_NONE;
6944 1.1 christos break;
6945 1.1 christos
6946 1.1 christos /* There used to be a long list of cases here, each one setting
6947 1.1 christos e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6948 1.1 christos in the corresponding bfd definition. To avoid duplication,
6949 1.1 christos the switch was removed. Machines that need special handling
6950 1.1 christos can generally do it in elf_backend_final_write_processing(),
6951 1.1 christos unless they need the information earlier than the final write.
6952 1.1 christos Such need can generally be supplied by replacing the tests for
6953 1.1 christos e_machine with the conditions used to determine it. */
6954 1.1 christos default:
6955 1.1 christos i_ehdrp->e_machine = bed->elf_machine_code;
6956 1.1 christos }
6957 1.1 christos
6958 1.1 christos i_ehdrp->e_version = bed->s->ev_current;
6959 1.1 christos i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6960 1.1 christos
6961 1.1 christos /* No program header, for now. */
6962 1.1 christos i_ehdrp->e_phoff = 0;
6963 1.1 christos i_ehdrp->e_phentsize = 0;
6964 1.1 christos i_ehdrp->e_phnum = 0;
6965 1.1 christos
6966 1.1 christos /* Each bfd section is section header entry. */
6967 1.1 christos i_ehdrp->e_entry = bfd_get_start_address (abfd);
6968 1.1 christos i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6969 1.14 christos
6970 1.1 christos elf_tdata (abfd)->symtab_hdr.sh_name =
6971 1.14 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6972 1.1 christos elf_tdata (abfd)->strtab_hdr.sh_name =
6973 1.14 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6974 1.1 christos elf_tdata (abfd)->shstrtab_hdr.sh_name =
6975 1.5 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6976 1.1 christos if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6977 1.14 christos || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6978 1.1 christos || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6979 1.14 christos return false;
6980 1.1 christos
6981 1.1 christos return true;
6982 1.12 christos }
6983 1.12 christos
6984 1.12 christos /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6985 1.12 christos
6986 1.12 christos FIXME: We used to have code here to sort the PT_LOAD segments into
6987 1.12 christos ascending order, as per the ELF spec. But this breaks some programs,
6988 1.12 christos including the Linux kernel. But really either the spec should be
6989 1.14 christos changed or the programs updated. */
6990 1.12 christos
6991 1.12 christos bool
6992 1.12 christos _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6993 1.12 christos {
6994 1.12 christos if (link_info != NULL && bfd_link_pie (link_info))
6995 1.12 christos {
6996 1.12 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6997 1.12 christos unsigned int num_segments = i_ehdrp->e_phnum;
6998 1.12 christos struct elf_obj_tdata *tdata = elf_tdata (obfd);
6999 1.12 christos Elf_Internal_Phdr *segment = tdata->phdr;
7000 1.12 christos Elf_Internal_Phdr *end_segment = &segment[num_segments];
7001 1.12 christos
7002 1.12 christos /* Find the lowest p_vaddr in PT_LOAD segments. */
7003 1.12 christos bfd_vma p_vaddr = (bfd_vma) -1;
7004 1.12 christos for (; segment < end_segment; segment++)
7005 1.12 christos if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7006 1.12 christos p_vaddr = segment->p_vaddr;
7007 1.12 christos
7008 1.12 christos /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7009 1.12 christos segments is non-zero. */
7010 1.12 christos if (p_vaddr)
7011 1.14 christos i_ehdrp->e_type = ET_EXEC;
7012 1.12 christos }
7013 1.12 christos return true;
7014 1.1 christos }
7015 1.5 christos
7016 1.1 christos /* Assign file positions for all the reloc sections which are not part
7017 1.14 christos of the loadable file image, and the file position of section headers. */
7018 1.6 christos
7019 1.1 christos static bool
7020 1.1 christos _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7021 1.6 christos {
7022 1.6 christos file_ptr off;
7023 1.5 christos Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7024 1.18 christos Elf_Internal_Shdr *shdrp;
7025 1.1 christos Elf_Internal_Ehdr *i_ehdrp;
7026 1.17 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7027 1.17 christos
7028 1.17 christos /* Skip non-load sections without section header. */
7029 1.17 christos if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7030 1.3 christos return true;
7031 1.1 christos
7032 1.6 christos off = elf_next_file_pos (abfd);
7033 1.6 christos
7034 1.6 christos shdrpp = elf_elfsections (abfd);
7035 1.1 christos end_shdrpp = shdrpp + elf_numsections (abfd);
7036 1.6 christos for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7037 1.6 christos {
7038 1.6 christos shdrp = *shdrpp;
7039 1.6 christos if (shdrp->sh_offset == -1)
7040 1.14 christos {
7041 1.14 christos asection *sec = shdrp->bfd_section;
7042 1.14 christos if (sec == NULL
7043 1.14 christos || shdrp->sh_type == SHT_REL
7044 1.14 christos || shdrp->sh_type == SHT_RELA)
7045 1.14 christos ;
7046 1.14 christos else if (bfd_section_is_ctf (sec))
7047 1.14 christos {
7048 1.14 christos /* Update section size and contents. */
7049 1.14 christos shdrp->sh_size = sec->size;
7050 1.14 christos shdrp->contents = sec->contents;
7051 1.14 christos }
7052 1.14 christos else if (shdrp->sh_name == -1u)
7053 1.14 christos {
7054 1.14 christos const char *name = sec->name;
7055 1.14 christos struct bfd_elf_section_data *d;
7056 1.14 christos
7057 1.14 christos /* Compress DWARF debug sections. */
7058 1.14 christos if (!bfd_compress_section (abfd, sec, shdrp->contents))
7059 1.14 christos return false;
7060 1.14 christos
7061 1.14 christos if (sec->compress_status == COMPRESS_SECTION_DONE
7062 1.14 christos && (abfd->flags & BFD_COMPRESS_GABI) == 0
7063 1.14 christos && name[1] == 'd')
7064 1.14 christos {
7065 1.14 christos /* If section is compressed with zlib-gnu, convert
7066 1.14 christos section name from .debug_* to .zdebug_*. */
7067 1.14 christos char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7068 1.14 christos if (new_name == NULL)
7069 1.14 christos return false;
7070 1.14 christos name = new_name;
7071 1.14 christos }
7072 1.14 christos /* Add section name to section name section. */
7073 1.14 christos shdrp->sh_name
7074 1.14 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7075 1.14 christos name, false);
7076 1.14 christos d = elf_section_data (sec);
7077 1.14 christos
7078 1.14 christos /* Add reloc section name to section name section. */
7079 1.14 christos if (d->rel.hdr
7080 1.14 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7081 1.14 christos name, false))
7082 1.14 christos return false;
7083 1.14 christos if (d->rela.hdr
7084 1.14 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7085 1.14 christos name, true))
7086 1.14 christos return false;
7087 1.14 christos
7088 1.14 christos /* Update section size and contents. */
7089 1.14 christos shdrp->sh_size = sec->size;
7090 1.14 christos shdrp->contents = sec->contents;
7091 1.1 christos sec->contents = NULL;
7092 1.18 christos }
7093 1.18 christos
7094 1.18 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7095 1.18 christos (abfd->flags & (EXEC_P | DYNAMIC))
7096 1.6 christos || bfd_get_format (abfd) == bfd_core,
7097 1.1 christos bed->s->log_file_align);
7098 1.1 christos }
7099 1.6 christos }
7100 1.6 christos
7101 1.6 christos /* Place section name section after DWARF debug sections have been
7102 1.6 christos compressed. */
7103 1.6 christos _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7104 1.18 christos shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7105 1.6 christos shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7106 1.6 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7107 1.5 christos
7108 1.18 christos /* Place the section headers. */
7109 1.5 christos i_ehdrp = elf_elfheader (abfd);
7110 1.5 christos off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7111 1.3 christos i_ehdrp->e_shoff = off;
7112 1.6 christos off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7113 1.14 christos elf_next_file_pos (abfd) = off;
7114 1.1 christos
7115 1.1 christos return true;
7116 1.14 christos }
7117 1.1 christos
7118 1.1 christos bool
7119 1.1 christos _bfd_elf_write_object_contents (bfd *abfd)
7120 1.1 christos {
7121 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7122 1.1 christos Elf_Internal_Shdr **i_shdrp;
7123 1.3 christos bool failed;
7124 1.1 christos unsigned int count, num_sec;
7125 1.1 christos struct elf_obj_tdata *t;
7126 1.1 christos
7127 1.14 christos if (! abfd->output_has_begun
7128 1.11 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7129 1.14 christos return false;
7130 1.14 christos /* Do not rewrite ELF data when the BFD has been opened for update.
7131 1.14 christos abfd->output_has_begun was set to TRUE on opening, so creation of
7132 1.14 christos new sections, and modification of existing section sizes was
7133 1.14 christos restricted. This means the ELF header, program headers and
7134 1.14 christos section headers can't have changed. If the contents of any
7135 1.11 christos sections has been modified, then those changes have already been
7136 1.11 christos written to the BFD. */
7137 1.11 christos else if (abfd->direction == both_direction)
7138 1.14 christos {
7139 1.11 christos BFD_ASSERT (abfd->output_has_begun);
7140 1.1 christos return true;
7141 1.1 christos }
7142 1.1 christos
7143 1.14 christos i_shdrp = elf_elfsections (abfd);
7144 1.1 christos
7145 1.1 christos failed = false;
7146 1.14 christos bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7147 1.1 christos if (failed)
7148 1.6 christos return false;
7149 1.14 christos
7150 1.1 christos if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7151 1.1 christos return false;
7152 1.1 christos
7153 1.1 christos /* After writing the headers, we need to write the sections too... */
7154 1.1 christos num_sec = elf_numsections (abfd);
7155 1.17 christos for (count = 1; count < num_sec; count++)
7156 1.17 christos {
7157 1.17 christos /* Don't set the sh_name field without section header. */
7158 1.17 christos if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7159 1.17 christos i_shdrp[count]->sh_name
7160 1.1 christos = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7161 1.11 christos i_shdrp[count]->sh_name);
7162 1.14 christos if (bed->elf_backend_section_processing)
7163 1.1 christos if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7164 1.1 christos return false;
7165 1.1 christos if (i_shdrp[count]->contents)
7166 1.1 christos {
7167 1.1 christos bfd_size_type amt = i_shdrp[count]->sh_size;
7168 1.17 christos
7169 1.14 christos if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7170 1.1 christos || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7171 1.1 christos return false;
7172 1.1 christos }
7173 1.1 christos }
7174 1.3 christos
7175 1.1 christos /* Write out the section header names. */
7176 1.17 christos t = elf_tdata (abfd);
7177 1.3 christos if (elf_shstrtab (abfd) != NULL
7178 1.1 christos && t->shstrtab_hdr.sh_offset != -1
7179 1.14 christos && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7180 1.1 christos || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7181 1.12 christos return false;
7182 1.14 christos
7183 1.1 christos if (!(*bed->elf_backend_final_write_processing) (abfd))
7184 1.1 christos return false;
7185 1.14 christos
7186 1.1 christos if (!bed->s->write_shdrs_and_ehdr (abfd))
7187 1.1 christos return false;
7188 1.14 christos
7189 1.14 christos /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7190 1.14 christos if (t->o->build_id.after_write_object_contents != NULL
7191 1.14 christos && !(*t->o->build_id.after_write_object_contents) (abfd))
7192 1.14 christos return false;
7193 1.14 christos if (t->o->package_metadata.after_write_object_contents != NULL
7194 1.1 christos && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7195 1.14 christos return false;
7196 1.1 christos
7197 1.1 christos return true;
7198 1.14 christos }
7199 1.1 christos
7200 1.1 christos bool
7201 1.1 christos _bfd_elf_write_corefile_contents (bfd *abfd)
7202 1.1 christos {
7203 1.1 christos /* Hopefully this can be done just like an object file. */
7204 1.1 christos return _bfd_elf_write_object_contents (abfd);
7205 1.1 christos }
7206 1.1 christos
7207 1.1 christos /* Given a section, search the header to find them. */
7208 1.1 christos
7209 1.1 christos unsigned int
7210 1.1 christos _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7211 1.1 christos {
7212 1.1 christos const struct elf_backend_data *bed;
7213 1.1 christos unsigned int sec_index;
7214 1.1 christos
7215 1.1 christos if (elf_section_data (asect) != NULL
7216 1.1 christos && elf_section_data (asect)->this_idx != 0)
7217 1.1 christos return elf_section_data (asect)->this_idx;
7218 1.1 christos
7219 1.1 christos if (bfd_is_abs_section (asect))
7220 1.1 christos sec_index = SHN_ABS;
7221 1.1 christos else if (bfd_is_com_section (asect))
7222 1.1 christos sec_index = SHN_COMMON;
7223 1.1 christos else if (bfd_is_und_section (asect))
7224 1.1 christos sec_index = SHN_UNDEF;
7225 1.1 christos else
7226 1.1 christos sec_index = SHN_BAD;
7227 1.1 christos
7228 1.1 christos bed = get_elf_backend_data (abfd);
7229 1.1 christos if (bed->elf_backend_section_from_bfd_section)
7230 1.1 christos {
7231 1.1 christos int retval = sec_index;
7232 1.1 christos
7233 1.1 christos if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7234 1.1 christos return retval;
7235 1.1 christos }
7236 1.1 christos
7237 1.1 christos if (sec_index == SHN_BAD)
7238 1.1 christos bfd_set_error (bfd_error_nonrepresentable_section);
7239 1.1 christos
7240 1.1 christos return sec_index;
7241 1.1 christos }
7242 1.1 christos
7243 1.1 christos /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7244 1.1 christos on error. */
7245 1.1 christos
7246 1.1 christos int
7247 1.1 christos _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7248 1.1 christos {
7249 1.1 christos asymbol *asym_ptr = *asym_ptr_ptr;
7250 1.1 christos int idx;
7251 1.1 christos flagword flags = asym_ptr->flags;
7252 1.1 christos
7253 1.1 christos /* When gas creates relocations against local labels, it creates its
7254 1.1 christos own symbol for the section, but does put the symbol into the
7255 1.1 christos symbol chain, so udata is 0. When the linker is generating
7256 1.1 christos relocatable output, this section symbol may be for one of the
7257 1.1 christos input sections rather than the output section. */
7258 1.1 christos if (asym_ptr->udata.i == 0
7259 1.1 christos && (flags & BSF_SECTION_SYM)
7260 1.1 christos && asym_ptr->section)
7261 1.1 christos {
7262 1.1 christos asection *sec;
7263 1.1 christos
7264 1.1 christos sec = asym_ptr->section;
7265 1.1 christos if (sec->owner != abfd && sec->output_section != NULL)
7266 1.14 christos sec = sec->output_section;
7267 1.14 christos if (sec->owner == abfd
7268 1.14 christos && sec->index < elf_num_section_syms (abfd)
7269 1.1 christos && elf_section_syms (abfd)[sec->index] != NULL)
7270 1.1 christos asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7271 1.1 christos }
7272 1.1 christos
7273 1.1 christos idx = asym_ptr->udata.i;
7274 1.1 christos
7275 1.1 christos if (idx == 0)
7276 1.1 christos {
7277 1.9 christos /* This case can occur when using --strip-symbol on a symbol
7278 1.9 christos which is used in a relocation entry. */
7279 1.11 christos _bfd_error_handler
7280 1.1 christos /* xgettext:c-format */
7281 1.1 christos (_("%pB: symbol `%s' required but not present"),
7282 1.1 christos abfd, bfd_asymbol_name (asym_ptr));
7283 1.1 christos bfd_set_error (bfd_error_no_symbols);
7284 1.1 christos return -1;
7285 1.1 christos }
7286 1.1 christos
7287 1.1 christos #if DEBUG & 4
7288 1.14 christos {
7289 1.14 christos fprintf (stderr,
7290 1.11 christos "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7291 1.1 christos " flags = 0x%.8x\n",
7292 1.1 christos (long) asym_ptr, asym_ptr->name, idx, flags);
7293 1.1 christos fflush (stderr);
7294 1.1 christos }
7295 1.1 christos #endif
7296 1.1 christos
7297 1.1 christos return idx;
7298 1.14 christos }
7299 1.14 christos
7300 1.14 christos static inline bfd_vma
7301 1.14 christos segment_size (Elf_Internal_Phdr *segment)
7302 1.14 christos {
7303 1.14 christos return (segment->p_memsz > segment->p_filesz
7304 1.14 christos ? segment->p_memsz : segment->p_filesz);
7305 1.14 christos }
7306 1.14 christos
7307 1.14 christos
7308 1.14 christos /* Returns the end address of the segment + 1. */
7309 1.14 christos static inline bfd_vma
7310 1.14 christos segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7311 1.14 christos {
7312 1.14 christos return start + segment_size (segment);
7313 1.14 christos }
7314 1.14 christos
7315 1.14 christos static inline bfd_size_type
7316 1.14 christos section_size (asection *section, Elf_Internal_Phdr *segment)
7317 1.14 christos {
7318 1.14 christos if ((section->flags & SEC_HAS_CONTENTS) != 0
7319 1.14 christos || (section->flags & SEC_THREAD_LOCAL) == 0
7320 1.14 christos || segment->p_type == PT_TLS)
7321 1.14 christos return section->size;
7322 1.14 christos return 0;
7323 1.14 christos }
7324 1.14 christos
7325 1.17 christos /* Returns TRUE if the given section is contained within the given
7326 1.14 christos segment. LMA addresses are compared against PADDR when
7327 1.14 christos USE_VADDR is false, VMA against VADDR when true. */
7328 1.14 christos static bool
7329 1.17 christos is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7330 1.14 christos bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7331 1.17 christos bool use_vaddr)
7332 1.17 christos {
7333 1.14 christos bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7334 1.14 christos bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7335 1.14 christos bfd_vma octet;
7336 1.14 christos if (_bfd_mul_overflow (addr, opb, &octet))
7337 1.14 christos return false;
7338 1.14 christos /* The third and fourth lines below are testing that the section end
7339 1.14 christos address is within the segment. It's written this way to avoid
7340 1.14 christos overflow. Add seg_addr + section_size to both sides of the
7341 1.14 christos inequality to make it obvious. */
7342 1.14 christos return (octet >= seg_addr
7343 1.14 christos && segment_size (segment) >= section_size (section, segment)
7344 1.14 christos && (octet - seg_addr
7345 1.14 christos <= segment_size (segment) - section_size (section, segment)));
7346 1.14 christos }
7347 1.14 christos
7348 1.14 christos /* Handle PT_NOTE segment. */
7349 1.14 christos static bool
7350 1.14 christos is_note (asection *s, Elf_Internal_Phdr *p)
7351 1.14 christos {
7352 1.14 christos return (p->p_type == PT_NOTE
7353 1.14 christos && elf_section_type (s) == SHT_NOTE
7354 1.14 christos && (ufile_ptr) s->filepos >= p->p_offset
7355 1.14 christos && p->p_filesz >= s->size
7356 1.14 christos && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7357 1.1 christos }
7358 1.1 christos
7359 1.14 christos /* Rewrite program header information. */
7360 1.14 christos
7361 1.1 christos static bool
7362 1.1 christos rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7363 1.1 christos {
7364 1.1 christos Elf_Internal_Ehdr *iehdr;
7365 1.1 christos struct elf_segment_map *map;
7366 1.1 christos struct elf_segment_map *map_first;
7367 1.1 christos struct elf_segment_map **pointer_to_map;
7368 1.1 christos Elf_Internal_Phdr *segment;
7369 1.1 christos asection *section;
7370 1.14 christos unsigned int i;
7371 1.14 christos unsigned int num_segments;
7372 1.1 christos bool phdr_included = false;
7373 1.1 christos bool p_paddr_valid;
7374 1.1 christos struct elf_segment_map *phdr_adjust_seg = NULL;
7375 1.12 christos unsigned int phdr_adjust_num = 0;
7376 1.1 christos const struct elf_backend_data *bed;
7377 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7378 1.1 christos
7379 1.1 christos bed = get_elf_backend_data (ibfd);
7380 1.1 christos iehdr = elf_elfheader (ibfd);
7381 1.1 christos
7382 1.1 christos map_first = NULL;
7383 1.1 christos pointer_to_map = &map_first;
7384 1.1 christos
7385 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum;
7386 1.1 christos
7387 1.1 christos /* The complicated case when p_vaddr is 0 is to handle the Solaris
7388 1.1 christos linker, which generates a PT_INTERP section with p_vaddr and
7389 1.1 christos p_memsz set to 0. */
7390 1.1 christos #define IS_SOLARIS_PT_INTERP(p, s) \
7391 1.1 christos (p->p_vaddr == 0 \
7392 1.1 christos && p->p_paddr == 0 \
7393 1.1 christos && p->p_memsz == 0 \
7394 1.1 christos && p->p_filesz > 0 \
7395 1.1 christos && (s->flags & SEC_HAS_CONTENTS) != 0 \
7396 1.1 christos && s->size > 0 \
7397 1.1 christos && (bfd_vma) s->filepos >= p->p_offset \
7398 1.1 christos && ((bfd_vma) s->filepos + s->size \
7399 1.1 christos <= p->p_offset + p->p_filesz))
7400 1.1 christos
7401 1.1 christos /* Decide if the given section should be included in the given segment.
7402 1.1 christos A section will be included if:
7403 1.1 christos 1. It is within the address space of the segment -- we use the LMA
7404 1.3 christos if that is set for the segment and the VMA otherwise,
7405 1.1 christos 2. It is an allocated section or a NOTE section in a PT_NOTE
7406 1.1 christos segment.
7407 1.1 christos 3. There is an output section associated with it,
7408 1.1 christos 4. The section has not already been allocated to a previous segment.
7409 1.1 christos 5. PT_GNU_STACK segments do not include any sections.
7410 1.1 christos 6. PT_TLS segment includes only SHF_TLS sections.
7411 1.1 christos 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7412 1.17 christos 8. PT_DYNAMIC should not contain empty sections at the beginning
7413 1.14 christos (with the possible exception of .dynamic). */
7414 1.17 christos #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7415 1.1 christos (((is_contained_by (section, segment, segment->p_paddr, \
7416 1.14 christos segment->p_vaddr, opb, !paddr_valid) \
7417 1.1 christos && (section->flags & SEC_ALLOC) != 0) \
7418 1.1 christos || is_note (section, segment)) \
7419 1.1 christos && segment->p_type != PT_GNU_STACK \
7420 1.1 christos && (segment->p_type != PT_TLS \
7421 1.1 christos || (section->flags & SEC_THREAD_LOCAL)) \
7422 1.1 christos && (segment->p_type == PT_LOAD \
7423 1.1 christos || segment->p_type == PT_TLS \
7424 1.14 christos || (section->flags & SEC_THREAD_LOCAL) == 0) \
7425 1.1 christos && (segment->p_type != PT_DYNAMIC \
7426 1.12 christos || section_size (section, segment) > 0 \
7427 1.12 christos || (segment->p_paddr \
7428 1.12 christos ? segment->p_paddr != section->lma * (opb) \
7429 1.11 christos : segment->p_vaddr != section->vma * (opb)) \
7430 1.1 christos || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7431 1.1 christos && (segment->p_type != PT_LOAD || !section->segment_mark))
7432 1.1 christos
7433 1.17 christos /* If the output section of a section in the input segment is NULL,
7434 1.17 christos it is removed from the corresponding output segment. */
7435 1.1 christos #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \
7436 1.1 christos (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7437 1.1 christos && section->output_section != NULL)
7438 1.1 christos
7439 1.14 christos /* Returns TRUE iff seg1 starts after the end of seg2. */
7440 1.1 christos #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7441 1.1 christos (seg1->field >= segment_end (seg2, seg2->field))
7442 1.1 christos
7443 1.1 christos /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7444 1.1 christos their VMA address ranges and their LMA address ranges overlap.
7445 1.1 christos It is possible to have overlapping VMA ranges without overlapping LMA
7446 1.1 christos ranges. RedBoot images for example can have both .data and .bss mapped
7447 1.1 christos to the same VMA range, but with the .data section mapped to a different
7448 1.1 christos LMA. */
7449 1.1 christos #define SEGMENT_OVERLAPS(seg1, seg2) \
7450 1.1 christos ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7451 1.1 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7452 1.1 christos && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7453 1.14 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7454 1.1 christos
7455 1.14 christos /* Initialise the segment mark field, and discard stupid alignment. */
7456 1.14 christos for (section = ibfd->sections; section != NULL; section = section->next)
7457 1.14 christos {
7458 1.14 christos asection *o = section->output_section;
7459 1.14 christos if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7460 1.14 christos o->alignment_power = 0;
7461 1.1 christos section->segment_mark = false;
7462 1.1 christos }
7463 1.1 christos
7464 1.1 christos /* The Solaris linker creates program headers in which all the
7465 1.1 christos p_paddr fields are zero. When we try to objcopy or strip such a
7466 1.14 christos file, we get confused. Check for this case, and if we find it
7467 1.1 christos don't set the p_paddr_valid fields. */
7468 1.1 christos p_paddr_valid = false;
7469 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7470 1.1 christos i < num_segments;
7471 1.1 christos i++, segment++)
7472 1.14 christos if (segment->p_paddr != 0)
7473 1.1 christos {
7474 1.1 christos p_paddr_valid = true;
7475 1.1 christos break;
7476 1.1 christos }
7477 1.1 christos
7478 1.1 christos /* Scan through the segments specified in the program header
7479 1.1 christos of the input BFD. For this first scan we look for overlaps
7480 1.1 christos in the loadable segments. These can be created by weird
7481 1.1 christos parameters to objcopy. Also, fix some solaris weirdness. */
7482 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7483 1.1 christos i < num_segments;
7484 1.1 christos i++, segment++)
7485 1.1 christos {
7486 1.1 christos unsigned int j;
7487 1.1 christos Elf_Internal_Phdr *segment2;
7488 1.1 christos
7489 1.1 christos if (segment->p_type == PT_INTERP)
7490 1.1 christos for (section = ibfd->sections; section; section = section->next)
7491 1.1 christos if (IS_SOLARIS_PT_INTERP (segment, section))
7492 1.1 christos {
7493 1.12 christos /* Mininal change so that the normal section to segment
7494 1.1 christos assignment code will work. */
7495 1.1 christos segment->p_vaddr = section->vma * opb;
7496 1.1 christos break;
7497 1.1 christos }
7498 1.1 christos
7499 1.1 christos if (segment->p_type != PT_LOAD)
7500 1.1 christos {
7501 1.1 christos /* Remove PT_GNU_RELRO segment. */
7502 1.1 christos if (segment->p_type == PT_GNU_RELRO)
7503 1.1 christos segment->p_type = PT_NULL;
7504 1.1 christos continue;
7505 1.1 christos }
7506 1.1 christos
7507 1.1 christos /* Determine if this segment overlaps any previous segments. */
7508 1.1 christos for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7509 1.1 christos {
7510 1.1 christos bfd_signed_vma extra_length;
7511 1.1 christos
7512 1.1 christos if (segment2->p_type != PT_LOAD
7513 1.1 christos || !SEGMENT_OVERLAPS (segment, segment2))
7514 1.1 christos continue;
7515 1.1 christos
7516 1.1 christos /* Merge the two segments together. */
7517 1.1 christos if (segment2->p_vaddr < segment->p_vaddr)
7518 1.1 christos {
7519 1.14 christos /* Extend SEGMENT2 to include SEGMENT and then delete
7520 1.14 christos SEGMENT. */
7521 1.1 christos extra_length = (segment_end (segment, segment->p_vaddr)
7522 1.1 christos - segment_end (segment2, segment2->p_vaddr));
7523 1.1 christos
7524 1.1 christos if (extra_length > 0)
7525 1.1 christos {
7526 1.1 christos segment2->p_memsz += extra_length;
7527 1.1 christos segment2->p_filesz += extra_length;
7528 1.1 christos }
7529 1.1 christos
7530 1.1 christos segment->p_type = PT_NULL;
7531 1.1 christos
7532 1.1 christos /* Since we have deleted P we must restart the outer loop. */
7533 1.1 christos i = 0;
7534 1.1 christos segment = elf_tdata (ibfd)->phdr;
7535 1.1 christos break;
7536 1.1 christos }
7537 1.1 christos else
7538 1.1 christos {
7539 1.14 christos /* Extend SEGMENT to include SEGMENT2 and then delete
7540 1.14 christos SEGMENT2. */
7541 1.1 christos extra_length = (segment_end (segment2, segment2->p_vaddr)
7542 1.1 christos - segment_end (segment, segment->p_vaddr));
7543 1.1 christos
7544 1.1 christos if (extra_length > 0)
7545 1.1 christos {
7546 1.1 christos segment->p_memsz += extra_length;
7547 1.1 christos segment->p_filesz += extra_length;
7548 1.1 christos }
7549 1.1 christos
7550 1.1 christos segment2->p_type = PT_NULL;
7551 1.1 christos }
7552 1.1 christos }
7553 1.1 christos }
7554 1.1 christos
7555 1.1 christos /* The second scan attempts to assign sections to segments. */
7556 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7557 1.1 christos i < num_segments;
7558 1.1 christos i++, segment++)
7559 1.1 christos {
7560 1.1 christos unsigned int section_count;
7561 1.1 christos asection **sections;
7562 1.11 christos asection *output_section;
7563 1.11 christos unsigned int isec;
7564 1.1 christos asection *matching_lma;
7565 1.12 christos asection *suggested_lma;
7566 1.1 christos unsigned int j;
7567 1.1 christos size_t amt;
7568 1.1 christos asection *first_section;
7569 1.1 christos
7570 1.1 christos if (segment->p_type == PT_NULL)
7571 1.1 christos continue;
7572 1.1 christos
7573 1.1 christos first_section = NULL;
7574 1.1 christos /* Compute how many sections might be placed into this segment. */
7575 1.1 christos for (section = ibfd->sections, section_count = 0;
7576 1.1 christos section != NULL;
7577 1.1 christos section = section->next)
7578 1.1 christos {
7579 1.17 christos /* Find the first section in the input segment, which may be
7580 1.1 christos removed from the corresponding output segment. */
7581 1.1 christos if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7582 1.1 christos {
7583 1.1 christos if (first_section == NULL)
7584 1.1 christos first_section = section;
7585 1.1 christos if (section->output_section != NULL)
7586 1.1 christos ++section_count;
7587 1.1 christos }
7588 1.1 christos }
7589 1.1 christos
7590 1.11 christos /* Allocate a segment map big enough to contain
7591 1.12 christos all of the sections we have selected. */
7592 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7593 1.1 christos amt += section_count * sizeof (asection *);
7594 1.14 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7595 1.1 christos if (map == NULL)
7596 1.1 christos return false;
7597 1.1 christos
7598 1.1 christos /* Initialise the fields of the segment map. Default to
7599 1.1 christos using the physical address of the segment in the input BFD. */
7600 1.1 christos map->next = NULL;
7601 1.1 christos map->p_type = segment->p_type;
7602 1.1 christos map->p_flags = segment->p_flags;
7603 1.14 christos map->p_flags_valid = 1;
7604 1.14 christos
7605 1.14 christos if (map->p_type == PT_LOAD
7606 1.14 christos && (ibfd->flags & D_PAGED) != 0
7607 1.14 christos && maxpagesize > 1
7608 1.14 christos && segment->p_align > 1)
7609 1.14 christos {
7610 1.14 christos map->p_align = segment->p_align;
7611 1.14 christos if (segment->p_align > maxpagesize)
7612 1.14 christos map->p_align = maxpagesize;
7613 1.14 christos map->p_align_valid = 1;
7614 1.1 christos }
7615 1.1 christos
7616 1.1 christos /* If the first section in the input segment is removed, there is
7617 1.1 christos no need to preserve segment physical address in the corresponding
7618 1.1 christos output segment. */
7619 1.1 christos if (!first_section || first_section->output_section != NULL)
7620 1.1 christos {
7621 1.1 christos map->p_paddr = segment->p_paddr;
7622 1.1 christos map->p_paddr_valid = p_paddr_valid;
7623 1.1 christos }
7624 1.1 christos
7625 1.1 christos /* Determine if this segment contains the ELF file header
7626 1.1 christos and if it contains the program headers themselves. */
7627 1.1 christos map->includes_filehdr = (segment->p_offset == 0
7628 1.1 christos && segment->p_filesz >= iehdr->e_ehsize);
7629 1.1 christos map->includes_phdrs = 0;
7630 1.1 christos
7631 1.1 christos if (!phdr_included || segment->p_type != PT_LOAD)
7632 1.1 christos {
7633 1.1 christos map->includes_phdrs =
7634 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7635 1.1 christos && (segment->p_offset + segment->p_filesz
7636 1.1 christos >= ((bfd_vma) iehdr->e_phoff
7637 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize)));
7638 1.14 christos
7639 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs)
7640 1.1 christos phdr_included = true;
7641 1.1 christos }
7642 1.1 christos
7643 1.1 christos if (section_count == 0)
7644 1.1 christos {
7645 1.1 christos /* Special segments, such as the PT_PHDR segment, may contain
7646 1.11 christos no sections, but ordinary, loadable segments should contain
7647 1.17 christos something. They are allowed by the ELF spec however, so only
7648 1.9 christos a warning is produced.
7649 1.9 christos Don't warn if an empty PT_LOAD contains the program headers.
7650 1.9 christos There is however the valid use case of embedded systems which
7651 1.9 christos have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7652 1.17 christos flash memory with zeros. No warning is shown for that case. */
7653 1.9 christos if (segment->p_type == PT_LOAD
7654 1.9 christos && !map->includes_phdrs
7655 1.11 christos && (segment->p_filesz > 0 || segment->p_memsz == 0))
7656 1.11 christos /* xgettext:c-format */
7657 1.11 christos _bfd_error_handler
7658 1.11 christos (_("%pB: warning: empty loadable segment detected"
7659 1.1 christos " at vaddr=%#" PRIx64 ", is this intentional?"),
7660 1.12 christos ibfd, (uint64_t) segment->p_vaddr);
7661 1.1 christos
7662 1.1 christos map->p_vaddr_offset = segment->p_vaddr / opb;
7663 1.1 christos map->count = 0;
7664 1.1 christos *pointer_to_map = map;
7665 1.1 christos pointer_to_map = &map->next;
7666 1.1 christos
7667 1.1 christos continue;
7668 1.1 christos }
7669 1.1 christos
7670 1.1 christos /* Now scan the sections in the input BFD again and attempt
7671 1.1 christos to add their corresponding output sections to the segment map.
7672 1.1 christos The problem here is how to handle an output section which has
7673 1.1 christos been moved (ie had its LMA changed). There are four possibilities:
7674 1.1 christos
7675 1.1 christos 1. None of the sections have been moved.
7676 1.1 christos In this case we can continue to use the segment LMA from the
7677 1.1 christos input BFD.
7678 1.1 christos
7679 1.1 christos 2. All of the sections have been moved by the same amount.
7680 1.1 christos In this case we can change the segment's LMA to match the LMA
7681 1.1 christos of the first section.
7682 1.1 christos
7683 1.1 christos 3. Some of the sections have been moved, others have not.
7684 1.1 christos In this case those sections which have not been moved can be
7685 1.1 christos placed in the current segment which will have to have its size,
7686 1.1 christos and possibly its LMA changed, and a new segment or segments will
7687 1.1 christos have to be created to contain the other sections.
7688 1.1 christos
7689 1.1 christos 4. The sections have been moved, but not by the same amount.
7690 1.1 christos In this case we can change the segment's LMA to match the LMA
7691 1.1 christos of the first section and we will have to create a new segment
7692 1.1 christos or segments to contain the other sections.
7693 1.1 christos
7694 1.1 christos In order to save time, we allocate an array to hold the section
7695 1.1 christos pointers that we are interested in. As these sections get assigned
7696 1.12 christos to a segment, they are removed from this array. */
7697 1.12 christos
7698 1.1 christos amt = section_count * sizeof (asection *);
7699 1.14 christos sections = (asection **) bfd_malloc (amt);
7700 1.1 christos if (sections == NULL)
7701 1.1 christos return false;
7702 1.1 christos
7703 1.1 christos /* Step One: Scan for segment vs section LMA conflicts.
7704 1.1 christos Also add the sections to the section array allocated above.
7705 1.1 christos Also add the sections to the current segment. In the common
7706 1.1 christos case, where the sections have not been moved, this means that
7707 1.1 christos we have completely filled the segment, and there is nothing
7708 1.11 christos more to do. */
7709 1.11 christos isec = 0;
7710 1.1 christos matching_lma = NULL;
7711 1.8 christos suggested_lma = NULL;
7712 1.1 christos
7713 1.1 christos for (section = first_section, j = 0;
7714 1.1 christos section != NULL;
7715 1.17 christos section = section->next)
7716 1.1 christos {
7717 1.1 christos if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7718 1.1 christos {
7719 1.1 christos output_section = section->output_section;
7720 1.1 christos
7721 1.1 christos sections[j++] = section;
7722 1.1 christos
7723 1.1 christos /* The Solaris native linker always sets p_paddr to 0.
7724 1.1 christos We try to catch that case here, and set it to the
7725 1.1 christos correct value. Note - some backends require that
7726 1.1 christos p_paddr be left as zero. */
7727 1.1 christos if (!p_paddr_valid
7728 1.1 christos && segment->p_vaddr != 0
7729 1.1 christos && !bed->want_p_paddr_set_to_zero
7730 1.11 christos && isec == 0
7731 1.11 christos && output_section->lma != 0
7732 1.11 christos && (align_power (segment->p_vaddr
7733 1.11 christos + (map->includes_filehdr
7734 1.11 christos ? iehdr->e_ehsize : 0)
7735 1.11 christos + (map->includes_phdrs
7736 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize
7737 1.12 christos : 0),
7738 1.1 christos output_section->alignment_power * opb)
7739 1.1 christos == (output_section->vma * opb)))
7740 1.1 christos map->p_paddr = segment->p_vaddr;
7741 1.1 christos
7742 1.14 christos /* Match up the physical address of the segment with the
7743 1.17 christos LMA address of the output section. */
7744 1.14 christos if (is_contained_by (output_section, segment, map->p_paddr,
7745 1.1 christos 0, opb, false)
7746 1.11 christos || is_note (section, segment))
7747 1.11 christos {
7748 1.11 christos if (matching_lma == NULL
7749 1.1 christos || output_section->lma < matching_lma->lma)
7750 1.1 christos matching_lma = output_section;
7751 1.1 christos
7752 1.1 christos /* We assume that if the section fits within the segment
7753 1.1 christos then it does not overlap any other section within that
7754 1.1 christos segment. */
7755 1.11 christos map->sections[isec++] = output_section;
7756 1.11 christos }
7757 1.1 christos else if (suggested_lma == NULL)
7758 1.1 christos suggested_lma = output_section;
7759 1.1 christos
7760 1.1 christos if (j == section_count)
7761 1.1 christos break;
7762 1.1 christos }
7763 1.1 christos }
7764 1.1 christos
7765 1.1 christos BFD_ASSERT (j == section_count);
7766 1.1 christos
7767 1.1 christos /* Step Two: Adjust the physical address of the current segment,
7768 1.1 christos if necessary. */
7769 1.1 christos if (isec == section_count)
7770 1.1 christos {
7771 1.1 christos /* All of the sections fitted within the segment as currently
7772 1.1 christos specified. This is the default case. Add the segment to
7773 1.1 christos the list of built segments and carry on to process the next
7774 1.1 christos program header in the input BFD. */
7775 1.1 christos map->count = section_count;
7776 1.1 christos *pointer_to_map = map;
7777 1.1 christos pointer_to_map = &map->next;
7778 1.12 christos
7779 1.12 christos if (p_paddr_valid
7780 1.12 christos && !bed->want_p_paddr_set_to_zero)
7781 1.12 christos {
7782 1.12 christos bfd_vma hdr_size = 0;
7783 1.12 christos if (map->includes_filehdr)
7784 1.12 christos hdr_size = iehdr->e_ehsize;
7785 1.12 christos if (map->includes_phdrs)
7786 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7787 1.12 christos
7788 1.12 christos /* Account for padding before the first section in the
7789 1.12 christos segment. */
7790 1.12 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7791 1.1 christos - matching_lma->lma);
7792 1.1 christos }
7793 1.1 christos
7794 1.1 christos free (sections);
7795 1.1 christos continue;
7796 1.1 christos }
7797 1.11 christos else
7798 1.11 christos {
7799 1.11 christos /* Change the current segment's physical address to match
7800 1.11 christos the LMA of the first section that fitted, or if no
7801 1.11 christos section fitted, the first section. */
7802 1.11 christos if (matching_lma == NULL)
7803 1.12 christos matching_lma = suggested_lma;
7804 1.1 christos
7805 1.1 christos map->p_paddr = matching_lma->lma * opb;
7806 1.1 christos
7807 1.11 christos /* Offset the segment physical address from the lma
7808 1.1 christos to allow for space taken up by elf headers. */
7809 1.11 christos if (map->includes_phdrs)
7810 1.11 christos {
7811 1.11 christos map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7812 1.11 christos
7813 1.11 christos /* iehdr->e_phnum is just an estimate of the number
7814 1.11 christos of program headers that we will need. Make a note
7815 1.11 christos here of the number we used and the segment we chose
7816 1.11 christos to hold these headers, so that we can adjust the
7817 1.11 christos offset when we know the correct value. */
7818 1.1 christos phdr_adjust_num = iehdr->e_phnum;
7819 1.1 christos phdr_adjust_seg = map;
7820 1.11 christos }
7821 1.1 christos
7822 1.11 christos if (map->includes_filehdr)
7823 1.11 christos {
7824 1.11 christos bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7825 1.11 christos map->p_paddr -= iehdr->e_ehsize;
7826 1.11 christos /* We've subtracted off the size of headers from the
7827 1.11 christos first section lma, but there may have been some
7828 1.11 christos alignment padding before that section too. Try to
7829 1.11 christos account for that by adjusting the segment lma down to
7830 1.11 christos the same alignment. */
7831 1.12 christos if (segment->p_align != 0 && segment->p_align < align)
7832 1.1 christos align = segment->p_align;
7833 1.1 christos map->p_paddr &= -(align * opb);
7834 1.1 christos }
7835 1.1 christos }
7836 1.1 christos
7837 1.1 christos /* Step Three: Loop over the sections again, this time assigning
7838 1.1 christos those that fit to the current segment and removing them from the
7839 1.1 christos sections array; but making sure not to leave large gaps. Once all
7840 1.1 christos possible sections have been assigned to the current segment it is
7841 1.1 christos added to the list of built segments and if sections still remain
7842 1.1 christos to be assigned, a new segment is constructed before repeating
7843 1.1 christos the loop. */
7844 1.1 christos isec = 0;
7845 1.1 christos do
7846 1.11 christos {
7847 1.1 christos map->count = 0;
7848 1.1 christos suggested_lma = NULL;
7849 1.1 christos
7850 1.1 christos /* Fill the current segment with sections that fit. */
7851 1.1 christos for (j = 0; j < section_count; j++)
7852 1.1 christos {
7853 1.1 christos section = sections[j];
7854 1.1 christos
7855 1.1 christos if (section == NULL)
7856 1.1 christos continue;
7857 1.1 christos
7858 1.1 christos output_section = section->output_section;
7859 1.1 christos
7860 1.14 christos BFD_ASSERT (output_section != NULL);
7861 1.17 christos
7862 1.14 christos if (is_contained_by (output_section, segment, map->p_paddr,
7863 1.1 christos 0, opb, false)
7864 1.1 christos || is_note (section, segment))
7865 1.1 christos {
7866 1.1 christos if (map->count == 0)
7867 1.1 christos {
7868 1.1 christos /* If the first section in a segment does not start at
7869 1.11 christos the beginning of the segment, then something is
7870 1.11 christos wrong. */
7871 1.11 christos if (align_power (map->p_paddr
7872 1.11 christos + (map->includes_filehdr
7873 1.11 christos ? iehdr->e_ehsize : 0)
7874 1.11 christos + (map->includes_phdrs
7875 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize
7876 1.12 christos : 0),
7877 1.12 christos output_section->alignment_power * opb)
7878 1.1 christos != output_section->lma * opb)
7879 1.1 christos goto sorry;
7880 1.1 christos }
7881 1.1 christos else
7882 1.1 christos {
7883 1.1 christos asection *prev_sec;
7884 1.1 christos
7885 1.1 christos prev_sec = map->sections[map->count - 1];
7886 1.1 christos
7887 1.1 christos /* If the gap between the end of the previous section
7888 1.1 christos and the start of this section is more than
7889 1.1 christos maxpagesize then we need to start a new segment. */
7890 1.1 christos if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7891 1.1 christos maxpagesize)
7892 1.1 christos < BFD_ALIGN (output_section->lma, maxpagesize))
7893 1.1 christos || (prev_sec->lma + prev_sec->size
7894 1.11 christos > output_section->lma))
7895 1.11 christos {
7896 1.1 christos if (suggested_lma == NULL)
7897 1.1 christos suggested_lma = output_section;
7898 1.1 christos
7899 1.1 christos continue;
7900 1.1 christos }
7901 1.1 christos }
7902 1.1 christos
7903 1.1 christos map->sections[map->count++] = output_section;
7904 1.11 christos ++isec;
7905 1.14 christos sections[j] = NULL;
7906 1.1 christos if (segment->p_type == PT_LOAD)
7907 1.11 christos section->segment_mark = true;
7908 1.11 christos }
7909 1.1 christos else if (suggested_lma == NULL)
7910 1.1 christos suggested_lma = output_section;
7911 1.11 christos }
7912 1.11 christos
7913 1.12 christos /* PR 23932. A corrupt input file may contain sections that cannot
7914 1.12 christos be assigned to any segment - because for example they have a
7915 1.12 christos negative size - or segments that do not contain any sections.
7916 1.1 christos But there are also valid reasons why a segment can be empty.
7917 1.1 christos So allow a count of zero. */
7918 1.1 christos
7919 1.1 christos /* Add the current segment to the list of built segments. */
7920 1.1 christos *pointer_to_map = map;
7921 1.1 christos pointer_to_map = &map->next;
7922 1.1 christos
7923 1.1 christos if (isec < section_count)
7924 1.1 christos {
7925 1.1 christos /* We still have not allocated all of the sections to
7926 1.11 christos segments. Create a new segment here, initialise it
7927 1.12 christos and carry on looping. */
7928 1.3 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7929 1.1 christos amt += section_count * sizeof (asection *);
7930 1.1 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7931 1.1 christos if (map == NULL)
7932 1.14 christos {
7933 1.1 christos free (sections);
7934 1.1 christos return false;
7935 1.1 christos }
7936 1.1 christos
7937 1.1 christos /* Initialise the fields of the segment map. Set the physical
7938 1.1 christos physical address to the LMA of the first section that has
7939 1.1 christos not yet been assigned. */
7940 1.1 christos map->next = NULL;
7941 1.1 christos map->p_type = segment->p_type;
7942 1.12 christos map->p_flags = segment->p_flags;
7943 1.1 christos map->p_flags_valid = 1;
7944 1.1 christos map->p_paddr = suggested_lma->lma * opb;
7945 1.1 christos map->p_paddr_valid = p_paddr_valid;
7946 1.1 christos map->includes_filehdr = 0;
7947 1.12 christos map->includes_phdrs = 0;
7948 1.12 christos }
7949 1.12 christos
7950 1.12 christos continue;
7951 1.12 christos sorry:
7952 1.14 christos bfd_set_error (bfd_error_sorry);
7953 1.1 christos free (sections);
7954 1.1 christos return false;
7955 1.1 christos }
7956 1.1 christos while (isec < section_count);
7957 1.1 christos
7958 1.1 christos free (sections);
7959 1.3 christos }
7960 1.1 christos
7961 1.1 christos elf_seg_map (obfd) = map_first;
7962 1.1 christos
7963 1.1 christos /* If we had to estimate the number of program headers that were
7964 1.1 christos going to be needed, then check our estimate now and adjust
7965 1.1 christos the offset if necessary. */
7966 1.1 christos if (phdr_adjust_seg != NULL)
7967 1.1 christos {
7968 1.1 christos unsigned int count;
7969 1.1 christos
7970 1.1 christos for (count = 0, map = map_first; map != NULL; map = map->next)
7971 1.1 christos count++;
7972 1.1 christos
7973 1.1 christos if (count > phdr_adjust_num)
7974 1.11 christos phdr_adjust_seg->p_paddr
7975 1.11 christos -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7976 1.11 christos
7977 1.11 christos for (map = map_first; map != NULL; map = map->next)
7978 1.11 christos if (map->p_type == PT_PHDR)
7979 1.11 christos {
7980 1.11 christos bfd_vma adjust
7981 1.11 christos = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7982 1.11 christos map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7983 1.1 christos break;
7984 1.1 christos }
7985 1.1 christos }
7986 1.1 christos
7987 1.1 christos #undef IS_SOLARIS_PT_INTERP
7988 1.1 christos #undef IS_SECTION_IN_INPUT_SEGMENT
7989 1.1 christos #undef INCLUDE_SECTION_IN_SEGMENT
7990 1.14 christos #undef SEGMENT_AFTER_SEGMENT
7991 1.14 christos #undef SEGMENT_OVERLAPS
7992 1.14 christos return true;
7993 1.14 christos }
7994 1.14 christos
7995 1.14 christos /* Return true if p_align in the ELF program header in ABFD is valid. */
7996 1.14 christos
7997 1.14 christos static bool
7998 1.14 christos elf_is_p_align_valid (bfd *abfd)
7999 1.14 christos {
8000 1.14 christos unsigned int i;
8001 1.14 christos Elf_Internal_Phdr *segment;
8002 1.14 christos unsigned int num_segments;
8003 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8004 1.14 christos bfd_size_type maxpagesize = bed->maxpagesize;
8005 1.14 christos bfd_size_type p_align = bed->p_align;
8006 1.14 christos
8007 1.14 christos /* Return true if the default p_align value isn't set or the maximum
8008 1.14 christos page size is the same as the minimum page size. */
8009 1.14 christos if (p_align == 0 || maxpagesize == bed->minpagesize)
8010 1.14 christos return true;
8011 1.14 christos
8012 1.14 christos /* When the default p_align value is set, p_align may be set to the
8013 1.14 christos default p_align value while segments are aligned to the maximum
8014 1.14 christos page size. In this case, the input p_align will be ignored and
8015 1.14 christos the maximum page size will be used to align the output segments. */
8016 1.14 christos segment = elf_tdata (abfd)->phdr;
8017 1.14 christos num_segments = elf_elfheader (abfd)->e_phnum;
8018 1.14 christos for (i = 0; i < num_segments; i++, segment++)
8019 1.14 christos if (segment->p_type == PT_LOAD
8020 1.14 christos && (segment->p_align != p_align
8021 1.14 christos || vma_page_aligned_bias (segment->p_vaddr,
8022 1.14 christos segment->p_offset,
8023 1.14 christos maxpagesize) != 0))
8024 1.14 christos return true;
8025 1.1 christos
8026 1.1 christos return false;
8027 1.1 christos }
8028 1.1 christos
8029 1.14 christos /* Copy ELF program header information. */
8030 1.1 christos
8031 1.1 christos static bool
8032 1.1 christos copy_elf_program_header (bfd *ibfd, bfd *obfd)
8033 1.1 christos {
8034 1.1 christos Elf_Internal_Ehdr *iehdr;
8035 1.1 christos struct elf_segment_map *map;
8036 1.1 christos struct elf_segment_map *map_first;
8037 1.1 christos struct elf_segment_map **pointer_to_map;
8038 1.1 christos Elf_Internal_Phdr *segment;
8039 1.14 christos unsigned int i;
8040 1.14 christos unsigned int num_segments;
8041 1.14 christos bool phdr_included = false;
8042 1.12 christos bool p_paddr_valid;
8043 1.1 christos bool p_palign_valid;
8044 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8045 1.1 christos
8046 1.1 christos iehdr = elf_elfheader (ibfd);
8047 1.1 christos
8048 1.1 christos map_first = NULL;
8049 1.1 christos pointer_to_map = &map_first;
8050 1.1 christos
8051 1.14 christos /* If all the segment p_paddr fields are zero, don't set
8052 1.1 christos map->p_paddr_valid. */
8053 1.1 christos p_paddr_valid = false;
8054 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum;
8055 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
8056 1.1 christos i < num_segments;
8057 1.1 christos i++, segment++)
8058 1.14 christos if (segment->p_paddr != 0)
8059 1.1 christos {
8060 1.1 christos p_paddr_valid = true;
8061 1.1 christos break;
8062 1.14 christos }
8063 1.14 christos
8064 1.1 christos p_palign_valid = elf_is_p_align_valid (ibfd);
8065 1.1 christos
8066 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
8067 1.1 christos i < num_segments;
8068 1.1 christos i++, segment++)
8069 1.1 christos {
8070 1.12 christos asection *section;
8071 1.1 christos unsigned int section_count;
8072 1.1 christos size_t amt;
8073 1.1 christos Elf_Internal_Shdr *this_hdr;
8074 1.1 christos asection *first_section = NULL;
8075 1.1 christos asection *lowest_section;
8076 1.1 christos
8077 1.1 christos /* Compute how many sections are in this segment. */
8078 1.1 christos for (section = ibfd->sections, section_count = 0;
8079 1.1 christos section != NULL;
8080 1.1 christos section = section->next)
8081 1.1 christos {
8082 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr);
8083 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8084 1.1 christos {
8085 1.1 christos if (first_section == NULL)
8086 1.1 christos first_section = section;
8087 1.1 christos section_count++;
8088 1.1 christos }
8089 1.1 christos }
8090 1.1 christos
8091 1.11 christos /* Allocate a segment map big enough to contain
8092 1.12 christos all of the sections we have selected. */
8093 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8094 1.1 christos amt += section_count * sizeof (asection *);
8095 1.14 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8096 1.1 christos if (map == NULL)
8097 1.1 christos return false;
8098 1.1 christos
8099 1.1 christos /* Initialize the fields of the output segment map with the
8100 1.1 christos input segment. */
8101 1.1 christos map->next = NULL;
8102 1.1 christos map->p_type = segment->p_type;
8103 1.1 christos map->p_flags = segment->p_flags;
8104 1.1 christos map->p_flags_valid = 1;
8105 1.1 christos map->p_paddr = segment->p_paddr;
8106 1.14 christos map->p_paddr_valid = p_paddr_valid;
8107 1.14 christos map->p_align = segment->p_align;
8108 1.14 christos /* Keep p_align of PT_GNU_STACK for stack alignment. */
8109 1.1 christos map->p_align_valid = (map->p_type == PT_GNU_STACK
8110 1.1 christos || p_palign_valid);
8111 1.3 christos map->p_vaddr_offset = 0;
8112 1.3 christos
8113 1.1 christos if (map->p_type == PT_GNU_RELRO
8114 1.1 christos || map->p_type == PT_GNU_STACK)
8115 1.1 christos {
8116 1.1 christos /* The PT_GNU_RELRO segment may contain the first a few
8117 1.3 christos bytes in the .got.plt section even if the whole .got.plt
8118 1.3 christos section isn't in the PT_GNU_RELRO segment. We won't
8119 1.3 christos change the size of the PT_GNU_RELRO segment.
8120 1.1 christos Similarly, PT_GNU_STACK size is significant on uclinux
8121 1.1 christos systems. */
8122 1.1 christos map->p_size = segment->p_memsz;
8123 1.1 christos map->p_size_valid = 1;
8124 1.1 christos }
8125 1.1 christos
8126 1.1 christos /* Determine if this segment contains the ELF file header
8127 1.1 christos and if it contains the program headers themselves. */
8128 1.1 christos map->includes_filehdr = (segment->p_offset == 0
8129 1.1 christos && segment->p_filesz >= iehdr->e_ehsize);
8130 1.1 christos
8131 1.1 christos map->includes_phdrs = 0;
8132 1.1 christos if (! phdr_included || segment->p_type != PT_LOAD)
8133 1.1 christos {
8134 1.1 christos map->includes_phdrs =
8135 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8136 1.1 christos && (segment->p_offset + segment->p_filesz
8137 1.1 christos >= ((bfd_vma) iehdr->e_phoff
8138 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize)));
8139 1.14 christos
8140 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs)
8141 1.1 christos phdr_included = true;
8142 1.5 christos }
8143 1.1 christos
8144 1.1 christos lowest_section = NULL;
8145 1.1 christos if (section_count != 0)
8146 1.1 christos {
8147 1.1 christos unsigned int isec = 0;
8148 1.1 christos
8149 1.1 christos for (section = first_section;
8150 1.1 christos section != NULL;
8151 1.1 christos section = section->next)
8152 1.1 christos {
8153 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr);
8154 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8155 1.1 christos {
8156 1.1 christos map->sections[isec++] = section->output_section;
8157 1.1 christos if ((section->flags & SEC_ALLOC) != 0)
8158 1.1 christos {
8159 1.5 christos bfd_vma seg_off;
8160 1.5 christos
8161 1.4 christos if (lowest_section == NULL
8162 1.4 christos || section->lma < lowest_section->lma)
8163 1.1 christos lowest_section = section;
8164 1.1 christos
8165 1.1 christos /* Section lmas are set up from PT_LOAD header
8166 1.1 christos p_paddr in _bfd_elf_make_section_from_shdr.
8167 1.1 christos If this header has a p_paddr that disagrees
8168 1.1 christos with the section lma, flag the p_paddr as
8169 1.1 christos invalid. */
8170 1.1 christos if ((section->flags & SEC_LOAD) != 0)
8171 1.1 christos seg_off = this_hdr->sh_offset - segment->p_offset;
8172 1.12 christos else
8173 1.14 christos seg_off = this_hdr->sh_addr - segment->p_vaddr;
8174 1.1 christos if (section->lma * opb - segment->p_paddr != seg_off)
8175 1.1 christos map->p_paddr_valid = false;
8176 1.1 christos }
8177 1.1 christos if (isec == section_count)
8178 1.1 christos break;
8179 1.1 christos }
8180 1.1 christos }
8181 1.12 christos }
8182 1.12 christos
8183 1.12 christos if (section_count == 0)
8184 1.11 christos map->p_vaddr_offset = segment->p_vaddr / opb;
8185 1.12 christos else if (map->p_paddr_valid)
8186 1.12 christos {
8187 1.12 christos /* Account for padding before the first section in the segment. */
8188 1.12 christos bfd_vma hdr_size = 0;
8189 1.12 christos if (map->includes_filehdr)
8190 1.12 christos hdr_size = iehdr->e_ehsize;
8191 1.12 christos if (map->includes_phdrs)
8192 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8193 1.12 christos
8194 1.11 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8195 1.3 christos - (lowest_section ? lowest_section->lma : 0));
8196 1.1 christos }
8197 1.1 christos
8198 1.1 christos map->count = section_count;
8199 1.1 christos *pointer_to_map = map;
8200 1.1 christos pointer_to_map = &map->next;
8201 1.3 christos }
8202 1.14 christos
8203 1.1 christos elf_seg_map (obfd) = map_first;
8204 1.1 christos return true;
8205 1.1 christos }
8206 1.1 christos
8207 1.1 christos /* Copy private BFD data. This copies or rewrites ELF program header
8208 1.14 christos information. */
8209 1.1 christos
8210 1.1 christos static bool
8211 1.14 christos copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8212 1.14 christos {
8213 1.1 christos bfd_vma maxpagesize;
8214 1.1 christos
8215 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8216 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8217 1.1 christos return true;
8218 1.14 christos
8219 1.1 christos if (elf_tdata (ibfd)->phdr == NULL)
8220 1.1 christos return true;
8221 1.1 christos
8222 1.1 christos if (ibfd->xvec == obfd->xvec)
8223 1.1 christos {
8224 1.1 christos /* Check to see if any sections in the input BFD
8225 1.17 christos covered by ELF program header have changed. */
8226 1.17 christos Elf_Internal_Phdr *segment;
8227 1.17 christos asection * section;
8228 1.1 christos asection * osec;
8229 1.1 christos asection * prev;
8230 1.1 christos unsigned int i, num_segments;
8231 1.1 christos Elf_Internal_Shdr *this_hdr;
8232 1.1 christos const struct elf_backend_data *bed;
8233 1.1 christos
8234 1.1 christos bed = get_elf_backend_data (ibfd);
8235 1.1 christos
8236 1.1 christos /* Regenerate the segment map if p_paddr is set to 0. */
8237 1.1 christos if (bed->want_p_paddr_set_to_zero)
8238 1.1 christos goto rewrite;
8239 1.1 christos
8240 1.1 christos /* Initialize the segment mark field. */
8241 1.14 christos for (section = obfd->sections; section != NULL;
8242 1.1 christos section = section->next)
8243 1.1 christos section->segment_mark = false;
8244 1.1 christos
8245 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum;
8246 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
8247 1.1 christos i < num_segments;
8248 1.1 christos i++, segment++)
8249 1.1 christos {
8250 1.1 christos /* PR binutils/3535. The Solaris linker always sets the p_paddr
8251 1.1 christos and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8252 1.1 christos which severly confuses things, so always regenerate the segment
8253 1.1 christos map in this case. */
8254 1.14 christos if (segment->p_paddr == 0
8255 1.14 christos && segment->p_memsz == 0
8256 1.1 christos && (segment->p_type == PT_INTERP
8257 1.1 christos || segment->p_type == PT_DYNAMIC))
8258 1.17 christos goto rewrite;
8259 1.1 christos
8260 1.1 christos for (section = ibfd->sections, prev = NULL;
8261 1.1 christos section != NULL; section = section->next)
8262 1.1 christos {
8263 1.1 christos /* We mark the output section so that we know it comes
8264 1.1 christos from the input BFD. */
8265 1.14 christos osec = section->output_section;
8266 1.1 christos if (osec)
8267 1.1 christos osec->segment_mark = true;
8268 1.1 christos
8269 1.1 christos /* Check if this section is covered by the segment. */
8270 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr);
8271 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8272 1.1 christos {
8273 1.1 christos /* FIXME: Check if its output section is changed or
8274 1.1 christos removed. What else do we need to check? */
8275 1.1 christos if (osec == NULL
8276 1.1 christos || section->flags != osec->flags
8277 1.1 christos || section->lma != osec->lma
8278 1.1 christos || section->vma != osec->vma
8279 1.1 christos || section->size != osec->size
8280 1.1 christos || section->rawsize != osec->rawsize
8281 1.17 christos || section->alignment_power != osec->alignment_power)
8282 1.17 christos goto rewrite;
8283 1.17 christos
8284 1.17 christos /* PR 31450: If this is an allocated section then make sure
8285 1.17 christos that this section's vma to lma relationship is the same
8286 1.17 christos as previous (allocated) section's. */
8287 1.17 christos if (prev != NULL
8288 1.17 christos && section->flags & SEC_ALLOC
8289 1.17 christos && section->lma - section->vma != prev->lma - prev->vma)
8290 1.17 christos goto rewrite;
8291 1.17 christos
8292 1.1 christos if (section->flags & SEC_ALLOC)
8293 1.1 christos prev = section;
8294 1.1 christos }
8295 1.1 christos }
8296 1.1 christos }
8297 1.1 christos
8298 1.1 christos /* Check to see if any output section do not come from the
8299 1.1 christos input BFD. */
8300 1.1 christos for (section = obfd->sections; section != NULL;
8301 1.11 christos section = section->next)
8302 1.1 christos {
8303 1.1 christos if (!section->segment_mark)
8304 1.14 christos goto rewrite;
8305 1.1 christos else
8306 1.1 christos section->segment_mark = false;
8307 1.1 christos }
8308 1.1 christos
8309 1.1 christos return copy_elf_program_header (ibfd, obfd);
8310 1.12 christos }
8311 1.14 christos
8312 1.3 christos rewrite:
8313 1.3 christos maxpagesize = 0;
8314 1.3 christos if (ibfd->xvec == obfd->xvec)
8315 1.3 christos {
8316 1.3 christos /* When rewriting program header, set the output maxpagesize to
8317 1.3 christos the maximum alignment of input PT_LOAD segments. */
8318 1.3 christos Elf_Internal_Phdr *segment;
8319 1.3 christos unsigned int i;
8320 1.3 christos unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8321 1.3 christos
8322 1.3 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
8323 1.3 christos i < num_segments;
8324 1.3 christos i++, segment++)
8325 1.6 christos if (segment->p_type == PT_LOAD
8326 1.6 christos && maxpagesize < segment->p_align)
8327 1.6 christos {
8328 1.9 christos /* PR 17512: file: f17299af. */
8329 1.11 christos if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8330 1.11 christos /* xgettext:c-format */
8331 1.11 christos _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8332 1.6 christos PRIx64 " is too large"),
8333 1.6 christos ibfd, (uint64_t) segment->p_align);
8334 1.6 christos else
8335 1.3 christos maxpagesize = segment->p_align;
8336 1.14 christos }
8337 1.14 christos }
8338 1.3 christos if (maxpagesize == 0)
8339 1.14 christos maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8340 1.1 christos
8341 1.1 christos return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8342 1.1 christos }
8343 1.1 christos
8344 1.14 christos /* Initialize private output section information from input section. */
8345 1.1 christos
8346 1.1 christos bool
8347 1.1 christos _bfd_elf_init_private_section_data (bfd *ibfd,
8348 1.1 christos asection *isec,
8349 1.1 christos bfd *obfd,
8350 1.1 christos asection *osec,
8351 1.1 christos struct bfd_link_info *link_info)
8352 1.1 christos
8353 1.14 christos {
8354 1.14 christos Elf_Internal_Shdr *ihdr, *ohdr;
8355 1.1 christos bool final_link = (link_info != NULL
8356 1.1 christos && !bfd_link_relocatable (link_info));
8357 1.1 christos
8358 1.14 christos if (ibfd->xvec->flavour != bfd_target_elf_flavour
8359 1.1 christos || obfd->xvec->flavour != bfd_target_elf_flavour)
8360 1.3 christos return true;
8361 1.3 christos
8362 1.12 christos BFD_ASSERT (elf_section_data (osec) != NULL);
8363 1.12 christos
8364 1.12 christos /* If this is a known ABI section, ELF section type and flags may
8365 1.12 christos have been set up when OSEC was created. For normal sections we
8366 1.12 christos allow the user to override the type and flags other than
8367 1.12 christos SHF_MASKOS and SHF_MASKPROC. */
8368 1.12 christos if (elf_section_type (osec) == SHT_PROGBITS
8369 1.12 christos || elf_section_type (osec) == SHT_NOTE
8370 1.12 christos || elf_section_type (osec) == SHT_NOBITS)
8371 1.12 christos elf_section_type (osec) = SHT_NULL;
8372 1.12 christos /* For objcopy and relocatable link, copy the ELF section type from
8373 1.12 christos the input file if the BFD section flags are the same. (If they
8374 1.12 christos are different the user may be doing something like
8375 1.1 christos "objcopy --set-section-flags .text=alloc,data".) For a final
8376 1.1 christos link allow some flags that the linker clears to differ. */
8377 1.1 christos if (elf_section_type (osec) == SHT_NULL
8378 1.1 christos && (osec->flags == isec->flags
8379 1.1 christos || (final_link
8380 1.1 christos && ((osec->flags ^ isec->flags)
8381 1.1 christos & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8382 1.1 christos elf_section_type (osec) = elf_section_type (isec);
8383 1.12 christos
8384 1.12 christos /* FIXME: Is this correct for all OS/PROC specific flags? */
8385 1.1 christos elf_section_flags (osec) = (elf_section_flags (isec)
8386 1.9 christos & (SHF_MASKOS | SHF_MASKPROC));
8387 1.12 christos
8388 1.12 christos /* Copy sh_info from input for mbind section. */
8389 1.9 christos if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8390 1.9 christos && elf_section_flags (isec) & SHF_GNU_MBIND)
8391 1.9 christos elf_section_data (osec)->this_hdr.sh_info
8392 1.1 christos = elf_section_data (isec)->this_hdr.sh_info;
8393 1.1 christos
8394 1.1 christos /* Set things up for objcopy and relocatable link. The output
8395 1.1 christos SHT_GROUP section will have its elf_next_in_group pointing back
8396 1.11 christos to the input group members. Ignore linker created group section.
8397 1.11 christos See elfNN_ia64_object_p in elfxx-ia64.c. */
8398 1.11 christos if ((link_info == NULL
8399 1.11 christos || !link_info->resolve_section_groups)
8400 1.11 christos && (elf_sec_group (isec) == NULL
8401 1.11 christos || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8402 1.11 christos {
8403 1.11 christos if (elf_section_flags (isec) & SHF_GROUP)
8404 1.11 christos elf_section_flags (osec) |= SHF_GROUP;
8405 1.11 christos elf_next_in_group (osec) = elf_next_in_group (isec);
8406 1.6 christos elf_section_data (osec)->group = elf_section_data (isec)->group;
8407 1.11 christos }
8408 1.11 christos
8409 1.11 christos /* If not decompress, preserve SHF_COMPRESSED. */
8410 1.11 christos if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8411 1.1 christos elf_section_flags (osec) |= (elf_section_flags (isec)
8412 1.1 christos & SHF_COMPRESSED);
8413 1.1 christos
8414 1.1 christos ihdr = &elf_section_data (isec)->this_hdr;
8415 1.1 christos
8416 1.1 christos /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8417 1.1 christos don't use the output section of the linked-to section since it
8418 1.1 christos may be NULL at this point. */
8419 1.1 christos if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8420 1.1 christos {
8421 1.1 christos ohdr = &elf_section_data (osec)->this_hdr;
8422 1.1 christos ohdr->sh_flags |= SHF_LINK_ORDER;
8423 1.1 christos elf_linked_to_section (osec) = elf_linked_to_section (isec);
8424 1.1 christos }
8425 1.1 christos
8426 1.14 christos osec->use_rela_p = isec->use_rela_p;
8427 1.1 christos
8428 1.1 christos return true;
8429 1.1 christos }
8430 1.1 christos
8431 1.1 christos /* Copy private section information. This copies over the entsize
8432 1.14 christos field, and sometimes the info field. */
8433 1.1 christos
8434 1.1 christos bool
8435 1.1 christos _bfd_elf_copy_private_section_data (bfd *ibfd,
8436 1.1 christos asection *isec,
8437 1.1 christos bfd *obfd,
8438 1.1 christos asection *osec)
8439 1.1 christos {
8440 1.1 christos Elf_Internal_Shdr *ihdr, *ohdr;
8441 1.1 christos
8442 1.14 christos if (ibfd->xvec->flavour != bfd_target_elf_flavour
8443 1.1 christos || obfd->xvec->flavour != bfd_target_elf_flavour)
8444 1.1 christos return true;
8445 1.1 christos
8446 1.1 christos ihdr = &elf_section_data (isec)->this_hdr;
8447 1.1 christos ohdr = &elf_section_data (osec)->this_hdr;
8448 1.1 christos
8449 1.1 christos ohdr->sh_entsize = ihdr->sh_entsize;
8450 1.1 christos
8451 1.1 christos if (ihdr->sh_type == SHT_SYMTAB
8452 1.1 christos || ihdr->sh_type == SHT_DYNSYM
8453 1.1 christos || ihdr->sh_type == SHT_GNU_verneed
8454 1.1 christos || ihdr->sh_type == SHT_GNU_verdef)
8455 1.1 christos ohdr->sh_info = ihdr->sh_info;
8456 1.1 christos
8457 1.1 christos return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8458 1.1 christos NULL);
8459 1.1 christos }
8460 1.1 christos
8461 1.1 christos /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8462 1.1 christos necessary if we are removing either the SHT_GROUP section or any of
8463 1.1 christos the group member sections. DISCARDED is the value that a section's
8464 1.1 christos output_section has if the section will be discarded, NULL when this
8465 1.1 christos function is called from objcopy, bfd_abs_section_ptr when called
8466 1.14 christos from the linker. */
8467 1.1 christos
8468 1.1 christos bool
8469 1.1 christos _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8470 1.1 christos {
8471 1.1 christos asection *isec;
8472 1.1 christos
8473 1.1 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8474 1.1 christos if (elf_section_type (isec) == SHT_GROUP)
8475 1.1 christos {
8476 1.1 christos asection *first = elf_next_in_group (isec);
8477 1.1 christos asection *s = first;
8478 1.1 christos bfd_size_type removed = 0;
8479 1.1 christos
8480 1.1 christos while (s != NULL)
8481 1.1 christos {
8482 1.1 christos /* If this member section is being output but the
8483 1.1 christos SHT_GROUP section is not, then clear the group info
8484 1.1 christos set up by _bfd_elf_copy_private_section_data. */
8485 1.1 christos if (s->output_section != discarded
8486 1.1 christos && isec->output_section == discarded)
8487 1.1 christos {
8488 1.1 christos elf_section_flags (s->output_section) &= ~SHF_GROUP;
8489 1.12 christos elf_group_name (s->output_section) = NULL;
8490 1.11 christos }
8491 1.11 christos else
8492 1.12 christos {
8493 1.12 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8494 1.12 christos if (s->output_section == discarded
8495 1.12 christos && isec->output_section != discarded)
8496 1.12 christos {
8497 1.12 christos /* Conversely, if the member section is not being
8498 1.12 christos output but the SHT_GROUP section is, then adjust
8499 1.12 christos its size. */
8500 1.12 christos removed += 4;
8501 1.12 christos if (elf_sec->rel.hdr != NULL
8502 1.12 christos && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8503 1.12 christos removed += 4;
8504 1.12 christos if (elf_sec->rela.hdr != NULL
8505 1.12 christos && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8506 1.12 christos removed += 4;
8507 1.12 christos }
8508 1.12 christos else
8509 1.12 christos {
8510 1.12 christos /* Also adjust for zero-sized relocation member
8511 1.12 christos section. */
8512 1.12 christos if (elf_sec->rel.hdr != NULL
8513 1.12 christos && elf_sec->rel.hdr->sh_size == 0)
8514 1.12 christos removed += 4;
8515 1.12 christos if (elf_sec->rela.hdr != NULL
8516 1.12 christos && elf_sec->rela.hdr->sh_size == 0)
8517 1.11 christos removed += 4;
8518 1.1 christos }
8519 1.1 christos }
8520 1.1 christos s = elf_next_in_group (s);
8521 1.1 christos if (s == first)
8522 1.1 christos break;
8523 1.1 christos }
8524 1.1 christos if (removed != 0)
8525 1.1 christos {
8526 1.1 christos if (discarded != NULL)
8527 1.11 christos {
8528 1.1 christos /* If we've been called for ld -r, then we need to
8529 1.1 christos adjust the input section size. */
8530 1.1 christos if (isec->rawsize == 0)
8531 1.11 christos isec->rawsize = isec->size;
8532 1.11 christos isec->size = isec->rawsize - removed;
8533 1.11 christos if (isec->size <= 4)
8534 1.11 christos {
8535 1.11 christos isec->size = 0;
8536 1.1 christos isec->flags |= SEC_EXCLUDE;
8537 1.14 christos }
8538 1.1 christos }
8539 1.1 christos else if (isec->output_section != NULL)
8540 1.1 christos {
8541 1.1 christos /* Adjust the output section size when called from
8542 1.11 christos objcopy. */
8543 1.11 christos isec->output_section->size -= removed;
8544 1.11 christos if (isec->output_section->size <= 4)
8545 1.11 christos {
8546 1.11 christos isec->output_section->size = 0;
8547 1.1 christos isec->output_section->flags |= SEC_EXCLUDE;
8548 1.1 christos }
8549 1.1 christos }
8550 1.1 christos }
8551 1.14 christos }
8552 1.1 christos
8553 1.1 christos return true;
8554 1.1 christos }
8555 1.1 christos
8556 1.14 christos /* Copy private header information. */
8557 1.1 christos
8558 1.1 christos bool
8559 1.1 christos _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8560 1.1 christos {
8561 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8562 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8563 1.1 christos return true;
8564 1.1 christos
8565 1.1 christos /* Copy over private BFD data if it has not already been copied.
8566 1.1 christos This must be done here, rather than in the copy_private_bfd_data
8567 1.1 christos entry point, because the latter is called after the section
8568 1.3 christos contents have been set, which means that the program headers have
8569 1.1 christos already been worked out. */
8570 1.1 christos if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8571 1.14 christos {
8572 1.1 christos if (! copy_private_bfd_data (ibfd, obfd))
8573 1.1 christos return false;
8574 1.1 christos }
8575 1.1 christos
8576 1.1 christos return _bfd_elf_fixup_group_sections (ibfd, NULL);
8577 1.1 christos }
8578 1.1 christos
8579 1.1 christos /* Copy private symbol information. If this symbol is in a section
8580 1.1 christos which we did not map into a BFD section, try to map the section
8581 1.1 christos index correctly. We use special macro definitions for the mapped
8582 1.1 christos section indices; these definitions are interpreted by the
8583 1.1 christos swap_out_syms function. */
8584 1.1 christos
8585 1.1 christos #define MAP_ONESYMTAB (SHN_HIOS + 1)
8586 1.1 christos #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8587 1.1 christos #define MAP_STRTAB (SHN_HIOS + 3)
8588 1.1 christos #define MAP_SHSTRTAB (SHN_HIOS + 4)
8589 1.14 christos #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8590 1.1 christos
8591 1.1 christos bool
8592 1.1 christos _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8593 1.1 christos asymbol *isymarg,
8594 1.1 christos bfd *obfd,
8595 1.1 christos asymbol *osymarg)
8596 1.1 christos {
8597 1.1 christos elf_symbol_type *isym, *osym;
8598 1.1 christos
8599 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8600 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8601 1.14 christos return true;
8602 1.14 christos
8603 1.1 christos isym = elf_symbol_from (isymarg);
8604 1.1 christos osym = elf_symbol_from (osymarg);
8605 1.1 christos
8606 1.1 christos if (isym != NULL
8607 1.1 christos && isym->internal_elf_sym.st_shndx != 0
8608 1.1 christos && osym != NULL
8609 1.1 christos && bfd_is_abs_section (isym->symbol.section))
8610 1.1 christos {
8611 1.1 christos unsigned int shndx;
8612 1.1 christos
8613 1.1 christos shndx = isym->internal_elf_sym.st_shndx;
8614 1.1 christos if (shndx == elf_onesymtab (ibfd))
8615 1.1 christos shndx = MAP_ONESYMTAB;
8616 1.17 christos else if (shndx == elf_dynsymtab (ibfd))
8617 1.1 christos shndx = MAP_DYNSYMTAB;
8618 1.17 christos else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8619 1.1 christos shndx = MAP_STRTAB;
8620 1.8 christos else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8621 1.1 christos shndx = MAP_SHSTRTAB;
8622 1.1 christos else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8623 1.1 christos shndx = MAP_SYM_SHNDX;
8624 1.1 christos osym->internal_elf_sym.st_shndx = shndx;
8625 1.14 christos }
8626 1.1 christos
8627 1.1 christos return true;
8628 1.1 christos }
8629 1.1 christos
8630 1.14 christos /* Swap out the symbols. */
8631 1.1 christos
8632 1.6 christos static bool
8633 1.14 christos swap_out_syms (bfd *abfd,
8634 1.14 christos struct elf_strtab_hash **sttp,
8635 1.1 christos int relocatable_p,
8636 1.1 christos struct bfd_link_info *info)
8637 1.12 christos {
8638 1.1 christos const struct elf_backend_data *bed;
8639 1.6 christos unsigned int symcount;
8640 1.1 christos asymbol **syms;
8641 1.1 christos struct elf_strtab_hash *stt;
8642 1.1 christos Elf_Internal_Shdr *symtab_hdr;
8643 1.6 christos Elf_Internal_Shdr *symtab_shndx_hdr;
8644 1.1 christos Elf_Internal_Shdr *symstrtab_hdr;
8645 1.1 christos struct elf_sym_strtab *symstrtab;
8646 1.6 christos bfd_byte *outbound_syms;
8647 1.12 christos bfd_byte *outbound_shndx;
8648 1.3 christos unsigned long outbound_syms_index;
8649 1.12 christos unsigned int idx;
8650 1.14 christos unsigned int num_locals;
8651 1.1 christos size_t amt;
8652 1.3 christos bool name_local_sections;
8653 1.14 christos
8654 1.1 christos if (!elf_map_symbols (abfd, &num_locals))
8655 1.1 christos return false;
8656 1.6 christos
8657 1.1 christos /* Dump out the symtabs. */
8658 1.14 christos stt = _bfd_elf_strtab_init ();
8659 1.1 christos if (stt == NULL)
8660 1.1 christos return false;
8661 1.1 christos
8662 1.1 christos bed = get_elf_backend_data (abfd);
8663 1.1 christos symcount = bfd_get_symcount (abfd);
8664 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8665 1.1 christos symtab_hdr->sh_type = SHT_SYMTAB;
8666 1.3 christos symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8667 1.1 christos symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8668 1.1 christos symtab_hdr->sh_info = num_locals + 1;
8669 1.1 christos symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8670 1.1 christos
8671 1.1 christos symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8672 1.6 christos symstrtab_hdr->sh_type = SHT_STRTAB;
8673 1.12 christos
8674 1.12 christos /* Allocate buffer to swap out the .strtab section. */
8675 1.6 christos if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8676 1.12 christos || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8677 1.6 christos {
8678 1.14 christos bfd_set_error (bfd_error_no_memory);
8679 1.6 christos _bfd_elf_strtab_free (stt);
8680 1.6 christos return false;
8681 1.12 christos }
8682 1.12 christos
8683 1.1 christos if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8684 1.12 christos || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8685 1.12 christos {
8686 1.12 christos error_no_mem:
8687 1.12 christos bfd_set_error (bfd_error_no_memory);
8688 1.6 christos error_return:
8689 1.14 christos free (symstrtab);
8690 1.1 christos _bfd_elf_strtab_free (stt);
8691 1.1 christos return false;
8692 1.6 christos }
8693 1.1 christos symtab_hdr->contents = outbound_syms;
8694 1.1 christos outbound_syms_index = 0;
8695 1.8 christos
8696 1.8 christos outbound_shndx = NULL;
8697 1.1 christos
8698 1.8 christos if (elf_symtab_shndx_list (abfd))
8699 1.8 christos {
8700 1.8 christos symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8701 1.12 christos if (symtab_shndx_hdr->sh_name != 0)
8702 1.12 christos {
8703 1.12 christos if (_bfd_mul_overflow (symcount + 1,
8704 1.12 christos sizeof (Elf_External_Sym_Shndx), &amt))
8705 1.8 christos goto error_no_mem;
8706 1.8 christos outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8707 1.1 christos if (outbound_shndx == NULL)
8708 1.8 christos goto error_return;
8709 1.8 christos
8710 1.8 christos symtab_shndx_hdr->contents = outbound_shndx;
8711 1.8 christos symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8712 1.8 christos symtab_shndx_hdr->sh_size = amt;
8713 1.8 christos symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8714 1.8 christos symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8715 1.1 christos }
8716 1.1 christos /* FIXME: What about any other headers in the list ? */
8717 1.1 christos }
8718 1.1 christos
8719 1.1 christos /* Now generate the data (for "contents"). */
8720 1.1 christos {
8721 1.1 christos /* Fill in zeroth symbol and swap it out. */
8722 1.1 christos Elf_Internal_Sym sym;
8723 1.1 christos sym.st_name = 0;
8724 1.1 christos sym.st_value = 0;
8725 1.1 christos sym.st_size = 0;
8726 1.1 christos sym.st_info = 0;
8727 1.1 christos sym.st_other = 0;
8728 1.17 christos sym.st_shndx = SHN_UNDEF;
8729 1.17 christos sym.st_target_internal = 0;
8730 1.6 christos symstrtab[outbound_syms_index].sym = sym;
8731 1.1 christos symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8732 1.1 christos outbound_syms_index++;
8733 1.1 christos }
8734 1.1 christos
8735 1.1 christos name_local_sections
8736 1.1 christos = (bed->elf_backend_name_local_section_symbols
8737 1.1 christos && bed->elf_backend_name_local_section_symbols (abfd));
8738 1.17 christos
8739 1.1 christos syms = bfd_get_outsymbols (abfd);
8740 1.1 christos for (idx = 0; idx < symcount; idx++)
8741 1.17 christos {
8742 1.1 christos Elf_Internal_Sym sym;
8743 1.1 christos
8744 1.1 christos flagword flags = syms[idx]->flags;
8745 1.1 christos if (!name_local_sections
8746 1.1 christos && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8747 1.18 christos {
8748 1.1 christos /* Local section symbols have no name. */
8749 1.1 christos sym.st_name = 0;
8750 1.1 christos }
8751 1.6 christos else
8752 1.6 christos {
8753 1.18 christos /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8754 1.18 christos to get the final offset for st_name. */
8755 1.6 christos size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8756 1.18 christos if (stridx == (size_t) -1)
8757 1.1 christos goto error_return;
8758 1.1 christos sym.st_name = stridx;
8759 1.17 christos }
8760 1.17 christos
8761 1.17 christos bfd_vma value = syms[idx]->value;
8762 1.1 christos elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8763 1.17 christos asection *sec = syms[idx]->section;
8764 1.1 christos
8765 1.1 christos if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8766 1.1 christos {
8767 1.1 christos /* ELF common symbols put the alignment into the `value' field,
8768 1.1 christos and the size into the `size' field. This is backwards from
8769 1.1 christos how BFD handles it, so reverse it here. */
8770 1.1 christos sym.st_size = value;
8771 1.1 christos if (type_ptr == NULL
8772 1.1 christos || type_ptr->internal_elf_sym.st_value == 0)
8773 1.1 christos sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8774 1.17 christos else
8775 1.1 christos sym.st_value = type_ptr->internal_elf_sym.st_value;
8776 1.1 christos sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8777 1.1 christos }
8778 1.1 christos else
8779 1.1 christos {
8780 1.1 christos unsigned int shndx;
8781 1.1 christos
8782 1.1 christos if (sec->output_section)
8783 1.1 christos {
8784 1.1 christos value += sec->output_offset;
8785 1.1 christos sec = sec->output_section;
8786 1.1 christos }
8787 1.1 christos
8788 1.1 christos /* Don't add in the section vma for relocatable output. */
8789 1.1 christos if (! relocatable_p)
8790 1.1 christos value += sec->vma;
8791 1.1 christos sym.st_value = value;
8792 1.1 christos sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8793 1.1 christos
8794 1.1 christos if (bfd_is_abs_section (sec)
8795 1.1 christos && type_ptr != NULL
8796 1.1 christos && type_ptr->internal_elf_sym.st_shndx != 0)
8797 1.1 christos {
8798 1.1 christos /* This symbol is in a real ELF section which we did
8799 1.1 christos not create as a BFD section. Undo the mapping done
8800 1.1 christos by copy_private_symbol_data. */
8801 1.1 christos shndx = type_ptr->internal_elf_sym.st_shndx;
8802 1.1 christos switch (shndx)
8803 1.1 christos {
8804 1.1 christos case MAP_ONESYMTAB:
8805 1.1 christos shndx = elf_onesymtab (abfd);
8806 1.1 christos break;
8807 1.1 christos case MAP_DYNSYMTAB:
8808 1.1 christos shndx = elf_dynsymtab (abfd);
8809 1.3 christos break;
8810 1.1 christos case MAP_STRTAB:
8811 1.1 christos shndx = elf_strtab_sec (abfd);
8812 1.3 christos break;
8813 1.1 christos case MAP_SHSTRTAB:
8814 1.1 christos shndx = elf_shstrtab_sec (abfd);
8815 1.8 christos break;
8816 1.8 christos case MAP_SYM_SHNDX:
8817 1.1 christos if (elf_symtab_shndx_list (abfd))
8818 1.12 christos shndx = elf_symtab_shndx_list (abfd)->ndx;
8819 1.12 christos break;
8820 1.12 christos case SHN_COMMON:
8821 1.12 christos case SHN_ABS:
8822 1.1 christos shndx = SHN_ABS;
8823 1.12 christos break;
8824 1.12 christos default:
8825 1.12 christos if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8826 1.12 christos {
8827 1.12 christos if (bed->symbol_section_index)
8828 1.12 christos shndx = bed->symbol_section_index (abfd, type_ptr);
8829 1.12 christos /* Otherwise just leave the index alone. */
8830 1.12 christos }
8831 1.12 christos else
8832 1.12 christos {
8833 1.12 christos if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8834 1.12 christos _bfd_error_handler (_("%pB: \
8835 1.12 christos Unable to handle section index %x in ELF symbol. Using ABS instead."),
8836 1.12 christos abfd, shndx);
8837 1.1 christos shndx = SHN_ABS;
8838 1.1 christos }
8839 1.1 christos break;
8840 1.1 christos }
8841 1.1 christos }
8842 1.1 christos else
8843 1.1 christos {
8844 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8845 1.1 christos
8846 1.1 christos if (shndx == SHN_BAD)
8847 1.1 christos {
8848 1.1 christos asection *sec2;
8849 1.1 christos
8850 1.1 christos /* Writing this would be a hell of a lot easier if
8851 1.1 christos we had some decent documentation on bfd, and
8852 1.1 christos knew what to expect of the library, and what to
8853 1.1 christos demand of applications. For example, it
8854 1.1 christos appears that `objcopy' might not set the
8855 1.1 christos section of a symbol to be a section that is
8856 1.8 christos actually in the output file. */
8857 1.8 christos sec2 = bfd_get_section_by_name (abfd, sec->name);
8858 1.8 christos if (sec2 != NULL)
8859 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8860 1.9 christos if (shndx == SHN_BAD)
8861 1.11 christos {
8862 1.11 christos /* xgettext:c-format */
8863 1.11 christos _bfd_error_handler
8864 1.11 christos (_("unable to find equivalent output section"
8865 1.11 christos " for symbol '%s' from section '%s'"),
8866 1.1 christos syms[idx]->name ? syms[idx]->name : "<Local sym>",
8867 1.6 christos sec->name);
8868 1.1 christos bfd_set_error (bfd_error_invalid_operation);
8869 1.1 christos goto error_return;
8870 1.1 christos }
8871 1.1 christos }
8872 1.1 christos }
8873 1.1 christos
8874 1.1 christos sym.st_shndx = shndx;
8875 1.17 christos }
8876 1.1 christos
8877 1.1 christos int type;
8878 1.1 christos if ((flags & BSF_THREAD_LOCAL) != 0)
8879 1.1 christos type = STT_TLS;
8880 1.1 christos else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8881 1.1 christos type = STT_GNU_IFUNC;
8882 1.1 christos else if ((flags & BSF_FUNCTION) != 0)
8883 1.1 christos type = STT_FUNC;
8884 1.1 christos else if ((flags & BSF_OBJECT) != 0)
8885 1.1 christos type = STT_OBJECT;
8886 1.1 christos else if ((flags & BSF_RELC) != 0)
8887 1.1 christos type = STT_RELC;
8888 1.1 christos else if ((flags & BSF_SRELC) != 0)
8889 1.1 christos type = STT_SRELC;
8890 1.1 christos else
8891 1.1 christos type = STT_NOTYPE;
8892 1.1 christos
8893 1.1 christos if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8894 1.1 christos type = STT_TLS;
8895 1.1 christos
8896 1.1 christos /* Processor-specific types. */
8897 1.1 christos if (type_ptr != NULL
8898 1.1 christos && bed->elf_backend_get_symbol_type)
8899 1.1 christos type = ((*bed->elf_backend_get_symbol_type)
8900 1.1 christos (&type_ptr->internal_elf_sym, type));
8901 1.1 christos
8902 1.1 christos if (flags & BSF_SECTION_SYM)
8903 1.1 christos {
8904 1.1 christos if (flags & BSF_GLOBAL)
8905 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8906 1.1 christos else
8907 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8908 1.1 christos }
8909 1.8 christos else if (bfd_is_com_section (syms[idx]->section))
8910 1.8 christos {
8911 1.8 christos if (type != STT_TLS)
8912 1.8 christos {
8913 1.8 christos if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8914 1.8 christos type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8915 1.8 christos ? STT_COMMON : STT_OBJECT);
8916 1.8 christos else
8917 1.8 christos type = ((flags & BSF_ELF_COMMON) != 0
8918 1.8 christos ? STT_COMMON : STT_OBJECT);
8919 1.1 christos }
8920 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8921 1.1 christos }
8922 1.1 christos else if (bfd_is_und_section (syms[idx]->section))
8923 1.1 christos sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8924 1.1 christos ? STB_WEAK
8925 1.1 christos : STB_GLOBAL),
8926 1.1 christos type);
8927 1.1 christos else if (flags & BSF_FILE)
8928 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8929 1.1 christos else
8930 1.1 christos {
8931 1.1 christos int bind = STB_LOCAL;
8932 1.1 christos
8933 1.1 christos if (flags & BSF_LOCAL)
8934 1.1 christos bind = STB_LOCAL;
8935 1.1 christos else if (flags & BSF_GNU_UNIQUE)
8936 1.1 christos bind = STB_GNU_UNIQUE;
8937 1.1 christos else if (flags & BSF_WEAK)
8938 1.1 christos bind = STB_WEAK;
8939 1.1 christos else if (flags & BSF_GLOBAL)
8940 1.1 christos bind = STB_GLOBAL;
8941 1.1 christos
8942 1.1 christos sym.st_info = ELF_ST_INFO (bind, type);
8943 1.1 christos }
8944 1.1 christos
8945 1.1 christos if (type_ptr != NULL)
8946 1.1 christos {
8947 1.1 christos sym.st_other = type_ptr->internal_elf_sym.st_other;
8948 1.1 christos sym.st_target_internal
8949 1.1 christos = type_ptr->internal_elf_sym.st_target_internal;
8950 1.1 christos }
8951 1.1 christos else
8952 1.1 christos {
8953 1.1 christos sym.st_other = 0;
8954 1.1 christos sym.st_target_internal = 0;
8955 1.17 christos }
8956 1.17 christos
8957 1.6 christos symstrtab[outbound_syms_index].sym = sym;
8958 1.6 christos symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8959 1.6 christos outbound_syms_index++;
8960 1.6 christos }
8961 1.6 christos
8962 1.6 christos /* Finalize the .strtab section. */
8963 1.6 christos _bfd_elf_strtab_finalize (stt);
8964 1.17 christos
8965 1.6 christos /* Swap out the .strtab section. */
8966 1.6 christos for (idx = 0; idx < outbound_syms_index; idx++)
8967 1.18 christos {
8968 1.6 christos struct elf_sym_strtab *elfsym = &symstrtab[idx];
8969 1.6 christos if (elfsym->sym.st_name != 0)
8970 1.14 christos elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8971 1.14 christos elfsym->sym.st_name);
8972 1.14 christos if (info && info->callbacks->ctf_new_symbol)
8973 1.14 christos info->callbacks->ctf_new_symbol (elfsym->dest_index,
8974 1.14 christos &elfsym->sym);
8975 1.14 christos
8976 1.6 christos /* Inform the linker of the addition of this symbol. */
8977 1.6 christos
8978 1.6 christos bed->s->swap_symbol_out (abfd, &elfsym->sym,
8979 1.6 christos (outbound_syms
8980 1.14 christos + (elfsym->dest_index
8981 1.14 christos * bed->s->sizeof_sym)),
8982 1.14 christos NPTR_ADD (outbound_shndx,
8983 1.1 christos (elfsym->dest_index
8984 1.6 christos * sizeof (Elf_External_Sym_Shndx))));
8985 1.1 christos }
8986 1.1 christos free (symstrtab);
8987 1.6 christos
8988 1.1 christos *sttp = stt;
8989 1.8 christos symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8990 1.1 christos symstrtab_hdr->sh_type = SHT_STRTAB;
8991 1.1 christos symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8992 1.1 christos symstrtab_hdr->sh_addr = 0;
8993 1.1 christos symstrtab_hdr->sh_entsize = 0;
8994 1.1 christos symstrtab_hdr->sh_link = 0;
8995 1.1 christos symstrtab_hdr->sh_info = 0;
8996 1.14 christos symstrtab_hdr->sh_addralign = 1;
8997 1.1 christos
8998 1.1 christos return true;
8999 1.1 christos }
9000 1.1 christos
9001 1.1 christos /* Return the number of bytes required to hold the symtab vector.
9002 1.1 christos
9003 1.1 christos Note that we base it on the count plus 1, since we will null terminate
9004 1.1 christos the vector allocated based on this size. However, the ELF symbol table
9005 1.1 christos always has a dummy entry as symbol #0, so it ends up even. */
9006 1.1 christos
9007 1.1 christos long
9008 1.11 christos _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9009 1.1 christos {
9010 1.1 christos bfd_size_type symcount;
9011 1.1 christos long symtab_size;
9012 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9013 1.12 christos
9014 1.11 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9015 1.11 christos if (symcount > LONG_MAX / sizeof (asymbol *))
9016 1.11 christos {
9017 1.11 christos bfd_set_error (bfd_error_file_too_big);
9018 1.12 christos return -1;
9019 1.12 christos }
9020 1.12 christos symtab_size = symcount * (sizeof (asymbol *));
9021 1.12 christos if (symcount == 0)
9022 1.12 christos symtab_size = sizeof (asymbol *);
9023 1.12 christos else if (!bfd_write_p (abfd))
9024 1.12 christos {
9025 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd);
9026 1.12 christos
9027 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize)
9028 1.12 christos {
9029 1.12 christos bfd_set_error (bfd_error_file_truncated);
9030 1.12 christos return -1;
9031 1.1 christos }
9032 1.1 christos }
9033 1.1 christos
9034 1.1 christos return symtab_size;
9035 1.1 christos }
9036 1.1 christos
9037 1.1 christos long
9038 1.11 christos _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9039 1.1 christos {
9040 1.1 christos bfd_size_type symcount;
9041 1.1 christos long symtab_size;
9042 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9043 1.1 christos
9044 1.17 christos if (elf_dynsymtab (abfd) == 0)
9045 1.17 christos {
9046 1.17 christos /* Check if there is dynamic symbol table. */
9047 1.17 christos symcount = elf_tdata (abfd)->dt_symtab_count;
9048 1.17 christos if (symcount)
9049 1.1 christos goto compute_symtab_size;
9050 1.1 christos
9051 1.1 christos bfd_set_error (bfd_error_invalid_operation);
9052 1.1 christos return -1;
9053 1.1 christos }
9054 1.12 christos
9055 1.11 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9056 1.11 christos if (symcount > LONG_MAX / sizeof (asymbol *))
9057 1.11 christos {
9058 1.11 christos bfd_set_error (bfd_error_file_too_big);
9059 1.17 christos return -1;
9060 1.17 christos }
9061 1.12 christos
9062 1.12 christos compute_symtab_size:
9063 1.12 christos symtab_size = symcount * (sizeof (asymbol *));
9064 1.12 christos if (symcount == 0)
9065 1.12 christos symtab_size = sizeof (asymbol *);
9066 1.12 christos else if (!bfd_write_p (abfd))
9067 1.12 christos {
9068 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd);
9069 1.12 christos
9070 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize)
9071 1.12 christos {
9072 1.12 christos bfd_set_error (bfd_error_file_truncated);
9073 1.12 christos return -1;
9074 1.1 christos }
9075 1.1 christos }
9076 1.1 christos
9077 1.1 christos return symtab_size;
9078 1.1 christos }
9079 1.12 christos
9080 1.1 christos long
9081 1.12 christos _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9082 1.12 christos {
9083 1.12 christos if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9084 1.12 christos {
9085 1.12 christos /* Sanity check reloc section size. */
9086 1.14 christos ufile_ptr filesize = bfd_get_file_size (abfd);
9087 1.12 christos
9088 1.14 christos if (filesize != 0)
9089 1.14 christos {
9090 1.14 christos struct bfd_elf_section_data *d = elf_section_data (asect);
9091 1.14 christos bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9092 1.14 christos bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9093 1.14 christos
9094 1.14 christos if (rel_size + rela_size > filesize
9095 1.14 christos || rel_size + rela_size < rel_size)
9096 1.14 christos {
9097 1.14 christos bfd_set_error (bfd_error_file_truncated);
9098 1.12 christos return -1;
9099 1.12 christos }
9100 1.12 christos }
9101 1.12 christos }
9102 1.12 christos
9103 1.12 christos #if SIZEOF_LONG == SIZEOF_INT
9104 1.12 christos if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9105 1.12 christos {
9106 1.12 christos bfd_set_error (bfd_error_file_too_big);
9107 1.12 christos return -1;
9108 1.14 christos }
9109 1.1 christos #endif
9110 1.1 christos return (asect->reloc_count + 1L) * sizeof (arelent *);
9111 1.1 christos }
9112 1.1 christos
9113 1.1 christos /* Canonicalize the relocs. */
9114 1.1 christos
9115 1.1 christos long
9116 1.1 christos _bfd_elf_canonicalize_reloc (bfd *abfd,
9117 1.1 christos sec_ptr section,
9118 1.1 christos arelent **relptr,
9119 1.1 christos asymbol **symbols)
9120 1.1 christos {
9121 1.1 christos arelent *tblptr;
9122 1.1 christos unsigned int i;
9123 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9124 1.1 christos
9125 1.1 christos if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9126 1.1 christos return -1;
9127 1.1 christos
9128 1.1 christos tblptr = section->relocation;
9129 1.1 christos for (i = 0; i < section->reloc_count; i++)
9130 1.1 christos *relptr++ = tblptr++;
9131 1.1 christos
9132 1.1 christos *relptr = NULL;
9133 1.1 christos
9134 1.1 christos return section->reloc_count;
9135 1.1 christos }
9136 1.1 christos
9137 1.1 christos long
9138 1.1 christos _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9139 1.14 christos {
9140 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9141 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9142 1.12 christos
9143 1.1 christos if (symcount >= 0)
9144 1.1 christos abfd->symcount = symcount;
9145 1.1 christos return symcount;
9146 1.1 christos }
9147 1.1 christos
9148 1.1 christos long
9149 1.1 christos _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9150 1.1 christos asymbol **allocation)
9151 1.14 christos {
9152 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9153 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9154 1.12 christos
9155 1.1 christos if (symcount >= 0)
9156 1.1 christos abfd->dynsymcount = symcount;
9157 1.1 christos return symcount;
9158 1.1 christos }
9159 1.1 christos
9160 1.1 christos /* Return the size required for the dynamic reloc entries. Any loadable
9161 1.1 christos section that was actually installed in the BFD, and has type SHT_REL
9162 1.1 christos or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9163 1.1 christos dynamic reloc section. */
9164 1.1 christos
9165 1.1 christos long
9166 1.12 christos _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9167 1.1 christos {
9168 1.1 christos bfd_size_type count, ext_rel_size;
9169 1.1 christos asection *s;
9170 1.1 christos
9171 1.1 christos if (elf_dynsymtab (abfd) == 0)
9172 1.1 christos {
9173 1.1 christos bfd_set_error (bfd_error_invalid_operation);
9174 1.1 christos return -1;
9175 1.11 christos }
9176 1.12 christos
9177 1.1 christos count = 1;
9178 1.1 christos ext_rel_size = 0;
9179 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
9180 1.17 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9181 1.17 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9182 1.11 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9183 1.17 christos && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9184 1.17 christos {
9185 1.12 christos ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9186 1.12 christos if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9187 1.12 christos {
9188 1.12 christos bfd_set_error (bfd_error_file_truncated);
9189 1.17 christos return -1;
9190 1.11 christos }
9191 1.11 christos count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9192 1.11 christos if (count > LONG_MAX / sizeof (arelent *))
9193 1.11 christos {
9194 1.11 christos bfd_set_error (bfd_error_file_too_big);
9195 1.11 christos return -1;
9196 1.12 christos }
9197 1.12 christos }
9198 1.12 christos if (count > 1 && !bfd_write_p (abfd))
9199 1.12 christos {
9200 1.12 christos /* Sanity check reloc section sizes. */
9201 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd);
9202 1.12 christos if (filesize != 0 && ext_rel_size > filesize)
9203 1.12 christos {
9204 1.12 christos bfd_set_error (bfd_error_file_truncated);
9205 1.12 christos return -1;
9206 1.11 christos }
9207 1.1 christos }
9208 1.1 christos return count * sizeof (arelent *);
9209 1.1 christos }
9210 1.1 christos
9211 1.1 christos /* Canonicalize the dynamic relocation entries. Note that we return the
9212 1.1 christos dynamic relocations as a single block, although they are actually
9213 1.1 christos associated with particular sections; the interface, which was
9214 1.1 christos designed for SunOS style shared libraries, expects that there is only
9215 1.1 christos one set of dynamic relocs. Any loadable section that was actually
9216 1.1 christos installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9217 1.1 christos dynamic symbol table, is considered to be a dynamic reloc section. */
9218 1.1 christos
9219 1.1 christos long
9220 1.1 christos _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9221 1.1 christos arelent **storage,
9222 1.14 christos asymbol **syms)
9223 1.1 christos {
9224 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9225 1.1 christos asection *s;
9226 1.1 christos long ret;
9227 1.1 christos
9228 1.1 christos if (elf_dynsymtab (abfd) == 0)
9229 1.1 christos {
9230 1.1 christos bfd_set_error (bfd_error_invalid_operation);
9231 1.1 christos return -1;
9232 1.1 christos }
9233 1.1 christos
9234 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9235 1.1 christos ret = 0;
9236 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
9237 1.1 christos {
9238 1.17 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9239 1.17 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9240 1.1 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9241 1.1 christos && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9242 1.1 christos {
9243 1.1 christos arelent *p;
9244 1.14 christos long count, i;
9245 1.1 christos
9246 1.17 christos if (! (*slurp_relocs) (abfd, s, syms, true))
9247 1.1 christos return -1;
9248 1.1 christos count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9249 1.1 christos p = s->relocation;
9250 1.1 christos for (i = 0; i < count; i++)
9251 1.1 christos *storage++ = p++;
9252 1.1 christos ret += count;
9253 1.1 christos }
9254 1.1 christos }
9255 1.1 christos
9256 1.1 christos *storage = NULL;
9257 1.1 christos
9258 1.1 christos return ret;
9259 1.1 christos }
9260 1.1 christos
9261 1.14 christos /* Read in the version information. */
9263 1.1 christos
9264 1.1 christos bool
9265 1.1 christos _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9266 1.12 christos {
9267 1.17 christos bfd_byte *contents = NULL;
9268 1.17 christos unsigned int freeidx = 0;
9269 1.1 christos size_t amt;
9270 1.17 christos void *contents_addr = NULL;
9271 1.1 christos size_t contents_size = 0;
9272 1.1 christos
9273 1.1 christos if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9274 1.1 christos {
9275 1.1 christos Elf_Internal_Shdr *hdr;
9276 1.1 christos Elf_External_Verneed *everneed;
9277 1.17 christos Elf_Internal_Verneed *iverneed;
9278 1.17 christos unsigned int i;
9279 1.1 christos bfd_byte *contents_end;
9280 1.17 christos size_t verneed_count;
9281 1.1 christos size_t verneed_size;
9282 1.17 christos
9283 1.17 christos if (elf_tdata (abfd)->dt_verneed != NULL)
9284 1.17 christos {
9285 1.17 christos hdr = NULL;
9286 1.1 christos contents = elf_tdata (abfd)->dt_verneed;
9287 1.17 christos verneed_count = elf_tdata (abfd)->dt_verneed_count;
9288 1.17 christos verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9289 1.17 christos }
9290 1.5 christos else
9291 1.17 christos {
9292 1.17 christos hdr = &elf_tdata (abfd)->dynverref_hdr;
9293 1.17 christos
9294 1.17 christos if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9295 1.17 christos {
9296 1.17 christos error_return_bad_verref:
9297 1.17 christos _bfd_error_handler
9298 1.17 christos (_("%pB: .gnu.version_r invalid entry"), abfd);
9299 1.17 christos bfd_set_error (bfd_error_bad_value);
9300 1.17 christos error_return_verref:
9301 1.17 christos elf_tdata (abfd)->verref = NULL;
9302 1.17 christos elf_tdata (abfd)->cverrefs = 0;
9303 1.17 christos goto error_return;
9304 1.17 christos }
9305 1.17 christos
9306 1.18 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9307 1.18 christos goto error_return_verref;
9308 1.17 christos contents_size = hdr->sh_size;
9309 1.17 christos contents = _bfd_mmap_temporary (abfd, contents_size,
9310 1.17 christos &contents_addr, &contents_size);
9311 1.17 christos if (contents == NULL)
9312 1.17 christos goto error_return_verref;
9313 1.17 christos
9314 1.5 christos verneed_size = hdr->sh_size;
9315 1.17 christos verneed_count = hdr->sh_info;
9316 1.17 christos }
9317 1.12 christos
9318 1.12 christos if (_bfd_mul_overflow (verneed_count,
9319 1.12 christos sizeof (Elf_Internal_Verneed), &amt))
9320 1.12 christos {
9321 1.14 christos bfd_set_error (bfd_error_file_too_big);
9322 1.14 christos goto error_return_verref;
9323 1.14 christos }
9324 1.5 christos if (amt == 0)
9325 1.1 christos goto error_return_verref;
9326 1.1 christos elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9327 1.1 christos if (elf_tdata (abfd)->verref == NULL)
9328 1.1 christos goto error_return_verref;
9329 1.17 christos
9330 1.17 christos BFD_ASSERT (sizeof (Elf_External_Verneed)
9331 1.1 christos == sizeof (Elf_External_Vernaux));
9332 1.1 christos contents_end = (contents + verneed_size
9333 1.17 christos - sizeof (Elf_External_Verneed));
9334 1.1 christos everneed = (Elf_External_Verneed *) contents;
9335 1.1 christos iverneed = elf_tdata (abfd)->verref;
9336 1.1 christos for (i = 0; i < verneed_count; i++, iverneed++)
9337 1.1 christos {
9338 1.1 christos Elf_External_Vernaux *evernaux;
9339 1.1 christos Elf_Internal_Vernaux *ivernaux;
9340 1.1 christos unsigned int j;
9341 1.1 christos
9342 1.1 christos _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9343 1.17 christos
9344 1.17 christos iverneed->vn_bfd = abfd;
9345 1.17 christos
9346 1.17 christos if (elf_use_dt_symtab_p (abfd))
9347 1.17 christos {
9348 1.17 christos if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9349 1.17 christos iverneed->vn_filename
9350 1.17 christos = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9351 1.17 christos else
9352 1.17 christos iverneed->vn_filename = NULL;
9353 1.17 christos }
9354 1.17 christos else if (hdr == NULL)
9355 1.17 christos goto error_return_bad_verref;
9356 1.17 christos else
9357 1.1 christos iverneed->vn_filename
9358 1.5 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9359 1.1 christos iverneed->vn_file);
9360 1.1 christos if (iverneed->vn_filename == NULL)
9361 1.1 christos goto error_return_bad_verref;
9362 1.1 christos
9363 1.1 christos if (iverneed->vn_cnt == 0)
9364 1.12 christos iverneed->vn_auxptr = NULL;
9365 1.12 christos else
9366 1.12 christos {
9367 1.12 christos if (_bfd_mul_overflow (iverneed->vn_cnt,
9368 1.12 christos sizeof (Elf_Internal_Vernaux), &amt))
9369 1.12 christos {
9370 1.1 christos bfd_set_error (bfd_error_file_too_big);
9371 1.12 christos goto error_return_verref;
9372 1.1 christos }
9373 1.1 christos iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9374 1.1 christos bfd_alloc (abfd, amt);
9375 1.1 christos if (iverneed->vn_auxptr == NULL)
9376 1.1 christos goto error_return_verref;
9377 1.1 christos }
9378 1.5 christos
9379 1.1 christos if (iverneed->vn_aux
9380 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed))
9381 1.1 christos goto error_return_bad_verref;
9382 1.1 christos
9383 1.1 christos evernaux = ((Elf_External_Vernaux *)
9384 1.1 christos ((bfd_byte *) everneed + iverneed->vn_aux));
9385 1.1 christos ivernaux = iverneed->vn_auxptr;
9386 1.1 christos for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9387 1.17 christos {
9388 1.17 christos _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9389 1.17 christos
9390 1.17 christos if (elf_use_dt_symtab_p (abfd))
9391 1.17 christos {
9392 1.17 christos if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9393 1.17 christos ivernaux->vna_nodename
9394 1.17 christos = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9395 1.17 christos else
9396 1.17 christos ivernaux->vna_nodename = NULL;
9397 1.17 christos }
9398 1.17 christos else if (hdr == NULL)
9399 1.17 christos goto error_return_bad_verref;
9400 1.17 christos else
9401 1.1 christos ivernaux->vna_nodename
9402 1.5 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9403 1.5 christos ivernaux->vna_name);
9404 1.5 christos if (ivernaux->vna_nodename == NULL)
9405 1.5 christos goto error_return_bad_verref;
9406 1.1 christos
9407 1.5 christos if (ivernaux->vna_other > freeidx)
9408 1.5 christos freeidx = ivernaux->vna_other;
9409 1.5 christos
9410 1.5 christos ivernaux->vna_nextptr = NULL;
9411 1.5 christos if (ivernaux->vna_next == 0)
9412 1.5 christos {
9413 1.1 christos iverneed->vn_cnt = j + 1;
9414 1.1 christos break;
9415 1.1 christos }
9416 1.1 christos if (j + 1 < iverneed->vn_cnt)
9417 1.1 christos ivernaux->vna_nextptr = ivernaux + 1;
9418 1.5 christos
9419 1.1 christos if (ivernaux->vna_next
9420 1.1 christos > (size_t) (contents_end - (bfd_byte *) evernaux))
9421 1.1 christos goto error_return_bad_verref;
9422 1.1 christos
9423 1.1 christos evernaux = ((Elf_External_Vernaux *)
9424 1.5 christos ((bfd_byte *) evernaux + ivernaux->vna_next));
9425 1.5 christos }
9426 1.5 christos
9427 1.17 christos iverneed->vn_nextref = NULL;
9428 1.1 christos if (iverneed->vn_next == 0)
9429 1.1 christos break;
9430 1.1 christos if (hdr != NULL && (i + 1 < hdr->sh_info))
9431 1.1 christos iverneed->vn_nextref = iverneed + 1;
9432 1.5 christos
9433 1.1 christos if (iverneed->vn_next
9434 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed))
9435 1.1 christos goto error_return_bad_verref;
9436 1.1 christos
9437 1.5 christos everneed = ((Elf_External_Verneed *)
9438 1.1 christos ((bfd_byte *) everneed + iverneed->vn_next));
9439 1.17 christos }
9440 1.18 christos elf_tdata (abfd)->cverrefs = i;
9441 1.1 christos
9442 1.17 christos if (contents != elf_tdata (abfd)->dt_verneed)
9443 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size);
9444 1.1 christos contents = NULL;
9445 1.17 christos contents_addr = NULL;
9446 1.1 christos }
9447 1.1 christos
9448 1.1 christos if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9449 1.1 christos {
9450 1.1 christos Elf_Internal_Shdr *hdr;
9451 1.1 christos Elf_External_Verdef *everdef;
9452 1.1 christos Elf_Internal_Verdef *iverdef;
9453 1.1 christos Elf_Internal_Verdef *iverdefarr;
9454 1.1 christos Elf_Internal_Verdef iverdefmem;
9455 1.17 christos unsigned int i;
9456 1.17 christos unsigned int maxidx;
9457 1.1 christos bfd_byte *contents_end_def, *contents_end_aux;
9458 1.17 christos size_t verdef_count;
9459 1.5 christos size_t verdef_size;
9460 1.17 christos
9461 1.17 christos if (elf_tdata (abfd)->dt_verdef != NULL)
9462 1.17 christos {
9463 1.17 christos hdr = NULL;
9464 1.5 christos contents = elf_tdata (abfd)->dt_verdef;
9465 1.17 christos verdef_count = elf_tdata (abfd)->dt_verdef_count;
9466 1.17 christos verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9467 1.17 christos }
9468 1.5 christos else
9469 1.17 christos {
9470 1.17 christos hdr = &elf_tdata (abfd)->dynverdef_hdr;
9471 1.17 christos
9472 1.17 christos if (hdr->sh_size < sizeof (Elf_External_Verdef))
9473 1.17 christos {
9474 1.17 christos error_return_bad_verdef:
9475 1.17 christos _bfd_error_handler
9476 1.17 christos (_("%pB: .gnu.version_d invalid entry"), abfd);
9477 1.17 christos bfd_set_error (bfd_error_bad_value);
9478 1.17 christos error_return_verdef:
9479 1.17 christos elf_tdata (abfd)->verdef = NULL;
9480 1.17 christos elf_tdata (abfd)->cverdefs = 0;
9481 1.17 christos goto error_return;
9482 1.17 christos }
9483 1.17 christos
9484 1.18 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9485 1.18 christos goto error_return_verdef;
9486 1.17 christos contents_size = hdr->sh_size;
9487 1.17 christos contents = _bfd_mmap_temporary (abfd, contents_size,
9488 1.17 christos &contents_addr, &contents_size);
9489 1.17 christos if (contents == NULL)
9490 1.17 christos goto error_return_verdef;
9491 1.17 christos
9492 1.17 christos BFD_ASSERT (sizeof (Elf_External_Verdef)
9493 1.17 christos >= sizeof (Elf_External_Verdaux));
9494 1.17 christos
9495 1.17 christos verdef_count = hdr->sh_info;
9496 1.17 christos verdef_size = hdr->sh_size;
9497 1.17 christos }
9498 1.17 christos
9499 1.17 christos contents_end_def = (contents + verdef_size
9500 1.1 christos - sizeof (Elf_External_Verdef));
9501 1.1 christos contents_end_aux = (contents + verdef_size
9502 1.1 christos - sizeof (Elf_External_Verdaux));
9503 1.1 christos
9504 1.1 christos /* We know the number of entries in the section but not the maximum
9505 1.1 christos index. Therefore we have to run through all entries and find
9506 1.17 christos the maximum. */
9507 1.1 christos everdef = (Elf_External_Verdef *) contents;
9508 1.1 christos maxidx = 0;
9509 1.1 christos for (i = 0; i < verdef_count; ++i)
9510 1.5 christos {
9511 1.5 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9512 1.1 christos
9513 1.1 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9514 1.1 christos goto error_return_bad_verdef;
9515 1.5 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9516 1.5 christos maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9517 1.5 christos
9518 1.1 christos if (iverdefmem.vd_next == 0)
9519 1.1 christos break;
9520 1.5 christos
9521 1.1 christos if (iverdefmem.vd_next
9522 1.1 christos > (size_t) (contents_end_def - (bfd_byte *) everdef))
9523 1.1 christos goto error_return_bad_verdef;
9524 1.1 christos
9525 1.1 christos everdef = ((Elf_External_Verdef *)
9526 1.1 christos ((bfd_byte *) everdef + iverdefmem.vd_next));
9527 1.1 christos }
9528 1.1 christos
9529 1.1 christos if (default_imported_symver)
9530 1.1 christos {
9531 1.1 christos if (freeidx > maxidx)
9532 1.1 christos maxidx = ++freeidx;
9533 1.12 christos else
9534 1.12 christos freeidx = ++maxidx;
9535 1.12 christos }
9536 1.12 christos if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9537 1.12 christos {
9538 1.17 christos bfd_set_error (bfd_error_file_too_big);
9539 1.17 christos goto error_return_verdef;
9540 1.17 christos }
9541 1.12 christos
9542 1.1 christos if (amt == 0)
9543 1.5 christos goto error_return_verdef;
9544 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9545 1.1 christos if (elf_tdata (abfd)->verdef == NULL)
9546 1.1 christos goto error_return_verdef;
9547 1.1 christos
9548 1.1 christos elf_tdata (abfd)->cverdefs = maxidx;
9549 1.17 christos
9550 1.1 christos everdef = (Elf_External_Verdef *) contents;
9551 1.1 christos iverdefarr = elf_tdata (abfd)->verdef;
9552 1.1 christos for (i = 0; i < verdef_count; ++i)
9553 1.1 christos {
9554 1.1 christos Elf_External_Verdaux *everdaux;
9555 1.1 christos Elf_Internal_Verdaux *iverdaux;
9556 1.1 christos unsigned int j;
9557 1.1 christos
9558 1.5 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9559 1.1 christos
9560 1.1 christos if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9561 1.8 christos goto error_return_bad_verdef;
9562 1.1 christos
9563 1.1 christos iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9564 1.1 christos memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9565 1.1 christos
9566 1.1 christos iverdef->vd_bfd = abfd;
9567 1.1 christos
9568 1.1 christos if (iverdef->vd_cnt == 0)
9569 1.12 christos iverdef->vd_auxptr = NULL;
9570 1.12 christos else
9571 1.12 christos {
9572 1.12 christos if (_bfd_mul_overflow (iverdef->vd_cnt,
9573 1.12 christos sizeof (Elf_Internal_Verdaux), &amt))
9574 1.12 christos {
9575 1.1 christos bfd_set_error (bfd_error_file_too_big);
9576 1.12 christos goto error_return_verdef;
9577 1.1 christos }
9578 1.1 christos iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9579 1.1 christos bfd_alloc (abfd, amt);
9580 1.1 christos if (iverdef->vd_auxptr == NULL)
9581 1.1 christos goto error_return_verdef;
9582 1.1 christos }
9583 1.5 christos
9584 1.1 christos if (iverdef->vd_aux
9585 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9586 1.1 christos goto error_return_bad_verdef;
9587 1.1 christos
9588 1.1 christos everdaux = ((Elf_External_Verdaux *)
9589 1.1 christos ((bfd_byte *) everdef + iverdef->vd_aux));
9590 1.1 christos iverdaux = iverdef->vd_auxptr;
9591 1.1 christos for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9592 1.17 christos {
9593 1.17 christos _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9594 1.17 christos
9595 1.17 christos if (elf_use_dt_symtab_p (abfd))
9596 1.17 christos {
9597 1.17 christos if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9598 1.17 christos iverdaux->vda_nodename
9599 1.17 christos = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9600 1.17 christos else
9601 1.17 christos iverdaux->vda_nodename = NULL;
9602 1.17 christos }
9603 1.17 christos else
9604 1.1 christos iverdaux->vda_nodename
9605 1.5 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9606 1.1 christos iverdaux->vda_name);
9607 1.5 christos if (iverdaux->vda_nodename == NULL)
9608 1.5 christos goto error_return_bad_verdef;
9609 1.5 christos
9610 1.5 christos iverdaux->vda_nextptr = NULL;
9611 1.5 christos if (iverdaux->vda_next == 0)
9612 1.5 christos {
9613 1.1 christos iverdef->vd_cnt = j + 1;
9614 1.1 christos break;
9615 1.1 christos }
9616 1.1 christos if (j + 1 < iverdef->vd_cnt)
9617 1.1 christos iverdaux->vda_nextptr = iverdaux + 1;
9618 1.5 christos
9619 1.1 christos if (iverdaux->vda_next
9620 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9621 1.1 christos goto error_return_bad_verdef;
9622 1.1 christos
9623 1.1 christos everdaux = ((Elf_External_Verdaux *)
9624 1.8 christos ((bfd_byte *) everdaux + iverdaux->vda_next));
9625 1.1 christos }
9626 1.1 christos
9627 1.1 christos iverdef->vd_nodename = NULL;
9628 1.5 christos if (iverdef->vd_cnt)
9629 1.5 christos iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9630 1.5 christos
9631 1.1 christos iverdef->vd_nextdef = NULL;
9632 1.1 christos if (iverdef->vd_next == 0)
9633 1.1 christos break;
9634 1.1 christos if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9635 1.1 christos iverdef->vd_nextdef = iverdef + 1;
9636 1.1 christos
9637 1.1 christos everdef = ((Elf_External_Verdef *)
9638 1.17 christos ((bfd_byte *) everdef + iverdef->vd_next));
9639 1.18 christos }
9640 1.1 christos
9641 1.17 christos if (contents != elf_tdata (abfd)->dt_verdef)
9642 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size);
9643 1.1 christos contents = NULL;
9644 1.1 christos contents_addr = NULL;
9645 1.1 christos }
9646 1.1 christos else if (default_imported_symver)
9647 1.1 christos {
9648 1.1 christos if (freeidx < 3)
9649 1.1 christos freeidx = 3;
9650 1.12 christos else
9651 1.12 christos freeidx++;
9652 1.12 christos
9653 1.12 christos if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9654 1.12 christos {
9655 1.17 christos bfd_set_error (bfd_error_file_too_big);
9656 1.17 christos goto error_return;
9657 1.12 christos }
9658 1.1 christos if (amt == 0)
9659 1.1 christos goto error_return;
9660 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9661 1.1 christos if (elf_tdata (abfd)->verdef == NULL)
9662 1.1 christos goto error_return;
9663 1.1 christos
9664 1.1 christos elf_tdata (abfd)->cverdefs = freeidx;
9665 1.1 christos }
9666 1.1 christos
9667 1.1 christos /* Create a default version based on the soname. */
9668 1.1 christos if (default_imported_symver)
9669 1.1 christos {
9670 1.3 christos Elf_Internal_Verdef *iverdef;
9671 1.1 christos Elf_Internal_Verdaux *iverdaux;
9672 1.1 christos
9673 1.1 christos iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9674 1.1 christos
9675 1.1 christos iverdef->vd_version = VER_DEF_CURRENT;
9676 1.1 christos iverdef->vd_flags = 0;
9677 1.1 christos iverdef->vd_ndx = freeidx;
9678 1.1 christos iverdef->vd_cnt = 1;
9679 1.1 christos
9680 1.1 christos iverdef->vd_bfd = abfd;
9681 1.1 christos
9682 1.1 christos iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9683 1.5 christos if (iverdef->vd_nodename == NULL)
9684 1.5 christos goto error_return_verdef;
9685 1.1 christos iverdef->vd_nextdef = NULL;
9686 1.1 christos iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9687 1.1 christos bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9688 1.1 christos if (iverdef->vd_auxptr == NULL)
9689 1.1 christos goto error_return_verdef;
9690 1.1 christos
9691 1.1 christos iverdaux = iverdef->vd_auxptr;
9692 1.14 christos iverdaux->vda_nodename = iverdef->vd_nodename;
9693 1.1 christos }
9694 1.1 christos
9695 1.17 christos return true;
9696 1.17 christos
9697 1.18 christos error_return:
9698 1.14 christos if (contents != elf_tdata (abfd)->dt_verneed
9699 1.1 christos && contents != elf_tdata (abfd)->dt_verdef)
9700 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size);
9701 1.1 christos return false;
9702 1.1 christos }
9703 1.1 christos
9704 1.1 christos asymbol *
9706 1.12 christos _bfd_elf_make_empty_symbol (bfd *abfd)
9707 1.1 christos {
9708 1.1 christos elf_symbol_type *newsym;
9709 1.5 christos
9710 1.5 christos newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9711 1.1 christos if (!newsym)
9712 1.1 christos return NULL;
9713 1.1 christos newsym->symbol.the_bfd = abfd;
9714 1.1 christos return &newsym->symbol;
9715 1.1 christos }
9716 1.1 christos
9717 1.1 christos void
9718 1.1 christos _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9719 1.1 christos asymbol *symbol,
9720 1.1 christos symbol_info *ret)
9721 1.1 christos {
9722 1.1 christos bfd_symbol_info (symbol, ret);
9723 1.1 christos }
9724 1.1 christos
9725 1.14 christos /* Return whether a symbol name implies a local symbol. Most targets
9726 1.1 christos use this function for the is_local_label_name entry point, but some
9727 1.1 christos override it. */
9728 1.1 christos
9729 1.1 christos bool
9730 1.1 christos _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9731 1.14 christos const char *name)
9732 1.1 christos {
9733 1.1 christos /* Normal local symbols start with ``.L''. */
9734 1.1 christos if (name[0] == '.' && name[1] == 'L')
9735 1.1 christos return true;
9736 1.14 christos
9737 1.1 christos /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9738 1.1 christos DWARF debugging symbols starting with ``..''. */
9739 1.1 christos if (name[0] == '.' && name[1] == '.')
9740 1.1 christos return true;
9741 1.1 christos
9742 1.1 christos /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9743 1.1 christos emitting DWARF debugging output. I suspect this is actually a
9744 1.1 christos small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9745 1.14 christos ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9746 1.1 christos underscore to be emitted on some ELF targets). For ease of use,
9747 1.6 christos we treat such symbols as local. */
9748 1.6 christos if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9749 1.6 christos return true;
9750 1.6 christos
9751 1.11 christos /* Treat assembler generated fake symbols, dollar local labels and
9752 1.6 christos forward-backward labels (aka local labels) as locals.
9753 1.6 christos These labels have the form:
9754 1.6 christos
9755 1.6 christos L0^A.* (fake symbols)
9756 1.6 christos
9757 1.6 christos [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9758 1.6 christos
9759 1.14 christos Versions which start with .L will have already been matched above,
9760 1.6 christos so we only need to match the rest. */
9761 1.6 christos if (name[0] == 'L' && ISDIGIT (name[1]))
9762 1.6 christos {
9763 1.6 christos bool ret = false;
9764 1.6 christos const char * p;
9765 1.6 christos char c;
9766 1.6 christos
9767 1.6 christos for (p = name + 2; (c = *p); p++)
9768 1.6 christos {
9769 1.14 christos if (c == 1 || c == 2)
9770 1.6 christos {
9771 1.6 christos if (c == 1 && p == name + 2)
9772 1.6 christos /* A fake symbol. */
9773 1.6 christos return true;
9774 1.6 christos
9775 1.6 christos /* FIXME: We are being paranoid here and treating symbols like
9776 1.14 christos L0^Bfoo as if there were non-local, on the grounds that the
9777 1.6 christos assembler will never generate them. But can any symbol
9778 1.6 christos containing an ASCII value in the range 1-31 ever be anything
9779 1.6 christos other than some kind of local ? */
9780 1.6 christos ret = true;
9781 1.14 christos }
9782 1.6 christos
9783 1.6 christos if (! ISDIGIT (c))
9784 1.6 christos {
9785 1.6 christos ret = false;
9786 1.6 christos break;
9787 1.6 christos }
9788 1.14 christos }
9789 1.1 christos return ret;
9790 1.1 christos }
9791 1.1 christos
9792 1.1 christos return false;
9793 1.1 christos }
9794 1.1 christos
9795 1.1 christos alent *
9796 1.1 christos _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9797 1.1 christos asymbol *symbol ATTRIBUTE_UNUSED)
9798 1.1 christos {
9799 1.14 christos abort ();
9800 1.1 christos return NULL;
9801 1.1 christos }
9802 1.1 christos
9803 1.1 christos bool
9804 1.1 christos _bfd_elf_set_arch_mach (bfd *abfd,
9805 1.1 christos enum bfd_architecture arch,
9806 1.1 christos unsigned long machine)
9807 1.1 christos {
9808 1.1 christos /* If this isn't the right architecture for this backend, and this
9809 1.14 christos isn't the generic backend, fail. */
9810 1.1 christos if (arch != get_elf_backend_data (abfd)->arch
9811 1.1 christos && arch != bfd_arch_unknown
9812 1.1 christos && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9813 1.1 christos return false;
9814 1.1 christos
9815 1.1 christos return bfd_default_set_arch_mach (abfd, arch, machine);
9816 1.1 christos }
9817 1.14 christos
9818 1.1 christos /* Find the nearest line to a particular section and offset,
9819 1.5 christos for error reporting. */
9820 1.1 christos
9821 1.1 christos bool
9822 1.1 christos _bfd_elf_find_nearest_line (bfd *abfd,
9823 1.1 christos asymbol **symbols,
9824 1.5 christos asection *section,
9825 1.5 christos bfd_vma offset,
9826 1.3 christos const char **filename_ptr,
9827 1.14 christos const char **functionname_ptr,
9828 1.14 christos unsigned int *line_ptr,
9829 1.14 christos unsigned int *discriminator_ptr)
9830 1.14 christos {
9831 1.14 christos return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9832 1.14 christos offset, filename_ptr,
9833 1.14 christos functionname_ptr, line_ptr,
9834 1.14 christos discriminator_ptr);
9835 1.14 christos }
9836 1.1 christos
9837 1.14 christos /* Find the nearest line to a particular section and offset,
9838 1.14 christos for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9839 1.14 christos can be optionally specified. */
9840 1.14 christos
9841 1.14 christos bool
9842 1.14 christos _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9843 1.14 christos const char *alt_filename,
9844 1.14 christos asymbol **symbols,
9845 1.14 christos asection *section,
9846 1.14 christos bfd_vma offset,
9847 1.14 christos const char **filename_ptr,
9848 1.14 christos const char **functionname_ptr,
9849 1.14 christos unsigned int *line_ptr,
9850 1.14 christos unsigned int *discriminator_ptr)
9851 1.14 christos {
9852 1.14 christos bool found;
9853 1.14 christos
9854 1.14 christos if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9855 1.14 christos section, offset, filename_ptr,
9856 1.14 christos functionname_ptr, line_ptr,
9857 1.12 christos discriminator_ptr,
9858 1.12 christos dwarf_debug_sections,
9859 1.12 christos &elf_tdata (abfd)->dwarf2_find_line_info))
9860 1.1 christos return true;
9861 1.1 christos
9862 1.5 christos if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9863 1.5 christos filename_ptr, functionname_ptr, line_ptr))
9864 1.5 christos {
9865 1.14 christos if (!*functionname_ptr)
9866 1.1 christos _bfd_elf_find_function (abfd, symbols, section, offset,
9867 1.1 christos *filename_ptr ? NULL : filename_ptr,
9868 1.1 christos functionname_ptr);
9869 1.1 christos return true;
9870 1.1 christos }
9871 1.1 christos
9872 1.14 christos if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9873 1.1 christos &found, filename_ptr,
9874 1.14 christos functionname_ptr, line_ptr,
9875 1.1 christos &elf_tdata (abfd)->line_info))
9876 1.1 christos return false;
9877 1.14 christos if (found && (*functionname_ptr || *line_ptr))
9878 1.1 christos return true;
9879 1.5 christos
9880 1.5 christos if (symbols == NULL)
9881 1.14 christos return false;
9882 1.1 christos
9883 1.1 christos if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9884 1.14 christos filename_ptr, functionname_ptr))
9885 1.1 christos return false;
9886 1.1 christos
9887 1.1 christos *line_ptr = 0;
9888 1.1 christos return true;
9889 1.14 christos }
9890 1.1 christos
9891 1.1 christos /* Find the line for a symbol. */
9892 1.1 christos
9893 1.14 christos bool
9894 1.5 christos _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9895 1.5 christos const char **filename_ptr, unsigned int *line_ptr)
9896 1.12 christos {
9897 1.14 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
9898 1.1 christos return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9899 1.1 christos filename_ptr, NULL, line_ptr, NULL,
9900 1.1 christos dwarf_debug_sections,
9901 1.1 christos &tdata->dwarf2_find_line_info);
9902 1.1 christos }
9903 1.1 christos
9904 1.1 christos /* After a call to bfd_find_nearest_line, successive calls to
9905 1.1 christos bfd_find_inliner_info can be used to get source information about
9906 1.14 christos each level of function inlining that terminated at the address
9907 1.1 christos passed to bfd_find_nearest_line. Currently this is only supported
9908 1.1 christos for DWARF2 with appropriate DWARF3 extensions. */
9909 1.1 christos
9910 1.1 christos bool
9911 1.1 christos _bfd_elf_find_inliner_info (bfd *abfd,
9912 1.14 christos const char **filename_ptr,
9913 1.14 christos const char **functionname_ptr,
9914 1.14 christos unsigned int *line_ptr)
9915 1.14 christos {
9916 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
9917 1.1 christos return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9918 1.1 christos functionname_ptr, line_ptr,
9919 1.1 christos &tdata->dwarf2_find_line_info);
9920 1.1 christos }
9921 1.1 christos
9922 1.1 christos int
9923 1.1 christos _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9924 1.8 christos {
9925 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9926 1.3 christos int ret = bed->s->sizeof_ehdr;
9927 1.1 christos
9928 1.1 christos if (!bfd_link_relocatable (info))
9929 1.1 christos {
9930 1.1 christos bfd_size_type phdr_size = elf_program_header_size (abfd);
9931 1.1 christos
9932 1.1 christos if (phdr_size == (bfd_size_type) -1)
9933 1.3 christos {
9934 1.1 christos struct elf_segment_map *m;
9935 1.1 christos
9936 1.1 christos phdr_size = 0;
9937 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9938 1.1 christos phdr_size += bed->s->sizeof_phdr;
9939 1.1 christos
9940 1.3 christos if (phdr_size == 0)
9941 1.1 christos phdr_size = get_program_header_size (abfd, info);
9942 1.1 christos }
9943 1.1 christos
9944 1.1 christos elf_program_header_size (abfd) = phdr_size;
9945 1.1 christos ret += phdr_size;
9946 1.1 christos }
9947 1.14 christos
9948 1.1 christos return ret;
9949 1.1 christos }
9950 1.1 christos
9951 1.1 christos bool
9952 1.1 christos _bfd_elf_set_section_contents (bfd *abfd,
9953 1.1 christos sec_ptr section,
9954 1.1 christos const void *location,
9955 1.1 christos file_ptr offset,
9956 1.1 christos bfd_size_type count)
9957 1.1 christos {
9958 1.14 christos Elf_Internal_Shdr *hdr;
9959 1.1 christos
9960 1.6 christos if (! abfd->output_has_begun
9961 1.14 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9962 1.6 christos return false;
9963 1.1 christos
9964 1.6 christos if (!count)
9965 1.6 christos return true;
9966 1.12 christos
9967 1.12 christos hdr = &elf_section_data (section)->this_hdr;
9968 1.12 christos if (hdr->sh_offset == (file_ptr) -1)
9969 1.12 christos {
9970 1.12 christos unsigned char *contents;
9971 1.14 christos
9972 1.12 christos if (bfd_section_is_ctf (section))
9973 1.12 christos /* Nothing to do with this section: the contents are generated
9974 1.12 christos later. */
9975 1.12 christos return true;
9976 1.14 christos
9977 1.14 christos if ((offset + count) > hdr->sh_size)
9978 1.12 christos {
9979 1.12 christos _bfd_error_handler
9980 1.12 christos (_("%pB:%pA: error: attempting to write"
9981 1.14 christos " over the end of the section"),
9982 1.12 christos abfd, section);
9983 1.12 christos
9984 1.12 christos bfd_set_error (bfd_error_invalid_operation);
9985 1.12 christos return false;
9986 1.12 christos }
9987 1.12 christos
9988 1.14 christos contents = hdr->contents;
9989 1.14 christos if (contents == NULL)
9990 1.12 christos {
9991 1.12 christos _bfd_error_handler
9992 1.12 christos (_("%pB:%pA: error: attempting to write"
9993 1.14 christos " section into an empty buffer"),
9994 1.12 christos abfd, section);
9995 1.12 christos
9996 1.6 christos bfd_set_error (bfd_error_invalid_operation);
9997 1.14 christos return false;
9998 1.6 christos }
9999 1.12 christos
10000 1.14 christos memcpy (contents + offset, location, count);
10001 1.14 christos return true;
10002 1.1 christos }
10003 1.1 christos
10004 1.14 christos return _bfd_generic_set_section_contents (abfd, section,
10005 1.1 christos location, offset, count);
10006 1.1 christos }
10007 1.1 christos
10008 1.1 christos bool
10009 1.1 christos _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10010 1.14 christos arelent *cache_ptr ATTRIBUTE_UNUSED,
10011 1.1 christos Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10012 1.1 christos {
10013 1.1 christos abort ();
10014 1.1 christos return false;
10015 1.14 christos }
10016 1.1 christos
10017 1.1 christos /* Try to convert a non-ELF reloc into an ELF one. */
10018 1.1 christos
10019 1.1 christos bool
10020 1.1 christos _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10021 1.1 christos {
10022 1.1 christos /* Check whether we really have an ELF howto. */
10023 1.1 christos
10024 1.1 christos if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10025 1.1 christos {
10026 1.1 christos bfd_reloc_code_real_type code;
10027 1.1 christos reloc_howto_type *howto;
10028 1.1 christos
10029 1.1 christos /* Alien reloc: Try to determine its type to replace it with an
10030 1.1 christos equivalent ELF reloc. */
10031 1.1 christos
10032 1.1 christos if (areloc->howto->pc_relative)
10033 1.1 christos {
10034 1.1 christos switch (areloc->howto->bitsize)
10035 1.1 christos {
10036 1.1 christos case 8:
10037 1.1 christos code = BFD_RELOC_8_PCREL;
10038 1.1 christos break;
10039 1.1 christos case 12:
10040 1.1 christos code = BFD_RELOC_12_PCREL;
10041 1.1 christos break;
10042 1.1 christos case 16:
10043 1.1 christos code = BFD_RELOC_16_PCREL;
10044 1.1 christos break;
10045 1.1 christos case 24:
10046 1.1 christos code = BFD_RELOC_24_PCREL;
10047 1.1 christos break;
10048 1.1 christos case 32:
10049 1.1 christos code = BFD_RELOC_32_PCREL;
10050 1.1 christos break;
10051 1.1 christos case 64:
10052 1.1 christos code = BFD_RELOC_64_PCREL;
10053 1.1 christos break;
10054 1.1 christos default:
10055 1.1 christos goto fail;
10056 1.12 christos }
10057 1.1 christos
10058 1.1 christos howto = bfd_reloc_type_lookup (abfd, code);
10059 1.1 christos
10060 1.1 christos if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10061 1.1 christos {
10062 1.1 christos if (howto->pcrel_offset)
10063 1.1 christos areloc->addend += areloc->address;
10064 1.1 christos else
10065 1.1 christos areloc->addend -= areloc->address; /* addend is unsigned!! */
10066 1.1 christos }
10067 1.1 christos }
10068 1.1 christos else
10069 1.1 christos {
10070 1.1 christos switch (areloc->howto->bitsize)
10071 1.1 christos {
10072 1.1 christos case 8:
10073 1.1 christos code = BFD_RELOC_8;
10074 1.1 christos break;
10075 1.1 christos case 14:
10076 1.1 christos code = BFD_RELOC_14;
10077 1.1 christos break;
10078 1.1 christos case 16:
10079 1.1 christos code = BFD_RELOC_16;
10080 1.1 christos break;
10081 1.1 christos case 26:
10082 1.1 christos code = BFD_RELOC_26;
10083 1.1 christos break;
10084 1.1 christos case 32:
10085 1.1 christos code = BFD_RELOC_32;
10086 1.1 christos break;
10087 1.1 christos case 64:
10088 1.1 christos code = BFD_RELOC_64;
10089 1.1 christos break;
10090 1.1 christos default:
10091 1.1 christos goto fail;
10092 1.1 christos }
10093 1.1 christos
10094 1.1 christos howto = bfd_reloc_type_lookup (abfd, code);
10095 1.1 christos }
10096 1.1 christos
10097 1.1 christos if (howto)
10098 1.1 christos areloc->howto = howto;
10099 1.14 christos else
10100 1.1 christos goto fail;
10101 1.1 christos }
10102 1.11 christos
10103 1.11 christos return true;
10104 1.11 christos
10105 1.12 christos fail:
10106 1.14 christos /* xgettext:c-format */
10107 1.1 christos _bfd_error_handler (_("%pB: %s unsupported"),
10108 1.1 christos abfd, areloc->howto->name);
10109 1.14 christos bfd_set_error (bfd_error_sorry);
10110 1.17 christos return false;
10111 1.1 christos }
10112 1.17 christos
10113 1.17 christos bool
10114 1.17 christos _bfd_elf_free_cached_info (bfd *abfd)
10115 1.17 christos {
10116 1.17 christos struct elf_obj_tdata *tdata;
10117 1.1 christos
10118 1.17 christos if ((bfd_get_format (abfd) == bfd_object
10119 1.1 christos || bfd_get_format (abfd) == bfd_core)
10120 1.3 christos && (tdata = elf_tdata (abfd)) != NULL)
10121 1.17 christos {
10122 1.14 christos if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10123 1.1 christos _bfd_elf_strtab_free (elf_shstrtab (abfd));
10124 1.1 christos _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10125 1.17 christos _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10126 1.1 christos _bfd_stab_cleanup (abfd, &tdata->line_info);
10127 1.1 christos }
10128 1.1 christos
10129 1.1 christos return _bfd_generic_bfd_free_cached_info (abfd);
10130 1.1 christos }
10131 1.1 christos
10132 1.1 christos /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10133 1.1 christos in the relocation's offset. Thus we cannot allow any sort of sanity
10134 1.1 christos range-checking to interfere. There is nothing else to do in processing
10135 1.1 christos this reloc. */
10136 1.1 christos
10137 1.1 christos bfd_reloc_status_type
10138 1.1 christos _bfd_elf_rel_vtable_reloc_fn
10139 1.1 christos (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10140 1.1 christos struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10141 1.1 christos void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10142 1.1 christos bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10143 1.1 christos {
10144 1.1 christos return bfd_reloc_ok;
10145 1.1 christos }
10146 1.1 christos
10147 1.1 christos /* Elf core file support. Much of this only works on native
10149 1.1 christos toolchains, since we rely on knowing the
10150 1.1 christos machine-dependent procfs structure in order to pick
10151 1.1 christos out details about the corefile. */
10152 1.1 christos
10153 1.1 christos #ifdef HAVE_SYS_PROCFS_H
10154 1.1 christos # include <sys/procfs.h>
10155 1.1 christos #endif
10156 1.1 christos
10157 1.1 christos /* Return a PID that identifies a "thread" for threaded cores, or the
10158 1.1 christos PID of the main process for non-threaded cores. */
10159 1.1 christos
10160 1.3 christos static int
10161 1.1 christos elfcore_make_pid (bfd *abfd)
10162 1.3 christos {
10163 1.1 christos int pid;
10164 1.1 christos
10165 1.1 christos pid = elf_tdata (abfd)->core->lwpid;
10166 1.1 christos if (pid == 0)
10167 1.14 christos pid = elf_tdata (abfd)->core->pid;
10168 1.14 christos
10169 1.14 christos return pid;
10170 1.1 christos }
10171 1.14 christos
10172 1.1 christos /* If there isn't a section called NAME, make one, using data from
10173 1.1 christos SECT. Note, this function will generate a reference to NAME, so
10174 1.1 christos you shouldn't deallocate or overwrite it. */
10175 1.1 christos
10176 1.1 christos static bool
10177 1.14 christos elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10178 1.1 christos {
10179 1.1 christos asection *sect2;
10180 1.1 christos
10181 1.14 christos if (bfd_get_section_by_name (abfd, name) != NULL)
10182 1.1 christos return true;
10183 1.1 christos
10184 1.1 christos sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10185 1.1 christos if (sect2 == NULL)
10186 1.14 christos return false;
10187 1.1 christos
10188 1.1 christos sect2->size = sect->size;
10189 1.1 christos sect2->filepos = sect->filepos;
10190 1.1 christos sect2->alignment_power = sect->alignment_power;
10191 1.1 christos return true;
10192 1.1 christos }
10193 1.1 christos
10194 1.1 christos /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10195 1.11 christos actually creates up to two pseudosections:
10196 1.14 christos - For the single-threaded case, a section named NAME, unless
10197 1.1 christos such a section already exists.
10198 1.1 christos - For the multi-threaded case, a section named "NAME/PID", where
10199 1.1 christos PID is elfcore_make_pid (abfd).
10200 1.1 christos Both pseudosections have identical contents. */
10201 1.1 christos bool
10202 1.1 christos _bfd_elfcore_make_pseudosection (bfd *abfd,
10203 1.1 christos char *name,
10204 1.1 christos size_t size,
10205 1.1 christos ufile_ptr filepos)
10206 1.1 christos {
10207 1.1 christos char buf[100];
10208 1.1 christos char *threaded_name;
10209 1.1 christos size_t len;
10210 1.1 christos asection *sect;
10211 1.1 christos
10212 1.1 christos /* Build the section name. */
10213 1.14 christos
10214 1.1 christos sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10215 1.1 christos len = strlen (buf) + 1;
10216 1.1 christos threaded_name = (char *) bfd_alloc (abfd, len);
10217 1.1 christos if (threaded_name == NULL)
10218 1.1 christos return false;
10219 1.14 christos memcpy (threaded_name, buf, len);
10220 1.1 christos
10221 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10222 1.1 christos SEC_HAS_CONTENTS);
10223 1.1 christos if (sect == NULL)
10224 1.1 christos return false;
10225 1.1 christos sect->size = size;
10226 1.1 christos sect->filepos = filepos;
10227 1.14 christos sect->alignment_power = 2;
10228 1.12 christos
10229 1.12 christos return elfcore_maybe_make_sect (abfd, name, sect);
10230 1.12 christos }
10231 1.12 christos
10232 1.12 christos static bool
10233 1.12 christos elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10234 1.12 christos size_t offs)
10235 1.14 christos {
10236 1.12 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10237 1.12 christos SEC_HAS_CONTENTS);
10238 1.12 christos
10239 1.12 christos if (sect == NULL)
10240 1.12 christos return false;
10241 1.14 christos
10242 1.12 christos sect->size = note->descsz - offs;
10243 1.12 christos sect->filepos = note->descpos + offs;
10244 1.1 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10245 1.1 christos
10246 1.1 christos return true;
10247 1.1 christos }
10248 1.1 christos
10249 1.1 christos /* prstatus_t exists on:
10250 1.1 christos solaris 2.5+
10251 1.1 christos linux 2.[01] + glibc
10252 1.14 christos unixware 4.2
10253 1.1 christos */
10254 1.1 christos
10255 1.1 christos #if defined (HAVE_PRSTATUS_T)
10256 1.1 christos
10257 1.1 christos static bool
10258 1.1 christos elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10259 1.1 christos {
10260 1.1 christos size_t size;
10261 1.1 christos int offset;
10262 1.1 christos
10263 1.1 christos if (note->descsz == sizeof (prstatus_t))
10264 1.1 christos {
10265 1.1 christos prstatus_t prstat;
10266 1.1 christos
10267 1.1 christos size = sizeof (prstat.pr_reg);
10268 1.3 christos offset = offsetof (prstatus_t, pr_reg);
10269 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat));
10270 1.3 christos
10271 1.3 christos /* Do not overwrite the core signal if it
10272 1.1 christos has already been set by another thread. */
10273 1.1 christos if (elf_tdata (abfd)->core->signal == 0)
10274 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10275 1.1 christos if (elf_tdata (abfd)->core->pid == 0)
10276 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid;
10277 1.1 christos
10278 1.1 christos /* pr_who exists on:
10279 1.1 christos solaris 2.5+
10280 1.3 christos unixware 4.2
10281 1.1 christos pr_who doesn't exist on:
10282 1.3 christos linux 2.[01]
10283 1.1 christos */
10284 1.1 christos #if defined (HAVE_PRSTATUS_T_PR_WHO)
10285 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10286 1.1 christos #else
10287 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10288 1.1 christos #endif
10289 1.1 christos }
10290 1.1 christos #if defined (HAVE_PRSTATUS32_T)
10291 1.1 christos else if (note->descsz == sizeof (prstatus32_t))
10292 1.1 christos {
10293 1.1 christos /* 64-bit host, 32-bit corefile */
10294 1.1 christos prstatus32_t prstat;
10295 1.1 christos
10296 1.1 christos size = sizeof (prstat.pr_reg);
10297 1.3 christos offset = offsetof (prstatus32_t, pr_reg);
10298 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat));
10299 1.3 christos
10300 1.3 christos /* Do not overwrite the core signal if it
10301 1.1 christos has already been set by another thread. */
10302 1.1 christos if (elf_tdata (abfd)->core->signal == 0)
10303 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10304 1.1 christos if (elf_tdata (abfd)->core->pid == 0)
10305 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid;
10306 1.1 christos
10307 1.1 christos /* pr_who exists on:
10308 1.1 christos solaris 2.5+
10309 1.3 christos unixware 4.2
10310 1.1 christos pr_who doesn't exist on:
10311 1.3 christos linux 2.[01]
10312 1.1 christos */
10313 1.1 christos #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10314 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10315 1.1 christos #else
10316 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10317 1.1 christos #endif
10318 1.1 christos }
10319 1.14 christos #endif /* HAVE_PRSTATUS32_T */
10320 1.1 christos else
10321 1.1 christos {
10322 1.1 christos /* Fail - we don't know how to handle any other
10323 1.1 christos note size (ie. data object type). */
10324 1.1 christos return true;
10325 1.1 christos }
10326 1.1 christos
10327 1.1 christos /* Make a ".reg/999" section and a ".reg" section. */
10328 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10329 1.14 christos size, note->descpos + offset);
10330 1.1 christos }
10331 1.1 christos #endif /* defined (HAVE_PRSTATUS_T) */
10332 1.1 christos
10333 1.1 christos /* Create a pseudosection containing the exact contents of NOTE. */
10334 1.1 christos static bool
10335 1.1 christos elfcore_make_note_pseudosection (bfd *abfd,
10336 1.1 christos char *name,
10337 1.1 christos Elf_Internal_Note *note)
10338 1.1 christos {
10339 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, name,
10340 1.1 christos note->descsz, note->descpos);
10341 1.1 christos }
10342 1.14 christos
10343 1.1 christos /* There isn't a consistent prfpregset_t across platforms,
10344 1.1 christos but it doesn't matter, because we don't have to pick this
10345 1.1 christos data structure apart. */
10346 1.1 christos
10347 1.1 christos static bool
10348 1.1 christos elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10349 1.1 christos {
10350 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10351 1.1 christos }
10352 1.14 christos
10353 1.1 christos /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10354 1.1 christos type of NT_PRXFPREG. Just include the whole note's contents
10355 1.1 christos literally. */
10356 1.1 christos
10357 1.1 christos static bool
10358 1.1 christos elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10359 1.1 christos {
10360 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10361 1.1 christos }
10362 1.14 christos
10363 1.1 christos /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10364 1.1 christos with a note type of NT_X86_XSTATE. Just include the whole note's
10365 1.1 christos contents literally. */
10366 1.1 christos
10367 1.1 christos static bool
10368 1.14 christos elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10369 1.1 christos {
10370 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10371 1.1 christos }
10372 1.1 christos
10373 1.1 christos static bool
10374 1.14 christos elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10375 1.1 christos {
10376 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10377 1.1 christos }
10378 1.1 christos
10379 1.1 christos static bool
10380 1.14 christos elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10381 1.11 christos {
10382 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10383 1.11 christos }
10384 1.11 christos
10385 1.11 christos static bool
10386 1.14 christos elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10387 1.11 christos {
10388 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10389 1.11 christos }
10390 1.11 christos
10391 1.11 christos static bool
10392 1.14 christos elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10393 1.11 christos {
10394 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10395 1.11 christos }
10396 1.11 christos
10397 1.11 christos static bool
10398 1.14 christos elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10399 1.11 christos {
10400 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10401 1.11 christos }
10402 1.11 christos
10403 1.11 christos static bool
10404 1.14 christos elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10405 1.11 christos {
10406 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10407 1.11 christos }
10408 1.11 christos
10409 1.11 christos static bool
10410 1.14 christos elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10411 1.11 christos {
10412 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10413 1.11 christos }
10414 1.11 christos
10415 1.11 christos static bool
10416 1.14 christos elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10417 1.11 christos {
10418 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10419 1.11 christos }
10420 1.11 christos
10421 1.11 christos static bool
10422 1.14 christos elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10423 1.11 christos {
10424 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10425 1.11 christos }
10426 1.11 christos
10427 1.11 christos static bool
10428 1.14 christos elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10429 1.11 christos {
10430 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10431 1.11 christos }
10432 1.11 christos
10433 1.11 christos static bool
10434 1.14 christos elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10435 1.11 christos {
10436 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10437 1.11 christos }
10438 1.11 christos
10439 1.11 christos static bool
10440 1.14 christos elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10441 1.11 christos {
10442 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10443 1.11 christos }
10444 1.11 christos
10445 1.11 christos static bool
10446 1.14 christos elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10447 1.11 christos {
10448 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10449 1.11 christos }
10450 1.11 christos
10451 1.11 christos static bool
10452 1.14 christos elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10453 1.11 christos {
10454 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10455 1.11 christos }
10456 1.11 christos
10457 1.11 christos static bool
10458 1.14 christos elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10459 1.1 christos {
10460 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10461 1.1 christos }
10462 1.1 christos
10463 1.1 christos static bool
10464 1.14 christos elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10465 1.1 christos {
10466 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10467 1.1 christos }
10468 1.1 christos
10469 1.1 christos static bool
10470 1.14 christos elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10471 1.1 christos {
10472 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10473 1.1 christos }
10474 1.1 christos
10475 1.1 christos static bool
10476 1.14 christos elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10477 1.1 christos {
10478 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10479 1.1 christos }
10480 1.1 christos
10481 1.1 christos static bool
10482 1.14 christos elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10483 1.1 christos {
10484 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10485 1.1 christos }
10486 1.1 christos
10487 1.1 christos static bool
10488 1.14 christos elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10489 1.1 christos {
10490 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10491 1.1 christos }
10492 1.1 christos
10493 1.1 christos static bool
10494 1.14 christos elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10495 1.3 christos {
10496 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10497 1.3 christos }
10498 1.3 christos
10499 1.3 christos static bool
10500 1.14 christos elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10501 1.3 christos {
10502 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10503 1.3 christos }
10504 1.3 christos
10505 1.3 christos static bool
10506 1.14 christos elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10507 1.3 christos {
10508 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10509 1.3 christos }
10510 1.3 christos
10511 1.3 christos static bool
10512 1.14 christos elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10513 1.6 christos {
10514 1.6 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10515 1.6 christos }
10516 1.6 christos
10517 1.6 christos static bool
10518 1.14 christos elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10519 1.6 christos {
10520 1.6 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10521 1.6 christos }
10522 1.6 christos
10523 1.6 christos static bool
10524 1.14 christos elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10525 1.11 christos {
10526 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10527 1.11 christos }
10528 1.11 christos
10529 1.11 christos static bool
10530 1.14 christos elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10531 1.11 christos {
10532 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10533 1.11 christos }
10534 1.11 christos
10535 1.11 christos static bool
10536 1.14 christos elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10537 1.3 christos {
10538 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10539 1.3 christos }
10540 1.3 christos
10541 1.3 christos static bool
10542 1.14 christos elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10543 1.3 christos {
10544 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10545 1.3 christos }
10546 1.3 christos
10547 1.3 christos static bool
10548 1.14 christos elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10549 1.3 christos {
10550 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10551 1.3 christos }
10552 1.3 christos
10553 1.3 christos static bool
10554 1.14 christos elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10555 1.3 christos {
10556 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10557 1.3 christos }
10558 1.3 christos
10559 1.3 christos static bool
10560 1.14 christos elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10561 1.11 christos {
10562 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10563 1.11 christos }
10564 1.11 christos
10565 1.11 christos static bool
10566 1.14 christos elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10567 1.11 christos {
10568 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10569 1.11 christos }
10570 1.11 christos
10571 1.11 christos static bool
10572 1.14 christos elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10573 1.14 christos {
10574 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10575 1.14 christos }
10576 1.14 christos
10577 1.14 christos static bool
10578 1.14 christos elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10579 1.14 christos {
10580 1.17 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10581 1.17 christos note);
10582 1.17 christos }
10583 1.17 christos
10584 1.17 christos static bool
10585 1.17 christos elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10586 1.17 christos {
10587 1.17 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10588 1.17 christos }
10589 1.17 christos
10590 1.17 christos static bool
10591 1.17 christos elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10592 1.17 christos {
10593 1.17 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10594 1.17 christos }
10595 1.17 christos
10596 1.17 christos /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10597 1.17 christos successful, otherwise return FALSE. */
10598 1.17 christos
10599 1.17 christos static bool
10600 1.17 christos elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10601 1.12 christos {
10602 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10603 1.12 christos }
10604 1.12 christos
10605 1.12 christos static bool
10606 1.14 christos elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10607 1.14 christos {
10608 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10609 1.14 christos }
10610 1.14 christos
10611 1.14 christos /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10612 1.14 christos successful otherwise, return FALSE. */
10613 1.14 christos
10614 1.14 christos static bool
10615 1.14 christos elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10616 1.14 christos {
10617 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10618 1.14 christos }
10619 1.14 christos
10620 1.14 christos /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10621 1.14 christos successful otherwise, return FALSE. */
10622 1.14 christos
10623 1.14 christos static bool
10624 1.14 christos elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10625 1.14 christos {
10626 1.14 christos return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10627 1.14 christos }
10628 1.14 christos
10629 1.14 christos static bool
10630 1.14 christos elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10631 1.14 christos {
10632 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10633 1.14 christos }
10634 1.14 christos
10635 1.14 christos static bool
10636 1.14 christos elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10637 1.14 christos {
10638 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10639 1.14 christos }
10640 1.14 christos
10641 1.14 christos static bool
10642 1.14 christos elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10643 1.14 christos {
10644 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10645 1.14 christos }
10646 1.14 christos
10647 1.14 christos static bool
10648 1.1 christos elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10649 1.1 christos {
10650 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10651 1.1 christos }
10652 1.1 christos
10653 1.1 christos #if defined (HAVE_PRPSINFO_T)
10654 1.1 christos typedef prpsinfo_t elfcore_psinfo_t;
10655 1.1 christos #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10656 1.1 christos typedef prpsinfo32_t elfcore_psinfo32_t;
10657 1.1 christos #endif
10658 1.1 christos #endif
10659 1.1 christos
10660 1.1 christos #if defined (HAVE_PSINFO_T)
10661 1.1 christos typedef psinfo_t elfcore_psinfo_t;
10662 1.1 christos #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10663 1.1 christos typedef psinfo32_t elfcore_psinfo32_t;
10664 1.1 christos #endif
10665 1.1 christos #endif
10666 1.1 christos
10667 1.1 christos /* return a malloc'ed copy of a string at START which is at
10668 1.1 christos most MAX bytes long, possibly without a terminating '\0'.
10669 1.1 christos the copy will always have a terminating '\0'. */
10670 1.1 christos
10671 1.1 christos char *
10672 1.1 christos _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10673 1.1 christos {
10674 1.1 christos char *dups;
10675 1.1 christos char *end = (char *) memchr (start, '\0', max);
10676 1.1 christos size_t len;
10677 1.1 christos
10678 1.1 christos if (end == NULL)
10679 1.1 christos len = max;
10680 1.1 christos else
10681 1.1 christos len = end - start;
10682 1.1 christos
10683 1.1 christos dups = (char *) bfd_alloc (abfd, len + 1);
10684 1.1 christos if (dups == NULL)
10685 1.1 christos return NULL;
10686 1.1 christos
10687 1.1 christos memcpy (dups, start, len);
10688 1.1 christos dups[len] = '\0';
10689 1.14 christos
10690 1.1 christos return dups;
10691 1.1 christos }
10692 1.1 christos
10693 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10694 1.1 christos static bool
10695 1.1 christos elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10696 1.1 christos {
10697 1.1 christos if (note->descsz == sizeof (elfcore_psinfo_t))
10698 1.3 christos {
10699 1.3 christos elfcore_psinfo_t psinfo;
10700 1.3 christos
10701 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo));
10702 1.1 christos
10703 1.1 christos #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10704 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10705 1.3 christos #endif
10706 1.1 christos elf_tdata (abfd)->core->program
10707 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10708 1.1 christos sizeof (psinfo.pr_fname));
10709 1.1 christos
10710 1.1 christos elf_tdata (abfd)->core->command
10711 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10712 1.1 christos sizeof (psinfo.pr_psargs));
10713 1.1 christos }
10714 1.1 christos #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10715 1.1 christos else if (note->descsz == sizeof (elfcore_psinfo32_t))
10716 1.1 christos {
10717 1.3 christos /* 64-bit host, 32-bit corefile */
10718 1.3 christos elfcore_psinfo32_t psinfo;
10719 1.3 christos
10720 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo));
10721 1.1 christos
10722 1.1 christos #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10723 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10724 1.3 christos #endif
10725 1.1 christos elf_tdata (abfd)->core->program
10726 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10727 1.1 christos sizeof (psinfo.pr_fname));
10728 1.1 christos
10729 1.1 christos elf_tdata (abfd)->core->command
10730 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10731 1.1 christos sizeof (psinfo.pr_psargs));
10732 1.1 christos }
10733 1.1 christos #endif
10734 1.14 christos
10735 1.1 christos else
10736 1.1 christos {
10737 1.1 christos /* Fail - we don't know how to handle any other
10738 1.1 christos note size (ie. data object type). */
10739 1.1 christos return true;
10740 1.1 christos }
10741 1.1 christos
10742 1.3 christos /* Note that for some reason, a spurious space is tacked
10743 1.1 christos onto the end of the args in some (at least one anyway)
10744 1.1 christos implementations, so strip it off if it exists. */
10745 1.1 christos
10746 1.1 christos {
10747 1.1 christos char *command = elf_tdata (abfd)->core->command;
10748 1.1 christos int n = strlen (command);
10749 1.14 christos
10750 1.1 christos if (0 < n && command[n - 1] == ' ')
10751 1.1 christos command[n - 1] = '\0';
10752 1.1 christos }
10753 1.1 christos
10754 1.14 christos return true;
10755 1.1 christos }
10756 1.1 christos #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10757 1.1 christos
10758 1.1 christos #if defined (HAVE_PSTATUS_T)
10759 1.1 christos static bool
10760 1.1 christos elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10761 1.1 christos {
10762 1.1 christos if (note->descsz == sizeof (pstatus_t)
10763 1.1 christos #if defined (HAVE_PXSTATUS_T)
10764 1.1 christos || note->descsz == sizeof (pxstatus_t)
10765 1.1 christos #endif
10766 1.1 christos )
10767 1.3 christos {
10768 1.1 christos pstatus_t pstat;
10769 1.1 christos
10770 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat));
10771 1.1 christos
10772 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid;
10773 1.1 christos }
10774 1.1 christos #if defined (HAVE_PSTATUS32_T)
10775 1.1 christos else if (note->descsz == sizeof (pstatus32_t))
10776 1.1 christos {
10777 1.3 christos /* 64-bit host, 32-bit corefile */
10778 1.1 christos pstatus32_t pstat;
10779 1.1 christos
10780 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat));
10781 1.1 christos
10782 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid;
10783 1.1 christos }
10784 1.14 christos #endif
10785 1.1 christos /* Could grab some more details from the "representative"
10786 1.1 christos lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10787 1.1 christos NT_LWPSTATUS note, presumably. */
10788 1.1 christos
10789 1.14 christos return true;
10790 1.1 christos }
10791 1.1 christos #endif /* defined (HAVE_PSTATUS_T) */
10792 1.1 christos
10793 1.1 christos #if defined (HAVE_LWPSTATUS_T)
10794 1.1 christos static bool
10795 1.1 christos elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10796 1.1 christos {
10797 1.1 christos lwpstatus_t lwpstat;
10798 1.1 christos char buf[100];
10799 1.1 christos char *name;
10800 1.1 christos size_t len;
10801 1.1 christos asection *sect;
10802 1.1 christos
10803 1.14 christos if (note->descsz != sizeof (lwpstat)
10804 1.1 christos #if defined (HAVE_LWPXSTATUS_T)
10805 1.1 christos && note->descsz != sizeof (lwpxstatus_t)
10806 1.1 christos #endif
10807 1.3 christos )
10808 1.1 christos return true;
10809 1.1 christos
10810 1.3 christos memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10811 1.3 christos
10812 1.1 christos elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10813 1.1 christos /* Do not overwrite the core signal if it has already been set by
10814 1.1 christos another thread. */
10815 1.1 christos if (elf_tdata (abfd)->core->signal == 0)
10816 1.1 christos elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10817 1.1 christos
10818 1.1 christos /* Make a ".reg/999" section. */
10819 1.14 christos
10820 1.1 christos sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10821 1.1 christos len = strlen (buf) + 1;
10822 1.1 christos name = bfd_alloc (abfd, len);
10823 1.1 christos if (name == NULL)
10824 1.14 christos return false;
10825 1.1 christos memcpy (name, buf, len);
10826 1.1 christos
10827 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10828 1.1 christos if (sect == NULL)
10829 1.1 christos return false;
10830 1.1 christos
10831 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10832 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10833 1.1 christos sect->filepos = note->descpos
10834 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10835 1.1 christos #endif
10836 1.1 christos
10837 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG)
10838 1.1 christos sect->size = sizeof (lwpstat.pr_reg);
10839 1.1 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10840 1.14 christos #endif
10841 1.1 christos
10842 1.1 christos sect->alignment_power = 2;
10843 1.1 christos
10844 1.1 christos if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10845 1.1 christos return false;
10846 1.1 christos
10847 1.1 christos /* Make a ".reg2/999" section */
10848 1.14 christos
10849 1.1 christos sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10850 1.1 christos len = strlen (buf) + 1;
10851 1.1 christos name = bfd_alloc (abfd, len);
10852 1.1 christos if (name == NULL)
10853 1.14 christos return false;
10854 1.1 christos memcpy (name, buf, len);
10855 1.1 christos
10856 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10857 1.1 christos if (sect == NULL)
10858 1.1 christos return false;
10859 1.1 christos
10860 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10861 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10862 1.1 christos sect->filepos = note->descpos
10863 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10864 1.1 christos #endif
10865 1.1 christos
10866 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10867 1.1 christos sect->size = sizeof (lwpstat.pr_fpreg);
10868 1.1 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10869 1.1 christos #endif
10870 1.1 christos
10871 1.1 christos sect->alignment_power = 2;
10872 1.12 christos
10873 1.12 christos return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10874 1.12 christos }
10875 1.12 christos #endif /* defined (HAVE_LWPSTATUS_T) */
10876 1.12 christos
10877 1.12 christos /* These constants, and the structure offsets used below, are defined by
10878 1.12 christos Cygwin's core_dump.h */
10879 1.14 christos #define NOTE_INFO_PROCESS 1
10880 1.1 christos #define NOTE_INFO_THREAD 2
10881 1.1 christos #define NOTE_INFO_MODULE 3
10882 1.1 christos #define NOTE_INFO_MODULE64 4
10883 1.1 christos
10884 1.1 christos static bool
10885 1.12 christos elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10886 1.1 christos {
10887 1.12 christos char buf[30];
10888 1.1 christos char *name;
10889 1.1 christos size_t len;
10890 1.1 christos unsigned int name_size;
10891 1.12 christos asection *sect;
10892 1.14 christos unsigned int type;
10893 1.1 christos int is_active_thread;
10894 1.14 christos bfd_vma base_addr;
10895 1.14 christos
10896 1.1 christos if (note->descsz < 4)
10897 1.1 christos return true;
10898 1.1 christos
10899 1.14 christos if (! startswith (note->namedata, "win32"))
10900 1.14 christos return true;
10901 1.12 christos
10902 1.12 christos type = bfd_get_32 (abfd, note->descdata);
10903 1.12 christos
10904 1.12 christos struct
10905 1.12 christos {
10906 1.12 christos const char *type_name;
10907 1.12 christos unsigned long min_size;
10908 1.12 christos } size_check[] =
10909 1.12 christos {
10910 1.12 christos { "NOTE_INFO_PROCESS", 12 },
10911 1.14 christos { "NOTE_INFO_THREAD", 12 },
10912 1.14 christos { "NOTE_INFO_MODULE", 12 },
10913 1.12 christos { "NOTE_INFO_MODULE64", 16 },
10914 1.12 christos };
10915 1.12 christos
10916 1.14 christos if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10917 1.14 christos return true;
10918 1.14 christos
10919 1.14 christos if (note->descsz < size_check[type - 1].min_size)
10920 1.12 christos {
10921 1.12 christos _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10922 1.1 christos " is too small"),
10923 1.1 christos abfd, size_check[type - 1].type_name, note->descsz);
10924 1.12 christos return true;
10925 1.3 christos }
10926 1.12 christos
10927 1.12 christos switch (type)
10928 1.1 christos {
10929 1.1 christos case NOTE_INFO_PROCESS:
10930 1.12 christos /* FIXME: need to add ->core->command. */
10931 1.12 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10932 1.14 christos elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10933 1.1 christos break;
10934 1.12 christos
10935 1.1 christos case NOTE_INFO_THREAD:
10936 1.1 christos /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10937 1.1 christos structure. */
10938 1.1 christos /* thread_info.tid */
10939 1.14 christos sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10940 1.1 christos
10941 1.1 christos len = strlen (buf) + 1;
10942 1.1 christos name = (char *) bfd_alloc (abfd, len);
10943 1.1 christos if (name == NULL)
10944 1.1 christos return false;
10945 1.14 christos
10946 1.1 christos memcpy (name, buf, len);
10947 1.1 christos
10948 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10949 1.1 christos if (sect == NULL)
10950 1.1 christos return false;
10951 1.1 christos
10952 1.1 christos /* sizeof (thread_info.thread_context) */
10953 1.1 christos sect->size = note->descsz - 12;
10954 1.1 christos /* offsetof (thread_info.thread_context) */
10955 1.1 christos sect->filepos = note->descpos + 12;
10956 1.1 christos sect->alignment_power = 2;
10957 1.1 christos
10958 1.14 christos /* thread_info.is_active_thread */
10959 1.1 christos is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10960 1.1 christos
10961 1.12 christos if (is_active_thread)
10962 1.12 christos if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10963 1.1 christos return false;
10964 1.12 christos break;
10965 1.14 christos
10966 1.14 christos case NOTE_INFO_MODULE:
10967 1.14 christos case NOTE_INFO_MODULE64:
10968 1.14 christos /* Make a ".module/xxxxxxxx" section. */
10969 1.14 christos if (type == NOTE_INFO_MODULE)
10970 1.14 christos {
10971 1.14 christos /* module_info.base_address */
10972 1.12 christos base_addr = bfd_get_32 (abfd, note->descdata + 4);
10973 1.14 christos sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10974 1.14 christos /* module_info.module_name_size */
10975 1.14 christos name_size = bfd_get_32 (abfd, note->descdata + 8);
10976 1.14 christos }
10977 1.14 christos else /* NOTE_INFO_MODULE64 */
10978 1.14 christos {
10979 1.14 christos /* module_info.base_address */
10980 1.1 christos base_addr = bfd_get_64 (abfd, note->descdata + 4);
10981 1.1 christos sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10982 1.1 christos /* module_info.module_name_size */
10983 1.1 christos name_size = bfd_get_32 (abfd, note->descdata + 12);
10984 1.14 christos }
10985 1.1 christos
10986 1.1 christos len = strlen (buf) + 1;
10987 1.1 christos name = (char *) bfd_alloc (abfd, len);
10988 1.1 christos if (name == NULL)
10989 1.1 christos return false;
10990 1.1 christos
10991 1.14 christos memcpy (name, buf, len);
10992 1.1 christos
10993 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10994 1.14 christos
10995 1.14 christos if (sect == NULL)
10996 1.14 christos return false;
10997 1.14 christos
10998 1.14 christos if (note->descsz < 12 + name_size)
10999 1.14 christos {
11000 1.12 christos _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11001 1.1 christos " is too small to contain a name of size %u"),
11002 1.1 christos abfd, note->descsz, name_size);
11003 1.1 christos return true;
11004 1.1 christos }
11005 1.1 christos
11006 1.1 christos sect->size = note->descsz;
11007 1.14 christos sect->filepos = note->descpos;
11008 1.1 christos sect->alignment_power = 2;
11009 1.1 christos break;
11010 1.14 christos
11011 1.1 christos default:
11012 1.1 christos return true;
11013 1.14 christos }
11014 1.1 christos
11015 1.1 christos return true;
11016 1.1 christos }
11017 1.1 christos
11018 1.1 christos static bool
11019 1.1 christos elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11020 1.1 christos {
11021 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11022 1.1 christos
11023 1.1 christos switch (note->type)
11024 1.1 christos {
11025 1.1 christos default:
11026 1.14 christos return true;
11027 1.1 christos
11028 1.1 christos case NT_PRSTATUS:
11029 1.1 christos if (bed->elf_backend_grok_prstatus)
11030 1.14 christos if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11031 1.1 christos return true;
11032 1.1 christos #if defined (HAVE_PRSTATUS_T)
11033 1.1 christos return elfcore_grok_prstatus (abfd, note);
11034 1.1 christos #else
11035 1.1 christos return true;
11036 1.1 christos #endif
11037 1.1 christos
11038 1.1 christos #if defined (HAVE_PSTATUS_T)
11039 1.1 christos case NT_PSTATUS:
11040 1.1 christos return elfcore_grok_pstatus (abfd, note);
11041 1.1 christos #endif
11042 1.1 christos
11043 1.1 christos #if defined (HAVE_LWPSTATUS_T)
11044 1.1 christos case NT_LWPSTATUS:
11045 1.1 christos return elfcore_grok_lwpstatus (abfd, note);
11046 1.1 christos #endif
11047 1.1 christos
11048 1.1 christos case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11049 1.1 christos return elfcore_grok_prfpreg (abfd, note);
11050 1.1 christos
11051 1.1 christos case NT_WIN32PSTATUS:
11052 1.1 christos return elfcore_grok_win32pstatus (abfd, note);
11053 1.1 christos
11054 1.14 christos case NT_PRXFPREG: /* Linux SSE extension */
11055 1.1 christos if (note->namesz == 6
11056 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
11057 1.1 christos return elfcore_grok_prxfpreg (abfd, note);
11058 1.1 christos else
11059 1.1 christos return true;
11060 1.1 christos
11061 1.14 christos case NT_X86_XSTATE: /* Linux XSAVE extension */
11062 1.1 christos if (note->namesz == 6
11063 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
11064 1.1 christos return elfcore_grok_xstatereg (abfd, note);
11065 1.1 christos else
11066 1.1 christos return true;
11067 1.1 christos
11068 1.14 christos case NT_PPC_VMX:
11069 1.1 christos if (note->namesz == 6
11070 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
11071 1.1 christos return elfcore_grok_ppc_vmx (abfd, note);
11072 1.11 christos else
11073 1.11 christos return true;
11074 1.11 christos
11075 1.14 christos case NT_PPC_VSX:
11076 1.11 christos if (note->namesz == 6
11077 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11078 1.11 christos return elfcore_grok_ppc_vsx (abfd, note);
11079 1.12 christos else
11080 1.12 christos return true;
11081 1.1 christos
11082 1.14 christos case NT_PPC_TAR:
11083 1.1 christos if (note->namesz == 6
11084 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11085 1.11 christos return elfcore_grok_ppc_tar (abfd, note);
11086 1.12 christos else
11087 1.12 christos return true;
11088 1.11 christos
11089 1.14 christos case NT_PPC_PPR:
11090 1.11 christos if (note->namesz == 6
11091 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11092 1.11 christos return elfcore_grok_ppc_ppr (abfd, note);
11093 1.12 christos else
11094 1.12 christos return true;
11095 1.11 christos
11096 1.14 christos case NT_PPC_DSCR:
11097 1.11 christos if (note->namesz == 6
11098 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11099 1.11 christos return elfcore_grok_ppc_dscr (abfd, note);
11100 1.12 christos else
11101 1.12 christos return true;
11102 1.11 christos
11103 1.14 christos case NT_PPC_EBB:
11104 1.11 christos if (note->namesz == 6
11105 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11106 1.1 christos return elfcore_grok_ppc_ebb (abfd, note);
11107 1.12 christos else
11108 1.12 christos return true;
11109 1.1 christos
11110 1.14 christos case NT_PPC_PMU:
11111 1.1 christos if (note->namesz == 6
11112 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11113 1.1 christos return elfcore_grok_ppc_pmu (abfd, note);
11114 1.12 christos else
11115 1.12 christos return true;
11116 1.1 christos
11117 1.14 christos case NT_PPC_TM_CGPR:
11118 1.1 christos if (note->namesz == 6
11119 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11120 1.1 christos return elfcore_grok_ppc_tm_cgpr (abfd, note);
11121 1.12 christos else
11122 1.12 christos return true;
11123 1.1 christos
11124 1.14 christos case NT_PPC_TM_CFPR:
11125 1.1 christos if (note->namesz == 6
11126 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11127 1.1 christos return elfcore_grok_ppc_tm_cfpr (abfd, note);
11128 1.12 christos else
11129 1.12 christos return true;
11130 1.1 christos
11131 1.14 christos case NT_PPC_TM_CVMX:
11132 1.1 christos if (note->namesz == 6
11133 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11134 1.1 christos return elfcore_grok_ppc_tm_cvmx (abfd, note);
11135 1.12 christos else
11136 1.12 christos return true;
11137 1.1 christos
11138 1.14 christos case NT_PPC_TM_CVSX:
11139 1.1 christos if (note->namesz == 6
11140 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11141 1.1 christos return elfcore_grok_ppc_tm_cvsx (abfd, note);
11142 1.12 christos else
11143 1.12 christos return true;
11144 1.1 christos
11145 1.14 christos case NT_PPC_TM_SPR:
11146 1.1 christos if (note->namesz == 6
11147 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11148 1.3 christos return elfcore_grok_ppc_tm_spr (abfd, note);
11149 1.12 christos else
11150 1.12 christos return true;
11151 1.3 christos
11152 1.14 christos case NT_PPC_TM_CTAR:
11153 1.3 christos if (note->namesz == 6
11154 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11155 1.3 christos return elfcore_grok_ppc_tm_ctar (abfd, note);
11156 1.12 christos else
11157 1.12 christos return true;
11158 1.3 christos
11159 1.14 christos case NT_PPC_TM_CPPR:
11160 1.3 christos if (note->namesz == 6
11161 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11162 1.3 christos return elfcore_grok_ppc_tm_cppr (abfd, note);
11163 1.12 christos else
11164 1.12 christos return true;
11165 1.3 christos
11166 1.14 christos case NT_PPC_TM_CDSCR:
11167 1.3 christos if (note->namesz == 6
11168 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11169 1.11 christos return elfcore_grok_ppc_tm_cdscr (abfd, note);
11170 1.11 christos else
11171 1.11 christos return true;
11172 1.11 christos
11173 1.14 christos case NT_S390_HIGH_GPRS:
11174 1.11 christos if (note->namesz == 6
11175 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11176 1.11 christos return elfcore_grok_s390_high_gprs (abfd, note);
11177 1.11 christos else
11178 1.11 christos return true;
11179 1.11 christos
11180 1.14 christos case NT_S390_TIMER:
11181 1.11 christos if (note->namesz == 6
11182 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11183 1.11 christos return elfcore_grok_s390_timer (abfd, note);
11184 1.11 christos else
11185 1.11 christos return true;
11186 1.11 christos
11187 1.14 christos case NT_S390_TODCMP:
11188 1.11 christos if (note->namesz == 6
11189 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11190 1.11 christos return elfcore_grok_s390_todcmp (abfd, note);
11191 1.11 christos else
11192 1.11 christos return true;
11193 1.11 christos
11194 1.14 christos case NT_S390_TODPREG:
11195 1.11 christos if (note->namesz == 6
11196 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11197 1.11 christos return elfcore_grok_s390_todpreg (abfd, note);
11198 1.11 christos else
11199 1.11 christos return true;
11200 1.11 christos
11201 1.14 christos case NT_S390_CTRS:
11202 1.11 christos if (note->namesz == 6
11203 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11204 1.11 christos return elfcore_grok_s390_ctrs (abfd, note);
11205 1.11 christos else
11206 1.11 christos return true;
11207 1.11 christos
11208 1.14 christos case NT_S390_PREFIX:
11209 1.11 christos if (note->namesz == 6
11210 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11211 1.11 christos return elfcore_grok_s390_prefix (abfd, note);
11212 1.11 christos else
11213 1.11 christos return true;
11214 1.11 christos
11215 1.14 christos case NT_S390_LAST_BREAK:
11216 1.11 christos if (note->namesz == 6
11217 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11218 1.11 christos return elfcore_grok_s390_last_break (abfd, note);
11219 1.11 christos else
11220 1.11 christos return true;
11221 1.11 christos
11222 1.14 christos case NT_S390_SYSTEM_CALL:
11223 1.11 christos if (note->namesz == 6
11224 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11225 1.11 christos return elfcore_grok_s390_system_call (abfd, note);
11226 1.11 christos else
11227 1.11 christos return true;
11228 1.11 christos
11229 1.14 christos case NT_S390_TDB:
11230 1.11 christos if (note->namesz == 6
11231 1.6 christos && strcmp (note->namedata, "LINUX") == 0)
11232 1.6 christos return elfcore_grok_s390_tdb (abfd, note);
11233 1.6 christos else
11234 1.6 christos return true;
11235 1.6 christos
11236 1.14 christos case NT_S390_VXRS_LOW:
11237 1.6 christos if (note->namesz == 6
11238 1.6 christos && strcmp (note->namedata, "LINUX") == 0)
11239 1.6 christos return elfcore_grok_s390_vxrs_low (abfd, note);
11240 1.6 christos else
11241 1.6 christos return true;
11242 1.6 christos
11243 1.14 christos case NT_S390_VXRS_HIGH:
11244 1.6 christos if (note->namesz == 6
11245 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11246 1.11 christos return elfcore_grok_s390_vxrs_high (abfd, note);
11247 1.11 christos else
11248 1.11 christos return true;
11249 1.11 christos
11250 1.14 christos case NT_S390_GS_CB:
11251 1.11 christos if (note->namesz == 6
11252 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11253 1.11 christos return elfcore_grok_s390_gs_cb (abfd, note);
11254 1.11 christos else
11255 1.11 christos return true;
11256 1.11 christos
11257 1.14 christos case NT_S390_GS_BC:
11258 1.11 christos if (note->namesz == 6
11259 1.12 christos && strcmp (note->namedata, "LINUX") == 0)
11260 1.12 christos return elfcore_grok_s390_gs_bc (abfd, note);
11261 1.12 christos else
11262 1.12 christos return true;
11263 1.12 christos
11264 1.14 christos case NT_ARC_V2:
11265 1.12 christos if (note->namesz == 6
11266 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
11267 1.3 christos return elfcore_grok_arc_v2 (abfd, note);
11268 1.3 christos else
11269 1.3 christos return true;
11270 1.3 christos
11271 1.14 christos case NT_ARM_VFP:
11272 1.3 christos if (note->namesz == 6
11273 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
11274 1.3 christos return elfcore_grok_arm_vfp (abfd, note);
11275 1.3 christos else
11276 1.3 christos return true;
11277 1.3 christos
11278 1.14 christos case NT_ARM_TLS:
11279 1.3 christos if (note->namesz == 6
11280 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
11281 1.3 christos return elfcore_grok_aarch_tls (abfd, note);
11282 1.3 christos else
11283 1.3 christos return true;
11284 1.3 christos
11285 1.14 christos case NT_ARM_HW_BREAK:
11286 1.3 christos if (note->namesz == 6
11287 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
11288 1.3 christos return elfcore_grok_aarch_hw_break (abfd, note);
11289 1.3 christos else
11290 1.3 christos return true;
11291 1.3 christos
11292 1.14 christos case NT_ARM_HW_WATCH:
11293 1.3 christos if (note->namesz == 6
11294 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11295 1.11 christos return elfcore_grok_aarch_hw_watch (abfd, note);
11296 1.11 christos else
11297 1.11 christos return true;
11298 1.11 christos
11299 1.14 christos case NT_ARM_SVE:
11300 1.11 christos if (note->namesz == 6
11301 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
11302 1.11 christos return elfcore_grok_aarch_sve (abfd, note);
11303 1.11 christos else
11304 1.11 christos return true;
11305 1.11 christos
11306 1.14 christos case NT_ARM_PAC_MASK:
11307 1.14 christos if (note->namesz == 6
11308 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
11309 1.14 christos return elfcore_grok_aarch_pauth (abfd, note);
11310 1.14 christos else
11311 1.14 christos return true;
11312 1.14 christos
11313 1.14 christos case NT_ARM_TAGGED_ADDR_CTRL:
11314 1.14 christos if (note->namesz == 6
11315 1.17 christos && strcmp (note->namedata, "LINUX") == 0)
11316 1.17 christos return elfcore_grok_aarch_mte (abfd, note);
11317 1.17 christos else
11318 1.17 christos return true;
11319 1.17 christos
11320 1.17 christos case NT_ARM_SSVE:
11321 1.17 christos if (note->namesz == 6
11322 1.17 christos && strcmp (note->namedata, "LINUX") == 0)
11323 1.17 christos return elfcore_grok_aarch_ssve (abfd, note);
11324 1.17 christos else
11325 1.17 christos return true;
11326 1.17 christos
11327 1.17 christos case NT_ARM_ZA:
11328 1.17 christos if (note->namesz == 6
11329 1.17 christos && strcmp (note->namedata, "LINUX") == 0)
11330 1.17 christos return elfcore_grok_aarch_za (abfd, note);
11331 1.17 christos else
11332 1.17 christos return true;
11333 1.17 christos
11334 1.17 christos case NT_ARM_ZT:
11335 1.17 christos if (note->namesz == 6
11336 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
11337 1.14 christos return elfcore_grok_aarch_zt (abfd, note);
11338 1.14 christos else
11339 1.14 christos return true;
11340 1.14 christos
11341 1.14 christos case NT_GDB_TDESC:
11342 1.14 christos if (note->namesz == 4
11343 1.14 christos && strcmp (note->namedata, "GDB") == 0)
11344 1.14 christos return elfcore_grok_gdb_tdesc (abfd, note);
11345 1.14 christos else
11346 1.14 christos return true;
11347 1.14 christos
11348 1.14 christos case NT_RISCV_CSR:
11349 1.14 christos if (note->namesz == 4
11350 1.14 christos && strcmp (note->namedata, "GDB") == 0)
11351 1.14 christos return elfcore_grok_riscv_csr (abfd, note);
11352 1.14 christos else
11353 1.14 christos return true;
11354 1.14 christos
11355 1.14 christos case NT_LARCH_CPUCFG:
11356 1.14 christos if (note->namesz == 6
11357 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
11358 1.14 christos return elfcore_grok_loongarch_cpucfg (abfd, note);
11359 1.14 christos else
11360 1.14 christos return true;
11361 1.14 christos
11362 1.14 christos case NT_LARCH_LBT:
11363 1.14 christos if (note->namesz == 6
11364 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
11365 1.14 christos return elfcore_grok_loongarch_lbt (abfd, note);
11366 1.14 christos else
11367 1.14 christos return true;
11368 1.14 christos
11369 1.14 christos case NT_LARCH_LSX:
11370 1.14 christos if (note->namesz == 6
11371 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
11372 1.14 christos return elfcore_grok_loongarch_lsx (abfd, note);
11373 1.14 christos else
11374 1.14 christos return true;
11375 1.14 christos
11376 1.14 christos case NT_LARCH_LASX:
11377 1.11 christos if (note->namesz == 6
11378 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
11379 1.1 christos return elfcore_grok_loongarch_lasx (abfd, note);
11380 1.1 christos else
11381 1.1 christos return true;
11382 1.14 christos
11383 1.1 christos case NT_PRPSINFO:
11384 1.1 christos case NT_PSINFO:
11385 1.1 christos if (bed->elf_backend_grok_psinfo)
11386 1.14 christos if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11387 1.1 christos return true;
11388 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11389 1.1 christos return elfcore_grok_psinfo (abfd, note);
11390 1.12 christos #else
11391 1.3 christos return true;
11392 1.3 christos #endif
11393 1.3 christos
11394 1.3 christos case NT_AUXV:
11395 1.3 christos return elfcore_make_auxv_note_section (abfd, note, 0);
11396 1.3 christos
11397 1.3 christos case NT_FILE:
11398 1.3 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11399 1.8 christos note);
11400 1.1 christos
11401 1.1 christos case NT_SIGINFO:
11402 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11403 1.14 christos note);
11404 1.1 christos
11405 1.1 christos }
11406 1.6 christos }
11407 1.3 christos
11408 1.3 christos static bool
11409 1.14 christos elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11410 1.1 christos {
11411 1.6 christos struct bfd_build_id* build_id;
11412 1.6 christos
11413 1.14 christos if (note->descsz == 0)
11414 1.3 christos return false;
11415 1.6 christos
11416 1.6 christos build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11417 1.6 christos if (build_id == NULL)
11418 1.1 christos return false;
11419 1.14 christos
11420 1.1 christos build_id->size = note->descsz;
11421 1.1 christos memcpy (build_id->data, note->descdata, note->descsz);
11422 1.14 christos abfd->build_id = build_id;
11423 1.1 christos
11424 1.1 christos return true;
11425 1.1 christos }
11426 1.1 christos
11427 1.1 christos static bool
11428 1.14 christos elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11429 1.1 christos {
11430 1.9 christos switch (note->type)
11431 1.9 christos {
11432 1.9 christos default:
11433 1.1 christos return true;
11434 1.1 christos
11435 1.1 christos case NT_GNU_PROPERTY_TYPE_0:
11436 1.1 christos return _bfd_elf_parse_gnu_properties (abfd, note);
11437 1.1 christos
11438 1.14 christos case NT_GNU_BUILD_ID:
11439 1.3 christos return elfobj_grok_gnu_build_id (abfd, note);
11440 1.3 christos }
11441 1.3 christos }
11442 1.12 christos
11443 1.12 christos static bool
11444 1.3 christos elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11445 1.3 christos {
11446 1.3 christos struct sdt_note *cur =
11447 1.3 christos (struct sdt_note *) bfd_alloc (abfd,
11448 1.3 christos sizeof (struct sdt_note) + note->descsz);
11449 1.3 christos
11450 1.3 christos cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11451 1.14 christos cur->size = (bfd_size_type) note->descsz;
11452 1.3 christos memcpy (cur->data, note->descdata, note->descsz);
11453 1.3 christos
11454 1.14 christos elf_tdata (abfd)->sdt_note_head = cur;
11455 1.3 christos
11456 1.3 christos return true;
11457 1.3 christos }
11458 1.3 christos
11459 1.3 christos static bool
11460 1.3 christos elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11461 1.3 christos {
11462 1.3 christos switch (note->type)
11463 1.14 christos {
11464 1.3 christos case NT_STAPSDT:
11465 1.3 christos return elfobj_grok_stapsdt_note_1 (abfd, note);
11466 1.3 christos
11467 1.14 christos default:
11468 1.8 christos return true;
11469 1.8 christos }
11470 1.8 christos }
11471 1.8 christos
11472 1.11 christos static bool
11473 1.8 christos elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11474 1.11 christos {
11475 1.8 christos size_t offset;
11476 1.14 christos
11477 1.8 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11478 1.8 christos {
11479 1.11 christos case ELFCLASS32:
11480 1.8 christos if (note->descsz < 108)
11481 1.14 christos return false;
11482 1.8 christos break;
11483 1.8 christos
11484 1.8 christos case ELFCLASS64:
11485 1.14 christos if (note->descsz < 120)
11486 1.8 christos return false;
11487 1.8 christos break;
11488 1.8 christos
11489 1.8 christos default:
11490 1.14 christos return false;
11491 1.11 christos }
11492 1.8 christos
11493 1.8 christos /* Check for version 1 in pr_version. */
11494 1.8 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11495 1.11 christos return false;
11496 1.8 christos
11497 1.8 christos offset = 4;
11498 1.8 christos
11499 1.8 christos /* Skip over pr_psinfosz. */
11500 1.8 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11501 1.8 christos offset += 4;
11502 1.8 christos else
11503 1.8 christos {
11504 1.8 christos offset += 4; /* Padding before pr_psinfosz. */
11505 1.8 christos offset += 8;
11506 1.8 christos }
11507 1.8 christos
11508 1.8 christos /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11509 1.8 christos elf_tdata (abfd)->core->program
11510 1.8 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11511 1.8 christos offset += 17;
11512 1.8 christos
11513 1.8 christos /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11514 1.8 christos elf_tdata (abfd)->core->command
11515 1.8 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11516 1.8 christos offset += 81;
11517 1.8 christos
11518 1.14 christos /* Padding before pr_pid. */
11519 1.8 christos offset += 2;
11520 1.8 christos
11521 1.8 christos /* The pr_pid field was added in version "1a". */
11522 1.8 christos if (note->descsz < offset + 4)
11523 1.14 christos return true;
11524 1.8 christos
11525 1.8 christos elf_tdata (abfd)->core->pid
11526 1.14 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11527 1.8 christos
11528 1.8 christos return true;
11529 1.8 christos }
11530 1.8 christos
11531 1.11 christos static bool
11532 1.8 christos elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11533 1.11 christos {
11534 1.11 christos size_t offset;
11535 1.11 christos size_t size;
11536 1.11 christos size_t min_size;
11537 1.11 christos
11538 1.11 christos /* Compute offset of pr_getregsz, skipping over pr_statussz.
11539 1.11 christos Also compute minimum size of this note. */
11540 1.8 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11541 1.8 christos {
11542 1.11 christos case ELFCLASS32:
11543 1.11 christos offset = 4 + 4;
11544 1.11 christos min_size = offset + (4 * 2) + 4 + 4 + 4;
11545 1.8 christos break;
11546 1.8 christos
11547 1.8 christos case ELFCLASS64:
11548 1.14 christos offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11549 1.8 christos min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11550 1.8 christos break;
11551 1.11 christos
11552 1.14 christos default:
11553 1.11 christos return false;
11554 1.11 christos }
11555 1.11 christos
11556 1.14 christos if (note->descsz < min_size)
11557 1.11 christos return false;
11558 1.8 christos
11559 1.11 christos /* Check for version 1 in pr_version. */
11560 1.11 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11561 1.11 christos return false;
11562 1.11 christos
11563 1.11 christos /* Extract size of pr_reg from pr_gregsetsz. */
11564 1.11 christos /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11565 1.8 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11566 1.11 christos {
11567 1.11 christos size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11568 1.11 christos offset += 4 * 2;
11569 1.11 christos }
11570 1.8 christos else
11571 1.11 christos {
11572 1.8 christos size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11573 1.8 christos offset += 8 * 2;
11574 1.11 christos }
11575 1.8 christos
11576 1.8 christos /* Skip over pr_osreldate. */
11577 1.8 christos offset += 4;
11578 1.8 christos
11579 1.8 christos /* Read signal from pr_cursig. */
11580 1.11 christos if (elf_tdata (abfd)->core->signal == 0)
11581 1.8 christos elf_tdata (abfd)->core->signal
11582 1.8 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11583 1.8 christos offset += 4;
11584 1.8 christos
11585 1.11 christos /* Read TID from pr_pid. */
11586 1.11 christos elf_tdata (abfd)->core->lwpid
11587 1.8 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11588 1.8 christos offset += 4;
11589 1.11 christos
11590 1.11 christos /* Padding before pr_reg. */
11591 1.14 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11592 1.11 christos offset += 4;
11593 1.8 christos
11594 1.8 christos /* Make sure that there is enough data remaining in the note. */
11595 1.8 christos if ((note->descsz - offset) < size)
11596 1.8 christos return false;
11597 1.8 christos
11598 1.14 christos /* Make a ".reg/999" section and a ".reg" section. */
11599 1.8 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11600 1.8 christos size, note->descpos + offset);
11601 1.11 christos }
11602 1.11 christos
11603 1.8 christos static bool
11604 1.8 christos elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11605 1.8 christos {
11606 1.11 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11607 1.11 christos
11608 1.14 christos switch (note->type)
11609 1.8 christos {
11610 1.8 christos case NT_PRSTATUS:
11611 1.8 christos if (bed->elf_backend_grok_freebsd_prstatus)
11612 1.8 christos if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11613 1.8 christos return true;
11614 1.8 christos return elfcore_grok_freebsd_prstatus (abfd, note);
11615 1.8 christos
11616 1.8 christos case NT_FPREGSET:
11617 1.8 christos return elfcore_grok_prfpreg (abfd, note);
11618 1.14 christos
11619 1.8 christos case NT_PRPSINFO:
11620 1.11 christos return elfcore_grok_freebsd_psinfo (abfd, note);
11621 1.11 christos
11622 1.11 christos case NT_FREEBSD_THRMISC:
11623 1.11 christos return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11624 1.11 christos
11625 1.11 christos case NT_FREEBSD_PROCSTAT_PROC:
11626 1.11 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11627 1.11 christos note);
11628 1.11 christos
11629 1.11 christos case NT_FREEBSD_PROCSTAT_FILES:
11630 1.11 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11631 1.11 christos note);
11632 1.8 christos
11633 1.12 christos case NT_FREEBSD_PROCSTAT_VMMAP:
11634 1.8 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11635 1.14 christos note);
11636 1.14 christos
11637 1.14 christos case NT_FREEBSD_PROCSTAT_AUXV:
11638 1.8 christos return elfcore_make_auxv_note_section (abfd, note, 4);
11639 1.14 christos
11640 1.8 christos case NT_FREEBSD_X86_SEGBASES:
11641 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11642 1.11 christos
11643 1.11 christos case NT_X86_XSTATE:
11644 1.11 christos return elfcore_grok_xstatereg (abfd, note);
11645 1.14 christos
11646 1.14 christos case NT_FREEBSD_PTLWPINFO:
11647 1.14 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11648 1.11 christos note);
11649 1.11 christos
11650 1.11 christos case NT_ARM_TLS:
11651 1.8 christos return elfcore_grok_aarch_tls (abfd, note);
11652 1.14 christos
11653 1.8 christos case NT_ARM_VFP:
11654 1.8 christos return elfcore_grok_arm_vfp (abfd, note);
11655 1.8 christos
11656 1.14 christos default:
11657 1.1 christos return true;
11658 1.1 christos }
11659 1.1 christos }
11660 1.1 christos
11661 1.1 christos static bool
11662 1.1 christos elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11663 1.1 christos {
11664 1.1 christos char *cp;
11665 1.14 christos
11666 1.1 christos cp = strchr (note->namedata, '@');
11667 1.14 christos if (cp != NULL)
11668 1.1 christos {
11669 1.1 christos *lwpidp = atoi(cp + 1);
11670 1.14 christos return true;
11671 1.1 christos }
11672 1.1 christos return false;
11673 1.11 christos }
11674 1.14 christos
11675 1.11 christos static bool
11676 1.1 christos elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11677 1.3 christos {
11678 1.1 christos if (note->descsz <= 0x7c + 31)
11679 1.1 christos return false;
11680 1.1 christos
11681 1.3 christos /* Signal number at offset 0x08. */
11682 1.1 christos elf_tdata (abfd)->core->signal
11683 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11684 1.1 christos
11685 1.3 christos /* Process ID at offset 0x50. */
11686 1.1 christos elf_tdata (abfd)->core->pid
11687 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11688 1.1 christos
11689 1.1 christos /* Command name at 0x7c (max 32 bytes, including nul). */
11690 1.1 christos elf_tdata (abfd)->core->command
11691 1.1 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11692 1.14 christos
11693 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11694 1.1 christos note);
11695 1.1 christos }
11696 1.1 christos
11697 1.1 christos static bool
11698 1.3 christos elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11699 1.1 christos {
11700 1.12 christos int lwp;
11701 1.1 christos
11702 1.12 christos if (elfcore_netbsd_get_lwpid (note, &lwp))
11703 1.1 christos elf_tdata (abfd)->core->lwpid = lwp;
11704 1.1 christos
11705 1.1 christos switch (note->type)
11706 1.1 christos {
11707 1.1 christos case NT_NETBSDCORE_PROCINFO:
11708 1.12 christos /* NetBSD-specific core "procinfo". Note that we expect to
11709 1.12 christos find this note before any of the others, which is fine,
11710 1.15 rin since the kernel writes this note out first when it
11711 1.12 christos creates a core file. */
11712 1.12 christos return elfcore_grok_netbsd_procinfo (abfd, note);
11713 1.12 christos case NT_NETBSDCORE_AUXV:
11714 1.12 christos /* NetBSD-specific Elf Auxiliary Vector data. */
11715 1.12 christos return elfcore_make_auxv_note_section (abfd, note, 0);
11716 1.12 christos case NT_NETBSDCORE_LWPSTATUS:
11717 1.1 christos return elfcore_make_note_pseudosection (abfd,
11718 1.1 christos ".note.netbsdcore.lwpstatus",
11719 1.7 christos note);
11720 1.7 christos default:
11721 1.7 christos break;
11722 1.7 christos }
11723 1.7 christos
11724 1.7 christos if (note->type == NT_NETBSDCORE_AUXV)
11725 1.14 christos {
11726 1.7 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
11727 1.7 christos SEC_HAS_CONTENTS);
11728 1.7 christos
11729 1.7 christos if (sect == NULL)
11730 1.14 christos return false;
11731 1.7 christos sect->size = note->descsz;
11732 1.7 christos sect->filepos = note->descpos;
11733 1.12 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11734 1.1 christos
11735 1.1 christos return true;
11736 1.1 christos }
11737 1.1 christos
11738 1.1 christos /* As of March 2020 there are no other machine-independent notes
11739 1.14 christos defined for NetBSD core files. If the note type is less
11740 1.1 christos than the start of the machine-dependent note types, we don't
11741 1.1 christos understand it. */
11742 1.1 christos
11743 1.1 christos if (note->type < NT_NETBSDCORE_FIRSTMACH)
11744 1.1 christos return true;
11745 1.1 christos
11746 1.1 christos
11747 1.10 christos switch (bfd_get_arch (abfd))
11748 1.1 christos {
11749 1.16 rin /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11750 1.1 christos PT_GETFPREGS == mach+2. */
11751 1.1 christos
11752 1.1 christos case bfd_arch_aarch64:
11753 1.1 christos case bfd_arch_alpha:
11754 1.1 christos case bfd_arch_riscv:
11755 1.1 christos case bfd_arch_sparc:
11756 1.1 christos switch (note->type)
11757 1.1 christos {
11758 1.1 christos case NT_NETBSDCORE_FIRSTMACH+0:
11759 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note);
11760 1.14 christos
11761 1.1 christos case NT_NETBSDCORE_FIRSTMACH+2:
11762 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11763 1.2 uwe
11764 1.2 uwe default:
11765 1.2 uwe return true;
11766 1.2 uwe }
11767 1.2 uwe
11768 1.2 uwe /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11769 1.2 uwe There's also old PT___GETREGS40 == mach + 1 for old reg
11770 1.2 uwe structure which lacks GBR. */
11771 1.2 uwe
11772 1.2 uwe case bfd_arch_sh:
11773 1.2 uwe switch (note->type)
11774 1.2 uwe {
11775 1.2 uwe case NT_NETBSDCORE_FIRSTMACH+3:
11776 1.2 uwe return elfcore_make_note_pseudosection (abfd, ".reg", note);
11777 1.14 christos
11778 1.2 uwe case NT_NETBSDCORE_FIRSTMACH+5:
11779 1.2 uwe return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11780 1.1 christos
11781 1.1 christos default:
11782 1.1 christos return true;
11783 1.1 christos }
11784 1.1 christos
11785 1.1 christos /* On all other arch's, PT_GETREGS == mach+1 and
11786 1.1 christos PT_GETFPREGS == mach+3. */
11787 1.1 christos
11788 1.1 christos default:
11789 1.1 christos switch (note->type)
11790 1.1 christos {
11791 1.1 christos case NT_NETBSDCORE_FIRSTMACH+1:
11792 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note);
11793 1.14 christos
11794 1.1 christos case NT_NETBSDCORE_FIRSTMACH+3:
11795 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11796 1.1 christos
11797 1.1 christos default:
11798 1.1 christos return true;
11799 1.14 christos }
11800 1.1 christos }
11801 1.1 christos /* NOTREACHED */
11802 1.11 christos }
11803 1.14 christos
11804 1.11 christos static bool
11805 1.1 christos elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11806 1.3 christos {
11807 1.1 christos if (note->descsz <= 0x48 + 31)
11808 1.1 christos return false;
11809 1.1 christos
11810 1.3 christos /* Signal number at offset 0x08. */
11811 1.1 christos elf_tdata (abfd)->core->signal
11812 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11813 1.1 christos
11814 1.3 christos /* Process ID at offset 0x20. */
11815 1.1 christos elf_tdata (abfd)->core->pid
11816 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11817 1.14 christos
11818 1.14 christos /* Command name at 0x48 (max 32 bytes, including nul). */
11819 1.14 christos elf_tdata (abfd)->core->command
11820 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11821 1.14 christos
11822 1.14 christos return true;
11823 1.14 christos }
11824 1.14 christos
11825 1.14 christos /* Processes Solaris's process status note.
11826 1.14 christos sig_off ~ offsetof(prstatus_t, pr_cursig)
11827 1.14 christos pid_off ~ offsetof(prstatus_t, pr_pid)
11828 1.14 christos lwpid_off ~ offsetof(prstatus_t, pr_who)
11829 1.14 christos gregset_size ~ sizeof(gregset_t)
11830 1.14 christos gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11831 1.14 christos
11832 1.14 christos static bool
11833 1.14 christos elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11834 1.14 christos int pid_off, int lwpid_off, size_t gregset_size,
11835 1.14 christos size_t gregset_offset)
11836 1.14 christos {
11837 1.14 christos asection *sect = NULL;
11838 1.14 christos elf_tdata (abfd)->core->signal
11839 1.14 christos = bfd_get_16 (abfd, note->descdata + sig_off);
11840 1.14 christos elf_tdata (abfd)->core->pid
11841 1.14 christos = bfd_get_32 (abfd, note->descdata + pid_off);
11842 1.14 christos elf_tdata (abfd)->core->lwpid
11843 1.14 christos = bfd_get_32 (abfd, note->descdata + lwpid_off);
11844 1.14 christos
11845 1.14 christos sect = bfd_get_section_by_name (abfd, ".reg");
11846 1.14 christos if (sect != NULL)
11847 1.14 christos sect->size = gregset_size;
11848 1.14 christos
11849 1.14 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11850 1.14 christos note->descpos + gregset_offset);
11851 1.14 christos }
11852 1.14 christos
11853 1.14 christos /* Gets program and arguments from a core.
11854 1.14 christos prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11855 1.14 christos comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11856 1.14 christos
11857 1.14 christos static bool
11858 1.14 christos elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11859 1.14 christos int prog_off, int comm_off)
11860 1.14 christos {
11861 1.14 christos elf_tdata (abfd)->core->program
11862 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11863 1.14 christos elf_tdata (abfd)->core->command
11864 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11865 1.14 christos
11866 1.14 christos return true;
11867 1.14 christos }
11868 1.14 christos
11869 1.14 christos /* Processes Solaris's LWP status note.
11870 1.14 christos gregset_size ~ sizeof(gregset_t)
11871 1.14 christos gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11872 1.14 christos fpregset_size ~ sizeof(fpregset_t)
11873 1.14 christos fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11874 1.14 christos
11875 1.14 christos static bool
11876 1.14 christos elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11877 1.14 christos size_t gregset_size, int gregset_off,
11878 1.14 christos size_t fpregset_size, int fpregset_off)
11879 1.14 christos {
11880 1.14 christos asection *sect = NULL;
11881 1.14 christos char reg2_section_name[16] = { 0 };
11882 1.14 christos
11883 1.14 christos (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11884 1.14 christos elf_tdata (abfd)->core->lwpid);
11885 1.14 christos
11886 1.14 christos /* offsetof(lwpstatus_t, pr_lwpid) */
11887 1.14 christos elf_tdata (abfd)->core->lwpid
11888 1.14 christos = bfd_get_32 (abfd, note->descdata + 4);
11889 1.14 christos /* offsetof(lwpstatus_t, pr_cursig) */
11890 1.14 christos elf_tdata (abfd)->core->signal
11891 1.14 christos = bfd_get_16 (abfd, note->descdata + 12);
11892 1.14 christos
11893 1.14 christos sect = bfd_get_section_by_name (abfd, ".reg");
11894 1.14 christos if (sect != NULL)
11895 1.14 christos sect->size = gregset_size;
11896 1.14 christos else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11897 1.14 christos note->descpos + gregset_off))
11898 1.14 christos return false;
11899 1.14 christos
11900 1.14 christos sect = bfd_get_section_by_name (abfd, reg2_section_name);
11901 1.14 christos if (sect != NULL)
11902 1.14 christos {
11903 1.14 christos sect->size = fpregset_size;
11904 1.14 christos sect->filepos = note->descpos + fpregset_off;
11905 1.14 christos sect->alignment_power = 2;
11906 1.14 christos }
11907 1.14 christos else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11908 1.14 christos note->descpos + fpregset_off))
11909 1.14 christos return false;
11910 1.14 christos
11911 1.14 christos return true;
11912 1.14 christos }
11913 1.14 christos
11914 1.14 christos static bool
11915 1.14 christos elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11916 1.14 christos {
11917 1.14 christos if (note == NULL)
11918 1.14 christos return false;
11919 1.14 christos
11920 1.14 christos /* core files are identified as 32- or 64-bit, SPARC or x86,
11921 1.14 christos by the size of the descsz which matches the sizeof()
11922 1.14 christos the type appropriate for that note type (e.g., prstatus_t for
11923 1.14 christos SOLARIS_NT_PRSTATUS) for the corresponding architecture
11924 1.14 christos on Solaris. The core file bitness may differ from the bitness of
11925 1.14 christos gdb itself, so fixed values are used instead of sizeof().
11926 1.14 christos Appropriate fixed offsets are also used to obtain data from
11927 1.14 christos the note. */
11928 1.14 christos
11929 1.14 christos switch ((int) note->type)
11930 1.14 christos {
11931 1.14 christos case SOLARIS_NT_PRSTATUS:
11932 1.14 christos switch (note->descsz)
11933 1.14 christos {
11934 1.14 christos case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11935 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11936 1.14 christos 136, 216, 308, 152, 356);
11937 1.14 christos case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11938 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11939 1.14 christos 264, 360, 520, 304, 600);
11940 1.14 christos case 432: /* sizeof(prstatus_t) Intel 32-bit */
11941 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11942 1.14 christos 136, 216, 308, 76, 356);
11943 1.14 christos case 824: /* sizeof(prstatus_t) Intel 64-bit */
11944 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11945 1.14 christos 264, 360, 520, 224, 600);
11946 1.14 christos default:
11947 1.14 christos return true;
11948 1.14 christos }
11949 1.14 christos
11950 1.14 christos case SOLARIS_NT_PSINFO:
11951 1.14 christos case SOLARIS_NT_PRPSINFO:
11952 1.14 christos switch (note->descsz)
11953 1.14 christos {
11954 1.14 christos case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11955 1.14 christos return elfcore_grok_solaris_info(abfd, note, 84, 100);
11956 1.14 christos case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11957 1.14 christos return elfcore_grok_solaris_info(abfd, note, 120, 136);
11958 1.14 christos case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11959 1.14 christos return elfcore_grok_solaris_info(abfd, note, 88, 104);
11960 1.14 christos case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11961 1.14 christos return elfcore_grok_solaris_info(abfd, note, 136, 152);
11962 1.14 christos default:
11963 1.14 christos return true;
11964 1.14 christos }
11965 1.14 christos
11966 1.14 christos case SOLARIS_NT_LWPSTATUS:
11967 1.14 christos switch (note->descsz)
11968 1.14 christos {
11969 1.14 christos case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11970 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11971 1.14 christos 152, 344, 400, 496);
11972 1.14 christos case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11973 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11974 1.14 christos 304, 544, 544, 848);
11975 1.14 christos case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11976 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11977 1.14 christos 76, 344, 380, 420);
11978 1.14 christos case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11979 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11980 1.14 christos 224, 544, 528, 768);
11981 1.14 christos default:
11982 1.14 christos return true;
11983 1.14 christos }
11984 1.14 christos
11985 1.14 christos case SOLARIS_NT_LWPSINFO:
11986 1.14 christos /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11987 1.14 christos if (note->descsz == 128 || note->descsz == 152)
11988 1.14 christos elf_tdata (abfd)->core->lwpid =
11989 1.14 christos bfd_get_32 (abfd, note->descdata + 4);
11990 1.14 christos break;
11991 1.14 christos
11992 1.1 christos default:
11993 1.1 christos break;
11994 1.14 christos }
11995 1.14 christos
11996 1.14 christos return true;
11997 1.14 christos }
11998 1.14 christos
11999 1.14 christos /* For name starting with "CORE" this may be either a Solaris
12000 1.14 christos core file or a gdb-generated core file. Do Solaris-specific
12001 1.14 christos processing on selected note types first with
12002 1.14 christos elfcore_grok_solaris_note(), then process the note
12003 1.14 christos in elfcore_grok_note(). */
12004 1.14 christos
12005 1.14 christos static bool
12006 1.14 christos elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12007 1.14 christos {
12008 1.14 christos if (!elfcore_grok_solaris_note_impl (abfd, note))
12009 1.14 christos return false;
12010 1.1 christos
12011 1.1 christos return elfcore_grok_note (abfd, note);
12012 1.1 christos }
12013 1.1 christos
12014 1.1 christos static bool
12015 1.1 christos elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12016 1.1 christos {
12017 1.1 christos if (note->type == NT_OPENBSD_PROCINFO)
12018 1.1 christos return elfcore_grok_openbsd_procinfo (abfd, note);
12019 1.1 christos
12020 1.1 christos if (note->type == NT_OPENBSD_REGS)
12021 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note);
12022 1.1 christos
12023 1.1 christos if (note->type == NT_OPENBSD_FPREGS)
12024 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12025 1.12 christos
12026 1.1 christos if (note->type == NT_OPENBSD_XFPREGS)
12027 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12028 1.14 christos
12029 1.14 christos if (note->type == NT_OPENBSD_AUXV)
12030 1.14 christos return elfcore_make_auxv_note_section (abfd, note, 0);
12031 1.14 christos
12032 1.14 christos if (note->type == NT_OPENBSD_WCOOKIE)
12033 1.14 christos {
12034 1.14 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12035 1.14 christos SEC_HAS_CONTENTS);
12036 1.14 christos
12037 1.14 christos if (sect == NULL)
12038 1.14 christos return false;
12039 1.14 christos sect->size = note->descsz;
12040 1.14 christos sect->filepos = note->descpos;
12041 1.14 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12042 1.1 christos
12043 1.1 christos return true;
12044 1.14 christos }
12045 1.1 christos
12046 1.1 christos return true;
12047 1.1 christos }
12048 1.1 christos
12049 1.1 christos static bool
12050 1.1 christos elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12051 1.1 christos {
12052 1.1 christos void *ddata = note->descdata;
12053 1.1 christos char buf[100];
12054 1.11 christos char *name;
12055 1.14 christos asection *sect;
12056 1.11 christos short sig;
12057 1.1 christos unsigned flags;
12058 1.3 christos
12059 1.1 christos if (note->descsz < 16)
12060 1.1 christos return false;
12061 1.1 christos
12062 1.1 christos /* nto_procfs_status 'pid' field is at offset 0. */
12063 1.1 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12064 1.1 christos
12065 1.1 christos /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12066 1.1 christos *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12067 1.1 christos
12068 1.1 christos /* nto_procfs_status 'flags' field is at offset 8. */
12069 1.3 christos flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12070 1.3 christos
12071 1.1 christos /* nto_procfs_status 'what' field is at offset 14. */
12072 1.1 christos if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12073 1.1 christos {
12074 1.1 christos elf_tdata (abfd)->core->signal = sig;
12075 1.1 christos elf_tdata (abfd)->core->lwpid = *tid;
12076 1.1 christos }
12077 1.3 christos
12078 1.1 christos /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12079 1.1 christos do not come from signals so we make sure we set the current
12080 1.1 christos thread just in case. */
12081 1.1 christos if (flags & 0x00000080)
12082 1.1 christos elf_tdata (abfd)->core->lwpid = *tid;
12083 1.1 christos
12084 1.14 christos /* Make a ".qnx_core_status/%d" section. */
12085 1.1 christos sprintf (buf, ".qnx_core_status/%ld", *tid);
12086 1.1 christos
12087 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12088 1.1 christos if (name == NULL)
12089 1.14 christos return false;
12090 1.1 christos strcpy (name, buf);
12091 1.11 christos
12092 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12093 1.1 christos if (sect == NULL)
12094 1.1 christos return false;
12095 1.1 christos
12096 1.1 christos sect->size = note->descsz;
12097 1.1 christos sect->filepos = note->descpos;
12098 1.14 christos sect->alignment_power = 2;
12099 1.1 christos
12100 1.1 christos return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12101 1.1 christos }
12102 1.1 christos
12103 1.1 christos static bool
12104 1.1 christos elfcore_grok_nto_regs (bfd *abfd,
12105 1.1 christos Elf_Internal_Note *note,
12106 1.1 christos long tid,
12107 1.1 christos char *base)
12108 1.1 christos {
12109 1.1 christos char buf[100];
12110 1.1 christos char *name;
12111 1.1 christos asection *sect;
12112 1.1 christos
12113 1.14 christos /* Make a "(base)/%d" section. */
12114 1.1 christos sprintf (buf, "%s/%ld", base, tid);
12115 1.1 christos
12116 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12117 1.1 christos if (name == NULL)
12118 1.14 christos return false;
12119 1.1 christos strcpy (name, buf);
12120 1.11 christos
12121 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12122 1.1 christos if (sect == NULL)
12123 1.1 christos return false;
12124 1.1 christos
12125 1.3 christos sect->size = note->descsz;
12126 1.1 christos sect->filepos = note->descpos;
12127 1.1 christos sect->alignment_power = 2;
12128 1.14 christos
12129 1.1 christos /* This is the current thread. */
12130 1.1 christos if (elf_tdata (abfd)->core->lwpid == tid)
12131 1.14 christos return elfcore_maybe_make_sect (abfd, base, sect);
12132 1.1 christos
12133 1.1 christos return true;
12134 1.1 christos }
12135 1.1 christos
12136 1.1 christos static bool
12137 1.1 christos elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12138 1.1 christos {
12139 1.1 christos /* Every GREG section has a STATUS section before it. Store the
12140 1.1 christos tid from the previous call to pass down to the next gregs
12141 1.17 christos function. */
12142 1.1 christos static long tid = 1;
12143 1.17 christos
12144 1.1 christos switch (note->type)
12145 1.17 christos {
12146 1.1 christos case QNT_CORE_INFO:
12147 1.17 christos return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12148 1.1 christos case QNT_CORE_STATUS:
12149 1.1 christos return elfcore_grok_nto_status (abfd, note, &tid);
12150 1.14 christos case QNT_CORE_GREG:
12151 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12152 1.1 christos case QNT_CORE_FPREG:
12153 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12154 1.14 christos default:
12155 1.1 christos return true;
12156 1.1 christos }
12157 1.1 christos }
12158 1.1 christos
12159 1.1 christos static bool
12160 1.1 christos elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12161 1.1 christos {
12162 1.1 christos char *name;
12163 1.1 christos asection *sect;
12164 1.1 christos size_t len;
12165 1.14 christos
12166 1.1 christos /* Use note name as section name. */
12167 1.1 christos len = note->namesz;
12168 1.1 christos name = (char *) bfd_alloc (abfd, len);
12169 1.1 christos if (name == NULL)
12170 1.1 christos return false;
12171 1.14 christos memcpy (name, note->namedata, len);
12172 1.1 christos name[len - 1] = '\0';
12173 1.11 christos
12174 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12175 1.1 christos if (sect == NULL)
12176 1.1 christos return false;
12177 1.14 christos
12178 1.1 christos sect->size = note->descsz;
12179 1.1 christos sect->filepos = note->descpos;
12180 1.1 christos sect->alignment_power = 1;
12181 1.1 christos
12182 1.1 christos return true;
12183 1.1 christos }
12184 1.1 christos
12185 1.1 christos /* Function: elfcore_write_note
12186 1.1 christos
12187 1.1 christos Inputs:
12188 1.1 christos buffer to hold note, and current size of buffer
12189 1.1 christos name of note
12190 1.1 christos type of note
12191 1.1 christos data for note
12192 1.1 christos size of data for note
12193 1.1 christos
12194 1.1 christos Writes note to end of buffer. ELF64 notes are written exactly as
12195 1.1 christos for ELF32, despite the current (as of 2006) ELF gabi specifying
12196 1.1 christos that they ought to have 8-byte namesz and descsz field, and have
12197 1.1 christos 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12198 1.1 christos
12199 1.1 christos Return:
12200 1.1 christos Pointer to realloc'd buffer, *BUFSIZ updated. */
12201 1.1 christos
12202 1.1 christos char *
12203 1.1 christos elfcore_write_note (bfd *abfd,
12204 1.1 christos char *buf,
12205 1.1 christos int *bufsiz,
12206 1.1 christos const char *name,
12207 1.1 christos int type,
12208 1.1 christos const void *input,
12209 1.1 christos int size)
12210 1.1 christos {
12211 1.1 christos Elf_External_Note *xnp;
12212 1.1 christos size_t namesz;
12213 1.1 christos size_t newspace;
12214 1.1 christos char *dest;
12215 1.1 christos
12216 1.1 christos namesz = 0;
12217 1.1 christos if (name != NULL)
12218 1.1 christos namesz = strlen (name) + 1;
12219 1.1 christos
12220 1.1 christos newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12221 1.1 christos
12222 1.1 christos buf = (char *) realloc (buf, *bufsiz + newspace);
12223 1.1 christos if (buf == NULL)
12224 1.1 christos return buf;
12225 1.1 christos dest = buf + *bufsiz;
12226 1.1 christos *bufsiz += newspace;
12227 1.1 christos xnp = (Elf_External_Note *) dest;
12228 1.1 christos H_PUT_32 (abfd, namesz, xnp->namesz);
12229 1.1 christos H_PUT_32 (abfd, size, xnp->descsz);
12230 1.1 christos H_PUT_32 (abfd, type, xnp->type);
12231 1.1 christos dest = xnp->name;
12232 1.1 christos if (name != NULL)
12233 1.1 christos {
12234 1.1 christos memcpy (dest, name, namesz);
12235 1.1 christos dest += namesz;
12236 1.1 christos while (namesz & 3)
12237 1.1 christos {
12238 1.1 christos *dest++ = '\0';
12239 1.1 christos ++namesz;
12240 1.1 christos }
12241 1.1 christos }
12242 1.1 christos memcpy (dest, input, size);
12243 1.1 christos dest += size;
12244 1.1 christos while (size & 3)
12245 1.1 christos {
12246 1.1 christos *dest++ = '\0';
12247 1.11 christos ++size;
12248 1.11 christos }
12249 1.11 christos return buf;
12250 1.11 christos }
12251 1.11 christos
12252 1.11 christos /* gcc-8 warns (*) on all the strncpy calls in this function about
12253 1.11 christos possible string truncation. The "truncation" is not a bug. We
12254 1.11 christos have an external representation of structs with fields that are not
12255 1.11 christos necessarily NULL terminated and corresponding internal
12256 1.11 christos representation fields that are one larger so that they can always
12257 1.11 christos be NULL terminated.
12258 1.11 christos gcc versions between 4.2 and 4.6 do not allow pragma control of
12259 1.11 christos diagnostics inside functions, giving a hard error if you try to use
12260 1.11 christos the finer control available with later versions.
12261 1.11 christos gcc prior to 4.2 warns about diagnostic push and pop.
12262 1.11 christos gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12263 1.11 christos unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12264 1.1 christos (*) Depending on your system header files! */
12265 1.1 christos #if GCC_VERSION >= 8000
12266 1.1 christos # pragma GCC diagnostic push
12267 1.1 christos # pragma GCC diagnostic ignored "-Wstringop-truncation"
12268 1.1 christos #endif
12269 1.1 christos char *
12270 1.1 christos elfcore_write_prpsinfo (bfd *abfd,
12271 1.1 christos char *buf,
12272 1.1 christos int *bufsiz,
12273 1.1 christos const char *fname,
12274 1.1 christos const char *psargs)
12275 1.1 christos {
12276 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12277 1.1 christos
12278 1.1 christos if (bed->elf_backend_write_core_note != NULL)
12279 1.1 christos {
12280 1.1 christos char *ret;
12281 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12282 1.3 christos NT_PRPSINFO, fname, psargs);
12283 1.11 christos if (ret != NULL)
12284 1.1 christos return ret;
12285 1.1 christos }
12286 1.11 christos
12287 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12288 1.1 christos # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12289 1.11 christos if (bed->s->elfclass == ELFCLASS32)
12290 1.1 christos {
12291 1.1 christos # if defined (HAVE_PSINFO32_T)
12292 1.11 christos psinfo32_t data;
12293 1.1 christos int note_type = NT_PSINFO;
12294 1.1 christos # else
12295 1.1 christos prpsinfo32_t data;
12296 1.1 christos int note_type = NT_PRPSINFO;
12297 1.1 christos # endif
12298 1.3 christos
12299 1.1 christos memset (&data, 0, sizeof (data));
12300 1.1 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12301 1.11 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12302 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12303 1.11 christos "CORE", note_type, &data, sizeof (data));
12304 1.1 christos }
12305 1.1 christos else
12306 1.11 christos # endif
12307 1.1 christos {
12308 1.1 christos # if defined (HAVE_PSINFO_T)
12309 1.11 christos psinfo_t data;
12310 1.1 christos int note_type = NT_PSINFO;
12311 1.1 christos # else
12312 1.1 christos prpsinfo_t data;
12313 1.1 christos int note_type = NT_PRPSINFO;
12314 1.1 christos # endif
12315 1.3 christos
12316 1.1 christos memset (&data, 0, sizeof (data));
12317 1.3 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12318 1.3 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12319 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12320 1.3 christos "CORE", note_type, &data, sizeof (data));
12321 1.3 christos }
12322 1.11 christos #endif /* PSINFO_T or PRPSINFO_T */
12323 1.11 christos
12324 1.11 christos free (buf);
12325 1.3 christos return NULL;
12326 1.3 christos }
12327 1.3 christos #if GCC_VERSION >= 8000
12328 1.3 christos # pragma GCC diagnostic pop
12329 1.3 christos #endif
12330 1.3 christos
12331 1.11 christos char *
12332 1.11 christos elfcore_write_linux_prpsinfo32
12333 1.11 christos (bfd *abfd, char *buf, int *bufsiz,
12334 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo)
12335 1.11 christos {
12336 1.11 christos if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12337 1.11 christos {
12338 1.11 christos struct elf_external_linux_prpsinfo32_ugid16 data;
12339 1.11 christos
12340 1.11 christos swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12341 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12342 1.11 christos &data, sizeof (data));
12343 1.11 christos }
12344 1.11 christos else
12345 1.11 christos {
12346 1.11 christos struct elf_external_linux_prpsinfo32_ugid32 data;
12347 1.3 christos
12348 1.3 christos swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12349 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12350 1.3 christos &data, sizeof (data));
12351 1.3 christos }
12352 1.3 christos }
12353 1.3 christos
12354 1.11 christos char *
12355 1.11 christos elfcore_write_linux_prpsinfo64
12356 1.11 christos (bfd *abfd, char *buf, int *bufsiz,
12357 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo)
12358 1.11 christos {
12359 1.11 christos if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12360 1.11 christos {
12361 1.11 christos struct elf_external_linux_prpsinfo64_ugid16 data;
12362 1.11 christos
12363 1.11 christos swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12364 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12365 1.11 christos "CORE", NT_PRPSINFO, &data, sizeof (data));
12366 1.11 christos }
12367 1.11 christos else
12368 1.11 christos {
12369 1.11 christos struct elf_external_linux_prpsinfo64_ugid32 data;
12370 1.1 christos
12371 1.1 christos swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12372 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12373 1.1 christos "CORE", NT_PRPSINFO, &data, sizeof (data));
12374 1.1 christos }
12375 1.1 christos }
12376 1.1 christos
12377 1.1 christos char *
12378 1.1 christos elfcore_write_prstatus (bfd *abfd,
12379 1.1 christos char *buf,
12380 1.1 christos int *bufsiz,
12381 1.1 christos long pid,
12382 1.1 christos int cursig,
12383 1.1 christos const void *gregs)
12384 1.1 christos {
12385 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12386 1.1 christos
12387 1.1 christos if (bed->elf_backend_write_core_note != NULL)
12388 1.1 christos {
12389 1.1 christos char *ret;
12390 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12391 1.1 christos NT_PRSTATUS,
12392 1.3 christos pid, cursig, gregs);
12393 1.1 christos if (ret != NULL)
12394 1.1 christos return ret;
12395 1.1 christos }
12396 1.1 christos
12397 1.1 christos #if defined (HAVE_PRSTATUS_T)
12398 1.1 christos #if defined (HAVE_PRSTATUS32_T)
12399 1.1 christos if (bed->s->elfclass == ELFCLASS32)
12400 1.1 christos {
12401 1.1 christos prstatus32_t prstat;
12402 1.3 christos
12403 1.1 christos memset (&prstat, 0, sizeof (prstat));
12404 1.1 christos prstat.pr_pid = pid;
12405 1.1 christos prstat.pr_cursig = cursig;
12406 1.1 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12407 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12408 1.1 christos NT_PRSTATUS, &prstat, sizeof (prstat));
12409 1.1 christos }
12410 1.1 christos else
12411 1.1 christos #endif
12412 1.1 christos {
12413 1.1 christos prstatus_t prstat;
12414 1.3 christos
12415 1.1 christos memset (&prstat, 0, sizeof (prstat));
12416 1.1 christos prstat.pr_pid = pid;
12417 1.3 christos prstat.pr_cursig = cursig;
12418 1.3 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12419 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12420 1.3 christos NT_PRSTATUS, &prstat, sizeof (prstat));
12421 1.1 christos }
12422 1.1 christos #endif /* HAVE_PRSTATUS_T */
12423 1.1 christos
12424 1.1 christos free (buf);
12425 1.1 christos return NULL;
12426 1.1 christos }
12427 1.1 christos
12428 1.1 christos #if defined (HAVE_LWPSTATUS_T)
12429 1.1 christos char *
12430 1.1 christos elfcore_write_lwpstatus (bfd *abfd,
12431 1.1 christos char *buf,
12432 1.1 christos int *bufsiz,
12433 1.1 christos long pid,
12434 1.1 christos int cursig,
12435 1.1 christos const void *gregs)
12436 1.1 christos {
12437 1.1 christos lwpstatus_t lwpstat;
12438 1.1 christos const char *note_name = "CORE";
12439 1.5 christos
12440 1.1 christos memset (&lwpstat, 0, sizeof (lwpstat));
12441 1.1 christos lwpstat.pr_lwpid = pid >> 16;
12442 1.1 christos lwpstat.pr_cursig = cursig;
12443 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG)
12444 1.1 christos memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12445 1.1 christos #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12446 1.1 christos #if !defined(gregs)
12447 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12448 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12449 1.1 christos #else
12450 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12451 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12452 1.1 christos #endif
12453 1.1 christos #endif
12454 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, note_name,
12455 1.1 christos NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12456 1.1 christos }
12457 1.1 christos #endif /* HAVE_LWPSTATUS_T */
12458 1.1 christos
12459 1.1 christos #if defined (HAVE_PSTATUS_T)
12460 1.1 christos char *
12461 1.1 christos elfcore_write_pstatus (bfd *abfd,
12462 1.1 christos char *buf,
12463 1.1 christos int *bufsiz,
12464 1.1 christos long pid,
12465 1.1 christos int cursig ATTRIBUTE_UNUSED,
12466 1.1 christos const void *gregs ATTRIBUTE_UNUSED)
12467 1.1 christos {
12468 1.1 christos const char *note_name = "CORE";
12469 1.1 christos #if defined (HAVE_PSTATUS32_T)
12470 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12471 1.1 christos
12472 1.1 christos if (bed->s->elfclass == ELFCLASS32)
12473 1.1 christos {
12474 1.1 christos pstatus32_t pstat;
12475 1.1 christos
12476 1.1 christos memset (&pstat, 0, sizeof (pstat));
12477 1.1 christos pstat.pr_pid = pid & 0xffff;
12478 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12479 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat));
12480 1.1 christos return buf;
12481 1.1 christos }
12482 1.1 christos else
12483 1.1 christos #endif
12484 1.1 christos {
12485 1.1 christos pstatus_t pstat;
12486 1.1 christos
12487 1.1 christos memset (&pstat, 0, sizeof (pstat));
12488 1.1 christos pstat.pr_pid = pid & 0xffff;
12489 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12490 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat));
12491 1.1 christos return buf;
12492 1.1 christos }
12493 1.1 christos }
12494 1.1 christos #endif /* HAVE_PSTATUS_T */
12495 1.1 christos
12496 1.1 christos char *
12497 1.1 christos elfcore_write_prfpreg (bfd *abfd,
12498 1.1 christos char *buf,
12499 1.1 christos int *bufsiz,
12500 1.1 christos const void *fpregs,
12501 1.1 christos int size)
12502 1.1 christos {
12503 1.1 christos const char *note_name = "CORE";
12504 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12505 1.1 christos note_name, NT_FPREGSET, fpregs, size);
12506 1.1 christos }
12507 1.1 christos
12508 1.1 christos char *
12509 1.1 christos elfcore_write_prxfpreg (bfd *abfd,
12510 1.1 christos char *buf,
12511 1.1 christos int *bufsiz,
12512 1.1 christos const void *xfpregs,
12513 1.1 christos int size)
12514 1.1 christos {
12515 1.1 christos char *note_name = "LINUX";
12516 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12517 1.1 christos note_name, NT_PRXFPREG, xfpregs, size);
12518 1.1 christos }
12519 1.6 christos
12520 1.6 christos char *
12521 1.6 christos elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12522 1.6 christos const void *xfpregs, int size)
12523 1.6 christos {
12524 1.1 christos char *note_name;
12525 1.1 christos if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12526 1.1 christos note_name = "FreeBSD";
12527 1.1 christos else
12528 1.1 christos note_name = "LINUX";
12529 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12530 1.14 christos note_name, NT_X86_XSTATE, xfpregs, size);
12531 1.14 christos }
12532 1.14 christos
12533 1.14 christos char *
12534 1.14 christos elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12535 1.14 christos const void *regs, int size)
12536 1.14 christos {
12537 1.14 christos char *note_name = "FreeBSD";
12538 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12539 1.1 christos note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12540 1.1 christos }
12541 1.1 christos
12542 1.1 christos char *
12543 1.1 christos elfcore_write_ppc_vmx (bfd *abfd,
12544 1.1 christos char *buf,
12545 1.1 christos int *bufsiz,
12546 1.1 christos const void *ppc_vmx,
12547 1.1 christos int size)
12548 1.1 christos {
12549 1.1 christos char *note_name = "LINUX";
12550 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12551 1.11 christos note_name, NT_PPC_VMX, ppc_vmx, size);
12552 1.11 christos }
12553 1.11 christos
12554 1.11 christos char *
12555 1.11 christos elfcore_write_ppc_vsx (bfd *abfd,
12556 1.11 christos char *buf,
12557 1.11 christos int *bufsiz,
12558 1.11 christos const void *ppc_vsx,
12559 1.11 christos int size)
12560 1.11 christos {
12561 1.11 christos char *note_name = "LINUX";
12562 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12563 1.12 christos note_name, NT_PPC_VSX, ppc_vsx, size);
12564 1.12 christos }
12565 1.12 christos
12566 1.12 christos char *
12567 1.1 christos elfcore_write_ppc_tar (bfd *abfd,
12568 1.1 christos char *buf,
12569 1.1 christos int *bufsiz,
12570 1.12 christos const void *ppc_tar,
12571 1.11 christos int size)
12572 1.11 christos {
12573 1.11 christos char *note_name = "LINUX";
12574 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12575 1.12 christos note_name, NT_PPC_TAR, ppc_tar, size);
12576 1.12 christos }
12577 1.12 christos
12578 1.12 christos char *
12579 1.11 christos elfcore_write_ppc_ppr (bfd *abfd,
12580 1.11 christos char *buf,
12581 1.11 christos int *bufsiz,
12582 1.12 christos const void *ppc_ppr,
12583 1.11 christos int size)
12584 1.11 christos {
12585 1.11 christos char *note_name = "LINUX";
12586 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12587 1.12 christos note_name, NT_PPC_PPR, ppc_ppr, size);
12588 1.12 christos }
12589 1.12 christos
12590 1.12 christos char *
12591 1.11 christos elfcore_write_ppc_dscr (bfd *abfd,
12592 1.11 christos char *buf,
12593 1.11 christos int *bufsiz,
12594 1.12 christos const void *ppc_dscr,
12595 1.11 christos int size)
12596 1.11 christos {
12597 1.11 christos char *note_name = "LINUX";
12598 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12599 1.12 christos note_name, NT_PPC_DSCR, ppc_dscr, size);
12600 1.12 christos }
12601 1.12 christos
12602 1.12 christos char *
12603 1.11 christos elfcore_write_ppc_ebb (bfd *abfd,
12604 1.11 christos char *buf,
12605 1.11 christos int *bufsiz,
12606 1.12 christos const void *ppc_ebb,
12607 1.11 christos int size)
12608 1.11 christos {
12609 1.11 christos char *note_name = "LINUX";
12610 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12611 1.12 christos note_name, NT_PPC_EBB, ppc_ebb, size);
12612 1.12 christos }
12613 1.12 christos
12614 1.12 christos char *
12615 1.11 christos elfcore_write_ppc_pmu (bfd *abfd,
12616 1.11 christos char *buf,
12617 1.11 christos int *bufsiz,
12618 1.12 christos const void *ppc_pmu,
12619 1.11 christos int size)
12620 1.11 christos {
12621 1.11 christos char *note_name = "LINUX";
12622 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12623 1.12 christos note_name, NT_PPC_PMU, ppc_pmu, size);
12624 1.12 christos }
12625 1.12 christos
12626 1.12 christos char *
12627 1.11 christos elfcore_write_ppc_tm_cgpr (bfd *abfd,
12628 1.11 christos char *buf,
12629 1.11 christos int *bufsiz,
12630 1.12 christos const void *ppc_tm_cgpr,
12631 1.11 christos int size)
12632 1.11 christos {
12633 1.11 christos char *note_name = "LINUX";
12634 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12635 1.12 christos note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12636 1.12 christos }
12637 1.12 christos
12638 1.12 christos char *
12639 1.11 christos elfcore_write_ppc_tm_cfpr (bfd *abfd,
12640 1.11 christos char *buf,
12641 1.11 christos int *bufsiz,
12642 1.12 christos const void *ppc_tm_cfpr,
12643 1.11 christos int size)
12644 1.11 christos {
12645 1.11 christos char *note_name = "LINUX";
12646 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12647 1.12 christos note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12648 1.12 christos }
12649 1.12 christos
12650 1.12 christos char *
12651 1.11 christos elfcore_write_ppc_tm_cvmx (bfd *abfd,
12652 1.11 christos char *buf,
12653 1.11 christos int *bufsiz,
12654 1.12 christos const void *ppc_tm_cvmx,
12655 1.11 christos int size)
12656 1.11 christos {
12657 1.11 christos char *note_name = "LINUX";
12658 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12659 1.12 christos note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12660 1.12 christos }
12661 1.12 christos
12662 1.12 christos char *
12663 1.11 christos elfcore_write_ppc_tm_cvsx (bfd *abfd,
12664 1.11 christos char *buf,
12665 1.11 christos int *bufsiz,
12666 1.12 christos const void *ppc_tm_cvsx,
12667 1.11 christos int size)
12668 1.11 christos {
12669 1.11 christos char *note_name = "LINUX";
12670 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12671 1.12 christos note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12672 1.12 christos }
12673 1.12 christos
12674 1.12 christos char *
12675 1.11 christos elfcore_write_ppc_tm_spr (bfd *abfd,
12676 1.11 christos char *buf,
12677 1.11 christos int *bufsiz,
12678 1.12 christos const void *ppc_tm_spr,
12679 1.11 christos int size)
12680 1.11 christos {
12681 1.11 christos char *note_name = "LINUX";
12682 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12683 1.12 christos note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12684 1.12 christos }
12685 1.12 christos
12686 1.12 christos char *
12687 1.11 christos elfcore_write_ppc_tm_ctar (bfd *abfd,
12688 1.11 christos char *buf,
12689 1.11 christos int *bufsiz,
12690 1.12 christos const void *ppc_tm_ctar,
12691 1.11 christos int size)
12692 1.11 christos {
12693 1.11 christos char *note_name = "LINUX";
12694 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12695 1.12 christos note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12696 1.12 christos }
12697 1.12 christos
12698 1.12 christos char *
12699 1.11 christos elfcore_write_ppc_tm_cppr (bfd *abfd,
12700 1.11 christos char *buf,
12701 1.11 christos int *bufsiz,
12702 1.12 christos const void *ppc_tm_cppr,
12703 1.11 christos int size)
12704 1.11 christos {
12705 1.11 christos char *note_name = "LINUX";
12706 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12707 1.12 christos note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12708 1.12 christos }
12709 1.12 christos
12710 1.12 christos char *
12711 1.11 christos elfcore_write_ppc_tm_cdscr (bfd *abfd,
12712 1.11 christos char *buf,
12713 1.11 christos int *bufsiz,
12714 1.12 christos const void *ppc_tm_cdscr,
12715 1.1 christos int size)
12716 1.1 christos {
12717 1.1 christos char *note_name = "LINUX";
12718 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12719 1.1 christos note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12720 1.1 christos }
12721 1.1 christos
12722 1.1 christos static char *
12723 1.1 christos elfcore_write_s390_high_gprs (bfd *abfd,
12724 1.1 christos char *buf,
12725 1.1 christos int *bufsiz,
12726 1.11 christos const void *s390_high_gprs,
12727 1.1 christos int size)
12728 1.1 christos {
12729 1.1 christos char *note_name = "LINUX";
12730 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12731 1.1 christos note_name, NT_S390_HIGH_GPRS,
12732 1.11 christos s390_high_gprs, size);
12733 1.11 christos }
12734 1.11 christos
12735 1.11 christos char *
12736 1.1 christos elfcore_write_s390_timer (bfd *abfd,
12737 1.1 christos char *buf,
12738 1.1 christos int *bufsiz,
12739 1.11 christos const void *s390_timer,
12740 1.1 christos int size)
12741 1.1 christos {
12742 1.1 christos char *note_name = "LINUX";
12743 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12744 1.11 christos note_name, NT_S390_TIMER, s390_timer, size);
12745 1.11 christos }
12746 1.11 christos
12747 1.11 christos char *
12748 1.1 christos elfcore_write_s390_todcmp (bfd *abfd,
12749 1.1 christos char *buf,
12750 1.1 christos int *bufsiz,
12751 1.11 christos const void *s390_todcmp,
12752 1.1 christos int size)
12753 1.1 christos {
12754 1.1 christos char *note_name = "LINUX";
12755 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12756 1.11 christos note_name, NT_S390_TODCMP, s390_todcmp, size);
12757 1.11 christos }
12758 1.11 christos
12759 1.11 christos char *
12760 1.1 christos elfcore_write_s390_todpreg (bfd *abfd,
12761 1.1 christos char *buf,
12762 1.1 christos int *bufsiz,
12763 1.11 christos const void *s390_todpreg,
12764 1.1 christos int size)
12765 1.1 christos {
12766 1.1 christos char *note_name = "LINUX";
12767 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12768 1.11 christos note_name, NT_S390_TODPREG, s390_todpreg, size);
12769 1.11 christos }
12770 1.11 christos
12771 1.11 christos char *
12772 1.1 christos elfcore_write_s390_ctrs (bfd *abfd,
12773 1.1 christos char *buf,
12774 1.1 christos int *bufsiz,
12775 1.11 christos const void *s390_ctrs,
12776 1.1 christos int size)
12777 1.1 christos {
12778 1.1 christos char *note_name = "LINUX";
12779 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12780 1.11 christos note_name, NT_S390_CTRS, s390_ctrs, size);
12781 1.11 christos }
12782 1.11 christos
12783 1.11 christos char *
12784 1.1 christos elfcore_write_s390_prefix (bfd *abfd,
12785 1.1 christos char *buf,
12786 1.1 christos int *bufsiz,
12787 1.11 christos const void *s390_prefix,
12788 1.1 christos int size)
12789 1.1 christos {
12790 1.1 christos char *note_name = "LINUX";
12791 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12792 1.3 christos note_name, NT_S390_PREFIX, s390_prefix, size);
12793 1.3 christos }
12794 1.3 christos
12795 1.3 christos char *
12796 1.3 christos elfcore_write_s390_last_break (bfd *abfd,
12797 1.3 christos char *buf,
12798 1.3 christos int *bufsiz,
12799 1.11 christos const void *s390_last_break,
12800 1.3 christos int size)
12801 1.3 christos {
12802 1.3 christos char *note_name = "LINUX";
12803 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12804 1.3 christos note_name, NT_S390_LAST_BREAK,
12805 1.3 christos s390_last_break, size);
12806 1.3 christos }
12807 1.3 christos
12808 1.3 christos char *
12809 1.3 christos elfcore_write_s390_system_call (bfd *abfd,
12810 1.3 christos char *buf,
12811 1.3 christos int *bufsiz,
12812 1.11 christos const void *s390_system_call,
12813 1.3 christos int size)
12814 1.3 christos {
12815 1.3 christos char *note_name = "LINUX";
12816 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12817 1.3 christos note_name, NT_S390_SYSTEM_CALL,
12818 1.3 christos s390_system_call, size);
12819 1.3 christos }
12820 1.3 christos
12821 1.3 christos char *
12822 1.3 christos elfcore_write_s390_tdb (bfd *abfd,
12823 1.3 christos char *buf,
12824 1.3 christos int *bufsiz,
12825 1.11 christos const void *s390_tdb,
12826 1.3 christos int size)
12827 1.3 christos {
12828 1.3 christos char *note_name = "LINUX";
12829 1.6 christos return elfcore_write_note (abfd, buf, bufsiz,
12830 1.6 christos note_name, NT_S390_TDB, s390_tdb, size);
12831 1.6 christos }
12832 1.6 christos
12833 1.6 christos char *
12834 1.6 christos elfcore_write_s390_vxrs_low (bfd *abfd,
12835 1.6 christos char *buf,
12836 1.6 christos int *bufsiz,
12837 1.6 christos const void *s390_vxrs_low,
12838 1.6 christos int size)
12839 1.6 christos {
12840 1.6 christos char *note_name = "LINUX";
12841 1.6 christos return elfcore_write_note (abfd, buf, bufsiz,
12842 1.6 christos note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12843 1.6 christos }
12844 1.6 christos
12845 1.6 christos char *
12846 1.6 christos elfcore_write_s390_vxrs_high (bfd *abfd,
12847 1.6 christos char *buf,
12848 1.6 christos int *bufsiz,
12849 1.6 christos const void *s390_vxrs_high,
12850 1.6 christos int size)
12851 1.6 christos {
12852 1.6 christos char *note_name = "LINUX";
12853 1.6 christos return elfcore_write_note (abfd, buf, bufsiz,
12854 1.11 christos note_name, NT_S390_VXRS_HIGH,
12855 1.11 christos s390_vxrs_high, size);
12856 1.11 christos }
12857 1.11 christos
12858 1.11 christos char *
12859 1.11 christos elfcore_write_s390_gs_cb (bfd *abfd,
12860 1.11 christos char *buf,
12861 1.11 christos int *bufsiz,
12862 1.11 christos const void *s390_gs_cb,
12863 1.11 christos int size)
12864 1.11 christos {
12865 1.11 christos char *note_name = "LINUX";
12866 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12867 1.11 christos note_name, NT_S390_GS_CB,
12868 1.11 christos s390_gs_cb, size);
12869 1.11 christos }
12870 1.11 christos
12871 1.11 christos char *
12872 1.11 christos elfcore_write_s390_gs_bc (bfd *abfd,
12873 1.11 christos char *buf,
12874 1.11 christos int *bufsiz,
12875 1.11 christos const void *s390_gs_bc,
12876 1.11 christos int size)
12877 1.11 christos {
12878 1.11 christos char *note_name = "LINUX";
12879 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12880 1.3 christos note_name, NT_S390_GS_BC,
12881 1.3 christos s390_gs_bc, size);
12882 1.3 christos }
12883 1.3 christos
12884 1.3 christos char *
12885 1.3 christos elfcore_write_arm_vfp (bfd *abfd,
12886 1.3 christos char *buf,
12887 1.3 christos int *bufsiz,
12888 1.3 christos const void *arm_vfp,
12889 1.3 christos int size)
12890 1.3 christos {
12891 1.3 christos char *note_name = "LINUX";
12892 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12893 1.3 christos note_name, NT_ARM_VFP, arm_vfp, size);
12894 1.3 christos }
12895 1.3 christos
12896 1.3 christos char *
12897 1.3 christos elfcore_write_aarch_tls (bfd *abfd,
12898 1.3 christos char *buf,
12899 1.3 christos int *bufsiz,
12900 1.3 christos const void *aarch_tls,
12901 1.3 christos int size)
12902 1.3 christos {
12903 1.3 christos char *note_name = "LINUX";
12904 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12905 1.3 christos note_name, NT_ARM_TLS, aarch_tls, size);
12906 1.3 christos }
12907 1.3 christos
12908 1.3 christos char *
12909 1.3 christos elfcore_write_aarch_hw_break (bfd *abfd,
12910 1.3 christos char *buf,
12911 1.3 christos int *bufsiz,
12912 1.3 christos const void *aarch_hw_break,
12913 1.3 christos int size)
12914 1.3 christos {
12915 1.3 christos char *note_name = "LINUX";
12916 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12917 1.3 christos note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12918 1.3 christos }
12919 1.3 christos
12920 1.3 christos char *
12921 1.3 christos elfcore_write_aarch_hw_watch (bfd *abfd,
12922 1.3 christos char *buf,
12923 1.3 christos int *bufsiz,
12924 1.3 christos const void *aarch_hw_watch,
12925 1.3 christos int size)
12926 1.3 christos {
12927 1.3 christos char *note_name = "LINUX";
12928 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12929 1.11 christos note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12930 1.11 christos }
12931 1.11 christos
12932 1.11 christos char *
12933 1.11 christos elfcore_write_aarch_sve (bfd *abfd,
12934 1.11 christos char *buf,
12935 1.11 christos int *bufsiz,
12936 1.11 christos const void *aarch_sve,
12937 1.11 christos int size)
12938 1.11 christos {
12939 1.11 christos char *note_name = "LINUX";
12940 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12941 1.11 christos note_name, NT_ARM_SVE, aarch_sve, size);
12942 1.11 christos }
12943 1.11 christos
12944 1.11 christos char *
12945 1.11 christos elfcore_write_aarch_pauth (bfd *abfd,
12946 1.11 christos char *buf,
12947 1.11 christos int *bufsiz,
12948 1.11 christos const void *aarch_pauth,
12949 1.11 christos int size)
12950 1.11 christos {
12951 1.11 christos char *note_name = "LINUX";
12952 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12953 1.14 christos note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12954 1.14 christos }
12955 1.14 christos
12956 1.14 christos char *
12957 1.14 christos elfcore_write_aarch_mte (bfd *abfd,
12958 1.14 christos char *buf,
12959 1.14 christos int *bufsiz,
12960 1.14 christos const void *aarch_mte,
12961 1.14 christos int size)
12962 1.14 christos {
12963 1.14 christos char *note_name = "LINUX";
12964 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12965 1.14 christos note_name, NT_ARM_TAGGED_ADDR_CTRL,
12966 1.17 christos aarch_mte,
12967 1.17 christos size);
12968 1.17 christos }
12969 1.17 christos
12970 1.17 christos char *
12971 1.17 christos elfcore_write_aarch_ssve (bfd *abfd,
12972 1.17 christos char *buf,
12973 1.17 christos int *bufsiz,
12974 1.17 christos const void *aarch_ssve,
12975 1.17 christos int size)
12976 1.17 christos {
12977 1.17 christos char *note_name = "LINUX";
12978 1.17 christos return elfcore_write_note (abfd, buf, bufsiz,
12979 1.17 christos note_name, NT_ARM_SSVE,
12980 1.17 christos aarch_ssve,
12981 1.17 christos size);
12982 1.17 christos }
12983 1.17 christos
12984 1.17 christos char *
12985 1.17 christos elfcore_write_aarch_za (bfd *abfd,
12986 1.17 christos char *buf,
12987 1.17 christos int *bufsiz,
12988 1.17 christos const void *aarch_za,
12989 1.17 christos int size)
12990 1.17 christos {
12991 1.17 christos char *note_name = "LINUX";
12992 1.17 christos return elfcore_write_note (abfd, buf, bufsiz,
12993 1.17 christos note_name, NT_ARM_ZA,
12994 1.17 christos aarch_za,
12995 1.17 christos size);
12996 1.17 christos }
12997 1.17 christos
12998 1.17 christos /* Write the buffer of zt register values in aarch_zt (length SIZE) into
12999 1.17 christos the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13000 1.17 christos written into. Return a pointer to the new start of the note buffer, to
13001 1.17 christos replace BUF which may no longer be valid. */
13002 1.17 christos
13003 1.17 christos char *
13004 1.17 christos elfcore_write_aarch_zt (bfd *abfd,
13005 1.17 christos char *buf,
13006 1.17 christos int *bufsiz,
13007 1.17 christos const void *aarch_zt,
13008 1.17 christos int size)
13009 1.17 christos {
13010 1.17 christos char *note_name = "LINUX";
13011 1.17 christos return elfcore_write_note (abfd, buf, bufsiz,
13012 1.17 christos note_name, NT_ARM_ZT,
13013 1.12 christos aarch_zt,
13014 1.12 christos size);
13015 1.12 christos }
13016 1.12 christos
13017 1.12 christos char *
13018 1.12 christos elfcore_write_arc_v2 (bfd *abfd,
13019 1.12 christos char *buf,
13020 1.12 christos int *bufsiz,
13021 1.12 christos const void *arc_v2,
13022 1.12 christos int size)
13023 1.12 christos {
13024 1.12 christos char *note_name = "LINUX";
13025 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
13026 1.14 christos note_name, NT_ARC_V2, arc_v2, size);
13027 1.14 christos }
13028 1.14 christos
13029 1.14 christos char *
13030 1.14 christos elfcore_write_loongarch_cpucfg (bfd *abfd,
13031 1.14 christos char *buf,
13032 1.14 christos int *bufsiz,
13033 1.14 christos const void *loongarch_cpucfg,
13034 1.14 christos int size)
13035 1.14 christos {
13036 1.14 christos char *note_name = "LINUX";
13037 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
13038 1.14 christos note_name, NT_LARCH_CPUCFG,
13039 1.14 christos loongarch_cpucfg, size);
13040 1.14 christos }
13041 1.14 christos
13042 1.14 christos char *
13043 1.14 christos elfcore_write_loongarch_lbt (bfd *abfd,
13044 1.14 christos char *buf,
13045 1.14 christos int *bufsiz,
13046 1.14 christos const void *loongarch_lbt,
13047 1.14 christos int size)
13048 1.14 christos {
13049 1.14 christos char *note_name = "LINUX";
13050 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
13051 1.14 christos note_name, NT_LARCH_LBT, loongarch_lbt, size);
13052 1.14 christos }
13053 1.14 christos
13054 1.14 christos char *
13055 1.14 christos elfcore_write_loongarch_lsx (bfd *abfd,
13056 1.14 christos char *buf,
13057 1.14 christos int *bufsiz,
13058 1.14 christos const void *loongarch_lsx,
13059 1.14 christos int size)
13060 1.14 christos {
13061 1.14 christos char *note_name = "LINUX";
13062 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
13063 1.14 christos note_name, NT_LARCH_LSX, loongarch_lsx, size);
13064 1.14 christos }
13065 1.14 christos
13066 1.14 christos char *
13067 1.14 christos elfcore_write_loongarch_lasx (bfd *abfd,
13068 1.14 christos char *buf,
13069 1.14 christos int *bufsiz,
13070 1.14 christos const void *loongarch_lasx,
13071 1.14 christos int size)
13072 1.14 christos {
13073 1.14 christos char *note_name = "LINUX";
13074 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
13075 1.14 christos note_name, NT_LARCH_LASX, loongarch_lasx, size);
13076 1.14 christos }
13077 1.14 christos
13078 1.14 christos /* Write the buffer of csr values in CSRS (length SIZE) into the note
13079 1.14 christos buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13080 1.14 christos written into. Return a pointer to the new start of the note buffer, to
13081 1.14 christos replace BUF which may no longer be valid. */
13082 1.14 christos
13083 1.14 christos char *
13084 1.14 christos elfcore_write_riscv_csr (bfd *abfd,
13085 1.14 christos char *buf,
13086 1.14 christos int *bufsiz,
13087 1.14 christos const void *csrs,
13088 1.14 christos int size)
13089 1.14 christos {
13090 1.14 christos const char *note_name = "GDB";
13091 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
13092 1.14 christos note_name, NT_RISCV_CSR, csrs, size);
13093 1.14 christos }
13094 1.14 christos
13095 1.14 christos /* Write the target description (a string) pointed to by TDESC, length
13096 1.14 christos SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13097 1.14 christos note is being written into. Return a pointer to the new start of the
13098 1.14 christos note buffer, to replace BUF which may no longer be valid. */
13099 1.14 christos
13100 1.14 christos char *
13101 1.14 christos elfcore_write_gdb_tdesc (bfd *abfd,
13102 1.14 christos char *buf,
13103 1.14 christos int *bufsiz,
13104 1.14 christos const void *tdesc,
13105 1.14 christos int size)
13106 1.14 christos {
13107 1.14 christos const char *note_name = "GDB";
13108 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
13109 1.1 christos note_name, NT_GDB_TDESC, tdesc, size);
13110 1.1 christos }
13111 1.1 christos
13112 1.1 christos char *
13113 1.1 christos elfcore_write_register_note (bfd *abfd,
13114 1.1 christos char *buf,
13115 1.1 christos int *bufsiz,
13116 1.1 christos const char *section,
13117 1.1 christos const void *data,
13118 1.1 christos int size)
13119 1.1 christos {
13120 1.1 christos if (strcmp (section, ".reg2") == 0)
13121 1.14 christos return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13122 1.14 christos if (strcmp (section, ".reg-xfp") == 0)
13123 1.1 christos return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13124 1.1 christos if (strcmp (section, ".reg-xstate") == 0)
13125 1.1 christos return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13126 1.1 christos if (strcmp (section, ".reg-x86-segbases") == 0)
13127 1.11 christos return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13128 1.11 christos if (strcmp (section, ".reg-ppc-vmx") == 0)
13129 1.11 christos return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13130 1.11 christos if (strcmp (section, ".reg-ppc-vsx") == 0)
13131 1.11 christos return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13132 1.11 christos if (strcmp (section, ".reg-ppc-tar") == 0)
13133 1.11 christos return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13134 1.11 christos if (strcmp (section, ".reg-ppc-ppr") == 0)
13135 1.11 christos return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13136 1.11 christos if (strcmp (section, ".reg-ppc-dscr") == 0)
13137 1.11 christos return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13138 1.11 christos if (strcmp (section, ".reg-ppc-ebb") == 0)
13139 1.11 christos return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13140 1.11 christos if (strcmp (section, ".reg-ppc-pmu") == 0)
13141 1.11 christos return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13142 1.11 christos if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13143 1.11 christos return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13144 1.11 christos if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13145 1.11 christos return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13146 1.11 christos if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13147 1.11 christos return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13148 1.11 christos if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13149 1.11 christos return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13150 1.11 christos if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13151 1.11 christos return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13152 1.11 christos if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13153 1.1 christos return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13154 1.1 christos if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13155 1.1 christos return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13156 1.1 christos if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13157 1.1 christos return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13158 1.1 christos if (strcmp (section, ".reg-s390-high-gprs") == 0)
13159 1.1 christos return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13160 1.1 christos if (strcmp (section, ".reg-s390-timer") == 0)
13161 1.1 christos return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13162 1.1 christos if (strcmp (section, ".reg-s390-todcmp") == 0)
13163 1.1 christos return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13164 1.1 christos if (strcmp (section, ".reg-s390-todpreg") == 0)
13165 1.3 christos return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13166 1.3 christos if (strcmp (section, ".reg-s390-ctrs") == 0)
13167 1.3 christos return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13168 1.3 christos if (strcmp (section, ".reg-s390-prefix") == 0)
13169 1.3 christos return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13170 1.3 christos if (strcmp (section, ".reg-s390-last-break") == 0)
13171 1.6 christos return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13172 1.6 christos if (strcmp (section, ".reg-s390-system-call") == 0)
13173 1.6 christos return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13174 1.6 christos if (strcmp (section, ".reg-s390-tdb") == 0)
13175 1.11 christos return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13176 1.11 christos if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13177 1.11 christos return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13178 1.11 christos if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13179 1.3 christos return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13180 1.3 christos if (strcmp (section, ".reg-s390-gs-cb") == 0)
13181 1.3 christos return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13182 1.3 christos if (strcmp (section, ".reg-s390-gs-bc") == 0)
13183 1.3 christos return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13184 1.3 christos if (strcmp (section, ".reg-arm-vfp") == 0)
13185 1.3 christos return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13186 1.3 christos if (strcmp (section, ".reg-aarch-tls") == 0)
13187 1.11 christos return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13188 1.11 christos if (strcmp (section, ".reg-aarch-hw-break") == 0)
13189 1.11 christos return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13190 1.11 christos if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13191 1.14 christos return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13192 1.14 christos if (strcmp (section, ".reg-aarch-sve") == 0)
13193 1.17 christos return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13194 1.17 christos if (strcmp (section, ".reg-aarch-pauth") == 0)
13195 1.17 christos return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13196 1.17 christos if (strcmp (section, ".reg-aarch-mte") == 0)
13197 1.17 christos return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13198 1.17 christos if (strcmp (section, ".reg-aarch-ssve") == 0)
13199 1.12 christos return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13200 1.12 christos if (strcmp (section, ".reg-aarch-za") == 0)
13201 1.14 christos return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13202 1.14 christos if (strcmp (section, ".reg-aarch-zt") == 0)
13203 1.14 christos return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13204 1.14 christos if (strcmp (section, ".reg-arc-v2") == 0)
13205 1.14 christos return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13206 1.14 christos if (strcmp (section, ".gdb-tdesc") == 0)
13207 1.14 christos return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13208 1.14 christos if (strcmp (section, ".reg-riscv-csr") == 0)
13209 1.14 christos return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13210 1.14 christos if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13211 1.14 christos return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13212 1.14 christos if (strcmp (section, ".reg-loongarch-lbt") == 0)
13213 1.1 christos return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13214 1.1 christos if (strcmp (section, ".reg-loongarch-lsx") == 0)
13215 1.1 christos return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13216 1.14 christos if (strcmp (section, ".reg-loongarch-lasx") == 0)
13217 1.14 christos return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13218 1.14 christos return NULL;
13219 1.14 christos }
13220 1.14 christos
13221 1.14 christos char *
13222 1.14 christos elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13223 1.14 christos const void *buf, int bufsiz)
13224 1.14 christos {
13225 1.11 christos return elfcore_write_note (obfd, note_data, note_size,
13226 1.11 christos "CORE", NT_FILE, buf, bufsiz);
13227 1.1 christos }
13228 1.1 christos
13229 1.1 christos static bool
13230 1.11 christos elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13231 1.11 christos size_t align)
13232 1.11 christos {
13233 1.11 christos char *p;
13234 1.11 christos
13235 1.11 christos /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13236 1.11 christos gABI specifies that PT_NOTE alignment should be aligned to 4
13237 1.14 christos bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13238 1.11 christos align is less than 4, we use 4 byte alignment. */
13239 1.1 christos if (align < 4)
13240 1.1 christos align = 4;
13241 1.1 christos if (align != 4 && align != 8)
13242 1.1 christos return false;
13243 1.1 christos
13244 1.1 christos p = buf;
13245 1.1 christos while (p < buf + size)
13246 1.14 christos {
13247 1.1 christos Elf_External_Note *xnp = (Elf_External_Note *) p;
13248 1.1 christos Elf_Internal_Note in;
13249 1.1 christos
13250 1.1 christos if (offsetof (Elf_External_Note, name) > buf - p + size)
13251 1.1 christos return false;
13252 1.1 christos
13253 1.14 christos in.type = H_GET_32 (abfd, xnp->type);
13254 1.1 christos
13255 1.1 christos in.namesz = H_GET_32 (abfd, xnp->namesz);
13256 1.11 christos in.namedata = xnp->name;
13257 1.1 christos if (in.namesz > buf - in.namedata + size)
13258 1.1 christos return false;
13259 1.1 christos
13260 1.1 christos in.descsz = H_GET_32 (abfd, xnp->descsz);
13261 1.14 christos in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13262 1.1 christos in.descpos = offset + (in.descdata - buf);
13263 1.1 christos if (in.descsz != 0
13264 1.11 christos && (in.descdata >= buf + size
13265 1.1 christos || in.descsz > buf - in.descdata + size))
13266 1.14 christos return false;
13267 1.1 christos
13268 1.1 christos switch (bfd_get_format (abfd))
13269 1.5 christos {
13270 1.5 christos default:
13271 1.5 christos return true;
13272 1.1 christos
13273 1.5 christos case bfd_core:
13274 1.5 christos {
13275 1.14 christos #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13276 1.1 christos struct
13277 1.5 christos {
13278 1.1 christos const char * string;
13279 1.5 christos size_t len;
13280 1.8 christos bool (*func) (bfd *, Elf_Internal_Note *);
13281 1.5 christos }
13282 1.14 christos grokers[] =
13283 1.5 christos {
13284 1.12 christos GROKER_ELEMENT ("", elfcore_grok_note),
13285 1.14 christos GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13286 1.14 christos GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13287 1.5 christos GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13288 1.5 christos GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13289 1.5 christos GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13290 1.5 christos GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13291 1.5 christos GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13292 1.5 christos };
13293 1.5 christos #undef GROKER_ELEMENT
13294 1.5 christos int i;
13295 1.5 christos
13296 1.5 christos for (i = ARRAY_SIZE (grokers); i--;)
13297 1.5 christos {
13298 1.14 christos if (in.namesz >= grokers[i].len
13299 1.5 christos && strncmp (in.namedata, grokers[i].string,
13300 1.5 christos grokers[i].len) == 0)
13301 1.5 christos {
13302 1.5 christos if (! grokers[i].func (abfd, & in))
13303 1.5 christos return false;
13304 1.1 christos break;
13305 1.1 christos }
13306 1.1 christos }
13307 1.1 christos break;
13308 1.1 christos }
13309 1.14 christos
13310 1.1 christos case bfd_object:
13311 1.3 christos if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13312 1.3 christos {
13313 1.3 christos if (! elfobj_grok_gnu_note (abfd, &in))
13314 1.3 christos return false;
13315 1.14 christos }
13316 1.3 christos else if (in.namesz == sizeof "stapsdt"
13317 1.1 christos && strcmp (in.namedata, "stapsdt") == 0)
13318 1.1 christos {
13319 1.1 christos if (! elfobj_grok_stapsdt_note (abfd, &in))
13320 1.11 christos return false;
13321 1.1 christos }
13322 1.1 christos break;
13323 1.14 christos }
13324 1.1 christos
13325 1.1 christos p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13326 1.14 christos }
13327 1.11 christos
13328 1.11 christos return true;
13329 1.1 christos }
13330 1.1 christos
13331 1.1 christos bool
13332 1.11 christos elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13333 1.14 christos size_t align)
13334 1.1 christos {
13335 1.1 christos char *buf;
13336 1.14 christos
13337 1.1 christos if (size == 0 || (size + 1) == 0)
13338 1.12 christos return true;
13339 1.1 christos
13340 1.14 christos if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13341 1.1 christos return false;
13342 1.5 christos
13343 1.5 christos buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13344 1.5 christos if (buf == NULL)
13345 1.5 christos return false;
13346 1.12 christos
13347 1.1 christos /* PR 17512: file: ec08f814
13348 1.1 christos 0-termintate the buffer so that string searches will not overflow. */
13349 1.14 christos buf[size] = 0;
13350 1.1 christos
13351 1.1 christos if (!elf_parse_notes (abfd, buf, size, offset, align))
13352 1.1 christos {
13353 1.14 christos free (buf);
13354 1.1 christos return false;
13355 1.1 christos }
13356 1.1 christos
13357 1.1 christos free (buf);
13358 1.1 christos return true;
13359 1.1 christos }
13360 1.1 christos
13361 1.1 christos /* Providing external access to the ELF program header table. */
13363 1.1 christos
13364 1.1 christos /* Return an upper bound on the number of bytes required to store a
13365 1.1 christos copy of ABFD's program header table entries. Return -1 if an error
13366 1.1 christos occurs; bfd_get_error will return an appropriate code. */
13367 1.1 christos
13368 1.1 christos long
13369 1.1 christos bfd_get_elf_phdr_upper_bound (bfd *abfd)
13370 1.1 christos {
13371 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour)
13372 1.1 christos {
13373 1.1 christos bfd_set_error (bfd_error_wrong_format);
13374 1.1 christos return -1;
13375 1.1 christos }
13376 1.1 christos
13377 1.1 christos return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13378 1.1 christos }
13379 1.1 christos
13380 1.1 christos /* Copy ABFD's program header table entries to *PHDRS. The entries
13381 1.1 christos will be stored as an array of Elf_Internal_Phdr structures, as
13382 1.1 christos defined in include/elf/internal.h. To find out how large the
13383 1.1 christos buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13384 1.1 christos
13385 1.1 christos Return the number of program header table entries read, or -1 if an
13386 1.1 christos error occurs; bfd_get_error will return an appropriate code. */
13387 1.1 christos
13388 1.1 christos int
13389 1.1 christos bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13390 1.1 christos {
13391 1.1 christos int num_phdrs;
13392 1.1 christos
13393 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour)
13394 1.11 christos {
13395 1.11 christos bfd_set_error (bfd_error_wrong_format);
13396 1.11 christos return -1;
13397 1.1 christos }
13398 1.1 christos
13399 1.1 christos num_phdrs = elf_elfheader (abfd)->e_phnum;
13400 1.1 christos if (num_phdrs != 0)
13401 1.1 christos memcpy (phdrs, elf_tdata (abfd)->phdr,
13402 1.4 christos num_phdrs * sizeof (Elf_Internal_Phdr));
13403 1.4 christos
13404 1.4 christos return num_phdrs;
13405 1.1 christos }
13406 1.1 christos
13407 1.1 christos enum elf_reloc_type_class
13408 1.1 christos _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13409 1.1 christos const asection *rel_sec ATTRIBUTE_UNUSED,
13410 1.1 christos const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13411 1.1 christos {
13412 1.1 christos return reloc_class_normal;
13413 1.1 christos }
13414 1.1 christos
13415 1.1 christos /* For RELA architectures, return the relocation value for a
13416 1.1 christos relocation against a local symbol. */
13417 1.1 christos
13418 1.1 christos bfd_vma
13419 1.1 christos _bfd_elf_rela_local_sym (bfd *abfd,
13420 1.1 christos Elf_Internal_Sym *sym,
13421 1.1 christos asection **psec,
13422 1.1 christos Elf_Internal_Rela *rel)
13423 1.1 christos {
13424 1.1 christos asection *sec = *psec;
13425 1.1 christos bfd_vma relocation;
13426 1.3 christos
13427 1.1 christos relocation = (sec->output_section->vma
13428 1.1 christos + sec->output_offset
13429 1.1 christos + sym->st_value);
13430 1.1 christos if ((sec->flags & SEC_MERGE)
13431 1.1 christos && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13432 1.1 christos && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13433 1.1 christos {
13434 1.1 christos rel->r_addend =
13435 1.1 christos _bfd_merged_section_offset (abfd, psec,
13436 1.1 christos elf_section_data (sec)->sec_info,
13437 1.1 christos sym->st_value + rel->r_addend);
13438 1.1 christos if (sec != *psec)
13439 1.1 christos {
13440 1.1 christos /* If we have changed the section, and our original section is
13441 1.1 christos marked with SEC_EXCLUDE, it means that the original
13442 1.1 christos SEC_MERGE section has been completely subsumed in some
13443 1.1 christos other SEC_MERGE section. In this case, we need to leave
13444 1.1 christos some info around for --emit-relocs. */
13445 1.1 christos if ((sec->flags & SEC_EXCLUDE) != 0)
13446 1.1 christos sec->kept_section = *psec;
13447 1.1 christos sec = *psec;
13448 1.1 christos }
13449 1.1 christos rel->r_addend -= relocation;
13450 1.1 christos rel->r_addend += sec->output_section->vma + sec->output_offset;
13451 1.1 christos }
13452 1.1 christos return relocation;
13453 1.1 christos }
13454 1.1 christos
13455 1.1 christos bfd_vma
13456 1.1 christos _bfd_elf_rel_local_sym (bfd *abfd,
13457 1.3 christos Elf_Internal_Sym *sym,
13458 1.1 christos asection **psec,
13459 1.1 christos bfd_vma addend)
13460 1.1 christos {
13461 1.1 christos asection *sec = *psec;
13462 1.1 christos
13463 1.1 christos if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13464 1.1 christos return sym->st_value + addend;
13465 1.8 christos
13466 1.8 christos return _bfd_merged_section_offset (abfd, psec,
13467 1.8 christos elf_section_data (sec)->sec_info,
13468 1.8 christos sym->st_value + addend);
13469 1.8 christos }
13470 1.8 christos
13471 1.1 christos /* Adjust an address within a section. Given OFFSET within SEC, return
13472 1.1 christos the new offset within the section, based upon changes made to the
13473 1.1 christos section. Returns -1 if the offset is now invalid.
13474 1.1 christos The offset (in abnd out) is in target sized bytes, however big a
13475 1.1 christos byte may be. */
13476 1.1 christos
13477 1.1 christos bfd_vma
13478 1.1 christos _bfd_elf_section_offset (bfd *abfd,
13479 1.3 christos struct bfd_link_info *info,
13480 1.1 christos asection *sec,
13481 1.1 christos bfd_vma offset)
13482 1.3 christos {
13483 1.1 christos switch (sec->sec_info_type)
13484 1.8 christos {
13485 1.1 christos case SEC_INFO_TYPE_STABS:
13486 1.3 christos return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13487 1.3 christos offset);
13488 1.8 christos case SEC_INFO_TYPE_EH_FRAME:
13489 1.3 christos return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13490 1.3 christos
13491 1.8 christos default:
13492 1.8 christos if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13493 1.8 christos {
13494 1.12 christos /* Reverse the offset. */
13495 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13496 1.3 christos bfd_size_type address_size = bed->s->arch_size / 8;
13497 1.1 christos
13498 1.1 christos /* address_size and sec->size are in octets. Convert
13499 1.1 christos to bytes before subtracting the original offset. */
13500 1.1 christos offset = ((sec->size - address_size)
13501 1.1 christos / bfd_octets_per_byte (abfd, sec) - offset);
13502 1.1 christos }
13503 1.1 christos return offset;
13504 1.1 christos }
13505 1.1 christos }
13506 1.1 christos
13507 1.1 christos long
13509 1.1 christos _bfd_elf_get_synthetic_symtab (bfd *abfd,
13510 1.1 christos long symcount ATTRIBUTE_UNUSED,
13511 1.1 christos asymbol **syms ATTRIBUTE_UNUSED,
13512 1.1 christos long dynsymcount,
13513 1.14 christos asymbol **dynsyms,
13514 1.1 christos asymbol **ret)
13515 1.1 christos {
13516 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13517 1.1 christos asection *relplt;
13518 1.1 christos asymbol *s;
13519 1.1 christos const char *relplt_name;
13520 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13521 1.1 christos arelent *p;
13522 1.1 christos long count, i, n;
13523 1.1 christos size_t size;
13524 1.1 christos Elf_Internal_Shdr *hdr;
13525 1.1 christos char *names;
13526 1.1 christos asection *plt;
13527 1.1 christos
13528 1.1 christos *ret = NULL;
13529 1.1 christos
13530 1.1 christos if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13531 1.1 christos return 0;
13532 1.1 christos
13533 1.1 christos if (dynsymcount <= 0)
13534 1.1 christos return 0;
13535 1.1 christos
13536 1.1 christos if (!bed->plt_sym_val)
13537 1.1 christos return 0;
13538 1.1 christos
13539 1.1 christos relplt_name = bed->relplt_name;
13540 1.1 christos if (relplt_name == NULL)
13541 1.1 christos relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13542 1.1 christos relplt = bfd_get_section_by_name (abfd, relplt_name);
13543 1.1 christos if (relplt == NULL)
13544 1.1 christos return 0;
13545 1.1 christos
13546 1.1 christos hdr = &elf_section_data (relplt)->this_hdr;
13547 1.1 christos if (hdr->sh_link != elf_dynsymtab (abfd)
13548 1.1 christos || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13549 1.14 christos return 0;
13550 1.1 christos
13551 1.1 christos plt = bfd_get_section_by_name (abfd, ".plt");
13552 1.17 christos if (plt == NULL)
13553 1.1 christos return 0;
13554 1.1 christos
13555 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13556 1.1 christos if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13557 1.1 christos return -1;
13558 1.1 christos
13559 1.1 christos count = NUM_SHDR_ENTRIES (hdr);
13560 1.1 christos size = count * sizeof (asymbol);
13561 1.1 christos p = relplt->relocation;
13562 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13563 1.1 christos {
13564 1.1 christos size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13565 1.1 christos if (p->addend != 0)
13566 1.1 christos {
13567 1.1 christos #ifdef BFD64
13568 1.1 christos size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13569 1.1 christos #else
13570 1.1 christos size += sizeof ("+0x") - 1 + 8;
13571 1.1 christos #endif
13572 1.1 christos }
13573 1.1 christos }
13574 1.1 christos
13575 1.1 christos s = *ret = (asymbol *) bfd_malloc (size);
13576 1.1 christos if (s == NULL)
13577 1.1 christos return -1;
13578 1.1 christos
13579 1.1 christos names = (char *) (s + count);
13580 1.1 christos p = relplt->relocation;
13581 1.1 christos n = 0;
13582 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13583 1.1 christos {
13584 1.1 christos size_t len;
13585 1.1 christos bfd_vma addr;
13586 1.1 christos
13587 1.1 christos addr = bed->plt_sym_val (i, plt, p);
13588 1.1 christos if (addr == (bfd_vma) -1)
13589 1.1 christos continue;
13590 1.1 christos
13591 1.1 christos *s = **p->sym_ptr_ptr;
13592 1.1 christos /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13593 1.1 christos we are defining a symbol, ensure one of them is set. */
13594 1.1 christos if ((s->flags & BSF_LOCAL) == 0)
13595 1.1 christos s->flags |= BSF_GLOBAL;
13596 1.1 christos s->flags |= BSF_SYNTHETIC;
13597 1.1 christos s->section = plt;
13598 1.1 christos s->value = addr - plt->vma;
13599 1.1 christos s->name = names;
13600 1.3 christos s->udata.p = NULL;
13601 1.1 christos len = strlen ((*p->sym_ptr_ptr)->name);
13602 1.1 christos memcpy (names, (*p->sym_ptr_ptr)->name, len);
13603 1.1 christos names += len;
13604 1.1 christos if (p->addend != 0)
13605 1.1 christos {
13606 1.1 christos char buf[30], *a;
13607 1.1 christos
13608 1.1 christos memcpy (names, "+0x", sizeof ("+0x") - 1);
13609 1.1 christos names += sizeof ("+0x") - 1;
13610 1.1 christos bfd_sprintf_vma (abfd, buf, p->addend);
13611 1.1 christos for (a = buf; *a == '0'; ++a)
13612 1.1 christos ;
13613 1.1 christos len = strlen (a);
13614 1.1 christos memcpy (names, a, len);
13615 1.1 christos names += len;
13616 1.1 christos }
13617 1.1 christos memcpy (names, "@plt", sizeof ("@plt"));
13618 1.9 christos names += sizeof ("@plt");
13619 1.9 christos ++s, ++n;
13620 1.11 christos }
13621 1.11 christos
13622 1.11 christos return n;
13623 1.1 christos }
13624 1.11 christos
13625 1.9 christos /* It is only used by x86-64 so far.
13626 1.1 christos ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13627 1.14 christos but current usage would allow all of _bfd_std_section to be zero. */
13628 1.12 christos static const asymbol lcomm_sym
13629 1.1 christos = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13630 1.12 christos asection _bfd_elf_large_com_section
13631 1.1 christos = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13632 1.1 christos "LARGE_COMMON", 0, SEC_IS_COMMON);
13633 1.1 christos
13634 1.12 christos bool
13635 1.12 christos _bfd_elf_final_write_processing (bfd *abfd)
13636 1.1 christos {
13637 1.12 christos Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13638 1.14 christos
13639 1.14 christos i_ehdrp = elf_elfheader (abfd);
13640 1.12 christos
13641 1.12 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13642 1.12 christos i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13643 1.12 christos
13644 1.12 christos /* Set the osabi field to ELFOSABI_GNU if the binary contains
13645 1.12 christos SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13646 1.12 christos or STB_GNU_UNIQUE binding. */
13647 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi != 0)
13648 1.14 christos {
13649 1.14 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13650 1.12 christos i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13651 1.14 christos else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13652 1.14 christos && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13653 1.12 christos {
13654 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13655 1.14 christos _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13656 1.14 christos "and FreeBSD targets"));
13657 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13658 1.14 christos _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13659 1.12 christos "only by GNU and FreeBSD targets"));
13660 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13661 1.12 christos _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13662 1.12 christos "only by GNU and FreeBSD targets"));
13663 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13664 1.1 christos _bfd_error_handler (_("GNU_RETAIN section is supported "
13665 1.1 christos "only by GNU and FreeBSD targets"));
13666 1.1 christos bfd_set_error (bfd_error_sorry);
13667 1.1 christos return false;
13668 1.1 christos }
13669 1.1 christos }
13670 1.1 christos return true;
13671 1.14 christos }
13672 1.1 christos
13673 1.1 christos
13674 1.1 christos /* Return TRUE for ELF symbol types that represent functions.
13675 1.1 christos This is the default version of this function, which is sufficient for
13676 1.1 christos most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13677 1.3 christos
13678 1.3 christos bool
13679 1.3 christos _bfd_elf_is_function_type (unsigned int type)
13680 1.3 christos {
13681 1.3 christos return (type == STT_FUNC
13682 1.3 christos || type == STT_GNU_IFUNC);
13683 1.3 christos }
13684 1.3 christos
13685 1.3 christos /* If the ELF symbol SYM might be a function in SEC, return the
13686 1.3 christos function size and set *CODE_OFF to the function's entry point,
13687 1.14 christos otherwise return zero. */
13688 1.3 christos
13689 1.3 christos bfd_size_type
13690 1.3 christos _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13691 1.3 christos bfd_vma *code_off)
13692 1.3 christos {
13693 1.3 christos bfd_size_type size;
13694 1.14 christos elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13695 1.14 christos
13696 1.14 christos if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13697 1.14 christos | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13698 1.14 christos || sym->section != sec)
13699 1.14 christos return 0;
13700 1.14 christos
13701 1.14 christos size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13702 1.14 christos
13703 1.14 christos /* In theory we should check that the symbol's type satisfies
13704 1.14 christos _bfd_elf_is_function_type(), but there are some function-like
13705 1.14 christos symbols which would fail this test. (eg _start). Instead
13706 1.14 christos we check for hidden, local, notype symbols with zero size.
13707 1.14 christos This type of symbol is generated by the annobin plugin for gcc
13708 1.3 christos and clang, and should not be considered to be a function symbol. */
13709 1.14 christos if (size == 0
13710 1.14 christos && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13711 1.3 christos && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13712 1.12 christos && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13713 1.12 christos return 0;
13714 1.12 christos
13715 1.12 christos *code_off = sym->value;
13716 1.12 christos /* Do not return 0 for the function's size. */
13717 1.12 christos return size ? size : 1;
13718 1.12 christos }
13719 1.12 christos
13720 1.12 christos /* Set to non-zero to enable some debug messages. */
13721 1.12 christos #define DEBUG_SECONDARY_RELOCS 0
13722 1.14 christos
13723 1.12 christos /* An internal-to-the-bfd-library only section type
13724 1.12 christos used to indicate a cached secondary reloc section. */
13725 1.12 christos #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13726 1.12 christos
13727 1.12 christos /* Create a BFD section to hold a secondary reloc section. */
13728 1.12 christos
13729 1.12 christos bool
13730 1.14 christos _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13731 1.12 christos Elf_Internal_Shdr *hdr,
13732 1.12 christos const char * name,
13733 1.12 christos unsigned int shindex)
13734 1.12 christos {
13735 1.12 christos /* We only support RELA secondary relocs. */
13736 1.12 christos if (hdr->sh_type != SHT_RELA)
13737 1.12 christos return false;
13738 1.12 christos
13739 1.12 christos #if DEBUG_SECONDARY_RELOCS
13740 1.12 christos fprintf (stderr, "secondary reloc section %s encountered\n", name);
13741 1.14 christos #endif
13742 1.14 christos hdr->sh_type = SHT_SECONDARY_RELOC;
13743 1.14 christos return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13744 1.14 christos }
13745 1.14 christos
13746 1.12 christos /* Read in any secondary relocs associated with SEC. */
13747 1.12 christos
13748 1.12 christos bool
13749 1.14 christos _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13750 1.12 christos asection * sec,
13751 1.14 christos asymbol ** symbols,
13752 1.12 christos bool dynamic)
13753 1.12 christos {
13754 1.12 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13755 1.12 christos asection * relsec;
13756 1.12 christos bool result = true;
13757 1.12 christos bfd_vma (*r_sym) (bfd_vma);
13758 1.12 christos ufile_ptr filesize;
13759 1.14 christos
13760 1.14 christos #if BFD_DEFAULT_TARGET_SIZE > 32
13761 1.14 christos if (bfd_arch_bits_per_address (abfd) != 32)
13762 1.14 christos r_sym = elf64_r_sym;
13763 1.12 christos else
13764 1.12 christos #endif
13765 1.14 christos r_sym = elf32_r_sym;
13766 1.12 christos
13767 1.12 christos if (!elf_section_data (sec)->has_secondary_relocs)
13768 1.12 christos return true;
13769 1.12 christos
13770 1.12 christos /* Discover if there are any secondary reloc sections
13771 1.12 christos associated with SEC. */
13772 1.12 christos filesize = bfd_get_file_size (abfd);
13773 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13774 1.12 christos {
13775 1.12 christos Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13776 1.12 christos
13777 1.12 christos if (hdr->sh_type == SHT_SECONDARY_RELOC
13778 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13779 1.14 christos && (hdr->sh_entsize == ebd->s->sizeof_rel
13780 1.12 christos || hdr->sh_entsize == ebd->s->sizeof_rela))
13781 1.12 christos {
13782 1.14 christos bfd_byte * native_relocs;
13783 1.12 christos bfd_byte * native_reloc;
13784 1.12 christos arelent * internal_relocs;
13785 1.12 christos arelent * internal_reloc;
13786 1.14 christos size_t i;
13787 1.12 christos unsigned int entsize;
13788 1.12 christos unsigned int symcount;
13789 1.12 christos bfd_size_type reloc_count;
13790 1.12 christos size_t amt;
13791 1.12 christos
13792 1.12 christos if (ebd->elf_info_to_howto == NULL)
13793 1.12 christos return false;
13794 1.14 christos
13795 1.14 christos #if DEBUG_SECONDARY_RELOCS
13796 1.14 christos fprintf (stderr, "read secondary relocs for %s from %s\n",
13797 1.14 christos sec->name, relsec->name);
13798 1.14 christos #endif
13799 1.14 christos entsize = hdr->sh_entsize;
13800 1.14 christos
13801 1.14 christos if (filesize != 0
13802 1.14 christos && ((ufile_ptr) hdr->sh_offset > filesize
13803 1.12 christos || hdr->sh_size > filesize - hdr->sh_offset))
13804 1.12 christos {
13805 1.12 christos bfd_set_error (bfd_error_file_truncated);
13806 1.14 christos result = false;
13807 1.12 christos continue;
13808 1.12 christos }
13809 1.12 christos
13810 1.12 christos native_relocs = bfd_malloc (hdr->sh_size);
13811 1.12 christos if (native_relocs == NULL)
13812 1.12 christos {
13813 1.12 christos result = false;
13814 1.12 christos continue;
13815 1.14 christos }
13816 1.12 christos
13817 1.12 christos reloc_count = NUM_SHDR_ENTRIES (hdr);
13818 1.12 christos if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13819 1.12 christos {
13820 1.12 christos free (native_relocs);
13821 1.12 christos bfd_set_error (bfd_error_file_too_big);
13822 1.12 christos result = false;
13823 1.14 christos continue;
13824 1.12 christos }
13825 1.12 christos
13826 1.12 christos internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13827 1.12 christos if (internal_relocs == NULL)
13828 1.17 christos {
13829 1.12 christos free (native_relocs);
13830 1.12 christos result = false;
13831 1.12 christos continue;
13832 1.12 christos }
13833 1.14 christos
13834 1.12 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13835 1.12 christos || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13836 1.12 christos {
13837 1.14 christos free (native_relocs);
13838 1.14 christos /* The internal_relocs will be freed when
13839 1.14 christos the memory for the bfd is released. */
13840 1.14 christos result = false;
13841 1.12 christos continue;
13842 1.12 christos }
13843 1.12 christos
13844 1.12 christos if (dynamic)
13845 1.12 christos symcount = bfd_get_dynamic_symcount (abfd);
13846 1.12 christos else
13847 1.14 christos symcount = bfd_get_symcount (abfd);
13848 1.12 christos
13849 1.12 christos for (i = 0, internal_reloc = internal_relocs,
13850 1.14 christos native_reloc = native_relocs;
13851 1.14 christos i < reloc_count;
13852 1.14 christos i++, internal_reloc++, native_reloc += entsize)
13853 1.14 christos {
13854 1.12 christos bool res;
13855 1.12 christos Elf_Internal_Rela rela;
13856 1.12 christos
13857 1.12 christos if (entsize == ebd->s->sizeof_rel)
13858 1.12 christos ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13859 1.12 christos else /* entsize == ebd->s->sizeof_rela */
13860 1.12 christos ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13861 1.12 christos
13862 1.12 christos /* The address of an ELF reloc is section relative for an object
13863 1.12 christos file, and absolute for an executable file or shared library.
13864 1.12 christos The address of a normal BFD reloc is always section relative,
13865 1.12 christos and the address of a dynamic reloc is absolute.. */
13866 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13867 1.12 christos internal_reloc->address = rela.r_offset;
13868 1.18 christos else
13869 1.12 christos internal_reloc->address = rela.r_offset - sec->vma;
13870 1.12 christos
13871 1.12 christos if (r_sym (rela.r_info) == STN_UNDEF)
13872 1.12 christos {
13873 1.12 christos /* FIXME: This and the error case below mean that we
13874 1.14 christos have a symbol on relocs that is not elf_symbol_type. */
13875 1.12 christos internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13876 1.12 christos }
13877 1.18 christos else if (r_sym (rela.r_info) > symcount)
13878 1.14 christos {
13879 1.12 christos _bfd_error_handler
13880 1.12 christos /* xgettext:c-format */
13881 1.12 christos (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13882 1.12 christos abfd, sec, i, (long) r_sym (rela.r_info));
13883 1.12 christos bfd_set_error (bfd_error_bad_value);
13884 1.12 christos internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13885 1.12 christos result = false;
13886 1.12 christos }
13887 1.12 christos else
13888 1.12 christos {
13889 1.12 christos asymbol **ps;
13890 1.12 christos
13891 1.12 christos ps = symbols + r_sym (rela.r_info) - 1;
13892 1.12 christos internal_reloc->sym_ptr_ptr = ps;
13893 1.12 christos /* Make sure that this symbol is not removed by strip. */
13894 1.12 christos (*ps)->flags |= BSF_KEEP;
13895 1.12 christos }
13896 1.14 christos
13897 1.14 christos internal_reloc->addend = rela.r_addend;
13898 1.12 christos
13899 1.12 christos res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13900 1.14 christos if (! res || internal_reloc->howto == NULL)
13901 1.12 christos {
13902 1.12 christos #if DEBUG_SECONDARY_RELOCS
13903 1.12 christos fprintf (stderr,
13904 1.12 christos "there is no howto associated with reloc %lx\n",
13905 1.12 christos rela.r_info);
13906 1.12 christos #endif
13907 1.12 christos result = false;
13908 1.12 christos }
13909 1.12 christos }
13910 1.12 christos
13911 1.12 christos free (native_relocs);
13912 1.12 christos /* Store the internal relocs. */
13913 1.12 christos elf_section_data (relsec)->sec_info = internal_relocs;
13914 1.12 christos }
13915 1.14 christos }
13916 1.14 christos
13917 1.14 christos return result;
13918 1.14 christos }
13919 1.14 christos
13920 1.12 christos /* Set the ELF section header fields of an output secondary reloc section. */
13921 1.12 christos
13922 1.12 christos bool
13923 1.12 christos _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13924 1.12 christos bfd *obfd ATTRIBUTE_UNUSED,
13925 1.12 christos const Elf_Internal_Shdr *isection,
13926 1.14 christos Elf_Internal_Shdr *osection)
13927 1.12 christos {
13928 1.12 christos asection * isec;
13929 1.14 christos asection * osec;
13930 1.12 christos struct bfd_elf_section_data * esd;
13931 1.12 christos
13932 1.12 christos if (isection == NULL)
13933 1.14 christos return false;
13934 1.12 christos
13935 1.12 christos if (isection->sh_type != SHT_SECONDARY_RELOC)
13936 1.12 christos return true;
13937 1.14 christos
13938 1.12 christos isec = isection->bfd_section;
13939 1.12 christos if (isec == NULL)
13940 1.12 christos return false;
13941 1.12 christos
13942 1.12 christos osec = osection->bfd_section;
13943 1.12 christos if (osec == NULL)
13944 1.12 christos return false;
13945 1.12 christos
13946 1.12 christos esd = elf_section_data (osec);
13947 1.12 christos BFD_ASSERT (esd->sec_info == NULL);
13948 1.12 christos esd->sec_info = elf_section_data (isec)->sec_info;
13949 1.14 christos osection->sh_type = SHT_RELA;
13950 1.14 christos osection->sh_link = elf_onesymtab (obfd);
13951 1.12 christos if (osection->sh_link == 0)
13952 1.12 christos {
13953 1.14 christos /* There is no symbol table - we are hosed... */
13954 1.12 christos _bfd_error_handler
13955 1.12 christos /* xgettext:c-format */
13956 1.14 christos (_("%pB(%pA): link section cannot be set"
13957 1.14 christos " because the output file does not have a symbol table"),
13958 1.12 christos obfd, osec);
13959 1.12 christos bfd_set_error (bfd_error_bad_value);
13960 1.12 christos return false;
13961 1.12 christos }
13962 1.12 christos
13963 1.12 christos /* Find the output section that corresponds to the isection's
13964 1.12 christos sh_info link. */
13965 1.12 christos if (isection->sh_info == 0
13966 1.14 christos || isection->sh_info >= elf_numsections (ibfd))
13967 1.12 christos {
13968 1.12 christos _bfd_error_handler
13969 1.12 christos /* xgettext:c-format */
13970 1.12 christos (_("%pB(%pA): info section index is invalid"),
13971 1.12 christos obfd, osec);
13972 1.12 christos bfd_set_error (bfd_error_bad_value);
13973 1.12 christos return false;
13974 1.12 christos }
13975 1.12 christos
13976 1.12 christos isection = elf_elfsections (ibfd)[isection->sh_info];
13977 1.14 christos
13978 1.14 christos if (isection == NULL
13979 1.12 christos || isection->bfd_section == NULL
13980 1.12 christos || isection->bfd_section->output_section == NULL)
13981 1.14 christos {
13982 1.12 christos _bfd_error_handler
13983 1.12 christos /* xgettext:c-format */
13984 1.12 christos (_("%pB(%pA): info section index cannot be set"
13985 1.12 christos " because the section is not in the output"),
13986 1.12 christos obfd, osec);
13987 1.14 christos bfd_set_error (bfd_error_bad_value);
13988 1.12 christos return false;
13989 1.12 christos }
13990 1.12 christos
13991 1.12 christos esd = elf_section_data (isection->bfd_section->output_section);
13992 1.12 christos BFD_ASSERT (esd != NULL);
13993 1.12 christos osection->sh_info = esd->this_idx;
13994 1.12 christos esd->has_secondary_relocs = true;
13995 1.14 christos #if DEBUG_SECONDARY_RELOCS
13996 1.12 christos fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13997 1.12 christos osec->name, osection->sh_link, osection->sh_info);
13998 1.12 christos fprintf (stderr, "mark section %s as having secondary relocs\n",
13999 1.12 christos bfd_section_name (isection->bfd_section->output_section));
14000 1.12 christos #endif
14001 1.12 christos
14002 1.12 christos return true;
14003 1.12 christos }
14004 1.12 christos
14005 1.14 christos /* Write out a secondary reloc section.
14006 1.12 christos
14007 1.12 christos FIXME: Currently this function can result in a serious performance penalty
14008 1.12 christos for files with secondary relocs and lots of sections. The proper way to
14009 1.12 christos fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14010 1.12 christos relocs together and then to have this function just walk that chain. */
14011 1.12 christos
14012 1.14 christos bool
14013 1.12 christos _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14014 1.12 christos {
14015 1.14 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14016 1.12 christos bfd_vma addr_offset;
14017 1.12 christos asection * relsec;
14018 1.12 christos bfd_vma (*r_info) (bfd_vma, bfd_vma);
14019 1.12 christos bool result = true;
14020 1.12 christos
14021 1.12 christos if (sec == NULL)
14022 1.12 christos return false;
14023 1.12 christos
14024 1.12 christos #if BFD_DEFAULT_TARGET_SIZE > 32
14025 1.12 christos if (bfd_arch_bits_per_address (abfd) != 32)
14026 1.12 christos r_info = elf64_r_info;
14027 1.12 christos else
14028 1.12 christos #endif
14029 1.12 christos r_info = elf32_r_info;
14030 1.12 christos
14031 1.12 christos /* The address of an ELF reloc is section relative for an object
14032 1.12 christos file, and absolute for an executable file or shared library.
14033 1.12 christos The address of a BFD reloc is always section relative. */
14034 1.12 christos addr_offset = 0;
14035 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14036 1.12 christos addr_offset = sec->vma;
14037 1.12 christos
14038 1.12 christos /* Discover if there are any secondary reloc sections
14039 1.12 christos associated with SEC. */
14040 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14041 1.12 christos {
14042 1.12 christos const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14043 1.14 christos Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14044 1.14 christos
14045 1.14 christos if (hdr->sh_type == SHT_RELA
14046 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14047 1.12 christos {
14048 1.12 christos asymbol * last_sym;
14049 1.12 christos int last_sym_idx;
14050 1.12 christos size_t reloc_count;
14051 1.12 christos size_t idx;
14052 1.12 christos bfd_size_type entsize;
14053 1.12 christos arelent * src_irel;
14054 1.12 christos bfd_byte * dst_rela;
14055 1.12 christos
14056 1.14 christos if (hdr->contents != NULL)
14057 1.12 christos {
14058 1.12 christos _bfd_error_handler
14059 1.12 christos /* xgettext:c-format */
14060 1.14 christos (_("%pB(%pA): error: secondary reloc section processed twice"),
14061 1.14 christos abfd, relsec);
14062 1.12 christos bfd_set_error (bfd_error_bad_value);
14063 1.12 christos result = false;
14064 1.12 christos continue;
14065 1.14 christos }
14066 1.14 christos
14067 1.12 christos entsize = hdr->sh_entsize;
14068 1.12 christos if (entsize == 0)
14069 1.14 christos {
14070 1.14 christos _bfd_error_handler
14071 1.14 christos /* xgettext:c-format */
14072 1.14 christos (_("%pB(%pA): error: secondary reloc section"
14073 1.14 christos " has zero sized entries"),
14074 1.14 christos abfd, relsec);
14075 1.14 christos bfd_set_error (bfd_error_bad_value);
14076 1.14 christos result = false;
14077 1.14 christos continue;
14078 1.14 christos }
14079 1.14 christos else if (entsize != ebd->s->sizeof_rel
14080 1.14 christos && entsize != ebd->s->sizeof_rela)
14081 1.14 christos {
14082 1.12 christos _bfd_error_handler
14083 1.12 christos /* xgettext:c-format */
14084 1.12 christos (_("%pB(%pA): error: secondary reloc section"
14085 1.14 christos " has non-standard sized entries"),
14086 1.14 christos abfd, relsec);
14087 1.14 christos bfd_set_error (bfd_error_bad_value);
14088 1.12 christos result = false;
14089 1.12 christos continue;
14090 1.12 christos }
14091 1.12 christos
14092 1.12 christos reloc_count = hdr->sh_size / entsize;
14093 1.12 christos hdr->sh_size = entsize * reloc_count;
14094 1.14 christos if (reloc_count == 0)
14095 1.12 christos {
14096 1.12 christos _bfd_error_handler
14097 1.12 christos /* xgettext:c-format */
14098 1.12 christos (_("%pB(%pA): error: secondary reloc section is empty!"),
14099 1.12 christos abfd, relsec);
14100 1.12 christos bfd_set_error (bfd_error_bad_value);
14101 1.12 christos result = false;
14102 1.12 christos continue;
14103 1.12 christos }
14104 1.12 christos
14105 1.12 christos hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14106 1.12 christos if (hdr->contents == NULL)
14107 1.12 christos continue;
14108 1.12 christos
14109 1.12 christos #if DEBUG_SECONDARY_RELOCS
14110 1.12 christos fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14111 1.12 christos reloc_count, sec->name, relsec->name);
14112 1.12 christos #endif
14113 1.12 christos last_sym = NULL;
14114 1.14 christos last_sym_idx = 0;
14115 1.14 christos dst_rela = hdr->contents;
14116 1.12 christos src_irel = (arelent *) esd->sec_info;
14117 1.12 christos if (src_irel == NULL)
14118 1.14 christos {
14119 1.12 christos _bfd_error_handler
14120 1.12 christos /* xgettext:c-format */
14121 1.12 christos (_("%pB(%pA): error: internal relocs missing"
14122 1.14 christos " for secondary reloc section"),
14123 1.12 christos abfd, relsec);
14124 1.12 christos bfd_set_error (bfd_error_bad_value);
14125 1.12 christos result = false;
14126 1.12 christos continue;
14127 1.12 christos }
14128 1.12 christos
14129 1.12 christos for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14130 1.12 christos {
14131 1.12 christos Elf_Internal_Rela src_rela;
14132 1.12 christos arelent *ptr;
14133 1.12 christos asymbol *sym;
14134 1.14 christos int n;
14135 1.12 christos
14136 1.12 christos ptr = src_irel + idx;
14137 1.14 christos if (ptr == NULL)
14138 1.12 christos {
14139 1.12 christos _bfd_error_handler
14140 1.12 christos /* xgettext:c-format */
14141 1.12 christos (_("%pB(%pA): error: reloc table entry %zu is empty"),
14142 1.12 christos abfd, relsec, idx);
14143 1.12 christos bfd_set_error (bfd_error_bad_value);
14144 1.12 christos result = false;
14145 1.12 christos break;
14146 1.12 christos }
14147 1.12 christos
14148 1.12 christos if (ptr->sym_ptr_ptr == NULL)
14149 1.12 christos {
14150 1.12 christos /* FIXME: Is this an error ? */
14151 1.12 christos n = 0;
14152 1.12 christos }
14153 1.12 christos else
14154 1.12 christos {
14155 1.12 christos sym = *ptr->sym_ptr_ptr;
14156 1.12 christos
14157 1.12 christos if (sym == last_sym)
14158 1.12 christos n = last_sym_idx;
14159 1.14 christos else
14160 1.14 christos {
14161 1.12 christos n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14162 1.12 christos if (n < 0)
14163 1.14 christos {
14164 1.12 christos _bfd_error_handler
14165 1.12 christos /* xgettext:c-format */
14166 1.12 christos (_("%pB(%pA): error: secondary reloc %zu"
14167 1.12 christos " references a missing symbol"),
14168 1.12 christos abfd, relsec, idx);
14169 1.12 christos bfd_set_error (bfd_error_bad_value);
14170 1.12 christos result = false;
14171 1.12 christos n = 0;
14172 1.12 christos }
14173 1.12 christos
14174 1.12 christos last_sym = sym;
14175 1.12 christos last_sym_idx = n;
14176 1.12 christos }
14177 1.14 christos
14178 1.14 christos if (sym->the_bfd != NULL
14179 1.12 christos && sym->the_bfd->xvec != abfd->xvec
14180 1.12 christos && ! _bfd_elf_validate_reloc (abfd, ptr))
14181 1.14 christos {
14182 1.12 christos _bfd_error_handler
14183 1.12 christos /* xgettext:c-format */
14184 1.12 christos (_("%pB(%pA): error: secondary reloc %zu"
14185 1.12 christos " references a deleted symbol"),
14186 1.12 christos abfd, relsec, idx);
14187 1.12 christos bfd_set_error (bfd_error_bad_value);
14188 1.12 christos result = false;
14189 1.12 christos n = 0;
14190 1.12 christos }
14191 1.14 christos }
14192 1.14 christos
14193 1.12 christos src_rela.r_offset = ptr->address + addr_offset;
14194 1.12 christos if (ptr->howto == NULL)
14195 1.14 christos {
14196 1.12 christos _bfd_error_handler
14197 1.12 christos /* xgettext:c-format */
14198 1.12 christos (_("%pB(%pA): error: secondary reloc %zu"
14199 1.12 christos " is of an unknown type"),
14200 1.12 christos abfd, relsec, idx);
14201 1.14 christos bfd_set_error (bfd_error_bad_value);
14202 1.14 christos result = false;
14203 1.14 christos src_rela.r_info = r_info (0, 0);
14204 1.14 christos }
14205 1.14 christos else
14206 1.12 christos src_rela.r_info = r_info (n, ptr->howto->type);
14207 1.12 christos src_rela.r_addend = ptr->addend;
14208 1.12 christos
14209 1.12 christos if (entsize == ebd->s->sizeof_rel)
14210 1.12 christos ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14211 1.12 christos else /* entsize == ebd->s->sizeof_rela */
14212 1.17 christos ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14213 1.17 christos }
14214 1.17 christos }
14215 1.17 christos }
14216 1.17 christos
14217 1.17 christos return result;
14218 1.17 christos }
14219 1.17 christos
14220 1.17 christos /* Mmap in section contents. If FINAL_LINK is false, set *BUF to NULL
14221 1.17 christos before calling bfd_get_full_section_contents. */
14222 1.17 christos
14223 1.17 christos static bool
14224 1.17 christos elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf,
14225 1.17 christos bool final_link)
14226 1.17 christos {
14227 1.17 christos #ifdef USE_MMAP
14228 1.17 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14229 1.17 christos if (bed->use_mmap
14230 1.17 christos && sec->compress_status == COMPRESS_SECTION_NONE
14231 1.17 christos && (sec->flags & SEC_LINKER_CREATED) == 0)
14232 1.17 christos {
14233 1.17 christos /* Use mmap only if section size >= the minimum mmap section
14234 1.17 christos size. */
14235 1.17 christos size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14236 1.17 christos size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14237 1.17 christos if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14238 1.17 christos {
14239 1.17 christos if (sec->contents != NULL)
14240 1.17 christos {
14241 1.17 christos if (!sec->mmapped_p)
14242 1.17 christos abort ();
14243 1.17 christos *buf = sec->contents;
14244 1.17 christos return true;
14245 1.17 christos }
14246 1.17 christos if (sec->mmapped_p)
14247 1.17 christos abort ();
14248 1.17 christos sec->mmapped_p = 1;
14249 1.17 christos
14250 1.17 christos /* Never use the preallocated buffer if mmapp is used. */
14251 1.17 christos *buf = NULL;
14252 1.17 christos }
14253 1.17 christos }
14254 1.17 christos #endif
14255 1.17 christos /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is
14256 1.17 christos true. If FINAL_LINK is false, *BUF is set to the preallocated
14257 1.17 christos buffer if USE_MMAP is undefined and *BUF is set to NULL if
14258 1.17 christos USE_MMAP is defined. */
14259 1.17 christos if (!final_link)
14260 1.17 christos *buf = NULL;
14261 1.17 christos bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14262 1.17 christos if (ret && sec->mmapped_p)
14263 1.17 christos *buf = sec->contents;
14264 1.17 christos return ret;
14265 1.17 christos }
14266 1.17 christos
14267 1.17 christos /* Mmap in section contents. */
14268 1.17 christos
14269 1.17 christos bool
14270 1.17 christos _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14271 1.17 christos {
14272 1.17 christos return elf_mmap_section_contents (abfd, sec, buf, false);
14273 1.17 christos }
14274 1.17 christos
14275 1.17 christos /* Mmap in the full section contents for the final link. */
14276 1.17 christos
14277 1.17 christos bool
14278 1.17 christos _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14279 1.17 christos bfd_byte **buf)
14280 1.17 christos {
14281 1.17 christos return elf_mmap_section_contents (abfd, sec, buf, true);
14282 1.17 christos }
14283 1.17 christos
14284 1.17 christos /* Munmap section contents. */
14285 1.17 christos
14286 1.17 christos void
14287 1.17 christos _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
14288 1.17 christos void *contents)
14289 1.17 christos {
14290 1.17 christos /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14291 1.17 christos CONTENTS may be NULL. */
14292 1.17 christos if (contents == NULL)
14293 1.17 christos return;
14294 1.17 christos
14295 1.17 christos #ifdef USE_MMAP
14296 1.17 christos if (sec->mmapped_p)
14297 1.17 christos {
14298 1.17 christos /* _bfd_elf_mmap_section_contents may return the previously
14299 1.17 christos mapped section contents. Munmap the section contents only
14300 1.17 christos if they haven't been cached. */
14301 1.17 christos if (elf_section_data (sec)->this_hdr.contents == contents)
14302 1.17 christos return;
14303 1.17 christos
14304 1.17 christos /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14305 1.17 christos malloced, CONTENTS_ADDR is set to NULL. */
14306 1.17 christos if (elf_section_data (sec)->contents_addr != NULL)
14307 1.17 christos {
14308 1.17 christos /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14309 1.17 christos if (munmap (elf_section_data (sec)->contents_addr,
14310 1.17 christos elf_section_data (sec)->contents_size) != 0)
14311 1.17 christos abort ();
14312 1.17 christos sec->mmapped_p = 0;
14313 1.17 christos sec->contents = NULL;
14314 1.17 christos elf_section_data (sec)->contents_addr = NULL;
14315 1.17 christos elf_section_data (sec)->contents_size = 0;
14316 1.17 christos return;
14317 1.17 christos }
14318 1.17 christos }
14319 1.17 christos #endif
14320 1.17 christos
14321 1.17 christos free (contents);
14322 1.17 christos }
14323 1.17 christos
14324 1.17 christos /* Munmap the full section contents for the final link. */
14325 1.17 christos
14326 1.17 christos void
14327 1.17 christos _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14328 1.17 christos {
14329 1.17 christos #ifdef USE_MMAP
14330 1.17 christos if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14331 1.17 christos {
14332 1.17 christos /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14333 1.17 christos malloced, CONTENTS_ADDR is set to NULL. */
14334 1.17 christos /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14335 1.17 christos if (munmap (elf_section_data (sec)->contents_addr,
14336 1.17 christos elf_section_data (sec)->contents_size) != 0)
14337 1.17 christos abort ();
14338 sec->mmapped_p = 0;
14339 sec->contents = NULL;
14340 elf_section_data (sec)->contents_addr = NULL;
14341 elf_section_data (sec)->contents_size = 0;
14342 }
14343 #endif
14344 }
14345