elf.c revision 1.1.1.12 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
1152 /* Returns TRUE if section A matches section B.
1154 Names, addresses and links may be different, but everything else
1155 should be the same. */
1156
1157 static bool
1158 section_match (const Elf_Internal_Shdr * a,
1159 const Elf_Internal_Shdr * b)
1160 {
1161 if (a->sh_type != b->sh_type
1162 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1163 || a->sh_addralign != b->sh_addralign
1164 || a->sh_entsize != b->sh_entsize)
1165 return false;
1166 if (a->sh_type == SHT_SYMTAB
1167 || a->sh_type == SHT_STRTAB)
1168 return true;
1169 return a->sh_size == b->sh_size;
1170 }
1171
1172 /* Find a section in OBFD that has the same characteristics
1173 as IHEADER. Return the index of this section or SHN_UNDEF if
1174 none can be found. Check's section HINT first, as this is likely
1175 to be the correct section. */
1176
1177 static unsigned int
1178 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1179 const unsigned int hint)
1180 {
1181 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1182 unsigned int i;
1183
1184 BFD_ASSERT (iheader != NULL);
1185
1186 /* See PR 20922 for a reproducer of the NULL test. */
1187 if (hint < elf_numsections (obfd)
1188 && oheaders[hint] != NULL
1189 && section_match (oheaders[hint], iheader))
1190 return hint;
1191
1192 for (i = 1; i < elf_numsections (obfd); i++)
1193 {
1194 Elf_Internal_Shdr * oheader = oheaders[i];
1195
1196 if (oheader == NULL)
1197 continue;
1198 if (section_match (oheader, iheader))
1199 /* FIXME: Do we care if there is a potential for
1200 multiple matches ? */
1201 return i;
1202 }
1203
1204 return SHN_UNDEF;
1205 }
1206
1207 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1208 Processor specific section, based upon a matching input section.
1209 Returns TRUE upon success, FALSE otherwise. */
1210
1211 static bool
1212 copy_special_section_fields (const bfd *ibfd,
1213 bfd *obfd,
1214 const Elf_Internal_Shdr *iheader,
1215 Elf_Internal_Shdr *oheader,
1216 const unsigned int secnum)
1217 {
1218 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1219 const Elf_Internal_Shdr **iheaders
1220 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1221 bool changed = false;
1222 unsigned int sh_link;
1223
1224 if (oheader->sh_type == SHT_NOBITS)
1225 {
1226 /* This is a feature for objcopy --only-keep-debug:
1227 When a section's type is changed to NOBITS, we preserve
1228 the sh_link and sh_info fields so that they can be
1229 matched up with the original.
1230
1231 Note: Strictly speaking these assignments are wrong.
1232 The sh_link and sh_info fields should point to the
1233 relevent sections in the output BFD, which may not be in
1234 the same location as they were in the input BFD. But
1235 the whole point of this action is to preserve the
1236 original values of the sh_link and sh_info fields, so
1237 that they can be matched up with the section headers in
1238 the original file. So strictly speaking we may be
1239 creating an invalid ELF file, but it is only for a file
1240 that just contains debug info and only for sections
1241 without any contents. */
1242 if (oheader->sh_link == 0)
1243 oheader->sh_link = iheader->sh_link;
1244 if (oheader->sh_info == 0)
1245 oheader->sh_info = iheader->sh_info;
1246 return true;
1247 }
1248
1249 /* Allow the target a chance to decide how these fields should be set. */
1250 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1251 iheader, oheader))
1252 return true;
1253
1254 /* We have an iheader which might match oheader, and which has non-zero
1255 sh_info and/or sh_link fields. Attempt to follow those links and find
1256 the section in the output bfd which corresponds to the linked section
1257 in the input bfd. */
1258 if (iheader->sh_link != SHN_UNDEF)
1259 {
1260 /* See PR 20931 for a reproducer. */
1261 if (iheader->sh_link >= elf_numsections (ibfd))
1262 {
1263 _bfd_error_handler
1264 /* xgettext:c-format */
1265 (_("%pB: invalid sh_link field (%d) in section number %d"),
1266 ibfd, iheader->sh_link, secnum);
1267 return false;
1268 }
1269
1270 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1271 if (sh_link != SHN_UNDEF)
1272 {
1273 oheader->sh_link = sh_link;
1274 changed = true;
1275 }
1276 else
1277 /* FIXME: Should we install iheader->sh_link
1278 if we could not find a match ? */
1279 _bfd_error_handler
1280 /* xgettext:c-format */
1281 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1282 }
1283
1284 if (iheader->sh_info)
1285 {
1286 /* The sh_info field can hold arbitrary information, but if the
1287 SHF_LINK_INFO flag is set then it should be interpreted as a
1288 section index. */
1289 if (iheader->sh_flags & SHF_INFO_LINK)
1290 {
1291 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1292 iheader->sh_info);
1293 if (sh_link != SHN_UNDEF)
1294 oheader->sh_flags |= SHF_INFO_LINK;
1295 }
1296 else
1297 /* No idea what it means - just copy it. */
1298 sh_link = iheader->sh_info;
1299
1300 if (sh_link != SHN_UNDEF)
1301 {
1302 oheader->sh_info = sh_link;
1303 changed = true;
1304 }
1305 else
1306 _bfd_error_handler
1307 /* xgettext:c-format */
1308 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1309 }
1310
1311 return changed;
1312 }
1313
1314 /* Copy the program header and other data from one object module to
1315 another. */
1316
1317 bool
1318 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1319 {
1320 const Elf_Internal_Shdr **iheaders
1321 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1322 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1323 const struct elf_backend_data *bed;
1324 unsigned int i;
1325
1326 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1327 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1328 return true;
1329
1330 if (!elf_flags_init (obfd))
1331 {
1332 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1333 elf_flags_init (obfd) = true;
1334 }
1335
1336 elf_gp (obfd) = elf_gp (ibfd);
1337
1338 /* Also copy the EI_OSABI field. */
1339 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1340 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1341
1342 /* If set, copy the EI_ABIVERSION field. */
1343 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1344 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1345 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1346
1347 /* Copy object attributes. */
1348 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1349
1350 if (iheaders == NULL || oheaders == NULL)
1351 return true;
1352
1353 bed = get_elf_backend_data (obfd);
1354
1355 /* Possibly copy other fields in the section header. */
1356 for (i = 1; i < elf_numsections (obfd); i++)
1357 {
1358 unsigned int j;
1359 Elf_Internal_Shdr * oheader = oheaders[i];
1360
1361 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1362 because of a special case need for generating separate debug info
1363 files. See below for more details. */
1364 if (oheader == NULL
1365 || (oheader->sh_type != SHT_NOBITS
1366 && oheader->sh_type < SHT_LOOS))
1367 continue;
1368
1369 /* Ignore empty sections, and sections whose
1370 fields have already been initialised. */
1371 if (oheader->sh_size == 0
1372 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1373 continue;
1374
1375 /* Scan for the matching section in the input bfd.
1376 First we try for a direct mapping between the input and
1377 output sections. */
1378 for (j = 1; j < elf_numsections (ibfd); j++)
1379 {
1380 const Elf_Internal_Shdr * iheader = iheaders[j];
1381
1382 if (iheader == NULL)
1383 continue;
1384
1385 if (oheader->bfd_section != NULL
1386 && iheader->bfd_section != NULL
1387 && iheader->bfd_section->output_section != NULL
1388 && iheader->bfd_section->output_section == oheader->bfd_section)
1389 {
1390 /* We have found a connection from the input section to
1391 the output section. Attempt to copy the header fields.
1392 If this fails then do not try any further sections -
1393 there should only be a one-to-one mapping between
1394 input and output. */
1395 if (!copy_special_section_fields (ibfd, obfd,
1396 iheader, oheader, i))
1397 j = elf_numsections (ibfd);
1398 break;
1399 }
1400 }
1401
1402 if (j < elf_numsections (ibfd))
1403 continue;
1404
1405 /* That failed. So try to deduce the corresponding input section.
1406 Unfortunately we cannot compare names as the output string table
1407 is empty, so instead we check size, address and type. */
1408 for (j = 1; j < elf_numsections (ibfd); j++)
1409 {
1410 const Elf_Internal_Shdr * iheader = iheaders[j];
1411
1412 if (iheader == NULL)
1413 continue;
1414
1415 /* Try matching fields in the input section's header.
1416 Since --only-keep-debug turns all non-debug sections into
1417 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1418 input type. */
1419 if ((oheader->sh_type == SHT_NOBITS
1420 || iheader->sh_type == oheader->sh_type)
1421 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1422 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1423 && iheader->sh_addralign == oheader->sh_addralign
1424 && iheader->sh_entsize == oheader->sh_entsize
1425 && iheader->sh_size == oheader->sh_size
1426 && iheader->sh_addr == oheader->sh_addr
1427 && (iheader->sh_info != oheader->sh_info
1428 || iheader->sh_link != oheader->sh_link))
1429 {
1430 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1431 break;
1432 }
1433 }
1434
1435 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1436 {
1437 /* Final attempt. Call the backend copy function
1438 with a NULL input section. */
1439 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1440 NULL, oheader);
1441 }
1442 }
1443
1444 return true;
1445 }
1446
1447 static const char *
1448 get_segment_type (unsigned int p_type)
1449 {
1450 const char *pt;
1451 switch (p_type)
1452 {
1453 case PT_NULL: pt = "NULL"; break;
1454 case PT_LOAD: pt = "LOAD"; break;
1455 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1456 case PT_INTERP: pt = "INTERP"; break;
1457 case PT_NOTE: pt = "NOTE"; break;
1458 case PT_SHLIB: pt = "SHLIB"; break;
1459 case PT_PHDR: pt = "PHDR"; break;
1460 case PT_TLS: pt = "TLS"; break;
1461 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1462 case PT_GNU_STACK: pt = "STACK"; break;
1463 case PT_GNU_RELRO: pt = "RELRO"; break;
1464 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1465 default: pt = NULL; break;
1466 }
1467 return pt;
1468 }
1469
1470 /* Print out the program headers. */
1471
1472 bool
1473 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1474 {
1475 FILE *f = (FILE *) farg;
1476 Elf_Internal_Phdr *p;
1477 asection *s;
1478 bfd_byte *dynbuf = NULL;
1479
1480 p = elf_tdata (abfd)->phdr;
1481 if (p != NULL)
1482 {
1483 unsigned int i, c;
1484
1485 fprintf (f, _("\nProgram Header:\n"));
1486 c = elf_elfheader (abfd)->e_phnum;
1487 for (i = 0; i < c; i++, p++)
1488 {
1489 const char *pt = get_segment_type (p->p_type);
1490 char buf[20];
1491
1492 if (pt == NULL)
1493 {
1494 sprintf (buf, "0x%lx", p->p_type);
1495 pt = buf;
1496 }
1497 fprintf (f, "%8s off 0x", pt);
1498 bfd_fprintf_vma (abfd, f, p->p_offset);
1499 fprintf (f, " vaddr 0x");
1500 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1501 fprintf (f, " paddr 0x");
1502 bfd_fprintf_vma (abfd, f, p->p_paddr);
1503 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1504 fprintf (f, " filesz 0x");
1505 bfd_fprintf_vma (abfd, f, p->p_filesz);
1506 fprintf (f, " memsz 0x");
1507 bfd_fprintf_vma (abfd, f, p->p_memsz);
1508 fprintf (f, " flags %c%c%c",
1509 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1510 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1511 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1512 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1513 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1514 fprintf (f, "\n");
1515 }
1516 }
1517
1518 s = bfd_get_section_by_name (abfd, ".dynamic");
1519 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1520 {
1521 unsigned int elfsec;
1522 unsigned long shlink;
1523 bfd_byte *extdyn, *extdynend;
1524 size_t extdynsize;
1525 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1526
1527 fprintf (f, _("\nDynamic Section:\n"));
1528
1529 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1530 goto error_return;
1531
1532 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1533 if (elfsec == SHN_BAD)
1534 goto error_return;
1535 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1536
1537 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1538 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1539
1540 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1541 (size_t) (extdynend - extdyn) >= extdynsize;
1542 extdyn += extdynsize)
1543 {
1544 Elf_Internal_Dyn dyn;
1545 const char *name = "";
1546 char ab[20];
1547 bool stringp;
1548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1549
1550 (*swap_dyn_in) (abfd, extdyn, &dyn);
1551
1552 if (dyn.d_tag == DT_NULL)
1553 break;
1554
1555 stringp = false;
1556 switch (dyn.d_tag)
1557 {
1558 default:
1559 if (bed->elf_backend_get_target_dtag)
1560 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1561
1562 if (!strcmp (name, ""))
1563 {
1564 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1565 name = ab;
1566 }
1567 break;
1568
1569 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1570 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1571 case DT_PLTGOT: name = "PLTGOT"; break;
1572 case DT_HASH: name = "HASH"; break;
1573 case DT_STRTAB: name = "STRTAB"; break;
1574 case DT_SYMTAB: name = "SYMTAB"; break;
1575 case DT_RELA: name = "RELA"; break;
1576 case DT_RELASZ: name = "RELASZ"; break;
1577 case DT_RELAENT: name = "RELAENT"; break;
1578 case DT_STRSZ: name = "STRSZ"; break;
1579 case DT_SYMENT: name = "SYMENT"; break;
1580 case DT_INIT: name = "INIT"; break;
1581 case DT_FINI: name = "FINI"; break;
1582 case DT_SONAME: name = "SONAME"; stringp = true; break;
1583 case DT_RPATH: name = "RPATH"; stringp = true; break;
1584 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1585 case DT_REL: name = "REL"; break;
1586 case DT_RELSZ: name = "RELSZ"; break;
1587 case DT_RELENT: name = "RELENT"; break;
1588 case DT_RELR: name = "RELR"; break;
1589 case DT_RELRSZ: name = "RELRSZ"; break;
1590 case DT_RELRENT: name = "RELRENT"; break;
1591 case DT_PLTREL: name = "PLTREL"; break;
1592 case DT_DEBUG: name = "DEBUG"; break;
1593 case DT_TEXTREL: name = "TEXTREL"; break;
1594 case DT_JMPREL: name = "JMPREL"; break;
1595 case DT_BIND_NOW: name = "BIND_NOW"; break;
1596 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1597 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1598 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1599 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1600 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1601 case DT_FLAGS: name = "FLAGS"; break;
1602 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1603 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1604 case DT_CHECKSUM: name = "CHECKSUM"; break;
1605 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1606 case DT_MOVEENT: name = "MOVEENT"; break;
1607 case DT_MOVESZ: name = "MOVESZ"; break;
1608 case DT_FEATURE: name = "FEATURE"; break;
1609 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1610 case DT_SYMINSZ: name = "SYMINSZ"; break;
1611 case DT_SYMINENT: name = "SYMINENT"; break;
1612 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1613 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1614 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1615 case DT_PLTPAD: name = "PLTPAD"; break;
1616 case DT_MOVETAB: name = "MOVETAB"; break;
1617 case DT_SYMINFO: name = "SYMINFO"; break;
1618 case DT_RELACOUNT: name = "RELACOUNT"; break;
1619 case DT_RELCOUNT: name = "RELCOUNT"; break;
1620 case DT_FLAGS_1: name = "FLAGS_1"; break;
1621 case DT_VERSYM: name = "VERSYM"; break;
1622 case DT_VERDEF: name = "VERDEF"; break;
1623 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1624 case DT_VERNEED: name = "VERNEED"; break;
1625 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1626 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1627 case DT_USED: name = "USED"; break;
1628 case DT_FILTER: name = "FILTER"; stringp = true; break;
1629 case DT_GNU_HASH: name = "GNU_HASH"; break;
1630 }
1631
1632 fprintf (f, " %-20s ", name);
1633 if (! stringp)
1634 {
1635 fprintf (f, "0x");
1636 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1637 }
1638 else
1639 {
1640 const char *string;
1641 unsigned int tagv = dyn.d_un.d_val;
1642
1643 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1644 if (string == NULL)
1645 goto error_return;
1646 fprintf (f, "%s", string);
1647 }
1648 fprintf (f, "\n");
1649 }
1650
1651 _bfd_elf_munmap_section_contents (s, dynbuf);
1652 dynbuf = NULL;
1653 }
1654
1655 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1656 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1657 {
1658 if (! _bfd_elf_slurp_version_tables (abfd, false))
1659 return false;
1660 }
1661
1662 if (elf_dynverdef (abfd) != 0)
1663 {
1664 Elf_Internal_Verdef *t;
1665
1666 fprintf (f, _("\nVersion definitions:\n"));
1667 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1668 {
1669 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1670 t->vd_flags, t->vd_hash,
1671 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1672 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1673 {
1674 Elf_Internal_Verdaux *a;
1675
1676 fprintf (f, "\t");
1677 for (a = t->vd_auxptr->vda_nextptr;
1678 a != NULL;
1679 a = a->vda_nextptr)
1680 fprintf (f, "%s ",
1681 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1682 fprintf (f, "\n");
1683 }
1684 }
1685 }
1686
1687 if (elf_dynverref (abfd) != 0)
1688 {
1689 Elf_Internal_Verneed *t;
1690
1691 fprintf (f, _("\nVersion References:\n"));
1692 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1693 {
1694 Elf_Internal_Vernaux *a;
1695
1696 fprintf (f, _(" required from %s:\n"),
1697 t->vn_filename ? t->vn_filename : "<corrupt>");
1698 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1699 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1700 a->vna_flags, a->vna_other,
1701 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1702 }
1703 }
1704
1705 return true;
1706
1707 error_return:
1708 _bfd_elf_munmap_section_contents (s, dynbuf);
1709 return false;
1710 }
1711
1712 /* Find the file offset corresponding to VMA by using the program
1713 headers. */
1714
1715 static file_ptr
1716 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1717 size_t size, size_t *max_size_p)
1718 {
1719 Elf_Internal_Phdr *seg;
1720 size_t i;
1721
1722 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1723 if (seg->p_type == PT_LOAD
1724 && vma >= (seg->p_vaddr & -seg->p_align)
1725 && vma + size <= seg->p_vaddr + seg->p_filesz)
1726 {
1727 if (max_size_p)
1728 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1729 return vma - seg->p_vaddr + seg->p_offset;
1730 }
1731
1732 if (max_size_p)
1733 *max_size_p = 0;
1734 bfd_set_error (bfd_error_invalid_operation);
1735 return (file_ptr) -1;
1736 }
1737
1738 /* Convert hash table to internal form. */
1739
1740 static bfd_vma *
1741 get_hash_table_data (bfd *abfd, bfd_size_type number,
1742 unsigned int ent_size, bfd_size_type filesize)
1743 {
1744 unsigned char *e_data = NULL;
1745 bfd_vma *i_data = NULL;
1746 bfd_size_type size;
1747 void *e_data_addr;
1748 size_t e_data_size ATTRIBUTE_UNUSED;
1749
1750 if (ent_size != 4 && ent_size != 8)
1751 return NULL;
1752
1753 if ((size_t) number != number)
1754 {
1755 bfd_set_error (bfd_error_file_too_big);
1756 return NULL;
1757 }
1758
1759 size = ent_size * number;
1760 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1761 attempting to allocate memory when the read is bound to fail. */
1762 if (size > filesize
1763 || number >= ~(size_t) 0 / ent_size
1764 || number >= ~(size_t) 0 / sizeof (*i_data))
1765 {
1766 bfd_set_error (bfd_error_file_too_big);
1767 return NULL;
1768 }
1769
1770 e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1771 if (e_data == NULL)
1772 return NULL;
1773
1774 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1775 if (i_data == NULL)
1776 {
1777 _bfd_munmap_temporary (e_data_addr, e_data_size);
1778 return NULL;
1779 }
1780
1781 if (ent_size == 4)
1782 while (number--)
1783 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1784 else
1785 while (number--)
1786 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1787
1788 _bfd_munmap_temporary (e_data_addr, e_data_size);
1789 return i_data;
1790 }
1791
1792 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1793 support DT_MIPS_XHASH? */
1794 #define DT_MIPS_XHASH 0x70000036
1795
1796 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1797
1798 bool
1799 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1800 Elf_Internal_Phdr *phdrs, size_t phnum,
1801 bfd_size_type filesize)
1802 {
1803 bfd_byte *extdyn, *extdynend;
1804 size_t extdynsize;
1805 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1806 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1807 Elf_Internal_Sym *);
1808 Elf_Internal_Dyn dyn;
1809 bfd_vma dt_hash = 0;
1810 bfd_vma dt_gnu_hash = 0;
1811 bfd_vma dt_mips_xhash = 0;
1812 bfd_vma dt_strtab = 0;
1813 bfd_vma dt_symtab = 0;
1814 size_t dt_strsz = 0;
1815 bfd_vma dt_versym = 0;
1816 bfd_vma dt_verdef = 0;
1817 bfd_vma dt_verneed = 0;
1818 bfd_byte *dynbuf = NULL;
1819 char *strbuf = NULL;
1820 bfd_vma *gnubuckets = NULL;
1821 bfd_vma *gnuchains = NULL;
1822 bfd_vma *mipsxlat = NULL;
1823 file_ptr saved_filepos, filepos;
1824 bool res = false;
1825 size_t amt;
1826 bfd_byte *esymbuf = NULL, *esym;
1827 bfd_size_type symcount;
1828 Elf_Internal_Sym *isymbuf = NULL;
1829 Elf_Internal_Sym *isym, *isymend;
1830 bfd_byte *versym = NULL;
1831 bfd_byte *verdef = NULL;
1832 bfd_byte *verneed = NULL;
1833 size_t verdef_size = 0;
1834 size_t verneed_size = 0;
1835 size_t extsym_size;
1836 const struct elf_backend_data *bed;
1837 void *dynbuf_addr = NULL;
1838 void *esymbuf_addr = NULL;
1839 size_t dynbuf_size = 0;
1840 size_t esymbuf_size = 0;
1841
1842 /* Return TRUE if symbol table is bad. */
1843 if (elf_bad_symtab (abfd))
1844 return true;
1845
1846 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
1847 if (elf_tdata (abfd)->dt_strtab != NULL)
1848 return true;
1849
1850 bed = get_elf_backend_data (abfd);
1851
1852 /* Save file position for elf_object_p. */
1853 saved_filepos = bfd_tell (abfd);
1854
1855 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1856 goto error_return;
1857
1858 dynbuf_size = phdr->p_filesz;
1859 dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1860 if (dynbuf == NULL)
1861 goto error_return;
1862
1863 extsym_size = bed->s->sizeof_sym;
1864 extdynsize = bed->s->sizeof_dyn;
1865 swap_dyn_in = bed->s->swap_dyn_in;
1866
1867 extdyn = dynbuf;
1868 if (phdr->p_filesz < extdynsize)
1869 goto error_return;
1870 extdynend = extdyn + phdr->p_filesz;
1871 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1872 {
1873 swap_dyn_in (abfd, extdyn, &dyn);
1874
1875 if (dyn.d_tag == DT_NULL)
1876 break;
1877
1878 switch (dyn.d_tag)
1879 {
1880 case DT_HASH:
1881 dt_hash = dyn.d_un.d_val;
1882 break;
1883 case DT_GNU_HASH:
1884 if (bed->elf_machine_code != EM_MIPS
1885 && bed->elf_machine_code != EM_MIPS_RS3_LE)
1886 dt_gnu_hash = dyn.d_un.d_val;
1887 break;
1888 case DT_STRTAB:
1889 dt_strtab = dyn.d_un.d_val;
1890 break;
1891 case DT_SYMTAB:
1892 dt_symtab = dyn.d_un.d_val;
1893 break;
1894 case DT_STRSZ:
1895 dt_strsz = dyn.d_un.d_val;
1896 break;
1897 case DT_SYMENT:
1898 if (dyn.d_un.d_val != extsym_size)
1899 goto error_return;
1900 break;
1901 case DT_VERSYM:
1902 dt_versym = dyn.d_un.d_val;
1903 break;
1904 case DT_VERDEF:
1905 dt_verdef = dyn.d_un.d_val;
1906 break;
1907 case DT_VERNEED:
1908 dt_verneed = dyn.d_un.d_val;
1909 break;
1910 default:
1911 if (dyn.d_tag == DT_MIPS_XHASH
1912 && (bed->elf_machine_code == EM_MIPS
1913 || bed->elf_machine_code == EM_MIPS_RS3_LE))
1914 {
1915 dt_gnu_hash = dyn.d_un.d_val;
1916 dt_mips_xhash = dyn.d_un.d_val;
1917 }
1918 break;
1919 }
1920 }
1921
1922 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1923 segment. */
1924 if ((!dt_hash && !dt_gnu_hash)
1925 || !dt_strtab
1926 || !dt_symtab
1927 || !dt_strsz)
1928 goto error_return;
1929
1930 /* Get dynamic string table. */
1931 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1932 if (filepos == (file_ptr) -1
1933 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1934 goto error_return;
1935
1936 /* Dynamic string table must be valid until ABFD is closed. */
1937 strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
1938 if (strbuf == NULL)
1939 goto error_return;
1940 if (strbuf[dt_strsz - 1] != 0)
1941 {
1942 /* It is an error if a string table is't terminated. */
1943 _bfd_error_handler
1944 /* xgettext:c-format */
1945 (_("%pB: DT_STRTAB table is corrupt"), abfd);
1946 strbuf[dt_strsz - 1] = 0;
1947 }
1948
1949 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
1950 DT_HASH since it is simpler than DT_GNU_HASH. */
1951 if (dt_hash)
1952 {
1953 unsigned char nb[16];
1954 unsigned int hash_ent_size;
1955
1956 switch (bed->elf_machine_code)
1957 {
1958 case EM_ALPHA:
1959 case EM_S390:
1960 case EM_S390_OLD:
1961 if (bed->s->elfclass == ELFCLASS64)
1962 {
1963 hash_ent_size = 8;
1964 break;
1965 }
1966 /* FALLTHROUGH */
1967 default:
1968 hash_ent_size = 4;
1969 break;
1970 }
1971
1972 filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
1973 NULL);
1974 if (filepos == (file_ptr) -1
1975 || bfd_seek (abfd, filepos, SEEK_SET) != 0
1976 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
1977 goto error_return;
1978
1979 /* The number of dynamic symbol table entries equals the number
1980 of chains. */
1981 if (hash_ent_size == 8)
1982 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
1983 else
1984 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
1985 }
1986 else
1987 {
1988 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
1989 bindings are in hash table. Since in dynamic symbol table,
1990 all symbols with STB_LOCAL binding are placed before symbols
1991 with other bindings and all undefined symbols are placed
1992 before defined ones, the highest symbol index in DT_GNU_HASH
1993 is the highest dynamic symbol table index. */
1994 unsigned char nb[16];
1995 bfd_vma ngnubuckets;
1996 bfd_vma gnusymidx;
1997 size_t i, ngnuchains;
1998 bfd_vma maxchain = 0xffffffff, bitmaskwords;
1999 bfd_vma buckets_vma;
2000
2001 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2002 sizeof (nb), NULL);
2003 if (filepos == (file_ptr) -1
2004 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2005 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2006 goto error_return;
2007
2008 ngnubuckets = bfd_get_32 (abfd, nb);
2009 gnusymidx = bfd_get_32 (abfd, nb + 4);
2010 bitmaskwords = bfd_get_32 (abfd, nb + 8);
2011 buckets_vma = dt_gnu_hash + 16;
2012 if (bed->s->elfclass == ELFCLASS32)
2013 buckets_vma += bitmaskwords * 4;
2014 else
2015 buckets_vma += bitmaskwords * 8;
2016 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2017 if (filepos == (file_ptr) -1
2018 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2019 goto error_return;
2020
2021 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2022 if (gnubuckets == NULL)
2023 goto error_return;
2024
2025 for (i = 0; i < ngnubuckets; i++)
2026 if (gnubuckets[i] != 0)
2027 {
2028 if (gnubuckets[i] < gnusymidx)
2029 goto error_return;
2030
2031 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2032 maxchain = gnubuckets[i];
2033 }
2034
2035 if (maxchain == 0xffffffff)
2036 {
2037 symcount = 0;
2038 goto empty_gnu_hash;
2039 }
2040
2041 maxchain -= gnusymidx;
2042 filepos = offset_from_vma (phdrs, phnum,
2043 buckets_vma + 4 * (ngnubuckets + maxchain),
2044 4, NULL);
2045 if (filepos == (file_ptr) -1
2046 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2047 goto error_return;
2048
2049 do
2050 {
2051 if (bfd_read (nb, 4, abfd) != 4)
2052 goto error_return;
2053 ++maxchain;
2054 if (maxchain == 0)
2055 goto error_return;
2056 }
2057 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2058
2059 filepos = offset_from_vma (phdrs, phnum,
2060 buckets_vma + 4 * ngnubuckets,
2061 4, NULL);
2062 if (filepos == (file_ptr) -1
2063 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2064 goto error_return;
2065
2066 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2067 if (gnuchains == NULL)
2068 goto error_return;
2069 ngnuchains = maxchain;
2070
2071 if (dt_mips_xhash)
2072 {
2073 filepos = offset_from_vma (phdrs, phnum,
2074 buckets_vma + 4 * (ngnubuckets + maxchain),
2075 4, NULL);
2076 if (filepos == (file_ptr) -1
2077 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2078 goto error_return;
2079
2080 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2081 if (mipsxlat == NULL)
2082 goto error_return;
2083 }
2084
2085 symcount = 0;
2086 for (i = 0; i < ngnubuckets; ++i)
2087 if (gnubuckets[i] != 0)
2088 {
2089 bfd_vma si = gnubuckets[i];
2090 bfd_vma off = si - gnusymidx;
2091 do
2092 {
2093 if (mipsxlat)
2094 {
2095 if (mipsxlat[off] >= symcount)
2096 symcount = mipsxlat[off] + 1;
2097 }
2098 else
2099 {
2100 if (si >= symcount)
2101 symcount = si + 1;
2102 }
2103 si++;
2104 }
2105 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2106 }
2107 }
2108
2109 /* Swap in dynamic symbol table. */
2110 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2111 {
2112 bfd_set_error (bfd_error_file_too_big);
2113 goto error_return;
2114 }
2115
2116 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2117 if (filepos == (file_ptr) -1
2118 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2119 goto error_return;
2120 esymbuf_size = amt;
2121 esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2122 &esymbuf_addr, &esymbuf_size);
2123 if (esymbuf == NULL)
2124 goto error_return;
2125
2126 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2127 {
2128 bfd_set_error (bfd_error_file_too_big);
2129 goto error_return;
2130 }
2131
2132 /* Dynamic symbol table must be valid until ABFD is closed. */
2133 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2134 if (isymbuf == NULL)
2135 goto error_return;
2136
2137 swap_symbol_in = bed->s->swap_symbol_in;
2138
2139 /* Convert the symbols to internal form. */
2140 isymend = isymbuf + symcount;
2141 for (esym = esymbuf, isym = isymbuf;
2142 isym < isymend;
2143 esym += extsym_size, isym++)
2144 if (!swap_symbol_in (abfd, esym, NULL, isym)
2145 || isym->st_name >= dt_strsz)
2146 {
2147 bfd_set_error (bfd_error_invalid_operation);
2148 goto error_return;
2149 }
2150
2151 if (dt_versym)
2152 {
2153 /* Swap in DT_VERSYM. */
2154 if (_bfd_mul_overflow (symcount, 2, &amt))
2155 {
2156 bfd_set_error (bfd_error_file_too_big);
2157 goto error_return;
2158 }
2159
2160 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2161 if (filepos == (file_ptr) -1
2162 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2163 goto error_return;
2164
2165 /* DT_VERSYM info must be valid until ABFD is closed. */
2166 versym = _bfd_mmap_persistent (abfd, amt);
2167
2168 if (dt_verdef)
2169 {
2170 /* Read in DT_VERDEF. */
2171 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2172 0, &verdef_size);
2173 if (filepos == (file_ptr) -1
2174 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2175 goto error_return;
2176
2177 /* DT_VERDEF info must be valid until ABFD is closed. */
2178 verdef = _bfd_mmap_persistent (abfd, verdef_size);
2179 }
2180
2181 if (dt_verneed)
2182 {
2183 /* Read in DT_VERNEED. */
2184 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2185 0, &verneed_size);
2186 if (filepos == (file_ptr) -1
2187 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2188 goto error_return;
2189
2190 /* DT_VERNEED info must be valid until ABFD is closed. */
2191 verneed = _bfd_mmap_persistent (abfd, verneed_size);
2192 }
2193 }
2194
2195 empty_gnu_hash:
2196 elf_tdata (abfd)->dt_strtab = strbuf;
2197 elf_tdata (abfd)->dt_strsz = dt_strsz;
2198 elf_tdata (abfd)->dt_symtab = isymbuf;
2199 elf_tdata (abfd)->dt_symtab_count = symcount;
2200 elf_tdata (abfd)->dt_versym = versym;
2201 elf_tdata (abfd)->dt_verdef = verdef;
2202 elf_tdata (abfd)->dt_verneed = verneed;
2203 elf_tdata (abfd)->dt_verdef_count
2204 = verdef_size / sizeof (Elf_External_Verdef);
2205 elf_tdata (abfd)->dt_verneed_count
2206 = verneed_size / sizeof (Elf_External_Verneed);
2207
2208 res = true;
2209
2210 error_return:
2211 /* Restore file position for elf_object_p. */
2212 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2213 res = false;
2214 _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2215 _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2216 free (gnubuckets);
2217 free (gnuchains);
2218 free (mipsxlat);
2219 return res;
2220 }
2221
2222 /* Reconstruct section from dynamic symbol. */
2223
2224 asection *
2225 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2226 Elf_Internal_Sym *isym)
2227 {
2228 asection *sec;
2229 flagword flags;
2230
2231 if (!elf_use_dt_symtab_p (abfd))
2232 return NULL;
2233
2234 flags = SEC_ALLOC | SEC_LOAD;
2235 switch (ELF_ST_TYPE (isym->st_info))
2236 {
2237 case STT_FUNC:
2238 case STT_GNU_IFUNC:
2239 sec = bfd_get_section_by_name (abfd, ".text");
2240 if (sec == NULL)
2241 sec = bfd_make_section_with_flags (abfd,
2242 ".text",
2243 flags | SEC_CODE);
2244 break;
2245 case STT_COMMON:
2246 sec = bfd_com_section_ptr;
2247 break;
2248 case STT_OBJECT:
2249 sec = bfd_get_section_by_name (abfd, ".data");
2250 if (sec == NULL)
2251 sec = bfd_make_section_with_flags (abfd,
2252 ".data",
2253 flags | SEC_DATA);
2254 break;
2255 case STT_TLS:
2256 sec = bfd_get_section_by_name (abfd, ".tdata");
2257 if (sec == NULL)
2258 sec = bfd_make_section_with_flags (abfd,
2259 ".tdata",
2260 (flags
2261 | SEC_DATA
2262 | SEC_THREAD_LOCAL));
2263 break;
2264 default:
2265 sec = bfd_abs_section_ptr;
2266 break;
2267 }
2268
2269 return sec;
2270 }
2271
2272 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2273 and return symbol version for symbol version itself. */
2274
2275 const char *
2276 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2277 bool base_p,
2278 bool *hidden)
2279 {
2280 const char *version_string = NULL;
2281 if ((elf_dynversym (abfd) != 0
2282 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2283 || (elf_tdata (abfd)->dt_versym != NULL
2284 && (elf_tdata (abfd)->dt_verdef != NULL
2285 || elf_tdata (abfd)->dt_verneed != NULL)))
2286 {
2287 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2288
2289 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2290 vernum &= VERSYM_VERSION;
2291
2292 if (vernum == 0)
2293 version_string = "";
2294 else if (vernum == 1
2295 && (vernum > elf_tdata (abfd)->cverdefs
2296 || (elf_tdata (abfd)->verdef[0].vd_flags
2297 == VER_FLG_BASE)))
2298 version_string = base_p ? "Base" : "";
2299 else if (vernum <= elf_tdata (abfd)->cverdefs)
2300 {
2301 const char *nodename
2302 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2303 version_string = "";
2304 if (base_p
2305 || nodename == NULL
2306 || symbol->name == NULL
2307 || strcmp (symbol->name, nodename) != 0)
2308 version_string = nodename;
2309 }
2310 else
2311 {
2312 Elf_Internal_Verneed *t;
2313
2314 version_string = _("<corrupt>");
2315 for (t = elf_tdata (abfd)->verref;
2316 t != NULL;
2317 t = t->vn_nextref)
2318 {
2319 Elf_Internal_Vernaux *a;
2320
2321 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2322 {
2323 if (a->vna_other == vernum)
2324 {
2325 *hidden = true;
2326 version_string = a->vna_nodename;
2327 break;
2328 }
2329 }
2330 }
2331 }
2332 }
2333 return version_string;
2334 }
2335
2336 /* Display ELF-specific fields of a symbol. */
2337
2338 void
2339 bfd_elf_print_symbol (bfd *abfd,
2340 void *filep,
2341 asymbol *symbol,
2342 bfd_print_symbol_type how)
2343 {
2344 FILE *file = (FILE *) filep;
2345 const char *symname = (symbol->name != bfd_symbol_error_name
2346 ? symbol->name : _("<corrupt>"));
2347
2348 switch (how)
2349 {
2350 case bfd_print_symbol_name:
2351 fprintf (file, "%s", symname);
2352 break;
2353 case bfd_print_symbol_more:
2354 fprintf (file, "elf ");
2355 bfd_fprintf_vma (abfd, file, symbol->value);
2356 fprintf (file, " %x", symbol->flags);
2357 break;
2358 case bfd_print_symbol_all:
2359 {
2360 const char *section_name;
2361 const char *name = NULL;
2362 const struct elf_backend_data *bed;
2363 unsigned char st_other;
2364 bfd_vma val;
2365 const char *version_string;
2366 bool hidden;
2367
2368 section_name = symbol->section ? symbol->section->name : "(*none*)";
2369
2370 bed = get_elf_backend_data (abfd);
2371 if (bed->elf_backend_print_symbol_all)
2372 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2373
2374 if (name != NULL)
2375 symname = name;
2376 else
2377 bfd_print_symbol_vandf (abfd, file, symbol);
2378
2379 fprintf (file, " %s\t", section_name);
2380 /* Print the "other" value for a symbol. For common symbols,
2381 we've already printed the size; now print the alignment.
2382 For other symbols, we have no specified alignment, and
2383 we've printed the address; now print the size. */
2384 if (symbol->section && bfd_is_com_section (symbol->section))
2385 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2386 else
2387 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2388 bfd_fprintf_vma (abfd, file, val);
2389
2390 /* If we have version information, print it. */
2391 version_string = _bfd_elf_get_symbol_version_string (abfd,
2392 symbol,
2393 true,
2394 &hidden);
2395 if (version_string)
2396 {
2397 if (!hidden)
2398 fprintf (file, " %-11s", version_string);
2399 else
2400 {
2401 int i;
2402
2403 fprintf (file, " (%s)", version_string);
2404 for (i = 10 - strlen (version_string); i > 0; --i)
2405 putc (' ', file);
2406 }
2407 }
2408
2409 /* If the st_other field is not zero, print it. */
2410 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2411
2412 switch (st_other)
2413 {
2414 case 0: break;
2415 case STV_INTERNAL: fprintf (file, " .internal"); break;
2416 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2417 case STV_PROTECTED: fprintf (file, " .protected"); break;
2418 default:
2419 /* Some other non-defined flags are also present, so print
2420 everything hex. */
2421 fprintf (file, " 0x%02x", (unsigned int) st_other);
2422 }
2423
2424 fprintf (file, " %s", symname);
2425 }
2426 break;
2427 }
2428 }
2429
2430 /* ELF .o/exec file reading */
2432
2433 /* Create a new bfd section from an ELF section header. */
2434
2435 bool
2436 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2437 {
2438 Elf_Internal_Shdr *hdr;
2439 Elf_Internal_Ehdr *ehdr;
2440 const struct elf_backend_data *bed;
2441 const char *name;
2442 bool ret = true;
2443
2444 if (shindex >= elf_numsections (abfd))
2445 return false;
2446
2447 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2448 sh_link or sh_info. Detect this here, by refusing to load a
2449 section that we are already in the process of loading. */
2450 if (elf_tdata (abfd)->being_created[shindex])
2451 {
2452 _bfd_error_handler
2453 (_("%pB: warning: loop in section dependencies detected"), abfd);
2454 return false;
2455 }
2456 elf_tdata (abfd)->being_created[shindex] = true;
2457
2458 hdr = elf_elfsections (abfd)[shindex];
2459 ehdr = elf_elfheader (abfd);
2460 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2461 hdr->sh_name);
2462 if (name == NULL)
2463 goto fail;
2464
2465 bed = get_elf_backend_data (abfd);
2466 switch (hdr->sh_type)
2467 {
2468 case SHT_NULL:
2469 /* Inactive section. Throw it away. */
2470 goto success;
2471
2472 case SHT_PROGBITS: /* Normal section with contents. */
2473 case SHT_NOBITS: /* .bss section. */
2474 case SHT_HASH: /* .hash section. */
2475 case SHT_NOTE: /* .note section. */
2476 case SHT_INIT_ARRAY: /* .init_array section. */
2477 case SHT_FINI_ARRAY: /* .fini_array section. */
2478 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2479 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2480 case SHT_GNU_HASH: /* .gnu.hash section. */
2481 case SHT_GNU_SFRAME: /* .sframe section. */
2482 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2483 goto success;
2484
2485 case SHT_DYNAMIC: /* Dynamic linking information. */
2486 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2487 goto fail;
2488
2489 if (hdr->sh_link > elf_numsections (abfd))
2490 {
2491 /* PR 10478: Accept Solaris binaries with a sh_link field
2492 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2493 switch (bfd_get_arch (abfd))
2494 {
2495 case bfd_arch_i386:
2496 case bfd_arch_sparc:
2497 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2498 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2499 break;
2500 /* Otherwise fall through. */
2501 default:
2502 goto fail;
2503 }
2504 }
2505 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2506 goto fail;
2507 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2508 {
2509 Elf_Internal_Shdr *dynsymhdr;
2510
2511 /* The shared libraries distributed with hpux11 have a bogus
2512 sh_link field for the ".dynamic" section. Find the
2513 string table for the ".dynsym" section instead. */
2514 if (elf_dynsymtab (abfd) != 0)
2515 {
2516 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2517 hdr->sh_link = dynsymhdr->sh_link;
2518 }
2519 else
2520 {
2521 unsigned int i, num_sec;
2522
2523 num_sec = elf_numsections (abfd);
2524 for (i = 1; i < num_sec; i++)
2525 {
2526 dynsymhdr = elf_elfsections (abfd)[i];
2527 if (dynsymhdr->sh_type == SHT_DYNSYM)
2528 {
2529 hdr->sh_link = dynsymhdr->sh_link;
2530 break;
2531 }
2532 }
2533 }
2534 }
2535 goto success;
2536
2537 case SHT_SYMTAB: /* A symbol table. */
2538 if (elf_onesymtab (abfd) == shindex)
2539 goto success;
2540
2541 if (hdr->sh_entsize != bed->s->sizeof_sym)
2542 goto fail;
2543
2544 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2545 {
2546 if (hdr->sh_size != 0)
2547 goto fail;
2548 /* Some assemblers erroneously set sh_info to one with a
2549 zero sh_size. ld sees this as a global symbol count
2550 of (unsigned) -1. Fix it here. */
2551 hdr->sh_info = 0;
2552 goto success;
2553 }
2554
2555 /* PR 18854: A binary might contain more than one symbol table.
2556 Unusual, but possible. Warn, but continue. */
2557 if (elf_onesymtab (abfd) != 0)
2558 {
2559 _bfd_error_handler
2560 /* xgettext:c-format */
2561 (_("%pB: warning: multiple symbol tables detected"
2562 " - ignoring the table in section %u"),
2563 abfd, shindex);
2564 goto success;
2565 }
2566 elf_onesymtab (abfd) = shindex;
2567 elf_symtab_hdr (abfd) = *hdr;
2568 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2569 abfd->flags |= HAS_SYMS;
2570
2571 /* Sometimes a shared object will map in the symbol table. If
2572 SHF_ALLOC is set, and this is a shared object, then we also
2573 treat this section as a BFD section. We can not base the
2574 decision purely on SHF_ALLOC, because that flag is sometimes
2575 set in a relocatable object file, which would confuse the
2576 linker. */
2577 if ((hdr->sh_flags & SHF_ALLOC) != 0
2578 && (abfd->flags & DYNAMIC) != 0
2579 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2580 shindex))
2581 goto fail;
2582
2583 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2584 can't read symbols without that section loaded as well. It
2585 is most likely specified by the next section header. */
2586 {
2587 elf_section_list * entry;
2588 unsigned int i, num_sec;
2589
2590 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2591 if (entry->hdr.sh_link == shindex)
2592 goto success;
2593
2594 num_sec = elf_numsections (abfd);
2595 for (i = shindex + 1; i < num_sec; i++)
2596 {
2597 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2598
2599 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2600 && hdr2->sh_link == shindex)
2601 break;
2602 }
2603
2604 if (i == num_sec)
2605 for (i = 1; i < shindex; i++)
2606 {
2607 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2608
2609 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2610 && hdr2->sh_link == shindex)
2611 break;
2612 }
2613
2614 if (i != shindex)
2615 ret = bfd_section_from_shdr (abfd, i);
2616 /* else FIXME: we have failed to find the symbol table.
2617 Should we issue an error? */
2618 goto success;
2619 }
2620
2621 case SHT_DYNSYM: /* A dynamic symbol table. */
2622 if (elf_dynsymtab (abfd) == shindex)
2623 goto success;
2624
2625 if (hdr->sh_entsize != bed->s->sizeof_sym)
2626 goto fail;
2627
2628 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2629 {
2630 if (hdr->sh_size != 0)
2631 goto fail;
2632
2633 /* Some linkers erroneously set sh_info to one with a
2634 zero sh_size. ld sees this as a global symbol count
2635 of (unsigned) -1. Fix it here. */
2636 hdr->sh_info = 0;
2637 goto success;
2638 }
2639
2640 /* PR 18854: A binary might contain more than one dynamic symbol table.
2641 Unusual, but possible. Warn, but continue. */
2642 if (elf_dynsymtab (abfd) != 0)
2643 {
2644 _bfd_error_handler
2645 /* xgettext:c-format */
2646 (_("%pB: warning: multiple dynamic symbol tables detected"
2647 " - ignoring the table in section %u"),
2648 abfd, shindex);
2649 goto success;
2650 }
2651 elf_dynsymtab (abfd) = shindex;
2652 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2653 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2654 abfd->flags |= HAS_SYMS;
2655
2656 /* Besides being a symbol table, we also treat this as a regular
2657 section, so that objcopy can handle it. */
2658 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2659 goto success;
2660
2661 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2662 {
2663 elf_section_list * entry;
2664
2665 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2666 if (entry->ndx == shindex)
2667 goto success;
2668
2669 entry = bfd_alloc (abfd, sizeof (*entry));
2670 if (entry == NULL)
2671 goto fail;
2672 entry->ndx = shindex;
2673 entry->hdr = * hdr;
2674 entry->next = elf_symtab_shndx_list (abfd);
2675 elf_symtab_shndx_list (abfd) = entry;
2676 elf_elfsections (abfd)[shindex] = & entry->hdr;
2677 goto success;
2678 }
2679
2680 case SHT_STRTAB: /* A string table. */
2681 if (hdr->bfd_section != NULL)
2682 goto success;
2683
2684 if (ehdr->e_shstrndx == shindex)
2685 {
2686 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2687 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2688 goto success;
2689 }
2690
2691 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2692 {
2693 symtab_strtab:
2694 elf_tdata (abfd)->strtab_hdr = *hdr;
2695 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2696 goto success;
2697 }
2698
2699 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2700 {
2701 dynsymtab_strtab:
2702 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2703 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2704 elf_elfsections (abfd)[shindex] = hdr;
2705 /* We also treat this as a regular section, so that objcopy
2706 can handle it. */
2707 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2708 shindex);
2709 goto success;
2710 }
2711
2712 /* If the string table isn't one of the above, then treat it as a
2713 regular section. We need to scan all the headers to be sure,
2714 just in case this strtab section appeared before the above. */
2715 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2716 {
2717 unsigned int i, num_sec;
2718
2719 num_sec = elf_numsections (abfd);
2720 for (i = 1; i < num_sec; i++)
2721 {
2722 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2723 if (hdr2->sh_link == shindex)
2724 {
2725 /* Prevent endless recursion on broken objects. */
2726 if (i == shindex)
2727 goto fail;
2728 if (! bfd_section_from_shdr (abfd, i))
2729 goto fail;
2730 if (elf_onesymtab (abfd) == i)
2731 goto symtab_strtab;
2732 if (elf_dynsymtab (abfd) == i)
2733 goto dynsymtab_strtab;
2734 }
2735 }
2736 }
2737 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2738 goto success;
2739
2740 case SHT_REL:
2741 case SHT_RELA:
2742 case SHT_RELR:
2743 /* *These* do a lot of work -- but build no sections! */
2744 {
2745 asection *target_sect;
2746 Elf_Internal_Shdr *hdr2, **p_hdr;
2747 unsigned int num_sec = elf_numsections (abfd);
2748 struct bfd_elf_section_data *esdt;
2749 bfd_size_type size;
2750
2751 if (hdr->sh_type == SHT_REL)
2752 size = bed->s->sizeof_rel;
2753 else if (hdr->sh_type == SHT_RELA)
2754 size = bed->s->sizeof_rela;
2755 else
2756 size = bed->s->arch_size / 8;
2757 if (hdr->sh_entsize != size)
2758 goto fail;
2759
2760 /* Check for a bogus link to avoid crashing. */
2761 if (hdr->sh_link >= num_sec)
2762 {
2763 _bfd_error_handler
2764 /* xgettext:c-format */
2765 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2766 abfd, hdr->sh_link, name, shindex);
2767 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2768 goto success;
2769 }
2770
2771 /* Get the symbol table. */
2772 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2773 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2774 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2775 goto fail;
2776
2777 /* If this is an alloc section in an executable or shared
2778 library, or the reloc section does not use the main symbol
2779 table we don't treat it as a reloc section. BFD can't
2780 adequately represent such a section, so at least for now,
2781 we don't try. We just present it as a normal section. We
2782 also can't use it as a reloc section if it points to the
2783 null section, an invalid section, another reloc section, or
2784 its sh_link points to the null section. */
2785 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2786 && (hdr->sh_flags & SHF_ALLOC) != 0)
2787 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2788 || hdr->sh_type == SHT_RELR
2789 || hdr->sh_link == SHN_UNDEF
2790 || hdr->sh_link != elf_onesymtab (abfd)
2791 || hdr->sh_info == SHN_UNDEF
2792 || hdr->sh_info >= num_sec
2793 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2794 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2795 {
2796 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2797 goto success;
2798 }
2799
2800 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2801 goto fail;
2802
2803 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2804 if (target_sect == NULL)
2805 goto fail;
2806
2807 esdt = elf_section_data (target_sect);
2808 if (hdr->sh_type == SHT_RELA)
2809 p_hdr = &esdt->rela.hdr;
2810 else
2811 p_hdr = &esdt->rel.hdr;
2812
2813 /* PR 17512: file: 0b4f81b7.
2814 Also see PR 24456, for a file which deliberately has two reloc
2815 sections. */
2816 if (*p_hdr != NULL)
2817 {
2818 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2819 {
2820 _bfd_error_handler
2821 /* xgettext:c-format */
2822 (_("%pB: warning: secondary relocation section '%s' "
2823 "for section %pA found - ignoring"),
2824 abfd, name, target_sect);
2825 }
2826 else
2827 esdt->has_secondary_relocs = true;
2828 goto success;
2829 }
2830
2831 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2832 if (hdr2 == NULL)
2833 goto fail;
2834 *hdr2 = *hdr;
2835 *p_hdr = hdr2;
2836 elf_elfsections (abfd)[shindex] = hdr2;
2837 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2838 * bed->s->int_rels_per_ext_rel);
2839 target_sect->flags |= SEC_RELOC;
2840 target_sect->relocation = NULL;
2841 target_sect->rel_filepos = hdr->sh_offset;
2842 /* In the section to which the relocations apply, mark whether
2843 its relocations are of the REL or RELA variety. */
2844 if (hdr->sh_size != 0)
2845 {
2846 if (hdr->sh_type == SHT_RELA)
2847 target_sect->use_rela_p = 1;
2848 }
2849 abfd->flags |= HAS_RELOC;
2850 goto success;
2851 }
2852
2853 case SHT_GNU_verdef:
2854 if (hdr->sh_info != 0)
2855 elf_dynverdef (abfd) = shindex;
2856 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2857 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2858 goto success;
2859
2860 case SHT_GNU_versym:
2861 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2862 goto fail;
2863
2864 elf_dynversym (abfd) = shindex;
2865 elf_tdata (abfd)->dynversym_hdr = *hdr;
2866 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2867 goto success;
2868
2869 case SHT_GNU_verneed:
2870 if (hdr->sh_info != 0)
2871 elf_dynverref (abfd) = shindex;
2872 elf_tdata (abfd)->dynverref_hdr = *hdr;
2873 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2874 goto success;
2875
2876 case SHT_SHLIB:
2877 goto success;
2878
2879 case SHT_GROUP:
2880 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2881 goto fail;
2882
2883 goto success;
2884
2885 default:
2886 /* Possibly an attributes section. */
2887 if (get_elf_backend_data (abfd)->target_os != is_solaris
2888 /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES. */
2889 && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2890 || hdr->sh_type == bed->obj_attrs_section_type))
2891 {
2892 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2893 goto fail;
2894 _bfd_elf_parse_attributes (abfd, hdr);
2895 goto success;
2896 }
2897
2898 /* Check for any processor-specific section types. */
2899 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2900 goto success;
2901
2902 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2903 {
2904 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2905 /* FIXME: How to properly handle allocated section reserved
2906 for applications? */
2907 _bfd_error_handler
2908 /* xgettext:c-format */
2909 (_("%pB: unknown type [%#x] section `%s'"),
2910 abfd, hdr->sh_type, name);
2911 else
2912 {
2913 /* Allow sections reserved for applications. */
2914 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2915 goto success;
2916 }
2917 }
2918 else if (hdr->sh_type >= SHT_LOPROC
2919 && hdr->sh_type <= SHT_HIPROC)
2920 /* FIXME: We should handle this section. */
2921 _bfd_error_handler
2922 /* xgettext:c-format */
2923 (_("%pB: unknown type [%#x] section `%s'"),
2924 abfd, hdr->sh_type, name);
2925 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2926 {
2927 /* Unrecognised OS-specific sections. */
2928 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2929 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2930 required to correctly process the section and the file should
2931 be rejected with an error message. */
2932 _bfd_error_handler
2933 /* xgettext:c-format */
2934 (_("%pB: unknown type [%#x] section `%s'"),
2935 abfd, hdr->sh_type, name);
2936 else
2937 {
2938 /* Otherwise it should be processed. */
2939 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2940 goto success;
2941 }
2942 }
2943 else
2944 /* FIXME: We should handle this section. */
2945 _bfd_error_handler
2946 /* xgettext:c-format */
2947 (_("%pB: unknown type [%#x] section `%s'"),
2948 abfd, hdr->sh_type, name);
2949
2950 goto fail;
2951 }
2952
2953 fail:
2954 ret = false;
2955 success:
2956 elf_tdata (abfd)->being_created[shindex] = false;
2957 return ret;
2958 }
2959
2960 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2961
2962 Elf_Internal_Sym *
2963 bfd_sym_from_r_symndx (struct sym_cache *cache,
2964 bfd *abfd,
2965 unsigned long r_symndx)
2966 {
2967 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2968
2969 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2970 {
2971 Elf_Internal_Shdr *symtab_hdr;
2972 unsigned char esym[sizeof (Elf64_External_Sym)];
2973 Elf_External_Sym_Shndx eshndx;
2974
2975 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2976 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2977 &cache->sym[ent], esym, &eshndx) == NULL)
2978 return NULL;
2979
2980 if (cache->abfd != abfd)
2981 {
2982 memset (cache->indx, -1, sizeof (cache->indx));
2983 cache->abfd = abfd;
2984 }
2985 cache->indx[ent] = r_symndx;
2986 }
2987
2988 return &cache->sym[ent];
2989 }
2990
2991 /* Given an ELF section number, retrieve the corresponding BFD
2992 section. */
2993
2994 asection *
2995 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2996 {
2997 if (sec_index >= elf_numsections (abfd))
2998 return NULL;
2999 return elf_elfsections (abfd)[sec_index]->bfd_section;
3000 }
3001
3002 static const struct bfd_elf_special_section special_sections_b[] =
3003 {
3004 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3005 { NULL, 0, 0, 0, 0 }
3006 };
3007
3008 static const struct bfd_elf_special_section special_sections_c[] =
3009 {
3010 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3011 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
3012 { NULL, 0, 0, 0, 0 }
3013 };
3014
3015 static const struct bfd_elf_special_section special_sections_d[] =
3016 {
3017 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3018 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3019 /* There are more DWARF sections than these, but they needn't be added here
3020 unless you have to cope with broken compilers that don't emit section
3021 attributes or you want to help the user writing assembler. */
3022 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
3023 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
3024 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
3025 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
3026 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3027 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3028 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3029 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3030 { NULL, 0, 0, 0, 0 }
3031 };
3032
3033 static const struct bfd_elf_special_section special_sections_f[] =
3034 {
3035 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3036 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3037 { NULL, 0 , 0, 0, 0 }
3038 };
3039
3040 static const struct bfd_elf_special_section special_sections_g[] =
3041 {
3042 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3043 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3044 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3045 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3046 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3047 { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3048 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3049 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3050 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3051 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3052 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3053 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3054 { NULL, 0, 0, 0, 0 }
3055 };
3056
3057 static const struct bfd_elf_special_section special_sections_h[] =
3058 {
3059 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3060 { NULL, 0, 0, 0, 0 }
3061 };
3062
3063 static const struct bfd_elf_special_section special_sections_i[] =
3064 {
3065 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3066 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3067 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3068 { NULL, 0, 0, 0, 0 }
3069 };
3070
3071 static const struct bfd_elf_special_section special_sections_l[] =
3072 {
3073 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3074 { NULL, 0, 0, 0, 0 }
3075 };
3076
3077 static const struct bfd_elf_special_section special_sections_n[] =
3078 {
3079 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3080 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3081 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3082 { NULL, 0, 0, 0, 0 }
3083 };
3084
3085 static const struct bfd_elf_special_section special_sections_p[] =
3086 {
3087 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3088 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3089 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3090 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3091 { NULL, 0, 0, 0, 0 }
3092 };
3093
3094 static const struct bfd_elf_special_section special_sections_r[] =
3095 {
3096 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3097 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3098 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3099 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3100 /* .relro_padding is generated by lld. It should not be confused with a
3101 reloc containing section, because otherwise elf_fake_sections() will
3102 set the entsize to 8, which may not be an actual multiple of the
3103 section's size.
3104 Note - this entry must appear before the ".rel" entry below. */
3105 { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3106 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3107 { NULL, 0, 0, 0, 0 }
3108 };
3109
3110 static const struct bfd_elf_special_section special_sections_s[] =
3111 {
3112 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3113 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3114 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3115 /* See struct bfd_elf_special_section declaration for the semantics of
3116 this special case where .prefix_length != strlen (.prefix). */
3117 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3118 { NULL, 0, 0, 0, 0 }
3119 };
3120
3121 static const struct bfd_elf_special_section special_sections_t[] =
3122 {
3123 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3124 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3125 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3126 { NULL, 0, 0, 0, 0 }
3127 };
3128
3129 static const struct bfd_elf_special_section special_sections_z[] =
3130 {
3131 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3132 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3133 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3134 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3135 { NULL, 0, 0, 0, 0 }
3136 };
3137
3138 static const struct bfd_elf_special_section * const special_sections[] =
3139 {
3140 special_sections_b, /* 'b' */
3141 special_sections_c, /* 'c' */
3142 special_sections_d, /* 'd' */
3143 NULL, /* 'e' */
3144 special_sections_f, /* 'f' */
3145 special_sections_g, /* 'g' */
3146 special_sections_h, /* 'h' */
3147 special_sections_i, /* 'i' */
3148 NULL, /* 'j' */
3149 NULL, /* 'k' */
3150 special_sections_l, /* 'l' */
3151 NULL, /* 'm' */
3152 special_sections_n, /* 'n' */
3153 NULL, /* 'o' */
3154 special_sections_p, /* 'p' */
3155 NULL, /* 'q' */
3156 special_sections_r, /* 'r' */
3157 special_sections_s, /* 's' */
3158 special_sections_t, /* 't' */
3159 NULL, /* 'u' */
3160 NULL, /* 'v' */
3161 NULL, /* 'w' */
3162 NULL, /* 'x' */
3163 NULL, /* 'y' */
3164 special_sections_z /* 'z' */
3165 };
3166
3167 const struct bfd_elf_special_section *
3168 _bfd_elf_get_special_section (const char *name,
3169 const struct bfd_elf_special_section *spec,
3170 unsigned int rela)
3171 {
3172 int i;
3173 int len;
3174
3175 len = strlen (name);
3176
3177 for (i = 0; spec[i].prefix != NULL; i++)
3178 {
3179 int suffix_len;
3180 int prefix_len = spec[i].prefix_length;
3181
3182 if (len < prefix_len)
3183 continue;
3184 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3185 continue;
3186
3187 suffix_len = spec[i].suffix_length;
3188 if (suffix_len <= 0)
3189 {
3190 if (name[prefix_len] != 0)
3191 {
3192 if (suffix_len == 0)
3193 continue;
3194 if (name[prefix_len] != '.'
3195 && (suffix_len == -2
3196 || (rela && spec[i].type == SHT_REL)))
3197 continue;
3198 }
3199 }
3200 else
3201 {
3202 if (len < prefix_len + suffix_len)
3203 continue;
3204 if (memcmp (name + len - suffix_len,
3205 spec[i].prefix + prefix_len,
3206 suffix_len) != 0)
3207 continue;
3208 }
3209 return &spec[i];
3210 }
3211
3212 return NULL;
3213 }
3214
3215 const struct bfd_elf_special_section *
3216 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3217 {
3218 int i;
3219 const struct bfd_elf_special_section *spec;
3220 const struct elf_backend_data *bed;
3221
3222 /* See if this is one of the special sections. */
3223 if (sec->name == NULL)
3224 return NULL;
3225
3226 bed = get_elf_backend_data (abfd);
3227 spec = bed->special_sections;
3228 if (spec)
3229 {
3230 spec = _bfd_elf_get_special_section (sec->name,
3231 bed->special_sections,
3232 sec->use_rela_p);
3233 if (spec != NULL)
3234 return spec;
3235 }
3236
3237 if (sec->name[0] != '.')
3238 return NULL;
3239
3240 i = sec->name[1] - 'b';
3241 if (i < 0 || i > 'z' - 'b')
3242 return NULL;
3243
3244 spec = special_sections[i];
3245
3246 if (spec == NULL)
3247 return NULL;
3248
3249 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3250 }
3251
3252 bool
3253 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3254 {
3255 struct bfd_elf_section_data *sdata;
3256 const struct elf_backend_data *bed;
3257 const struct bfd_elf_special_section *ssect;
3258
3259 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3260 if (sdata == NULL)
3261 {
3262 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3263 sizeof (*sdata));
3264 if (sdata == NULL)
3265 return false;
3266 sec->used_by_bfd = sdata;
3267 }
3268
3269 /* Indicate whether or not this section should use RELA relocations. */
3270 bed = get_elf_backend_data (abfd);
3271 sec->use_rela_p = bed->default_use_rela_p;
3272
3273 /* Set up ELF section type and flags for newly created sections, if
3274 there is an ABI mandated section. */
3275 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3276 if (ssect != NULL)
3277 {
3278 elf_section_type (sec) = ssect->type;
3279 elf_section_flags (sec) = ssect->attr;
3280 }
3281
3282 return _bfd_generic_new_section_hook (abfd, sec);
3283 }
3284
3285 /* Create a new bfd section from an ELF program header.
3286
3287 Since program segments have no names, we generate a synthetic name
3288 of the form segment<NUM>, where NUM is generally the index in the
3289 program header table. For segments that are split (see below) we
3290 generate the names segment<NUM>a and segment<NUM>b.
3291
3292 Note that some program segments may have a file size that is different than
3293 (less than) the memory size. All this means is that at execution the
3294 system must allocate the amount of memory specified by the memory size,
3295 but only initialize it with the first "file size" bytes read from the
3296 file. This would occur for example, with program segments consisting
3297 of combined data+bss.
3298
3299 To handle the above situation, this routine generates TWO bfd sections
3300 for the single program segment. The first has the length specified by
3301 the file size of the segment, and the second has the length specified
3302 by the difference between the two sizes. In effect, the segment is split
3303 into its initialized and uninitialized parts. */
3304
3305 bool
3306 _bfd_elf_make_section_from_phdr (bfd *abfd,
3307 Elf_Internal_Phdr *hdr,
3308 int hdr_index,
3309 const char *type_name)
3310 {
3311 asection *newsect;
3312 char *name;
3313 char namebuf[64];
3314 size_t len;
3315 int split;
3316 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3317
3318 split = ((hdr->p_memsz > 0)
3319 && (hdr->p_filesz > 0)
3320 && (hdr->p_memsz > hdr->p_filesz));
3321
3322 if (hdr->p_filesz > 0)
3323 {
3324 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3325 len = strlen (namebuf) + 1;
3326 name = (char *) bfd_alloc (abfd, len);
3327 if (!name)
3328 return false;
3329 memcpy (name, namebuf, len);
3330 newsect = bfd_make_section (abfd, name);
3331 if (newsect == NULL)
3332 return false;
3333 newsect->vma = hdr->p_vaddr / opb;
3334 newsect->lma = hdr->p_paddr / opb;
3335 newsect->size = hdr->p_filesz;
3336 newsect->filepos = hdr->p_offset;
3337 newsect->flags |= SEC_HAS_CONTENTS;
3338 newsect->alignment_power = bfd_log2 (hdr->p_align);
3339 if (hdr->p_type == PT_LOAD)
3340 {
3341 newsect->flags |= SEC_ALLOC;
3342 newsect->flags |= SEC_LOAD;
3343 if (hdr->p_flags & PF_X)
3344 {
3345 /* FIXME: all we known is that it has execute PERMISSION,
3346 may be data. */
3347 newsect->flags |= SEC_CODE;
3348 }
3349 }
3350 if (!(hdr->p_flags & PF_W))
3351 {
3352 newsect->flags |= SEC_READONLY;
3353 }
3354 }
3355
3356 if (hdr->p_memsz > hdr->p_filesz)
3357 {
3358 bfd_vma align;
3359
3360 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3361 len = strlen (namebuf) + 1;
3362 name = (char *) bfd_alloc (abfd, len);
3363 if (!name)
3364 return false;
3365 memcpy (name, namebuf, len);
3366 newsect = bfd_make_section (abfd, name);
3367 if (newsect == NULL)
3368 return false;
3369 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3370 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3371 newsect->size = hdr->p_memsz - hdr->p_filesz;
3372 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3373 align = newsect->vma & -newsect->vma;
3374 if (align == 0 || align > hdr->p_align)
3375 align = hdr->p_align;
3376 newsect->alignment_power = bfd_log2 (align);
3377 if (hdr->p_type == PT_LOAD)
3378 {
3379 newsect->flags |= SEC_ALLOC;
3380 if (hdr->p_flags & PF_X)
3381 newsect->flags |= SEC_CODE;
3382 }
3383 if (!(hdr->p_flags & PF_W))
3384 newsect->flags |= SEC_READONLY;
3385 }
3386
3387 return true;
3388 }
3389
3390 static bool
3391 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3392 {
3393 /* The return value is ignored. Build-ids are considered optional. */
3394 if (templ->xvec->flavour == bfd_target_elf_flavour)
3395 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3396 (templ, offset);
3397 return false;
3398 }
3399
3400 bool
3401 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3402 {
3403 const struct elf_backend_data *bed;
3404
3405 switch (hdr->p_type)
3406 {
3407 case PT_NULL:
3408 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3409
3410 case PT_LOAD:
3411 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3412 return false;
3413 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3414 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3415 return true;
3416
3417 case PT_DYNAMIC:
3418 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3419
3420 case PT_INTERP:
3421 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3422
3423 case PT_NOTE:
3424 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3425 return false;
3426 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3427 hdr->p_align))
3428 return false;
3429 return true;
3430
3431 case PT_SHLIB:
3432 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3433
3434 case PT_PHDR:
3435 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3436
3437 case PT_GNU_EH_FRAME:
3438 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3439 "eh_frame_hdr");
3440
3441 case PT_GNU_STACK:
3442 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3443
3444 case PT_GNU_RELRO:
3445 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3446
3447 case PT_GNU_SFRAME:
3448 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3449 "sframe");
3450
3451 default:
3452 /* Check for any processor-specific program segment types. */
3453 bed = get_elf_backend_data (abfd);
3454 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3455 }
3456 }
3457
3458 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3459 REL or RELA. */
3460
3461 Elf_Internal_Shdr *
3462 _bfd_elf_single_rel_hdr (asection *sec)
3463 {
3464 if (elf_section_data (sec)->rel.hdr)
3465 {
3466 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3467 return elf_section_data (sec)->rel.hdr;
3468 }
3469 else
3470 return elf_section_data (sec)->rela.hdr;
3471 }
3472
3473 static bool
3474 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3475 Elf_Internal_Shdr *rel_hdr,
3476 const char *sec_name,
3477 bool use_rela_p)
3478 {
3479 char *name = (char *) bfd_alloc (abfd,
3480 sizeof ".rela" + strlen (sec_name));
3481 if (name == NULL)
3482 return false;
3483
3484 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3485 rel_hdr->sh_name =
3486 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3487 false);
3488 if (rel_hdr->sh_name == (unsigned int) -1)
3489 return false;
3490
3491 return true;
3492 }
3493
3494 /* Allocate and initialize a section-header for a new reloc section,
3495 containing relocations against ASECT. It is stored in RELDATA. If
3496 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3497 relocations. */
3498
3499 static bool
3500 _bfd_elf_init_reloc_shdr (bfd *abfd,
3501 struct bfd_elf_section_reloc_data *reldata,
3502 const char *sec_name,
3503 bool use_rela_p,
3504 bool delay_sh_name_p)
3505 {
3506 Elf_Internal_Shdr *rel_hdr;
3507 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3508
3509 BFD_ASSERT (reldata->hdr == NULL);
3510 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3511 if (rel_hdr == NULL)
3512 return false;
3513 reldata->hdr = rel_hdr;
3514
3515 if (delay_sh_name_p)
3516 rel_hdr->sh_name = (unsigned int) -1;
3517 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3518 use_rela_p))
3519 return false;
3520 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3521 rel_hdr->sh_entsize = (use_rela_p
3522 ? bed->s->sizeof_rela
3523 : bed->s->sizeof_rel);
3524 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3525 rel_hdr->sh_flags = 0;
3526 rel_hdr->sh_addr = 0;
3527 rel_hdr->sh_size = 0;
3528 rel_hdr->sh_offset = 0;
3529
3530 return true;
3531 }
3532
3533 /* Return the default section type based on the passed in section flags. */
3534
3535 int
3536 bfd_elf_get_default_section_type (flagword flags)
3537 {
3538 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3539 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3540 return SHT_NOBITS;
3541 return SHT_PROGBITS;
3542 }
3543
3544 struct fake_section_arg
3545 {
3546 struct bfd_link_info *link_info;
3547 bool failed;
3548 };
3549
3550 /* Set up an ELF internal section header for a section. */
3551
3552 static void
3553 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3554 {
3555 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3556 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3557 struct bfd_elf_section_data *esd = elf_section_data (asect);
3558 Elf_Internal_Shdr *this_hdr;
3559 unsigned int sh_type;
3560 const char *name = asect->name;
3561 bool delay_sh_name_p = false;
3562 bfd_vma mask;
3563
3564 if (arg->failed)
3565 {
3566 /* We already failed; just get out of the bfd_map_over_sections
3567 loop. */
3568 return;
3569 }
3570
3571 this_hdr = &esd->this_hdr;
3572
3573 /* ld: compress DWARF debug sections with names: .debug_*. */
3574 if (arg->link_info
3575 && (abfd->flags & BFD_COMPRESS) != 0
3576 && (asect->flags & SEC_DEBUGGING) != 0
3577 && (asect->flags & SEC_ALLOC) == 0
3578 && (asect->flags & SEC_HAS_CONTENTS) != 0
3579 && name[1] == 'd'
3580 && name[6] == '_')
3581 {
3582 /* If this section will be compressed, delay adding section
3583 name to section name section after it is compressed in
3584 _bfd_elf_assign_file_positions_for_non_load. */
3585 delay_sh_name_p = true;
3586 }
3587
3588 if (delay_sh_name_p)
3589 this_hdr->sh_name = (unsigned int) -1;
3590 else
3591 {
3592 this_hdr->sh_name
3593 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3594 name, false);
3595 if (this_hdr->sh_name == (unsigned int) -1)
3596 {
3597 arg->failed = true;
3598 return;
3599 }
3600 }
3601
3602 /* Don't clear sh_flags. Assembler may set additional bits. */
3603
3604 if ((asect->flags & SEC_ALLOC) != 0
3605 || asect->user_set_vma)
3606 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3607 else
3608 this_hdr->sh_addr = 0;
3609
3610 this_hdr->sh_offset = 0;
3611 this_hdr->sh_size = asect->size;
3612 this_hdr->sh_link = 0;
3613 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3614 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3615 {
3616 _bfd_error_handler
3617 /* xgettext:c-format */
3618 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3619 abfd, asect->alignment_power, asect);
3620 arg->failed = true;
3621 return;
3622 }
3623 /* Set sh_addralign to the highest power of two given by alignment
3624 consistent with the section VMA. Linker scripts can force VMA. */
3625 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3626 this_hdr->sh_addralign = mask & -mask;
3627 /* The sh_entsize and sh_info fields may have been set already by
3628 copy_private_section_data. */
3629
3630 this_hdr->bfd_section = asect;
3631 this_hdr->contents = NULL;
3632
3633 /* If the section type is unspecified, we set it based on
3634 asect->flags. */
3635 if (asect->type != 0)
3636 sh_type = asect->type;
3637 else if ((asect->flags & SEC_GROUP) != 0)
3638 sh_type = SHT_GROUP;
3639 else
3640 sh_type = bfd_elf_get_default_section_type (asect->flags);
3641
3642 if (this_hdr->sh_type == SHT_NULL)
3643 this_hdr->sh_type = sh_type;
3644 else if (this_hdr->sh_type == SHT_NOBITS
3645 && sh_type == SHT_PROGBITS
3646 && (asect->flags & SEC_ALLOC) != 0)
3647 {
3648 /* Warn if we are changing a NOBITS section to PROGBITS, but
3649 allow the link to proceed. This can happen when users link
3650 non-bss input sections to bss output sections, or emit data
3651 to a bss output section via a linker script. */
3652 _bfd_error_handler
3653 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3654 this_hdr->sh_type = sh_type;
3655 }
3656
3657 switch (this_hdr->sh_type)
3658 {
3659 default:
3660 break;
3661
3662 case SHT_STRTAB:
3663 case SHT_NOTE:
3664 case SHT_NOBITS:
3665 case SHT_PROGBITS:
3666 break;
3667
3668 case SHT_INIT_ARRAY:
3669 case SHT_FINI_ARRAY:
3670 case SHT_PREINIT_ARRAY:
3671 this_hdr->sh_entsize = bed->s->arch_size / 8;
3672 break;
3673
3674 case SHT_HASH:
3675 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3676 break;
3677
3678 case SHT_DYNSYM:
3679 this_hdr->sh_entsize = bed->s->sizeof_sym;
3680 break;
3681
3682 case SHT_DYNAMIC:
3683 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3684 break;
3685
3686 case SHT_RELA:
3687 if (get_elf_backend_data (abfd)->may_use_rela_p)
3688 this_hdr->sh_entsize = bed->s->sizeof_rela;
3689 break;
3690
3691 case SHT_REL:
3692 if (get_elf_backend_data (abfd)->may_use_rel_p)
3693 this_hdr->sh_entsize = bed->s->sizeof_rel;
3694 break;
3695
3696 case SHT_GNU_versym:
3697 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3698 break;
3699
3700 case SHT_GNU_verdef:
3701 this_hdr->sh_entsize = 0;
3702 /* objcopy or strip will copy over sh_info, but may not set
3703 cverdefs. The linker will set cverdefs, but sh_info will be
3704 zero. */
3705 if (this_hdr->sh_info == 0)
3706 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3707 else
3708 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3709 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3710 break;
3711
3712 case SHT_GNU_verneed:
3713 this_hdr->sh_entsize = 0;
3714 /* objcopy or strip will copy over sh_info, but may not set
3715 cverrefs. The linker will set cverrefs, but sh_info will be
3716 zero. */
3717 if (this_hdr->sh_info == 0)
3718 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3719 else
3720 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3721 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3722 break;
3723
3724 case SHT_GROUP:
3725 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3726 break;
3727
3728 case SHT_GNU_HASH:
3729 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3730 break;
3731 }
3732
3733 if ((asect->flags & SEC_ALLOC) != 0)
3734 this_hdr->sh_flags |= SHF_ALLOC;
3735 if ((asect->flags & SEC_READONLY) == 0)
3736 this_hdr->sh_flags |= SHF_WRITE;
3737 if ((asect->flags & SEC_CODE) != 0)
3738 this_hdr->sh_flags |= SHF_EXECINSTR;
3739 if ((asect->flags & SEC_MERGE) != 0)
3740 {
3741 this_hdr->sh_flags |= SHF_MERGE;
3742 this_hdr->sh_entsize = asect->entsize;
3743 }
3744 if ((asect->flags & SEC_STRINGS) != 0)
3745 {
3746 this_hdr->sh_flags |= SHF_STRINGS;
3747 this_hdr->sh_entsize = asect->entsize;
3748 }
3749 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3750 this_hdr->sh_flags |= SHF_GROUP;
3751 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3752 {
3753 this_hdr->sh_flags |= SHF_TLS;
3754 if (asect->size == 0
3755 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3756 {
3757 struct bfd_link_order *o = asect->map_tail.link_order;
3758
3759 this_hdr->sh_size = 0;
3760 if (o != NULL)
3761 {
3762 this_hdr->sh_size = o->offset + o->size;
3763 if (this_hdr->sh_size != 0)
3764 this_hdr->sh_type = SHT_NOBITS;
3765 }
3766 }
3767 }
3768 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3769 this_hdr->sh_flags |= SHF_EXCLUDE;
3770
3771 /* If the section has relocs, set up a section header for the
3772 SHT_REL[A] section. If two relocation sections are required for
3773 this section, it is up to the processor-specific back-end to
3774 create the other. */
3775 if ((asect->flags & SEC_RELOC) != 0)
3776 {
3777 /* When doing a relocatable link, create both REL and RELA sections if
3778 needed. */
3779 if (arg->link_info
3780 /* Do the normal setup if we wouldn't create any sections here. */
3781 && esd->rel.count + esd->rela.count > 0
3782 && (bfd_link_relocatable (arg->link_info)
3783 || arg->link_info->emitrelocations))
3784 {
3785 if (esd->rel.count && esd->rel.hdr == NULL
3786 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3787 false, delay_sh_name_p))
3788 {
3789 arg->failed = true;
3790 return;
3791 }
3792 if (esd->rela.count && esd->rela.hdr == NULL
3793 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3794 true, delay_sh_name_p))
3795 {
3796 arg->failed = true;
3797 return;
3798 }
3799 }
3800 else if (!_bfd_elf_init_reloc_shdr (abfd,
3801 (asect->use_rela_p
3802 ? &esd->rela : &esd->rel),
3803 name,
3804 asect->use_rela_p,
3805 delay_sh_name_p))
3806 {
3807 arg->failed = true;
3808 return;
3809 }
3810 }
3811
3812 /* Check for processor-specific section types. */
3813 sh_type = this_hdr->sh_type;
3814 if (bed->elf_backend_fake_sections
3815 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3816 {
3817 arg->failed = true;
3818 return;
3819 }
3820
3821 if (sh_type == SHT_NOBITS && asect->size != 0)
3822 {
3823 /* Don't change the header type from NOBITS if we are being
3824 called for objcopy --only-keep-debug. */
3825 this_hdr->sh_type = sh_type;
3826 }
3827 }
3828
3829 /* Fill in the contents of a SHT_GROUP section. Called from
3830 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3831 when ELF targets use the generic linker, ld. Called for ld -r
3832 from bfd_elf_final_link. */
3833
3834 void
3835 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3836 {
3837 bool *failedptr = (bool *) failedptrarg;
3838 asection *elt, *first;
3839 unsigned char *loc;
3840 bool gas;
3841
3842 /* Ignore linker created group section. See elfNN_ia64_object_p in
3843 elfxx-ia64.c. */
3844 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3845 || sec->size == 0
3846 || *failedptr)
3847 return;
3848
3849 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3850 {
3851 unsigned long symindx = 0;
3852
3853 /* elf_group_id will have been set up by objcopy and the
3854 generic linker. */
3855 if (elf_group_id (sec) != NULL)
3856 symindx = elf_group_id (sec)->udata.i;
3857
3858 if (symindx == 0)
3859 {
3860 /* If called from the assembler, swap_out_syms will have set up
3861 elf_section_syms.
3862 PR 25699: A corrupt input file could contain bogus group info. */
3863 if (sec->index >= elf_num_section_syms (abfd)
3864 || elf_section_syms (abfd)[sec->index] == NULL)
3865 {
3866 *failedptr = true;
3867 return;
3868 }
3869 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3870 }
3871 elf_section_data (sec)->this_hdr.sh_info = symindx;
3872 }
3873 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3874 {
3875 /* The ELF backend linker sets sh_info to -2 when the group
3876 signature symbol is global, and thus the index can't be
3877 set until all local symbols are output. */
3878 asection *igroup;
3879 struct bfd_elf_section_data *sec_data;
3880 unsigned long symndx;
3881 unsigned long extsymoff;
3882 struct elf_link_hash_entry *h;
3883
3884 /* The point of this little dance to the first SHF_GROUP section
3885 then back to the SHT_GROUP section is that this gets us to
3886 the SHT_GROUP in the input object. */
3887 igroup = elf_sec_group (elf_next_in_group (sec));
3888 sec_data = elf_section_data (igroup);
3889 symndx = sec_data->this_hdr.sh_info;
3890 extsymoff = 0;
3891 if (!elf_bad_symtab (igroup->owner))
3892 {
3893 Elf_Internal_Shdr *symtab_hdr;
3894
3895 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3896 extsymoff = symtab_hdr->sh_info;
3897 }
3898 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3899 while (h->root.type == bfd_link_hash_indirect
3900 || h->root.type == bfd_link_hash_warning)
3901 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3902
3903 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3904 }
3905
3906 /* The contents won't be allocated for "ld -r" or objcopy. */
3907 gas = true;
3908 if (sec->contents == NULL)
3909 {
3910 gas = false;
3911 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3912
3913 /* Arrange for the section to be written out. */
3914 elf_section_data (sec)->this_hdr.contents = sec->contents;
3915 if (sec->contents == NULL)
3916 {
3917 *failedptr = true;
3918 return;
3919 }
3920 sec->alloced = 1;
3921 }
3922
3923 loc = sec->contents + sec->size;
3924
3925 /* Get the pointer to the first section in the group that gas
3926 squirreled away here. objcopy arranges for this to be set to the
3927 start of the input section group. */
3928 first = elt = elf_next_in_group (sec);
3929
3930 /* First element is a flag word. Rest of section is elf section
3931 indices for all the sections of the group. Write them backwards
3932 just to keep the group in the same order as given in .section
3933 directives, not that it matters. */
3934 while (elt != NULL)
3935 {
3936 asection *s;
3937
3938 s = elt;
3939 if (!gas)
3940 s = s->output_section;
3941 if (s != NULL
3942 && !bfd_is_abs_section (s))
3943 {
3944 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3945 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3946
3947 if (elf_sec->rel.hdr != NULL
3948 && (gas
3949 || (input_elf_sec->rel.hdr != NULL
3950 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3951 {
3952 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3953 loc -= 4;
3954 if (loc == sec->contents)
3955 break;
3956 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3957 }
3958 if (elf_sec->rela.hdr != NULL
3959 && (gas
3960 || (input_elf_sec->rela.hdr != NULL
3961 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3962 {
3963 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3964 loc -= 4;
3965 if (loc == sec->contents)
3966 break;
3967 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3968 }
3969 loc -= 4;
3970 if (loc == sec->contents)
3971 break;
3972 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3973 }
3974 elt = elf_next_in_group (elt);
3975 if (elt == first)
3976 break;
3977 }
3978
3979 /* We should always get here with loc == sec->contents + 4. Return
3980 an error for bogus SHT_GROUP sections. */
3981 loc -= 4;
3982 if (loc != sec->contents)
3983 {
3984 /* xgettext:c-format */
3985 _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
3986 abfd, sec);
3987 bfd_set_error (bfd_error_bad_value);
3988 *failedptr = true;
3989 return;
3990 }
3991
3992 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3993 }
3994
3995 /* Given NAME, the name of a relocation section stripped of its
3996 .rel/.rela prefix, return the section in ABFD to which the
3997 relocations apply. */
3998
3999 asection *
4000 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4001 {
4002 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4003 section likely apply to .got.plt or .got section. */
4004 if (get_elf_backend_data (abfd)->want_got_plt
4005 && strcmp (name, ".plt") == 0)
4006 {
4007 asection *sec;
4008
4009 name = ".got.plt";
4010 sec = bfd_get_section_by_name (abfd, name);
4011 if (sec != NULL)
4012 return sec;
4013 name = ".got";
4014 }
4015
4016 return bfd_get_section_by_name (abfd, name);
4017 }
4018
4019 /* Return the section to which RELOC_SEC applies. */
4020
4021 static asection *
4022 elf_get_reloc_section (asection *reloc_sec)
4023 {
4024 const char *name;
4025 unsigned int type;
4026 bfd *abfd;
4027 const struct elf_backend_data *bed;
4028
4029 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4030 if (type != SHT_REL && type != SHT_RELA)
4031 return NULL;
4032
4033 /* We look up the section the relocs apply to by name. */
4034 name = reloc_sec->name;
4035 if (!startswith (name, ".rel"))
4036 return NULL;
4037 name += 4;
4038 if (type == SHT_RELA && *name++ != 'a')
4039 return NULL;
4040
4041 abfd = reloc_sec->owner;
4042 bed = get_elf_backend_data (abfd);
4043 return bed->get_reloc_section (abfd, name);
4044 }
4045
4046 /* Assign all ELF section numbers. The dummy first section is handled here
4047 too. The link/info pointers for the standard section types are filled
4048 in here too, while we're at it. LINK_INFO will be 0 when arriving
4049 here for gas, objcopy, and when using the generic ELF linker. */
4050
4051 static bool
4052 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4053 {
4054 struct elf_obj_tdata *t = elf_tdata (abfd);
4055 asection *sec;
4056 unsigned int section_number;
4057 Elf_Internal_Shdr **i_shdrp;
4058 struct bfd_elf_section_data *d;
4059 bool need_symtab;
4060 size_t amt;
4061
4062 section_number = 1;
4063
4064 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4065
4066 /* SHT_GROUP sections are in relocatable files only. */
4067 if (link_info == NULL || !link_info->resolve_section_groups)
4068 {
4069 size_t reloc_count = 0;
4070
4071 /* Put SHT_GROUP sections first. */
4072 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4073 {
4074 d = elf_section_data (sec);
4075
4076 if (d->this_hdr.sh_type == SHT_GROUP)
4077 {
4078 if (sec->flags & SEC_LINKER_CREATED)
4079 {
4080 /* Remove the linker created SHT_GROUP sections. */
4081 bfd_section_list_remove (abfd, sec);
4082 abfd->section_count--;
4083 }
4084 else
4085 d->this_idx = section_number++;
4086 }
4087
4088 /* Count relocations. */
4089 reloc_count += sec->reloc_count;
4090 }
4091
4092 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4093 if (reloc_count == 0)
4094 abfd->flags &= ~HAS_RELOC;
4095 else
4096 abfd->flags |= HAS_RELOC;
4097 }
4098
4099 for (sec = abfd->sections; sec; sec = sec->next)
4100 {
4101 d = elf_section_data (sec);
4102
4103 if (d->this_hdr.sh_type != SHT_GROUP)
4104 d->this_idx = section_number++;
4105 if (d->this_hdr.sh_name != (unsigned int) -1)
4106 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4107 if (d->rel.hdr)
4108 {
4109 d->rel.idx = section_number++;
4110 if (d->rel.hdr->sh_name != (unsigned int) -1)
4111 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4112 }
4113 else
4114 d->rel.idx = 0;
4115
4116 if (d->rela.hdr)
4117 {
4118 d->rela.idx = section_number++;
4119 if (d->rela.hdr->sh_name != (unsigned int) -1)
4120 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4121 }
4122 else
4123 d->rela.idx = 0;
4124 }
4125
4126 need_symtab = (bfd_get_symcount (abfd) > 0
4127 || (link_info == NULL
4128 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4129 == HAS_RELOC)));
4130 if (need_symtab)
4131 {
4132 elf_onesymtab (abfd) = section_number++;
4133 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4134 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4135 {
4136 elf_section_list *entry;
4137
4138 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4139
4140 entry = bfd_zalloc (abfd, sizeof (*entry));
4141 entry->ndx = section_number++;
4142 elf_symtab_shndx_list (abfd) = entry;
4143 entry->hdr.sh_name
4144 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4145 ".symtab_shndx", false);
4146 if (entry->hdr.sh_name == (unsigned int) -1)
4147 return false;
4148 }
4149 elf_strtab_sec (abfd) = section_number++;
4150 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4151 }
4152
4153 elf_shstrtab_sec (abfd) = section_number++;
4154 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4155 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4156
4157 if (section_number >= SHN_LORESERVE)
4158 {
4159 /* xgettext:c-format */
4160 _bfd_error_handler (_("%pB: too many sections: %u"),
4161 abfd, section_number);
4162 return false;
4163 }
4164
4165 elf_numsections (abfd) = section_number;
4166 elf_elfheader (abfd)->e_shnum = section_number;
4167
4168 /* Set up the list of section header pointers, in agreement with the
4169 indices. */
4170 amt = section_number * sizeof (Elf_Internal_Shdr *);
4171 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4172 if (i_shdrp == NULL)
4173 return false;
4174
4175 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4176 sizeof (Elf_Internal_Shdr));
4177 if (i_shdrp[0] == NULL)
4178 {
4179 bfd_release (abfd, i_shdrp);
4180 return false;
4181 }
4182
4183 elf_elfsections (abfd) = i_shdrp;
4184
4185 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4186 if (need_symtab)
4187 {
4188 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4189 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4190 {
4191 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4192 BFD_ASSERT (entry != NULL);
4193 i_shdrp[entry->ndx] = & entry->hdr;
4194 entry->hdr.sh_link = elf_onesymtab (abfd);
4195 }
4196 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4197 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4198 }
4199
4200 for (sec = abfd->sections; sec; sec = sec->next)
4201 {
4202 asection *s;
4203
4204 d = elf_section_data (sec);
4205
4206 i_shdrp[d->this_idx] = &d->this_hdr;
4207 if (d->rel.idx != 0)
4208 i_shdrp[d->rel.idx] = d->rel.hdr;
4209 if (d->rela.idx != 0)
4210 i_shdrp[d->rela.idx] = d->rela.hdr;
4211
4212 /* Fill in the sh_link and sh_info fields while we're at it. */
4213
4214 /* sh_link of a reloc section is the section index of the symbol
4215 table. sh_info is the section index of the section to which
4216 the relocation entries apply. */
4217 if (d->rel.idx != 0)
4218 {
4219 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4220 d->rel.hdr->sh_info = d->this_idx;
4221 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4222 }
4223 if (d->rela.idx != 0)
4224 {
4225 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4226 d->rela.hdr->sh_info = d->this_idx;
4227 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4228 }
4229
4230 /* We need to set up sh_link for SHF_LINK_ORDER. */
4231 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4232 {
4233 s = elf_linked_to_section (sec);
4234 /* We can now have a NULL linked section pointer.
4235 This happens when the sh_link field is 0, which is done
4236 when a linked to section is discarded but the linking
4237 section has been retained for some reason. */
4238 if (s)
4239 {
4240 /* Check discarded linkonce section. */
4241 if (discarded_section (s))
4242 {
4243 asection *kept;
4244 _bfd_error_handler
4245 /* xgettext:c-format */
4246 (_("%pB: sh_link of section `%pA' points to"
4247 " discarded section `%pA' of `%pB'"),
4248 abfd, d->this_hdr.bfd_section, s, s->owner);
4249 /* Point to the kept section if it has the same
4250 size as the discarded one. */
4251 kept = _bfd_elf_check_kept_section (s, link_info);
4252 if (kept == NULL)
4253 {
4254 bfd_set_error (bfd_error_bad_value);
4255 return false;
4256 }
4257 s = kept;
4258 }
4259 /* Handle objcopy. */
4260 else if (s->output_section == NULL)
4261 {
4262 _bfd_error_handler
4263 /* xgettext:c-format */
4264 (_("%pB: sh_link of section `%pA' points to"
4265 " removed section `%pA' of `%pB'"),
4266 abfd, d->this_hdr.bfd_section, s, s->owner);
4267 bfd_set_error (bfd_error_bad_value);
4268 return false;
4269 }
4270 s = s->output_section;
4271 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4272 }
4273 }
4274
4275 switch (d->this_hdr.sh_type)
4276 {
4277 case SHT_REL:
4278 case SHT_RELA:
4279 /* sh_link is the section index of the symbol table.
4280 sh_info is the section index of the section to which the
4281 relocation entries apply. */
4282 if (d->this_hdr.sh_link == 0)
4283 {
4284 /* FIXME maybe: If this is a reloc section which we are
4285 treating as a normal section then we likely should
4286 not be assuming its sh_link is .dynsym or .symtab. */
4287 if ((sec->flags & SEC_ALLOC) != 0)
4288 {
4289 s = bfd_get_section_by_name (abfd, ".dynsym");
4290 if (s != NULL)
4291 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4292 }
4293 else
4294 d->this_hdr.sh_link = elf_onesymtab (abfd);
4295 }
4296
4297 s = elf_get_reloc_section (sec);
4298 if (s != NULL)
4299 {
4300 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4301 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4302 }
4303 break;
4304
4305 case SHT_STRTAB:
4306 /* We assume that a section named .stab*str is a stabs
4307 string section. We look for a section with the same name
4308 but without the trailing ``str'', and set its sh_link
4309 field to point to this section. */
4310 if (startswith (sec->name, ".stab")
4311 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4312 {
4313 size_t len;
4314 char *alc;
4315
4316 len = strlen (sec->name);
4317 alc = (char *) bfd_malloc (len - 2);
4318 if (alc == NULL)
4319 return false;
4320 memcpy (alc, sec->name, len - 3);
4321 alc[len - 3] = '\0';
4322 s = bfd_get_section_by_name (abfd, alc);
4323 free (alc);
4324 if (s != NULL)
4325 {
4326 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4327
4328 /* This is a .stab section. */
4329 elf_section_data (s)->this_hdr.sh_entsize = 12;
4330 }
4331 }
4332 break;
4333
4334 case SHT_DYNAMIC:
4335 case SHT_DYNSYM:
4336 case SHT_GNU_verneed:
4337 case SHT_GNU_verdef:
4338 /* sh_link is the section header index of the string table
4339 used for the dynamic entries, or the symbol table, or the
4340 version strings. */
4341 s = bfd_get_section_by_name (abfd, ".dynstr");
4342 if (s != NULL)
4343 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4344 break;
4345
4346 case SHT_GNU_LIBLIST:
4347 /* sh_link is the section header index of the prelink library
4348 list used for the dynamic entries, or the symbol table, or
4349 the version strings. */
4350 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4351 ? ".dynstr" : ".gnu.libstr"));
4352 if (s != NULL)
4353 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4354 break;
4355
4356 case SHT_HASH:
4357 case SHT_GNU_HASH:
4358 case SHT_GNU_versym:
4359 /* sh_link is the section header index of the symbol table
4360 this hash table or version table is for. */
4361 s = bfd_get_section_by_name (abfd, ".dynsym");
4362 if (s != NULL)
4363 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4364 break;
4365
4366 case SHT_GROUP:
4367 d->this_hdr.sh_link = elf_onesymtab (abfd);
4368 }
4369 }
4370
4371 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4372 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4373 debug section name from .debug_* to .zdebug_* if needed. */
4374
4375 return true;
4376 }
4377
4378 static bool
4379 sym_is_global (bfd *abfd, asymbol *sym)
4380 {
4381 /* If the backend has a special mapping, use it. */
4382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4383 if (bed->elf_backend_sym_is_global)
4384 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4385
4386 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4387 || bfd_is_und_section (bfd_asymbol_section (sym))
4388 || bfd_is_com_section (bfd_asymbol_section (sym)));
4389 }
4390
4391 /* Filter global symbols of ABFD to include in the import library. All
4392 SYMCOUNT symbols of ABFD can be examined from their pointers in
4393 SYMS. Pointers of symbols to keep should be stored contiguously at
4394 the beginning of that array.
4395
4396 Returns the number of symbols to keep. */
4397
4398 unsigned int
4399 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4400 asymbol **syms, long symcount)
4401 {
4402 long src_count, dst_count = 0;
4403
4404 for (src_count = 0; src_count < symcount; src_count++)
4405 {
4406 asymbol *sym = syms[src_count];
4407 char *name = (char *) bfd_asymbol_name (sym);
4408 struct bfd_link_hash_entry *h;
4409
4410 if (!sym_is_global (abfd, sym))
4411 continue;
4412
4413 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4414 if (h == NULL)
4415 continue;
4416 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4417 continue;
4418 if (h->linker_def || h->ldscript_def)
4419 continue;
4420
4421 syms[dst_count++] = sym;
4422 }
4423
4424 syms[dst_count] = NULL;
4425
4426 return dst_count;
4427 }
4428
4429 /* Don't output symbols for sections that are not going to be output,
4430 that are duplicates or there is no BFD section. */
4431
4432 static bool
4433 ignore_sym (asymbol *sym)
4434 {
4435 if (sym == NULL)
4436 return false;
4437
4438 if (sym->section == NULL)
4439 return true;
4440
4441 if ((sym->flags & BSF_SECTION_SYM) != 0)
4442 {
4443 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4444 return true;
4445 /* With ld -r on generic elf targets it is possible to have
4446 multiple section symbols in the output for a given section.
4447 We'd like to get rid of all but the first one. This drops
4448 them if the first input section is non-zero size, but fails
4449 to do so if the first input section is zero sized. */
4450 if (sym->section->output_offset != 0)
4451 return true;
4452 }
4453
4454 return discarded_section (sym->section);
4455 }
4456
4457 /* Map symbol from it's internal number to the external number, moving
4458 all local symbols to be at the head of the list. */
4459
4460 static bool
4461 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4462 {
4463 unsigned int symcount = bfd_get_symcount (abfd);
4464 asymbol **syms = bfd_get_outsymbols (abfd);
4465 asymbol **sect_syms;
4466 unsigned int num_locals = 0;
4467 unsigned int num_globals = 0;
4468 unsigned int max_index = 0;
4469 unsigned int idx;
4470 asection *asect;
4471 asymbol **new_syms;
4472 size_t amt;
4473
4474 #ifdef DEBUG
4475 fprintf (stderr, "elf_map_symbols\n");
4476 fflush (stderr);
4477 #endif
4478
4479 for (asect = abfd->sections; asect; asect = asect->next)
4480 {
4481 if (max_index < asect->index)
4482 max_index = asect->index;
4483 }
4484
4485 max_index++;
4486 amt = max_index * sizeof (asymbol *);
4487 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4488 if (sect_syms == NULL)
4489 return false;
4490 elf_section_syms (abfd) = sect_syms;
4491 elf_num_section_syms (abfd) = max_index;
4492
4493 /* Init sect_syms entries for any section symbols we have already
4494 decided to output. */
4495 for (idx = 0; idx < symcount; idx++)
4496 {
4497 asymbol *sym = syms[idx];
4498
4499 if ((sym->flags & BSF_SECTION_SYM) != 0
4500 && sym->value == 0
4501 && !ignore_sym (sym)
4502 && !bfd_is_abs_section (sym->section))
4503 {
4504 asection *sec = sym->section;
4505
4506 if (sec->owner != abfd)
4507 {
4508 sec = sec->output_section;
4509 if (sec == NULL)
4510 return false;
4511 }
4512
4513 sect_syms[sec->index] = syms[idx];
4514 }
4515 }
4516
4517 /* Classify all of the symbols. */
4518 for (idx = 0; idx < symcount; idx++)
4519 {
4520 if (ignore_sym (syms[idx]))
4521 continue;
4522 if (sym_is_global (abfd, syms[idx]))
4523 num_globals++;
4524 else
4525 num_locals++;
4526 }
4527
4528 /* We will be adding a section symbol for each normal BFD section. Most
4529 sections will already have a section symbol in outsymbols, but
4530 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4531 at least in that case. */
4532 for (asect = abfd->sections; asect; asect = asect->next)
4533 {
4534 asymbol *sym = asect->symbol;
4535 /* Don't include ignored section symbols. */
4536 if (!ignore_sym (sym)
4537 && sect_syms[asect->index] == NULL)
4538 {
4539 if (sym_is_global (abfd, asect->symbol))
4540 num_globals++;
4541 else
4542 num_locals++;
4543 }
4544 }
4545
4546 /* Now sort the symbols so the local symbols are first. */
4547 amt = (num_locals + num_globals) * sizeof (asymbol *);
4548 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4549 if (new_syms == NULL)
4550 return false;
4551
4552 unsigned int num_globals2 = 0;
4553 unsigned int num_locals2 = 0;
4554 for (idx = 0; idx < symcount; idx++)
4555 {
4556 asymbol *sym = syms[idx];
4557 unsigned int i;
4558
4559 if (ignore_sym (sym))
4560 continue;
4561
4562 if (sym_is_global (abfd, sym))
4563 i = num_locals + num_globals2++;
4564 else
4565 i = num_locals2++;
4566 new_syms[i] = sym;
4567 sym->udata.i = i + 1;
4568 }
4569 for (asect = abfd->sections; asect; asect = asect->next)
4570 {
4571 asymbol *sym = asect->symbol;
4572 if (!ignore_sym (sym)
4573 && sect_syms[asect->index] == NULL)
4574 {
4575 unsigned int i;
4576
4577 sect_syms[asect->index] = sym;
4578 if (sym_is_global (abfd, sym))
4579 i = num_locals + num_globals2++;
4580 else
4581 i = num_locals2++;
4582 new_syms[i] = sym;
4583 sym->udata.i = i + 1;
4584 }
4585 }
4586
4587 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4588
4589 *pnum_locals = num_locals;
4590 return true;
4591 }
4592
4593 /* Assign a file position to a section, optionally aligning to the
4594 required section alignment. */
4595
4596 file_ptr
4597 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4598 file_ptr offset,
4599 bool align,
4600 unsigned char log_file_align)
4601 {
4602 if (i_shdrp->sh_addralign > 1)
4603 {
4604 file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4605
4606 if (align)
4607 offset = BFD_ALIGN (offset, salign);
4608 else if (log_file_align)
4609 {
4610 /* Heuristic: Cap alignment at log_file_align. */
4611 file_ptr falign = 1u << log_file_align;
4612
4613 offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4614 }
4615 }
4616 i_shdrp->sh_offset = offset;
4617 if (i_shdrp->bfd_section != NULL)
4618 i_shdrp->bfd_section->filepos = offset;
4619 if (i_shdrp->sh_type != SHT_NOBITS)
4620 offset += i_shdrp->sh_size;
4621 return offset;
4622 }
4623
4624 /* Compute the file positions we are going to put the sections at, and
4625 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4626 is not NULL, this is being called by the ELF backend linker. */
4627
4628 bool
4629 _bfd_elf_compute_section_file_positions (bfd *abfd,
4630 struct bfd_link_info *link_info)
4631 {
4632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4633 struct fake_section_arg fsargs;
4634 bool failed;
4635 struct elf_strtab_hash *strtab = NULL;
4636 Elf_Internal_Shdr *shstrtab_hdr;
4637 bool need_symtab;
4638
4639 if (abfd->output_has_begun)
4640 return true;
4641
4642 /* Do any elf backend specific processing first. */
4643 if (bed->elf_backend_begin_write_processing)
4644 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4645
4646 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4647 return false;
4648
4649 fsargs.failed = false;
4650 fsargs.link_info = link_info;
4651 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4652 if (fsargs.failed)
4653 return false;
4654
4655 if (!assign_section_numbers (abfd, link_info))
4656 return false;
4657
4658 /* The backend linker builds symbol table information itself. */
4659 need_symtab = (link_info == NULL
4660 && (bfd_get_symcount (abfd) > 0
4661 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4662 == HAS_RELOC)));
4663 if (need_symtab)
4664 {
4665 /* Non-zero if doing a relocatable link. */
4666 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4667
4668 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4669 return false;
4670 }
4671
4672 failed = false;
4673 if (link_info == NULL)
4674 {
4675 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4676 if (failed)
4677 goto err_free_strtab;
4678 }
4679
4680 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4681 /* sh_name was set in init_file_header. */
4682 shstrtab_hdr->sh_type = SHT_STRTAB;
4683 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4684 shstrtab_hdr->sh_addr = 0;
4685 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4686 shstrtab_hdr->sh_entsize = 0;
4687 shstrtab_hdr->sh_link = 0;
4688 shstrtab_hdr->sh_info = 0;
4689 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4690 shstrtab_hdr->sh_addralign = 1;
4691
4692 if (!assign_file_positions_except_relocs (abfd, link_info))
4693 goto err_free_strtab;
4694
4695 if (strtab != NULL)
4696 {
4697 file_ptr off;
4698 Elf_Internal_Shdr *hdr;
4699
4700 off = elf_next_file_pos (abfd);
4701
4702 hdr = & elf_symtab_hdr (abfd);
4703 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4704
4705 if (elf_symtab_shndx_list (abfd) != NULL)
4706 {
4707 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4708 if (hdr->sh_size != 0)
4709 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4710 /* FIXME: What about other symtab_shndx sections in the list ? */
4711 }
4712
4713 hdr = &elf_tdata (abfd)->strtab_hdr;
4714 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4715
4716 elf_next_file_pos (abfd) = off;
4717
4718 /* Now that we know where the .strtab section goes, write it
4719 out. */
4720 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4721 || ! _bfd_elf_strtab_emit (abfd, strtab))
4722 goto err_free_strtab;
4723 _bfd_elf_strtab_free (strtab);
4724 }
4725
4726 abfd->output_has_begun = true;
4727 return true;
4728
4729 err_free_strtab:
4730 if (strtab != NULL)
4731 _bfd_elf_strtab_free (strtab);
4732 return false;
4733 }
4734
4735 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4736 function effectively returns whether --eh-frame-hdr is given on the
4737 command line. After size_dynamic_sections the result reflects
4738 whether .eh_frame_hdr will actually be output (sizing isn't done
4739 until ldemul_after_allocation). */
4740
4741 static asection *
4742 elf_eh_frame_hdr (const struct bfd_link_info *info)
4743 {
4744 if (info != NULL && is_elf_hash_table (info->hash))
4745 return elf_hash_table (info)->eh_info.hdr_sec;
4746 return NULL;
4747 }
4748
4749 /* Make an initial estimate of the size of the program header. If we
4750 get the number wrong here, we'll redo section placement. */
4751
4752 static bfd_size_type
4753 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4754 {
4755 size_t segs;
4756 asection *s;
4757 const struct elf_backend_data *bed;
4758
4759 /* Assume we will need exactly two PT_LOAD segments: one for text
4760 and one for data. */
4761 segs = 2;
4762
4763 s = bfd_get_section_by_name (abfd, ".interp");
4764 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4765 {
4766 /* If we have a loadable interpreter section, we need a
4767 PT_INTERP segment. In this case, assume we also need a
4768 PT_PHDR segment, although that may not be true for all
4769 targets. */
4770 segs += 2;
4771 }
4772
4773 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4774 {
4775 /* We need a PT_DYNAMIC segment. */
4776 ++segs;
4777 }
4778
4779 if (info != NULL && info->relro)
4780 {
4781 /* We need a PT_GNU_RELRO segment. */
4782 ++segs;
4783 }
4784
4785 if (elf_eh_frame_hdr (info))
4786 {
4787 /* We need a PT_GNU_EH_FRAME segment. */
4788 ++segs;
4789 }
4790
4791 if (elf_stack_flags (abfd))
4792 {
4793 /* We need a PT_GNU_STACK segment. */
4794 ++segs;
4795 }
4796
4797 if (elf_sframe (abfd))
4798 {
4799 /* We need a PT_GNU_SFRAME segment. */
4800 ++segs;
4801 }
4802
4803 s = bfd_get_section_by_name (abfd,
4804 NOTE_GNU_PROPERTY_SECTION_NAME);
4805 if (s != NULL && s->size != 0)
4806 {
4807 /* We need a PT_GNU_PROPERTY segment. */
4808 ++segs;
4809 }
4810
4811 for (s = abfd->sections; s != NULL; s = s->next)
4812 {
4813 if ((s->flags & SEC_LOAD) != 0
4814 && elf_section_type (s) == SHT_NOTE)
4815 {
4816 unsigned int alignment_power;
4817 /* We need a PT_NOTE segment. */
4818 ++segs;
4819 /* Try to create just one PT_NOTE segment for all adjacent
4820 loadable SHT_NOTE sections. gABI requires that within a
4821 PT_NOTE segment (and also inside of each SHT_NOTE section)
4822 each note should have the same alignment. So we check
4823 whether the sections are correctly aligned. */
4824 alignment_power = s->alignment_power;
4825 while (s->next != NULL
4826 && s->next->alignment_power == alignment_power
4827 && (s->next->flags & SEC_LOAD) != 0
4828 && elf_section_type (s->next) == SHT_NOTE)
4829 s = s->next;
4830 }
4831 }
4832
4833 for (s = abfd->sections; s != NULL; s = s->next)
4834 {
4835 if (s->flags & SEC_THREAD_LOCAL)
4836 {
4837 /* We need a PT_TLS segment. */
4838 ++segs;
4839 break;
4840 }
4841 }
4842
4843 bed = get_elf_backend_data (abfd);
4844
4845 if ((abfd->flags & D_PAGED) != 0
4846 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4847 {
4848 /* Add a PT_GNU_MBIND segment for each mbind section. */
4849 bfd_vma commonpagesize;
4850 unsigned int page_align_power;
4851
4852 if (info != NULL)
4853 commonpagesize = info->commonpagesize;
4854 else
4855 commonpagesize = bed->commonpagesize;
4856 page_align_power = bfd_log2 (commonpagesize);
4857 for (s = abfd->sections; s != NULL; s = s->next)
4858 if (elf_section_flags (s) & SHF_GNU_MBIND)
4859 {
4860 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4861 {
4862 _bfd_error_handler
4863 /* xgettext:c-format */
4864 (_("%pB: GNU_MBIND section `%pA' has invalid "
4865 "sh_info field: %d"),
4866 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4867 continue;
4868 }
4869 /* Align mbind section to page size. */
4870 if (s->alignment_power < page_align_power)
4871 s->alignment_power = page_align_power;
4872 segs ++;
4873 }
4874 }
4875
4876 /* Let the backend count up any program headers it might need. */
4877 if (bed->elf_backend_additional_program_headers)
4878 {
4879 int a;
4880
4881 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4882 if (a == -1)
4883 abort ();
4884 segs += a;
4885 }
4886
4887 return segs * bed->s->sizeof_phdr;
4888 }
4889
4890 /* Find the segment that contains the output_section of section. */
4891
4892 Elf_Internal_Phdr *
4893 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4894 {
4895 struct elf_segment_map *m;
4896 Elf_Internal_Phdr *p;
4897
4898 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4899 m != NULL;
4900 m = m->next, p++)
4901 {
4902 int i;
4903
4904 for (i = m->count - 1; i >= 0; i--)
4905 if (m->sections[i] == section)
4906 return p;
4907 }
4908
4909 return NULL;
4910 }
4911
4912 /* Create a mapping from a set of sections to a program segment. */
4913
4914 static struct elf_segment_map *
4915 make_mapping (bfd *abfd,
4916 asection **sections,
4917 unsigned int from,
4918 unsigned int to,
4919 bool phdr)
4920 {
4921 struct elf_segment_map *m;
4922 unsigned int i;
4923 asection **hdrpp;
4924 size_t amt;
4925
4926 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4927 amt += (to - from) * sizeof (asection *);
4928 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4929 if (m == NULL)
4930 return NULL;
4931 m->next = NULL;
4932 m->p_type = PT_LOAD;
4933 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4934 m->sections[i - from] = *hdrpp;
4935 m->count = to - from;
4936
4937 if (from == 0 && phdr)
4938 {
4939 /* Include the headers in the first PT_LOAD segment. */
4940 m->includes_filehdr = 1;
4941 m->includes_phdrs = 1;
4942 }
4943
4944 return m;
4945 }
4946
4947 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4948 on failure. */
4949
4950 struct elf_segment_map *
4951 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4952 {
4953 struct elf_segment_map *m;
4954
4955 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4956 sizeof (struct elf_segment_map));
4957 if (m == NULL)
4958 return NULL;
4959 m->next = NULL;
4960 m->p_type = PT_DYNAMIC;
4961 m->count = 1;
4962 m->sections[0] = dynsec;
4963
4964 return m;
4965 }
4966
4967 /* Possibly add or remove segments from the segment map. */
4968
4969 static bool
4970 elf_modify_segment_map (bfd *abfd,
4971 struct bfd_link_info *info,
4972 bool remove_empty_load)
4973 {
4974 struct elf_segment_map **m;
4975 const struct elf_backend_data *bed;
4976
4977 /* The placement algorithm assumes that non allocated sections are
4978 not in PT_LOAD segments. We ensure this here by removing such
4979 sections from the segment map. We also remove excluded
4980 sections. Finally, any PT_LOAD segment without sections is
4981 removed. */
4982 m = &elf_seg_map (abfd);
4983 while (*m)
4984 {
4985 unsigned int i, new_count;
4986
4987 for (new_count = 0, i = 0; i < (*m)->count; i++)
4988 {
4989 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4990 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4991 || (*m)->p_type != PT_LOAD))
4992 {
4993 (*m)->sections[new_count] = (*m)->sections[i];
4994 new_count++;
4995 }
4996 }
4997 (*m)->count = new_count;
4998
4999 if (remove_empty_load
5000 && (*m)->p_type == PT_LOAD
5001 && (*m)->count == 0
5002 && !(*m)->includes_phdrs)
5003 *m = (*m)->next;
5004 else
5005 m = &(*m)->next;
5006 }
5007
5008 bed = get_elf_backend_data (abfd);
5009 if (bed->elf_backend_modify_segment_map != NULL)
5010 {
5011 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5012 return false;
5013 }
5014
5015 return true;
5016 }
5017
5018 #define IS_TBSS(s) \
5019 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5020
5021 /* Set up a mapping from BFD sections to program segments. Update
5022 NEED_LAYOUT if the section layout is changed. */
5023
5024 bool
5025 _bfd_elf_map_sections_to_segments (bfd *abfd,
5026 struct bfd_link_info *info,
5027 bool *need_layout)
5028 {
5029 unsigned int count;
5030 struct elf_segment_map *m;
5031 asection **sections = NULL;
5032 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5033 bool no_user_phdrs;
5034
5035 no_user_phdrs = elf_seg_map (abfd) == NULL;
5036
5037 if (info != NULL)
5038 {
5039 info->user_phdrs = !no_user_phdrs;
5040
5041 /* Size the relative relocations if DT_RELR is enabled. */
5042 if (info->enable_dt_relr
5043 && need_layout != NULL
5044 && bed->size_relative_relocs
5045 && !bed->size_relative_relocs (info, need_layout))
5046 info->callbacks->fatal
5047 (_("%P: failed to size relative relocations\n"));
5048 }
5049
5050 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5051 {
5052 asection *s;
5053 unsigned int i;
5054 struct elf_segment_map *mfirst;
5055 struct elf_segment_map **pm;
5056 asection *last_hdr;
5057 bfd_vma last_size;
5058 unsigned int hdr_index;
5059 bfd_vma maxpagesize;
5060 asection **hdrpp;
5061 bool phdr_in_segment;
5062 bool writable;
5063 bool executable;
5064 unsigned int tls_count = 0;
5065 asection *first_tls = NULL;
5066 asection *first_mbind = NULL;
5067 asection *dynsec, *eh_frame_hdr;
5068 asection *sframe;
5069 size_t amt;
5070 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5071 bfd_size_type phdr_size; /* Octets/bytes. */
5072 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5073
5074 /* Select the allocated sections, and sort them. */
5075
5076 amt = bfd_count_sections (abfd) * sizeof (asection *);
5077 sections = (asection **) bfd_malloc (amt);
5078 if (sections == NULL)
5079 goto error_return;
5080
5081 /* Calculate top address, avoiding undefined behaviour of shift
5082 left operator when shift count is equal to size of type
5083 being shifted. */
5084 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5085 addr_mask = (addr_mask << 1) + 1;
5086
5087 i = 0;
5088 for (s = abfd->sections; s != NULL; s = s->next)
5089 {
5090 if ((s->flags & SEC_ALLOC) != 0)
5091 {
5092 /* target_index is unused until bfd_elf_final_link
5093 starts output of section symbols. Use it to make
5094 qsort stable. */
5095 s->target_index = i;
5096 sections[i] = s;
5097 ++i;
5098 /* A wrapping section potentially clashes with header. */
5099 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5100 wrap_to = (s->lma + s->size / opb) & addr_mask;
5101 }
5102 }
5103 BFD_ASSERT (i <= bfd_count_sections (abfd));
5104 count = i;
5105
5106 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5107
5108 phdr_size = elf_program_header_size (abfd);
5109 if (phdr_size == (bfd_size_type) -1)
5110 phdr_size = get_program_header_size (abfd, info);
5111 phdr_size += bed->s->sizeof_ehdr;
5112 /* phdr_size is compared to LMA values which are in bytes. */
5113 phdr_size /= opb;
5114 if (info != NULL)
5115 maxpagesize = info->maxpagesize;
5116 else
5117 maxpagesize = bed->maxpagesize;
5118 if (maxpagesize == 0)
5119 maxpagesize = 1;
5120 phdr_in_segment = info != NULL && info->load_phdrs;
5121 if (count != 0
5122 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5123 >= (phdr_size & (maxpagesize - 1))))
5124 /* For compatibility with old scripts that may not be using
5125 SIZEOF_HEADERS, add headers when it looks like space has
5126 been left for them. */
5127 phdr_in_segment = true;
5128
5129 /* Build the mapping. */
5130 mfirst = NULL;
5131 pm = &mfirst;
5132
5133 /* If we have a .interp section, then create a PT_PHDR segment for
5134 the program headers and a PT_INTERP segment for the .interp
5135 section. */
5136 s = bfd_get_section_by_name (abfd, ".interp");
5137 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5138 {
5139 amt = sizeof (struct elf_segment_map);
5140 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5141 if (m == NULL)
5142 goto error_return;
5143 m->next = NULL;
5144 m->p_type = PT_PHDR;
5145 m->p_flags = PF_R;
5146 m->p_flags_valid = 1;
5147 m->includes_phdrs = 1;
5148 phdr_in_segment = true;
5149 *pm = m;
5150 pm = &m->next;
5151
5152 amt = sizeof (struct elf_segment_map);
5153 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5154 if (m == NULL)
5155 goto error_return;
5156 m->next = NULL;
5157 m->p_type = PT_INTERP;
5158 m->count = 1;
5159 m->sections[0] = s;
5160
5161 *pm = m;
5162 pm = &m->next;
5163 }
5164
5165 /* Look through the sections. We put sections in the same program
5166 segment when the start of the second section can be placed within
5167 a few bytes of the end of the first section. */
5168 last_hdr = NULL;
5169 last_size = 0;
5170 hdr_index = 0;
5171 writable = false;
5172 executable = false;
5173 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5174 if (dynsec != NULL
5175 && (dynsec->flags & SEC_LOAD) == 0)
5176 dynsec = NULL;
5177
5178 if ((abfd->flags & D_PAGED) == 0)
5179 phdr_in_segment = false;
5180
5181 /* Deal with -Ttext or something similar such that the first section
5182 is not adjacent to the program headers. This is an
5183 approximation, since at this point we don't know exactly how many
5184 program headers we will need. */
5185 if (phdr_in_segment && count > 0)
5186 {
5187 bfd_vma phdr_lma; /* Bytes. */
5188 bool separate_phdr = false;
5189
5190 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5191 if (info != NULL
5192 && info->separate_code
5193 && (sections[0]->flags & SEC_CODE) != 0)
5194 {
5195 /* If data sections should be separate from code and
5196 thus not executable, and the first section is
5197 executable then put the file and program headers in
5198 their own PT_LOAD. */
5199 if (!info->one_rosegment)
5200 separate_phdr = true;
5201
5202 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5203 == (sections[0]->lma & addr_mask & -maxpagesize)))
5204 {
5205 /* The file and program headers are currently on the
5206 same page as the first section. Put them on the
5207 previous page if we can. */
5208 if (phdr_lma >= maxpagesize)
5209 phdr_lma -= maxpagesize;
5210 else
5211 separate_phdr = false;
5212 }
5213 }
5214 if ((sections[0]->lma & addr_mask) < phdr_lma
5215 || (sections[0]->lma & addr_mask) < phdr_size)
5216 /* If file and program headers would be placed at the end
5217 of memory then it's probably better to omit them. */
5218 phdr_in_segment = false;
5219 else if (phdr_lma < wrap_to)
5220 /* If a section wraps around to where we'll be placing
5221 file and program headers, then the headers will be
5222 overwritten. */
5223 phdr_in_segment = false;
5224 else if (separate_phdr)
5225 {
5226 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5227 if (m == NULL)
5228 goto error_return;
5229 m->p_paddr = phdr_lma * opb;
5230 m->p_vaddr_offset
5231 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5232 m->p_paddr_valid = 1;
5233 *pm = m;
5234 pm = &m->next;
5235 phdr_in_segment = false;
5236 }
5237 }
5238
5239 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5240 {
5241 asection *hdr;
5242 bool new_segment;
5243
5244 hdr = *hdrpp;
5245
5246 /* See if this section and the last one will fit in the same
5247 segment. */
5248
5249 if (last_hdr == NULL)
5250 {
5251 /* If we don't have a segment yet, then we don't need a new
5252 one (we build the last one after this loop). */
5253 new_segment = false;
5254 }
5255 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5256 {
5257 /* If this section has a different relation between the
5258 virtual address and the load address, then we need a new
5259 segment. */
5260 new_segment = true;
5261 }
5262 else if (hdr->lma < last_hdr->lma + last_size
5263 || last_hdr->lma + last_size < last_hdr->lma)
5264 {
5265 /* If this section has a load address that makes it overlap
5266 the previous section, then we need a new segment. */
5267 new_segment = true;
5268 }
5269 else if ((abfd->flags & D_PAGED) != 0
5270 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5271 == (hdr->lma & -maxpagesize)))
5272 {
5273 /* If we are demand paged then we can't map two disk
5274 pages onto the same memory page. */
5275 new_segment = false;
5276 }
5277 /* In the next test we have to be careful when last_hdr->lma is close
5278 to the end of the address space. If the aligned address wraps
5279 around to the start of the address space, then there are no more
5280 pages left in memory and it is OK to assume that the current
5281 section can be included in the current segment. */
5282 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5283 + maxpagesize > last_hdr->lma)
5284 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5285 + maxpagesize <= hdr->lma))
5286 {
5287 /* If putting this section in this segment would force us to
5288 skip a page in the segment, then we need a new segment. */
5289 new_segment = true;
5290 }
5291 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5292 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5293 {
5294 /* We don't want to put a loaded section after a
5295 nonloaded (ie. bss style) section in the same segment
5296 as that will force the non-loaded section to be loaded.
5297 Consider .tbss sections as loaded for this purpose. */
5298 new_segment = true;
5299 }
5300 else if ((abfd->flags & D_PAGED) == 0)
5301 {
5302 /* If the file is not demand paged, which means that we
5303 don't require the sections to be correctly aligned in the
5304 file, then there is no other reason for a new segment. */
5305 new_segment = false;
5306 }
5307 else if (info != NULL
5308 && info->separate_code
5309 && executable != ((hdr->flags & SEC_CODE) != 0))
5310 {
5311 new_segment = true;
5312 }
5313 else if (! writable
5314 && (hdr->flags & SEC_READONLY) == 0)
5315 {
5316 /* We don't want to put a writable section in a read only
5317 segment. */
5318 new_segment = true;
5319 }
5320 else
5321 {
5322 /* Otherwise, we can use the same segment. */
5323 new_segment = false;
5324 }
5325
5326 /* Allow interested parties a chance to override our decision. */
5327 if (last_hdr != NULL
5328 && info != NULL
5329 && info->callbacks->override_segment_assignment != NULL)
5330 new_segment
5331 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5332 last_hdr,
5333 new_segment);
5334
5335 if (! new_segment)
5336 {
5337 if ((hdr->flags & SEC_READONLY) == 0)
5338 writable = true;
5339 if ((hdr->flags & SEC_CODE) != 0)
5340 executable = true;
5341 last_hdr = hdr;
5342 /* .tbss sections effectively have zero size. */
5343 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5344 continue;
5345 }
5346
5347 /* We need a new program segment. We must create a new program
5348 header holding all the sections from hdr_index until hdr. */
5349
5350 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5351 if (m == NULL)
5352 goto error_return;
5353
5354 *pm = m;
5355 pm = &m->next;
5356
5357 if ((hdr->flags & SEC_READONLY) == 0)
5358 writable = true;
5359 else
5360 writable = false;
5361
5362 if ((hdr->flags & SEC_CODE) == 0)
5363 executable = false;
5364 else
5365 executable = true;
5366
5367 last_hdr = hdr;
5368 /* .tbss sections effectively have zero size. */
5369 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5370 hdr_index = i;
5371 phdr_in_segment = false;
5372 }
5373
5374 /* Create a final PT_LOAD program segment, but not if it's just
5375 for .tbss. */
5376 if (last_hdr != NULL
5377 && (i - hdr_index != 1
5378 || !IS_TBSS (last_hdr)))
5379 {
5380 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5381 if (m == NULL)
5382 goto error_return;
5383
5384 *pm = m;
5385 pm = &m->next;
5386 }
5387
5388 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5389 if (dynsec != NULL)
5390 {
5391 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5392 if (m == NULL)
5393 goto error_return;
5394 *pm = m;
5395 pm = &m->next;
5396 }
5397
5398 /* For each batch of consecutive loadable SHT_NOTE sections,
5399 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5400 because if we link together nonloadable .note sections and
5401 loadable .note sections, we will generate two .note sections
5402 in the output file. */
5403 for (s = abfd->sections; s != NULL; s = s->next)
5404 {
5405 if ((s->flags & SEC_LOAD) != 0
5406 && elf_section_type (s) == SHT_NOTE)
5407 {
5408 asection *s2;
5409 unsigned int alignment_power = s->alignment_power;
5410
5411 count = 1;
5412 for (s2 = s; s2->next != NULL; s2 = s2->next)
5413 {
5414 if (s2->next->alignment_power == alignment_power
5415 && (s2->next->flags & SEC_LOAD) != 0
5416 && elf_section_type (s2->next) == SHT_NOTE
5417 && align_power (s2->lma + s2->size / opb,
5418 alignment_power)
5419 == s2->next->lma)
5420 count++;
5421 else
5422 break;
5423 }
5424 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5425 amt += count * sizeof (asection *);
5426 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5427 if (m == NULL)
5428 goto error_return;
5429 m->next = NULL;
5430 m->p_type = PT_NOTE;
5431 m->count = count;
5432 while (count > 1)
5433 {
5434 m->sections[m->count - count--] = s;
5435 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5436 s = s->next;
5437 }
5438 m->sections[m->count - 1] = s;
5439 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5440 *pm = m;
5441 pm = &m->next;
5442 }
5443 if (s->flags & SEC_THREAD_LOCAL)
5444 {
5445 if (! tls_count)
5446 first_tls = s;
5447 tls_count++;
5448 }
5449 if (first_mbind == NULL
5450 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5451 first_mbind = s;
5452 }
5453
5454 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5455 if (tls_count > 0)
5456 {
5457 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5458 amt += tls_count * sizeof (asection *);
5459 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5460 if (m == NULL)
5461 goto error_return;
5462 m->next = NULL;
5463 m->p_type = PT_TLS;
5464 m->count = tls_count;
5465 /* Mandated PF_R. */
5466 m->p_flags = PF_R;
5467 m->p_flags_valid = 1;
5468 s = first_tls;
5469 for (i = 0; i < tls_count; ++i)
5470 {
5471 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5472 {
5473 _bfd_error_handler
5474 (_("%pB: TLS sections are not adjacent:"), abfd);
5475 s = first_tls;
5476 i = 0;
5477 while (i < tls_count)
5478 {
5479 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5480 {
5481 _bfd_error_handler (_(" TLS: %pA"), s);
5482 i++;
5483 }
5484 else
5485 _bfd_error_handler (_(" non-TLS: %pA"), s);
5486 s = s->next;
5487 }
5488 bfd_set_error (bfd_error_bad_value);
5489 goto error_return;
5490 }
5491 m->sections[i] = s;
5492 s = s->next;
5493 }
5494
5495 *pm = m;
5496 pm = &m->next;
5497 }
5498
5499 if (first_mbind
5500 && (abfd->flags & D_PAGED) != 0
5501 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5502 for (s = first_mbind; s != NULL; s = s->next)
5503 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5504 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5505 {
5506 /* Mandated PF_R. */
5507 unsigned long p_flags = PF_R;
5508 if ((s->flags & SEC_READONLY) == 0)
5509 p_flags |= PF_W;
5510 if ((s->flags & SEC_CODE) != 0)
5511 p_flags |= PF_X;
5512
5513 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5514 m = bfd_zalloc (abfd, amt);
5515 if (m == NULL)
5516 goto error_return;
5517 m->next = NULL;
5518 m->p_type = (PT_GNU_MBIND_LO
5519 + elf_section_data (s)->this_hdr.sh_info);
5520 m->count = 1;
5521 m->p_flags_valid = 1;
5522 m->sections[0] = s;
5523 m->p_flags = p_flags;
5524
5525 *pm = m;
5526 pm = &m->next;
5527 }
5528
5529 s = bfd_get_section_by_name (abfd,
5530 NOTE_GNU_PROPERTY_SECTION_NAME);
5531 if (s != NULL && s->size != 0)
5532 {
5533 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5534 m = bfd_zalloc (abfd, amt);
5535 if (m == NULL)
5536 goto error_return;
5537 m->next = NULL;
5538 m->p_type = PT_GNU_PROPERTY;
5539 m->count = 1;
5540 m->p_flags_valid = 1;
5541 m->sections[0] = s;
5542 m->p_flags = PF_R;
5543 *pm = m;
5544 pm = &m->next;
5545 }
5546
5547 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5548 segment. */
5549 eh_frame_hdr = elf_eh_frame_hdr (info);
5550 if (eh_frame_hdr != NULL
5551 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5552 {
5553 amt = sizeof (struct elf_segment_map);
5554 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5555 if (m == NULL)
5556 goto error_return;
5557 m->next = NULL;
5558 m->p_type = PT_GNU_EH_FRAME;
5559 m->count = 1;
5560 m->sections[0] = eh_frame_hdr->output_section;
5561
5562 *pm = m;
5563 pm = &m->next;
5564 }
5565
5566 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5567 segment. */
5568 sframe = elf_sframe (abfd);
5569 if (sframe != NULL
5570 && (sframe->output_section->flags & SEC_LOAD) != 0
5571 && sframe->size != 0)
5572 {
5573 amt = sizeof (struct elf_segment_map);
5574 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5575 if (m == NULL)
5576 goto error_return;
5577 m->next = NULL;
5578 m->p_type = PT_GNU_SFRAME;
5579 m->count = 1;
5580 m->sections[0] = sframe->output_section;
5581
5582 *pm = m;
5583 pm = &m->next;
5584 }
5585
5586 if (elf_stack_flags (abfd))
5587 {
5588 amt = sizeof (struct elf_segment_map);
5589 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5590 if (m == NULL)
5591 goto error_return;
5592 m->next = NULL;
5593 m->p_type = PT_GNU_STACK;
5594 m->p_flags = elf_stack_flags (abfd);
5595 m->p_align = bed->stack_align;
5596 m->p_flags_valid = 1;
5597 m->p_align_valid = m->p_align != 0;
5598 if (info->stacksize > 0)
5599 {
5600 m->p_size = info->stacksize;
5601 m->p_size_valid = 1;
5602 }
5603
5604 *pm = m;
5605 pm = &m->next;
5606 }
5607
5608 if (info != NULL && info->relro)
5609 {
5610 for (m = mfirst; m != NULL; m = m->next)
5611 {
5612 if (m->p_type == PT_LOAD
5613 && m->count != 0
5614 && m->sections[0]->vma >= info->relro_start
5615 && m->sections[0]->vma < info->relro_end)
5616 {
5617 i = m->count;
5618 while (--i != (unsigned) -1)
5619 {
5620 if (m->sections[i]->size > 0
5621 && (m->sections[i]->flags & SEC_LOAD) != 0
5622 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5623 break;
5624 }
5625
5626 if (i != (unsigned) -1)
5627 break;
5628 }
5629 }
5630
5631 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5632 if (m != NULL)
5633 {
5634 amt = sizeof (struct elf_segment_map);
5635 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5636 if (m == NULL)
5637 goto error_return;
5638 m->next = NULL;
5639 m->p_type = PT_GNU_RELRO;
5640 *pm = m;
5641 pm = &m->next;
5642 }
5643 }
5644
5645 free (sections);
5646 elf_seg_map (abfd) = mfirst;
5647 }
5648
5649 if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5650 return false;
5651
5652 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5653 ++count;
5654 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5655
5656 return true;
5657
5658 error_return:
5659 free (sections);
5660 return false;
5661 }
5662
5663 /* Sort sections by address. */
5664
5665 static int
5666 elf_sort_sections (const void *arg1, const void *arg2)
5667 {
5668 const asection *sec1 = *(const asection **) arg1;
5669 const asection *sec2 = *(const asection **) arg2;
5670 bfd_size_type size1, size2;
5671
5672 /* Sort by LMA first, since this is the address used to
5673 place the section into a segment. */
5674 if (sec1->lma < sec2->lma)
5675 return -1;
5676 else if (sec1->lma > sec2->lma)
5677 return 1;
5678
5679 /* Then sort by VMA. Normally the LMA and the VMA will be
5680 the same, and this will do nothing. */
5681 if (sec1->vma < sec2->vma)
5682 return -1;
5683 else if (sec1->vma > sec2->vma)
5684 return 1;
5685
5686 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5687
5688 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5689 && (x)->size != 0)
5690
5691 if (TOEND (sec1))
5692 {
5693 if (!TOEND (sec2))
5694 return 1;
5695 }
5696 else if (TOEND (sec2))
5697 return -1;
5698
5699 #undef TOEND
5700
5701 /* Sort by size, to put zero sized sections
5702 before others at the same address. */
5703
5704 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5705 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5706
5707 if (size1 < size2)
5708 return -1;
5709 if (size1 > size2)
5710 return 1;
5711
5712 return sec1->target_index - sec2->target_index;
5713 }
5714
5715 /* This qsort comparison functions sorts PT_LOAD segments first and
5716 by p_paddr, for assign_file_positions_for_load_sections. */
5717
5718 static int
5719 elf_sort_segments (const void *arg1, const void *arg2)
5720 {
5721 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5722 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5723
5724 if (m1->p_type != m2->p_type)
5725 {
5726 if (m1->p_type == PT_NULL)
5727 return 1;
5728 if (m2->p_type == PT_NULL)
5729 return -1;
5730 return m1->p_type < m2->p_type ? -1 : 1;
5731 }
5732 if (m1->includes_filehdr != m2->includes_filehdr)
5733 return m1->includes_filehdr ? -1 : 1;
5734 if (m1->no_sort_lma != m2->no_sort_lma)
5735 return m1->no_sort_lma ? -1 : 1;
5736 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5737 {
5738 bfd_vma lma1, lma2; /* Octets. */
5739 lma1 = 0;
5740 if (m1->p_paddr_valid)
5741 lma1 = m1->p_paddr;
5742 else if (m1->count != 0)
5743 {
5744 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5745 m1->sections[0]);
5746 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5747 }
5748 lma2 = 0;
5749 if (m2->p_paddr_valid)
5750 lma2 = m2->p_paddr;
5751 else if (m2->count != 0)
5752 {
5753 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5754 m2->sections[0]);
5755 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5756 }
5757 if (lma1 != lma2)
5758 return lma1 < lma2 ? -1 : 1;
5759 }
5760 if (m1->idx != m2->idx)
5761 return m1->idx < m2->idx ? -1 : 1;
5762 return 0;
5763 }
5764
5765 /* Ian Lance Taylor writes:
5766
5767 We shouldn't be using % with a negative signed number. That's just
5768 not good. We have to make sure either that the number is not
5769 negative, or that the number has an unsigned type. When the types
5770 are all the same size they wind up as unsigned. When file_ptr is a
5771 larger signed type, the arithmetic winds up as signed long long,
5772 which is wrong.
5773
5774 What we're trying to say here is something like ``increase OFF by
5775 the least amount that will cause it to be equal to the VMA modulo
5776 the page size.'' */
5777 /* In other words, something like:
5778
5779 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5780 off_offset = off % bed->maxpagesize;
5781 if (vma_offset < off_offset)
5782 adjustment = vma_offset + bed->maxpagesize - off_offset;
5783 else
5784 adjustment = vma_offset - off_offset;
5785
5786 which can be collapsed into the expression below. */
5787
5788 static file_ptr
5789 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5790 {
5791 /* PR binutils/16199: Handle an alignment of zero. */
5792 if (maxpagesize == 0)
5793 maxpagesize = 1;
5794 return ((vma - off) % maxpagesize);
5795 }
5796
5797 static void
5798 print_segment_map (const struct elf_segment_map *m)
5799 {
5800 unsigned int j;
5801 const char *pt = get_segment_type (m->p_type);
5802 char buf[32];
5803
5804 if (pt == NULL)
5805 {
5806 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5807 sprintf (buf, "LOPROC+%7.7x",
5808 (unsigned int) (m->p_type - PT_LOPROC));
5809 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5810 sprintf (buf, "LOOS+%7.7x",
5811 (unsigned int) (m->p_type - PT_LOOS));
5812 else
5813 snprintf (buf, sizeof (buf), "%8.8x",
5814 (unsigned int) m->p_type);
5815 pt = buf;
5816 }
5817 fflush (stdout);
5818 fprintf (stderr, "%s:", pt);
5819 for (j = 0; j < m->count; j++)
5820 fprintf (stderr, " %s", m->sections [j]->name);
5821 putc ('\n',stderr);
5822 fflush (stderr);
5823 }
5824
5825 /* Assign file positions to the sections based on the mapping from
5826 sections to segments. This function also sets up some fields in
5827 the file header. */
5828
5829 static bool
5830 assign_file_positions_for_load_sections (bfd *abfd,
5831 struct bfd_link_info *link_info)
5832 {
5833 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5834 struct elf_segment_map *m;
5835 struct elf_segment_map *phdr_load_seg;
5836 Elf_Internal_Phdr *phdrs;
5837 Elf_Internal_Phdr *p;
5838 file_ptr off; /* Octets. */
5839 bfd_size_type maxpagesize;
5840 unsigned int alloc, actual;
5841 unsigned int i, j;
5842 struct elf_segment_map **sorted_seg_map;
5843 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5844
5845 if (link_info == NULL
5846 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5847 return false;
5848
5849 alloc = 0;
5850 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5851 m->idx = alloc++;
5852
5853 if (alloc)
5854 {
5855 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5856 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5857 }
5858 else
5859 {
5860 /* PR binutils/12467. */
5861 elf_elfheader (abfd)->e_phoff = 0;
5862 elf_elfheader (abfd)->e_phentsize = 0;
5863 }
5864
5865 elf_elfheader (abfd)->e_phnum = alloc;
5866
5867 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5868 {
5869 actual = alloc;
5870 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5871 }
5872 else
5873 {
5874 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5875 BFD_ASSERT (elf_program_header_size (abfd)
5876 == actual * bed->s->sizeof_phdr);
5877 BFD_ASSERT (actual >= alloc);
5878 }
5879
5880 if (alloc == 0)
5881 {
5882 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5883 return true;
5884 }
5885
5886 /* We're writing the size in elf_program_header_size (abfd),
5887 see assign_file_positions_except_relocs, so make sure we have
5888 that amount allocated, with trailing space cleared.
5889 The variable alloc contains the computed need, while
5890 elf_program_header_size (abfd) contains the size used for the
5891 layout.
5892 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5893 where the layout is forced to according to a larger size in the
5894 last iterations for the testcase ld-elf/header. */
5895 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5896 + alloc * sizeof (*sorted_seg_map)));
5897 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5898 elf_tdata (abfd)->phdr = phdrs;
5899 if (phdrs == NULL)
5900 return false;
5901
5902 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5903 {
5904 sorted_seg_map[j] = m;
5905 /* If elf_segment_map is not from map_sections_to_segments, the
5906 sections may not be correctly ordered. NOTE: sorting should
5907 not be done to the PT_NOTE section of a corefile, which may
5908 contain several pseudo-sections artificially created by bfd.
5909 Sorting these pseudo-sections breaks things badly. */
5910 if (m->count > 1
5911 && !(elf_elfheader (abfd)->e_type == ET_CORE
5912 && m->p_type == PT_NOTE))
5913 {
5914 for (i = 0; i < m->count; i++)
5915 m->sections[i]->target_index = i;
5916 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5917 elf_sort_sections);
5918 }
5919 }
5920 if (alloc > 1)
5921 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5922 elf_sort_segments);
5923
5924 maxpagesize = 1;
5925 if ((abfd->flags & D_PAGED) != 0)
5926 {
5927 if (link_info != NULL)
5928 maxpagesize = link_info->maxpagesize;
5929 else
5930 maxpagesize = bed->maxpagesize;
5931 }
5932
5933 /* Sections must map to file offsets past the ELF file header. */
5934 off = bed->s->sizeof_ehdr;
5935 /* And if one of the PT_LOAD headers doesn't include the program
5936 headers then we'll be mapping program headers in the usual
5937 position after the ELF file header. */
5938 phdr_load_seg = NULL;
5939 for (j = 0; j < alloc; j++)
5940 {
5941 m = sorted_seg_map[j];
5942 if (m->p_type != PT_LOAD)
5943 break;
5944 if (m->includes_phdrs)
5945 {
5946 phdr_load_seg = m;
5947 break;
5948 }
5949 }
5950 if (phdr_load_seg == NULL)
5951 off += actual * bed->s->sizeof_phdr;
5952
5953 for (j = 0; j < alloc; j++)
5954 {
5955 asection **secpp;
5956 bfd_vma off_adjust; /* Octets. */
5957 bool no_contents;
5958 bfd_size_type align_pagesize;
5959
5960 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5961 number of sections with contents contributing to both p_filesz
5962 and p_memsz, followed by a number of sections with no contents
5963 that just contribute to p_memsz. In this loop, OFF tracks next
5964 available file offset for PT_LOAD and PT_NOTE segments. */
5965 m = sorted_seg_map[j];
5966 p = phdrs + m->idx;
5967 p->p_type = m->p_type;
5968 p->p_flags = m->p_flags;
5969
5970 if (m->count == 0)
5971 p->p_vaddr = m->p_vaddr_offset * opb;
5972 else
5973 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5974
5975 if (m->p_paddr_valid)
5976 p->p_paddr = m->p_paddr;
5977 else if (m->count == 0)
5978 p->p_paddr = 0;
5979 else
5980 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5981
5982 align_pagesize = 0;
5983 if (p->p_type == PT_LOAD
5984 && (abfd->flags & D_PAGED) != 0)
5985 {
5986 /* p_align in demand paged PT_LOAD segments effectively stores
5987 the maximum page size. When copying an executable with
5988 objcopy, we set m->p_align from the input file. Use this
5989 value for maxpagesize rather than bed->maxpagesize, which
5990 may be different. Note that we use maxpagesize for PT_TLS
5991 segment alignment later in this function, so we are relying
5992 on at least one PT_LOAD segment appearing before a PT_TLS
5993 segment. */
5994 if (m->p_align_valid)
5995 maxpagesize = m->p_align;
5996 else if (bed->p_align != 0
5997 && (link_info == NULL
5998 || !link_info->maxpagesize_is_set))
5999 /* We will lay out this binary using maxpagesize but set
6000 p->p_align later to the possibly smaller bed->p_align.
6001 The run-time loader will then be able to load this
6002 binary when the system page size is maxpagesize, but if
6003 the system page size is smaller can use p->p_align.
6004 In either case p->p_align will be increased if
6005 necessary to match section alignment. */
6006 align_pagesize = bed->p_align;
6007
6008 p->p_align = maxpagesize;
6009 }
6010 else if (m->p_align_valid)
6011 p->p_align = m->p_align;
6012 else if (m->count == 0)
6013 p->p_align = 1 << bed->s->log_file_align;
6014
6015 if (m == phdr_load_seg)
6016 off += actual * bed->s->sizeof_phdr;
6017
6018 no_contents = false;
6019 off_adjust = 0;
6020 if (p->p_type == PT_LOAD
6021 && m->count > 0)
6022 {
6023 bfd_size_type align; /* Bytes. */
6024 unsigned int align_power = 0;
6025
6026 if (m->p_align_valid)
6027 align = p->p_align;
6028 else
6029 {
6030 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6031 {
6032 unsigned int secalign;
6033
6034 secalign = bfd_section_alignment (*secpp);
6035 if (secalign > align_power)
6036 align_power = secalign;
6037 }
6038 align = (bfd_size_type) 1 << align_power;
6039 /* If a section requires alignment higher than the
6040 minimum p_align value, don't reduce a maxpagesize
6041 p->p_align set earlier in this function. */
6042 if (align > bed->p_align)
6043 align_pagesize = 0;
6044 if (align < maxpagesize)
6045 align = maxpagesize;
6046 else
6047 {
6048 /* If a section requires alignment higher than the
6049 maximum page size, set p_align to the section
6050 alignment. */
6051 if ((abfd->flags & D_PAGED) != 0)
6052 p->p_align = align;
6053 }
6054 }
6055
6056 for (i = 0; i < m->count; i++)
6057 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6058 /* If we aren't making room for this section, then
6059 it must be SHT_NOBITS regardless of what we've
6060 set via struct bfd_elf_special_section. */
6061 elf_section_type (m->sections[i]) = SHT_NOBITS;
6062
6063 /* Find out whether this segment contains any loadable
6064 sections. */
6065 no_contents = true;
6066 for (i = 0; i < m->count; i++)
6067 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6068 {
6069 no_contents = false;
6070 break;
6071 }
6072
6073 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6074
6075 /* Broken hardware and/or kernel require that files do not
6076 map the same page with different permissions on some hppa
6077 processors. */
6078 if (j != 0
6079 && (abfd->flags & D_PAGED) != 0
6080 && bed->no_page_alias
6081 && (off & (maxpagesize - 1)) != 0
6082 && ((off & -maxpagesize)
6083 == ((off + off_adjust) & -maxpagesize)))
6084 off_adjust += maxpagesize;
6085 off += off_adjust;
6086 if (no_contents)
6087 {
6088 /* We shouldn't need to align the segment on disk since
6089 the segment doesn't need file space, but the gABI
6090 arguably requires the alignment and glibc ld.so
6091 checks it. So to comply with the alignment
6092 requirement but not waste file space, we adjust
6093 p_offset for just this segment. (OFF_ADJUST is
6094 subtracted from OFF later.) This may put p_offset
6095 past the end of file, but that shouldn't matter. */
6096 }
6097 else
6098 off_adjust = 0;
6099 }
6100 /* Make sure the .dynamic section is the first section in the
6101 PT_DYNAMIC segment. */
6102 else if (p->p_type == PT_DYNAMIC
6103 && m->count > 1
6104 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6105 {
6106 _bfd_error_handler
6107 (_("%pB: The first section in the PT_DYNAMIC segment"
6108 " is not the .dynamic section"),
6109 abfd);
6110 bfd_set_error (bfd_error_bad_value);
6111 return false;
6112 }
6113 /* Set the note section type to SHT_NOTE. */
6114 else if (p->p_type == PT_NOTE)
6115 for (i = 0; i < m->count; i++)
6116 elf_section_type (m->sections[i]) = SHT_NOTE;
6117
6118 if (m->includes_filehdr)
6119 {
6120 if (!m->p_flags_valid)
6121 p->p_flags |= PF_R;
6122 p->p_filesz = bed->s->sizeof_ehdr;
6123 p->p_memsz = bed->s->sizeof_ehdr;
6124 if (p->p_type == PT_LOAD)
6125 {
6126 if (m->count > 0)
6127 {
6128 if (p->p_vaddr < (bfd_vma) off
6129 || (!m->p_paddr_valid
6130 && p->p_paddr < (bfd_vma) off))
6131 {
6132 _bfd_error_handler
6133 (_("%pB: not enough room for program headers,"
6134 " try linking with -N"),
6135 abfd);
6136 bfd_set_error (bfd_error_bad_value);
6137 return false;
6138 }
6139 p->p_vaddr -= off;
6140 if (!m->p_paddr_valid)
6141 p->p_paddr -= off;
6142 }
6143 }
6144 else if (sorted_seg_map[0]->includes_filehdr)
6145 {
6146 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6147 p->p_vaddr = filehdr->p_vaddr;
6148 if (!m->p_paddr_valid)
6149 p->p_paddr = filehdr->p_paddr;
6150 }
6151 }
6152
6153 if (m->includes_phdrs)
6154 {
6155 if (!m->p_flags_valid)
6156 p->p_flags |= PF_R;
6157 p->p_filesz += actual * bed->s->sizeof_phdr;
6158 p->p_memsz += actual * bed->s->sizeof_phdr;
6159 if (!m->includes_filehdr)
6160 {
6161 if (p->p_type == PT_LOAD)
6162 {
6163 p->p_offset = off - actual * bed->s->sizeof_phdr;
6164 elf_elfheader (abfd)->e_phoff = p->p_offset;
6165 if (m->count > 0)
6166 {
6167 p->p_vaddr -= off - p->p_offset;
6168 if (!m->p_paddr_valid)
6169 p->p_paddr -= off - p->p_offset;
6170 }
6171 }
6172 else if (phdr_load_seg != NULL)
6173 {
6174 /* Also set PT_PHDR to match phdr_load_seg. We've
6175 sorted segments so that phdr_load_seg will
6176 already be set by the code immediately above. */
6177 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6178 bfd_vma phdr_off = 0; /* Octets. */
6179 if (phdr_load_seg->includes_filehdr)
6180 phdr_off = bed->s->sizeof_ehdr;
6181 p->p_vaddr = phdr->p_vaddr + phdr_off;
6182 if (!m->p_paddr_valid)
6183 p->p_paddr = phdr->p_paddr + phdr_off;
6184 p->p_offset = phdr->p_offset + phdr_off;
6185 }
6186 else
6187 p->p_offset = bed->s->sizeof_ehdr;
6188 }
6189 }
6190
6191 if (p->p_type == PT_LOAD
6192 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6193 {
6194 if (!m->includes_filehdr && !m->includes_phdrs)
6195 {
6196 p->p_offset = off;
6197 if (no_contents)
6198 {
6199 /* Put meaningless p_offset for PT_LOAD segments
6200 without file contents somewhere within the first
6201 page, in an attempt to not point past EOF. */
6202 bfd_size_type align = maxpagesize;
6203 if (align < p->p_align)
6204 align = p->p_align;
6205 if (align < 1)
6206 align = 1;
6207 /* Avoid p_offset of zero, which might be wrongly
6208 interpreted as the segment being the first one,
6209 containing the file header. PR32763. */
6210 p->p_offset = (off + align - 1) % align + 1;
6211 }
6212 }
6213 else
6214 {
6215 file_ptr adjust; /* Octets. */
6216
6217 adjust = off - (p->p_offset + p->p_filesz);
6218 if (!no_contents)
6219 p->p_filesz += adjust;
6220 p->p_memsz += adjust;
6221 }
6222 }
6223
6224 if (align_pagesize)
6225 p->p_align = align_pagesize;
6226
6227 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6228 maps. Set filepos for sections in PT_LOAD segments, and in
6229 core files, for sections in PT_NOTE segments.
6230 assign_file_positions_for_non_load_sections will set filepos
6231 for other sections and update p_filesz for other segments. */
6232 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6233 {
6234 asection *sec;
6235 bfd_size_type align;
6236 Elf_Internal_Shdr *this_hdr;
6237
6238 sec = *secpp;
6239 this_hdr = &elf_section_data (sec)->this_hdr;
6240 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6241
6242 if ((p->p_type == PT_LOAD
6243 || p->p_type == PT_TLS)
6244 && (this_hdr->sh_type != SHT_NOBITS
6245 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6246 && ((this_hdr->sh_flags & SHF_TLS) == 0
6247 || p->p_type == PT_TLS))))
6248 {
6249 bfd_vma p_start = p->p_paddr; /* Octets. */
6250 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6251 bfd_vma s_start = sec->lma * opb; /* Octets. */
6252 bfd_vma adjust = s_start - p_end; /* Octets. */
6253
6254 if (adjust != 0
6255 && (s_start < p_end
6256 || p_end < p_start))
6257 {
6258 _bfd_error_handler
6259 /* xgettext:c-format */
6260 (_("%pB: section %pA lma %#" PRIx64
6261 " adjusted to %#" PRIx64),
6262 abfd, sec, (uint64_t) s_start / opb,
6263 (uint64_t) p_end / opb);
6264 adjust = 0;
6265 sec->lma = p_end / opb;
6266 }
6267 p->p_memsz += adjust;
6268
6269 if (p->p_type == PT_LOAD)
6270 {
6271 if (this_hdr->sh_type != SHT_NOBITS)
6272 {
6273 off_adjust = 0;
6274 if (p->p_filesz + adjust < p->p_memsz)
6275 {
6276 /* We have a PROGBITS section following NOBITS ones.
6277 Allocate file space for the NOBITS section(s).
6278 We don't need to write out the zeros, posix
6279 fseek past the end of data already written
6280 followed by a write at that location is
6281 guaranteed to result in zeros being read
6282 from the gap. */
6283 adjust = p->p_memsz - p->p_filesz;
6284 }
6285 }
6286 /* We only adjust sh_offset in SHT_NOBITS sections
6287 as would seem proper for their address when the
6288 section is first in the segment. sh_offset
6289 doesn't really have any significance for
6290 SHT_NOBITS anyway, apart from a notional position
6291 relative to other sections. Historically we
6292 didn't bother with adjusting sh_offset and some
6293 programs depend on it not being adjusted. See
6294 pr12921 and pr25662. */
6295 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6296 {
6297 off += adjust;
6298 if (this_hdr->sh_type == SHT_NOBITS)
6299 off_adjust += adjust;
6300 }
6301 }
6302 if (this_hdr->sh_type != SHT_NOBITS)
6303 p->p_filesz += adjust;
6304 }
6305
6306 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6307 {
6308 /* The section at i == 0 is the one that actually contains
6309 everything. */
6310 if (i == 0)
6311 {
6312 this_hdr->sh_offset = sec->filepos = off;
6313 off += this_hdr->sh_size;
6314 p->p_filesz = this_hdr->sh_size;
6315 p->p_memsz = 0;
6316 p->p_align = 1;
6317 }
6318 else
6319 {
6320 /* The rest are fake sections that shouldn't be written. */
6321 sec->filepos = 0;
6322 sec->size = 0;
6323 sec->flags = 0;
6324 continue;
6325 }
6326 }
6327 else
6328 {
6329 if (this_hdr->sh_type == SHT_NOBITS
6330 && (this_hdr->sh_flags & SHF_TLS) != 0
6331 && this_hdr->sh_offset == 0)
6332 {
6333 /* Set sh_offset for .tbss sections to their nominal
6334 offset after aligning. They are not loaded from
6335 disk so the value doesn't really matter, except
6336 when the .tbss section is the first one in a
6337 PT_TLS segment. In that case it sets the
6338 p_offset for the PT_TLS segment, which according
6339 to the ELF gABI ought to satisfy
6340 p_offset % p_align == p_vaddr % p_align. */
6341 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6342 off, align);
6343 this_hdr->sh_offset = sec->filepos = off + adjust;
6344 }
6345 else if (p->p_type == PT_LOAD)
6346 {
6347 this_hdr->sh_offset = sec->filepos = off;
6348 if (this_hdr->sh_type != SHT_NOBITS)
6349 off += this_hdr->sh_size;
6350 }
6351
6352 if (this_hdr->sh_type != SHT_NOBITS)
6353 {
6354 p->p_filesz += this_hdr->sh_size;
6355 /* A load section without SHF_ALLOC is something like
6356 a note section in a PT_NOTE segment. These take
6357 file space but are not loaded into memory. */
6358 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6359 p->p_memsz += this_hdr->sh_size;
6360 }
6361 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6362 {
6363 if (p->p_type == PT_TLS)
6364 p->p_memsz += this_hdr->sh_size;
6365
6366 /* .tbss is special. It doesn't contribute to p_memsz of
6367 normal segments. */
6368 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6369 p->p_memsz += this_hdr->sh_size;
6370 }
6371
6372 if (align > p->p_align
6373 && !m->p_align_valid
6374 && (p->p_type != PT_LOAD
6375 || (abfd->flags & D_PAGED) == 0))
6376 p->p_align = align;
6377 }
6378
6379 if (!m->p_flags_valid)
6380 {
6381 p->p_flags |= PF_R;
6382 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6383 p->p_flags |= PF_X;
6384 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6385 p->p_flags |= PF_W;
6386 }
6387 }
6388
6389 off -= off_adjust;
6390
6391 /* PR ld/20815 - Check that the program header segment, if
6392 present, will be loaded into memory. */
6393 if (p->p_type == PT_PHDR
6394 && phdr_load_seg == NULL
6395 && !(bed->elf_backend_allow_non_load_phdr != NULL
6396 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6397 {
6398 /* The fix for this error is usually to edit the linker script being
6399 used and set up the program headers manually. Either that or
6400 leave room for the headers at the start of the SECTIONS. */
6401 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6402 " by LOAD segment"),
6403 abfd);
6404 if (link_info == NULL)
6405 return false;
6406 /* Arrange for the linker to exit with an error, deleting
6407 the output file unless --noinhibit-exec is given. */
6408 link_info->callbacks->info ("%X");
6409 }
6410
6411 /* Check that all sections are in a PT_LOAD segment.
6412 Don't check funky gdb generated core files. */
6413 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6414 {
6415 bool check_vma = true;
6416
6417 for (i = 1; i < m->count; i++)
6418 if (m->sections[i]->vma == m->sections[i - 1]->vma
6419 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6420 ->this_hdr), p) != 0
6421 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6422 ->this_hdr), p) != 0)
6423 {
6424 /* Looks like we have overlays packed into the segment. */
6425 check_vma = false;
6426 break;
6427 }
6428
6429 for (i = 0; i < m->count; i++)
6430 {
6431 Elf_Internal_Shdr *this_hdr;
6432 asection *sec;
6433
6434 sec = m->sections[i];
6435 this_hdr = &(elf_section_data(sec)->this_hdr);
6436 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6437 && !ELF_TBSS_SPECIAL (this_hdr, p))
6438 {
6439 _bfd_error_handler
6440 /* xgettext:c-format */
6441 (_("%pB: section `%pA' can't be allocated in segment %u"),
6442 abfd, sec, m->idx);
6443 print_segment_map (m);
6444 }
6445 }
6446 }
6447 }
6448
6449 elf_next_file_pos (abfd) = off;
6450
6451 if (link_info != NULL
6452 && phdr_load_seg != NULL
6453 && phdr_load_seg->includes_filehdr)
6454 {
6455 /* There is a segment that contains both the file headers and the
6456 program headers, so provide a symbol __ehdr_start pointing there.
6457 A program can use this to examine itself robustly. */
6458
6459 struct elf_link_hash_table *htab = elf_hash_table (link_info);
6460 struct elf_link_hash_entry *hash = htab->hehdr_start;
6461
6462 /* If the symbol was referenced and not defined, define it. */
6463 if (hash != NULL
6464 && (hash->root.type == bfd_link_hash_new
6465 || hash->root.type == bfd_link_hash_undefined
6466 || hash->root.type == bfd_link_hash_undefweak
6467 || hash->root.type == bfd_link_hash_common))
6468 {
6469 asection *s = NULL;
6470 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6471
6472 if (phdr_load_seg->count != 0)
6473 /* The segment contains sections, so use the first one. */
6474 s = phdr_load_seg->sections[0];
6475 else
6476 /* Use the first (i.e. lowest-addressed) section in any segment. */
6477 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6478 if (m->p_type == PT_LOAD && m->count != 0)
6479 {
6480 s = m->sections[0];
6481 break;
6482 }
6483
6484 if (s != NULL)
6485 {
6486 hash->root.u.def.value = filehdr_vaddr - s->vma;
6487 hash->root.u.def.section = s;
6488 }
6489 else
6490 {
6491 hash->root.u.def.value = filehdr_vaddr;
6492 hash->root.u.def.section = bfd_abs_section_ptr;
6493 }
6494
6495 hash->root.type = bfd_link_hash_defined;
6496 hash->def_regular = 1;
6497 hash->non_elf = 0;
6498 }
6499 }
6500
6501 return true;
6502 }
6503
6504 /* Determine if a bfd is a debuginfo file. Unfortunately there
6505 is no defined method for detecting such files, so we have to
6506 use heuristics instead. */
6507
6508 bool
6509 is_debuginfo_file (bfd *abfd)
6510 {
6511 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6512 return false;
6513
6514 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6515 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6516 Elf_Internal_Shdr **headerp;
6517
6518 for (headerp = start_headers; headerp < end_headers; headerp ++)
6519 {
6520 Elf_Internal_Shdr *header = * headerp;
6521
6522 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6523 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6524 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6525 && header->sh_type != SHT_NOBITS
6526 && header->sh_type != SHT_NOTE)
6527 return false;
6528 }
6529
6530 return true;
6531 }
6532
6533 /* Assign file positions for other sections, except for compressed debug
6534 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6535
6536 static bool
6537 assign_file_positions_for_non_load_sections (bfd *abfd,
6538 struct bfd_link_info *link_info)
6539 {
6540 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6541 Elf_Internal_Shdr **i_shdrpp;
6542 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6543 Elf_Internal_Phdr *phdrs;
6544 Elf_Internal_Phdr *p;
6545 struct elf_segment_map *m;
6546 file_ptr off;
6547 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6548 bfd_vma maxpagesize;
6549
6550 if (link_info != NULL)
6551 maxpagesize = link_info->maxpagesize;
6552 else
6553 maxpagesize = bed->maxpagesize;
6554 i_shdrpp = elf_elfsections (abfd);
6555 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6556 off = elf_next_file_pos (abfd);
6557 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6558 {
6559 Elf_Internal_Shdr *hdr;
6560 bfd_vma align;
6561
6562 hdr = *hdrpp;
6563 if (hdr->bfd_section != NULL
6564 && (hdr->bfd_section->filepos != 0
6565 || (hdr->sh_type == SHT_NOBITS
6566 && hdr->contents == NULL)))
6567 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6568 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6569 {
6570 if (hdr->sh_size != 0
6571 /* PR 24717 - debuginfo files are known to be not strictly
6572 compliant with the ELF standard. In particular they often
6573 have .note.gnu.property sections that are outside of any
6574 loadable segment. This is not a problem for such files,
6575 so do not warn about them. */
6576 && ! is_debuginfo_file (abfd))
6577 _bfd_error_handler
6578 /* xgettext:c-format */
6579 (_("%pB: warning: allocated section `%s' not in segment"),
6580 abfd,
6581 (hdr->bfd_section == NULL
6582 ? "*unknown*"
6583 : hdr->bfd_section->name));
6584 /* We don't need to page align empty sections. */
6585 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6586 align = maxpagesize;
6587 else
6588 align = hdr->sh_addralign & -hdr->sh_addralign;
6589 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6590 off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6591 bed->s->log_file_align);
6592 }
6593 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6594 && hdr->bfd_section == NULL)
6595 /* We don't know the offset of these sections yet:
6596 their size has not been decided. */
6597 || (abfd->is_linker_output
6598 && hdr->bfd_section != NULL
6599 && (hdr->sh_name == -1u
6600 || bfd_section_is_ctf (hdr->bfd_section)))
6601 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6602 || (elf_symtab_shndx_list (abfd) != NULL
6603 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6604 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6605 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6606 hdr->sh_offset = -1;
6607 else
6608 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6609 }
6610 elf_next_file_pos (abfd) = off;
6611
6612 /* Now that we have set the section file positions, we can set up
6613 the file positions for the non PT_LOAD segments. */
6614 phdrs = elf_tdata (abfd)->phdr;
6615 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6616 {
6617 if (p->p_type == PT_GNU_RELRO)
6618 {
6619 bfd_vma start, end; /* Bytes. */
6620 bool ok;
6621
6622 if (link_info != NULL)
6623 {
6624 /* During linking the range of the RELRO segment is passed
6625 in link_info. Note that there may be padding between
6626 relro_start and the first RELRO section. */
6627 start = link_info->relro_start;
6628 end = link_info->relro_end;
6629 }
6630 else if (m->count != 0)
6631 {
6632 if (!m->p_size_valid)
6633 abort ();
6634 start = m->sections[0]->vma;
6635 end = start + m->p_size / opb;
6636 }
6637 else
6638 {
6639 start = 0;
6640 end = 0;
6641 }
6642
6643 ok = false;
6644 if (start < end)
6645 {
6646 struct elf_segment_map *lm;
6647 const Elf_Internal_Phdr *lp;
6648 unsigned int i;
6649
6650 /* Find a LOAD segment containing a section in the RELRO
6651 segment. */
6652 for (lm = elf_seg_map (abfd), lp = phdrs;
6653 lm != NULL;
6654 lm = lm->next, lp++)
6655 {
6656 if (lp->p_type == PT_LOAD
6657 && lm->count != 0
6658 && (lm->sections[lm->count - 1]->vma
6659 + (!IS_TBSS (lm->sections[lm->count - 1])
6660 ? lm->sections[lm->count - 1]->size / opb
6661 : 0)) > start
6662 && lm->sections[0]->vma < end)
6663 break;
6664 }
6665
6666 if (lm != NULL)
6667 {
6668 /* Find the section starting the RELRO segment. */
6669 for (i = 0; i < lm->count; i++)
6670 {
6671 asection *s = lm->sections[i];
6672 if (s->vma >= start
6673 && s->vma < end
6674 && s->size != 0)
6675 break;
6676 }
6677
6678 if (i < lm->count)
6679 {
6680 p->p_vaddr = lm->sections[i]->vma * opb;
6681 p->p_paddr = lm->sections[i]->lma * opb;
6682 p->p_offset = lm->sections[i]->filepos;
6683 p->p_memsz = end * opb - p->p_vaddr;
6684 p->p_filesz = p->p_memsz;
6685
6686 /* The RELRO segment typically ends a few bytes
6687 into .got.plt but other layouts are possible.
6688 In cases where the end does not match any
6689 loaded section (for instance is in file
6690 padding), trim p_filesz back to correspond to
6691 the end of loaded section contents. */
6692 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6693 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6694
6695 /* Preserve the alignment and flags if they are
6696 valid. The gold linker generates RW/4 for
6697 the PT_GNU_RELRO section. It is better for
6698 objcopy/strip to honor these attributes
6699 otherwise gdb will choke when using separate
6700 debug files. */
6701 if (!m->p_align_valid)
6702 p->p_align = 1;
6703 if (!m->p_flags_valid)
6704 p->p_flags = PF_R;
6705 ok = true;
6706 }
6707 }
6708 }
6709
6710 if (!ok)
6711 {
6712 if (link_info != NULL)
6713 _bfd_error_handler
6714 (_("%pB: warning: unable to allocate any sections"
6715 " to PT_GNU_RELRO segment"),
6716 abfd);
6717 memset (p, 0, sizeof *p);
6718 }
6719 }
6720 else if (p->p_type == PT_GNU_STACK)
6721 {
6722 if (m->p_size_valid)
6723 p->p_memsz = m->p_size;
6724 }
6725 else if (m->count != 0)
6726 {
6727 unsigned int i;
6728
6729 if (p->p_type != PT_LOAD
6730 && (p->p_type != PT_NOTE
6731 || bfd_get_format (abfd) != bfd_core))
6732 {
6733 /* A user specified segment layout may include a PHDR
6734 segment that overlaps with a LOAD segment... */
6735 if (p->p_type == PT_PHDR)
6736 {
6737 m->count = 0;
6738 continue;
6739 }
6740
6741 if (m->includes_filehdr || m->includes_phdrs)
6742 {
6743 /* PR 17512: file: 2195325e. */
6744 _bfd_error_handler
6745 (_("%pB: error: non-load segment %d includes file header "
6746 "and/or program header"),
6747 abfd, (int) (p - phdrs));
6748 return false;
6749 }
6750
6751 p->p_filesz = 0;
6752 p->p_offset = m->sections[0]->filepos;
6753 for (i = m->count; i-- != 0;)
6754 {
6755 asection *sect = m->sections[i];
6756 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6757 if (hdr->sh_type != SHT_NOBITS)
6758 {
6759 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6760 /* NB: p_memsz of the loadable PT_NOTE segment
6761 should be the same as p_filesz. */
6762 if (p->p_type == PT_NOTE
6763 && (hdr->sh_flags & SHF_ALLOC) != 0)
6764 p->p_memsz = p->p_filesz;
6765 break;
6766 }
6767 }
6768 }
6769 }
6770 }
6771
6772 return true;
6773 }
6774
6775 static elf_section_list *
6776 find_section_in_list (unsigned int i, elf_section_list * list)
6777 {
6778 for (;list != NULL; list = list->next)
6779 if (list->ndx == i)
6780 break;
6781 return list;
6782 }
6783
6784 /* Work out the file positions of all the sections. This is called by
6785 _bfd_elf_compute_section_file_positions. All the section sizes and
6786 VMAs must be known before this is called.
6787
6788 Reloc sections come in two flavours: Those processed specially as
6789 "side-channel" data attached to a section to which they apply, and
6790 those that bfd doesn't process as relocations. The latter sort are
6791 stored in a normal bfd section by bfd_section_from_shdr. We don't
6792 consider the former sort here, unless they form part of the loadable
6793 image. Reloc sections not assigned here (and compressed debugging
6794 sections and CTF sections which nothing else in the file can rely
6795 upon) will be handled later by assign_file_positions_for_relocs.
6796
6797 We also don't set the positions of the .symtab and .strtab here. */
6798
6799 static bool
6800 assign_file_positions_except_relocs (bfd *abfd,
6801 struct bfd_link_info *link_info)
6802 {
6803 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6804 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6806 unsigned int alloc;
6807
6808 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6809 && bfd_get_format (abfd) != bfd_core)
6810 {
6811 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6812 unsigned int num_sec = elf_numsections (abfd);
6813 Elf_Internal_Shdr **hdrpp;
6814 unsigned int i;
6815 file_ptr off;
6816
6817 /* Start after the ELF header. */
6818 off = i_ehdrp->e_ehsize;
6819
6820 /* We are not creating an executable, which means that we are
6821 not creating a program header, and that the actual order of
6822 the sections in the file is unimportant. */
6823 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6824 {
6825 Elf_Internal_Shdr *hdr;
6826
6827 hdr = *hdrpp;
6828 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6829 && hdr->bfd_section == NULL)
6830 /* Do not assign offsets for these sections yet: we don't know
6831 their sizes. */
6832 || (abfd->is_linker_output
6833 && hdr->bfd_section != NULL
6834 && (hdr->sh_name == -1u
6835 || bfd_section_is_ctf (hdr->bfd_section)))
6836 || i == elf_onesymtab (abfd)
6837 || (elf_symtab_shndx_list (abfd) != NULL
6838 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6839 || i == elf_strtab_sec (abfd)
6840 || i == elf_shstrtab_sec (abfd))
6841 {
6842 hdr->sh_offset = -1;
6843 }
6844 else
6845 /* There shouldn't be a need to effect "capped" file alignment here,
6846 yet at least the Linux kernel's modpost utility was found to be
6847 unhappy without. While the issue was addressed there, let's be
6848 kind for at least the foreseeable future ... */
6849 off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6850 bed->s->log_file_align);
6851 }
6852
6853 elf_next_file_pos (abfd) = off;
6854 elf_program_header_size (abfd) = 0;
6855 }
6856 else
6857 {
6858 /* Assign file positions for the loaded sections based on the
6859 assignment of sections to segments. */
6860 if (!assign_file_positions_for_load_sections (abfd, link_info))
6861 return false;
6862
6863 /* And for non-load sections. */
6864 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6865 return false;
6866 }
6867
6868 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6869 return false;
6870
6871 /* Write out the program headers. */
6872 alloc = i_ehdrp->e_phnum;
6873 if (alloc != 0)
6874 {
6875 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6876 {
6877 bool warned_tls = false;
6878 bool warned_rwx = false;
6879
6880 /* Memory resident segments with non-zero size and RWX
6881 permissions are a security risk, so we generate a warning
6882 here if we are creating any. */
6883 unsigned int i;
6884
6885 for (i = 0; i < alloc; i++)
6886 {
6887 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6888
6889 if (phdr->p_memsz == 0)
6890 continue;
6891
6892 if (! warned_tls
6893 && phdr->p_type == PT_TLS
6894 && (phdr->p_flags & PF_X))
6895 {
6896 if (link_info->warn_is_error_for_rwx_segments)
6897 {
6898 _bfd_error_handler (_("\
6899 error: %pB has a TLS segment with execute permission"),
6900 abfd);
6901 return false;
6902 }
6903
6904 _bfd_error_handler (_("\
6905 warning: %pB has a TLS segment with execute permission"),
6906 abfd);
6907 if (warned_rwx)
6908 break;
6909
6910 warned_tls = true;
6911 }
6912 else if (! warned_rwx
6913 && phdr->p_type == PT_LOAD
6914 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6915 == (PF_R | PF_W | PF_X)))
6916 {
6917 if (link_info->warn_is_error_for_rwx_segments)
6918 {
6919 _bfd_error_handler (_("\
6920 error: %pB has a LOAD segment with RWX permissions"),
6921 abfd);
6922 return false;
6923 }
6924
6925 _bfd_error_handler (_("\
6926 warning: %pB has a LOAD segment with RWX permissions"),
6927 abfd);
6928 if (warned_tls)
6929 break;
6930
6931 warned_rwx = true;
6932 }
6933 }
6934 }
6935
6936 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6937 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6938 return false;
6939 }
6940
6941 return true;
6942 }
6943
6944 bool
6945 _bfd_elf_init_file_header (bfd *abfd,
6946 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6947 {
6948 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6949 struct elf_strtab_hash *shstrtab;
6950 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6951
6952 i_ehdrp = elf_elfheader (abfd);
6953
6954 shstrtab = _bfd_elf_strtab_init ();
6955 if (shstrtab == NULL)
6956 return false;
6957
6958 elf_shstrtab (abfd) = shstrtab;
6959
6960 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6961 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6962 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6963 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6964
6965 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6966 i_ehdrp->e_ident[EI_DATA] =
6967 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6968 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6969
6970 if ((abfd->flags & DYNAMIC) != 0)
6971 i_ehdrp->e_type = ET_DYN;
6972 else if ((abfd->flags & EXEC_P) != 0)
6973 i_ehdrp->e_type = ET_EXEC;
6974 else if (bfd_get_format (abfd) == bfd_core)
6975 i_ehdrp->e_type = ET_CORE;
6976 else
6977 i_ehdrp->e_type = ET_REL;
6978
6979 switch (bfd_get_arch (abfd))
6980 {
6981 case bfd_arch_unknown:
6982 i_ehdrp->e_machine = EM_NONE;
6983 break;
6984
6985 /* There used to be a long list of cases here, each one setting
6986 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6987 in the corresponding bfd definition. To avoid duplication,
6988 the switch was removed. Machines that need special handling
6989 can generally do it in elf_backend_final_write_processing(),
6990 unless they need the information earlier than the final write.
6991 Such need can generally be supplied by replacing the tests for
6992 e_machine with the conditions used to determine it. */
6993 default:
6994 i_ehdrp->e_machine = bed->elf_machine_code;
6995 }
6996
6997 i_ehdrp->e_version = bed->s->ev_current;
6998 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6999
7000 /* No program header, for now. */
7001 i_ehdrp->e_phoff = 0;
7002 i_ehdrp->e_phentsize = 0;
7003 i_ehdrp->e_phnum = 0;
7004
7005 /* Each bfd section is section header entry. */
7006 i_ehdrp->e_entry = bfd_get_start_address (abfd);
7007 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7008
7009 elf_tdata (abfd)->symtab_hdr.sh_name =
7010 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7011 elf_tdata (abfd)->strtab_hdr.sh_name =
7012 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7013 elf_tdata (abfd)->shstrtab_hdr.sh_name =
7014 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7015 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7016 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7017 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7018 return false;
7019
7020 return true;
7021 }
7022
7023 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7024
7025 FIXME: We used to have code here to sort the PT_LOAD segments into
7026 ascending order, as per the ELF spec. But this breaks some programs,
7027 including the Linux kernel. But really either the spec should be
7028 changed or the programs updated. */
7029
7030 bool
7031 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7032 {
7033 if (link_info != NULL && bfd_link_pie (link_info))
7034 {
7035 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7036 unsigned int num_segments = i_ehdrp->e_phnum;
7037 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7038 Elf_Internal_Phdr *segment = tdata->phdr;
7039 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7040
7041 /* Find the lowest p_vaddr in PT_LOAD segments. */
7042 bfd_vma p_vaddr = (bfd_vma) -1;
7043 for (; segment < end_segment; segment++)
7044 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7045 p_vaddr = segment->p_vaddr;
7046
7047 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7048 segments is non-zero. */
7049 if (p_vaddr)
7050 i_ehdrp->e_type = ET_EXEC;
7051 }
7052 return true;
7053 }
7054
7055 /* Assign file positions for all the reloc sections which are not part
7056 of the loadable file image, and the file position of section headers. */
7057
7058 static bool
7059 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7060 {
7061 file_ptr off;
7062 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7063 Elf_Internal_Shdr *shdrp;
7064 Elf_Internal_Ehdr *i_ehdrp;
7065 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7066
7067 /* Skip non-load sections without section header. */
7068 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7069 return true;
7070
7071 off = elf_next_file_pos (abfd);
7072
7073 shdrpp = elf_elfsections (abfd);
7074 end_shdrpp = shdrpp + elf_numsections (abfd);
7075 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7076 {
7077 shdrp = *shdrpp;
7078 if (shdrp->sh_offset == -1)
7079 {
7080 asection *sec = shdrp->bfd_section;
7081 if (sec == NULL
7082 || shdrp->sh_type == SHT_REL
7083 || shdrp->sh_type == SHT_RELA)
7084 ;
7085 else if (bfd_section_is_ctf (sec))
7086 {
7087 /* Update section size and contents. */
7088 shdrp->sh_size = sec->size;
7089 shdrp->contents = sec->contents;
7090 }
7091 else if (shdrp->sh_name == -1u)
7092 {
7093 const char *name = sec->name;
7094 struct bfd_elf_section_data *d;
7095
7096 /* Compress DWARF debug sections. */
7097 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7098 return false;
7099
7100 if (sec->compress_status == COMPRESS_SECTION_DONE
7101 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7102 && name[1] == 'd')
7103 {
7104 /* If section is compressed with zlib-gnu, convert
7105 section name from .debug_* to .zdebug_*. */
7106 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7107 if (new_name == NULL)
7108 return false;
7109 name = new_name;
7110 }
7111 /* Add section name to section name section. */
7112 shdrp->sh_name
7113 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7114 name, false);
7115 d = elf_section_data (sec);
7116
7117 /* Add reloc section name to section name section. */
7118 if (d->rel.hdr
7119 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7120 name, false))
7121 return false;
7122 if (d->rela.hdr
7123 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7124 name, true))
7125 return false;
7126
7127 /* Update section size and contents. */
7128 shdrp->sh_size = sec->size;
7129 shdrp->contents = sec->contents;
7130 sec->contents = NULL;
7131 }
7132
7133 off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7134 (abfd->flags & (EXEC_P | DYNAMIC))
7135 || bfd_get_format (abfd) == bfd_core,
7136 bed->s->log_file_align);
7137 }
7138 }
7139
7140 /* Place section name section after DWARF debug sections have been
7141 compressed. */
7142 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7143 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7144 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7145 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7146
7147 /* Place the section headers. */
7148 i_ehdrp = elf_elfheader (abfd);
7149 off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7150 i_ehdrp->e_shoff = off;
7151 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7152 elf_next_file_pos (abfd) = off;
7153
7154 return true;
7155 }
7156
7157 bool
7158 _bfd_elf_write_object_contents (bfd *abfd)
7159 {
7160 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7161 Elf_Internal_Shdr **i_shdrp;
7162 bool failed;
7163 unsigned int count, num_sec;
7164 struct elf_obj_tdata *t;
7165
7166 if (! abfd->output_has_begun
7167 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7168 return false;
7169 /* Do not rewrite ELF data when the BFD has been opened for update.
7170 abfd->output_has_begun was set to TRUE on opening, so creation of
7171 new sections, and modification of existing section sizes was
7172 restricted. This means the ELF header, program headers and
7173 section headers can't have changed. If the contents of any
7174 sections has been modified, then those changes have already been
7175 written to the BFD. */
7176 else if (abfd->direction == both_direction)
7177 {
7178 BFD_ASSERT (abfd->output_has_begun);
7179 return true;
7180 }
7181
7182 i_shdrp = elf_elfsections (abfd);
7183
7184 failed = false;
7185 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7186 if (failed)
7187 return false;
7188
7189 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7190 return false;
7191
7192 /* After writing the headers, we need to write the sections too... */
7193 num_sec = elf_numsections (abfd);
7194 for (count = 1; count < num_sec; count++)
7195 {
7196 /* Don't set the sh_name field without section header. */
7197 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7198 i_shdrp[count]->sh_name
7199 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7200 i_shdrp[count]->sh_name);
7201 if (bed->elf_backend_section_processing)
7202 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7203 return false;
7204 if (i_shdrp[count]->contents)
7205 {
7206 bfd_size_type amt = i_shdrp[count]->sh_size;
7207
7208 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7209 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7210 return false;
7211 }
7212 }
7213
7214 /* Write out the section header names. */
7215 t = elf_tdata (abfd);
7216 if (elf_shstrtab (abfd) != NULL
7217 && t->shstrtab_hdr.sh_offset != -1
7218 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7219 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7220 return false;
7221
7222 if (!(*bed->elf_backend_final_write_processing) (abfd))
7223 return false;
7224
7225 if (!bed->s->write_shdrs_and_ehdr (abfd))
7226 return false;
7227
7228 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7229 if (t->o->build_id.after_write_object_contents != NULL
7230 && !(*t->o->build_id.after_write_object_contents) (abfd))
7231 return false;
7232 if (t->o->package_metadata.after_write_object_contents != NULL
7233 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7234 return false;
7235
7236 return true;
7237 }
7238
7239 bool
7240 _bfd_elf_write_corefile_contents (bfd *abfd)
7241 {
7242 /* Hopefully this can be done just like an object file. */
7243 return _bfd_elf_write_object_contents (abfd);
7244 }
7245
7246 /* Given a section, search the header to find them. */
7247
7248 unsigned int
7249 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7250 {
7251 const struct elf_backend_data *bed;
7252 unsigned int sec_index;
7253
7254 if (elf_section_data (asect) != NULL
7255 && elf_section_data (asect)->this_idx != 0)
7256 return elf_section_data (asect)->this_idx;
7257
7258 if (bfd_is_abs_section (asect))
7259 sec_index = SHN_ABS;
7260 else if (bfd_is_com_section (asect))
7261 sec_index = SHN_COMMON;
7262 else if (bfd_is_und_section (asect))
7263 sec_index = SHN_UNDEF;
7264 else
7265 sec_index = SHN_BAD;
7266
7267 bed = get_elf_backend_data (abfd);
7268 if (bed->elf_backend_section_from_bfd_section)
7269 {
7270 int retval = sec_index;
7271
7272 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7273 return retval;
7274 }
7275
7276 if (sec_index == SHN_BAD)
7277 bfd_set_error (bfd_error_nonrepresentable_section);
7278
7279 return sec_index;
7280 }
7281
7282 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7283 on error. */
7284
7285 int
7286 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7287 {
7288 asymbol *asym_ptr = *asym_ptr_ptr;
7289 int idx;
7290 flagword flags = asym_ptr->flags;
7291
7292 /* When gas creates relocations against local labels, it creates its
7293 own symbol for the section, but does put the symbol into the
7294 symbol chain, so udata is 0. When the linker is generating
7295 relocatable output, this section symbol may be for one of the
7296 input sections rather than the output section. */
7297 if (asym_ptr->udata.i == 0
7298 && (flags & BSF_SECTION_SYM)
7299 && asym_ptr->section)
7300 {
7301 asection *sec;
7302
7303 sec = asym_ptr->section;
7304 if (sec->owner != abfd && sec->output_section != NULL)
7305 sec = sec->output_section;
7306 if (sec->owner == abfd
7307 && sec->index < elf_num_section_syms (abfd)
7308 && elf_section_syms (abfd)[sec->index] != NULL)
7309 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7310 }
7311
7312 idx = asym_ptr->udata.i;
7313
7314 if (idx == 0)
7315 {
7316 /* This case can occur when using --strip-symbol on a symbol
7317 which is used in a relocation entry. */
7318 _bfd_error_handler
7319 /* xgettext:c-format */
7320 (_("%pB: symbol `%s' required but not present"),
7321 abfd, bfd_asymbol_name (asym_ptr));
7322 bfd_set_error (bfd_error_no_symbols);
7323 return -1;
7324 }
7325
7326 #if DEBUG & 4
7327 {
7328 fprintf (stderr,
7329 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7330 " flags = 0x%.8x\n",
7331 (long) asym_ptr, asym_ptr->name, idx, flags);
7332 fflush (stderr);
7333 }
7334 #endif
7335
7336 return idx;
7337 }
7338
7339 static inline bfd_vma
7340 segment_size (Elf_Internal_Phdr *segment)
7341 {
7342 return (segment->p_memsz > segment->p_filesz
7343 ? segment->p_memsz : segment->p_filesz);
7344 }
7345
7346
7347 /* Returns the end address of the segment + 1. */
7348 static inline bfd_vma
7349 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7350 {
7351 return start + segment_size (segment);
7352 }
7353
7354 static inline bfd_size_type
7355 section_size (asection *section, Elf_Internal_Phdr *segment)
7356 {
7357 if ((section->flags & SEC_HAS_CONTENTS) != 0
7358 || (section->flags & SEC_THREAD_LOCAL) == 0
7359 || segment->p_type == PT_TLS)
7360 return section->size;
7361 return 0;
7362 }
7363
7364 /* Returns TRUE if the given section is contained within the given
7365 segment. LMA addresses are compared against PADDR when
7366 USE_VADDR is false, VMA against VADDR when true. */
7367 static bool
7368 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7369 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7370 bool use_vaddr)
7371 {
7372 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7373 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7374 bfd_vma octet;
7375 if (_bfd_mul_overflow (addr, opb, &octet))
7376 return false;
7377 /* The third and fourth lines below are testing that the section end
7378 address is within the segment. It's written this way to avoid
7379 overflow. Add seg_addr + section_size to both sides of the
7380 inequality to make it obvious. */
7381 return (octet >= seg_addr
7382 && segment_size (segment) >= section_size (section, segment)
7383 && (octet - seg_addr
7384 <= segment_size (segment) - section_size (section, segment)));
7385 }
7386
7387 /* Handle PT_NOTE segment. */
7388 static bool
7389 is_note (asection *s, Elf_Internal_Phdr *p)
7390 {
7391 return (p->p_type == PT_NOTE
7392 && elf_section_type (s) == SHT_NOTE
7393 && (ufile_ptr) s->filepos >= p->p_offset
7394 && p->p_filesz >= s->size
7395 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7396 }
7397
7398 /* Rewrite program header information. */
7399
7400 static bool
7401 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7402 {
7403 Elf_Internal_Ehdr *iehdr;
7404 struct elf_segment_map *map;
7405 struct elf_segment_map *map_first;
7406 struct elf_segment_map **pointer_to_map;
7407 Elf_Internal_Phdr *segment;
7408 asection *section;
7409 unsigned int i;
7410 unsigned int num_segments;
7411 bool phdr_included = false;
7412 bool p_paddr_valid;
7413 struct elf_segment_map *phdr_adjust_seg = NULL;
7414 unsigned int phdr_adjust_num = 0;
7415 const struct elf_backend_data *bed;
7416 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7417
7418 bed = get_elf_backend_data (ibfd);
7419 iehdr = elf_elfheader (ibfd);
7420
7421 map_first = NULL;
7422 pointer_to_map = &map_first;
7423
7424 num_segments = elf_elfheader (ibfd)->e_phnum;
7425
7426 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7427 linker, which generates a PT_INTERP section with p_vaddr and
7428 p_memsz set to 0. */
7429 #define IS_SOLARIS_PT_INTERP(p, s) \
7430 (p->p_vaddr == 0 \
7431 && p->p_paddr == 0 \
7432 && p->p_memsz == 0 \
7433 && p->p_filesz > 0 \
7434 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7435 && s->size > 0 \
7436 && (bfd_vma) s->filepos >= p->p_offset \
7437 && ((bfd_vma) s->filepos + s->size \
7438 <= p->p_offset + p->p_filesz))
7439
7440 /* Decide if the given section should be included in the given segment.
7441 A section will be included if:
7442 1. It is within the address space of the segment -- we use the LMA
7443 if that is set for the segment and the VMA otherwise,
7444 2. It is an allocated section or a NOTE section in a PT_NOTE
7445 segment.
7446 3. There is an output section associated with it,
7447 4. The section has not already been allocated to a previous segment.
7448 5. PT_GNU_STACK segments do not include any sections.
7449 6. PT_TLS segment includes only SHF_TLS sections.
7450 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7451 8. PT_DYNAMIC should not contain empty sections at the beginning
7452 (with the possible exception of .dynamic). */
7453 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7454 (((is_contained_by (section, segment, segment->p_paddr, \
7455 segment->p_vaddr, opb, !paddr_valid) \
7456 && (section->flags & SEC_ALLOC) != 0) \
7457 || is_note (section, segment)) \
7458 && segment->p_type != PT_GNU_STACK \
7459 && (segment->p_type != PT_TLS \
7460 || (section->flags & SEC_THREAD_LOCAL)) \
7461 && (segment->p_type == PT_LOAD \
7462 || segment->p_type == PT_TLS \
7463 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7464 && (segment->p_type != PT_DYNAMIC \
7465 || section_size (section, segment) > 0 \
7466 || (segment->p_paddr \
7467 ? segment->p_paddr != section->lma * (opb) \
7468 : segment->p_vaddr != section->vma * (opb)) \
7469 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7470 && (segment->p_type != PT_LOAD || !section->segment_mark))
7471
7472 /* If the output section of a section in the input segment is NULL,
7473 it is removed from the corresponding output segment. */
7474 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \
7475 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7476 && section->output_section != NULL)
7477
7478 /* Returns TRUE iff seg1 starts after the end of seg2. */
7479 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7480 (seg1->field >= segment_end (seg2, seg2->field))
7481
7482 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7483 their VMA address ranges and their LMA address ranges overlap.
7484 It is possible to have overlapping VMA ranges without overlapping LMA
7485 ranges. RedBoot images for example can have both .data and .bss mapped
7486 to the same VMA range, but with the .data section mapped to a different
7487 LMA. */
7488 #define SEGMENT_OVERLAPS(seg1, seg2) \
7489 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7490 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7491 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7492 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7493
7494 /* Initialise the segment mark field, and discard stupid alignment. */
7495 for (section = ibfd->sections; section != NULL; section = section->next)
7496 {
7497 asection *o = section->output_section;
7498 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7499 o->alignment_power = 0;
7500 section->segment_mark = false;
7501 }
7502
7503 /* The Solaris linker creates program headers in which all the
7504 p_paddr fields are zero. When we try to objcopy or strip such a
7505 file, we get confused. Check for this case, and if we find it
7506 don't set the p_paddr_valid fields. */
7507 p_paddr_valid = false;
7508 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7509 i < num_segments;
7510 i++, segment++)
7511 if (segment->p_paddr != 0)
7512 {
7513 p_paddr_valid = true;
7514 break;
7515 }
7516
7517 /* Scan through the segments specified in the program header
7518 of the input BFD. For this first scan we look for overlaps
7519 in the loadable segments. These can be created by weird
7520 parameters to objcopy. Also, fix some solaris weirdness. */
7521 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7522 i < num_segments;
7523 i++, segment++)
7524 {
7525 unsigned int j;
7526 Elf_Internal_Phdr *segment2;
7527
7528 if (segment->p_type == PT_INTERP)
7529 for (section = ibfd->sections; section; section = section->next)
7530 if (IS_SOLARIS_PT_INTERP (segment, section))
7531 {
7532 /* Mininal change so that the normal section to segment
7533 assignment code will work. */
7534 segment->p_vaddr = section->vma * opb;
7535 break;
7536 }
7537
7538 if (segment->p_type != PT_LOAD)
7539 {
7540 /* Remove PT_GNU_RELRO segment. */
7541 if (segment->p_type == PT_GNU_RELRO)
7542 segment->p_type = PT_NULL;
7543 continue;
7544 }
7545
7546 /* Determine if this segment overlaps any previous segments. */
7547 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7548 {
7549 bfd_signed_vma extra_length;
7550
7551 if (segment2->p_type != PT_LOAD
7552 || !SEGMENT_OVERLAPS (segment, segment2))
7553 continue;
7554
7555 /* Merge the two segments together. */
7556 if (segment2->p_vaddr < segment->p_vaddr)
7557 {
7558 /* Extend SEGMENT2 to include SEGMENT and then delete
7559 SEGMENT. */
7560 extra_length = (segment_end (segment, segment->p_vaddr)
7561 - segment_end (segment2, segment2->p_vaddr));
7562
7563 if (extra_length > 0)
7564 {
7565 segment2->p_memsz += extra_length;
7566 segment2->p_filesz += extra_length;
7567 }
7568
7569 segment->p_type = PT_NULL;
7570
7571 /* Since we have deleted P we must restart the outer loop. */
7572 i = 0;
7573 segment = elf_tdata (ibfd)->phdr;
7574 break;
7575 }
7576 else
7577 {
7578 /* Extend SEGMENT to include SEGMENT2 and then delete
7579 SEGMENT2. */
7580 extra_length = (segment_end (segment2, segment2->p_vaddr)
7581 - segment_end (segment, segment->p_vaddr));
7582
7583 if (extra_length > 0)
7584 {
7585 segment->p_memsz += extra_length;
7586 segment->p_filesz += extra_length;
7587 }
7588
7589 segment2->p_type = PT_NULL;
7590 }
7591 }
7592 }
7593
7594 /* The second scan attempts to assign sections to segments. */
7595 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7596 i < num_segments;
7597 i++, segment++)
7598 {
7599 unsigned int section_count;
7600 asection **sections;
7601 asection *output_section;
7602 unsigned int isec;
7603 asection *matching_lma;
7604 asection *suggested_lma;
7605 unsigned int j;
7606 size_t amt;
7607 asection *first_section;
7608
7609 if (segment->p_type == PT_NULL)
7610 continue;
7611
7612 first_section = NULL;
7613 /* Compute how many sections might be placed into this segment. */
7614 for (section = ibfd->sections, section_count = 0;
7615 section != NULL;
7616 section = section->next)
7617 {
7618 /* Find the first section in the input segment, which may be
7619 removed from the corresponding output segment. */
7620 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7621 {
7622 if (first_section == NULL)
7623 first_section = section;
7624 if (section->output_section != NULL)
7625 ++section_count;
7626 }
7627 }
7628
7629 /* Allocate a segment map big enough to contain
7630 all of the sections we have selected. */
7631 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7632 amt += section_count * sizeof (asection *);
7633 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7634 if (map == NULL)
7635 return false;
7636
7637 /* Initialise the fields of the segment map. Default to
7638 using the physical address of the segment in the input BFD. */
7639 map->next = NULL;
7640 map->p_type = segment->p_type;
7641 map->p_flags = segment->p_flags;
7642 map->p_flags_valid = 1;
7643
7644 if (map->p_type == PT_LOAD
7645 && (ibfd->flags & D_PAGED) != 0
7646 && maxpagesize > 1
7647 && segment->p_align > 1)
7648 {
7649 map->p_align = segment->p_align;
7650 if (segment->p_align > maxpagesize)
7651 map->p_align = maxpagesize;
7652 map->p_align_valid = 1;
7653 }
7654
7655 /* If the first section in the input segment is removed, there is
7656 no need to preserve segment physical address in the corresponding
7657 output segment. */
7658 if (!first_section || first_section->output_section != NULL)
7659 {
7660 map->p_paddr = segment->p_paddr;
7661 map->p_paddr_valid = p_paddr_valid;
7662 }
7663
7664 /* Determine if this segment contains the ELF file header
7665 and if it contains the program headers themselves. */
7666 map->includes_filehdr = (segment->p_offset == 0
7667 && segment->p_filesz >= iehdr->e_ehsize);
7668 map->includes_phdrs = 0;
7669
7670 if (!phdr_included || segment->p_type != PT_LOAD)
7671 {
7672 map->includes_phdrs =
7673 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7674 && (segment->p_offset + segment->p_filesz
7675 >= ((bfd_vma) iehdr->e_phoff
7676 + iehdr->e_phnum * iehdr->e_phentsize)));
7677
7678 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7679 phdr_included = true;
7680 }
7681
7682 if (section_count == 0)
7683 {
7684 /* Special segments, such as the PT_PHDR segment, may contain
7685 no sections, but ordinary, loadable segments should contain
7686 something. They are allowed by the ELF spec however, so only
7687 a warning is produced.
7688 Don't warn if an empty PT_LOAD contains the program headers.
7689 There is however the valid use case of embedded systems which
7690 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7691 flash memory with zeros. No warning is shown for that case. */
7692 if (segment->p_type == PT_LOAD
7693 && !map->includes_phdrs
7694 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7695 /* xgettext:c-format */
7696 _bfd_error_handler
7697 (_("%pB: warning: empty loadable segment detected"
7698 " at vaddr=%#" PRIx64 ", is this intentional?"),
7699 ibfd, (uint64_t) segment->p_vaddr);
7700
7701 map->p_vaddr_offset = segment->p_vaddr / opb;
7702 map->count = 0;
7703 *pointer_to_map = map;
7704 pointer_to_map = &map->next;
7705
7706 continue;
7707 }
7708
7709 /* Now scan the sections in the input BFD again and attempt
7710 to add their corresponding output sections to the segment map.
7711 The problem here is how to handle an output section which has
7712 been moved (ie had its LMA changed). There are four possibilities:
7713
7714 1. None of the sections have been moved.
7715 In this case we can continue to use the segment LMA from the
7716 input BFD.
7717
7718 2. All of the sections have been moved by the same amount.
7719 In this case we can change the segment's LMA to match the LMA
7720 of the first section.
7721
7722 3. Some of the sections have been moved, others have not.
7723 In this case those sections which have not been moved can be
7724 placed in the current segment which will have to have its size,
7725 and possibly its LMA changed, and a new segment or segments will
7726 have to be created to contain the other sections.
7727
7728 4. The sections have been moved, but not by the same amount.
7729 In this case we can change the segment's LMA to match the LMA
7730 of the first section and we will have to create a new segment
7731 or segments to contain the other sections.
7732
7733 In order to save time, we allocate an array to hold the section
7734 pointers that we are interested in. As these sections get assigned
7735 to a segment, they are removed from this array. */
7736
7737 amt = section_count * sizeof (asection *);
7738 sections = (asection **) bfd_malloc (amt);
7739 if (sections == NULL)
7740 return false;
7741
7742 /* Step One: Scan for segment vs section LMA conflicts.
7743 Also add the sections to the section array allocated above.
7744 Also add the sections to the current segment. In the common
7745 case, where the sections have not been moved, this means that
7746 we have completely filled the segment, and there is nothing
7747 more to do. */
7748 isec = 0;
7749 matching_lma = NULL;
7750 suggested_lma = NULL;
7751
7752 for (section = first_section, j = 0;
7753 section != NULL;
7754 section = section->next)
7755 {
7756 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7757 {
7758 output_section = section->output_section;
7759
7760 sections[j++] = section;
7761
7762 /* The Solaris native linker always sets p_paddr to 0.
7763 We try to catch that case here, and set it to the
7764 correct value. Note - some backends require that
7765 p_paddr be left as zero. */
7766 if (!p_paddr_valid
7767 && segment->p_vaddr != 0
7768 && !bed->want_p_paddr_set_to_zero
7769 && isec == 0
7770 && output_section->lma != 0
7771 && (align_power (segment->p_vaddr
7772 + (map->includes_filehdr
7773 ? iehdr->e_ehsize : 0)
7774 + (map->includes_phdrs
7775 ? iehdr->e_phnum * iehdr->e_phentsize
7776 : 0),
7777 output_section->alignment_power * opb)
7778 == (output_section->vma * opb)))
7779 map->p_paddr = segment->p_vaddr;
7780
7781 /* Match up the physical address of the segment with the
7782 LMA address of the output section. */
7783 if (is_contained_by (output_section, segment, map->p_paddr,
7784 0, opb, false)
7785 || is_note (section, segment))
7786 {
7787 if (matching_lma == NULL
7788 || output_section->lma < matching_lma->lma)
7789 matching_lma = output_section;
7790
7791 /* We assume that if the section fits within the segment
7792 then it does not overlap any other section within that
7793 segment. */
7794 map->sections[isec++] = output_section;
7795 }
7796 else if (suggested_lma == NULL)
7797 suggested_lma = output_section;
7798
7799 if (j == section_count)
7800 break;
7801 }
7802 }
7803
7804 BFD_ASSERT (j == section_count);
7805
7806 /* Step Two: Adjust the physical address of the current segment,
7807 if necessary. */
7808 if (isec == section_count)
7809 {
7810 /* All of the sections fitted within the segment as currently
7811 specified. This is the default case. Add the segment to
7812 the list of built segments and carry on to process the next
7813 program header in the input BFD. */
7814 map->count = section_count;
7815 *pointer_to_map = map;
7816 pointer_to_map = &map->next;
7817
7818 if (p_paddr_valid
7819 && !bed->want_p_paddr_set_to_zero)
7820 {
7821 bfd_vma hdr_size = 0;
7822 if (map->includes_filehdr)
7823 hdr_size = iehdr->e_ehsize;
7824 if (map->includes_phdrs)
7825 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7826
7827 /* Account for padding before the first section in the
7828 segment. */
7829 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7830 - matching_lma->lma);
7831 }
7832
7833 free (sections);
7834 continue;
7835 }
7836 else
7837 {
7838 /* Change the current segment's physical address to match
7839 the LMA of the first section that fitted, or if no
7840 section fitted, the first section. */
7841 if (matching_lma == NULL)
7842 matching_lma = suggested_lma;
7843
7844 map->p_paddr = matching_lma->lma * opb;
7845
7846 /* Offset the segment physical address from the lma
7847 to allow for space taken up by elf headers. */
7848 if (map->includes_phdrs)
7849 {
7850 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7851
7852 /* iehdr->e_phnum is just an estimate of the number
7853 of program headers that we will need. Make a note
7854 here of the number we used and the segment we chose
7855 to hold these headers, so that we can adjust the
7856 offset when we know the correct value. */
7857 phdr_adjust_num = iehdr->e_phnum;
7858 phdr_adjust_seg = map;
7859 }
7860
7861 if (map->includes_filehdr)
7862 {
7863 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7864 map->p_paddr -= iehdr->e_ehsize;
7865 /* We've subtracted off the size of headers from the
7866 first section lma, but there may have been some
7867 alignment padding before that section too. Try to
7868 account for that by adjusting the segment lma down to
7869 the same alignment. */
7870 if (segment->p_align != 0 && segment->p_align < align)
7871 align = segment->p_align;
7872 map->p_paddr &= -(align * opb);
7873 }
7874 }
7875
7876 /* Step Three: Loop over the sections again, this time assigning
7877 those that fit to the current segment and removing them from the
7878 sections array; but making sure not to leave large gaps. Once all
7879 possible sections have been assigned to the current segment it is
7880 added to the list of built segments and if sections still remain
7881 to be assigned, a new segment is constructed before repeating
7882 the loop. */
7883 isec = 0;
7884 do
7885 {
7886 map->count = 0;
7887 suggested_lma = NULL;
7888
7889 /* Fill the current segment with sections that fit. */
7890 for (j = 0; j < section_count; j++)
7891 {
7892 section = sections[j];
7893
7894 if (section == NULL)
7895 continue;
7896
7897 output_section = section->output_section;
7898
7899 BFD_ASSERT (output_section != NULL);
7900
7901 if (is_contained_by (output_section, segment, map->p_paddr,
7902 0, opb, false)
7903 || is_note (section, segment))
7904 {
7905 if (map->count == 0)
7906 {
7907 /* If the first section in a segment does not start at
7908 the beginning of the segment, then something is
7909 wrong. */
7910 if (align_power (map->p_paddr
7911 + (map->includes_filehdr
7912 ? iehdr->e_ehsize : 0)
7913 + (map->includes_phdrs
7914 ? iehdr->e_phnum * iehdr->e_phentsize
7915 : 0),
7916 output_section->alignment_power * opb)
7917 != output_section->lma * opb)
7918 goto sorry;
7919 }
7920 else
7921 {
7922 asection *prev_sec;
7923
7924 prev_sec = map->sections[map->count - 1];
7925
7926 /* If the gap between the end of the previous section
7927 and the start of this section is more than
7928 maxpagesize then we need to start a new segment. */
7929 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7930 maxpagesize)
7931 < BFD_ALIGN (output_section->lma, maxpagesize))
7932 || (prev_sec->lma + prev_sec->size
7933 > output_section->lma))
7934 {
7935 if (suggested_lma == NULL)
7936 suggested_lma = output_section;
7937
7938 continue;
7939 }
7940 }
7941
7942 map->sections[map->count++] = output_section;
7943 ++isec;
7944 sections[j] = NULL;
7945 if (segment->p_type == PT_LOAD)
7946 section->segment_mark = true;
7947 }
7948 else if (suggested_lma == NULL)
7949 suggested_lma = output_section;
7950 }
7951
7952 /* PR 23932. A corrupt input file may contain sections that cannot
7953 be assigned to any segment - because for example they have a
7954 negative size - or segments that do not contain any sections.
7955 But there are also valid reasons why a segment can be empty.
7956 So allow a count of zero. */
7957
7958 /* Add the current segment to the list of built segments. */
7959 *pointer_to_map = map;
7960 pointer_to_map = &map->next;
7961
7962 if (isec < section_count)
7963 {
7964 /* We still have not allocated all of the sections to
7965 segments. Create a new segment here, initialise it
7966 and carry on looping. */
7967 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7968 amt += section_count * sizeof (asection *);
7969 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7970 if (map == NULL)
7971 {
7972 free (sections);
7973 return false;
7974 }
7975
7976 /* Initialise the fields of the segment map. Set the physical
7977 physical address to the LMA of the first section that has
7978 not yet been assigned. */
7979 map->next = NULL;
7980 map->p_type = segment->p_type;
7981 map->p_flags = segment->p_flags;
7982 map->p_flags_valid = 1;
7983 map->p_paddr = suggested_lma->lma * opb;
7984 map->p_paddr_valid = p_paddr_valid;
7985 map->includes_filehdr = 0;
7986 map->includes_phdrs = 0;
7987 }
7988
7989 continue;
7990 sorry:
7991 bfd_set_error (bfd_error_sorry);
7992 free (sections);
7993 return false;
7994 }
7995 while (isec < section_count);
7996
7997 free (sections);
7998 }
7999
8000 elf_seg_map (obfd) = map_first;
8001
8002 /* If we had to estimate the number of program headers that were
8003 going to be needed, then check our estimate now and adjust
8004 the offset if necessary. */
8005 if (phdr_adjust_seg != NULL)
8006 {
8007 unsigned int count;
8008
8009 for (count = 0, map = map_first; map != NULL; map = map->next)
8010 count++;
8011
8012 if (count > phdr_adjust_num)
8013 phdr_adjust_seg->p_paddr
8014 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8015
8016 for (map = map_first; map != NULL; map = map->next)
8017 if (map->p_type == PT_PHDR)
8018 {
8019 bfd_vma adjust
8020 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8021 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8022 break;
8023 }
8024 }
8025
8026 #undef IS_SOLARIS_PT_INTERP
8027 #undef IS_SECTION_IN_INPUT_SEGMENT
8028 #undef INCLUDE_SECTION_IN_SEGMENT
8029 #undef SEGMENT_AFTER_SEGMENT
8030 #undef SEGMENT_OVERLAPS
8031 return true;
8032 }
8033
8034 /* Return true if p_align in the ELF program header in ABFD is valid. */
8035
8036 static bool
8037 elf_is_p_align_valid (bfd *abfd)
8038 {
8039 unsigned int i;
8040 Elf_Internal_Phdr *segment;
8041 unsigned int num_segments;
8042 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8043 bfd_size_type maxpagesize = bed->maxpagesize;
8044 bfd_size_type p_align = bed->p_align;
8045
8046 /* Return true if the default p_align value isn't set or the maximum
8047 page size is the same as the minimum page size. */
8048 if (p_align == 0 || maxpagesize == bed->minpagesize)
8049 return true;
8050
8051 /* When the default p_align value is set, p_align may be set to the
8052 default p_align value while segments are aligned to the maximum
8053 page size. In this case, the input p_align will be ignored and
8054 the maximum page size will be used to align the output segments. */
8055 segment = elf_tdata (abfd)->phdr;
8056 num_segments = elf_elfheader (abfd)->e_phnum;
8057 for (i = 0; i < num_segments; i++, segment++)
8058 if (segment->p_type == PT_LOAD
8059 && (segment->p_align != p_align
8060 || vma_page_aligned_bias (segment->p_vaddr,
8061 segment->p_offset,
8062 maxpagesize) != 0))
8063 return true;
8064
8065 return false;
8066 }
8067
8068 /* Copy ELF program header information. */
8069
8070 static bool
8071 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8072 {
8073 Elf_Internal_Ehdr *iehdr;
8074 struct elf_segment_map *map;
8075 struct elf_segment_map *map_first;
8076 struct elf_segment_map **pointer_to_map;
8077 Elf_Internal_Phdr *segment;
8078 unsigned int i;
8079 unsigned int num_segments;
8080 bool phdr_included = false;
8081 bool p_paddr_valid;
8082 bool p_palign_valid;
8083 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8084
8085 iehdr = elf_elfheader (ibfd);
8086
8087 map_first = NULL;
8088 pointer_to_map = &map_first;
8089
8090 /* If all the segment p_paddr fields are zero, don't set
8091 map->p_paddr_valid. */
8092 p_paddr_valid = false;
8093 num_segments = elf_elfheader (ibfd)->e_phnum;
8094 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8095 i < num_segments;
8096 i++, segment++)
8097 if (segment->p_paddr != 0)
8098 {
8099 p_paddr_valid = true;
8100 break;
8101 }
8102
8103 p_palign_valid = elf_is_p_align_valid (ibfd);
8104
8105 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8106 i < num_segments;
8107 i++, segment++)
8108 {
8109 asection *section;
8110 unsigned int section_count;
8111 size_t amt;
8112 Elf_Internal_Shdr *this_hdr;
8113 asection *first_section = NULL;
8114 asection *lowest_section;
8115
8116 /* Compute how many sections are in this segment. */
8117 for (section = ibfd->sections, section_count = 0;
8118 section != NULL;
8119 section = section->next)
8120 {
8121 this_hdr = &(elf_section_data(section)->this_hdr);
8122 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8123 {
8124 if (first_section == NULL)
8125 first_section = section;
8126 section_count++;
8127 }
8128 }
8129
8130 /* Allocate a segment map big enough to contain
8131 all of the sections we have selected. */
8132 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8133 amt += section_count * sizeof (asection *);
8134 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8135 if (map == NULL)
8136 return false;
8137
8138 /* Initialize the fields of the output segment map with the
8139 input segment. */
8140 map->next = NULL;
8141 map->p_type = segment->p_type;
8142 map->p_flags = segment->p_flags;
8143 map->p_flags_valid = 1;
8144 map->p_paddr = segment->p_paddr;
8145 map->p_paddr_valid = p_paddr_valid;
8146 map->p_align = segment->p_align;
8147 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8148 map->p_align_valid = (map->p_type == PT_GNU_STACK
8149 || p_palign_valid);
8150 map->p_vaddr_offset = 0;
8151
8152 if (map->p_type == PT_GNU_RELRO
8153 || map->p_type == PT_GNU_STACK)
8154 {
8155 /* The PT_GNU_RELRO segment may contain the first a few
8156 bytes in the .got.plt section even if the whole .got.plt
8157 section isn't in the PT_GNU_RELRO segment. We won't
8158 change the size of the PT_GNU_RELRO segment.
8159 Similarly, PT_GNU_STACK size is significant on uclinux
8160 systems. */
8161 map->p_size = segment->p_memsz;
8162 map->p_size_valid = 1;
8163 }
8164
8165 /* Determine if this segment contains the ELF file header
8166 and if it contains the program headers themselves. */
8167 map->includes_filehdr = (segment->p_offset == 0
8168 && segment->p_filesz >= iehdr->e_ehsize);
8169
8170 map->includes_phdrs = 0;
8171 if (! phdr_included || segment->p_type != PT_LOAD)
8172 {
8173 map->includes_phdrs =
8174 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8175 && (segment->p_offset + segment->p_filesz
8176 >= ((bfd_vma) iehdr->e_phoff
8177 + iehdr->e_phnum * iehdr->e_phentsize)));
8178
8179 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8180 phdr_included = true;
8181 }
8182
8183 lowest_section = NULL;
8184 if (section_count != 0)
8185 {
8186 unsigned int isec = 0;
8187
8188 for (section = first_section;
8189 section != NULL;
8190 section = section->next)
8191 {
8192 this_hdr = &(elf_section_data(section)->this_hdr);
8193 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8194 {
8195 map->sections[isec++] = section->output_section;
8196 if ((section->flags & SEC_ALLOC) != 0)
8197 {
8198 bfd_vma seg_off;
8199
8200 if (lowest_section == NULL
8201 || section->lma < lowest_section->lma)
8202 lowest_section = section;
8203
8204 /* Section lmas are set up from PT_LOAD header
8205 p_paddr in _bfd_elf_make_section_from_shdr.
8206 If this header has a p_paddr that disagrees
8207 with the section lma, flag the p_paddr as
8208 invalid. */
8209 if ((section->flags & SEC_LOAD) != 0)
8210 seg_off = this_hdr->sh_offset - segment->p_offset;
8211 else
8212 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8213 if (section->lma * opb - segment->p_paddr != seg_off)
8214 map->p_paddr_valid = false;
8215 }
8216 if (isec == section_count)
8217 break;
8218 }
8219 }
8220 }
8221
8222 if (section_count == 0)
8223 map->p_vaddr_offset = segment->p_vaddr / opb;
8224 else if (map->p_paddr_valid)
8225 {
8226 /* Account for padding before the first section in the segment. */
8227 bfd_vma hdr_size = 0;
8228 if (map->includes_filehdr)
8229 hdr_size = iehdr->e_ehsize;
8230 if (map->includes_phdrs)
8231 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8232
8233 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8234 - (lowest_section ? lowest_section->lma : 0));
8235 }
8236
8237 map->count = section_count;
8238 *pointer_to_map = map;
8239 pointer_to_map = &map->next;
8240 }
8241
8242 elf_seg_map (obfd) = map_first;
8243 return true;
8244 }
8245
8246 /* Copy private BFD data. This copies or rewrites ELF program header
8247 information. */
8248
8249 static bool
8250 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8251 {
8252 bfd_vma maxpagesize;
8253
8254 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8255 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8256 return true;
8257
8258 if (elf_tdata (ibfd)->phdr == NULL)
8259 return true;
8260
8261 if (ibfd->xvec == obfd->xvec)
8262 {
8263 /* Check to see if any sections in the input BFD
8264 covered by ELF program header have changed. */
8265 Elf_Internal_Phdr *segment;
8266 asection * section;
8267 asection * osec;
8268 asection * prev;
8269 unsigned int i, num_segments;
8270 Elf_Internal_Shdr *this_hdr;
8271 const struct elf_backend_data *bed;
8272
8273 bed = get_elf_backend_data (ibfd);
8274
8275 /* Regenerate the segment map if p_paddr is set to 0. */
8276 if (bed->want_p_paddr_set_to_zero)
8277 goto rewrite;
8278
8279 /* Initialize the segment mark field. */
8280 for (section = obfd->sections; section != NULL;
8281 section = section->next)
8282 section->segment_mark = false;
8283
8284 num_segments = elf_elfheader (ibfd)->e_phnum;
8285 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8286 i < num_segments;
8287 i++, segment++)
8288 {
8289 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8290 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8291 which severly confuses things, so always regenerate the segment
8292 map in this case. */
8293 if (segment->p_paddr == 0
8294 && segment->p_memsz == 0
8295 && (segment->p_type == PT_INTERP
8296 || segment->p_type == PT_DYNAMIC))
8297 goto rewrite;
8298
8299 for (section = ibfd->sections, prev = NULL;
8300 section != NULL; section = section->next)
8301 {
8302 /* We mark the output section so that we know it comes
8303 from the input BFD. */
8304 osec = section->output_section;
8305 if (osec)
8306 osec->segment_mark = true;
8307
8308 /* Check if this section is covered by the segment. */
8309 this_hdr = &(elf_section_data(section)->this_hdr);
8310 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8311 {
8312 /* FIXME: Check if its output section is changed or
8313 removed. What else do we need to check? */
8314 if (osec == NULL
8315 || section->flags != osec->flags
8316 || section->lma != osec->lma
8317 || section->vma != osec->vma
8318 || section->size != osec->size
8319 || section->rawsize != osec->rawsize
8320 || section->alignment_power != osec->alignment_power)
8321 goto rewrite;
8322
8323 /* PR 31450: If this is an allocated section then make sure
8324 that this section's vma to lma relationship is the same
8325 as previous (allocated) section's. */
8326 if (prev != NULL
8327 && section->flags & SEC_ALLOC
8328 && section->lma - section->vma != prev->lma - prev->vma)
8329 goto rewrite;
8330
8331 if (section->flags & SEC_ALLOC)
8332 prev = section;
8333 }
8334 }
8335 }
8336
8337 /* Check to see if any output section do not come from the
8338 input BFD. */
8339 for (section = obfd->sections; section != NULL;
8340 section = section->next)
8341 {
8342 if (!section->segment_mark)
8343 goto rewrite;
8344 else
8345 section->segment_mark = false;
8346 }
8347
8348 return copy_elf_program_header (ibfd, obfd);
8349 }
8350
8351 rewrite:
8352 maxpagesize = 0;
8353 if (ibfd->xvec == obfd->xvec)
8354 {
8355 /* When rewriting program header, set the output maxpagesize to
8356 the maximum alignment of input PT_LOAD segments. */
8357 Elf_Internal_Phdr *segment;
8358 unsigned int i;
8359 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8360
8361 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8362 i < num_segments;
8363 i++, segment++)
8364 if (segment->p_type == PT_LOAD
8365 && maxpagesize < segment->p_align)
8366 {
8367 /* PR 17512: file: f17299af. */
8368 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8369 /* xgettext:c-format */
8370 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8371 PRIx64 " is too large"),
8372 ibfd, (uint64_t) segment->p_align);
8373 else
8374 maxpagesize = segment->p_align;
8375 }
8376 }
8377 if (maxpagesize == 0)
8378 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8379
8380 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8381 }
8382
8383 /* Copy private section information from input to output. This function
8384 is called both by objcopy where LINK_INFO is NULL, and ld where
8385 LINK_INFO is non-NULL in the usual case but NULL for a special case
8386 when dealing with LTO IR or cloning output sections. */
8387
8388 bool
8389 _bfd_elf_copy_private_section_data (bfd *ibfd,
8390 asection *isec,
8391 bfd *obfd,
8392 asection *osec,
8393 struct bfd_link_info *link_info)
8394 {
8395 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8396 || obfd->xvec->flavour != bfd_target_elf_flavour)
8397 return true;
8398
8399 Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8400 Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8401 if (link_info == NULL)
8402 {
8403 ohdr->sh_entsize = ihdr->sh_entsize;
8404
8405 if (ihdr->sh_type == SHT_SYMTAB
8406 || ihdr->sh_type == SHT_DYNSYM
8407 || ihdr->sh_type == SHT_GNU_verneed
8408 || ihdr->sh_type == SHT_GNU_verdef)
8409 ohdr->sh_info = ihdr->sh_info;
8410 }
8411
8412 /* If this is a known ABI section, ELF section type and flags may
8413 have been set up when OSEC was created. For normal sections we
8414 allow the user to override the type and flags other than
8415 SHF_MASKOS and SHF_MASKPROC. */
8416 if (elf_section_type (osec) == SHT_PROGBITS
8417 || elf_section_type (osec) == SHT_NOTE
8418 || elf_section_type (osec) == SHT_NOBITS)
8419 elf_section_type (osec) = SHT_NULL;
8420
8421 /* For objcopy and relocatable link, copy the ELF section type from
8422 the input file if the BFD section flags are the same. (If they
8423 are different the user may be doing something like
8424 "objcopy --set-section-flags .text=alloc,data".) For a final
8425 link allow some flags that the linker clears to differ. */
8426 bool final_link = (link_info != NULL
8427 && !bfd_link_relocatable (link_info));
8428 if (elf_section_type (osec) == SHT_NULL
8429 && (osec->flags == isec->flags
8430 || (final_link
8431 && ((osec->flags ^ isec->flags)
8432 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8433 elf_section_type (osec) = elf_section_type (isec);
8434
8435 /* FIXME: Is this correct for all OS/PROC specific flags? */
8436 elf_section_flags (osec) = (elf_section_flags (isec)
8437 & (SHF_MASKOS | SHF_MASKPROC));
8438
8439 /* Copy sh_info from input for mbind section. */
8440 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8441 && elf_section_flags (isec) & SHF_GNU_MBIND)
8442 elf_section_data (osec)->this_hdr.sh_info
8443 = elf_section_data (isec)->this_hdr.sh_info;
8444
8445 /* Set things up for objcopy and relocatable link. The output
8446 SHT_GROUP section will have its elf_next_in_group pointing back
8447 to the input group members. Ignore linker created group section.
8448 See elfNN_ia64_object_p in elfxx-ia64.c. */
8449 if ((link_info == NULL
8450 || !link_info->resolve_section_groups)
8451 && (elf_sec_group (isec) == NULL
8452 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8453 {
8454 if (elf_section_flags (isec) & SHF_GROUP)
8455 elf_section_flags (osec) |= SHF_GROUP;
8456 elf_next_in_group (osec) = elf_next_in_group (isec);
8457 elf_section_data (osec)->group = elf_section_data (isec)->group;
8458 }
8459
8460 /* If not decompress, preserve SHF_COMPRESSED. */
8461 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8462 elf_section_flags (osec) |= (elf_section_flags (isec)
8463 & SHF_COMPRESSED);
8464
8465 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8466 don't use the output section of the linked-to section since it
8467 may be NULL at this point. */
8468 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8469 {
8470 ohdr->sh_flags |= SHF_LINK_ORDER;
8471 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8472 }
8473
8474 osec->use_rela_p = isec->use_rela_p;
8475
8476 return true;
8477 }
8478
8479 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8480 necessary if we are removing either the SHT_GROUP section or any of
8481 the group member sections. DISCARDED is the value that a section's
8482 output_section has if the section will be discarded, NULL when this
8483 function is called from objcopy, bfd_abs_section_ptr when called
8484 from the linker. */
8485
8486 bool
8487 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8488 {
8489 asection *isec;
8490
8491 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8492 if (elf_section_type (isec) == SHT_GROUP)
8493 {
8494 asection *first = elf_next_in_group (isec);
8495 asection *s = first;
8496 bfd_size_type removed = 0;
8497
8498 while (s != NULL)
8499 {
8500 /* If this member section is being output but the
8501 SHT_GROUP section is not, then clear the group info
8502 set up by _bfd_elf_copy_private_section_data. */
8503 if (s->output_section != discarded
8504 && isec->output_section == discarded)
8505 {
8506 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8507 elf_group_name (s->output_section) = NULL;
8508 }
8509 else
8510 {
8511 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8512 if (s->output_section == discarded
8513 && isec->output_section != discarded)
8514 {
8515 /* Conversely, if the member section is not being
8516 output but the SHT_GROUP section is, then adjust
8517 its size. */
8518 removed += 4;
8519 if (elf_sec->rel.hdr != NULL
8520 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8521 removed += 4;
8522 if (elf_sec->rela.hdr != NULL
8523 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8524 removed += 4;
8525 }
8526 else
8527 {
8528 /* Also adjust for zero-sized relocation member
8529 section. */
8530 if (elf_sec->rel.hdr != NULL
8531 && elf_sec->rel.hdr->sh_size == 0)
8532 removed += 4;
8533 if (elf_sec->rela.hdr != NULL
8534 && elf_sec->rela.hdr->sh_size == 0)
8535 removed += 4;
8536 }
8537 }
8538 s = elf_next_in_group (s);
8539 if (s == first)
8540 break;
8541 }
8542 if (removed != 0)
8543 {
8544 if (discarded != NULL)
8545 {
8546 /* If we've been called for ld -r, then we need to
8547 adjust the input section size. */
8548 if (isec->rawsize == 0)
8549 isec->rawsize = isec->size;
8550 isec->size = isec->rawsize - removed;
8551 if (isec->size <= 4)
8552 {
8553 isec->size = 0;
8554 isec->flags |= SEC_EXCLUDE;
8555 }
8556 }
8557 else if (isec->output_section != NULL)
8558 {
8559 /* Adjust the output section size when called from
8560 objcopy. */
8561 isec->output_section->size -= removed;
8562 if (isec->output_section->size <= 4)
8563 {
8564 isec->output_section->size = 0;
8565 isec->output_section->flags |= SEC_EXCLUDE;
8566 }
8567 }
8568 }
8569 }
8570
8571 return true;
8572 }
8573
8574 /* Copy private header information. */
8575
8576 bool
8577 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8578 {
8579 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8580 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8581 return true;
8582
8583 /* Copy over private BFD data if it has not already been copied.
8584 This must be done here, rather than in the copy_private_bfd_data
8585 entry point, because the latter is called after the section
8586 contents have been set, which means that the program headers have
8587 already been worked out. */
8588 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8589 {
8590 if (! copy_private_bfd_data (ibfd, obfd))
8591 return false;
8592 }
8593
8594 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8595 }
8596
8597 /* Copy private symbol information. If this symbol is in a section
8598 which we did not map into a BFD section, try to map the section
8599 index correctly. We use special macro definitions for the mapped
8600 section indices; these definitions are interpreted by the
8601 swap_out_syms function. */
8602
8603 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8604 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8605 #define MAP_STRTAB (SHN_HIOS + 3)
8606 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8607 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8608
8609 bool
8610 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8611 asymbol *isymarg,
8612 bfd *obfd,
8613 asymbol *osymarg)
8614 {
8615 elf_symbol_type *isym, *osym;
8616
8617 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8618 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8619 return true;
8620
8621 isym = elf_symbol_from (isymarg);
8622 osym = elf_symbol_from (osymarg);
8623
8624 if (isym != NULL
8625 && isym->internal_elf_sym.st_shndx != 0
8626 && osym != NULL
8627 && bfd_is_abs_section (isym->symbol.section))
8628 {
8629 unsigned int shndx;
8630
8631 shndx = isym->internal_elf_sym.st_shndx;
8632 if (shndx == elf_onesymtab (ibfd))
8633 shndx = MAP_ONESYMTAB;
8634 else if (shndx == elf_dynsymtab (ibfd))
8635 shndx = MAP_DYNSYMTAB;
8636 else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8637 shndx = MAP_STRTAB;
8638 else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8639 shndx = MAP_SHSTRTAB;
8640 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8641 shndx = MAP_SYM_SHNDX;
8642 osym->internal_elf_sym.st_shndx = shndx;
8643 }
8644
8645 return true;
8646 }
8647
8648 /* Swap out the symbols. */
8649
8650 static bool
8651 swap_out_syms (bfd *abfd,
8652 struct elf_strtab_hash **sttp,
8653 int relocatable_p,
8654 struct bfd_link_info *info)
8655 {
8656 const struct elf_backend_data *bed;
8657 unsigned int symcount;
8658 asymbol **syms;
8659 struct elf_strtab_hash *stt;
8660 Elf_Internal_Shdr *symtab_hdr;
8661 Elf_Internal_Shdr *symtab_shndx_hdr;
8662 Elf_Internal_Shdr *symstrtab_hdr;
8663 struct elf_sym_strtab *symstrtab;
8664 bfd_byte *outbound_syms;
8665 bfd_byte *outbound_shndx;
8666 unsigned long outbound_syms_index;
8667 unsigned int idx;
8668 unsigned int num_locals;
8669 size_t amt;
8670 bool name_local_sections;
8671
8672 if (!elf_map_symbols (abfd, &num_locals))
8673 return false;
8674
8675 /* Dump out the symtabs. */
8676 stt = _bfd_elf_strtab_init ();
8677 if (stt == NULL)
8678 return false;
8679
8680 bed = get_elf_backend_data (abfd);
8681 symcount = bfd_get_symcount (abfd);
8682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8683 symtab_hdr->sh_type = SHT_SYMTAB;
8684 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8685 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8686 symtab_hdr->sh_info = num_locals + 1;
8687 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8688
8689 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8690 symstrtab_hdr->sh_type = SHT_STRTAB;
8691
8692 /* Allocate buffer to swap out the .strtab section. */
8693 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8694 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8695 {
8696 bfd_set_error (bfd_error_no_memory);
8697 _bfd_elf_strtab_free (stt);
8698 return false;
8699 }
8700
8701 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8702 || (outbound_syms = bfd_malloc (amt)) == NULL)
8703 {
8704 error_no_mem:
8705 bfd_set_error (bfd_error_no_memory);
8706 error_return:
8707 free (symstrtab);
8708 _bfd_elf_strtab_free (stt);
8709 return false;
8710 }
8711 symtab_hdr->contents = outbound_syms;
8712 outbound_syms_index = 0;
8713
8714 outbound_shndx = NULL;
8715
8716 if (elf_symtab_shndx_list (abfd))
8717 {
8718 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8719 if (symtab_shndx_hdr->sh_name != 0)
8720 {
8721 if (_bfd_mul_overflow (symcount + 1,
8722 sizeof (Elf_External_Sym_Shndx), &amt))
8723 goto error_no_mem;
8724 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8725 if (outbound_shndx == NULL)
8726 goto error_return;
8727
8728 symtab_shndx_hdr->contents = outbound_shndx;
8729 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8730 symtab_shndx_hdr->sh_size = amt;
8731 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8732 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8733 }
8734 /* FIXME: What about any other headers in the list ? */
8735 }
8736
8737 /* Now generate the data (for "contents"). */
8738 {
8739 /* Fill in zeroth symbol and swap it out. */
8740 Elf_Internal_Sym sym;
8741 sym.st_name = 0;
8742 sym.st_value = 0;
8743 sym.st_size = 0;
8744 sym.st_info = 0;
8745 sym.st_other = 0;
8746 sym.st_shndx = SHN_UNDEF;
8747 sym.st_target_internal = 0;
8748 symstrtab[outbound_syms_index].sym = sym;
8749 symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8750 outbound_syms_index++;
8751 }
8752
8753 name_local_sections
8754 = (bed->elf_backend_name_local_section_symbols
8755 && bed->elf_backend_name_local_section_symbols (abfd));
8756
8757 syms = bfd_get_outsymbols (abfd);
8758 for (idx = 0; idx < symcount; idx++)
8759 {
8760 Elf_Internal_Sym sym;
8761
8762 flagword flags = syms[idx]->flags;
8763 if (!name_local_sections
8764 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8765 {
8766 /* Local section symbols have no name. */
8767 sym.st_name = 0;
8768 }
8769 else
8770 {
8771 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8772 to get the final offset for st_name. */
8773 size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8774 if (stridx == (size_t) -1)
8775 goto error_return;
8776 sym.st_name = stridx;
8777 }
8778
8779 bfd_vma value = syms[idx]->value;
8780 elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8781 asection *sec = syms[idx]->section;
8782
8783 if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8784 {
8785 /* ELF common symbols put the alignment into the `value' field,
8786 and the size into the `size' field. This is backwards from
8787 how BFD handles it, so reverse it here. */
8788 sym.st_size = value;
8789 if (type_ptr == NULL
8790 || type_ptr->internal_elf_sym.st_value == 0)
8791 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8792 else
8793 sym.st_value = type_ptr->internal_elf_sym.st_value;
8794 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8795 }
8796 else
8797 {
8798 unsigned int shndx;
8799
8800 if (sec->output_section)
8801 {
8802 value += sec->output_offset;
8803 sec = sec->output_section;
8804 }
8805
8806 /* Don't add in the section vma for relocatable output. */
8807 if (! relocatable_p)
8808 value += sec->vma;
8809 sym.st_value = value;
8810 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8811
8812 if (bfd_is_abs_section (sec)
8813 && type_ptr != NULL
8814 && type_ptr->internal_elf_sym.st_shndx != 0)
8815 {
8816 /* This symbol is in a real ELF section which we did
8817 not create as a BFD section. Undo the mapping done
8818 by copy_private_symbol_data. */
8819 shndx = type_ptr->internal_elf_sym.st_shndx;
8820 switch (shndx)
8821 {
8822 case MAP_ONESYMTAB:
8823 shndx = elf_onesymtab (abfd);
8824 break;
8825 case MAP_DYNSYMTAB:
8826 shndx = elf_dynsymtab (abfd);
8827 break;
8828 case MAP_STRTAB:
8829 shndx = elf_strtab_sec (abfd);
8830 break;
8831 case MAP_SHSTRTAB:
8832 shndx = elf_shstrtab_sec (abfd);
8833 break;
8834 case MAP_SYM_SHNDX:
8835 if (elf_symtab_shndx_list (abfd))
8836 shndx = elf_symtab_shndx_list (abfd)->ndx;
8837 break;
8838 case SHN_COMMON:
8839 case SHN_ABS:
8840 shndx = SHN_ABS;
8841 break;
8842 default:
8843 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8844 {
8845 if (bed->symbol_section_index)
8846 shndx = bed->symbol_section_index (abfd, type_ptr);
8847 /* Otherwise just leave the index alone. */
8848 }
8849 else
8850 {
8851 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8852 _bfd_error_handler (_("%pB: \
8853 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8854 abfd, shndx);
8855 shndx = SHN_ABS;
8856 }
8857 break;
8858 }
8859 }
8860 else
8861 {
8862 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8863
8864 if (shndx == SHN_BAD)
8865 {
8866 asection *sec2;
8867
8868 /* Writing this would be a hell of a lot easier if
8869 we had some decent documentation on bfd, and
8870 knew what to expect of the library, and what to
8871 demand of applications. For example, it
8872 appears that `objcopy' might not set the
8873 section of a symbol to be a section that is
8874 actually in the output file. */
8875 sec2 = bfd_get_section_by_name (abfd, sec->name);
8876 if (sec2 != NULL)
8877 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8878 if (shndx == SHN_BAD)
8879 {
8880 /* xgettext:c-format */
8881 _bfd_error_handler
8882 (_("unable to find equivalent output section"
8883 " for symbol '%s' from section '%s'"),
8884 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8885 sec->name);
8886 bfd_set_error (bfd_error_invalid_operation);
8887 goto error_return;
8888 }
8889 }
8890 }
8891
8892 sym.st_shndx = shndx;
8893 }
8894
8895 int type;
8896 if ((flags & BSF_THREAD_LOCAL) != 0)
8897 type = STT_TLS;
8898 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8899 type = STT_GNU_IFUNC;
8900 else if ((flags & BSF_FUNCTION) != 0)
8901 type = STT_FUNC;
8902 else if ((flags & BSF_OBJECT) != 0)
8903 type = STT_OBJECT;
8904 else if ((flags & BSF_RELC) != 0)
8905 type = STT_RELC;
8906 else if ((flags & BSF_SRELC) != 0)
8907 type = STT_SRELC;
8908 else
8909 type = STT_NOTYPE;
8910
8911 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8912 type = STT_TLS;
8913
8914 /* Processor-specific types. */
8915 if (type_ptr != NULL
8916 && bed->elf_backend_get_symbol_type)
8917 type = ((*bed->elf_backend_get_symbol_type)
8918 (&type_ptr->internal_elf_sym, type));
8919
8920 if (flags & BSF_SECTION_SYM)
8921 {
8922 if (flags & BSF_GLOBAL)
8923 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8924 else
8925 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8926 }
8927 else if (bfd_is_com_section (syms[idx]->section))
8928 {
8929 if (type != STT_TLS)
8930 {
8931 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8932 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8933 ? STT_COMMON : STT_OBJECT);
8934 else
8935 type = ((flags & BSF_ELF_COMMON) != 0
8936 ? STT_COMMON : STT_OBJECT);
8937 }
8938 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8939 }
8940 else if (bfd_is_und_section (syms[idx]->section))
8941 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8942 ? STB_WEAK
8943 : STB_GLOBAL),
8944 type);
8945 else if (flags & BSF_FILE)
8946 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8947 else
8948 {
8949 int bind = STB_LOCAL;
8950
8951 if (flags & BSF_LOCAL)
8952 bind = STB_LOCAL;
8953 else if (flags & BSF_GNU_UNIQUE)
8954 bind = STB_GNU_UNIQUE;
8955 else if (flags & BSF_WEAK)
8956 bind = STB_WEAK;
8957 else if (flags & BSF_GLOBAL)
8958 bind = STB_GLOBAL;
8959
8960 sym.st_info = ELF_ST_INFO (bind, type);
8961 }
8962
8963 if (type_ptr != NULL)
8964 {
8965 sym.st_other = type_ptr->internal_elf_sym.st_other;
8966 sym.st_target_internal
8967 = type_ptr->internal_elf_sym.st_target_internal;
8968 }
8969 else
8970 {
8971 sym.st_other = 0;
8972 sym.st_target_internal = 0;
8973 }
8974
8975 symstrtab[outbound_syms_index].sym = sym;
8976 symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8977 outbound_syms_index++;
8978 }
8979
8980 /* Finalize the .strtab section. */
8981 _bfd_elf_strtab_finalize (stt);
8982
8983 /* Swap out the .strtab section. */
8984 for (idx = 0; idx < outbound_syms_index; idx++)
8985 {
8986 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8987 if (elfsym->sym.st_name != 0)
8988 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8989 elfsym->sym.st_name);
8990 if (info && info->callbacks->ctf_new_symbol)
8991 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8992 &elfsym->sym);
8993
8994 /* Inform the linker of the addition of this symbol. */
8995
8996 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8997 (outbound_syms
8998 + (elfsym->dest_index
8999 * bed->s->sizeof_sym)),
9000 NPTR_ADD (outbound_shndx,
9001 (elfsym->dest_index
9002 * sizeof (Elf_External_Sym_Shndx))));
9003 }
9004 free (symstrtab);
9005
9006 *sttp = stt;
9007 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9008 symstrtab_hdr->sh_type = SHT_STRTAB;
9009 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9010 symstrtab_hdr->sh_addr = 0;
9011 symstrtab_hdr->sh_entsize = 0;
9012 symstrtab_hdr->sh_link = 0;
9013 symstrtab_hdr->sh_info = 0;
9014 symstrtab_hdr->sh_addralign = 1;
9015
9016 return true;
9017 }
9018
9019 /* Return the number of bytes required to hold the symtab vector.
9020
9021 Note that we base it on the count plus 1, since we will null terminate
9022 the vector allocated based on this size. However, the ELF symbol table
9023 always has a dummy entry as symbol #0, so it ends up even. */
9024
9025 long
9026 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9027 {
9028 bfd_size_type symcount;
9029 long symtab_size;
9030 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9031
9032 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9033 if (symcount > LONG_MAX / sizeof (asymbol *))
9034 {
9035 bfd_set_error (bfd_error_file_too_big);
9036 return -1;
9037 }
9038 symtab_size = symcount * (sizeof (asymbol *));
9039 if (symcount == 0)
9040 symtab_size = sizeof (asymbol *);
9041 else if (!bfd_write_p (abfd))
9042 {
9043 ufile_ptr filesize = bfd_get_file_size (abfd);
9044
9045 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9046 {
9047 bfd_set_error (bfd_error_file_truncated);
9048 return -1;
9049 }
9050 }
9051
9052 return symtab_size;
9053 }
9054
9055 long
9056 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9057 {
9058 bfd_size_type symcount;
9059 long symtab_size;
9060 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9061
9062 if (elf_dynsymtab (abfd) == 0)
9063 {
9064 /* Check if there is dynamic symbol table. */
9065 symcount = elf_tdata (abfd)->dt_symtab_count;
9066 if (symcount)
9067 goto compute_symtab_size;
9068
9069 bfd_set_error (bfd_error_invalid_operation);
9070 return -1;
9071 }
9072
9073 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9074 if (symcount > LONG_MAX / sizeof (asymbol *))
9075 {
9076 bfd_set_error (bfd_error_file_too_big);
9077 return -1;
9078 }
9079
9080 compute_symtab_size:
9081 symtab_size = symcount * (sizeof (asymbol *));
9082 if (symcount == 0)
9083 symtab_size = sizeof (asymbol *);
9084 else if (!bfd_write_p (abfd))
9085 {
9086 ufile_ptr filesize = bfd_get_file_size (abfd);
9087
9088 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9089 {
9090 bfd_set_error (bfd_error_file_truncated);
9091 return -1;
9092 }
9093 }
9094
9095 return symtab_size;
9096 }
9097
9098 long
9099 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9100 {
9101 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9102 {
9103 /* Sanity check reloc section size. */
9104 ufile_ptr filesize = bfd_get_file_size (abfd);
9105
9106 if (filesize != 0)
9107 {
9108 struct bfd_elf_section_data *d = elf_section_data (asect);
9109 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9110 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9111
9112 if (rel_size + rela_size > filesize
9113 || rel_size + rela_size < rel_size)
9114 {
9115 bfd_set_error (bfd_error_file_truncated);
9116 return -1;
9117 }
9118 }
9119 }
9120
9121 #if SIZEOF_LONG == SIZEOF_INT
9122 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9123 {
9124 bfd_set_error (bfd_error_file_too_big);
9125 return -1;
9126 }
9127 #endif
9128 return (asect->reloc_count + 1L) * sizeof (arelent *);
9129 }
9130
9131 /* Canonicalize the relocs. */
9132
9133 long
9134 _bfd_elf_canonicalize_reloc (bfd *abfd,
9135 sec_ptr section,
9136 arelent **relptr,
9137 asymbol **symbols)
9138 {
9139 arelent *tblptr;
9140 unsigned int i;
9141 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9142
9143 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9144 return -1;
9145
9146 tblptr = section->relocation;
9147 for (i = 0; i < section->reloc_count; i++)
9148 *relptr++ = tblptr++;
9149
9150 *relptr = NULL;
9151
9152 return section->reloc_count;
9153 }
9154
9155 long
9156 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9157 {
9158 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9159 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9160
9161 if (symcount >= 0)
9162 abfd->symcount = symcount;
9163 return symcount;
9164 }
9165
9166 long
9167 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9168 asymbol **allocation)
9169 {
9170 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9171 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9172
9173 if (symcount >= 0)
9174 abfd->dynsymcount = symcount;
9175 return symcount;
9176 }
9177
9178 /* Return the size required for the dynamic reloc entries. Any loadable
9179 section that was actually installed in the BFD, and has type SHT_REL
9180 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9181 dynamic reloc section. */
9182
9183 long
9184 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9185 {
9186 bfd_size_type count, ext_rel_size;
9187 asection *s;
9188
9189 if (elf_dynsymtab (abfd) == 0)
9190 {
9191 bfd_set_error (bfd_error_invalid_operation);
9192 return -1;
9193 }
9194
9195 count = 1;
9196 ext_rel_size = 0;
9197 for (s = abfd->sections; s != NULL; s = s->next)
9198 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9199 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9200 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9201 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9202 {
9203 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9204 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9205 {
9206 bfd_set_error (bfd_error_file_truncated);
9207 return -1;
9208 }
9209 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9210 if (count > LONG_MAX / sizeof (arelent *))
9211 {
9212 bfd_set_error (bfd_error_file_too_big);
9213 return -1;
9214 }
9215 }
9216 if (count > 1 && !bfd_write_p (abfd))
9217 {
9218 /* Sanity check reloc section sizes. */
9219 ufile_ptr filesize = bfd_get_file_size (abfd);
9220 if (filesize != 0 && ext_rel_size > filesize)
9221 {
9222 bfd_set_error (bfd_error_file_truncated);
9223 return -1;
9224 }
9225 }
9226 return count * sizeof (arelent *);
9227 }
9228
9229 /* Canonicalize the dynamic relocation entries. Note that we return the
9230 dynamic relocations as a single block, although they are actually
9231 associated with particular sections; the interface, which was
9232 designed for SunOS style shared libraries, expects that there is only
9233 one set of dynamic relocs. Any loadable section that was actually
9234 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9235 dynamic symbol table, is considered to be a dynamic reloc section. */
9236
9237 long
9238 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9239 arelent **storage,
9240 asymbol **syms)
9241 {
9242 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9243 asection *s;
9244 long ret;
9245
9246 if (elf_dynsymtab (abfd) == 0)
9247 {
9248 bfd_set_error (bfd_error_invalid_operation);
9249 return -1;
9250 }
9251
9252 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9253 ret = 0;
9254 for (s = abfd->sections; s != NULL; s = s->next)
9255 {
9256 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9257 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9258 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9259 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9260 {
9261 arelent *p;
9262 long count, i;
9263
9264 if (! (*slurp_relocs) (abfd, s, syms, true))
9265 return -1;
9266 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9267 p = s->relocation;
9268 for (i = 0; i < count; i++)
9269 *storage++ = p++;
9270 ret += count;
9271 }
9272 }
9273
9274 *storage = NULL;
9275
9276 return ret;
9277 }
9278
9279 /* Read in the version information. */
9281
9282 bool
9283 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9284 {
9285 bfd_byte *contents = NULL;
9286 unsigned int freeidx = 0;
9287 size_t amt;
9288 void *contents_addr = NULL;
9289 size_t contents_size = 0;
9290
9291 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9292 {
9293 Elf_Internal_Shdr *hdr;
9294 Elf_External_Verneed *everneed;
9295 Elf_Internal_Verneed *iverneed;
9296 unsigned int i;
9297 bfd_byte *contents_end;
9298 size_t verneed_count;
9299 size_t verneed_size;
9300
9301 if (elf_tdata (abfd)->dt_verneed != NULL)
9302 {
9303 hdr = NULL;
9304 contents = elf_tdata (abfd)->dt_verneed;
9305 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9306 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9307 }
9308 else
9309 {
9310 hdr = &elf_tdata (abfd)->dynverref_hdr;
9311
9312 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9313 {
9314 error_return_bad_verref:
9315 _bfd_error_handler
9316 (_("%pB: .gnu.version_r invalid entry"), abfd);
9317 bfd_set_error (bfd_error_bad_value);
9318 error_return_verref:
9319 elf_tdata (abfd)->verref = NULL;
9320 elf_tdata (abfd)->cverrefs = 0;
9321 goto error_return;
9322 }
9323
9324 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9325 goto error_return_verref;
9326 contents_size = hdr->sh_size;
9327 contents = _bfd_mmap_temporary (abfd, contents_size,
9328 &contents_addr, &contents_size);
9329 if (contents == NULL)
9330 goto error_return_verref;
9331
9332 verneed_size = hdr->sh_size;
9333 verneed_count = hdr->sh_info;
9334 }
9335
9336 if (_bfd_mul_overflow (verneed_count,
9337 sizeof (Elf_Internal_Verneed), &amt))
9338 {
9339 bfd_set_error (bfd_error_file_too_big);
9340 goto error_return_verref;
9341 }
9342 if (amt == 0)
9343 goto error_return_verref;
9344 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9345 if (elf_tdata (abfd)->verref == NULL)
9346 goto error_return_verref;
9347
9348 BFD_ASSERT (sizeof (Elf_External_Verneed)
9349 == sizeof (Elf_External_Vernaux));
9350 contents_end = (contents + verneed_size
9351 - sizeof (Elf_External_Verneed));
9352 everneed = (Elf_External_Verneed *) contents;
9353 iverneed = elf_tdata (abfd)->verref;
9354 for (i = 0; i < verneed_count; i++, iverneed++)
9355 {
9356 Elf_External_Vernaux *evernaux;
9357 Elf_Internal_Vernaux *ivernaux;
9358 unsigned int j;
9359
9360 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9361
9362 iverneed->vn_bfd = abfd;
9363
9364 if (elf_use_dt_symtab_p (abfd))
9365 {
9366 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9367 iverneed->vn_filename
9368 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9369 else
9370 iverneed->vn_filename = NULL;
9371 }
9372 else if (hdr == NULL)
9373 goto error_return_bad_verref;
9374 else
9375 iverneed->vn_filename
9376 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9377 iverneed->vn_file);
9378 if (iverneed->vn_filename == NULL)
9379 goto error_return_bad_verref;
9380
9381 if (iverneed->vn_cnt == 0)
9382 iverneed->vn_auxptr = NULL;
9383 else
9384 {
9385 if (_bfd_mul_overflow (iverneed->vn_cnt,
9386 sizeof (Elf_Internal_Vernaux), &amt))
9387 {
9388 bfd_set_error (bfd_error_file_too_big);
9389 goto error_return_verref;
9390 }
9391 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9392 bfd_alloc (abfd, amt);
9393 if (iverneed->vn_auxptr == NULL)
9394 goto error_return_verref;
9395 }
9396
9397 if (iverneed->vn_aux
9398 > (size_t) (contents_end - (bfd_byte *) everneed))
9399 goto error_return_bad_verref;
9400
9401 evernaux = ((Elf_External_Vernaux *)
9402 ((bfd_byte *) everneed + iverneed->vn_aux));
9403 ivernaux = iverneed->vn_auxptr;
9404 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9405 {
9406 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9407
9408 if (elf_use_dt_symtab_p (abfd))
9409 {
9410 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9411 ivernaux->vna_nodename
9412 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9413 else
9414 ivernaux->vna_nodename = NULL;
9415 }
9416 else if (hdr == NULL)
9417 goto error_return_bad_verref;
9418 else
9419 ivernaux->vna_nodename
9420 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9421 ivernaux->vna_name);
9422 if (ivernaux->vna_nodename == NULL)
9423 goto error_return_bad_verref;
9424
9425 if (ivernaux->vna_other > freeidx)
9426 freeidx = ivernaux->vna_other;
9427
9428 ivernaux->vna_nextptr = NULL;
9429 if (ivernaux->vna_next == 0)
9430 {
9431 iverneed->vn_cnt = j + 1;
9432 break;
9433 }
9434 if (j + 1 < iverneed->vn_cnt)
9435 ivernaux->vna_nextptr = ivernaux + 1;
9436
9437 if (ivernaux->vna_next
9438 > (size_t) (contents_end - (bfd_byte *) evernaux))
9439 goto error_return_bad_verref;
9440
9441 evernaux = ((Elf_External_Vernaux *)
9442 ((bfd_byte *) evernaux + ivernaux->vna_next));
9443 }
9444
9445 iverneed->vn_nextref = NULL;
9446 if (iverneed->vn_next == 0)
9447 break;
9448 if (hdr != NULL && (i + 1 < hdr->sh_info))
9449 iverneed->vn_nextref = iverneed + 1;
9450
9451 if (iverneed->vn_next
9452 > (size_t) (contents_end - (bfd_byte *) everneed))
9453 goto error_return_bad_verref;
9454
9455 everneed = ((Elf_External_Verneed *)
9456 ((bfd_byte *) everneed + iverneed->vn_next));
9457 }
9458 elf_tdata (abfd)->cverrefs = i;
9459
9460 if (contents != elf_tdata (abfd)->dt_verneed)
9461 _bfd_munmap_temporary (contents_addr, contents_size);
9462 contents = NULL;
9463 contents_addr = NULL;
9464 }
9465
9466 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9467 {
9468 Elf_Internal_Shdr *hdr;
9469 Elf_External_Verdef *everdef;
9470 Elf_Internal_Verdef *iverdef;
9471 Elf_Internal_Verdef *iverdefarr;
9472 Elf_Internal_Verdef iverdefmem;
9473 unsigned int i;
9474 unsigned int maxidx;
9475 bfd_byte *contents_end_def, *contents_end_aux;
9476 size_t verdef_count;
9477 size_t verdef_size;
9478
9479 if (elf_tdata (abfd)->dt_verdef != NULL)
9480 {
9481 hdr = NULL;
9482 contents = elf_tdata (abfd)->dt_verdef;
9483 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9484 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9485 }
9486 else
9487 {
9488 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9489
9490 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9491 {
9492 error_return_bad_verdef:
9493 _bfd_error_handler
9494 (_("%pB: .gnu.version_d invalid entry"), abfd);
9495 bfd_set_error (bfd_error_bad_value);
9496 error_return_verdef:
9497 elf_tdata (abfd)->verdef = NULL;
9498 elf_tdata (abfd)->cverdefs = 0;
9499 goto error_return;
9500 }
9501
9502 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9503 goto error_return_verdef;
9504 contents_size = hdr->sh_size;
9505 contents = _bfd_mmap_temporary (abfd, contents_size,
9506 &contents_addr, &contents_size);
9507 if (contents == NULL)
9508 goto error_return_verdef;
9509
9510 BFD_ASSERT (sizeof (Elf_External_Verdef)
9511 >= sizeof (Elf_External_Verdaux));
9512
9513 verdef_count = hdr->sh_info;
9514 verdef_size = hdr->sh_size;
9515 }
9516
9517 contents_end_def = (contents + verdef_size
9518 - sizeof (Elf_External_Verdef));
9519 contents_end_aux = (contents + verdef_size
9520 - sizeof (Elf_External_Verdaux));
9521
9522 /* We know the number of entries in the section but not the maximum
9523 index. Therefore we have to run through all entries and find
9524 the maximum. */
9525 everdef = (Elf_External_Verdef *) contents;
9526 maxidx = 0;
9527 for (i = 0; i < verdef_count; ++i)
9528 {
9529 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9530
9531 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9532 goto error_return_bad_verdef;
9533 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9534 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9535
9536 if (iverdefmem.vd_next == 0)
9537 break;
9538
9539 if (iverdefmem.vd_next
9540 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9541 goto error_return_bad_verdef;
9542
9543 everdef = ((Elf_External_Verdef *)
9544 ((bfd_byte *) everdef + iverdefmem.vd_next));
9545 }
9546
9547 if (default_imported_symver)
9548 {
9549 if (freeidx > maxidx)
9550 maxidx = ++freeidx;
9551 else
9552 freeidx = ++maxidx;
9553 }
9554 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9555 {
9556 bfd_set_error (bfd_error_file_too_big);
9557 goto error_return_verdef;
9558 }
9559
9560 if (amt == 0)
9561 goto error_return_verdef;
9562 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9563 if (elf_tdata (abfd)->verdef == NULL)
9564 goto error_return_verdef;
9565
9566 elf_tdata (abfd)->cverdefs = maxidx;
9567
9568 everdef = (Elf_External_Verdef *) contents;
9569 iverdefarr = elf_tdata (abfd)->verdef;
9570 for (i = 0; i < verdef_count; ++i)
9571 {
9572 Elf_External_Verdaux *everdaux;
9573 Elf_Internal_Verdaux *iverdaux;
9574 unsigned int j;
9575
9576 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9577
9578 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9579 goto error_return_bad_verdef;
9580
9581 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9582 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9583
9584 iverdef->vd_bfd = abfd;
9585
9586 if (iverdef->vd_cnt == 0)
9587 iverdef->vd_auxptr = NULL;
9588 else
9589 {
9590 if (_bfd_mul_overflow (iverdef->vd_cnt,
9591 sizeof (Elf_Internal_Verdaux), &amt))
9592 {
9593 bfd_set_error (bfd_error_file_too_big);
9594 goto error_return_verdef;
9595 }
9596 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9597 bfd_alloc (abfd, amt);
9598 if (iverdef->vd_auxptr == NULL)
9599 goto error_return_verdef;
9600 }
9601
9602 if (iverdef->vd_aux
9603 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9604 goto error_return_bad_verdef;
9605
9606 everdaux = ((Elf_External_Verdaux *)
9607 ((bfd_byte *) everdef + iverdef->vd_aux));
9608 iverdaux = iverdef->vd_auxptr;
9609 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9610 {
9611 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9612
9613 if (elf_use_dt_symtab_p (abfd))
9614 {
9615 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9616 iverdaux->vda_nodename
9617 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9618 else
9619 iverdaux->vda_nodename = NULL;
9620 }
9621 else
9622 iverdaux->vda_nodename
9623 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9624 iverdaux->vda_name);
9625 if (iverdaux->vda_nodename == NULL)
9626 goto error_return_bad_verdef;
9627
9628 iverdaux->vda_nextptr = NULL;
9629 if (iverdaux->vda_next == 0)
9630 {
9631 iverdef->vd_cnt = j + 1;
9632 break;
9633 }
9634 if (j + 1 < iverdef->vd_cnt)
9635 iverdaux->vda_nextptr = iverdaux + 1;
9636
9637 if (iverdaux->vda_next
9638 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9639 goto error_return_bad_verdef;
9640
9641 everdaux = ((Elf_External_Verdaux *)
9642 ((bfd_byte *) everdaux + iverdaux->vda_next));
9643 }
9644
9645 iverdef->vd_nodename = NULL;
9646 if (iverdef->vd_cnt)
9647 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9648
9649 iverdef->vd_nextdef = NULL;
9650 if (iverdef->vd_next == 0)
9651 break;
9652 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9653 iverdef->vd_nextdef = iverdef + 1;
9654
9655 everdef = ((Elf_External_Verdef *)
9656 ((bfd_byte *) everdef + iverdef->vd_next));
9657 }
9658
9659 if (contents != elf_tdata (abfd)->dt_verdef)
9660 _bfd_munmap_temporary (contents_addr, contents_size);
9661 contents = NULL;
9662 contents_addr = NULL;
9663 }
9664 else if (default_imported_symver)
9665 {
9666 if (freeidx < 3)
9667 freeidx = 3;
9668 else
9669 freeidx++;
9670
9671 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9672 {
9673 bfd_set_error (bfd_error_file_too_big);
9674 goto error_return;
9675 }
9676 if (amt == 0)
9677 goto error_return;
9678 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9679 if (elf_tdata (abfd)->verdef == NULL)
9680 goto error_return;
9681
9682 elf_tdata (abfd)->cverdefs = freeidx;
9683 }
9684
9685 /* Create a default version based on the soname. */
9686 if (default_imported_symver)
9687 {
9688 Elf_Internal_Verdef *iverdef;
9689 Elf_Internal_Verdaux *iverdaux;
9690
9691 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9692
9693 iverdef->vd_version = VER_DEF_CURRENT;
9694 iverdef->vd_flags = 0;
9695 iverdef->vd_ndx = freeidx;
9696 iverdef->vd_cnt = 1;
9697
9698 iverdef->vd_bfd = abfd;
9699
9700 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9701 if (iverdef->vd_nodename == NULL)
9702 goto error_return_verdef;
9703 iverdef->vd_nextdef = NULL;
9704 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9705 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9706 if (iverdef->vd_auxptr == NULL)
9707 goto error_return_verdef;
9708
9709 iverdaux = iverdef->vd_auxptr;
9710 iverdaux->vda_nodename = iverdef->vd_nodename;
9711 }
9712
9713 return true;
9714
9715 error_return:
9716 if (contents != elf_tdata (abfd)->dt_verneed
9717 && contents != elf_tdata (abfd)->dt_verdef)
9718 _bfd_munmap_temporary (contents_addr, contents_size);
9719 return false;
9720 }
9721
9722 asymbol *
9724 _bfd_elf_make_empty_symbol (bfd *abfd)
9725 {
9726 elf_symbol_type *newsym;
9727
9728 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9729 if (!newsym)
9730 return NULL;
9731 newsym->symbol.the_bfd = abfd;
9732 return &newsym->symbol;
9733 }
9734
9735 void
9736 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9737 asymbol *symbol,
9738 symbol_info *ret)
9739 {
9740 bfd_symbol_info (symbol, ret);
9741 }
9742
9743 /* Return whether a symbol name implies a local symbol. Most targets
9744 use this function for the is_local_label_name entry point, but some
9745 override it. */
9746
9747 bool
9748 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9749 const char *name)
9750 {
9751 /* Normal local symbols start with ``.L''. */
9752 if (name[0] == '.' && name[1] == 'L')
9753 return true;
9754
9755 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9756 DWARF debugging symbols starting with ``..''. */
9757 if (name[0] == '.' && name[1] == '.')
9758 return true;
9759
9760 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9761 emitting DWARF debugging output. I suspect this is actually a
9762 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9763 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9764 underscore to be emitted on some ELF targets). For ease of use,
9765 we treat such symbols as local. */
9766 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9767 return true;
9768
9769 /* Treat assembler generated fake symbols, dollar local labels and
9770 forward-backward labels (aka local labels) as locals.
9771 These labels have the form:
9772
9773 L0^A.* (fake symbols)
9774
9775 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9776
9777 Versions which start with .L will have already been matched above,
9778 so we only need to match the rest. */
9779 if (name[0] == 'L' && ISDIGIT (name[1]))
9780 {
9781 bool ret = false;
9782 const char * p;
9783 char c;
9784
9785 for (p = name + 2; (c = *p); p++)
9786 {
9787 if (c == 1 || c == 2)
9788 {
9789 if (c == 1 && p == name + 2)
9790 /* A fake symbol. */
9791 return true;
9792
9793 /* FIXME: We are being paranoid here and treating symbols like
9794 L0^Bfoo as if there were non-local, on the grounds that the
9795 assembler will never generate them. But can any symbol
9796 containing an ASCII value in the range 1-31 ever be anything
9797 other than some kind of local ? */
9798 ret = true;
9799 }
9800
9801 if (! ISDIGIT (c))
9802 {
9803 ret = false;
9804 break;
9805 }
9806 }
9807 return ret;
9808 }
9809
9810 return false;
9811 }
9812
9813 alent *
9814 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9815 asymbol *symbol ATTRIBUTE_UNUSED)
9816 {
9817 abort ();
9818 return NULL;
9819 }
9820
9821 bool
9822 _bfd_elf_set_arch_mach (bfd *abfd,
9823 enum bfd_architecture arch,
9824 unsigned long machine)
9825 {
9826 /* If this isn't the right architecture for this backend, and this
9827 isn't the generic backend, fail. */
9828 if (arch != get_elf_backend_data (abfd)->arch
9829 && arch != bfd_arch_unknown
9830 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9831 return false;
9832
9833 return bfd_default_set_arch_mach (abfd, arch, machine);
9834 }
9835
9836 /* Find the nearest line to a particular section and offset,
9837 for error reporting. */
9838
9839 bool
9840 _bfd_elf_find_nearest_line (bfd *abfd,
9841 asymbol **symbols,
9842 asection *section,
9843 bfd_vma offset,
9844 const char **filename_ptr,
9845 const char **functionname_ptr,
9846 unsigned int *line_ptr,
9847 unsigned int *discriminator_ptr)
9848 {
9849 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9850 offset, filename_ptr,
9851 functionname_ptr, line_ptr,
9852 discriminator_ptr);
9853 }
9854
9855 /* Find the nearest line to a particular section and offset,
9856 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9857 can be optionally specified. */
9858
9859 bool
9860 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9861 const char *alt_filename,
9862 asymbol **symbols,
9863 asection *section,
9864 bfd_vma offset,
9865 const char **filename_ptr,
9866 const char **functionname_ptr,
9867 unsigned int *line_ptr,
9868 unsigned int *discriminator_ptr)
9869 {
9870 bool found;
9871
9872 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9873 section, offset, filename_ptr,
9874 functionname_ptr, line_ptr,
9875 discriminator_ptr,
9876 dwarf_debug_sections,
9877 &elf_tdata (abfd)->dwarf2_find_line_info))
9878 return true;
9879
9880 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9881 filename_ptr, functionname_ptr, line_ptr))
9882 {
9883 if (!*functionname_ptr)
9884 _bfd_elf_find_function (abfd, symbols, section, offset,
9885 *filename_ptr ? NULL : filename_ptr,
9886 functionname_ptr);
9887 return true;
9888 }
9889
9890 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9891 &found, filename_ptr,
9892 functionname_ptr, line_ptr,
9893 &elf_tdata (abfd)->line_info))
9894 return false;
9895 if (found && (*functionname_ptr || *line_ptr))
9896 return true;
9897
9898 if (symbols == NULL)
9899 return false;
9900
9901 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9902 filename_ptr, functionname_ptr))
9903 return false;
9904
9905 *line_ptr = 0;
9906 return true;
9907 }
9908
9909 /* Find the line for a symbol. */
9910
9911 bool
9912 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9913 const char **filename_ptr, unsigned int *line_ptr)
9914 {
9915 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9916 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9917 filename_ptr, NULL, line_ptr, NULL,
9918 dwarf_debug_sections,
9919 &tdata->dwarf2_find_line_info);
9920 }
9921
9922 /* After a call to bfd_find_nearest_line, successive calls to
9923 bfd_find_inliner_info can be used to get source information about
9924 each level of function inlining that terminated at the address
9925 passed to bfd_find_nearest_line. Currently this is only supported
9926 for DWARF2 with appropriate DWARF3 extensions. */
9927
9928 bool
9929 _bfd_elf_find_inliner_info (bfd *abfd,
9930 const char **filename_ptr,
9931 const char **functionname_ptr,
9932 unsigned int *line_ptr)
9933 {
9934 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9935 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9936 functionname_ptr, line_ptr,
9937 &tdata->dwarf2_find_line_info);
9938 }
9939
9940 int
9941 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9942 {
9943 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9944 int ret = bed->s->sizeof_ehdr;
9945
9946 if (!bfd_link_relocatable (info))
9947 {
9948 bfd_size_type phdr_size = elf_program_header_size (abfd);
9949
9950 if (phdr_size == (bfd_size_type) -1)
9951 {
9952 struct elf_segment_map *m;
9953
9954 phdr_size = 0;
9955 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9956 phdr_size += bed->s->sizeof_phdr;
9957
9958 if (phdr_size == 0)
9959 phdr_size = get_program_header_size (abfd, info);
9960 }
9961
9962 elf_program_header_size (abfd) = phdr_size;
9963 ret += phdr_size;
9964 }
9965
9966 return ret;
9967 }
9968
9969 bool
9970 _bfd_elf_set_section_contents (bfd *abfd,
9971 sec_ptr section,
9972 const void *location,
9973 file_ptr offset,
9974 bfd_size_type count)
9975 {
9976 Elf_Internal_Shdr *hdr;
9977
9978 if (! abfd->output_has_begun
9979 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9980 return false;
9981
9982 if (!count)
9983 return true;
9984
9985 hdr = &elf_section_data (section)->this_hdr;
9986 if (hdr->sh_offset == (file_ptr) -1)
9987 {
9988 unsigned char *contents;
9989
9990 if (bfd_section_is_ctf (section))
9991 /* Nothing to do with this section: the contents are generated
9992 later. */
9993 return true;
9994
9995 if ((offset + count) > hdr->sh_size)
9996 {
9997 _bfd_error_handler
9998 (_("%pB:%pA: error: attempting to write"
9999 " over the end of the section"),
10000 abfd, section);
10001
10002 bfd_set_error (bfd_error_invalid_operation);
10003 return false;
10004 }
10005
10006 contents = hdr->contents;
10007 if (contents == NULL)
10008 {
10009 _bfd_error_handler
10010 (_("%pB:%pA: error: attempting to write"
10011 " section into an empty buffer"),
10012 abfd, section);
10013
10014 bfd_set_error (bfd_error_invalid_operation);
10015 return false;
10016 }
10017
10018 memcpy (contents + offset, location, count);
10019 return true;
10020 }
10021
10022 return _bfd_generic_set_section_contents (abfd, section,
10023 location, offset, count);
10024 }
10025
10026 bool
10027 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10028 arelent *cache_ptr ATTRIBUTE_UNUSED,
10029 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10030 {
10031 abort ();
10032 return false;
10033 }
10034
10035 /* Try to convert a non-ELF reloc into an ELF one. */
10036
10037 bool
10038 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10039 {
10040 /* Check whether we really have an ELF howto. */
10041
10042 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10043 {
10044 bfd_reloc_code_real_type code;
10045 reloc_howto_type *howto;
10046
10047 /* Alien reloc: Try to determine its type to replace it with an
10048 equivalent ELF reloc. */
10049
10050 if (areloc->howto->pc_relative)
10051 {
10052 switch (areloc->howto->bitsize)
10053 {
10054 case 8:
10055 code = BFD_RELOC_8_PCREL;
10056 break;
10057 case 12:
10058 code = BFD_RELOC_12_PCREL;
10059 break;
10060 case 16:
10061 code = BFD_RELOC_16_PCREL;
10062 break;
10063 case 24:
10064 code = BFD_RELOC_24_PCREL;
10065 break;
10066 case 32:
10067 code = BFD_RELOC_32_PCREL;
10068 break;
10069 case 64:
10070 code = BFD_RELOC_64_PCREL;
10071 break;
10072 default:
10073 goto fail;
10074 }
10075
10076 howto = bfd_reloc_type_lookup (abfd, code);
10077
10078 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10079 {
10080 if (howto->pcrel_offset)
10081 areloc->addend += areloc->address;
10082 else
10083 areloc->addend -= areloc->address; /* addend is unsigned!! */
10084 }
10085 }
10086 else
10087 {
10088 switch (areloc->howto->bitsize)
10089 {
10090 case 8:
10091 code = BFD_RELOC_8;
10092 break;
10093 case 14:
10094 code = BFD_RELOC_14;
10095 break;
10096 case 16:
10097 code = BFD_RELOC_16;
10098 break;
10099 case 26:
10100 code = BFD_RELOC_26;
10101 break;
10102 case 32:
10103 code = BFD_RELOC_32;
10104 break;
10105 case 64:
10106 code = BFD_RELOC_64;
10107 break;
10108 default:
10109 goto fail;
10110 }
10111
10112 howto = bfd_reloc_type_lookup (abfd, code);
10113 }
10114
10115 if (howto)
10116 areloc->howto = howto;
10117 else
10118 goto fail;
10119 }
10120
10121 return true;
10122
10123 fail:
10124 /* xgettext:c-format */
10125 _bfd_error_handler (_("%pB: %s unsupported"),
10126 abfd, areloc->howto->name);
10127 bfd_set_error (bfd_error_sorry);
10128 return false;
10129 }
10130
10131 bool
10132 _bfd_elf_free_cached_info (bfd *abfd)
10133 {
10134 struct elf_obj_tdata *tdata;
10135
10136 if ((bfd_get_format (abfd) == bfd_object
10137 || bfd_get_format (abfd) == bfd_core)
10138 && (tdata = elf_tdata (abfd)) != NULL)
10139 {
10140 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10141 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10142 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10143 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10144 _bfd_stab_cleanup (abfd, &tdata->line_info);
10145 for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10146 {
10147 _bfd_elf_munmap_section_contents (sec, sec->contents);
10148 if (!sec->alloced)
10149 {
10150 free (elf_section_data (sec)->this_hdr.contents);
10151 elf_section_data (sec)->this_hdr.contents = NULL;
10152 }
10153 free (elf_section_data (sec)->relocs);
10154 elf_section_data (sec)->relocs = NULL;
10155 if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10156 {
10157 struct eh_frame_sec_info *sec_info
10158 = elf_section_data (sec)->sec_info;
10159 free (sec_info->cies);
10160 }
10161 }
10162 free (tdata->symtab_hdr.contents);
10163 tdata->symtab_hdr.contents = NULL;
10164 }
10165
10166 return _bfd_generic_bfd_free_cached_info (abfd);
10167 }
10168
10169 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10170 in the relocation's offset. Thus we cannot allow any sort of sanity
10171 range-checking to interfere. There is nothing else to do in processing
10172 this reloc. */
10173
10174 bfd_reloc_status_type
10175 _bfd_elf_rel_vtable_reloc_fn
10176 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10177 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10178 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10179 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10180 {
10181 return bfd_reloc_ok;
10182 }
10183
10184 /* Elf core file support. Much of this only works on native
10186 toolchains, since we rely on knowing the
10187 machine-dependent procfs structure in order to pick
10188 out details about the corefile. */
10189
10190 #ifdef HAVE_SYS_PROCFS_H
10191 # include <sys/procfs.h>
10192 #endif
10193
10194 /* Return a PID that identifies a "thread" for threaded cores, or the
10195 PID of the main process for non-threaded cores. */
10196
10197 static int
10198 elfcore_make_pid (bfd *abfd)
10199 {
10200 int pid;
10201
10202 pid = elf_tdata (abfd)->core->lwpid;
10203 if (pid == 0)
10204 pid = elf_tdata (abfd)->core->pid;
10205
10206 return pid;
10207 }
10208
10209 /* If there isn't a section called NAME, make one, using data from
10210 SECT. Note, this function will generate a reference to NAME, so
10211 you shouldn't deallocate or overwrite it. */
10212
10213 static bool
10214 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10215 {
10216 asection *sect2;
10217
10218 if (bfd_get_section_by_name (abfd, name) != NULL)
10219 return true;
10220
10221 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10222 if (sect2 == NULL)
10223 return false;
10224
10225 sect2->size = sect->size;
10226 sect2->filepos = sect->filepos;
10227 sect2->alignment_power = sect->alignment_power;
10228 return true;
10229 }
10230
10231 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10232 actually creates up to two pseudosections:
10233 - For the single-threaded case, a section named NAME, unless
10234 such a section already exists.
10235 - For the multi-threaded case, a section named "NAME/PID", where
10236 PID is elfcore_make_pid (abfd).
10237 Both pseudosections have identical contents. */
10238 bool
10239 _bfd_elfcore_make_pseudosection (bfd *abfd,
10240 char *name,
10241 size_t size,
10242 ufile_ptr filepos)
10243 {
10244 char buf[100];
10245 char *threaded_name;
10246 size_t len;
10247 asection *sect;
10248
10249 /* Build the section name. */
10250
10251 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10252 len = strlen (buf) + 1;
10253 threaded_name = (char *) bfd_alloc (abfd, len);
10254 if (threaded_name == NULL)
10255 return false;
10256 memcpy (threaded_name, buf, len);
10257
10258 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10259 SEC_HAS_CONTENTS);
10260 if (sect == NULL)
10261 return false;
10262 sect->size = size;
10263 sect->filepos = filepos;
10264 sect->alignment_power = 2;
10265
10266 return elfcore_maybe_make_sect (abfd, name, sect);
10267 }
10268
10269 static bool
10270 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10271 size_t offs)
10272 {
10273 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10274 SEC_HAS_CONTENTS);
10275
10276 if (sect == NULL)
10277 return false;
10278
10279 sect->size = note->descsz - offs;
10280 sect->filepos = note->descpos + offs;
10281 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10282
10283 return true;
10284 }
10285
10286 /* prstatus_t exists on:
10287 solaris 2.5+
10288 linux 2.[01] + glibc
10289 unixware 4.2
10290 */
10291
10292 #if defined (HAVE_PRSTATUS_T)
10293
10294 static bool
10295 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10296 {
10297 size_t size;
10298 int offset;
10299
10300 if (note->descsz == sizeof (prstatus_t))
10301 {
10302 prstatus_t prstat;
10303
10304 size = sizeof (prstat.pr_reg);
10305 offset = offsetof (prstatus_t, pr_reg);
10306 memcpy (&prstat, note->descdata, sizeof (prstat));
10307
10308 /* Do not overwrite the core signal if it
10309 has already been set by another thread. */
10310 if (elf_tdata (abfd)->core->signal == 0)
10311 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10312 if (elf_tdata (abfd)->core->pid == 0)
10313 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10314
10315 /* pr_who exists on:
10316 solaris 2.5+
10317 unixware 4.2
10318 pr_who doesn't exist on:
10319 linux 2.[01]
10320 */
10321 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10322 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10323 #else
10324 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10325 #endif
10326 }
10327 #if defined (HAVE_PRSTATUS32_T)
10328 else if (note->descsz == sizeof (prstatus32_t))
10329 {
10330 /* 64-bit host, 32-bit corefile */
10331 prstatus32_t prstat;
10332
10333 size = sizeof (prstat.pr_reg);
10334 offset = offsetof (prstatus32_t, pr_reg);
10335 memcpy (&prstat, note->descdata, sizeof (prstat));
10336
10337 /* Do not overwrite the core signal if it
10338 has already been set by another thread. */
10339 if (elf_tdata (abfd)->core->signal == 0)
10340 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10341 if (elf_tdata (abfd)->core->pid == 0)
10342 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10343
10344 /* pr_who exists on:
10345 solaris 2.5+
10346 unixware 4.2
10347 pr_who doesn't exist on:
10348 linux 2.[01]
10349 */
10350 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10351 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10352 #else
10353 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10354 #endif
10355 }
10356 #endif /* HAVE_PRSTATUS32_T */
10357 else
10358 {
10359 /* Fail - we don't know how to handle any other
10360 note size (ie. data object type). */
10361 return true;
10362 }
10363
10364 /* Make a ".reg/999" section and a ".reg" section. */
10365 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10366 size, note->descpos + offset);
10367 }
10368 #endif /* defined (HAVE_PRSTATUS_T) */
10369
10370 /* Create a pseudosection containing the exact contents of NOTE. */
10371 static bool
10372 elfcore_make_note_pseudosection (bfd *abfd,
10373 char *name,
10374 Elf_Internal_Note *note)
10375 {
10376 return _bfd_elfcore_make_pseudosection (abfd, name,
10377 note->descsz, note->descpos);
10378 }
10379
10380 /* There isn't a consistent prfpregset_t across platforms,
10381 but it doesn't matter, because we don't have to pick this
10382 data structure apart. */
10383
10384 static bool
10385 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10386 {
10387 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10388 }
10389
10390 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10391 type of NT_PRXFPREG. Just include the whole note's contents
10392 literally. */
10393
10394 static bool
10395 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10396 {
10397 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10398 }
10399
10400 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10401 with a note type of NT_X86_XSTATE. Just include the whole note's
10402 contents literally. */
10403
10404 static bool
10405 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10406 {
10407 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10408 }
10409
10410 static bool
10411 elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10412 {
10413 return elfcore_make_note_pseudosection (abfd, ".reg-ssp", note);
10414 }
10415
10416 static bool
10417 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10418 {
10419 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10420 }
10421
10422 static bool
10423 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10424 {
10425 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10426 }
10427
10428 static bool
10429 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10430 {
10431 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10432 }
10433
10434 static bool
10435 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10436 {
10437 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10438 }
10439
10440 static bool
10441 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10442 {
10443 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10444 }
10445
10446 static bool
10447 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10448 {
10449 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10450 }
10451
10452 static bool
10453 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10454 {
10455 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10456 }
10457
10458 static bool
10459 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10460 {
10461 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10462 }
10463
10464 static bool
10465 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10466 {
10467 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10468 }
10469
10470 static bool
10471 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10472 {
10473 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10474 }
10475
10476 static bool
10477 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10478 {
10479 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10480 }
10481
10482 static bool
10483 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10484 {
10485 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10486 }
10487
10488 static bool
10489 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10490 {
10491 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10492 }
10493
10494 static bool
10495 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10496 {
10497 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10498 }
10499
10500 static bool
10501 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10502 {
10503 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10504 }
10505
10506 static bool
10507 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10508 {
10509 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10510 }
10511
10512 static bool
10513 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10514 {
10515 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10516 }
10517
10518 static bool
10519 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10520 {
10521 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10522 }
10523
10524 static bool
10525 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10526 {
10527 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10528 }
10529
10530 static bool
10531 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10532 {
10533 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10534 }
10535
10536 static bool
10537 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10538 {
10539 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10540 }
10541
10542 static bool
10543 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10544 {
10545 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10546 }
10547
10548 static bool
10549 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10550 {
10551 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10552 }
10553
10554 static bool
10555 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10556 {
10557 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10558 }
10559
10560 static bool
10561 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10562 {
10563 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10564 }
10565
10566 static bool
10567 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10568 {
10569 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10570 }
10571
10572 static bool
10573 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10574 {
10575 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10576 }
10577
10578 static bool
10579 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10580 {
10581 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10582 }
10583
10584 static bool
10585 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10586 {
10587 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10588 }
10589
10590 static bool
10591 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10592 {
10593 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10594 }
10595
10596 static bool
10597 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10598 {
10599 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10600 }
10601
10602 static bool
10603 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10604 {
10605 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10606 }
10607
10608 static bool
10609 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10610 {
10611 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10612 }
10613
10614 static bool
10615 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10616 {
10617 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10618 }
10619
10620 static bool
10621 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10622 {
10623 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10624 note);
10625 }
10626
10627 static bool
10628 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10629 {
10630 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10631 }
10632
10633 static bool
10634 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10635 {
10636 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10637 }
10638
10639 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10640 successful, otherwise return FALSE. */
10641
10642 static bool
10643 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10644 {
10645 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10646 }
10647
10648 /* Convert NOTE into a bfd_section called ".reg-aarch-gcs". Return TRUE if
10649 successful, otherwise return FALSE. */
10650
10651 static bool
10652 elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10653 {
10654 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-gcs", note);
10655 }
10656
10657 static bool
10658 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10659 {
10660 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10661 }
10662
10663 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10664 successful otherwise, return FALSE. */
10665
10666 static bool
10667 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10668 {
10669 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10670 }
10671
10672 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10673 successful otherwise, return FALSE. */
10674
10675 static bool
10676 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10677 {
10678 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10679 }
10680
10681 static bool
10682 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10683 {
10684 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10685 }
10686
10687 static bool
10688 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10689 {
10690 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10691 }
10692
10693 static bool
10694 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10695 {
10696 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10697 }
10698
10699 static bool
10700 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10701 {
10702 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10703 }
10704
10705 #if defined (HAVE_PRPSINFO_T)
10706 typedef prpsinfo_t elfcore_psinfo_t;
10707 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10708 typedef prpsinfo32_t elfcore_psinfo32_t;
10709 #endif
10710 #endif
10711
10712 #if defined (HAVE_PSINFO_T)
10713 typedef psinfo_t elfcore_psinfo_t;
10714 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10715 typedef psinfo32_t elfcore_psinfo32_t;
10716 #endif
10717 #endif
10718
10719 /* return a malloc'ed copy of a string at START which is at
10720 most MAX bytes long, possibly without a terminating '\0'.
10721 the copy will always have a terminating '\0'. */
10722
10723 char *
10724 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10725 {
10726 char *dups;
10727 char *end = (char *) memchr (start, '\0', max);
10728 size_t len;
10729
10730 if (end == NULL)
10731 len = max;
10732 else
10733 len = end - start;
10734
10735 dups = (char *) bfd_alloc (abfd, len + 1);
10736 if (dups == NULL)
10737 return NULL;
10738
10739 memcpy (dups, start, len);
10740 dups[len] = '\0';
10741
10742 return dups;
10743 }
10744
10745 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10746 static bool
10747 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10748 {
10749 if (note->descsz == sizeof (elfcore_psinfo_t))
10750 {
10751 elfcore_psinfo_t psinfo;
10752
10753 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10754
10755 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10756 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10757 #endif
10758 elf_tdata (abfd)->core->program
10759 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10760 sizeof (psinfo.pr_fname));
10761
10762 elf_tdata (abfd)->core->command
10763 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10764 sizeof (psinfo.pr_psargs));
10765 }
10766 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10767 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10768 {
10769 /* 64-bit host, 32-bit corefile */
10770 elfcore_psinfo32_t psinfo;
10771
10772 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10773
10774 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10775 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10776 #endif
10777 elf_tdata (abfd)->core->program
10778 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10779 sizeof (psinfo.pr_fname));
10780
10781 elf_tdata (abfd)->core->command
10782 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10783 sizeof (psinfo.pr_psargs));
10784 }
10785 #endif
10786
10787 else
10788 {
10789 /* Fail - we don't know how to handle any other
10790 note size (ie. data object type). */
10791 return true;
10792 }
10793
10794 /* Note that for some reason, a spurious space is tacked
10795 onto the end of the args in some (at least one anyway)
10796 implementations, so strip it off if it exists. */
10797
10798 {
10799 char *command = elf_tdata (abfd)->core->command;
10800 int n = strlen (command);
10801
10802 if (0 < n && command[n - 1] == ' ')
10803 command[n - 1] = '\0';
10804 }
10805
10806 return true;
10807 }
10808 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10809
10810 #if defined (HAVE_PSTATUS_T)
10811 static bool
10812 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10813 {
10814 if (note->descsz == sizeof (pstatus_t)
10815 #if defined (HAVE_PXSTATUS_T)
10816 || note->descsz == sizeof (pxstatus_t)
10817 #endif
10818 )
10819 {
10820 pstatus_t pstat;
10821
10822 memcpy (&pstat, note->descdata, sizeof (pstat));
10823
10824 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10825 }
10826 #if defined (HAVE_PSTATUS32_T)
10827 else if (note->descsz == sizeof (pstatus32_t))
10828 {
10829 /* 64-bit host, 32-bit corefile */
10830 pstatus32_t pstat;
10831
10832 memcpy (&pstat, note->descdata, sizeof (pstat));
10833
10834 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10835 }
10836 #endif
10837 /* Could grab some more details from the "representative"
10838 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10839 NT_LWPSTATUS note, presumably. */
10840
10841 return true;
10842 }
10843 #endif /* defined (HAVE_PSTATUS_T) */
10844
10845 #if defined (HAVE_LWPSTATUS_T)
10846 static bool
10847 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10848 {
10849 lwpstatus_t lwpstat;
10850 char buf[100];
10851 char *name;
10852 size_t len;
10853 asection *sect;
10854
10855 if (note->descsz != sizeof (lwpstat)
10856 #if defined (HAVE_LWPXSTATUS_T)
10857 && note->descsz != sizeof (lwpxstatus_t)
10858 #endif
10859 )
10860 return true;
10861
10862 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10863
10864 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10865 /* Do not overwrite the core signal if it has already been set by
10866 another thread. */
10867 if (elf_tdata (abfd)->core->signal == 0)
10868 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10869
10870 /* Make a ".reg/999" section. */
10871
10872 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10873 len = strlen (buf) + 1;
10874 name = bfd_alloc (abfd, len);
10875 if (name == NULL)
10876 return false;
10877 memcpy (name, buf, len);
10878
10879 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10880 if (sect == NULL)
10881 return false;
10882
10883 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10884 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10885 sect->filepos = note->descpos
10886 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10887 #endif
10888
10889 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10890 sect->size = sizeof (lwpstat.pr_reg);
10891 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10892 #endif
10893
10894 sect->alignment_power = 2;
10895
10896 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10897 return false;
10898
10899 /* Make a ".reg2/999" section */
10900
10901 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10902 len = strlen (buf) + 1;
10903 name = bfd_alloc (abfd, len);
10904 if (name == NULL)
10905 return false;
10906 memcpy (name, buf, len);
10907
10908 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10909 if (sect == NULL)
10910 return false;
10911
10912 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10913 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10914 sect->filepos = note->descpos
10915 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10916 #endif
10917
10918 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10919 sect->size = sizeof (lwpstat.pr_fpreg);
10920 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10921 #endif
10922
10923 sect->alignment_power = 2;
10924
10925 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10926 }
10927 #endif /* defined (HAVE_LWPSTATUS_T) */
10928
10929 /* These constants, and the structure offsets used below, are defined by
10930 Cygwin's core_dump.h */
10931 #define NOTE_INFO_PROCESS 1
10932 #define NOTE_INFO_THREAD 2
10933 #define NOTE_INFO_MODULE 3
10934 #define NOTE_INFO_MODULE64 4
10935
10936 static bool
10937 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10938 {
10939 char buf[30];
10940 char *name;
10941 size_t len;
10942 unsigned int name_size;
10943 asection *sect;
10944 unsigned int type;
10945 int is_active_thread;
10946 bfd_vma base_addr;
10947
10948 if (note->descsz < 4)
10949 return true;
10950
10951 if (! startswith (note->namedata, "win32"))
10952 return true;
10953
10954 type = bfd_get_32 (abfd, note->descdata);
10955
10956 struct
10957 {
10958 const char *type_name;
10959 unsigned long min_size;
10960 } size_check[] =
10961 {
10962 { "NOTE_INFO_PROCESS", 12 },
10963 { "NOTE_INFO_THREAD", 12 },
10964 { "NOTE_INFO_MODULE", 12 },
10965 { "NOTE_INFO_MODULE64", 16 },
10966 };
10967
10968 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10969 return true;
10970
10971 if (note->descsz < size_check[type - 1].min_size)
10972 {
10973 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10974 " is too small"),
10975 abfd, size_check[type - 1].type_name, note->descsz);
10976 return true;
10977 }
10978
10979 switch (type)
10980 {
10981 case NOTE_INFO_PROCESS:
10982 /* FIXME: need to add ->core->command. */
10983 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10984 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10985 break;
10986
10987 case NOTE_INFO_THREAD:
10988 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10989 structure. */
10990 /* thread_info.tid */
10991 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10992
10993 len = strlen (buf) + 1;
10994 name = (char *) bfd_alloc (abfd, len);
10995 if (name == NULL)
10996 return false;
10997
10998 memcpy (name, buf, len);
10999
11000 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11001 if (sect == NULL)
11002 return false;
11003
11004 /* sizeof (thread_info.thread_context) */
11005 sect->size = note->descsz - 12;
11006 /* offsetof (thread_info.thread_context) */
11007 sect->filepos = note->descpos + 12;
11008 sect->alignment_power = 2;
11009
11010 /* thread_info.is_active_thread */
11011 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11012
11013 if (is_active_thread)
11014 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11015 return false;
11016 break;
11017
11018 case NOTE_INFO_MODULE:
11019 case NOTE_INFO_MODULE64:
11020 /* Make a ".module/xxxxxxxx" section. */
11021 if (type == NOTE_INFO_MODULE)
11022 {
11023 /* module_info.base_address */
11024 base_addr = bfd_get_32 (abfd, note->descdata + 4);
11025 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11026 /* module_info.module_name_size */
11027 name_size = bfd_get_32 (abfd, note->descdata + 8);
11028 }
11029 else /* NOTE_INFO_MODULE64 */
11030 {
11031 /* module_info.base_address */
11032 base_addr = bfd_get_64 (abfd, note->descdata + 4);
11033 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11034 /* module_info.module_name_size */
11035 name_size = bfd_get_32 (abfd, note->descdata + 12);
11036 }
11037
11038 len = strlen (buf) + 1;
11039 name = (char *) bfd_alloc (abfd, len);
11040 if (name == NULL)
11041 return false;
11042
11043 memcpy (name, buf, len);
11044
11045 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11046
11047 if (sect == NULL)
11048 return false;
11049
11050 if (note->descsz < 12 + name_size)
11051 {
11052 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11053 " is too small to contain a name of size %u"),
11054 abfd, note->descsz, name_size);
11055 return true;
11056 }
11057
11058 sect->size = note->descsz;
11059 sect->filepos = note->descpos;
11060 sect->alignment_power = 2;
11061 break;
11062
11063 default:
11064 return true;
11065 }
11066
11067 return true;
11068 }
11069
11070 static bool
11071 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11072 {
11073 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11074
11075 switch (note->type)
11076 {
11077 default:
11078 return true;
11079
11080 case NT_PRSTATUS:
11081 if (bed->elf_backend_grok_prstatus)
11082 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11083 return true;
11084 #if defined (HAVE_PRSTATUS_T)
11085 return elfcore_grok_prstatus (abfd, note);
11086 #else
11087 return true;
11088 #endif
11089
11090 #if defined (HAVE_PSTATUS_T)
11091 case NT_PSTATUS:
11092 return elfcore_grok_pstatus (abfd, note);
11093 #endif
11094
11095 #if defined (HAVE_LWPSTATUS_T)
11096 case NT_LWPSTATUS:
11097 return elfcore_grok_lwpstatus (abfd, note);
11098 #endif
11099
11100 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11101 return elfcore_grok_prfpreg (abfd, note);
11102
11103 case NT_WIN32PSTATUS:
11104 return elfcore_grok_win32pstatus (abfd, note);
11105
11106 case NT_PRXFPREG: /* Linux SSE extension */
11107 if (note->namesz == 6
11108 && strcmp (note->namedata, "LINUX") == 0)
11109 return elfcore_grok_prxfpreg (abfd, note);
11110 else
11111 return true;
11112
11113 case NT_X86_XSTATE: /* Linux XSAVE extension */
11114 if (note->namesz == 6
11115 && strcmp (note->namedata, "LINUX") == 0)
11116 return elfcore_grok_xstatereg (abfd, note);
11117 else
11118 return true;
11119
11120 case NT_X86_SHSTK: /* Linux CET extension. */
11121 if (note->namesz == 6
11122 && strcmp (note->namedata, "LINUX") == 0)
11123 return elfcore_grok_sspreg (abfd, note);
11124 else
11125 return true;
11126
11127 case NT_PPC_VMX:
11128 if (note->namesz == 6
11129 && strcmp (note->namedata, "LINUX") == 0)
11130 return elfcore_grok_ppc_vmx (abfd, note);
11131 else
11132 return true;
11133
11134 case NT_PPC_VSX:
11135 if (note->namesz == 6
11136 && strcmp (note->namedata, "LINUX") == 0)
11137 return elfcore_grok_ppc_vsx (abfd, note);
11138 else
11139 return true;
11140
11141 case NT_PPC_TAR:
11142 if (note->namesz == 6
11143 && strcmp (note->namedata, "LINUX") == 0)
11144 return elfcore_grok_ppc_tar (abfd, note);
11145 else
11146 return true;
11147
11148 case NT_PPC_PPR:
11149 if (note->namesz == 6
11150 && strcmp (note->namedata, "LINUX") == 0)
11151 return elfcore_grok_ppc_ppr (abfd, note);
11152 else
11153 return true;
11154
11155 case NT_PPC_DSCR:
11156 if (note->namesz == 6
11157 && strcmp (note->namedata, "LINUX") == 0)
11158 return elfcore_grok_ppc_dscr (abfd, note);
11159 else
11160 return true;
11161
11162 case NT_PPC_EBB:
11163 if (note->namesz == 6
11164 && strcmp (note->namedata, "LINUX") == 0)
11165 return elfcore_grok_ppc_ebb (abfd, note);
11166 else
11167 return true;
11168
11169 case NT_PPC_PMU:
11170 if (note->namesz == 6
11171 && strcmp (note->namedata, "LINUX") == 0)
11172 return elfcore_grok_ppc_pmu (abfd, note);
11173 else
11174 return true;
11175
11176 case NT_PPC_TM_CGPR:
11177 if (note->namesz == 6
11178 && strcmp (note->namedata, "LINUX") == 0)
11179 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11180 else
11181 return true;
11182
11183 case NT_PPC_TM_CFPR:
11184 if (note->namesz == 6
11185 && strcmp (note->namedata, "LINUX") == 0)
11186 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11187 else
11188 return true;
11189
11190 case NT_PPC_TM_CVMX:
11191 if (note->namesz == 6
11192 && strcmp (note->namedata, "LINUX") == 0)
11193 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11194 else
11195 return true;
11196
11197 case NT_PPC_TM_CVSX:
11198 if (note->namesz == 6
11199 && strcmp (note->namedata, "LINUX") == 0)
11200 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11201 else
11202 return true;
11203
11204 case NT_PPC_TM_SPR:
11205 if (note->namesz == 6
11206 && strcmp (note->namedata, "LINUX") == 0)
11207 return elfcore_grok_ppc_tm_spr (abfd, note);
11208 else
11209 return true;
11210
11211 case NT_PPC_TM_CTAR:
11212 if (note->namesz == 6
11213 && strcmp (note->namedata, "LINUX") == 0)
11214 return elfcore_grok_ppc_tm_ctar (abfd, note);
11215 else
11216 return true;
11217
11218 case NT_PPC_TM_CPPR:
11219 if (note->namesz == 6
11220 && strcmp (note->namedata, "LINUX") == 0)
11221 return elfcore_grok_ppc_tm_cppr (abfd, note);
11222 else
11223 return true;
11224
11225 case NT_PPC_TM_CDSCR:
11226 if (note->namesz == 6
11227 && strcmp (note->namedata, "LINUX") == 0)
11228 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11229 else
11230 return true;
11231
11232 case NT_S390_HIGH_GPRS:
11233 if (note->namesz == 6
11234 && strcmp (note->namedata, "LINUX") == 0)
11235 return elfcore_grok_s390_high_gprs (abfd, note);
11236 else
11237 return true;
11238
11239 case NT_S390_TIMER:
11240 if (note->namesz == 6
11241 && strcmp (note->namedata, "LINUX") == 0)
11242 return elfcore_grok_s390_timer (abfd, note);
11243 else
11244 return true;
11245
11246 case NT_S390_TODCMP:
11247 if (note->namesz == 6
11248 && strcmp (note->namedata, "LINUX") == 0)
11249 return elfcore_grok_s390_todcmp (abfd, note);
11250 else
11251 return true;
11252
11253 case NT_S390_TODPREG:
11254 if (note->namesz == 6
11255 && strcmp (note->namedata, "LINUX") == 0)
11256 return elfcore_grok_s390_todpreg (abfd, note);
11257 else
11258 return true;
11259
11260 case NT_S390_CTRS:
11261 if (note->namesz == 6
11262 && strcmp (note->namedata, "LINUX") == 0)
11263 return elfcore_grok_s390_ctrs (abfd, note);
11264 else
11265 return true;
11266
11267 case NT_S390_PREFIX:
11268 if (note->namesz == 6
11269 && strcmp (note->namedata, "LINUX") == 0)
11270 return elfcore_grok_s390_prefix (abfd, note);
11271 else
11272 return true;
11273
11274 case NT_S390_LAST_BREAK:
11275 if (note->namesz == 6
11276 && strcmp (note->namedata, "LINUX") == 0)
11277 return elfcore_grok_s390_last_break (abfd, note);
11278 else
11279 return true;
11280
11281 case NT_S390_SYSTEM_CALL:
11282 if (note->namesz == 6
11283 && strcmp (note->namedata, "LINUX") == 0)
11284 return elfcore_grok_s390_system_call (abfd, note);
11285 else
11286 return true;
11287
11288 case NT_S390_TDB:
11289 if (note->namesz == 6
11290 && strcmp (note->namedata, "LINUX") == 0)
11291 return elfcore_grok_s390_tdb (abfd, note);
11292 else
11293 return true;
11294
11295 case NT_S390_VXRS_LOW:
11296 if (note->namesz == 6
11297 && strcmp (note->namedata, "LINUX") == 0)
11298 return elfcore_grok_s390_vxrs_low (abfd, note);
11299 else
11300 return true;
11301
11302 case NT_S390_VXRS_HIGH:
11303 if (note->namesz == 6
11304 && strcmp (note->namedata, "LINUX") == 0)
11305 return elfcore_grok_s390_vxrs_high (abfd, note);
11306 else
11307 return true;
11308
11309 case NT_S390_GS_CB:
11310 if (note->namesz == 6
11311 && strcmp (note->namedata, "LINUX") == 0)
11312 return elfcore_grok_s390_gs_cb (abfd, note);
11313 else
11314 return true;
11315
11316 case NT_S390_GS_BC:
11317 if (note->namesz == 6
11318 && strcmp (note->namedata, "LINUX") == 0)
11319 return elfcore_grok_s390_gs_bc (abfd, note);
11320 else
11321 return true;
11322
11323 case NT_ARC_V2:
11324 if (note->namesz == 6
11325 && strcmp (note->namedata, "LINUX") == 0)
11326 return elfcore_grok_arc_v2 (abfd, note);
11327 else
11328 return true;
11329
11330 case NT_ARM_VFP:
11331 if (note->namesz == 6
11332 && strcmp (note->namedata, "LINUX") == 0)
11333 return elfcore_grok_arm_vfp (abfd, note);
11334 else
11335 return true;
11336
11337 case NT_ARM_TLS:
11338 if (note->namesz == 6
11339 && strcmp (note->namedata, "LINUX") == 0)
11340 return elfcore_grok_aarch_tls (abfd, note);
11341 else
11342 return true;
11343
11344 case NT_ARM_HW_BREAK:
11345 if (note->namesz == 6
11346 && strcmp (note->namedata, "LINUX") == 0)
11347 return elfcore_grok_aarch_hw_break (abfd, note);
11348 else
11349 return true;
11350
11351 case NT_ARM_HW_WATCH:
11352 if (note->namesz == 6
11353 && strcmp (note->namedata, "LINUX") == 0)
11354 return elfcore_grok_aarch_hw_watch (abfd, note);
11355 else
11356 return true;
11357
11358 case NT_ARM_SVE:
11359 if (note->namesz == 6
11360 && strcmp (note->namedata, "LINUX") == 0)
11361 return elfcore_grok_aarch_sve (abfd, note);
11362 else
11363 return true;
11364
11365 case NT_ARM_PAC_MASK:
11366 if (note->namesz == 6
11367 && strcmp (note->namedata, "LINUX") == 0)
11368 return elfcore_grok_aarch_pauth (abfd, note);
11369 else
11370 return true;
11371
11372 case NT_ARM_TAGGED_ADDR_CTRL:
11373 if (note->namesz == 6
11374 && strcmp (note->namedata, "LINUX") == 0)
11375 return elfcore_grok_aarch_mte (abfd, note);
11376 else
11377 return true;
11378
11379 case NT_ARM_SSVE:
11380 if (note->namesz == 6
11381 && strcmp (note->namedata, "LINUX") == 0)
11382 return elfcore_grok_aarch_ssve (abfd, note);
11383 else
11384 return true;
11385
11386 case NT_ARM_ZA:
11387 if (note->namesz == 6
11388 && strcmp (note->namedata, "LINUX") == 0)
11389 return elfcore_grok_aarch_za (abfd, note);
11390 else
11391 return true;
11392
11393 case NT_ARM_ZT:
11394 if (note->namesz == 6
11395 && strcmp (note->namedata, "LINUX") == 0)
11396 return elfcore_grok_aarch_zt (abfd, note);
11397 else
11398 return true;
11399
11400 case NT_ARM_GCS:
11401 if (note->namesz == 6 && strcmp (note->namedata, "LINUX") == 0)
11402 return elfcore_grok_aarch_gcs (abfd, note);
11403 else
11404 return true;
11405
11406 case NT_GDB_TDESC:
11407 if (note->namesz == 4
11408 && strcmp (note->namedata, "GDB") == 0)
11409 return elfcore_grok_gdb_tdesc (abfd, note);
11410 else
11411 return true;
11412
11413 case NT_RISCV_CSR:
11414 if (note->namesz == 4
11415 && strcmp (note->namedata, "GDB") == 0)
11416 return elfcore_grok_riscv_csr (abfd, note);
11417 else
11418 return true;
11419
11420 case NT_LARCH_CPUCFG:
11421 if (note->namesz == 6
11422 && strcmp (note->namedata, "LINUX") == 0)
11423 return elfcore_grok_loongarch_cpucfg (abfd, note);
11424 else
11425 return true;
11426
11427 case NT_LARCH_LBT:
11428 if (note->namesz == 6
11429 && strcmp (note->namedata, "LINUX") == 0)
11430 return elfcore_grok_loongarch_lbt (abfd, note);
11431 else
11432 return true;
11433
11434 case NT_LARCH_LSX:
11435 if (note->namesz == 6
11436 && strcmp (note->namedata, "LINUX") == 0)
11437 return elfcore_grok_loongarch_lsx (abfd, note);
11438 else
11439 return true;
11440
11441 case NT_LARCH_LASX:
11442 if (note->namesz == 6
11443 && strcmp (note->namedata, "LINUX") == 0)
11444 return elfcore_grok_loongarch_lasx (abfd, note);
11445 else
11446 return true;
11447
11448 case NT_PRPSINFO:
11449 case NT_PSINFO:
11450 if (bed->elf_backend_grok_psinfo)
11451 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11452 return true;
11453 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11454 return elfcore_grok_psinfo (abfd, note);
11455 #else
11456 return true;
11457 #endif
11458
11459 case NT_AUXV:
11460 return elfcore_make_auxv_note_section (abfd, note, 0);
11461
11462 case NT_FILE:
11463 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11464 note);
11465
11466 case NT_SIGINFO:
11467 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11468 note);
11469
11470 }
11471 }
11472
11473 static bool
11474 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11475 {
11476 struct bfd_build_id* build_id;
11477
11478 if (note->descsz == 0)
11479 return false;
11480
11481 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11482 if (build_id == NULL)
11483 return false;
11484
11485 build_id->size = note->descsz;
11486 memcpy (build_id->data, note->descdata, note->descsz);
11487 abfd->build_id = build_id;
11488
11489 return true;
11490 }
11491
11492 static bool
11493 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11494 {
11495 switch (note->type)
11496 {
11497 default:
11498 return true;
11499
11500 case NT_GNU_PROPERTY_TYPE_0:
11501 return _bfd_elf_parse_gnu_properties (abfd, note);
11502
11503 case NT_GNU_BUILD_ID:
11504 return elfobj_grok_gnu_build_id (abfd, note);
11505 }
11506 }
11507
11508 static bool
11509 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11510 {
11511 struct sdt_note *cur =
11512 (struct sdt_note *) bfd_alloc (abfd,
11513 sizeof (struct sdt_note) + note->descsz);
11514
11515 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11516 cur->size = (bfd_size_type) note->descsz;
11517 memcpy (cur->data, note->descdata, note->descsz);
11518
11519 elf_tdata (abfd)->sdt_note_head = cur;
11520
11521 return true;
11522 }
11523
11524 static bool
11525 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11526 {
11527 switch (note->type)
11528 {
11529 case NT_STAPSDT:
11530 return elfobj_grok_stapsdt_note_1 (abfd, note);
11531
11532 default:
11533 return true;
11534 }
11535 }
11536
11537 static bool
11538 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11539 {
11540 size_t offset;
11541
11542 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11543 {
11544 case ELFCLASS32:
11545 if (note->descsz < 108)
11546 return false;
11547 break;
11548
11549 case ELFCLASS64:
11550 if (note->descsz < 120)
11551 return false;
11552 break;
11553
11554 default:
11555 return false;
11556 }
11557
11558 /* Check for version 1 in pr_version. */
11559 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11560 return false;
11561
11562 offset = 4;
11563
11564 /* Skip over pr_psinfosz. */
11565 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11566 offset += 4;
11567 else
11568 {
11569 offset += 4; /* Padding before pr_psinfosz. */
11570 offset += 8;
11571 }
11572
11573 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11574 elf_tdata (abfd)->core->program
11575 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11576 offset += 17;
11577
11578 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11579 elf_tdata (abfd)->core->command
11580 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11581 offset += 81;
11582
11583 /* Padding before pr_pid. */
11584 offset += 2;
11585
11586 /* The pr_pid field was added in version "1a". */
11587 if (note->descsz < offset + 4)
11588 return true;
11589
11590 elf_tdata (abfd)->core->pid
11591 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11592
11593 return true;
11594 }
11595
11596 static bool
11597 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11598 {
11599 size_t offset;
11600 size_t size;
11601 size_t min_size;
11602
11603 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11604 Also compute minimum size of this note. */
11605 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11606 {
11607 case ELFCLASS32:
11608 offset = 4 + 4;
11609 min_size = offset + (4 * 2) + 4 + 4 + 4;
11610 break;
11611
11612 case ELFCLASS64:
11613 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11614 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11615 break;
11616
11617 default:
11618 return false;
11619 }
11620
11621 if (note->descsz < min_size)
11622 return false;
11623
11624 /* Check for version 1 in pr_version. */
11625 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11626 return false;
11627
11628 /* Extract size of pr_reg from pr_gregsetsz. */
11629 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11630 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11631 {
11632 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11633 offset += 4 * 2;
11634 }
11635 else
11636 {
11637 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11638 offset += 8 * 2;
11639 }
11640
11641 /* Skip over pr_osreldate. */
11642 offset += 4;
11643
11644 /* Read signal from pr_cursig. */
11645 if (elf_tdata (abfd)->core->signal == 0)
11646 elf_tdata (abfd)->core->signal
11647 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11648 offset += 4;
11649
11650 /* Read TID from pr_pid. */
11651 elf_tdata (abfd)->core->lwpid
11652 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11653 offset += 4;
11654
11655 /* Padding before pr_reg. */
11656 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11657 offset += 4;
11658
11659 /* Make sure that there is enough data remaining in the note. */
11660 if ((note->descsz - offset) < size)
11661 return false;
11662
11663 /* Make a ".reg/999" section and a ".reg" section. */
11664 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11665 size, note->descpos + offset);
11666 }
11667
11668 static bool
11669 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11670 {
11671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11672
11673 switch (note->type)
11674 {
11675 case NT_PRSTATUS:
11676 if (bed->elf_backend_grok_freebsd_prstatus)
11677 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11678 return true;
11679 return elfcore_grok_freebsd_prstatus (abfd, note);
11680
11681 case NT_FPREGSET:
11682 return elfcore_grok_prfpreg (abfd, note);
11683
11684 case NT_PRPSINFO:
11685 return elfcore_grok_freebsd_psinfo (abfd, note);
11686
11687 case NT_FREEBSD_THRMISC:
11688 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11689
11690 case NT_FREEBSD_PROCSTAT_PROC:
11691 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11692 note);
11693
11694 case NT_FREEBSD_PROCSTAT_FILES:
11695 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11696 note);
11697
11698 case NT_FREEBSD_PROCSTAT_VMMAP:
11699 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11700 note);
11701
11702 case NT_FREEBSD_PROCSTAT_AUXV:
11703 return elfcore_make_auxv_note_section (abfd, note, 4);
11704
11705 case NT_FREEBSD_X86_SEGBASES:
11706 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11707
11708 case NT_X86_XSTATE:
11709 return elfcore_grok_xstatereg (abfd, note);
11710
11711 case NT_FREEBSD_PTLWPINFO:
11712 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11713 note);
11714
11715 case NT_ARM_TLS:
11716 return elfcore_grok_aarch_tls (abfd, note);
11717
11718 case NT_ARM_VFP:
11719 return elfcore_grok_arm_vfp (abfd, note);
11720
11721 default:
11722 return true;
11723 }
11724 }
11725
11726 static bool
11727 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11728 {
11729 char *cp;
11730
11731 cp = strchr (note->namedata, '@');
11732 if (cp != NULL)
11733 {
11734 *lwpidp = atoi(cp + 1);
11735 return true;
11736 }
11737 return false;
11738 }
11739
11740 static bool
11741 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11742 {
11743 if (note->descsz <= 0x7c + 31)
11744 return false;
11745
11746 /* Signal number at offset 0x08. */
11747 elf_tdata (abfd)->core->signal
11748 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11749
11750 /* Process ID at offset 0x50. */
11751 elf_tdata (abfd)->core->pid
11752 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11753
11754 /* Command name at 0x7c (max 32 bytes, including nul). */
11755 elf_tdata (abfd)->core->command
11756 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11757
11758 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11759 note);
11760 }
11761
11762 static bool
11763 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11764 {
11765 int lwp;
11766
11767 if (elfcore_netbsd_get_lwpid (note, &lwp))
11768 elf_tdata (abfd)->core->lwpid = lwp;
11769
11770 switch (note->type)
11771 {
11772 case NT_NETBSDCORE_PROCINFO:
11773 /* NetBSD-specific core "procinfo". Note that we expect to
11774 find this note before any of the others, which is fine,
11775 since the kernel writes this note out first when it
11776 creates a core file. */
11777 return elfcore_grok_netbsd_procinfo (abfd, note);
11778 case NT_NETBSDCORE_AUXV:
11779 /* NetBSD-specific Elf Auxiliary Vector data. */
11780 return elfcore_make_auxv_note_section (abfd, note, 4);
11781 case NT_NETBSDCORE_LWPSTATUS:
11782 return elfcore_make_note_pseudosection (abfd,
11783 ".note.netbsdcore.lwpstatus",
11784 note);
11785 default:
11786 break;
11787 }
11788
11789 /* As of March 2020 there are no other machine-independent notes
11790 defined for NetBSD core files. If the note type is less
11791 than the start of the machine-dependent note types, we don't
11792 understand it. */
11793
11794 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11795 return true;
11796
11797
11798 switch (bfd_get_arch (abfd))
11799 {
11800 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11801 PT_GETFPREGS == mach+2. */
11802
11803 case bfd_arch_aarch64:
11804 case bfd_arch_alpha:
11805 case bfd_arch_sparc:
11806 switch (note->type)
11807 {
11808 case NT_NETBSDCORE_FIRSTMACH+0:
11809 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11810
11811 case NT_NETBSDCORE_FIRSTMACH+2:
11812 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11813
11814 default:
11815 return true;
11816 }
11817
11818 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11819 There's also old PT___GETREGS40 == mach + 1 for old reg
11820 structure which lacks GBR. */
11821
11822 case bfd_arch_sh:
11823 switch (note->type)
11824 {
11825 case NT_NETBSDCORE_FIRSTMACH+3:
11826 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11827
11828 case NT_NETBSDCORE_FIRSTMACH+5:
11829 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11830
11831 default:
11832 return true;
11833 }
11834
11835 /* On all other arch's, PT_GETREGS == mach+1 and
11836 PT_GETFPREGS == mach+3. */
11837
11838 default:
11839 switch (note->type)
11840 {
11841 case NT_NETBSDCORE_FIRSTMACH+1:
11842 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11843
11844 case NT_NETBSDCORE_FIRSTMACH+3:
11845 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11846
11847 default:
11848 return true;
11849 }
11850 }
11851 /* NOTREACHED */
11852 }
11853
11854 static bool
11855 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11856 {
11857 if (note->descsz <= 0x48 + 31)
11858 return false;
11859
11860 /* Signal number at offset 0x08. */
11861 elf_tdata (abfd)->core->signal
11862 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11863
11864 /* Process ID at offset 0x20. */
11865 elf_tdata (abfd)->core->pid
11866 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11867
11868 /* Command name at 0x48 (max 32 bytes, including nul). */
11869 elf_tdata (abfd)->core->command
11870 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11871
11872 return true;
11873 }
11874
11875 /* Processes Solaris's process status note.
11876 sig_off ~ offsetof(prstatus_t, pr_cursig)
11877 pid_off ~ offsetof(prstatus_t, pr_pid)
11878 lwpid_off ~ offsetof(prstatus_t, pr_who)
11879 gregset_size ~ sizeof(gregset_t)
11880 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11881
11882 static bool
11883 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11884 int pid_off, int lwpid_off, size_t gregset_size,
11885 size_t gregset_offset)
11886 {
11887 asection *sect = NULL;
11888 elf_tdata (abfd)->core->signal
11889 = bfd_get_16 (abfd, note->descdata + sig_off);
11890 elf_tdata (abfd)->core->pid
11891 = bfd_get_32 (abfd, note->descdata + pid_off);
11892 elf_tdata (abfd)->core->lwpid
11893 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11894
11895 sect = bfd_get_section_by_name (abfd, ".reg");
11896 if (sect != NULL)
11897 sect->size = gregset_size;
11898
11899 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11900 note->descpos + gregset_offset);
11901 }
11902
11903 /* Gets program and arguments from a core.
11904 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11905 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11906
11907 static bool
11908 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11909 int prog_off, int comm_off)
11910 {
11911 elf_tdata (abfd)->core->program
11912 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11913 elf_tdata (abfd)->core->command
11914 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11915
11916 return true;
11917 }
11918
11919 /* Processes Solaris's LWP status note.
11920 gregset_size ~ sizeof(gregset_t)
11921 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11922 fpregset_size ~ sizeof(fpregset_t)
11923 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11924
11925 static bool
11926 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11927 size_t gregset_size, int gregset_off,
11928 size_t fpregset_size, int fpregset_off)
11929 {
11930 asection *sect = NULL;
11931 char reg2_section_name[16] = { 0 };
11932
11933 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11934 elf_tdata (abfd)->core->lwpid);
11935
11936 /* offsetof(lwpstatus_t, pr_lwpid) */
11937 elf_tdata (abfd)->core->lwpid
11938 = bfd_get_32 (abfd, note->descdata + 4);
11939 /* offsetof(lwpstatus_t, pr_cursig) */
11940 elf_tdata (abfd)->core->signal
11941 = bfd_get_16 (abfd, note->descdata + 12);
11942
11943 sect = bfd_get_section_by_name (abfd, ".reg");
11944 if (sect != NULL)
11945 sect->size = gregset_size;
11946 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11947 note->descpos + gregset_off))
11948 return false;
11949
11950 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11951 if (sect != NULL)
11952 {
11953 sect->size = fpregset_size;
11954 sect->filepos = note->descpos + fpregset_off;
11955 sect->alignment_power = 2;
11956 }
11957 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11958 note->descpos + fpregset_off))
11959 return false;
11960
11961 return true;
11962 }
11963
11964 static bool
11965 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11966 {
11967 if (note == NULL)
11968 return false;
11969
11970 /* core files are identified as 32- or 64-bit, SPARC or x86,
11971 by the size of the descsz which matches the sizeof()
11972 the type appropriate for that note type (e.g., prstatus_t for
11973 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11974 on Solaris. The core file bitness may differ from the bitness of
11975 gdb itself, so fixed values are used instead of sizeof().
11976 Appropriate fixed offsets are also used to obtain data from
11977 the note. */
11978
11979 switch ((int) note->type)
11980 {
11981 case SOLARIS_NT_PRSTATUS:
11982 switch (note->descsz)
11983 {
11984 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11985 return elfcore_grok_solaris_prstatus(abfd, note,
11986 136, 216, 308, 152, 356);
11987 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11988 return elfcore_grok_solaris_prstatus(abfd, note,
11989 264, 360, 520, 304, 600);
11990 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11991 return elfcore_grok_solaris_prstatus(abfd, note,
11992 136, 216, 308, 76, 356);
11993 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11994 return elfcore_grok_solaris_prstatus(abfd, note,
11995 264, 360, 520, 224, 600);
11996 default:
11997 return true;
11998 }
11999
12000 case SOLARIS_NT_PSINFO:
12001 case SOLARIS_NT_PRPSINFO:
12002 switch (note->descsz)
12003 {
12004 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12005 return elfcore_grok_solaris_info(abfd, note, 84, 100);
12006 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12007 return elfcore_grok_solaris_info(abfd, note, 120, 136);
12008 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12009 return elfcore_grok_solaris_info(abfd, note, 88, 104);
12010 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12011 return elfcore_grok_solaris_info(abfd, note, 136, 152);
12012 default:
12013 return true;
12014 }
12015
12016 case SOLARIS_NT_LWPSTATUS:
12017 switch (note->descsz)
12018 {
12019 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12020 return elfcore_grok_solaris_lwpstatus(abfd, note,
12021 152, 344, 400, 496);
12022 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12023 return elfcore_grok_solaris_lwpstatus(abfd, note,
12024 304, 544, 544, 848);
12025 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12026 return elfcore_grok_solaris_lwpstatus(abfd, note,
12027 76, 344, 380, 420);
12028 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12029 return elfcore_grok_solaris_lwpstatus(abfd, note,
12030 224, 544, 528, 768);
12031 default:
12032 return true;
12033 }
12034
12035 case SOLARIS_NT_LWPSINFO:
12036 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12037 if (note->descsz == 128 || note->descsz == 152)
12038 elf_tdata (abfd)->core->lwpid =
12039 bfd_get_32 (abfd, note->descdata + 4);
12040 break;
12041
12042 default:
12043 break;
12044 }
12045
12046 return true;
12047 }
12048
12049 /* For name starting with "CORE" this may be either a Solaris
12050 core file or a gdb-generated core file. Do Solaris-specific
12051 processing on selected note types first with
12052 elfcore_grok_solaris_note(), then process the note
12053 in elfcore_grok_note(). */
12054
12055 static bool
12056 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12057 {
12058 if (!elfcore_grok_solaris_note_impl (abfd, note))
12059 return false;
12060
12061 return elfcore_grok_note (abfd, note);
12062 }
12063
12064 static bool
12065 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12066 {
12067 if (note->type == NT_OPENBSD_PROCINFO)
12068 return elfcore_grok_openbsd_procinfo (abfd, note);
12069
12070 if (note->type == NT_OPENBSD_REGS)
12071 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12072
12073 if (note->type == NT_OPENBSD_FPREGS)
12074 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12075
12076 if (note->type == NT_OPENBSD_XFPREGS)
12077 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12078
12079 if (note->type == NT_OPENBSD_AUXV)
12080 return elfcore_make_auxv_note_section (abfd, note, 0);
12081
12082 if (note->type == NT_OPENBSD_WCOOKIE)
12083 {
12084 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12085 SEC_HAS_CONTENTS);
12086
12087 if (sect == NULL)
12088 return false;
12089 sect->size = note->descsz;
12090 sect->filepos = note->descpos;
12091 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12092
12093 return true;
12094 }
12095
12096 return true;
12097 }
12098
12099 static bool
12100 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12101 {
12102 void *ddata = note->descdata;
12103 char buf[100];
12104 char *name;
12105 asection *sect;
12106 short sig;
12107 unsigned flags;
12108
12109 if (note->descsz < 16)
12110 return false;
12111
12112 /* nto_procfs_status 'pid' field is at offset 0. */
12113 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12114
12115 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12116 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12117
12118 /* nto_procfs_status 'flags' field is at offset 8. */
12119 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12120
12121 /* nto_procfs_status 'what' field is at offset 14. */
12122 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12123 {
12124 elf_tdata (abfd)->core->signal = sig;
12125 elf_tdata (abfd)->core->lwpid = *tid;
12126 }
12127
12128 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12129 do not come from signals so we make sure we set the current
12130 thread just in case. */
12131 if (flags & 0x00000080)
12132 elf_tdata (abfd)->core->lwpid = *tid;
12133
12134 /* Make a ".qnx_core_status/%d" section. */
12135 sprintf (buf, ".qnx_core_status/%ld", *tid);
12136
12137 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12138 if (name == NULL)
12139 return false;
12140 strcpy (name, buf);
12141
12142 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12143 if (sect == NULL)
12144 return false;
12145
12146 sect->size = note->descsz;
12147 sect->filepos = note->descpos;
12148 sect->alignment_power = 2;
12149
12150 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12151 }
12152
12153 static bool
12154 elfcore_grok_nto_regs (bfd *abfd,
12155 Elf_Internal_Note *note,
12156 long tid,
12157 char *base)
12158 {
12159 char buf[100];
12160 char *name;
12161 asection *sect;
12162
12163 /* Make a "(base)/%d" section. */
12164 sprintf (buf, "%s/%ld", base, tid);
12165
12166 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12167 if (name == NULL)
12168 return false;
12169 strcpy (name, buf);
12170
12171 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12172 if (sect == NULL)
12173 return false;
12174
12175 sect->size = note->descsz;
12176 sect->filepos = note->descpos;
12177 sect->alignment_power = 2;
12178
12179 /* This is the current thread. */
12180 if (elf_tdata (abfd)->core->lwpid == tid)
12181 return elfcore_maybe_make_sect (abfd, base, sect);
12182
12183 return true;
12184 }
12185
12186 static bool
12187 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12188 {
12189 /* Every GREG section has a STATUS section before it. Store the
12190 tid from the previous call to pass down to the next gregs
12191 function. */
12192 static long tid = 1;
12193
12194 switch (note->type)
12195 {
12196 case QNT_CORE_INFO:
12197 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12198 case QNT_CORE_STATUS:
12199 return elfcore_grok_nto_status (abfd, note, &tid);
12200 case QNT_CORE_GREG:
12201 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12202 case QNT_CORE_FPREG:
12203 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12204 default:
12205 return true;
12206 }
12207 }
12208
12209 static bool
12210 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12211 {
12212 char *name;
12213 asection *sect;
12214 size_t len;
12215
12216 /* Use note name as section name. */
12217 len = note->namesz;
12218 name = (char *) bfd_alloc (abfd, len);
12219 if (name == NULL)
12220 return false;
12221 memcpy (name, note->namedata, len);
12222 name[len - 1] = '\0';
12223
12224 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12225 if (sect == NULL)
12226 return false;
12227
12228 sect->size = note->descsz;
12229 sect->filepos = note->descpos;
12230 sect->alignment_power = 1;
12231
12232 return true;
12233 }
12234
12235 /* Function: elfcore_write_note
12236
12237 Inputs:
12238 buffer to hold note, and current size of buffer
12239 name of note
12240 type of note
12241 data for note
12242 size of data for note
12243
12244 Writes note to end of buffer. ELF64 notes are written exactly as
12245 for ELF32, despite the current (as of 2006) ELF gabi specifying
12246 that they ought to have 8-byte namesz and descsz field, and have
12247 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12248
12249 Return:
12250 Pointer to realloc'd buffer, *BUFSIZ updated. */
12251
12252 char *
12253 elfcore_write_note (bfd *abfd,
12254 char *buf,
12255 int *bufsiz,
12256 const char *name,
12257 int type,
12258 const void *input,
12259 int size)
12260 {
12261 Elf_External_Note *xnp;
12262 size_t namesz;
12263 size_t newspace;
12264 char *dest;
12265
12266 namesz = 0;
12267 if (name != NULL)
12268 namesz = strlen (name) + 1;
12269
12270 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12271
12272 buf = (char *) realloc (buf, *bufsiz + newspace);
12273 if (buf == NULL)
12274 return buf;
12275 dest = buf + *bufsiz;
12276 *bufsiz += newspace;
12277 xnp = (Elf_External_Note *) dest;
12278 H_PUT_32 (abfd, namesz, xnp->namesz);
12279 H_PUT_32 (abfd, size, xnp->descsz);
12280 H_PUT_32 (abfd, type, xnp->type);
12281 dest = xnp->name;
12282 if (name != NULL)
12283 {
12284 memcpy (dest, name, namesz);
12285 dest += namesz;
12286 while (namesz & 3)
12287 {
12288 *dest++ = '\0';
12289 ++namesz;
12290 }
12291 }
12292 memcpy (dest, input, size);
12293 dest += size;
12294 while (size & 3)
12295 {
12296 *dest++ = '\0';
12297 ++size;
12298 }
12299 return buf;
12300 }
12301
12302 /* gcc-8 warns (*) on all the strncpy calls in this function about
12303 possible string truncation. The "truncation" is not a bug. We
12304 have an external representation of structs with fields that are not
12305 necessarily NULL terminated and corresponding internal
12306 representation fields that are one larger so that they can always
12307 be NULL terminated.
12308 gcc versions between 4.2 and 4.6 do not allow pragma control of
12309 diagnostics inside functions, giving a hard error if you try to use
12310 the finer control available with later versions.
12311 gcc prior to 4.2 warns about diagnostic push and pop.
12312 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12313 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12314 (*) Depending on your system header files! */
12315 #if GCC_VERSION >= 8000
12316 # pragma GCC diagnostic push
12317 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12318 #endif
12319 char *
12320 elfcore_write_prpsinfo (bfd *abfd,
12321 char *buf,
12322 int *bufsiz,
12323 const char *fname,
12324 const char *psargs)
12325 {
12326 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12327
12328 if (bed->elf_backend_write_core_note != NULL)
12329 {
12330 char *ret;
12331 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12332 NT_PRPSINFO, fname, psargs);
12333 if (ret != NULL)
12334 return ret;
12335 }
12336
12337 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12338 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12339 if (bed->s->elfclass == ELFCLASS32)
12340 {
12341 # if defined (HAVE_PSINFO32_T)
12342 psinfo32_t data;
12343 int note_type = NT_PSINFO;
12344 # else
12345 prpsinfo32_t data;
12346 int note_type = NT_PRPSINFO;
12347 # endif
12348
12349 memset (&data, 0, sizeof (data));
12350 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12351 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12352 return elfcore_write_note (abfd, buf, bufsiz,
12353 "CORE", note_type, &data, sizeof (data));
12354 }
12355 else
12356 # endif
12357 {
12358 # if defined (HAVE_PSINFO_T)
12359 psinfo_t data;
12360 int note_type = NT_PSINFO;
12361 # else
12362 prpsinfo_t data;
12363 int note_type = NT_PRPSINFO;
12364 # endif
12365
12366 memset (&data, 0, sizeof (data));
12367 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12368 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12369 return elfcore_write_note (abfd, buf, bufsiz,
12370 "CORE", note_type, &data, sizeof (data));
12371 }
12372 #endif /* PSINFO_T or PRPSINFO_T */
12373
12374 free (buf);
12375 return NULL;
12376 }
12377 #if GCC_VERSION >= 8000
12378 # pragma GCC diagnostic pop
12379 #endif
12380
12381 char *
12382 elfcore_write_linux_prpsinfo32
12383 (bfd *abfd, char *buf, int *bufsiz,
12384 const struct elf_internal_linux_prpsinfo *prpsinfo)
12385 {
12386 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12387 {
12388 struct elf_external_linux_prpsinfo32_ugid16 data;
12389
12390 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12391 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12392 &data, sizeof (data));
12393 }
12394 else
12395 {
12396 struct elf_external_linux_prpsinfo32_ugid32 data;
12397
12398 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12399 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12400 &data, sizeof (data));
12401 }
12402 }
12403
12404 char *
12405 elfcore_write_linux_prpsinfo64
12406 (bfd *abfd, char *buf, int *bufsiz,
12407 const struct elf_internal_linux_prpsinfo *prpsinfo)
12408 {
12409 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12410 {
12411 struct elf_external_linux_prpsinfo64_ugid16 data;
12412
12413 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12414 return elfcore_write_note (abfd, buf, bufsiz,
12415 "CORE", NT_PRPSINFO, &data, sizeof (data));
12416 }
12417 else
12418 {
12419 struct elf_external_linux_prpsinfo64_ugid32 data;
12420
12421 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12422 return elfcore_write_note (abfd, buf, bufsiz,
12423 "CORE", NT_PRPSINFO, &data, sizeof (data));
12424 }
12425 }
12426
12427 char *
12428 elfcore_write_prstatus (bfd *abfd,
12429 char *buf,
12430 int *bufsiz,
12431 long pid,
12432 int cursig,
12433 const void *gregs)
12434 {
12435 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12436
12437 if (bed->elf_backend_write_core_note != NULL)
12438 {
12439 char *ret;
12440 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12441 NT_PRSTATUS,
12442 pid, cursig, gregs);
12443 if (ret != NULL)
12444 return ret;
12445 }
12446
12447 #if defined (HAVE_PRSTATUS_T)
12448 #if defined (HAVE_PRSTATUS32_T)
12449 if (bed->s->elfclass == ELFCLASS32)
12450 {
12451 prstatus32_t prstat;
12452
12453 memset (&prstat, 0, sizeof (prstat));
12454 prstat.pr_pid = pid;
12455 prstat.pr_cursig = cursig;
12456 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12457 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12458 NT_PRSTATUS, &prstat, sizeof (prstat));
12459 }
12460 else
12461 #endif
12462 {
12463 prstatus_t prstat;
12464
12465 memset (&prstat, 0, sizeof (prstat));
12466 prstat.pr_pid = pid;
12467 prstat.pr_cursig = cursig;
12468 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12469 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12470 NT_PRSTATUS, &prstat, sizeof (prstat));
12471 }
12472 #endif /* HAVE_PRSTATUS_T */
12473
12474 free (buf);
12475 return NULL;
12476 }
12477
12478 #if defined (HAVE_LWPSTATUS_T)
12479 char *
12480 elfcore_write_lwpstatus (bfd *abfd,
12481 char *buf,
12482 int *bufsiz,
12483 long pid,
12484 int cursig,
12485 const void *gregs)
12486 {
12487 lwpstatus_t lwpstat;
12488 const char *note_name = "CORE";
12489
12490 memset (&lwpstat, 0, sizeof (lwpstat));
12491 lwpstat.pr_lwpid = pid >> 16;
12492 lwpstat.pr_cursig = cursig;
12493 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12494 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12495 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12496 #if !defined(gregs)
12497 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12498 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12499 #else
12500 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12501 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12502 #endif
12503 #endif
12504 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12505 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12506 }
12507 #endif /* HAVE_LWPSTATUS_T */
12508
12509 #if defined (HAVE_PSTATUS_T)
12510 char *
12511 elfcore_write_pstatus (bfd *abfd,
12512 char *buf,
12513 int *bufsiz,
12514 long pid,
12515 int cursig ATTRIBUTE_UNUSED,
12516 const void *gregs ATTRIBUTE_UNUSED)
12517 {
12518 const char *note_name = "CORE";
12519 #if defined (HAVE_PSTATUS32_T)
12520 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12521
12522 if (bed->s->elfclass == ELFCLASS32)
12523 {
12524 pstatus32_t pstat;
12525
12526 memset (&pstat, 0, sizeof (pstat));
12527 pstat.pr_pid = pid & 0xffff;
12528 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12529 NT_PSTATUS, &pstat, sizeof (pstat));
12530 return buf;
12531 }
12532 else
12533 #endif
12534 {
12535 pstatus_t pstat;
12536
12537 memset (&pstat, 0, sizeof (pstat));
12538 pstat.pr_pid = pid & 0xffff;
12539 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12540 NT_PSTATUS, &pstat, sizeof (pstat));
12541 return buf;
12542 }
12543 }
12544 #endif /* HAVE_PSTATUS_T */
12545
12546 char *
12547 elfcore_write_prfpreg (bfd *abfd,
12548 char *buf,
12549 int *bufsiz,
12550 const void *fpregs,
12551 int size)
12552 {
12553 const char *note_name = "CORE";
12554 return elfcore_write_note (abfd, buf, bufsiz,
12555 note_name, NT_FPREGSET, fpregs, size);
12556 }
12557
12558 char *
12559 elfcore_write_prxfpreg (bfd *abfd,
12560 char *buf,
12561 int *bufsiz,
12562 const void *xfpregs,
12563 int size)
12564 {
12565 char *note_name = "LINUX";
12566 return elfcore_write_note (abfd, buf, bufsiz,
12567 note_name, NT_PRXFPREG, xfpregs, size);
12568 }
12569
12570 char *
12571 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12572 const void *xfpregs, int size)
12573 {
12574 char *note_name;
12575 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12576 note_name = "FreeBSD";
12577 else
12578 note_name = "LINUX";
12579 return elfcore_write_note (abfd, buf, bufsiz,
12580 note_name, NT_X86_XSTATE, xfpregs, size);
12581 }
12582
12583 static char *
12584 elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12585 const void *ssp, int size)
12586 {
12587 const char *note_name = "LINUX";
12588 return elfcore_write_note (abfd, buf, bufsiz,
12589 note_name, NT_X86_SHSTK, ssp, size);
12590 }
12591
12592 char *
12593 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12594 const void *regs, int size)
12595 {
12596 char *note_name = "FreeBSD";
12597 return elfcore_write_note (abfd, buf, bufsiz,
12598 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12599 }
12600
12601 char *
12602 elfcore_write_ppc_vmx (bfd *abfd,
12603 char *buf,
12604 int *bufsiz,
12605 const void *ppc_vmx,
12606 int size)
12607 {
12608 char *note_name = "LINUX";
12609 return elfcore_write_note (abfd, buf, bufsiz,
12610 note_name, NT_PPC_VMX, ppc_vmx, size);
12611 }
12612
12613 char *
12614 elfcore_write_ppc_vsx (bfd *abfd,
12615 char *buf,
12616 int *bufsiz,
12617 const void *ppc_vsx,
12618 int size)
12619 {
12620 char *note_name = "LINUX";
12621 return elfcore_write_note (abfd, buf, bufsiz,
12622 note_name, NT_PPC_VSX, ppc_vsx, size);
12623 }
12624
12625 char *
12626 elfcore_write_ppc_tar (bfd *abfd,
12627 char *buf,
12628 int *bufsiz,
12629 const void *ppc_tar,
12630 int size)
12631 {
12632 char *note_name = "LINUX";
12633 return elfcore_write_note (abfd, buf, bufsiz,
12634 note_name, NT_PPC_TAR, ppc_tar, size);
12635 }
12636
12637 char *
12638 elfcore_write_ppc_ppr (bfd *abfd,
12639 char *buf,
12640 int *bufsiz,
12641 const void *ppc_ppr,
12642 int size)
12643 {
12644 char *note_name = "LINUX";
12645 return elfcore_write_note (abfd, buf, bufsiz,
12646 note_name, NT_PPC_PPR, ppc_ppr, size);
12647 }
12648
12649 char *
12650 elfcore_write_ppc_dscr (bfd *abfd,
12651 char *buf,
12652 int *bufsiz,
12653 const void *ppc_dscr,
12654 int size)
12655 {
12656 char *note_name = "LINUX";
12657 return elfcore_write_note (abfd, buf, bufsiz,
12658 note_name, NT_PPC_DSCR, ppc_dscr, size);
12659 }
12660
12661 char *
12662 elfcore_write_ppc_ebb (bfd *abfd,
12663 char *buf,
12664 int *bufsiz,
12665 const void *ppc_ebb,
12666 int size)
12667 {
12668 char *note_name = "LINUX";
12669 return elfcore_write_note (abfd, buf, bufsiz,
12670 note_name, NT_PPC_EBB, ppc_ebb, size);
12671 }
12672
12673 char *
12674 elfcore_write_ppc_pmu (bfd *abfd,
12675 char *buf,
12676 int *bufsiz,
12677 const void *ppc_pmu,
12678 int size)
12679 {
12680 char *note_name = "LINUX";
12681 return elfcore_write_note (abfd, buf, bufsiz,
12682 note_name, NT_PPC_PMU, ppc_pmu, size);
12683 }
12684
12685 char *
12686 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12687 char *buf,
12688 int *bufsiz,
12689 const void *ppc_tm_cgpr,
12690 int size)
12691 {
12692 char *note_name = "LINUX";
12693 return elfcore_write_note (abfd, buf, bufsiz,
12694 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12695 }
12696
12697 char *
12698 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12699 char *buf,
12700 int *bufsiz,
12701 const void *ppc_tm_cfpr,
12702 int size)
12703 {
12704 char *note_name = "LINUX";
12705 return elfcore_write_note (abfd, buf, bufsiz,
12706 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12707 }
12708
12709 char *
12710 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12711 char *buf,
12712 int *bufsiz,
12713 const void *ppc_tm_cvmx,
12714 int size)
12715 {
12716 char *note_name = "LINUX";
12717 return elfcore_write_note (abfd, buf, bufsiz,
12718 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12719 }
12720
12721 char *
12722 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12723 char *buf,
12724 int *bufsiz,
12725 const void *ppc_tm_cvsx,
12726 int size)
12727 {
12728 char *note_name = "LINUX";
12729 return elfcore_write_note (abfd, buf, bufsiz,
12730 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12731 }
12732
12733 char *
12734 elfcore_write_ppc_tm_spr (bfd *abfd,
12735 char *buf,
12736 int *bufsiz,
12737 const void *ppc_tm_spr,
12738 int size)
12739 {
12740 char *note_name = "LINUX";
12741 return elfcore_write_note (abfd, buf, bufsiz,
12742 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12743 }
12744
12745 char *
12746 elfcore_write_ppc_tm_ctar (bfd *abfd,
12747 char *buf,
12748 int *bufsiz,
12749 const void *ppc_tm_ctar,
12750 int size)
12751 {
12752 char *note_name = "LINUX";
12753 return elfcore_write_note (abfd, buf, bufsiz,
12754 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12755 }
12756
12757 char *
12758 elfcore_write_ppc_tm_cppr (bfd *abfd,
12759 char *buf,
12760 int *bufsiz,
12761 const void *ppc_tm_cppr,
12762 int size)
12763 {
12764 char *note_name = "LINUX";
12765 return elfcore_write_note (abfd, buf, bufsiz,
12766 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12767 }
12768
12769 char *
12770 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12771 char *buf,
12772 int *bufsiz,
12773 const void *ppc_tm_cdscr,
12774 int size)
12775 {
12776 char *note_name = "LINUX";
12777 return elfcore_write_note (abfd, buf, bufsiz,
12778 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12779 }
12780
12781 static char *
12782 elfcore_write_s390_high_gprs (bfd *abfd,
12783 char *buf,
12784 int *bufsiz,
12785 const void *s390_high_gprs,
12786 int size)
12787 {
12788 char *note_name = "LINUX";
12789 return elfcore_write_note (abfd, buf, bufsiz,
12790 note_name, NT_S390_HIGH_GPRS,
12791 s390_high_gprs, size);
12792 }
12793
12794 char *
12795 elfcore_write_s390_timer (bfd *abfd,
12796 char *buf,
12797 int *bufsiz,
12798 const void *s390_timer,
12799 int size)
12800 {
12801 char *note_name = "LINUX";
12802 return elfcore_write_note (abfd, buf, bufsiz,
12803 note_name, NT_S390_TIMER, s390_timer, size);
12804 }
12805
12806 char *
12807 elfcore_write_s390_todcmp (bfd *abfd,
12808 char *buf,
12809 int *bufsiz,
12810 const void *s390_todcmp,
12811 int size)
12812 {
12813 char *note_name = "LINUX";
12814 return elfcore_write_note (abfd, buf, bufsiz,
12815 note_name, NT_S390_TODCMP, s390_todcmp, size);
12816 }
12817
12818 char *
12819 elfcore_write_s390_todpreg (bfd *abfd,
12820 char *buf,
12821 int *bufsiz,
12822 const void *s390_todpreg,
12823 int size)
12824 {
12825 char *note_name = "LINUX";
12826 return elfcore_write_note (abfd, buf, bufsiz,
12827 note_name, NT_S390_TODPREG, s390_todpreg, size);
12828 }
12829
12830 char *
12831 elfcore_write_s390_ctrs (bfd *abfd,
12832 char *buf,
12833 int *bufsiz,
12834 const void *s390_ctrs,
12835 int size)
12836 {
12837 char *note_name = "LINUX";
12838 return elfcore_write_note (abfd, buf, bufsiz,
12839 note_name, NT_S390_CTRS, s390_ctrs, size);
12840 }
12841
12842 char *
12843 elfcore_write_s390_prefix (bfd *abfd,
12844 char *buf,
12845 int *bufsiz,
12846 const void *s390_prefix,
12847 int size)
12848 {
12849 char *note_name = "LINUX";
12850 return elfcore_write_note (abfd, buf, bufsiz,
12851 note_name, NT_S390_PREFIX, s390_prefix, size);
12852 }
12853
12854 char *
12855 elfcore_write_s390_last_break (bfd *abfd,
12856 char *buf,
12857 int *bufsiz,
12858 const void *s390_last_break,
12859 int size)
12860 {
12861 char *note_name = "LINUX";
12862 return elfcore_write_note (abfd, buf, bufsiz,
12863 note_name, NT_S390_LAST_BREAK,
12864 s390_last_break, size);
12865 }
12866
12867 char *
12868 elfcore_write_s390_system_call (bfd *abfd,
12869 char *buf,
12870 int *bufsiz,
12871 const void *s390_system_call,
12872 int size)
12873 {
12874 char *note_name = "LINUX";
12875 return elfcore_write_note (abfd, buf, bufsiz,
12876 note_name, NT_S390_SYSTEM_CALL,
12877 s390_system_call, size);
12878 }
12879
12880 char *
12881 elfcore_write_s390_tdb (bfd *abfd,
12882 char *buf,
12883 int *bufsiz,
12884 const void *s390_tdb,
12885 int size)
12886 {
12887 char *note_name = "LINUX";
12888 return elfcore_write_note (abfd, buf, bufsiz,
12889 note_name, NT_S390_TDB, s390_tdb, size);
12890 }
12891
12892 char *
12893 elfcore_write_s390_vxrs_low (bfd *abfd,
12894 char *buf,
12895 int *bufsiz,
12896 const void *s390_vxrs_low,
12897 int size)
12898 {
12899 char *note_name = "LINUX";
12900 return elfcore_write_note (abfd, buf, bufsiz,
12901 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12902 }
12903
12904 char *
12905 elfcore_write_s390_vxrs_high (bfd *abfd,
12906 char *buf,
12907 int *bufsiz,
12908 const void *s390_vxrs_high,
12909 int size)
12910 {
12911 char *note_name = "LINUX";
12912 return elfcore_write_note (abfd, buf, bufsiz,
12913 note_name, NT_S390_VXRS_HIGH,
12914 s390_vxrs_high, size);
12915 }
12916
12917 char *
12918 elfcore_write_s390_gs_cb (bfd *abfd,
12919 char *buf,
12920 int *bufsiz,
12921 const void *s390_gs_cb,
12922 int size)
12923 {
12924 char *note_name = "LINUX";
12925 return elfcore_write_note (abfd, buf, bufsiz,
12926 note_name, NT_S390_GS_CB,
12927 s390_gs_cb, size);
12928 }
12929
12930 char *
12931 elfcore_write_s390_gs_bc (bfd *abfd,
12932 char *buf,
12933 int *bufsiz,
12934 const void *s390_gs_bc,
12935 int size)
12936 {
12937 char *note_name = "LINUX";
12938 return elfcore_write_note (abfd, buf, bufsiz,
12939 note_name, NT_S390_GS_BC,
12940 s390_gs_bc, size);
12941 }
12942
12943 char *
12944 elfcore_write_arm_vfp (bfd *abfd,
12945 char *buf,
12946 int *bufsiz,
12947 const void *arm_vfp,
12948 int size)
12949 {
12950 char *note_name = "LINUX";
12951 return elfcore_write_note (abfd, buf, bufsiz,
12952 note_name, NT_ARM_VFP, arm_vfp, size);
12953 }
12954
12955 char *
12956 elfcore_write_aarch_tls (bfd *abfd,
12957 char *buf,
12958 int *bufsiz,
12959 const void *aarch_tls,
12960 int size)
12961 {
12962 char *note_name = "LINUX";
12963 return elfcore_write_note (abfd, buf, bufsiz,
12964 note_name, NT_ARM_TLS, aarch_tls, size);
12965 }
12966
12967 char *
12968 elfcore_write_aarch_hw_break (bfd *abfd,
12969 char *buf,
12970 int *bufsiz,
12971 const void *aarch_hw_break,
12972 int size)
12973 {
12974 char *note_name = "LINUX";
12975 return elfcore_write_note (abfd, buf, bufsiz,
12976 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12977 }
12978
12979 char *
12980 elfcore_write_aarch_hw_watch (bfd *abfd,
12981 char *buf,
12982 int *bufsiz,
12983 const void *aarch_hw_watch,
12984 int size)
12985 {
12986 char *note_name = "LINUX";
12987 return elfcore_write_note (abfd, buf, bufsiz,
12988 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12989 }
12990
12991 char *
12992 elfcore_write_aarch_sve (bfd *abfd,
12993 char *buf,
12994 int *bufsiz,
12995 const void *aarch_sve,
12996 int size)
12997 {
12998 char *note_name = "LINUX";
12999 return elfcore_write_note (abfd, buf, bufsiz,
13000 note_name, NT_ARM_SVE, aarch_sve, size);
13001 }
13002
13003 char *
13004 elfcore_write_aarch_pauth (bfd *abfd,
13005 char *buf,
13006 int *bufsiz,
13007 const void *aarch_pauth,
13008 int size)
13009 {
13010 char *note_name = "LINUX";
13011 return elfcore_write_note (abfd, buf, bufsiz,
13012 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13013 }
13014
13015 char *
13016 elfcore_write_aarch_mte (bfd *abfd,
13017 char *buf,
13018 int *bufsiz,
13019 const void *aarch_mte,
13020 int size)
13021 {
13022 char *note_name = "LINUX";
13023 return elfcore_write_note (abfd, buf, bufsiz,
13024 note_name, NT_ARM_TAGGED_ADDR_CTRL,
13025 aarch_mte,
13026 size);
13027 }
13028
13029 char *
13030 elfcore_write_aarch_ssve (bfd *abfd,
13031 char *buf,
13032 int *bufsiz,
13033 const void *aarch_ssve,
13034 int size)
13035 {
13036 char *note_name = "LINUX";
13037 return elfcore_write_note (abfd, buf, bufsiz,
13038 note_name, NT_ARM_SSVE,
13039 aarch_ssve,
13040 size);
13041 }
13042
13043 char *
13044 elfcore_write_aarch_za (bfd *abfd,
13045 char *buf,
13046 int *bufsiz,
13047 const void *aarch_za,
13048 int size)
13049 {
13050 char *note_name = "LINUX";
13051 return elfcore_write_note (abfd, buf, bufsiz,
13052 note_name, NT_ARM_ZA,
13053 aarch_za,
13054 size);
13055 }
13056
13057 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13058 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13059 written into. Return a pointer to the new start of the note buffer, to
13060 replace BUF which may no longer be valid. */
13061
13062 char *
13063 elfcore_write_aarch_zt (bfd *abfd,
13064 char *buf,
13065 int *bufsiz,
13066 const void *aarch_zt,
13067 int size)
13068 {
13069 char *note_name = "LINUX";
13070 return elfcore_write_note (abfd, buf, bufsiz,
13071 note_name, NT_ARM_ZT,
13072 aarch_zt,
13073 size);
13074 }
13075
13076 /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
13077 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13078 written into. Return a pointer to the new start of the note buffer, to
13079 replace BUF which may no longer be valid. */
13080
13081 static char *
13082 elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
13083 const void *aarch_gcs, int size)
13084 {
13085 const char *note_name = "LINUX";
13086 return elfcore_write_note (abfd, buf, bufsiz, note_name, NT_ARM_GCS,
13087 aarch_gcs, size);
13088 }
13089
13090 char *
13091 elfcore_write_arc_v2 (bfd *abfd,
13092 char *buf,
13093 int *bufsiz,
13094 const void *arc_v2,
13095 int size)
13096 {
13097 char *note_name = "LINUX";
13098 return elfcore_write_note (abfd, buf, bufsiz,
13099 note_name, NT_ARC_V2, arc_v2, size);
13100 }
13101
13102 char *
13103 elfcore_write_loongarch_cpucfg (bfd *abfd,
13104 char *buf,
13105 int *bufsiz,
13106 const void *loongarch_cpucfg,
13107 int size)
13108 {
13109 char *note_name = "LINUX";
13110 return elfcore_write_note (abfd, buf, bufsiz,
13111 note_name, NT_LARCH_CPUCFG,
13112 loongarch_cpucfg, size);
13113 }
13114
13115 char *
13116 elfcore_write_loongarch_lbt (bfd *abfd,
13117 char *buf,
13118 int *bufsiz,
13119 const void *loongarch_lbt,
13120 int size)
13121 {
13122 char *note_name = "LINUX";
13123 return elfcore_write_note (abfd, buf, bufsiz,
13124 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13125 }
13126
13127 char *
13128 elfcore_write_loongarch_lsx (bfd *abfd,
13129 char *buf,
13130 int *bufsiz,
13131 const void *loongarch_lsx,
13132 int size)
13133 {
13134 char *note_name = "LINUX";
13135 return elfcore_write_note (abfd, buf, bufsiz,
13136 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13137 }
13138
13139 char *
13140 elfcore_write_loongarch_lasx (bfd *abfd,
13141 char *buf,
13142 int *bufsiz,
13143 const void *loongarch_lasx,
13144 int size)
13145 {
13146 char *note_name = "LINUX";
13147 return elfcore_write_note (abfd, buf, bufsiz,
13148 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13149 }
13150
13151 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13152 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13153 written into. Return a pointer to the new start of the note buffer, to
13154 replace BUF which may no longer be valid. */
13155
13156 char *
13157 elfcore_write_riscv_csr (bfd *abfd,
13158 char *buf,
13159 int *bufsiz,
13160 const void *csrs,
13161 int size)
13162 {
13163 const char *note_name = "GDB";
13164 return elfcore_write_note (abfd, buf, bufsiz,
13165 note_name, NT_RISCV_CSR, csrs, size);
13166 }
13167
13168 /* Write the target description (a string) pointed to by TDESC, length
13169 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13170 note is being written into. Return a pointer to the new start of the
13171 note buffer, to replace BUF which may no longer be valid. */
13172
13173 char *
13174 elfcore_write_gdb_tdesc (bfd *abfd,
13175 char *buf,
13176 int *bufsiz,
13177 const void *tdesc,
13178 int size)
13179 {
13180 const char *note_name = "GDB";
13181 return elfcore_write_note (abfd, buf, bufsiz,
13182 note_name, NT_GDB_TDESC, tdesc, size);
13183 }
13184
13185 char *
13186 elfcore_write_register_note (bfd *abfd,
13187 char *buf,
13188 int *bufsiz,
13189 const char *section,
13190 const void *data,
13191 int size)
13192 {
13193 if (strcmp (section, ".reg2") == 0)
13194 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13195 if (strcmp (section, ".reg-xfp") == 0)
13196 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13197 if (strcmp (section, ".reg-xstate") == 0)
13198 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13199 if (strcmp (section, ".reg-x86-segbases") == 0)
13200 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13201 if (strcmp (section, ".reg-ssp") == 0)
13202 return elfcore_write_sspreg (abfd, buf, bufsiz, data, size);
13203 if (strcmp (section, ".reg-ppc-vmx") == 0)
13204 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13205 if (strcmp (section, ".reg-ppc-vsx") == 0)
13206 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13207 if (strcmp (section, ".reg-ppc-tar") == 0)
13208 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13209 if (strcmp (section, ".reg-ppc-ppr") == 0)
13210 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13211 if (strcmp (section, ".reg-ppc-dscr") == 0)
13212 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13213 if (strcmp (section, ".reg-ppc-ebb") == 0)
13214 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13215 if (strcmp (section, ".reg-ppc-pmu") == 0)
13216 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13217 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13218 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13219 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13220 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13221 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13222 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13223 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13224 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13225 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13226 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13227 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13228 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13229 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13230 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13231 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13232 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13233 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13234 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13235 if (strcmp (section, ".reg-s390-timer") == 0)
13236 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13237 if (strcmp (section, ".reg-s390-todcmp") == 0)
13238 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13239 if (strcmp (section, ".reg-s390-todpreg") == 0)
13240 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13241 if (strcmp (section, ".reg-s390-ctrs") == 0)
13242 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13243 if (strcmp (section, ".reg-s390-prefix") == 0)
13244 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13245 if (strcmp (section, ".reg-s390-last-break") == 0)
13246 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13247 if (strcmp (section, ".reg-s390-system-call") == 0)
13248 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13249 if (strcmp (section, ".reg-s390-tdb") == 0)
13250 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13251 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13252 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13253 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13254 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13255 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13256 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13257 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13258 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13259 if (strcmp (section, ".reg-arm-vfp") == 0)
13260 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13261 if (strcmp (section, ".reg-aarch-tls") == 0)
13262 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13263 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13264 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13265 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13266 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13267 if (strcmp (section, ".reg-aarch-sve") == 0)
13268 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13269 if (strcmp (section, ".reg-aarch-pauth") == 0)
13270 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13271 if (strcmp (section, ".reg-aarch-mte") == 0)
13272 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13273 if (strcmp (section, ".reg-aarch-ssve") == 0)
13274 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13275 if (strcmp (section, ".reg-aarch-za") == 0)
13276 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13277 if (strcmp (section, ".reg-aarch-zt") == 0)
13278 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13279 if (strcmp (section, ".reg-aarch-gcs") == 0)
13280 return elfcore_write_aarch_gcs (abfd, buf, bufsiz, data, size);
13281 if (strcmp (section, ".reg-arc-v2") == 0)
13282 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13283 if (strcmp (section, ".gdb-tdesc") == 0)
13284 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13285 if (strcmp (section, ".reg-riscv-csr") == 0)
13286 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13287 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13288 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13289 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13290 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13291 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13292 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13293 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13294 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13295 return NULL;
13296 }
13297
13298 char *
13299 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13300 const void *buf, int bufsiz)
13301 {
13302 return elfcore_write_note (obfd, note_data, note_size,
13303 "CORE", NT_FILE, buf, bufsiz);
13304 }
13305
13306 static bool
13307 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13308 size_t align)
13309 {
13310 char *p;
13311
13312 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13313 gABI specifies that PT_NOTE alignment should be aligned to 4
13314 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13315 align is less than 4, we use 4 byte alignment. */
13316 if (align < 4)
13317 align = 4;
13318 if (align != 4 && align != 8)
13319 return false;
13320
13321 p = buf;
13322 while (p < buf + size)
13323 {
13324 Elf_External_Note *xnp = (Elf_External_Note *) p;
13325 Elf_Internal_Note in;
13326
13327 if (offsetof (Elf_External_Note, name) > buf - p + size)
13328 return false;
13329
13330 in.type = H_GET_32 (abfd, xnp->type);
13331
13332 in.namesz = H_GET_32 (abfd, xnp->namesz);
13333 in.namedata = xnp->name;
13334 if (in.namesz > buf - in.namedata + size)
13335 return false;
13336
13337 in.descsz = H_GET_32 (abfd, xnp->descsz);
13338 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13339 in.descpos = offset + (in.descdata - buf);
13340 if (in.descsz != 0
13341 && (in.descdata >= buf + size
13342 || in.descsz > buf - in.descdata + size))
13343 return false;
13344
13345 switch (bfd_get_format (abfd))
13346 {
13347 default:
13348 return true;
13349
13350 case bfd_core:
13351 {
13352 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13353 struct
13354 {
13355 const char * string;
13356 size_t len;
13357 bool (*func) (bfd *, Elf_Internal_Note *);
13358 }
13359 grokers[] =
13360 {
13361 GROKER_ELEMENT ("", elfcore_grok_note),
13362 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13363 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13364 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13365 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13366 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13367 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13368 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13369 };
13370 #undef GROKER_ELEMENT
13371 int i;
13372
13373 for (i = ARRAY_SIZE (grokers); i--;)
13374 {
13375 if (in.namesz >= grokers[i].len
13376 && strncmp (in.namedata, grokers[i].string,
13377 grokers[i].len) == 0)
13378 {
13379 if (! grokers[i].func (abfd, & in))
13380 return false;
13381 break;
13382 }
13383 }
13384 break;
13385 }
13386
13387 case bfd_object:
13388 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13389 {
13390 if (! elfobj_grok_gnu_note (abfd, &in))
13391 return false;
13392 }
13393 else if (in.namesz == sizeof "stapsdt"
13394 && strcmp (in.namedata, "stapsdt") == 0)
13395 {
13396 if (! elfobj_grok_stapsdt_note (abfd, &in))
13397 return false;
13398 }
13399 break;
13400 }
13401
13402 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13403 }
13404
13405 return true;
13406 }
13407
13408 bool
13409 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13410 size_t align)
13411 {
13412 char *buf;
13413
13414 if (size == 0 || (size + 1) == 0)
13415 return true;
13416
13417 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13418 return false;
13419
13420 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13421 if (buf == NULL)
13422 return false;
13423
13424 /* PR 17512: file: ec08f814
13425 0-termintate the buffer so that string searches will not overflow. */
13426 buf[size] = 0;
13427
13428 if (!elf_parse_notes (abfd, buf, size, offset, align))
13429 {
13430 free (buf);
13431 return false;
13432 }
13433
13434 free (buf);
13435 return true;
13436 }
13437
13438 /* Providing external access to the ELF program header table. */
13440
13441 /* Return an upper bound on the number of bytes required to store a
13442 copy of ABFD's program header table entries. Return -1 if an error
13443 occurs; bfd_get_error will return an appropriate code. */
13444
13445 long
13446 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13447 {
13448 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13449 {
13450 bfd_set_error (bfd_error_wrong_format);
13451 return -1;
13452 }
13453
13454 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13455 }
13456
13457 /* Copy ABFD's program header table entries to *PHDRS. The entries
13458 will be stored as an array of Elf_Internal_Phdr structures, as
13459 defined in include/elf/internal.h. To find out how large the
13460 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13461
13462 Return the number of program header table entries read, or -1 if an
13463 error occurs; bfd_get_error will return an appropriate code. */
13464
13465 int
13466 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13467 {
13468 int num_phdrs;
13469
13470 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13471 {
13472 bfd_set_error (bfd_error_wrong_format);
13473 return -1;
13474 }
13475
13476 num_phdrs = elf_elfheader (abfd)->e_phnum;
13477 if (num_phdrs != 0)
13478 memcpy (phdrs, elf_tdata (abfd)->phdr,
13479 num_phdrs * sizeof (Elf_Internal_Phdr));
13480
13481 return num_phdrs;
13482 }
13483
13484 enum elf_reloc_type_class
13485 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13486 const asection *rel_sec ATTRIBUTE_UNUSED,
13487 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13488 {
13489 return reloc_class_normal;
13490 }
13491
13492 /* For RELA architectures, return the relocation value for a
13493 relocation against a local symbol. */
13494
13495 bfd_vma
13496 _bfd_elf_rela_local_sym (bfd *abfd,
13497 Elf_Internal_Sym *sym,
13498 asection **psec,
13499 Elf_Internal_Rela *rel)
13500 {
13501 asection *sec = *psec;
13502 bfd_vma relocation;
13503
13504 relocation = (sec->output_section->vma
13505 + sec->output_offset
13506 + sym->st_value);
13507 if ((sec->flags & SEC_MERGE)
13508 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13509 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13510 {
13511 rel->r_addend =
13512 _bfd_merged_section_offset (abfd, psec,
13513 elf_section_data (sec)->sec_info,
13514 sym->st_value + rel->r_addend);
13515 if (sec != *psec)
13516 {
13517 /* If we have changed the section, and our original section is
13518 marked with SEC_EXCLUDE, it means that the original
13519 SEC_MERGE section has been completely subsumed in some
13520 other SEC_MERGE section. In this case, we need to leave
13521 some info around for --emit-relocs. */
13522 if ((sec->flags & SEC_EXCLUDE) != 0)
13523 sec->kept_section = *psec;
13524 sec = *psec;
13525 }
13526 rel->r_addend -= relocation;
13527 rel->r_addend += sec->output_section->vma + sec->output_offset;
13528 }
13529 return relocation;
13530 }
13531
13532 bfd_vma
13533 _bfd_elf_rel_local_sym (bfd *abfd,
13534 Elf_Internal_Sym *sym,
13535 asection **psec,
13536 bfd_vma addend)
13537 {
13538 asection *sec = *psec;
13539
13540 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13541 return sym->st_value + addend;
13542
13543 return _bfd_merged_section_offset (abfd, psec,
13544 elf_section_data (sec)->sec_info,
13545 sym->st_value + addend);
13546 }
13547
13548 /* Adjust an address within a section. Given OFFSET within SEC, return
13549 the new offset within the section, based upon changes made to the
13550 section. Returns -1 if the offset is now invalid.
13551 The offset (in abnd out) is in target sized bytes, however big a
13552 byte may be. */
13553
13554 bfd_vma
13555 _bfd_elf_section_offset (bfd *abfd,
13556 struct bfd_link_info *info,
13557 asection *sec,
13558 bfd_vma offset)
13559 {
13560 switch (sec->sec_info_type)
13561 {
13562 case SEC_INFO_TYPE_STABS:
13563 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13564 offset);
13565 case SEC_INFO_TYPE_EH_FRAME:
13566 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13567
13568 case SEC_INFO_TYPE_SFRAME:
13569 return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13570
13571 default:
13572 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13573 {
13574 /* Reverse the offset. */
13575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13576 bfd_size_type address_size = bed->s->arch_size / 8;
13577
13578 /* address_size and sec->size are in octets. Convert
13579 to bytes before subtracting the original offset. */
13580 offset = ((sec->size - address_size)
13581 / bfd_octets_per_byte (abfd, sec) - offset);
13582 }
13583 return offset;
13584 }
13585 }
13586
13587 long
13589 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13590 long symcount ATTRIBUTE_UNUSED,
13591 asymbol **syms ATTRIBUTE_UNUSED,
13592 long dynsymcount,
13593 asymbol **dynsyms,
13594 asymbol **ret)
13595 {
13596 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13597 asection *relplt;
13598 asymbol *s;
13599 const char *relplt_name;
13600 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13601 arelent *p;
13602 long count, i, n;
13603 size_t size;
13604 Elf_Internal_Shdr *hdr;
13605 char *names;
13606 asection *plt;
13607
13608 *ret = NULL;
13609
13610 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13611 return 0;
13612
13613 if (dynsymcount <= 0)
13614 return 0;
13615
13616 if (!bed->plt_sym_val)
13617 return 0;
13618
13619 relplt_name = bed->relplt_name;
13620 if (relplt_name == NULL)
13621 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13622 relplt = bfd_get_section_by_name (abfd, relplt_name);
13623 if (relplt == NULL)
13624 return 0;
13625
13626 hdr = &elf_section_data (relplt)->this_hdr;
13627 if (hdr->sh_link != elf_dynsymtab (abfd)
13628 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13629 return 0;
13630
13631 plt = bfd_get_section_by_name (abfd, ".plt");
13632 if (plt == NULL)
13633 return 0;
13634
13635 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13636 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13637 return -1;
13638
13639 count = NUM_SHDR_ENTRIES (hdr);
13640 size = count * sizeof (asymbol);
13641 p = relplt->relocation;
13642 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13643 {
13644 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13645 if (p->addend != 0)
13646 {
13647 #ifdef BFD64
13648 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13649 #else
13650 size += sizeof ("+0x") - 1 + 8;
13651 #endif
13652 }
13653 }
13654
13655 s = *ret = (asymbol *) bfd_malloc (size);
13656 if (s == NULL)
13657 return -1;
13658
13659 names = (char *) (s + count);
13660 p = relplt->relocation;
13661 n = 0;
13662 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13663 {
13664 size_t len;
13665 bfd_vma addr;
13666
13667 addr = bed->plt_sym_val (i, plt, p);
13668 if (addr == (bfd_vma) -1)
13669 continue;
13670
13671 *s = **p->sym_ptr_ptr;
13672 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13673 we are defining a symbol, ensure one of them is set. */
13674 if ((s->flags & BSF_LOCAL) == 0)
13675 s->flags |= BSF_GLOBAL;
13676 s->flags |= BSF_SYNTHETIC;
13677 s->section = plt;
13678 s->value = addr - plt->vma;
13679 s->name = names;
13680 s->udata.p = NULL;
13681 len = strlen ((*p->sym_ptr_ptr)->name);
13682 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13683 names += len;
13684 if (p->addend != 0)
13685 {
13686 char buf[30], *a;
13687
13688 memcpy (names, "+0x", sizeof ("+0x") - 1);
13689 names += sizeof ("+0x") - 1;
13690 bfd_sprintf_vma (abfd, buf, p->addend);
13691 for (a = buf; *a == '0'; ++a)
13692 ;
13693 len = strlen (a);
13694 memcpy (names, a, len);
13695 names += len;
13696 }
13697 memcpy (names, "@plt", sizeof ("@plt"));
13698 names += sizeof ("@plt");
13699 ++s, ++n;
13700 }
13701
13702 return n;
13703 }
13704
13705 /* It is only used by x86-64 so far.
13706 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13707 but current usage would allow all of _bfd_std_section to be zero. */
13708 static const asymbol lcomm_sym
13709 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13710 asection _bfd_elf_large_com_section
13711 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13712 "LARGE_COMMON", 0, SEC_IS_COMMON);
13713
13714 bool
13715 _bfd_elf_final_write_processing (bfd *abfd)
13716 {
13717 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13718
13719 i_ehdrp = elf_elfheader (abfd);
13720
13721 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13722 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13723
13724 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13725 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13726 or STB_GNU_UNIQUE binding. */
13727 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13728 {
13729 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13730 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13731 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13732 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13733 {
13734 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13735 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13736 "and FreeBSD targets"));
13737 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13738 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13739 "only by GNU and FreeBSD targets"));
13740 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13741 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13742 "only by GNU and FreeBSD targets"));
13743 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13744 _bfd_error_handler (_("GNU_RETAIN section is supported "
13745 "only by GNU and FreeBSD targets"));
13746 bfd_set_error (bfd_error_sorry);
13747 return false;
13748 }
13749 }
13750 return true;
13751 }
13752
13753
13754 /* Return TRUE for ELF symbol types that represent functions.
13755 This is the default version of this function, which is sufficient for
13756 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13757
13758 bool
13759 _bfd_elf_is_function_type (unsigned int type)
13760 {
13761 return (type == STT_FUNC
13762 || type == STT_GNU_IFUNC);
13763 }
13764
13765 /* If the ELF symbol SYM might be a function in SEC, return the
13766 function size and set *CODE_OFF to the function's entry point,
13767 otherwise return zero. */
13768
13769 bfd_size_type
13770 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13771 bfd_vma *code_off)
13772 {
13773 bfd_size_type size;
13774 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13775
13776 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13777 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13778 || sym->section != sec)
13779 return 0;
13780
13781 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13782
13783 /* In theory we should check that the symbol's type satisfies
13784 _bfd_elf_is_function_type(), but there are some function-like
13785 symbols which would fail this test. (eg _start). Instead
13786 we check for hidden, local, notype symbols with zero size.
13787 This type of symbol is generated by the annobin plugin for gcc
13788 and clang, and should not be considered to be a function symbol. */
13789 if (size == 0
13790 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13791 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13792 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13793 return 0;
13794
13795 *code_off = sym->value;
13796 /* Do not return 0 for the function's size. */
13797 return size ? size : 1;
13798 }
13799
13800 /* Set to non-zero to enable some debug messages. */
13801 #define DEBUG_SECONDARY_RELOCS 0
13802
13803 /* An internal-to-the-bfd-library only section type
13804 used to indicate a cached secondary reloc section. */
13805 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13806
13807 /* Create a BFD section to hold a secondary reloc section. */
13808
13809 bool
13810 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13811 Elf_Internal_Shdr *hdr,
13812 const char * name,
13813 unsigned int shindex)
13814 {
13815 /* We only support RELA secondary relocs. */
13816 if (hdr->sh_type != SHT_RELA)
13817 return false;
13818
13819 #if DEBUG_SECONDARY_RELOCS
13820 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13821 #endif
13822 hdr->sh_type = SHT_SECONDARY_RELOC;
13823 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13824 }
13825
13826 /* Read in any secondary relocs associated with SEC. */
13827
13828 bool
13829 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13830 asection * sec,
13831 asymbol ** symbols,
13832 bool dynamic)
13833 {
13834 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13835 asection * relsec;
13836 bool result = true;
13837 bfd_vma (*r_sym) (bfd_vma);
13838 ufile_ptr filesize;
13839
13840 #if BFD_DEFAULT_TARGET_SIZE > 32
13841 if (bfd_arch_bits_per_address (abfd) != 32)
13842 r_sym = elf64_r_sym;
13843 else
13844 #endif
13845 r_sym = elf32_r_sym;
13846
13847 if (!elf_section_data (sec)->has_secondary_relocs)
13848 return true;
13849
13850 /* Discover if there are any secondary reloc sections
13851 associated with SEC. */
13852 filesize = bfd_get_file_size (abfd);
13853 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13854 {
13855 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13856
13857 if (hdr->sh_type == SHT_SECONDARY_RELOC
13858 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13859 && (hdr->sh_entsize == ebd->s->sizeof_rel
13860 || hdr->sh_entsize == ebd->s->sizeof_rela))
13861 {
13862 bfd_byte * native_relocs;
13863 bfd_byte * native_reloc;
13864 arelent * internal_relocs;
13865 arelent * internal_reloc;
13866 size_t i;
13867 unsigned int entsize;
13868 unsigned int symcount;
13869 bfd_size_type reloc_count;
13870 size_t amt;
13871
13872 if (ebd->elf_info_to_howto == NULL)
13873 return false;
13874
13875 #if DEBUG_SECONDARY_RELOCS
13876 fprintf (stderr, "read secondary relocs for %s from %s\n",
13877 sec->name, relsec->name);
13878 #endif
13879 entsize = hdr->sh_entsize;
13880
13881 if (filesize != 0
13882 && ((ufile_ptr) hdr->sh_offset > filesize
13883 || hdr->sh_size > filesize - hdr->sh_offset))
13884 {
13885 bfd_set_error (bfd_error_file_truncated);
13886 result = false;
13887 continue;
13888 }
13889
13890 native_relocs = bfd_malloc (hdr->sh_size);
13891 if (native_relocs == NULL)
13892 {
13893 result = false;
13894 continue;
13895 }
13896
13897 reloc_count = NUM_SHDR_ENTRIES (hdr);
13898 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13899 {
13900 free (native_relocs);
13901 bfd_set_error (bfd_error_file_too_big);
13902 result = false;
13903 continue;
13904 }
13905
13906 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13907 if (internal_relocs == NULL)
13908 {
13909 free (native_relocs);
13910 result = false;
13911 continue;
13912 }
13913
13914 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13915 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13916 {
13917 free (native_relocs);
13918 /* The internal_relocs will be freed when
13919 the memory for the bfd is released. */
13920 result = false;
13921 continue;
13922 }
13923
13924 if (dynamic)
13925 symcount = bfd_get_dynamic_symcount (abfd);
13926 else
13927 symcount = bfd_get_symcount (abfd);
13928
13929 for (i = 0, internal_reloc = internal_relocs,
13930 native_reloc = native_relocs;
13931 i < reloc_count;
13932 i++, internal_reloc++, native_reloc += entsize)
13933 {
13934 bool res;
13935 Elf_Internal_Rela rela;
13936
13937 if (entsize == ebd->s->sizeof_rel)
13938 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13939 else /* entsize == ebd->s->sizeof_rela */
13940 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13941
13942 /* The address of an ELF reloc is section relative for an object
13943 file, and absolute for an executable file or shared library.
13944 The address of a normal BFD reloc is always section relative,
13945 and the address of a dynamic reloc is absolute.. */
13946 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13947 internal_reloc->address = rela.r_offset;
13948 else
13949 internal_reloc->address = rela.r_offset - sec->vma;
13950
13951 if (r_sym (rela.r_info) == STN_UNDEF)
13952 {
13953 /* FIXME: This and the error case below mean that we
13954 have a symbol on relocs that is not elf_symbol_type. */
13955 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13956 }
13957 else if (r_sym (rela.r_info) > symcount)
13958 {
13959 _bfd_error_handler
13960 /* xgettext:c-format */
13961 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13962 abfd, sec, i, (long) r_sym (rela.r_info));
13963 bfd_set_error (bfd_error_bad_value);
13964 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13965 result = false;
13966 }
13967 else
13968 {
13969 asymbol **ps;
13970
13971 ps = symbols + r_sym (rela.r_info) - 1;
13972 internal_reloc->sym_ptr_ptr = ps;
13973 /* Make sure that this symbol is not removed by strip. */
13974 (*ps)->flags |= BSF_KEEP;
13975 }
13976
13977 internal_reloc->addend = rela.r_addend;
13978
13979 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13980 if (! res || internal_reloc->howto == NULL)
13981 {
13982 #if DEBUG_SECONDARY_RELOCS
13983 fprintf (stderr,
13984 "there is no howto associated with reloc %lx\n",
13985 rela.r_info);
13986 #endif
13987 result = false;
13988 }
13989 }
13990
13991 free (native_relocs);
13992 /* Store the internal relocs. */
13993 elf_section_data (relsec)->sec_info = internal_relocs;
13994 }
13995 }
13996
13997 return result;
13998 }
13999
14000 /* Set the ELF section header fields of an output secondary reloc section. */
14001
14002 bool
14003 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14004 bfd *obfd ATTRIBUTE_UNUSED,
14005 const Elf_Internal_Shdr *isection,
14006 Elf_Internal_Shdr *osection)
14007 {
14008 asection * isec;
14009 asection * osec;
14010 struct bfd_elf_section_data * esd;
14011
14012 if (isection == NULL)
14013 return false;
14014
14015 if (isection->sh_type != SHT_SECONDARY_RELOC)
14016 return true;
14017
14018 isec = isection->bfd_section;
14019 if (isec == NULL)
14020 return false;
14021
14022 osec = osection->bfd_section;
14023 if (osec == NULL)
14024 return false;
14025
14026 esd = elf_section_data (osec);
14027 BFD_ASSERT (esd->sec_info == NULL);
14028 esd->sec_info = elf_section_data (isec)->sec_info;
14029 osection->sh_type = SHT_RELA;
14030 osection->sh_link = elf_onesymtab (obfd);
14031 if (osection->sh_link == 0)
14032 {
14033 /* There is no symbol table - we are hosed... */
14034 _bfd_error_handler
14035 /* xgettext:c-format */
14036 (_("%pB(%pA): link section cannot be set"
14037 " because the output file does not have a symbol table"),
14038 obfd, osec);
14039 bfd_set_error (bfd_error_bad_value);
14040 return false;
14041 }
14042
14043 /* Find the output section that corresponds to the isection's
14044 sh_info link. */
14045 if (isection->sh_info == 0
14046 || isection->sh_info >= elf_numsections (ibfd))
14047 {
14048 _bfd_error_handler
14049 /* xgettext:c-format */
14050 (_("%pB(%pA): info section index is invalid"),
14051 obfd, osec);
14052 bfd_set_error (bfd_error_bad_value);
14053 return false;
14054 }
14055
14056 isection = elf_elfsections (ibfd)[isection->sh_info];
14057
14058 if (isection == NULL
14059 || isection->bfd_section == NULL
14060 || isection->bfd_section->output_section == NULL)
14061 {
14062 _bfd_error_handler
14063 /* xgettext:c-format */
14064 (_("%pB(%pA): info section index cannot be set"
14065 " because the section is not in the output"),
14066 obfd, osec);
14067 bfd_set_error (bfd_error_bad_value);
14068 return false;
14069 }
14070
14071 esd = elf_section_data (isection->bfd_section->output_section);
14072 BFD_ASSERT (esd != NULL);
14073 osection->sh_info = esd->this_idx;
14074 esd->has_secondary_relocs = true;
14075 #if DEBUG_SECONDARY_RELOCS
14076 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14077 osec->name, osection->sh_link, osection->sh_info);
14078 fprintf (stderr, "mark section %s as having secondary relocs\n",
14079 bfd_section_name (isection->bfd_section->output_section));
14080 #endif
14081
14082 return true;
14083 }
14084
14085 /* Write out a secondary reloc section.
14086
14087 FIXME: Currently this function can result in a serious performance penalty
14088 for files with secondary relocs and lots of sections. The proper way to
14089 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14090 relocs together and then to have this function just walk that chain. */
14091
14092 bool
14093 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14094 {
14095 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14096 bfd_vma addr_offset;
14097 asection * relsec;
14098 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14099 bool result = true;
14100
14101 if (sec == NULL)
14102 return false;
14103
14104 #if BFD_DEFAULT_TARGET_SIZE > 32
14105 if (bfd_arch_bits_per_address (abfd) != 32)
14106 r_info = elf64_r_info;
14107 else
14108 #endif
14109 r_info = elf32_r_info;
14110
14111 /* The address of an ELF reloc is section relative for an object
14112 file, and absolute for an executable file or shared library.
14113 The address of a BFD reloc is always section relative. */
14114 addr_offset = 0;
14115 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14116 addr_offset = sec->vma;
14117
14118 /* Discover if there are any secondary reloc sections
14119 associated with SEC. */
14120 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14121 {
14122 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14123 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14124
14125 if (hdr->sh_type == SHT_RELA
14126 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14127 {
14128 asymbol * last_sym;
14129 int last_sym_idx;
14130 size_t reloc_count;
14131 size_t idx;
14132 bfd_size_type entsize;
14133 arelent * src_irel;
14134 bfd_byte * dst_rela;
14135
14136 if (hdr->contents != NULL)
14137 {
14138 _bfd_error_handler
14139 /* xgettext:c-format */
14140 (_("%pB(%pA): error: secondary reloc section processed twice"),
14141 abfd, relsec);
14142 bfd_set_error (bfd_error_bad_value);
14143 result = false;
14144 continue;
14145 }
14146
14147 entsize = hdr->sh_entsize;
14148 if (entsize == 0)
14149 {
14150 _bfd_error_handler
14151 /* xgettext:c-format */
14152 (_("%pB(%pA): error: secondary reloc section"
14153 " has zero sized entries"),
14154 abfd, relsec);
14155 bfd_set_error (bfd_error_bad_value);
14156 result = false;
14157 continue;
14158 }
14159 else if (entsize != ebd->s->sizeof_rel
14160 && entsize != ebd->s->sizeof_rela)
14161 {
14162 _bfd_error_handler
14163 /* xgettext:c-format */
14164 (_("%pB(%pA): error: secondary reloc section"
14165 " has non-standard sized entries"),
14166 abfd, relsec);
14167 bfd_set_error (bfd_error_bad_value);
14168 result = false;
14169 continue;
14170 }
14171
14172 reloc_count = hdr->sh_size / entsize;
14173 hdr->sh_size = entsize * reloc_count;
14174 if (reloc_count == 0)
14175 {
14176 _bfd_error_handler
14177 /* xgettext:c-format */
14178 (_("%pB(%pA): error: secondary reloc section is empty!"),
14179 abfd, relsec);
14180 bfd_set_error (bfd_error_bad_value);
14181 result = false;
14182 continue;
14183 }
14184
14185 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14186 if (hdr->contents == NULL)
14187 continue;
14188 relsec->alloced = 1;
14189
14190 #if DEBUG_SECONDARY_RELOCS
14191 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14192 reloc_count, sec->name, relsec->name);
14193 #endif
14194 last_sym = NULL;
14195 last_sym_idx = 0;
14196 dst_rela = hdr->contents;
14197 src_irel = (arelent *) esd->sec_info;
14198 if (src_irel == NULL)
14199 {
14200 _bfd_error_handler
14201 /* xgettext:c-format */
14202 (_("%pB(%pA): error: internal relocs missing"
14203 " for secondary reloc section"),
14204 abfd, relsec);
14205 bfd_set_error (bfd_error_bad_value);
14206 result = false;
14207 continue;
14208 }
14209
14210 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14211 {
14212 Elf_Internal_Rela src_rela;
14213 arelent *ptr;
14214 asymbol *sym;
14215 int n;
14216
14217 ptr = src_irel + idx;
14218 if (ptr == NULL)
14219 {
14220 _bfd_error_handler
14221 /* xgettext:c-format */
14222 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14223 abfd, relsec, idx);
14224 bfd_set_error (bfd_error_bad_value);
14225 result = false;
14226 break;
14227 }
14228
14229 if (ptr->sym_ptr_ptr == NULL)
14230 {
14231 /* FIXME: Is this an error ? */
14232 n = 0;
14233 }
14234 else
14235 {
14236 sym = *ptr->sym_ptr_ptr;
14237
14238 if (sym == last_sym)
14239 n = last_sym_idx;
14240 else
14241 {
14242 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14243 if (n < 0)
14244 {
14245 _bfd_error_handler
14246 /* xgettext:c-format */
14247 (_("%pB(%pA): error: secondary reloc %zu"
14248 " references a missing symbol"),
14249 abfd, relsec, idx);
14250 bfd_set_error (bfd_error_bad_value);
14251 result = false;
14252 n = 0;
14253 }
14254
14255 last_sym = sym;
14256 last_sym_idx = n;
14257 }
14258
14259 if (sym->the_bfd != NULL
14260 && sym->the_bfd->xvec != abfd->xvec
14261 && ! _bfd_elf_validate_reloc (abfd, ptr))
14262 {
14263 _bfd_error_handler
14264 /* xgettext:c-format */
14265 (_("%pB(%pA): error: secondary reloc %zu"
14266 " references a deleted symbol"),
14267 abfd, relsec, idx);
14268 bfd_set_error (bfd_error_bad_value);
14269 result = false;
14270 n = 0;
14271 }
14272 }
14273
14274 src_rela.r_offset = ptr->address + addr_offset;
14275 if (ptr->howto == NULL)
14276 {
14277 _bfd_error_handler
14278 /* xgettext:c-format */
14279 (_("%pB(%pA): error: secondary reloc %zu"
14280 " is of an unknown type"),
14281 abfd, relsec, idx);
14282 bfd_set_error (bfd_error_bad_value);
14283 result = false;
14284 src_rela.r_info = r_info (0, 0);
14285 }
14286 else
14287 src_rela.r_info = r_info (n, ptr->howto->type);
14288 src_rela.r_addend = ptr->addend;
14289
14290 if (entsize == ebd->s->sizeof_rel)
14291 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14292 else /* entsize == ebd->s->sizeof_rela */
14293 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14294 }
14295 }
14296 }
14297
14298 return result;
14299 }
14300
14301 /* Mmap in section contents. */
14302
14303 static bool
14304 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14305 {
14306 #ifdef USE_MMAP
14307 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14308 if (bed->use_mmap
14309 && sec->compress_status == COMPRESS_SECTION_NONE
14310 && (sec->flags & SEC_LINKER_CREATED) == 0)
14311 {
14312 /* Use mmap only if section size >= the minimum mmap section
14313 size. */
14314 size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14315 size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14316 if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14317 {
14318 if (sec->contents != NULL)
14319 {
14320 if (!sec->mmapped_p)
14321 abort ();
14322 *buf = sec->contents;
14323 return true;
14324 }
14325 if (sec->mmapped_p)
14326 abort ();
14327 sec->mmapped_p = 1;
14328
14329 /* We can't use the final link preallocated buffer for mmap. */
14330 *buf = NULL;
14331 }
14332 }
14333 #endif
14334 /* FIXME: We should not get here if sec->alloced is set. */
14335 bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14336 if (ret && sec->mmapped_p)
14337 *buf = sec->contents;
14338 return ret;
14339 }
14340
14341 /* Mmap in section contents. */
14342
14343 bool
14344 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14345 {
14346 *buf = NULL;
14347 return elf_mmap_section_contents (abfd, sec, buf);
14348 }
14349
14350 /* Mmap in the full section contents for the final link. */
14351
14352 bool
14353 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14354 bfd_byte **buf)
14355 {
14356 return elf_mmap_section_contents (abfd, sec, buf);
14357 }
14358
14359 /* Munmap section contents. */
14360
14361 void
14362 _bfd_elf_munmap_section_contents (asection *sec, void *contents)
14363 {
14364 /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14365 CONTENTS may be NULL. */
14366 if (contents == NULL)
14367 return;
14368
14369 if (sec->alloced
14370 /* What a tangled web we weave with section contents.
14371 FIXME: We shouldn't need to test anything but sec->alloced
14372 here, but there are cases where a buffer is allocated for a
14373 section but then another buffer is malloc'd anyway. eg.
14374 trace through ld-elf/eh4 testcase on x86_64. */
14375 && (sec->contents == contents
14376 || elf_section_data (sec)->this_hdr.contents == contents))
14377 return;
14378
14379 /* Don't leave pointers to data we are about to munmap or free. */
14380 if (sec->contents == contents)
14381 sec->contents = NULL;
14382 if (elf_section_data (sec)->this_hdr.contents == contents)
14383 elf_section_data (sec)->this_hdr.contents = NULL;
14384
14385 #ifdef USE_MMAP
14386 if (sec->mmapped_p)
14387 {
14388 /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14389 malloced, CONTENTS_ADDR is set to NULL. */
14390 if (elf_section_data (sec)->contents_addr != NULL)
14391 {
14392 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14393 if (munmap (elf_section_data (sec)->contents_addr,
14394 elf_section_data (sec)->contents_size) != 0)
14395 abort ();
14396 sec->mmapped_p = 0;
14397 elf_section_data (sec)->contents_addr = NULL;
14398 elf_section_data (sec)->contents_size = 0;
14399 return;
14400 }
14401 }
14402 #endif
14403
14404 free (contents);
14405 }
14406
14407 /* Munmap the full section contents for the final link. */
14408
14409 void
14410 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14411 {
14412 #ifdef USE_MMAP
14413 if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14414 {
14415 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14416 malloced, CONTENTS_ADDR is set to NULL. */
14417 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */
14418 if (munmap (elf_section_data (sec)->contents_addr,
14419 elf_section_data (sec)->contents_size) != 0)
14420 abort ();
14421 sec->mmapped_p = 0;
14422 sec->contents = NULL;
14423 elf_section_data (sec)->this_hdr.contents = NULL;
14424 elf_section_data (sec)->contents_addr = NULL;
14425 elf_section_data (sec)->contents_size = 0;
14426 }
14427 #endif
14428 }
14429