]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf.c
[gdb/build] Work around GCC ipa-modref bug
[thirdparty/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2
3 Copyright (C) 1993-2025 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22
23 /*
24 SECTION
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
34
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include <limits.h>
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
47
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
51
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
58
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
62
63 /* Swap in a Verdef structure. */
64
65 void
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
69 {
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
77 }
78
79 /* Swap out a Verdef structure. */
80
81 void
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
85 {
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93 }
94
95 /* Swap in a Verdaux structure. */
96
97 void
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
101 {
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
104 }
105
106 /* Swap out a Verdaux structure. */
107
108 void
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
112 {
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115 }
116
117 /* Swap in a Verneed structure. */
118
119 void
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
123 {
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
129 }
130
131 /* Swap out a Verneed structure. */
132
133 void
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
137 {
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143 }
144
145 /* Swap in a Vernaux structure. */
146
147 void
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
151 {
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
157 }
158
159 /* Swap out a Vernaux structure. */
160
161 void
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
165 {
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171 }
172
173 /* Swap in a Versym structure. */
174
175 void
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
179 {
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181 }
182
183 /* Swap out a Versym structure. */
184
185 void
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
189 {
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191 }
192
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
195
196 unsigned long
197 bfd_elf_hash (const char *namearg)
198 {
199 uint32_t h = 0;
200
201 for (const unsigned char *name = (const unsigned char *) namearg;
202 *name; name++)
203 {
204 h = (h << 4) + *name;
205 h ^= (h >> 24) & 0xf0;
206 }
207 return h & 0x0fffffff;
208 }
209
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
212
213 unsigned long
214 bfd_elf_gnu_hash (const char *namearg)
215 {
216 uint32_t h = 5381;
217
218 for (const unsigned char *name = (const unsigned char *) namearg;
219 *name; name++)
220 h = (h << 5) + h + *name;
221 return h;
222 }
223
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set. */
226 bool
227 bfd_elf_allocate_object (bfd *abfd,
228 size_t object_size)
229 {
230 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
231 abfd->tdata.any = bfd_zalloc (abfd, object_size);
232 if (abfd->tdata.any == NULL)
233 return false;
234
235 elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
236 if (abfd->direction != read_direction)
237 {
238 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
239 if (o == NULL)
240 return false;
241 elf_tdata (abfd)->o = o;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
243 }
244 return true;
245 }
246
247
248 bool
249 bfd_elf_make_object (bfd *abfd)
250 {
251 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
252 }
253
254 bool
255 bfd_elf_mkcorefile (bfd *abfd)
256 {
257 /* I think this can be done just like an object file. */
258 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
259 return false;
260 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
261 return elf_tdata (abfd)->core != NULL;
262 }
263
264 char *
265 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
266 {
267 Elf_Internal_Shdr **i_shdrp;
268 bfd_byte *shstrtab = NULL;
269 file_ptr offset;
270 bfd_size_type shstrtabsize;
271
272 i_shdrp = elf_elfsections (abfd);
273 if (i_shdrp == 0
274 || shindex >= elf_numsections (abfd)
275 || i_shdrp[shindex] == 0)
276 return NULL;
277
278 shstrtab = i_shdrp[shindex]->contents;
279 if (shstrtab == NULL)
280 {
281 /* No cached one, attempt to read, and cache what we read. */
282 offset = i_shdrp[shindex]->sh_offset;
283 shstrtabsize = i_shdrp[shindex]->sh_size;
284
285 if (shstrtabsize == 0
286 || bfd_seek (abfd, offset, SEEK_SET) != 0
287 || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
288 {
289 /* Once we've failed to read it, make sure we don't keep
290 trying. Otherwise, we'll keep allocating space for
291 the string table over and over. */
292 i_shdrp[shindex]->sh_size = 0;
293 }
294 else if (shstrtab[shstrtabsize - 1] != 0)
295 {
296 /* It is an error if a string table isn't terminated. */
297 _bfd_error_handler
298 /* xgettext:c-format */
299 (_("%pB: string table [%u] is corrupt"), abfd, shindex);
300 shstrtab[shstrtabsize - 1] = 0;
301 }
302 i_shdrp[shindex]->contents = shstrtab;
303 }
304 return (char *) shstrtab;
305 }
306
307 char *
308 bfd_elf_string_from_elf_section (bfd *abfd,
309 unsigned int shindex,
310 unsigned int strindex)
311 {
312 Elf_Internal_Shdr *hdr;
313
314 if (strindex == 0)
315 return "";
316
317 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 return NULL;
319
320 hdr = elf_elfsections (abfd)[shindex];
321
322 if (hdr->contents == NULL)
323 {
324 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325 {
326 /* PR 17512: file: f057ec89. */
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: attempt to load strings from"
329 " a non-string section (number %d)"),
330 abfd, shindex);
331 return NULL;
332 }
333
334 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335 return NULL;
336 }
337 else
338 {
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345 return NULL;
346 }
347
348 if (strindex >= hdr->sh_size)
349 {
350 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351 _bfd_error_handler
352 /* xgettext:c-format */
353 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354 abfd, strindex, (uint64_t) hdr->sh_size,
355 (shindex == shstrndx && strindex == hdr->sh_name
356 ? ".shstrtab"
357 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358 return NULL;
359 }
360
361 return ((char *) hdr->contents) + strindex;
362 }
363
364 /* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
371
372 Elf_Internal_Sym *
373 bfd_elf_get_elf_syms (bfd *ibfd,
374 Elf_Internal_Shdr *symtab_hdr,
375 size_t symcount,
376 size_t symoffset,
377 Elf_Internal_Sym *intsym_buf,
378 void *extsym_buf,
379 Elf_External_Sym_Shndx *extshndx_buf)
380 {
381 Elf_Internal_Shdr *shndx_hdr;
382 void *alloc_ext;
383 const bfd_byte *esym;
384 Elf_External_Sym_Shndx *alloc_extshndx;
385 Elf_External_Sym_Shndx *shndx;
386 Elf_Internal_Sym *alloc_intsym;
387 Elf_Internal_Sym *isym;
388 Elf_Internal_Sym *isymend;
389 const struct elf_backend_data *bed;
390 size_t extsym_size;
391 size_t amt;
392 file_ptr pos;
393
394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395 abort ();
396
397 if (symcount == 0)
398 return intsym_buf;
399
400 if (elf_use_dt_symtab_p (ibfd))
401 {
402 /* Use dynamic symbol table. */
403 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
404 {
405 bfd_set_error (bfd_error_invalid_operation);
406 return NULL;
407 }
408 return elf_tdata (ibfd)->dt_symtab + symoffset;
409 }
410
411 /* Normal syms might have section extension entries. */
412 shndx_hdr = NULL;
413 if (elf_symtab_shndx_list (ibfd) != NULL)
414 {
415 elf_section_list * entry;
416 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417
418 /* Find an index section that is linked to this symtab section. */
419 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420 {
421 /* PR 20063. */
422 if (entry->hdr.sh_link >= elf_numsections (ibfd))
423 continue;
424
425 if (sections[entry->hdr.sh_link] == symtab_hdr)
426 {
427 shndx_hdr = & entry->hdr;
428 break;
429 };
430 }
431
432 if (shndx_hdr == NULL)
433 {
434 if (symtab_hdr == &elf_symtab_hdr (ibfd))
435 /* Not really accurate, but this was how the old code used
436 to work. */
437 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
438 /* Otherwise we do nothing. The assumption is that
439 the index table will not be needed. */
440 }
441 }
442
443 /* Read the symbols. */
444 alloc_ext = NULL;
445 alloc_extshndx = NULL;
446 alloc_intsym = NULL;
447 bed = get_elf_backend_data (ibfd);
448 extsym_size = bed->s->sizeof_sym;
449 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
450 {
451 bfd_set_error (bfd_error_file_too_big);
452 return NULL;
453 }
454 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
455 size_t alloc_ext_size = amt;
456 if (bfd_seek (ibfd, pos, SEEK_SET) != 0
457 || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
458 &alloc_ext, ibfd, false))
459 {
460 intsym_buf = NULL;
461 goto out2;
462 }
463
464 size_t alloc_extshndx_size = 0;
465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
466 extshndx_buf = NULL;
467 else
468 {
469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
470 {
471 bfd_set_error (bfd_error_file_too_big);
472 intsym_buf = NULL;
473 goto out1;
474 }
475 alloc_extshndx_size = amt;
476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 if (bfd_seek (ibfd, pos, SEEK_SET) != 0
478 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
479 &alloc_extshndx_size,
480 (void **) &alloc_extshndx,
481 ibfd, false))
482 {
483 intsym_buf = NULL;
484 goto out1;
485 }
486 }
487
488 if (intsym_buf == NULL)
489 {
490 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
491 {
492 bfd_set_error (bfd_error_file_too_big);
493 goto out1;
494 }
495 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
496 intsym_buf = alloc_intsym;
497 if (intsym_buf == NULL)
498 goto out1;
499 }
500
501 /* Convert the symbols to internal form. */
502 isymend = intsym_buf + symcount;
503 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
504 shndx = extshndx_buf;
505 isym < isymend;
506 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
507 {
508 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
509 {
510 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
511 /* xgettext:c-format */
512 _bfd_error_handler (_("%pB symbol number %lu references"
513 " nonexistent SHT_SYMTAB_SHNDX section"),
514 ibfd, (unsigned long) symoffset);
515 free (alloc_intsym);
516 intsym_buf = NULL;
517 goto out1;
518 }
519
520 /* PR 33019: Do not accept unsupported binding values - they will
521 likely cause problems later on. */
522 int bind = ELF_ST_BIND (isym->st_info);
523 if (bind > STB_WEAK && bind < STB_LOOS)
524 {
525 /* xgettext:c-format */
526 _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
527 ibfd, (unsigned long) (isym - intsym_buf), bind);
528 free (alloc_intsym);
529 intsym_buf = NULL;
530 goto out1;
531 }
532
533 /* Paranoia: Also refuse to accept the only undefined symbol type: 7. */
534 int t = ELF_ST_TYPE (isym->st_info);
535 if (t == 7)
536 {
537 /* xgettext:c-format */
538 _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
539 ibfd, (unsigned long) (isym - intsym_buf), t);
540 free (alloc_intsym);
541 intsym_buf = NULL;
542 goto out1;
543 }
544 }
545
546 out1:
547 _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
548 out2:
549 _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
550
551 return intsym_buf;
552 }
553
554 /* Look up a symbol name. */
555 static const char *
556 bfd_elf_sym_name_raw (bfd *abfd,
557 Elf_Internal_Shdr *symtab_hdr,
558 Elf_Internal_Sym *isym)
559 {
560 unsigned int iname = isym->st_name;
561 unsigned int shindex = symtab_hdr->sh_link;
562
563 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
564 /* Check for a bogus st_shndx to avoid crashing. */
565 && isym->st_shndx < elf_numsections (abfd))
566 {
567 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
568 shindex = elf_elfheader (abfd)->e_shstrndx;
569 }
570
571 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
572 }
573
574 const char *
575 bfd_elf_sym_name (bfd *abfd,
576 Elf_Internal_Shdr *symtab_hdr,
577 Elf_Internal_Sym *isym,
578 asection *sym_sec)
579 {
580 const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
581 if (name == NULL)
582 name = bfd_symbol_error_name;
583 else if (sym_sec && *name == '\0')
584 name = bfd_section_name (sym_sec);
585
586 return name;
587 }
588
589 /* Return the name of the group signature symbol. Why isn't the
590 signature just a string? */
591
592 static const char *
593 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
594 {
595 Elf_Internal_Shdr *hdr;
596 unsigned char esym[sizeof (Elf64_External_Sym)];
597 Elf_External_Sym_Shndx eshndx;
598 Elf_Internal_Sym isym;
599
600 /* First we need to ensure the symbol table is available. Make sure
601 that it is a symbol table section. */
602 if (ghdr->sh_link >= elf_numsections (abfd))
603 return NULL;
604 hdr = elf_elfsections (abfd) [ghdr->sh_link];
605 if (hdr->sh_type != SHT_SYMTAB
606 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
607 return NULL;
608
609 /* Go read the symbol. */
610 hdr = &elf_tdata (abfd)->symtab_hdr;
611 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
612 &isym, esym, &eshndx) == NULL)
613 return NULL;
614
615 return bfd_elf_sym_name_raw (abfd, hdr, &isym);
616 }
617
618 static bool
619 is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
620 {
621 return (shdr->sh_size >= minsize
622 && shdr->sh_entsize == GRP_ENTRY_SIZE
623 && shdr->sh_size % GRP_ENTRY_SIZE == 0
624 && shdr->bfd_section != NULL);
625 }
626
627
628 /* Set next_in_group, sec_group list pointers, and group names. */
629
630 static bool
631 process_sht_group_entries (bfd *abfd,
632 Elf_Internal_Shdr *ghdr, unsigned int gidx)
633 {
634 unsigned char *contents;
635
636 /* Read the raw contents. */
637 if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
638 {
639 _bfd_error_handler
640 /* xgettext:c-format */
641 (_("%pB: could not read contents of group [%u]"), abfd, gidx);
642 return false;
643 }
644
645 asection *last_elt = NULL;
646 const char *gname = NULL;
647 unsigned char *p = contents + ghdr->sh_size;
648 while (1)
649 {
650 unsigned int idx;
651 Elf_Internal_Shdr *shdr;
652 asection *elt;
653
654 p -= 4;
655 idx = H_GET_32 (abfd, p);
656 if (p == contents)
657 {
658 if ((idx & GRP_COMDAT) != 0)
659 ghdr->bfd_section->flags
660 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
661 break;
662 }
663
664 if (idx == 0
665 || idx >= elf_numsections (abfd)
666 || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
667 || ((elt = shdr->bfd_section) != NULL
668 && elf_sec_group (elt) != NULL
669 && elf_sec_group (elt) != ghdr->bfd_section))
670 {
671 _bfd_error_handler
672 (_("%pB: invalid entry (%#x) in group [%u]"),
673 abfd, idx, gidx);
674 continue;
675 }
676
677 /* PR binutils/23199: According to the ELF gABI all sections in
678 a group must be marked with SHF_GROUP, but some tools
679 generate broken objects. Fix them up here. */
680 shdr->sh_flags |= SHF_GROUP;
681
682 if (elt == NULL)
683 {
684 if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
685 {
686 const char *name = bfd_elf_string_from_elf_section
687 (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
688
689 _bfd_error_handler
690 /* xgettext:c-format */
691 (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
692 abfd, shdr->sh_type, name, gidx);
693 }
694 continue;
695 }
696
697 /* Don't try to add a section to elf_next_in_group list twice. */
698 if (elf_sec_group (elt) != NULL)
699 continue;
700
701 if (last_elt == NULL)
702 {
703 /* Start a circular list with one element.
704 It will be in reverse order to match what gas does. */
705 elf_next_in_group (elt) = elt;
706 /* Point the group section to it. */
707 elf_next_in_group (ghdr->bfd_section) = elt;
708 gname = group_signature (abfd, ghdr);
709 if (gname == NULL)
710 {
711 free (contents);
712 return false;
713 }
714 }
715 else
716 {
717 elf_next_in_group (elt) = elf_next_in_group (last_elt);
718 elf_next_in_group (last_elt) = elt;
719 }
720 last_elt = elt;
721 elf_group_name (elt) = gname;
722 elf_sec_group (elt) = ghdr->bfd_section;
723 }
724
725 free (contents);
726 return true;
727 }
728
729 bool
730 _bfd_elf_setup_sections (bfd *abfd)
731 {
732 bool result = true;
733
734 /* Process SHF_LINK_ORDER. */
735 for (asection *s = abfd->sections; s != NULL; s = s->next)
736 {
737 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
738 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
739 {
740 unsigned int elfsec = this_hdr->sh_link;
741 /* An sh_link value of 0 is now allowed. It indicates that linked
742 to section has already been discarded, but that the current
743 section has been retained for some other reason. This linking
744 section is still a candidate for later garbage collection
745 however. */
746 if (elfsec == 0)
747 {
748 elf_linked_to_section (s) = NULL;
749 }
750 else
751 {
752 asection *linksec = NULL;
753
754 if (elfsec < elf_numsections (abfd))
755 {
756 this_hdr = elf_elfsections (abfd)[elfsec];
757 linksec = this_hdr->bfd_section;
758 }
759
760 /* PR 1991, 2008:
761 Some strip/objcopy may leave an incorrect value in
762 sh_link. We don't want to proceed. */
763 if (linksec == NULL)
764 {
765 _bfd_error_handler
766 /* xgettext:c-format */
767 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
768 s->owner, elfsec, s);
769 result = false;
770 }
771
772 elf_linked_to_section (s) = linksec;
773 }
774 }
775 }
776
777 /* Process section groups. */
778 for (unsigned int i = 1; i < elf_numsections (abfd); i++)
779 {
780 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
781
782 if (shdr && shdr->sh_type == SHT_GROUP)
783 {
784 if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
785 {
786 if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
787 && !process_sht_group_entries (abfd, shdr, i))
788 result = false;
789 }
790 else
791 {
792 /* PR binutils/18758: Beware of corrupt binaries with
793 invalid group data. */
794 _bfd_error_handler
795 /* xgettext:c-format */
796 (_("%pB: section group entry number %u is corrupt"), abfd, i);
797 result = false;
798 }
799 }
800 }
801
802 return result;
803 }
804
805 bool
806 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
807 {
808 return elf_next_in_group (sec) != NULL;
809 }
810
811 const char *
812 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
813 {
814 if (elf_sec_group (sec) != NULL)
815 return elf_group_name (sec);
816 return NULL;
817 }
818
819 /* Make a BFD section from an ELF section. We store a pointer to the
820 BFD section in the bfd_section field of the header. */
821
822 bool
823 _bfd_elf_make_section_from_shdr (bfd *abfd,
824 Elf_Internal_Shdr *hdr,
825 const char *name,
826 int shindex)
827 {
828 asection *newsect;
829 flagword flags;
830 const struct elf_backend_data *bed;
831 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
832
833 if (hdr->bfd_section != NULL)
834 return true;
835
836 newsect = bfd_make_section_anyway (abfd, name);
837 if (newsect == NULL)
838 return false;
839
840 hdr->bfd_section = newsect;
841 elf_section_data (newsect)->this_hdr = *hdr;
842 elf_section_data (newsect)->this_idx = shindex;
843
844 /* Always use the real type/flags. */
845 elf_section_type (newsect) = hdr->sh_type;
846 elf_section_flags (newsect) = hdr->sh_flags;
847
848 newsect->filepos = hdr->sh_offset;
849
850 flags = SEC_NO_FLAGS;
851 if (hdr->sh_type != SHT_NOBITS)
852 flags |= SEC_HAS_CONTENTS;
853 if (hdr->sh_type == SHT_GROUP)
854 flags |= SEC_GROUP;
855 if ((hdr->sh_flags & SHF_ALLOC) != 0)
856 {
857 flags |= SEC_ALLOC;
858 if (hdr->sh_type != SHT_NOBITS)
859 flags |= SEC_LOAD;
860 }
861 if ((hdr->sh_flags & SHF_WRITE) == 0)
862 flags |= SEC_READONLY;
863 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
864 flags |= SEC_CODE;
865 else if ((flags & SEC_LOAD) != 0)
866 flags |= SEC_DATA;
867 if ((hdr->sh_flags & SHF_MERGE) != 0)
868 {
869 flags |= SEC_MERGE;
870 newsect->entsize = hdr->sh_entsize;
871 }
872 if ((hdr->sh_flags & SHF_STRINGS) != 0)
873 {
874 flags |= SEC_STRINGS;
875 newsect->entsize = hdr->sh_entsize;
876 }
877 if ((hdr->sh_flags & SHF_TLS) != 0)
878 flags |= SEC_THREAD_LOCAL;
879 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
880 flags |= SEC_EXCLUDE;
881
882 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
883 {
884 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
885 but binutils as of 2019-07-23 did not set the EI_OSABI header
886 byte. */
887 case ELFOSABI_GNU:
888 case ELFOSABI_FREEBSD:
889 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
890 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
891 /* Fall through */
892 case ELFOSABI_NONE:
893 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
894 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
895 break;
896 }
897
898 if ((flags & SEC_ALLOC) == 0)
899 {
900 /* The debugging sections appear to be recognized only by name,
901 not any sort of flag. Their SEC_ALLOC bits are cleared. */
902 if (name [0] == '.')
903 {
904 if (startswith (name, ".debug")
905 || startswith (name, ".gnu.debuglto_.debug_")
906 || startswith (name, ".gnu.linkonce.wi.")
907 || startswith (name, ".zdebug"))
908 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
909 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
910 || startswith (name, ".note.gnu"))
911 {
912 flags |= SEC_ELF_OCTETS;
913 opb = 1;
914 }
915 else if (startswith (name, ".line")
916 || startswith (name, ".stab")
917 || strcmp (name, ".gdb_index") == 0)
918 flags |= SEC_DEBUGGING;
919 }
920 }
921
922 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
923 || !bfd_set_section_size (newsect, hdr->sh_size)
924 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
925 & -hdr->sh_addralign)))
926 return false;
927
928 /* As a GNU extension, if the name begins with .gnu.linkonce, we
929 only link a single copy of the section. This is used to support
930 g++. g++ will emit each template expansion in its own section.
931 The symbols will be defined as weak, so that multiple definitions
932 are permitted. The GNU linker extension is to actually discard
933 all but one of the sections. */
934 if (startswith (name, ".gnu.linkonce")
935 && elf_next_in_group (newsect) == NULL)
936 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
937
938 if (!bfd_set_section_flags (newsect, flags))
939 return false;
940
941 bed = get_elf_backend_data (abfd);
942 if (bed->elf_backend_section_flags)
943 if (!bed->elf_backend_section_flags (hdr))
944 return false;
945
946 /* We do not parse the PT_NOTE segments as we are interested even in the
947 separate debug info files which may have the segments offsets corrupted.
948 PT_NOTEs from the core files are currently not parsed using BFD. */
949 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
950 {
951 bfd_byte *contents;
952
953 if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
954 return false;
955
956 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
957 hdr->sh_offset, hdr->sh_addralign);
958 _bfd_elf_munmap_section_contents (newsect, contents);
959 }
960
961 if ((newsect->flags & SEC_ALLOC) != 0)
962 {
963 Elf_Internal_Phdr *phdr;
964 unsigned int i, nload;
965
966 /* Some ELF linkers produce binaries with all the program header
967 p_paddr fields zero. If we have such a binary with more than
968 one PT_LOAD header, then leave the section lma equal to vma
969 so that we don't create sections with overlapping lma. */
970 phdr = elf_tdata (abfd)->phdr;
971 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
972 if (phdr->p_paddr != 0)
973 break;
974 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
975 ++nload;
976 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
977 return true;
978
979 phdr = elf_tdata (abfd)->phdr;
980 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
981 {
982 if (((phdr->p_type == PT_LOAD
983 && (hdr->sh_flags & SHF_TLS) == 0)
984 || phdr->p_type == PT_TLS)
985 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
986 {
987 if ((newsect->flags & SEC_LOAD) == 0)
988 newsect->lma = (phdr->p_paddr
989 + hdr->sh_addr - phdr->p_vaddr) / opb;
990 else
991 /* We used to use the same adjustment for SEC_LOAD
992 sections, but that doesn't work if the segment
993 is packed with code from multiple VMAs.
994 Instead we calculate the section LMA based on
995 the segment LMA. It is assumed that the
996 segment will contain sections with contiguous
997 LMAs, even if the VMAs are not. */
998 newsect->lma = (phdr->p_paddr
999 + hdr->sh_offset - phdr->p_offset) / opb;
1000
1001 /* With contiguous segments, we can't tell from file
1002 offsets whether a section with zero size should
1003 be placed at the end of one segment or the
1004 beginning of the next. Decide based on vaddr. */
1005 if (hdr->sh_addr >= phdr->p_vaddr
1006 && (hdr->sh_addr + hdr->sh_size
1007 <= phdr->p_vaddr + phdr->p_memsz))
1008 break;
1009 }
1010 }
1011 }
1012
1013 /* Compress/decompress DWARF debug sections with names: .debug_*,
1014 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1015 if ((newsect->flags & SEC_DEBUGGING) != 0
1016 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1017 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1018 {
1019 enum { nothing, compress, decompress } action = nothing;
1020 int compression_header_size;
1021 bfd_size_type uncompressed_size;
1022 unsigned int uncompressed_align_power;
1023 enum compression_type ch_type = ch_none;
1024 bool compressed
1025 = bfd_is_section_compressed_info (abfd, newsect,
1026 &compression_header_size,
1027 &uncompressed_size,
1028 &uncompressed_align_power,
1029 &ch_type);
1030
1031 /* Should we decompress? */
1032 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1033 action = decompress;
1034
1035 /* Should we compress? Or convert to a different compression? */
1036 else if ((abfd->flags & BFD_COMPRESS) != 0
1037 && newsect->size != 0
1038 && compression_header_size >= 0
1039 && uncompressed_size > 0)
1040 {
1041 if (!compressed)
1042 action = compress;
1043 else
1044 {
1045 enum compression_type new_ch_type = ch_none;
1046 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1047 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1048 ? ch_compress_zstd : ch_compress_zlib);
1049 if (new_ch_type != ch_type)
1050 action = compress;
1051 }
1052 }
1053
1054 if (action == compress)
1055 {
1056 if (!bfd_init_section_compress_status (abfd, newsect))
1057 {
1058 _bfd_error_handler
1059 /* xgettext:c-format */
1060 (_("%pB: unable to compress section %s"), abfd, name);
1061 return false;
1062 }
1063 }
1064 else if (action == decompress)
1065 {
1066 if (!bfd_init_section_decompress_status (abfd, newsect))
1067 {
1068 _bfd_error_handler
1069 /* xgettext:c-format */
1070 (_("%pB: unable to decompress section %s"), abfd, name);
1071 return false;
1072 }
1073 #ifndef HAVE_ZSTD
1074 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1075 {
1076 _bfd_error_handler
1077 /* xgettext:c-format */
1078 (_ ("%pB: section %s is compressed with zstd, but BFD "
1079 "is not built with zstd support"),
1080 abfd, name);
1081 newsect->compress_status = COMPRESS_SECTION_NONE;
1082 return false;
1083 }
1084 #endif
1085 if (abfd->is_linker_input
1086 && name[1] == 'z')
1087 {
1088 /* Rename section from .zdebug_* to .debug_* so that ld
1089 scripts will see this section as a debug section. */
1090 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1091 if (new_name == NULL)
1092 return false;
1093 bfd_rename_section (newsect, new_name);
1094 }
1095 }
1096 }
1097
1098 return true;
1099 }
1100
1101 const char *const bfd_elf_section_type_names[] =
1102 {
1103 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1104 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1105 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1106 };
1107
1108 /* ELF relocs are against symbols. If we are producing relocatable
1109 output, and the reloc is against an external symbol, and nothing
1110 has given us any additional addend, the resulting reloc will also
1111 be against the same symbol. In such a case, we don't want to
1112 change anything about the way the reloc is handled, since it will
1113 all be done at final link time. Rather than put special case code
1114 into bfd_perform_relocation, all the reloc types use this howto
1115 function, or should call this function for relocatable output. */
1116
1117 bfd_reloc_status_type
1118 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1119 arelent *reloc_entry,
1120 asymbol *symbol,
1121 void *data ATTRIBUTE_UNUSED,
1122 asection *input_section,
1123 bfd *output_bfd,
1124 char **error_message ATTRIBUTE_UNUSED)
1125 {
1126 if (output_bfd != NULL
1127 && (symbol->flags & BSF_SECTION_SYM) == 0
1128 && (! reloc_entry->howto->partial_inplace
1129 || reloc_entry->addend == 0))
1130 {
1131 reloc_entry->address += input_section->output_offset;
1132 return bfd_reloc_ok;
1133 }
1134
1135 /* In some cases the relocation should be treated as output section
1136 relative, as when linking ELF DWARF into PE COFF. Many ELF
1137 targets lack section relative relocations and instead use
1138 ordinary absolute relocations for references between DWARF
1139 sections. That is arguably a bug in those targets but it happens
1140 to work for the usual case of linking to non-loaded ELF debug
1141 sections with VMAs forced to zero. PE COFF on the other hand
1142 doesn't allow a section VMA of zero. */
1143 if (output_bfd == NULL
1144 && !reloc_entry->howto->pc_relative
1145 && (symbol->section->flags & SEC_DEBUGGING) != 0
1146 && (input_section->flags & SEC_DEBUGGING) != 0)
1147 reloc_entry->addend -= symbol->section->output_section->vma;
1148
1149 return bfd_reloc_continue;
1150 }
1151 \f
1152 /* Returns TRUE if section A matches section B.
1153 Names, addresses and links may be different, but everything else
1154 should be the same. */
1155
1156 static bool
1157 section_match (const Elf_Internal_Shdr * a,
1158 const Elf_Internal_Shdr * b)
1159 {
1160 if (a->sh_type != b->sh_type
1161 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1162 || a->sh_addralign != b->sh_addralign
1163 || a->sh_entsize != b->sh_entsize)
1164 return false;
1165 if (a->sh_type == SHT_SYMTAB
1166 || a->sh_type == SHT_STRTAB)
1167 return true;
1168 return a->sh_size == b->sh_size;
1169 }
1170
1171 /* Find a section in OBFD that has the same characteristics
1172 as IHEADER. Return the index of this section or SHN_UNDEF if
1173 none can be found. Check's section HINT first, as this is likely
1174 to be the correct section. */
1175
1176 static unsigned int
1177 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1178 const unsigned int hint)
1179 {
1180 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1181 unsigned int i;
1182
1183 BFD_ASSERT (iheader != NULL);
1184
1185 /* See PR 20922 for a reproducer of the NULL test. */
1186 if (hint < elf_numsections (obfd)
1187 && oheaders[hint] != NULL
1188 && section_match (oheaders[hint], iheader))
1189 return hint;
1190
1191 for (i = 1; i < elf_numsections (obfd); i++)
1192 {
1193 Elf_Internal_Shdr * oheader = oheaders[i];
1194
1195 if (oheader == NULL)
1196 continue;
1197 if (section_match (oheader, iheader))
1198 /* FIXME: Do we care if there is a potential for
1199 multiple matches ? */
1200 return i;
1201 }
1202
1203 return SHN_UNDEF;
1204 }
1205
1206 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1207 Processor specific section, based upon a matching input section.
1208 Returns TRUE upon success, FALSE otherwise. */
1209
1210 static bool
1211 copy_special_section_fields (const bfd *ibfd,
1212 bfd *obfd,
1213 const Elf_Internal_Shdr *iheader,
1214 Elf_Internal_Shdr *oheader,
1215 const unsigned int secnum)
1216 {
1217 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1218 const Elf_Internal_Shdr **iheaders
1219 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1220 bool changed = false;
1221 unsigned int sh_link;
1222
1223 if (oheader->sh_type == SHT_NOBITS)
1224 {
1225 /* This is a feature for objcopy --only-keep-debug:
1226 When a section's type is changed to NOBITS, we preserve
1227 the sh_link and sh_info fields so that they can be
1228 matched up with the original.
1229
1230 Note: Strictly speaking these assignments are wrong.
1231 The sh_link and sh_info fields should point to the
1232 relevent sections in the output BFD, which may not be in
1233 the same location as they were in the input BFD. But
1234 the whole point of this action is to preserve the
1235 original values of the sh_link and sh_info fields, so
1236 that they can be matched up with the section headers in
1237 the original file. So strictly speaking we may be
1238 creating an invalid ELF file, but it is only for a file
1239 that just contains debug info and only for sections
1240 without any contents. */
1241 if (oheader->sh_link == 0)
1242 oheader->sh_link = iheader->sh_link;
1243 if (oheader->sh_info == 0)
1244 oheader->sh_info = iheader->sh_info;
1245 return true;
1246 }
1247
1248 /* Allow the target a chance to decide how these fields should be set. */
1249 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1250 iheader, oheader))
1251 return true;
1252
1253 /* We have an iheader which might match oheader, and which has non-zero
1254 sh_info and/or sh_link fields. Attempt to follow those links and find
1255 the section in the output bfd which corresponds to the linked section
1256 in the input bfd. */
1257 if (iheader->sh_link != SHN_UNDEF)
1258 {
1259 /* See PR 20931 for a reproducer. */
1260 if (iheader->sh_link >= elf_numsections (ibfd))
1261 {
1262 _bfd_error_handler
1263 /* xgettext:c-format */
1264 (_("%pB: invalid sh_link field (%d) in section number %d"),
1265 ibfd, iheader->sh_link, secnum);
1266 return false;
1267 }
1268
1269 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1270 if (sh_link != SHN_UNDEF)
1271 {
1272 oheader->sh_link = sh_link;
1273 changed = true;
1274 }
1275 else
1276 /* FIXME: Should we install iheader->sh_link
1277 if we could not find a match ? */
1278 _bfd_error_handler
1279 /* xgettext:c-format */
1280 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1281 }
1282
1283 if (iheader->sh_info)
1284 {
1285 /* The sh_info field can hold arbitrary information, but if the
1286 SHF_LINK_INFO flag is set then it should be interpreted as a
1287 section index. */
1288 if (iheader->sh_flags & SHF_INFO_LINK)
1289 {
1290 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1291 iheader->sh_info);
1292 if (sh_link != SHN_UNDEF)
1293 oheader->sh_flags |= SHF_INFO_LINK;
1294 }
1295 else
1296 /* No idea what it means - just copy it. */
1297 sh_link = iheader->sh_info;
1298
1299 if (sh_link != SHN_UNDEF)
1300 {
1301 oheader->sh_info = sh_link;
1302 changed = true;
1303 }
1304 else
1305 _bfd_error_handler
1306 /* xgettext:c-format */
1307 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1308 }
1309
1310 return changed;
1311 }
1312
1313 /* Copy the program header and other data from one object module to
1314 another. */
1315
1316 bool
1317 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1318 {
1319 const Elf_Internal_Shdr **iheaders
1320 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1321 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1322 const struct elf_backend_data *bed;
1323 unsigned int i;
1324
1325 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1326 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1327 return true;
1328
1329 if (!elf_flags_init (obfd))
1330 {
1331 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1332 elf_flags_init (obfd) = true;
1333 }
1334
1335 elf_gp (obfd) = elf_gp (ibfd);
1336
1337 /* Also copy the EI_OSABI field. */
1338 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1339 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1340
1341 /* If set, copy the EI_ABIVERSION field. */
1342 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1343 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1344 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1345
1346 /* Copy object attributes. */
1347 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1348
1349 if (iheaders == NULL || oheaders == NULL)
1350 return true;
1351
1352 bed = get_elf_backend_data (obfd);
1353
1354 /* Possibly copy other fields in the section header. */
1355 for (i = 1; i < elf_numsections (obfd); i++)
1356 {
1357 unsigned int j;
1358 Elf_Internal_Shdr * oheader = oheaders[i];
1359
1360 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1361 because of a special case need for generating separate debug info
1362 files. See below for more details. */
1363 if (oheader == NULL
1364 || (oheader->sh_type != SHT_NOBITS
1365 && oheader->sh_type < SHT_LOOS))
1366 continue;
1367
1368 /* Ignore empty sections, and sections whose
1369 fields have already been initialised. */
1370 if (oheader->sh_size == 0
1371 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1372 continue;
1373
1374 /* Scan for the matching section in the input bfd.
1375 First we try for a direct mapping between the input and
1376 output sections. */
1377 for (j = 1; j < elf_numsections (ibfd); j++)
1378 {
1379 const Elf_Internal_Shdr * iheader = iheaders[j];
1380
1381 if (iheader == NULL)
1382 continue;
1383
1384 if (oheader->bfd_section != NULL
1385 && iheader->bfd_section != NULL
1386 && iheader->bfd_section->output_section != NULL
1387 && iheader->bfd_section->output_section == oheader->bfd_section)
1388 {
1389 /* We have found a connection from the input section to
1390 the output section. Attempt to copy the header fields.
1391 If this fails then do not try any further sections -
1392 there should only be a one-to-one mapping between
1393 input and output. */
1394 if (!copy_special_section_fields (ibfd, obfd,
1395 iheader, oheader, i))
1396 j = elf_numsections (ibfd);
1397 break;
1398 }
1399 }
1400
1401 if (j < elf_numsections (ibfd))
1402 continue;
1403
1404 /* That failed. So try to deduce the corresponding input section.
1405 Unfortunately we cannot compare names as the output string table
1406 is empty, so instead we check size, address and type. */
1407 for (j = 1; j < elf_numsections (ibfd); j++)
1408 {
1409 const Elf_Internal_Shdr * iheader = iheaders[j];
1410
1411 if (iheader == NULL)
1412 continue;
1413
1414 /* Try matching fields in the input section's header.
1415 Since --only-keep-debug turns all non-debug sections into
1416 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1417 input type. */
1418 if ((oheader->sh_type == SHT_NOBITS
1419 || iheader->sh_type == oheader->sh_type)
1420 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1421 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1422 && iheader->sh_addralign == oheader->sh_addralign
1423 && iheader->sh_entsize == oheader->sh_entsize
1424 && iheader->sh_size == oheader->sh_size
1425 && iheader->sh_addr == oheader->sh_addr
1426 && (iheader->sh_info != oheader->sh_info
1427 || iheader->sh_link != oheader->sh_link))
1428 {
1429 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1430 break;
1431 }
1432 }
1433
1434 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1435 {
1436 /* Final attempt. Call the backend copy function
1437 with a NULL input section. */
1438 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1439 NULL, oheader);
1440 }
1441 }
1442
1443 return true;
1444 }
1445
1446 static const char *
1447 get_segment_type (unsigned int p_type)
1448 {
1449 const char *pt;
1450 switch (p_type)
1451 {
1452 case PT_NULL: pt = "NULL"; break;
1453 case PT_LOAD: pt = "LOAD"; break;
1454 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1455 case PT_INTERP: pt = "INTERP"; break;
1456 case PT_NOTE: pt = "NOTE"; break;
1457 case PT_SHLIB: pt = "SHLIB"; break;
1458 case PT_PHDR: pt = "PHDR"; break;
1459 case PT_TLS: pt = "TLS"; break;
1460 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1461 case PT_GNU_STACK: pt = "STACK"; break;
1462 case PT_GNU_RELRO: pt = "RELRO"; break;
1463 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1464 default: pt = NULL; break;
1465 }
1466 return pt;
1467 }
1468
1469 /* Print out the program headers. */
1470
1471 bool
1472 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1473 {
1474 FILE *f = (FILE *) farg;
1475 Elf_Internal_Phdr *p;
1476 asection *s;
1477 bfd_byte *dynbuf = NULL;
1478
1479 p = elf_tdata (abfd)->phdr;
1480 if (p != NULL)
1481 {
1482 unsigned int i, c;
1483
1484 fprintf (f, _("\nProgram Header:\n"));
1485 c = elf_elfheader (abfd)->e_phnum;
1486 for (i = 0; i < c; i++, p++)
1487 {
1488 const char *pt = get_segment_type (p->p_type);
1489 char buf[20];
1490
1491 if (pt == NULL)
1492 {
1493 sprintf (buf, "0x%lx", p->p_type);
1494 pt = buf;
1495 }
1496 fprintf (f, "%8s off 0x", pt);
1497 bfd_fprintf_vma (abfd, f, p->p_offset);
1498 fprintf (f, " vaddr 0x");
1499 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1500 fprintf (f, " paddr 0x");
1501 bfd_fprintf_vma (abfd, f, p->p_paddr);
1502 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1503 fprintf (f, " filesz 0x");
1504 bfd_fprintf_vma (abfd, f, p->p_filesz);
1505 fprintf (f, " memsz 0x");
1506 bfd_fprintf_vma (abfd, f, p->p_memsz);
1507 fprintf (f, " flags %c%c%c",
1508 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1509 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1510 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1511 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1512 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1513 fprintf (f, "\n");
1514 }
1515 }
1516
1517 s = bfd_get_section_by_name (abfd, ".dynamic");
1518 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1519 {
1520 unsigned int elfsec;
1521 unsigned long shlink;
1522 bfd_byte *extdyn, *extdynend;
1523 size_t extdynsize;
1524 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1525
1526 fprintf (f, _("\nDynamic Section:\n"));
1527
1528 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1529 goto error_return;
1530
1531 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1532 if (elfsec == SHN_BAD)
1533 goto error_return;
1534 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1535
1536 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1537 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1538
1539 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1540 (size_t) (extdynend - extdyn) >= extdynsize;
1541 extdyn += extdynsize)
1542 {
1543 Elf_Internal_Dyn dyn;
1544 const char *name = "";
1545 char ab[20];
1546 bool stringp;
1547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1548
1549 (*swap_dyn_in) (abfd, extdyn, &dyn);
1550
1551 if (dyn.d_tag == DT_NULL)
1552 break;
1553
1554 stringp = false;
1555 switch (dyn.d_tag)
1556 {
1557 default:
1558 if (bed->elf_backend_get_target_dtag)
1559 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1560
1561 if (!strcmp (name, ""))
1562 {
1563 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1564 name = ab;
1565 }
1566 break;
1567
1568 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1569 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1570 case DT_PLTGOT: name = "PLTGOT"; break;
1571 case DT_HASH: name = "HASH"; break;
1572 case DT_STRTAB: name = "STRTAB"; break;
1573 case DT_SYMTAB: name = "SYMTAB"; break;
1574 case DT_RELA: name = "RELA"; break;
1575 case DT_RELASZ: name = "RELASZ"; break;
1576 case DT_RELAENT: name = "RELAENT"; break;
1577 case DT_STRSZ: name = "STRSZ"; break;
1578 case DT_SYMENT: name = "SYMENT"; break;
1579 case DT_INIT: name = "INIT"; break;
1580 case DT_FINI: name = "FINI"; break;
1581 case DT_SONAME: name = "SONAME"; stringp = true; break;
1582 case DT_RPATH: name = "RPATH"; stringp = true; break;
1583 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1584 case DT_REL: name = "REL"; break;
1585 case DT_RELSZ: name = "RELSZ"; break;
1586 case DT_RELENT: name = "RELENT"; break;
1587 case DT_RELR: name = "RELR"; break;
1588 case DT_RELRSZ: name = "RELRSZ"; break;
1589 case DT_RELRENT: name = "RELRENT"; break;
1590 case DT_PLTREL: name = "PLTREL"; break;
1591 case DT_DEBUG: name = "DEBUG"; break;
1592 case DT_TEXTREL: name = "TEXTREL"; break;
1593 case DT_JMPREL: name = "JMPREL"; break;
1594 case DT_BIND_NOW: name = "BIND_NOW"; break;
1595 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1596 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1597 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1598 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1599 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1600 case DT_FLAGS: name = "FLAGS"; break;
1601 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1602 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1603 case DT_CHECKSUM: name = "CHECKSUM"; break;
1604 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1605 case DT_MOVEENT: name = "MOVEENT"; break;
1606 case DT_MOVESZ: name = "MOVESZ"; break;
1607 case DT_FEATURE: name = "FEATURE"; break;
1608 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1609 case DT_SYMINSZ: name = "SYMINSZ"; break;
1610 case DT_SYMINENT: name = "SYMINENT"; break;
1611 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1612 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1613 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1614 case DT_PLTPAD: name = "PLTPAD"; break;
1615 case DT_MOVETAB: name = "MOVETAB"; break;
1616 case DT_SYMINFO: name = "SYMINFO"; break;
1617 case DT_RELACOUNT: name = "RELACOUNT"; break;
1618 case DT_RELCOUNT: name = "RELCOUNT"; break;
1619 case DT_FLAGS_1: name = "FLAGS_1"; break;
1620 case DT_VERSYM: name = "VERSYM"; break;
1621 case DT_VERDEF: name = "VERDEF"; break;
1622 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1623 case DT_VERNEED: name = "VERNEED"; break;
1624 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1625 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1626 case DT_USED: name = "USED"; break;
1627 case DT_FILTER: name = "FILTER"; stringp = true; break;
1628 case DT_GNU_HASH: name = "GNU_HASH"; break;
1629 }
1630
1631 fprintf (f, " %-20s ", name);
1632 if (! stringp)
1633 {
1634 fprintf (f, "0x");
1635 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1636 }
1637 else
1638 {
1639 const char *string;
1640 unsigned int tagv = dyn.d_un.d_val;
1641
1642 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1643 if (string == NULL)
1644 goto error_return;
1645 fprintf (f, "%s", string);
1646 }
1647 fprintf (f, "\n");
1648 }
1649
1650 _bfd_elf_munmap_section_contents (s, dynbuf);
1651 dynbuf = NULL;
1652 }
1653
1654 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1655 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1656 {
1657 if (! _bfd_elf_slurp_version_tables (abfd, false))
1658 return false;
1659 }
1660
1661 if (elf_dynverdef (abfd) != 0)
1662 {
1663 Elf_Internal_Verdef *t;
1664
1665 fprintf (f, _("\nVersion definitions:\n"));
1666 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1667 {
1668 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1669 t->vd_flags, t->vd_hash,
1670 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1671 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1672 {
1673 Elf_Internal_Verdaux *a;
1674
1675 fprintf (f, "\t");
1676 for (a = t->vd_auxptr->vda_nextptr;
1677 a != NULL;
1678 a = a->vda_nextptr)
1679 fprintf (f, "%s ",
1680 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1681 fprintf (f, "\n");
1682 }
1683 }
1684 }
1685
1686 if (elf_dynverref (abfd) != 0)
1687 {
1688 Elf_Internal_Verneed *t;
1689
1690 fprintf (f, _("\nVersion References:\n"));
1691 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1692 {
1693 Elf_Internal_Vernaux *a;
1694
1695 fprintf (f, _(" required from %s:\n"),
1696 t->vn_filename ? t->vn_filename : "<corrupt>");
1697 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1698 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1699 a->vna_flags, a->vna_other,
1700 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1701 }
1702 }
1703
1704 return true;
1705
1706 error_return:
1707 _bfd_elf_munmap_section_contents (s, dynbuf);
1708 return false;
1709 }
1710
1711 /* Find the file offset corresponding to VMA by using the program
1712 headers. */
1713
1714 static file_ptr
1715 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1716 size_t size, size_t *max_size_p)
1717 {
1718 Elf_Internal_Phdr *seg;
1719 size_t i;
1720
1721 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1722 if (seg->p_type == PT_LOAD
1723 && vma >= (seg->p_vaddr & -seg->p_align)
1724 && vma + size <= seg->p_vaddr + seg->p_filesz)
1725 {
1726 if (max_size_p)
1727 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1728 return vma - seg->p_vaddr + seg->p_offset;
1729 }
1730
1731 if (max_size_p)
1732 *max_size_p = 0;
1733 bfd_set_error (bfd_error_invalid_operation);
1734 return (file_ptr) -1;
1735 }
1736
1737 /* Convert hash table to internal form. */
1738
1739 static bfd_vma *
1740 get_hash_table_data (bfd *abfd, bfd_size_type number,
1741 unsigned int ent_size, bfd_size_type filesize)
1742 {
1743 unsigned char *e_data = NULL;
1744 bfd_vma *i_data = NULL;
1745 bfd_size_type size;
1746 void *e_data_addr;
1747 size_t e_data_size ATTRIBUTE_UNUSED;
1748
1749 if (ent_size != 4 && ent_size != 8)
1750 return NULL;
1751
1752 if ((size_t) number != number)
1753 {
1754 bfd_set_error (bfd_error_file_too_big);
1755 return NULL;
1756 }
1757
1758 size = ent_size * number;
1759 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1760 attempting to allocate memory when the read is bound to fail. */
1761 if (size > filesize
1762 || number >= ~(size_t) 0 / ent_size
1763 || number >= ~(size_t) 0 / sizeof (*i_data))
1764 {
1765 bfd_set_error (bfd_error_file_too_big);
1766 return NULL;
1767 }
1768
1769 e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1770 if (e_data == NULL)
1771 return NULL;
1772
1773 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1774 if (i_data == NULL)
1775 {
1776 _bfd_munmap_temporary (e_data_addr, e_data_size);
1777 return NULL;
1778 }
1779
1780 if (ent_size == 4)
1781 while (number--)
1782 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1783 else
1784 while (number--)
1785 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1786
1787 _bfd_munmap_temporary (e_data_addr, e_data_size);
1788 return i_data;
1789 }
1790
1791 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1792 support DT_MIPS_XHASH? */
1793 #define DT_MIPS_XHASH 0x70000036
1794
1795 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1796
1797 bool
1798 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1799 Elf_Internal_Phdr *phdrs, size_t phnum,
1800 bfd_size_type filesize)
1801 {
1802 bfd_byte *extdyn, *extdynend;
1803 size_t extdynsize;
1804 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1805 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1806 Elf_Internal_Sym *);
1807 Elf_Internal_Dyn dyn;
1808 bfd_vma dt_hash = 0;
1809 bfd_vma dt_gnu_hash = 0;
1810 bfd_vma dt_mips_xhash = 0;
1811 bfd_vma dt_strtab = 0;
1812 bfd_vma dt_symtab = 0;
1813 size_t dt_strsz = 0;
1814 bfd_vma dt_versym = 0;
1815 bfd_vma dt_verdef = 0;
1816 bfd_vma dt_verneed = 0;
1817 bfd_byte *dynbuf = NULL;
1818 char *strbuf = NULL;
1819 bfd_vma *gnubuckets = NULL;
1820 bfd_vma *gnuchains = NULL;
1821 bfd_vma *mipsxlat = NULL;
1822 file_ptr saved_filepos, filepos;
1823 bool res = false;
1824 size_t amt;
1825 bfd_byte *esymbuf = NULL, *esym;
1826 bfd_size_type symcount;
1827 Elf_Internal_Sym *isymbuf = NULL;
1828 Elf_Internal_Sym *isym, *isymend;
1829 bfd_byte *versym = NULL;
1830 bfd_byte *verdef = NULL;
1831 bfd_byte *verneed = NULL;
1832 size_t verdef_size = 0;
1833 size_t verneed_size = 0;
1834 size_t extsym_size;
1835 const struct elf_backend_data *bed;
1836 void *dynbuf_addr = NULL;
1837 void *esymbuf_addr = NULL;
1838 size_t dynbuf_size = 0;
1839 size_t esymbuf_size = 0;
1840
1841 /* Return TRUE if symbol table is bad. */
1842 if (elf_bad_symtab (abfd))
1843 return true;
1844
1845 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
1846 if (elf_tdata (abfd)->dt_strtab != NULL)
1847 return true;
1848
1849 bed = get_elf_backend_data (abfd);
1850
1851 /* Save file position for elf_object_p. */
1852 saved_filepos = bfd_tell (abfd);
1853
1854 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1855 goto error_return;
1856
1857 dynbuf_size = phdr->p_filesz;
1858 dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1859 if (dynbuf == NULL)
1860 goto error_return;
1861
1862 extsym_size = bed->s->sizeof_sym;
1863 extdynsize = bed->s->sizeof_dyn;
1864 swap_dyn_in = bed->s->swap_dyn_in;
1865
1866 extdyn = dynbuf;
1867 if (phdr->p_filesz < extdynsize)
1868 goto error_return;
1869 extdynend = extdyn + phdr->p_filesz;
1870 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1871 {
1872 swap_dyn_in (abfd, extdyn, &dyn);
1873
1874 if (dyn.d_tag == DT_NULL)
1875 break;
1876
1877 switch (dyn.d_tag)
1878 {
1879 case DT_HASH:
1880 dt_hash = dyn.d_un.d_val;
1881 break;
1882 case DT_GNU_HASH:
1883 if (bed->elf_machine_code != EM_MIPS
1884 && bed->elf_machine_code != EM_MIPS_RS3_LE)
1885 dt_gnu_hash = dyn.d_un.d_val;
1886 break;
1887 case DT_STRTAB:
1888 dt_strtab = dyn.d_un.d_val;
1889 break;
1890 case DT_SYMTAB:
1891 dt_symtab = dyn.d_un.d_val;
1892 break;
1893 case DT_STRSZ:
1894 dt_strsz = dyn.d_un.d_val;
1895 break;
1896 case DT_SYMENT:
1897 if (dyn.d_un.d_val != extsym_size)
1898 goto error_return;
1899 break;
1900 case DT_VERSYM:
1901 dt_versym = dyn.d_un.d_val;
1902 break;
1903 case DT_VERDEF:
1904 dt_verdef = dyn.d_un.d_val;
1905 break;
1906 case DT_VERNEED:
1907 dt_verneed = dyn.d_un.d_val;
1908 break;
1909 default:
1910 if (dyn.d_tag == DT_MIPS_XHASH
1911 && (bed->elf_machine_code == EM_MIPS
1912 || bed->elf_machine_code == EM_MIPS_RS3_LE))
1913 {
1914 dt_gnu_hash = dyn.d_un.d_val;
1915 dt_mips_xhash = dyn.d_un.d_val;
1916 }
1917 break;
1918 }
1919 }
1920
1921 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1922 segment. */
1923 if ((!dt_hash && !dt_gnu_hash)
1924 || !dt_strtab
1925 || !dt_symtab
1926 || !dt_strsz)
1927 goto error_return;
1928
1929 /* Get dynamic string table. */
1930 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1931 if (filepos == (file_ptr) -1
1932 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1933 goto error_return;
1934
1935 /* Dynamic string table must be valid until ABFD is closed. */
1936 strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
1937 if (strbuf == NULL)
1938 goto error_return;
1939 if (strbuf[dt_strsz - 1] != 0)
1940 {
1941 /* It is an error if a string table is't terminated. */
1942 _bfd_error_handler
1943 /* xgettext:c-format */
1944 (_("%pB: DT_STRTAB table is corrupt"), abfd);
1945 strbuf[dt_strsz - 1] = 0;
1946 }
1947
1948 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
1949 DT_HASH since it is simpler than DT_GNU_HASH. */
1950 if (dt_hash)
1951 {
1952 unsigned char nb[16];
1953 unsigned int hash_ent_size;
1954
1955 switch (bed->elf_machine_code)
1956 {
1957 case EM_ALPHA:
1958 case EM_S390:
1959 case EM_S390_OLD:
1960 if (bed->s->elfclass == ELFCLASS64)
1961 {
1962 hash_ent_size = 8;
1963 break;
1964 }
1965 /* FALLTHROUGH */
1966 default:
1967 hash_ent_size = 4;
1968 break;
1969 }
1970
1971 filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
1972 NULL);
1973 if (filepos == (file_ptr) -1
1974 || bfd_seek (abfd, filepos, SEEK_SET) != 0
1975 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
1976 goto error_return;
1977
1978 /* The number of dynamic symbol table entries equals the number
1979 of chains. */
1980 if (hash_ent_size == 8)
1981 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
1982 else
1983 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
1984 }
1985 else
1986 {
1987 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
1988 bindings are in hash table. Since in dynamic symbol table,
1989 all symbols with STB_LOCAL binding are placed before symbols
1990 with other bindings and all undefined symbols are placed
1991 before defined ones, the highest symbol index in DT_GNU_HASH
1992 is the highest dynamic symbol table index. */
1993 unsigned char nb[16];
1994 bfd_vma ngnubuckets;
1995 bfd_vma gnusymidx;
1996 size_t i, ngnuchains;
1997 bfd_vma maxchain = 0xffffffff, bitmaskwords;
1998 bfd_vma buckets_vma;
1999
2000 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2001 sizeof (nb), NULL);
2002 if (filepos == (file_ptr) -1
2003 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2004 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2005 goto error_return;
2006
2007 ngnubuckets = bfd_get_32 (abfd, nb);
2008 gnusymidx = bfd_get_32 (abfd, nb + 4);
2009 bitmaskwords = bfd_get_32 (abfd, nb + 8);
2010 buckets_vma = dt_gnu_hash + 16;
2011 if (bed->s->elfclass == ELFCLASS32)
2012 buckets_vma += bitmaskwords * 4;
2013 else
2014 buckets_vma += bitmaskwords * 8;
2015 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2016 if (filepos == (file_ptr) -1
2017 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2018 goto error_return;
2019
2020 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2021 if (gnubuckets == NULL)
2022 goto error_return;
2023
2024 for (i = 0; i < ngnubuckets; i++)
2025 if (gnubuckets[i] != 0)
2026 {
2027 if (gnubuckets[i] < gnusymidx)
2028 goto error_return;
2029
2030 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2031 maxchain = gnubuckets[i];
2032 }
2033
2034 if (maxchain == 0xffffffff)
2035 {
2036 symcount = 0;
2037 goto empty_gnu_hash;
2038 }
2039
2040 maxchain -= gnusymidx;
2041 filepos = offset_from_vma (phdrs, phnum,
2042 buckets_vma + 4 * (ngnubuckets + maxchain),
2043 4, NULL);
2044 if (filepos == (file_ptr) -1
2045 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2046 goto error_return;
2047
2048 do
2049 {
2050 if (bfd_read (nb, 4, abfd) != 4)
2051 goto error_return;
2052 ++maxchain;
2053 if (maxchain == 0)
2054 goto error_return;
2055 }
2056 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2057
2058 filepos = offset_from_vma (phdrs, phnum,
2059 buckets_vma + 4 * ngnubuckets,
2060 4, NULL);
2061 if (filepos == (file_ptr) -1
2062 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2063 goto error_return;
2064
2065 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2066 if (gnuchains == NULL)
2067 goto error_return;
2068 ngnuchains = maxchain;
2069
2070 if (dt_mips_xhash)
2071 {
2072 filepos = offset_from_vma (phdrs, phnum,
2073 buckets_vma + 4 * (ngnubuckets + maxchain),
2074 4, NULL);
2075 if (filepos == (file_ptr) -1
2076 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2077 goto error_return;
2078
2079 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2080 if (mipsxlat == NULL)
2081 goto error_return;
2082 }
2083
2084 symcount = 0;
2085 for (i = 0; i < ngnubuckets; ++i)
2086 if (gnubuckets[i] != 0)
2087 {
2088 bfd_vma si = gnubuckets[i];
2089 bfd_vma off = si - gnusymidx;
2090 do
2091 {
2092 if (mipsxlat)
2093 {
2094 if (mipsxlat[off] >= symcount)
2095 symcount = mipsxlat[off] + 1;
2096 }
2097 else
2098 {
2099 if (si >= symcount)
2100 symcount = si + 1;
2101 }
2102 si++;
2103 }
2104 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2105 }
2106 }
2107
2108 /* Swap in dynamic symbol table. */
2109 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2110 {
2111 bfd_set_error (bfd_error_file_too_big);
2112 goto error_return;
2113 }
2114
2115 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2116 if (filepos == (file_ptr) -1
2117 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2118 goto error_return;
2119 esymbuf_size = amt;
2120 esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2121 &esymbuf_addr, &esymbuf_size);
2122 if (esymbuf == NULL)
2123 goto error_return;
2124
2125 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2126 {
2127 bfd_set_error (bfd_error_file_too_big);
2128 goto error_return;
2129 }
2130
2131 /* Dynamic symbol table must be valid until ABFD is closed. */
2132 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2133 if (isymbuf == NULL)
2134 goto error_return;
2135
2136 swap_symbol_in = bed->s->swap_symbol_in;
2137
2138 /* Convert the symbols to internal form. */
2139 isymend = isymbuf + symcount;
2140 for (esym = esymbuf, isym = isymbuf;
2141 isym < isymend;
2142 esym += extsym_size, isym++)
2143 if (!swap_symbol_in (abfd, esym, NULL, isym)
2144 || isym->st_name >= dt_strsz)
2145 {
2146 bfd_set_error (bfd_error_invalid_operation);
2147 goto error_return;
2148 }
2149
2150 if (dt_versym)
2151 {
2152 /* Swap in DT_VERSYM. */
2153 if (_bfd_mul_overflow (symcount, 2, &amt))
2154 {
2155 bfd_set_error (bfd_error_file_too_big);
2156 goto error_return;
2157 }
2158
2159 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2160 if (filepos == (file_ptr) -1
2161 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2162 goto error_return;
2163
2164 /* DT_VERSYM info must be valid until ABFD is closed. */
2165 versym = _bfd_mmap_persistent (abfd, amt);
2166
2167 if (dt_verdef)
2168 {
2169 /* Read in DT_VERDEF. */
2170 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2171 0, &verdef_size);
2172 if (filepos == (file_ptr) -1
2173 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2174 goto error_return;
2175
2176 /* DT_VERDEF info must be valid until ABFD is closed. */
2177 verdef = _bfd_mmap_persistent (abfd, verdef_size);
2178 }
2179
2180 if (dt_verneed)
2181 {
2182 /* Read in DT_VERNEED. */
2183 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2184 0, &verneed_size);
2185 if (filepos == (file_ptr) -1
2186 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2187 goto error_return;
2188
2189 /* DT_VERNEED info must be valid until ABFD is closed. */
2190 verneed = _bfd_mmap_persistent (abfd, verneed_size);
2191 }
2192 }
2193
2194 empty_gnu_hash:
2195 elf_tdata (abfd)->dt_strtab = strbuf;
2196 elf_tdata (abfd)->dt_strsz = dt_strsz;
2197 elf_tdata (abfd)->dt_symtab = isymbuf;
2198 elf_tdata (abfd)->dt_symtab_count = symcount;
2199 elf_tdata (abfd)->dt_versym = versym;
2200 elf_tdata (abfd)->dt_verdef = verdef;
2201 elf_tdata (abfd)->dt_verneed = verneed;
2202 elf_tdata (abfd)->dt_verdef_count
2203 = verdef_size / sizeof (Elf_External_Verdef);
2204 elf_tdata (abfd)->dt_verneed_count
2205 = verneed_size / sizeof (Elf_External_Verneed);
2206
2207 res = true;
2208
2209 error_return:
2210 /* Restore file position for elf_object_p. */
2211 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2212 res = false;
2213 _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2214 _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2215 free (gnubuckets);
2216 free (gnuchains);
2217 free (mipsxlat);
2218 return res;
2219 }
2220
2221 /* Reconstruct section from dynamic symbol. */
2222
2223 asection *
2224 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2225 Elf_Internal_Sym *isym)
2226 {
2227 asection *sec;
2228 flagword flags;
2229
2230 if (!elf_use_dt_symtab_p (abfd))
2231 return NULL;
2232
2233 flags = SEC_ALLOC | SEC_LOAD;
2234 switch (ELF_ST_TYPE (isym->st_info))
2235 {
2236 case STT_FUNC:
2237 case STT_GNU_IFUNC:
2238 sec = bfd_get_section_by_name (abfd, ".text");
2239 if (sec == NULL)
2240 sec = bfd_make_section_with_flags (abfd,
2241 ".text",
2242 flags | SEC_CODE);
2243 break;
2244 case STT_COMMON:
2245 sec = bfd_com_section_ptr;
2246 break;
2247 case STT_OBJECT:
2248 sec = bfd_get_section_by_name (abfd, ".data");
2249 if (sec == NULL)
2250 sec = bfd_make_section_with_flags (abfd,
2251 ".data",
2252 flags | SEC_DATA);
2253 break;
2254 case STT_TLS:
2255 sec = bfd_get_section_by_name (abfd, ".tdata");
2256 if (sec == NULL)
2257 sec = bfd_make_section_with_flags (abfd,
2258 ".tdata",
2259 (flags
2260 | SEC_DATA
2261 | SEC_THREAD_LOCAL));
2262 break;
2263 default:
2264 sec = bfd_abs_section_ptr;
2265 break;
2266 }
2267
2268 return sec;
2269 }
2270
2271 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2272 and return symbol version for symbol version itself. */
2273
2274 const char *
2275 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2276 bool base_p,
2277 bool *hidden)
2278 {
2279 const char *version_string = NULL;
2280 if ((elf_dynversym (abfd) != 0
2281 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2282 || (elf_tdata (abfd)->dt_versym != NULL
2283 && (elf_tdata (abfd)->dt_verdef != NULL
2284 || elf_tdata (abfd)->dt_verneed != NULL)))
2285 {
2286 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2287
2288 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2289 vernum &= VERSYM_VERSION;
2290
2291 if (vernum == 0)
2292 version_string = "";
2293 else if (vernum == 1
2294 && (vernum > elf_tdata (abfd)->cverdefs
2295 || (elf_tdata (abfd)->verdef[0].vd_flags
2296 == VER_FLG_BASE)))
2297 version_string = base_p ? "Base" : "";
2298 else if (vernum <= elf_tdata (abfd)->cverdefs)
2299 {
2300 const char *nodename
2301 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2302 version_string = "";
2303 if (base_p
2304 || nodename == NULL
2305 || symbol->name == NULL
2306 || strcmp (symbol->name, nodename) != 0)
2307 version_string = nodename;
2308 }
2309 else
2310 {
2311 Elf_Internal_Verneed *t;
2312
2313 version_string = _("<corrupt>");
2314 for (t = elf_tdata (abfd)->verref;
2315 t != NULL;
2316 t = t->vn_nextref)
2317 {
2318 Elf_Internal_Vernaux *a;
2319
2320 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2321 {
2322 if (a->vna_other == vernum)
2323 {
2324 *hidden = true;
2325 version_string = a->vna_nodename;
2326 break;
2327 }
2328 }
2329 }
2330 }
2331 }
2332 return version_string;
2333 }
2334
2335 /* Display ELF-specific fields of a symbol. */
2336
2337 void
2338 bfd_elf_print_symbol (bfd *abfd,
2339 void *filep,
2340 asymbol *symbol,
2341 bfd_print_symbol_type how)
2342 {
2343 FILE *file = (FILE *) filep;
2344 const char *symname = (symbol->name != bfd_symbol_error_name
2345 ? symbol->name : _("<corrupt>"));
2346
2347 switch (how)
2348 {
2349 case bfd_print_symbol_name:
2350 fprintf (file, "%s", symname);
2351 break;
2352 case bfd_print_symbol_more:
2353 fprintf (file, "elf ");
2354 bfd_fprintf_vma (abfd, file, symbol->value);
2355 fprintf (file, " %x", symbol->flags);
2356 break;
2357 case bfd_print_symbol_all:
2358 {
2359 const char *section_name;
2360 const char *name = NULL;
2361 const struct elf_backend_data *bed;
2362 unsigned char st_other;
2363 bfd_vma val;
2364 const char *version_string;
2365 bool hidden;
2366
2367 section_name = symbol->section ? symbol->section->name : "(*none*)";
2368
2369 bed = get_elf_backend_data (abfd);
2370 if (bed->elf_backend_print_symbol_all)
2371 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2372
2373 if (name != NULL)
2374 symname = name;
2375 else
2376 bfd_print_symbol_vandf (abfd, file, symbol);
2377
2378 fprintf (file, " %s\t", section_name);
2379 /* Print the "other" value for a symbol. For common symbols,
2380 we've already printed the size; now print the alignment.
2381 For other symbols, we have no specified alignment, and
2382 we've printed the address; now print the size. */
2383 if (symbol->section && bfd_is_com_section (symbol->section))
2384 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2385 else
2386 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2387 bfd_fprintf_vma (abfd, file, val);
2388
2389 /* If we have version information, print it. */
2390 version_string = _bfd_elf_get_symbol_version_string (abfd,
2391 symbol,
2392 true,
2393 &hidden);
2394 if (version_string)
2395 {
2396 if (!hidden)
2397 fprintf (file, " %-11s", version_string);
2398 else
2399 {
2400 int i;
2401
2402 fprintf (file, " (%s)", version_string);
2403 for (i = 10 - strlen (version_string); i > 0; --i)
2404 putc (' ', file);
2405 }
2406 }
2407
2408 /* If the st_other field is not zero, print it. */
2409 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2410
2411 switch (st_other)
2412 {
2413 case 0: break;
2414 case STV_INTERNAL: fprintf (file, " .internal"); break;
2415 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2416 case STV_PROTECTED: fprintf (file, " .protected"); break;
2417 default:
2418 /* Some other non-defined flags are also present, so print
2419 everything hex. */
2420 fprintf (file, " 0x%02x", (unsigned int) st_other);
2421 }
2422
2423 fprintf (file, " %s", symname);
2424 }
2425 break;
2426 }
2427 }
2428 \f
2429 /* ELF .o/exec file reading */
2430
2431 /* Create a new bfd section from an ELF section header. */
2432
2433 bool
2434 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2435 {
2436 Elf_Internal_Shdr *hdr;
2437 Elf_Internal_Ehdr *ehdr;
2438 const struct elf_backend_data *bed;
2439 const char *name;
2440 bool ret = true;
2441
2442 if (shindex >= elf_numsections (abfd))
2443 return false;
2444
2445 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2446 sh_link or sh_info. Detect this here, by refusing to load a
2447 section that we are already in the process of loading. */
2448 if (elf_tdata (abfd)->being_created[shindex])
2449 {
2450 _bfd_error_handler
2451 (_("%pB: warning: loop in section dependencies detected"), abfd);
2452 return false;
2453 }
2454 elf_tdata (abfd)->being_created[shindex] = true;
2455
2456 hdr = elf_elfsections (abfd)[shindex];
2457 ehdr = elf_elfheader (abfd);
2458 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2459 hdr->sh_name);
2460 if (name == NULL)
2461 goto fail;
2462
2463 bed = get_elf_backend_data (abfd);
2464 switch (hdr->sh_type)
2465 {
2466 case SHT_NULL:
2467 /* Inactive section. Throw it away. */
2468 goto success;
2469
2470 case SHT_PROGBITS: /* Normal section with contents. */
2471 case SHT_NOBITS: /* .bss section. */
2472 case SHT_HASH: /* .hash section. */
2473 case SHT_NOTE: /* .note section. */
2474 case SHT_INIT_ARRAY: /* .init_array section. */
2475 case SHT_FINI_ARRAY: /* .fini_array section. */
2476 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2477 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2478 case SHT_GNU_HASH: /* .gnu.hash section. */
2479 case SHT_GNU_SFRAME: /* .sframe section. */
2480 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2481 goto success;
2482
2483 case SHT_DYNAMIC: /* Dynamic linking information. */
2484 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2485 goto fail;
2486
2487 if (hdr->sh_link > elf_numsections (abfd))
2488 {
2489 /* PR 10478: Accept Solaris binaries with a sh_link field
2490 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2491 switch (bfd_get_arch (abfd))
2492 {
2493 case bfd_arch_i386:
2494 case bfd_arch_sparc:
2495 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2496 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2497 break;
2498 /* Otherwise fall through. */
2499 default:
2500 goto fail;
2501 }
2502 }
2503 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2504 goto fail;
2505 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2506 {
2507 Elf_Internal_Shdr *dynsymhdr;
2508
2509 /* The shared libraries distributed with hpux11 have a bogus
2510 sh_link field for the ".dynamic" section. Find the
2511 string table for the ".dynsym" section instead. */
2512 if (elf_dynsymtab (abfd) != 0)
2513 {
2514 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2515 hdr->sh_link = dynsymhdr->sh_link;
2516 }
2517 else
2518 {
2519 unsigned int i, num_sec;
2520
2521 num_sec = elf_numsections (abfd);
2522 for (i = 1; i < num_sec; i++)
2523 {
2524 dynsymhdr = elf_elfsections (abfd)[i];
2525 if (dynsymhdr->sh_type == SHT_DYNSYM)
2526 {
2527 hdr->sh_link = dynsymhdr->sh_link;
2528 break;
2529 }
2530 }
2531 }
2532 }
2533 goto success;
2534
2535 case SHT_SYMTAB: /* A symbol table. */
2536 if (elf_onesymtab (abfd) == shindex)
2537 goto success;
2538
2539 if (hdr->sh_entsize != bed->s->sizeof_sym)
2540 goto fail;
2541
2542 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2543 {
2544 if (hdr->sh_size != 0)
2545 goto fail;
2546 /* Some assemblers erroneously set sh_info to one with a
2547 zero sh_size. ld sees this as a global symbol count
2548 of (unsigned) -1. Fix it here. */
2549 hdr->sh_info = 0;
2550 goto success;
2551 }
2552
2553 /* PR 18854: A binary might contain more than one symbol table.
2554 Unusual, but possible. Warn, but continue. */
2555 if (elf_onesymtab (abfd) != 0)
2556 {
2557 _bfd_error_handler
2558 /* xgettext:c-format */
2559 (_("%pB: warning: multiple symbol tables detected"
2560 " - ignoring the table in section %u"),
2561 abfd, shindex);
2562 goto success;
2563 }
2564 elf_onesymtab (abfd) = shindex;
2565 elf_symtab_hdr (abfd) = *hdr;
2566 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2567 abfd->flags |= HAS_SYMS;
2568
2569 /* Sometimes a shared object will map in the symbol table. If
2570 SHF_ALLOC is set, and this is a shared object, then we also
2571 treat this section as a BFD section. We can not base the
2572 decision purely on SHF_ALLOC, because that flag is sometimes
2573 set in a relocatable object file, which would confuse the
2574 linker. */
2575 if ((hdr->sh_flags & SHF_ALLOC) != 0
2576 && (abfd->flags & DYNAMIC) != 0
2577 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2578 shindex))
2579 goto fail;
2580
2581 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2582 can't read symbols without that section loaded as well. It
2583 is most likely specified by the next section header. */
2584 {
2585 elf_section_list * entry;
2586 unsigned int i, num_sec;
2587
2588 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2589 if (entry->hdr.sh_link == shindex)
2590 goto success;
2591
2592 num_sec = elf_numsections (abfd);
2593 for (i = shindex + 1; i < num_sec; i++)
2594 {
2595 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2596
2597 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2598 && hdr2->sh_link == shindex)
2599 break;
2600 }
2601
2602 if (i == num_sec)
2603 for (i = 1; i < shindex; i++)
2604 {
2605 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2606
2607 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2608 && hdr2->sh_link == shindex)
2609 break;
2610 }
2611
2612 if (i != shindex)
2613 ret = bfd_section_from_shdr (abfd, i);
2614 /* else FIXME: we have failed to find the symbol table.
2615 Should we issue an error? */
2616 goto success;
2617 }
2618
2619 case SHT_DYNSYM: /* A dynamic symbol table. */
2620 if (elf_dynsymtab (abfd) == shindex)
2621 goto success;
2622
2623 if (hdr->sh_entsize != bed->s->sizeof_sym)
2624 goto fail;
2625
2626 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2627 {
2628 if (hdr->sh_size != 0)
2629 goto fail;
2630
2631 /* Some linkers erroneously set sh_info to one with a
2632 zero sh_size. ld sees this as a global symbol count
2633 of (unsigned) -1. Fix it here. */
2634 hdr->sh_info = 0;
2635 goto success;
2636 }
2637
2638 /* PR 18854: A binary might contain more than one dynamic symbol table.
2639 Unusual, but possible. Warn, but continue. */
2640 if (elf_dynsymtab (abfd) != 0)
2641 {
2642 _bfd_error_handler
2643 /* xgettext:c-format */
2644 (_("%pB: warning: multiple dynamic symbol tables detected"
2645 " - ignoring the table in section %u"),
2646 abfd, shindex);
2647 goto success;
2648 }
2649 elf_dynsymtab (abfd) = shindex;
2650 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2651 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2652 abfd->flags |= HAS_SYMS;
2653
2654 /* Besides being a symbol table, we also treat this as a regular
2655 section, so that objcopy can handle it. */
2656 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2657 goto success;
2658
2659 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2660 {
2661 elf_section_list * entry;
2662
2663 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2664 if (entry->ndx == shindex)
2665 goto success;
2666
2667 entry = bfd_alloc (abfd, sizeof (*entry));
2668 if (entry == NULL)
2669 goto fail;
2670 entry->ndx = shindex;
2671 entry->hdr = * hdr;
2672 entry->next = elf_symtab_shndx_list (abfd);
2673 elf_symtab_shndx_list (abfd) = entry;
2674 elf_elfsections (abfd)[shindex] = & entry->hdr;
2675 goto success;
2676 }
2677
2678 case SHT_STRTAB: /* A string table. */
2679 if (hdr->bfd_section != NULL)
2680 goto success;
2681
2682 if (ehdr->e_shstrndx == shindex)
2683 {
2684 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2685 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2686 goto success;
2687 }
2688
2689 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2690 {
2691 symtab_strtab:
2692 elf_tdata (abfd)->strtab_hdr = *hdr;
2693 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2694 goto success;
2695 }
2696
2697 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2698 {
2699 dynsymtab_strtab:
2700 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2701 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2702 elf_elfsections (abfd)[shindex] = hdr;
2703 /* We also treat this as a regular section, so that objcopy
2704 can handle it. */
2705 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2706 shindex);
2707 goto success;
2708 }
2709
2710 /* If the string table isn't one of the above, then treat it as a
2711 regular section. We need to scan all the headers to be sure,
2712 just in case this strtab section appeared before the above. */
2713 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2714 {
2715 unsigned int i, num_sec;
2716
2717 num_sec = elf_numsections (abfd);
2718 for (i = 1; i < num_sec; i++)
2719 {
2720 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2721 if (hdr2->sh_link == shindex)
2722 {
2723 /* Prevent endless recursion on broken objects. */
2724 if (i == shindex)
2725 goto fail;
2726 if (! bfd_section_from_shdr (abfd, i))
2727 goto fail;
2728 if (elf_onesymtab (abfd) == i)
2729 goto symtab_strtab;
2730 if (elf_dynsymtab (abfd) == i)
2731 goto dynsymtab_strtab;
2732 }
2733 }
2734 }
2735 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2736 goto success;
2737
2738 case SHT_REL:
2739 case SHT_RELA:
2740 case SHT_RELR:
2741 /* *These* do a lot of work -- but build no sections! */
2742 {
2743 asection *target_sect;
2744 Elf_Internal_Shdr *hdr2, **p_hdr;
2745 unsigned int num_sec = elf_numsections (abfd);
2746 struct bfd_elf_section_data *esdt;
2747 bfd_size_type size;
2748
2749 if (hdr->sh_type == SHT_REL)
2750 size = bed->s->sizeof_rel;
2751 else if (hdr->sh_type == SHT_RELA)
2752 size = bed->s->sizeof_rela;
2753 else
2754 size = bed->s->arch_size / 8;
2755 if (hdr->sh_entsize != size)
2756 goto fail;
2757
2758 /* Check for a bogus link to avoid crashing. */
2759 if (hdr->sh_link >= num_sec)
2760 {
2761 _bfd_error_handler
2762 /* xgettext:c-format */
2763 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2764 abfd, hdr->sh_link, name, shindex);
2765 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2766 goto success;
2767 }
2768
2769 /* Get the symbol table. */
2770 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2771 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2772 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2773 goto fail;
2774
2775 /* If this is an alloc section in an executable or shared
2776 library, or the reloc section does not use the main symbol
2777 table we don't treat it as a reloc section. BFD can't
2778 adequately represent such a section, so at least for now,
2779 we don't try. We just present it as a normal section. We
2780 also can't use it as a reloc section if it points to the
2781 null section, an invalid section, another reloc section, or
2782 its sh_link points to the null section. */
2783 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2784 && (hdr->sh_flags & SHF_ALLOC) != 0)
2785 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2786 || hdr->sh_type == SHT_RELR
2787 || hdr->sh_link == SHN_UNDEF
2788 || hdr->sh_link != elf_onesymtab (abfd)
2789 || hdr->sh_info == SHN_UNDEF
2790 || hdr->sh_info >= num_sec
2791 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2792 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2793 {
2794 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2795 goto success;
2796 }
2797
2798 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2799 goto fail;
2800
2801 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2802 if (target_sect == NULL)
2803 goto fail;
2804
2805 esdt = elf_section_data (target_sect);
2806 if (hdr->sh_type == SHT_RELA)
2807 p_hdr = &esdt->rela.hdr;
2808 else
2809 p_hdr = &esdt->rel.hdr;
2810
2811 /* PR 17512: file: 0b4f81b7.
2812 Also see PR 24456, for a file which deliberately has two reloc
2813 sections. */
2814 if (*p_hdr != NULL)
2815 {
2816 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2817 {
2818 _bfd_error_handler
2819 /* xgettext:c-format */
2820 (_("%pB: warning: secondary relocation section '%s' "
2821 "for section %pA found - ignoring"),
2822 abfd, name, target_sect);
2823 }
2824 else
2825 esdt->has_secondary_relocs = true;
2826 goto success;
2827 }
2828
2829 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2830 if (hdr2 == NULL)
2831 goto fail;
2832 *hdr2 = *hdr;
2833 *p_hdr = hdr2;
2834 elf_elfsections (abfd)[shindex] = hdr2;
2835 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2836 * bed->s->int_rels_per_ext_rel);
2837 target_sect->flags |= SEC_RELOC;
2838 target_sect->relocation = NULL;
2839 target_sect->rel_filepos = hdr->sh_offset;
2840 /* In the section to which the relocations apply, mark whether
2841 its relocations are of the REL or RELA variety. */
2842 if (hdr->sh_size != 0)
2843 {
2844 if (hdr->sh_type == SHT_RELA)
2845 target_sect->use_rela_p = 1;
2846 }
2847 abfd->flags |= HAS_RELOC;
2848 goto success;
2849 }
2850
2851 case SHT_GNU_verdef:
2852 if (hdr->sh_info != 0)
2853 elf_dynverdef (abfd) = shindex;
2854 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2855 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2856 goto success;
2857
2858 case SHT_GNU_versym:
2859 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2860 goto fail;
2861
2862 elf_dynversym (abfd) = shindex;
2863 elf_tdata (abfd)->dynversym_hdr = *hdr;
2864 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2865 goto success;
2866
2867 case SHT_GNU_verneed:
2868 if (hdr->sh_info != 0)
2869 elf_dynverref (abfd) = shindex;
2870 elf_tdata (abfd)->dynverref_hdr = *hdr;
2871 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2872 goto success;
2873
2874 case SHT_SHLIB:
2875 goto success;
2876
2877 case SHT_GROUP:
2878 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2879 goto fail;
2880
2881 goto success;
2882
2883 default:
2884 /* Possibly an attributes section. */
2885 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2886 || hdr->sh_type == bed->obj_attrs_section_type)
2887 {
2888 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2889 goto fail;
2890 _bfd_elf_parse_attributes (abfd, hdr);
2891 goto success;
2892 }
2893
2894 /* Check for any processor-specific section types. */
2895 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2896 goto success;
2897
2898 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2899 {
2900 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2901 /* FIXME: How to properly handle allocated section reserved
2902 for applications? */
2903 _bfd_error_handler
2904 /* xgettext:c-format */
2905 (_("%pB: unknown type [%#x] section `%s'"),
2906 abfd, hdr->sh_type, name);
2907 else
2908 {
2909 /* Allow sections reserved for applications. */
2910 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2911 goto success;
2912 }
2913 }
2914 else if (hdr->sh_type >= SHT_LOPROC
2915 && hdr->sh_type <= SHT_HIPROC)
2916 /* FIXME: We should handle this section. */
2917 _bfd_error_handler
2918 /* xgettext:c-format */
2919 (_("%pB: unknown type [%#x] section `%s'"),
2920 abfd, hdr->sh_type, name);
2921 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2922 {
2923 /* Unrecognised OS-specific sections. */
2924 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2925 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2926 required to correctly process the section and the file should
2927 be rejected with an error message. */
2928 _bfd_error_handler
2929 /* xgettext:c-format */
2930 (_("%pB: unknown type [%#x] section `%s'"),
2931 abfd, hdr->sh_type, name);
2932 else
2933 {
2934 /* Otherwise it should be processed. */
2935 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2936 goto success;
2937 }
2938 }
2939 else
2940 /* FIXME: We should handle this section. */
2941 _bfd_error_handler
2942 /* xgettext:c-format */
2943 (_("%pB: unknown type [%#x] section `%s'"),
2944 abfd, hdr->sh_type, name);
2945
2946 goto fail;
2947 }
2948
2949 fail:
2950 ret = false;
2951 success:
2952 elf_tdata (abfd)->being_created[shindex] = false;
2953 return ret;
2954 }
2955
2956 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2957
2958 Elf_Internal_Sym *
2959 bfd_sym_from_r_symndx (struct sym_cache *cache,
2960 bfd *abfd,
2961 unsigned long r_symndx)
2962 {
2963 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2964
2965 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2966 {
2967 Elf_Internal_Shdr *symtab_hdr;
2968 unsigned char esym[sizeof (Elf64_External_Sym)];
2969 Elf_External_Sym_Shndx eshndx;
2970
2971 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2972 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2973 &cache->sym[ent], esym, &eshndx) == NULL)
2974 return NULL;
2975
2976 if (cache->abfd != abfd)
2977 {
2978 memset (cache->indx, -1, sizeof (cache->indx));
2979 cache->abfd = abfd;
2980 }
2981 cache->indx[ent] = r_symndx;
2982 }
2983
2984 return &cache->sym[ent];
2985 }
2986
2987 /* Given an ELF section number, retrieve the corresponding BFD
2988 section. */
2989
2990 asection *
2991 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2992 {
2993 if (sec_index >= elf_numsections (abfd))
2994 return NULL;
2995 return elf_elfsections (abfd)[sec_index]->bfd_section;
2996 }
2997
2998 static const struct bfd_elf_special_section special_sections_b[] =
2999 {
3000 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3001 { NULL, 0, 0, 0, 0 }
3002 };
3003
3004 static const struct bfd_elf_special_section special_sections_c[] =
3005 {
3006 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3007 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
3008 { NULL, 0, 0, 0, 0 }
3009 };
3010
3011 static const struct bfd_elf_special_section special_sections_d[] =
3012 {
3013 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3014 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3015 /* There are more DWARF sections than these, but they needn't be added here
3016 unless you have to cope with broken compilers that don't emit section
3017 attributes or you want to help the user writing assembler. */
3018 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
3019 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
3020 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
3021 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
3022 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3023 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3024 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3025 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3026 { NULL, 0, 0, 0, 0 }
3027 };
3028
3029 static const struct bfd_elf_special_section special_sections_f[] =
3030 {
3031 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3032 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3033 { NULL, 0 , 0, 0, 0 }
3034 };
3035
3036 static const struct bfd_elf_special_section special_sections_g[] =
3037 {
3038 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3039 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3040 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3041 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3042 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3043 { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3044 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3045 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3046 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3047 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3048 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3049 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3050 { NULL, 0, 0, 0, 0 }
3051 };
3052
3053 static const struct bfd_elf_special_section special_sections_h[] =
3054 {
3055 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3056 { NULL, 0, 0, 0, 0 }
3057 };
3058
3059 static const struct bfd_elf_special_section special_sections_i[] =
3060 {
3061 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3062 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3063 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3064 { NULL, 0, 0, 0, 0 }
3065 };
3066
3067 static const struct bfd_elf_special_section special_sections_l[] =
3068 {
3069 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3070 { NULL, 0, 0, 0, 0 }
3071 };
3072
3073 static const struct bfd_elf_special_section special_sections_n[] =
3074 {
3075 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3076 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3077 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3078 { NULL, 0, 0, 0, 0 }
3079 };
3080
3081 static const struct bfd_elf_special_section special_sections_p[] =
3082 {
3083 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3084 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3085 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3086 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3087 { NULL, 0, 0, 0, 0 }
3088 };
3089
3090 static const struct bfd_elf_special_section special_sections_r[] =
3091 {
3092 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3093 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3094 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3095 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3096 /* .relro_padding is generated by lld. It should not be confused with a
3097 reloc containing section, because otherwise elf_fake_sections() will
3098 set the entsize to 8, which may not be an actual multiple of the
3099 section's size.
3100 Note - this entry must appear before the ".rel" entry below. */
3101 { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3102 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3103 { NULL, 0, 0, 0, 0 }
3104 };
3105
3106 static const struct bfd_elf_special_section special_sections_s[] =
3107 {
3108 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3109 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3110 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3111 /* See struct bfd_elf_special_section declaration for the semantics of
3112 this special case where .prefix_length != strlen (.prefix). */
3113 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3114 { NULL, 0, 0, 0, 0 }
3115 };
3116
3117 static const struct bfd_elf_special_section special_sections_t[] =
3118 {
3119 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3120 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3121 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3122 { NULL, 0, 0, 0, 0 }
3123 };
3124
3125 static const struct bfd_elf_special_section special_sections_z[] =
3126 {
3127 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3128 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3129 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3130 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3131 { NULL, 0, 0, 0, 0 }
3132 };
3133
3134 static const struct bfd_elf_special_section * const special_sections[] =
3135 {
3136 special_sections_b, /* 'b' */
3137 special_sections_c, /* 'c' */
3138 special_sections_d, /* 'd' */
3139 NULL, /* 'e' */
3140 special_sections_f, /* 'f' */
3141 special_sections_g, /* 'g' */
3142 special_sections_h, /* 'h' */
3143 special_sections_i, /* 'i' */
3144 NULL, /* 'j' */
3145 NULL, /* 'k' */
3146 special_sections_l, /* 'l' */
3147 NULL, /* 'm' */
3148 special_sections_n, /* 'n' */
3149 NULL, /* 'o' */
3150 special_sections_p, /* 'p' */
3151 NULL, /* 'q' */
3152 special_sections_r, /* 'r' */
3153 special_sections_s, /* 's' */
3154 special_sections_t, /* 't' */
3155 NULL, /* 'u' */
3156 NULL, /* 'v' */
3157 NULL, /* 'w' */
3158 NULL, /* 'x' */
3159 NULL, /* 'y' */
3160 special_sections_z /* 'z' */
3161 };
3162
3163 const struct bfd_elf_special_section *
3164 _bfd_elf_get_special_section (const char *name,
3165 const struct bfd_elf_special_section *spec,
3166 unsigned int rela)
3167 {
3168 int i;
3169 int len;
3170
3171 len = strlen (name);
3172
3173 for (i = 0; spec[i].prefix != NULL; i++)
3174 {
3175 int suffix_len;
3176 int prefix_len = spec[i].prefix_length;
3177
3178 if (len < prefix_len)
3179 continue;
3180 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3181 continue;
3182
3183 suffix_len = spec[i].suffix_length;
3184 if (suffix_len <= 0)
3185 {
3186 if (name[prefix_len] != 0)
3187 {
3188 if (suffix_len == 0)
3189 continue;
3190 if (name[prefix_len] != '.'
3191 && (suffix_len == -2
3192 || (rela && spec[i].type == SHT_REL)))
3193 continue;
3194 }
3195 }
3196 else
3197 {
3198 if (len < prefix_len + suffix_len)
3199 continue;
3200 if (memcmp (name + len - suffix_len,
3201 spec[i].prefix + prefix_len,
3202 suffix_len) != 0)
3203 continue;
3204 }
3205 return &spec[i];
3206 }
3207
3208 return NULL;
3209 }
3210
3211 const struct bfd_elf_special_section *
3212 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3213 {
3214 int i;
3215 const struct bfd_elf_special_section *spec;
3216 const struct elf_backend_data *bed;
3217
3218 /* See if this is one of the special sections. */
3219 if (sec->name == NULL)
3220 return NULL;
3221
3222 bed = get_elf_backend_data (abfd);
3223 spec = bed->special_sections;
3224 if (spec)
3225 {
3226 spec = _bfd_elf_get_special_section (sec->name,
3227 bed->special_sections,
3228 sec->use_rela_p);
3229 if (spec != NULL)
3230 return spec;
3231 }
3232
3233 if (sec->name[0] != '.')
3234 return NULL;
3235
3236 i = sec->name[1] - 'b';
3237 if (i < 0 || i > 'z' - 'b')
3238 return NULL;
3239
3240 spec = special_sections[i];
3241
3242 if (spec == NULL)
3243 return NULL;
3244
3245 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3246 }
3247
3248 bool
3249 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3250 {
3251 struct bfd_elf_section_data *sdata;
3252 const struct elf_backend_data *bed;
3253 const struct bfd_elf_special_section *ssect;
3254
3255 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3256 if (sdata == NULL)
3257 {
3258 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3259 sizeof (*sdata));
3260 if (sdata == NULL)
3261 return false;
3262 sec->used_by_bfd = sdata;
3263 }
3264
3265 /* Indicate whether or not this section should use RELA relocations. */
3266 bed = get_elf_backend_data (abfd);
3267 sec->use_rela_p = bed->default_use_rela_p;
3268
3269 /* Set up ELF section type and flags for newly created sections, if
3270 there is an ABI mandated section. */
3271 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3272 if (ssect != NULL)
3273 {
3274 elf_section_type (sec) = ssect->type;
3275 elf_section_flags (sec) = ssect->attr;
3276 }
3277
3278 return _bfd_generic_new_section_hook (abfd, sec);
3279 }
3280
3281 /* Create a new bfd section from an ELF program header.
3282
3283 Since program segments have no names, we generate a synthetic name
3284 of the form segment<NUM>, where NUM is generally the index in the
3285 program header table. For segments that are split (see below) we
3286 generate the names segment<NUM>a and segment<NUM>b.
3287
3288 Note that some program segments may have a file size that is different than
3289 (less than) the memory size. All this means is that at execution the
3290 system must allocate the amount of memory specified by the memory size,
3291 but only initialize it with the first "file size" bytes read from the
3292 file. This would occur for example, with program segments consisting
3293 of combined data+bss.
3294
3295 To handle the above situation, this routine generates TWO bfd sections
3296 for the single program segment. The first has the length specified by
3297 the file size of the segment, and the second has the length specified
3298 by the difference between the two sizes. In effect, the segment is split
3299 into its initialized and uninitialized parts. */
3300
3301 bool
3302 _bfd_elf_make_section_from_phdr (bfd *abfd,
3303 Elf_Internal_Phdr *hdr,
3304 int hdr_index,
3305 const char *type_name)
3306 {
3307 asection *newsect;
3308 char *name;
3309 char namebuf[64];
3310 size_t len;
3311 int split;
3312 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3313
3314 split = ((hdr->p_memsz > 0)
3315 && (hdr->p_filesz > 0)
3316 && (hdr->p_memsz > hdr->p_filesz));
3317
3318 if (hdr->p_filesz > 0)
3319 {
3320 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3321 len = strlen (namebuf) + 1;
3322 name = (char *) bfd_alloc (abfd, len);
3323 if (!name)
3324 return false;
3325 memcpy (name, namebuf, len);
3326 newsect = bfd_make_section (abfd, name);
3327 if (newsect == NULL)
3328 return false;
3329 newsect->vma = hdr->p_vaddr / opb;
3330 newsect->lma = hdr->p_paddr / opb;
3331 newsect->size = hdr->p_filesz;
3332 newsect->filepos = hdr->p_offset;
3333 newsect->flags |= SEC_HAS_CONTENTS;
3334 newsect->alignment_power = bfd_log2 (hdr->p_align);
3335 if (hdr->p_type == PT_LOAD)
3336 {
3337 newsect->flags |= SEC_ALLOC;
3338 newsect->flags |= SEC_LOAD;
3339 if (hdr->p_flags & PF_X)
3340 {
3341 /* FIXME: all we known is that it has execute PERMISSION,
3342 may be data. */
3343 newsect->flags |= SEC_CODE;
3344 }
3345 }
3346 if (!(hdr->p_flags & PF_W))
3347 {
3348 newsect->flags |= SEC_READONLY;
3349 }
3350 }
3351
3352 if (hdr->p_memsz > hdr->p_filesz)
3353 {
3354 bfd_vma align;
3355
3356 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3357 len = strlen (namebuf) + 1;
3358 name = (char *) bfd_alloc (abfd, len);
3359 if (!name)
3360 return false;
3361 memcpy (name, namebuf, len);
3362 newsect = bfd_make_section (abfd, name);
3363 if (newsect == NULL)
3364 return false;
3365 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3366 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3367 newsect->size = hdr->p_memsz - hdr->p_filesz;
3368 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3369 align = newsect->vma & -newsect->vma;
3370 if (align == 0 || align > hdr->p_align)
3371 align = hdr->p_align;
3372 newsect->alignment_power = bfd_log2 (align);
3373 if (hdr->p_type == PT_LOAD)
3374 {
3375 newsect->flags |= SEC_ALLOC;
3376 if (hdr->p_flags & PF_X)
3377 newsect->flags |= SEC_CODE;
3378 }
3379 if (!(hdr->p_flags & PF_W))
3380 newsect->flags |= SEC_READONLY;
3381 }
3382
3383 return true;
3384 }
3385
3386 static bool
3387 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3388 {
3389 /* The return value is ignored. Build-ids are considered optional. */
3390 if (templ->xvec->flavour == bfd_target_elf_flavour)
3391 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3392 (templ, offset);
3393 return false;
3394 }
3395
3396 bool
3397 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3398 {
3399 const struct elf_backend_data *bed;
3400
3401 switch (hdr->p_type)
3402 {
3403 case PT_NULL:
3404 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3405
3406 case PT_LOAD:
3407 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3408 return false;
3409 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3410 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3411 return true;
3412
3413 case PT_DYNAMIC:
3414 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3415
3416 case PT_INTERP:
3417 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3418
3419 case PT_NOTE:
3420 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3421 return false;
3422 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3423 hdr->p_align))
3424 return false;
3425 return true;
3426
3427 case PT_SHLIB:
3428 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3429
3430 case PT_PHDR:
3431 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3432
3433 case PT_GNU_EH_FRAME:
3434 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3435 "eh_frame_hdr");
3436
3437 case PT_GNU_STACK:
3438 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3439
3440 case PT_GNU_RELRO:
3441 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3442
3443 case PT_GNU_SFRAME:
3444 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3445 "sframe");
3446
3447 default:
3448 /* Check for any processor-specific program segment types. */
3449 bed = get_elf_backend_data (abfd);
3450 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3451 }
3452 }
3453
3454 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3455 REL or RELA. */
3456
3457 Elf_Internal_Shdr *
3458 _bfd_elf_single_rel_hdr (asection *sec)
3459 {
3460 if (elf_section_data (sec)->rel.hdr)
3461 {
3462 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3463 return elf_section_data (sec)->rel.hdr;
3464 }
3465 else
3466 return elf_section_data (sec)->rela.hdr;
3467 }
3468
3469 static bool
3470 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3471 Elf_Internal_Shdr *rel_hdr,
3472 const char *sec_name,
3473 bool use_rela_p)
3474 {
3475 char *name = (char *) bfd_alloc (abfd,
3476 sizeof ".rela" + strlen (sec_name));
3477 if (name == NULL)
3478 return false;
3479
3480 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3481 rel_hdr->sh_name =
3482 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3483 false);
3484 if (rel_hdr->sh_name == (unsigned int) -1)
3485 return false;
3486
3487 return true;
3488 }
3489
3490 /* Allocate and initialize a section-header for a new reloc section,
3491 containing relocations against ASECT. It is stored in RELDATA. If
3492 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3493 relocations. */
3494
3495 static bool
3496 _bfd_elf_init_reloc_shdr (bfd *abfd,
3497 struct bfd_elf_section_reloc_data *reldata,
3498 const char *sec_name,
3499 bool use_rela_p,
3500 bool delay_sh_name_p)
3501 {
3502 Elf_Internal_Shdr *rel_hdr;
3503 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3504
3505 BFD_ASSERT (reldata->hdr == NULL);
3506 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3507 if (rel_hdr == NULL)
3508 return false;
3509 reldata->hdr = rel_hdr;
3510
3511 if (delay_sh_name_p)
3512 rel_hdr->sh_name = (unsigned int) -1;
3513 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3514 use_rela_p))
3515 return false;
3516 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3517 rel_hdr->sh_entsize = (use_rela_p
3518 ? bed->s->sizeof_rela
3519 : bed->s->sizeof_rel);
3520 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3521 rel_hdr->sh_flags = 0;
3522 rel_hdr->sh_addr = 0;
3523 rel_hdr->sh_size = 0;
3524 rel_hdr->sh_offset = 0;
3525
3526 return true;
3527 }
3528
3529 /* Return the default section type based on the passed in section flags. */
3530
3531 int
3532 bfd_elf_get_default_section_type (flagword flags)
3533 {
3534 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3535 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3536 return SHT_NOBITS;
3537 return SHT_PROGBITS;
3538 }
3539
3540 struct fake_section_arg
3541 {
3542 struct bfd_link_info *link_info;
3543 bool failed;
3544 };
3545
3546 /* Set up an ELF internal section header for a section. */
3547
3548 static void
3549 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3550 {
3551 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3552 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3553 struct bfd_elf_section_data *esd = elf_section_data (asect);
3554 Elf_Internal_Shdr *this_hdr;
3555 unsigned int sh_type;
3556 const char *name = asect->name;
3557 bool delay_sh_name_p = false;
3558 bfd_vma mask;
3559
3560 if (arg->failed)
3561 {
3562 /* We already failed; just get out of the bfd_map_over_sections
3563 loop. */
3564 return;
3565 }
3566
3567 this_hdr = &esd->this_hdr;
3568
3569 /* ld: compress DWARF debug sections with names: .debug_*. */
3570 if (arg->link_info
3571 && (abfd->flags & BFD_COMPRESS) != 0
3572 && (asect->flags & SEC_DEBUGGING) != 0
3573 && (asect->flags & SEC_ALLOC) == 0
3574 && (asect->flags & SEC_HAS_CONTENTS) != 0
3575 && name[1] == 'd'
3576 && name[6] == '_')
3577 {
3578 /* If this section will be compressed, delay adding section
3579 name to section name section after it is compressed in
3580 _bfd_elf_assign_file_positions_for_non_load. */
3581 delay_sh_name_p = true;
3582 }
3583
3584 if (delay_sh_name_p)
3585 this_hdr->sh_name = (unsigned int) -1;
3586 else
3587 {
3588 this_hdr->sh_name
3589 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3590 name, false);
3591 if (this_hdr->sh_name == (unsigned int) -1)
3592 {
3593 arg->failed = true;
3594 return;
3595 }
3596 }
3597
3598 /* Don't clear sh_flags. Assembler may set additional bits. */
3599
3600 if ((asect->flags & SEC_ALLOC) != 0
3601 || asect->user_set_vma)
3602 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3603 else
3604 this_hdr->sh_addr = 0;
3605
3606 this_hdr->sh_offset = 0;
3607 this_hdr->sh_size = asect->size;
3608 this_hdr->sh_link = 0;
3609 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3610 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3611 {
3612 _bfd_error_handler
3613 /* xgettext:c-format */
3614 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3615 abfd, asect->alignment_power, asect);
3616 arg->failed = true;
3617 return;
3618 }
3619 /* Set sh_addralign to the highest power of two given by alignment
3620 consistent with the section VMA. Linker scripts can force VMA. */
3621 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3622 this_hdr->sh_addralign = mask & -mask;
3623 /* The sh_entsize and sh_info fields may have been set already by
3624 copy_private_section_data. */
3625
3626 this_hdr->bfd_section = asect;
3627 this_hdr->contents = NULL;
3628
3629 /* If the section type is unspecified, we set it based on
3630 asect->flags. */
3631 if (asect->type != 0)
3632 sh_type = asect->type;
3633 else if ((asect->flags & SEC_GROUP) != 0)
3634 sh_type = SHT_GROUP;
3635 else
3636 sh_type = bfd_elf_get_default_section_type (asect->flags);
3637
3638 if (this_hdr->sh_type == SHT_NULL)
3639 this_hdr->sh_type = sh_type;
3640 else if (this_hdr->sh_type == SHT_NOBITS
3641 && sh_type == SHT_PROGBITS
3642 && (asect->flags & SEC_ALLOC) != 0)
3643 {
3644 /* Warn if we are changing a NOBITS section to PROGBITS, but
3645 allow the link to proceed. This can happen when users link
3646 non-bss input sections to bss output sections, or emit data
3647 to a bss output section via a linker script. */
3648 _bfd_error_handler
3649 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3650 this_hdr->sh_type = sh_type;
3651 }
3652
3653 switch (this_hdr->sh_type)
3654 {
3655 default:
3656 break;
3657
3658 case SHT_STRTAB:
3659 case SHT_NOTE:
3660 case SHT_NOBITS:
3661 case SHT_PROGBITS:
3662 break;
3663
3664 case SHT_INIT_ARRAY:
3665 case SHT_FINI_ARRAY:
3666 case SHT_PREINIT_ARRAY:
3667 this_hdr->sh_entsize = bed->s->arch_size / 8;
3668 break;
3669
3670 case SHT_HASH:
3671 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3672 break;
3673
3674 case SHT_DYNSYM:
3675 this_hdr->sh_entsize = bed->s->sizeof_sym;
3676 break;
3677
3678 case SHT_DYNAMIC:
3679 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3680 break;
3681
3682 case SHT_RELA:
3683 if (get_elf_backend_data (abfd)->may_use_rela_p)
3684 this_hdr->sh_entsize = bed->s->sizeof_rela;
3685 break;
3686
3687 case SHT_REL:
3688 if (get_elf_backend_data (abfd)->may_use_rel_p)
3689 this_hdr->sh_entsize = bed->s->sizeof_rel;
3690 break;
3691
3692 case SHT_GNU_versym:
3693 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3694 break;
3695
3696 case SHT_GNU_verdef:
3697 this_hdr->sh_entsize = 0;
3698 /* objcopy or strip will copy over sh_info, but may not set
3699 cverdefs. The linker will set cverdefs, but sh_info will be
3700 zero. */
3701 if (this_hdr->sh_info == 0)
3702 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3703 else
3704 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3705 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3706 break;
3707
3708 case SHT_GNU_verneed:
3709 this_hdr->sh_entsize = 0;
3710 /* objcopy or strip will copy over sh_info, but may not set
3711 cverrefs. The linker will set cverrefs, but sh_info will be
3712 zero. */
3713 if (this_hdr->sh_info == 0)
3714 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3715 else
3716 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3717 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3718 break;
3719
3720 case SHT_GROUP:
3721 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3722 break;
3723
3724 case SHT_GNU_HASH:
3725 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3726 break;
3727 }
3728
3729 if ((asect->flags & SEC_ALLOC) != 0)
3730 this_hdr->sh_flags |= SHF_ALLOC;
3731 if ((asect->flags & SEC_READONLY) == 0)
3732 this_hdr->sh_flags |= SHF_WRITE;
3733 if ((asect->flags & SEC_CODE) != 0)
3734 this_hdr->sh_flags |= SHF_EXECINSTR;
3735 if ((asect->flags & SEC_MERGE) != 0)
3736 {
3737 this_hdr->sh_flags |= SHF_MERGE;
3738 this_hdr->sh_entsize = asect->entsize;
3739 }
3740 if ((asect->flags & SEC_STRINGS) != 0)
3741 {
3742 this_hdr->sh_flags |= SHF_STRINGS;
3743 this_hdr->sh_entsize = asect->entsize;
3744 }
3745 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3746 this_hdr->sh_flags |= SHF_GROUP;
3747 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3748 {
3749 this_hdr->sh_flags |= SHF_TLS;
3750 if (asect->size == 0
3751 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3752 {
3753 struct bfd_link_order *o = asect->map_tail.link_order;
3754
3755 this_hdr->sh_size = 0;
3756 if (o != NULL)
3757 {
3758 this_hdr->sh_size = o->offset + o->size;
3759 if (this_hdr->sh_size != 0)
3760 this_hdr->sh_type = SHT_NOBITS;
3761 }
3762 }
3763 }
3764 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3765 this_hdr->sh_flags |= SHF_EXCLUDE;
3766
3767 /* If the section has relocs, set up a section header for the
3768 SHT_REL[A] section. If two relocation sections are required for
3769 this section, it is up to the processor-specific back-end to
3770 create the other. */
3771 if ((asect->flags & SEC_RELOC) != 0)
3772 {
3773 /* When doing a relocatable link, create both REL and RELA sections if
3774 needed. */
3775 if (arg->link_info
3776 /* Do the normal setup if we wouldn't create any sections here. */
3777 && esd->rel.count + esd->rela.count > 0
3778 && (bfd_link_relocatable (arg->link_info)
3779 || arg->link_info->emitrelocations))
3780 {
3781 if (esd->rel.count && esd->rel.hdr == NULL
3782 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3783 false, delay_sh_name_p))
3784 {
3785 arg->failed = true;
3786 return;
3787 }
3788 if (esd->rela.count && esd->rela.hdr == NULL
3789 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3790 true, delay_sh_name_p))
3791 {
3792 arg->failed = true;
3793 return;
3794 }
3795 }
3796 else if (!_bfd_elf_init_reloc_shdr (abfd,
3797 (asect->use_rela_p
3798 ? &esd->rela : &esd->rel),
3799 name,
3800 asect->use_rela_p,
3801 delay_sh_name_p))
3802 {
3803 arg->failed = true;
3804 return;
3805 }
3806 }
3807
3808 /* Check for processor-specific section types. */
3809 sh_type = this_hdr->sh_type;
3810 if (bed->elf_backend_fake_sections
3811 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3812 {
3813 arg->failed = true;
3814 return;
3815 }
3816
3817 if (sh_type == SHT_NOBITS && asect->size != 0)
3818 {
3819 /* Don't change the header type from NOBITS if we are being
3820 called for objcopy --only-keep-debug. */
3821 this_hdr->sh_type = sh_type;
3822 }
3823 }
3824
3825 /* Fill in the contents of a SHT_GROUP section. Called from
3826 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3827 when ELF targets use the generic linker, ld. Called for ld -r
3828 from bfd_elf_final_link. */
3829
3830 void
3831 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3832 {
3833 bool *failedptr = (bool *) failedptrarg;
3834 asection *elt, *first;
3835 unsigned char *loc;
3836 bool gas;
3837
3838 /* Ignore linker created group section. See elfNN_ia64_object_p in
3839 elfxx-ia64.c. */
3840 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3841 || sec->size == 0
3842 || *failedptr)
3843 return;
3844
3845 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3846 {
3847 unsigned long symindx = 0;
3848
3849 /* elf_group_id will have been set up by objcopy and the
3850 generic linker. */
3851 if (elf_group_id (sec) != NULL)
3852 symindx = elf_group_id (sec)->udata.i;
3853
3854 if (symindx == 0)
3855 {
3856 /* If called from the assembler, swap_out_syms will have set up
3857 elf_section_syms.
3858 PR 25699: A corrupt input file could contain bogus group info. */
3859 if (sec->index >= elf_num_section_syms (abfd)
3860 || elf_section_syms (abfd)[sec->index] == NULL)
3861 {
3862 *failedptr = true;
3863 return;
3864 }
3865 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3866 }
3867 elf_section_data (sec)->this_hdr.sh_info = symindx;
3868 }
3869 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3870 {
3871 /* The ELF backend linker sets sh_info to -2 when the group
3872 signature symbol is global, and thus the index can't be
3873 set until all local symbols are output. */
3874 asection *igroup;
3875 struct bfd_elf_section_data *sec_data;
3876 unsigned long symndx;
3877 unsigned long extsymoff;
3878 struct elf_link_hash_entry *h;
3879
3880 /* The point of this little dance to the first SHF_GROUP section
3881 then back to the SHT_GROUP section is that this gets us to
3882 the SHT_GROUP in the input object. */
3883 igroup = elf_sec_group (elf_next_in_group (sec));
3884 sec_data = elf_section_data (igroup);
3885 symndx = sec_data->this_hdr.sh_info;
3886 extsymoff = 0;
3887 if (!elf_bad_symtab (igroup->owner))
3888 {
3889 Elf_Internal_Shdr *symtab_hdr;
3890
3891 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3892 extsymoff = symtab_hdr->sh_info;
3893 }
3894 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3895 while (h->root.type == bfd_link_hash_indirect
3896 || h->root.type == bfd_link_hash_warning)
3897 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3898
3899 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3900 }
3901
3902 /* The contents won't be allocated for "ld -r" or objcopy. */
3903 gas = true;
3904 if (sec->contents == NULL)
3905 {
3906 gas = false;
3907 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3908
3909 /* Arrange for the section to be written out. */
3910 elf_section_data (sec)->this_hdr.contents = sec->contents;
3911 if (sec->contents == NULL)
3912 {
3913 *failedptr = true;
3914 return;
3915 }
3916 sec->alloced = 1;
3917 }
3918
3919 loc = sec->contents + sec->size;
3920
3921 /* Get the pointer to the first section in the group that gas
3922 squirreled away here. objcopy arranges for this to be set to the
3923 start of the input section group. */
3924 first = elt = elf_next_in_group (sec);
3925
3926 /* First element is a flag word. Rest of section is elf section
3927 indices for all the sections of the group. Write them backwards
3928 just to keep the group in the same order as given in .section
3929 directives, not that it matters. */
3930 while (elt != NULL)
3931 {
3932 asection *s;
3933
3934 s = elt;
3935 if (!gas)
3936 s = s->output_section;
3937 if (s != NULL
3938 && !bfd_is_abs_section (s))
3939 {
3940 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3941 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3942
3943 if (elf_sec->rel.hdr != NULL
3944 && (gas
3945 || (input_elf_sec->rel.hdr != NULL
3946 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3947 {
3948 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3949 loc -= 4;
3950 if (loc == sec->contents)
3951 break;
3952 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3953 }
3954 if (elf_sec->rela.hdr != NULL
3955 && (gas
3956 || (input_elf_sec->rela.hdr != NULL
3957 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3958 {
3959 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3960 loc -= 4;
3961 if (loc == sec->contents)
3962 break;
3963 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3964 }
3965 loc -= 4;
3966 if (loc == sec->contents)
3967 break;
3968 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3969 }
3970 elt = elf_next_in_group (elt);
3971 if (elt == first)
3972 break;
3973 }
3974
3975 /* We should always get here with loc == sec->contents + 4. Return
3976 an error for bogus SHT_GROUP sections. */
3977 loc -= 4;
3978 if (loc != sec->contents)
3979 {
3980 /* xgettext:c-format */
3981 _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
3982 abfd, sec);
3983 bfd_set_error (bfd_error_bad_value);
3984 *failedptr = true;
3985 return;
3986 }
3987
3988 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3989 }
3990
3991 /* Given NAME, the name of a relocation section stripped of its
3992 .rel/.rela prefix, return the section in ABFD to which the
3993 relocations apply. */
3994
3995 asection *
3996 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3997 {
3998 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3999 section likely apply to .got.plt or .got section. */
4000 if (get_elf_backend_data (abfd)->want_got_plt
4001 && strcmp (name, ".plt") == 0)
4002 {
4003 asection *sec;
4004
4005 name = ".got.plt";
4006 sec = bfd_get_section_by_name (abfd, name);
4007 if (sec != NULL)
4008 return sec;
4009 name = ".got";
4010 }
4011
4012 return bfd_get_section_by_name (abfd, name);
4013 }
4014
4015 /* Return the section to which RELOC_SEC applies. */
4016
4017 static asection *
4018 elf_get_reloc_section (asection *reloc_sec)
4019 {
4020 const char *name;
4021 unsigned int type;
4022 bfd *abfd;
4023 const struct elf_backend_data *bed;
4024
4025 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4026 if (type != SHT_REL && type != SHT_RELA)
4027 return NULL;
4028
4029 /* We look up the section the relocs apply to by name. */
4030 name = reloc_sec->name;
4031 if (!startswith (name, ".rel"))
4032 return NULL;
4033 name += 4;
4034 if (type == SHT_RELA && *name++ != 'a')
4035 return NULL;
4036
4037 abfd = reloc_sec->owner;
4038 bed = get_elf_backend_data (abfd);
4039 return bed->get_reloc_section (abfd, name);
4040 }
4041
4042 /* Assign all ELF section numbers. The dummy first section is handled here
4043 too. The link/info pointers for the standard section types are filled
4044 in here too, while we're at it. LINK_INFO will be 0 when arriving
4045 here for gas, objcopy, and when using the generic ELF linker. */
4046
4047 static bool
4048 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4049 {
4050 struct elf_obj_tdata *t = elf_tdata (abfd);
4051 asection *sec;
4052 unsigned int section_number;
4053 Elf_Internal_Shdr **i_shdrp;
4054 struct bfd_elf_section_data *d;
4055 bool need_symtab;
4056 size_t amt;
4057
4058 section_number = 1;
4059
4060 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4061
4062 /* SHT_GROUP sections are in relocatable files only. */
4063 if (link_info == NULL || !link_info->resolve_section_groups)
4064 {
4065 size_t reloc_count = 0;
4066
4067 /* Put SHT_GROUP sections first. */
4068 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4069 {
4070 d = elf_section_data (sec);
4071
4072 if (d->this_hdr.sh_type == SHT_GROUP)
4073 {
4074 if (sec->flags & SEC_LINKER_CREATED)
4075 {
4076 /* Remove the linker created SHT_GROUP sections. */
4077 bfd_section_list_remove (abfd, sec);
4078 abfd->section_count--;
4079 }
4080 else
4081 d->this_idx = section_number++;
4082 }
4083
4084 /* Count relocations. */
4085 reloc_count += sec->reloc_count;
4086 }
4087
4088 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4089 if (reloc_count == 0)
4090 abfd->flags &= ~HAS_RELOC;
4091 else
4092 abfd->flags |= HAS_RELOC;
4093 }
4094
4095 for (sec = abfd->sections; sec; sec = sec->next)
4096 {
4097 d = elf_section_data (sec);
4098
4099 if (d->this_hdr.sh_type != SHT_GROUP)
4100 d->this_idx = section_number++;
4101 if (d->this_hdr.sh_name != (unsigned int) -1)
4102 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4103 if (d->rel.hdr)
4104 {
4105 d->rel.idx = section_number++;
4106 if (d->rel.hdr->sh_name != (unsigned int) -1)
4107 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4108 }
4109 else
4110 d->rel.idx = 0;
4111
4112 if (d->rela.hdr)
4113 {
4114 d->rela.idx = section_number++;
4115 if (d->rela.hdr->sh_name != (unsigned int) -1)
4116 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4117 }
4118 else
4119 d->rela.idx = 0;
4120 }
4121
4122 need_symtab = (bfd_get_symcount (abfd) > 0
4123 || (link_info == NULL
4124 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4125 == HAS_RELOC)));
4126 if (need_symtab)
4127 {
4128 elf_onesymtab (abfd) = section_number++;
4129 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4130 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4131 {
4132 elf_section_list *entry;
4133
4134 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4135
4136 entry = bfd_zalloc (abfd, sizeof (*entry));
4137 entry->ndx = section_number++;
4138 elf_symtab_shndx_list (abfd) = entry;
4139 entry->hdr.sh_name
4140 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4141 ".symtab_shndx", false);
4142 if (entry->hdr.sh_name == (unsigned int) -1)
4143 return false;
4144 }
4145 elf_strtab_sec (abfd) = section_number++;
4146 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4147 }
4148
4149 elf_shstrtab_sec (abfd) = section_number++;
4150 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4151 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4152
4153 if (section_number >= SHN_LORESERVE)
4154 {
4155 /* xgettext:c-format */
4156 _bfd_error_handler (_("%pB: too many sections: %u"),
4157 abfd, section_number);
4158 return false;
4159 }
4160
4161 elf_numsections (abfd) = section_number;
4162 elf_elfheader (abfd)->e_shnum = section_number;
4163
4164 /* Set up the list of section header pointers, in agreement with the
4165 indices. */
4166 amt = section_number * sizeof (Elf_Internal_Shdr *);
4167 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4168 if (i_shdrp == NULL)
4169 return false;
4170
4171 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4172 sizeof (Elf_Internal_Shdr));
4173 if (i_shdrp[0] == NULL)
4174 {
4175 bfd_release (abfd, i_shdrp);
4176 return false;
4177 }
4178
4179 elf_elfsections (abfd) = i_shdrp;
4180
4181 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4182 if (need_symtab)
4183 {
4184 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4185 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4186 {
4187 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4188 BFD_ASSERT (entry != NULL);
4189 i_shdrp[entry->ndx] = & entry->hdr;
4190 entry->hdr.sh_link = elf_onesymtab (abfd);
4191 }
4192 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4193 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4194 }
4195
4196 for (sec = abfd->sections; sec; sec = sec->next)
4197 {
4198 asection *s;
4199
4200 d = elf_section_data (sec);
4201
4202 i_shdrp[d->this_idx] = &d->this_hdr;
4203 if (d->rel.idx != 0)
4204 i_shdrp[d->rel.idx] = d->rel.hdr;
4205 if (d->rela.idx != 0)
4206 i_shdrp[d->rela.idx] = d->rela.hdr;
4207
4208 /* Fill in the sh_link and sh_info fields while we're at it. */
4209
4210 /* sh_link of a reloc section is the section index of the symbol
4211 table. sh_info is the section index of the section to which
4212 the relocation entries apply. */
4213 if (d->rel.idx != 0)
4214 {
4215 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4216 d->rel.hdr->sh_info = d->this_idx;
4217 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4218 }
4219 if (d->rela.idx != 0)
4220 {
4221 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4222 d->rela.hdr->sh_info = d->this_idx;
4223 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4224 }
4225
4226 /* We need to set up sh_link for SHF_LINK_ORDER. */
4227 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4228 {
4229 s = elf_linked_to_section (sec);
4230 /* We can now have a NULL linked section pointer.
4231 This happens when the sh_link field is 0, which is done
4232 when a linked to section is discarded but the linking
4233 section has been retained for some reason. */
4234 if (s)
4235 {
4236 /* Check discarded linkonce section. */
4237 if (discarded_section (s))
4238 {
4239 asection *kept;
4240 _bfd_error_handler
4241 /* xgettext:c-format */
4242 (_("%pB: sh_link of section `%pA' points to"
4243 " discarded section `%pA' of `%pB'"),
4244 abfd, d->this_hdr.bfd_section, s, s->owner);
4245 /* Point to the kept section if it has the same
4246 size as the discarded one. */
4247 kept = _bfd_elf_check_kept_section (s, link_info);
4248 if (kept == NULL)
4249 {
4250 bfd_set_error (bfd_error_bad_value);
4251 return false;
4252 }
4253 s = kept;
4254 }
4255 /* Handle objcopy. */
4256 else if (s->output_section == NULL)
4257 {
4258 _bfd_error_handler
4259 /* xgettext:c-format */
4260 (_("%pB: sh_link of section `%pA' points to"
4261 " removed section `%pA' of `%pB'"),
4262 abfd, d->this_hdr.bfd_section, s, s->owner);
4263 bfd_set_error (bfd_error_bad_value);
4264 return false;
4265 }
4266 s = s->output_section;
4267 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4268 }
4269 }
4270
4271 switch (d->this_hdr.sh_type)
4272 {
4273 case SHT_REL:
4274 case SHT_RELA:
4275 /* sh_link is the section index of the symbol table.
4276 sh_info is the section index of the section to which the
4277 relocation entries apply. */
4278 if (d->this_hdr.sh_link == 0)
4279 {
4280 /* FIXME maybe: If this is a reloc section which we are
4281 treating as a normal section then we likely should
4282 not be assuming its sh_link is .dynsym or .symtab. */
4283 if ((sec->flags & SEC_ALLOC) != 0)
4284 {
4285 s = bfd_get_section_by_name (abfd, ".dynsym");
4286 if (s != NULL)
4287 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4288 }
4289 else
4290 d->this_hdr.sh_link = elf_onesymtab (abfd);
4291 }
4292
4293 s = elf_get_reloc_section (sec);
4294 if (s != NULL)
4295 {
4296 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4297 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4298 }
4299 break;
4300
4301 case SHT_STRTAB:
4302 /* We assume that a section named .stab*str is a stabs
4303 string section. We look for a section with the same name
4304 but without the trailing ``str'', and set its sh_link
4305 field to point to this section. */
4306 if (startswith (sec->name, ".stab")
4307 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4308 {
4309 size_t len;
4310 char *alc;
4311
4312 len = strlen (sec->name);
4313 alc = (char *) bfd_malloc (len - 2);
4314 if (alc == NULL)
4315 return false;
4316 memcpy (alc, sec->name, len - 3);
4317 alc[len - 3] = '\0';
4318 s = bfd_get_section_by_name (abfd, alc);
4319 free (alc);
4320 if (s != NULL)
4321 {
4322 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4323
4324 /* This is a .stab section. */
4325 elf_section_data (s)->this_hdr.sh_entsize = 12;
4326 }
4327 }
4328 break;
4329
4330 case SHT_DYNAMIC:
4331 case SHT_DYNSYM:
4332 case SHT_GNU_verneed:
4333 case SHT_GNU_verdef:
4334 /* sh_link is the section header index of the string table
4335 used for the dynamic entries, or the symbol table, or the
4336 version strings. */
4337 s = bfd_get_section_by_name (abfd, ".dynstr");
4338 if (s != NULL)
4339 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4340 break;
4341
4342 case SHT_GNU_LIBLIST:
4343 /* sh_link is the section header index of the prelink library
4344 list used for the dynamic entries, or the symbol table, or
4345 the version strings. */
4346 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4347 ? ".dynstr" : ".gnu.libstr"));
4348 if (s != NULL)
4349 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4350 break;
4351
4352 case SHT_HASH:
4353 case SHT_GNU_HASH:
4354 case SHT_GNU_versym:
4355 /* sh_link is the section header index of the symbol table
4356 this hash table or version table is for. */
4357 s = bfd_get_section_by_name (abfd, ".dynsym");
4358 if (s != NULL)
4359 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4360 break;
4361
4362 case SHT_GROUP:
4363 d->this_hdr.sh_link = elf_onesymtab (abfd);
4364 }
4365 }
4366
4367 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4368 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4369 debug section name from .debug_* to .zdebug_* if needed. */
4370
4371 return true;
4372 }
4373
4374 static bool
4375 sym_is_global (bfd *abfd, asymbol *sym)
4376 {
4377 /* If the backend has a special mapping, use it. */
4378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4379 if (bed->elf_backend_sym_is_global)
4380 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4381
4382 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4383 || bfd_is_und_section (bfd_asymbol_section (sym))
4384 || bfd_is_com_section (bfd_asymbol_section (sym)));
4385 }
4386
4387 /* Filter global symbols of ABFD to include in the import library. All
4388 SYMCOUNT symbols of ABFD can be examined from their pointers in
4389 SYMS. Pointers of symbols to keep should be stored contiguously at
4390 the beginning of that array.
4391
4392 Returns the number of symbols to keep. */
4393
4394 unsigned int
4395 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4396 asymbol **syms, long symcount)
4397 {
4398 long src_count, dst_count = 0;
4399
4400 for (src_count = 0; src_count < symcount; src_count++)
4401 {
4402 asymbol *sym = syms[src_count];
4403 char *name = (char *) bfd_asymbol_name (sym);
4404 struct bfd_link_hash_entry *h;
4405
4406 if (!sym_is_global (abfd, sym))
4407 continue;
4408
4409 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4410 if (h == NULL)
4411 continue;
4412 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4413 continue;
4414 if (h->linker_def || h->ldscript_def)
4415 continue;
4416
4417 syms[dst_count++] = sym;
4418 }
4419
4420 syms[dst_count] = NULL;
4421
4422 return dst_count;
4423 }
4424
4425 /* Don't output symbols for sections that are not going to be output,
4426 that are duplicates or there is no BFD section. */
4427
4428 static bool
4429 ignore_sym (asymbol *sym)
4430 {
4431 if (sym == NULL)
4432 return false;
4433
4434 if (sym->section == NULL)
4435 return true;
4436
4437 if ((sym->flags & BSF_SECTION_SYM) != 0)
4438 {
4439 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4440 return true;
4441 /* With ld -r on generic elf targets it is possible to have
4442 multiple section symbols in the output for a given section.
4443 We'd like to get rid of all but the first one. This drops
4444 them if the first input section is non-zero size, but fails
4445 to do so if the first input section is zero sized. */
4446 if (sym->section->output_offset != 0)
4447 return true;
4448 }
4449
4450 return discarded_section (sym->section);
4451 }
4452
4453 /* Map symbol from it's internal number to the external number, moving
4454 all local symbols to be at the head of the list. */
4455
4456 static bool
4457 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4458 {
4459 unsigned int symcount = bfd_get_symcount (abfd);
4460 asymbol **syms = bfd_get_outsymbols (abfd);
4461 asymbol **sect_syms;
4462 unsigned int num_locals = 0;
4463 unsigned int num_globals = 0;
4464 unsigned int max_index = 0;
4465 unsigned int idx;
4466 asection *asect;
4467 asymbol **new_syms;
4468 size_t amt;
4469
4470 #ifdef DEBUG
4471 fprintf (stderr, "elf_map_symbols\n");
4472 fflush (stderr);
4473 #endif
4474
4475 for (asect = abfd->sections; asect; asect = asect->next)
4476 {
4477 if (max_index < asect->index)
4478 max_index = asect->index;
4479 }
4480
4481 max_index++;
4482 amt = max_index * sizeof (asymbol *);
4483 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4484 if (sect_syms == NULL)
4485 return false;
4486 elf_section_syms (abfd) = sect_syms;
4487 elf_num_section_syms (abfd) = max_index;
4488
4489 /* Init sect_syms entries for any section symbols we have already
4490 decided to output. */
4491 for (idx = 0; idx < symcount; idx++)
4492 {
4493 asymbol *sym = syms[idx];
4494
4495 if ((sym->flags & BSF_SECTION_SYM) != 0
4496 && sym->value == 0
4497 && !ignore_sym (sym)
4498 && !bfd_is_abs_section (sym->section))
4499 {
4500 asection *sec = sym->section;
4501
4502 if (sec->owner != abfd)
4503 {
4504 sec = sec->output_section;
4505 if (sec == NULL)
4506 return false;
4507 }
4508
4509 sect_syms[sec->index] = syms[idx];
4510 }
4511 }
4512
4513 /* Classify all of the symbols. */
4514 for (idx = 0; idx < symcount; idx++)
4515 {
4516 if (ignore_sym (syms[idx]))
4517 continue;
4518 if (sym_is_global (abfd, syms[idx]))
4519 num_globals++;
4520 else
4521 num_locals++;
4522 }
4523
4524 /* We will be adding a section symbol for each normal BFD section. Most
4525 sections will already have a section symbol in outsymbols, but
4526 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4527 at least in that case. */
4528 for (asect = abfd->sections; asect; asect = asect->next)
4529 {
4530 asymbol *sym = asect->symbol;
4531 /* Don't include ignored section symbols. */
4532 if (!ignore_sym (sym)
4533 && sect_syms[asect->index] == NULL)
4534 {
4535 if (sym_is_global (abfd, asect->symbol))
4536 num_globals++;
4537 else
4538 num_locals++;
4539 }
4540 }
4541
4542 /* Now sort the symbols so the local symbols are first. */
4543 amt = (num_locals + num_globals) * sizeof (asymbol *);
4544 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4545 if (new_syms == NULL)
4546 return false;
4547
4548 unsigned int num_globals2 = 0;
4549 unsigned int num_locals2 = 0;
4550 for (idx = 0; idx < symcount; idx++)
4551 {
4552 asymbol *sym = syms[idx];
4553 unsigned int i;
4554
4555 if (ignore_sym (sym))
4556 continue;
4557
4558 if (sym_is_global (abfd, sym))
4559 i = num_locals + num_globals2++;
4560 else
4561 i = num_locals2++;
4562 new_syms[i] = sym;
4563 sym->udata.i = i + 1;
4564 }
4565 for (asect = abfd->sections; asect; asect = asect->next)
4566 {
4567 asymbol *sym = asect->symbol;
4568 if (!ignore_sym (sym)
4569 && sect_syms[asect->index] == NULL)
4570 {
4571 unsigned int i;
4572
4573 sect_syms[asect->index] = sym;
4574 if (sym_is_global (abfd, sym))
4575 i = num_locals + num_globals2++;
4576 else
4577 i = num_locals2++;
4578 new_syms[i] = sym;
4579 sym->udata.i = i + 1;
4580 }
4581 }
4582
4583 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4584
4585 *pnum_locals = num_locals;
4586 return true;
4587 }
4588
4589 /* Assign a file position to a section, optionally aligning to the
4590 required section alignment. */
4591
4592 file_ptr
4593 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4594 file_ptr offset,
4595 bool align,
4596 unsigned char log_file_align)
4597 {
4598 if (i_shdrp->sh_addralign > 1)
4599 {
4600 file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4601
4602 if (align)
4603 offset = BFD_ALIGN (offset, salign);
4604 else if (log_file_align)
4605 {
4606 /* Heuristic: Cap alignment at log_file_align. */
4607 file_ptr falign = 1u << log_file_align;
4608
4609 offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4610 }
4611 }
4612 i_shdrp->sh_offset = offset;
4613 if (i_shdrp->bfd_section != NULL)
4614 i_shdrp->bfd_section->filepos = offset;
4615 if (i_shdrp->sh_type != SHT_NOBITS)
4616 offset += i_shdrp->sh_size;
4617 return offset;
4618 }
4619
4620 /* Compute the file positions we are going to put the sections at, and
4621 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4622 is not NULL, this is being called by the ELF backend linker. */
4623
4624 bool
4625 _bfd_elf_compute_section_file_positions (bfd *abfd,
4626 struct bfd_link_info *link_info)
4627 {
4628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4629 struct fake_section_arg fsargs;
4630 bool failed;
4631 struct elf_strtab_hash *strtab = NULL;
4632 Elf_Internal_Shdr *shstrtab_hdr;
4633 bool need_symtab;
4634
4635 if (abfd->output_has_begun)
4636 return true;
4637
4638 /* Do any elf backend specific processing first. */
4639 if (bed->elf_backend_begin_write_processing)
4640 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4641
4642 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4643 return false;
4644
4645 fsargs.failed = false;
4646 fsargs.link_info = link_info;
4647 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4648 if (fsargs.failed)
4649 return false;
4650
4651 if (!assign_section_numbers (abfd, link_info))
4652 return false;
4653
4654 /* The backend linker builds symbol table information itself. */
4655 need_symtab = (link_info == NULL
4656 && (bfd_get_symcount (abfd) > 0
4657 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4658 == HAS_RELOC)));
4659 if (need_symtab)
4660 {
4661 /* Non-zero if doing a relocatable link. */
4662 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4663
4664 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4665 return false;
4666 }
4667
4668 failed = false;
4669 if (link_info == NULL)
4670 {
4671 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4672 if (failed)
4673 goto err_free_strtab;
4674 }
4675
4676 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4677 /* sh_name was set in init_file_header. */
4678 shstrtab_hdr->sh_type = SHT_STRTAB;
4679 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4680 shstrtab_hdr->sh_addr = 0;
4681 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4682 shstrtab_hdr->sh_entsize = 0;
4683 shstrtab_hdr->sh_link = 0;
4684 shstrtab_hdr->sh_info = 0;
4685 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4686 shstrtab_hdr->sh_addralign = 1;
4687
4688 if (!assign_file_positions_except_relocs (abfd, link_info))
4689 goto err_free_strtab;
4690
4691 if (strtab != NULL)
4692 {
4693 file_ptr off;
4694 Elf_Internal_Shdr *hdr;
4695
4696 off = elf_next_file_pos (abfd);
4697
4698 hdr = & elf_symtab_hdr (abfd);
4699 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4700
4701 if (elf_symtab_shndx_list (abfd) != NULL)
4702 {
4703 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4704 if (hdr->sh_size != 0)
4705 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4706 /* FIXME: What about other symtab_shndx sections in the list ? */
4707 }
4708
4709 hdr = &elf_tdata (abfd)->strtab_hdr;
4710 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4711
4712 elf_next_file_pos (abfd) = off;
4713
4714 /* Now that we know where the .strtab section goes, write it
4715 out. */
4716 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4717 || ! _bfd_elf_strtab_emit (abfd, strtab))
4718 goto err_free_strtab;
4719 _bfd_elf_strtab_free (strtab);
4720 }
4721
4722 abfd->output_has_begun = true;
4723 return true;
4724
4725 err_free_strtab:
4726 if (strtab != NULL)
4727 _bfd_elf_strtab_free (strtab);
4728 return false;
4729 }
4730
4731 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4732 function effectively returns whether --eh-frame-hdr is given on the
4733 command line. After size_dynamic_sections the result reflects
4734 whether .eh_frame_hdr will actually be output (sizing isn't done
4735 until ldemul_after_allocation). */
4736
4737 static asection *
4738 elf_eh_frame_hdr (const struct bfd_link_info *info)
4739 {
4740 if (info != NULL && is_elf_hash_table (info->hash))
4741 return elf_hash_table (info)->eh_info.hdr_sec;
4742 return NULL;
4743 }
4744
4745 /* Make an initial estimate of the size of the program header. If we
4746 get the number wrong here, we'll redo section placement. */
4747
4748 static bfd_size_type
4749 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4750 {
4751 size_t segs;
4752 asection *s;
4753 const struct elf_backend_data *bed;
4754
4755 /* Assume we will need exactly two PT_LOAD segments: one for text
4756 and one for data. */
4757 segs = 2;
4758
4759 s = bfd_get_section_by_name (abfd, ".interp");
4760 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4761 {
4762 /* If we have a loadable interpreter section, we need a
4763 PT_INTERP segment. In this case, assume we also need a
4764 PT_PHDR segment, although that may not be true for all
4765 targets. */
4766 segs += 2;
4767 }
4768
4769 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4770 {
4771 /* We need a PT_DYNAMIC segment. */
4772 ++segs;
4773 }
4774
4775 if (info != NULL && info->relro)
4776 {
4777 /* We need a PT_GNU_RELRO segment. */
4778 ++segs;
4779 }
4780
4781 if (elf_eh_frame_hdr (info))
4782 {
4783 /* We need a PT_GNU_EH_FRAME segment. */
4784 ++segs;
4785 }
4786
4787 if (elf_stack_flags (abfd))
4788 {
4789 /* We need a PT_GNU_STACK segment. */
4790 ++segs;
4791 }
4792
4793 if (elf_sframe (abfd))
4794 {
4795 /* We need a PT_GNU_SFRAME segment. */
4796 ++segs;
4797 }
4798
4799 s = bfd_get_section_by_name (abfd,
4800 NOTE_GNU_PROPERTY_SECTION_NAME);
4801 if (s != NULL && s->size != 0)
4802 {
4803 /* We need a PT_GNU_PROPERTY segment. */
4804 ++segs;
4805 }
4806
4807 for (s = abfd->sections; s != NULL; s = s->next)
4808 {
4809 if ((s->flags & SEC_LOAD) != 0
4810 && elf_section_type (s) == SHT_NOTE)
4811 {
4812 unsigned int alignment_power;
4813 /* We need a PT_NOTE segment. */
4814 ++segs;
4815 /* Try to create just one PT_NOTE segment for all adjacent
4816 loadable SHT_NOTE sections. gABI requires that within a
4817 PT_NOTE segment (and also inside of each SHT_NOTE section)
4818 each note should have the same alignment. So we check
4819 whether the sections are correctly aligned. */
4820 alignment_power = s->alignment_power;
4821 while (s->next != NULL
4822 && s->next->alignment_power == alignment_power
4823 && (s->next->flags & SEC_LOAD) != 0
4824 && elf_section_type (s->next) == SHT_NOTE)
4825 s = s->next;
4826 }
4827 }
4828
4829 for (s = abfd->sections; s != NULL; s = s->next)
4830 {
4831 if (s->flags & SEC_THREAD_LOCAL)
4832 {
4833 /* We need a PT_TLS segment. */
4834 ++segs;
4835 break;
4836 }
4837 }
4838
4839 bed = get_elf_backend_data (abfd);
4840
4841 if ((abfd->flags & D_PAGED) != 0
4842 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4843 {
4844 /* Add a PT_GNU_MBIND segment for each mbind section. */
4845 bfd_vma commonpagesize;
4846 unsigned int page_align_power;
4847
4848 if (info != NULL)
4849 commonpagesize = info->commonpagesize;
4850 else
4851 commonpagesize = bed->commonpagesize;
4852 page_align_power = bfd_log2 (commonpagesize);
4853 for (s = abfd->sections; s != NULL; s = s->next)
4854 if (elf_section_flags (s) & SHF_GNU_MBIND)
4855 {
4856 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4857 {
4858 _bfd_error_handler
4859 /* xgettext:c-format */
4860 (_("%pB: GNU_MBIND section `%pA' has invalid "
4861 "sh_info field: %d"),
4862 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4863 continue;
4864 }
4865 /* Align mbind section to page size. */
4866 if (s->alignment_power < page_align_power)
4867 s->alignment_power = page_align_power;
4868 segs ++;
4869 }
4870 }
4871
4872 /* Let the backend count up any program headers it might need. */
4873 if (bed->elf_backend_additional_program_headers)
4874 {
4875 int a;
4876
4877 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4878 if (a == -1)
4879 abort ();
4880 segs += a;
4881 }
4882
4883 return segs * bed->s->sizeof_phdr;
4884 }
4885
4886 /* Find the segment that contains the output_section of section. */
4887
4888 Elf_Internal_Phdr *
4889 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4890 {
4891 struct elf_segment_map *m;
4892 Elf_Internal_Phdr *p;
4893
4894 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4895 m != NULL;
4896 m = m->next, p++)
4897 {
4898 int i;
4899
4900 for (i = m->count - 1; i >= 0; i--)
4901 if (m->sections[i] == section)
4902 return p;
4903 }
4904
4905 return NULL;
4906 }
4907
4908 /* Create a mapping from a set of sections to a program segment. */
4909
4910 static struct elf_segment_map *
4911 make_mapping (bfd *abfd,
4912 asection **sections,
4913 unsigned int from,
4914 unsigned int to,
4915 bool phdr)
4916 {
4917 struct elf_segment_map *m;
4918 unsigned int i;
4919 asection **hdrpp;
4920 size_t amt;
4921
4922 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4923 amt += (to - from) * sizeof (asection *);
4924 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4925 if (m == NULL)
4926 return NULL;
4927 m->next = NULL;
4928 m->p_type = PT_LOAD;
4929 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4930 m->sections[i - from] = *hdrpp;
4931 m->count = to - from;
4932
4933 if (from == 0 && phdr)
4934 {
4935 /* Include the headers in the first PT_LOAD segment. */
4936 m->includes_filehdr = 1;
4937 m->includes_phdrs = 1;
4938 }
4939
4940 return m;
4941 }
4942
4943 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4944 on failure. */
4945
4946 struct elf_segment_map *
4947 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4948 {
4949 struct elf_segment_map *m;
4950
4951 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4952 sizeof (struct elf_segment_map));
4953 if (m == NULL)
4954 return NULL;
4955 m->next = NULL;
4956 m->p_type = PT_DYNAMIC;
4957 m->count = 1;
4958 m->sections[0] = dynsec;
4959
4960 return m;
4961 }
4962
4963 /* Possibly add or remove segments from the segment map. */
4964
4965 static bool
4966 elf_modify_segment_map (bfd *abfd,
4967 struct bfd_link_info *info,
4968 bool remove_empty_load)
4969 {
4970 struct elf_segment_map **m;
4971 const struct elf_backend_data *bed;
4972
4973 /* The placement algorithm assumes that non allocated sections are
4974 not in PT_LOAD segments. We ensure this here by removing such
4975 sections from the segment map. We also remove excluded
4976 sections. Finally, any PT_LOAD segment without sections is
4977 removed. */
4978 m = &elf_seg_map (abfd);
4979 while (*m)
4980 {
4981 unsigned int i, new_count;
4982
4983 for (new_count = 0, i = 0; i < (*m)->count; i++)
4984 {
4985 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4986 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4987 || (*m)->p_type != PT_LOAD))
4988 {
4989 (*m)->sections[new_count] = (*m)->sections[i];
4990 new_count++;
4991 }
4992 }
4993 (*m)->count = new_count;
4994
4995 if (remove_empty_load
4996 && (*m)->p_type == PT_LOAD
4997 && (*m)->count == 0
4998 && !(*m)->includes_phdrs)
4999 *m = (*m)->next;
5000 else
5001 m = &(*m)->next;
5002 }
5003
5004 bed = get_elf_backend_data (abfd);
5005 if (bed->elf_backend_modify_segment_map != NULL)
5006 {
5007 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5008 return false;
5009 }
5010
5011 return true;
5012 }
5013
5014 #define IS_TBSS(s) \
5015 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5016
5017 /* Set up a mapping from BFD sections to program segments. Update
5018 NEED_LAYOUT if the section layout is changed. */
5019
5020 bool
5021 _bfd_elf_map_sections_to_segments (bfd *abfd,
5022 struct bfd_link_info *info,
5023 bool *need_layout)
5024 {
5025 unsigned int count;
5026 struct elf_segment_map *m;
5027 asection **sections = NULL;
5028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5029 bool no_user_phdrs;
5030
5031 no_user_phdrs = elf_seg_map (abfd) == NULL;
5032
5033 if (info != NULL)
5034 {
5035 info->user_phdrs = !no_user_phdrs;
5036
5037 /* Size the relative relocations if DT_RELR is enabled. */
5038 if (info->enable_dt_relr
5039 && need_layout != NULL
5040 && bed->size_relative_relocs
5041 && !bed->size_relative_relocs (info, need_layout))
5042 info->callbacks->fatal
5043 (_("%P: failed to size relative relocations\n"));
5044 }
5045
5046 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5047 {
5048 asection *s;
5049 unsigned int i;
5050 struct elf_segment_map *mfirst;
5051 struct elf_segment_map **pm;
5052 asection *last_hdr;
5053 bfd_vma last_size;
5054 unsigned int hdr_index;
5055 bfd_vma maxpagesize;
5056 asection **hdrpp;
5057 bool phdr_in_segment;
5058 bool writable;
5059 bool executable;
5060 unsigned int tls_count = 0;
5061 asection *first_tls = NULL;
5062 asection *first_mbind = NULL;
5063 asection *dynsec, *eh_frame_hdr;
5064 asection *sframe;
5065 size_t amt;
5066 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5067 bfd_size_type phdr_size; /* Octets/bytes. */
5068 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5069
5070 /* Select the allocated sections, and sort them. */
5071
5072 amt = bfd_count_sections (abfd) * sizeof (asection *);
5073 sections = (asection **) bfd_malloc (amt);
5074 if (sections == NULL)
5075 goto error_return;
5076
5077 /* Calculate top address, avoiding undefined behaviour of shift
5078 left operator when shift count is equal to size of type
5079 being shifted. */
5080 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5081 addr_mask = (addr_mask << 1) + 1;
5082
5083 i = 0;
5084 for (s = abfd->sections; s != NULL; s = s->next)
5085 {
5086 if ((s->flags & SEC_ALLOC) != 0)
5087 {
5088 /* target_index is unused until bfd_elf_final_link
5089 starts output of section symbols. Use it to make
5090 qsort stable. */
5091 s->target_index = i;
5092 sections[i] = s;
5093 ++i;
5094 /* A wrapping section potentially clashes with header. */
5095 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5096 wrap_to = (s->lma + s->size / opb) & addr_mask;
5097 }
5098 }
5099 BFD_ASSERT (i <= bfd_count_sections (abfd));
5100 count = i;
5101
5102 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5103
5104 phdr_size = elf_program_header_size (abfd);
5105 if (phdr_size == (bfd_size_type) -1)
5106 phdr_size = get_program_header_size (abfd, info);
5107 phdr_size += bed->s->sizeof_ehdr;
5108 /* phdr_size is compared to LMA values which are in bytes. */
5109 phdr_size /= opb;
5110 if (info != NULL)
5111 maxpagesize = info->maxpagesize;
5112 else
5113 maxpagesize = bed->maxpagesize;
5114 if (maxpagesize == 0)
5115 maxpagesize = 1;
5116 phdr_in_segment = info != NULL && info->load_phdrs;
5117 if (count != 0
5118 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5119 >= (phdr_size & (maxpagesize - 1))))
5120 /* For compatibility with old scripts that may not be using
5121 SIZEOF_HEADERS, add headers when it looks like space has
5122 been left for them. */
5123 phdr_in_segment = true;
5124
5125 /* Build the mapping. */
5126 mfirst = NULL;
5127 pm = &mfirst;
5128
5129 /* If we have a .interp section, then create a PT_PHDR segment for
5130 the program headers and a PT_INTERP segment for the .interp
5131 section. */
5132 s = bfd_get_section_by_name (abfd, ".interp");
5133 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5134 {
5135 amt = sizeof (struct elf_segment_map);
5136 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5137 if (m == NULL)
5138 goto error_return;
5139 m->next = NULL;
5140 m->p_type = PT_PHDR;
5141 m->p_flags = PF_R;
5142 m->p_flags_valid = 1;
5143 m->includes_phdrs = 1;
5144 phdr_in_segment = true;
5145 *pm = m;
5146 pm = &m->next;
5147
5148 amt = sizeof (struct elf_segment_map);
5149 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5150 if (m == NULL)
5151 goto error_return;
5152 m->next = NULL;
5153 m->p_type = PT_INTERP;
5154 m->count = 1;
5155 m->sections[0] = s;
5156
5157 *pm = m;
5158 pm = &m->next;
5159 }
5160
5161 /* Look through the sections. We put sections in the same program
5162 segment when the start of the second section can be placed within
5163 a few bytes of the end of the first section. */
5164 last_hdr = NULL;
5165 last_size = 0;
5166 hdr_index = 0;
5167 writable = false;
5168 executable = false;
5169 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5170 if (dynsec != NULL
5171 && (dynsec->flags & SEC_LOAD) == 0)
5172 dynsec = NULL;
5173
5174 if ((abfd->flags & D_PAGED) == 0)
5175 phdr_in_segment = false;
5176
5177 /* Deal with -Ttext or something similar such that the first section
5178 is not adjacent to the program headers. This is an
5179 approximation, since at this point we don't know exactly how many
5180 program headers we will need. */
5181 if (phdr_in_segment && count > 0)
5182 {
5183 bfd_vma phdr_lma; /* Bytes. */
5184 bool separate_phdr = false;
5185
5186 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5187 if (info != NULL
5188 && info->separate_code
5189 && (sections[0]->flags & SEC_CODE) != 0)
5190 {
5191 /* If data sections should be separate from code and
5192 thus not executable, and the first section is
5193 executable then put the file and program headers in
5194 their own PT_LOAD. */
5195 if (!info->one_rosegment)
5196 separate_phdr = true;
5197
5198 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5199 == (sections[0]->lma & addr_mask & -maxpagesize)))
5200 {
5201 /* The file and program headers are currently on the
5202 same page as the first section. Put them on the
5203 previous page if we can. */
5204 if (phdr_lma >= maxpagesize)
5205 phdr_lma -= maxpagesize;
5206 else
5207 separate_phdr = false;
5208 }
5209 }
5210 if ((sections[0]->lma & addr_mask) < phdr_lma
5211 || (sections[0]->lma & addr_mask) < phdr_size)
5212 /* If file and program headers would be placed at the end
5213 of memory then it's probably better to omit them. */
5214 phdr_in_segment = false;
5215 else if (phdr_lma < wrap_to)
5216 /* If a section wraps around to where we'll be placing
5217 file and program headers, then the headers will be
5218 overwritten. */
5219 phdr_in_segment = false;
5220 else if (separate_phdr)
5221 {
5222 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5223 if (m == NULL)
5224 goto error_return;
5225 m->p_paddr = phdr_lma * opb;
5226 m->p_vaddr_offset
5227 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5228 m->p_paddr_valid = 1;
5229 *pm = m;
5230 pm = &m->next;
5231 phdr_in_segment = false;
5232 }
5233 }
5234
5235 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5236 {
5237 asection *hdr;
5238 bool new_segment;
5239
5240 hdr = *hdrpp;
5241
5242 /* See if this section and the last one will fit in the same
5243 segment. */
5244
5245 if (last_hdr == NULL)
5246 {
5247 /* If we don't have a segment yet, then we don't need a new
5248 one (we build the last one after this loop). */
5249 new_segment = false;
5250 }
5251 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5252 {
5253 /* If this section has a different relation between the
5254 virtual address and the load address, then we need a new
5255 segment. */
5256 new_segment = true;
5257 }
5258 else if (hdr->lma < last_hdr->lma + last_size
5259 || last_hdr->lma + last_size < last_hdr->lma)
5260 {
5261 /* If this section has a load address that makes it overlap
5262 the previous section, then we need a new segment. */
5263 new_segment = true;
5264 }
5265 else if ((abfd->flags & D_PAGED) != 0
5266 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5267 == (hdr->lma & -maxpagesize)))
5268 {
5269 /* If we are demand paged then we can't map two disk
5270 pages onto the same memory page. */
5271 new_segment = false;
5272 }
5273 /* In the next test we have to be careful when last_hdr->lma is close
5274 to the end of the address space. If the aligned address wraps
5275 around to the start of the address space, then there are no more
5276 pages left in memory and it is OK to assume that the current
5277 section can be included in the current segment. */
5278 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5279 + maxpagesize > last_hdr->lma)
5280 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5281 + maxpagesize <= hdr->lma))
5282 {
5283 /* If putting this section in this segment would force us to
5284 skip a page in the segment, then we need a new segment. */
5285 new_segment = true;
5286 }
5287 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5288 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5289 {
5290 /* We don't want to put a loaded section after a
5291 nonloaded (ie. bss style) section in the same segment
5292 as that will force the non-loaded section to be loaded.
5293 Consider .tbss sections as loaded for this purpose. */
5294 new_segment = true;
5295 }
5296 else if ((abfd->flags & D_PAGED) == 0)
5297 {
5298 /* If the file is not demand paged, which means that we
5299 don't require the sections to be correctly aligned in the
5300 file, then there is no other reason for a new segment. */
5301 new_segment = false;
5302 }
5303 else if (info != NULL
5304 && info->separate_code
5305 && executable != ((hdr->flags & SEC_CODE) != 0))
5306 {
5307 new_segment = true;
5308 }
5309 else if (! writable
5310 && (hdr->flags & SEC_READONLY) == 0)
5311 {
5312 /* We don't want to put a writable section in a read only
5313 segment. */
5314 new_segment = true;
5315 }
5316 else
5317 {
5318 /* Otherwise, we can use the same segment. */
5319 new_segment = false;
5320 }
5321
5322 /* Allow interested parties a chance to override our decision. */
5323 if (last_hdr != NULL
5324 && info != NULL
5325 && info->callbacks->override_segment_assignment != NULL)
5326 new_segment
5327 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5328 last_hdr,
5329 new_segment);
5330
5331 if (! new_segment)
5332 {
5333 if ((hdr->flags & SEC_READONLY) == 0)
5334 writable = true;
5335 if ((hdr->flags & SEC_CODE) != 0)
5336 executable = true;
5337 last_hdr = hdr;
5338 /* .tbss sections effectively have zero size. */
5339 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5340 continue;
5341 }
5342
5343 /* We need a new program segment. We must create a new program
5344 header holding all the sections from hdr_index until hdr. */
5345
5346 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5347 if (m == NULL)
5348 goto error_return;
5349
5350 *pm = m;
5351 pm = &m->next;
5352
5353 if ((hdr->flags & SEC_READONLY) == 0)
5354 writable = true;
5355 else
5356 writable = false;
5357
5358 if ((hdr->flags & SEC_CODE) == 0)
5359 executable = false;
5360 else
5361 executable = true;
5362
5363 last_hdr = hdr;
5364 /* .tbss sections effectively have zero size. */
5365 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5366 hdr_index = i;
5367 phdr_in_segment = false;
5368 }
5369
5370 /* Create a final PT_LOAD program segment, but not if it's just
5371 for .tbss. */
5372 if (last_hdr != NULL
5373 && (i - hdr_index != 1
5374 || !IS_TBSS (last_hdr)))
5375 {
5376 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5377 if (m == NULL)
5378 goto error_return;
5379
5380 *pm = m;
5381 pm = &m->next;
5382 }
5383
5384 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5385 if (dynsec != NULL)
5386 {
5387 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5388 if (m == NULL)
5389 goto error_return;
5390 *pm = m;
5391 pm = &m->next;
5392 }
5393
5394 /* For each batch of consecutive loadable SHT_NOTE sections,
5395 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5396 because if we link together nonloadable .note sections and
5397 loadable .note sections, we will generate two .note sections
5398 in the output file. */
5399 for (s = abfd->sections; s != NULL; s = s->next)
5400 {
5401 if ((s->flags & SEC_LOAD) != 0
5402 && elf_section_type (s) == SHT_NOTE)
5403 {
5404 asection *s2;
5405 unsigned int alignment_power = s->alignment_power;
5406
5407 count = 1;
5408 for (s2 = s; s2->next != NULL; s2 = s2->next)
5409 {
5410 if (s2->next->alignment_power == alignment_power
5411 && (s2->next->flags & SEC_LOAD) != 0
5412 && elf_section_type (s2->next) == SHT_NOTE
5413 && align_power (s2->lma + s2->size / opb,
5414 alignment_power)
5415 == s2->next->lma)
5416 count++;
5417 else
5418 break;
5419 }
5420 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5421 amt += count * sizeof (asection *);
5422 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5423 if (m == NULL)
5424 goto error_return;
5425 m->next = NULL;
5426 m->p_type = PT_NOTE;
5427 m->count = count;
5428 while (count > 1)
5429 {
5430 m->sections[m->count - count--] = s;
5431 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5432 s = s->next;
5433 }
5434 m->sections[m->count - 1] = s;
5435 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5436 *pm = m;
5437 pm = &m->next;
5438 }
5439 if (s->flags & SEC_THREAD_LOCAL)
5440 {
5441 if (! tls_count)
5442 first_tls = s;
5443 tls_count++;
5444 }
5445 if (first_mbind == NULL
5446 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5447 first_mbind = s;
5448 }
5449
5450 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5451 if (tls_count > 0)
5452 {
5453 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5454 amt += tls_count * sizeof (asection *);
5455 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5456 if (m == NULL)
5457 goto error_return;
5458 m->next = NULL;
5459 m->p_type = PT_TLS;
5460 m->count = tls_count;
5461 /* Mandated PF_R. */
5462 m->p_flags = PF_R;
5463 m->p_flags_valid = 1;
5464 s = first_tls;
5465 for (i = 0; i < tls_count; ++i)
5466 {
5467 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5468 {
5469 _bfd_error_handler
5470 (_("%pB: TLS sections are not adjacent:"), abfd);
5471 s = first_tls;
5472 i = 0;
5473 while (i < tls_count)
5474 {
5475 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5476 {
5477 _bfd_error_handler (_(" TLS: %pA"), s);
5478 i++;
5479 }
5480 else
5481 _bfd_error_handler (_(" non-TLS: %pA"), s);
5482 s = s->next;
5483 }
5484 bfd_set_error (bfd_error_bad_value);
5485 goto error_return;
5486 }
5487 m->sections[i] = s;
5488 s = s->next;
5489 }
5490
5491 *pm = m;
5492 pm = &m->next;
5493 }
5494
5495 if (first_mbind
5496 && (abfd->flags & D_PAGED) != 0
5497 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5498 for (s = first_mbind; s != NULL; s = s->next)
5499 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5500 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5501 {
5502 /* Mandated PF_R. */
5503 unsigned long p_flags = PF_R;
5504 if ((s->flags & SEC_READONLY) == 0)
5505 p_flags |= PF_W;
5506 if ((s->flags & SEC_CODE) != 0)
5507 p_flags |= PF_X;
5508
5509 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5510 m = bfd_zalloc (abfd, amt);
5511 if (m == NULL)
5512 goto error_return;
5513 m->next = NULL;
5514 m->p_type = (PT_GNU_MBIND_LO
5515 + elf_section_data (s)->this_hdr.sh_info);
5516 m->count = 1;
5517 m->p_flags_valid = 1;
5518 m->sections[0] = s;
5519 m->p_flags = p_flags;
5520
5521 *pm = m;
5522 pm = &m->next;
5523 }
5524
5525 s = bfd_get_section_by_name (abfd,
5526 NOTE_GNU_PROPERTY_SECTION_NAME);
5527 if (s != NULL && s->size != 0)
5528 {
5529 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5530 m = bfd_zalloc (abfd, amt);
5531 if (m == NULL)
5532 goto error_return;
5533 m->next = NULL;
5534 m->p_type = PT_GNU_PROPERTY;
5535 m->count = 1;
5536 m->p_flags_valid = 1;
5537 m->sections[0] = s;
5538 m->p_flags = PF_R;
5539 *pm = m;
5540 pm = &m->next;
5541 }
5542
5543 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5544 segment. */
5545 eh_frame_hdr = elf_eh_frame_hdr (info);
5546 if (eh_frame_hdr != NULL
5547 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5548 {
5549 amt = sizeof (struct elf_segment_map);
5550 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5551 if (m == NULL)
5552 goto error_return;
5553 m->next = NULL;
5554 m->p_type = PT_GNU_EH_FRAME;
5555 m->count = 1;
5556 m->sections[0] = eh_frame_hdr->output_section;
5557
5558 *pm = m;
5559 pm = &m->next;
5560 }
5561
5562 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5563 segment. */
5564 sframe = elf_sframe (abfd);
5565 if (sframe != NULL
5566 && (sframe->output_section->flags & SEC_LOAD) != 0
5567 && sframe->size != 0)
5568 {
5569 amt = sizeof (struct elf_segment_map);
5570 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5571 if (m == NULL)
5572 goto error_return;
5573 m->next = NULL;
5574 m->p_type = PT_GNU_SFRAME;
5575 m->count = 1;
5576 m->sections[0] = sframe->output_section;
5577
5578 *pm = m;
5579 pm = &m->next;
5580 }
5581
5582 if (elf_stack_flags (abfd))
5583 {
5584 amt = sizeof (struct elf_segment_map);
5585 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5586 if (m == NULL)
5587 goto error_return;
5588 m->next = NULL;
5589 m->p_type = PT_GNU_STACK;
5590 m->p_flags = elf_stack_flags (abfd);
5591 m->p_align = bed->stack_align;
5592 m->p_flags_valid = 1;
5593 m->p_align_valid = m->p_align != 0;
5594 if (info->stacksize > 0)
5595 {
5596 m->p_size = info->stacksize;
5597 m->p_size_valid = 1;
5598 }
5599
5600 *pm = m;
5601 pm = &m->next;
5602 }
5603
5604 if (info != NULL && info->relro)
5605 {
5606 for (m = mfirst; m != NULL; m = m->next)
5607 {
5608 if (m->p_type == PT_LOAD
5609 && m->count != 0
5610 && m->sections[0]->vma >= info->relro_start
5611 && m->sections[0]->vma < info->relro_end)
5612 {
5613 i = m->count;
5614 while (--i != (unsigned) -1)
5615 {
5616 if (m->sections[i]->size > 0
5617 && (m->sections[i]->flags & SEC_LOAD) != 0
5618 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5619 break;
5620 }
5621
5622 if (i != (unsigned) -1)
5623 break;
5624 }
5625 }
5626
5627 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5628 if (m != NULL)
5629 {
5630 amt = sizeof (struct elf_segment_map);
5631 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5632 if (m == NULL)
5633 goto error_return;
5634 m->next = NULL;
5635 m->p_type = PT_GNU_RELRO;
5636 *pm = m;
5637 pm = &m->next;
5638 }
5639 }
5640
5641 free (sections);
5642 elf_seg_map (abfd) = mfirst;
5643 }
5644
5645 if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5646 return false;
5647
5648 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5649 ++count;
5650 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5651
5652 return true;
5653
5654 error_return:
5655 free (sections);
5656 return false;
5657 }
5658
5659 /* Sort sections by address. */
5660
5661 static int
5662 elf_sort_sections (const void *arg1, const void *arg2)
5663 {
5664 const asection *sec1 = *(const asection **) arg1;
5665 const asection *sec2 = *(const asection **) arg2;
5666 bfd_size_type size1, size2;
5667
5668 /* Sort by LMA first, since this is the address used to
5669 place the section into a segment. */
5670 if (sec1->lma < sec2->lma)
5671 return -1;
5672 else if (sec1->lma > sec2->lma)
5673 return 1;
5674
5675 /* Then sort by VMA. Normally the LMA and the VMA will be
5676 the same, and this will do nothing. */
5677 if (sec1->vma < sec2->vma)
5678 return -1;
5679 else if (sec1->vma > sec2->vma)
5680 return 1;
5681
5682 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5683
5684 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5685 && (x)->size != 0)
5686
5687 if (TOEND (sec1))
5688 {
5689 if (!TOEND (sec2))
5690 return 1;
5691 }
5692 else if (TOEND (sec2))
5693 return -1;
5694
5695 #undef TOEND
5696
5697 /* Sort by size, to put zero sized sections
5698 before others at the same address. */
5699
5700 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5701 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5702
5703 if (size1 < size2)
5704 return -1;
5705 if (size1 > size2)
5706 return 1;
5707
5708 return sec1->target_index - sec2->target_index;
5709 }
5710
5711 /* This qsort comparison functions sorts PT_LOAD segments first and
5712 by p_paddr, for assign_file_positions_for_load_sections. */
5713
5714 static int
5715 elf_sort_segments (const void *arg1, const void *arg2)
5716 {
5717 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5718 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5719
5720 if (m1->p_type != m2->p_type)
5721 {
5722 if (m1->p_type == PT_NULL)
5723 return 1;
5724 if (m2->p_type == PT_NULL)
5725 return -1;
5726 return m1->p_type < m2->p_type ? -1 : 1;
5727 }
5728 if (m1->includes_filehdr != m2->includes_filehdr)
5729 return m1->includes_filehdr ? -1 : 1;
5730 if (m1->no_sort_lma != m2->no_sort_lma)
5731 return m1->no_sort_lma ? -1 : 1;
5732 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5733 {
5734 bfd_vma lma1, lma2; /* Octets. */
5735 lma1 = 0;
5736 if (m1->p_paddr_valid)
5737 lma1 = m1->p_paddr;
5738 else if (m1->count != 0)
5739 {
5740 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5741 m1->sections[0]);
5742 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5743 }
5744 lma2 = 0;
5745 if (m2->p_paddr_valid)
5746 lma2 = m2->p_paddr;
5747 else if (m2->count != 0)
5748 {
5749 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5750 m2->sections[0]);
5751 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5752 }
5753 if (lma1 != lma2)
5754 return lma1 < lma2 ? -1 : 1;
5755 }
5756 if (m1->idx != m2->idx)
5757 return m1->idx < m2->idx ? -1 : 1;
5758 return 0;
5759 }
5760
5761 /* Ian Lance Taylor writes:
5762
5763 We shouldn't be using % with a negative signed number. That's just
5764 not good. We have to make sure either that the number is not
5765 negative, or that the number has an unsigned type. When the types
5766 are all the same size they wind up as unsigned. When file_ptr is a
5767 larger signed type, the arithmetic winds up as signed long long,
5768 which is wrong.
5769
5770 What we're trying to say here is something like ``increase OFF by
5771 the least amount that will cause it to be equal to the VMA modulo
5772 the page size.'' */
5773 /* In other words, something like:
5774
5775 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5776 off_offset = off % bed->maxpagesize;
5777 if (vma_offset < off_offset)
5778 adjustment = vma_offset + bed->maxpagesize - off_offset;
5779 else
5780 adjustment = vma_offset - off_offset;
5781
5782 which can be collapsed into the expression below. */
5783
5784 static file_ptr
5785 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5786 {
5787 /* PR binutils/16199: Handle an alignment of zero. */
5788 if (maxpagesize == 0)
5789 maxpagesize = 1;
5790 return ((vma - off) % maxpagesize);
5791 }
5792
5793 static void
5794 print_segment_map (const struct elf_segment_map *m)
5795 {
5796 unsigned int j;
5797 const char *pt = get_segment_type (m->p_type);
5798 char buf[32];
5799
5800 if (pt == NULL)
5801 {
5802 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5803 sprintf (buf, "LOPROC+%7.7x",
5804 (unsigned int) (m->p_type - PT_LOPROC));
5805 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5806 sprintf (buf, "LOOS+%7.7x",
5807 (unsigned int) (m->p_type - PT_LOOS));
5808 else
5809 snprintf (buf, sizeof (buf), "%8.8x",
5810 (unsigned int) m->p_type);
5811 pt = buf;
5812 }
5813 fflush (stdout);
5814 fprintf (stderr, "%s:", pt);
5815 for (j = 0; j < m->count; j++)
5816 fprintf (stderr, " %s", m->sections [j]->name);
5817 putc ('\n',stderr);
5818 fflush (stderr);
5819 }
5820
5821 /* Assign file positions to the sections based on the mapping from
5822 sections to segments. This function also sets up some fields in
5823 the file header. */
5824
5825 static bool
5826 assign_file_positions_for_load_sections (bfd *abfd,
5827 struct bfd_link_info *link_info)
5828 {
5829 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5830 struct elf_segment_map *m;
5831 struct elf_segment_map *phdr_load_seg;
5832 Elf_Internal_Phdr *phdrs;
5833 Elf_Internal_Phdr *p;
5834 file_ptr off; /* Octets. */
5835 bfd_size_type maxpagesize;
5836 unsigned int alloc, actual;
5837 unsigned int i, j;
5838 struct elf_segment_map **sorted_seg_map;
5839 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5840
5841 if (link_info == NULL
5842 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5843 return false;
5844
5845 alloc = 0;
5846 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5847 m->idx = alloc++;
5848
5849 if (alloc)
5850 {
5851 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5852 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5853 }
5854 else
5855 {
5856 /* PR binutils/12467. */
5857 elf_elfheader (abfd)->e_phoff = 0;
5858 elf_elfheader (abfd)->e_phentsize = 0;
5859 }
5860
5861 elf_elfheader (abfd)->e_phnum = alloc;
5862
5863 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5864 {
5865 actual = alloc;
5866 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5867 }
5868 else
5869 {
5870 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5871 BFD_ASSERT (elf_program_header_size (abfd)
5872 == actual * bed->s->sizeof_phdr);
5873 BFD_ASSERT (actual >= alloc);
5874 }
5875
5876 if (alloc == 0)
5877 {
5878 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5879 return true;
5880 }
5881
5882 /* We're writing the size in elf_program_header_size (abfd),
5883 see assign_file_positions_except_relocs, so make sure we have
5884 that amount allocated, with trailing space cleared.
5885 The variable alloc contains the computed need, while
5886 elf_program_header_size (abfd) contains the size used for the
5887 layout.
5888 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5889 where the layout is forced to according to a larger size in the
5890 last iterations for the testcase ld-elf/header. */
5891 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5892 + alloc * sizeof (*sorted_seg_map)));
5893 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5894 elf_tdata (abfd)->phdr = phdrs;
5895 if (phdrs == NULL)
5896 return false;
5897
5898 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5899 {
5900 sorted_seg_map[j] = m;
5901 /* If elf_segment_map is not from map_sections_to_segments, the
5902 sections may not be correctly ordered. NOTE: sorting should
5903 not be done to the PT_NOTE section of a corefile, which may
5904 contain several pseudo-sections artificially created by bfd.
5905 Sorting these pseudo-sections breaks things badly. */
5906 if (m->count > 1
5907 && !(elf_elfheader (abfd)->e_type == ET_CORE
5908 && m->p_type == PT_NOTE))
5909 {
5910 for (i = 0; i < m->count; i++)
5911 m->sections[i]->target_index = i;
5912 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5913 elf_sort_sections);
5914 }
5915 }
5916 if (alloc > 1)
5917 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5918 elf_sort_segments);
5919
5920 maxpagesize = 1;
5921 if ((abfd->flags & D_PAGED) != 0)
5922 {
5923 if (link_info != NULL)
5924 maxpagesize = link_info->maxpagesize;
5925 else
5926 maxpagesize = bed->maxpagesize;
5927 }
5928
5929 /* Sections must map to file offsets past the ELF file header. */
5930 off = bed->s->sizeof_ehdr;
5931 /* And if one of the PT_LOAD headers doesn't include the program
5932 headers then we'll be mapping program headers in the usual
5933 position after the ELF file header. */
5934 phdr_load_seg = NULL;
5935 for (j = 0; j < alloc; j++)
5936 {
5937 m = sorted_seg_map[j];
5938 if (m->p_type != PT_LOAD)
5939 break;
5940 if (m->includes_phdrs)
5941 {
5942 phdr_load_seg = m;
5943 break;
5944 }
5945 }
5946 if (phdr_load_seg == NULL)
5947 off += actual * bed->s->sizeof_phdr;
5948
5949 for (j = 0; j < alloc; j++)
5950 {
5951 asection **secpp;
5952 bfd_vma off_adjust; /* Octets. */
5953 bool no_contents;
5954 bfd_size_type align_pagesize;
5955
5956 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5957 number of sections with contents contributing to both p_filesz
5958 and p_memsz, followed by a number of sections with no contents
5959 that just contribute to p_memsz. In this loop, OFF tracks next
5960 available file offset for PT_LOAD and PT_NOTE segments. */
5961 m = sorted_seg_map[j];
5962 p = phdrs + m->idx;
5963 p->p_type = m->p_type;
5964 p->p_flags = m->p_flags;
5965
5966 if (m->count == 0)
5967 p->p_vaddr = m->p_vaddr_offset * opb;
5968 else
5969 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5970
5971 if (m->p_paddr_valid)
5972 p->p_paddr = m->p_paddr;
5973 else if (m->count == 0)
5974 p->p_paddr = 0;
5975 else
5976 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5977
5978 align_pagesize = 0;
5979 if (p->p_type == PT_LOAD
5980 && (abfd->flags & D_PAGED) != 0)
5981 {
5982 /* p_align in demand paged PT_LOAD segments effectively stores
5983 the maximum page size. When copying an executable with
5984 objcopy, we set m->p_align from the input file. Use this
5985 value for maxpagesize rather than bed->maxpagesize, which
5986 may be different. Note that we use maxpagesize for PT_TLS
5987 segment alignment later in this function, so we are relying
5988 on at least one PT_LOAD segment appearing before a PT_TLS
5989 segment. */
5990 if (m->p_align_valid)
5991 maxpagesize = m->p_align;
5992 else if (bed->p_align != 0
5993 && (link_info == NULL
5994 || !link_info->maxpagesize_is_set))
5995 /* We will lay out this binary using maxpagesize but set
5996 p->p_align later to the possibly smaller bed->p_align.
5997 The run-time loader will then be able to load this
5998 binary when the system page size is maxpagesize, but if
5999 the system page size is smaller can use p->p_align.
6000 In either case p->p_align will be increased if
6001 necessary to match section alignment. */
6002 align_pagesize = bed->p_align;
6003
6004 p->p_align = maxpagesize;
6005 }
6006 else if (m->p_align_valid)
6007 p->p_align = m->p_align;
6008 else if (m->count == 0)
6009 p->p_align = 1 << bed->s->log_file_align;
6010
6011 if (m == phdr_load_seg)
6012 off += actual * bed->s->sizeof_phdr;
6013
6014 no_contents = false;
6015 off_adjust = 0;
6016 if (p->p_type == PT_LOAD
6017 && m->count > 0)
6018 {
6019 bfd_size_type align; /* Bytes. */
6020 unsigned int align_power = 0;
6021
6022 if (m->p_align_valid)
6023 align = p->p_align;
6024 else
6025 {
6026 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6027 {
6028 unsigned int secalign;
6029
6030 secalign = bfd_section_alignment (*secpp);
6031 if (secalign > align_power)
6032 align_power = secalign;
6033 }
6034 align = (bfd_size_type) 1 << align_power;
6035 /* If a section requires alignment higher than the
6036 minimum p_align value, don't reduce a maxpagesize
6037 p->p_align set earlier in this function. */
6038 if (align > bed->p_align)
6039 align_pagesize = 0;
6040 if (align < maxpagesize)
6041 align = maxpagesize;
6042 else
6043 {
6044 /* If a section requires alignment higher than the
6045 maximum page size, set p_align to the section
6046 alignment. */
6047 if ((abfd->flags & D_PAGED) != 0)
6048 p->p_align = align;
6049 }
6050 }
6051
6052 for (i = 0; i < m->count; i++)
6053 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6054 /* If we aren't making room for this section, then
6055 it must be SHT_NOBITS regardless of what we've
6056 set via struct bfd_elf_special_section. */
6057 elf_section_type (m->sections[i]) = SHT_NOBITS;
6058
6059 /* Find out whether this segment contains any loadable
6060 sections. */
6061 no_contents = true;
6062 for (i = 0; i < m->count; i++)
6063 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6064 {
6065 no_contents = false;
6066 break;
6067 }
6068
6069 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6070
6071 /* Broken hardware and/or kernel require that files do not
6072 map the same page with different permissions on some hppa
6073 processors. */
6074 if (j != 0
6075 && (abfd->flags & D_PAGED) != 0
6076 && bed->no_page_alias
6077 && (off & (maxpagesize - 1)) != 0
6078 && ((off & -maxpagesize)
6079 == ((off + off_adjust) & -maxpagesize)))
6080 off_adjust += maxpagesize;
6081 off += off_adjust;
6082 if (no_contents)
6083 {
6084 /* We shouldn't need to align the segment on disk since
6085 the segment doesn't need file space, but the gABI
6086 arguably requires the alignment and glibc ld.so
6087 checks it. So to comply with the alignment
6088 requirement but not waste file space, we adjust
6089 p_offset for just this segment. (OFF_ADJUST is
6090 subtracted from OFF later.) This may put p_offset
6091 past the end of file, but that shouldn't matter. */
6092 }
6093 else
6094 off_adjust = 0;
6095 }
6096 /* Make sure the .dynamic section is the first section in the
6097 PT_DYNAMIC segment. */
6098 else if (p->p_type == PT_DYNAMIC
6099 && m->count > 1
6100 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6101 {
6102 _bfd_error_handler
6103 (_("%pB: The first section in the PT_DYNAMIC segment"
6104 " is not the .dynamic section"),
6105 abfd);
6106 bfd_set_error (bfd_error_bad_value);
6107 return false;
6108 }
6109 /* Set the note section type to SHT_NOTE. */
6110 else if (p->p_type == PT_NOTE)
6111 for (i = 0; i < m->count; i++)
6112 elf_section_type (m->sections[i]) = SHT_NOTE;
6113
6114 if (m->includes_filehdr)
6115 {
6116 if (!m->p_flags_valid)
6117 p->p_flags |= PF_R;
6118 p->p_filesz = bed->s->sizeof_ehdr;
6119 p->p_memsz = bed->s->sizeof_ehdr;
6120 if (p->p_type == PT_LOAD)
6121 {
6122 if (m->count > 0)
6123 {
6124 if (p->p_vaddr < (bfd_vma) off
6125 || (!m->p_paddr_valid
6126 && p->p_paddr < (bfd_vma) off))
6127 {
6128 _bfd_error_handler
6129 (_("%pB: not enough room for program headers,"
6130 " try linking with -N"),
6131 abfd);
6132 bfd_set_error (bfd_error_bad_value);
6133 return false;
6134 }
6135 p->p_vaddr -= off;
6136 if (!m->p_paddr_valid)
6137 p->p_paddr -= off;
6138 }
6139 }
6140 else if (sorted_seg_map[0]->includes_filehdr)
6141 {
6142 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6143 p->p_vaddr = filehdr->p_vaddr;
6144 if (!m->p_paddr_valid)
6145 p->p_paddr = filehdr->p_paddr;
6146 }
6147 }
6148
6149 if (m->includes_phdrs)
6150 {
6151 if (!m->p_flags_valid)
6152 p->p_flags |= PF_R;
6153 p->p_filesz += actual * bed->s->sizeof_phdr;
6154 p->p_memsz += actual * bed->s->sizeof_phdr;
6155 if (!m->includes_filehdr)
6156 {
6157 if (p->p_type == PT_LOAD)
6158 {
6159 p->p_offset = off - actual * bed->s->sizeof_phdr;
6160 elf_elfheader (abfd)->e_phoff = p->p_offset;
6161 if (m->count > 0)
6162 {
6163 p->p_vaddr -= off - p->p_offset;
6164 if (!m->p_paddr_valid)
6165 p->p_paddr -= off - p->p_offset;
6166 }
6167 }
6168 else if (phdr_load_seg != NULL)
6169 {
6170 /* Also set PT_PHDR to match phdr_load_seg. We've
6171 sorted segments so that phdr_load_seg will
6172 already be set by the code immediately above. */
6173 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6174 bfd_vma phdr_off = 0; /* Octets. */
6175 if (phdr_load_seg->includes_filehdr)
6176 phdr_off = bed->s->sizeof_ehdr;
6177 p->p_vaddr = phdr->p_vaddr + phdr_off;
6178 if (!m->p_paddr_valid)
6179 p->p_paddr = phdr->p_paddr + phdr_off;
6180 p->p_offset = phdr->p_offset + phdr_off;
6181 }
6182 else
6183 p->p_offset = bed->s->sizeof_ehdr;
6184 }
6185 }
6186
6187 if (p->p_type == PT_LOAD
6188 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6189 {
6190 if (!m->includes_filehdr && !m->includes_phdrs)
6191 {
6192 p->p_offset = off;
6193 if (no_contents)
6194 {
6195 /* Put meaningless p_offset for PT_LOAD segments
6196 without file contents somewhere within the first
6197 page, in an attempt to not point past EOF. */
6198 bfd_size_type align = maxpagesize;
6199 if (align < p->p_align)
6200 align = p->p_align;
6201 if (align < 1)
6202 align = 1;
6203 /* Avoid p_offset of zero, which might be wrongly
6204 interpreted as the segment being the first one,
6205 containing the file header. PR32763. */
6206 p->p_offset = (off + align - 1) % align + 1;
6207 }
6208 }
6209 else
6210 {
6211 file_ptr adjust; /* Octets. */
6212
6213 adjust = off - (p->p_offset + p->p_filesz);
6214 if (!no_contents)
6215 p->p_filesz += adjust;
6216 p->p_memsz += adjust;
6217 }
6218 }
6219
6220 if (align_pagesize)
6221 p->p_align = align_pagesize;
6222
6223 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6224 maps. Set filepos for sections in PT_LOAD segments, and in
6225 core files, for sections in PT_NOTE segments.
6226 assign_file_positions_for_non_load_sections will set filepos
6227 for other sections and update p_filesz for other segments. */
6228 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6229 {
6230 asection *sec;
6231 bfd_size_type align;
6232 Elf_Internal_Shdr *this_hdr;
6233
6234 sec = *secpp;
6235 this_hdr = &elf_section_data (sec)->this_hdr;
6236 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6237
6238 if ((p->p_type == PT_LOAD
6239 || p->p_type == PT_TLS)
6240 && (this_hdr->sh_type != SHT_NOBITS
6241 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6242 && ((this_hdr->sh_flags & SHF_TLS) == 0
6243 || p->p_type == PT_TLS))))
6244 {
6245 bfd_vma p_start = p->p_paddr; /* Octets. */
6246 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6247 bfd_vma s_start = sec->lma * opb; /* Octets. */
6248 bfd_vma adjust = s_start - p_end; /* Octets. */
6249
6250 if (adjust != 0
6251 && (s_start < p_end
6252 || p_end < p_start))
6253 {
6254 _bfd_error_handler
6255 /* xgettext:c-format */
6256 (_("%pB: section %pA lma %#" PRIx64
6257 " adjusted to %#" PRIx64),
6258 abfd, sec, (uint64_t) s_start / opb,
6259 (uint64_t) p_end / opb);
6260 adjust = 0;
6261 sec->lma = p_end / opb;
6262 }
6263 p->p_memsz += adjust;
6264
6265 if (p->p_type == PT_LOAD)
6266 {
6267 if (this_hdr->sh_type != SHT_NOBITS)
6268 {
6269 off_adjust = 0;
6270 if (p->p_filesz + adjust < p->p_memsz)
6271 {
6272 /* We have a PROGBITS section following NOBITS ones.
6273 Allocate file space for the NOBITS section(s).
6274 We don't need to write out the zeros, posix
6275 fseek past the end of data already written
6276 followed by a write at that location is
6277 guaranteed to result in zeros being read
6278 from the gap. */
6279 adjust = p->p_memsz - p->p_filesz;
6280 }
6281 }
6282 /* We only adjust sh_offset in SHT_NOBITS sections
6283 as would seem proper for their address when the
6284 section is first in the segment. sh_offset
6285 doesn't really have any significance for
6286 SHT_NOBITS anyway, apart from a notional position
6287 relative to other sections. Historically we
6288 didn't bother with adjusting sh_offset and some
6289 programs depend on it not being adjusted. See
6290 pr12921 and pr25662. */
6291 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6292 {
6293 off += adjust;
6294 if (this_hdr->sh_type == SHT_NOBITS)
6295 off_adjust += adjust;
6296 }
6297 }
6298 if (this_hdr->sh_type != SHT_NOBITS)
6299 p->p_filesz += adjust;
6300 }
6301
6302 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6303 {
6304 /* The section at i == 0 is the one that actually contains
6305 everything. */
6306 if (i == 0)
6307 {
6308 this_hdr->sh_offset = sec->filepos = off;
6309 off += this_hdr->sh_size;
6310 p->p_filesz = this_hdr->sh_size;
6311 p->p_memsz = 0;
6312 p->p_align = 1;
6313 }
6314 else
6315 {
6316 /* The rest are fake sections that shouldn't be written. */
6317 sec->filepos = 0;
6318 sec->size = 0;
6319 sec->flags = 0;
6320 continue;
6321 }
6322 }
6323 else
6324 {
6325 if (this_hdr->sh_type == SHT_NOBITS
6326 && (this_hdr->sh_flags & SHF_TLS) != 0
6327 && this_hdr->sh_offset == 0)
6328 {
6329 /* Set sh_offset for .tbss sections to their nominal
6330 offset after aligning. They are not loaded from
6331 disk so the value doesn't really matter, except
6332 when the .tbss section is the first one in a
6333 PT_TLS segment. In that case it sets the
6334 p_offset for the PT_TLS segment, which according
6335 to the ELF gABI ought to satisfy
6336 p_offset % p_align == p_vaddr % p_align. */
6337 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6338 off, align);
6339 this_hdr->sh_offset = sec->filepos = off + adjust;
6340 }
6341 else if (p->p_type == PT_LOAD)
6342 {
6343 this_hdr->sh_offset = sec->filepos = off;
6344 if (this_hdr->sh_type != SHT_NOBITS)
6345 off += this_hdr->sh_size;
6346 }
6347
6348 if (this_hdr->sh_type != SHT_NOBITS)
6349 {
6350 p->p_filesz += this_hdr->sh_size;
6351 /* A load section without SHF_ALLOC is something like
6352 a note section in a PT_NOTE segment. These take
6353 file space but are not loaded into memory. */
6354 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6355 p->p_memsz += this_hdr->sh_size;
6356 }
6357 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6358 {
6359 if (p->p_type == PT_TLS)
6360 p->p_memsz += this_hdr->sh_size;
6361
6362 /* .tbss is special. It doesn't contribute to p_memsz of
6363 normal segments. */
6364 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6365 p->p_memsz += this_hdr->sh_size;
6366 }
6367
6368 if (align > p->p_align
6369 && !m->p_align_valid
6370 && (p->p_type != PT_LOAD
6371 || (abfd->flags & D_PAGED) == 0))
6372 p->p_align = align;
6373 }
6374
6375 if (!m->p_flags_valid)
6376 {
6377 p->p_flags |= PF_R;
6378 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6379 p->p_flags |= PF_X;
6380 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6381 p->p_flags |= PF_W;
6382 }
6383 }
6384
6385 off -= off_adjust;
6386
6387 /* PR ld/20815 - Check that the program header segment, if
6388 present, will be loaded into memory. */
6389 if (p->p_type == PT_PHDR
6390 && phdr_load_seg == NULL
6391 && !(bed->elf_backend_allow_non_load_phdr != NULL
6392 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6393 {
6394 /* The fix for this error is usually to edit the linker script being
6395 used and set up the program headers manually. Either that or
6396 leave room for the headers at the start of the SECTIONS. */
6397 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6398 " by LOAD segment"),
6399 abfd);
6400 if (link_info == NULL)
6401 return false;
6402 /* Arrange for the linker to exit with an error, deleting
6403 the output file unless --noinhibit-exec is given. */
6404 link_info->callbacks->info ("%X");
6405 }
6406
6407 /* Check that all sections are in a PT_LOAD segment.
6408 Don't check funky gdb generated core files. */
6409 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6410 {
6411 bool check_vma = true;
6412
6413 for (i = 1; i < m->count; i++)
6414 if (m->sections[i]->vma == m->sections[i - 1]->vma
6415 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6416 ->this_hdr), p) != 0
6417 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6418 ->this_hdr), p) != 0)
6419 {
6420 /* Looks like we have overlays packed into the segment. */
6421 check_vma = false;
6422 break;
6423 }
6424
6425 for (i = 0; i < m->count; i++)
6426 {
6427 Elf_Internal_Shdr *this_hdr;
6428 asection *sec;
6429
6430 sec = m->sections[i];
6431 this_hdr = &(elf_section_data(sec)->this_hdr);
6432 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6433 && !ELF_TBSS_SPECIAL (this_hdr, p))
6434 {
6435 _bfd_error_handler
6436 /* xgettext:c-format */
6437 (_("%pB: section `%pA' can't be allocated in segment %u"),
6438 abfd, sec, m->idx);
6439 print_segment_map (m);
6440 }
6441 }
6442 }
6443 }
6444
6445 elf_next_file_pos (abfd) = off;
6446
6447 if (link_info != NULL
6448 && phdr_load_seg != NULL
6449 && phdr_load_seg->includes_filehdr)
6450 {
6451 /* There is a segment that contains both the file headers and the
6452 program headers, so provide a symbol __ehdr_start pointing there.
6453 A program can use this to examine itself robustly. */
6454
6455 struct elf_link_hash_table *htab = elf_hash_table (link_info);
6456 struct elf_link_hash_entry *hash = htab->hehdr_start;
6457
6458 /* If the symbol was referenced and not defined, define it. */
6459 if (hash != NULL
6460 && (hash->root.type == bfd_link_hash_new
6461 || hash->root.type == bfd_link_hash_undefined
6462 || hash->root.type == bfd_link_hash_undefweak
6463 || hash->root.type == bfd_link_hash_common))
6464 {
6465 asection *s = NULL;
6466 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6467
6468 if (phdr_load_seg->count != 0)
6469 /* The segment contains sections, so use the first one. */
6470 s = phdr_load_seg->sections[0];
6471 else
6472 /* Use the first (i.e. lowest-addressed) section in any segment. */
6473 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6474 if (m->p_type == PT_LOAD && m->count != 0)
6475 {
6476 s = m->sections[0];
6477 break;
6478 }
6479
6480 if (s != NULL)
6481 {
6482 hash->root.u.def.value = filehdr_vaddr - s->vma;
6483 hash->root.u.def.section = s;
6484 }
6485 else
6486 {
6487 hash->root.u.def.value = filehdr_vaddr;
6488 hash->root.u.def.section = bfd_abs_section_ptr;
6489 }
6490
6491 hash->root.type = bfd_link_hash_defined;
6492 hash->def_regular = 1;
6493 hash->non_elf = 0;
6494 }
6495 }
6496
6497 return true;
6498 }
6499
6500 /* Determine if a bfd is a debuginfo file. Unfortunately there
6501 is no defined method for detecting such files, so we have to
6502 use heuristics instead. */
6503
6504 bool
6505 is_debuginfo_file (bfd *abfd)
6506 {
6507 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6508 return false;
6509
6510 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6511 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6512 Elf_Internal_Shdr **headerp;
6513
6514 for (headerp = start_headers; headerp < end_headers; headerp ++)
6515 {
6516 Elf_Internal_Shdr *header = * headerp;
6517
6518 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6519 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6520 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6521 && header->sh_type != SHT_NOBITS
6522 && header->sh_type != SHT_NOTE)
6523 return false;
6524 }
6525
6526 return true;
6527 }
6528
6529 /* Assign file positions for other sections, except for compressed debug
6530 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6531
6532 static bool
6533 assign_file_positions_for_non_load_sections (bfd *abfd,
6534 struct bfd_link_info *link_info)
6535 {
6536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6537 Elf_Internal_Shdr **i_shdrpp;
6538 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6539 Elf_Internal_Phdr *phdrs;
6540 Elf_Internal_Phdr *p;
6541 struct elf_segment_map *m;
6542 file_ptr off;
6543 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6544 bfd_vma maxpagesize;
6545
6546 if (link_info != NULL)
6547 maxpagesize = link_info->maxpagesize;
6548 else
6549 maxpagesize = bed->maxpagesize;
6550 i_shdrpp = elf_elfsections (abfd);
6551 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6552 off = elf_next_file_pos (abfd);
6553 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6554 {
6555 Elf_Internal_Shdr *hdr;
6556 bfd_vma align;
6557
6558 hdr = *hdrpp;
6559 if (hdr->bfd_section != NULL
6560 && (hdr->bfd_section->filepos != 0
6561 || (hdr->sh_type == SHT_NOBITS
6562 && hdr->contents == NULL)))
6563 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6564 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6565 {
6566 if (hdr->sh_size != 0
6567 /* PR 24717 - debuginfo files are known to be not strictly
6568 compliant with the ELF standard. In particular they often
6569 have .note.gnu.property sections that are outside of any
6570 loadable segment. This is not a problem for such files,
6571 so do not warn about them. */
6572 && ! is_debuginfo_file (abfd))
6573 _bfd_error_handler
6574 /* xgettext:c-format */
6575 (_("%pB: warning: allocated section `%s' not in segment"),
6576 abfd,
6577 (hdr->bfd_section == NULL
6578 ? "*unknown*"
6579 : hdr->bfd_section->name));
6580 /* We don't need to page align empty sections. */
6581 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6582 align = maxpagesize;
6583 else
6584 align = hdr->sh_addralign & -hdr->sh_addralign;
6585 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6586 off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6587 bed->s->log_file_align);
6588 }
6589 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6590 && hdr->bfd_section == NULL)
6591 /* We don't know the offset of these sections yet:
6592 their size has not been decided. */
6593 || (abfd->is_linker_output
6594 && hdr->bfd_section != NULL
6595 && (hdr->sh_name == -1u
6596 || bfd_section_is_ctf (hdr->bfd_section)))
6597 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6598 || (elf_symtab_shndx_list (abfd) != NULL
6599 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6600 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6601 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6602 hdr->sh_offset = -1;
6603 else
6604 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6605 }
6606 elf_next_file_pos (abfd) = off;
6607
6608 /* Now that we have set the section file positions, we can set up
6609 the file positions for the non PT_LOAD segments. */
6610 phdrs = elf_tdata (abfd)->phdr;
6611 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6612 {
6613 if (p->p_type == PT_GNU_RELRO)
6614 {
6615 bfd_vma start, end; /* Bytes. */
6616 bool ok;
6617
6618 if (link_info != NULL)
6619 {
6620 /* During linking the range of the RELRO segment is passed
6621 in link_info. Note that there may be padding between
6622 relro_start and the first RELRO section. */
6623 start = link_info->relro_start;
6624 end = link_info->relro_end;
6625 }
6626 else if (m->count != 0)
6627 {
6628 if (!m->p_size_valid)
6629 abort ();
6630 start = m->sections[0]->vma;
6631 end = start + m->p_size / opb;
6632 }
6633 else
6634 {
6635 start = 0;
6636 end = 0;
6637 }
6638
6639 ok = false;
6640 if (start < end)
6641 {
6642 struct elf_segment_map *lm;
6643 const Elf_Internal_Phdr *lp;
6644 unsigned int i;
6645
6646 /* Find a LOAD segment containing a section in the RELRO
6647 segment. */
6648 for (lm = elf_seg_map (abfd), lp = phdrs;
6649 lm != NULL;
6650 lm = lm->next, lp++)
6651 {
6652 if (lp->p_type == PT_LOAD
6653 && lm->count != 0
6654 && (lm->sections[lm->count - 1]->vma
6655 + (!IS_TBSS (lm->sections[lm->count - 1])
6656 ? lm->sections[lm->count - 1]->size / opb
6657 : 0)) > start
6658 && lm->sections[0]->vma < end)
6659 break;
6660 }
6661
6662 if (lm != NULL)
6663 {
6664 /* Find the section starting the RELRO segment. */
6665 for (i = 0; i < lm->count; i++)
6666 {
6667 asection *s = lm->sections[i];
6668 if (s->vma >= start
6669 && s->vma < end
6670 && s->size != 0)
6671 break;
6672 }
6673
6674 if (i < lm->count)
6675 {
6676 p->p_vaddr = lm->sections[i]->vma * opb;
6677 p->p_paddr = lm->sections[i]->lma * opb;
6678 p->p_offset = lm->sections[i]->filepos;
6679 p->p_memsz = end * opb - p->p_vaddr;
6680 p->p_filesz = p->p_memsz;
6681
6682 /* The RELRO segment typically ends a few bytes
6683 into .got.plt but other layouts are possible.
6684 In cases where the end does not match any
6685 loaded section (for instance is in file
6686 padding), trim p_filesz back to correspond to
6687 the end of loaded section contents. */
6688 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6689 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6690
6691 /* Preserve the alignment and flags if they are
6692 valid. The gold linker generates RW/4 for
6693 the PT_GNU_RELRO section. It is better for
6694 objcopy/strip to honor these attributes
6695 otherwise gdb will choke when using separate
6696 debug files. */
6697 if (!m->p_align_valid)
6698 p->p_align = 1;
6699 if (!m->p_flags_valid)
6700 p->p_flags = PF_R;
6701 ok = true;
6702 }
6703 }
6704 }
6705
6706 if (!ok)
6707 {
6708 if (link_info != NULL)
6709 _bfd_error_handler
6710 (_("%pB: warning: unable to allocate any sections"
6711 " to PT_GNU_RELRO segment"),
6712 abfd);
6713 memset (p, 0, sizeof *p);
6714 }
6715 }
6716 else if (p->p_type == PT_GNU_STACK)
6717 {
6718 if (m->p_size_valid)
6719 p->p_memsz = m->p_size;
6720 }
6721 else if (m->count != 0)
6722 {
6723 unsigned int i;
6724
6725 if (p->p_type != PT_LOAD
6726 && (p->p_type != PT_NOTE
6727 || bfd_get_format (abfd) != bfd_core))
6728 {
6729 /* A user specified segment layout may include a PHDR
6730 segment that overlaps with a LOAD segment... */
6731 if (p->p_type == PT_PHDR)
6732 {
6733 m->count = 0;
6734 continue;
6735 }
6736
6737 if (m->includes_filehdr || m->includes_phdrs)
6738 {
6739 /* PR 17512: file: 2195325e. */
6740 _bfd_error_handler
6741 (_("%pB: error: non-load segment %d includes file header "
6742 "and/or program header"),
6743 abfd, (int) (p - phdrs));
6744 return false;
6745 }
6746
6747 p->p_filesz = 0;
6748 p->p_offset = m->sections[0]->filepos;
6749 for (i = m->count; i-- != 0;)
6750 {
6751 asection *sect = m->sections[i];
6752 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6753 if (hdr->sh_type != SHT_NOBITS)
6754 {
6755 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6756 /* NB: p_memsz of the loadable PT_NOTE segment
6757 should be the same as p_filesz. */
6758 if (p->p_type == PT_NOTE
6759 && (hdr->sh_flags & SHF_ALLOC) != 0)
6760 p->p_memsz = p->p_filesz;
6761 break;
6762 }
6763 }
6764 }
6765 }
6766 }
6767
6768 return true;
6769 }
6770
6771 static elf_section_list *
6772 find_section_in_list (unsigned int i, elf_section_list * list)
6773 {
6774 for (;list != NULL; list = list->next)
6775 if (list->ndx == i)
6776 break;
6777 return list;
6778 }
6779
6780 /* Work out the file positions of all the sections. This is called by
6781 _bfd_elf_compute_section_file_positions. All the section sizes and
6782 VMAs must be known before this is called.
6783
6784 Reloc sections come in two flavours: Those processed specially as
6785 "side-channel" data attached to a section to which they apply, and
6786 those that bfd doesn't process as relocations. The latter sort are
6787 stored in a normal bfd section by bfd_section_from_shdr. We don't
6788 consider the former sort here, unless they form part of the loadable
6789 image. Reloc sections not assigned here (and compressed debugging
6790 sections and CTF sections which nothing else in the file can rely
6791 upon) will be handled later by assign_file_positions_for_relocs.
6792
6793 We also don't set the positions of the .symtab and .strtab here. */
6794
6795 static bool
6796 assign_file_positions_except_relocs (bfd *abfd,
6797 struct bfd_link_info *link_info)
6798 {
6799 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6800 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6802 unsigned int alloc;
6803
6804 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6805 && bfd_get_format (abfd) != bfd_core)
6806 {
6807 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6808 unsigned int num_sec = elf_numsections (abfd);
6809 Elf_Internal_Shdr **hdrpp;
6810 unsigned int i;
6811 file_ptr off;
6812
6813 /* Start after the ELF header. */
6814 off = i_ehdrp->e_ehsize;
6815
6816 /* We are not creating an executable, which means that we are
6817 not creating a program header, and that the actual order of
6818 the sections in the file is unimportant. */
6819 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6820 {
6821 Elf_Internal_Shdr *hdr;
6822
6823 hdr = *hdrpp;
6824 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6825 && hdr->bfd_section == NULL)
6826 /* Do not assign offsets for these sections yet: we don't know
6827 their sizes. */
6828 || (abfd->is_linker_output
6829 && hdr->bfd_section != NULL
6830 && (hdr->sh_name == -1u
6831 || bfd_section_is_ctf (hdr->bfd_section)))
6832 || i == elf_onesymtab (abfd)
6833 || (elf_symtab_shndx_list (abfd) != NULL
6834 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6835 || i == elf_strtab_sec (abfd)
6836 || i == elf_shstrtab_sec (abfd))
6837 {
6838 hdr->sh_offset = -1;
6839 }
6840 else
6841 /* There shouldn't be a need to effect "capped" file alignment here,
6842 yet at least the Linux kernel's modpost utility was found to be
6843 unhappy without. While the issue was addressed there, let's be
6844 kind for at least the foreseeable future ... */
6845 off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6846 bed->s->log_file_align);
6847 }
6848
6849 elf_next_file_pos (abfd) = off;
6850 elf_program_header_size (abfd) = 0;
6851 }
6852 else
6853 {
6854 /* Assign file positions for the loaded sections based on the
6855 assignment of sections to segments. */
6856 if (!assign_file_positions_for_load_sections (abfd, link_info))
6857 return false;
6858
6859 /* And for non-load sections. */
6860 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6861 return false;
6862 }
6863
6864 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6865 return false;
6866
6867 /* Write out the program headers. */
6868 alloc = i_ehdrp->e_phnum;
6869 if (alloc != 0)
6870 {
6871 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6872 {
6873 bool warned_tls = false;
6874 bool warned_rwx = false;
6875
6876 /* Memory resident segments with non-zero size and RWX
6877 permissions are a security risk, so we generate a warning
6878 here if we are creating any. */
6879 unsigned int i;
6880
6881 for (i = 0; i < alloc; i++)
6882 {
6883 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6884
6885 if (phdr->p_memsz == 0)
6886 continue;
6887
6888 if (! warned_tls
6889 && phdr->p_type == PT_TLS
6890 && (phdr->p_flags & PF_X))
6891 {
6892 if (link_info->warn_is_error_for_rwx_segments)
6893 {
6894 _bfd_error_handler (_("\
6895 error: %pB has a TLS segment with execute permission"),
6896 abfd);
6897 return false;
6898 }
6899
6900 _bfd_error_handler (_("\
6901 warning: %pB has a TLS segment with execute permission"),
6902 abfd);
6903 if (warned_rwx)
6904 break;
6905
6906 warned_tls = true;
6907 }
6908 else if (! warned_rwx
6909 && phdr->p_type == PT_LOAD
6910 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6911 == (PF_R | PF_W | PF_X)))
6912 {
6913 if (link_info->warn_is_error_for_rwx_segments)
6914 {
6915 _bfd_error_handler (_("\
6916 error: %pB has a LOAD segment with RWX permissions"),
6917 abfd);
6918 return false;
6919 }
6920
6921 _bfd_error_handler (_("\
6922 warning: %pB has a LOAD segment with RWX permissions"),
6923 abfd);
6924 if (warned_tls)
6925 break;
6926
6927 warned_rwx = true;
6928 }
6929 }
6930 }
6931
6932 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6933 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6934 return false;
6935 }
6936
6937 return true;
6938 }
6939
6940 bool
6941 _bfd_elf_init_file_header (bfd *abfd,
6942 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6943 {
6944 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6945 struct elf_strtab_hash *shstrtab;
6946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6947
6948 i_ehdrp = elf_elfheader (abfd);
6949
6950 shstrtab = _bfd_elf_strtab_init ();
6951 if (shstrtab == NULL)
6952 return false;
6953
6954 elf_shstrtab (abfd) = shstrtab;
6955
6956 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6957 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6958 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6959 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6960
6961 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6962 i_ehdrp->e_ident[EI_DATA] =
6963 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6964 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6965
6966 if ((abfd->flags & DYNAMIC) != 0)
6967 i_ehdrp->e_type = ET_DYN;
6968 else if ((abfd->flags & EXEC_P) != 0)
6969 i_ehdrp->e_type = ET_EXEC;
6970 else if (bfd_get_format (abfd) == bfd_core)
6971 i_ehdrp->e_type = ET_CORE;
6972 else
6973 i_ehdrp->e_type = ET_REL;
6974
6975 switch (bfd_get_arch (abfd))
6976 {
6977 case bfd_arch_unknown:
6978 i_ehdrp->e_machine = EM_NONE;
6979 break;
6980
6981 /* There used to be a long list of cases here, each one setting
6982 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6983 in the corresponding bfd definition. To avoid duplication,
6984 the switch was removed. Machines that need special handling
6985 can generally do it in elf_backend_final_write_processing(),
6986 unless they need the information earlier than the final write.
6987 Such need can generally be supplied by replacing the tests for
6988 e_machine with the conditions used to determine it. */
6989 default:
6990 i_ehdrp->e_machine = bed->elf_machine_code;
6991 }
6992
6993 i_ehdrp->e_version = bed->s->ev_current;
6994 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6995
6996 /* No program header, for now. */
6997 i_ehdrp->e_phoff = 0;
6998 i_ehdrp->e_phentsize = 0;
6999 i_ehdrp->e_phnum = 0;
7000
7001 /* Each bfd section is section header entry. */
7002 i_ehdrp->e_entry = bfd_get_start_address (abfd);
7003 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7004
7005 elf_tdata (abfd)->symtab_hdr.sh_name =
7006 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7007 elf_tdata (abfd)->strtab_hdr.sh_name =
7008 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7009 elf_tdata (abfd)->shstrtab_hdr.sh_name =
7010 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7011 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7012 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7013 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7014 return false;
7015
7016 return true;
7017 }
7018
7019 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7020
7021 FIXME: We used to have code here to sort the PT_LOAD segments into
7022 ascending order, as per the ELF spec. But this breaks some programs,
7023 including the Linux kernel. But really either the spec should be
7024 changed or the programs updated. */
7025
7026 bool
7027 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7028 {
7029 if (link_info != NULL && bfd_link_pie (link_info))
7030 {
7031 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7032 unsigned int num_segments = i_ehdrp->e_phnum;
7033 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7034 Elf_Internal_Phdr *segment = tdata->phdr;
7035 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7036
7037 /* Find the lowest p_vaddr in PT_LOAD segments. */
7038 bfd_vma p_vaddr = (bfd_vma) -1;
7039 for (; segment < end_segment; segment++)
7040 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7041 p_vaddr = segment->p_vaddr;
7042
7043 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7044 segments is non-zero. */
7045 if (p_vaddr)
7046 i_ehdrp->e_type = ET_EXEC;
7047 }
7048 return true;
7049 }
7050
7051 /* Assign file positions for all the reloc sections which are not part
7052 of the loadable file image, and the file position of section headers. */
7053
7054 static bool
7055 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7056 {
7057 file_ptr off;
7058 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7059 Elf_Internal_Shdr *shdrp;
7060 Elf_Internal_Ehdr *i_ehdrp;
7061 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7062
7063 /* Skip non-load sections without section header. */
7064 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7065 return true;
7066
7067 off = elf_next_file_pos (abfd);
7068
7069 shdrpp = elf_elfsections (abfd);
7070 end_shdrpp = shdrpp + elf_numsections (abfd);
7071 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7072 {
7073 shdrp = *shdrpp;
7074 if (shdrp->sh_offset == -1)
7075 {
7076 asection *sec = shdrp->bfd_section;
7077 if (sec == NULL
7078 || shdrp->sh_type == SHT_REL
7079 || shdrp->sh_type == SHT_RELA)
7080 ;
7081 else if (bfd_section_is_ctf (sec))
7082 {
7083 /* Update section size and contents. */
7084 shdrp->sh_size = sec->size;
7085 shdrp->contents = sec->contents;
7086 }
7087 else if (shdrp->sh_name == -1u)
7088 {
7089 const char *name = sec->name;
7090 struct bfd_elf_section_data *d;
7091
7092 /* Compress DWARF debug sections. */
7093 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7094 return false;
7095
7096 if (sec->compress_status == COMPRESS_SECTION_DONE
7097 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7098 && name[1] == 'd')
7099 {
7100 /* If section is compressed with zlib-gnu, convert
7101 section name from .debug_* to .zdebug_*. */
7102 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7103 if (new_name == NULL)
7104 return false;
7105 name = new_name;
7106 }
7107 /* Add section name to section name section. */
7108 shdrp->sh_name
7109 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7110 name, false);
7111 d = elf_section_data (sec);
7112
7113 /* Add reloc section name to section name section. */
7114 if (d->rel.hdr
7115 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7116 name, false))
7117 return false;
7118 if (d->rela.hdr
7119 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7120 name, true))
7121 return false;
7122
7123 /* Update section size and contents. */
7124 shdrp->sh_size = sec->size;
7125 shdrp->contents = sec->contents;
7126 sec->contents = NULL;
7127 }
7128
7129 off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7130 (abfd->flags & (EXEC_P | DYNAMIC))
7131 || bfd_get_format (abfd) == bfd_core,
7132 bed->s->log_file_align);
7133 }
7134 }
7135
7136 /* Place section name section after DWARF debug sections have been
7137 compressed. */
7138 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7139 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7140 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7141 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7142
7143 /* Place the section headers. */
7144 i_ehdrp = elf_elfheader (abfd);
7145 off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7146 i_ehdrp->e_shoff = off;
7147 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7148 elf_next_file_pos (abfd) = off;
7149
7150 return true;
7151 }
7152
7153 bool
7154 _bfd_elf_write_object_contents (bfd *abfd)
7155 {
7156 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7157 Elf_Internal_Shdr **i_shdrp;
7158 bool failed;
7159 unsigned int count, num_sec;
7160 struct elf_obj_tdata *t;
7161
7162 if (! abfd->output_has_begun
7163 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7164 return false;
7165 /* Do not rewrite ELF data when the BFD has been opened for update.
7166 abfd->output_has_begun was set to TRUE on opening, so creation of
7167 new sections, and modification of existing section sizes was
7168 restricted. This means the ELF header, program headers and
7169 section headers can't have changed. If the contents of any
7170 sections has been modified, then those changes have already been
7171 written to the BFD. */
7172 else if (abfd->direction == both_direction)
7173 {
7174 BFD_ASSERT (abfd->output_has_begun);
7175 return true;
7176 }
7177
7178 i_shdrp = elf_elfsections (abfd);
7179
7180 failed = false;
7181 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7182 if (failed)
7183 return false;
7184
7185 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7186 return false;
7187
7188 /* After writing the headers, we need to write the sections too... */
7189 num_sec = elf_numsections (abfd);
7190 for (count = 1; count < num_sec; count++)
7191 {
7192 /* Don't set the sh_name field without section header. */
7193 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7194 i_shdrp[count]->sh_name
7195 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7196 i_shdrp[count]->sh_name);
7197 if (bed->elf_backend_section_processing)
7198 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7199 return false;
7200 if (i_shdrp[count]->contents)
7201 {
7202 bfd_size_type amt = i_shdrp[count]->sh_size;
7203
7204 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7205 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7206 return false;
7207 }
7208 }
7209
7210 /* Write out the section header names. */
7211 t = elf_tdata (abfd);
7212 if (elf_shstrtab (abfd) != NULL
7213 && t->shstrtab_hdr.sh_offset != -1
7214 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7215 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7216 return false;
7217
7218 if (!(*bed->elf_backend_final_write_processing) (abfd))
7219 return false;
7220
7221 if (!bed->s->write_shdrs_and_ehdr (abfd))
7222 return false;
7223
7224 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7225 if (t->o->build_id.after_write_object_contents != NULL
7226 && !(*t->o->build_id.after_write_object_contents) (abfd))
7227 return false;
7228 if (t->o->package_metadata.after_write_object_contents != NULL
7229 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7230 return false;
7231
7232 return true;
7233 }
7234
7235 bool
7236 _bfd_elf_write_corefile_contents (bfd *abfd)
7237 {
7238 /* Hopefully this can be done just like an object file. */
7239 return _bfd_elf_write_object_contents (abfd);
7240 }
7241
7242 /* Given a section, search the header to find them. */
7243
7244 unsigned int
7245 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7246 {
7247 const struct elf_backend_data *bed;
7248 unsigned int sec_index;
7249
7250 if (elf_section_data (asect) != NULL
7251 && elf_section_data (asect)->this_idx != 0)
7252 return elf_section_data (asect)->this_idx;
7253
7254 if (bfd_is_abs_section (asect))
7255 sec_index = SHN_ABS;
7256 else if (bfd_is_com_section (asect))
7257 sec_index = SHN_COMMON;
7258 else if (bfd_is_und_section (asect))
7259 sec_index = SHN_UNDEF;
7260 else
7261 sec_index = SHN_BAD;
7262
7263 bed = get_elf_backend_data (abfd);
7264 if (bed->elf_backend_section_from_bfd_section)
7265 {
7266 int retval = sec_index;
7267
7268 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7269 return retval;
7270 }
7271
7272 if (sec_index == SHN_BAD)
7273 bfd_set_error (bfd_error_nonrepresentable_section);
7274
7275 return sec_index;
7276 }
7277
7278 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7279 on error. */
7280
7281 int
7282 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7283 {
7284 asymbol *asym_ptr = *asym_ptr_ptr;
7285 int idx;
7286 flagword flags = asym_ptr->flags;
7287
7288 /* When gas creates relocations against local labels, it creates its
7289 own symbol for the section, but does put the symbol into the
7290 symbol chain, so udata is 0. When the linker is generating
7291 relocatable output, this section symbol may be for one of the
7292 input sections rather than the output section. */
7293 if (asym_ptr->udata.i == 0
7294 && (flags & BSF_SECTION_SYM)
7295 && asym_ptr->section)
7296 {
7297 asection *sec;
7298
7299 sec = asym_ptr->section;
7300 if (sec->owner != abfd && sec->output_section != NULL)
7301 sec = sec->output_section;
7302 if (sec->owner == abfd
7303 && sec->index < elf_num_section_syms (abfd)
7304 && elf_section_syms (abfd)[sec->index] != NULL)
7305 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7306 }
7307
7308 idx = asym_ptr->udata.i;
7309
7310 if (idx == 0)
7311 {
7312 /* This case can occur when using --strip-symbol on a symbol
7313 which is used in a relocation entry. */
7314 _bfd_error_handler
7315 /* xgettext:c-format */
7316 (_("%pB: symbol `%s' required but not present"),
7317 abfd, bfd_asymbol_name (asym_ptr));
7318 bfd_set_error (bfd_error_no_symbols);
7319 return -1;
7320 }
7321
7322 #if DEBUG & 4
7323 {
7324 fprintf (stderr,
7325 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7326 " flags = 0x%.8x\n",
7327 (long) asym_ptr, asym_ptr->name, idx, flags);
7328 fflush (stderr);
7329 }
7330 #endif
7331
7332 return idx;
7333 }
7334
7335 static inline bfd_vma
7336 segment_size (Elf_Internal_Phdr *segment)
7337 {
7338 return (segment->p_memsz > segment->p_filesz
7339 ? segment->p_memsz : segment->p_filesz);
7340 }
7341
7342
7343 /* Returns the end address of the segment + 1. */
7344 static inline bfd_vma
7345 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7346 {
7347 return start + segment_size (segment);
7348 }
7349
7350 static inline bfd_size_type
7351 section_size (asection *section, Elf_Internal_Phdr *segment)
7352 {
7353 if ((section->flags & SEC_HAS_CONTENTS) != 0
7354 || (section->flags & SEC_THREAD_LOCAL) == 0
7355 || segment->p_type == PT_TLS)
7356 return section->size;
7357 return 0;
7358 }
7359
7360 /* Returns TRUE if the given section is contained within the given
7361 segment. LMA addresses are compared against PADDR when
7362 USE_VADDR is false, VMA against VADDR when true. */
7363 static bool
7364 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7365 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7366 bool use_vaddr)
7367 {
7368 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7369 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7370 bfd_vma octet;
7371 if (_bfd_mul_overflow (addr, opb, &octet))
7372 return false;
7373 /* The third and fourth lines below are testing that the section end
7374 address is within the segment. It's written this way to avoid
7375 overflow. Add seg_addr + section_size to both sides of the
7376 inequality to make it obvious. */
7377 return (octet >= seg_addr
7378 && segment_size (segment) >= section_size (section, segment)
7379 && (octet - seg_addr
7380 <= segment_size (segment) - section_size (section, segment)));
7381 }
7382
7383 /* Handle PT_NOTE segment. */
7384 static bool
7385 is_note (asection *s, Elf_Internal_Phdr *p)
7386 {
7387 return (p->p_type == PT_NOTE
7388 && elf_section_type (s) == SHT_NOTE
7389 && (ufile_ptr) s->filepos >= p->p_offset
7390 && p->p_filesz >= s->size
7391 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7392 }
7393
7394 /* Rewrite program header information. */
7395
7396 static bool
7397 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7398 {
7399 Elf_Internal_Ehdr *iehdr;
7400 struct elf_segment_map *map;
7401 struct elf_segment_map *map_first;
7402 struct elf_segment_map **pointer_to_map;
7403 Elf_Internal_Phdr *segment;
7404 asection *section;
7405 unsigned int i;
7406 unsigned int num_segments;
7407 bool phdr_included = false;
7408 bool p_paddr_valid;
7409 struct elf_segment_map *phdr_adjust_seg = NULL;
7410 unsigned int phdr_adjust_num = 0;
7411 const struct elf_backend_data *bed;
7412 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7413
7414 bed = get_elf_backend_data (ibfd);
7415 iehdr = elf_elfheader (ibfd);
7416
7417 map_first = NULL;
7418 pointer_to_map = &map_first;
7419
7420 num_segments = elf_elfheader (ibfd)->e_phnum;
7421
7422 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7423 linker, which generates a PT_INTERP section with p_vaddr and
7424 p_memsz set to 0. */
7425 #define IS_SOLARIS_PT_INTERP(p, s) \
7426 (p->p_vaddr == 0 \
7427 && p->p_paddr == 0 \
7428 && p->p_memsz == 0 \
7429 && p->p_filesz > 0 \
7430 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7431 && s->size > 0 \
7432 && (bfd_vma) s->filepos >= p->p_offset \
7433 && ((bfd_vma) s->filepos + s->size \
7434 <= p->p_offset + p->p_filesz))
7435
7436 /* Decide if the given section should be included in the given segment.
7437 A section will be included if:
7438 1. It is within the address space of the segment -- we use the LMA
7439 if that is set for the segment and the VMA otherwise,
7440 2. It is an allocated section or a NOTE section in a PT_NOTE
7441 segment.
7442 3. There is an output section associated with it,
7443 4. The section has not already been allocated to a previous segment.
7444 5. PT_GNU_STACK segments do not include any sections.
7445 6. PT_TLS segment includes only SHF_TLS sections.
7446 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7447 8. PT_DYNAMIC should not contain empty sections at the beginning
7448 (with the possible exception of .dynamic). */
7449 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7450 (((is_contained_by (section, segment, segment->p_paddr, \
7451 segment->p_vaddr, opb, !paddr_valid) \
7452 && (section->flags & SEC_ALLOC) != 0) \
7453 || is_note (section, segment)) \
7454 && segment->p_type != PT_GNU_STACK \
7455 && (segment->p_type != PT_TLS \
7456 || (section->flags & SEC_THREAD_LOCAL)) \
7457 && (segment->p_type == PT_LOAD \
7458 || segment->p_type == PT_TLS \
7459 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7460 && (segment->p_type != PT_DYNAMIC \
7461 || section_size (section, segment) > 0 \
7462 || (segment->p_paddr \
7463 ? segment->p_paddr != section->lma * (opb) \
7464 : segment->p_vaddr != section->vma * (opb)) \
7465 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7466 && (segment->p_type != PT_LOAD || !section->segment_mark))
7467
7468 /* If the output section of a section in the input segment is NULL,
7469 it is removed from the corresponding output segment. */
7470 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \
7471 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7472 && section->output_section != NULL)
7473
7474 /* Returns TRUE iff seg1 starts after the end of seg2. */
7475 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7476 (seg1->field >= segment_end (seg2, seg2->field))
7477
7478 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7479 their VMA address ranges and their LMA address ranges overlap.
7480 It is possible to have overlapping VMA ranges without overlapping LMA
7481 ranges. RedBoot images for example can have both .data and .bss mapped
7482 to the same VMA range, but with the .data section mapped to a different
7483 LMA. */
7484 #define SEGMENT_OVERLAPS(seg1, seg2) \
7485 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7486 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7487 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7488 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7489
7490 /* Initialise the segment mark field, and discard stupid alignment. */
7491 for (section = ibfd->sections; section != NULL; section = section->next)
7492 {
7493 asection *o = section->output_section;
7494 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7495 o->alignment_power = 0;
7496 section->segment_mark = false;
7497 }
7498
7499 /* The Solaris linker creates program headers in which all the
7500 p_paddr fields are zero. When we try to objcopy or strip such a
7501 file, we get confused. Check for this case, and if we find it
7502 don't set the p_paddr_valid fields. */
7503 p_paddr_valid = false;
7504 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7505 i < num_segments;
7506 i++, segment++)
7507 if (segment->p_paddr != 0)
7508 {
7509 p_paddr_valid = true;
7510 break;
7511 }
7512
7513 /* Scan through the segments specified in the program header
7514 of the input BFD. For this first scan we look for overlaps
7515 in the loadable segments. These can be created by weird
7516 parameters to objcopy. Also, fix some solaris weirdness. */
7517 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7518 i < num_segments;
7519 i++, segment++)
7520 {
7521 unsigned int j;
7522 Elf_Internal_Phdr *segment2;
7523
7524 if (segment->p_type == PT_INTERP)
7525 for (section = ibfd->sections; section; section = section->next)
7526 if (IS_SOLARIS_PT_INTERP (segment, section))
7527 {
7528 /* Mininal change so that the normal section to segment
7529 assignment code will work. */
7530 segment->p_vaddr = section->vma * opb;
7531 break;
7532 }
7533
7534 if (segment->p_type != PT_LOAD)
7535 {
7536 /* Remove PT_GNU_RELRO segment. */
7537 if (segment->p_type == PT_GNU_RELRO)
7538 segment->p_type = PT_NULL;
7539 continue;
7540 }
7541
7542 /* Determine if this segment overlaps any previous segments. */
7543 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7544 {
7545 bfd_signed_vma extra_length;
7546
7547 if (segment2->p_type != PT_LOAD
7548 || !SEGMENT_OVERLAPS (segment, segment2))
7549 continue;
7550
7551 /* Merge the two segments together. */
7552 if (segment2->p_vaddr < segment->p_vaddr)
7553 {
7554 /* Extend SEGMENT2 to include SEGMENT and then delete
7555 SEGMENT. */
7556 extra_length = (segment_end (segment, segment->p_vaddr)
7557 - segment_end (segment2, segment2->p_vaddr));
7558
7559 if (extra_length > 0)
7560 {
7561 segment2->p_memsz += extra_length;
7562 segment2->p_filesz += extra_length;
7563 }
7564
7565 segment->p_type = PT_NULL;
7566
7567 /* Since we have deleted P we must restart the outer loop. */
7568 i = 0;
7569 segment = elf_tdata (ibfd)->phdr;
7570 break;
7571 }
7572 else
7573 {
7574 /* Extend SEGMENT to include SEGMENT2 and then delete
7575 SEGMENT2. */
7576 extra_length = (segment_end (segment2, segment2->p_vaddr)
7577 - segment_end (segment, segment->p_vaddr));
7578
7579 if (extra_length > 0)
7580 {
7581 segment->p_memsz += extra_length;
7582 segment->p_filesz += extra_length;
7583 }
7584
7585 segment2->p_type = PT_NULL;
7586 }
7587 }
7588 }
7589
7590 /* The second scan attempts to assign sections to segments. */
7591 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7592 i < num_segments;
7593 i++, segment++)
7594 {
7595 unsigned int section_count;
7596 asection **sections;
7597 asection *output_section;
7598 unsigned int isec;
7599 asection *matching_lma;
7600 asection *suggested_lma;
7601 unsigned int j;
7602 size_t amt;
7603 asection *first_section;
7604
7605 if (segment->p_type == PT_NULL)
7606 continue;
7607
7608 first_section = NULL;
7609 /* Compute how many sections might be placed into this segment. */
7610 for (section = ibfd->sections, section_count = 0;
7611 section != NULL;
7612 section = section->next)
7613 {
7614 /* Find the first section in the input segment, which may be
7615 removed from the corresponding output segment. */
7616 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7617 {
7618 if (first_section == NULL)
7619 first_section = section;
7620 if (section->output_section != NULL)
7621 ++section_count;
7622 }
7623 }
7624
7625 /* Allocate a segment map big enough to contain
7626 all of the sections we have selected. */
7627 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7628 amt += section_count * sizeof (asection *);
7629 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7630 if (map == NULL)
7631 return false;
7632
7633 /* Initialise the fields of the segment map. Default to
7634 using the physical address of the segment in the input BFD. */
7635 map->next = NULL;
7636 map->p_type = segment->p_type;
7637 map->p_flags = segment->p_flags;
7638 map->p_flags_valid = 1;
7639
7640 if (map->p_type == PT_LOAD
7641 && (ibfd->flags & D_PAGED) != 0
7642 && maxpagesize > 1
7643 && segment->p_align > 1)
7644 {
7645 map->p_align = segment->p_align;
7646 if (segment->p_align > maxpagesize)
7647 map->p_align = maxpagesize;
7648 map->p_align_valid = 1;
7649 }
7650
7651 /* If the first section in the input segment is removed, there is
7652 no need to preserve segment physical address in the corresponding
7653 output segment. */
7654 if (!first_section || first_section->output_section != NULL)
7655 {
7656 map->p_paddr = segment->p_paddr;
7657 map->p_paddr_valid = p_paddr_valid;
7658 }
7659
7660 /* Determine if this segment contains the ELF file header
7661 and if it contains the program headers themselves. */
7662 map->includes_filehdr = (segment->p_offset == 0
7663 && segment->p_filesz >= iehdr->e_ehsize);
7664 map->includes_phdrs = 0;
7665
7666 if (!phdr_included || segment->p_type != PT_LOAD)
7667 {
7668 map->includes_phdrs =
7669 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7670 && (segment->p_offset + segment->p_filesz
7671 >= ((bfd_vma) iehdr->e_phoff
7672 + iehdr->e_phnum * iehdr->e_phentsize)));
7673
7674 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7675 phdr_included = true;
7676 }
7677
7678 if (section_count == 0)
7679 {
7680 /* Special segments, such as the PT_PHDR segment, may contain
7681 no sections, but ordinary, loadable segments should contain
7682 something. They are allowed by the ELF spec however, so only
7683 a warning is produced.
7684 Don't warn if an empty PT_LOAD contains the program headers.
7685 There is however the valid use case of embedded systems which
7686 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7687 flash memory with zeros. No warning is shown for that case. */
7688 if (segment->p_type == PT_LOAD
7689 && !map->includes_phdrs
7690 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7691 /* xgettext:c-format */
7692 _bfd_error_handler
7693 (_("%pB: warning: empty loadable segment detected"
7694 " at vaddr=%#" PRIx64 ", is this intentional?"),
7695 ibfd, (uint64_t) segment->p_vaddr);
7696
7697 map->p_vaddr_offset = segment->p_vaddr / opb;
7698 map->count = 0;
7699 *pointer_to_map = map;
7700 pointer_to_map = &map->next;
7701
7702 continue;
7703 }
7704
7705 /* Now scan the sections in the input BFD again and attempt
7706 to add their corresponding output sections to the segment map.
7707 The problem here is how to handle an output section which has
7708 been moved (ie had its LMA changed). There are four possibilities:
7709
7710 1. None of the sections have been moved.
7711 In this case we can continue to use the segment LMA from the
7712 input BFD.
7713
7714 2. All of the sections have been moved by the same amount.
7715 In this case we can change the segment's LMA to match the LMA
7716 of the first section.
7717
7718 3. Some of the sections have been moved, others have not.
7719 In this case those sections which have not been moved can be
7720 placed in the current segment which will have to have its size,
7721 and possibly its LMA changed, and a new segment or segments will
7722 have to be created to contain the other sections.
7723
7724 4. The sections have been moved, but not by the same amount.
7725 In this case we can change the segment's LMA to match the LMA
7726 of the first section and we will have to create a new segment
7727 or segments to contain the other sections.
7728
7729 In order to save time, we allocate an array to hold the section
7730 pointers that we are interested in. As these sections get assigned
7731 to a segment, they are removed from this array. */
7732
7733 amt = section_count * sizeof (asection *);
7734 sections = (asection **) bfd_malloc (amt);
7735 if (sections == NULL)
7736 return false;
7737
7738 /* Step One: Scan for segment vs section LMA conflicts.
7739 Also add the sections to the section array allocated above.
7740 Also add the sections to the current segment. In the common
7741 case, where the sections have not been moved, this means that
7742 we have completely filled the segment, and there is nothing
7743 more to do. */
7744 isec = 0;
7745 matching_lma = NULL;
7746 suggested_lma = NULL;
7747
7748 for (section = first_section, j = 0;
7749 section != NULL;
7750 section = section->next)
7751 {
7752 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7753 {
7754 output_section = section->output_section;
7755
7756 sections[j++] = section;
7757
7758 /* The Solaris native linker always sets p_paddr to 0.
7759 We try to catch that case here, and set it to the
7760 correct value. Note - some backends require that
7761 p_paddr be left as zero. */
7762 if (!p_paddr_valid
7763 && segment->p_vaddr != 0
7764 && !bed->want_p_paddr_set_to_zero
7765 && isec == 0
7766 && output_section->lma != 0
7767 && (align_power (segment->p_vaddr
7768 + (map->includes_filehdr
7769 ? iehdr->e_ehsize : 0)
7770 + (map->includes_phdrs
7771 ? iehdr->e_phnum * iehdr->e_phentsize
7772 : 0),
7773 output_section->alignment_power * opb)
7774 == (output_section->vma * opb)))
7775 map->p_paddr = segment->p_vaddr;
7776
7777 /* Match up the physical address of the segment with the
7778 LMA address of the output section. */
7779 if (is_contained_by (output_section, segment, map->p_paddr,
7780 0, opb, false)
7781 || is_note (section, segment))
7782 {
7783 if (matching_lma == NULL
7784 || output_section->lma < matching_lma->lma)
7785 matching_lma = output_section;
7786
7787 /* We assume that if the section fits within the segment
7788 then it does not overlap any other section within that
7789 segment. */
7790 map->sections[isec++] = output_section;
7791 }
7792 else if (suggested_lma == NULL)
7793 suggested_lma = output_section;
7794
7795 if (j == section_count)
7796 break;
7797 }
7798 }
7799
7800 BFD_ASSERT (j == section_count);
7801
7802 /* Step Two: Adjust the physical address of the current segment,
7803 if necessary. */
7804 if (isec == section_count)
7805 {
7806 /* All of the sections fitted within the segment as currently
7807 specified. This is the default case. Add the segment to
7808 the list of built segments and carry on to process the next
7809 program header in the input BFD. */
7810 map->count = section_count;
7811 *pointer_to_map = map;
7812 pointer_to_map = &map->next;
7813
7814 if (p_paddr_valid
7815 && !bed->want_p_paddr_set_to_zero)
7816 {
7817 bfd_vma hdr_size = 0;
7818 if (map->includes_filehdr)
7819 hdr_size = iehdr->e_ehsize;
7820 if (map->includes_phdrs)
7821 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7822
7823 /* Account for padding before the first section in the
7824 segment. */
7825 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7826 - matching_lma->lma);
7827 }
7828
7829 free (sections);
7830 continue;
7831 }
7832 else
7833 {
7834 /* Change the current segment's physical address to match
7835 the LMA of the first section that fitted, or if no
7836 section fitted, the first section. */
7837 if (matching_lma == NULL)
7838 matching_lma = suggested_lma;
7839
7840 map->p_paddr = matching_lma->lma * opb;
7841
7842 /* Offset the segment physical address from the lma
7843 to allow for space taken up by elf headers. */
7844 if (map->includes_phdrs)
7845 {
7846 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7847
7848 /* iehdr->e_phnum is just an estimate of the number
7849 of program headers that we will need. Make a note
7850 here of the number we used and the segment we chose
7851 to hold these headers, so that we can adjust the
7852 offset when we know the correct value. */
7853 phdr_adjust_num = iehdr->e_phnum;
7854 phdr_adjust_seg = map;
7855 }
7856
7857 if (map->includes_filehdr)
7858 {
7859 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7860 map->p_paddr -= iehdr->e_ehsize;
7861 /* We've subtracted off the size of headers from the
7862 first section lma, but there may have been some
7863 alignment padding before that section too. Try to
7864 account for that by adjusting the segment lma down to
7865 the same alignment. */
7866 if (segment->p_align != 0 && segment->p_align < align)
7867 align = segment->p_align;
7868 map->p_paddr &= -(align * opb);
7869 }
7870 }
7871
7872 /* Step Three: Loop over the sections again, this time assigning
7873 those that fit to the current segment and removing them from the
7874 sections array; but making sure not to leave large gaps. Once all
7875 possible sections have been assigned to the current segment it is
7876 added to the list of built segments and if sections still remain
7877 to be assigned, a new segment is constructed before repeating
7878 the loop. */
7879 isec = 0;
7880 do
7881 {
7882 map->count = 0;
7883 suggested_lma = NULL;
7884
7885 /* Fill the current segment with sections that fit. */
7886 for (j = 0; j < section_count; j++)
7887 {
7888 section = sections[j];
7889
7890 if (section == NULL)
7891 continue;
7892
7893 output_section = section->output_section;
7894
7895 BFD_ASSERT (output_section != NULL);
7896
7897 if (is_contained_by (output_section, segment, map->p_paddr,
7898 0, opb, false)
7899 || is_note (section, segment))
7900 {
7901 if (map->count == 0)
7902 {
7903 /* If the first section in a segment does not start at
7904 the beginning of the segment, then something is
7905 wrong. */
7906 if (align_power (map->p_paddr
7907 + (map->includes_filehdr
7908 ? iehdr->e_ehsize : 0)
7909 + (map->includes_phdrs
7910 ? iehdr->e_phnum * iehdr->e_phentsize
7911 : 0),
7912 output_section->alignment_power * opb)
7913 != output_section->lma * opb)
7914 goto sorry;
7915 }
7916 else
7917 {
7918 asection *prev_sec;
7919
7920 prev_sec = map->sections[map->count - 1];
7921
7922 /* If the gap between the end of the previous section
7923 and the start of this section is more than
7924 maxpagesize then we need to start a new segment. */
7925 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7926 maxpagesize)
7927 < BFD_ALIGN (output_section->lma, maxpagesize))
7928 || (prev_sec->lma + prev_sec->size
7929 > output_section->lma))
7930 {
7931 if (suggested_lma == NULL)
7932 suggested_lma = output_section;
7933
7934 continue;
7935 }
7936 }
7937
7938 map->sections[map->count++] = output_section;
7939 ++isec;
7940 sections[j] = NULL;
7941 if (segment->p_type == PT_LOAD)
7942 section->segment_mark = true;
7943 }
7944 else if (suggested_lma == NULL)
7945 suggested_lma = output_section;
7946 }
7947
7948 /* PR 23932. A corrupt input file may contain sections that cannot
7949 be assigned to any segment - because for example they have a
7950 negative size - or segments that do not contain any sections.
7951 But there are also valid reasons why a segment can be empty.
7952 So allow a count of zero. */
7953
7954 /* Add the current segment to the list of built segments. */
7955 *pointer_to_map = map;
7956 pointer_to_map = &map->next;
7957
7958 if (isec < section_count)
7959 {
7960 /* We still have not allocated all of the sections to
7961 segments. Create a new segment here, initialise it
7962 and carry on looping. */
7963 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7964 amt += section_count * sizeof (asection *);
7965 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7966 if (map == NULL)
7967 {
7968 free (sections);
7969 return false;
7970 }
7971
7972 /* Initialise the fields of the segment map. Set the physical
7973 physical address to the LMA of the first section that has
7974 not yet been assigned. */
7975 map->next = NULL;
7976 map->p_type = segment->p_type;
7977 map->p_flags = segment->p_flags;
7978 map->p_flags_valid = 1;
7979 map->p_paddr = suggested_lma->lma * opb;
7980 map->p_paddr_valid = p_paddr_valid;
7981 map->includes_filehdr = 0;
7982 map->includes_phdrs = 0;
7983 }
7984
7985 continue;
7986 sorry:
7987 bfd_set_error (bfd_error_sorry);
7988 free (sections);
7989 return false;
7990 }
7991 while (isec < section_count);
7992
7993 free (sections);
7994 }
7995
7996 elf_seg_map (obfd) = map_first;
7997
7998 /* If we had to estimate the number of program headers that were
7999 going to be needed, then check our estimate now and adjust
8000 the offset if necessary. */
8001 if (phdr_adjust_seg != NULL)
8002 {
8003 unsigned int count;
8004
8005 for (count = 0, map = map_first; map != NULL; map = map->next)
8006 count++;
8007
8008 if (count > phdr_adjust_num)
8009 phdr_adjust_seg->p_paddr
8010 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8011
8012 for (map = map_first; map != NULL; map = map->next)
8013 if (map->p_type == PT_PHDR)
8014 {
8015 bfd_vma adjust
8016 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8017 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8018 break;
8019 }
8020 }
8021
8022 #undef IS_SOLARIS_PT_INTERP
8023 #undef IS_SECTION_IN_INPUT_SEGMENT
8024 #undef INCLUDE_SECTION_IN_SEGMENT
8025 #undef SEGMENT_AFTER_SEGMENT
8026 #undef SEGMENT_OVERLAPS
8027 return true;
8028 }
8029
8030 /* Return true if p_align in the ELF program header in ABFD is valid. */
8031
8032 static bool
8033 elf_is_p_align_valid (bfd *abfd)
8034 {
8035 unsigned int i;
8036 Elf_Internal_Phdr *segment;
8037 unsigned int num_segments;
8038 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8039 bfd_size_type maxpagesize = bed->maxpagesize;
8040 bfd_size_type p_align = bed->p_align;
8041
8042 /* Return true if the default p_align value isn't set or the maximum
8043 page size is the same as the minimum page size. */
8044 if (p_align == 0 || maxpagesize == bed->minpagesize)
8045 return true;
8046
8047 /* When the default p_align value is set, p_align may be set to the
8048 default p_align value while segments are aligned to the maximum
8049 page size. In this case, the input p_align will be ignored and
8050 the maximum page size will be used to align the output segments. */
8051 segment = elf_tdata (abfd)->phdr;
8052 num_segments = elf_elfheader (abfd)->e_phnum;
8053 for (i = 0; i < num_segments; i++, segment++)
8054 if (segment->p_type == PT_LOAD
8055 && (segment->p_align != p_align
8056 || vma_page_aligned_bias (segment->p_vaddr,
8057 segment->p_offset,
8058 maxpagesize) != 0))
8059 return true;
8060
8061 return false;
8062 }
8063
8064 /* Copy ELF program header information. */
8065
8066 static bool
8067 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8068 {
8069 Elf_Internal_Ehdr *iehdr;
8070 struct elf_segment_map *map;
8071 struct elf_segment_map *map_first;
8072 struct elf_segment_map **pointer_to_map;
8073 Elf_Internal_Phdr *segment;
8074 unsigned int i;
8075 unsigned int num_segments;
8076 bool phdr_included = false;
8077 bool p_paddr_valid;
8078 bool p_palign_valid;
8079 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8080
8081 iehdr = elf_elfheader (ibfd);
8082
8083 map_first = NULL;
8084 pointer_to_map = &map_first;
8085
8086 /* If all the segment p_paddr fields are zero, don't set
8087 map->p_paddr_valid. */
8088 p_paddr_valid = false;
8089 num_segments = elf_elfheader (ibfd)->e_phnum;
8090 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8091 i < num_segments;
8092 i++, segment++)
8093 if (segment->p_paddr != 0)
8094 {
8095 p_paddr_valid = true;
8096 break;
8097 }
8098
8099 p_palign_valid = elf_is_p_align_valid (ibfd);
8100
8101 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8102 i < num_segments;
8103 i++, segment++)
8104 {
8105 asection *section;
8106 unsigned int section_count;
8107 size_t amt;
8108 Elf_Internal_Shdr *this_hdr;
8109 asection *first_section = NULL;
8110 asection *lowest_section;
8111
8112 /* Compute how many sections are in this segment. */
8113 for (section = ibfd->sections, section_count = 0;
8114 section != NULL;
8115 section = section->next)
8116 {
8117 this_hdr = &(elf_section_data(section)->this_hdr);
8118 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8119 {
8120 if (first_section == NULL)
8121 first_section = section;
8122 section_count++;
8123 }
8124 }
8125
8126 /* Allocate a segment map big enough to contain
8127 all of the sections we have selected. */
8128 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8129 amt += section_count * sizeof (asection *);
8130 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8131 if (map == NULL)
8132 return false;
8133
8134 /* Initialize the fields of the output segment map with the
8135 input segment. */
8136 map->next = NULL;
8137 map->p_type = segment->p_type;
8138 map->p_flags = segment->p_flags;
8139 map->p_flags_valid = 1;
8140 map->p_paddr = segment->p_paddr;
8141 map->p_paddr_valid = p_paddr_valid;
8142 map->p_align = segment->p_align;
8143 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8144 map->p_align_valid = (map->p_type == PT_GNU_STACK
8145 || p_palign_valid);
8146 map->p_vaddr_offset = 0;
8147
8148 if (map->p_type == PT_GNU_RELRO
8149 || map->p_type == PT_GNU_STACK)
8150 {
8151 /* The PT_GNU_RELRO segment may contain the first a few
8152 bytes in the .got.plt section even if the whole .got.plt
8153 section isn't in the PT_GNU_RELRO segment. We won't
8154 change the size of the PT_GNU_RELRO segment.
8155 Similarly, PT_GNU_STACK size is significant on uclinux
8156 systems. */
8157 map->p_size = segment->p_memsz;
8158 map->p_size_valid = 1;
8159 }
8160
8161 /* Determine if this segment contains the ELF file header
8162 and if it contains the program headers themselves. */
8163 map->includes_filehdr = (segment->p_offset == 0
8164 && segment->p_filesz >= iehdr->e_ehsize);
8165
8166 map->includes_phdrs = 0;
8167 if (! phdr_included || segment->p_type != PT_LOAD)
8168 {
8169 map->includes_phdrs =
8170 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8171 && (segment->p_offset + segment->p_filesz
8172 >= ((bfd_vma) iehdr->e_phoff
8173 + iehdr->e_phnum * iehdr->e_phentsize)));
8174
8175 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8176 phdr_included = true;
8177 }
8178
8179 lowest_section = NULL;
8180 if (section_count != 0)
8181 {
8182 unsigned int isec = 0;
8183
8184 for (section = first_section;
8185 section != NULL;
8186 section = section->next)
8187 {
8188 this_hdr = &(elf_section_data(section)->this_hdr);
8189 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8190 {
8191 map->sections[isec++] = section->output_section;
8192 if ((section->flags & SEC_ALLOC) != 0)
8193 {
8194 bfd_vma seg_off;
8195
8196 if (lowest_section == NULL
8197 || section->lma < lowest_section->lma)
8198 lowest_section = section;
8199
8200 /* Section lmas are set up from PT_LOAD header
8201 p_paddr in _bfd_elf_make_section_from_shdr.
8202 If this header has a p_paddr that disagrees
8203 with the section lma, flag the p_paddr as
8204 invalid. */
8205 if ((section->flags & SEC_LOAD) != 0)
8206 seg_off = this_hdr->sh_offset - segment->p_offset;
8207 else
8208 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8209 if (section->lma * opb - segment->p_paddr != seg_off)
8210 map->p_paddr_valid = false;
8211 }
8212 if (isec == section_count)
8213 break;
8214 }
8215 }
8216 }
8217
8218 if (section_count == 0)
8219 map->p_vaddr_offset = segment->p_vaddr / opb;
8220 else if (map->p_paddr_valid)
8221 {
8222 /* Account for padding before the first section in the segment. */
8223 bfd_vma hdr_size = 0;
8224 if (map->includes_filehdr)
8225 hdr_size = iehdr->e_ehsize;
8226 if (map->includes_phdrs)
8227 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8228
8229 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8230 - (lowest_section ? lowest_section->lma : 0));
8231 }
8232
8233 map->count = section_count;
8234 *pointer_to_map = map;
8235 pointer_to_map = &map->next;
8236 }
8237
8238 elf_seg_map (obfd) = map_first;
8239 return true;
8240 }
8241
8242 /* Copy private BFD data. This copies or rewrites ELF program header
8243 information. */
8244
8245 static bool
8246 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8247 {
8248 bfd_vma maxpagesize;
8249
8250 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8251 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8252 return true;
8253
8254 if (elf_tdata (ibfd)->phdr == NULL)
8255 return true;
8256
8257 if (ibfd->xvec == obfd->xvec)
8258 {
8259 /* Check to see if any sections in the input BFD
8260 covered by ELF program header have changed. */
8261 Elf_Internal_Phdr *segment;
8262 asection * section;
8263 asection * osec;
8264 asection * prev;
8265 unsigned int i, num_segments;
8266 Elf_Internal_Shdr *this_hdr;
8267 const struct elf_backend_data *bed;
8268
8269 bed = get_elf_backend_data (ibfd);
8270
8271 /* Regenerate the segment map if p_paddr is set to 0. */
8272 if (bed->want_p_paddr_set_to_zero)
8273 goto rewrite;
8274
8275 /* Initialize the segment mark field. */
8276 for (section = obfd->sections; section != NULL;
8277 section = section->next)
8278 section->segment_mark = false;
8279
8280 num_segments = elf_elfheader (ibfd)->e_phnum;
8281 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8282 i < num_segments;
8283 i++, segment++)
8284 {
8285 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8286 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8287 which severly confuses things, so always regenerate the segment
8288 map in this case. */
8289 if (segment->p_paddr == 0
8290 && segment->p_memsz == 0
8291 && (segment->p_type == PT_INTERP
8292 || segment->p_type == PT_DYNAMIC))
8293 goto rewrite;
8294
8295 for (section = ibfd->sections, prev = NULL;
8296 section != NULL; section = section->next)
8297 {
8298 /* We mark the output section so that we know it comes
8299 from the input BFD. */
8300 osec = section->output_section;
8301 if (osec)
8302 osec->segment_mark = true;
8303
8304 /* Check if this section is covered by the segment. */
8305 this_hdr = &(elf_section_data(section)->this_hdr);
8306 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8307 {
8308 /* FIXME: Check if its output section is changed or
8309 removed. What else do we need to check? */
8310 if (osec == NULL
8311 || section->flags != osec->flags
8312 || section->lma != osec->lma
8313 || section->vma != osec->vma
8314 || section->size != osec->size
8315 || section->rawsize != osec->rawsize
8316 || section->alignment_power != osec->alignment_power)
8317 goto rewrite;
8318
8319 /* PR 31450: If this is an allocated section then make sure
8320 that this section's vma to lma relationship is the same
8321 as previous (allocated) section's. */
8322 if (prev != NULL
8323 && section->flags & SEC_ALLOC
8324 && section->lma - section->vma != prev->lma - prev->vma)
8325 goto rewrite;
8326
8327 if (section->flags & SEC_ALLOC)
8328 prev = section;
8329 }
8330 }
8331 }
8332
8333 /* Check to see if any output section do not come from the
8334 input BFD. */
8335 for (section = obfd->sections; section != NULL;
8336 section = section->next)
8337 {
8338 if (!section->segment_mark)
8339 goto rewrite;
8340 else
8341 section->segment_mark = false;
8342 }
8343
8344 return copy_elf_program_header (ibfd, obfd);
8345 }
8346
8347 rewrite:
8348 maxpagesize = 0;
8349 if (ibfd->xvec == obfd->xvec)
8350 {
8351 /* When rewriting program header, set the output maxpagesize to
8352 the maximum alignment of input PT_LOAD segments. */
8353 Elf_Internal_Phdr *segment;
8354 unsigned int i;
8355 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8356
8357 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8358 i < num_segments;
8359 i++, segment++)
8360 if (segment->p_type == PT_LOAD
8361 && maxpagesize < segment->p_align)
8362 {
8363 /* PR 17512: file: f17299af. */
8364 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8365 /* xgettext:c-format */
8366 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8367 PRIx64 " is too large"),
8368 ibfd, (uint64_t) segment->p_align);
8369 else
8370 maxpagesize = segment->p_align;
8371 }
8372 }
8373 if (maxpagesize == 0)
8374 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8375
8376 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8377 }
8378
8379 /* Copy private section information from input to output. This function
8380 is called both by objcopy where LINK_INFO is NULL, and ld where
8381 LINK_INFO is non-NULL in the usual case but NULL for a special case
8382 when dealing with LTO IR or cloning output sections. */
8383
8384 bool
8385 _bfd_elf_copy_private_section_data (bfd *ibfd,
8386 asection *isec,
8387 bfd *obfd,
8388 asection *osec,
8389 struct bfd_link_info *link_info)
8390 {
8391 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8392 || obfd->xvec->flavour != bfd_target_elf_flavour)
8393 return true;
8394
8395 Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8396 Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8397 if (link_info == NULL)
8398 {
8399 ohdr->sh_entsize = ihdr->sh_entsize;
8400
8401 if (ihdr->sh_type == SHT_SYMTAB
8402 || ihdr->sh_type == SHT_DYNSYM
8403 || ihdr->sh_type == SHT_GNU_verneed
8404 || ihdr->sh_type == SHT_GNU_verdef)
8405 ohdr->sh_info = ihdr->sh_info;
8406 }
8407
8408 /* If this is a known ABI section, ELF section type and flags may
8409 have been set up when OSEC was created. For normal sections we
8410 allow the user to override the type and flags other than
8411 SHF_MASKOS and SHF_MASKPROC. */
8412 if (elf_section_type (osec) == SHT_PROGBITS
8413 || elf_section_type (osec) == SHT_NOTE
8414 || elf_section_type (osec) == SHT_NOBITS)
8415 elf_section_type (osec) = SHT_NULL;
8416
8417 /* For objcopy and relocatable link, copy the ELF section type from
8418 the input file if the BFD section flags are the same. (If they
8419 are different the user may be doing something like
8420 "objcopy --set-section-flags .text=alloc,data".) For a final
8421 link allow some flags that the linker clears to differ. */
8422 bool final_link = (link_info != NULL
8423 && !bfd_link_relocatable (link_info));
8424 if (elf_section_type (osec) == SHT_NULL
8425 && (osec->flags == isec->flags
8426 || (final_link
8427 && ((osec->flags ^ isec->flags)
8428 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8429 elf_section_type (osec) = elf_section_type (isec);
8430
8431 /* FIXME: Is this correct for all OS/PROC specific flags? */
8432 elf_section_flags (osec) = (elf_section_flags (isec)
8433 & (SHF_MASKOS | SHF_MASKPROC));
8434
8435 /* Copy sh_info from input for mbind section. */
8436 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8437 && elf_section_flags (isec) & SHF_GNU_MBIND)
8438 elf_section_data (osec)->this_hdr.sh_info
8439 = elf_section_data (isec)->this_hdr.sh_info;
8440
8441 /* Set things up for objcopy and relocatable link. The output
8442 SHT_GROUP section will have its elf_next_in_group pointing back
8443 to the input group members. Ignore linker created group section.
8444 See elfNN_ia64_object_p in elfxx-ia64.c. */
8445 if ((link_info == NULL
8446 || !link_info->resolve_section_groups)
8447 && (elf_sec_group (isec) == NULL
8448 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8449 {
8450 if (elf_section_flags (isec) & SHF_GROUP)
8451 elf_section_flags (osec) |= SHF_GROUP;
8452 elf_next_in_group (osec) = elf_next_in_group (isec);
8453 elf_section_data (osec)->group = elf_section_data (isec)->group;
8454 }
8455
8456 /* If not decompress, preserve SHF_COMPRESSED. */
8457 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8458 elf_section_flags (osec) |= (elf_section_flags (isec)
8459 & SHF_COMPRESSED);
8460
8461 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8462 don't use the output section of the linked-to section since it
8463 may be NULL at this point. */
8464 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8465 {
8466 ohdr->sh_flags |= SHF_LINK_ORDER;
8467 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8468 }
8469
8470 osec->use_rela_p = isec->use_rela_p;
8471
8472 return true;
8473 }
8474
8475 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8476 necessary if we are removing either the SHT_GROUP section or any of
8477 the group member sections. DISCARDED is the value that a section's
8478 output_section has if the section will be discarded, NULL when this
8479 function is called from objcopy, bfd_abs_section_ptr when called
8480 from the linker. */
8481
8482 bool
8483 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8484 {
8485 asection *isec;
8486
8487 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8488 if (elf_section_type (isec) == SHT_GROUP)
8489 {
8490 asection *first = elf_next_in_group (isec);
8491 asection *s = first;
8492 bfd_size_type removed = 0;
8493
8494 while (s != NULL)
8495 {
8496 /* If this member section is being output but the
8497 SHT_GROUP section is not, then clear the group info
8498 set up by _bfd_elf_copy_private_section_data. */
8499 if (s->output_section != discarded
8500 && isec->output_section == discarded)
8501 {
8502 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8503 elf_group_name (s->output_section) = NULL;
8504 }
8505 else
8506 {
8507 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8508 if (s->output_section == discarded
8509 && isec->output_section != discarded)
8510 {
8511 /* Conversely, if the member section is not being
8512 output but the SHT_GROUP section is, then adjust
8513 its size. */
8514 removed += 4;
8515 if (elf_sec->rel.hdr != NULL
8516 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8517 removed += 4;
8518 if (elf_sec->rela.hdr != NULL
8519 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8520 removed += 4;
8521 }
8522 else
8523 {
8524 /* Also adjust for zero-sized relocation member
8525 section. */
8526 if (elf_sec->rel.hdr != NULL
8527 && elf_sec->rel.hdr->sh_size == 0)
8528 removed += 4;
8529 if (elf_sec->rela.hdr != NULL
8530 && elf_sec->rela.hdr->sh_size == 0)
8531 removed += 4;
8532 }
8533 }
8534 s = elf_next_in_group (s);
8535 if (s == first)
8536 break;
8537 }
8538 if (removed != 0)
8539 {
8540 if (discarded != NULL)
8541 {
8542 /* If we've been called for ld -r, then we need to
8543 adjust the input section size. */
8544 if (isec->rawsize == 0)
8545 isec->rawsize = isec->size;
8546 isec->size = isec->rawsize - removed;
8547 if (isec->size <= 4)
8548 {
8549 isec->size = 0;
8550 isec->flags |= SEC_EXCLUDE;
8551 }
8552 }
8553 else if (isec->output_section != NULL)
8554 {
8555 /* Adjust the output section size when called from
8556 objcopy. */
8557 isec->output_section->size -= removed;
8558 if (isec->output_section->size <= 4)
8559 {
8560 isec->output_section->size = 0;
8561 isec->output_section->flags |= SEC_EXCLUDE;
8562 }
8563 }
8564 }
8565 }
8566
8567 return true;
8568 }
8569
8570 /* Copy private header information. */
8571
8572 bool
8573 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8574 {
8575 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8576 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8577 return true;
8578
8579 /* Copy over private BFD data if it has not already been copied.
8580 This must be done here, rather than in the copy_private_bfd_data
8581 entry point, because the latter is called after the section
8582 contents have been set, which means that the program headers have
8583 already been worked out. */
8584 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8585 {
8586 if (! copy_private_bfd_data (ibfd, obfd))
8587 return false;
8588 }
8589
8590 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8591 }
8592
8593 /* Copy private symbol information. If this symbol is in a section
8594 which we did not map into a BFD section, try to map the section
8595 index correctly. We use special macro definitions for the mapped
8596 section indices; these definitions are interpreted by the
8597 swap_out_syms function. */
8598
8599 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8600 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8601 #define MAP_STRTAB (SHN_HIOS + 3)
8602 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8603 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8604
8605 bool
8606 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8607 asymbol *isymarg,
8608 bfd *obfd,
8609 asymbol *osymarg)
8610 {
8611 elf_symbol_type *isym, *osym;
8612
8613 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8614 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8615 return true;
8616
8617 isym = elf_symbol_from (isymarg);
8618 osym = elf_symbol_from (osymarg);
8619
8620 if (isym != NULL
8621 && isym->internal_elf_sym.st_shndx != 0
8622 && osym != NULL
8623 && bfd_is_abs_section (isym->symbol.section))
8624 {
8625 unsigned int shndx;
8626
8627 shndx = isym->internal_elf_sym.st_shndx;
8628 if (shndx == elf_onesymtab (ibfd))
8629 shndx = MAP_ONESYMTAB;
8630 else if (shndx == elf_dynsymtab (ibfd))
8631 shndx = MAP_DYNSYMTAB;
8632 else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8633 shndx = MAP_STRTAB;
8634 else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8635 shndx = MAP_SHSTRTAB;
8636 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8637 shndx = MAP_SYM_SHNDX;
8638 osym->internal_elf_sym.st_shndx = shndx;
8639 }
8640
8641 return true;
8642 }
8643
8644 /* Swap out the symbols. */
8645
8646 static bool
8647 swap_out_syms (bfd *abfd,
8648 struct elf_strtab_hash **sttp,
8649 int relocatable_p,
8650 struct bfd_link_info *info)
8651 {
8652 const struct elf_backend_data *bed;
8653 unsigned int symcount;
8654 asymbol **syms;
8655 struct elf_strtab_hash *stt;
8656 Elf_Internal_Shdr *symtab_hdr;
8657 Elf_Internal_Shdr *symtab_shndx_hdr;
8658 Elf_Internal_Shdr *symstrtab_hdr;
8659 struct elf_sym_strtab *symstrtab;
8660 bfd_byte *outbound_syms;
8661 bfd_byte *outbound_shndx;
8662 unsigned long outbound_syms_index;
8663 unsigned int idx;
8664 unsigned int num_locals;
8665 size_t amt;
8666 bool name_local_sections;
8667
8668 if (!elf_map_symbols (abfd, &num_locals))
8669 return false;
8670
8671 /* Dump out the symtabs. */
8672 stt = _bfd_elf_strtab_init ();
8673 if (stt == NULL)
8674 return false;
8675
8676 bed = get_elf_backend_data (abfd);
8677 symcount = bfd_get_symcount (abfd);
8678 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8679 symtab_hdr->sh_type = SHT_SYMTAB;
8680 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8681 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8682 symtab_hdr->sh_info = num_locals + 1;
8683 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8684
8685 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8686 symstrtab_hdr->sh_type = SHT_STRTAB;
8687
8688 /* Allocate buffer to swap out the .strtab section. */
8689 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8690 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8691 {
8692 bfd_set_error (bfd_error_no_memory);
8693 _bfd_elf_strtab_free (stt);
8694 return false;
8695 }
8696
8697 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8698 || (outbound_syms = bfd_malloc (amt)) == NULL)
8699 {
8700 error_no_mem:
8701 bfd_set_error (bfd_error_no_memory);
8702 error_return:
8703 free (symstrtab);
8704 _bfd_elf_strtab_free (stt);
8705 return false;
8706 }
8707 symtab_hdr->contents = outbound_syms;
8708 outbound_syms_index = 0;
8709
8710 outbound_shndx = NULL;
8711
8712 if (elf_symtab_shndx_list (abfd))
8713 {
8714 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8715 if (symtab_shndx_hdr->sh_name != 0)
8716 {
8717 if (_bfd_mul_overflow (symcount + 1,
8718 sizeof (Elf_External_Sym_Shndx), &amt))
8719 goto error_no_mem;
8720 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8721 if (outbound_shndx == NULL)
8722 goto error_return;
8723
8724 symtab_shndx_hdr->contents = outbound_shndx;
8725 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8726 symtab_shndx_hdr->sh_size = amt;
8727 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8728 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8729 }
8730 /* FIXME: What about any other headers in the list ? */
8731 }
8732
8733 /* Now generate the data (for "contents"). */
8734 {
8735 /* Fill in zeroth symbol and swap it out. */
8736 Elf_Internal_Sym sym;
8737 sym.st_name = 0;
8738 sym.st_value = 0;
8739 sym.st_size = 0;
8740 sym.st_info = 0;
8741 sym.st_other = 0;
8742 sym.st_shndx = SHN_UNDEF;
8743 sym.st_target_internal = 0;
8744 symstrtab[outbound_syms_index].sym = sym;
8745 symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8746 outbound_syms_index++;
8747 }
8748
8749 name_local_sections
8750 = (bed->elf_backend_name_local_section_symbols
8751 && bed->elf_backend_name_local_section_symbols (abfd));
8752
8753 syms = bfd_get_outsymbols (abfd);
8754 for (idx = 0; idx < symcount; idx++)
8755 {
8756 Elf_Internal_Sym sym;
8757
8758 flagword flags = syms[idx]->flags;
8759 if (!name_local_sections
8760 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8761 {
8762 /* Local section symbols have no name. */
8763 sym.st_name = 0;
8764 }
8765 else
8766 {
8767 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8768 to get the final offset for st_name. */
8769 size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8770 if (stridx == (size_t) -1)
8771 goto error_return;
8772 sym.st_name = stridx;
8773 }
8774
8775 bfd_vma value = syms[idx]->value;
8776 elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8777 asection *sec = syms[idx]->section;
8778
8779 if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8780 {
8781 /* ELF common symbols put the alignment into the `value' field,
8782 and the size into the `size' field. This is backwards from
8783 how BFD handles it, so reverse it here. */
8784 sym.st_size = value;
8785 if (type_ptr == NULL
8786 || type_ptr->internal_elf_sym.st_value == 0)
8787 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8788 else
8789 sym.st_value = type_ptr->internal_elf_sym.st_value;
8790 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8791 }
8792 else
8793 {
8794 unsigned int shndx;
8795
8796 if (sec->output_section)
8797 {
8798 value += sec->output_offset;
8799 sec = sec->output_section;
8800 }
8801
8802 /* Don't add in the section vma for relocatable output. */
8803 if (! relocatable_p)
8804 value += sec->vma;
8805 sym.st_value = value;
8806 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8807
8808 if (bfd_is_abs_section (sec)
8809 && type_ptr != NULL
8810 && type_ptr->internal_elf_sym.st_shndx != 0)
8811 {
8812 /* This symbol is in a real ELF section which we did
8813 not create as a BFD section. Undo the mapping done
8814 by copy_private_symbol_data. */
8815 shndx = type_ptr->internal_elf_sym.st_shndx;
8816 switch (shndx)
8817 {
8818 case MAP_ONESYMTAB:
8819 shndx = elf_onesymtab (abfd);
8820 break;
8821 case MAP_DYNSYMTAB:
8822 shndx = elf_dynsymtab (abfd);
8823 break;
8824 case MAP_STRTAB:
8825 shndx = elf_strtab_sec (abfd);
8826 break;
8827 case MAP_SHSTRTAB:
8828 shndx = elf_shstrtab_sec (abfd);
8829 break;
8830 case MAP_SYM_SHNDX:
8831 if (elf_symtab_shndx_list (abfd))
8832 shndx = elf_symtab_shndx_list (abfd)->ndx;
8833 break;
8834 case SHN_COMMON:
8835 case SHN_ABS:
8836 shndx = SHN_ABS;
8837 break;
8838 default:
8839 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8840 {
8841 if (bed->symbol_section_index)
8842 shndx = bed->symbol_section_index (abfd, type_ptr);
8843 /* Otherwise just leave the index alone. */
8844 }
8845 else
8846 {
8847 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8848 _bfd_error_handler (_("%pB: \
8849 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8850 abfd, shndx);
8851 shndx = SHN_ABS;
8852 }
8853 break;
8854 }
8855 }
8856 else
8857 {
8858 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8859
8860 if (shndx == SHN_BAD)
8861 {
8862 asection *sec2;
8863
8864 /* Writing this would be a hell of a lot easier if
8865 we had some decent documentation on bfd, and
8866 knew what to expect of the library, and what to
8867 demand of applications. For example, it
8868 appears that `objcopy' might not set the
8869 section of a symbol to be a section that is
8870 actually in the output file. */
8871 sec2 = bfd_get_section_by_name (abfd, sec->name);
8872 if (sec2 != NULL)
8873 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8874 if (shndx == SHN_BAD)
8875 {
8876 /* xgettext:c-format */
8877 _bfd_error_handler
8878 (_("unable to find equivalent output section"
8879 " for symbol '%s' from section '%s'"),
8880 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8881 sec->name);
8882 bfd_set_error (bfd_error_invalid_operation);
8883 goto error_return;
8884 }
8885 }
8886 }
8887
8888 sym.st_shndx = shndx;
8889 }
8890
8891 int type;
8892 if ((flags & BSF_THREAD_LOCAL) != 0)
8893 type = STT_TLS;
8894 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8895 type = STT_GNU_IFUNC;
8896 else if ((flags & BSF_FUNCTION) != 0)
8897 type = STT_FUNC;
8898 else if ((flags & BSF_OBJECT) != 0)
8899 type = STT_OBJECT;
8900 else if ((flags & BSF_RELC) != 0)
8901 type = STT_RELC;
8902 else if ((flags & BSF_SRELC) != 0)
8903 type = STT_SRELC;
8904 else
8905 type = STT_NOTYPE;
8906
8907 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8908 type = STT_TLS;
8909
8910 /* Processor-specific types. */
8911 if (type_ptr != NULL
8912 && bed->elf_backend_get_symbol_type)
8913 type = ((*bed->elf_backend_get_symbol_type)
8914 (&type_ptr->internal_elf_sym, type));
8915
8916 if (flags & BSF_SECTION_SYM)
8917 {
8918 if (flags & BSF_GLOBAL)
8919 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8920 else
8921 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8922 }
8923 else if (bfd_is_com_section (syms[idx]->section))
8924 {
8925 if (type != STT_TLS)
8926 {
8927 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8928 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8929 ? STT_COMMON : STT_OBJECT);
8930 else
8931 type = ((flags & BSF_ELF_COMMON) != 0
8932 ? STT_COMMON : STT_OBJECT);
8933 }
8934 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8935 }
8936 else if (bfd_is_und_section (syms[idx]->section))
8937 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8938 ? STB_WEAK
8939 : STB_GLOBAL),
8940 type);
8941 else if (flags & BSF_FILE)
8942 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8943 else
8944 {
8945 int bind = STB_LOCAL;
8946
8947 if (flags & BSF_LOCAL)
8948 bind = STB_LOCAL;
8949 else if (flags & BSF_GNU_UNIQUE)
8950 bind = STB_GNU_UNIQUE;
8951 else if (flags & BSF_WEAK)
8952 bind = STB_WEAK;
8953 else if (flags & BSF_GLOBAL)
8954 bind = STB_GLOBAL;
8955
8956 sym.st_info = ELF_ST_INFO (bind, type);
8957 }
8958
8959 if (type_ptr != NULL)
8960 {
8961 sym.st_other = type_ptr->internal_elf_sym.st_other;
8962 sym.st_target_internal
8963 = type_ptr->internal_elf_sym.st_target_internal;
8964 }
8965 else
8966 {
8967 sym.st_other = 0;
8968 sym.st_target_internal = 0;
8969 }
8970
8971 symstrtab[outbound_syms_index].sym = sym;
8972 symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8973 outbound_syms_index++;
8974 }
8975
8976 /* Finalize the .strtab section. */
8977 _bfd_elf_strtab_finalize (stt);
8978
8979 /* Swap out the .strtab section. */
8980 for (idx = 0; idx < outbound_syms_index; idx++)
8981 {
8982 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8983 if (elfsym->sym.st_name != 0)
8984 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8985 elfsym->sym.st_name);
8986 if (info && info->callbacks->ctf_new_symbol)
8987 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8988 &elfsym->sym);
8989
8990 /* Inform the linker of the addition of this symbol. */
8991
8992 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8993 (outbound_syms
8994 + (elfsym->dest_index
8995 * bed->s->sizeof_sym)),
8996 NPTR_ADD (outbound_shndx,
8997 (elfsym->dest_index
8998 * sizeof (Elf_External_Sym_Shndx))));
8999 }
9000 free (symstrtab);
9001
9002 *sttp = stt;
9003 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9004 symstrtab_hdr->sh_type = SHT_STRTAB;
9005 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9006 symstrtab_hdr->sh_addr = 0;
9007 symstrtab_hdr->sh_entsize = 0;
9008 symstrtab_hdr->sh_link = 0;
9009 symstrtab_hdr->sh_info = 0;
9010 symstrtab_hdr->sh_addralign = 1;
9011
9012 return true;
9013 }
9014
9015 /* Return the number of bytes required to hold the symtab vector.
9016
9017 Note that we base it on the count plus 1, since we will null terminate
9018 the vector allocated based on this size. However, the ELF symbol table
9019 always has a dummy entry as symbol #0, so it ends up even. */
9020
9021 long
9022 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9023 {
9024 bfd_size_type symcount;
9025 long symtab_size;
9026 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9027
9028 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9029 if (symcount > LONG_MAX / sizeof (asymbol *))
9030 {
9031 bfd_set_error (bfd_error_file_too_big);
9032 return -1;
9033 }
9034 symtab_size = symcount * (sizeof (asymbol *));
9035 if (symcount == 0)
9036 symtab_size = sizeof (asymbol *);
9037 else if (!bfd_write_p (abfd))
9038 {
9039 ufile_ptr filesize = bfd_get_file_size (abfd);
9040
9041 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9042 {
9043 bfd_set_error (bfd_error_file_truncated);
9044 return -1;
9045 }
9046 }
9047
9048 return symtab_size;
9049 }
9050
9051 long
9052 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9053 {
9054 bfd_size_type symcount;
9055 long symtab_size;
9056 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9057
9058 if (elf_dynsymtab (abfd) == 0)
9059 {
9060 /* Check if there is dynamic symbol table. */
9061 symcount = elf_tdata (abfd)->dt_symtab_count;
9062 if (symcount)
9063 goto compute_symtab_size;
9064
9065 bfd_set_error (bfd_error_invalid_operation);
9066 return -1;
9067 }
9068
9069 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9070 if (symcount > LONG_MAX / sizeof (asymbol *))
9071 {
9072 bfd_set_error (bfd_error_file_too_big);
9073 return -1;
9074 }
9075
9076 compute_symtab_size:
9077 symtab_size = symcount * (sizeof (asymbol *));
9078 if (symcount == 0)
9079 symtab_size = sizeof (asymbol *);
9080 else if (!bfd_write_p (abfd))
9081 {
9082 ufile_ptr filesize = bfd_get_file_size (abfd);
9083
9084 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9085 {
9086 bfd_set_error (bfd_error_file_truncated);
9087 return -1;
9088 }
9089 }
9090
9091 return symtab_size;
9092 }
9093
9094 long
9095 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9096 {
9097 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9098 {
9099 /* Sanity check reloc section size. */
9100 ufile_ptr filesize = bfd_get_file_size (abfd);
9101
9102 if (filesize != 0)
9103 {
9104 struct bfd_elf_section_data *d = elf_section_data (asect);
9105 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9106 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9107
9108 if (rel_size + rela_size > filesize
9109 || rel_size + rela_size < rel_size)
9110 {
9111 bfd_set_error (bfd_error_file_truncated);
9112 return -1;
9113 }
9114 }
9115 }
9116
9117 #if SIZEOF_LONG == SIZEOF_INT
9118 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9119 {
9120 bfd_set_error (bfd_error_file_too_big);
9121 return -1;
9122 }
9123 #endif
9124 return (asect->reloc_count + 1L) * sizeof (arelent *);
9125 }
9126
9127 /* Canonicalize the relocs. */
9128
9129 long
9130 _bfd_elf_canonicalize_reloc (bfd *abfd,
9131 sec_ptr section,
9132 arelent **relptr,
9133 asymbol **symbols)
9134 {
9135 arelent *tblptr;
9136 unsigned int i;
9137 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9138
9139 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9140 return -1;
9141
9142 tblptr = section->relocation;
9143 for (i = 0; i < section->reloc_count; i++)
9144 *relptr++ = tblptr++;
9145
9146 *relptr = NULL;
9147
9148 return section->reloc_count;
9149 }
9150
9151 long
9152 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9153 {
9154 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9155 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9156
9157 if (symcount >= 0)
9158 abfd->symcount = symcount;
9159 return symcount;
9160 }
9161
9162 long
9163 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9164 asymbol **allocation)
9165 {
9166 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9167 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9168
9169 if (symcount >= 0)
9170 abfd->dynsymcount = symcount;
9171 return symcount;
9172 }
9173
9174 /* Return the size required for the dynamic reloc entries. Any loadable
9175 section that was actually installed in the BFD, and has type SHT_REL
9176 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9177 dynamic reloc section. */
9178
9179 long
9180 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9181 {
9182 bfd_size_type count, ext_rel_size;
9183 asection *s;
9184
9185 if (elf_dynsymtab (abfd) == 0)
9186 {
9187 bfd_set_error (bfd_error_invalid_operation);
9188 return -1;
9189 }
9190
9191 count = 1;
9192 ext_rel_size = 0;
9193 for (s = abfd->sections; s != NULL; s = s->next)
9194 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9195 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9196 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9197 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9198 {
9199 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9200 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9201 {
9202 bfd_set_error (bfd_error_file_truncated);
9203 return -1;
9204 }
9205 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9206 if (count > LONG_MAX / sizeof (arelent *))
9207 {
9208 bfd_set_error (bfd_error_file_too_big);
9209 return -1;
9210 }
9211 }
9212 if (count > 1 && !bfd_write_p (abfd))
9213 {
9214 /* Sanity check reloc section sizes. */
9215 ufile_ptr filesize = bfd_get_file_size (abfd);
9216 if (filesize != 0 && ext_rel_size > filesize)
9217 {
9218 bfd_set_error (bfd_error_file_truncated);
9219 return -1;
9220 }
9221 }
9222 return count * sizeof (arelent *);
9223 }
9224
9225 /* Canonicalize the dynamic relocation entries. Note that we return the
9226 dynamic relocations as a single block, although they are actually
9227 associated with particular sections; the interface, which was
9228 designed for SunOS style shared libraries, expects that there is only
9229 one set of dynamic relocs. Any loadable section that was actually
9230 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9231 dynamic symbol table, is considered to be a dynamic reloc section. */
9232
9233 long
9234 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9235 arelent **storage,
9236 asymbol **syms)
9237 {
9238 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9239 asection *s;
9240 long ret;
9241
9242 if (elf_dynsymtab (abfd) == 0)
9243 {
9244 bfd_set_error (bfd_error_invalid_operation);
9245 return -1;
9246 }
9247
9248 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9249 ret = 0;
9250 for (s = abfd->sections; s != NULL; s = s->next)
9251 {
9252 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9253 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9254 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9255 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9256 {
9257 arelent *p;
9258 long count, i;
9259
9260 if (! (*slurp_relocs) (abfd, s, syms, true))
9261 return -1;
9262 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9263 p = s->relocation;
9264 for (i = 0; i < count; i++)
9265 *storage++ = p++;
9266 ret += count;
9267 }
9268 }
9269
9270 *storage = NULL;
9271
9272 return ret;
9273 }
9274 \f
9275 /* Read in the version information. */
9276
9277 bool
9278 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9279 {
9280 bfd_byte *contents = NULL;
9281 unsigned int freeidx = 0;
9282 size_t amt;
9283 void *contents_addr = NULL;
9284 size_t contents_size = 0;
9285
9286 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9287 {
9288 Elf_Internal_Shdr *hdr;
9289 Elf_External_Verneed *everneed;
9290 Elf_Internal_Verneed *iverneed;
9291 unsigned int i;
9292 bfd_byte *contents_end;
9293 size_t verneed_count;
9294 size_t verneed_size;
9295
9296 if (elf_tdata (abfd)->dt_verneed != NULL)
9297 {
9298 hdr = NULL;
9299 contents = elf_tdata (abfd)->dt_verneed;
9300 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9301 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9302 }
9303 else
9304 {
9305 hdr = &elf_tdata (abfd)->dynverref_hdr;
9306
9307 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9308 {
9309 error_return_bad_verref:
9310 _bfd_error_handler
9311 (_("%pB: .gnu.version_r invalid entry"), abfd);
9312 bfd_set_error (bfd_error_bad_value);
9313 error_return_verref:
9314 elf_tdata (abfd)->verref = NULL;
9315 elf_tdata (abfd)->cverrefs = 0;
9316 goto error_return;
9317 }
9318
9319 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9320 goto error_return_verref;
9321 contents_size = hdr->sh_size;
9322 contents = _bfd_mmap_temporary (abfd, contents_size,
9323 &contents_addr, &contents_size);
9324 if (contents == NULL)
9325 goto error_return_verref;
9326
9327 verneed_size = hdr->sh_size;
9328 verneed_count = hdr->sh_info;
9329 }
9330
9331 if (_bfd_mul_overflow (verneed_count,
9332 sizeof (Elf_Internal_Verneed), &amt))
9333 {
9334 bfd_set_error (bfd_error_file_too_big);
9335 goto error_return_verref;
9336 }
9337 if (amt == 0)
9338 goto error_return_verref;
9339 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9340 if (elf_tdata (abfd)->verref == NULL)
9341 goto error_return_verref;
9342
9343 BFD_ASSERT (sizeof (Elf_External_Verneed)
9344 == sizeof (Elf_External_Vernaux));
9345 contents_end = (contents + verneed_size
9346 - sizeof (Elf_External_Verneed));
9347 everneed = (Elf_External_Verneed *) contents;
9348 iverneed = elf_tdata (abfd)->verref;
9349 for (i = 0; i < verneed_count; i++, iverneed++)
9350 {
9351 Elf_External_Vernaux *evernaux;
9352 Elf_Internal_Vernaux *ivernaux;
9353 unsigned int j;
9354
9355 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9356
9357 iverneed->vn_bfd = abfd;
9358
9359 if (elf_use_dt_symtab_p (abfd))
9360 {
9361 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9362 iverneed->vn_filename
9363 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9364 else
9365 iverneed->vn_filename = NULL;
9366 }
9367 else if (hdr == NULL)
9368 goto error_return_bad_verref;
9369 else
9370 iverneed->vn_filename
9371 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9372 iverneed->vn_file);
9373 if (iverneed->vn_filename == NULL)
9374 goto error_return_bad_verref;
9375
9376 if (iverneed->vn_cnt == 0)
9377 iverneed->vn_auxptr = NULL;
9378 else
9379 {
9380 if (_bfd_mul_overflow (iverneed->vn_cnt,
9381 sizeof (Elf_Internal_Vernaux), &amt))
9382 {
9383 bfd_set_error (bfd_error_file_too_big);
9384 goto error_return_verref;
9385 }
9386 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9387 bfd_alloc (abfd, amt);
9388 if (iverneed->vn_auxptr == NULL)
9389 goto error_return_verref;
9390 }
9391
9392 if (iverneed->vn_aux
9393 > (size_t) (contents_end - (bfd_byte *) everneed))
9394 goto error_return_bad_verref;
9395
9396 evernaux = ((Elf_External_Vernaux *)
9397 ((bfd_byte *) everneed + iverneed->vn_aux));
9398 ivernaux = iverneed->vn_auxptr;
9399 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9400 {
9401 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9402
9403 if (elf_use_dt_symtab_p (abfd))
9404 {
9405 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9406 ivernaux->vna_nodename
9407 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9408 else
9409 ivernaux->vna_nodename = NULL;
9410 }
9411 else if (hdr == NULL)
9412 goto error_return_bad_verref;
9413 else
9414 ivernaux->vna_nodename
9415 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9416 ivernaux->vna_name);
9417 if (ivernaux->vna_nodename == NULL)
9418 goto error_return_bad_verref;
9419
9420 if (ivernaux->vna_other > freeidx)
9421 freeidx = ivernaux->vna_other;
9422
9423 ivernaux->vna_nextptr = NULL;
9424 if (ivernaux->vna_next == 0)
9425 {
9426 iverneed->vn_cnt = j + 1;
9427 break;
9428 }
9429 if (j + 1 < iverneed->vn_cnt)
9430 ivernaux->vna_nextptr = ivernaux + 1;
9431
9432 if (ivernaux->vna_next
9433 > (size_t) (contents_end - (bfd_byte *) evernaux))
9434 goto error_return_bad_verref;
9435
9436 evernaux = ((Elf_External_Vernaux *)
9437 ((bfd_byte *) evernaux + ivernaux->vna_next));
9438 }
9439
9440 iverneed->vn_nextref = NULL;
9441 if (iverneed->vn_next == 0)
9442 break;
9443 if (hdr != NULL && (i + 1 < hdr->sh_info))
9444 iverneed->vn_nextref = iverneed + 1;
9445
9446 if (iverneed->vn_next
9447 > (size_t) (contents_end - (bfd_byte *) everneed))
9448 goto error_return_bad_verref;
9449
9450 everneed = ((Elf_External_Verneed *)
9451 ((bfd_byte *) everneed + iverneed->vn_next));
9452 }
9453 elf_tdata (abfd)->cverrefs = i;
9454
9455 if (contents != elf_tdata (abfd)->dt_verneed)
9456 _bfd_munmap_temporary (contents_addr, contents_size);
9457 contents = NULL;
9458 contents_addr = NULL;
9459 }
9460
9461 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9462 {
9463 Elf_Internal_Shdr *hdr;
9464 Elf_External_Verdef *everdef;
9465 Elf_Internal_Verdef *iverdef;
9466 Elf_Internal_Verdef *iverdefarr;
9467 Elf_Internal_Verdef iverdefmem;
9468 unsigned int i;
9469 unsigned int maxidx;
9470 bfd_byte *contents_end_def, *contents_end_aux;
9471 size_t verdef_count;
9472 size_t verdef_size;
9473
9474 if (elf_tdata (abfd)->dt_verdef != NULL)
9475 {
9476 hdr = NULL;
9477 contents = elf_tdata (abfd)->dt_verdef;
9478 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9479 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9480 }
9481 else
9482 {
9483 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9484
9485 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9486 {
9487 error_return_bad_verdef:
9488 _bfd_error_handler
9489 (_("%pB: .gnu.version_d invalid entry"), abfd);
9490 bfd_set_error (bfd_error_bad_value);
9491 error_return_verdef:
9492 elf_tdata (abfd)->verdef = NULL;
9493 elf_tdata (abfd)->cverdefs = 0;
9494 goto error_return;
9495 }
9496
9497 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9498 goto error_return_verdef;
9499 contents_size = hdr->sh_size;
9500 contents = _bfd_mmap_temporary (abfd, contents_size,
9501 &contents_addr, &contents_size);
9502 if (contents == NULL)
9503 goto error_return_verdef;
9504
9505 BFD_ASSERT (sizeof (Elf_External_Verdef)
9506 >= sizeof (Elf_External_Verdaux));
9507
9508 verdef_count = hdr->sh_info;
9509 verdef_size = hdr->sh_size;
9510 }
9511
9512 contents_end_def = (contents + verdef_size
9513 - sizeof (Elf_External_Verdef));
9514 contents_end_aux = (contents + verdef_size
9515 - sizeof (Elf_External_Verdaux));
9516
9517 /* We know the number of entries in the section but not the maximum
9518 index. Therefore we have to run through all entries and find
9519 the maximum. */
9520 everdef = (Elf_External_Verdef *) contents;
9521 maxidx = 0;
9522 for (i = 0; i < verdef_count; ++i)
9523 {
9524 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9525
9526 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9527 goto error_return_bad_verdef;
9528 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9529 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9530
9531 if (iverdefmem.vd_next == 0)
9532 break;
9533
9534 if (iverdefmem.vd_next
9535 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9536 goto error_return_bad_verdef;
9537
9538 everdef = ((Elf_External_Verdef *)
9539 ((bfd_byte *) everdef + iverdefmem.vd_next));
9540 }
9541
9542 if (default_imported_symver)
9543 {
9544 if (freeidx > maxidx)
9545 maxidx = ++freeidx;
9546 else
9547 freeidx = ++maxidx;
9548 }
9549 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9550 {
9551 bfd_set_error (bfd_error_file_too_big);
9552 goto error_return_verdef;
9553 }
9554
9555 if (amt == 0)
9556 goto error_return_verdef;
9557 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9558 if (elf_tdata (abfd)->verdef == NULL)
9559 goto error_return_verdef;
9560
9561 elf_tdata (abfd)->cverdefs = maxidx;
9562
9563 everdef = (Elf_External_Verdef *) contents;
9564 iverdefarr = elf_tdata (abfd)->verdef;
9565 for (i = 0; i < verdef_count; ++i)
9566 {
9567 Elf_External_Verdaux *everdaux;
9568 Elf_Internal_Verdaux *iverdaux;
9569 unsigned int j;
9570
9571 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9572
9573 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9574 goto error_return_bad_verdef;
9575
9576 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9577 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9578
9579 iverdef->vd_bfd = abfd;
9580
9581 if (iverdef->vd_cnt == 0)
9582 iverdef->vd_auxptr = NULL;
9583 else
9584 {
9585 if (_bfd_mul_overflow (iverdef->vd_cnt,
9586 sizeof (Elf_Internal_Verdaux), &amt))
9587 {
9588 bfd_set_error (bfd_error_file_too_big);
9589 goto error_return_verdef;
9590 }
9591 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9592 bfd_alloc (abfd, amt);
9593 if (iverdef->vd_auxptr == NULL)
9594 goto error_return_verdef;
9595 }
9596
9597 if (iverdef->vd_aux
9598 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9599 goto error_return_bad_verdef;
9600
9601 everdaux = ((Elf_External_Verdaux *)
9602 ((bfd_byte *) everdef + iverdef->vd_aux));
9603 iverdaux = iverdef->vd_auxptr;
9604 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9605 {
9606 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9607
9608 if (elf_use_dt_symtab_p (abfd))
9609 {
9610 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9611 iverdaux->vda_nodename
9612 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9613 else
9614 iverdaux->vda_nodename = NULL;
9615 }
9616 else
9617 iverdaux->vda_nodename
9618 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9619 iverdaux->vda_name);
9620 if (iverdaux->vda_nodename == NULL)
9621 goto error_return_bad_verdef;
9622
9623 iverdaux->vda_nextptr = NULL;
9624 if (iverdaux->vda_next == 0)
9625 {
9626 iverdef->vd_cnt = j + 1;
9627 break;
9628 }
9629 if (j + 1 < iverdef->vd_cnt)
9630 iverdaux->vda_nextptr = iverdaux + 1;
9631
9632 if (iverdaux->vda_next
9633 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9634 goto error_return_bad_verdef;
9635
9636 everdaux = ((Elf_External_Verdaux *)
9637 ((bfd_byte *) everdaux + iverdaux->vda_next));
9638 }
9639
9640 iverdef->vd_nodename = NULL;
9641 if (iverdef->vd_cnt)
9642 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9643
9644 iverdef->vd_nextdef = NULL;
9645 if (iverdef->vd_next == 0)
9646 break;
9647 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9648 iverdef->vd_nextdef = iverdef + 1;
9649
9650 everdef = ((Elf_External_Verdef *)
9651 ((bfd_byte *) everdef + iverdef->vd_next));
9652 }
9653
9654 if (contents != elf_tdata (abfd)->dt_verdef)
9655 _bfd_munmap_temporary (contents_addr, contents_size);
9656 contents = NULL;
9657 contents_addr = NULL;
9658 }
9659 else if (default_imported_symver)
9660 {
9661 if (freeidx < 3)
9662 freeidx = 3;
9663 else
9664 freeidx++;
9665
9666 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9667 {
9668 bfd_set_error (bfd_error_file_too_big);
9669 goto error_return;
9670 }
9671 if (amt == 0)
9672 goto error_return;
9673 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9674 if (elf_tdata (abfd)->verdef == NULL)
9675 goto error_return;
9676
9677 elf_tdata (abfd)->cverdefs = freeidx;
9678 }
9679
9680 /* Create a default version based on the soname. */
9681 if (default_imported_symver)
9682 {
9683 Elf_Internal_Verdef *iverdef;
9684 Elf_Internal_Verdaux *iverdaux;
9685
9686 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9687
9688 iverdef->vd_version = VER_DEF_CURRENT;
9689 iverdef->vd_flags = 0;
9690 iverdef->vd_ndx = freeidx;
9691 iverdef->vd_cnt = 1;
9692
9693 iverdef->vd_bfd = abfd;
9694
9695 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9696 if (iverdef->vd_nodename == NULL)
9697 goto error_return_verdef;
9698 iverdef->vd_nextdef = NULL;
9699 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9700 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9701 if (iverdef->vd_auxptr == NULL)
9702 goto error_return_verdef;
9703
9704 iverdaux = iverdef->vd_auxptr;
9705 iverdaux->vda_nodename = iverdef->vd_nodename;
9706 }
9707
9708 return true;
9709
9710 error_return:
9711 if (contents != elf_tdata (abfd)->dt_verneed
9712 && contents != elf_tdata (abfd)->dt_verdef)
9713 _bfd_munmap_temporary (contents_addr, contents_size);
9714 return false;
9715 }
9716 \f
9717 asymbol *
9718 _bfd_elf_make_empty_symbol (bfd *abfd)
9719 {
9720 elf_symbol_type *newsym;
9721
9722 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9723 if (!newsym)
9724 return NULL;
9725 newsym->symbol.the_bfd = abfd;
9726 return &newsym->symbol;
9727 }
9728
9729 void
9730 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9731 asymbol *symbol,
9732 symbol_info *ret)
9733 {
9734 bfd_symbol_info (symbol, ret);
9735 }
9736
9737 /* Return whether a symbol name implies a local symbol. Most targets
9738 use this function for the is_local_label_name entry point, but some
9739 override it. */
9740
9741 bool
9742 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9743 const char *name)
9744 {
9745 /* Normal local symbols start with ``.L''. */
9746 if (name[0] == '.' && name[1] == 'L')
9747 return true;
9748
9749 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9750 DWARF debugging symbols starting with ``..''. */
9751 if (name[0] == '.' && name[1] == '.')
9752 return true;
9753
9754 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9755 emitting DWARF debugging output. I suspect this is actually a
9756 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9757 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9758 underscore to be emitted on some ELF targets). For ease of use,
9759 we treat such symbols as local. */
9760 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9761 return true;
9762
9763 /* Treat assembler generated fake symbols, dollar local labels and
9764 forward-backward labels (aka local labels) as locals.
9765 These labels have the form:
9766
9767 L0^A.* (fake symbols)
9768
9769 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9770
9771 Versions which start with .L will have already been matched above,
9772 so we only need to match the rest. */
9773 if (name[0] == 'L' && ISDIGIT (name[1]))
9774 {
9775 bool ret = false;
9776 const char * p;
9777 char c;
9778
9779 for (p = name + 2; (c = *p); p++)
9780 {
9781 if (c == 1 || c == 2)
9782 {
9783 if (c == 1 && p == name + 2)
9784 /* A fake symbol. */
9785 return true;
9786
9787 /* FIXME: We are being paranoid here and treating symbols like
9788 L0^Bfoo as if there were non-local, on the grounds that the
9789 assembler will never generate them. But can any symbol
9790 containing an ASCII value in the range 1-31 ever be anything
9791 other than some kind of local ? */
9792 ret = true;
9793 }
9794
9795 if (! ISDIGIT (c))
9796 {
9797 ret = false;
9798 break;
9799 }
9800 }
9801 return ret;
9802 }
9803
9804 return false;
9805 }
9806
9807 alent *
9808 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9809 asymbol *symbol ATTRIBUTE_UNUSED)
9810 {
9811 abort ();
9812 return NULL;
9813 }
9814
9815 bool
9816 _bfd_elf_set_arch_mach (bfd *abfd,
9817 enum bfd_architecture arch,
9818 unsigned long machine)
9819 {
9820 /* If this isn't the right architecture for this backend, and this
9821 isn't the generic backend, fail. */
9822 if (arch != get_elf_backend_data (abfd)->arch
9823 && arch != bfd_arch_unknown
9824 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9825 return false;
9826
9827 return bfd_default_set_arch_mach (abfd, arch, machine);
9828 }
9829
9830 /* Find the nearest line to a particular section and offset,
9831 for error reporting. */
9832
9833 bool
9834 _bfd_elf_find_nearest_line (bfd *abfd,
9835 asymbol **symbols,
9836 asection *section,
9837 bfd_vma offset,
9838 const char **filename_ptr,
9839 const char **functionname_ptr,
9840 unsigned int *line_ptr,
9841 unsigned int *discriminator_ptr)
9842 {
9843 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9844 offset, filename_ptr,
9845 functionname_ptr, line_ptr,
9846 discriminator_ptr);
9847 }
9848
9849 /* Find the nearest line to a particular section and offset,
9850 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9851 can be optionally specified. */
9852
9853 bool
9854 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9855 const char *alt_filename,
9856 asymbol **symbols,
9857 asection *section,
9858 bfd_vma offset,
9859 const char **filename_ptr,
9860 const char **functionname_ptr,
9861 unsigned int *line_ptr,
9862 unsigned int *discriminator_ptr)
9863 {
9864 bool found;
9865
9866 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9867 section, offset, filename_ptr,
9868 functionname_ptr, line_ptr,
9869 discriminator_ptr,
9870 dwarf_debug_sections,
9871 &elf_tdata (abfd)->dwarf2_find_line_info))
9872 return true;
9873
9874 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9875 filename_ptr, functionname_ptr, line_ptr))
9876 {
9877 if (!*functionname_ptr)
9878 _bfd_elf_find_function (abfd, symbols, section, offset,
9879 *filename_ptr ? NULL : filename_ptr,
9880 functionname_ptr);
9881 return true;
9882 }
9883
9884 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9885 &found, filename_ptr,
9886 functionname_ptr, line_ptr,
9887 &elf_tdata (abfd)->line_info))
9888 return false;
9889 if (found && (*functionname_ptr || *line_ptr))
9890 return true;
9891
9892 if (symbols == NULL)
9893 return false;
9894
9895 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9896 filename_ptr, functionname_ptr))
9897 return false;
9898
9899 *line_ptr = 0;
9900 return true;
9901 }
9902
9903 /* Find the line for a symbol. */
9904
9905 bool
9906 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9907 const char **filename_ptr, unsigned int *line_ptr)
9908 {
9909 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9910 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9911 filename_ptr, NULL, line_ptr, NULL,
9912 dwarf_debug_sections,
9913 &tdata->dwarf2_find_line_info);
9914 }
9915
9916 /* After a call to bfd_find_nearest_line, successive calls to
9917 bfd_find_inliner_info can be used to get source information about
9918 each level of function inlining that terminated at the address
9919 passed to bfd_find_nearest_line. Currently this is only supported
9920 for DWARF2 with appropriate DWARF3 extensions. */
9921
9922 bool
9923 _bfd_elf_find_inliner_info (bfd *abfd,
9924 const char **filename_ptr,
9925 const char **functionname_ptr,
9926 unsigned int *line_ptr)
9927 {
9928 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9929 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9930 functionname_ptr, line_ptr,
9931 &tdata->dwarf2_find_line_info);
9932 }
9933
9934 int
9935 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9936 {
9937 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9938 int ret = bed->s->sizeof_ehdr;
9939
9940 if (!bfd_link_relocatable (info))
9941 {
9942 bfd_size_type phdr_size = elf_program_header_size (abfd);
9943
9944 if (phdr_size == (bfd_size_type) -1)
9945 {
9946 struct elf_segment_map *m;
9947
9948 phdr_size = 0;
9949 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9950 phdr_size += bed->s->sizeof_phdr;
9951
9952 if (phdr_size == 0)
9953 phdr_size = get_program_header_size (abfd, info);
9954 }
9955
9956 elf_program_header_size (abfd) = phdr_size;
9957 ret += phdr_size;
9958 }
9959
9960 return ret;
9961 }
9962
9963 bool
9964 _bfd_elf_set_section_contents (bfd *abfd,
9965 sec_ptr section,
9966 const void *location,
9967 file_ptr offset,
9968 bfd_size_type count)
9969 {
9970 Elf_Internal_Shdr *hdr;
9971
9972 if (! abfd->output_has_begun
9973 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9974 return false;
9975
9976 if (!count)
9977 return true;
9978
9979 hdr = &elf_section_data (section)->this_hdr;
9980 if (hdr->sh_offset == (file_ptr) -1)
9981 {
9982 unsigned char *contents;
9983
9984 if (bfd_section_is_ctf (section))
9985 /* Nothing to do with this section: the contents are generated
9986 later. */
9987 return true;
9988
9989 if ((offset + count) > hdr->sh_size)
9990 {
9991 _bfd_error_handler
9992 (_("%pB:%pA: error: attempting to write"
9993 " over the end of the section"),
9994 abfd, section);
9995
9996 bfd_set_error (bfd_error_invalid_operation);
9997 return false;
9998 }
9999
10000 contents = hdr->contents;
10001 if (contents == NULL)
10002 {
10003 _bfd_error_handler
10004 (_("%pB:%pA: error: attempting to write"
10005 " section into an empty buffer"),
10006 abfd, section);
10007
10008 bfd_set_error (bfd_error_invalid_operation);
10009 return false;
10010 }
10011
10012 memcpy (contents + offset, location, count);
10013 return true;
10014 }
10015
10016 return _bfd_generic_set_section_contents (abfd, section,
10017 location, offset, count);
10018 }
10019
10020 bool
10021 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10022 arelent *cache_ptr ATTRIBUTE_UNUSED,
10023 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10024 {
10025 abort ();
10026 return false;
10027 }
10028
10029 /* Try to convert a non-ELF reloc into an ELF one. */
10030
10031 bool
10032 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10033 {
10034 /* Check whether we really have an ELF howto. */
10035
10036 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10037 {
10038 bfd_reloc_code_real_type code;
10039 reloc_howto_type *howto;
10040
10041 /* Alien reloc: Try to determine its type to replace it with an
10042 equivalent ELF reloc. */
10043
10044 if (areloc->howto->pc_relative)
10045 {
10046 switch (areloc->howto->bitsize)
10047 {
10048 case 8:
10049 code = BFD_RELOC_8_PCREL;
10050 break;
10051 case 12:
10052 code = BFD_RELOC_12_PCREL;
10053 break;
10054 case 16:
10055 code = BFD_RELOC_16_PCREL;
10056 break;
10057 case 24:
10058 code = BFD_RELOC_24_PCREL;
10059 break;
10060 case 32:
10061 code = BFD_RELOC_32_PCREL;
10062 break;
10063 case 64:
10064 code = BFD_RELOC_64_PCREL;
10065 break;
10066 default:
10067 goto fail;
10068 }
10069
10070 howto = bfd_reloc_type_lookup (abfd, code);
10071
10072 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10073 {
10074 if (howto->pcrel_offset)
10075 areloc->addend += areloc->address;
10076 else
10077 areloc->addend -= areloc->address; /* addend is unsigned!! */
10078 }
10079 }
10080 else
10081 {
10082 switch (areloc->howto->bitsize)
10083 {
10084 case 8:
10085 code = BFD_RELOC_8;
10086 break;
10087 case 14:
10088 code = BFD_RELOC_14;
10089 break;
10090 case 16:
10091 code = BFD_RELOC_16;
10092 break;
10093 case 26:
10094 code = BFD_RELOC_26;
10095 break;
10096 case 32:
10097 code = BFD_RELOC_32;
10098 break;
10099 case 64:
10100 code = BFD_RELOC_64;
10101 break;
10102 default:
10103 goto fail;
10104 }
10105
10106 howto = bfd_reloc_type_lookup (abfd, code);
10107 }
10108
10109 if (howto)
10110 areloc->howto = howto;
10111 else
10112 goto fail;
10113 }
10114
10115 return true;
10116
10117 fail:
10118 /* xgettext:c-format */
10119 _bfd_error_handler (_("%pB: %s unsupported"),
10120 abfd, areloc->howto->name);
10121 bfd_set_error (bfd_error_sorry);
10122 return false;
10123 }
10124
10125 bool
10126 _bfd_elf_free_cached_info (bfd *abfd)
10127 {
10128 struct elf_obj_tdata *tdata;
10129
10130 if ((bfd_get_format (abfd) == bfd_object
10131 || bfd_get_format (abfd) == bfd_core)
10132 && (tdata = elf_tdata (abfd)) != NULL)
10133 {
10134 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10135 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10136 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10137 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10138 _bfd_stab_cleanup (abfd, &tdata->line_info);
10139 for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10140 {
10141 _bfd_elf_munmap_section_contents (sec, sec->contents);
10142 if (!sec->alloced)
10143 {
10144 free (elf_section_data (sec)->this_hdr.contents);
10145 elf_section_data (sec)->this_hdr.contents = NULL;
10146 }
10147 free (elf_section_data (sec)->relocs);
10148 elf_section_data (sec)->relocs = NULL;
10149 if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10150 {
10151 struct eh_frame_sec_info *sec_info
10152 = elf_section_data (sec)->sec_info;
10153 free (sec_info->cies);
10154 }
10155 }
10156 free (tdata->symtab_hdr.contents);
10157 tdata->symtab_hdr.contents = NULL;
10158 }
10159
10160 return _bfd_generic_bfd_free_cached_info (abfd);
10161 }
10162
10163 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10164 in the relocation's offset. Thus we cannot allow any sort of sanity
10165 range-checking to interfere. There is nothing else to do in processing
10166 this reloc. */
10167
10168 bfd_reloc_status_type
10169 _bfd_elf_rel_vtable_reloc_fn
10170 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10171 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10172 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10173 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10174 {
10175 return bfd_reloc_ok;
10176 }
10177 \f
10178 /* Elf core file support. Much of this only works on native
10179 toolchains, since we rely on knowing the
10180 machine-dependent procfs structure in order to pick
10181 out details about the corefile. */
10182
10183 #ifdef HAVE_SYS_PROCFS_H
10184 # include <sys/procfs.h>
10185 #endif
10186
10187 /* Return a PID that identifies a "thread" for threaded cores, or the
10188 PID of the main process for non-threaded cores. */
10189
10190 static int
10191 elfcore_make_pid (bfd *abfd)
10192 {
10193 int pid;
10194
10195 pid = elf_tdata (abfd)->core->lwpid;
10196 if (pid == 0)
10197 pid = elf_tdata (abfd)->core->pid;
10198
10199 return pid;
10200 }
10201
10202 /* If there isn't a section called NAME, make one, using data from
10203 SECT. Note, this function will generate a reference to NAME, so
10204 you shouldn't deallocate or overwrite it. */
10205
10206 static bool
10207 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10208 {
10209 asection *sect2;
10210
10211 if (bfd_get_section_by_name (abfd, name) != NULL)
10212 return true;
10213
10214 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10215 if (sect2 == NULL)
10216 return false;
10217
10218 sect2->size = sect->size;
10219 sect2->filepos = sect->filepos;
10220 sect2->alignment_power = sect->alignment_power;
10221 return true;
10222 }
10223
10224 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10225 actually creates up to two pseudosections:
10226 - For the single-threaded case, a section named NAME, unless
10227 such a section already exists.
10228 - For the multi-threaded case, a section named "NAME/PID", where
10229 PID is elfcore_make_pid (abfd).
10230 Both pseudosections have identical contents. */
10231 bool
10232 _bfd_elfcore_make_pseudosection (bfd *abfd,
10233 char *name,
10234 size_t size,
10235 ufile_ptr filepos)
10236 {
10237 char buf[100];
10238 char *threaded_name;
10239 size_t len;
10240 asection *sect;
10241
10242 /* Build the section name. */
10243
10244 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10245 len = strlen (buf) + 1;
10246 threaded_name = (char *) bfd_alloc (abfd, len);
10247 if (threaded_name == NULL)
10248 return false;
10249 memcpy (threaded_name, buf, len);
10250
10251 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10252 SEC_HAS_CONTENTS);
10253 if (sect == NULL)
10254 return false;
10255 sect->size = size;
10256 sect->filepos = filepos;
10257 sect->alignment_power = 2;
10258
10259 return elfcore_maybe_make_sect (abfd, name, sect);
10260 }
10261
10262 static bool
10263 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10264 size_t offs)
10265 {
10266 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10267 SEC_HAS_CONTENTS);
10268
10269 if (sect == NULL)
10270 return false;
10271
10272 sect->size = note->descsz - offs;
10273 sect->filepos = note->descpos + offs;
10274 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10275
10276 return true;
10277 }
10278
10279 /* prstatus_t exists on:
10280 solaris 2.5+
10281 linux 2.[01] + glibc
10282 unixware 4.2
10283 */
10284
10285 #if defined (HAVE_PRSTATUS_T)
10286
10287 static bool
10288 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10289 {
10290 size_t size;
10291 int offset;
10292
10293 if (note->descsz == sizeof (prstatus_t))
10294 {
10295 prstatus_t prstat;
10296
10297 size = sizeof (prstat.pr_reg);
10298 offset = offsetof (prstatus_t, pr_reg);
10299 memcpy (&prstat, note->descdata, sizeof (prstat));
10300
10301 /* Do not overwrite the core signal if it
10302 has already been set by another thread. */
10303 if (elf_tdata (abfd)->core->signal == 0)
10304 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10305 if (elf_tdata (abfd)->core->pid == 0)
10306 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10307
10308 /* pr_who exists on:
10309 solaris 2.5+
10310 unixware 4.2
10311 pr_who doesn't exist on:
10312 linux 2.[01]
10313 */
10314 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10315 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10316 #else
10317 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10318 #endif
10319 }
10320 #if defined (HAVE_PRSTATUS32_T)
10321 else if (note->descsz == sizeof (prstatus32_t))
10322 {
10323 /* 64-bit host, 32-bit corefile */
10324 prstatus32_t prstat;
10325
10326 size = sizeof (prstat.pr_reg);
10327 offset = offsetof (prstatus32_t, pr_reg);
10328 memcpy (&prstat, note->descdata, sizeof (prstat));
10329
10330 /* Do not overwrite the core signal if it
10331 has already been set by another thread. */
10332 if (elf_tdata (abfd)->core->signal == 0)
10333 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10334 if (elf_tdata (abfd)->core->pid == 0)
10335 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10336
10337 /* pr_who exists on:
10338 solaris 2.5+
10339 unixware 4.2
10340 pr_who doesn't exist on:
10341 linux 2.[01]
10342 */
10343 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10344 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10345 #else
10346 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10347 #endif
10348 }
10349 #endif /* HAVE_PRSTATUS32_T */
10350 else
10351 {
10352 /* Fail - we don't know how to handle any other
10353 note size (ie. data object type). */
10354 return true;
10355 }
10356
10357 /* Make a ".reg/999" section and a ".reg" section. */
10358 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10359 size, note->descpos + offset);
10360 }
10361 #endif /* defined (HAVE_PRSTATUS_T) */
10362
10363 /* Create a pseudosection containing the exact contents of NOTE. */
10364 static bool
10365 elfcore_make_note_pseudosection (bfd *abfd,
10366 char *name,
10367 Elf_Internal_Note *note)
10368 {
10369 return _bfd_elfcore_make_pseudosection (abfd, name,
10370 note->descsz, note->descpos);
10371 }
10372
10373 /* There isn't a consistent prfpregset_t across platforms,
10374 but it doesn't matter, because we don't have to pick this
10375 data structure apart. */
10376
10377 static bool
10378 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10379 {
10380 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10381 }
10382
10383 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10384 type of NT_PRXFPREG. Just include the whole note's contents
10385 literally. */
10386
10387 static bool
10388 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10389 {
10390 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10391 }
10392
10393 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10394 with a note type of NT_X86_XSTATE. Just include the whole note's
10395 contents literally. */
10396
10397 static bool
10398 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10399 {
10400 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10401 }
10402
10403 static bool
10404 elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10405 {
10406 return elfcore_make_note_pseudosection (abfd, ".reg-ssp", note);
10407 }
10408
10409 static bool
10410 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10411 {
10412 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10413 }
10414
10415 static bool
10416 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10417 {
10418 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10419 }
10420
10421 static bool
10422 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10423 {
10424 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10425 }
10426
10427 static bool
10428 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10429 {
10430 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10431 }
10432
10433 static bool
10434 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10435 {
10436 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10437 }
10438
10439 static bool
10440 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10441 {
10442 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10443 }
10444
10445 static bool
10446 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10447 {
10448 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10449 }
10450
10451 static bool
10452 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10453 {
10454 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10455 }
10456
10457 static bool
10458 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10459 {
10460 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10461 }
10462
10463 static bool
10464 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10465 {
10466 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10467 }
10468
10469 static bool
10470 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10471 {
10472 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10473 }
10474
10475 static bool
10476 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10477 {
10478 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10479 }
10480
10481 static bool
10482 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10483 {
10484 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10485 }
10486
10487 static bool
10488 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10489 {
10490 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10491 }
10492
10493 static bool
10494 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10495 {
10496 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10497 }
10498
10499 static bool
10500 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10501 {
10502 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10503 }
10504
10505 static bool
10506 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10507 {
10508 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10509 }
10510
10511 static bool
10512 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10513 {
10514 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10515 }
10516
10517 static bool
10518 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10519 {
10520 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10521 }
10522
10523 static bool
10524 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10525 {
10526 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10527 }
10528
10529 static bool
10530 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10531 {
10532 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10533 }
10534
10535 static bool
10536 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10537 {
10538 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10539 }
10540
10541 static bool
10542 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10543 {
10544 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10545 }
10546
10547 static bool
10548 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10549 {
10550 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10551 }
10552
10553 static bool
10554 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10555 {
10556 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10557 }
10558
10559 static bool
10560 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10561 {
10562 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10563 }
10564
10565 static bool
10566 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10567 {
10568 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10569 }
10570
10571 static bool
10572 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10573 {
10574 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10575 }
10576
10577 static bool
10578 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10579 {
10580 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10581 }
10582
10583 static bool
10584 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10585 {
10586 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10587 }
10588
10589 static bool
10590 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10591 {
10592 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10593 }
10594
10595 static bool
10596 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10597 {
10598 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10599 }
10600
10601 static bool
10602 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10603 {
10604 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10605 }
10606
10607 static bool
10608 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10609 {
10610 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10611 }
10612
10613 static bool
10614 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10615 {
10616 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10617 note);
10618 }
10619
10620 static bool
10621 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10622 {
10623 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10624 }
10625
10626 static bool
10627 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10628 {
10629 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10630 }
10631
10632 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10633 successful, otherwise return FALSE. */
10634
10635 static bool
10636 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10637 {
10638 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10639 }
10640
10641 /* Convert NOTE into a bfd_section called ".reg-aarch-gcs". Return TRUE if
10642 successful, otherwise return FALSE. */
10643
10644 static bool
10645 elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10646 {
10647 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-gcs", note);
10648 }
10649
10650 static bool
10651 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10652 {
10653 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10654 }
10655
10656 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10657 successful otherwise, return FALSE. */
10658
10659 static bool
10660 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10661 {
10662 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10663 }
10664
10665 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10666 successful otherwise, return FALSE. */
10667
10668 static bool
10669 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10670 {
10671 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10672 }
10673
10674 static bool
10675 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10676 {
10677 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10678 }
10679
10680 static bool
10681 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10682 {
10683 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10684 }
10685
10686 static bool
10687 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10688 {
10689 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10690 }
10691
10692 static bool
10693 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10694 {
10695 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10696 }
10697
10698 #if defined (HAVE_PRPSINFO_T)
10699 typedef prpsinfo_t elfcore_psinfo_t;
10700 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10701 typedef prpsinfo32_t elfcore_psinfo32_t;
10702 #endif
10703 #endif
10704
10705 #if defined (HAVE_PSINFO_T)
10706 typedef psinfo_t elfcore_psinfo_t;
10707 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10708 typedef psinfo32_t elfcore_psinfo32_t;
10709 #endif
10710 #endif
10711
10712 /* return a malloc'ed copy of a string at START which is at
10713 most MAX bytes long, possibly without a terminating '\0'.
10714 the copy will always have a terminating '\0'. */
10715
10716 char *
10717 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10718 {
10719 char *dups;
10720 char *end = (char *) memchr (start, '\0', max);
10721 size_t len;
10722
10723 if (end == NULL)
10724 len = max;
10725 else
10726 len = end - start;
10727
10728 dups = (char *) bfd_alloc (abfd, len + 1);
10729 if (dups == NULL)
10730 return NULL;
10731
10732 memcpy (dups, start, len);
10733 dups[len] = '\0';
10734
10735 return dups;
10736 }
10737
10738 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10739 static bool
10740 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10741 {
10742 if (note->descsz == sizeof (elfcore_psinfo_t))
10743 {
10744 elfcore_psinfo_t psinfo;
10745
10746 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10747
10748 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10749 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10750 #endif
10751 elf_tdata (abfd)->core->program
10752 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10753 sizeof (psinfo.pr_fname));
10754
10755 elf_tdata (abfd)->core->command
10756 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10757 sizeof (psinfo.pr_psargs));
10758 }
10759 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10760 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10761 {
10762 /* 64-bit host, 32-bit corefile */
10763 elfcore_psinfo32_t psinfo;
10764
10765 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10766
10767 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10768 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10769 #endif
10770 elf_tdata (abfd)->core->program
10771 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10772 sizeof (psinfo.pr_fname));
10773
10774 elf_tdata (abfd)->core->command
10775 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10776 sizeof (psinfo.pr_psargs));
10777 }
10778 #endif
10779
10780 else
10781 {
10782 /* Fail - we don't know how to handle any other
10783 note size (ie. data object type). */
10784 return true;
10785 }
10786
10787 /* Note that for some reason, a spurious space is tacked
10788 onto the end of the args in some (at least one anyway)
10789 implementations, so strip it off if it exists. */
10790
10791 {
10792 char *command = elf_tdata (abfd)->core->command;
10793 int n = strlen (command);
10794
10795 if (0 < n && command[n - 1] == ' ')
10796 command[n - 1] = '\0';
10797 }
10798
10799 return true;
10800 }
10801 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10802
10803 #if defined (HAVE_PSTATUS_T)
10804 static bool
10805 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10806 {
10807 if (note->descsz == sizeof (pstatus_t)
10808 #if defined (HAVE_PXSTATUS_T)
10809 || note->descsz == sizeof (pxstatus_t)
10810 #endif
10811 )
10812 {
10813 pstatus_t pstat;
10814
10815 memcpy (&pstat, note->descdata, sizeof (pstat));
10816
10817 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10818 }
10819 #if defined (HAVE_PSTATUS32_T)
10820 else if (note->descsz == sizeof (pstatus32_t))
10821 {
10822 /* 64-bit host, 32-bit corefile */
10823 pstatus32_t pstat;
10824
10825 memcpy (&pstat, note->descdata, sizeof (pstat));
10826
10827 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10828 }
10829 #endif
10830 /* Could grab some more details from the "representative"
10831 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10832 NT_LWPSTATUS note, presumably. */
10833
10834 return true;
10835 }
10836 #endif /* defined (HAVE_PSTATUS_T) */
10837
10838 #if defined (HAVE_LWPSTATUS_T)
10839 static bool
10840 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10841 {
10842 lwpstatus_t lwpstat;
10843 char buf[100];
10844 char *name;
10845 size_t len;
10846 asection *sect;
10847
10848 if (note->descsz != sizeof (lwpstat)
10849 #if defined (HAVE_LWPXSTATUS_T)
10850 && note->descsz != sizeof (lwpxstatus_t)
10851 #endif
10852 )
10853 return true;
10854
10855 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10856
10857 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10858 /* Do not overwrite the core signal if it has already been set by
10859 another thread. */
10860 if (elf_tdata (abfd)->core->signal == 0)
10861 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10862
10863 /* Make a ".reg/999" section. */
10864
10865 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10866 len = strlen (buf) + 1;
10867 name = bfd_alloc (abfd, len);
10868 if (name == NULL)
10869 return false;
10870 memcpy (name, buf, len);
10871
10872 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10873 if (sect == NULL)
10874 return false;
10875
10876 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10877 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10878 sect->filepos = note->descpos
10879 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10880 #endif
10881
10882 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10883 sect->size = sizeof (lwpstat.pr_reg);
10884 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10885 #endif
10886
10887 sect->alignment_power = 2;
10888
10889 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10890 return false;
10891
10892 /* Make a ".reg2/999" section */
10893
10894 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10895 len = strlen (buf) + 1;
10896 name = bfd_alloc (abfd, len);
10897 if (name == NULL)
10898 return false;
10899 memcpy (name, buf, len);
10900
10901 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10902 if (sect == NULL)
10903 return false;
10904
10905 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10906 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10907 sect->filepos = note->descpos
10908 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10909 #endif
10910
10911 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10912 sect->size = sizeof (lwpstat.pr_fpreg);
10913 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10914 #endif
10915
10916 sect->alignment_power = 2;
10917
10918 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10919 }
10920 #endif /* defined (HAVE_LWPSTATUS_T) */
10921
10922 /* These constants, and the structure offsets used below, are defined by
10923 Cygwin's core_dump.h */
10924 #define NOTE_INFO_PROCESS 1
10925 #define NOTE_INFO_THREAD 2
10926 #define NOTE_INFO_MODULE 3
10927 #define NOTE_INFO_MODULE64 4
10928
10929 static bool
10930 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10931 {
10932 char buf[30];
10933 char *name;
10934 size_t len;
10935 unsigned int name_size;
10936 asection *sect;
10937 unsigned int type;
10938 int is_active_thread;
10939 bfd_vma base_addr;
10940
10941 if (note->descsz < 4)
10942 return true;
10943
10944 if (! startswith (note->namedata, "win32"))
10945 return true;
10946
10947 type = bfd_get_32 (abfd, note->descdata);
10948
10949 struct
10950 {
10951 const char *type_name;
10952 unsigned long min_size;
10953 } size_check[] =
10954 {
10955 { "NOTE_INFO_PROCESS", 12 },
10956 { "NOTE_INFO_THREAD", 12 },
10957 { "NOTE_INFO_MODULE", 12 },
10958 { "NOTE_INFO_MODULE64", 16 },
10959 };
10960
10961 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10962 return true;
10963
10964 if (note->descsz < size_check[type - 1].min_size)
10965 {
10966 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10967 " is too small"),
10968 abfd, size_check[type - 1].type_name, note->descsz);
10969 return true;
10970 }
10971
10972 switch (type)
10973 {
10974 case NOTE_INFO_PROCESS:
10975 /* FIXME: need to add ->core->command. */
10976 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10977 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10978 break;
10979
10980 case NOTE_INFO_THREAD:
10981 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10982 structure. */
10983 /* thread_info.tid */
10984 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10985
10986 len = strlen (buf) + 1;
10987 name = (char *) bfd_alloc (abfd, len);
10988 if (name == NULL)
10989 return false;
10990
10991 memcpy (name, buf, len);
10992
10993 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10994 if (sect == NULL)
10995 return false;
10996
10997 /* sizeof (thread_info.thread_context) */
10998 sect->size = note->descsz - 12;
10999 /* offsetof (thread_info.thread_context) */
11000 sect->filepos = note->descpos + 12;
11001 sect->alignment_power = 2;
11002
11003 /* thread_info.is_active_thread */
11004 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11005
11006 if (is_active_thread)
11007 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11008 return false;
11009 break;
11010
11011 case NOTE_INFO_MODULE:
11012 case NOTE_INFO_MODULE64:
11013 /* Make a ".module/xxxxxxxx" section. */
11014 if (type == NOTE_INFO_MODULE)
11015 {
11016 /* module_info.base_address */
11017 base_addr = bfd_get_32 (abfd, note->descdata + 4);
11018 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11019 /* module_info.module_name_size */
11020 name_size = bfd_get_32 (abfd, note->descdata + 8);
11021 }
11022 else /* NOTE_INFO_MODULE64 */
11023 {
11024 /* module_info.base_address */
11025 base_addr = bfd_get_64 (abfd, note->descdata + 4);
11026 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11027 /* module_info.module_name_size */
11028 name_size = bfd_get_32 (abfd, note->descdata + 12);
11029 }
11030
11031 len = strlen (buf) + 1;
11032 name = (char *) bfd_alloc (abfd, len);
11033 if (name == NULL)
11034 return false;
11035
11036 memcpy (name, buf, len);
11037
11038 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11039
11040 if (sect == NULL)
11041 return false;
11042
11043 if (note->descsz < 12 + name_size)
11044 {
11045 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11046 " is too small to contain a name of size %u"),
11047 abfd, note->descsz, name_size);
11048 return true;
11049 }
11050
11051 sect->size = note->descsz;
11052 sect->filepos = note->descpos;
11053 sect->alignment_power = 2;
11054 break;
11055
11056 default:
11057 return true;
11058 }
11059
11060 return true;
11061 }
11062
11063 static bool
11064 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11065 {
11066 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11067
11068 switch (note->type)
11069 {
11070 default:
11071 return true;
11072
11073 case NT_PRSTATUS:
11074 if (bed->elf_backend_grok_prstatus)
11075 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11076 return true;
11077 #if defined (HAVE_PRSTATUS_T)
11078 return elfcore_grok_prstatus (abfd, note);
11079 #else
11080 return true;
11081 #endif
11082
11083 #if defined (HAVE_PSTATUS_T)
11084 case NT_PSTATUS:
11085 return elfcore_grok_pstatus (abfd, note);
11086 #endif
11087
11088 #if defined (HAVE_LWPSTATUS_T)
11089 case NT_LWPSTATUS:
11090 return elfcore_grok_lwpstatus (abfd, note);
11091 #endif
11092
11093 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11094 return elfcore_grok_prfpreg (abfd, note);
11095
11096 case NT_WIN32PSTATUS:
11097 return elfcore_grok_win32pstatus (abfd, note);
11098
11099 case NT_PRXFPREG: /* Linux SSE extension */
11100 if (note->namesz == 6
11101 && strcmp (note->namedata, "LINUX") == 0)
11102 return elfcore_grok_prxfpreg (abfd, note);
11103 else
11104 return true;
11105
11106 case NT_X86_XSTATE: /* Linux XSAVE extension */
11107 if (note->namesz == 6
11108 && strcmp (note->namedata, "LINUX") == 0)
11109 return elfcore_grok_xstatereg (abfd, note);
11110 else
11111 return true;
11112
11113 case NT_X86_SHSTK: /* Linux CET extension. */
11114 if (note->namesz == 6
11115 && strcmp (note->namedata, "LINUX") == 0)
11116 return elfcore_grok_sspreg (abfd, note);
11117 else
11118 return true;
11119
11120 case NT_PPC_VMX:
11121 if (note->namesz == 6
11122 && strcmp (note->namedata, "LINUX") == 0)
11123 return elfcore_grok_ppc_vmx (abfd, note);
11124 else
11125 return true;
11126
11127 case NT_PPC_VSX:
11128 if (note->namesz == 6
11129 && strcmp (note->namedata, "LINUX") == 0)
11130 return elfcore_grok_ppc_vsx (abfd, note);
11131 else
11132 return true;
11133
11134 case NT_PPC_TAR:
11135 if (note->namesz == 6
11136 && strcmp (note->namedata, "LINUX") == 0)
11137 return elfcore_grok_ppc_tar (abfd, note);
11138 else
11139 return true;
11140
11141 case NT_PPC_PPR:
11142 if (note->namesz == 6
11143 && strcmp (note->namedata, "LINUX") == 0)
11144 return elfcore_grok_ppc_ppr (abfd, note);
11145 else
11146 return true;
11147
11148 case NT_PPC_DSCR:
11149 if (note->namesz == 6
11150 && strcmp (note->namedata, "LINUX") == 0)
11151 return elfcore_grok_ppc_dscr (abfd, note);
11152 else
11153 return true;
11154
11155 case NT_PPC_EBB:
11156 if (note->namesz == 6
11157 && strcmp (note->namedata, "LINUX") == 0)
11158 return elfcore_grok_ppc_ebb (abfd, note);
11159 else
11160 return true;
11161
11162 case NT_PPC_PMU:
11163 if (note->namesz == 6
11164 && strcmp (note->namedata, "LINUX") == 0)
11165 return elfcore_grok_ppc_pmu (abfd, note);
11166 else
11167 return true;
11168
11169 case NT_PPC_TM_CGPR:
11170 if (note->namesz == 6
11171 && strcmp (note->namedata, "LINUX") == 0)
11172 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11173 else
11174 return true;
11175
11176 case NT_PPC_TM_CFPR:
11177 if (note->namesz == 6
11178 && strcmp (note->namedata, "LINUX") == 0)
11179 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11180 else
11181 return true;
11182
11183 case NT_PPC_TM_CVMX:
11184 if (note->namesz == 6
11185 && strcmp (note->namedata, "LINUX") == 0)
11186 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11187 else
11188 return true;
11189
11190 case NT_PPC_TM_CVSX:
11191 if (note->namesz == 6
11192 && strcmp (note->namedata, "LINUX") == 0)
11193 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11194 else
11195 return true;
11196
11197 case NT_PPC_TM_SPR:
11198 if (note->namesz == 6
11199 && strcmp (note->namedata, "LINUX") == 0)
11200 return elfcore_grok_ppc_tm_spr (abfd, note);
11201 else
11202 return true;
11203
11204 case NT_PPC_TM_CTAR:
11205 if (note->namesz == 6
11206 && strcmp (note->namedata, "LINUX") == 0)
11207 return elfcore_grok_ppc_tm_ctar (abfd, note);
11208 else
11209 return true;
11210
11211 case NT_PPC_TM_CPPR:
11212 if (note->namesz == 6
11213 && strcmp (note->namedata, "LINUX") == 0)
11214 return elfcore_grok_ppc_tm_cppr (abfd, note);
11215 else
11216 return true;
11217
11218 case NT_PPC_TM_CDSCR:
11219 if (note->namesz == 6
11220 && strcmp (note->namedata, "LINUX") == 0)
11221 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11222 else
11223 return true;
11224
11225 case NT_S390_HIGH_GPRS:
11226 if (note->namesz == 6
11227 && strcmp (note->namedata, "LINUX") == 0)
11228 return elfcore_grok_s390_high_gprs (abfd, note);
11229 else
11230 return true;
11231
11232 case NT_S390_TIMER:
11233 if (note->namesz == 6
11234 && strcmp (note->namedata, "LINUX") == 0)
11235 return elfcore_grok_s390_timer (abfd, note);
11236 else
11237 return true;
11238
11239 case NT_S390_TODCMP:
11240 if (note->namesz == 6
11241 && strcmp (note->namedata, "LINUX") == 0)
11242 return elfcore_grok_s390_todcmp (abfd, note);
11243 else
11244 return true;
11245
11246 case NT_S390_TODPREG:
11247 if (note->namesz == 6
11248 && strcmp (note->namedata, "LINUX") == 0)
11249 return elfcore_grok_s390_todpreg (abfd, note);
11250 else
11251 return true;
11252
11253 case NT_S390_CTRS:
11254 if (note->namesz == 6
11255 && strcmp (note->namedata, "LINUX") == 0)
11256 return elfcore_grok_s390_ctrs (abfd, note);
11257 else
11258 return true;
11259
11260 case NT_S390_PREFIX:
11261 if (note->namesz == 6
11262 && strcmp (note->namedata, "LINUX") == 0)
11263 return elfcore_grok_s390_prefix (abfd, note);
11264 else
11265 return true;
11266
11267 case NT_S390_LAST_BREAK:
11268 if (note->namesz == 6
11269 && strcmp (note->namedata, "LINUX") == 0)
11270 return elfcore_grok_s390_last_break (abfd, note);
11271 else
11272 return true;
11273
11274 case NT_S390_SYSTEM_CALL:
11275 if (note->namesz == 6
11276 && strcmp (note->namedata, "LINUX") == 0)
11277 return elfcore_grok_s390_system_call (abfd, note);
11278 else
11279 return true;
11280
11281 case NT_S390_TDB:
11282 if (note->namesz == 6
11283 && strcmp (note->namedata, "LINUX") == 0)
11284 return elfcore_grok_s390_tdb (abfd, note);
11285 else
11286 return true;
11287
11288 case NT_S390_VXRS_LOW:
11289 if (note->namesz == 6
11290 && strcmp (note->namedata, "LINUX") == 0)
11291 return elfcore_grok_s390_vxrs_low (abfd, note);
11292 else
11293 return true;
11294
11295 case NT_S390_VXRS_HIGH:
11296 if (note->namesz == 6
11297 && strcmp (note->namedata, "LINUX") == 0)
11298 return elfcore_grok_s390_vxrs_high (abfd, note);
11299 else
11300 return true;
11301
11302 case NT_S390_GS_CB:
11303 if (note->namesz == 6
11304 && strcmp (note->namedata, "LINUX") == 0)
11305 return elfcore_grok_s390_gs_cb (abfd, note);
11306 else
11307 return true;
11308
11309 case NT_S390_GS_BC:
11310 if (note->namesz == 6
11311 && strcmp (note->namedata, "LINUX") == 0)
11312 return elfcore_grok_s390_gs_bc (abfd, note);
11313 else
11314 return true;
11315
11316 case NT_ARC_V2:
11317 if (note->namesz == 6
11318 && strcmp (note->namedata, "LINUX") == 0)
11319 return elfcore_grok_arc_v2 (abfd, note);
11320 else
11321 return true;
11322
11323 case NT_ARM_VFP:
11324 if (note->namesz == 6
11325 && strcmp (note->namedata, "LINUX") == 0)
11326 return elfcore_grok_arm_vfp (abfd, note);
11327 else
11328 return true;
11329
11330 case NT_ARM_TLS:
11331 if (note->namesz == 6
11332 && strcmp (note->namedata, "LINUX") == 0)
11333 return elfcore_grok_aarch_tls (abfd, note);
11334 else
11335 return true;
11336
11337 case NT_ARM_HW_BREAK:
11338 if (note->namesz == 6
11339 && strcmp (note->namedata, "LINUX") == 0)
11340 return elfcore_grok_aarch_hw_break (abfd, note);
11341 else
11342 return true;
11343
11344 case NT_ARM_HW_WATCH:
11345 if (note->namesz == 6
11346 && strcmp (note->namedata, "LINUX") == 0)
11347 return elfcore_grok_aarch_hw_watch (abfd, note);
11348 else
11349 return true;
11350
11351 case NT_ARM_SVE:
11352 if (note->namesz == 6
11353 && strcmp (note->namedata, "LINUX") == 0)
11354 return elfcore_grok_aarch_sve (abfd, note);
11355 else
11356 return true;
11357
11358 case NT_ARM_PAC_MASK:
11359 if (note->namesz == 6
11360 && strcmp (note->namedata, "LINUX") == 0)
11361 return elfcore_grok_aarch_pauth (abfd, note);
11362 else
11363 return true;
11364
11365 case NT_ARM_TAGGED_ADDR_CTRL:
11366 if (note->namesz == 6
11367 && strcmp (note->namedata, "LINUX") == 0)
11368 return elfcore_grok_aarch_mte (abfd, note);
11369 else
11370 return true;
11371
11372 case NT_ARM_SSVE:
11373 if (note->namesz == 6
11374 && strcmp (note->namedata, "LINUX") == 0)
11375 return elfcore_grok_aarch_ssve (abfd, note);
11376 else
11377 return true;
11378
11379 case NT_ARM_ZA:
11380 if (note->namesz == 6
11381 && strcmp (note->namedata, "LINUX") == 0)
11382 return elfcore_grok_aarch_za (abfd, note);
11383 else
11384 return true;
11385
11386 case NT_ARM_ZT:
11387 if (note->namesz == 6
11388 && strcmp (note->namedata, "LINUX") == 0)
11389 return elfcore_grok_aarch_zt (abfd, note);
11390 else
11391 return true;
11392
11393 case NT_ARM_GCS:
11394 if (note->namesz == 6 && strcmp (note->namedata, "LINUX") == 0)
11395 return elfcore_grok_aarch_gcs (abfd, note);
11396 else
11397 return true;
11398
11399 case NT_GDB_TDESC:
11400 if (note->namesz == 4
11401 && strcmp (note->namedata, "GDB") == 0)
11402 return elfcore_grok_gdb_tdesc (abfd, note);
11403 else
11404 return true;
11405
11406 case NT_RISCV_CSR:
11407 if (note->namesz == 4
11408 && strcmp (note->namedata, "GDB") == 0)
11409 return elfcore_grok_riscv_csr (abfd, note);
11410 else
11411 return true;
11412
11413 case NT_LARCH_CPUCFG:
11414 if (note->namesz == 6
11415 && strcmp (note->namedata, "LINUX") == 0)
11416 return elfcore_grok_loongarch_cpucfg (abfd, note);
11417 else
11418 return true;
11419
11420 case NT_LARCH_LBT:
11421 if (note->namesz == 6
11422 && strcmp (note->namedata, "LINUX") == 0)
11423 return elfcore_grok_loongarch_lbt (abfd, note);
11424 else
11425 return true;
11426
11427 case NT_LARCH_LSX:
11428 if (note->namesz == 6
11429 && strcmp (note->namedata, "LINUX") == 0)
11430 return elfcore_grok_loongarch_lsx (abfd, note);
11431 else
11432 return true;
11433
11434 case NT_LARCH_LASX:
11435 if (note->namesz == 6
11436 && strcmp (note->namedata, "LINUX") == 0)
11437 return elfcore_grok_loongarch_lasx (abfd, note);
11438 else
11439 return true;
11440
11441 case NT_PRPSINFO:
11442 case NT_PSINFO:
11443 if (bed->elf_backend_grok_psinfo)
11444 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11445 return true;
11446 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11447 return elfcore_grok_psinfo (abfd, note);
11448 #else
11449 return true;
11450 #endif
11451
11452 case NT_AUXV:
11453 return elfcore_make_auxv_note_section (abfd, note, 0);
11454
11455 case NT_FILE:
11456 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11457 note);
11458
11459 case NT_SIGINFO:
11460 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11461 note);
11462
11463 }
11464 }
11465
11466 static bool
11467 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11468 {
11469 struct bfd_build_id* build_id;
11470
11471 if (note->descsz == 0)
11472 return false;
11473
11474 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11475 if (build_id == NULL)
11476 return false;
11477
11478 build_id->size = note->descsz;
11479 memcpy (build_id->data, note->descdata, note->descsz);
11480 abfd->build_id = build_id;
11481
11482 return true;
11483 }
11484
11485 static bool
11486 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11487 {
11488 switch (note->type)
11489 {
11490 default:
11491 return true;
11492
11493 case NT_GNU_PROPERTY_TYPE_0:
11494 return _bfd_elf_parse_gnu_properties (abfd, note);
11495
11496 case NT_GNU_BUILD_ID:
11497 return elfobj_grok_gnu_build_id (abfd, note);
11498 }
11499 }
11500
11501 static bool
11502 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11503 {
11504 struct sdt_note *cur =
11505 (struct sdt_note *) bfd_alloc (abfd,
11506 sizeof (struct sdt_note) + note->descsz);
11507
11508 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11509 cur->size = (bfd_size_type) note->descsz;
11510 memcpy (cur->data, note->descdata, note->descsz);
11511
11512 elf_tdata (abfd)->sdt_note_head = cur;
11513
11514 return true;
11515 }
11516
11517 static bool
11518 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11519 {
11520 switch (note->type)
11521 {
11522 case NT_STAPSDT:
11523 return elfobj_grok_stapsdt_note_1 (abfd, note);
11524
11525 default:
11526 return true;
11527 }
11528 }
11529
11530 static bool
11531 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11532 {
11533 size_t offset;
11534
11535 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11536 {
11537 case ELFCLASS32:
11538 if (note->descsz < 108)
11539 return false;
11540 break;
11541
11542 case ELFCLASS64:
11543 if (note->descsz < 120)
11544 return false;
11545 break;
11546
11547 default:
11548 return false;
11549 }
11550
11551 /* Check for version 1 in pr_version. */
11552 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11553 return false;
11554
11555 offset = 4;
11556
11557 /* Skip over pr_psinfosz. */
11558 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11559 offset += 4;
11560 else
11561 {
11562 offset += 4; /* Padding before pr_psinfosz. */
11563 offset += 8;
11564 }
11565
11566 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11567 elf_tdata (abfd)->core->program
11568 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11569 offset += 17;
11570
11571 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11572 elf_tdata (abfd)->core->command
11573 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11574 offset += 81;
11575
11576 /* Padding before pr_pid. */
11577 offset += 2;
11578
11579 /* The pr_pid field was added in version "1a". */
11580 if (note->descsz < offset + 4)
11581 return true;
11582
11583 elf_tdata (abfd)->core->pid
11584 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11585
11586 return true;
11587 }
11588
11589 static bool
11590 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11591 {
11592 size_t offset;
11593 size_t size;
11594 size_t min_size;
11595
11596 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11597 Also compute minimum size of this note. */
11598 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11599 {
11600 case ELFCLASS32:
11601 offset = 4 + 4;
11602 min_size = offset + (4 * 2) + 4 + 4 + 4;
11603 break;
11604
11605 case ELFCLASS64:
11606 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11607 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11608 break;
11609
11610 default:
11611 return false;
11612 }
11613
11614 if (note->descsz < min_size)
11615 return false;
11616
11617 /* Check for version 1 in pr_version. */
11618 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11619 return false;
11620
11621 /* Extract size of pr_reg from pr_gregsetsz. */
11622 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11623 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11624 {
11625 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11626 offset += 4 * 2;
11627 }
11628 else
11629 {
11630 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11631 offset += 8 * 2;
11632 }
11633
11634 /* Skip over pr_osreldate. */
11635 offset += 4;
11636
11637 /* Read signal from pr_cursig. */
11638 if (elf_tdata (abfd)->core->signal == 0)
11639 elf_tdata (abfd)->core->signal
11640 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11641 offset += 4;
11642
11643 /* Read TID from pr_pid. */
11644 elf_tdata (abfd)->core->lwpid
11645 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11646 offset += 4;
11647
11648 /* Padding before pr_reg. */
11649 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11650 offset += 4;
11651
11652 /* Make sure that there is enough data remaining in the note. */
11653 if ((note->descsz - offset) < size)
11654 return false;
11655
11656 /* Make a ".reg/999" section and a ".reg" section. */
11657 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11658 size, note->descpos + offset);
11659 }
11660
11661 static bool
11662 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11663 {
11664 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11665
11666 switch (note->type)
11667 {
11668 case NT_PRSTATUS:
11669 if (bed->elf_backend_grok_freebsd_prstatus)
11670 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11671 return true;
11672 return elfcore_grok_freebsd_prstatus (abfd, note);
11673
11674 case NT_FPREGSET:
11675 return elfcore_grok_prfpreg (abfd, note);
11676
11677 case NT_PRPSINFO:
11678 return elfcore_grok_freebsd_psinfo (abfd, note);
11679
11680 case NT_FREEBSD_THRMISC:
11681 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11682
11683 case NT_FREEBSD_PROCSTAT_PROC:
11684 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11685 note);
11686
11687 case NT_FREEBSD_PROCSTAT_FILES:
11688 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11689 note);
11690
11691 case NT_FREEBSD_PROCSTAT_VMMAP:
11692 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11693 note);
11694
11695 case NT_FREEBSD_PROCSTAT_AUXV:
11696 return elfcore_make_auxv_note_section (abfd, note, 4);
11697
11698 case NT_FREEBSD_X86_SEGBASES:
11699 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11700
11701 case NT_X86_XSTATE:
11702 return elfcore_grok_xstatereg (abfd, note);
11703
11704 case NT_FREEBSD_PTLWPINFO:
11705 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11706 note);
11707
11708 case NT_ARM_TLS:
11709 return elfcore_grok_aarch_tls (abfd, note);
11710
11711 case NT_ARM_VFP:
11712 return elfcore_grok_arm_vfp (abfd, note);
11713
11714 default:
11715 return true;
11716 }
11717 }
11718
11719 static bool
11720 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11721 {
11722 char *cp;
11723
11724 cp = strchr (note->namedata, '@');
11725 if (cp != NULL)
11726 {
11727 *lwpidp = atoi(cp + 1);
11728 return true;
11729 }
11730 return false;
11731 }
11732
11733 static bool
11734 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11735 {
11736 if (note->descsz <= 0x7c + 31)
11737 return false;
11738
11739 /* Signal number at offset 0x08. */
11740 elf_tdata (abfd)->core->signal
11741 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11742
11743 /* Process ID at offset 0x50. */
11744 elf_tdata (abfd)->core->pid
11745 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11746
11747 /* Command name at 0x7c (max 32 bytes, including nul). */
11748 elf_tdata (abfd)->core->command
11749 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11750
11751 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11752 note);
11753 }
11754
11755 static bool
11756 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11757 {
11758 int lwp;
11759
11760 if (elfcore_netbsd_get_lwpid (note, &lwp))
11761 elf_tdata (abfd)->core->lwpid = lwp;
11762
11763 switch (note->type)
11764 {
11765 case NT_NETBSDCORE_PROCINFO:
11766 /* NetBSD-specific core "procinfo". Note that we expect to
11767 find this note before any of the others, which is fine,
11768 since the kernel writes this note out first when it
11769 creates a core file. */
11770 return elfcore_grok_netbsd_procinfo (abfd, note);
11771 case NT_NETBSDCORE_AUXV:
11772 /* NetBSD-specific Elf Auxiliary Vector data. */
11773 return elfcore_make_auxv_note_section (abfd, note, 4);
11774 case NT_NETBSDCORE_LWPSTATUS:
11775 return elfcore_make_note_pseudosection (abfd,
11776 ".note.netbsdcore.lwpstatus",
11777 note);
11778 default:
11779 break;
11780 }
11781
11782 /* As of March 2020 there are no other machine-independent notes
11783 defined for NetBSD core files. If the note type is less
11784 than the start of the machine-dependent note types, we don't
11785 understand it. */
11786
11787 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11788 return true;
11789
11790
11791 switch (bfd_get_arch (abfd))
11792 {
11793 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11794 PT_GETFPREGS == mach+2. */
11795
11796 case bfd_arch_aarch64:
11797 case bfd_arch_alpha:
11798 case bfd_arch_sparc:
11799 switch (note->type)
11800 {
11801 case NT_NETBSDCORE_FIRSTMACH+0:
11802 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11803
11804 case NT_NETBSDCORE_FIRSTMACH+2:
11805 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11806
11807 default:
11808 return true;
11809 }
11810
11811 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11812 There's also old PT___GETREGS40 == mach + 1 for old reg
11813 structure which lacks GBR. */
11814
11815 case bfd_arch_sh:
11816 switch (note->type)
11817 {
11818 case NT_NETBSDCORE_FIRSTMACH+3:
11819 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11820
11821 case NT_NETBSDCORE_FIRSTMACH+5:
11822 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11823
11824 default:
11825 return true;
11826 }
11827
11828 /* On all other arch's, PT_GETREGS == mach+1 and
11829 PT_GETFPREGS == mach+3. */
11830
11831 default:
11832 switch (note->type)
11833 {
11834 case NT_NETBSDCORE_FIRSTMACH+1:
11835 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11836
11837 case NT_NETBSDCORE_FIRSTMACH+3:
11838 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11839
11840 default:
11841 return true;
11842 }
11843 }
11844 /* NOTREACHED */
11845 }
11846
11847 static bool
11848 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11849 {
11850 if (note->descsz <= 0x48 + 31)
11851 return false;
11852
11853 /* Signal number at offset 0x08. */
11854 elf_tdata (abfd)->core->signal
11855 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11856
11857 /* Process ID at offset 0x20. */
11858 elf_tdata (abfd)->core->pid
11859 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11860
11861 /* Command name at 0x48 (max 32 bytes, including nul). */
11862 elf_tdata (abfd)->core->command
11863 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11864
11865 return true;
11866 }
11867
11868 /* Processes Solaris's process status note.
11869 sig_off ~ offsetof(prstatus_t, pr_cursig)
11870 pid_off ~ offsetof(prstatus_t, pr_pid)
11871 lwpid_off ~ offsetof(prstatus_t, pr_who)
11872 gregset_size ~ sizeof(gregset_t)
11873 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11874
11875 static bool
11876 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11877 int pid_off, int lwpid_off, size_t gregset_size,
11878 size_t gregset_offset)
11879 {
11880 asection *sect = NULL;
11881 elf_tdata (abfd)->core->signal
11882 = bfd_get_16 (abfd, note->descdata + sig_off);
11883 elf_tdata (abfd)->core->pid
11884 = bfd_get_32 (abfd, note->descdata + pid_off);
11885 elf_tdata (abfd)->core->lwpid
11886 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11887
11888 sect = bfd_get_section_by_name (abfd, ".reg");
11889 if (sect != NULL)
11890 sect->size = gregset_size;
11891
11892 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11893 note->descpos + gregset_offset);
11894 }
11895
11896 /* Gets program and arguments from a core.
11897 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11898 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11899
11900 static bool
11901 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11902 int prog_off, int comm_off)
11903 {
11904 elf_tdata (abfd)->core->program
11905 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11906 elf_tdata (abfd)->core->command
11907 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11908
11909 return true;
11910 }
11911
11912 /* Processes Solaris's LWP status note.
11913 gregset_size ~ sizeof(gregset_t)
11914 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11915 fpregset_size ~ sizeof(fpregset_t)
11916 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11917
11918 static bool
11919 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11920 size_t gregset_size, int gregset_off,
11921 size_t fpregset_size, int fpregset_off)
11922 {
11923 asection *sect = NULL;
11924 char reg2_section_name[16] = { 0 };
11925
11926 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11927 elf_tdata (abfd)->core->lwpid);
11928
11929 /* offsetof(lwpstatus_t, pr_lwpid) */
11930 elf_tdata (abfd)->core->lwpid
11931 = bfd_get_32 (abfd, note->descdata + 4);
11932 /* offsetof(lwpstatus_t, pr_cursig) */
11933 elf_tdata (abfd)->core->signal
11934 = bfd_get_16 (abfd, note->descdata + 12);
11935
11936 sect = bfd_get_section_by_name (abfd, ".reg");
11937 if (sect != NULL)
11938 sect->size = gregset_size;
11939 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11940 note->descpos + gregset_off))
11941 return false;
11942
11943 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11944 if (sect != NULL)
11945 {
11946 sect->size = fpregset_size;
11947 sect->filepos = note->descpos + fpregset_off;
11948 sect->alignment_power = 2;
11949 }
11950 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11951 note->descpos + fpregset_off))
11952 return false;
11953
11954 return true;
11955 }
11956
11957 static bool
11958 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11959 {
11960 if (note == NULL)
11961 return false;
11962
11963 /* core files are identified as 32- or 64-bit, SPARC or x86,
11964 by the size of the descsz which matches the sizeof()
11965 the type appropriate for that note type (e.g., prstatus_t for
11966 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11967 on Solaris. The core file bitness may differ from the bitness of
11968 gdb itself, so fixed values are used instead of sizeof().
11969 Appropriate fixed offsets are also used to obtain data from
11970 the note. */
11971
11972 switch ((int) note->type)
11973 {
11974 case SOLARIS_NT_PRSTATUS:
11975 switch (note->descsz)
11976 {
11977 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11978 return elfcore_grok_solaris_prstatus(abfd, note,
11979 136, 216, 308, 152, 356);
11980 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11981 return elfcore_grok_solaris_prstatus(abfd, note,
11982 264, 360, 520, 304, 600);
11983 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11984 return elfcore_grok_solaris_prstatus(abfd, note,
11985 136, 216, 308, 76, 356);
11986 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11987 return elfcore_grok_solaris_prstatus(abfd, note,
11988 264, 360, 520, 224, 600);
11989 default:
11990 return true;
11991 }
11992
11993 case SOLARIS_NT_PSINFO:
11994 case SOLARIS_NT_PRPSINFO:
11995 switch (note->descsz)
11996 {
11997 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11998 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11999 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12000 return elfcore_grok_solaris_info(abfd, note, 120, 136);
12001 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12002 return elfcore_grok_solaris_info(abfd, note, 88, 104);
12003 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12004 return elfcore_grok_solaris_info(abfd, note, 136, 152);
12005 default:
12006 return true;
12007 }
12008
12009 case SOLARIS_NT_LWPSTATUS:
12010 switch (note->descsz)
12011 {
12012 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12013 return elfcore_grok_solaris_lwpstatus(abfd, note,
12014 152, 344, 400, 496);
12015 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12016 return elfcore_grok_solaris_lwpstatus(abfd, note,
12017 304, 544, 544, 848);
12018 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12019 return elfcore_grok_solaris_lwpstatus(abfd, note,
12020 76, 344, 380, 420);
12021 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12022 return elfcore_grok_solaris_lwpstatus(abfd, note,
12023 224, 544, 528, 768);
12024 default:
12025 return true;
12026 }
12027
12028 case SOLARIS_NT_LWPSINFO:
12029 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12030 if (note->descsz == 128 || note->descsz == 152)
12031 elf_tdata (abfd)->core->lwpid =
12032 bfd_get_32 (abfd, note->descdata + 4);
12033 break;
12034
12035 default:
12036 break;
12037 }
12038
12039 return true;
12040 }
12041
12042 /* For name starting with "CORE" this may be either a Solaris
12043 core file or a gdb-generated core file. Do Solaris-specific
12044 processing on selected note types first with
12045 elfcore_grok_solaris_note(), then process the note
12046 in elfcore_grok_note(). */
12047
12048 static bool
12049 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12050 {
12051 if (!elfcore_grok_solaris_note_impl (abfd, note))
12052 return false;
12053
12054 return elfcore_grok_note (abfd, note);
12055 }
12056
12057 static bool
12058 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12059 {
12060 if (note->type == NT_OPENBSD_PROCINFO)
12061 return elfcore_grok_openbsd_procinfo (abfd, note);
12062
12063 if (note->type == NT_OPENBSD_REGS)
12064 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12065
12066 if (note->type == NT_OPENBSD_FPREGS)
12067 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12068
12069 if (note->type == NT_OPENBSD_XFPREGS)
12070 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12071
12072 if (note->type == NT_OPENBSD_AUXV)
12073 return elfcore_make_auxv_note_section (abfd, note, 0);
12074
12075 if (note->type == NT_OPENBSD_WCOOKIE)
12076 {
12077 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12078 SEC_HAS_CONTENTS);
12079
12080 if (sect == NULL)
12081 return false;
12082 sect->size = note->descsz;
12083 sect->filepos = note->descpos;
12084 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12085
12086 return true;
12087 }
12088
12089 return true;
12090 }
12091
12092 static bool
12093 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12094 {
12095 void *ddata = note->descdata;
12096 char buf[100];
12097 char *name;
12098 asection *sect;
12099 short sig;
12100 unsigned flags;
12101
12102 if (note->descsz < 16)
12103 return false;
12104
12105 /* nto_procfs_status 'pid' field is at offset 0. */
12106 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12107
12108 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12109 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12110
12111 /* nto_procfs_status 'flags' field is at offset 8. */
12112 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12113
12114 /* nto_procfs_status 'what' field is at offset 14. */
12115 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12116 {
12117 elf_tdata (abfd)->core->signal = sig;
12118 elf_tdata (abfd)->core->lwpid = *tid;
12119 }
12120
12121 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12122 do not come from signals so we make sure we set the current
12123 thread just in case. */
12124 if (flags & 0x00000080)
12125 elf_tdata (abfd)->core->lwpid = *tid;
12126
12127 /* Make a ".qnx_core_status/%d" section. */
12128 sprintf (buf, ".qnx_core_status/%ld", *tid);
12129
12130 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12131 if (name == NULL)
12132 return false;
12133 strcpy (name, buf);
12134
12135 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12136 if (sect == NULL)
12137 return false;
12138
12139 sect->size = note->descsz;
12140 sect->filepos = note->descpos;
12141 sect->alignment_power = 2;
12142
12143 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12144 }
12145
12146 static bool
12147 elfcore_grok_nto_regs (bfd *abfd,
12148 Elf_Internal_Note *note,
12149 long tid,
12150 char *base)
12151 {
12152 char buf[100];
12153 char *name;
12154 asection *sect;
12155
12156 /* Make a "(base)/%d" section. */
12157 sprintf (buf, "%s/%ld", base, tid);
12158
12159 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12160 if (name == NULL)
12161 return false;
12162 strcpy (name, buf);
12163
12164 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12165 if (sect == NULL)
12166 return false;
12167
12168 sect->size = note->descsz;
12169 sect->filepos = note->descpos;
12170 sect->alignment_power = 2;
12171
12172 /* This is the current thread. */
12173 if (elf_tdata (abfd)->core->lwpid == tid)
12174 return elfcore_maybe_make_sect (abfd, base, sect);
12175
12176 return true;
12177 }
12178
12179 static bool
12180 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12181 {
12182 /* Every GREG section has a STATUS section before it. Store the
12183 tid from the previous call to pass down to the next gregs
12184 function. */
12185 static long tid = 1;
12186
12187 switch (note->type)
12188 {
12189 case QNT_CORE_INFO:
12190 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12191 case QNT_CORE_STATUS:
12192 return elfcore_grok_nto_status (abfd, note, &tid);
12193 case QNT_CORE_GREG:
12194 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12195 case QNT_CORE_FPREG:
12196 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12197 default:
12198 return true;
12199 }
12200 }
12201
12202 static bool
12203 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12204 {
12205 char *name;
12206 asection *sect;
12207 size_t len;
12208
12209 /* Use note name as section name. */
12210 len = note->namesz;
12211 name = (char *) bfd_alloc (abfd, len);
12212 if (name == NULL)
12213 return false;
12214 memcpy (name, note->namedata, len);
12215 name[len - 1] = '\0';
12216
12217 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12218 if (sect == NULL)
12219 return false;
12220
12221 sect->size = note->descsz;
12222 sect->filepos = note->descpos;
12223 sect->alignment_power = 1;
12224
12225 return true;
12226 }
12227
12228 /* Function: elfcore_write_note
12229
12230 Inputs:
12231 buffer to hold note, and current size of buffer
12232 name of note
12233 type of note
12234 data for note
12235 size of data for note
12236
12237 Writes note to end of buffer. ELF64 notes are written exactly as
12238 for ELF32, despite the current (as of 2006) ELF gabi specifying
12239 that they ought to have 8-byte namesz and descsz field, and have
12240 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12241
12242 Return:
12243 Pointer to realloc'd buffer, *BUFSIZ updated. */
12244
12245 char *
12246 elfcore_write_note (bfd *abfd,
12247 char *buf,
12248 int *bufsiz,
12249 const char *name,
12250 int type,
12251 const void *input,
12252 int size)
12253 {
12254 Elf_External_Note *xnp;
12255 size_t namesz;
12256 size_t newspace;
12257 char *dest;
12258
12259 namesz = 0;
12260 if (name != NULL)
12261 namesz = strlen (name) + 1;
12262
12263 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12264
12265 buf = (char *) realloc (buf, *bufsiz + newspace);
12266 if (buf == NULL)
12267 return buf;
12268 dest = buf + *bufsiz;
12269 *bufsiz += newspace;
12270 xnp = (Elf_External_Note *) dest;
12271 H_PUT_32 (abfd, namesz, xnp->namesz);
12272 H_PUT_32 (abfd, size, xnp->descsz);
12273 H_PUT_32 (abfd, type, xnp->type);
12274 dest = xnp->name;
12275 if (name != NULL)
12276 {
12277 memcpy (dest, name, namesz);
12278 dest += namesz;
12279 while (namesz & 3)
12280 {
12281 *dest++ = '\0';
12282 ++namesz;
12283 }
12284 }
12285 memcpy (dest, input, size);
12286 dest += size;
12287 while (size & 3)
12288 {
12289 *dest++ = '\0';
12290 ++size;
12291 }
12292 return buf;
12293 }
12294
12295 /* gcc-8 warns (*) on all the strncpy calls in this function about
12296 possible string truncation. The "truncation" is not a bug. We
12297 have an external representation of structs with fields that are not
12298 necessarily NULL terminated and corresponding internal
12299 representation fields that are one larger so that they can always
12300 be NULL terminated.
12301 gcc versions between 4.2 and 4.6 do not allow pragma control of
12302 diagnostics inside functions, giving a hard error if you try to use
12303 the finer control available with later versions.
12304 gcc prior to 4.2 warns about diagnostic push and pop.
12305 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12306 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12307 (*) Depending on your system header files! */
12308 #if GCC_VERSION >= 8000
12309 # pragma GCC diagnostic push
12310 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12311 #endif
12312 char *
12313 elfcore_write_prpsinfo (bfd *abfd,
12314 char *buf,
12315 int *bufsiz,
12316 const char *fname,
12317 const char *psargs)
12318 {
12319 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12320
12321 if (bed->elf_backend_write_core_note != NULL)
12322 {
12323 char *ret;
12324 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12325 NT_PRPSINFO, fname, psargs);
12326 if (ret != NULL)
12327 return ret;
12328 }
12329
12330 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12331 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12332 if (bed->s->elfclass == ELFCLASS32)
12333 {
12334 # if defined (HAVE_PSINFO32_T)
12335 psinfo32_t data;
12336 int note_type = NT_PSINFO;
12337 # else
12338 prpsinfo32_t data;
12339 int note_type = NT_PRPSINFO;
12340 # endif
12341
12342 memset (&data, 0, sizeof (data));
12343 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12344 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12345 return elfcore_write_note (abfd, buf, bufsiz,
12346 "CORE", note_type, &data, sizeof (data));
12347 }
12348 else
12349 # endif
12350 {
12351 # if defined (HAVE_PSINFO_T)
12352 psinfo_t data;
12353 int note_type = NT_PSINFO;
12354 # else
12355 prpsinfo_t data;
12356 int note_type = NT_PRPSINFO;
12357 # endif
12358
12359 memset (&data, 0, sizeof (data));
12360 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12361 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12362 return elfcore_write_note (abfd, buf, bufsiz,
12363 "CORE", note_type, &data, sizeof (data));
12364 }
12365 #endif /* PSINFO_T or PRPSINFO_T */
12366
12367 free (buf);
12368 return NULL;
12369 }
12370 #if GCC_VERSION >= 8000
12371 # pragma GCC diagnostic pop
12372 #endif
12373
12374 char *
12375 elfcore_write_linux_prpsinfo32
12376 (bfd *abfd, char *buf, int *bufsiz,
12377 const struct elf_internal_linux_prpsinfo *prpsinfo)
12378 {
12379 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12380 {
12381 struct elf_external_linux_prpsinfo32_ugid16 data;
12382
12383 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12384 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12385 &data, sizeof (data));
12386 }
12387 else
12388 {
12389 struct elf_external_linux_prpsinfo32_ugid32 data;
12390
12391 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12392 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12393 &data, sizeof (data));
12394 }
12395 }
12396
12397 char *
12398 elfcore_write_linux_prpsinfo64
12399 (bfd *abfd, char *buf, int *bufsiz,
12400 const struct elf_internal_linux_prpsinfo *prpsinfo)
12401 {
12402 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12403 {
12404 struct elf_external_linux_prpsinfo64_ugid16 data;
12405
12406 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12407 return elfcore_write_note (abfd, buf, bufsiz,
12408 "CORE", NT_PRPSINFO, &data, sizeof (data));
12409 }
12410 else
12411 {
12412 struct elf_external_linux_prpsinfo64_ugid32 data;
12413
12414 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12415 return elfcore_write_note (abfd, buf, bufsiz,
12416 "CORE", NT_PRPSINFO, &data, sizeof (data));
12417 }
12418 }
12419
12420 char *
12421 elfcore_write_prstatus (bfd *abfd,
12422 char *buf,
12423 int *bufsiz,
12424 long pid,
12425 int cursig,
12426 const void *gregs)
12427 {
12428 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12429
12430 if (bed->elf_backend_write_core_note != NULL)
12431 {
12432 char *ret;
12433 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12434 NT_PRSTATUS,
12435 pid, cursig, gregs);
12436 if (ret != NULL)
12437 return ret;
12438 }
12439
12440 #if defined (HAVE_PRSTATUS_T)
12441 #if defined (HAVE_PRSTATUS32_T)
12442 if (bed->s->elfclass == ELFCLASS32)
12443 {
12444 prstatus32_t prstat;
12445
12446 memset (&prstat, 0, sizeof (prstat));
12447 prstat.pr_pid = pid;
12448 prstat.pr_cursig = cursig;
12449 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12450 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12451 NT_PRSTATUS, &prstat, sizeof (prstat));
12452 }
12453 else
12454 #endif
12455 {
12456 prstatus_t prstat;
12457
12458 memset (&prstat, 0, sizeof (prstat));
12459 prstat.pr_pid = pid;
12460 prstat.pr_cursig = cursig;
12461 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12462 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12463 NT_PRSTATUS, &prstat, sizeof (prstat));
12464 }
12465 #endif /* HAVE_PRSTATUS_T */
12466
12467 free (buf);
12468 return NULL;
12469 }
12470
12471 #if defined (HAVE_LWPSTATUS_T)
12472 char *
12473 elfcore_write_lwpstatus (bfd *abfd,
12474 char *buf,
12475 int *bufsiz,
12476 long pid,
12477 int cursig,
12478 const void *gregs)
12479 {
12480 lwpstatus_t lwpstat;
12481 const char *note_name = "CORE";
12482
12483 memset (&lwpstat, 0, sizeof (lwpstat));
12484 lwpstat.pr_lwpid = pid >> 16;
12485 lwpstat.pr_cursig = cursig;
12486 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12487 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12488 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12489 #if !defined(gregs)
12490 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12491 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12492 #else
12493 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12494 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12495 #endif
12496 #endif
12497 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12498 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12499 }
12500 #endif /* HAVE_LWPSTATUS_T */
12501
12502 #if defined (HAVE_PSTATUS_T)
12503 char *
12504 elfcore_write_pstatus (bfd *abfd,
12505 char *buf,
12506 int *bufsiz,
12507 long pid,
12508 int cursig ATTRIBUTE_UNUSED,
12509 const void *gregs ATTRIBUTE_UNUSED)
12510 {
12511 const char *note_name = "CORE";
12512 #if defined (HAVE_PSTATUS32_T)
12513 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12514
12515 if (bed->s->elfclass == ELFCLASS32)
12516 {
12517 pstatus32_t pstat;
12518
12519 memset (&pstat, 0, sizeof (pstat));
12520 pstat.pr_pid = pid & 0xffff;
12521 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12522 NT_PSTATUS, &pstat, sizeof (pstat));
12523 return buf;
12524 }
12525 else
12526 #endif
12527 {
12528 pstatus_t pstat;
12529
12530 memset (&pstat, 0, sizeof (pstat));
12531 pstat.pr_pid = pid & 0xffff;
12532 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12533 NT_PSTATUS, &pstat, sizeof (pstat));
12534 return buf;
12535 }
12536 }
12537 #endif /* HAVE_PSTATUS_T */
12538
12539 char *
12540 elfcore_write_prfpreg (bfd *abfd,
12541 char *buf,
12542 int *bufsiz,
12543 const void *fpregs,
12544 int size)
12545 {
12546 const char *note_name = "CORE";
12547 return elfcore_write_note (abfd, buf, bufsiz,
12548 note_name, NT_FPREGSET, fpregs, size);
12549 }
12550
12551 char *
12552 elfcore_write_prxfpreg (bfd *abfd,
12553 char *buf,
12554 int *bufsiz,
12555 const void *xfpregs,
12556 int size)
12557 {
12558 char *note_name = "LINUX";
12559 return elfcore_write_note (abfd, buf, bufsiz,
12560 note_name, NT_PRXFPREG, xfpregs, size);
12561 }
12562
12563 char *
12564 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12565 const void *xfpregs, int size)
12566 {
12567 char *note_name;
12568 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12569 note_name = "FreeBSD";
12570 else
12571 note_name = "LINUX";
12572 return elfcore_write_note (abfd, buf, bufsiz,
12573 note_name, NT_X86_XSTATE, xfpregs, size);
12574 }
12575
12576 static char *
12577 elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12578 const void *ssp, int size)
12579 {
12580 const char *note_name = "LINUX";
12581 return elfcore_write_note (abfd, buf, bufsiz,
12582 note_name, NT_X86_SHSTK, ssp, size);
12583 }
12584
12585 char *
12586 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12587 const void *regs, int size)
12588 {
12589 char *note_name = "FreeBSD";
12590 return elfcore_write_note (abfd, buf, bufsiz,
12591 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12592 }
12593
12594 char *
12595 elfcore_write_ppc_vmx (bfd *abfd,
12596 char *buf,
12597 int *bufsiz,
12598 const void *ppc_vmx,
12599 int size)
12600 {
12601 char *note_name = "LINUX";
12602 return elfcore_write_note (abfd, buf, bufsiz,
12603 note_name, NT_PPC_VMX, ppc_vmx, size);
12604 }
12605
12606 char *
12607 elfcore_write_ppc_vsx (bfd *abfd,
12608 char *buf,
12609 int *bufsiz,
12610 const void *ppc_vsx,
12611 int size)
12612 {
12613 char *note_name = "LINUX";
12614 return elfcore_write_note (abfd, buf, bufsiz,
12615 note_name, NT_PPC_VSX, ppc_vsx, size);
12616 }
12617
12618 char *
12619 elfcore_write_ppc_tar (bfd *abfd,
12620 char *buf,
12621 int *bufsiz,
12622 const void *ppc_tar,
12623 int size)
12624 {
12625 char *note_name = "LINUX";
12626 return elfcore_write_note (abfd, buf, bufsiz,
12627 note_name, NT_PPC_TAR, ppc_tar, size);
12628 }
12629
12630 char *
12631 elfcore_write_ppc_ppr (bfd *abfd,
12632 char *buf,
12633 int *bufsiz,
12634 const void *ppc_ppr,
12635 int size)
12636 {
12637 char *note_name = "LINUX";
12638 return elfcore_write_note (abfd, buf, bufsiz,
12639 note_name, NT_PPC_PPR, ppc_ppr, size);
12640 }
12641
12642 char *
12643 elfcore_write_ppc_dscr (bfd *abfd,
12644 char *buf,
12645 int *bufsiz,
12646 const void *ppc_dscr,
12647 int size)
12648 {
12649 char *note_name = "LINUX";
12650 return elfcore_write_note (abfd, buf, bufsiz,
12651 note_name, NT_PPC_DSCR, ppc_dscr, size);
12652 }
12653
12654 char *
12655 elfcore_write_ppc_ebb (bfd *abfd,
12656 char *buf,
12657 int *bufsiz,
12658 const void *ppc_ebb,
12659 int size)
12660 {
12661 char *note_name = "LINUX";
12662 return elfcore_write_note (abfd, buf, bufsiz,
12663 note_name, NT_PPC_EBB, ppc_ebb, size);
12664 }
12665
12666 char *
12667 elfcore_write_ppc_pmu (bfd *abfd,
12668 char *buf,
12669 int *bufsiz,
12670 const void *ppc_pmu,
12671 int size)
12672 {
12673 char *note_name = "LINUX";
12674 return elfcore_write_note (abfd, buf, bufsiz,
12675 note_name, NT_PPC_PMU, ppc_pmu, size);
12676 }
12677
12678 char *
12679 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12680 char *buf,
12681 int *bufsiz,
12682 const void *ppc_tm_cgpr,
12683 int size)
12684 {
12685 char *note_name = "LINUX";
12686 return elfcore_write_note (abfd, buf, bufsiz,
12687 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12688 }
12689
12690 char *
12691 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12692 char *buf,
12693 int *bufsiz,
12694 const void *ppc_tm_cfpr,
12695 int size)
12696 {
12697 char *note_name = "LINUX";
12698 return elfcore_write_note (abfd, buf, bufsiz,
12699 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12700 }
12701
12702 char *
12703 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12704 char *buf,
12705 int *bufsiz,
12706 const void *ppc_tm_cvmx,
12707 int size)
12708 {
12709 char *note_name = "LINUX";
12710 return elfcore_write_note (abfd, buf, bufsiz,
12711 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12712 }
12713
12714 char *
12715 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12716 char *buf,
12717 int *bufsiz,
12718 const void *ppc_tm_cvsx,
12719 int size)
12720 {
12721 char *note_name = "LINUX";
12722 return elfcore_write_note (abfd, buf, bufsiz,
12723 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12724 }
12725
12726 char *
12727 elfcore_write_ppc_tm_spr (bfd *abfd,
12728 char *buf,
12729 int *bufsiz,
12730 const void *ppc_tm_spr,
12731 int size)
12732 {
12733 char *note_name = "LINUX";
12734 return elfcore_write_note (abfd, buf, bufsiz,
12735 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12736 }
12737
12738 char *
12739 elfcore_write_ppc_tm_ctar (bfd *abfd,
12740 char *buf,
12741 int *bufsiz,
12742 const void *ppc_tm_ctar,
12743 int size)
12744 {
12745 char *note_name = "LINUX";
12746 return elfcore_write_note (abfd, buf, bufsiz,
12747 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12748 }
12749
12750 char *
12751 elfcore_write_ppc_tm_cppr (bfd *abfd,
12752 char *buf,
12753 int *bufsiz,
12754 const void *ppc_tm_cppr,
12755 int size)
12756 {
12757 char *note_name = "LINUX";
12758 return elfcore_write_note (abfd, buf, bufsiz,
12759 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12760 }
12761
12762 char *
12763 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12764 char *buf,
12765 int *bufsiz,
12766 const void *ppc_tm_cdscr,
12767 int size)
12768 {
12769 char *note_name = "LINUX";
12770 return elfcore_write_note (abfd, buf, bufsiz,
12771 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12772 }
12773
12774 static char *
12775 elfcore_write_s390_high_gprs (bfd *abfd,
12776 char *buf,
12777 int *bufsiz,
12778 const void *s390_high_gprs,
12779 int size)
12780 {
12781 char *note_name = "LINUX";
12782 return elfcore_write_note (abfd, buf, bufsiz,
12783 note_name, NT_S390_HIGH_GPRS,
12784 s390_high_gprs, size);
12785 }
12786
12787 char *
12788 elfcore_write_s390_timer (bfd *abfd,
12789 char *buf,
12790 int *bufsiz,
12791 const void *s390_timer,
12792 int size)
12793 {
12794 char *note_name = "LINUX";
12795 return elfcore_write_note (abfd, buf, bufsiz,
12796 note_name, NT_S390_TIMER, s390_timer, size);
12797 }
12798
12799 char *
12800 elfcore_write_s390_todcmp (bfd *abfd,
12801 char *buf,
12802 int *bufsiz,
12803 const void *s390_todcmp,
12804 int size)
12805 {
12806 char *note_name = "LINUX";
12807 return elfcore_write_note (abfd, buf, bufsiz,
12808 note_name, NT_S390_TODCMP, s390_todcmp, size);
12809 }
12810
12811 char *
12812 elfcore_write_s390_todpreg (bfd *abfd,
12813 char *buf,
12814 int *bufsiz,
12815 const void *s390_todpreg,
12816 int size)
12817 {
12818 char *note_name = "LINUX";
12819 return elfcore_write_note (abfd, buf, bufsiz,
12820 note_name, NT_S390_TODPREG, s390_todpreg, size);
12821 }
12822
12823 char *
12824 elfcore_write_s390_ctrs (bfd *abfd,
12825 char *buf,
12826 int *bufsiz,
12827 const void *s390_ctrs,
12828 int size)
12829 {
12830 char *note_name = "LINUX";
12831 return elfcore_write_note (abfd, buf, bufsiz,
12832 note_name, NT_S390_CTRS, s390_ctrs, size);
12833 }
12834
12835 char *
12836 elfcore_write_s390_prefix (bfd *abfd,
12837 char *buf,
12838 int *bufsiz,
12839 const void *s390_prefix,
12840 int size)
12841 {
12842 char *note_name = "LINUX";
12843 return elfcore_write_note (abfd, buf, bufsiz,
12844 note_name, NT_S390_PREFIX, s390_prefix, size);
12845 }
12846
12847 char *
12848 elfcore_write_s390_last_break (bfd *abfd,
12849 char *buf,
12850 int *bufsiz,
12851 const void *s390_last_break,
12852 int size)
12853 {
12854 char *note_name = "LINUX";
12855 return elfcore_write_note (abfd, buf, bufsiz,
12856 note_name, NT_S390_LAST_BREAK,
12857 s390_last_break, size);
12858 }
12859
12860 char *
12861 elfcore_write_s390_system_call (bfd *abfd,
12862 char *buf,
12863 int *bufsiz,
12864 const void *s390_system_call,
12865 int size)
12866 {
12867 char *note_name = "LINUX";
12868 return elfcore_write_note (abfd, buf, bufsiz,
12869 note_name, NT_S390_SYSTEM_CALL,
12870 s390_system_call, size);
12871 }
12872
12873 char *
12874 elfcore_write_s390_tdb (bfd *abfd,
12875 char *buf,
12876 int *bufsiz,
12877 const void *s390_tdb,
12878 int size)
12879 {
12880 char *note_name = "LINUX";
12881 return elfcore_write_note (abfd, buf, bufsiz,
12882 note_name, NT_S390_TDB, s390_tdb, size);
12883 }
12884
12885 char *
12886 elfcore_write_s390_vxrs_low (bfd *abfd,
12887 char *buf,
12888 int *bufsiz,
12889 const void *s390_vxrs_low,
12890 int size)
12891 {
12892 char *note_name = "LINUX";
12893 return elfcore_write_note (abfd, buf, bufsiz,
12894 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12895 }
12896
12897 char *
12898 elfcore_write_s390_vxrs_high (bfd *abfd,
12899 char *buf,
12900 int *bufsiz,
12901 const void *s390_vxrs_high,
12902 int size)
12903 {
12904 char *note_name = "LINUX";
12905 return elfcore_write_note (abfd, buf, bufsiz,
12906 note_name, NT_S390_VXRS_HIGH,
12907 s390_vxrs_high, size);
12908 }
12909
12910 char *
12911 elfcore_write_s390_gs_cb (bfd *abfd,
12912 char *buf,
12913 int *bufsiz,
12914 const void *s390_gs_cb,
12915 int size)
12916 {
12917 char *note_name = "LINUX";
12918 return elfcore_write_note (abfd, buf, bufsiz,
12919 note_name, NT_S390_GS_CB,
12920 s390_gs_cb, size);
12921 }
12922
12923 char *
12924 elfcore_write_s390_gs_bc (bfd *abfd,
12925 char *buf,
12926 int *bufsiz,
12927 const void *s390_gs_bc,
12928 int size)
12929 {
12930 char *note_name = "LINUX";
12931 return elfcore_write_note (abfd, buf, bufsiz,
12932 note_name, NT_S390_GS_BC,
12933 s390_gs_bc, size);
12934 }
12935
12936 char *
12937 elfcore_write_arm_vfp (bfd *abfd,
12938 char *buf,
12939 int *bufsiz,
12940 const void *arm_vfp,
12941 int size)
12942 {
12943 char *note_name = "LINUX";
12944 return elfcore_write_note (abfd, buf, bufsiz,
12945 note_name, NT_ARM_VFP, arm_vfp, size);
12946 }
12947
12948 char *
12949 elfcore_write_aarch_tls (bfd *abfd,
12950 char *buf,
12951 int *bufsiz,
12952 const void *aarch_tls,
12953 int size)
12954 {
12955 char *note_name = "LINUX";
12956 return elfcore_write_note (abfd, buf, bufsiz,
12957 note_name, NT_ARM_TLS, aarch_tls, size);
12958 }
12959
12960 char *
12961 elfcore_write_aarch_hw_break (bfd *abfd,
12962 char *buf,
12963 int *bufsiz,
12964 const void *aarch_hw_break,
12965 int size)
12966 {
12967 char *note_name = "LINUX";
12968 return elfcore_write_note (abfd, buf, bufsiz,
12969 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12970 }
12971
12972 char *
12973 elfcore_write_aarch_hw_watch (bfd *abfd,
12974 char *buf,
12975 int *bufsiz,
12976 const void *aarch_hw_watch,
12977 int size)
12978 {
12979 char *note_name = "LINUX";
12980 return elfcore_write_note (abfd, buf, bufsiz,
12981 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12982 }
12983
12984 char *
12985 elfcore_write_aarch_sve (bfd *abfd,
12986 char *buf,
12987 int *bufsiz,
12988 const void *aarch_sve,
12989 int size)
12990 {
12991 char *note_name = "LINUX";
12992 return elfcore_write_note (abfd, buf, bufsiz,
12993 note_name, NT_ARM_SVE, aarch_sve, size);
12994 }
12995
12996 char *
12997 elfcore_write_aarch_pauth (bfd *abfd,
12998 char *buf,
12999 int *bufsiz,
13000 const void *aarch_pauth,
13001 int size)
13002 {
13003 char *note_name = "LINUX";
13004 return elfcore_write_note (abfd, buf, bufsiz,
13005 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13006 }
13007
13008 char *
13009 elfcore_write_aarch_mte (bfd *abfd,
13010 char *buf,
13011 int *bufsiz,
13012 const void *aarch_mte,
13013 int size)
13014 {
13015 char *note_name = "LINUX";
13016 return elfcore_write_note (abfd, buf, bufsiz,
13017 note_name, NT_ARM_TAGGED_ADDR_CTRL,
13018 aarch_mte,
13019 size);
13020 }
13021
13022 char *
13023 elfcore_write_aarch_ssve (bfd *abfd,
13024 char *buf,
13025 int *bufsiz,
13026 const void *aarch_ssve,
13027 int size)
13028 {
13029 char *note_name = "LINUX";
13030 return elfcore_write_note (abfd, buf, bufsiz,
13031 note_name, NT_ARM_SSVE,
13032 aarch_ssve,
13033 size);
13034 }
13035
13036 char *
13037 elfcore_write_aarch_za (bfd *abfd,
13038 char *buf,
13039 int *bufsiz,
13040 const void *aarch_za,
13041 int size)
13042 {
13043 char *note_name = "LINUX";
13044 return elfcore_write_note (abfd, buf, bufsiz,
13045 note_name, NT_ARM_ZA,
13046 aarch_za,
13047 size);
13048 }
13049
13050 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13051 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13052 written into. Return a pointer to the new start of the note buffer, to
13053 replace BUF which may no longer be valid. */
13054
13055 char *
13056 elfcore_write_aarch_zt (bfd *abfd,
13057 char *buf,
13058 int *bufsiz,
13059 const void *aarch_zt,
13060 int size)
13061 {
13062 char *note_name = "LINUX";
13063 return elfcore_write_note (abfd, buf, bufsiz,
13064 note_name, NT_ARM_ZT,
13065 aarch_zt,
13066 size);
13067 }
13068
13069 /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
13070 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13071 written into. Return a pointer to the new start of the note buffer, to
13072 replace BUF which may no longer be valid. */
13073
13074 static char *
13075 elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
13076 const void *aarch_gcs, int size)
13077 {
13078 const char *note_name = "LINUX";
13079 return elfcore_write_note (abfd, buf, bufsiz, note_name, NT_ARM_GCS,
13080 aarch_gcs, size);
13081 }
13082
13083 char *
13084 elfcore_write_arc_v2 (bfd *abfd,
13085 char *buf,
13086 int *bufsiz,
13087 const void *arc_v2,
13088 int size)
13089 {
13090 char *note_name = "LINUX";
13091 return elfcore_write_note (abfd, buf, bufsiz,
13092 note_name, NT_ARC_V2, arc_v2, size);
13093 }
13094
13095 char *
13096 elfcore_write_loongarch_cpucfg (bfd *abfd,
13097 char *buf,
13098 int *bufsiz,
13099 const void *loongarch_cpucfg,
13100 int size)
13101 {
13102 char *note_name = "LINUX";
13103 return elfcore_write_note (abfd, buf, bufsiz,
13104 note_name, NT_LARCH_CPUCFG,
13105 loongarch_cpucfg, size);
13106 }
13107
13108 char *
13109 elfcore_write_loongarch_lbt (bfd *abfd,
13110 char *buf,
13111 int *bufsiz,
13112 const void *loongarch_lbt,
13113 int size)
13114 {
13115 char *note_name = "LINUX";
13116 return elfcore_write_note (abfd, buf, bufsiz,
13117 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13118 }
13119
13120 char *
13121 elfcore_write_loongarch_lsx (bfd *abfd,
13122 char *buf,
13123 int *bufsiz,
13124 const void *loongarch_lsx,
13125 int size)
13126 {
13127 char *note_name = "LINUX";
13128 return elfcore_write_note (abfd, buf, bufsiz,
13129 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13130 }
13131
13132 char *
13133 elfcore_write_loongarch_lasx (bfd *abfd,
13134 char *buf,
13135 int *bufsiz,
13136 const void *loongarch_lasx,
13137 int size)
13138 {
13139 char *note_name = "LINUX";
13140 return elfcore_write_note (abfd, buf, bufsiz,
13141 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13142 }
13143
13144 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13145 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13146 written into. Return a pointer to the new start of the note buffer, to
13147 replace BUF which may no longer be valid. */
13148
13149 char *
13150 elfcore_write_riscv_csr (bfd *abfd,
13151 char *buf,
13152 int *bufsiz,
13153 const void *csrs,
13154 int size)
13155 {
13156 const char *note_name = "GDB";
13157 return elfcore_write_note (abfd, buf, bufsiz,
13158 note_name, NT_RISCV_CSR, csrs, size);
13159 }
13160
13161 /* Write the target description (a string) pointed to by TDESC, length
13162 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13163 note is being written into. Return a pointer to the new start of the
13164 note buffer, to replace BUF which may no longer be valid. */
13165
13166 char *
13167 elfcore_write_gdb_tdesc (bfd *abfd,
13168 char *buf,
13169 int *bufsiz,
13170 const void *tdesc,
13171 int size)
13172 {
13173 const char *note_name = "GDB";
13174 return elfcore_write_note (abfd, buf, bufsiz,
13175 note_name, NT_GDB_TDESC, tdesc, size);
13176 }
13177
13178 char *
13179 elfcore_write_register_note (bfd *abfd,
13180 char *buf,
13181 int *bufsiz,
13182 const char *section,
13183 const void *data,
13184 int size)
13185 {
13186 if (strcmp (section, ".reg2") == 0)
13187 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13188 if (strcmp (section, ".reg-xfp") == 0)
13189 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13190 if (strcmp (section, ".reg-xstate") == 0)
13191 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13192 if (strcmp (section, ".reg-x86-segbases") == 0)
13193 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13194 if (strcmp (section, ".reg-ssp") == 0)
13195 return elfcore_write_sspreg (abfd, buf, bufsiz, data, size);
13196 if (strcmp (section, ".reg-ppc-vmx") == 0)
13197 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13198 if (strcmp (section, ".reg-ppc-vsx") == 0)
13199 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13200 if (strcmp (section, ".reg-ppc-tar") == 0)
13201 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13202 if (strcmp (section, ".reg-ppc-ppr") == 0)
13203 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13204 if (strcmp (section, ".reg-ppc-dscr") == 0)
13205 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13206 if (strcmp (section, ".reg-ppc-ebb") == 0)
13207 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13208 if (strcmp (section, ".reg-ppc-pmu") == 0)
13209 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13210 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13211 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13212 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13213 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13214 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13215 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13216 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13217 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13218 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13219 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13220 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13221 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13222 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13223 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13224 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13225 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13226 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13227 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13228 if (strcmp (section, ".reg-s390-timer") == 0)
13229 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13230 if (strcmp (section, ".reg-s390-todcmp") == 0)
13231 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13232 if (strcmp (section, ".reg-s390-todpreg") == 0)
13233 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13234 if (strcmp (section, ".reg-s390-ctrs") == 0)
13235 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13236 if (strcmp (section, ".reg-s390-prefix") == 0)
13237 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13238 if (strcmp (section, ".reg-s390-last-break") == 0)
13239 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13240 if (strcmp (section, ".reg-s390-system-call") == 0)
13241 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13242 if (strcmp (section, ".reg-s390-tdb") == 0)
13243 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13244 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13245 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13246 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13247 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13248 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13249 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13250 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13251 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13252 if (strcmp (section, ".reg-arm-vfp") == 0)
13253 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13254 if (strcmp (section, ".reg-aarch-tls") == 0)
13255 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13256 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13257 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13258 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13259 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13260 if (strcmp (section, ".reg-aarch-sve") == 0)
13261 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13262 if (strcmp (section, ".reg-aarch-pauth") == 0)
13263 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13264 if (strcmp (section, ".reg-aarch-mte") == 0)
13265 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13266 if (strcmp (section, ".reg-aarch-ssve") == 0)
13267 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13268 if (strcmp (section, ".reg-aarch-za") == 0)
13269 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13270 if (strcmp (section, ".reg-aarch-zt") == 0)
13271 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13272 if (strcmp (section, ".reg-aarch-gcs") == 0)
13273 return elfcore_write_aarch_gcs (abfd, buf, bufsiz, data, size);
13274 if (strcmp (section, ".reg-arc-v2") == 0)
13275 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13276 if (strcmp (section, ".gdb-tdesc") == 0)
13277 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13278 if (strcmp (section, ".reg-riscv-csr") == 0)
13279 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13280 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13281 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13282 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13283 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13284 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13285 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13286 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13287 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13288 return NULL;
13289 }
13290
13291 char *
13292 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13293 const void *buf, int bufsiz)
13294 {
13295 return elfcore_write_note (obfd, note_data, note_size,
13296 "CORE", NT_FILE, buf, bufsiz);
13297 }
13298
13299 static bool
13300 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13301 size_t align)
13302 {
13303 char *p;
13304
13305 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13306 gABI specifies that PT_NOTE alignment should be aligned to 4
13307 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13308 align is less than 4, we use 4 byte alignment. */
13309 if (align < 4)
13310 align = 4;
13311 if (align != 4 && align != 8)
13312 return false;
13313
13314 p = buf;
13315 while (p < buf + size)
13316 {
13317 Elf_External_Note *xnp = (Elf_External_Note *) p;
13318 Elf_Internal_Note in;
13319
13320 if (offsetof (Elf_External_Note, name) > buf - p + size)
13321 return false;
13322
13323 in.type = H_GET_32 (abfd, xnp->type);
13324
13325 in.namesz = H_GET_32 (abfd, xnp->namesz);
13326 in.namedata = xnp->name;
13327 if (in.namesz > buf - in.namedata + size)
13328 return false;
13329
13330 in.descsz = H_GET_32 (abfd, xnp->descsz);
13331 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13332 in.descpos = offset + (in.descdata - buf);
13333 if (in.descsz != 0
13334 && (in.descdata >= buf + size
13335 || in.descsz > buf - in.descdata + size))
13336 return false;
13337
13338 switch (bfd_get_format (abfd))
13339 {
13340 default:
13341 return true;
13342
13343 case bfd_core:
13344 {
13345 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13346 struct
13347 {
13348 const char * string;
13349 size_t len;
13350 bool (*func) (bfd *, Elf_Internal_Note *);
13351 }
13352 grokers[] =
13353 {
13354 GROKER_ELEMENT ("", elfcore_grok_note),
13355 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13356 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13357 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13358 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13359 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13360 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13361 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13362 };
13363 #undef GROKER_ELEMENT
13364 int i;
13365
13366 for (i = ARRAY_SIZE (grokers); i--;)
13367 {
13368 if (in.namesz >= grokers[i].len
13369 && strncmp (in.namedata, grokers[i].string,
13370 grokers[i].len) == 0)
13371 {
13372 if (! grokers[i].func (abfd, & in))
13373 return false;
13374 break;
13375 }
13376 }
13377 break;
13378 }
13379
13380 case bfd_object:
13381 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13382 {
13383 if (! elfobj_grok_gnu_note (abfd, &in))
13384 return false;
13385 }
13386 else if (in.namesz == sizeof "stapsdt"
13387 && strcmp (in.namedata, "stapsdt") == 0)
13388 {
13389 if (! elfobj_grok_stapsdt_note (abfd, &in))
13390 return false;
13391 }
13392 break;
13393 }
13394
13395 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13396 }
13397
13398 return true;
13399 }
13400
13401 bool
13402 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13403 size_t align)
13404 {
13405 char *buf;
13406
13407 if (size == 0 || (size + 1) == 0)
13408 return true;
13409
13410 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13411 return false;
13412
13413 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13414 if (buf == NULL)
13415 return false;
13416
13417 /* PR 17512: file: ec08f814
13418 0-termintate the buffer so that string searches will not overflow. */
13419 buf[size] = 0;
13420
13421 if (!elf_parse_notes (abfd, buf, size, offset, align))
13422 {
13423 free (buf);
13424 return false;
13425 }
13426
13427 free (buf);
13428 return true;
13429 }
13430 \f
13431 /* Providing external access to the ELF program header table. */
13432
13433 /* Return an upper bound on the number of bytes required to store a
13434 copy of ABFD's program header table entries. Return -1 if an error
13435 occurs; bfd_get_error will return an appropriate code. */
13436
13437 long
13438 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13439 {
13440 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13441 {
13442 bfd_set_error (bfd_error_wrong_format);
13443 return -1;
13444 }
13445
13446 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13447 }
13448
13449 /* Copy ABFD's program header table entries to *PHDRS. The entries
13450 will be stored as an array of Elf_Internal_Phdr structures, as
13451 defined in include/elf/internal.h. To find out how large the
13452 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13453
13454 Return the number of program header table entries read, or -1 if an
13455 error occurs; bfd_get_error will return an appropriate code. */
13456
13457 int
13458 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13459 {
13460 int num_phdrs;
13461
13462 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13463 {
13464 bfd_set_error (bfd_error_wrong_format);
13465 return -1;
13466 }
13467
13468 num_phdrs = elf_elfheader (abfd)->e_phnum;
13469 if (num_phdrs != 0)
13470 memcpy (phdrs, elf_tdata (abfd)->phdr,
13471 num_phdrs * sizeof (Elf_Internal_Phdr));
13472
13473 return num_phdrs;
13474 }
13475
13476 enum elf_reloc_type_class
13477 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13478 const asection *rel_sec ATTRIBUTE_UNUSED,
13479 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13480 {
13481 return reloc_class_normal;
13482 }
13483
13484 /* For RELA architectures, return the relocation value for a
13485 relocation against a local symbol. */
13486
13487 bfd_vma
13488 _bfd_elf_rela_local_sym (bfd *abfd,
13489 Elf_Internal_Sym *sym,
13490 asection **psec,
13491 Elf_Internal_Rela *rel)
13492 {
13493 asection *sec = *psec;
13494 bfd_vma relocation;
13495
13496 relocation = (sec->output_section->vma
13497 + sec->output_offset
13498 + sym->st_value);
13499 if ((sec->flags & SEC_MERGE)
13500 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13501 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13502 {
13503 rel->r_addend =
13504 _bfd_merged_section_offset (abfd, psec,
13505 elf_section_data (sec)->sec_info,
13506 sym->st_value + rel->r_addend);
13507 if (sec != *psec)
13508 {
13509 /* If we have changed the section, and our original section is
13510 marked with SEC_EXCLUDE, it means that the original
13511 SEC_MERGE section has been completely subsumed in some
13512 other SEC_MERGE section. In this case, we need to leave
13513 some info around for --emit-relocs. */
13514 if ((sec->flags & SEC_EXCLUDE) != 0)
13515 sec->kept_section = *psec;
13516 sec = *psec;
13517 }
13518 rel->r_addend -= relocation;
13519 rel->r_addend += sec->output_section->vma + sec->output_offset;
13520 }
13521 return relocation;
13522 }
13523
13524 bfd_vma
13525 _bfd_elf_rel_local_sym (bfd *abfd,
13526 Elf_Internal_Sym *sym,
13527 asection **psec,
13528 bfd_vma addend)
13529 {
13530 asection *sec = *psec;
13531
13532 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13533 return sym->st_value + addend;
13534
13535 return _bfd_merged_section_offset (abfd, psec,
13536 elf_section_data (sec)->sec_info,
13537 sym->st_value + addend);
13538 }
13539
13540 /* Adjust an address within a section. Given OFFSET within SEC, return
13541 the new offset within the section, based upon changes made to the
13542 section. Returns -1 if the offset is now invalid.
13543 The offset (in abnd out) is in target sized bytes, however big a
13544 byte may be. */
13545
13546 bfd_vma
13547 _bfd_elf_section_offset (bfd *abfd,
13548 struct bfd_link_info *info,
13549 asection *sec,
13550 bfd_vma offset)
13551 {
13552 switch (sec->sec_info_type)
13553 {
13554 case SEC_INFO_TYPE_STABS:
13555 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13556 offset);
13557 case SEC_INFO_TYPE_EH_FRAME:
13558 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13559
13560 case SEC_INFO_TYPE_SFRAME:
13561 return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13562
13563 default:
13564 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13565 {
13566 /* Reverse the offset. */
13567 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13568 bfd_size_type address_size = bed->s->arch_size / 8;
13569
13570 /* address_size and sec->size are in octets. Convert
13571 to bytes before subtracting the original offset. */
13572 offset = ((sec->size - address_size)
13573 / bfd_octets_per_byte (abfd, sec) - offset);
13574 }
13575 return offset;
13576 }
13577 }
13578 \f
13579 long
13580 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13581 long symcount ATTRIBUTE_UNUSED,
13582 asymbol **syms ATTRIBUTE_UNUSED,
13583 long dynsymcount,
13584 asymbol **dynsyms,
13585 asymbol **ret)
13586 {
13587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13588 asection *relplt;
13589 asymbol *s;
13590 const char *relplt_name;
13591 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13592 arelent *p;
13593 long count, i, n;
13594 size_t size;
13595 Elf_Internal_Shdr *hdr;
13596 char *names;
13597 asection *plt;
13598
13599 *ret = NULL;
13600
13601 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13602 return 0;
13603
13604 if (dynsymcount <= 0)
13605 return 0;
13606
13607 if (!bed->plt_sym_val)
13608 return 0;
13609
13610 relplt_name = bed->relplt_name;
13611 if (relplt_name == NULL)
13612 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13613 relplt = bfd_get_section_by_name (abfd, relplt_name);
13614 if (relplt == NULL)
13615 return 0;
13616
13617 hdr = &elf_section_data (relplt)->this_hdr;
13618 if (hdr->sh_link != elf_dynsymtab (abfd)
13619 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13620 return 0;
13621
13622 plt = bfd_get_section_by_name (abfd, ".plt");
13623 if (plt == NULL)
13624 return 0;
13625
13626 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13627 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13628 return -1;
13629
13630 count = NUM_SHDR_ENTRIES (hdr);
13631 size = count * sizeof (asymbol);
13632 p = relplt->relocation;
13633 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13634 {
13635 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13636 if (p->addend != 0)
13637 {
13638 #ifdef BFD64
13639 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13640 #else
13641 size += sizeof ("+0x") - 1 + 8;
13642 #endif
13643 }
13644 }
13645
13646 s = *ret = (asymbol *) bfd_malloc (size);
13647 if (s == NULL)
13648 return -1;
13649
13650 names = (char *) (s + count);
13651 p = relplt->relocation;
13652 n = 0;
13653 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13654 {
13655 size_t len;
13656 bfd_vma addr;
13657
13658 addr = bed->plt_sym_val (i, plt, p);
13659 if (addr == (bfd_vma) -1)
13660 continue;
13661
13662 *s = **p->sym_ptr_ptr;
13663 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13664 we are defining a symbol, ensure one of them is set. */
13665 if ((s->flags & BSF_LOCAL) == 0)
13666 s->flags |= BSF_GLOBAL;
13667 s->flags |= BSF_SYNTHETIC;
13668 s->section = plt;
13669 s->value = addr - plt->vma;
13670 s->name = names;
13671 s->udata.p = NULL;
13672 len = strlen ((*p->sym_ptr_ptr)->name);
13673 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13674 names += len;
13675 if (p->addend != 0)
13676 {
13677 char buf[30], *a;
13678
13679 memcpy (names, "+0x", sizeof ("+0x") - 1);
13680 names += sizeof ("+0x") - 1;
13681 bfd_sprintf_vma (abfd, buf, p->addend);
13682 for (a = buf; *a == '0'; ++a)
13683 ;
13684 len = strlen (a);
13685 memcpy (names, a, len);
13686 names += len;
13687 }
13688 memcpy (names, "@plt", sizeof ("@plt"));
13689 names += sizeof ("@plt");
13690 ++s, ++n;
13691 }
13692
13693 return n;
13694 }
13695
13696 /* It is only used by x86-64 so far.
13697 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13698 but current usage would allow all of _bfd_std_section to be zero. */
13699 static const asymbol lcomm_sym
13700 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13701 asection _bfd_elf_large_com_section
13702 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13703 "LARGE_COMMON", 0, SEC_IS_COMMON);
13704
13705 bool
13706 _bfd_elf_final_write_processing (bfd *abfd)
13707 {
13708 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13709
13710 i_ehdrp = elf_elfheader (abfd);
13711
13712 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13713 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13714
13715 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13716 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13717 or STB_GNU_UNIQUE binding. */
13718 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13719 {
13720 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13721 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13722 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13723 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13724 {
13725 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13726 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13727 "and FreeBSD targets"));
13728 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13729 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13730 "only by GNU and FreeBSD targets"));
13731 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13732 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13733 "only by GNU and FreeBSD targets"));
13734 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13735 _bfd_error_handler (_("GNU_RETAIN section is supported "
13736 "only by GNU and FreeBSD targets"));
13737 bfd_set_error (bfd_error_sorry);
13738 return false;
13739 }
13740 }
13741 return true;
13742 }
13743
13744
13745 /* Return TRUE for ELF symbol types that represent functions.
13746 This is the default version of this function, which is sufficient for
13747 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13748
13749 bool
13750 _bfd_elf_is_function_type (unsigned int type)
13751 {
13752 return (type == STT_FUNC
13753 || type == STT_GNU_IFUNC);
13754 }
13755
13756 /* If the ELF symbol SYM might be a function in SEC, return the
13757 function size and set *CODE_OFF to the function's entry point,
13758 otherwise return zero. */
13759
13760 bfd_size_type
13761 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13762 bfd_vma *code_off)
13763 {
13764 bfd_size_type size;
13765 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13766
13767 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13768 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13769 || sym->section != sec)
13770 return 0;
13771
13772 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13773
13774 /* In theory we should check that the symbol's type satisfies
13775 _bfd_elf_is_function_type(), but there are some function-like
13776 symbols which would fail this test. (eg _start). Instead
13777 we check for hidden, local, notype symbols with zero size.
13778 This type of symbol is generated by the annobin plugin for gcc
13779 and clang, and should not be considered to be a function symbol. */
13780 if (size == 0
13781 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13782 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13783 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13784 return 0;
13785
13786 *code_off = sym->value;
13787 /* Do not return 0 for the function's size. */
13788 return size ? size : 1;
13789 }
13790
13791 /* Set to non-zero to enable some debug messages. */
13792 #define DEBUG_SECONDARY_RELOCS 0
13793
13794 /* An internal-to-the-bfd-library only section type
13795 used to indicate a cached secondary reloc section. */
13796 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13797
13798 /* Create a BFD section to hold a secondary reloc section. */
13799
13800 bool
13801 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13802 Elf_Internal_Shdr *hdr,
13803 const char * name,
13804 unsigned int shindex)
13805 {
13806 /* We only support RELA secondary relocs. */
13807 if (hdr->sh_type != SHT_RELA)
13808 return false;
13809
13810 #if DEBUG_SECONDARY_RELOCS
13811 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13812 #endif
13813 hdr->sh_type = SHT_SECONDARY_RELOC;
13814 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13815 }
13816
13817 /* Read in any secondary relocs associated with SEC. */
13818
13819 bool
13820 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13821 asection * sec,
13822 asymbol ** symbols,
13823 bool dynamic)
13824 {
13825 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13826 asection * relsec;
13827 bool result = true;
13828 bfd_vma (*r_sym) (bfd_vma);
13829 ufile_ptr filesize;
13830
13831 #if BFD_DEFAULT_TARGET_SIZE > 32
13832 if (bfd_arch_bits_per_address (abfd) != 32)
13833 r_sym = elf64_r_sym;
13834 else
13835 #endif
13836 r_sym = elf32_r_sym;
13837
13838 if (!elf_section_data (sec)->has_secondary_relocs)
13839 return true;
13840
13841 /* Discover if there are any secondary reloc sections
13842 associated with SEC. */
13843 filesize = bfd_get_file_size (abfd);
13844 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13845 {
13846 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13847
13848 if (hdr->sh_type == SHT_SECONDARY_RELOC
13849 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13850 && (hdr->sh_entsize == ebd->s->sizeof_rel
13851 || hdr->sh_entsize == ebd->s->sizeof_rela))
13852 {
13853 bfd_byte * native_relocs;
13854 bfd_byte * native_reloc;
13855 arelent * internal_relocs;
13856 arelent * internal_reloc;
13857 size_t i;
13858 unsigned int entsize;
13859 unsigned int symcount;
13860 bfd_size_type reloc_count;
13861 size_t amt;
13862
13863 if (ebd->elf_info_to_howto == NULL)
13864 return false;
13865
13866 #if DEBUG_SECONDARY_RELOCS
13867 fprintf (stderr, "read secondary relocs for %s from %s\n",
13868 sec->name, relsec->name);
13869 #endif
13870 entsize = hdr->sh_entsize;
13871
13872 if (filesize != 0
13873 && ((ufile_ptr) hdr->sh_offset > filesize
13874 || hdr->sh_size > filesize - hdr->sh_offset))
13875 {
13876 bfd_set_error (bfd_error_file_truncated);
13877 result = false;
13878 continue;
13879 }
13880
13881 native_relocs = bfd_malloc (hdr->sh_size);
13882 if (native_relocs == NULL)
13883 {
13884 result = false;
13885 continue;
13886 }
13887
13888 reloc_count = NUM_SHDR_ENTRIES (hdr);
13889 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13890 {
13891 free (native_relocs);
13892 bfd_set_error (bfd_error_file_too_big);
13893 result = false;
13894 continue;
13895 }
13896
13897 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13898 if (internal_relocs == NULL)
13899 {
13900 free (native_relocs);
13901 result = false;
13902 continue;
13903 }
13904
13905 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13906 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13907 {
13908 free (native_relocs);
13909 /* The internal_relocs will be freed when
13910 the memory for the bfd is released. */
13911 result = false;
13912 continue;
13913 }
13914
13915 if (dynamic)
13916 symcount = bfd_get_dynamic_symcount (abfd);
13917 else
13918 symcount = bfd_get_symcount (abfd);
13919
13920 for (i = 0, internal_reloc = internal_relocs,
13921 native_reloc = native_relocs;
13922 i < reloc_count;
13923 i++, internal_reloc++, native_reloc += entsize)
13924 {
13925 bool res;
13926 Elf_Internal_Rela rela;
13927
13928 if (entsize == ebd->s->sizeof_rel)
13929 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13930 else /* entsize == ebd->s->sizeof_rela */
13931 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13932
13933 /* The address of an ELF reloc is section relative for an object
13934 file, and absolute for an executable file or shared library.
13935 The address of a normal BFD reloc is always section relative,
13936 and the address of a dynamic reloc is absolute.. */
13937 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13938 internal_reloc->address = rela.r_offset;
13939 else
13940 internal_reloc->address = rela.r_offset - sec->vma;
13941
13942 if (r_sym (rela.r_info) == STN_UNDEF)
13943 {
13944 /* FIXME: This and the error case below mean that we
13945 have a symbol on relocs that is not elf_symbol_type. */
13946 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13947 }
13948 else if (r_sym (rela.r_info) > symcount)
13949 {
13950 _bfd_error_handler
13951 /* xgettext:c-format */
13952 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13953 abfd, sec, i, (long) r_sym (rela.r_info));
13954 bfd_set_error (bfd_error_bad_value);
13955 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13956 result = false;
13957 }
13958 else
13959 {
13960 asymbol **ps;
13961
13962 ps = symbols + r_sym (rela.r_info) - 1;
13963 internal_reloc->sym_ptr_ptr = ps;
13964 /* Make sure that this symbol is not removed by strip. */
13965 (*ps)->flags |= BSF_KEEP;
13966 }
13967
13968 internal_reloc->addend = rela.r_addend;
13969
13970 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13971 if (! res || internal_reloc->howto == NULL)
13972 {
13973 #if DEBUG_SECONDARY_RELOCS
13974 fprintf (stderr,
13975 "there is no howto associated with reloc %lx\n",
13976 rela.r_info);
13977 #endif
13978 result = false;
13979 }
13980 }
13981
13982 free (native_relocs);
13983 /* Store the internal relocs. */
13984 elf_section_data (relsec)->sec_info = internal_relocs;
13985 }
13986 }
13987
13988 return result;
13989 }
13990
13991 /* Set the ELF section header fields of an output secondary reloc section. */
13992
13993 bool
13994 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13995 bfd *obfd ATTRIBUTE_UNUSED,
13996 const Elf_Internal_Shdr *isection,
13997 Elf_Internal_Shdr *osection)
13998 {
13999 asection * isec;
14000 asection * osec;
14001 struct bfd_elf_section_data * esd;
14002
14003 if (isection == NULL)
14004 return false;
14005
14006 if (isection->sh_type != SHT_SECONDARY_RELOC)
14007 return true;
14008
14009 isec = isection->bfd_section;
14010 if (isec == NULL)
14011 return false;
14012
14013 osec = osection->bfd_section;
14014 if (osec == NULL)
14015 return false;
14016
14017 esd = elf_section_data (osec);
14018 BFD_ASSERT (esd->sec_info == NULL);
14019 esd->sec_info = elf_section_data (isec)->sec_info;
14020 osection->sh_type = SHT_RELA;
14021 osection->sh_link = elf_onesymtab (obfd);
14022 if (osection->sh_link == 0)
14023 {
14024 /* There is no symbol table - we are hosed... */
14025 _bfd_error_handler
14026 /* xgettext:c-format */
14027 (_("%pB(%pA): link section cannot be set"
14028 " because the output file does not have a symbol table"),
14029 obfd, osec);
14030 bfd_set_error (bfd_error_bad_value);
14031 return false;
14032 }
14033
14034 /* Find the output section that corresponds to the isection's
14035 sh_info link. */
14036 if (isection->sh_info == 0
14037 || isection->sh_info >= elf_numsections (ibfd))
14038 {
14039 _bfd_error_handler
14040 /* xgettext:c-format */
14041 (_("%pB(%pA): info section index is invalid"),
14042 obfd, osec);
14043 bfd_set_error (bfd_error_bad_value);
14044 return false;
14045 }
14046
14047 isection = elf_elfsections (ibfd)[isection->sh_info];
14048
14049 if (isection == NULL
14050 || isection->bfd_section == NULL
14051 || isection->bfd_section->output_section == NULL)
14052 {
14053 _bfd_error_handler
14054 /* xgettext:c-format */
14055 (_("%pB(%pA): info section index cannot be set"
14056 " because the section is not in the output"),
14057 obfd, osec);
14058 bfd_set_error (bfd_error_bad_value);
14059 return false;
14060 }
14061
14062 esd = elf_section_data (isection->bfd_section->output_section);
14063 BFD_ASSERT (esd != NULL);
14064 osection->sh_info = esd->this_idx;
14065 esd->has_secondary_relocs = true;
14066 #if DEBUG_SECONDARY_RELOCS
14067 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14068 osec->name, osection->sh_link, osection->sh_info);
14069 fprintf (stderr, "mark section %s as having secondary relocs\n",
14070 bfd_section_name (isection->bfd_section->output_section));
14071 #endif
14072
14073 return true;
14074 }
14075
14076 /* Write out a secondary reloc section.
14077
14078 FIXME: Currently this function can result in a serious performance penalty
14079 for files with secondary relocs and lots of sections. The proper way to
14080 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14081 relocs together and then to have this function just walk that chain. */
14082
14083 bool
14084 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14085 {
14086 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14087 bfd_vma addr_offset;
14088 asection * relsec;
14089 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14090 bool result = true;
14091
14092 if (sec == NULL)
14093 return false;
14094
14095 #if BFD_DEFAULT_TARGET_SIZE > 32
14096 if (bfd_arch_bits_per_address (abfd) != 32)
14097 r_info = elf64_r_info;
14098 else
14099 #endif
14100 r_info = elf32_r_info;
14101
14102 /* The address of an ELF reloc is section relative for an object
14103 file, and absolute for an executable file or shared library.
14104 The address of a BFD reloc is always section relative. */
14105 addr_offset = 0;
14106 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14107 addr_offset = sec->vma;
14108
14109 /* Discover if there are any secondary reloc sections
14110 associated with SEC. */
14111 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14112 {
14113 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14114 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14115
14116 if (hdr->sh_type == SHT_RELA
14117 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14118 {
14119 asymbol * last_sym;
14120 int last_sym_idx;
14121 size_t reloc_count;
14122 size_t idx;
14123 bfd_size_type entsize;
14124 arelent * src_irel;
14125 bfd_byte * dst_rela;
14126
14127 if (hdr->contents != NULL)
14128 {
14129 _bfd_error_handler
14130 /* xgettext:c-format */
14131 (_("%pB(%pA): error: secondary reloc section processed twice"),
14132 abfd, relsec);
14133 bfd_set_error (bfd_error_bad_value);
14134 result = false;
14135 continue;
14136 }
14137
14138 entsize = hdr->sh_entsize;
14139 if (entsize == 0)
14140 {
14141 _bfd_error_handler
14142 /* xgettext:c-format */
14143 (_("%pB(%pA): error: secondary reloc section"
14144 " has zero sized entries"),
14145 abfd, relsec);
14146 bfd_set_error (bfd_error_bad_value);
14147 result = false;
14148 continue;
14149 }
14150 else if (entsize != ebd->s->sizeof_rel
14151 && entsize != ebd->s->sizeof_rela)
14152 {
14153 _bfd_error_handler
14154 /* xgettext:c-format */
14155 (_("%pB(%pA): error: secondary reloc section"
14156 " has non-standard sized entries"),
14157 abfd, relsec);
14158 bfd_set_error (bfd_error_bad_value);
14159 result = false;
14160 continue;
14161 }
14162
14163 reloc_count = hdr->sh_size / entsize;
14164 hdr->sh_size = entsize * reloc_count;
14165 if (reloc_count == 0)
14166 {
14167 _bfd_error_handler
14168 /* xgettext:c-format */
14169 (_("%pB(%pA): error: secondary reloc section is empty!"),
14170 abfd, relsec);
14171 bfd_set_error (bfd_error_bad_value);
14172 result = false;
14173 continue;
14174 }
14175
14176 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14177 if (hdr->contents == NULL)
14178 continue;
14179 relsec->alloced = 1;
14180
14181 #if DEBUG_SECONDARY_RELOCS
14182 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14183 reloc_count, sec->name, relsec->name);
14184 #endif
14185 last_sym = NULL;
14186 last_sym_idx = 0;
14187 dst_rela = hdr->contents;
14188 src_irel = (arelent *) esd->sec_info;
14189 if (src_irel == NULL)
14190 {
14191 _bfd_error_handler
14192 /* xgettext:c-format */
14193 (_("%pB(%pA): error: internal relocs missing"
14194 " for secondary reloc section"),
14195 abfd, relsec);
14196 bfd_set_error (bfd_error_bad_value);
14197 result = false;
14198 continue;
14199 }
14200
14201 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14202 {
14203 Elf_Internal_Rela src_rela;
14204 arelent *ptr;
14205 asymbol *sym;
14206 int n;
14207
14208 ptr = src_irel + idx;
14209 if (ptr == NULL)
14210 {
14211 _bfd_error_handler
14212 /* xgettext:c-format */
14213 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14214 abfd, relsec, idx);
14215 bfd_set_error (bfd_error_bad_value);
14216 result = false;
14217 break;
14218 }
14219
14220 if (ptr->sym_ptr_ptr == NULL)
14221 {
14222 /* FIXME: Is this an error ? */
14223 n = 0;
14224 }
14225 else
14226 {
14227 sym = *ptr->sym_ptr_ptr;
14228
14229 if (sym == last_sym)
14230 n = last_sym_idx;
14231 else
14232 {
14233 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14234 if (n < 0)
14235 {
14236 _bfd_error_handler
14237 /* xgettext:c-format */
14238 (_("%pB(%pA): error: secondary reloc %zu"
14239 " references a missing symbol"),
14240 abfd, relsec, idx);
14241 bfd_set_error (bfd_error_bad_value);
14242 result = false;
14243 n = 0;
14244 }
14245
14246 last_sym = sym;
14247 last_sym_idx = n;
14248 }
14249
14250 if (sym->the_bfd != NULL
14251 && sym->the_bfd->xvec != abfd->xvec
14252 && ! _bfd_elf_validate_reloc (abfd, ptr))
14253 {
14254 _bfd_error_handler
14255 /* xgettext:c-format */
14256 (_("%pB(%pA): error: secondary reloc %zu"
14257 " references a deleted symbol"),
14258 abfd, relsec, idx);
14259 bfd_set_error (bfd_error_bad_value);
14260 result = false;
14261 n = 0;
14262 }
14263 }
14264
14265 src_rela.r_offset = ptr->address + addr_offset;
14266 if (ptr->howto == NULL)
14267 {
14268 _bfd_error_handler
14269 /* xgettext:c-format */
14270 (_("%pB(%pA): error: secondary reloc %zu"
14271 " is of an unknown type"),
14272 abfd, relsec, idx);
14273 bfd_set_error (bfd_error_bad_value);
14274 result = false;
14275 src_rela.r_info = r_info (0, 0);
14276 }
14277 else
14278 src_rela.r_info = r_info (n, ptr->howto->type);
14279 src_rela.r_addend = ptr->addend;
14280
14281 if (entsize == ebd->s->sizeof_rel)
14282 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14283 else /* entsize == ebd->s->sizeof_rela */
14284 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14285 }
14286 }
14287 }
14288
14289 return result;
14290 }
14291
14292 /* Mmap in section contents. */
14293
14294 static bool
14295 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14296 {
14297 #ifdef USE_MMAP
14298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14299 if (bed->use_mmap
14300 && sec->compress_status == COMPRESS_SECTION_NONE
14301 && (sec->flags & SEC_LINKER_CREATED) == 0)
14302 {
14303 /* Use mmap only if section size >= the minimum mmap section
14304 size. */
14305 size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14306 size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14307 if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14308 {
14309 if (sec->contents != NULL)
14310 {
14311 if (!sec->mmapped_p)
14312 abort ();
14313 *buf = sec->contents;
14314 return true;
14315 }
14316 if (sec->mmapped_p)
14317 abort ();
14318 sec->mmapped_p = 1;
14319
14320 /* We can't use the final link preallocated buffer for mmap. */
14321 *buf = NULL;
14322 }
14323 }
14324 #endif
14325 /* FIXME: We should not get here if sec->alloced is set. */
14326 bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14327 if (ret && sec->mmapped_p)
14328 *buf = sec->contents;
14329 return ret;
14330 }
14331
14332 /* Mmap in section contents. */
14333
14334 bool
14335 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14336 {
14337 *buf = NULL;
14338 return elf_mmap_section_contents (abfd, sec, buf);
14339 }
14340
14341 /* Mmap in the full section contents for the final link. */
14342
14343 bool
14344 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14345 bfd_byte **buf)
14346 {
14347 return elf_mmap_section_contents (abfd, sec, buf);
14348 }
14349
14350 /* Munmap section contents. */
14351
14352 void
14353 _bfd_elf_munmap_section_contents (asection *sec, void *contents)
14354 {
14355 /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14356 CONTENTS may be NULL. */
14357 if (contents == NULL)
14358 return;
14359
14360 if (sec->alloced
14361 /* What a tangled web we weave with section contents.
14362 FIXME: We shouldn't need to test anything but sec->alloced
14363 here, but there are cases where a buffer is allocated for a
14364 section but then another buffer is malloc'd anyway. eg.
14365 trace through ld-elf/eh4 testcase on x86_64. */
14366 && (sec->contents == contents
14367 || elf_section_data (sec)->this_hdr.contents == contents))
14368 return;
14369
14370 /* Don't leave pointers to data we are about to munmap or free. */
14371 if (sec->contents == contents)
14372 sec->contents = NULL;
14373 if (elf_section_data (sec)->this_hdr.contents == contents)
14374 elf_section_data (sec)->this_hdr.contents = NULL;
14375
14376 #ifdef USE_MMAP
14377 if (sec->mmapped_p)
14378 {
14379 /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14380 malloced, CONTENTS_ADDR is set to NULL. */
14381 if (elf_section_data (sec)->contents_addr != NULL)
14382 {
14383 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14384 if (munmap (elf_section_data (sec)->contents_addr,
14385 elf_section_data (sec)->contents_size) != 0)
14386 abort ();
14387 sec->mmapped_p = 0;
14388 elf_section_data (sec)->contents_addr = NULL;
14389 elf_section_data (sec)->contents_size = 0;
14390 return;
14391 }
14392 }
14393 #endif
14394
14395 free (contents);
14396 }
14397
14398 /* Munmap the full section contents for the final link. */
14399
14400 void
14401 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14402 {
14403 #ifdef USE_MMAP
14404 if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14405 {
14406 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14407 malloced, CONTENTS_ADDR is set to NULL. */
14408 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14409 if (munmap (elf_section_data (sec)->contents_addr,
14410 elf_section_data (sec)->contents_size) != 0)
14411 abort ();
14412 sec->mmapped_p = 0;
14413 sec->contents = NULL;
14414 elf_section_data (sec)->this_hdr.contents = NULL;
14415 elf_section_data (sec)->contents_addr = NULL;
14416 elf_section_data (sec)->contents_size = 0;
14417 }
14418 #endif
14419 }