go32 sanity check
[binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2
3 Copyright (C) 1993-2022 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 const unsigned char *name = (const unsigned char *) namearg;
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
203
204 while ((ch = *name++) != '\0')
205 {
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
208 {
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
213 }
214 }
215 return h & 0xffffffff;
216 }
217
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
220
221 unsigned long
222 bfd_elf_gnu_hash (const char *namearg)
223 {
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
227
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
231 }
232
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bool
236 bfd_elf_allocate_object (bfd *abfd,
237 size_t object_size,
238 enum elf_target_id object_id)
239 {
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
243 return false;
244
245 elf_object_id (abfd) = object_id;
246 if (abfd->direction != read_direction)
247 {
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
250 return false;
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 }
254 return true;
255 }
256
257
258 bool
259 bfd_elf_make_object (bfd *abfd)
260 {
261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
263 bed->target_id);
264 }
265
266 bool
267 bfd_elf_mkcorefile (bfd *abfd)
268 {
269 /* I think this can be done just like an object file. */
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271 return false;
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
274 }
275
276 char *
277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
278 {
279 Elf_Internal_Shdr **i_shdrp;
280 bfd_byte *shstrtab = NULL;
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
283
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
288 return NULL;
289
290 shstrtab = i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
292 {
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
296
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize + 1 <= 1
300 || bfd_seek (abfd, offset, SEEK_SET) != 0
301 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
302 shstrtabsize)) == NULL)
303 {
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp[shindex]->sh_size = 0;
308 }
309 else
310 shstrtab[shstrtabsize] = '\0';
311 i_shdrp[shindex]->contents = shstrtab;
312 }
313 return (char *) shstrtab;
314 }
315
316 char *
317 bfd_elf_string_from_elf_section (bfd *abfd,
318 unsigned int shindex,
319 unsigned int strindex)
320 {
321 Elf_Internal_Shdr *hdr;
322
323 if (strindex == 0)
324 return "";
325
326 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
327 return NULL;
328
329 hdr = elf_elfsections (abfd)[shindex];
330
331 if (hdr->contents == NULL)
332 {
333 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
334 {
335 /* PR 17512: file: f057ec89. */
336 /* xgettext:c-format */
337 _bfd_error_handler (_("%pB: attempt to load strings from"
338 " a non-string section (number %d)"),
339 abfd, shindex);
340 return NULL;
341 }
342
343 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
344 return NULL;
345 }
346 else
347 {
348 /* PR 24273: The string section's contents may have already
349 been loaded elsewhere, eg because a corrupt file has the
350 string section index in the ELF header pointing at a group
351 section. So be paranoid, and test that the last byte of
352 the section is zero. */
353 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
354 return NULL;
355 }
356
357 if (strindex >= hdr->sh_size)
358 {
359 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
360 _bfd_error_handler
361 /* xgettext:c-format */
362 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
363 abfd, strindex, (uint64_t) hdr->sh_size,
364 (shindex == shstrndx && strindex == hdr->sh_name
365 ? ".shstrtab"
366 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
367 return NULL;
368 }
369
370 return ((char *) hdr->contents) + strindex;
371 }
372
373 /* Read and convert symbols to internal format.
374 SYMCOUNT specifies the number of symbols to read, starting from
375 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 are non-NULL, they are used to store the internal symbols, external
377 symbols, and symbol section index extensions, respectively.
378 Returns a pointer to the internal symbol buffer (malloced if necessary)
379 or NULL if there were no symbols or some kind of problem. */
380
381 Elf_Internal_Sym *
382 bfd_elf_get_elf_syms (bfd *ibfd,
383 Elf_Internal_Shdr *symtab_hdr,
384 size_t symcount,
385 size_t symoffset,
386 Elf_Internal_Sym *intsym_buf,
387 void *extsym_buf,
388 Elf_External_Sym_Shndx *extshndx_buf)
389 {
390 Elf_Internal_Shdr *shndx_hdr;
391 void *alloc_ext;
392 const bfd_byte *esym;
393 Elf_External_Sym_Shndx *alloc_extshndx;
394 Elf_External_Sym_Shndx *shndx;
395 Elf_Internal_Sym *alloc_intsym;
396 Elf_Internal_Sym *isym;
397 Elf_Internal_Sym *isymend;
398 const struct elf_backend_data *bed;
399 size_t extsym_size;
400 size_t amt;
401 file_ptr pos;
402
403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
404 abort ();
405
406 if (symcount == 0)
407 return intsym_buf;
408
409 /* Normal syms might have section extension entries. */
410 shndx_hdr = NULL;
411 if (elf_symtab_shndx_list (ibfd) != NULL)
412 {
413 elf_section_list * entry;
414 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
415
416 /* Find an index section that is linked to this symtab section. */
417 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
418 {
419 /* PR 20063. */
420 if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 continue;
422
423 if (sections[entry->hdr.sh_link] == symtab_hdr)
424 {
425 shndx_hdr = & entry->hdr;
426 break;
427 };
428 }
429
430 if (shndx_hdr == NULL)
431 {
432 if (symtab_hdr == &elf_symtab_hdr (ibfd))
433 /* Not really accurate, but this was how the old code used
434 to work. */
435 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
436 /* Otherwise we do nothing. The assumption is that
437 the index table will not be needed. */
438 }
439 }
440
441 /* Read the symbols. */
442 alloc_ext = NULL;
443 alloc_extshndx = NULL;
444 alloc_intsym = NULL;
445 bed = get_elf_backend_data (ibfd);
446 extsym_size = bed->s->sizeof_sym;
447 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
448 {
449 bfd_set_error (bfd_error_file_too_big);
450 intsym_buf = NULL;
451 goto out;
452 }
453 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
454 if (extsym_buf == NULL)
455 {
456 alloc_ext = bfd_malloc (amt);
457 extsym_buf = alloc_ext;
458 }
459 if (extsym_buf == NULL
460 || bfd_seek (ibfd, pos, SEEK_SET) != 0
461 || bfd_bread (extsym_buf, amt, ibfd) != amt)
462 {
463 intsym_buf = NULL;
464 goto out;
465 }
466
467 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
468 extshndx_buf = NULL;
469 else
470 {
471 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
472 {
473 bfd_set_error (bfd_error_file_too_big);
474 intsym_buf = NULL;
475 goto out;
476 }
477 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
478 if (extshndx_buf == NULL)
479 {
480 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
481 extshndx_buf = alloc_extshndx;
482 }
483 if (extshndx_buf == NULL
484 || bfd_seek (ibfd, pos, SEEK_SET) != 0
485 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
486 {
487 intsym_buf = NULL;
488 goto out;
489 }
490 }
491
492 if (intsym_buf == NULL)
493 {
494 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
495 {
496 bfd_set_error (bfd_error_file_too_big);
497 goto out;
498 }
499 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
500 intsym_buf = alloc_intsym;
501 if (intsym_buf == NULL)
502 goto out;
503 }
504
505 /* Convert the symbols to internal form. */
506 isymend = intsym_buf + symcount;
507 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
508 shndx = extshndx_buf;
509 isym < isymend;
510 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
511 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
512 {
513 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
514 /* xgettext:c-format */
515 _bfd_error_handler (_("%pB symbol number %lu references"
516 " nonexistent SHT_SYMTAB_SHNDX section"),
517 ibfd, (unsigned long) symoffset);
518 free (alloc_intsym);
519 intsym_buf = NULL;
520 goto out;
521 }
522
523 out:
524 free (alloc_ext);
525 free (alloc_extshndx);
526
527 return intsym_buf;
528 }
529
530 /* Look up a symbol name. */
531 const char *
532 bfd_elf_sym_name (bfd *abfd,
533 Elf_Internal_Shdr *symtab_hdr,
534 Elf_Internal_Sym *isym,
535 asection *sym_sec)
536 {
537 const char *name;
538 unsigned int iname = isym->st_name;
539 unsigned int shindex = symtab_hdr->sh_link;
540
541 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
542 /* Check for a bogus st_shndx to avoid crashing. */
543 && isym->st_shndx < elf_numsections (abfd))
544 {
545 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
546 shindex = elf_elfheader (abfd)->e_shstrndx;
547 }
548
549 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
550 if (name == NULL)
551 name = "(null)";
552 else if (sym_sec && *name == '\0')
553 name = bfd_section_name (sym_sec);
554
555 return name;
556 }
557
558 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
559 sections. The first element is the flags, the rest are section
560 pointers. */
561
562 typedef union elf_internal_group {
563 Elf_Internal_Shdr *shdr;
564 unsigned int flags;
565 } Elf_Internal_Group;
566
567 /* Return the name of the group signature symbol. Why isn't the
568 signature just a string? */
569
570 static const char *
571 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
572 {
573 Elf_Internal_Shdr *hdr;
574 unsigned char esym[sizeof (Elf64_External_Sym)];
575 Elf_External_Sym_Shndx eshndx;
576 Elf_Internal_Sym isym;
577
578 /* First we need to ensure the symbol table is available. Make sure
579 that it is a symbol table section. */
580 if (ghdr->sh_link >= elf_numsections (abfd))
581 return NULL;
582 hdr = elf_elfsections (abfd) [ghdr->sh_link];
583 if (hdr->sh_type != SHT_SYMTAB
584 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
585 return NULL;
586
587 /* Go read the symbol. */
588 hdr = &elf_tdata (abfd)->symtab_hdr;
589 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
590 &isym, esym, &eshndx) == NULL)
591 return NULL;
592
593 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
594 }
595
596 /* Set next_in_group list pointer, and group name for NEWSECT. */
597
598 static bool
599 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
600 {
601 unsigned int num_group = elf_tdata (abfd)->num_group;
602
603 /* If num_group is zero, read in all SHT_GROUP sections. The count
604 is set to -1 if there are no SHT_GROUP sections. */
605 if (num_group == 0)
606 {
607 unsigned int i, shnum;
608
609 /* First count the number of groups. If we have a SHT_GROUP
610 section with just a flag word (ie. sh_size is 4), ignore it. */
611 shnum = elf_numsections (abfd);
612 num_group = 0;
613
614 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
615 ( (shdr)->sh_type == SHT_GROUP \
616 && (shdr)->sh_size >= minsize \
617 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
618 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
619
620 for (i = 0; i < shnum; i++)
621 {
622 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
623
624 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
625 num_group += 1;
626 }
627
628 if (num_group == 0)
629 {
630 num_group = (unsigned) -1;
631 elf_tdata (abfd)->num_group = num_group;
632 elf_tdata (abfd)->group_sect_ptr = NULL;
633 }
634 else
635 {
636 /* We keep a list of elf section headers for group sections,
637 so we can find them quickly. */
638 size_t amt;
639
640 elf_tdata (abfd)->num_group = num_group;
641 amt = num_group * sizeof (Elf_Internal_Shdr *);
642 elf_tdata (abfd)->group_sect_ptr
643 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
644 if (elf_tdata (abfd)->group_sect_ptr == NULL)
645 return false;
646 num_group = 0;
647
648 for (i = 0; i < shnum; i++)
649 {
650 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
651
652 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
653 {
654 unsigned char *src;
655 Elf_Internal_Group *dest;
656
657 /* Make sure the group section has a BFD section
658 attached to it. */
659 if (!bfd_section_from_shdr (abfd, i))
660 return false;
661
662 /* Add to list of sections. */
663 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
664 num_group += 1;
665
666 /* Read the raw contents. */
667 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
668 shdr->contents = NULL;
669 if (_bfd_mul_overflow (shdr->sh_size,
670 sizeof (*dest) / 4, &amt)
671 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
672 || !(shdr->contents
673 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
674 {
675 _bfd_error_handler
676 /* xgettext:c-format */
677 (_("%pB: invalid size field in group section"
678 " header: %#" PRIx64 ""),
679 abfd, (uint64_t) shdr->sh_size);
680 bfd_set_error (bfd_error_bad_value);
681 -- num_group;
682 continue;
683 }
684
685 /* Translate raw contents, a flag word followed by an
686 array of elf section indices all in target byte order,
687 to the flag word followed by an array of elf section
688 pointers. */
689 src = shdr->contents + shdr->sh_size;
690 dest = (Elf_Internal_Group *) (shdr->contents + amt);
691
692 while (1)
693 {
694 unsigned int idx;
695
696 src -= 4;
697 --dest;
698 idx = H_GET_32 (abfd, src);
699 if (src == shdr->contents)
700 {
701 dest->shdr = NULL;
702 dest->flags = idx;
703 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
704 shdr->bfd_section->flags
705 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
706 break;
707 }
708 if (idx < shnum)
709 {
710 dest->shdr = elf_elfsections (abfd)[idx];
711 /* PR binutils/23199: All sections in a
712 section group should be marked with
713 SHF_GROUP. But some tools generate
714 broken objects without SHF_GROUP. Fix
715 them up here. */
716 dest->shdr->sh_flags |= SHF_GROUP;
717 }
718 if (idx >= shnum
719 || dest->shdr->sh_type == SHT_GROUP)
720 {
721 _bfd_error_handler
722 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
723 abfd, i);
724 dest->shdr = NULL;
725 }
726 }
727 }
728 }
729
730 /* PR 17510: Corrupt binaries might contain invalid groups. */
731 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
732 {
733 elf_tdata (abfd)->num_group = num_group;
734
735 /* If all groups are invalid then fail. */
736 if (num_group == 0)
737 {
738 elf_tdata (abfd)->group_sect_ptr = NULL;
739 elf_tdata (abfd)->num_group = num_group = -1;
740 _bfd_error_handler
741 (_("%pB: no valid group sections found"), abfd);
742 bfd_set_error (bfd_error_bad_value);
743 }
744 }
745 }
746 }
747
748 if (num_group != (unsigned) -1)
749 {
750 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
751 unsigned int j;
752
753 for (j = 0; j < num_group; j++)
754 {
755 /* Begin search from previous found group. */
756 unsigned i = (j + search_offset) % num_group;
757
758 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
759 Elf_Internal_Group *idx;
760 bfd_size_type n_elt;
761
762 if (shdr == NULL)
763 continue;
764
765 idx = (Elf_Internal_Group *) shdr->contents;
766 if (idx == NULL || shdr->sh_size < 4)
767 {
768 /* See PR 21957 for a reproducer. */
769 /* xgettext:c-format */
770 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
771 abfd, shdr->bfd_section);
772 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
773 bfd_set_error (bfd_error_bad_value);
774 return false;
775 }
776 n_elt = shdr->sh_size / 4;
777
778 /* Look through this group's sections to see if current
779 section is a member. */
780 while (--n_elt != 0)
781 if ((++idx)->shdr == hdr)
782 {
783 asection *s = NULL;
784
785 /* We are a member of this group. Go looking through
786 other members to see if any others are linked via
787 next_in_group. */
788 idx = (Elf_Internal_Group *) shdr->contents;
789 n_elt = shdr->sh_size / 4;
790 while (--n_elt != 0)
791 if ((++idx)->shdr != NULL
792 && (s = idx->shdr->bfd_section) != NULL
793 && elf_next_in_group (s) != NULL)
794 break;
795 if (n_elt != 0)
796 {
797 /* Snarf the group name from other member, and
798 insert current section in circular list. */
799 elf_group_name (newsect) = elf_group_name (s);
800 elf_next_in_group (newsect) = elf_next_in_group (s);
801 elf_next_in_group (s) = newsect;
802 }
803 else
804 {
805 const char *gname;
806
807 gname = group_signature (abfd, shdr);
808 if (gname == NULL)
809 return false;
810 elf_group_name (newsect) = gname;
811
812 /* Start a circular list with one element. */
813 elf_next_in_group (newsect) = newsect;
814 }
815
816 /* If the group section has been created, point to the
817 new member. */
818 if (shdr->bfd_section != NULL)
819 elf_next_in_group (shdr->bfd_section) = newsect;
820
821 elf_tdata (abfd)->group_search_offset = i;
822 j = num_group - 1;
823 break;
824 }
825 }
826 }
827
828 if (elf_group_name (newsect) == NULL)
829 {
830 /* xgettext:c-format */
831 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
832 abfd, newsect);
833 /* PR 29532: Return true here, even though the group info has not been
834 read. Separate debug info files can have empty group sections, but
835 we do not want this to prevent them from being loaded as otherwise
836 GDB will not be able to use them. */
837 return true;
838 }
839 return true;
840 }
841
842 bool
843 _bfd_elf_setup_sections (bfd *abfd)
844 {
845 unsigned int i;
846 unsigned int num_group = elf_tdata (abfd)->num_group;
847 bool result = true;
848 asection *s;
849
850 /* Process SHF_LINK_ORDER. */
851 for (s = abfd->sections; s != NULL; s = s->next)
852 {
853 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
854 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
855 {
856 unsigned int elfsec = this_hdr->sh_link;
857 /* An sh_link value of 0 is now allowed. It indicates that linked
858 to section has already been discarded, but that the current
859 section has been retained for some other reason. This linking
860 section is still a candidate for later garbage collection
861 however. */
862 if (elfsec == 0)
863 {
864 elf_linked_to_section (s) = NULL;
865 }
866 else
867 {
868 asection *linksec = NULL;
869
870 if (elfsec < elf_numsections (abfd))
871 {
872 this_hdr = elf_elfsections (abfd)[elfsec];
873 linksec = this_hdr->bfd_section;
874 }
875
876 /* PR 1991, 2008:
877 Some strip/objcopy may leave an incorrect value in
878 sh_link. We don't want to proceed. */
879 if (linksec == NULL)
880 {
881 _bfd_error_handler
882 /* xgettext:c-format */
883 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
884 s->owner, elfsec, s);
885 result = false;
886 }
887
888 elf_linked_to_section (s) = linksec;
889 }
890 }
891 else if (this_hdr->sh_type == SHT_GROUP
892 && elf_next_in_group (s) == NULL)
893 {
894 _bfd_error_handler
895 /* xgettext:c-format */
896 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
897 abfd, elf_section_data (s)->this_idx);
898 result = false;
899 }
900 }
901
902 /* Process section groups. */
903 if (num_group == (unsigned) -1)
904 return result;
905
906 for (i = 0; i < num_group; i++)
907 {
908 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
909 Elf_Internal_Group *idx;
910 unsigned int n_elt;
911
912 /* PR binutils/18758: Beware of corrupt binaries with invalid
913 group data. */
914 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
915 {
916 _bfd_error_handler
917 /* xgettext:c-format */
918 (_("%pB: section group entry number %u is corrupt"),
919 abfd, i);
920 result = false;
921 continue;
922 }
923
924 idx = (Elf_Internal_Group *) shdr->contents;
925 n_elt = shdr->sh_size / 4;
926
927 while (--n_elt != 0)
928 {
929 ++ idx;
930
931 if (idx->shdr == NULL)
932 continue;
933 else if (idx->shdr->bfd_section)
934 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
935 else if (idx->shdr->sh_type != SHT_RELA
936 && idx->shdr->sh_type != SHT_REL)
937 {
938 /* There are some unknown sections in the group. */
939 _bfd_error_handler
940 /* xgettext:c-format */
941 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
942 abfd,
943 idx->shdr->sh_type,
944 bfd_elf_string_from_elf_section (abfd,
945 (elf_elfheader (abfd)
946 ->e_shstrndx),
947 idx->shdr->sh_name),
948 shdr->bfd_section);
949 result = false;
950 }
951 }
952 }
953
954 return result;
955 }
956
957 bool
958 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959 {
960 return elf_next_in_group (sec) != NULL;
961 }
962
963 const char *
964 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
965 {
966 if (elf_sec_group (sec) != NULL)
967 return elf_group_name (sec);
968 return NULL;
969 }
970
971 static char *
972 convert_debug_to_zdebug (bfd *abfd, const char *name)
973 {
974 unsigned int len = strlen (name);
975 char *new_name = bfd_alloc (abfd, len + 2);
976 if (new_name == NULL)
977 return NULL;
978 new_name[0] = '.';
979 new_name[1] = 'z';
980 memcpy (new_name + 2, name + 1, len);
981 return new_name;
982 }
983
984 static char *
985 convert_zdebug_to_debug (bfd *abfd, const char *name)
986 {
987 unsigned int len = strlen (name);
988 char *new_name = bfd_alloc (abfd, len);
989 if (new_name == NULL)
990 return NULL;
991 new_name[0] = '.';
992 memcpy (new_name + 1, name + 2, len - 1);
993 return new_name;
994 }
995
996 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
997
998 struct lto_section
999 {
1000 int16_t major_version;
1001 int16_t minor_version;
1002 unsigned char slim_object;
1003
1004 /* Flags is a private field that is not defined publicly. */
1005 uint16_t flags;
1006 };
1007
1008 /* Make a BFD section from an ELF section. We store a pointer to the
1009 BFD section in the bfd_section field of the header. */
1010
1011 bool
1012 _bfd_elf_make_section_from_shdr (bfd *abfd,
1013 Elf_Internal_Shdr *hdr,
1014 const char *name,
1015 int shindex)
1016 {
1017 asection *newsect;
1018 flagword flags;
1019 const struct elf_backend_data *bed;
1020 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
1021
1022 if (hdr->bfd_section != NULL)
1023 return true;
1024
1025 newsect = bfd_make_section_anyway (abfd, name);
1026 if (newsect == NULL)
1027 return false;
1028
1029 hdr->bfd_section = newsect;
1030 elf_section_data (newsect)->this_hdr = *hdr;
1031 elf_section_data (newsect)->this_idx = shindex;
1032
1033 /* Always use the real type/flags. */
1034 elf_section_type (newsect) = hdr->sh_type;
1035 elf_section_flags (newsect) = hdr->sh_flags;
1036
1037 newsect->filepos = hdr->sh_offset;
1038
1039 flags = SEC_NO_FLAGS;
1040 if (hdr->sh_type != SHT_NOBITS)
1041 flags |= SEC_HAS_CONTENTS;
1042 if (hdr->sh_type == SHT_GROUP)
1043 flags |= SEC_GROUP;
1044 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1045 {
1046 flags |= SEC_ALLOC;
1047 if (hdr->sh_type != SHT_NOBITS)
1048 flags |= SEC_LOAD;
1049 }
1050 if ((hdr->sh_flags & SHF_WRITE) == 0)
1051 flags |= SEC_READONLY;
1052 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1053 flags |= SEC_CODE;
1054 else if ((flags & SEC_LOAD) != 0)
1055 flags |= SEC_DATA;
1056 if ((hdr->sh_flags & SHF_MERGE) != 0)
1057 {
1058 flags |= SEC_MERGE;
1059 newsect->entsize = hdr->sh_entsize;
1060 }
1061 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1062 flags |= SEC_STRINGS;
1063 if (hdr->sh_flags & SHF_GROUP)
1064 if (!setup_group (abfd, hdr, newsect))
1065 return false;
1066 if ((hdr->sh_flags & SHF_TLS) != 0)
1067 flags |= SEC_THREAD_LOCAL;
1068 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1069 flags |= SEC_EXCLUDE;
1070
1071 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1072 {
1073 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1074 but binutils as of 2019-07-23 did not set the EI_OSABI header
1075 byte. */
1076 case ELFOSABI_GNU:
1077 case ELFOSABI_FREEBSD:
1078 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1079 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1080 /* Fall through */
1081 case ELFOSABI_NONE:
1082 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1083 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1084 break;
1085 }
1086
1087 if ((flags & SEC_ALLOC) == 0)
1088 {
1089 /* The debugging sections appear to be recognized only by name,
1090 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1091 if (name [0] == '.')
1092 {
1093 if (startswith (name, ".debug")
1094 || startswith (name, ".gnu.debuglto_.debug_")
1095 || startswith (name, ".gnu.linkonce.wi.")
1096 || startswith (name, ".zdebug"))
1097 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1098 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1099 || startswith (name, ".note.gnu"))
1100 {
1101 flags |= SEC_ELF_OCTETS;
1102 opb = 1;
1103 }
1104 else if (startswith (name, ".line")
1105 || startswith (name, ".stab")
1106 || strcmp (name, ".gdb_index") == 0)
1107 flags |= SEC_DEBUGGING;
1108 }
1109 }
1110
1111 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1112 || !bfd_set_section_size (newsect, hdr->sh_size)
1113 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1114 & -hdr->sh_addralign)))
1115 return false;
1116
1117 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1118 only link a single copy of the section. This is used to support
1119 g++. g++ will emit each template expansion in its own section.
1120 The symbols will be defined as weak, so that multiple definitions
1121 are permitted. The GNU linker extension is to actually discard
1122 all but one of the sections. */
1123 if (startswith (name, ".gnu.linkonce")
1124 && elf_next_in_group (newsect) == NULL)
1125 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1126
1127 if (!bfd_set_section_flags (newsect, flags))
1128 return false;
1129
1130 bed = get_elf_backend_data (abfd);
1131 if (bed->elf_backend_section_flags)
1132 if (!bed->elf_backend_section_flags (hdr))
1133 return false;
1134
1135 /* We do not parse the PT_NOTE segments as we are interested even in the
1136 separate debug info files which may have the segments offsets corrupted.
1137 PT_NOTEs from the core files are currently not parsed using BFD. */
1138 if (hdr->sh_type == SHT_NOTE)
1139 {
1140 bfd_byte *contents;
1141
1142 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1143 return false;
1144
1145 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1146 hdr->sh_offset, hdr->sh_addralign);
1147 free (contents);
1148 }
1149
1150 if ((newsect->flags & SEC_ALLOC) != 0)
1151 {
1152 Elf_Internal_Phdr *phdr;
1153 unsigned int i, nload;
1154
1155 /* Some ELF linkers produce binaries with all the program header
1156 p_paddr fields zero. If we have such a binary with more than
1157 one PT_LOAD header, then leave the section lma equal to vma
1158 so that we don't create sections with overlapping lma. */
1159 phdr = elf_tdata (abfd)->phdr;
1160 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1161 if (phdr->p_paddr != 0)
1162 break;
1163 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1164 ++nload;
1165 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1166 return true;
1167
1168 phdr = elf_tdata (abfd)->phdr;
1169 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1170 {
1171 if (((phdr->p_type == PT_LOAD
1172 && (hdr->sh_flags & SHF_TLS) == 0)
1173 || phdr->p_type == PT_TLS)
1174 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1175 {
1176 if ((newsect->flags & SEC_LOAD) == 0)
1177 newsect->lma = (phdr->p_paddr
1178 + hdr->sh_addr - phdr->p_vaddr) / opb;
1179 else
1180 /* We used to use the same adjustment for SEC_LOAD
1181 sections, but that doesn't work if the segment
1182 is packed with code from multiple VMAs.
1183 Instead we calculate the section LMA based on
1184 the segment LMA. It is assumed that the
1185 segment will contain sections with contiguous
1186 LMAs, even if the VMAs are not. */
1187 newsect->lma = (phdr->p_paddr
1188 + hdr->sh_offset - phdr->p_offset) / opb;
1189
1190 /* With contiguous segments, we can't tell from file
1191 offsets whether a section with zero size should
1192 be placed at the end of one segment or the
1193 beginning of the next. Decide based on vaddr. */
1194 if (hdr->sh_addr >= phdr->p_vaddr
1195 && (hdr->sh_addr + hdr->sh_size
1196 <= phdr->p_vaddr + phdr->p_memsz))
1197 break;
1198 }
1199 }
1200 }
1201
1202 /* Compress/decompress DWARF debug sections with names: .debug_*,
1203 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1204 if ((newsect->flags & SEC_DEBUGGING) != 0
1205 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1206 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1207 {
1208 enum { nothing, compress, decompress } action = nothing;
1209 int compression_header_size;
1210 bfd_size_type uncompressed_size;
1211 unsigned int uncompressed_align_power;
1212 unsigned int ch_type = 0;
1213 bool compressed
1214 = bfd_is_section_compressed_info (abfd, newsect,
1215 &compression_header_size,
1216 &uncompressed_size,
1217 &uncompressed_align_power,
1218 &ch_type);
1219
1220 /* Should we decompress? */
1221 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1222 action = decompress;
1223
1224 /* Should we compress? Or convert to a different compression? */
1225 else if ((abfd->flags & BFD_COMPRESS) != 0
1226 && newsect->size != 0
1227 && compression_header_size >= 0
1228 && uncompressed_size > 0)
1229 {
1230 if (!compressed)
1231 action = compress;
1232 else
1233 {
1234 unsigned int new_ch_type = 0;
1235 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1236 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1237 ? ELFCOMPRESS_ZSTD : ELFCOMPRESS_ZLIB);
1238 if (new_ch_type != ch_type)
1239 action = compress;
1240 }
1241 }
1242
1243 if (action == compress)
1244 {
1245 if (!bfd_init_section_compress_status (abfd, newsect))
1246 {
1247 _bfd_error_handler
1248 /* xgettext:c-format */
1249 (_("%pB: unable to compress section %s"), abfd, name);
1250 return false;
1251 }
1252 }
1253 else if (action == decompress)
1254 {
1255 if (!bfd_init_section_decompress_status (abfd, newsect))
1256 {
1257 _bfd_error_handler
1258 /* xgettext:c-format */
1259 (_("%pB: unable to decompress section %s"), abfd, name);
1260 return false;
1261 }
1262 #ifndef HAVE_ZSTD
1263 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1264 {
1265 _bfd_error_handler
1266 /* xgettext:c-format */
1267 (_ ("%pB: section %s is compressed with zstd, but BFD "
1268 "is not built with zstd support"),
1269 abfd, name);
1270 newsect->compress_status = COMPRESS_SECTION_NONE;
1271 return false;
1272 }
1273 #endif
1274 }
1275
1276 if (action != nothing)
1277 {
1278 if (abfd->is_linker_input)
1279 {
1280 if (name[1] == 'z'
1281 && (action == decompress
1282 || (action == compress
1283 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1284 {
1285 /* Convert section name from .zdebug_* to .debug_* so
1286 that linker will consider this section as a debug
1287 section. */
1288 char *new_name = convert_zdebug_to_debug (abfd, name);
1289 if (new_name == NULL)
1290 return false;
1291 bfd_rename_section (newsect, new_name);
1292 }
1293 }
1294 else
1295 /* For objdump, don't rename the section. For objcopy, delay
1296 section rename to elf_fake_sections. */
1297 newsect->flags |= SEC_ELF_RENAME;
1298 }
1299 }
1300
1301 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1302 section. */
1303 if (startswith (name, ".gnu.lto_.lto."))
1304 {
1305 struct lto_section lsection;
1306 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1307 sizeof (struct lto_section)))
1308 abfd->lto_slim_object = lsection.slim_object;
1309 }
1310
1311 return true;
1312 }
1313
1314 const char *const bfd_elf_section_type_names[] =
1315 {
1316 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1317 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1318 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1319 };
1320
1321 /* ELF relocs are against symbols. If we are producing relocatable
1322 output, and the reloc is against an external symbol, and nothing
1323 has given us any additional addend, the resulting reloc will also
1324 be against the same symbol. In such a case, we don't want to
1325 change anything about the way the reloc is handled, since it will
1326 all be done at final link time. Rather than put special case code
1327 into bfd_perform_relocation, all the reloc types use this howto
1328 function, or should call this function for relocatable output. */
1329
1330 bfd_reloc_status_type
1331 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1332 arelent *reloc_entry,
1333 asymbol *symbol,
1334 void *data ATTRIBUTE_UNUSED,
1335 asection *input_section,
1336 bfd *output_bfd,
1337 char **error_message ATTRIBUTE_UNUSED)
1338 {
1339 if (output_bfd != NULL
1340 && (symbol->flags & BSF_SECTION_SYM) == 0
1341 && (! reloc_entry->howto->partial_inplace
1342 || reloc_entry->addend == 0))
1343 {
1344 reloc_entry->address += input_section->output_offset;
1345 return bfd_reloc_ok;
1346 }
1347
1348 /* In some cases the relocation should be treated as output section
1349 relative, as when linking ELF DWARF into PE COFF. Many ELF
1350 targets lack section relative relocations and instead use
1351 ordinary absolute relocations for references between DWARF
1352 sections. That is arguably a bug in those targets but it happens
1353 to work for the usual case of linking to non-loaded ELF debug
1354 sections with VMAs forced to zero. PE COFF on the other hand
1355 doesn't allow a section VMA of zero. */
1356 if (output_bfd == NULL
1357 && !reloc_entry->howto->pc_relative
1358 && (symbol->section->flags & SEC_DEBUGGING) != 0
1359 && (input_section->flags & SEC_DEBUGGING) != 0)
1360 reloc_entry->addend -= symbol->section->output_section->vma;
1361
1362 return bfd_reloc_continue;
1363 }
1364 \f
1365 /* Returns TRUE if section A matches section B.
1366 Names, addresses and links may be different, but everything else
1367 should be the same. */
1368
1369 static bool
1370 section_match (const Elf_Internal_Shdr * a,
1371 const Elf_Internal_Shdr * b)
1372 {
1373 if (a->sh_type != b->sh_type
1374 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1375 || a->sh_addralign != b->sh_addralign
1376 || a->sh_entsize != b->sh_entsize)
1377 return false;
1378 if (a->sh_type == SHT_SYMTAB
1379 || a->sh_type == SHT_STRTAB)
1380 return true;
1381 return a->sh_size == b->sh_size;
1382 }
1383
1384 /* Find a section in OBFD that has the same characteristics
1385 as IHEADER. Return the index of this section or SHN_UNDEF if
1386 none can be found. Check's section HINT first, as this is likely
1387 to be the correct section. */
1388
1389 static unsigned int
1390 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1391 const unsigned int hint)
1392 {
1393 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1394 unsigned int i;
1395
1396 BFD_ASSERT (iheader != NULL);
1397
1398 /* See PR 20922 for a reproducer of the NULL test. */
1399 if (hint < elf_numsections (obfd)
1400 && oheaders[hint] != NULL
1401 && section_match (oheaders[hint], iheader))
1402 return hint;
1403
1404 for (i = 1; i < elf_numsections (obfd); i++)
1405 {
1406 Elf_Internal_Shdr * oheader = oheaders[i];
1407
1408 if (oheader == NULL)
1409 continue;
1410 if (section_match (oheader, iheader))
1411 /* FIXME: Do we care if there is a potential for
1412 multiple matches ? */
1413 return i;
1414 }
1415
1416 return SHN_UNDEF;
1417 }
1418
1419 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1420 Processor specific section, based upon a matching input section.
1421 Returns TRUE upon success, FALSE otherwise. */
1422
1423 static bool
1424 copy_special_section_fields (const bfd *ibfd,
1425 bfd *obfd,
1426 const Elf_Internal_Shdr *iheader,
1427 Elf_Internal_Shdr *oheader,
1428 const unsigned int secnum)
1429 {
1430 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1431 const Elf_Internal_Shdr **iheaders
1432 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1433 bool changed = false;
1434 unsigned int sh_link;
1435
1436 if (oheader->sh_type == SHT_NOBITS)
1437 {
1438 /* This is a feature for objcopy --only-keep-debug:
1439 When a section's type is changed to NOBITS, we preserve
1440 the sh_link and sh_info fields so that they can be
1441 matched up with the original.
1442
1443 Note: Strictly speaking these assignments are wrong.
1444 The sh_link and sh_info fields should point to the
1445 relevent sections in the output BFD, which may not be in
1446 the same location as they were in the input BFD. But
1447 the whole point of this action is to preserve the
1448 original values of the sh_link and sh_info fields, so
1449 that they can be matched up with the section headers in
1450 the original file. So strictly speaking we may be
1451 creating an invalid ELF file, but it is only for a file
1452 that just contains debug info and only for sections
1453 without any contents. */
1454 if (oheader->sh_link == 0)
1455 oheader->sh_link = iheader->sh_link;
1456 if (oheader->sh_info == 0)
1457 oheader->sh_info = iheader->sh_info;
1458 return true;
1459 }
1460
1461 /* Allow the target a chance to decide how these fields should be set. */
1462 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1463 iheader, oheader))
1464 return true;
1465
1466 /* We have an iheader which might match oheader, and which has non-zero
1467 sh_info and/or sh_link fields. Attempt to follow those links and find
1468 the section in the output bfd which corresponds to the linked section
1469 in the input bfd. */
1470 if (iheader->sh_link != SHN_UNDEF)
1471 {
1472 /* See PR 20931 for a reproducer. */
1473 if (iheader->sh_link >= elf_numsections (ibfd))
1474 {
1475 _bfd_error_handler
1476 /* xgettext:c-format */
1477 (_("%pB: invalid sh_link field (%d) in section number %d"),
1478 ibfd, iheader->sh_link, secnum);
1479 return false;
1480 }
1481
1482 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1483 if (sh_link != SHN_UNDEF)
1484 {
1485 oheader->sh_link = sh_link;
1486 changed = true;
1487 }
1488 else
1489 /* FIXME: Should we install iheader->sh_link
1490 if we could not find a match ? */
1491 _bfd_error_handler
1492 /* xgettext:c-format */
1493 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1494 }
1495
1496 if (iheader->sh_info)
1497 {
1498 /* The sh_info field can hold arbitrary information, but if the
1499 SHF_LINK_INFO flag is set then it should be interpreted as a
1500 section index. */
1501 if (iheader->sh_flags & SHF_INFO_LINK)
1502 {
1503 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1504 iheader->sh_info);
1505 if (sh_link != SHN_UNDEF)
1506 oheader->sh_flags |= SHF_INFO_LINK;
1507 }
1508 else
1509 /* No idea what it means - just copy it. */
1510 sh_link = iheader->sh_info;
1511
1512 if (sh_link != SHN_UNDEF)
1513 {
1514 oheader->sh_info = sh_link;
1515 changed = true;
1516 }
1517 else
1518 _bfd_error_handler
1519 /* xgettext:c-format */
1520 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1521 }
1522
1523 return changed;
1524 }
1525
1526 /* Copy the program header and other data from one object module to
1527 another. */
1528
1529 bool
1530 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1531 {
1532 const Elf_Internal_Shdr **iheaders
1533 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1534 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1535 const struct elf_backend_data *bed;
1536 unsigned int i;
1537
1538 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1539 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1540 return true;
1541
1542 if (!elf_flags_init (obfd))
1543 {
1544 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1545 elf_flags_init (obfd) = true;
1546 }
1547
1548 elf_gp (obfd) = elf_gp (ibfd);
1549
1550 /* Also copy the EI_OSABI field. */
1551 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1552 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1553
1554 /* If set, copy the EI_ABIVERSION field. */
1555 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1556 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1557 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1558
1559 /* Copy object attributes. */
1560 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1561
1562 if (iheaders == NULL || oheaders == NULL)
1563 return true;
1564
1565 bed = get_elf_backend_data (obfd);
1566
1567 /* Possibly copy other fields in the section header. */
1568 for (i = 1; i < elf_numsections (obfd); i++)
1569 {
1570 unsigned int j;
1571 Elf_Internal_Shdr * oheader = oheaders[i];
1572
1573 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1574 because of a special case need for generating separate debug info
1575 files. See below for more details. */
1576 if (oheader == NULL
1577 || (oheader->sh_type != SHT_NOBITS
1578 && oheader->sh_type < SHT_LOOS))
1579 continue;
1580
1581 /* Ignore empty sections, and sections whose
1582 fields have already been initialised. */
1583 if (oheader->sh_size == 0
1584 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1585 continue;
1586
1587 /* Scan for the matching section in the input bfd.
1588 First we try for a direct mapping between the input and
1589 output sections. */
1590 for (j = 1; j < elf_numsections (ibfd); j++)
1591 {
1592 const Elf_Internal_Shdr * iheader = iheaders[j];
1593
1594 if (iheader == NULL)
1595 continue;
1596
1597 if (oheader->bfd_section != NULL
1598 && iheader->bfd_section != NULL
1599 && iheader->bfd_section->output_section != NULL
1600 && iheader->bfd_section->output_section == oheader->bfd_section)
1601 {
1602 /* We have found a connection from the input section to
1603 the output section. Attempt to copy the header fields.
1604 If this fails then do not try any further sections -
1605 there should only be a one-to-one mapping between
1606 input and output. */
1607 if (!copy_special_section_fields (ibfd, obfd,
1608 iheader, oheader, i))
1609 j = elf_numsections (ibfd);
1610 break;
1611 }
1612 }
1613
1614 if (j < elf_numsections (ibfd))
1615 continue;
1616
1617 /* That failed. So try to deduce the corresponding input section.
1618 Unfortunately we cannot compare names as the output string table
1619 is empty, so instead we check size, address and type. */
1620 for (j = 1; j < elf_numsections (ibfd); j++)
1621 {
1622 const Elf_Internal_Shdr * iheader = iheaders[j];
1623
1624 if (iheader == NULL)
1625 continue;
1626
1627 /* Try matching fields in the input section's header.
1628 Since --only-keep-debug turns all non-debug sections into
1629 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1630 input type. */
1631 if ((oheader->sh_type == SHT_NOBITS
1632 || iheader->sh_type == oheader->sh_type)
1633 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1634 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1635 && iheader->sh_addralign == oheader->sh_addralign
1636 && iheader->sh_entsize == oheader->sh_entsize
1637 && iheader->sh_size == oheader->sh_size
1638 && iheader->sh_addr == oheader->sh_addr
1639 && (iheader->sh_info != oheader->sh_info
1640 || iheader->sh_link != oheader->sh_link))
1641 {
1642 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1643 break;
1644 }
1645 }
1646
1647 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1648 {
1649 /* Final attempt. Call the backend copy function
1650 with a NULL input section. */
1651 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1652 NULL, oheader);
1653 }
1654 }
1655
1656 return true;
1657 }
1658
1659 static const char *
1660 get_segment_type (unsigned int p_type)
1661 {
1662 const char *pt;
1663 switch (p_type)
1664 {
1665 case PT_NULL: pt = "NULL"; break;
1666 case PT_LOAD: pt = "LOAD"; break;
1667 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1668 case PT_INTERP: pt = "INTERP"; break;
1669 case PT_NOTE: pt = "NOTE"; break;
1670 case PT_SHLIB: pt = "SHLIB"; break;
1671 case PT_PHDR: pt = "PHDR"; break;
1672 case PT_TLS: pt = "TLS"; break;
1673 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1674 case PT_GNU_STACK: pt = "STACK"; break;
1675 case PT_GNU_RELRO: pt = "RELRO"; break;
1676 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1677 default: pt = NULL; break;
1678 }
1679 return pt;
1680 }
1681
1682 /* Print out the program headers. */
1683
1684 bool
1685 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1686 {
1687 FILE *f = (FILE *) farg;
1688 Elf_Internal_Phdr *p;
1689 asection *s;
1690 bfd_byte *dynbuf = NULL;
1691
1692 p = elf_tdata (abfd)->phdr;
1693 if (p != NULL)
1694 {
1695 unsigned int i, c;
1696
1697 fprintf (f, _("\nProgram Header:\n"));
1698 c = elf_elfheader (abfd)->e_phnum;
1699 for (i = 0; i < c; i++, p++)
1700 {
1701 const char *pt = get_segment_type (p->p_type);
1702 char buf[20];
1703
1704 if (pt == NULL)
1705 {
1706 sprintf (buf, "0x%lx", p->p_type);
1707 pt = buf;
1708 }
1709 fprintf (f, "%8s off 0x", pt);
1710 bfd_fprintf_vma (abfd, f, p->p_offset);
1711 fprintf (f, " vaddr 0x");
1712 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1713 fprintf (f, " paddr 0x");
1714 bfd_fprintf_vma (abfd, f, p->p_paddr);
1715 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1716 fprintf (f, " filesz 0x");
1717 bfd_fprintf_vma (abfd, f, p->p_filesz);
1718 fprintf (f, " memsz 0x");
1719 bfd_fprintf_vma (abfd, f, p->p_memsz);
1720 fprintf (f, " flags %c%c%c",
1721 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1722 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1723 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1724 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1725 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1726 fprintf (f, "\n");
1727 }
1728 }
1729
1730 s = bfd_get_section_by_name (abfd, ".dynamic");
1731 if (s != NULL)
1732 {
1733 unsigned int elfsec;
1734 unsigned long shlink;
1735 bfd_byte *extdyn, *extdynend;
1736 size_t extdynsize;
1737 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1738
1739 fprintf (f, _("\nDynamic Section:\n"));
1740
1741 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1742 goto error_return;
1743
1744 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1745 if (elfsec == SHN_BAD)
1746 goto error_return;
1747 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1748
1749 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1750 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1751
1752 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1753 (size_t) (extdynend - extdyn) >= extdynsize;
1754 extdyn += extdynsize)
1755 {
1756 Elf_Internal_Dyn dyn;
1757 const char *name = "";
1758 char ab[20];
1759 bool stringp;
1760 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1761
1762 (*swap_dyn_in) (abfd, extdyn, &dyn);
1763
1764 if (dyn.d_tag == DT_NULL)
1765 break;
1766
1767 stringp = false;
1768 switch (dyn.d_tag)
1769 {
1770 default:
1771 if (bed->elf_backend_get_target_dtag)
1772 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1773
1774 if (!strcmp (name, ""))
1775 {
1776 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1777 name = ab;
1778 }
1779 break;
1780
1781 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1782 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1783 case DT_PLTGOT: name = "PLTGOT"; break;
1784 case DT_HASH: name = "HASH"; break;
1785 case DT_STRTAB: name = "STRTAB"; break;
1786 case DT_SYMTAB: name = "SYMTAB"; break;
1787 case DT_RELA: name = "RELA"; break;
1788 case DT_RELASZ: name = "RELASZ"; break;
1789 case DT_RELAENT: name = "RELAENT"; break;
1790 case DT_STRSZ: name = "STRSZ"; break;
1791 case DT_SYMENT: name = "SYMENT"; break;
1792 case DT_INIT: name = "INIT"; break;
1793 case DT_FINI: name = "FINI"; break;
1794 case DT_SONAME: name = "SONAME"; stringp = true; break;
1795 case DT_RPATH: name = "RPATH"; stringp = true; break;
1796 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1797 case DT_REL: name = "REL"; break;
1798 case DT_RELSZ: name = "RELSZ"; break;
1799 case DT_RELENT: name = "RELENT"; break;
1800 case DT_RELR: name = "RELR"; break;
1801 case DT_RELRSZ: name = "RELRSZ"; break;
1802 case DT_RELRENT: name = "RELRENT"; break;
1803 case DT_PLTREL: name = "PLTREL"; break;
1804 case DT_DEBUG: name = "DEBUG"; break;
1805 case DT_TEXTREL: name = "TEXTREL"; break;
1806 case DT_JMPREL: name = "JMPREL"; break;
1807 case DT_BIND_NOW: name = "BIND_NOW"; break;
1808 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1809 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1810 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1811 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1812 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1813 case DT_FLAGS: name = "FLAGS"; break;
1814 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1815 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1816 case DT_CHECKSUM: name = "CHECKSUM"; break;
1817 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1818 case DT_MOVEENT: name = "MOVEENT"; break;
1819 case DT_MOVESZ: name = "MOVESZ"; break;
1820 case DT_FEATURE: name = "FEATURE"; break;
1821 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1822 case DT_SYMINSZ: name = "SYMINSZ"; break;
1823 case DT_SYMINENT: name = "SYMINENT"; break;
1824 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1825 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1826 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1827 case DT_PLTPAD: name = "PLTPAD"; break;
1828 case DT_MOVETAB: name = "MOVETAB"; break;
1829 case DT_SYMINFO: name = "SYMINFO"; break;
1830 case DT_RELACOUNT: name = "RELACOUNT"; break;
1831 case DT_RELCOUNT: name = "RELCOUNT"; break;
1832 case DT_FLAGS_1: name = "FLAGS_1"; break;
1833 case DT_VERSYM: name = "VERSYM"; break;
1834 case DT_VERDEF: name = "VERDEF"; break;
1835 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1836 case DT_VERNEED: name = "VERNEED"; break;
1837 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1838 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1839 case DT_USED: name = "USED"; break;
1840 case DT_FILTER: name = "FILTER"; stringp = true; break;
1841 case DT_GNU_HASH: name = "GNU_HASH"; break;
1842 }
1843
1844 fprintf (f, " %-20s ", name);
1845 if (! stringp)
1846 {
1847 fprintf (f, "0x");
1848 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1849 }
1850 else
1851 {
1852 const char *string;
1853 unsigned int tagv = dyn.d_un.d_val;
1854
1855 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1856 if (string == NULL)
1857 goto error_return;
1858 fprintf (f, "%s", string);
1859 }
1860 fprintf (f, "\n");
1861 }
1862
1863 free (dynbuf);
1864 dynbuf = NULL;
1865 }
1866
1867 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1868 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1869 {
1870 if (! _bfd_elf_slurp_version_tables (abfd, false))
1871 return false;
1872 }
1873
1874 if (elf_dynverdef (abfd) != 0)
1875 {
1876 Elf_Internal_Verdef *t;
1877
1878 fprintf (f, _("\nVersion definitions:\n"));
1879 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1880 {
1881 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1882 t->vd_flags, t->vd_hash,
1883 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1884 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1885 {
1886 Elf_Internal_Verdaux *a;
1887
1888 fprintf (f, "\t");
1889 for (a = t->vd_auxptr->vda_nextptr;
1890 a != NULL;
1891 a = a->vda_nextptr)
1892 fprintf (f, "%s ",
1893 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1894 fprintf (f, "\n");
1895 }
1896 }
1897 }
1898
1899 if (elf_dynverref (abfd) != 0)
1900 {
1901 Elf_Internal_Verneed *t;
1902
1903 fprintf (f, _("\nVersion References:\n"));
1904 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1905 {
1906 Elf_Internal_Vernaux *a;
1907
1908 fprintf (f, _(" required from %s:\n"),
1909 t->vn_filename ? t->vn_filename : "<corrupt>");
1910 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1911 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1912 a->vna_flags, a->vna_other,
1913 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1914 }
1915 }
1916
1917 return true;
1918
1919 error_return:
1920 free (dynbuf);
1921 return false;
1922 }
1923
1924 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1925 and return symbol version for symbol version itself. */
1926
1927 const char *
1928 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1929 bool base_p,
1930 bool *hidden)
1931 {
1932 const char *version_string = NULL;
1933 if (elf_dynversym (abfd) != 0
1934 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1935 {
1936 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1937
1938 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1939 vernum &= VERSYM_VERSION;
1940
1941 if (vernum == 0)
1942 version_string = "";
1943 else if (vernum == 1
1944 && (vernum > elf_tdata (abfd)->cverdefs
1945 || (elf_tdata (abfd)->verdef[0].vd_flags
1946 == VER_FLG_BASE)))
1947 version_string = base_p ? "Base" : "";
1948 else if (vernum <= elf_tdata (abfd)->cverdefs)
1949 {
1950 const char *nodename
1951 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1952 version_string = "";
1953 if (base_p
1954 || nodename == NULL
1955 || symbol->name == NULL
1956 || strcmp (symbol->name, nodename) != 0)
1957 version_string = nodename;
1958 }
1959 else
1960 {
1961 Elf_Internal_Verneed *t;
1962
1963 version_string = _("<corrupt>");
1964 for (t = elf_tdata (abfd)->verref;
1965 t != NULL;
1966 t = t->vn_nextref)
1967 {
1968 Elf_Internal_Vernaux *a;
1969
1970 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1971 {
1972 if (a->vna_other == vernum)
1973 {
1974 *hidden = true;
1975 version_string = a->vna_nodename;
1976 break;
1977 }
1978 }
1979 }
1980 }
1981 }
1982 return version_string;
1983 }
1984
1985 /* Display ELF-specific fields of a symbol. */
1986
1987 void
1988 bfd_elf_print_symbol (bfd *abfd,
1989 void *filep,
1990 asymbol *symbol,
1991 bfd_print_symbol_type how)
1992 {
1993 FILE *file = (FILE *) filep;
1994 switch (how)
1995 {
1996 case bfd_print_symbol_name:
1997 fprintf (file, "%s", symbol->name);
1998 break;
1999 case bfd_print_symbol_more:
2000 fprintf (file, "elf ");
2001 bfd_fprintf_vma (abfd, file, symbol->value);
2002 fprintf (file, " %x", symbol->flags);
2003 break;
2004 case bfd_print_symbol_all:
2005 {
2006 const char *section_name;
2007 const char *name = NULL;
2008 const struct elf_backend_data *bed;
2009 unsigned char st_other;
2010 bfd_vma val;
2011 const char *version_string;
2012 bool hidden;
2013
2014 section_name = symbol->section ? symbol->section->name : "(*none*)";
2015
2016 bed = get_elf_backend_data (abfd);
2017 if (bed->elf_backend_print_symbol_all)
2018 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2019
2020 if (name == NULL)
2021 {
2022 name = symbol->name;
2023 bfd_print_symbol_vandf (abfd, file, symbol);
2024 }
2025
2026 fprintf (file, " %s\t", section_name);
2027 /* Print the "other" value for a symbol. For common symbols,
2028 we've already printed the size; now print the alignment.
2029 For other symbols, we have no specified alignment, and
2030 we've printed the address; now print the size. */
2031 if (symbol->section && bfd_is_com_section (symbol->section))
2032 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2033 else
2034 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2035 bfd_fprintf_vma (abfd, file, val);
2036
2037 /* If we have version information, print it. */
2038 version_string = _bfd_elf_get_symbol_version_string (abfd,
2039 symbol,
2040 true,
2041 &hidden);
2042 if (version_string)
2043 {
2044 if (!hidden)
2045 fprintf (file, " %-11s", version_string);
2046 else
2047 {
2048 int i;
2049
2050 fprintf (file, " (%s)", version_string);
2051 for (i = 10 - strlen (version_string); i > 0; --i)
2052 putc (' ', file);
2053 }
2054 }
2055
2056 /* If the st_other field is not zero, print it. */
2057 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2058
2059 switch (st_other)
2060 {
2061 case 0: break;
2062 case STV_INTERNAL: fprintf (file, " .internal"); break;
2063 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2064 case STV_PROTECTED: fprintf (file, " .protected"); break;
2065 default:
2066 /* Some other non-defined flags are also present, so print
2067 everything hex. */
2068 fprintf (file, " 0x%02x", (unsigned int) st_other);
2069 }
2070
2071 fprintf (file, " %s", name);
2072 }
2073 break;
2074 }
2075 }
2076 \f
2077 /* ELF .o/exec file reading */
2078
2079 /* Create a new bfd section from an ELF section header. */
2080
2081 bool
2082 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2083 {
2084 Elf_Internal_Shdr *hdr;
2085 Elf_Internal_Ehdr *ehdr;
2086 const struct elf_backend_data *bed;
2087 const char *name;
2088 bool ret = true;
2089
2090 if (shindex >= elf_numsections (abfd))
2091 return false;
2092
2093 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2094 sh_link or sh_info. Detect this here, by refusing to load a
2095 section that we are already in the process of loading. */
2096 if (elf_tdata (abfd)->being_created[shindex])
2097 {
2098 _bfd_error_handler
2099 (_("%pB: warning: loop in section dependencies detected"), abfd);
2100 return false;
2101 }
2102 elf_tdata (abfd)->being_created[shindex] = true;
2103
2104 hdr = elf_elfsections (abfd)[shindex];
2105 ehdr = elf_elfheader (abfd);
2106 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2107 hdr->sh_name);
2108 if (name == NULL)
2109 goto fail;
2110
2111 bed = get_elf_backend_data (abfd);
2112 switch (hdr->sh_type)
2113 {
2114 case SHT_NULL:
2115 /* Inactive section. Throw it away. */
2116 goto success;
2117
2118 case SHT_PROGBITS: /* Normal section with contents. */
2119 case SHT_NOBITS: /* .bss section. */
2120 case SHT_HASH: /* .hash section. */
2121 case SHT_NOTE: /* .note section. */
2122 case SHT_INIT_ARRAY: /* .init_array section. */
2123 case SHT_FINI_ARRAY: /* .fini_array section. */
2124 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2125 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2126 case SHT_GNU_HASH: /* .gnu.hash section. */
2127 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2128 goto success;
2129
2130 case SHT_DYNAMIC: /* Dynamic linking information. */
2131 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2132 goto fail;
2133
2134 if (hdr->sh_link > elf_numsections (abfd))
2135 {
2136 /* PR 10478: Accept Solaris binaries with a sh_link field
2137 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2138 switch (bfd_get_arch (abfd))
2139 {
2140 case bfd_arch_i386:
2141 case bfd_arch_sparc:
2142 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2143 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2144 break;
2145 /* Otherwise fall through. */
2146 default:
2147 goto fail;
2148 }
2149 }
2150 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2151 goto fail;
2152 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2153 {
2154 Elf_Internal_Shdr *dynsymhdr;
2155
2156 /* The shared libraries distributed with hpux11 have a bogus
2157 sh_link field for the ".dynamic" section. Find the
2158 string table for the ".dynsym" section instead. */
2159 if (elf_dynsymtab (abfd) != 0)
2160 {
2161 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2162 hdr->sh_link = dynsymhdr->sh_link;
2163 }
2164 else
2165 {
2166 unsigned int i, num_sec;
2167
2168 num_sec = elf_numsections (abfd);
2169 for (i = 1; i < num_sec; i++)
2170 {
2171 dynsymhdr = elf_elfsections (abfd)[i];
2172 if (dynsymhdr->sh_type == SHT_DYNSYM)
2173 {
2174 hdr->sh_link = dynsymhdr->sh_link;
2175 break;
2176 }
2177 }
2178 }
2179 }
2180 goto success;
2181
2182 case SHT_SYMTAB: /* A symbol table. */
2183 if (elf_onesymtab (abfd) == shindex)
2184 goto success;
2185
2186 if (hdr->sh_entsize != bed->s->sizeof_sym)
2187 goto fail;
2188
2189 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2190 {
2191 if (hdr->sh_size != 0)
2192 goto fail;
2193 /* Some assemblers erroneously set sh_info to one with a
2194 zero sh_size. ld sees this as a global symbol count
2195 of (unsigned) -1. Fix it here. */
2196 hdr->sh_info = 0;
2197 goto success;
2198 }
2199
2200 /* PR 18854: A binary might contain more than one symbol table.
2201 Unusual, but possible. Warn, but continue. */
2202 if (elf_onesymtab (abfd) != 0)
2203 {
2204 _bfd_error_handler
2205 /* xgettext:c-format */
2206 (_("%pB: warning: multiple symbol tables detected"
2207 " - ignoring the table in section %u"),
2208 abfd, shindex);
2209 goto success;
2210 }
2211 elf_onesymtab (abfd) = shindex;
2212 elf_symtab_hdr (abfd) = *hdr;
2213 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2214 abfd->flags |= HAS_SYMS;
2215
2216 /* Sometimes a shared object will map in the symbol table. If
2217 SHF_ALLOC is set, and this is a shared object, then we also
2218 treat this section as a BFD section. We can not base the
2219 decision purely on SHF_ALLOC, because that flag is sometimes
2220 set in a relocatable object file, which would confuse the
2221 linker. */
2222 if ((hdr->sh_flags & SHF_ALLOC) != 0
2223 && (abfd->flags & DYNAMIC) != 0
2224 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2225 shindex))
2226 goto fail;
2227
2228 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2229 can't read symbols without that section loaded as well. It
2230 is most likely specified by the next section header. */
2231 {
2232 elf_section_list * entry;
2233 unsigned int i, num_sec;
2234
2235 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2236 if (entry->hdr.sh_link == shindex)
2237 goto success;
2238
2239 num_sec = elf_numsections (abfd);
2240 for (i = shindex + 1; i < num_sec; i++)
2241 {
2242 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2243
2244 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2245 && hdr2->sh_link == shindex)
2246 break;
2247 }
2248
2249 if (i == num_sec)
2250 for (i = 1; i < shindex; i++)
2251 {
2252 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2253
2254 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2255 && hdr2->sh_link == shindex)
2256 break;
2257 }
2258
2259 if (i != shindex)
2260 ret = bfd_section_from_shdr (abfd, i);
2261 /* else FIXME: we have failed to find the symbol table.
2262 Should we issue an error? */
2263 goto success;
2264 }
2265
2266 case SHT_DYNSYM: /* A dynamic symbol table. */
2267 if (elf_dynsymtab (abfd) == shindex)
2268 goto success;
2269
2270 if (hdr->sh_entsize != bed->s->sizeof_sym)
2271 goto fail;
2272
2273 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2274 {
2275 if (hdr->sh_size != 0)
2276 goto fail;
2277
2278 /* Some linkers erroneously set sh_info to one with a
2279 zero sh_size. ld sees this as a global symbol count
2280 of (unsigned) -1. Fix it here. */
2281 hdr->sh_info = 0;
2282 goto success;
2283 }
2284
2285 /* PR 18854: A binary might contain more than one dynamic symbol table.
2286 Unusual, but possible. Warn, but continue. */
2287 if (elf_dynsymtab (abfd) != 0)
2288 {
2289 _bfd_error_handler
2290 /* xgettext:c-format */
2291 (_("%pB: warning: multiple dynamic symbol tables detected"
2292 " - ignoring the table in section %u"),
2293 abfd, shindex);
2294 goto success;
2295 }
2296 elf_dynsymtab (abfd) = shindex;
2297 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2298 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2299 abfd->flags |= HAS_SYMS;
2300
2301 /* Besides being a symbol table, we also treat this as a regular
2302 section, so that objcopy can handle it. */
2303 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2304 goto success;
2305
2306 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2307 {
2308 elf_section_list * entry;
2309
2310 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2311 if (entry->ndx == shindex)
2312 goto success;
2313
2314 entry = bfd_alloc (abfd, sizeof (*entry));
2315 if (entry == NULL)
2316 goto fail;
2317 entry->ndx = shindex;
2318 entry->hdr = * hdr;
2319 entry->next = elf_symtab_shndx_list (abfd);
2320 elf_symtab_shndx_list (abfd) = entry;
2321 elf_elfsections (abfd)[shindex] = & entry->hdr;
2322 goto success;
2323 }
2324
2325 case SHT_STRTAB: /* A string table. */
2326 if (hdr->bfd_section != NULL)
2327 goto success;
2328
2329 if (ehdr->e_shstrndx == shindex)
2330 {
2331 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2332 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2333 goto success;
2334 }
2335
2336 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2337 {
2338 symtab_strtab:
2339 elf_tdata (abfd)->strtab_hdr = *hdr;
2340 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2341 goto success;
2342 }
2343
2344 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2345 {
2346 dynsymtab_strtab:
2347 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2348 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2349 elf_elfsections (abfd)[shindex] = hdr;
2350 /* We also treat this as a regular section, so that objcopy
2351 can handle it. */
2352 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2353 shindex);
2354 goto success;
2355 }
2356
2357 /* If the string table isn't one of the above, then treat it as a
2358 regular section. We need to scan all the headers to be sure,
2359 just in case this strtab section appeared before the above. */
2360 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2361 {
2362 unsigned int i, num_sec;
2363
2364 num_sec = elf_numsections (abfd);
2365 for (i = 1; i < num_sec; i++)
2366 {
2367 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2368 if (hdr2->sh_link == shindex)
2369 {
2370 /* Prevent endless recursion on broken objects. */
2371 if (i == shindex)
2372 goto fail;
2373 if (! bfd_section_from_shdr (abfd, i))
2374 goto fail;
2375 if (elf_onesymtab (abfd) == i)
2376 goto symtab_strtab;
2377 if (elf_dynsymtab (abfd) == i)
2378 goto dynsymtab_strtab;
2379 }
2380 }
2381 }
2382 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2383 goto success;
2384
2385 case SHT_REL:
2386 case SHT_RELA:
2387 case SHT_RELR:
2388 /* *These* do a lot of work -- but build no sections! */
2389 {
2390 asection *target_sect;
2391 Elf_Internal_Shdr *hdr2, **p_hdr;
2392 unsigned int num_sec = elf_numsections (abfd);
2393 struct bfd_elf_section_data *esdt;
2394 bfd_size_type size;
2395
2396 if (hdr->sh_type == SHT_REL)
2397 size = bed->s->sizeof_rel;
2398 else if (hdr->sh_type == SHT_RELA)
2399 size = bed->s->sizeof_rela;
2400 else
2401 size = bed->s->arch_size / 8;
2402 if (hdr->sh_entsize != size)
2403 goto fail;
2404
2405 /* Check for a bogus link to avoid crashing. */
2406 if (hdr->sh_link >= num_sec)
2407 {
2408 _bfd_error_handler
2409 /* xgettext:c-format */
2410 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2411 abfd, hdr->sh_link, name, shindex);
2412 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2413 goto success;
2414 }
2415
2416 /* Get the symbol table. */
2417 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2418 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2419 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2420 goto fail;
2421
2422 /* If this is an alloc section in an executable or shared
2423 library, or the reloc section does not use the main symbol
2424 table we don't treat it as a reloc section. BFD can't
2425 adequately represent such a section, so at least for now,
2426 we don't try. We just present it as a normal section. We
2427 also can't use it as a reloc section if it points to the
2428 null section, an invalid section, another reloc section, or
2429 its sh_link points to the null section. */
2430 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2431 && (hdr->sh_flags & SHF_ALLOC) != 0)
2432 || hdr->sh_type == SHT_RELR
2433 || hdr->sh_link == SHN_UNDEF
2434 || hdr->sh_link != elf_onesymtab (abfd)
2435 || hdr->sh_info == SHN_UNDEF
2436 || hdr->sh_info >= num_sec
2437 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2438 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2439 {
2440 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2441 goto success;
2442 }
2443
2444 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2445 goto fail;
2446
2447 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2448 if (target_sect == NULL)
2449 goto fail;
2450
2451 esdt = elf_section_data (target_sect);
2452 if (hdr->sh_type == SHT_RELA)
2453 p_hdr = &esdt->rela.hdr;
2454 else
2455 p_hdr = &esdt->rel.hdr;
2456
2457 /* PR 17512: file: 0b4f81b7.
2458 Also see PR 24456, for a file which deliberately has two reloc
2459 sections. */
2460 if (*p_hdr != NULL)
2461 {
2462 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2463 {
2464 _bfd_error_handler
2465 /* xgettext:c-format */
2466 (_("%pB: warning: secondary relocation section '%s' "
2467 "for section %pA found - ignoring"),
2468 abfd, name, target_sect);
2469 }
2470 else
2471 esdt->has_secondary_relocs = true;
2472 goto success;
2473 }
2474
2475 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2476 if (hdr2 == NULL)
2477 goto fail;
2478 *hdr2 = *hdr;
2479 *p_hdr = hdr2;
2480 elf_elfsections (abfd)[shindex] = hdr2;
2481 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2482 * bed->s->int_rels_per_ext_rel);
2483 target_sect->flags |= SEC_RELOC;
2484 target_sect->relocation = NULL;
2485 target_sect->rel_filepos = hdr->sh_offset;
2486 /* In the section to which the relocations apply, mark whether
2487 its relocations are of the REL or RELA variety. */
2488 if (hdr->sh_size != 0)
2489 {
2490 if (hdr->sh_type == SHT_RELA)
2491 target_sect->use_rela_p = 1;
2492 }
2493 abfd->flags |= HAS_RELOC;
2494 goto success;
2495 }
2496
2497 case SHT_GNU_verdef:
2498 if (hdr->sh_info != 0)
2499 elf_dynverdef (abfd) = shindex;
2500 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2501 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2502 goto success;
2503
2504 case SHT_GNU_versym:
2505 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2506 goto fail;
2507
2508 elf_dynversym (abfd) = shindex;
2509 elf_tdata (abfd)->dynversym_hdr = *hdr;
2510 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2511 goto success;
2512
2513 case SHT_GNU_verneed:
2514 if (hdr->sh_info != 0)
2515 elf_dynverref (abfd) = shindex;
2516 elf_tdata (abfd)->dynverref_hdr = *hdr;
2517 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2518 goto success;
2519
2520 case SHT_SHLIB:
2521 goto success;
2522
2523 case SHT_GROUP:
2524 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2525 goto fail;
2526
2527 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2528 goto fail;
2529
2530 goto success;
2531
2532 default:
2533 /* Possibly an attributes section. */
2534 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2535 || hdr->sh_type == bed->obj_attrs_section_type)
2536 {
2537 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2538 goto fail;
2539 _bfd_elf_parse_attributes (abfd, hdr);
2540 goto success;
2541 }
2542
2543 /* Check for any processor-specific section types. */
2544 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2545 goto success;
2546
2547 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2548 {
2549 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2550 /* FIXME: How to properly handle allocated section reserved
2551 for applications? */
2552 _bfd_error_handler
2553 /* xgettext:c-format */
2554 (_("%pB: unknown type [%#x] section `%s'"),
2555 abfd, hdr->sh_type, name);
2556 else
2557 {
2558 /* Allow sections reserved for applications. */
2559 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2560 goto success;
2561 }
2562 }
2563 else if (hdr->sh_type >= SHT_LOPROC
2564 && hdr->sh_type <= SHT_HIPROC)
2565 /* FIXME: We should handle this section. */
2566 _bfd_error_handler
2567 /* xgettext:c-format */
2568 (_("%pB: unknown type [%#x] section `%s'"),
2569 abfd, hdr->sh_type, name);
2570 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2571 {
2572 /* Unrecognised OS-specific sections. */
2573 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2574 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2575 required to correctly process the section and the file should
2576 be rejected with an error message. */
2577 _bfd_error_handler
2578 /* xgettext:c-format */
2579 (_("%pB: unknown type [%#x] section `%s'"),
2580 abfd, hdr->sh_type, name);
2581 else
2582 {
2583 /* Otherwise it should be processed. */
2584 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2585 goto success;
2586 }
2587 }
2588 else
2589 /* FIXME: We should handle this section. */
2590 _bfd_error_handler
2591 /* xgettext:c-format */
2592 (_("%pB: unknown type [%#x] section `%s'"),
2593 abfd, hdr->sh_type, name);
2594
2595 goto fail;
2596 }
2597
2598 fail:
2599 ret = false;
2600 success:
2601 elf_tdata (abfd)->being_created[shindex] = false;
2602 return ret;
2603 }
2604
2605 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2606
2607 Elf_Internal_Sym *
2608 bfd_sym_from_r_symndx (struct sym_cache *cache,
2609 bfd *abfd,
2610 unsigned long r_symndx)
2611 {
2612 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2613
2614 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2615 {
2616 Elf_Internal_Shdr *symtab_hdr;
2617 unsigned char esym[sizeof (Elf64_External_Sym)];
2618 Elf_External_Sym_Shndx eshndx;
2619
2620 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2621 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2622 &cache->sym[ent], esym, &eshndx) == NULL)
2623 return NULL;
2624
2625 if (cache->abfd != abfd)
2626 {
2627 memset (cache->indx, -1, sizeof (cache->indx));
2628 cache->abfd = abfd;
2629 }
2630 cache->indx[ent] = r_symndx;
2631 }
2632
2633 return &cache->sym[ent];
2634 }
2635
2636 /* Given an ELF section number, retrieve the corresponding BFD
2637 section. */
2638
2639 asection *
2640 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2641 {
2642 if (sec_index >= elf_numsections (abfd))
2643 return NULL;
2644 return elf_elfsections (abfd)[sec_index]->bfd_section;
2645 }
2646
2647 static const struct bfd_elf_special_section special_sections_b[] =
2648 {
2649 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2650 { NULL, 0, 0, 0, 0 }
2651 };
2652
2653 static const struct bfd_elf_special_section special_sections_c[] =
2654 {
2655 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2656 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2657 { NULL, 0, 0, 0, 0 }
2658 };
2659
2660 static const struct bfd_elf_special_section special_sections_d[] =
2661 {
2662 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2663 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2664 /* There are more DWARF sections than these, but they needn't be added here
2665 unless you have to cope with broken compilers that don't emit section
2666 attributes or you want to help the user writing assembler. */
2667 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2668 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2669 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2670 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2671 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2672 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2673 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2674 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2675 { NULL, 0, 0, 0, 0 }
2676 };
2677
2678 static const struct bfd_elf_special_section special_sections_f[] =
2679 {
2680 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2681 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2682 { NULL, 0 , 0, 0, 0 }
2683 };
2684
2685 static const struct bfd_elf_special_section special_sections_g[] =
2686 {
2687 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2688 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2689 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2690 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2691 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2692 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2693 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2694 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2695 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2696 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2697 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2698 { NULL, 0, 0, 0, 0 }
2699 };
2700
2701 static const struct bfd_elf_special_section special_sections_h[] =
2702 {
2703 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2704 { NULL, 0, 0, 0, 0 }
2705 };
2706
2707 static const struct bfd_elf_special_section special_sections_i[] =
2708 {
2709 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2710 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2711 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2712 { NULL, 0, 0, 0, 0 }
2713 };
2714
2715 static const struct bfd_elf_special_section special_sections_l[] =
2716 {
2717 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2718 { NULL, 0, 0, 0, 0 }
2719 };
2720
2721 static const struct bfd_elf_special_section special_sections_n[] =
2722 {
2723 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2724 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2725 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2726 { NULL, 0, 0, 0, 0 }
2727 };
2728
2729 static const struct bfd_elf_special_section special_sections_p[] =
2730 {
2731 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2732 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2733 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2734 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2735 { NULL, 0, 0, 0, 0 }
2736 };
2737
2738 static const struct bfd_elf_special_section special_sections_r[] =
2739 {
2740 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2741 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2742 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2743 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2744 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2745 { NULL, 0, 0, 0, 0 }
2746 };
2747
2748 static const struct bfd_elf_special_section special_sections_s[] =
2749 {
2750 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2751 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2752 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2753 /* See struct bfd_elf_special_section declaration for the semantics of
2754 this special case where .prefix_length != strlen (.prefix). */
2755 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2756 { NULL, 0, 0, 0, 0 }
2757 };
2758
2759 static const struct bfd_elf_special_section special_sections_t[] =
2760 {
2761 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2762 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2763 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2764 { NULL, 0, 0, 0, 0 }
2765 };
2766
2767 static const struct bfd_elf_special_section special_sections_z[] =
2768 {
2769 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2770 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2771 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2772 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2773 { NULL, 0, 0, 0, 0 }
2774 };
2775
2776 static const struct bfd_elf_special_section * const special_sections[] =
2777 {
2778 special_sections_b, /* 'b' */
2779 special_sections_c, /* 'c' */
2780 special_sections_d, /* 'd' */
2781 NULL, /* 'e' */
2782 special_sections_f, /* 'f' */
2783 special_sections_g, /* 'g' */
2784 special_sections_h, /* 'h' */
2785 special_sections_i, /* 'i' */
2786 NULL, /* 'j' */
2787 NULL, /* 'k' */
2788 special_sections_l, /* 'l' */
2789 NULL, /* 'm' */
2790 special_sections_n, /* 'n' */
2791 NULL, /* 'o' */
2792 special_sections_p, /* 'p' */
2793 NULL, /* 'q' */
2794 special_sections_r, /* 'r' */
2795 special_sections_s, /* 's' */
2796 special_sections_t, /* 't' */
2797 NULL, /* 'u' */
2798 NULL, /* 'v' */
2799 NULL, /* 'w' */
2800 NULL, /* 'x' */
2801 NULL, /* 'y' */
2802 special_sections_z /* 'z' */
2803 };
2804
2805 const struct bfd_elf_special_section *
2806 _bfd_elf_get_special_section (const char *name,
2807 const struct bfd_elf_special_section *spec,
2808 unsigned int rela)
2809 {
2810 int i;
2811 int len;
2812
2813 len = strlen (name);
2814
2815 for (i = 0; spec[i].prefix != NULL; i++)
2816 {
2817 int suffix_len;
2818 int prefix_len = spec[i].prefix_length;
2819
2820 if (len < prefix_len)
2821 continue;
2822 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2823 continue;
2824
2825 suffix_len = spec[i].suffix_length;
2826 if (suffix_len <= 0)
2827 {
2828 if (name[prefix_len] != 0)
2829 {
2830 if (suffix_len == 0)
2831 continue;
2832 if (name[prefix_len] != '.'
2833 && (suffix_len == -2
2834 || (rela && spec[i].type == SHT_REL)))
2835 continue;
2836 }
2837 }
2838 else
2839 {
2840 if (len < prefix_len + suffix_len)
2841 continue;
2842 if (memcmp (name + len - suffix_len,
2843 spec[i].prefix + prefix_len,
2844 suffix_len) != 0)
2845 continue;
2846 }
2847 return &spec[i];
2848 }
2849
2850 return NULL;
2851 }
2852
2853 const struct bfd_elf_special_section *
2854 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2855 {
2856 int i;
2857 const struct bfd_elf_special_section *spec;
2858 const struct elf_backend_data *bed;
2859
2860 /* See if this is one of the special sections. */
2861 if (sec->name == NULL)
2862 return NULL;
2863
2864 bed = get_elf_backend_data (abfd);
2865 spec = bed->special_sections;
2866 if (spec)
2867 {
2868 spec = _bfd_elf_get_special_section (sec->name,
2869 bed->special_sections,
2870 sec->use_rela_p);
2871 if (spec != NULL)
2872 return spec;
2873 }
2874
2875 if (sec->name[0] != '.')
2876 return NULL;
2877
2878 i = sec->name[1] - 'b';
2879 if (i < 0 || i > 'z' - 'b')
2880 return NULL;
2881
2882 spec = special_sections[i];
2883
2884 if (spec == NULL)
2885 return NULL;
2886
2887 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2888 }
2889
2890 bool
2891 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2892 {
2893 struct bfd_elf_section_data *sdata;
2894 const struct elf_backend_data *bed;
2895 const struct bfd_elf_special_section *ssect;
2896
2897 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2898 if (sdata == NULL)
2899 {
2900 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2901 sizeof (*sdata));
2902 if (sdata == NULL)
2903 return false;
2904 sec->used_by_bfd = sdata;
2905 }
2906
2907 /* Indicate whether or not this section should use RELA relocations. */
2908 bed = get_elf_backend_data (abfd);
2909 sec->use_rela_p = bed->default_use_rela_p;
2910
2911 /* Set up ELF section type and flags for newly created sections, if
2912 there is an ABI mandated section. */
2913 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2914 if (ssect != NULL)
2915 {
2916 elf_section_type (sec) = ssect->type;
2917 elf_section_flags (sec) = ssect->attr;
2918 }
2919
2920 return _bfd_generic_new_section_hook (abfd, sec);
2921 }
2922
2923 /* Create a new bfd section from an ELF program header.
2924
2925 Since program segments have no names, we generate a synthetic name
2926 of the form segment<NUM>, where NUM is generally the index in the
2927 program header table. For segments that are split (see below) we
2928 generate the names segment<NUM>a and segment<NUM>b.
2929
2930 Note that some program segments may have a file size that is different than
2931 (less than) the memory size. All this means is that at execution the
2932 system must allocate the amount of memory specified by the memory size,
2933 but only initialize it with the first "file size" bytes read from the
2934 file. This would occur for example, with program segments consisting
2935 of combined data+bss.
2936
2937 To handle the above situation, this routine generates TWO bfd sections
2938 for the single program segment. The first has the length specified by
2939 the file size of the segment, and the second has the length specified
2940 by the difference between the two sizes. In effect, the segment is split
2941 into its initialized and uninitialized parts. */
2942
2943 bool
2944 _bfd_elf_make_section_from_phdr (bfd *abfd,
2945 Elf_Internal_Phdr *hdr,
2946 int hdr_index,
2947 const char *type_name)
2948 {
2949 asection *newsect;
2950 char *name;
2951 char namebuf[64];
2952 size_t len;
2953 int split;
2954 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2955
2956 split = ((hdr->p_memsz > 0)
2957 && (hdr->p_filesz > 0)
2958 && (hdr->p_memsz > hdr->p_filesz));
2959
2960 if (hdr->p_filesz > 0)
2961 {
2962 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2963 len = strlen (namebuf) + 1;
2964 name = (char *) bfd_alloc (abfd, len);
2965 if (!name)
2966 return false;
2967 memcpy (name, namebuf, len);
2968 newsect = bfd_make_section (abfd, name);
2969 if (newsect == NULL)
2970 return false;
2971 newsect->vma = hdr->p_vaddr / opb;
2972 newsect->lma = hdr->p_paddr / opb;
2973 newsect->size = hdr->p_filesz;
2974 newsect->filepos = hdr->p_offset;
2975 newsect->flags |= SEC_HAS_CONTENTS;
2976 newsect->alignment_power = bfd_log2 (hdr->p_align);
2977 if (hdr->p_type == PT_LOAD)
2978 {
2979 newsect->flags |= SEC_ALLOC;
2980 newsect->flags |= SEC_LOAD;
2981 if (hdr->p_flags & PF_X)
2982 {
2983 /* FIXME: all we known is that it has execute PERMISSION,
2984 may be data. */
2985 newsect->flags |= SEC_CODE;
2986 }
2987 }
2988 if (!(hdr->p_flags & PF_W))
2989 {
2990 newsect->flags |= SEC_READONLY;
2991 }
2992 }
2993
2994 if (hdr->p_memsz > hdr->p_filesz)
2995 {
2996 bfd_vma align;
2997
2998 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2999 len = strlen (namebuf) + 1;
3000 name = (char *) bfd_alloc (abfd, len);
3001 if (!name)
3002 return false;
3003 memcpy (name, namebuf, len);
3004 newsect = bfd_make_section (abfd, name);
3005 if (newsect == NULL)
3006 return false;
3007 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3008 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3009 newsect->size = hdr->p_memsz - hdr->p_filesz;
3010 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3011 align = newsect->vma & -newsect->vma;
3012 if (align == 0 || align > hdr->p_align)
3013 align = hdr->p_align;
3014 newsect->alignment_power = bfd_log2 (align);
3015 if (hdr->p_type == PT_LOAD)
3016 {
3017 newsect->flags |= SEC_ALLOC;
3018 if (hdr->p_flags & PF_X)
3019 newsect->flags |= SEC_CODE;
3020 }
3021 if (!(hdr->p_flags & PF_W))
3022 newsect->flags |= SEC_READONLY;
3023 }
3024
3025 return true;
3026 }
3027
3028 static bool
3029 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3030 {
3031 /* The return value is ignored. Build-ids are considered optional. */
3032 if (templ->xvec->flavour == bfd_target_elf_flavour)
3033 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3034 (templ, offset);
3035 return false;
3036 }
3037
3038 bool
3039 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3040 {
3041 const struct elf_backend_data *bed;
3042
3043 switch (hdr->p_type)
3044 {
3045 case PT_NULL:
3046 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3047
3048 case PT_LOAD:
3049 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3050 return false;
3051 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3052 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3053 return true;
3054
3055 case PT_DYNAMIC:
3056 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3057
3058 case PT_INTERP:
3059 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3060
3061 case PT_NOTE:
3062 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3063 return false;
3064 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3065 hdr->p_align))
3066 return false;
3067 return true;
3068
3069 case PT_SHLIB:
3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3071
3072 case PT_PHDR:
3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3074
3075 case PT_GNU_EH_FRAME:
3076 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3077 "eh_frame_hdr");
3078
3079 case PT_GNU_STACK:
3080 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3081
3082 case PT_GNU_RELRO:
3083 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3084
3085 case PT_GNU_SFRAME:
3086 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3087 "sframe");
3088
3089 default:
3090 /* Check for any processor-specific program segment types. */
3091 bed = get_elf_backend_data (abfd);
3092 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3093 }
3094 }
3095
3096 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3097 REL or RELA. */
3098
3099 Elf_Internal_Shdr *
3100 _bfd_elf_single_rel_hdr (asection *sec)
3101 {
3102 if (elf_section_data (sec)->rel.hdr)
3103 {
3104 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3105 return elf_section_data (sec)->rel.hdr;
3106 }
3107 else
3108 return elf_section_data (sec)->rela.hdr;
3109 }
3110
3111 static bool
3112 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3113 Elf_Internal_Shdr *rel_hdr,
3114 const char *sec_name,
3115 bool use_rela_p)
3116 {
3117 char *name = (char *) bfd_alloc (abfd,
3118 sizeof ".rela" + strlen (sec_name));
3119 if (name == NULL)
3120 return false;
3121
3122 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3123 rel_hdr->sh_name =
3124 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3125 false);
3126 if (rel_hdr->sh_name == (unsigned int) -1)
3127 return false;
3128
3129 return true;
3130 }
3131
3132 /* Allocate and initialize a section-header for a new reloc section,
3133 containing relocations against ASECT. It is stored in RELDATA. If
3134 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3135 relocations. */
3136
3137 static bool
3138 _bfd_elf_init_reloc_shdr (bfd *abfd,
3139 struct bfd_elf_section_reloc_data *reldata,
3140 const char *sec_name,
3141 bool use_rela_p,
3142 bool delay_st_name_p)
3143 {
3144 Elf_Internal_Shdr *rel_hdr;
3145 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3146
3147 BFD_ASSERT (reldata->hdr == NULL);
3148 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3149 reldata->hdr = rel_hdr;
3150
3151 if (delay_st_name_p)
3152 rel_hdr->sh_name = (unsigned int) -1;
3153 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3154 use_rela_p))
3155 return false;
3156 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3157 rel_hdr->sh_entsize = (use_rela_p
3158 ? bed->s->sizeof_rela
3159 : bed->s->sizeof_rel);
3160 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3161 rel_hdr->sh_flags = 0;
3162 rel_hdr->sh_addr = 0;
3163 rel_hdr->sh_size = 0;
3164 rel_hdr->sh_offset = 0;
3165
3166 return true;
3167 }
3168
3169 /* Return the default section type based on the passed in section flags. */
3170
3171 int
3172 bfd_elf_get_default_section_type (flagword flags)
3173 {
3174 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3175 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3176 return SHT_NOBITS;
3177 return SHT_PROGBITS;
3178 }
3179
3180 struct fake_section_arg
3181 {
3182 struct bfd_link_info *link_info;
3183 bool failed;
3184 };
3185
3186 /* Set up an ELF internal section header for a section. */
3187
3188 static void
3189 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3190 {
3191 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3192 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3193 struct bfd_elf_section_data *esd = elf_section_data (asect);
3194 Elf_Internal_Shdr *this_hdr;
3195 unsigned int sh_type;
3196 const char *name = asect->name;
3197 bool delay_st_name_p = false;
3198 bfd_vma mask;
3199
3200 if (arg->failed)
3201 {
3202 /* We already failed; just get out of the bfd_map_over_sections
3203 loop. */
3204 return;
3205 }
3206
3207 this_hdr = &esd->this_hdr;
3208
3209 if (arg->link_info)
3210 {
3211 /* ld: compress DWARF debug sections with names: .debug_*. */
3212 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3213 && (asect->flags & SEC_DEBUGGING)
3214 && name[1] == 'd'
3215 && name[6] == '_')
3216 {
3217 /* Set SEC_ELF_COMPRESS to indicate this section should be
3218 compressed. */
3219 asect->flags |= SEC_ELF_COMPRESS;
3220 /* If this section will be compressed, delay adding section
3221 name to section name section after it is compressed in
3222 _bfd_elf_assign_file_positions_for_non_load. */
3223 delay_st_name_p = true;
3224 }
3225 }
3226 else if ((asect->flags & SEC_ELF_RENAME))
3227 {
3228 /* objcopy: rename output DWARF debug section. */
3229 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3230 {
3231 /* When we decompress or compress with SHF_COMPRESSED,
3232 convert section name from .zdebug_* to .debug_* if
3233 needed. */
3234 if (name[1] == 'z')
3235 {
3236 char *new_name = convert_zdebug_to_debug (abfd, name);
3237 if (new_name == NULL)
3238 {
3239 arg->failed = true;
3240 return;
3241 }
3242 name = new_name;
3243 }
3244 }
3245 else if (asect->compress_status == COMPRESS_SECTION_DONE
3246 && name[1] == 'd')
3247 {
3248 /* PR binutils/18087: Compression does not always make a
3249 section smaller. So only rename the section when
3250 compression has actually taken place. If input section
3251 name is .zdebug_*, we should never compress it again. */
3252 char *new_name = convert_debug_to_zdebug (abfd, name);
3253 if (new_name == NULL)
3254 {
3255 arg->failed = true;
3256 return;
3257 }
3258 name = new_name;
3259 }
3260 }
3261
3262 if (delay_st_name_p)
3263 this_hdr->sh_name = (unsigned int) -1;
3264 else
3265 {
3266 this_hdr->sh_name
3267 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3268 name, false);
3269 if (this_hdr->sh_name == (unsigned int) -1)
3270 {
3271 arg->failed = true;
3272 return;
3273 }
3274 }
3275
3276 /* Don't clear sh_flags. Assembler may set additional bits. */
3277
3278 if ((asect->flags & SEC_ALLOC) != 0
3279 || asect->user_set_vma)
3280 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3281 else
3282 this_hdr->sh_addr = 0;
3283
3284 this_hdr->sh_offset = 0;
3285 this_hdr->sh_size = asect->size;
3286 this_hdr->sh_link = 0;
3287 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3288 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3289 {
3290 _bfd_error_handler
3291 /* xgettext:c-format */
3292 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3293 abfd, asect->alignment_power, asect);
3294 arg->failed = true;
3295 return;
3296 }
3297 /* Set sh_addralign to the highest power of two given by alignment
3298 consistent with the section VMA. Linker scripts can force VMA. */
3299 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3300 this_hdr->sh_addralign = mask & -mask;
3301 /* The sh_entsize and sh_info fields may have been set already by
3302 copy_private_section_data. */
3303
3304 this_hdr->bfd_section = asect;
3305 this_hdr->contents = NULL;
3306
3307 /* If the section type is unspecified, we set it based on
3308 asect->flags. */
3309 if (asect->type != 0)
3310 sh_type = asect->type;
3311 else if ((asect->flags & SEC_GROUP) != 0)
3312 sh_type = SHT_GROUP;
3313 else
3314 sh_type = bfd_elf_get_default_section_type (asect->flags);
3315
3316 if (this_hdr->sh_type == SHT_NULL)
3317 this_hdr->sh_type = sh_type;
3318 else if (this_hdr->sh_type == SHT_NOBITS
3319 && sh_type == SHT_PROGBITS
3320 && (asect->flags & SEC_ALLOC) != 0)
3321 {
3322 /* Warn if we are changing a NOBITS section to PROGBITS, but
3323 allow the link to proceed. This can happen when users link
3324 non-bss input sections to bss output sections, or emit data
3325 to a bss output section via a linker script. */
3326 _bfd_error_handler
3327 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3328 this_hdr->sh_type = sh_type;
3329 }
3330
3331 switch (this_hdr->sh_type)
3332 {
3333 default:
3334 break;
3335
3336 case SHT_STRTAB:
3337 case SHT_NOTE:
3338 case SHT_NOBITS:
3339 case SHT_PROGBITS:
3340 break;
3341
3342 case SHT_INIT_ARRAY:
3343 case SHT_FINI_ARRAY:
3344 case SHT_PREINIT_ARRAY:
3345 this_hdr->sh_entsize = bed->s->arch_size / 8;
3346 break;
3347
3348 case SHT_HASH:
3349 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3350 break;
3351
3352 case SHT_DYNSYM:
3353 this_hdr->sh_entsize = bed->s->sizeof_sym;
3354 break;
3355
3356 case SHT_DYNAMIC:
3357 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3358 break;
3359
3360 case SHT_RELA:
3361 if (get_elf_backend_data (abfd)->may_use_rela_p)
3362 this_hdr->sh_entsize = bed->s->sizeof_rela;
3363 break;
3364
3365 case SHT_REL:
3366 if (get_elf_backend_data (abfd)->may_use_rel_p)
3367 this_hdr->sh_entsize = bed->s->sizeof_rel;
3368 break;
3369
3370 case SHT_GNU_versym:
3371 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3372 break;
3373
3374 case SHT_GNU_verdef:
3375 this_hdr->sh_entsize = 0;
3376 /* objcopy or strip will copy over sh_info, but may not set
3377 cverdefs. The linker will set cverdefs, but sh_info will be
3378 zero. */
3379 if (this_hdr->sh_info == 0)
3380 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3381 else
3382 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3383 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3384 break;
3385
3386 case SHT_GNU_verneed:
3387 this_hdr->sh_entsize = 0;
3388 /* objcopy or strip will copy over sh_info, but may not set
3389 cverrefs. The linker will set cverrefs, but sh_info will be
3390 zero. */
3391 if (this_hdr->sh_info == 0)
3392 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3393 else
3394 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3395 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3396 break;
3397
3398 case SHT_GROUP:
3399 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3400 break;
3401
3402 case SHT_GNU_HASH:
3403 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3404 break;
3405 }
3406
3407 if ((asect->flags & SEC_ALLOC) != 0)
3408 this_hdr->sh_flags |= SHF_ALLOC;
3409 if ((asect->flags & SEC_READONLY) == 0)
3410 this_hdr->sh_flags |= SHF_WRITE;
3411 if ((asect->flags & SEC_CODE) != 0)
3412 this_hdr->sh_flags |= SHF_EXECINSTR;
3413 if ((asect->flags & SEC_MERGE) != 0)
3414 {
3415 this_hdr->sh_flags |= SHF_MERGE;
3416 this_hdr->sh_entsize = asect->entsize;
3417 }
3418 if ((asect->flags & SEC_STRINGS) != 0)
3419 this_hdr->sh_flags |= SHF_STRINGS;
3420 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3421 this_hdr->sh_flags |= SHF_GROUP;
3422 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3423 {
3424 this_hdr->sh_flags |= SHF_TLS;
3425 if (asect->size == 0
3426 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3427 {
3428 struct bfd_link_order *o = asect->map_tail.link_order;
3429
3430 this_hdr->sh_size = 0;
3431 if (o != NULL)
3432 {
3433 this_hdr->sh_size = o->offset + o->size;
3434 if (this_hdr->sh_size != 0)
3435 this_hdr->sh_type = SHT_NOBITS;
3436 }
3437 }
3438 }
3439 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3440 this_hdr->sh_flags |= SHF_EXCLUDE;
3441
3442 /* If the section has relocs, set up a section header for the
3443 SHT_REL[A] section. If two relocation sections are required for
3444 this section, it is up to the processor-specific back-end to
3445 create the other. */
3446 if ((asect->flags & SEC_RELOC) != 0)
3447 {
3448 /* When doing a relocatable link, create both REL and RELA sections if
3449 needed. */
3450 if (arg->link_info
3451 /* Do the normal setup if we wouldn't create any sections here. */
3452 && esd->rel.count + esd->rela.count > 0
3453 && (bfd_link_relocatable (arg->link_info)
3454 || arg->link_info->emitrelocations))
3455 {
3456 if (esd->rel.count && esd->rel.hdr == NULL
3457 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3458 false, delay_st_name_p))
3459 {
3460 arg->failed = true;
3461 return;
3462 }
3463 if (esd->rela.count && esd->rela.hdr == NULL
3464 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3465 true, delay_st_name_p))
3466 {
3467 arg->failed = true;
3468 return;
3469 }
3470 }
3471 else if (!_bfd_elf_init_reloc_shdr (abfd,
3472 (asect->use_rela_p
3473 ? &esd->rela : &esd->rel),
3474 name,
3475 asect->use_rela_p,
3476 delay_st_name_p))
3477 {
3478 arg->failed = true;
3479 return;
3480 }
3481 }
3482
3483 /* Check for processor-specific section types. */
3484 sh_type = this_hdr->sh_type;
3485 if (bed->elf_backend_fake_sections
3486 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3487 {
3488 arg->failed = true;
3489 return;
3490 }
3491
3492 if (sh_type == SHT_NOBITS && asect->size != 0)
3493 {
3494 /* Don't change the header type from NOBITS if we are being
3495 called for objcopy --only-keep-debug. */
3496 this_hdr->sh_type = sh_type;
3497 }
3498 }
3499
3500 /* Fill in the contents of a SHT_GROUP section. Called from
3501 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3502 when ELF targets use the generic linker, ld. Called for ld -r
3503 from bfd_elf_final_link. */
3504
3505 void
3506 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3507 {
3508 bool *failedptr = (bool *) failedptrarg;
3509 asection *elt, *first;
3510 unsigned char *loc;
3511 bool gas;
3512
3513 /* Ignore linker created group section. See elfNN_ia64_object_p in
3514 elfxx-ia64.c. */
3515 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3516 || sec->size == 0
3517 || *failedptr)
3518 return;
3519
3520 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3521 {
3522 unsigned long symindx = 0;
3523
3524 /* elf_group_id will have been set up by objcopy and the
3525 generic linker. */
3526 if (elf_group_id (sec) != NULL)
3527 symindx = elf_group_id (sec)->udata.i;
3528
3529 if (symindx == 0)
3530 {
3531 /* If called from the assembler, swap_out_syms will have set up
3532 elf_section_syms.
3533 PR 25699: A corrupt input file could contain bogus group info. */
3534 if (sec->index >= elf_num_section_syms (abfd)
3535 || elf_section_syms (abfd)[sec->index] == NULL)
3536 {
3537 *failedptr = true;
3538 return;
3539 }
3540 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3541 }
3542 elf_section_data (sec)->this_hdr.sh_info = symindx;
3543 }
3544 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3545 {
3546 /* The ELF backend linker sets sh_info to -2 when the group
3547 signature symbol is global, and thus the index can't be
3548 set until all local symbols are output. */
3549 asection *igroup;
3550 struct bfd_elf_section_data *sec_data;
3551 unsigned long symndx;
3552 unsigned long extsymoff;
3553 struct elf_link_hash_entry *h;
3554
3555 /* The point of this little dance to the first SHF_GROUP section
3556 then back to the SHT_GROUP section is that this gets us to
3557 the SHT_GROUP in the input object. */
3558 igroup = elf_sec_group (elf_next_in_group (sec));
3559 sec_data = elf_section_data (igroup);
3560 symndx = sec_data->this_hdr.sh_info;
3561 extsymoff = 0;
3562 if (!elf_bad_symtab (igroup->owner))
3563 {
3564 Elf_Internal_Shdr *symtab_hdr;
3565
3566 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3567 extsymoff = symtab_hdr->sh_info;
3568 }
3569 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3570 while (h->root.type == bfd_link_hash_indirect
3571 || h->root.type == bfd_link_hash_warning)
3572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3573
3574 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3575 }
3576
3577 /* The contents won't be allocated for "ld -r" or objcopy. */
3578 gas = true;
3579 if (sec->contents == NULL)
3580 {
3581 gas = false;
3582 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3583
3584 /* Arrange for the section to be written out. */
3585 elf_section_data (sec)->this_hdr.contents = sec->contents;
3586 if (sec->contents == NULL)
3587 {
3588 *failedptr = true;
3589 return;
3590 }
3591 }
3592
3593 loc = sec->contents + sec->size;
3594
3595 /* Get the pointer to the first section in the group that gas
3596 squirreled away here. objcopy arranges for this to be set to the
3597 start of the input section group. */
3598 first = elt = elf_next_in_group (sec);
3599
3600 /* First element is a flag word. Rest of section is elf section
3601 indices for all the sections of the group. Write them backwards
3602 just to keep the group in the same order as given in .section
3603 directives, not that it matters. */
3604 while (elt != NULL)
3605 {
3606 asection *s;
3607
3608 s = elt;
3609 if (!gas)
3610 s = s->output_section;
3611 if (s != NULL
3612 && !bfd_is_abs_section (s))
3613 {
3614 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3615 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3616
3617 if (elf_sec->rel.hdr != NULL
3618 && (gas
3619 || (input_elf_sec->rel.hdr != NULL
3620 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3621 {
3622 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3623 loc -= 4;
3624 if (loc == sec->contents)
3625 break;
3626 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3627 }
3628 if (elf_sec->rela.hdr != NULL
3629 && (gas
3630 || (input_elf_sec->rela.hdr != NULL
3631 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3632 {
3633 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3634 loc -= 4;
3635 if (loc == sec->contents)
3636 break;
3637 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3638 }
3639 loc -= 4;
3640 if (loc == sec->contents)
3641 break;
3642 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3643 }
3644 elt = elf_next_in_group (elt);
3645 if (elt == first)
3646 break;
3647 }
3648
3649 /* We should always get here with loc == sec->contents + 4, but it is
3650 possible to craft bogus SHT_GROUP sections that will cause segfaults
3651 in objcopy without checking loc here and in the loop above. */
3652 if (loc == sec->contents)
3653 BFD_ASSERT (0);
3654 else
3655 {
3656 loc -= 4;
3657 if (loc != sec->contents)
3658 {
3659 BFD_ASSERT (0);
3660 memset (sec->contents + 4, 0, loc - sec->contents);
3661 loc = sec->contents;
3662 }
3663 }
3664
3665 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3666 }
3667
3668 /* Given NAME, the name of a relocation section stripped of its
3669 .rel/.rela prefix, return the section in ABFD to which the
3670 relocations apply. */
3671
3672 asection *
3673 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3674 {
3675 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3676 section likely apply to .got.plt or .got section. */
3677 if (get_elf_backend_data (abfd)->want_got_plt
3678 && strcmp (name, ".plt") == 0)
3679 {
3680 asection *sec;
3681
3682 name = ".got.plt";
3683 sec = bfd_get_section_by_name (abfd, name);
3684 if (sec != NULL)
3685 return sec;
3686 name = ".got";
3687 }
3688
3689 return bfd_get_section_by_name (abfd, name);
3690 }
3691
3692 /* Return the section to which RELOC_SEC applies. */
3693
3694 static asection *
3695 elf_get_reloc_section (asection *reloc_sec)
3696 {
3697 const char *name;
3698 unsigned int type;
3699 bfd *abfd;
3700 const struct elf_backend_data *bed;
3701
3702 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3703 if (type != SHT_REL && type != SHT_RELA)
3704 return NULL;
3705
3706 /* We look up the section the relocs apply to by name. */
3707 name = reloc_sec->name;
3708 if (!startswith (name, ".rel"))
3709 return NULL;
3710 name += 4;
3711 if (type == SHT_RELA && *name++ != 'a')
3712 return NULL;
3713
3714 abfd = reloc_sec->owner;
3715 bed = get_elf_backend_data (abfd);
3716 return bed->get_reloc_section (abfd, name);
3717 }
3718
3719 /* Assign all ELF section numbers. The dummy first section is handled here
3720 too. The link/info pointers for the standard section types are filled
3721 in here too, while we're at it. LINK_INFO will be 0 when arriving
3722 here for gas, objcopy, and when using the generic ELF linker. */
3723
3724 static bool
3725 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3726 {
3727 struct elf_obj_tdata *t = elf_tdata (abfd);
3728 asection *sec;
3729 unsigned int section_number;
3730 Elf_Internal_Shdr **i_shdrp;
3731 struct bfd_elf_section_data *d;
3732 bool need_symtab;
3733 size_t amt;
3734
3735 section_number = 1;
3736
3737 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3738
3739 /* SHT_GROUP sections are in relocatable files only. */
3740 if (link_info == NULL || !link_info->resolve_section_groups)
3741 {
3742 size_t reloc_count = 0;
3743
3744 /* Put SHT_GROUP sections first. */
3745 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3746 {
3747 d = elf_section_data (sec);
3748
3749 if (d->this_hdr.sh_type == SHT_GROUP)
3750 {
3751 if (sec->flags & SEC_LINKER_CREATED)
3752 {
3753 /* Remove the linker created SHT_GROUP sections. */
3754 bfd_section_list_remove (abfd, sec);
3755 abfd->section_count--;
3756 }
3757 else
3758 d->this_idx = section_number++;
3759 }
3760
3761 /* Count relocations. */
3762 reloc_count += sec->reloc_count;
3763 }
3764
3765 /* Set/clear HAS_RELOC depending on whether there are relocations. */
3766 if (reloc_count == 0)
3767 abfd->flags &= ~HAS_RELOC;
3768 else
3769 abfd->flags |= HAS_RELOC;
3770 }
3771
3772 for (sec = abfd->sections; sec; sec = sec->next)
3773 {
3774 d = elf_section_data (sec);
3775
3776 if (d->this_hdr.sh_type != SHT_GROUP)
3777 d->this_idx = section_number++;
3778 if (d->this_hdr.sh_name != (unsigned int) -1)
3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3780 if (d->rel.hdr)
3781 {
3782 d->rel.idx = section_number++;
3783 if (d->rel.hdr->sh_name != (unsigned int) -1)
3784 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3785 }
3786 else
3787 d->rel.idx = 0;
3788
3789 if (d->rela.hdr)
3790 {
3791 d->rela.idx = section_number++;
3792 if (d->rela.hdr->sh_name != (unsigned int) -1)
3793 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3794 }
3795 else
3796 d->rela.idx = 0;
3797 }
3798
3799 need_symtab = (bfd_get_symcount (abfd) > 0
3800 || (link_info == NULL
3801 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3802 == HAS_RELOC)));
3803 if (need_symtab)
3804 {
3805 elf_onesymtab (abfd) = section_number++;
3806 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3807 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3808 {
3809 elf_section_list *entry;
3810
3811 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3812
3813 entry = bfd_zalloc (abfd, sizeof (*entry));
3814 entry->ndx = section_number++;
3815 elf_symtab_shndx_list (abfd) = entry;
3816 entry->hdr.sh_name
3817 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3818 ".symtab_shndx", false);
3819 if (entry->hdr.sh_name == (unsigned int) -1)
3820 return false;
3821 }
3822 elf_strtab_sec (abfd) = section_number++;
3823 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3824 }
3825
3826 elf_shstrtab_sec (abfd) = section_number++;
3827 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3828 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3829
3830 if (section_number >= SHN_LORESERVE)
3831 {
3832 /* xgettext:c-format */
3833 _bfd_error_handler (_("%pB: too many sections: %u"),
3834 abfd, section_number);
3835 return false;
3836 }
3837
3838 elf_numsections (abfd) = section_number;
3839 elf_elfheader (abfd)->e_shnum = section_number;
3840
3841 /* Set up the list of section header pointers, in agreement with the
3842 indices. */
3843 amt = section_number * sizeof (Elf_Internal_Shdr *);
3844 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3845 if (i_shdrp == NULL)
3846 return false;
3847
3848 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3849 sizeof (Elf_Internal_Shdr));
3850 if (i_shdrp[0] == NULL)
3851 {
3852 bfd_release (abfd, i_shdrp);
3853 return false;
3854 }
3855
3856 elf_elfsections (abfd) = i_shdrp;
3857
3858 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3859 if (need_symtab)
3860 {
3861 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3862 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3863 {
3864 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3865 BFD_ASSERT (entry != NULL);
3866 i_shdrp[entry->ndx] = & entry->hdr;
3867 entry->hdr.sh_link = elf_onesymtab (abfd);
3868 }
3869 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3870 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3871 }
3872
3873 for (sec = abfd->sections; sec; sec = sec->next)
3874 {
3875 asection *s;
3876
3877 d = elf_section_data (sec);
3878
3879 i_shdrp[d->this_idx] = &d->this_hdr;
3880 if (d->rel.idx != 0)
3881 i_shdrp[d->rel.idx] = d->rel.hdr;
3882 if (d->rela.idx != 0)
3883 i_shdrp[d->rela.idx] = d->rela.hdr;
3884
3885 /* Fill in the sh_link and sh_info fields while we're at it. */
3886
3887 /* sh_link of a reloc section is the section index of the symbol
3888 table. sh_info is the section index of the section to which
3889 the relocation entries apply. */
3890 if (d->rel.idx != 0)
3891 {
3892 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3893 d->rel.hdr->sh_info = d->this_idx;
3894 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3895 }
3896 if (d->rela.idx != 0)
3897 {
3898 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3899 d->rela.hdr->sh_info = d->this_idx;
3900 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3901 }
3902
3903 /* We need to set up sh_link for SHF_LINK_ORDER. */
3904 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3905 {
3906 s = elf_linked_to_section (sec);
3907 /* We can now have a NULL linked section pointer.
3908 This happens when the sh_link field is 0, which is done
3909 when a linked to section is discarded but the linking
3910 section has been retained for some reason. */
3911 if (s)
3912 {
3913 /* Check discarded linkonce section. */
3914 if (discarded_section (s))
3915 {
3916 asection *kept;
3917 _bfd_error_handler
3918 /* xgettext:c-format */
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " discarded section `%pA' of `%pB'"),
3921 abfd, d->this_hdr.bfd_section, s, s->owner);
3922 /* Point to the kept section if it has the same
3923 size as the discarded one. */
3924 kept = _bfd_elf_check_kept_section (s, link_info);
3925 if (kept == NULL)
3926 {
3927 bfd_set_error (bfd_error_bad_value);
3928 return false;
3929 }
3930 s = kept;
3931 }
3932 /* Handle objcopy. */
3933 else if (s->output_section == NULL)
3934 {
3935 _bfd_error_handler
3936 /* xgettext:c-format */
3937 (_("%pB: sh_link of section `%pA' points to"
3938 " removed section `%pA' of `%pB'"),
3939 abfd, d->this_hdr.bfd_section, s, s->owner);
3940 bfd_set_error (bfd_error_bad_value);
3941 return false;
3942 }
3943 s = s->output_section;
3944 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3945 }
3946 }
3947
3948 switch (d->this_hdr.sh_type)
3949 {
3950 case SHT_REL:
3951 case SHT_RELA:
3952 /* A reloc section which we are treating as a normal BFD
3953 section. sh_link is the section index of the symbol
3954 table. sh_info is the section index of the section to
3955 which the relocation entries apply. We assume that an
3956 allocated reloc section uses the dynamic symbol table
3957 if there is one. Otherwise we guess the normal symbol
3958 table. FIXME: How can we be sure? */
3959 if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
3960 {
3961 s = bfd_get_section_by_name (abfd, ".dynsym");
3962 if (s != NULL)
3963 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3964 }
3965 if (d->this_hdr.sh_link == 0)
3966 d->this_hdr.sh_link = elf_onesymtab (abfd);
3967
3968 s = elf_get_reloc_section (sec);
3969 if (s != NULL)
3970 {
3971 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3972 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3973 }
3974 break;
3975
3976 case SHT_STRTAB:
3977 /* We assume that a section named .stab*str is a stabs
3978 string section. We look for a section with the same name
3979 but without the trailing ``str'', and set its sh_link
3980 field to point to this section. */
3981 if (startswith (sec->name, ".stab")
3982 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3983 {
3984 size_t len;
3985 char *alc;
3986
3987 len = strlen (sec->name);
3988 alc = (char *) bfd_malloc (len - 2);
3989 if (alc == NULL)
3990 return false;
3991 memcpy (alc, sec->name, len - 3);
3992 alc[len - 3] = '\0';
3993 s = bfd_get_section_by_name (abfd, alc);
3994 free (alc);
3995 if (s != NULL)
3996 {
3997 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3998
3999 /* This is a .stab section. */
4000 elf_section_data (s)->this_hdr.sh_entsize = 12;
4001 }
4002 }
4003 break;
4004
4005 case SHT_DYNAMIC:
4006 case SHT_DYNSYM:
4007 case SHT_GNU_verneed:
4008 case SHT_GNU_verdef:
4009 /* sh_link is the section header index of the string table
4010 used for the dynamic entries, or the symbol table, or the
4011 version strings. */
4012 s = bfd_get_section_by_name (abfd, ".dynstr");
4013 if (s != NULL)
4014 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4015 break;
4016
4017 case SHT_GNU_LIBLIST:
4018 /* sh_link is the section header index of the prelink library
4019 list used for the dynamic entries, or the symbol table, or
4020 the version strings. */
4021 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4022 ? ".dynstr" : ".gnu.libstr"));
4023 if (s != NULL)
4024 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4025 break;
4026
4027 case SHT_HASH:
4028 case SHT_GNU_HASH:
4029 case SHT_GNU_versym:
4030 /* sh_link is the section header index of the symbol table
4031 this hash table or version table is for. */
4032 s = bfd_get_section_by_name (abfd, ".dynsym");
4033 if (s != NULL)
4034 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4035 break;
4036
4037 case SHT_GROUP:
4038 d->this_hdr.sh_link = elf_onesymtab (abfd);
4039 }
4040 }
4041
4042 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4043 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4044 debug section name from .debug_* to .zdebug_* if needed. */
4045
4046 return true;
4047 }
4048
4049 static bool
4050 sym_is_global (bfd *abfd, asymbol *sym)
4051 {
4052 /* If the backend has a special mapping, use it. */
4053 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4054 if (bed->elf_backend_sym_is_global)
4055 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4056
4057 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4058 || bfd_is_und_section (bfd_asymbol_section (sym))
4059 || bfd_is_com_section (bfd_asymbol_section (sym)));
4060 }
4061
4062 /* Filter global symbols of ABFD to include in the import library. All
4063 SYMCOUNT symbols of ABFD can be examined from their pointers in
4064 SYMS. Pointers of symbols to keep should be stored contiguously at
4065 the beginning of that array.
4066
4067 Returns the number of symbols to keep. */
4068
4069 unsigned int
4070 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4071 asymbol **syms, long symcount)
4072 {
4073 long src_count, dst_count = 0;
4074
4075 for (src_count = 0; src_count < symcount; src_count++)
4076 {
4077 asymbol *sym = syms[src_count];
4078 char *name = (char *) bfd_asymbol_name (sym);
4079 struct bfd_link_hash_entry *h;
4080
4081 if (!sym_is_global (abfd, sym))
4082 continue;
4083
4084 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4085 if (h == NULL)
4086 continue;
4087 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4088 continue;
4089 if (h->linker_def || h->ldscript_def)
4090 continue;
4091
4092 syms[dst_count++] = sym;
4093 }
4094
4095 syms[dst_count] = NULL;
4096
4097 return dst_count;
4098 }
4099
4100 /* Don't output section symbols for sections that are not going to be
4101 output, that are duplicates or there is no BFD section. */
4102
4103 static bool
4104 ignore_section_sym (bfd *abfd, asymbol *sym)
4105 {
4106 elf_symbol_type *type_ptr;
4107
4108 if (sym == NULL)
4109 return false;
4110
4111 if ((sym->flags & BSF_SECTION_SYM) == 0)
4112 return false;
4113
4114 /* Ignore the section symbol if it isn't used. */
4115 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4116 return true;
4117
4118 if (sym->section == NULL)
4119 return true;
4120
4121 type_ptr = elf_symbol_from (sym);
4122 return ((type_ptr != NULL
4123 && type_ptr->internal_elf_sym.st_shndx != 0
4124 && bfd_is_abs_section (sym->section))
4125 || !(sym->section->owner == abfd
4126 || (sym->section->output_section != NULL
4127 && sym->section->output_section->owner == abfd
4128 && sym->section->output_offset == 0)
4129 || bfd_is_abs_section (sym->section)));
4130 }
4131
4132 /* Map symbol from it's internal number to the external number, moving
4133 all local symbols to be at the head of the list. */
4134
4135 static bool
4136 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4137 {
4138 unsigned int symcount = bfd_get_symcount (abfd);
4139 asymbol **syms = bfd_get_outsymbols (abfd);
4140 asymbol **sect_syms;
4141 unsigned int num_locals = 0;
4142 unsigned int num_globals = 0;
4143 unsigned int num_locals2 = 0;
4144 unsigned int num_globals2 = 0;
4145 unsigned int max_index = 0;
4146 unsigned int idx;
4147 asection *asect;
4148 asymbol **new_syms;
4149 size_t amt;
4150
4151 #ifdef DEBUG
4152 fprintf (stderr, "elf_map_symbols\n");
4153 fflush (stderr);
4154 #endif
4155
4156 for (asect = abfd->sections; asect; asect = asect->next)
4157 {
4158 if (max_index < asect->index)
4159 max_index = asect->index;
4160 }
4161
4162 max_index++;
4163 amt = max_index * sizeof (asymbol *);
4164 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4165 if (sect_syms == NULL)
4166 return false;
4167 elf_section_syms (abfd) = sect_syms;
4168 elf_num_section_syms (abfd) = max_index;
4169
4170 /* Init sect_syms entries for any section symbols we have already
4171 decided to output. */
4172 for (idx = 0; idx < symcount; idx++)
4173 {
4174 asymbol *sym = syms[idx];
4175
4176 if ((sym->flags & BSF_SECTION_SYM) != 0
4177 && sym->value == 0
4178 && !ignore_section_sym (abfd, sym)
4179 && !bfd_is_abs_section (sym->section))
4180 {
4181 asection *sec = sym->section;
4182
4183 if (sec->owner != abfd)
4184 sec = sec->output_section;
4185
4186 sect_syms[sec->index] = syms[idx];
4187 }
4188 }
4189
4190 /* Classify all of the symbols. */
4191 for (idx = 0; idx < symcount; idx++)
4192 {
4193 if (sym_is_global (abfd, syms[idx]))
4194 num_globals++;
4195 else if (!ignore_section_sym (abfd, syms[idx]))
4196 num_locals++;
4197 }
4198
4199 /* We will be adding a section symbol for each normal BFD section. Most
4200 sections will already have a section symbol in outsymbols, but
4201 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4202 at least in that case. */
4203 for (asect = abfd->sections; asect; asect = asect->next)
4204 {
4205 asymbol *sym = asect->symbol;
4206 /* Don't include ignored section symbols. */
4207 if (!ignore_section_sym (abfd, sym)
4208 && sect_syms[asect->index] == NULL)
4209 {
4210 if (!sym_is_global (abfd, asect->symbol))
4211 num_locals++;
4212 else
4213 num_globals++;
4214 }
4215 }
4216
4217 /* Now sort the symbols so the local symbols are first. */
4218 amt = (num_locals + num_globals) * sizeof (asymbol *);
4219 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4220 if (new_syms == NULL)
4221 return false;
4222
4223 for (idx = 0; idx < symcount; idx++)
4224 {
4225 asymbol *sym = syms[idx];
4226 unsigned int i;
4227
4228 if (sym_is_global (abfd, sym))
4229 i = num_locals + num_globals2++;
4230 /* Don't include ignored section symbols. */
4231 else if (!ignore_section_sym (abfd, sym))
4232 i = num_locals2++;
4233 else
4234 continue;
4235 new_syms[i] = sym;
4236 sym->udata.i = i + 1;
4237 }
4238 for (asect = abfd->sections; asect; asect = asect->next)
4239 {
4240 asymbol *sym = asect->symbol;
4241 if (!ignore_section_sym (abfd, sym)
4242 && sect_syms[asect->index] == NULL)
4243 {
4244 unsigned int i;
4245
4246 sect_syms[asect->index] = sym;
4247 if (!sym_is_global (abfd, sym))
4248 i = num_locals2++;
4249 else
4250 i = num_locals + num_globals2++;
4251 new_syms[i] = sym;
4252 sym->udata.i = i + 1;
4253 }
4254 }
4255
4256 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4257
4258 *pnum_locals = num_locals;
4259 return true;
4260 }
4261
4262 /* Align to the maximum file alignment that could be required for any
4263 ELF data structure. */
4264
4265 static inline file_ptr
4266 align_file_position (file_ptr off, int align)
4267 {
4268 return (off + align - 1) & ~(align - 1);
4269 }
4270
4271 /* Assign a file position to a section, optionally aligning to the
4272 required section alignment. */
4273
4274 file_ptr
4275 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4276 file_ptr offset,
4277 bool align)
4278 {
4279 if (align && i_shdrp->sh_addralign > 1)
4280 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4281 i_shdrp->sh_offset = offset;
4282 if (i_shdrp->bfd_section != NULL)
4283 i_shdrp->bfd_section->filepos = offset;
4284 if (i_shdrp->sh_type != SHT_NOBITS)
4285 offset += i_shdrp->sh_size;
4286 return offset;
4287 }
4288
4289 /* Compute the file positions we are going to put the sections at, and
4290 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4291 is not NULL, this is being called by the ELF backend linker. */
4292
4293 bool
4294 _bfd_elf_compute_section_file_positions (bfd *abfd,
4295 struct bfd_link_info *link_info)
4296 {
4297 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4298 struct fake_section_arg fsargs;
4299 bool failed;
4300 struct elf_strtab_hash *strtab = NULL;
4301 Elf_Internal_Shdr *shstrtab_hdr;
4302 bool need_symtab;
4303
4304 if (abfd->output_has_begun)
4305 return true;
4306
4307 /* Do any elf backend specific processing first. */
4308 if (bed->elf_backend_begin_write_processing)
4309 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4310
4311 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4312 return false;
4313
4314 fsargs.failed = false;
4315 fsargs.link_info = link_info;
4316 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4317 if (fsargs.failed)
4318 return false;
4319
4320 if (!assign_section_numbers (abfd, link_info))
4321 return false;
4322
4323 /* The backend linker builds symbol table information itself. */
4324 need_symtab = (link_info == NULL
4325 && (bfd_get_symcount (abfd) > 0
4326 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4327 == HAS_RELOC)));
4328 if (need_symtab)
4329 {
4330 /* Non-zero if doing a relocatable link. */
4331 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4332
4333 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4334 return false;
4335 }
4336
4337 failed = false;
4338 if (link_info == NULL)
4339 {
4340 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4341 if (failed)
4342 return false;
4343 }
4344
4345 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4346 /* sh_name was set in init_file_header. */
4347 shstrtab_hdr->sh_type = SHT_STRTAB;
4348 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4349 shstrtab_hdr->sh_addr = 0;
4350 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4351 shstrtab_hdr->sh_entsize = 0;
4352 shstrtab_hdr->sh_link = 0;
4353 shstrtab_hdr->sh_info = 0;
4354 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4355 shstrtab_hdr->sh_addralign = 1;
4356
4357 if (!assign_file_positions_except_relocs (abfd, link_info))
4358 return false;
4359
4360 if (need_symtab)
4361 {
4362 file_ptr off;
4363 Elf_Internal_Shdr *hdr;
4364
4365 off = elf_next_file_pos (abfd);
4366
4367 hdr = & elf_symtab_hdr (abfd);
4368 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4369
4370 if (elf_symtab_shndx_list (abfd) != NULL)
4371 {
4372 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4373 if (hdr->sh_size != 0)
4374 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4375 /* FIXME: What about other symtab_shndx sections in the list ? */
4376 }
4377
4378 hdr = &elf_tdata (abfd)->strtab_hdr;
4379 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4380
4381 elf_next_file_pos (abfd) = off;
4382
4383 /* Now that we know where the .strtab section goes, write it
4384 out. */
4385 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4386 || ! _bfd_elf_strtab_emit (abfd, strtab))
4387 return false;
4388 _bfd_elf_strtab_free (strtab);
4389 }
4390
4391 abfd->output_has_begun = true;
4392
4393 return true;
4394 }
4395
4396 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4397 function effectively returns whether --eh-frame-hdr is given on the
4398 command line. After size_dynamic_sections the result reflects
4399 whether .eh_frame_hdr will actually be output (sizing isn't done
4400 until ldemul_after_allocation). */
4401
4402 static asection *
4403 elf_eh_frame_hdr (const struct bfd_link_info *info)
4404 {
4405 if (info != NULL && is_elf_hash_table (info->hash))
4406 return elf_hash_table (info)->eh_info.hdr_sec;
4407 return NULL;
4408 }
4409
4410 /* Make an initial estimate of the size of the program header. If we
4411 get the number wrong here, we'll redo section placement. */
4412
4413 static bfd_size_type
4414 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4415 {
4416 size_t segs;
4417 asection *s;
4418 const struct elf_backend_data *bed;
4419
4420 /* Assume we will need exactly two PT_LOAD segments: one for text
4421 and one for data. */
4422 segs = 2;
4423
4424 s = bfd_get_section_by_name (abfd, ".interp");
4425 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4426 {
4427 /* If we have a loadable interpreter section, we need a
4428 PT_INTERP segment. In this case, assume we also need a
4429 PT_PHDR segment, although that may not be true for all
4430 targets. */
4431 segs += 2;
4432 }
4433
4434 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4435 {
4436 /* We need a PT_DYNAMIC segment. */
4437 ++segs;
4438 }
4439
4440 if (info != NULL && info->relro)
4441 {
4442 /* We need a PT_GNU_RELRO segment. */
4443 ++segs;
4444 }
4445
4446 if (elf_eh_frame_hdr (info))
4447 {
4448 /* We need a PT_GNU_EH_FRAME segment. */
4449 ++segs;
4450 }
4451
4452 if (elf_stack_flags (abfd))
4453 {
4454 /* We need a PT_GNU_STACK segment. */
4455 ++segs;
4456 }
4457
4458 if (elf_sframe (abfd))
4459 {
4460 /* We need a PT_GNU_SFRAME segment. */
4461 ++segs;
4462 }
4463
4464 s = bfd_get_section_by_name (abfd,
4465 NOTE_GNU_PROPERTY_SECTION_NAME);
4466 if (s != NULL && s->size != 0)
4467 {
4468 /* We need a PT_GNU_PROPERTY segment. */
4469 ++segs;
4470 }
4471
4472 for (s = abfd->sections; s != NULL; s = s->next)
4473 {
4474 if ((s->flags & SEC_LOAD) != 0
4475 && elf_section_type (s) == SHT_NOTE)
4476 {
4477 unsigned int alignment_power;
4478 /* We need a PT_NOTE segment. */
4479 ++segs;
4480 /* Try to create just one PT_NOTE segment for all adjacent
4481 loadable SHT_NOTE sections. gABI requires that within a
4482 PT_NOTE segment (and also inside of each SHT_NOTE section)
4483 each note should have the same alignment. So we check
4484 whether the sections are correctly aligned. */
4485 alignment_power = s->alignment_power;
4486 while (s->next != NULL
4487 && s->next->alignment_power == alignment_power
4488 && (s->next->flags & SEC_LOAD) != 0
4489 && elf_section_type (s->next) == SHT_NOTE)
4490 s = s->next;
4491 }
4492 }
4493
4494 for (s = abfd->sections; s != NULL; s = s->next)
4495 {
4496 if (s->flags & SEC_THREAD_LOCAL)
4497 {
4498 /* We need a PT_TLS segment. */
4499 ++segs;
4500 break;
4501 }
4502 }
4503
4504 bed = get_elf_backend_data (abfd);
4505
4506 if ((abfd->flags & D_PAGED) != 0
4507 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4508 {
4509 /* Add a PT_GNU_MBIND segment for each mbind section. */
4510 bfd_vma commonpagesize;
4511 unsigned int page_align_power;
4512
4513 if (info != NULL)
4514 commonpagesize = info->commonpagesize;
4515 else
4516 commonpagesize = bed->commonpagesize;
4517 page_align_power = bfd_log2 (commonpagesize);
4518 for (s = abfd->sections; s != NULL; s = s->next)
4519 if (elf_section_flags (s) & SHF_GNU_MBIND)
4520 {
4521 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4522 {
4523 _bfd_error_handler
4524 /* xgettext:c-format */
4525 (_("%pB: GNU_MBIND section `%pA' has invalid "
4526 "sh_info field: %d"),
4527 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4528 continue;
4529 }
4530 /* Align mbind section to page size. */
4531 if (s->alignment_power < page_align_power)
4532 s->alignment_power = page_align_power;
4533 segs ++;
4534 }
4535 }
4536
4537 /* Let the backend count up any program headers it might need. */
4538 if (bed->elf_backend_additional_program_headers)
4539 {
4540 int a;
4541
4542 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4543 if (a == -1)
4544 abort ();
4545 segs += a;
4546 }
4547
4548 return segs * bed->s->sizeof_phdr;
4549 }
4550
4551 /* Find the segment that contains the output_section of section. */
4552
4553 Elf_Internal_Phdr *
4554 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4555 {
4556 struct elf_segment_map *m;
4557 Elf_Internal_Phdr *p;
4558
4559 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4560 m != NULL;
4561 m = m->next, p++)
4562 {
4563 int i;
4564
4565 for (i = m->count - 1; i >= 0; i--)
4566 if (m->sections[i] == section)
4567 return p;
4568 }
4569
4570 return NULL;
4571 }
4572
4573 /* Create a mapping from a set of sections to a program segment. */
4574
4575 static struct elf_segment_map *
4576 make_mapping (bfd *abfd,
4577 asection **sections,
4578 unsigned int from,
4579 unsigned int to,
4580 bool phdr)
4581 {
4582 struct elf_segment_map *m;
4583 unsigned int i;
4584 asection **hdrpp;
4585 size_t amt;
4586
4587 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4588 amt += (to - from) * sizeof (asection *);
4589 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4590 if (m == NULL)
4591 return NULL;
4592 m->next = NULL;
4593 m->p_type = PT_LOAD;
4594 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4595 m->sections[i - from] = *hdrpp;
4596 m->count = to - from;
4597
4598 if (from == 0 && phdr)
4599 {
4600 /* Include the headers in the first PT_LOAD segment. */
4601 m->includes_filehdr = 1;
4602 m->includes_phdrs = 1;
4603 }
4604
4605 return m;
4606 }
4607
4608 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4609 on failure. */
4610
4611 struct elf_segment_map *
4612 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4613 {
4614 struct elf_segment_map *m;
4615
4616 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4617 sizeof (struct elf_segment_map));
4618 if (m == NULL)
4619 return NULL;
4620 m->next = NULL;
4621 m->p_type = PT_DYNAMIC;
4622 m->count = 1;
4623 m->sections[0] = dynsec;
4624
4625 return m;
4626 }
4627
4628 /* Possibly add or remove segments from the segment map. */
4629
4630 static bool
4631 elf_modify_segment_map (bfd *abfd,
4632 struct bfd_link_info *info,
4633 bool remove_empty_load)
4634 {
4635 struct elf_segment_map **m;
4636 const struct elf_backend_data *bed;
4637
4638 /* The placement algorithm assumes that non allocated sections are
4639 not in PT_LOAD segments. We ensure this here by removing such
4640 sections from the segment map. We also remove excluded
4641 sections. Finally, any PT_LOAD segment without sections is
4642 removed. */
4643 m = &elf_seg_map (abfd);
4644 while (*m)
4645 {
4646 unsigned int i, new_count;
4647
4648 for (new_count = 0, i = 0; i < (*m)->count; i++)
4649 {
4650 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4651 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4652 || (*m)->p_type != PT_LOAD))
4653 {
4654 (*m)->sections[new_count] = (*m)->sections[i];
4655 new_count++;
4656 }
4657 }
4658 (*m)->count = new_count;
4659
4660 if (remove_empty_load
4661 && (*m)->p_type == PT_LOAD
4662 && (*m)->count == 0
4663 && !(*m)->includes_phdrs)
4664 *m = (*m)->next;
4665 else
4666 m = &(*m)->next;
4667 }
4668
4669 bed = get_elf_backend_data (abfd);
4670 if (bed->elf_backend_modify_segment_map != NULL)
4671 {
4672 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4673 return false;
4674 }
4675
4676 return true;
4677 }
4678
4679 #define IS_TBSS(s) \
4680 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4681
4682 /* Set up a mapping from BFD sections to program segments. Update
4683 NEED_LAYOUT if the section layout is changed. */
4684
4685 bool
4686 _bfd_elf_map_sections_to_segments (bfd *abfd,
4687 struct bfd_link_info *info,
4688 bool *need_layout)
4689 {
4690 unsigned int count;
4691 struct elf_segment_map *m;
4692 asection **sections = NULL;
4693 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4694 bool no_user_phdrs;
4695
4696 no_user_phdrs = elf_seg_map (abfd) == NULL;
4697
4698 if (info != NULL)
4699 {
4700 info->user_phdrs = !no_user_phdrs;
4701
4702 /* Size the relative relocations if DT_RELR is enabled. */
4703 if (info->enable_dt_relr
4704 && need_layout != NULL
4705 && bed->size_relative_relocs
4706 && !bed->size_relative_relocs (info, need_layout))
4707 info->callbacks->einfo
4708 (_("%F%P: failed to size relative relocations\n"));
4709 }
4710
4711 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4712 {
4713 asection *s;
4714 unsigned int i;
4715 struct elf_segment_map *mfirst;
4716 struct elf_segment_map **pm;
4717 asection *last_hdr;
4718 bfd_vma last_size;
4719 unsigned int hdr_index;
4720 bfd_vma maxpagesize;
4721 asection **hdrpp;
4722 bool phdr_in_segment;
4723 bool writable;
4724 bool executable;
4725 unsigned int tls_count = 0;
4726 asection *first_tls = NULL;
4727 asection *first_mbind = NULL;
4728 asection *dynsec, *eh_frame_hdr;
4729 asection *sframe;
4730 size_t amt;
4731 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4732 bfd_size_type phdr_size; /* Octets/bytes. */
4733 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4734
4735 /* Select the allocated sections, and sort them. */
4736
4737 amt = bfd_count_sections (abfd) * sizeof (asection *);
4738 sections = (asection **) bfd_malloc (amt);
4739 if (sections == NULL)
4740 goto error_return;
4741
4742 /* Calculate top address, avoiding undefined behaviour of shift
4743 left operator when shift count is equal to size of type
4744 being shifted. */
4745 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4746 addr_mask = (addr_mask << 1) + 1;
4747
4748 i = 0;
4749 for (s = abfd->sections; s != NULL; s = s->next)
4750 {
4751 if ((s->flags & SEC_ALLOC) != 0)
4752 {
4753 /* target_index is unused until bfd_elf_final_link
4754 starts output of section symbols. Use it to make
4755 qsort stable. */
4756 s->target_index = i;
4757 sections[i] = s;
4758 ++i;
4759 /* A wrapping section potentially clashes with header. */
4760 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4761 wrap_to = (s->lma + s->size / opb) & addr_mask;
4762 }
4763 }
4764 BFD_ASSERT (i <= bfd_count_sections (abfd));
4765 count = i;
4766
4767 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4768
4769 phdr_size = elf_program_header_size (abfd);
4770 if (phdr_size == (bfd_size_type) -1)
4771 phdr_size = get_program_header_size (abfd, info);
4772 phdr_size += bed->s->sizeof_ehdr;
4773 /* phdr_size is compared to LMA values which are in bytes. */
4774 phdr_size /= opb;
4775 if (info != NULL)
4776 maxpagesize = info->maxpagesize;
4777 else
4778 maxpagesize = bed->maxpagesize;
4779 if (maxpagesize == 0)
4780 maxpagesize = 1;
4781 phdr_in_segment = info != NULL && info->load_phdrs;
4782 if (count != 0
4783 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4784 >= (phdr_size & (maxpagesize - 1))))
4785 /* For compatibility with old scripts that may not be using
4786 SIZEOF_HEADERS, add headers when it looks like space has
4787 been left for them. */
4788 phdr_in_segment = true;
4789
4790 /* Build the mapping. */
4791 mfirst = NULL;
4792 pm = &mfirst;
4793
4794 /* If we have a .interp section, then create a PT_PHDR segment for
4795 the program headers and a PT_INTERP segment for the .interp
4796 section. */
4797 s = bfd_get_section_by_name (abfd, ".interp");
4798 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4799 {
4800 amt = sizeof (struct elf_segment_map);
4801 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4802 if (m == NULL)
4803 goto error_return;
4804 m->next = NULL;
4805 m->p_type = PT_PHDR;
4806 m->p_flags = PF_R;
4807 m->p_flags_valid = 1;
4808 m->includes_phdrs = 1;
4809 phdr_in_segment = true;
4810 *pm = m;
4811 pm = &m->next;
4812
4813 amt = sizeof (struct elf_segment_map);
4814 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4815 if (m == NULL)
4816 goto error_return;
4817 m->next = NULL;
4818 m->p_type = PT_INTERP;
4819 m->count = 1;
4820 m->sections[0] = s;
4821
4822 *pm = m;
4823 pm = &m->next;
4824 }
4825
4826 /* Look through the sections. We put sections in the same program
4827 segment when the start of the second section can be placed within
4828 a few bytes of the end of the first section. */
4829 last_hdr = NULL;
4830 last_size = 0;
4831 hdr_index = 0;
4832 writable = false;
4833 executable = false;
4834 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4835 if (dynsec != NULL
4836 && (dynsec->flags & SEC_LOAD) == 0)
4837 dynsec = NULL;
4838
4839 if ((abfd->flags & D_PAGED) == 0)
4840 phdr_in_segment = false;
4841
4842 /* Deal with -Ttext or something similar such that the first section
4843 is not adjacent to the program headers. This is an
4844 approximation, since at this point we don't know exactly how many
4845 program headers we will need. */
4846 if (phdr_in_segment && count > 0)
4847 {
4848 bfd_vma phdr_lma; /* Bytes. */
4849 bool separate_phdr = false;
4850
4851 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4852 if (info != NULL
4853 && info->separate_code
4854 && (sections[0]->flags & SEC_CODE) != 0)
4855 {
4856 /* If data sections should be separate from code and
4857 thus not executable, and the first section is
4858 executable then put the file and program headers in
4859 their own PT_LOAD. */
4860 separate_phdr = true;
4861 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4862 == (sections[0]->lma & addr_mask & -maxpagesize)))
4863 {
4864 /* The file and program headers are currently on the
4865 same page as the first section. Put them on the
4866 previous page if we can. */
4867 if (phdr_lma >= maxpagesize)
4868 phdr_lma -= maxpagesize;
4869 else
4870 separate_phdr = false;
4871 }
4872 }
4873 if ((sections[0]->lma & addr_mask) < phdr_lma
4874 || (sections[0]->lma & addr_mask) < phdr_size)
4875 /* If file and program headers would be placed at the end
4876 of memory then it's probably better to omit them. */
4877 phdr_in_segment = false;
4878 else if (phdr_lma < wrap_to)
4879 /* If a section wraps around to where we'll be placing
4880 file and program headers, then the headers will be
4881 overwritten. */
4882 phdr_in_segment = false;
4883 else if (separate_phdr)
4884 {
4885 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4886 if (m == NULL)
4887 goto error_return;
4888 m->p_paddr = phdr_lma * opb;
4889 m->p_vaddr_offset
4890 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4891 m->p_paddr_valid = 1;
4892 *pm = m;
4893 pm = &m->next;
4894 phdr_in_segment = false;
4895 }
4896 }
4897
4898 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4899 {
4900 asection *hdr;
4901 bool new_segment;
4902
4903 hdr = *hdrpp;
4904
4905 /* See if this section and the last one will fit in the same
4906 segment. */
4907
4908 if (last_hdr == NULL)
4909 {
4910 /* If we don't have a segment yet, then we don't need a new
4911 one (we build the last one after this loop). */
4912 new_segment = false;
4913 }
4914 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4915 {
4916 /* If this section has a different relation between the
4917 virtual address and the load address, then we need a new
4918 segment. */
4919 new_segment = true;
4920 }
4921 else if (hdr->lma < last_hdr->lma + last_size
4922 || last_hdr->lma + last_size < last_hdr->lma)
4923 {
4924 /* If this section has a load address that makes it overlap
4925 the previous section, then we need a new segment. */
4926 new_segment = true;
4927 }
4928 else if ((abfd->flags & D_PAGED) != 0
4929 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4930 == (hdr->lma & -maxpagesize)))
4931 {
4932 /* If we are demand paged then we can't map two disk
4933 pages onto the same memory page. */
4934 new_segment = false;
4935 }
4936 /* In the next test we have to be careful when last_hdr->lma is close
4937 to the end of the address space. If the aligned address wraps
4938 around to the start of the address space, then there are no more
4939 pages left in memory and it is OK to assume that the current
4940 section can be included in the current segment. */
4941 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4942 + maxpagesize > last_hdr->lma)
4943 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4944 + maxpagesize <= hdr->lma))
4945 {
4946 /* If putting this section in this segment would force us to
4947 skip a page in the segment, then we need a new segment. */
4948 new_segment = true;
4949 }
4950 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4951 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4952 {
4953 /* We don't want to put a loaded section after a
4954 nonloaded (ie. bss style) section in the same segment
4955 as that will force the non-loaded section to be loaded.
4956 Consider .tbss sections as loaded for this purpose. */
4957 new_segment = true;
4958 }
4959 else if ((abfd->flags & D_PAGED) == 0)
4960 {
4961 /* If the file is not demand paged, which means that we
4962 don't require the sections to be correctly aligned in the
4963 file, then there is no other reason for a new segment. */
4964 new_segment = false;
4965 }
4966 else if (info != NULL
4967 && info->separate_code
4968 && executable != ((hdr->flags & SEC_CODE) != 0))
4969 {
4970 new_segment = true;
4971 }
4972 else if (! writable
4973 && (hdr->flags & SEC_READONLY) == 0)
4974 {
4975 /* We don't want to put a writable section in a read only
4976 segment. */
4977 new_segment = true;
4978 }
4979 else
4980 {
4981 /* Otherwise, we can use the same segment. */
4982 new_segment = false;
4983 }
4984
4985 /* Allow interested parties a chance to override our decision. */
4986 if (last_hdr != NULL
4987 && info != NULL
4988 && info->callbacks->override_segment_assignment != NULL)
4989 new_segment
4990 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4991 last_hdr,
4992 new_segment);
4993
4994 if (! new_segment)
4995 {
4996 if ((hdr->flags & SEC_READONLY) == 0)
4997 writable = true;
4998 if ((hdr->flags & SEC_CODE) != 0)
4999 executable = true;
5000 last_hdr = hdr;
5001 /* .tbss sections effectively have zero size. */
5002 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5003 continue;
5004 }
5005
5006 /* We need a new program segment. We must create a new program
5007 header holding all the sections from hdr_index until hdr. */
5008
5009 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5010 if (m == NULL)
5011 goto error_return;
5012
5013 *pm = m;
5014 pm = &m->next;
5015
5016 if ((hdr->flags & SEC_READONLY) == 0)
5017 writable = true;
5018 else
5019 writable = false;
5020
5021 if ((hdr->flags & SEC_CODE) == 0)
5022 executable = false;
5023 else
5024 executable = true;
5025
5026 last_hdr = hdr;
5027 /* .tbss sections effectively have zero size. */
5028 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5029 hdr_index = i;
5030 phdr_in_segment = false;
5031 }
5032
5033 /* Create a final PT_LOAD program segment, but not if it's just
5034 for .tbss. */
5035 if (last_hdr != NULL
5036 && (i - hdr_index != 1
5037 || !IS_TBSS (last_hdr)))
5038 {
5039 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5040 if (m == NULL)
5041 goto error_return;
5042
5043 *pm = m;
5044 pm = &m->next;
5045 }
5046
5047 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5048 if (dynsec != NULL)
5049 {
5050 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5051 if (m == NULL)
5052 goto error_return;
5053 *pm = m;
5054 pm = &m->next;
5055 }
5056
5057 /* For each batch of consecutive loadable SHT_NOTE sections,
5058 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5059 because if we link together nonloadable .note sections and
5060 loadable .note sections, we will generate two .note sections
5061 in the output file. */
5062 for (s = abfd->sections; s != NULL; s = s->next)
5063 {
5064 if ((s->flags & SEC_LOAD) != 0
5065 && elf_section_type (s) == SHT_NOTE)
5066 {
5067 asection *s2;
5068 unsigned int alignment_power = s->alignment_power;
5069
5070 count = 1;
5071 for (s2 = s; s2->next != NULL; s2 = s2->next)
5072 {
5073 if (s2->next->alignment_power == alignment_power
5074 && (s2->next->flags & SEC_LOAD) != 0
5075 && elf_section_type (s2->next) == SHT_NOTE
5076 && align_power (s2->lma + s2->size / opb,
5077 alignment_power)
5078 == s2->next->lma)
5079 count++;
5080 else
5081 break;
5082 }
5083 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5084 amt += count * sizeof (asection *);
5085 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5086 if (m == NULL)
5087 goto error_return;
5088 m->next = NULL;
5089 m->p_type = PT_NOTE;
5090 m->count = count;
5091 while (count > 1)
5092 {
5093 m->sections[m->count - count--] = s;
5094 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5095 s = s->next;
5096 }
5097 m->sections[m->count - 1] = s;
5098 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5099 *pm = m;
5100 pm = &m->next;
5101 }
5102 if (s->flags & SEC_THREAD_LOCAL)
5103 {
5104 if (! tls_count)
5105 first_tls = s;
5106 tls_count++;
5107 }
5108 if (first_mbind == NULL
5109 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5110 first_mbind = s;
5111 }
5112
5113 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5114 if (tls_count > 0)
5115 {
5116 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5117 amt += tls_count * sizeof (asection *);
5118 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5119 if (m == NULL)
5120 goto error_return;
5121 m->next = NULL;
5122 m->p_type = PT_TLS;
5123 m->count = tls_count;
5124 /* Mandated PF_R. */
5125 m->p_flags = PF_R;
5126 m->p_flags_valid = 1;
5127 s = first_tls;
5128 for (i = 0; i < tls_count; ++i)
5129 {
5130 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5131 {
5132 _bfd_error_handler
5133 (_("%pB: TLS sections are not adjacent:"), abfd);
5134 s = first_tls;
5135 i = 0;
5136 while (i < tls_count)
5137 {
5138 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5139 {
5140 _bfd_error_handler (_(" TLS: %pA"), s);
5141 i++;
5142 }
5143 else
5144 _bfd_error_handler (_(" non-TLS: %pA"), s);
5145 s = s->next;
5146 }
5147 bfd_set_error (bfd_error_bad_value);
5148 goto error_return;
5149 }
5150 m->sections[i] = s;
5151 s = s->next;
5152 }
5153
5154 *pm = m;
5155 pm = &m->next;
5156 }
5157
5158 if (first_mbind
5159 && (abfd->flags & D_PAGED) != 0
5160 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5161 for (s = first_mbind; s != NULL; s = s->next)
5162 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5163 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5164 {
5165 /* Mandated PF_R. */
5166 unsigned long p_flags = PF_R;
5167 if ((s->flags & SEC_READONLY) == 0)
5168 p_flags |= PF_W;
5169 if ((s->flags & SEC_CODE) != 0)
5170 p_flags |= PF_X;
5171
5172 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5173 m = bfd_zalloc (abfd, amt);
5174 if (m == NULL)
5175 goto error_return;
5176 m->next = NULL;
5177 m->p_type = (PT_GNU_MBIND_LO
5178 + elf_section_data (s)->this_hdr.sh_info);
5179 m->count = 1;
5180 m->p_flags_valid = 1;
5181 m->sections[0] = s;
5182 m->p_flags = p_flags;
5183
5184 *pm = m;
5185 pm = &m->next;
5186 }
5187
5188 s = bfd_get_section_by_name (abfd,
5189 NOTE_GNU_PROPERTY_SECTION_NAME);
5190 if (s != NULL && s->size != 0)
5191 {
5192 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5193 m = bfd_zalloc (abfd, amt);
5194 if (m == NULL)
5195 goto error_return;
5196 m->next = NULL;
5197 m->p_type = PT_GNU_PROPERTY;
5198 m->count = 1;
5199 m->p_flags_valid = 1;
5200 m->sections[0] = s;
5201 m->p_flags = PF_R;
5202 *pm = m;
5203 pm = &m->next;
5204 }
5205
5206 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5207 segment. */
5208 eh_frame_hdr = elf_eh_frame_hdr (info);
5209 if (eh_frame_hdr != NULL
5210 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5211 {
5212 amt = sizeof (struct elf_segment_map);
5213 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5214 if (m == NULL)
5215 goto error_return;
5216 m->next = NULL;
5217 m->p_type = PT_GNU_EH_FRAME;
5218 m->count = 1;
5219 m->sections[0] = eh_frame_hdr->output_section;
5220
5221 *pm = m;
5222 pm = &m->next;
5223 }
5224
5225 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5226 segment. */
5227 sframe = elf_sframe (abfd);
5228 if (sframe != NULL
5229 && (sframe->output_section->flags & SEC_LOAD) != 0
5230 && sframe->size != 0)
5231 {
5232 amt = sizeof (struct elf_segment_map);
5233 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5234 if (m == NULL)
5235 goto error_return;
5236 m->next = NULL;
5237 m->p_type = PT_GNU_SFRAME;
5238 m->count = 1;
5239 m->sections[0] = sframe->output_section;
5240
5241 *pm = m;
5242 pm = &m->next;
5243 }
5244
5245 if (elf_stack_flags (abfd))
5246 {
5247 amt = sizeof (struct elf_segment_map);
5248 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5249 if (m == NULL)
5250 goto error_return;
5251 m->next = NULL;
5252 m->p_type = PT_GNU_STACK;
5253 m->p_flags = elf_stack_flags (abfd);
5254 m->p_align = bed->stack_align;
5255 m->p_flags_valid = 1;
5256 m->p_align_valid = m->p_align != 0;
5257 if (info->stacksize > 0)
5258 {
5259 m->p_size = info->stacksize;
5260 m->p_size_valid = 1;
5261 }
5262
5263 *pm = m;
5264 pm = &m->next;
5265 }
5266
5267 if (info != NULL && info->relro)
5268 {
5269 for (m = mfirst; m != NULL; m = m->next)
5270 {
5271 if (m->p_type == PT_LOAD
5272 && m->count != 0
5273 && m->sections[0]->vma >= info->relro_start
5274 && m->sections[0]->vma < info->relro_end)
5275 {
5276 i = m->count;
5277 while (--i != (unsigned) -1)
5278 {
5279 if (m->sections[i]->size > 0
5280 && (m->sections[i]->flags & SEC_LOAD) != 0
5281 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5282 break;
5283 }
5284
5285 if (i != (unsigned) -1)
5286 break;
5287 }
5288 }
5289
5290 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5291 if (m != NULL)
5292 {
5293 amt = sizeof (struct elf_segment_map);
5294 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5295 if (m == NULL)
5296 goto error_return;
5297 m->next = NULL;
5298 m->p_type = PT_GNU_RELRO;
5299 *pm = m;
5300 pm = &m->next;
5301 }
5302 }
5303
5304 free (sections);
5305 elf_seg_map (abfd) = mfirst;
5306 }
5307
5308 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5309 return false;
5310
5311 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5312 ++count;
5313 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5314
5315 return true;
5316
5317 error_return:
5318 free (sections);
5319 return false;
5320 }
5321
5322 /* Sort sections by address. */
5323
5324 static int
5325 elf_sort_sections (const void *arg1, const void *arg2)
5326 {
5327 const asection *sec1 = *(const asection **) arg1;
5328 const asection *sec2 = *(const asection **) arg2;
5329 bfd_size_type size1, size2;
5330
5331 /* Sort by LMA first, since this is the address used to
5332 place the section into a segment. */
5333 if (sec1->lma < sec2->lma)
5334 return -1;
5335 else if (sec1->lma > sec2->lma)
5336 return 1;
5337
5338 /* Then sort by VMA. Normally the LMA and the VMA will be
5339 the same, and this will do nothing. */
5340 if (sec1->vma < sec2->vma)
5341 return -1;
5342 else if (sec1->vma > sec2->vma)
5343 return 1;
5344
5345 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5346
5347 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5348 && (x)->size != 0)
5349
5350 if (TOEND (sec1))
5351 {
5352 if (!TOEND (sec2))
5353 return 1;
5354 }
5355 else if (TOEND (sec2))
5356 return -1;
5357
5358 #undef TOEND
5359
5360 /* Sort by size, to put zero sized sections
5361 before others at the same address. */
5362
5363 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5364 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5365
5366 if (size1 < size2)
5367 return -1;
5368 if (size1 > size2)
5369 return 1;
5370
5371 return sec1->target_index - sec2->target_index;
5372 }
5373
5374 /* This qsort comparison functions sorts PT_LOAD segments first and
5375 by p_paddr, for assign_file_positions_for_load_sections. */
5376
5377 static int
5378 elf_sort_segments (const void *arg1, const void *arg2)
5379 {
5380 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5381 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5382
5383 if (m1->p_type != m2->p_type)
5384 {
5385 if (m1->p_type == PT_NULL)
5386 return 1;
5387 if (m2->p_type == PT_NULL)
5388 return -1;
5389 return m1->p_type < m2->p_type ? -1 : 1;
5390 }
5391 if (m1->includes_filehdr != m2->includes_filehdr)
5392 return m1->includes_filehdr ? -1 : 1;
5393 if (m1->no_sort_lma != m2->no_sort_lma)
5394 return m1->no_sort_lma ? -1 : 1;
5395 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5396 {
5397 bfd_vma lma1, lma2; /* Octets. */
5398 lma1 = 0;
5399 if (m1->p_paddr_valid)
5400 lma1 = m1->p_paddr;
5401 else if (m1->count != 0)
5402 {
5403 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5404 m1->sections[0]);
5405 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5406 }
5407 lma2 = 0;
5408 if (m2->p_paddr_valid)
5409 lma2 = m2->p_paddr;
5410 else if (m2->count != 0)
5411 {
5412 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5413 m2->sections[0]);
5414 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5415 }
5416 if (lma1 != lma2)
5417 return lma1 < lma2 ? -1 : 1;
5418 }
5419 if (m1->idx != m2->idx)
5420 return m1->idx < m2->idx ? -1 : 1;
5421 return 0;
5422 }
5423
5424 /* Ian Lance Taylor writes:
5425
5426 We shouldn't be using % with a negative signed number. That's just
5427 not good. We have to make sure either that the number is not
5428 negative, or that the number has an unsigned type. When the types
5429 are all the same size they wind up as unsigned. When file_ptr is a
5430 larger signed type, the arithmetic winds up as signed long long,
5431 which is wrong.
5432
5433 What we're trying to say here is something like ``increase OFF by
5434 the least amount that will cause it to be equal to the VMA modulo
5435 the page size.'' */
5436 /* In other words, something like:
5437
5438 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5439 off_offset = off % bed->maxpagesize;
5440 if (vma_offset < off_offset)
5441 adjustment = vma_offset + bed->maxpagesize - off_offset;
5442 else
5443 adjustment = vma_offset - off_offset;
5444
5445 which can be collapsed into the expression below. */
5446
5447 static file_ptr
5448 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5449 {
5450 /* PR binutils/16199: Handle an alignment of zero. */
5451 if (maxpagesize == 0)
5452 maxpagesize = 1;
5453 return ((vma - off) % maxpagesize);
5454 }
5455
5456 static void
5457 print_segment_map (const struct elf_segment_map *m)
5458 {
5459 unsigned int j;
5460 const char *pt = get_segment_type (m->p_type);
5461 char buf[32];
5462
5463 if (pt == NULL)
5464 {
5465 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5466 sprintf (buf, "LOPROC+%7.7x",
5467 (unsigned int) (m->p_type - PT_LOPROC));
5468 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5469 sprintf (buf, "LOOS+%7.7x",
5470 (unsigned int) (m->p_type - PT_LOOS));
5471 else
5472 snprintf (buf, sizeof (buf), "%8.8x",
5473 (unsigned int) m->p_type);
5474 pt = buf;
5475 }
5476 fflush (stdout);
5477 fprintf (stderr, "%s:", pt);
5478 for (j = 0; j < m->count; j++)
5479 fprintf (stderr, " %s", m->sections [j]->name);
5480 putc ('\n',stderr);
5481 fflush (stderr);
5482 }
5483
5484 static bool
5485 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5486 {
5487 void *buf;
5488 bool ret;
5489
5490 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5491 return false;
5492 buf = bfd_zmalloc (len);
5493 if (buf == NULL)
5494 return false;
5495 ret = bfd_bwrite (buf, len, abfd) == len;
5496 free (buf);
5497 return ret;
5498 }
5499
5500 /* Assign file positions to the sections based on the mapping from
5501 sections to segments. This function also sets up some fields in
5502 the file header. */
5503
5504 static bool
5505 assign_file_positions_for_load_sections (bfd *abfd,
5506 struct bfd_link_info *link_info)
5507 {
5508 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5509 struct elf_segment_map *m;
5510 struct elf_segment_map *phdr_load_seg;
5511 Elf_Internal_Phdr *phdrs;
5512 Elf_Internal_Phdr *p;
5513 file_ptr off; /* Octets. */
5514 bfd_size_type maxpagesize;
5515 unsigned int alloc, actual;
5516 unsigned int i, j;
5517 struct elf_segment_map **sorted_seg_map;
5518 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5519
5520 if (link_info == NULL
5521 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5522 return false;
5523
5524 alloc = 0;
5525 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5526 m->idx = alloc++;
5527
5528 if (alloc)
5529 {
5530 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5531 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5532 }
5533 else
5534 {
5535 /* PR binutils/12467. */
5536 elf_elfheader (abfd)->e_phoff = 0;
5537 elf_elfheader (abfd)->e_phentsize = 0;
5538 }
5539
5540 elf_elfheader (abfd)->e_phnum = alloc;
5541
5542 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5543 {
5544 actual = alloc;
5545 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5546 }
5547 else
5548 {
5549 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5550 BFD_ASSERT (elf_program_header_size (abfd)
5551 == actual * bed->s->sizeof_phdr);
5552 BFD_ASSERT (actual >= alloc);
5553 }
5554
5555 if (alloc == 0)
5556 {
5557 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5558 return true;
5559 }
5560
5561 /* We're writing the size in elf_program_header_size (abfd),
5562 see assign_file_positions_except_relocs, so make sure we have
5563 that amount allocated, with trailing space cleared.
5564 The variable alloc contains the computed need, while
5565 elf_program_header_size (abfd) contains the size used for the
5566 layout.
5567 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5568 where the layout is forced to according to a larger size in the
5569 last iterations for the testcase ld-elf/header. */
5570 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5571 + alloc * sizeof (*sorted_seg_map)));
5572 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5573 elf_tdata (abfd)->phdr = phdrs;
5574 if (phdrs == NULL)
5575 return false;
5576
5577 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5578 {
5579 sorted_seg_map[j] = m;
5580 /* If elf_segment_map is not from map_sections_to_segments, the
5581 sections may not be correctly ordered. NOTE: sorting should
5582 not be done to the PT_NOTE section of a corefile, which may
5583 contain several pseudo-sections artificially created by bfd.
5584 Sorting these pseudo-sections breaks things badly. */
5585 if (m->count > 1
5586 && !(elf_elfheader (abfd)->e_type == ET_CORE
5587 && m->p_type == PT_NOTE))
5588 {
5589 for (i = 0; i < m->count; i++)
5590 m->sections[i]->target_index = i;
5591 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5592 elf_sort_sections);
5593 }
5594 }
5595 if (alloc > 1)
5596 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5597 elf_sort_segments);
5598
5599 maxpagesize = 1;
5600 if ((abfd->flags & D_PAGED) != 0)
5601 {
5602 if (link_info != NULL)
5603 maxpagesize = link_info->maxpagesize;
5604 else
5605 maxpagesize = bed->maxpagesize;
5606 }
5607
5608 /* Sections must map to file offsets past the ELF file header. */
5609 off = bed->s->sizeof_ehdr;
5610 /* And if one of the PT_LOAD headers doesn't include the program
5611 headers then we'll be mapping program headers in the usual
5612 position after the ELF file header. */
5613 phdr_load_seg = NULL;
5614 for (j = 0; j < alloc; j++)
5615 {
5616 m = sorted_seg_map[j];
5617 if (m->p_type != PT_LOAD)
5618 break;
5619 if (m->includes_phdrs)
5620 {
5621 phdr_load_seg = m;
5622 break;
5623 }
5624 }
5625 if (phdr_load_seg == NULL)
5626 off += actual * bed->s->sizeof_phdr;
5627
5628 for (j = 0; j < alloc; j++)
5629 {
5630 asection **secpp;
5631 bfd_vma off_adjust; /* Octets. */
5632 bool no_contents;
5633 bfd_size_type p_align;
5634 bool p_align_p;
5635
5636 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5637 number of sections with contents contributing to both p_filesz
5638 and p_memsz, followed by a number of sections with no contents
5639 that just contribute to p_memsz. In this loop, OFF tracks next
5640 available file offset for PT_LOAD and PT_NOTE segments. */
5641 m = sorted_seg_map[j];
5642 p = phdrs + m->idx;
5643 p->p_type = m->p_type;
5644 p->p_flags = m->p_flags;
5645 p_align = bed->p_align;
5646 p_align_p = false;
5647
5648 if (m->count == 0)
5649 p->p_vaddr = m->p_vaddr_offset * opb;
5650 else
5651 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5652
5653 if (m->p_paddr_valid)
5654 p->p_paddr = m->p_paddr;
5655 else if (m->count == 0)
5656 p->p_paddr = 0;
5657 else
5658 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5659
5660 if (p->p_type == PT_LOAD
5661 && (abfd->flags & D_PAGED) != 0)
5662 {
5663 /* p_align in demand paged PT_LOAD segments effectively stores
5664 the maximum page size. When copying an executable with
5665 objcopy, we set m->p_align from the input file. Use this
5666 value for maxpagesize rather than bed->maxpagesize, which
5667 may be different. Note that we use maxpagesize for PT_TLS
5668 segment alignment later in this function, so we are relying
5669 on at least one PT_LOAD segment appearing before a PT_TLS
5670 segment. */
5671 if (m->p_align_valid)
5672 maxpagesize = m->p_align;
5673 else if (p_align != 0
5674 && (link_info == NULL
5675 || !link_info->maxpagesize_is_set))
5676 /* Set p_align to the default p_align value while laying
5677 out segments aligning to the maximum page size or the
5678 largest section alignment. The run-time loader can
5679 align segments to the default p_align value or the
5680 maximum page size, depending on system page size. */
5681 p_align_p = true;
5682
5683 p->p_align = maxpagesize;
5684 }
5685 else if (m->p_align_valid)
5686 p->p_align = m->p_align;
5687 else if (m->count == 0)
5688 p->p_align = 1 << bed->s->log_file_align;
5689
5690 if (m == phdr_load_seg)
5691 {
5692 if (!m->includes_filehdr)
5693 p->p_offset = off;
5694 off += actual * bed->s->sizeof_phdr;
5695 }
5696
5697 no_contents = false;
5698 off_adjust = 0;
5699 if (p->p_type == PT_LOAD
5700 && m->count > 0)
5701 {
5702 bfd_size_type align; /* Bytes. */
5703 unsigned int align_power = 0;
5704
5705 if (m->p_align_valid)
5706 align = p->p_align;
5707 else
5708 {
5709 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5710 {
5711 unsigned int secalign;
5712
5713 secalign = bfd_section_alignment (*secpp);
5714 if (secalign > align_power)
5715 align_power = secalign;
5716 }
5717 align = (bfd_size_type) 1 << align_power;
5718 if (align < maxpagesize)
5719 {
5720 /* If a section requires alignment higher than the
5721 default p_align value, don't set p_align to the
5722 default p_align value. */
5723 if (align > p_align)
5724 p_align_p = false;
5725 align = maxpagesize;
5726 }
5727 else
5728 {
5729 /* If a section requires alignment higher than the
5730 maximum page size, set p_align to the section
5731 alignment. */
5732 p_align_p = true;
5733 p_align = align;
5734 }
5735 }
5736
5737 for (i = 0; i < m->count; i++)
5738 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5739 /* If we aren't making room for this section, then
5740 it must be SHT_NOBITS regardless of what we've
5741 set via struct bfd_elf_special_section. */
5742 elf_section_type (m->sections[i]) = SHT_NOBITS;
5743
5744 /* Find out whether this segment contains any loadable
5745 sections. */
5746 no_contents = true;
5747 for (i = 0; i < m->count; i++)
5748 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5749 {
5750 no_contents = false;
5751 break;
5752 }
5753
5754 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5755
5756 /* Broken hardware and/or kernel require that files do not
5757 map the same page with different permissions on some hppa
5758 processors. */
5759 if (j != 0
5760 && (abfd->flags & D_PAGED) != 0
5761 && bed->no_page_alias
5762 && (off & (maxpagesize - 1)) != 0
5763 && ((off & -maxpagesize)
5764 == ((off + off_adjust) & -maxpagesize)))
5765 off_adjust += maxpagesize;
5766 off += off_adjust;
5767 if (no_contents)
5768 {
5769 /* We shouldn't need to align the segment on disk since
5770 the segment doesn't need file space, but the gABI
5771 arguably requires the alignment and glibc ld.so
5772 checks it. So to comply with the alignment
5773 requirement but not waste file space, we adjust
5774 p_offset for just this segment. (OFF_ADJUST is
5775 subtracted from OFF later.) This may put p_offset
5776 past the end of file, but that shouldn't matter. */
5777 }
5778 else
5779 off_adjust = 0;
5780 }
5781 /* Make sure the .dynamic section is the first section in the
5782 PT_DYNAMIC segment. */
5783 else if (p->p_type == PT_DYNAMIC
5784 && m->count > 1
5785 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5786 {
5787 _bfd_error_handler
5788 (_("%pB: The first section in the PT_DYNAMIC segment"
5789 " is not the .dynamic section"),
5790 abfd);
5791 bfd_set_error (bfd_error_bad_value);
5792 return false;
5793 }
5794 /* Set the note section type to SHT_NOTE. */
5795 else if (p->p_type == PT_NOTE)
5796 for (i = 0; i < m->count; i++)
5797 elf_section_type (m->sections[i]) = SHT_NOTE;
5798
5799 if (m->includes_filehdr)
5800 {
5801 if (!m->p_flags_valid)
5802 p->p_flags |= PF_R;
5803 p->p_filesz = bed->s->sizeof_ehdr;
5804 p->p_memsz = bed->s->sizeof_ehdr;
5805 if (p->p_type == PT_LOAD)
5806 {
5807 if (m->count > 0)
5808 {
5809 if (p->p_vaddr < (bfd_vma) off
5810 || (!m->p_paddr_valid
5811 && p->p_paddr < (bfd_vma) off))
5812 {
5813 _bfd_error_handler
5814 (_("%pB: not enough room for program headers,"
5815 " try linking with -N"),
5816 abfd);
5817 bfd_set_error (bfd_error_bad_value);
5818 return false;
5819 }
5820 p->p_vaddr -= off;
5821 if (!m->p_paddr_valid)
5822 p->p_paddr -= off;
5823 }
5824 }
5825 else if (sorted_seg_map[0]->includes_filehdr)
5826 {
5827 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5828 p->p_vaddr = filehdr->p_vaddr;
5829 if (!m->p_paddr_valid)
5830 p->p_paddr = filehdr->p_paddr;
5831 }
5832 }
5833
5834 if (m->includes_phdrs)
5835 {
5836 if (!m->p_flags_valid)
5837 p->p_flags |= PF_R;
5838 p->p_filesz += actual * bed->s->sizeof_phdr;
5839 p->p_memsz += actual * bed->s->sizeof_phdr;
5840 if (!m->includes_filehdr)
5841 {
5842 if (p->p_type == PT_LOAD)
5843 {
5844 elf_elfheader (abfd)->e_phoff = p->p_offset;
5845 if (m->count > 0)
5846 {
5847 p->p_vaddr -= off - p->p_offset;
5848 if (!m->p_paddr_valid)
5849 p->p_paddr -= off - p->p_offset;
5850 }
5851 }
5852 else if (phdr_load_seg != NULL)
5853 {
5854 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5855 bfd_vma phdr_off = 0; /* Octets. */
5856 if (phdr_load_seg->includes_filehdr)
5857 phdr_off = bed->s->sizeof_ehdr;
5858 p->p_vaddr = phdr->p_vaddr + phdr_off;
5859 if (!m->p_paddr_valid)
5860 p->p_paddr = phdr->p_paddr + phdr_off;
5861 p->p_offset = phdr->p_offset + phdr_off;
5862 }
5863 else
5864 p->p_offset = bed->s->sizeof_ehdr;
5865 }
5866 }
5867
5868 if (p->p_type == PT_LOAD
5869 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5870 {
5871 if (!m->includes_filehdr && !m->includes_phdrs)
5872 {
5873 p->p_offset = off;
5874 if (no_contents)
5875 {
5876 /* Put meaningless p_offset for PT_LOAD segments
5877 without file contents somewhere within the first
5878 page, in an attempt to not point past EOF. */
5879 bfd_size_type align = maxpagesize;
5880 if (align < p->p_align)
5881 align = p->p_align;
5882 if (align < 1)
5883 align = 1;
5884 p->p_offset = off % align;
5885 }
5886 }
5887 else
5888 {
5889 file_ptr adjust; /* Octets. */
5890
5891 adjust = off - (p->p_offset + p->p_filesz);
5892 if (!no_contents)
5893 p->p_filesz += adjust;
5894 p->p_memsz += adjust;
5895 }
5896 }
5897
5898 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5899 maps. Set filepos for sections in PT_LOAD segments, and in
5900 core files, for sections in PT_NOTE segments.
5901 assign_file_positions_for_non_load_sections will set filepos
5902 for other sections and update p_filesz for other segments. */
5903 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5904 {
5905 asection *sec;
5906 bfd_size_type align;
5907 Elf_Internal_Shdr *this_hdr;
5908
5909 sec = *secpp;
5910 this_hdr = &elf_section_data (sec)->this_hdr;
5911 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5912
5913 if ((p->p_type == PT_LOAD
5914 || p->p_type == PT_TLS)
5915 && (this_hdr->sh_type != SHT_NOBITS
5916 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5917 && ((this_hdr->sh_flags & SHF_TLS) == 0
5918 || p->p_type == PT_TLS))))
5919 {
5920 bfd_vma p_start = p->p_paddr; /* Octets. */
5921 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5922 bfd_vma s_start = sec->lma * opb; /* Octets. */
5923 bfd_vma adjust = s_start - p_end; /* Octets. */
5924
5925 if (adjust != 0
5926 && (s_start < p_end
5927 || p_end < p_start))
5928 {
5929 _bfd_error_handler
5930 /* xgettext:c-format */
5931 (_("%pB: section %pA lma %#" PRIx64
5932 " adjusted to %#" PRIx64),
5933 abfd, sec, (uint64_t) s_start / opb,
5934 (uint64_t) p_end / opb);
5935 adjust = 0;
5936 sec->lma = p_end / opb;
5937 }
5938 p->p_memsz += adjust;
5939
5940 if (p->p_type == PT_LOAD)
5941 {
5942 if (this_hdr->sh_type != SHT_NOBITS)
5943 {
5944 off_adjust = 0;
5945 if (p->p_filesz + adjust < p->p_memsz)
5946 {
5947 /* We have a PROGBITS section following NOBITS ones.
5948 Allocate file space for the NOBITS section(s) and
5949 zero it. */
5950 adjust = p->p_memsz - p->p_filesz;
5951 if (!write_zeros (abfd, off, adjust))
5952 return false;
5953 }
5954 }
5955 /* We only adjust sh_offset in SHT_NOBITS sections
5956 as would seem proper for their address when the
5957 section is first in the segment. sh_offset
5958 doesn't really have any significance for
5959 SHT_NOBITS anyway, apart from a notional position
5960 relative to other sections. Historically we
5961 didn't bother with adjusting sh_offset and some
5962 programs depend on it not being adjusted. See
5963 pr12921 and pr25662. */
5964 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5965 {
5966 off += adjust;
5967 if (this_hdr->sh_type == SHT_NOBITS)
5968 off_adjust += adjust;
5969 }
5970 }
5971 if (this_hdr->sh_type != SHT_NOBITS)
5972 p->p_filesz += adjust;
5973 }
5974
5975 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5976 {
5977 /* The section at i == 0 is the one that actually contains
5978 everything. */
5979 if (i == 0)
5980 {
5981 this_hdr->sh_offset = sec->filepos = off;
5982 off += this_hdr->sh_size;
5983 p->p_filesz = this_hdr->sh_size;
5984 p->p_memsz = 0;
5985 p->p_align = 1;
5986 }
5987 else
5988 {
5989 /* The rest are fake sections that shouldn't be written. */
5990 sec->filepos = 0;
5991 sec->size = 0;
5992 sec->flags = 0;
5993 continue;
5994 }
5995 }
5996 else
5997 {
5998 if (p->p_type == PT_LOAD)
5999 {
6000 this_hdr->sh_offset = sec->filepos = off;
6001 if (this_hdr->sh_type != SHT_NOBITS)
6002 off += this_hdr->sh_size;
6003 }
6004 else if (this_hdr->sh_type == SHT_NOBITS
6005 && (this_hdr->sh_flags & SHF_TLS) != 0
6006 && this_hdr->sh_offset == 0)
6007 {
6008 /* This is a .tbss section that didn't get a PT_LOAD.
6009 (See _bfd_elf_map_sections_to_segments "Create a
6010 final PT_LOAD".) Set sh_offset to the value it
6011 would have if we had created a zero p_filesz and
6012 p_memsz PT_LOAD header for the section. This
6013 also makes the PT_TLS header have the same
6014 p_offset value. */
6015 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6016 off, align);
6017 this_hdr->sh_offset = sec->filepos = off + adjust;
6018 }
6019
6020 if (this_hdr->sh_type != SHT_NOBITS)
6021 {
6022 p->p_filesz += this_hdr->sh_size;
6023 /* A load section without SHF_ALLOC is something like
6024 a note section in a PT_NOTE segment. These take
6025 file space but are not loaded into memory. */
6026 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6027 p->p_memsz += this_hdr->sh_size;
6028 }
6029 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6030 {
6031 if (p->p_type == PT_TLS)
6032 p->p_memsz += this_hdr->sh_size;
6033
6034 /* .tbss is special. It doesn't contribute to p_memsz of
6035 normal segments. */
6036 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6037 p->p_memsz += this_hdr->sh_size;
6038 }
6039
6040 if (align > p->p_align
6041 && !m->p_align_valid
6042 && (p->p_type != PT_LOAD
6043 || (abfd->flags & D_PAGED) == 0))
6044 p->p_align = align;
6045 }
6046
6047 if (!m->p_flags_valid)
6048 {
6049 p->p_flags |= PF_R;
6050 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6051 p->p_flags |= PF_X;
6052 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6053 p->p_flags |= PF_W;
6054 }
6055 }
6056
6057 off -= off_adjust;
6058
6059 /* PR ld/20815 - Check that the program header segment, if
6060 present, will be loaded into memory. */
6061 if (p->p_type == PT_PHDR
6062 && phdr_load_seg == NULL
6063 && !(bed->elf_backend_allow_non_load_phdr != NULL
6064 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6065 {
6066 /* The fix for this error is usually to edit the linker script being
6067 used and set up the program headers manually. Either that or
6068 leave room for the headers at the start of the SECTIONS. */
6069 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6070 " by LOAD segment"),
6071 abfd);
6072 if (link_info == NULL)
6073 return false;
6074 /* Arrange for the linker to exit with an error, deleting
6075 the output file unless --noinhibit-exec is given. */
6076 link_info->callbacks->info ("%X");
6077 }
6078
6079 /* Check that all sections are in a PT_LOAD segment.
6080 Don't check funky gdb generated core files. */
6081 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6082 {
6083 bool check_vma = true;
6084
6085 for (i = 1; i < m->count; i++)
6086 if (m->sections[i]->vma == m->sections[i - 1]->vma
6087 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6088 ->this_hdr), p) != 0
6089 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6090 ->this_hdr), p) != 0)
6091 {
6092 /* Looks like we have overlays packed into the segment. */
6093 check_vma = false;
6094 break;
6095 }
6096
6097 for (i = 0; i < m->count; i++)
6098 {
6099 Elf_Internal_Shdr *this_hdr;
6100 asection *sec;
6101
6102 sec = m->sections[i];
6103 this_hdr = &(elf_section_data(sec)->this_hdr);
6104 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6105 && !ELF_TBSS_SPECIAL (this_hdr, p))
6106 {
6107 _bfd_error_handler
6108 /* xgettext:c-format */
6109 (_("%pB: section `%pA' can't be allocated in segment %d"),
6110 abfd, sec, j);
6111 print_segment_map (m);
6112 }
6113 }
6114
6115 if (p_align_p)
6116 p->p_align = p_align;
6117 }
6118 }
6119
6120 elf_next_file_pos (abfd) = off;
6121
6122 if (link_info != NULL
6123 && phdr_load_seg != NULL
6124 && phdr_load_seg->includes_filehdr)
6125 {
6126 /* There is a segment that contains both the file headers and the
6127 program headers, so provide a symbol __ehdr_start pointing there.
6128 A program can use this to examine itself robustly. */
6129
6130 struct elf_link_hash_entry *hash
6131 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6132 false, false, true);
6133 /* If the symbol was referenced and not defined, define it. */
6134 if (hash != NULL
6135 && (hash->root.type == bfd_link_hash_new
6136 || hash->root.type == bfd_link_hash_undefined
6137 || hash->root.type == bfd_link_hash_undefweak
6138 || hash->root.type == bfd_link_hash_common))
6139 {
6140 asection *s = NULL;
6141 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6142
6143 if (phdr_load_seg->count != 0)
6144 /* The segment contains sections, so use the first one. */
6145 s = phdr_load_seg->sections[0];
6146 else
6147 /* Use the first (i.e. lowest-addressed) section in any segment. */
6148 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6149 if (m->p_type == PT_LOAD && m->count != 0)
6150 {
6151 s = m->sections[0];
6152 break;
6153 }
6154
6155 if (s != NULL)
6156 {
6157 hash->root.u.def.value = filehdr_vaddr - s->vma;
6158 hash->root.u.def.section = s;
6159 }
6160 else
6161 {
6162 hash->root.u.def.value = filehdr_vaddr;
6163 hash->root.u.def.section = bfd_abs_section_ptr;
6164 }
6165
6166 hash->root.type = bfd_link_hash_defined;
6167 hash->def_regular = 1;
6168 hash->non_elf = 0;
6169 }
6170 }
6171
6172 return true;
6173 }
6174
6175 /* Determine if a bfd is a debuginfo file. Unfortunately there
6176 is no defined method for detecting such files, so we have to
6177 use heuristics instead. */
6178
6179 bool
6180 is_debuginfo_file (bfd *abfd)
6181 {
6182 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6183 return false;
6184
6185 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6186 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6187 Elf_Internal_Shdr **headerp;
6188
6189 for (headerp = start_headers; headerp < end_headers; headerp ++)
6190 {
6191 Elf_Internal_Shdr *header = * headerp;
6192
6193 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6194 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6195 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6196 && header->sh_type != SHT_NOBITS
6197 && header->sh_type != SHT_NOTE)
6198 return false;
6199 }
6200
6201 return true;
6202 }
6203
6204 /* Assign file positions for other sections, except for compressed debug
6205 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6206
6207 static bool
6208 assign_file_positions_for_non_load_sections (bfd *abfd,
6209 struct bfd_link_info *link_info)
6210 {
6211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6212 Elf_Internal_Shdr **i_shdrpp;
6213 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6214 Elf_Internal_Phdr *phdrs;
6215 Elf_Internal_Phdr *p;
6216 struct elf_segment_map *m;
6217 file_ptr off;
6218 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6219 bfd_vma maxpagesize;
6220
6221 if (link_info != NULL)
6222 maxpagesize = link_info->maxpagesize;
6223 else
6224 maxpagesize = bed->maxpagesize;
6225 i_shdrpp = elf_elfsections (abfd);
6226 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6227 off = elf_next_file_pos (abfd);
6228 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6229 {
6230 Elf_Internal_Shdr *hdr;
6231 bfd_vma align;
6232
6233 hdr = *hdrpp;
6234 if (hdr->bfd_section != NULL
6235 && (hdr->bfd_section->filepos != 0
6236 || (hdr->sh_type == SHT_NOBITS
6237 && hdr->contents == NULL)))
6238 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6239 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6240 {
6241 if (hdr->sh_size != 0
6242 /* PR 24717 - debuginfo files are known to be not strictly
6243 compliant with the ELF standard. In particular they often
6244 have .note.gnu.property sections that are outside of any
6245 loadable segment. This is not a problem for such files,
6246 so do not warn about them. */
6247 && ! is_debuginfo_file (abfd))
6248 _bfd_error_handler
6249 /* xgettext:c-format */
6250 (_("%pB: warning: allocated section `%s' not in segment"),
6251 abfd,
6252 (hdr->bfd_section == NULL
6253 ? "*unknown*"
6254 : hdr->bfd_section->name));
6255 /* We don't need to page align empty sections. */
6256 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6257 align = maxpagesize;
6258 else
6259 align = hdr->sh_addralign & -hdr->sh_addralign;
6260 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6261 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6262 false);
6263 }
6264 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6265 && hdr->bfd_section == NULL)
6266 /* We don't know the offset of these sections yet:
6267 their size has not been decided. */
6268 || (hdr->bfd_section != NULL
6269 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6270 || (bfd_section_is_ctf (hdr->bfd_section)
6271 && abfd->is_linker_output)))
6272 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6273 || (elf_symtab_shndx_list (abfd) != NULL
6274 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6275 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6276 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6277 hdr->sh_offset = -1;
6278 else
6279 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6280 }
6281 elf_next_file_pos (abfd) = off;
6282
6283 /* Now that we have set the section file positions, we can set up
6284 the file positions for the non PT_LOAD segments. */
6285 phdrs = elf_tdata (abfd)->phdr;
6286 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6287 {
6288 if (p->p_type == PT_GNU_RELRO)
6289 {
6290 bfd_vma start, end; /* Bytes. */
6291 bool ok;
6292
6293 if (link_info != NULL)
6294 {
6295 /* During linking the range of the RELRO segment is passed
6296 in link_info. Note that there may be padding between
6297 relro_start and the first RELRO section. */
6298 start = link_info->relro_start;
6299 end = link_info->relro_end;
6300 }
6301 else if (m->count != 0)
6302 {
6303 if (!m->p_size_valid)
6304 abort ();
6305 start = m->sections[0]->vma;
6306 end = start + m->p_size / opb;
6307 }
6308 else
6309 {
6310 start = 0;
6311 end = 0;
6312 }
6313
6314 ok = false;
6315 if (start < end)
6316 {
6317 struct elf_segment_map *lm;
6318 const Elf_Internal_Phdr *lp;
6319 unsigned int i;
6320
6321 /* Find a LOAD segment containing a section in the RELRO
6322 segment. */
6323 for (lm = elf_seg_map (abfd), lp = phdrs;
6324 lm != NULL;
6325 lm = lm->next, lp++)
6326 {
6327 if (lp->p_type == PT_LOAD
6328 && lm->count != 0
6329 && (lm->sections[lm->count - 1]->vma
6330 + (!IS_TBSS (lm->sections[lm->count - 1])
6331 ? lm->sections[lm->count - 1]->size / opb
6332 : 0)) > start
6333 && lm->sections[0]->vma < end)
6334 break;
6335 }
6336
6337 if (lm != NULL)
6338 {
6339 /* Find the section starting the RELRO segment. */
6340 for (i = 0; i < lm->count; i++)
6341 {
6342 asection *s = lm->sections[i];
6343 if (s->vma >= start
6344 && s->vma < end
6345 && s->size != 0)
6346 break;
6347 }
6348
6349 if (i < lm->count)
6350 {
6351 p->p_vaddr = lm->sections[i]->vma * opb;
6352 p->p_paddr = lm->sections[i]->lma * opb;
6353 p->p_offset = lm->sections[i]->filepos;
6354 p->p_memsz = end * opb - p->p_vaddr;
6355 p->p_filesz = p->p_memsz;
6356
6357 /* The RELRO segment typically ends a few bytes
6358 into .got.plt but other layouts are possible.
6359 In cases where the end does not match any
6360 loaded section (for instance is in file
6361 padding), trim p_filesz back to correspond to
6362 the end of loaded section contents. */
6363 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6364 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6365
6366 /* Preserve the alignment and flags if they are
6367 valid. The gold linker generates RW/4 for
6368 the PT_GNU_RELRO section. It is better for
6369 objcopy/strip to honor these attributes
6370 otherwise gdb will choke when using separate
6371 debug files. */
6372 if (!m->p_align_valid)
6373 p->p_align = 1;
6374 if (!m->p_flags_valid)
6375 p->p_flags = PF_R;
6376 ok = true;
6377 }
6378 }
6379 }
6380
6381 if (!ok)
6382 {
6383 if (link_info != NULL)
6384 _bfd_error_handler
6385 (_("%pB: warning: unable to allocate any sections"
6386 " to PT_GNU_RELRO segment"),
6387 abfd);
6388 memset (p, 0, sizeof *p);
6389 }
6390 }
6391 else if (p->p_type == PT_GNU_STACK)
6392 {
6393 if (m->p_size_valid)
6394 p->p_memsz = m->p_size;
6395 }
6396 else if (m->count != 0)
6397 {
6398 unsigned int i;
6399
6400 if (p->p_type != PT_LOAD
6401 && (p->p_type != PT_NOTE
6402 || bfd_get_format (abfd) != bfd_core))
6403 {
6404 /* A user specified segment layout may include a PHDR
6405 segment that overlaps with a LOAD segment... */
6406 if (p->p_type == PT_PHDR)
6407 {
6408 m->count = 0;
6409 continue;
6410 }
6411
6412 if (m->includes_filehdr || m->includes_phdrs)
6413 {
6414 /* PR 17512: file: 2195325e. */
6415 _bfd_error_handler
6416 (_("%pB: error: non-load segment %d includes file header "
6417 "and/or program header"),
6418 abfd, (int) (p - phdrs));
6419 return false;
6420 }
6421
6422 p->p_filesz = 0;
6423 p->p_offset = m->sections[0]->filepos;
6424 for (i = m->count; i-- != 0;)
6425 {
6426 asection *sect = m->sections[i];
6427 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6428 if (hdr->sh_type != SHT_NOBITS)
6429 {
6430 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6431 /* NB: p_memsz of the loadable PT_NOTE segment
6432 should be the same as p_filesz. */
6433 if (p->p_type == PT_NOTE
6434 && (hdr->sh_flags & SHF_ALLOC) != 0)
6435 p->p_memsz = p->p_filesz;
6436 break;
6437 }
6438 }
6439 }
6440 }
6441 }
6442
6443 return true;
6444 }
6445
6446 static elf_section_list *
6447 find_section_in_list (unsigned int i, elf_section_list * list)
6448 {
6449 for (;list != NULL; list = list->next)
6450 if (list->ndx == i)
6451 break;
6452 return list;
6453 }
6454
6455 /* Work out the file positions of all the sections. This is called by
6456 _bfd_elf_compute_section_file_positions. All the section sizes and
6457 VMAs must be known before this is called.
6458
6459 Reloc sections come in two flavours: Those processed specially as
6460 "side-channel" data attached to a section to which they apply, and
6461 those that bfd doesn't process as relocations. The latter sort are
6462 stored in a normal bfd section by bfd_section_from_shdr. We don't
6463 consider the former sort here, unless they form part of the loadable
6464 image. Reloc sections not assigned here (and compressed debugging
6465 sections and CTF sections which nothing else in the file can rely
6466 upon) will be handled later by assign_file_positions_for_relocs.
6467
6468 We also don't set the positions of the .symtab and .strtab here. */
6469
6470 static bool
6471 assign_file_positions_except_relocs (bfd *abfd,
6472 struct bfd_link_info *link_info)
6473 {
6474 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6475 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6477 unsigned int alloc;
6478
6479 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6480 && bfd_get_format (abfd) != bfd_core)
6481 {
6482 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6483 unsigned int num_sec = elf_numsections (abfd);
6484 Elf_Internal_Shdr **hdrpp;
6485 unsigned int i;
6486 file_ptr off;
6487
6488 /* Start after the ELF header. */
6489 off = i_ehdrp->e_ehsize;
6490
6491 /* We are not creating an executable, which means that we are
6492 not creating a program header, and that the actual order of
6493 the sections in the file is unimportant. */
6494 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6495 {
6496 Elf_Internal_Shdr *hdr;
6497
6498 hdr = *hdrpp;
6499 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6500 && hdr->bfd_section == NULL)
6501 /* Do not assign offsets for these sections yet: we don't know
6502 their sizes. */
6503 || (hdr->bfd_section != NULL
6504 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6505 || (bfd_section_is_ctf (hdr->bfd_section)
6506 && abfd->is_linker_output)))
6507 || i == elf_onesymtab (abfd)
6508 || (elf_symtab_shndx_list (abfd) != NULL
6509 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6510 || i == elf_strtab_sec (abfd)
6511 || i == elf_shstrtab_sec (abfd))
6512 {
6513 hdr->sh_offset = -1;
6514 }
6515 else
6516 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6517 }
6518
6519 elf_next_file_pos (abfd) = off;
6520 elf_program_header_size (abfd) = 0;
6521 }
6522 else
6523 {
6524 /* Assign file positions for the loaded sections based on the
6525 assignment of sections to segments. */
6526 if (!assign_file_positions_for_load_sections (abfd, link_info))
6527 return false;
6528
6529 /* And for non-load sections. */
6530 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6531 return false;
6532 }
6533
6534 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6535 return false;
6536
6537 /* Write out the program headers. */
6538 alloc = i_ehdrp->e_phnum;
6539 if (alloc != 0)
6540 {
6541 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6542 {
6543 /* Memory resident segments with non-zero size and RWX
6544 permissions are a security risk, so we generate a warning
6545 here if we are creating any. */
6546 unsigned int i;
6547
6548 for (i = 0; i < alloc; i++)
6549 {
6550 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6551
6552 if (phdr->p_memsz == 0)
6553 continue;
6554
6555 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6556 _bfd_error_handler (_("warning: %pB has a TLS segment"
6557 " with execute permission"),
6558 abfd);
6559 else if (phdr->p_type == PT_LOAD
6560 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6561 == (PF_R | PF_W | PF_X)))
6562 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6563 " with RWX permissions"),
6564 abfd);
6565 }
6566 }
6567
6568 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6569 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6570 return false;
6571 }
6572
6573 return true;
6574 }
6575
6576 bool
6577 _bfd_elf_init_file_header (bfd *abfd,
6578 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6579 {
6580 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6581 struct elf_strtab_hash *shstrtab;
6582 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6583
6584 i_ehdrp = elf_elfheader (abfd);
6585
6586 shstrtab = _bfd_elf_strtab_init ();
6587 if (shstrtab == NULL)
6588 return false;
6589
6590 elf_shstrtab (abfd) = shstrtab;
6591
6592 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6593 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6594 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6595 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6596
6597 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6598 i_ehdrp->e_ident[EI_DATA] =
6599 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6600 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6601
6602 if ((abfd->flags & DYNAMIC) != 0)
6603 i_ehdrp->e_type = ET_DYN;
6604 else if ((abfd->flags & EXEC_P) != 0)
6605 i_ehdrp->e_type = ET_EXEC;
6606 else if (bfd_get_format (abfd) == bfd_core)
6607 i_ehdrp->e_type = ET_CORE;
6608 else
6609 i_ehdrp->e_type = ET_REL;
6610
6611 switch (bfd_get_arch (abfd))
6612 {
6613 case bfd_arch_unknown:
6614 i_ehdrp->e_machine = EM_NONE;
6615 break;
6616
6617 /* There used to be a long list of cases here, each one setting
6618 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6619 in the corresponding bfd definition. To avoid duplication,
6620 the switch was removed. Machines that need special handling
6621 can generally do it in elf_backend_final_write_processing(),
6622 unless they need the information earlier than the final write.
6623 Such need can generally be supplied by replacing the tests for
6624 e_machine with the conditions used to determine it. */
6625 default:
6626 i_ehdrp->e_machine = bed->elf_machine_code;
6627 }
6628
6629 i_ehdrp->e_version = bed->s->ev_current;
6630 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6631
6632 /* No program header, for now. */
6633 i_ehdrp->e_phoff = 0;
6634 i_ehdrp->e_phentsize = 0;
6635 i_ehdrp->e_phnum = 0;
6636
6637 /* Each bfd section is section header entry. */
6638 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6639 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6640
6641 elf_tdata (abfd)->symtab_hdr.sh_name =
6642 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6643 elf_tdata (abfd)->strtab_hdr.sh_name =
6644 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6645 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6646 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6647 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6648 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6649 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6650 return false;
6651
6652 return true;
6653 }
6654
6655 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6656
6657 FIXME: We used to have code here to sort the PT_LOAD segments into
6658 ascending order, as per the ELF spec. But this breaks some programs,
6659 including the Linux kernel. But really either the spec should be
6660 changed or the programs updated. */
6661
6662 bool
6663 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6664 {
6665 if (link_info != NULL && bfd_link_pie (link_info))
6666 {
6667 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6668 unsigned int num_segments = i_ehdrp->e_phnum;
6669 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6670 Elf_Internal_Phdr *segment = tdata->phdr;
6671 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6672
6673 /* Find the lowest p_vaddr in PT_LOAD segments. */
6674 bfd_vma p_vaddr = (bfd_vma) -1;
6675 for (; segment < end_segment; segment++)
6676 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6677 p_vaddr = segment->p_vaddr;
6678
6679 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6680 segments is non-zero. */
6681 if (p_vaddr)
6682 i_ehdrp->e_type = ET_EXEC;
6683 }
6684 return true;
6685 }
6686
6687 /* Assign file positions for all the reloc sections which are not part
6688 of the loadable file image, and the file position of section headers. */
6689
6690 static bool
6691 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6692 {
6693 file_ptr off;
6694 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6695 Elf_Internal_Shdr *shdrp;
6696 Elf_Internal_Ehdr *i_ehdrp;
6697 const struct elf_backend_data *bed;
6698
6699 off = elf_next_file_pos (abfd);
6700
6701 shdrpp = elf_elfsections (abfd);
6702 end_shdrpp = shdrpp + elf_numsections (abfd);
6703 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6704 {
6705 shdrp = *shdrpp;
6706 if (shdrp->sh_offset == -1)
6707 {
6708 asection *sec = shdrp->bfd_section;
6709 bool is_rel = (shdrp->sh_type == SHT_REL
6710 || shdrp->sh_type == SHT_RELA);
6711 bool is_ctf = sec && bfd_section_is_ctf (sec);
6712 if (is_rel
6713 || is_ctf
6714 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6715 {
6716 if (!is_rel && !is_ctf)
6717 {
6718 const char *name = sec->name;
6719 struct bfd_elf_section_data *d;
6720
6721 /* Compress DWARF debug sections. */
6722 if (!bfd_compress_section (abfd, sec,
6723 shdrp->contents))
6724 return false;
6725
6726 if (sec->compress_status == COMPRESS_SECTION_DONE
6727 && (abfd->flags & BFD_COMPRESS_GABI) == 0
6728 && name[1] == 'd')
6729 {
6730 /* If section is compressed with zlib-gnu, convert
6731 section name from .debug_* to .zdebug_*. */
6732 char *new_name
6733 = convert_debug_to_zdebug (abfd, name);
6734 if (new_name == NULL)
6735 return false;
6736 name = new_name;
6737 }
6738 /* Add section name to section name section. */
6739 if (shdrp->sh_name != (unsigned int) -1)
6740 abort ();
6741 shdrp->sh_name
6742 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6743 name, false);
6744 d = elf_section_data (sec);
6745
6746 /* Add reloc section name to section name section. */
6747 if (d->rel.hdr
6748 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6749 name, false))
6750 return false;
6751 if (d->rela.hdr
6752 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6753 name, true))
6754 return false;
6755
6756 /* Update section size and contents. */
6757 shdrp->sh_size = sec->size;
6758 shdrp->contents = sec->contents;
6759 shdrp->bfd_section->contents = NULL;
6760 }
6761 else if (is_ctf)
6762 {
6763 /* Update section size and contents. */
6764 shdrp->sh_size = sec->size;
6765 shdrp->contents = sec->contents;
6766 }
6767
6768 off = _bfd_elf_assign_file_position_for_section (shdrp, off,
6769 true);
6770 }
6771 }
6772 }
6773
6774 /* Place section name section after DWARF debug sections have been
6775 compressed. */
6776 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6777 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6778 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6779 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6780
6781 /* Place the section headers. */
6782 i_ehdrp = elf_elfheader (abfd);
6783 bed = get_elf_backend_data (abfd);
6784 off = align_file_position (off, 1 << bed->s->log_file_align);
6785 i_ehdrp->e_shoff = off;
6786 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6787 elf_next_file_pos (abfd) = off;
6788
6789 return true;
6790 }
6791
6792 bool
6793 _bfd_elf_write_object_contents (bfd *abfd)
6794 {
6795 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6796 Elf_Internal_Shdr **i_shdrp;
6797 bool failed;
6798 unsigned int count, num_sec;
6799 struct elf_obj_tdata *t;
6800
6801 if (! abfd->output_has_begun
6802 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6803 return false;
6804 /* Do not rewrite ELF data when the BFD has been opened for update.
6805 abfd->output_has_begun was set to TRUE on opening, so creation of
6806 new sections, and modification of existing section sizes was
6807 restricted. This means the ELF header, program headers and
6808 section headers can't have changed. If the contents of any
6809 sections has been modified, then those changes have already been
6810 written to the BFD. */
6811 else if (abfd->direction == both_direction)
6812 {
6813 BFD_ASSERT (abfd->output_has_begun);
6814 return true;
6815 }
6816
6817 i_shdrp = elf_elfsections (abfd);
6818
6819 failed = false;
6820 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6821 if (failed)
6822 return false;
6823
6824 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6825 return false;
6826
6827 /* After writing the headers, we need to write the sections too... */
6828 num_sec = elf_numsections (abfd);
6829 for (count = 1; count < num_sec; count++)
6830 {
6831 i_shdrp[count]->sh_name
6832 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6833 i_shdrp[count]->sh_name);
6834 if (bed->elf_backend_section_processing)
6835 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6836 return false;
6837 if (i_shdrp[count]->contents)
6838 {
6839 bfd_size_type amt = i_shdrp[count]->sh_size;
6840
6841 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6842 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6843 return false;
6844 }
6845 }
6846
6847 /* Write out the section header names. */
6848 t = elf_tdata (abfd);
6849 if (elf_shstrtab (abfd) != NULL
6850 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6851 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6852 return false;
6853
6854 if (!(*bed->elf_backend_final_write_processing) (abfd))
6855 return false;
6856
6857 if (!bed->s->write_shdrs_and_ehdr (abfd))
6858 return false;
6859
6860 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6861 if (t->o->build_id.after_write_object_contents != NULL
6862 && !(*t->o->build_id.after_write_object_contents) (abfd))
6863 return false;
6864 if (t->o->package_metadata.after_write_object_contents != NULL
6865 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6866 return false;
6867
6868 return true;
6869 }
6870
6871 bool
6872 _bfd_elf_write_corefile_contents (bfd *abfd)
6873 {
6874 /* Hopefully this can be done just like an object file. */
6875 return _bfd_elf_write_object_contents (abfd);
6876 }
6877
6878 /* Given a section, search the header to find them. */
6879
6880 unsigned int
6881 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6882 {
6883 const struct elf_backend_data *bed;
6884 unsigned int sec_index;
6885
6886 if (elf_section_data (asect) != NULL
6887 && elf_section_data (asect)->this_idx != 0)
6888 return elf_section_data (asect)->this_idx;
6889
6890 if (bfd_is_abs_section (asect))
6891 sec_index = SHN_ABS;
6892 else if (bfd_is_com_section (asect))
6893 sec_index = SHN_COMMON;
6894 else if (bfd_is_und_section (asect))
6895 sec_index = SHN_UNDEF;
6896 else
6897 sec_index = SHN_BAD;
6898
6899 bed = get_elf_backend_data (abfd);
6900 if (bed->elf_backend_section_from_bfd_section)
6901 {
6902 int retval = sec_index;
6903
6904 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6905 return retval;
6906 }
6907
6908 if (sec_index == SHN_BAD)
6909 bfd_set_error (bfd_error_nonrepresentable_section);
6910
6911 return sec_index;
6912 }
6913
6914 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6915 on error. */
6916
6917 int
6918 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6919 {
6920 asymbol *asym_ptr = *asym_ptr_ptr;
6921 int idx;
6922 flagword flags = asym_ptr->flags;
6923
6924 /* When gas creates relocations against local labels, it creates its
6925 own symbol for the section, but does put the symbol into the
6926 symbol chain, so udata is 0. When the linker is generating
6927 relocatable output, this section symbol may be for one of the
6928 input sections rather than the output section. */
6929 if (asym_ptr->udata.i == 0
6930 && (flags & BSF_SECTION_SYM)
6931 && asym_ptr->section)
6932 {
6933 asection *sec;
6934
6935 sec = asym_ptr->section;
6936 if (sec->owner != abfd && sec->output_section != NULL)
6937 sec = sec->output_section;
6938 if (sec->owner == abfd
6939 && sec->index < elf_num_section_syms (abfd)
6940 && elf_section_syms (abfd)[sec->index] != NULL)
6941 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
6942 }
6943
6944 idx = asym_ptr->udata.i;
6945
6946 if (idx == 0)
6947 {
6948 /* This case can occur when using --strip-symbol on a symbol
6949 which is used in a relocation entry. */
6950 _bfd_error_handler
6951 /* xgettext:c-format */
6952 (_("%pB: symbol `%s' required but not present"),
6953 abfd, bfd_asymbol_name (asym_ptr));
6954 bfd_set_error (bfd_error_no_symbols);
6955 return -1;
6956 }
6957
6958 #if DEBUG & 4
6959 {
6960 fprintf (stderr,
6961 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6962 " flags = 0x%.8x\n",
6963 (long) asym_ptr, asym_ptr->name, idx, flags);
6964 fflush (stderr);
6965 }
6966 #endif
6967
6968 return idx;
6969 }
6970
6971 static inline bfd_vma
6972 segment_size (Elf_Internal_Phdr *segment)
6973 {
6974 return (segment->p_memsz > segment->p_filesz
6975 ? segment->p_memsz : segment->p_filesz);
6976 }
6977
6978
6979 /* Returns the end address of the segment + 1. */
6980 static inline bfd_vma
6981 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6982 {
6983 return start + segment_size (segment);
6984 }
6985
6986 static inline bfd_size_type
6987 section_size (asection *section, Elf_Internal_Phdr *segment)
6988 {
6989 if ((section->flags & SEC_HAS_CONTENTS) != 0
6990 || (section->flags & SEC_THREAD_LOCAL) == 0
6991 || segment->p_type == PT_TLS)
6992 return section->size;
6993 return 0;
6994 }
6995
6996 /* Returns TRUE if the given section is contained within the given
6997 segment. LMA addresses are compared against PADDR when
6998 bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true. */
6999 static bool
7000 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7001 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7002 const struct elf_backend_data *bed)
7003 {
7004 bfd_vma seg_addr = !bed->want_p_paddr_set_to_zero ? paddr : vaddr;
7005 bfd_vma addr = !bed->want_p_paddr_set_to_zero ? section->lma : section->vma;
7006 bfd_vma octet;
7007 if (_bfd_mul_overflow (addr, opb, &octet))
7008 return false;
7009 /* The third and fourth lines below are testing that the section end
7010 address is within the segment. It's written this way to avoid
7011 overflow. Add seg_addr + section_size to both sides of the
7012 inequality to make it obvious. */
7013 return (octet >= seg_addr
7014 && segment_size (segment) >= section_size (section, segment)
7015 && (octet - seg_addr
7016 <= segment_size (segment) - section_size (section, segment)));
7017 }
7018
7019 /* Handle PT_NOTE segment. */
7020 static bool
7021 is_note (asection *s, Elf_Internal_Phdr *p)
7022 {
7023 return (p->p_type == PT_NOTE
7024 && elf_section_type (s) == SHT_NOTE
7025 && (ufile_ptr) s->filepos >= p->p_offset
7026 && p->p_filesz >= s->size
7027 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7028 }
7029
7030 /* Rewrite program header information. */
7031
7032 static bool
7033 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7034 {
7035 Elf_Internal_Ehdr *iehdr;
7036 struct elf_segment_map *map;
7037 struct elf_segment_map *map_first;
7038 struct elf_segment_map **pointer_to_map;
7039 Elf_Internal_Phdr *segment;
7040 asection *section;
7041 unsigned int i;
7042 unsigned int num_segments;
7043 bool phdr_included = false;
7044 bool p_paddr_valid;
7045 struct elf_segment_map *phdr_adjust_seg = NULL;
7046 unsigned int phdr_adjust_num = 0;
7047 const struct elf_backend_data *bed;
7048 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7049
7050 bed = get_elf_backend_data (ibfd);
7051 iehdr = elf_elfheader (ibfd);
7052
7053 map_first = NULL;
7054 pointer_to_map = &map_first;
7055
7056 num_segments = elf_elfheader (ibfd)->e_phnum;
7057
7058 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7059 linker, which generates a PT_INTERP section with p_vaddr and
7060 p_memsz set to 0. */
7061 #define IS_SOLARIS_PT_INTERP(p, s) \
7062 (p->p_vaddr == 0 \
7063 && p->p_paddr == 0 \
7064 && p->p_memsz == 0 \
7065 && p->p_filesz > 0 \
7066 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7067 && s->size > 0 \
7068 && (bfd_vma) s->filepos >= p->p_offset \
7069 && ((bfd_vma) s->filepos + s->size \
7070 <= p->p_offset + p->p_filesz))
7071
7072 /* Decide if the given section should be included in the given segment.
7073 A section will be included if:
7074 1. It is within the address space of the segment -- we use the LMA
7075 if that is set for the segment and the VMA otherwise,
7076 2. It is an allocated section or a NOTE section in a PT_NOTE
7077 segment.
7078 3. There is an output section associated with it,
7079 4. The section has not already been allocated to a previous segment.
7080 5. PT_GNU_STACK segments do not include any sections.
7081 6. PT_TLS segment includes only SHF_TLS sections.
7082 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7083 8. PT_DYNAMIC should not contain empty sections at the beginning
7084 (with the possible exception of .dynamic). */
7085 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7086 (((is_contained_by (section, segment, segment->p_paddr, \
7087 segment->p_vaddr, opb, bed) \
7088 && (section->flags & SEC_ALLOC) != 0) \
7089 || is_note (section, segment)) \
7090 && segment->p_type != PT_GNU_STACK \
7091 && (segment->p_type != PT_TLS \
7092 || (section->flags & SEC_THREAD_LOCAL)) \
7093 && (segment->p_type == PT_LOAD \
7094 || segment->p_type == PT_TLS \
7095 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7096 && (segment->p_type != PT_DYNAMIC \
7097 || section_size (section, segment) > 0 \
7098 || (segment->p_paddr \
7099 ? segment->p_paddr != section->lma * (opb) \
7100 : segment->p_vaddr != section->vma * (opb)) \
7101 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7102 && (segment->p_type != PT_LOAD || !section->segment_mark))
7103
7104 /* If the output section of a section in the input segment is NULL,
7105 it is removed from the corresponding output segment. */
7106 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7107 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7108 && section->output_section != NULL)
7109
7110 /* Returns TRUE iff seg1 starts after the end of seg2. */
7111 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7112 (seg1->field >= segment_end (seg2, seg2->field))
7113
7114 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7115 their VMA address ranges and their LMA address ranges overlap.
7116 It is possible to have overlapping VMA ranges without overlapping LMA
7117 ranges. RedBoot images for example can have both .data and .bss mapped
7118 to the same VMA range, but with the .data section mapped to a different
7119 LMA. */
7120 #define SEGMENT_OVERLAPS(seg1, seg2) \
7121 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7122 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7123 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7124 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7125
7126 /* Initialise the segment mark field, and discard stupid alignment. */
7127 for (section = ibfd->sections; section != NULL; section = section->next)
7128 {
7129 asection *o = section->output_section;
7130 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7131 o->alignment_power = 0;
7132 section->segment_mark = false;
7133 }
7134
7135 /* The Solaris linker creates program headers in which all the
7136 p_paddr fields are zero. When we try to objcopy or strip such a
7137 file, we get confused. Check for this case, and if we find it
7138 don't set the p_paddr_valid fields. */
7139 p_paddr_valid = false;
7140 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7141 i < num_segments;
7142 i++, segment++)
7143 if (segment->p_paddr != 0)
7144 {
7145 p_paddr_valid = true;
7146 break;
7147 }
7148
7149 /* Scan through the segments specified in the program header
7150 of the input BFD. For this first scan we look for overlaps
7151 in the loadable segments. These can be created by weird
7152 parameters to objcopy. Also, fix some solaris weirdness. */
7153 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7154 i < num_segments;
7155 i++, segment++)
7156 {
7157 unsigned int j;
7158 Elf_Internal_Phdr *segment2;
7159
7160 if (segment->p_type == PT_INTERP)
7161 for (section = ibfd->sections; section; section = section->next)
7162 if (IS_SOLARIS_PT_INTERP (segment, section))
7163 {
7164 /* Mininal change so that the normal section to segment
7165 assignment code will work. */
7166 segment->p_vaddr = section->vma * opb;
7167 break;
7168 }
7169
7170 if (segment->p_type != PT_LOAD)
7171 {
7172 /* Remove PT_GNU_RELRO segment. */
7173 if (segment->p_type == PT_GNU_RELRO)
7174 segment->p_type = PT_NULL;
7175 continue;
7176 }
7177
7178 /* Determine if this segment overlaps any previous segments. */
7179 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7180 {
7181 bfd_signed_vma extra_length;
7182
7183 if (segment2->p_type != PT_LOAD
7184 || !SEGMENT_OVERLAPS (segment, segment2))
7185 continue;
7186
7187 /* Merge the two segments together. */
7188 if (segment2->p_vaddr < segment->p_vaddr)
7189 {
7190 /* Extend SEGMENT2 to include SEGMENT and then delete
7191 SEGMENT. */
7192 extra_length = (segment_end (segment, segment->p_vaddr)
7193 - segment_end (segment2, segment2->p_vaddr));
7194
7195 if (extra_length > 0)
7196 {
7197 segment2->p_memsz += extra_length;
7198 segment2->p_filesz += extra_length;
7199 }
7200
7201 segment->p_type = PT_NULL;
7202
7203 /* Since we have deleted P we must restart the outer loop. */
7204 i = 0;
7205 segment = elf_tdata (ibfd)->phdr;
7206 break;
7207 }
7208 else
7209 {
7210 /* Extend SEGMENT to include SEGMENT2 and then delete
7211 SEGMENT2. */
7212 extra_length = (segment_end (segment2, segment2->p_vaddr)
7213 - segment_end (segment, segment->p_vaddr));
7214
7215 if (extra_length > 0)
7216 {
7217 segment->p_memsz += extra_length;
7218 segment->p_filesz += extra_length;
7219 }
7220
7221 segment2->p_type = PT_NULL;
7222 }
7223 }
7224 }
7225
7226 /* The second scan attempts to assign sections to segments. */
7227 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7228 i < num_segments;
7229 i++, segment++)
7230 {
7231 unsigned int section_count;
7232 asection **sections;
7233 asection *output_section;
7234 unsigned int isec;
7235 asection *matching_lma;
7236 asection *suggested_lma;
7237 unsigned int j;
7238 size_t amt;
7239 asection *first_section;
7240
7241 if (segment->p_type == PT_NULL)
7242 continue;
7243
7244 first_section = NULL;
7245 /* Compute how many sections might be placed into this segment. */
7246 for (section = ibfd->sections, section_count = 0;
7247 section != NULL;
7248 section = section->next)
7249 {
7250 /* Find the first section in the input segment, which may be
7251 removed from the corresponding output segment. */
7252 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7253 {
7254 if (first_section == NULL)
7255 first_section = section;
7256 if (section->output_section != NULL)
7257 ++section_count;
7258 }
7259 }
7260
7261 /* Allocate a segment map big enough to contain
7262 all of the sections we have selected. */
7263 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7264 amt += section_count * sizeof (asection *);
7265 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7266 if (map == NULL)
7267 return false;
7268
7269 /* Initialise the fields of the segment map. Default to
7270 using the physical address of the segment in the input BFD. */
7271 map->next = NULL;
7272 map->p_type = segment->p_type;
7273 map->p_flags = segment->p_flags;
7274 map->p_flags_valid = 1;
7275
7276 if (map->p_type == PT_LOAD
7277 && (ibfd->flags & D_PAGED) != 0
7278 && maxpagesize > 1
7279 && segment->p_align > 1)
7280 {
7281 map->p_align = segment->p_align;
7282 if (segment->p_align > maxpagesize)
7283 map->p_align = maxpagesize;
7284 map->p_align_valid = 1;
7285 }
7286
7287 /* If the first section in the input segment is removed, there is
7288 no need to preserve segment physical address in the corresponding
7289 output segment. */
7290 if (!first_section || first_section->output_section != NULL)
7291 {
7292 map->p_paddr = segment->p_paddr;
7293 map->p_paddr_valid = p_paddr_valid;
7294 }
7295
7296 /* Determine if this segment contains the ELF file header
7297 and if it contains the program headers themselves. */
7298 map->includes_filehdr = (segment->p_offset == 0
7299 && segment->p_filesz >= iehdr->e_ehsize);
7300 map->includes_phdrs = 0;
7301
7302 if (!phdr_included || segment->p_type != PT_LOAD)
7303 {
7304 map->includes_phdrs =
7305 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7306 && (segment->p_offset + segment->p_filesz
7307 >= ((bfd_vma) iehdr->e_phoff
7308 + iehdr->e_phnum * iehdr->e_phentsize)));
7309
7310 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7311 phdr_included = true;
7312 }
7313
7314 if (section_count == 0)
7315 {
7316 /* Special segments, such as the PT_PHDR segment, may contain
7317 no sections, but ordinary, loadable segments should contain
7318 something. They are allowed by the ELF spec however, so only
7319 a warning is produced.
7320 There is however the valid use case of embedded systems which
7321 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7322 flash memory with zeros. No warning is shown for that case. */
7323 if (segment->p_type == PT_LOAD
7324 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7325 /* xgettext:c-format */
7326 _bfd_error_handler
7327 (_("%pB: warning: empty loadable segment detected"
7328 " at vaddr=%#" PRIx64 ", is this intentional?"),
7329 ibfd, (uint64_t) segment->p_vaddr);
7330
7331 map->p_vaddr_offset = segment->p_vaddr / opb;
7332 map->count = 0;
7333 *pointer_to_map = map;
7334 pointer_to_map = &map->next;
7335
7336 continue;
7337 }
7338
7339 /* Now scan the sections in the input BFD again and attempt
7340 to add their corresponding output sections to the segment map.
7341 The problem here is how to handle an output section which has
7342 been moved (ie had its LMA changed). There are four possibilities:
7343
7344 1. None of the sections have been moved.
7345 In this case we can continue to use the segment LMA from the
7346 input BFD.
7347
7348 2. All of the sections have been moved by the same amount.
7349 In this case we can change the segment's LMA to match the LMA
7350 of the first section.
7351
7352 3. Some of the sections have been moved, others have not.
7353 In this case those sections which have not been moved can be
7354 placed in the current segment which will have to have its size,
7355 and possibly its LMA changed, and a new segment or segments will
7356 have to be created to contain the other sections.
7357
7358 4. The sections have been moved, but not by the same amount.
7359 In this case we can change the segment's LMA to match the LMA
7360 of the first section and we will have to create a new segment
7361 or segments to contain the other sections.
7362
7363 In order to save time, we allocate an array to hold the section
7364 pointers that we are interested in. As these sections get assigned
7365 to a segment, they are removed from this array. */
7366
7367 amt = section_count * sizeof (asection *);
7368 sections = (asection **) bfd_malloc (amt);
7369 if (sections == NULL)
7370 return false;
7371
7372 /* Step One: Scan for segment vs section LMA conflicts.
7373 Also add the sections to the section array allocated above.
7374 Also add the sections to the current segment. In the common
7375 case, where the sections have not been moved, this means that
7376 we have completely filled the segment, and there is nothing
7377 more to do. */
7378 isec = 0;
7379 matching_lma = NULL;
7380 suggested_lma = NULL;
7381
7382 for (section = first_section, j = 0;
7383 section != NULL;
7384 section = section->next)
7385 {
7386 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7387 {
7388 output_section = section->output_section;
7389
7390 sections[j++] = section;
7391
7392 /* The Solaris native linker always sets p_paddr to 0.
7393 We try to catch that case here, and set it to the
7394 correct value. Note - some backends require that
7395 p_paddr be left as zero. */
7396 if (!p_paddr_valid
7397 && segment->p_vaddr != 0
7398 && !bed->want_p_paddr_set_to_zero
7399 && isec == 0
7400 && output_section->lma != 0
7401 && (align_power (segment->p_vaddr
7402 + (map->includes_filehdr
7403 ? iehdr->e_ehsize : 0)
7404 + (map->includes_phdrs
7405 ? iehdr->e_phnum * iehdr->e_phentsize
7406 : 0),
7407 output_section->alignment_power * opb)
7408 == (output_section->vma * opb)))
7409 map->p_paddr = segment->p_vaddr;
7410
7411 /* Match up the physical address of the segment with the
7412 LMA address of the output section. */
7413 if (is_contained_by (output_section, segment, map->p_paddr,
7414 map->p_paddr + map->p_vaddr_offset, opb, bed)
7415 || is_note (section, segment))
7416 {
7417 if (matching_lma == NULL
7418 || output_section->lma < matching_lma->lma)
7419 matching_lma = output_section;
7420
7421 /* We assume that if the section fits within the segment
7422 then it does not overlap any other section within that
7423 segment. */
7424 map->sections[isec++] = output_section;
7425 }
7426 else if (suggested_lma == NULL)
7427 suggested_lma = output_section;
7428
7429 if (j == section_count)
7430 break;
7431 }
7432 }
7433
7434 BFD_ASSERT (j == section_count);
7435
7436 /* Step Two: Adjust the physical address of the current segment,
7437 if necessary. */
7438 if (isec == section_count)
7439 {
7440 /* All of the sections fitted within the segment as currently
7441 specified. This is the default case. Add the segment to
7442 the list of built segments and carry on to process the next
7443 program header in the input BFD. */
7444 map->count = section_count;
7445 *pointer_to_map = map;
7446 pointer_to_map = &map->next;
7447
7448 if (p_paddr_valid
7449 && !bed->want_p_paddr_set_to_zero)
7450 {
7451 bfd_vma hdr_size = 0;
7452 if (map->includes_filehdr)
7453 hdr_size = iehdr->e_ehsize;
7454 if (map->includes_phdrs)
7455 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7456
7457 /* Account for padding before the first section in the
7458 segment. */
7459 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7460 - matching_lma->lma);
7461 }
7462
7463 free (sections);
7464 continue;
7465 }
7466 else
7467 {
7468 /* Change the current segment's physical address to match
7469 the LMA of the first section that fitted, or if no
7470 section fitted, the first section. */
7471 if (matching_lma == NULL)
7472 matching_lma = suggested_lma;
7473
7474 map->p_paddr = matching_lma->lma * opb;
7475
7476 /* Offset the segment physical address from the lma
7477 to allow for space taken up by elf headers. */
7478 if (map->includes_phdrs)
7479 {
7480 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7481
7482 /* iehdr->e_phnum is just an estimate of the number
7483 of program headers that we will need. Make a note
7484 here of the number we used and the segment we chose
7485 to hold these headers, so that we can adjust the
7486 offset when we know the correct value. */
7487 phdr_adjust_num = iehdr->e_phnum;
7488 phdr_adjust_seg = map;
7489 }
7490
7491 if (map->includes_filehdr)
7492 {
7493 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7494 map->p_paddr -= iehdr->e_ehsize;
7495 /* We've subtracted off the size of headers from the
7496 first section lma, but there may have been some
7497 alignment padding before that section too. Try to
7498 account for that by adjusting the segment lma down to
7499 the same alignment. */
7500 if (segment->p_align != 0 && segment->p_align < align)
7501 align = segment->p_align;
7502 map->p_paddr &= -(align * opb);
7503 }
7504 }
7505
7506 /* Step Three: Loop over the sections again, this time assigning
7507 those that fit to the current segment and removing them from the
7508 sections array; but making sure not to leave large gaps. Once all
7509 possible sections have been assigned to the current segment it is
7510 added to the list of built segments and if sections still remain
7511 to be assigned, a new segment is constructed before repeating
7512 the loop. */
7513 isec = 0;
7514 do
7515 {
7516 map->count = 0;
7517 suggested_lma = NULL;
7518
7519 /* Fill the current segment with sections that fit. */
7520 for (j = 0; j < section_count; j++)
7521 {
7522 section = sections[j];
7523
7524 if (section == NULL)
7525 continue;
7526
7527 output_section = section->output_section;
7528
7529 BFD_ASSERT (output_section != NULL);
7530
7531 if (is_contained_by (output_section, segment, map->p_paddr,
7532 map->p_paddr + map->p_vaddr_offset, opb, bed)
7533 || is_note (section, segment))
7534 {
7535 if (map->count == 0)
7536 {
7537 /* If the first section in a segment does not start at
7538 the beginning of the segment, then something is
7539 wrong. */
7540 if (align_power (map->p_paddr
7541 + (map->includes_filehdr
7542 ? iehdr->e_ehsize : 0)
7543 + (map->includes_phdrs
7544 ? iehdr->e_phnum * iehdr->e_phentsize
7545 : 0),
7546 output_section->alignment_power * opb)
7547 != output_section->lma * opb)
7548 goto sorry;
7549 }
7550 else
7551 {
7552 asection *prev_sec;
7553
7554 prev_sec = map->sections[map->count - 1];
7555
7556 /* If the gap between the end of the previous section
7557 and the start of this section is more than
7558 maxpagesize then we need to start a new segment. */
7559 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7560 maxpagesize)
7561 < BFD_ALIGN (output_section->lma, maxpagesize))
7562 || (prev_sec->lma + prev_sec->size
7563 > output_section->lma))
7564 {
7565 if (suggested_lma == NULL)
7566 suggested_lma = output_section;
7567
7568 continue;
7569 }
7570 }
7571
7572 map->sections[map->count++] = output_section;
7573 ++isec;
7574 sections[j] = NULL;
7575 if (segment->p_type == PT_LOAD)
7576 section->segment_mark = true;
7577 }
7578 else if (suggested_lma == NULL)
7579 suggested_lma = output_section;
7580 }
7581
7582 /* PR 23932. A corrupt input file may contain sections that cannot
7583 be assigned to any segment - because for example they have a
7584 negative size - or segments that do not contain any sections.
7585 But there are also valid reasons why a segment can be empty.
7586 So allow a count of zero. */
7587
7588 /* Add the current segment to the list of built segments. */
7589 *pointer_to_map = map;
7590 pointer_to_map = &map->next;
7591
7592 if (isec < section_count)
7593 {
7594 /* We still have not allocated all of the sections to
7595 segments. Create a new segment here, initialise it
7596 and carry on looping. */
7597 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7598 amt += section_count * sizeof (asection *);
7599 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7600 if (map == NULL)
7601 {
7602 free (sections);
7603 return false;
7604 }
7605
7606 /* Initialise the fields of the segment map. Set the physical
7607 physical address to the LMA of the first section that has
7608 not yet been assigned. */
7609 map->next = NULL;
7610 map->p_type = segment->p_type;
7611 map->p_flags = segment->p_flags;
7612 map->p_flags_valid = 1;
7613 map->p_paddr = suggested_lma->lma * opb;
7614 map->p_paddr_valid = p_paddr_valid;
7615 map->includes_filehdr = 0;
7616 map->includes_phdrs = 0;
7617 }
7618
7619 continue;
7620 sorry:
7621 bfd_set_error (bfd_error_sorry);
7622 free (sections);
7623 return false;
7624 }
7625 while (isec < section_count);
7626
7627 free (sections);
7628 }
7629
7630 elf_seg_map (obfd) = map_first;
7631
7632 /* If we had to estimate the number of program headers that were
7633 going to be needed, then check our estimate now and adjust
7634 the offset if necessary. */
7635 if (phdr_adjust_seg != NULL)
7636 {
7637 unsigned int count;
7638
7639 for (count = 0, map = map_first; map != NULL; map = map->next)
7640 count++;
7641
7642 if (count > phdr_adjust_num)
7643 phdr_adjust_seg->p_paddr
7644 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7645
7646 for (map = map_first; map != NULL; map = map->next)
7647 if (map->p_type == PT_PHDR)
7648 {
7649 bfd_vma adjust
7650 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7651 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7652 break;
7653 }
7654 }
7655
7656 #undef IS_SOLARIS_PT_INTERP
7657 #undef IS_SECTION_IN_INPUT_SEGMENT
7658 #undef INCLUDE_SECTION_IN_SEGMENT
7659 #undef SEGMENT_AFTER_SEGMENT
7660 #undef SEGMENT_OVERLAPS
7661 return true;
7662 }
7663
7664 /* Return true if p_align in the ELF program header in ABFD is valid. */
7665
7666 static bool
7667 elf_is_p_align_valid (bfd *abfd)
7668 {
7669 unsigned int i;
7670 Elf_Internal_Phdr *segment;
7671 unsigned int num_segments;
7672 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7673 bfd_size_type maxpagesize = bed->maxpagesize;
7674 bfd_size_type p_align = bed->p_align;
7675
7676 /* Return true if the default p_align value isn't set or the maximum
7677 page size is the same as the minimum page size. */
7678 if (p_align == 0 || maxpagesize == bed->minpagesize)
7679 return true;
7680
7681 /* When the default p_align value is set, p_align may be set to the
7682 default p_align value while segments are aligned to the maximum
7683 page size. In this case, the input p_align will be ignored and
7684 the maximum page size will be used to align the output segments. */
7685 segment = elf_tdata (abfd)->phdr;
7686 num_segments = elf_elfheader (abfd)->e_phnum;
7687 for (i = 0; i < num_segments; i++, segment++)
7688 if (segment->p_type == PT_LOAD
7689 && (segment->p_align != p_align
7690 || vma_page_aligned_bias (segment->p_vaddr,
7691 segment->p_offset,
7692 maxpagesize) != 0))
7693 return true;
7694
7695 return false;
7696 }
7697
7698 /* Copy ELF program header information. */
7699
7700 static bool
7701 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7702 {
7703 Elf_Internal_Ehdr *iehdr;
7704 struct elf_segment_map *map;
7705 struct elf_segment_map *map_first;
7706 struct elf_segment_map **pointer_to_map;
7707 Elf_Internal_Phdr *segment;
7708 unsigned int i;
7709 unsigned int num_segments;
7710 bool phdr_included = false;
7711 bool p_paddr_valid;
7712 bool p_palign_valid;
7713 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7714
7715 iehdr = elf_elfheader (ibfd);
7716
7717 map_first = NULL;
7718 pointer_to_map = &map_first;
7719
7720 /* If all the segment p_paddr fields are zero, don't set
7721 map->p_paddr_valid. */
7722 p_paddr_valid = false;
7723 num_segments = elf_elfheader (ibfd)->e_phnum;
7724 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7725 i < num_segments;
7726 i++, segment++)
7727 if (segment->p_paddr != 0)
7728 {
7729 p_paddr_valid = true;
7730 break;
7731 }
7732
7733 p_palign_valid = elf_is_p_align_valid (ibfd);
7734
7735 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7736 i < num_segments;
7737 i++, segment++)
7738 {
7739 asection *section;
7740 unsigned int section_count;
7741 size_t amt;
7742 Elf_Internal_Shdr *this_hdr;
7743 asection *first_section = NULL;
7744 asection *lowest_section;
7745
7746 /* Compute how many sections are in this segment. */
7747 for (section = ibfd->sections, section_count = 0;
7748 section != NULL;
7749 section = section->next)
7750 {
7751 this_hdr = &(elf_section_data(section)->this_hdr);
7752 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7753 {
7754 if (first_section == NULL)
7755 first_section = section;
7756 section_count++;
7757 }
7758 }
7759
7760 /* Allocate a segment map big enough to contain
7761 all of the sections we have selected. */
7762 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7763 amt += section_count * sizeof (asection *);
7764 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7765 if (map == NULL)
7766 return false;
7767
7768 /* Initialize the fields of the output segment map with the
7769 input segment. */
7770 map->next = NULL;
7771 map->p_type = segment->p_type;
7772 map->p_flags = segment->p_flags;
7773 map->p_flags_valid = 1;
7774 map->p_paddr = segment->p_paddr;
7775 map->p_paddr_valid = p_paddr_valid;
7776 map->p_align = segment->p_align;
7777 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7778 map->p_align_valid = (map->p_type == PT_GNU_STACK
7779 || p_palign_valid);
7780 map->p_vaddr_offset = 0;
7781
7782 if (map->p_type == PT_GNU_RELRO
7783 || map->p_type == PT_GNU_STACK)
7784 {
7785 /* The PT_GNU_RELRO segment may contain the first a few
7786 bytes in the .got.plt section even if the whole .got.plt
7787 section isn't in the PT_GNU_RELRO segment. We won't
7788 change the size of the PT_GNU_RELRO segment.
7789 Similarly, PT_GNU_STACK size is significant on uclinux
7790 systems. */
7791 map->p_size = segment->p_memsz;
7792 map->p_size_valid = 1;
7793 }
7794
7795 /* Determine if this segment contains the ELF file header
7796 and if it contains the program headers themselves. */
7797 map->includes_filehdr = (segment->p_offset == 0
7798 && segment->p_filesz >= iehdr->e_ehsize);
7799
7800 map->includes_phdrs = 0;
7801 if (! phdr_included || segment->p_type != PT_LOAD)
7802 {
7803 map->includes_phdrs =
7804 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7805 && (segment->p_offset + segment->p_filesz
7806 >= ((bfd_vma) iehdr->e_phoff
7807 + iehdr->e_phnum * iehdr->e_phentsize)));
7808
7809 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7810 phdr_included = true;
7811 }
7812
7813 lowest_section = NULL;
7814 if (section_count != 0)
7815 {
7816 unsigned int isec = 0;
7817
7818 for (section = first_section;
7819 section != NULL;
7820 section = section->next)
7821 {
7822 this_hdr = &(elf_section_data(section)->this_hdr);
7823 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7824 {
7825 map->sections[isec++] = section->output_section;
7826 if ((section->flags & SEC_ALLOC) != 0)
7827 {
7828 bfd_vma seg_off;
7829
7830 if (lowest_section == NULL
7831 || section->lma < lowest_section->lma)
7832 lowest_section = section;
7833
7834 /* Section lmas are set up from PT_LOAD header
7835 p_paddr in _bfd_elf_make_section_from_shdr.
7836 If this header has a p_paddr that disagrees
7837 with the section lma, flag the p_paddr as
7838 invalid. */
7839 if ((section->flags & SEC_LOAD) != 0)
7840 seg_off = this_hdr->sh_offset - segment->p_offset;
7841 else
7842 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7843 if (section->lma * opb - segment->p_paddr != seg_off)
7844 map->p_paddr_valid = false;
7845 }
7846 if (isec == section_count)
7847 break;
7848 }
7849 }
7850 }
7851
7852 if (section_count == 0)
7853 map->p_vaddr_offset = segment->p_vaddr / opb;
7854 else if (map->p_paddr_valid)
7855 {
7856 /* Account for padding before the first section in the segment. */
7857 bfd_vma hdr_size = 0;
7858 if (map->includes_filehdr)
7859 hdr_size = iehdr->e_ehsize;
7860 if (map->includes_phdrs)
7861 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7862
7863 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7864 - (lowest_section ? lowest_section->lma : 0));
7865 }
7866
7867 map->count = section_count;
7868 *pointer_to_map = map;
7869 pointer_to_map = &map->next;
7870 }
7871
7872 elf_seg_map (obfd) = map_first;
7873 return true;
7874 }
7875
7876 /* Copy private BFD data. This copies or rewrites ELF program header
7877 information. */
7878
7879 static bool
7880 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7881 {
7882 bfd_vma maxpagesize;
7883
7884 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7885 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7886 return true;
7887
7888 if (elf_tdata (ibfd)->phdr == NULL)
7889 return true;
7890
7891 if (ibfd->xvec == obfd->xvec)
7892 {
7893 /* Check to see if any sections in the input BFD
7894 covered by ELF program header have changed. */
7895 Elf_Internal_Phdr *segment;
7896 asection *section, *osec;
7897 unsigned int i, num_segments;
7898 Elf_Internal_Shdr *this_hdr;
7899 const struct elf_backend_data *bed;
7900
7901 bed = get_elf_backend_data (ibfd);
7902
7903 /* Regenerate the segment map if p_paddr is set to 0. */
7904 if (bed->want_p_paddr_set_to_zero)
7905 goto rewrite;
7906
7907 /* Initialize the segment mark field. */
7908 for (section = obfd->sections; section != NULL;
7909 section = section->next)
7910 section->segment_mark = false;
7911
7912 num_segments = elf_elfheader (ibfd)->e_phnum;
7913 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7914 i < num_segments;
7915 i++, segment++)
7916 {
7917 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7918 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7919 which severly confuses things, so always regenerate the segment
7920 map in this case. */
7921 if (segment->p_paddr == 0
7922 && segment->p_memsz == 0
7923 && (segment->p_type == PT_INTERP
7924 || segment->p_type == PT_DYNAMIC))
7925 goto rewrite;
7926
7927 for (section = ibfd->sections;
7928 section != NULL; section = section->next)
7929 {
7930 /* We mark the output section so that we know it comes
7931 from the input BFD. */
7932 osec = section->output_section;
7933 if (osec)
7934 osec->segment_mark = true;
7935
7936 /* Check if this section is covered by the segment. */
7937 this_hdr = &(elf_section_data(section)->this_hdr);
7938 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7939 {
7940 /* FIXME: Check if its output section is changed or
7941 removed. What else do we need to check? */
7942 if (osec == NULL
7943 || section->flags != osec->flags
7944 || section->lma != osec->lma
7945 || section->vma != osec->vma
7946 || section->size != osec->size
7947 || section->rawsize != osec->rawsize
7948 || section->alignment_power != osec->alignment_power)
7949 goto rewrite;
7950 }
7951 }
7952 }
7953
7954 /* Check to see if any output section do not come from the
7955 input BFD. */
7956 for (section = obfd->sections; section != NULL;
7957 section = section->next)
7958 {
7959 if (!section->segment_mark)
7960 goto rewrite;
7961 else
7962 section->segment_mark = false;
7963 }
7964
7965 return copy_elf_program_header (ibfd, obfd);
7966 }
7967
7968 rewrite:
7969 maxpagesize = 0;
7970 if (ibfd->xvec == obfd->xvec)
7971 {
7972 /* When rewriting program header, set the output maxpagesize to
7973 the maximum alignment of input PT_LOAD segments. */
7974 Elf_Internal_Phdr *segment;
7975 unsigned int i;
7976 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7977
7978 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7979 i < num_segments;
7980 i++, segment++)
7981 if (segment->p_type == PT_LOAD
7982 && maxpagesize < segment->p_align)
7983 {
7984 /* PR 17512: file: f17299af. */
7985 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7986 /* xgettext:c-format */
7987 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7988 PRIx64 " is too large"),
7989 ibfd, (uint64_t) segment->p_align);
7990 else
7991 maxpagesize = segment->p_align;
7992 }
7993 }
7994 if (maxpagesize == 0)
7995 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7996
7997 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7998 }
7999
8000 /* Initialize private output section information from input section. */
8001
8002 bool
8003 _bfd_elf_init_private_section_data (bfd *ibfd,
8004 asection *isec,
8005 bfd *obfd,
8006 asection *osec,
8007 struct bfd_link_info *link_info)
8008
8009 {
8010 Elf_Internal_Shdr *ihdr, *ohdr;
8011 bool final_link = (link_info != NULL
8012 && !bfd_link_relocatable (link_info));
8013
8014 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8015 || obfd->xvec->flavour != bfd_target_elf_flavour)
8016 return true;
8017
8018 BFD_ASSERT (elf_section_data (osec) != NULL);
8019
8020 /* If this is a known ABI section, ELF section type and flags may
8021 have been set up when OSEC was created. For normal sections we
8022 allow the user to override the type and flags other than
8023 SHF_MASKOS and SHF_MASKPROC. */
8024 if (elf_section_type (osec) == SHT_PROGBITS
8025 || elf_section_type (osec) == SHT_NOTE
8026 || elf_section_type (osec) == SHT_NOBITS)
8027 elf_section_type (osec) = SHT_NULL;
8028 /* For objcopy and relocatable link, copy the ELF section type from
8029 the input file if the BFD section flags are the same. (If they
8030 are different the user may be doing something like
8031 "objcopy --set-section-flags .text=alloc,data".) For a final
8032 link allow some flags that the linker clears to differ. */
8033 if (elf_section_type (osec) == SHT_NULL
8034 && (osec->flags == isec->flags
8035 || (final_link
8036 && ((osec->flags ^ isec->flags)
8037 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8038 elf_section_type (osec) = elf_section_type (isec);
8039
8040 /* FIXME: Is this correct for all OS/PROC specific flags? */
8041 elf_section_flags (osec) = (elf_section_flags (isec)
8042 & (SHF_MASKOS | SHF_MASKPROC));
8043
8044 /* Copy sh_info from input for mbind section. */
8045 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8046 && elf_section_flags (isec) & SHF_GNU_MBIND)
8047 elf_section_data (osec)->this_hdr.sh_info
8048 = elf_section_data (isec)->this_hdr.sh_info;
8049
8050 /* Set things up for objcopy and relocatable link. The output
8051 SHT_GROUP section will have its elf_next_in_group pointing back
8052 to the input group members. Ignore linker created group section.
8053 See elfNN_ia64_object_p in elfxx-ia64.c. */
8054 if ((link_info == NULL
8055 || !link_info->resolve_section_groups)
8056 && (elf_sec_group (isec) == NULL
8057 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8058 {
8059 if (elf_section_flags (isec) & SHF_GROUP)
8060 elf_section_flags (osec) |= SHF_GROUP;
8061 elf_next_in_group (osec) = elf_next_in_group (isec);
8062 elf_section_data (osec)->group = elf_section_data (isec)->group;
8063 }
8064
8065 /* If not decompress, preserve SHF_COMPRESSED. */
8066 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8067 elf_section_flags (osec) |= (elf_section_flags (isec)
8068 & SHF_COMPRESSED);
8069
8070 ihdr = &elf_section_data (isec)->this_hdr;
8071
8072 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8073 don't use the output section of the linked-to section since it
8074 may be NULL at this point. */
8075 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8076 {
8077 ohdr = &elf_section_data (osec)->this_hdr;
8078 ohdr->sh_flags |= SHF_LINK_ORDER;
8079 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8080 }
8081
8082 osec->use_rela_p = isec->use_rela_p;
8083
8084 return true;
8085 }
8086
8087 /* Copy private section information. This copies over the entsize
8088 field, and sometimes the info field. */
8089
8090 bool
8091 _bfd_elf_copy_private_section_data (bfd *ibfd,
8092 asection *isec,
8093 bfd *obfd,
8094 asection *osec)
8095 {
8096 Elf_Internal_Shdr *ihdr, *ohdr;
8097
8098 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8099 || obfd->xvec->flavour != bfd_target_elf_flavour)
8100 return true;
8101
8102 ihdr = &elf_section_data (isec)->this_hdr;
8103 ohdr = &elf_section_data (osec)->this_hdr;
8104
8105 ohdr->sh_entsize = ihdr->sh_entsize;
8106
8107 if (ihdr->sh_type == SHT_SYMTAB
8108 || ihdr->sh_type == SHT_DYNSYM
8109 || ihdr->sh_type == SHT_GNU_verneed
8110 || ihdr->sh_type == SHT_GNU_verdef)
8111 ohdr->sh_info = ihdr->sh_info;
8112
8113 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8114 NULL);
8115 }
8116
8117 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8118 necessary if we are removing either the SHT_GROUP section or any of
8119 the group member sections. DISCARDED is the value that a section's
8120 output_section has if the section will be discarded, NULL when this
8121 function is called from objcopy, bfd_abs_section_ptr when called
8122 from the linker. */
8123
8124 bool
8125 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8126 {
8127 asection *isec;
8128
8129 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8130 if (elf_section_type (isec) == SHT_GROUP)
8131 {
8132 asection *first = elf_next_in_group (isec);
8133 asection *s = first;
8134 bfd_size_type removed = 0;
8135
8136 while (s != NULL)
8137 {
8138 /* If this member section is being output but the
8139 SHT_GROUP section is not, then clear the group info
8140 set up by _bfd_elf_copy_private_section_data. */
8141 if (s->output_section != discarded
8142 && isec->output_section == discarded)
8143 {
8144 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8145 elf_group_name (s->output_section) = NULL;
8146 }
8147 else
8148 {
8149 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8150 if (s->output_section == discarded
8151 && isec->output_section != discarded)
8152 {
8153 /* Conversely, if the member section is not being
8154 output but the SHT_GROUP section is, then adjust
8155 its size. */
8156 removed += 4;
8157 if (elf_sec->rel.hdr != NULL
8158 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8159 removed += 4;
8160 if (elf_sec->rela.hdr != NULL
8161 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8162 removed += 4;
8163 }
8164 else
8165 {
8166 /* Also adjust for zero-sized relocation member
8167 section. */
8168 if (elf_sec->rel.hdr != NULL
8169 && elf_sec->rel.hdr->sh_size == 0)
8170 removed += 4;
8171 if (elf_sec->rela.hdr != NULL
8172 && elf_sec->rela.hdr->sh_size == 0)
8173 removed += 4;
8174 }
8175 }
8176 s = elf_next_in_group (s);
8177 if (s == first)
8178 break;
8179 }
8180 if (removed != 0)
8181 {
8182 if (discarded != NULL)
8183 {
8184 /* If we've been called for ld -r, then we need to
8185 adjust the input section size. */
8186 if (isec->rawsize == 0)
8187 isec->rawsize = isec->size;
8188 isec->size = isec->rawsize - removed;
8189 if (isec->size <= 4)
8190 {
8191 isec->size = 0;
8192 isec->flags |= SEC_EXCLUDE;
8193 }
8194 }
8195 else if (isec->output_section != NULL)
8196 {
8197 /* Adjust the output section size when called from
8198 objcopy. */
8199 isec->output_section->size -= removed;
8200 if (isec->output_section->size <= 4)
8201 {
8202 isec->output_section->size = 0;
8203 isec->output_section->flags |= SEC_EXCLUDE;
8204 }
8205 }
8206 }
8207 }
8208
8209 return true;
8210 }
8211
8212 /* Copy private header information. */
8213
8214 bool
8215 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8216 {
8217 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8218 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8219 return true;
8220
8221 /* Copy over private BFD data if it has not already been copied.
8222 This must be done here, rather than in the copy_private_bfd_data
8223 entry point, because the latter is called after the section
8224 contents have been set, which means that the program headers have
8225 already been worked out. */
8226 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8227 {
8228 if (! copy_private_bfd_data (ibfd, obfd))
8229 return false;
8230 }
8231
8232 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8233 }
8234
8235 /* Copy private symbol information. If this symbol is in a section
8236 which we did not map into a BFD section, try to map the section
8237 index correctly. We use special macro definitions for the mapped
8238 section indices; these definitions are interpreted by the
8239 swap_out_syms function. */
8240
8241 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8242 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8243 #define MAP_STRTAB (SHN_HIOS + 3)
8244 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8245 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8246
8247 bool
8248 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8249 asymbol *isymarg,
8250 bfd *obfd,
8251 asymbol *osymarg)
8252 {
8253 elf_symbol_type *isym, *osym;
8254
8255 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8256 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8257 return true;
8258
8259 isym = elf_symbol_from (isymarg);
8260 osym = elf_symbol_from (osymarg);
8261
8262 if (isym != NULL
8263 && isym->internal_elf_sym.st_shndx != 0
8264 && osym != NULL
8265 && bfd_is_abs_section (isym->symbol.section))
8266 {
8267 unsigned int shndx;
8268
8269 shndx = isym->internal_elf_sym.st_shndx;
8270 if (shndx == elf_onesymtab (ibfd))
8271 shndx = MAP_ONESYMTAB;
8272 else if (shndx == elf_dynsymtab (ibfd))
8273 shndx = MAP_DYNSYMTAB;
8274 else if (shndx == elf_strtab_sec (ibfd))
8275 shndx = MAP_STRTAB;
8276 else if (shndx == elf_shstrtab_sec (ibfd))
8277 shndx = MAP_SHSTRTAB;
8278 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8279 shndx = MAP_SYM_SHNDX;
8280 osym->internal_elf_sym.st_shndx = shndx;
8281 }
8282
8283 return true;
8284 }
8285
8286 /* Swap out the symbols. */
8287
8288 static bool
8289 swap_out_syms (bfd *abfd,
8290 struct elf_strtab_hash **sttp,
8291 int relocatable_p,
8292 struct bfd_link_info *info)
8293 {
8294 const struct elf_backend_data *bed;
8295 unsigned int symcount;
8296 asymbol **syms;
8297 struct elf_strtab_hash *stt;
8298 Elf_Internal_Shdr *symtab_hdr;
8299 Elf_Internal_Shdr *symtab_shndx_hdr;
8300 Elf_Internal_Shdr *symstrtab_hdr;
8301 struct elf_sym_strtab *symstrtab;
8302 bfd_byte *outbound_syms;
8303 bfd_byte *outbound_shndx;
8304 unsigned long outbound_syms_index;
8305 unsigned int idx;
8306 unsigned int num_locals;
8307 size_t amt;
8308 bool name_local_sections;
8309
8310 if (!elf_map_symbols (abfd, &num_locals))
8311 return false;
8312
8313 /* Dump out the symtabs. */
8314 stt = _bfd_elf_strtab_init ();
8315 if (stt == NULL)
8316 return false;
8317
8318 bed = get_elf_backend_data (abfd);
8319 symcount = bfd_get_symcount (abfd);
8320 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8321 symtab_hdr->sh_type = SHT_SYMTAB;
8322 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8323 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8324 symtab_hdr->sh_info = num_locals + 1;
8325 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8326
8327 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8328 symstrtab_hdr->sh_type = SHT_STRTAB;
8329
8330 /* Allocate buffer to swap out the .strtab section. */
8331 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8332 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8333 {
8334 bfd_set_error (bfd_error_no_memory);
8335 _bfd_elf_strtab_free (stt);
8336 return false;
8337 }
8338
8339 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8340 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8341 {
8342 error_no_mem:
8343 bfd_set_error (bfd_error_no_memory);
8344 error_return:
8345 free (symstrtab);
8346 _bfd_elf_strtab_free (stt);
8347 return false;
8348 }
8349 symtab_hdr->contents = outbound_syms;
8350 outbound_syms_index = 0;
8351
8352 outbound_shndx = NULL;
8353
8354 if (elf_symtab_shndx_list (abfd))
8355 {
8356 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8357 if (symtab_shndx_hdr->sh_name != 0)
8358 {
8359 if (_bfd_mul_overflow (symcount + 1,
8360 sizeof (Elf_External_Sym_Shndx), &amt))
8361 goto error_no_mem;
8362 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8363 if (outbound_shndx == NULL)
8364 goto error_return;
8365
8366 symtab_shndx_hdr->contents = outbound_shndx;
8367 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8368 symtab_shndx_hdr->sh_size = amt;
8369 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8370 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8371 }
8372 /* FIXME: What about any other headers in the list ? */
8373 }
8374
8375 /* Now generate the data (for "contents"). */
8376 {
8377 /* Fill in zeroth symbol and swap it out. */
8378 Elf_Internal_Sym sym;
8379 sym.st_name = 0;
8380 sym.st_value = 0;
8381 sym.st_size = 0;
8382 sym.st_info = 0;
8383 sym.st_other = 0;
8384 sym.st_shndx = SHN_UNDEF;
8385 sym.st_target_internal = 0;
8386 symstrtab[0].sym = sym;
8387 symstrtab[0].dest_index = outbound_syms_index;
8388 outbound_syms_index++;
8389 }
8390
8391 name_local_sections
8392 = (bed->elf_backend_name_local_section_symbols
8393 && bed->elf_backend_name_local_section_symbols (abfd));
8394
8395 syms = bfd_get_outsymbols (abfd);
8396 for (idx = 0; idx < symcount;)
8397 {
8398 Elf_Internal_Sym sym;
8399 bfd_vma value = syms[idx]->value;
8400 elf_symbol_type *type_ptr;
8401 flagword flags = syms[idx]->flags;
8402 int type;
8403
8404 if (!name_local_sections
8405 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8406 {
8407 /* Local section symbols have no name. */
8408 sym.st_name = (unsigned long) -1;
8409 }
8410 else
8411 {
8412 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8413 to get the final offset for st_name. */
8414 sym.st_name
8415 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8416 false);
8417 if (sym.st_name == (unsigned long) -1)
8418 goto error_return;
8419 }
8420
8421 type_ptr = elf_symbol_from (syms[idx]);
8422
8423 if ((flags & BSF_SECTION_SYM) == 0
8424 && bfd_is_com_section (syms[idx]->section))
8425 {
8426 /* ELF common symbols put the alignment into the `value' field,
8427 and the size into the `size' field. This is backwards from
8428 how BFD handles it, so reverse it here. */
8429 sym.st_size = value;
8430 if (type_ptr == NULL
8431 || type_ptr->internal_elf_sym.st_value == 0)
8432 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8433 else
8434 sym.st_value = type_ptr->internal_elf_sym.st_value;
8435 sym.st_shndx = _bfd_elf_section_from_bfd_section
8436 (abfd, syms[idx]->section);
8437 }
8438 else
8439 {
8440 asection *sec = syms[idx]->section;
8441 unsigned int shndx;
8442
8443 if (sec->output_section)
8444 {
8445 value += sec->output_offset;
8446 sec = sec->output_section;
8447 }
8448
8449 /* Don't add in the section vma for relocatable output. */
8450 if (! relocatable_p)
8451 value += sec->vma;
8452 sym.st_value = value;
8453 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8454
8455 if (bfd_is_abs_section (sec)
8456 && type_ptr != NULL
8457 && type_ptr->internal_elf_sym.st_shndx != 0)
8458 {
8459 /* This symbol is in a real ELF section which we did
8460 not create as a BFD section. Undo the mapping done
8461 by copy_private_symbol_data. */
8462 shndx = type_ptr->internal_elf_sym.st_shndx;
8463 switch (shndx)
8464 {
8465 case MAP_ONESYMTAB:
8466 shndx = elf_onesymtab (abfd);
8467 break;
8468 case MAP_DYNSYMTAB:
8469 shndx = elf_dynsymtab (abfd);
8470 break;
8471 case MAP_STRTAB:
8472 shndx = elf_strtab_sec (abfd);
8473 break;
8474 case MAP_SHSTRTAB:
8475 shndx = elf_shstrtab_sec (abfd);
8476 break;
8477 case MAP_SYM_SHNDX:
8478 if (elf_symtab_shndx_list (abfd))
8479 shndx = elf_symtab_shndx_list (abfd)->ndx;
8480 break;
8481 case SHN_COMMON:
8482 case SHN_ABS:
8483 shndx = SHN_ABS;
8484 break;
8485 default:
8486 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8487 {
8488 if (bed->symbol_section_index)
8489 shndx = bed->symbol_section_index (abfd, type_ptr);
8490 /* Otherwise just leave the index alone. */
8491 }
8492 else
8493 {
8494 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8495 _bfd_error_handler (_("%pB: \
8496 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8497 abfd, shndx);
8498 shndx = SHN_ABS;
8499 }
8500 break;
8501 }
8502 }
8503 else
8504 {
8505 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8506
8507 if (shndx == SHN_BAD)
8508 {
8509 asection *sec2;
8510
8511 /* Writing this would be a hell of a lot easier if
8512 we had some decent documentation on bfd, and
8513 knew what to expect of the library, and what to
8514 demand of applications. For example, it
8515 appears that `objcopy' might not set the
8516 section of a symbol to be a section that is
8517 actually in the output file. */
8518 sec2 = bfd_get_section_by_name (abfd, sec->name);
8519 if (sec2 != NULL)
8520 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8521 if (shndx == SHN_BAD)
8522 {
8523 /* xgettext:c-format */
8524 _bfd_error_handler
8525 (_("unable to find equivalent output section"
8526 " for symbol '%s' from section '%s'"),
8527 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8528 sec->name);
8529 bfd_set_error (bfd_error_invalid_operation);
8530 goto error_return;
8531 }
8532 }
8533 }
8534
8535 sym.st_shndx = shndx;
8536 }
8537
8538 if ((flags & BSF_THREAD_LOCAL) != 0)
8539 type = STT_TLS;
8540 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8541 type = STT_GNU_IFUNC;
8542 else if ((flags & BSF_FUNCTION) != 0)
8543 type = STT_FUNC;
8544 else if ((flags & BSF_OBJECT) != 0)
8545 type = STT_OBJECT;
8546 else if ((flags & BSF_RELC) != 0)
8547 type = STT_RELC;
8548 else if ((flags & BSF_SRELC) != 0)
8549 type = STT_SRELC;
8550 else
8551 type = STT_NOTYPE;
8552
8553 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8554 type = STT_TLS;
8555
8556 /* Processor-specific types. */
8557 if (type_ptr != NULL
8558 && bed->elf_backend_get_symbol_type)
8559 type = ((*bed->elf_backend_get_symbol_type)
8560 (&type_ptr->internal_elf_sym, type));
8561
8562 if (flags & BSF_SECTION_SYM)
8563 {
8564 if (flags & BSF_GLOBAL)
8565 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8566 else
8567 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8568 }
8569 else if (bfd_is_com_section (syms[idx]->section))
8570 {
8571 if (type != STT_TLS)
8572 {
8573 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8574 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8575 ? STT_COMMON : STT_OBJECT);
8576 else
8577 type = ((flags & BSF_ELF_COMMON) != 0
8578 ? STT_COMMON : STT_OBJECT);
8579 }
8580 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8581 }
8582 else if (bfd_is_und_section (syms[idx]->section))
8583 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8584 ? STB_WEAK
8585 : STB_GLOBAL),
8586 type);
8587 else if (flags & BSF_FILE)
8588 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8589 else
8590 {
8591 int bind = STB_LOCAL;
8592
8593 if (flags & BSF_LOCAL)
8594 bind = STB_LOCAL;
8595 else if (flags & BSF_GNU_UNIQUE)
8596 bind = STB_GNU_UNIQUE;
8597 else if (flags & BSF_WEAK)
8598 bind = STB_WEAK;
8599 else if (flags & BSF_GLOBAL)
8600 bind = STB_GLOBAL;
8601
8602 sym.st_info = ELF_ST_INFO (bind, type);
8603 }
8604
8605 if (type_ptr != NULL)
8606 {
8607 sym.st_other = type_ptr->internal_elf_sym.st_other;
8608 sym.st_target_internal
8609 = type_ptr->internal_elf_sym.st_target_internal;
8610 }
8611 else
8612 {
8613 sym.st_other = 0;
8614 sym.st_target_internal = 0;
8615 }
8616
8617 idx++;
8618 symstrtab[idx].sym = sym;
8619 symstrtab[idx].dest_index = outbound_syms_index;
8620
8621 outbound_syms_index++;
8622 }
8623
8624 /* Finalize the .strtab section. */
8625 _bfd_elf_strtab_finalize (stt);
8626
8627 /* Swap out the .strtab section. */
8628 for (idx = 0; idx <= symcount; idx++)
8629 {
8630 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8631 if (elfsym->sym.st_name == (unsigned long) -1)
8632 elfsym->sym.st_name = 0;
8633 else
8634 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8635 elfsym->sym.st_name);
8636 if (info && info->callbacks->ctf_new_symbol)
8637 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8638 &elfsym->sym);
8639
8640 /* Inform the linker of the addition of this symbol. */
8641
8642 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8643 (outbound_syms
8644 + (elfsym->dest_index
8645 * bed->s->sizeof_sym)),
8646 NPTR_ADD (outbound_shndx,
8647 (elfsym->dest_index
8648 * sizeof (Elf_External_Sym_Shndx))));
8649 }
8650 free (symstrtab);
8651
8652 *sttp = stt;
8653 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8654 symstrtab_hdr->sh_type = SHT_STRTAB;
8655 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8656 symstrtab_hdr->sh_addr = 0;
8657 symstrtab_hdr->sh_entsize = 0;
8658 symstrtab_hdr->sh_link = 0;
8659 symstrtab_hdr->sh_info = 0;
8660 symstrtab_hdr->sh_addralign = 1;
8661
8662 return true;
8663 }
8664
8665 /* Return the number of bytes required to hold the symtab vector.
8666
8667 Note that we base it on the count plus 1, since we will null terminate
8668 the vector allocated based on this size. However, the ELF symbol table
8669 always has a dummy entry as symbol #0, so it ends up even. */
8670
8671 long
8672 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8673 {
8674 bfd_size_type symcount;
8675 long symtab_size;
8676 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8677
8678 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8679 if (symcount > LONG_MAX / sizeof (asymbol *))
8680 {
8681 bfd_set_error (bfd_error_file_too_big);
8682 return -1;
8683 }
8684 symtab_size = symcount * (sizeof (asymbol *));
8685 if (symcount == 0)
8686 symtab_size = sizeof (asymbol *);
8687 else if (!bfd_write_p (abfd))
8688 {
8689 ufile_ptr filesize = bfd_get_file_size (abfd);
8690
8691 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8692 {
8693 bfd_set_error (bfd_error_file_truncated);
8694 return -1;
8695 }
8696 }
8697
8698 return symtab_size;
8699 }
8700
8701 long
8702 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8703 {
8704 bfd_size_type symcount;
8705 long symtab_size;
8706 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8707
8708 if (elf_dynsymtab (abfd) == 0)
8709 {
8710 bfd_set_error (bfd_error_invalid_operation);
8711 return -1;
8712 }
8713
8714 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8715 if (symcount > LONG_MAX / sizeof (asymbol *))
8716 {
8717 bfd_set_error (bfd_error_file_too_big);
8718 return -1;
8719 }
8720 symtab_size = symcount * (sizeof (asymbol *));
8721 if (symcount == 0)
8722 symtab_size = sizeof (asymbol *);
8723 else if (!bfd_write_p (abfd))
8724 {
8725 ufile_ptr filesize = bfd_get_file_size (abfd);
8726
8727 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8728 {
8729 bfd_set_error (bfd_error_file_truncated);
8730 return -1;
8731 }
8732 }
8733
8734 return symtab_size;
8735 }
8736
8737 long
8738 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8739 {
8740 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8741 {
8742 /* Sanity check reloc section size. */
8743 ufile_ptr filesize = bfd_get_file_size (abfd);
8744
8745 if (filesize != 0)
8746 {
8747 struct bfd_elf_section_data *d = elf_section_data (asect);
8748 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
8749 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
8750
8751 if (rel_size + rela_size > filesize
8752 || rel_size + rela_size < rel_size)
8753 {
8754 bfd_set_error (bfd_error_file_truncated);
8755 return -1;
8756 }
8757 }
8758 }
8759
8760 #if SIZEOF_LONG == SIZEOF_INT
8761 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8762 {
8763 bfd_set_error (bfd_error_file_too_big);
8764 return -1;
8765 }
8766 #endif
8767 return (asect->reloc_count + 1L) * sizeof (arelent *);
8768 }
8769
8770 /* Canonicalize the relocs. */
8771
8772 long
8773 _bfd_elf_canonicalize_reloc (bfd *abfd,
8774 sec_ptr section,
8775 arelent **relptr,
8776 asymbol **symbols)
8777 {
8778 arelent *tblptr;
8779 unsigned int i;
8780 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8781
8782 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8783 return -1;
8784
8785 tblptr = section->relocation;
8786 for (i = 0; i < section->reloc_count; i++)
8787 *relptr++ = tblptr++;
8788
8789 *relptr = NULL;
8790
8791 return section->reloc_count;
8792 }
8793
8794 long
8795 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8796 {
8797 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8798 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8799
8800 if (symcount >= 0)
8801 abfd->symcount = symcount;
8802 return symcount;
8803 }
8804
8805 long
8806 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8807 asymbol **allocation)
8808 {
8809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8810 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8811
8812 if (symcount >= 0)
8813 abfd->dynsymcount = symcount;
8814 return symcount;
8815 }
8816
8817 /* Return the size required for the dynamic reloc entries. Any loadable
8818 section that was actually installed in the BFD, and has type SHT_REL
8819 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8820 dynamic reloc section. */
8821
8822 long
8823 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8824 {
8825 bfd_size_type count, ext_rel_size;
8826 asection *s;
8827
8828 if (elf_dynsymtab (abfd) == 0)
8829 {
8830 bfd_set_error (bfd_error_invalid_operation);
8831 return -1;
8832 }
8833
8834 count = 1;
8835 ext_rel_size = 0;
8836 for (s = abfd->sections; s != NULL; s = s->next)
8837 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8838 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8839 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8840 {
8841 ext_rel_size += s->size;
8842 if (ext_rel_size < s->size)
8843 {
8844 bfd_set_error (bfd_error_file_truncated);
8845 return -1;
8846 }
8847 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8848 if (count > LONG_MAX / sizeof (arelent *))
8849 {
8850 bfd_set_error (bfd_error_file_too_big);
8851 return -1;
8852 }
8853 }
8854 if (count > 1 && !bfd_write_p (abfd))
8855 {
8856 /* Sanity check reloc section sizes. */
8857 ufile_ptr filesize = bfd_get_file_size (abfd);
8858 if (filesize != 0 && ext_rel_size > filesize)
8859 {
8860 bfd_set_error (bfd_error_file_truncated);
8861 return -1;
8862 }
8863 }
8864 return count * sizeof (arelent *);
8865 }
8866
8867 /* Canonicalize the dynamic relocation entries. Note that we return the
8868 dynamic relocations as a single block, although they are actually
8869 associated with particular sections; the interface, which was
8870 designed for SunOS style shared libraries, expects that there is only
8871 one set of dynamic relocs. Any loadable section that was actually
8872 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8873 dynamic symbol table, is considered to be a dynamic reloc section. */
8874
8875 long
8876 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8877 arelent **storage,
8878 asymbol **syms)
8879 {
8880 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8881 asection *s;
8882 long ret;
8883
8884 if (elf_dynsymtab (abfd) == 0)
8885 {
8886 bfd_set_error (bfd_error_invalid_operation);
8887 return -1;
8888 }
8889
8890 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8891 ret = 0;
8892 for (s = abfd->sections; s != NULL; s = s->next)
8893 {
8894 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8895 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8896 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8897 {
8898 arelent *p;
8899 long count, i;
8900
8901 if (! (*slurp_relocs) (abfd, s, syms, true))
8902 return -1;
8903 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8904 p = s->relocation;
8905 for (i = 0; i < count; i++)
8906 *storage++ = p++;
8907 ret += count;
8908 }
8909 }
8910
8911 *storage = NULL;
8912
8913 return ret;
8914 }
8915 \f
8916 /* Read in the version information. */
8917
8918 bool
8919 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8920 {
8921 bfd_byte *contents = NULL;
8922 unsigned int freeidx = 0;
8923 size_t amt;
8924
8925 if (elf_dynverref (abfd) != 0)
8926 {
8927 Elf_Internal_Shdr *hdr;
8928 Elf_External_Verneed *everneed;
8929 Elf_Internal_Verneed *iverneed;
8930 unsigned int i;
8931 bfd_byte *contents_end;
8932
8933 hdr = &elf_tdata (abfd)->dynverref_hdr;
8934
8935 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8936 {
8937 error_return_bad_verref:
8938 _bfd_error_handler
8939 (_("%pB: .gnu.version_r invalid entry"), abfd);
8940 bfd_set_error (bfd_error_bad_value);
8941 error_return_verref:
8942 elf_tdata (abfd)->verref = NULL;
8943 elf_tdata (abfd)->cverrefs = 0;
8944 goto error_return;
8945 }
8946
8947 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8948 goto error_return_verref;
8949 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8950 if (contents == NULL)
8951 goto error_return_verref;
8952
8953 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8954 {
8955 bfd_set_error (bfd_error_file_too_big);
8956 goto error_return_verref;
8957 }
8958 if (amt == 0)
8959 goto error_return_verref;
8960 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
8961 if (elf_tdata (abfd)->verref == NULL)
8962 goto error_return_verref;
8963
8964 BFD_ASSERT (sizeof (Elf_External_Verneed)
8965 == sizeof (Elf_External_Vernaux));
8966 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8967 everneed = (Elf_External_Verneed *) contents;
8968 iverneed = elf_tdata (abfd)->verref;
8969 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8970 {
8971 Elf_External_Vernaux *evernaux;
8972 Elf_Internal_Vernaux *ivernaux;
8973 unsigned int j;
8974
8975 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8976
8977 iverneed->vn_bfd = abfd;
8978
8979 iverneed->vn_filename =
8980 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8981 iverneed->vn_file);
8982 if (iverneed->vn_filename == NULL)
8983 goto error_return_bad_verref;
8984
8985 if (iverneed->vn_cnt == 0)
8986 iverneed->vn_auxptr = NULL;
8987 else
8988 {
8989 if (_bfd_mul_overflow (iverneed->vn_cnt,
8990 sizeof (Elf_Internal_Vernaux), &amt))
8991 {
8992 bfd_set_error (bfd_error_file_too_big);
8993 goto error_return_verref;
8994 }
8995 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8996 bfd_alloc (abfd, amt);
8997 if (iverneed->vn_auxptr == NULL)
8998 goto error_return_verref;
8999 }
9000
9001 if (iverneed->vn_aux
9002 > (size_t) (contents_end - (bfd_byte *) everneed))
9003 goto error_return_bad_verref;
9004
9005 evernaux = ((Elf_External_Vernaux *)
9006 ((bfd_byte *) everneed + iverneed->vn_aux));
9007 ivernaux = iverneed->vn_auxptr;
9008 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9009 {
9010 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9011
9012 ivernaux->vna_nodename =
9013 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9014 ivernaux->vna_name);
9015 if (ivernaux->vna_nodename == NULL)
9016 goto error_return_bad_verref;
9017
9018 if (ivernaux->vna_other > freeidx)
9019 freeidx = ivernaux->vna_other;
9020
9021 ivernaux->vna_nextptr = NULL;
9022 if (ivernaux->vna_next == 0)
9023 {
9024 iverneed->vn_cnt = j + 1;
9025 break;
9026 }
9027 if (j + 1 < iverneed->vn_cnt)
9028 ivernaux->vna_nextptr = ivernaux + 1;
9029
9030 if (ivernaux->vna_next
9031 > (size_t) (contents_end - (bfd_byte *) evernaux))
9032 goto error_return_bad_verref;
9033
9034 evernaux = ((Elf_External_Vernaux *)
9035 ((bfd_byte *) evernaux + ivernaux->vna_next));
9036 }
9037
9038 iverneed->vn_nextref = NULL;
9039 if (iverneed->vn_next == 0)
9040 break;
9041 if (i + 1 < hdr->sh_info)
9042 iverneed->vn_nextref = iverneed + 1;
9043
9044 if (iverneed->vn_next
9045 > (size_t) (contents_end - (bfd_byte *) everneed))
9046 goto error_return_bad_verref;
9047
9048 everneed = ((Elf_External_Verneed *)
9049 ((bfd_byte *) everneed + iverneed->vn_next));
9050 }
9051 elf_tdata (abfd)->cverrefs = i;
9052
9053 free (contents);
9054 contents = NULL;
9055 }
9056
9057 if (elf_dynverdef (abfd) != 0)
9058 {
9059 Elf_Internal_Shdr *hdr;
9060 Elf_External_Verdef *everdef;
9061 Elf_Internal_Verdef *iverdef;
9062 Elf_Internal_Verdef *iverdefarr;
9063 Elf_Internal_Verdef iverdefmem;
9064 unsigned int i;
9065 unsigned int maxidx;
9066 bfd_byte *contents_end_def, *contents_end_aux;
9067
9068 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9069
9070 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9071 {
9072 error_return_bad_verdef:
9073 _bfd_error_handler
9074 (_("%pB: .gnu.version_d invalid entry"), abfd);
9075 bfd_set_error (bfd_error_bad_value);
9076 error_return_verdef:
9077 elf_tdata (abfd)->verdef = NULL;
9078 elf_tdata (abfd)->cverdefs = 0;
9079 goto error_return;
9080 }
9081
9082 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9083 goto error_return_verdef;
9084 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9085 if (contents == NULL)
9086 goto error_return_verdef;
9087
9088 BFD_ASSERT (sizeof (Elf_External_Verdef)
9089 >= sizeof (Elf_External_Verdaux));
9090 contents_end_def = contents + hdr->sh_size
9091 - sizeof (Elf_External_Verdef);
9092 contents_end_aux = contents + hdr->sh_size
9093 - sizeof (Elf_External_Verdaux);
9094
9095 /* We know the number of entries in the section but not the maximum
9096 index. Therefore we have to run through all entries and find
9097 the maximum. */
9098 everdef = (Elf_External_Verdef *) contents;
9099 maxidx = 0;
9100 for (i = 0; i < hdr->sh_info; ++i)
9101 {
9102 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9103
9104 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9105 goto error_return_bad_verdef;
9106 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9107 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9108
9109 if (iverdefmem.vd_next == 0)
9110 break;
9111
9112 if (iverdefmem.vd_next
9113 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9114 goto error_return_bad_verdef;
9115
9116 everdef = ((Elf_External_Verdef *)
9117 ((bfd_byte *) everdef + iverdefmem.vd_next));
9118 }
9119
9120 if (default_imported_symver)
9121 {
9122 if (freeidx > maxidx)
9123 maxidx = ++freeidx;
9124 else
9125 freeidx = ++maxidx;
9126 }
9127 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9128 {
9129 bfd_set_error (bfd_error_file_too_big);
9130 goto error_return_verdef;
9131 }
9132 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9133 if (elf_tdata (abfd)->verdef == NULL)
9134 goto error_return_verdef;
9135
9136 elf_tdata (abfd)->cverdefs = maxidx;
9137
9138 everdef = (Elf_External_Verdef *) contents;
9139 iverdefarr = elf_tdata (abfd)->verdef;
9140 for (i = 0; i < hdr->sh_info; i++)
9141 {
9142 Elf_External_Verdaux *everdaux;
9143 Elf_Internal_Verdaux *iverdaux;
9144 unsigned int j;
9145
9146 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9147
9148 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9149 goto error_return_bad_verdef;
9150
9151 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9152 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9153
9154 iverdef->vd_bfd = abfd;
9155
9156 if (iverdef->vd_cnt == 0)
9157 iverdef->vd_auxptr = NULL;
9158 else
9159 {
9160 if (_bfd_mul_overflow (iverdef->vd_cnt,
9161 sizeof (Elf_Internal_Verdaux), &amt))
9162 {
9163 bfd_set_error (bfd_error_file_too_big);
9164 goto error_return_verdef;
9165 }
9166 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9167 bfd_alloc (abfd, amt);
9168 if (iverdef->vd_auxptr == NULL)
9169 goto error_return_verdef;
9170 }
9171
9172 if (iverdef->vd_aux
9173 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9174 goto error_return_bad_verdef;
9175
9176 everdaux = ((Elf_External_Verdaux *)
9177 ((bfd_byte *) everdef + iverdef->vd_aux));
9178 iverdaux = iverdef->vd_auxptr;
9179 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9180 {
9181 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9182
9183 iverdaux->vda_nodename =
9184 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9185 iverdaux->vda_name);
9186 if (iverdaux->vda_nodename == NULL)
9187 goto error_return_bad_verdef;
9188
9189 iverdaux->vda_nextptr = NULL;
9190 if (iverdaux->vda_next == 0)
9191 {
9192 iverdef->vd_cnt = j + 1;
9193 break;
9194 }
9195 if (j + 1 < iverdef->vd_cnt)
9196 iverdaux->vda_nextptr = iverdaux + 1;
9197
9198 if (iverdaux->vda_next
9199 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9200 goto error_return_bad_verdef;
9201
9202 everdaux = ((Elf_External_Verdaux *)
9203 ((bfd_byte *) everdaux + iverdaux->vda_next));
9204 }
9205
9206 iverdef->vd_nodename = NULL;
9207 if (iverdef->vd_cnt)
9208 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9209
9210 iverdef->vd_nextdef = NULL;
9211 if (iverdef->vd_next == 0)
9212 break;
9213 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9214 iverdef->vd_nextdef = iverdef + 1;
9215
9216 everdef = ((Elf_External_Verdef *)
9217 ((bfd_byte *) everdef + iverdef->vd_next));
9218 }
9219
9220 free (contents);
9221 contents = NULL;
9222 }
9223 else if (default_imported_symver)
9224 {
9225 if (freeidx < 3)
9226 freeidx = 3;
9227 else
9228 freeidx++;
9229
9230 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9231 {
9232 bfd_set_error (bfd_error_file_too_big);
9233 goto error_return;
9234 }
9235 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9236 if (elf_tdata (abfd)->verdef == NULL)
9237 goto error_return;
9238
9239 elf_tdata (abfd)->cverdefs = freeidx;
9240 }
9241
9242 /* Create a default version based on the soname. */
9243 if (default_imported_symver)
9244 {
9245 Elf_Internal_Verdef *iverdef;
9246 Elf_Internal_Verdaux *iverdaux;
9247
9248 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9249
9250 iverdef->vd_version = VER_DEF_CURRENT;
9251 iverdef->vd_flags = 0;
9252 iverdef->vd_ndx = freeidx;
9253 iverdef->vd_cnt = 1;
9254
9255 iverdef->vd_bfd = abfd;
9256
9257 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9258 if (iverdef->vd_nodename == NULL)
9259 goto error_return_verdef;
9260 iverdef->vd_nextdef = NULL;
9261 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9262 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9263 if (iverdef->vd_auxptr == NULL)
9264 goto error_return_verdef;
9265
9266 iverdaux = iverdef->vd_auxptr;
9267 iverdaux->vda_nodename = iverdef->vd_nodename;
9268 }
9269
9270 return true;
9271
9272 error_return:
9273 free (contents);
9274 return false;
9275 }
9276 \f
9277 asymbol *
9278 _bfd_elf_make_empty_symbol (bfd *abfd)
9279 {
9280 elf_symbol_type *newsym;
9281
9282 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9283 if (!newsym)
9284 return NULL;
9285 newsym->symbol.the_bfd = abfd;
9286 return &newsym->symbol;
9287 }
9288
9289 void
9290 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9291 asymbol *symbol,
9292 symbol_info *ret)
9293 {
9294 bfd_symbol_info (symbol, ret);
9295 }
9296
9297 /* Return whether a symbol name implies a local symbol. Most targets
9298 use this function for the is_local_label_name entry point, but some
9299 override it. */
9300
9301 bool
9302 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9303 const char *name)
9304 {
9305 /* Normal local symbols start with ``.L''. */
9306 if (name[0] == '.' && name[1] == 'L')
9307 return true;
9308
9309 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9310 DWARF debugging symbols starting with ``..''. */
9311 if (name[0] == '.' && name[1] == '.')
9312 return true;
9313
9314 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9315 emitting DWARF debugging output. I suspect this is actually a
9316 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9317 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9318 underscore to be emitted on some ELF targets). For ease of use,
9319 we treat such symbols as local. */
9320 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9321 return true;
9322
9323 /* Treat assembler generated fake symbols, dollar local labels and
9324 forward-backward labels (aka local labels) as locals.
9325 These labels have the form:
9326
9327 L0^A.* (fake symbols)
9328
9329 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9330
9331 Versions which start with .L will have already been matched above,
9332 so we only need to match the rest. */
9333 if (name[0] == 'L' && ISDIGIT (name[1]))
9334 {
9335 bool ret = false;
9336 const char * p;
9337 char c;
9338
9339 for (p = name + 2; (c = *p); p++)
9340 {
9341 if (c == 1 || c == 2)
9342 {
9343 if (c == 1 && p == name + 2)
9344 /* A fake symbol. */
9345 return true;
9346
9347 /* FIXME: We are being paranoid here and treating symbols like
9348 L0^Bfoo as if there were non-local, on the grounds that the
9349 assembler will never generate them. But can any symbol
9350 containing an ASCII value in the range 1-31 ever be anything
9351 other than some kind of local ? */
9352 ret = true;
9353 }
9354
9355 if (! ISDIGIT (c))
9356 {
9357 ret = false;
9358 break;
9359 }
9360 }
9361 return ret;
9362 }
9363
9364 return false;
9365 }
9366
9367 alent *
9368 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9369 asymbol *symbol ATTRIBUTE_UNUSED)
9370 {
9371 abort ();
9372 return NULL;
9373 }
9374
9375 bool
9376 _bfd_elf_set_arch_mach (bfd *abfd,
9377 enum bfd_architecture arch,
9378 unsigned long machine)
9379 {
9380 /* If this isn't the right architecture for this backend, and this
9381 isn't the generic backend, fail. */
9382 if (arch != get_elf_backend_data (abfd)->arch
9383 && arch != bfd_arch_unknown
9384 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9385 return false;
9386
9387 return bfd_default_set_arch_mach (abfd, arch, machine);
9388 }
9389
9390 /* Find the nearest line to a particular section and offset,
9391 for error reporting. */
9392
9393 bool
9394 _bfd_elf_find_nearest_line (bfd *abfd,
9395 asymbol **symbols,
9396 asection *section,
9397 bfd_vma offset,
9398 const char **filename_ptr,
9399 const char **functionname_ptr,
9400 unsigned int *line_ptr,
9401 unsigned int *discriminator_ptr)
9402 {
9403 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9404 offset, filename_ptr,
9405 functionname_ptr, line_ptr,
9406 discriminator_ptr);
9407 }
9408
9409 /* Find the nearest line to a particular section and offset,
9410 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9411 can be optionally specified. */
9412
9413 bool
9414 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9415 const char *alt_filename,
9416 asymbol **symbols,
9417 asection *section,
9418 bfd_vma offset,
9419 const char **filename_ptr,
9420 const char **functionname_ptr,
9421 unsigned int *line_ptr,
9422 unsigned int *discriminator_ptr)
9423 {
9424 bool found;
9425
9426 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9427 section, offset, filename_ptr,
9428 functionname_ptr, line_ptr,
9429 discriminator_ptr,
9430 dwarf_debug_sections,
9431 &elf_tdata (abfd)->dwarf2_find_line_info))
9432 return true;
9433
9434 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9435 filename_ptr, functionname_ptr, line_ptr))
9436 {
9437 if (!*functionname_ptr)
9438 _bfd_elf_find_function (abfd, symbols, section, offset,
9439 *filename_ptr ? NULL : filename_ptr,
9440 functionname_ptr);
9441 return true;
9442 }
9443
9444 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9445 &found, filename_ptr,
9446 functionname_ptr, line_ptr,
9447 &elf_tdata (abfd)->line_info))
9448 return false;
9449 if (found && (*functionname_ptr || *line_ptr))
9450 return true;
9451
9452 if (symbols == NULL)
9453 return false;
9454
9455 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9456 filename_ptr, functionname_ptr))
9457 return false;
9458
9459 *line_ptr = 0;
9460 return true;
9461 }
9462
9463 /* Find the line for a symbol. */
9464
9465 bool
9466 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9467 const char **filename_ptr, unsigned int *line_ptr)
9468 {
9469 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9470 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9471 filename_ptr, NULL, line_ptr, NULL,
9472 dwarf_debug_sections,
9473 &tdata->dwarf2_find_line_info);
9474 }
9475
9476 /* After a call to bfd_find_nearest_line, successive calls to
9477 bfd_find_inliner_info can be used to get source information about
9478 each level of function inlining that terminated at the address
9479 passed to bfd_find_nearest_line. Currently this is only supported
9480 for DWARF2 with appropriate DWARF3 extensions. */
9481
9482 bool
9483 _bfd_elf_find_inliner_info (bfd *abfd,
9484 const char **filename_ptr,
9485 const char **functionname_ptr,
9486 unsigned int *line_ptr)
9487 {
9488 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9489 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9490 functionname_ptr, line_ptr,
9491 &tdata->dwarf2_find_line_info);
9492 }
9493
9494 int
9495 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9496 {
9497 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9498 int ret = bed->s->sizeof_ehdr;
9499
9500 if (!bfd_link_relocatable (info))
9501 {
9502 bfd_size_type phdr_size = elf_program_header_size (abfd);
9503
9504 if (phdr_size == (bfd_size_type) -1)
9505 {
9506 struct elf_segment_map *m;
9507
9508 phdr_size = 0;
9509 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9510 phdr_size += bed->s->sizeof_phdr;
9511
9512 if (phdr_size == 0)
9513 phdr_size = get_program_header_size (abfd, info);
9514 }
9515
9516 elf_program_header_size (abfd) = phdr_size;
9517 ret += phdr_size;
9518 }
9519
9520 return ret;
9521 }
9522
9523 bool
9524 _bfd_elf_set_section_contents (bfd *abfd,
9525 sec_ptr section,
9526 const void *location,
9527 file_ptr offset,
9528 bfd_size_type count)
9529 {
9530 Elf_Internal_Shdr *hdr;
9531
9532 if (! abfd->output_has_begun
9533 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9534 return false;
9535
9536 if (!count)
9537 return true;
9538
9539 hdr = &elf_section_data (section)->this_hdr;
9540 if (hdr->sh_offset == (file_ptr) -1)
9541 {
9542 unsigned char *contents;
9543
9544 if (bfd_section_is_ctf (section))
9545 /* Nothing to do with this section: the contents are generated
9546 later. */
9547 return true;
9548
9549 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9550 {
9551 _bfd_error_handler
9552 (_("%pB:%pA: error: attempting to write"
9553 " into an unallocated compressed section"),
9554 abfd, section);
9555 bfd_set_error (bfd_error_invalid_operation);
9556 return false;
9557 }
9558
9559 if ((offset + count) > hdr->sh_size)
9560 {
9561 _bfd_error_handler
9562 (_("%pB:%pA: error: attempting to write"
9563 " over the end of the section"),
9564 abfd, section);
9565
9566 bfd_set_error (bfd_error_invalid_operation);
9567 return false;
9568 }
9569
9570 contents = hdr->contents;
9571 if (contents == NULL)
9572 {
9573 _bfd_error_handler
9574 (_("%pB:%pA: error: attempting to write"
9575 " section into an empty buffer"),
9576 abfd, section);
9577
9578 bfd_set_error (bfd_error_invalid_operation);
9579 return false;
9580 }
9581
9582 memcpy (contents + offset, location, count);
9583 return true;
9584 }
9585
9586 return _bfd_generic_set_section_contents (abfd, section,
9587 location, offset, count);
9588 }
9589
9590 bool
9591 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9592 arelent *cache_ptr ATTRIBUTE_UNUSED,
9593 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9594 {
9595 abort ();
9596 return false;
9597 }
9598
9599 /* Try to convert a non-ELF reloc into an ELF one. */
9600
9601 bool
9602 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9603 {
9604 /* Check whether we really have an ELF howto. */
9605
9606 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9607 {
9608 bfd_reloc_code_real_type code;
9609 reloc_howto_type *howto;
9610
9611 /* Alien reloc: Try to determine its type to replace it with an
9612 equivalent ELF reloc. */
9613
9614 if (areloc->howto->pc_relative)
9615 {
9616 switch (areloc->howto->bitsize)
9617 {
9618 case 8:
9619 code = BFD_RELOC_8_PCREL;
9620 break;
9621 case 12:
9622 code = BFD_RELOC_12_PCREL;
9623 break;
9624 case 16:
9625 code = BFD_RELOC_16_PCREL;
9626 break;
9627 case 24:
9628 code = BFD_RELOC_24_PCREL;
9629 break;
9630 case 32:
9631 code = BFD_RELOC_32_PCREL;
9632 break;
9633 case 64:
9634 code = BFD_RELOC_64_PCREL;
9635 break;
9636 default:
9637 goto fail;
9638 }
9639
9640 howto = bfd_reloc_type_lookup (abfd, code);
9641
9642 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9643 {
9644 if (howto->pcrel_offset)
9645 areloc->addend += areloc->address;
9646 else
9647 areloc->addend -= areloc->address; /* addend is unsigned!! */
9648 }
9649 }
9650 else
9651 {
9652 switch (areloc->howto->bitsize)
9653 {
9654 case 8:
9655 code = BFD_RELOC_8;
9656 break;
9657 case 14:
9658 code = BFD_RELOC_14;
9659 break;
9660 case 16:
9661 code = BFD_RELOC_16;
9662 break;
9663 case 26:
9664 code = BFD_RELOC_26;
9665 break;
9666 case 32:
9667 code = BFD_RELOC_32;
9668 break;
9669 case 64:
9670 code = BFD_RELOC_64;
9671 break;
9672 default:
9673 goto fail;
9674 }
9675
9676 howto = bfd_reloc_type_lookup (abfd, code);
9677 }
9678
9679 if (howto)
9680 areloc->howto = howto;
9681 else
9682 goto fail;
9683 }
9684
9685 return true;
9686
9687 fail:
9688 /* xgettext:c-format */
9689 _bfd_error_handler (_("%pB: %s unsupported"),
9690 abfd, areloc->howto->name);
9691 bfd_set_error (bfd_error_sorry);
9692 return false;
9693 }
9694
9695 bool
9696 _bfd_elf_close_and_cleanup (bfd *abfd)
9697 {
9698 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9699 if (tdata != NULL
9700 && (bfd_get_format (abfd) == bfd_object
9701 || bfd_get_format (abfd) == bfd_core))
9702 {
9703 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9704 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9705 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9706 _bfd_stab_cleanup (abfd, &tdata->line_info);
9707 }
9708
9709 return _bfd_generic_close_and_cleanup (abfd);
9710 }
9711
9712 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9713 in the relocation's offset. Thus we cannot allow any sort of sanity
9714 range-checking to interfere. There is nothing else to do in processing
9715 this reloc. */
9716
9717 bfd_reloc_status_type
9718 _bfd_elf_rel_vtable_reloc_fn
9719 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9720 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9721 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9722 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9723 {
9724 return bfd_reloc_ok;
9725 }
9726 \f
9727 /* Elf core file support. Much of this only works on native
9728 toolchains, since we rely on knowing the
9729 machine-dependent procfs structure in order to pick
9730 out details about the corefile. */
9731
9732 #ifdef HAVE_SYS_PROCFS_H
9733 # include <sys/procfs.h>
9734 #endif
9735
9736 /* Return a PID that identifies a "thread" for threaded cores, or the
9737 PID of the main process for non-threaded cores. */
9738
9739 static int
9740 elfcore_make_pid (bfd *abfd)
9741 {
9742 int pid;
9743
9744 pid = elf_tdata (abfd)->core->lwpid;
9745 if (pid == 0)
9746 pid = elf_tdata (abfd)->core->pid;
9747
9748 return pid;
9749 }
9750
9751 /* If there isn't a section called NAME, make one, using data from
9752 SECT. Note, this function will generate a reference to NAME, so
9753 you shouldn't deallocate or overwrite it. */
9754
9755 static bool
9756 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9757 {
9758 asection *sect2;
9759
9760 if (bfd_get_section_by_name (abfd, name) != NULL)
9761 return true;
9762
9763 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9764 if (sect2 == NULL)
9765 return false;
9766
9767 sect2->size = sect->size;
9768 sect2->filepos = sect->filepos;
9769 sect2->alignment_power = sect->alignment_power;
9770 return true;
9771 }
9772
9773 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9774 actually creates up to two pseudosections:
9775 - For the single-threaded case, a section named NAME, unless
9776 such a section already exists.
9777 - For the multi-threaded case, a section named "NAME/PID", where
9778 PID is elfcore_make_pid (abfd).
9779 Both pseudosections have identical contents. */
9780 bool
9781 _bfd_elfcore_make_pseudosection (bfd *abfd,
9782 char *name,
9783 size_t size,
9784 ufile_ptr filepos)
9785 {
9786 char buf[100];
9787 char *threaded_name;
9788 size_t len;
9789 asection *sect;
9790
9791 /* Build the section name. */
9792
9793 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9794 len = strlen (buf) + 1;
9795 threaded_name = (char *) bfd_alloc (abfd, len);
9796 if (threaded_name == NULL)
9797 return false;
9798 memcpy (threaded_name, buf, len);
9799
9800 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9801 SEC_HAS_CONTENTS);
9802 if (sect == NULL)
9803 return false;
9804 sect->size = size;
9805 sect->filepos = filepos;
9806 sect->alignment_power = 2;
9807
9808 return elfcore_maybe_make_sect (abfd, name, sect);
9809 }
9810
9811 static bool
9812 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9813 size_t offs)
9814 {
9815 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9816 SEC_HAS_CONTENTS);
9817
9818 if (sect == NULL)
9819 return false;
9820
9821 sect->size = note->descsz - offs;
9822 sect->filepos = note->descpos + offs;
9823 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9824
9825 return true;
9826 }
9827
9828 /* prstatus_t exists on:
9829 solaris 2.5+
9830 linux 2.[01] + glibc
9831 unixware 4.2
9832 */
9833
9834 #if defined (HAVE_PRSTATUS_T)
9835
9836 static bool
9837 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9838 {
9839 size_t size;
9840 int offset;
9841
9842 if (note->descsz == sizeof (prstatus_t))
9843 {
9844 prstatus_t prstat;
9845
9846 size = sizeof (prstat.pr_reg);
9847 offset = offsetof (prstatus_t, pr_reg);
9848 memcpy (&prstat, note->descdata, sizeof (prstat));
9849
9850 /* Do not overwrite the core signal if it
9851 has already been set by another thread. */
9852 if (elf_tdata (abfd)->core->signal == 0)
9853 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9854 if (elf_tdata (abfd)->core->pid == 0)
9855 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9856
9857 /* pr_who exists on:
9858 solaris 2.5+
9859 unixware 4.2
9860 pr_who doesn't exist on:
9861 linux 2.[01]
9862 */
9863 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9864 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9865 #else
9866 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9867 #endif
9868 }
9869 #if defined (HAVE_PRSTATUS32_T)
9870 else if (note->descsz == sizeof (prstatus32_t))
9871 {
9872 /* 64-bit host, 32-bit corefile */
9873 prstatus32_t prstat;
9874
9875 size = sizeof (prstat.pr_reg);
9876 offset = offsetof (prstatus32_t, pr_reg);
9877 memcpy (&prstat, note->descdata, sizeof (prstat));
9878
9879 /* Do not overwrite the core signal if it
9880 has already been set by another thread. */
9881 if (elf_tdata (abfd)->core->signal == 0)
9882 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9883 if (elf_tdata (abfd)->core->pid == 0)
9884 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9885
9886 /* pr_who exists on:
9887 solaris 2.5+
9888 unixware 4.2
9889 pr_who doesn't exist on:
9890 linux 2.[01]
9891 */
9892 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9893 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9894 #else
9895 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9896 #endif
9897 }
9898 #endif /* HAVE_PRSTATUS32_T */
9899 else
9900 {
9901 /* Fail - we don't know how to handle any other
9902 note size (ie. data object type). */
9903 return true;
9904 }
9905
9906 /* Make a ".reg/999" section and a ".reg" section. */
9907 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9908 size, note->descpos + offset);
9909 }
9910 #endif /* defined (HAVE_PRSTATUS_T) */
9911
9912 /* Create a pseudosection containing the exact contents of NOTE. */
9913 static bool
9914 elfcore_make_note_pseudosection (bfd *abfd,
9915 char *name,
9916 Elf_Internal_Note *note)
9917 {
9918 return _bfd_elfcore_make_pseudosection (abfd, name,
9919 note->descsz, note->descpos);
9920 }
9921
9922 /* There isn't a consistent prfpregset_t across platforms,
9923 but it doesn't matter, because we don't have to pick this
9924 data structure apart. */
9925
9926 static bool
9927 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9928 {
9929 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9930 }
9931
9932 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9933 type of NT_PRXFPREG. Just include the whole note's contents
9934 literally. */
9935
9936 static bool
9937 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9938 {
9939 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9940 }
9941
9942 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9943 with a note type of NT_X86_XSTATE. Just include the whole note's
9944 contents literally. */
9945
9946 static bool
9947 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9948 {
9949 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9950 }
9951
9952 static bool
9953 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9954 {
9955 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9956 }
9957
9958 static bool
9959 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9960 {
9961 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9962 }
9963
9964 static bool
9965 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9966 {
9967 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9968 }
9969
9970 static bool
9971 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9972 {
9973 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9974 }
9975
9976 static bool
9977 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9978 {
9979 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9980 }
9981
9982 static bool
9983 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9984 {
9985 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9986 }
9987
9988 static bool
9989 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9990 {
9991 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9992 }
9993
9994 static bool
9995 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9996 {
9997 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9998 }
9999
10000 static bool
10001 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10002 {
10003 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10004 }
10005
10006 static bool
10007 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10008 {
10009 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10010 }
10011
10012 static bool
10013 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10014 {
10015 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10016 }
10017
10018 static bool
10019 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10020 {
10021 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10022 }
10023
10024 static bool
10025 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10026 {
10027 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10028 }
10029
10030 static bool
10031 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10032 {
10033 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10034 }
10035
10036 static bool
10037 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10038 {
10039 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10040 }
10041
10042 static bool
10043 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10044 {
10045 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10046 }
10047
10048 static bool
10049 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10050 {
10051 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10052 }
10053
10054 static bool
10055 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10056 {
10057 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10058 }
10059
10060 static bool
10061 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10062 {
10063 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10064 }
10065
10066 static bool
10067 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10068 {
10069 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10070 }
10071
10072 static bool
10073 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10074 {
10075 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10076 }
10077
10078 static bool
10079 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10080 {
10081 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10082 }
10083
10084 static bool
10085 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10086 {
10087 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10088 }
10089
10090 static bool
10091 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10092 {
10093 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10094 }
10095
10096 static bool
10097 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10098 {
10099 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10100 }
10101
10102 static bool
10103 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10104 {
10105 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10106 }
10107
10108 static bool
10109 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10110 {
10111 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10112 }
10113
10114 static bool
10115 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10116 {
10117 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10118 }
10119
10120 static bool
10121 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10122 {
10123 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10124 }
10125
10126 static bool
10127 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10128 {
10129 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10130 }
10131
10132 static bool
10133 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10134 {
10135 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10136 }
10137
10138 static bool
10139 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10140 {
10141 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10142 }
10143
10144 static bool
10145 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10146 {
10147 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10148 }
10149
10150 static bool
10151 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10152 {
10153 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10154 }
10155
10156 static bool
10157 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10158 {
10159 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10160 note);
10161 }
10162
10163 static bool
10164 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10165 {
10166 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10167 }
10168
10169 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10170 successful otherwise, return FALSE. */
10171
10172 static bool
10173 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10174 {
10175 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10176 }
10177
10178 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10179 successful otherwise, return FALSE. */
10180
10181 static bool
10182 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10183 {
10184 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10185 }
10186
10187 static bool
10188 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10189 {
10190 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10191 }
10192
10193 static bool
10194 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10195 {
10196 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10197 }
10198
10199 static bool
10200 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10201 {
10202 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10203 }
10204
10205 static bool
10206 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10207 {
10208 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10209 }
10210
10211 #if defined (HAVE_PRPSINFO_T)
10212 typedef prpsinfo_t elfcore_psinfo_t;
10213 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10214 typedef prpsinfo32_t elfcore_psinfo32_t;
10215 #endif
10216 #endif
10217
10218 #if defined (HAVE_PSINFO_T)
10219 typedef psinfo_t elfcore_psinfo_t;
10220 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10221 typedef psinfo32_t elfcore_psinfo32_t;
10222 #endif
10223 #endif
10224
10225 /* return a malloc'ed copy of a string at START which is at
10226 most MAX bytes long, possibly without a terminating '\0'.
10227 the copy will always have a terminating '\0'. */
10228
10229 char *
10230 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10231 {
10232 char *dups;
10233 char *end = (char *) memchr (start, '\0', max);
10234 size_t len;
10235
10236 if (end == NULL)
10237 len = max;
10238 else
10239 len = end - start;
10240
10241 dups = (char *) bfd_alloc (abfd, len + 1);
10242 if (dups == NULL)
10243 return NULL;
10244
10245 memcpy (dups, start, len);
10246 dups[len] = '\0';
10247
10248 return dups;
10249 }
10250
10251 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10252 static bool
10253 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10254 {
10255 if (note->descsz == sizeof (elfcore_psinfo_t))
10256 {
10257 elfcore_psinfo_t psinfo;
10258
10259 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10260
10261 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10262 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10263 #endif
10264 elf_tdata (abfd)->core->program
10265 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10266 sizeof (psinfo.pr_fname));
10267
10268 elf_tdata (abfd)->core->command
10269 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10270 sizeof (psinfo.pr_psargs));
10271 }
10272 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10273 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10274 {
10275 /* 64-bit host, 32-bit corefile */
10276 elfcore_psinfo32_t psinfo;
10277
10278 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10279
10280 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10281 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10282 #endif
10283 elf_tdata (abfd)->core->program
10284 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10285 sizeof (psinfo.pr_fname));
10286
10287 elf_tdata (abfd)->core->command
10288 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10289 sizeof (psinfo.pr_psargs));
10290 }
10291 #endif
10292
10293 else
10294 {
10295 /* Fail - we don't know how to handle any other
10296 note size (ie. data object type). */
10297 return true;
10298 }
10299
10300 /* Note that for some reason, a spurious space is tacked
10301 onto the end of the args in some (at least one anyway)
10302 implementations, so strip it off if it exists. */
10303
10304 {
10305 char *command = elf_tdata (abfd)->core->command;
10306 int n = strlen (command);
10307
10308 if (0 < n && command[n - 1] == ' ')
10309 command[n - 1] = '\0';
10310 }
10311
10312 return true;
10313 }
10314 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10315
10316 #if defined (HAVE_PSTATUS_T)
10317 static bool
10318 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10319 {
10320 if (note->descsz == sizeof (pstatus_t)
10321 #if defined (HAVE_PXSTATUS_T)
10322 || note->descsz == sizeof (pxstatus_t)
10323 #endif
10324 )
10325 {
10326 pstatus_t pstat;
10327
10328 memcpy (&pstat, note->descdata, sizeof (pstat));
10329
10330 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10331 }
10332 #if defined (HAVE_PSTATUS32_T)
10333 else if (note->descsz == sizeof (pstatus32_t))
10334 {
10335 /* 64-bit host, 32-bit corefile */
10336 pstatus32_t pstat;
10337
10338 memcpy (&pstat, note->descdata, sizeof (pstat));
10339
10340 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10341 }
10342 #endif
10343 /* Could grab some more details from the "representative"
10344 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10345 NT_LWPSTATUS note, presumably. */
10346
10347 return true;
10348 }
10349 #endif /* defined (HAVE_PSTATUS_T) */
10350
10351 #if defined (HAVE_LWPSTATUS_T)
10352 static bool
10353 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10354 {
10355 lwpstatus_t lwpstat;
10356 char buf[100];
10357 char *name;
10358 size_t len;
10359 asection *sect;
10360
10361 if (note->descsz != sizeof (lwpstat)
10362 #if defined (HAVE_LWPXSTATUS_T)
10363 && note->descsz != sizeof (lwpxstatus_t)
10364 #endif
10365 )
10366 return true;
10367
10368 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10369
10370 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10371 /* Do not overwrite the core signal if it has already been set by
10372 another thread. */
10373 if (elf_tdata (abfd)->core->signal == 0)
10374 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10375
10376 /* Make a ".reg/999" section. */
10377
10378 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10379 len = strlen (buf) + 1;
10380 name = bfd_alloc (abfd, len);
10381 if (name == NULL)
10382 return false;
10383 memcpy (name, buf, len);
10384
10385 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10386 if (sect == NULL)
10387 return false;
10388
10389 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10390 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10391 sect->filepos = note->descpos
10392 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10393 #endif
10394
10395 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10396 sect->size = sizeof (lwpstat.pr_reg);
10397 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10398 #endif
10399
10400 sect->alignment_power = 2;
10401
10402 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10403 return false;
10404
10405 /* Make a ".reg2/999" section */
10406
10407 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10408 len = strlen (buf) + 1;
10409 name = bfd_alloc (abfd, len);
10410 if (name == NULL)
10411 return false;
10412 memcpy (name, buf, len);
10413
10414 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10415 if (sect == NULL)
10416 return false;
10417
10418 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10419 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10420 sect->filepos = note->descpos
10421 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10422 #endif
10423
10424 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10425 sect->size = sizeof (lwpstat.pr_fpreg);
10426 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10427 #endif
10428
10429 sect->alignment_power = 2;
10430
10431 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10432 }
10433 #endif /* defined (HAVE_LWPSTATUS_T) */
10434
10435 /* These constants, and the structure offsets used below, are defined by
10436 Cygwin's core_dump.h */
10437 #define NOTE_INFO_PROCESS 1
10438 #define NOTE_INFO_THREAD 2
10439 #define NOTE_INFO_MODULE 3
10440 #define NOTE_INFO_MODULE64 4
10441
10442 static bool
10443 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10444 {
10445 char buf[30];
10446 char *name;
10447 size_t len;
10448 unsigned int name_size;
10449 asection *sect;
10450 unsigned int type;
10451 int is_active_thread;
10452 bfd_vma base_addr;
10453
10454 if (note->descsz < 4)
10455 return true;
10456
10457 if (! startswith (note->namedata, "win32"))
10458 return true;
10459
10460 type = bfd_get_32 (abfd, note->descdata);
10461
10462 struct
10463 {
10464 const char *type_name;
10465 unsigned long min_size;
10466 } size_check[] =
10467 {
10468 { "NOTE_INFO_PROCESS", 12 },
10469 { "NOTE_INFO_THREAD", 12 },
10470 { "NOTE_INFO_MODULE", 12 },
10471 { "NOTE_INFO_MODULE64", 16 },
10472 };
10473
10474 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10475 return true;
10476
10477 if (note->descsz < size_check[type - 1].min_size)
10478 {
10479 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10480 " is too small"),
10481 abfd, size_check[type - 1].type_name, note->descsz);
10482 return true;
10483 }
10484
10485 switch (type)
10486 {
10487 case NOTE_INFO_PROCESS:
10488 /* FIXME: need to add ->core->command. */
10489 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10490 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10491 break;
10492
10493 case NOTE_INFO_THREAD:
10494 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10495 structure. */
10496 /* thread_info.tid */
10497 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10498
10499 len = strlen (buf) + 1;
10500 name = (char *) bfd_alloc (abfd, len);
10501 if (name == NULL)
10502 return false;
10503
10504 memcpy (name, buf, len);
10505
10506 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10507 if (sect == NULL)
10508 return false;
10509
10510 /* sizeof (thread_info.thread_context) */
10511 sect->size = note->descsz - 12;
10512 /* offsetof (thread_info.thread_context) */
10513 sect->filepos = note->descpos + 12;
10514 sect->alignment_power = 2;
10515
10516 /* thread_info.is_active_thread */
10517 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10518
10519 if (is_active_thread)
10520 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10521 return false;
10522 break;
10523
10524 case NOTE_INFO_MODULE:
10525 case NOTE_INFO_MODULE64:
10526 /* Make a ".module/xxxxxxxx" section. */
10527 if (type == NOTE_INFO_MODULE)
10528 {
10529 /* module_info.base_address */
10530 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10531 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10532 /* module_info.module_name_size */
10533 name_size = bfd_get_32 (abfd, note->descdata + 8);
10534 }
10535 else /* NOTE_INFO_MODULE64 */
10536 {
10537 /* module_info.base_address */
10538 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10539 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10540 /* module_info.module_name_size */
10541 name_size = bfd_get_32 (abfd, note->descdata + 12);
10542 }
10543
10544 len = strlen (buf) + 1;
10545 name = (char *) bfd_alloc (abfd, len);
10546 if (name == NULL)
10547 return false;
10548
10549 memcpy (name, buf, len);
10550
10551 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10552
10553 if (sect == NULL)
10554 return false;
10555
10556 if (note->descsz < 12 + name_size)
10557 {
10558 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10559 " is too small to contain a name of size %u"),
10560 abfd, note->descsz, name_size);
10561 return true;
10562 }
10563
10564 sect->size = note->descsz;
10565 sect->filepos = note->descpos;
10566 sect->alignment_power = 2;
10567 break;
10568
10569 default:
10570 return true;
10571 }
10572
10573 return true;
10574 }
10575
10576 static bool
10577 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10578 {
10579 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10580
10581 switch (note->type)
10582 {
10583 default:
10584 return true;
10585
10586 case NT_PRSTATUS:
10587 if (bed->elf_backend_grok_prstatus)
10588 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10589 return true;
10590 #if defined (HAVE_PRSTATUS_T)
10591 return elfcore_grok_prstatus (abfd, note);
10592 #else
10593 return true;
10594 #endif
10595
10596 #if defined (HAVE_PSTATUS_T)
10597 case NT_PSTATUS:
10598 return elfcore_grok_pstatus (abfd, note);
10599 #endif
10600
10601 #if defined (HAVE_LWPSTATUS_T)
10602 case NT_LWPSTATUS:
10603 return elfcore_grok_lwpstatus (abfd, note);
10604 #endif
10605
10606 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10607 return elfcore_grok_prfpreg (abfd, note);
10608
10609 case NT_WIN32PSTATUS:
10610 return elfcore_grok_win32pstatus (abfd, note);
10611
10612 case NT_PRXFPREG: /* Linux SSE extension */
10613 if (note->namesz == 6
10614 && strcmp (note->namedata, "LINUX") == 0)
10615 return elfcore_grok_prxfpreg (abfd, note);
10616 else
10617 return true;
10618
10619 case NT_X86_XSTATE: /* Linux XSAVE extension */
10620 if (note->namesz == 6
10621 && strcmp (note->namedata, "LINUX") == 0)
10622 return elfcore_grok_xstatereg (abfd, note);
10623 else
10624 return true;
10625
10626 case NT_PPC_VMX:
10627 if (note->namesz == 6
10628 && strcmp (note->namedata, "LINUX") == 0)
10629 return elfcore_grok_ppc_vmx (abfd, note);
10630 else
10631 return true;
10632
10633 case NT_PPC_VSX:
10634 if (note->namesz == 6
10635 && strcmp (note->namedata, "LINUX") == 0)
10636 return elfcore_grok_ppc_vsx (abfd, note);
10637 else
10638 return true;
10639
10640 case NT_PPC_TAR:
10641 if (note->namesz == 6
10642 && strcmp (note->namedata, "LINUX") == 0)
10643 return elfcore_grok_ppc_tar (abfd, note);
10644 else
10645 return true;
10646
10647 case NT_PPC_PPR:
10648 if (note->namesz == 6
10649 && strcmp (note->namedata, "LINUX") == 0)
10650 return elfcore_grok_ppc_ppr (abfd, note);
10651 else
10652 return true;
10653
10654 case NT_PPC_DSCR:
10655 if (note->namesz == 6
10656 && strcmp (note->namedata, "LINUX") == 0)
10657 return elfcore_grok_ppc_dscr (abfd, note);
10658 else
10659 return true;
10660
10661 case NT_PPC_EBB:
10662 if (note->namesz == 6
10663 && strcmp (note->namedata, "LINUX") == 0)
10664 return elfcore_grok_ppc_ebb (abfd, note);
10665 else
10666 return true;
10667
10668 case NT_PPC_PMU:
10669 if (note->namesz == 6
10670 && strcmp (note->namedata, "LINUX") == 0)
10671 return elfcore_grok_ppc_pmu (abfd, note);
10672 else
10673 return true;
10674
10675 case NT_PPC_TM_CGPR:
10676 if (note->namesz == 6
10677 && strcmp (note->namedata, "LINUX") == 0)
10678 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10679 else
10680 return true;
10681
10682 case NT_PPC_TM_CFPR:
10683 if (note->namesz == 6
10684 && strcmp (note->namedata, "LINUX") == 0)
10685 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10686 else
10687 return true;
10688
10689 case NT_PPC_TM_CVMX:
10690 if (note->namesz == 6
10691 && strcmp (note->namedata, "LINUX") == 0)
10692 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10693 else
10694 return true;
10695
10696 case NT_PPC_TM_CVSX:
10697 if (note->namesz == 6
10698 && strcmp (note->namedata, "LINUX") == 0)
10699 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10700 else
10701 return true;
10702
10703 case NT_PPC_TM_SPR:
10704 if (note->namesz == 6
10705 && strcmp (note->namedata, "LINUX") == 0)
10706 return elfcore_grok_ppc_tm_spr (abfd, note);
10707 else
10708 return true;
10709
10710 case NT_PPC_TM_CTAR:
10711 if (note->namesz == 6
10712 && strcmp (note->namedata, "LINUX") == 0)
10713 return elfcore_grok_ppc_tm_ctar (abfd, note);
10714 else
10715 return true;
10716
10717 case NT_PPC_TM_CPPR:
10718 if (note->namesz == 6
10719 && strcmp (note->namedata, "LINUX") == 0)
10720 return elfcore_grok_ppc_tm_cppr (abfd, note);
10721 else
10722 return true;
10723
10724 case NT_PPC_TM_CDSCR:
10725 if (note->namesz == 6
10726 && strcmp (note->namedata, "LINUX") == 0)
10727 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10728 else
10729 return true;
10730
10731 case NT_S390_HIGH_GPRS:
10732 if (note->namesz == 6
10733 && strcmp (note->namedata, "LINUX") == 0)
10734 return elfcore_grok_s390_high_gprs (abfd, note);
10735 else
10736 return true;
10737
10738 case NT_S390_TIMER:
10739 if (note->namesz == 6
10740 && strcmp (note->namedata, "LINUX") == 0)
10741 return elfcore_grok_s390_timer (abfd, note);
10742 else
10743 return true;
10744
10745 case NT_S390_TODCMP:
10746 if (note->namesz == 6
10747 && strcmp (note->namedata, "LINUX") == 0)
10748 return elfcore_grok_s390_todcmp (abfd, note);
10749 else
10750 return true;
10751
10752 case NT_S390_TODPREG:
10753 if (note->namesz == 6
10754 && strcmp (note->namedata, "LINUX") == 0)
10755 return elfcore_grok_s390_todpreg (abfd, note);
10756 else
10757 return true;
10758
10759 case NT_S390_CTRS:
10760 if (note->namesz == 6
10761 && strcmp (note->namedata, "LINUX") == 0)
10762 return elfcore_grok_s390_ctrs (abfd, note);
10763 else
10764 return true;
10765
10766 case NT_S390_PREFIX:
10767 if (note->namesz == 6
10768 && strcmp (note->namedata, "LINUX") == 0)
10769 return elfcore_grok_s390_prefix (abfd, note);
10770 else
10771 return true;
10772
10773 case NT_S390_LAST_BREAK:
10774 if (note->namesz == 6
10775 && strcmp (note->namedata, "LINUX") == 0)
10776 return elfcore_grok_s390_last_break (abfd, note);
10777 else
10778 return true;
10779
10780 case NT_S390_SYSTEM_CALL:
10781 if (note->namesz == 6
10782 && strcmp (note->namedata, "LINUX") == 0)
10783 return elfcore_grok_s390_system_call (abfd, note);
10784 else
10785 return true;
10786
10787 case NT_S390_TDB:
10788 if (note->namesz == 6
10789 && strcmp (note->namedata, "LINUX") == 0)
10790 return elfcore_grok_s390_tdb (abfd, note);
10791 else
10792 return true;
10793
10794 case NT_S390_VXRS_LOW:
10795 if (note->namesz == 6
10796 && strcmp (note->namedata, "LINUX") == 0)
10797 return elfcore_grok_s390_vxrs_low (abfd, note);
10798 else
10799 return true;
10800
10801 case NT_S390_VXRS_HIGH:
10802 if (note->namesz == 6
10803 && strcmp (note->namedata, "LINUX") == 0)
10804 return elfcore_grok_s390_vxrs_high (abfd, note);
10805 else
10806 return true;
10807
10808 case NT_S390_GS_CB:
10809 if (note->namesz == 6
10810 && strcmp (note->namedata, "LINUX") == 0)
10811 return elfcore_grok_s390_gs_cb (abfd, note);
10812 else
10813 return true;
10814
10815 case NT_S390_GS_BC:
10816 if (note->namesz == 6
10817 && strcmp (note->namedata, "LINUX") == 0)
10818 return elfcore_grok_s390_gs_bc (abfd, note);
10819 else
10820 return true;
10821
10822 case NT_ARC_V2:
10823 if (note->namesz == 6
10824 && strcmp (note->namedata, "LINUX") == 0)
10825 return elfcore_grok_arc_v2 (abfd, note);
10826 else
10827 return true;
10828
10829 case NT_ARM_VFP:
10830 if (note->namesz == 6
10831 && strcmp (note->namedata, "LINUX") == 0)
10832 return elfcore_grok_arm_vfp (abfd, note);
10833 else
10834 return true;
10835
10836 case NT_ARM_TLS:
10837 if (note->namesz == 6
10838 && strcmp (note->namedata, "LINUX") == 0)
10839 return elfcore_grok_aarch_tls (abfd, note);
10840 else
10841 return true;
10842
10843 case NT_ARM_HW_BREAK:
10844 if (note->namesz == 6
10845 && strcmp (note->namedata, "LINUX") == 0)
10846 return elfcore_grok_aarch_hw_break (abfd, note);
10847 else
10848 return true;
10849
10850 case NT_ARM_HW_WATCH:
10851 if (note->namesz == 6
10852 && strcmp (note->namedata, "LINUX") == 0)
10853 return elfcore_grok_aarch_hw_watch (abfd, note);
10854 else
10855 return true;
10856
10857 case NT_ARM_SVE:
10858 if (note->namesz == 6
10859 && strcmp (note->namedata, "LINUX") == 0)
10860 return elfcore_grok_aarch_sve (abfd, note);
10861 else
10862 return true;
10863
10864 case NT_ARM_PAC_MASK:
10865 if (note->namesz == 6
10866 && strcmp (note->namedata, "LINUX") == 0)
10867 return elfcore_grok_aarch_pauth (abfd, note);
10868 else
10869 return true;
10870
10871 case NT_ARM_TAGGED_ADDR_CTRL:
10872 if (note->namesz == 6
10873 && strcmp (note->namedata, "LINUX") == 0)
10874 return elfcore_grok_aarch_mte (abfd, note);
10875 else
10876 return true;
10877
10878 case NT_GDB_TDESC:
10879 if (note->namesz == 4
10880 && strcmp (note->namedata, "GDB") == 0)
10881 return elfcore_grok_gdb_tdesc (abfd, note);
10882 else
10883 return true;
10884
10885 case NT_RISCV_CSR:
10886 if (note->namesz == 4
10887 && strcmp (note->namedata, "GDB") == 0)
10888 return elfcore_grok_riscv_csr (abfd, note);
10889 else
10890 return true;
10891
10892 case NT_LARCH_CPUCFG:
10893 if (note->namesz == 6
10894 && strcmp (note->namedata, "LINUX") == 0)
10895 return elfcore_grok_loongarch_cpucfg (abfd, note);
10896 else
10897 return true;
10898
10899 case NT_LARCH_LBT:
10900 if (note->namesz == 6
10901 && strcmp (note->namedata, "LINUX") == 0)
10902 return elfcore_grok_loongarch_lbt (abfd, note);
10903 else
10904 return true;
10905
10906 case NT_LARCH_LSX:
10907 if (note->namesz == 6
10908 && strcmp (note->namedata, "LINUX") == 0)
10909 return elfcore_grok_loongarch_lsx (abfd, note);
10910 else
10911 return true;
10912
10913 case NT_LARCH_LASX:
10914 if (note->namesz == 6
10915 && strcmp (note->namedata, "LINUX") == 0)
10916 return elfcore_grok_loongarch_lasx (abfd, note);
10917 else
10918 return true;
10919
10920 case NT_PRPSINFO:
10921 case NT_PSINFO:
10922 if (bed->elf_backend_grok_psinfo)
10923 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10924 return true;
10925 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10926 return elfcore_grok_psinfo (abfd, note);
10927 #else
10928 return true;
10929 #endif
10930
10931 case NT_AUXV:
10932 return elfcore_make_auxv_note_section (abfd, note, 0);
10933
10934 case NT_FILE:
10935 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10936 note);
10937
10938 case NT_SIGINFO:
10939 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10940 note);
10941
10942 }
10943 }
10944
10945 static bool
10946 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10947 {
10948 struct bfd_build_id* build_id;
10949
10950 if (note->descsz == 0)
10951 return false;
10952
10953 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10954 if (build_id == NULL)
10955 return false;
10956
10957 build_id->size = note->descsz;
10958 memcpy (build_id->data, note->descdata, note->descsz);
10959 abfd->build_id = build_id;
10960
10961 return true;
10962 }
10963
10964 static bool
10965 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10966 {
10967 switch (note->type)
10968 {
10969 default:
10970 return true;
10971
10972 case NT_GNU_PROPERTY_TYPE_0:
10973 return _bfd_elf_parse_gnu_properties (abfd, note);
10974
10975 case NT_GNU_BUILD_ID:
10976 return elfobj_grok_gnu_build_id (abfd, note);
10977 }
10978 }
10979
10980 static bool
10981 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10982 {
10983 struct sdt_note *cur =
10984 (struct sdt_note *) bfd_alloc (abfd,
10985 sizeof (struct sdt_note) + note->descsz);
10986
10987 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10988 cur->size = (bfd_size_type) note->descsz;
10989 memcpy (cur->data, note->descdata, note->descsz);
10990
10991 elf_tdata (abfd)->sdt_note_head = cur;
10992
10993 return true;
10994 }
10995
10996 static bool
10997 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10998 {
10999 switch (note->type)
11000 {
11001 case NT_STAPSDT:
11002 return elfobj_grok_stapsdt_note_1 (abfd, note);
11003
11004 default:
11005 return true;
11006 }
11007 }
11008
11009 static bool
11010 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11011 {
11012 size_t offset;
11013
11014 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11015 {
11016 case ELFCLASS32:
11017 if (note->descsz < 108)
11018 return false;
11019 break;
11020
11021 case ELFCLASS64:
11022 if (note->descsz < 120)
11023 return false;
11024 break;
11025
11026 default:
11027 return false;
11028 }
11029
11030 /* Check for version 1 in pr_version. */
11031 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11032 return false;
11033
11034 offset = 4;
11035
11036 /* Skip over pr_psinfosz. */
11037 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11038 offset += 4;
11039 else
11040 {
11041 offset += 4; /* Padding before pr_psinfosz. */
11042 offset += 8;
11043 }
11044
11045 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11046 elf_tdata (abfd)->core->program
11047 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11048 offset += 17;
11049
11050 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11051 elf_tdata (abfd)->core->command
11052 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11053 offset += 81;
11054
11055 /* Padding before pr_pid. */
11056 offset += 2;
11057
11058 /* The pr_pid field was added in version "1a". */
11059 if (note->descsz < offset + 4)
11060 return true;
11061
11062 elf_tdata (abfd)->core->pid
11063 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11064
11065 return true;
11066 }
11067
11068 static bool
11069 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11070 {
11071 size_t offset;
11072 size_t size;
11073 size_t min_size;
11074
11075 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11076 Also compute minimum size of this note. */
11077 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11078 {
11079 case ELFCLASS32:
11080 offset = 4 + 4;
11081 min_size = offset + (4 * 2) + 4 + 4 + 4;
11082 break;
11083
11084 case ELFCLASS64:
11085 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11086 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11087 break;
11088
11089 default:
11090 return false;
11091 }
11092
11093 if (note->descsz < min_size)
11094 return false;
11095
11096 /* Check for version 1 in pr_version. */
11097 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11098 return false;
11099
11100 /* Extract size of pr_reg from pr_gregsetsz. */
11101 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11102 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11103 {
11104 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11105 offset += 4 * 2;
11106 }
11107 else
11108 {
11109 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11110 offset += 8 * 2;
11111 }
11112
11113 /* Skip over pr_osreldate. */
11114 offset += 4;
11115
11116 /* Read signal from pr_cursig. */
11117 if (elf_tdata (abfd)->core->signal == 0)
11118 elf_tdata (abfd)->core->signal
11119 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11120 offset += 4;
11121
11122 /* Read TID from pr_pid. */
11123 elf_tdata (abfd)->core->lwpid
11124 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11125 offset += 4;
11126
11127 /* Padding before pr_reg. */
11128 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11129 offset += 4;
11130
11131 /* Make sure that there is enough data remaining in the note. */
11132 if ((note->descsz - offset) < size)
11133 return false;
11134
11135 /* Make a ".reg/999" section and a ".reg" section. */
11136 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11137 size, note->descpos + offset);
11138 }
11139
11140 static bool
11141 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11142 {
11143 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11144
11145 switch (note->type)
11146 {
11147 case NT_PRSTATUS:
11148 if (bed->elf_backend_grok_freebsd_prstatus)
11149 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11150 return true;
11151 return elfcore_grok_freebsd_prstatus (abfd, note);
11152
11153 case NT_FPREGSET:
11154 return elfcore_grok_prfpreg (abfd, note);
11155
11156 case NT_PRPSINFO:
11157 return elfcore_grok_freebsd_psinfo (abfd, note);
11158
11159 case NT_FREEBSD_THRMISC:
11160 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11161
11162 case NT_FREEBSD_PROCSTAT_PROC:
11163 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11164 note);
11165
11166 case NT_FREEBSD_PROCSTAT_FILES:
11167 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11168 note);
11169
11170 case NT_FREEBSD_PROCSTAT_VMMAP:
11171 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11172 note);
11173
11174 case NT_FREEBSD_PROCSTAT_AUXV:
11175 return elfcore_make_auxv_note_section (abfd, note, 4);
11176
11177 case NT_FREEBSD_X86_SEGBASES:
11178 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11179
11180 case NT_X86_XSTATE:
11181 return elfcore_grok_xstatereg (abfd, note);
11182
11183 case NT_FREEBSD_PTLWPINFO:
11184 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11185 note);
11186
11187 case NT_ARM_TLS:
11188 return elfcore_grok_aarch_tls (abfd, note);
11189
11190 case NT_ARM_VFP:
11191 return elfcore_grok_arm_vfp (abfd, note);
11192
11193 default:
11194 return true;
11195 }
11196 }
11197
11198 static bool
11199 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11200 {
11201 char *cp;
11202
11203 cp = strchr (note->namedata, '@');
11204 if (cp != NULL)
11205 {
11206 *lwpidp = atoi(cp + 1);
11207 return true;
11208 }
11209 return false;
11210 }
11211
11212 static bool
11213 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11214 {
11215 if (note->descsz <= 0x7c + 31)
11216 return false;
11217
11218 /* Signal number at offset 0x08. */
11219 elf_tdata (abfd)->core->signal
11220 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11221
11222 /* Process ID at offset 0x50. */
11223 elf_tdata (abfd)->core->pid
11224 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11225
11226 /* Command name at 0x7c (max 32 bytes, including nul). */
11227 elf_tdata (abfd)->core->command
11228 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11229
11230 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11231 note);
11232 }
11233
11234 static bool
11235 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11236 {
11237 int lwp;
11238
11239 if (elfcore_netbsd_get_lwpid (note, &lwp))
11240 elf_tdata (abfd)->core->lwpid = lwp;
11241
11242 switch (note->type)
11243 {
11244 case NT_NETBSDCORE_PROCINFO:
11245 /* NetBSD-specific core "procinfo". Note that we expect to
11246 find this note before any of the others, which is fine,
11247 since the kernel writes this note out first when it
11248 creates a core file. */
11249 return elfcore_grok_netbsd_procinfo (abfd, note);
11250 case NT_NETBSDCORE_AUXV:
11251 /* NetBSD-specific Elf Auxiliary Vector data. */
11252 return elfcore_make_auxv_note_section (abfd, note, 4);
11253 case NT_NETBSDCORE_LWPSTATUS:
11254 return elfcore_make_note_pseudosection (abfd,
11255 ".note.netbsdcore.lwpstatus",
11256 note);
11257 default:
11258 break;
11259 }
11260
11261 /* As of March 2020 there are no other machine-independent notes
11262 defined for NetBSD core files. If the note type is less
11263 than the start of the machine-dependent note types, we don't
11264 understand it. */
11265
11266 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11267 return true;
11268
11269
11270 switch (bfd_get_arch (abfd))
11271 {
11272 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11273 PT_GETFPREGS == mach+2. */
11274
11275 case bfd_arch_aarch64:
11276 case bfd_arch_alpha:
11277 case bfd_arch_sparc:
11278 switch (note->type)
11279 {
11280 case NT_NETBSDCORE_FIRSTMACH+0:
11281 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11282
11283 case NT_NETBSDCORE_FIRSTMACH+2:
11284 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11285
11286 default:
11287 return true;
11288 }
11289
11290 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11291 There's also old PT___GETREGS40 == mach + 1 for old reg
11292 structure which lacks GBR. */
11293
11294 case bfd_arch_sh:
11295 switch (note->type)
11296 {
11297 case NT_NETBSDCORE_FIRSTMACH+3:
11298 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11299
11300 case NT_NETBSDCORE_FIRSTMACH+5:
11301 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11302
11303 default:
11304 return true;
11305 }
11306
11307 /* On all other arch's, PT_GETREGS == mach+1 and
11308 PT_GETFPREGS == mach+3. */
11309
11310 default:
11311 switch (note->type)
11312 {
11313 case NT_NETBSDCORE_FIRSTMACH+1:
11314 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11315
11316 case NT_NETBSDCORE_FIRSTMACH+3:
11317 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11318
11319 default:
11320 return true;
11321 }
11322 }
11323 /* NOTREACHED */
11324 }
11325
11326 static bool
11327 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11328 {
11329 if (note->descsz <= 0x48 + 31)
11330 return false;
11331
11332 /* Signal number at offset 0x08. */
11333 elf_tdata (abfd)->core->signal
11334 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11335
11336 /* Process ID at offset 0x20. */
11337 elf_tdata (abfd)->core->pid
11338 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11339
11340 /* Command name at 0x48 (max 32 bytes, including nul). */
11341 elf_tdata (abfd)->core->command
11342 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11343
11344 return true;
11345 }
11346
11347 /* Processes Solaris's process status note.
11348 sig_off ~ offsetof(prstatus_t, pr_cursig)
11349 pid_off ~ offsetof(prstatus_t, pr_pid)
11350 lwpid_off ~ offsetof(prstatus_t, pr_who)
11351 gregset_size ~ sizeof(gregset_t)
11352 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11353
11354 static bool
11355 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11356 int pid_off, int lwpid_off, size_t gregset_size,
11357 size_t gregset_offset)
11358 {
11359 asection *sect = NULL;
11360 elf_tdata (abfd)->core->signal
11361 = bfd_get_16 (abfd, note->descdata + sig_off);
11362 elf_tdata (abfd)->core->pid
11363 = bfd_get_32 (abfd, note->descdata + pid_off);
11364 elf_tdata (abfd)->core->lwpid
11365 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11366
11367 sect = bfd_get_section_by_name (abfd, ".reg");
11368 if (sect != NULL)
11369 sect->size = gregset_size;
11370
11371 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11372 note->descpos + gregset_offset);
11373 }
11374
11375 /* Gets program and arguments from a core.
11376 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11377 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11378
11379 static bool
11380 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11381 int prog_off, int comm_off)
11382 {
11383 elf_tdata (abfd)->core->program
11384 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11385 elf_tdata (abfd)->core->command
11386 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11387
11388 return true;
11389 }
11390
11391 /* Processes Solaris's LWP status note.
11392 gregset_size ~ sizeof(gregset_t)
11393 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11394 fpregset_size ~ sizeof(fpregset_t)
11395 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11396
11397 static bool
11398 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11399 size_t gregset_size, int gregset_off,
11400 size_t fpregset_size, int fpregset_off)
11401 {
11402 asection *sect = NULL;
11403 char reg2_section_name[16] = { 0 };
11404
11405 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11406 elf_tdata (abfd)->core->lwpid);
11407
11408 /* offsetof(lwpstatus_t, pr_lwpid) */
11409 elf_tdata (abfd)->core->lwpid
11410 = bfd_get_32 (abfd, note->descdata + 4);
11411 /* offsetof(lwpstatus_t, pr_cursig) */
11412 elf_tdata (abfd)->core->signal
11413 = bfd_get_16 (abfd, note->descdata + 12);
11414
11415 sect = bfd_get_section_by_name (abfd, ".reg");
11416 if (sect != NULL)
11417 sect->size = gregset_size;
11418 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11419 note->descpos + gregset_off))
11420 return false;
11421
11422 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11423 if (sect != NULL)
11424 {
11425 sect->size = fpregset_size;
11426 sect->filepos = note->descpos + fpregset_off;
11427 sect->alignment_power = 2;
11428 }
11429 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11430 note->descpos + fpregset_off))
11431 return false;
11432
11433 return true;
11434 }
11435
11436 static bool
11437 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11438 {
11439 if (note == NULL)
11440 return false;
11441
11442 /* core files are identified as 32- or 64-bit, SPARC or x86,
11443 by the size of the descsz which matches the sizeof()
11444 the type appropriate for that note type (e.g., prstatus_t for
11445 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11446 on Solaris. The core file bitness may differ from the bitness of
11447 gdb itself, so fixed values are used instead of sizeof().
11448 Appropriate fixed offsets are also used to obtain data from
11449 the note. */
11450
11451 switch ((int) note->type)
11452 {
11453 case SOLARIS_NT_PRSTATUS:
11454 switch (note->descsz)
11455 {
11456 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11457 return elfcore_grok_solaris_prstatus(abfd, note,
11458 136, 216, 308, 152, 356);
11459 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11460 return elfcore_grok_solaris_prstatus(abfd, note,
11461 264, 360, 520, 304, 600);
11462 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11463 return elfcore_grok_solaris_prstatus(abfd, note,
11464 136, 216, 308, 76, 356);
11465 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11466 return elfcore_grok_solaris_prstatus(abfd, note,
11467 264, 360, 520, 224, 600);
11468 default:
11469 return true;
11470 }
11471
11472 case SOLARIS_NT_PSINFO:
11473 case SOLARIS_NT_PRPSINFO:
11474 switch (note->descsz)
11475 {
11476 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11477 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11478 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11479 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11480 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11481 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11482 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11483 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11484 default:
11485 return true;
11486 }
11487
11488 case SOLARIS_NT_LWPSTATUS:
11489 switch (note->descsz)
11490 {
11491 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11492 return elfcore_grok_solaris_lwpstatus(abfd, note,
11493 152, 344, 400, 496);
11494 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11495 return elfcore_grok_solaris_lwpstatus(abfd, note,
11496 304, 544, 544, 848);
11497 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11498 return elfcore_grok_solaris_lwpstatus(abfd, note,
11499 76, 344, 380, 420);
11500 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11501 return elfcore_grok_solaris_lwpstatus(abfd, note,
11502 224, 544, 528, 768);
11503 default:
11504 return true;
11505 }
11506
11507 case SOLARIS_NT_LWPSINFO:
11508 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11509 if (note->descsz == 128 || note->descsz == 152)
11510 elf_tdata (abfd)->core->lwpid =
11511 bfd_get_32 (abfd, note->descdata + 4);
11512 break;
11513
11514 default:
11515 break;
11516 }
11517
11518 return true;
11519 }
11520
11521 /* For name starting with "CORE" this may be either a Solaris
11522 core file or a gdb-generated core file. Do Solaris-specific
11523 processing on selected note types first with
11524 elfcore_grok_solaris_note(), then process the note
11525 in elfcore_grok_note(). */
11526
11527 static bool
11528 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11529 {
11530 if (!elfcore_grok_solaris_note_impl (abfd, note))
11531 return false;
11532
11533 return elfcore_grok_note (abfd, note);
11534 }
11535
11536 static bool
11537 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11538 {
11539 if (note->type == NT_OPENBSD_PROCINFO)
11540 return elfcore_grok_openbsd_procinfo (abfd, note);
11541
11542 if (note->type == NT_OPENBSD_REGS)
11543 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11544
11545 if (note->type == NT_OPENBSD_FPREGS)
11546 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11547
11548 if (note->type == NT_OPENBSD_XFPREGS)
11549 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11550
11551 if (note->type == NT_OPENBSD_AUXV)
11552 return elfcore_make_auxv_note_section (abfd, note, 0);
11553
11554 if (note->type == NT_OPENBSD_WCOOKIE)
11555 {
11556 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11557 SEC_HAS_CONTENTS);
11558
11559 if (sect == NULL)
11560 return false;
11561 sect->size = note->descsz;
11562 sect->filepos = note->descpos;
11563 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11564
11565 return true;
11566 }
11567
11568 return true;
11569 }
11570
11571 static bool
11572 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11573 {
11574 void *ddata = note->descdata;
11575 char buf[100];
11576 char *name;
11577 asection *sect;
11578 short sig;
11579 unsigned flags;
11580
11581 if (note->descsz < 16)
11582 return false;
11583
11584 /* nto_procfs_status 'pid' field is at offset 0. */
11585 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11586
11587 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11588 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11589
11590 /* nto_procfs_status 'flags' field is at offset 8. */
11591 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11592
11593 /* nto_procfs_status 'what' field is at offset 14. */
11594 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11595 {
11596 elf_tdata (abfd)->core->signal = sig;
11597 elf_tdata (abfd)->core->lwpid = *tid;
11598 }
11599
11600 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11601 do not come from signals so we make sure we set the current
11602 thread just in case. */
11603 if (flags & 0x00000080)
11604 elf_tdata (abfd)->core->lwpid = *tid;
11605
11606 /* Make a ".qnx_core_status/%d" section. */
11607 sprintf (buf, ".qnx_core_status/%ld", *tid);
11608
11609 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11610 if (name == NULL)
11611 return false;
11612 strcpy (name, buf);
11613
11614 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11615 if (sect == NULL)
11616 return false;
11617
11618 sect->size = note->descsz;
11619 sect->filepos = note->descpos;
11620 sect->alignment_power = 2;
11621
11622 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11623 }
11624
11625 static bool
11626 elfcore_grok_nto_regs (bfd *abfd,
11627 Elf_Internal_Note *note,
11628 long tid,
11629 char *base)
11630 {
11631 char buf[100];
11632 char *name;
11633 asection *sect;
11634
11635 /* Make a "(base)/%d" section. */
11636 sprintf (buf, "%s/%ld", base, tid);
11637
11638 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11639 if (name == NULL)
11640 return false;
11641 strcpy (name, buf);
11642
11643 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11644 if (sect == NULL)
11645 return false;
11646
11647 sect->size = note->descsz;
11648 sect->filepos = note->descpos;
11649 sect->alignment_power = 2;
11650
11651 /* This is the current thread. */
11652 if (elf_tdata (abfd)->core->lwpid == tid)
11653 return elfcore_maybe_make_sect (abfd, base, sect);
11654
11655 return true;
11656 }
11657
11658 #define BFD_QNT_CORE_INFO 7
11659 #define BFD_QNT_CORE_STATUS 8
11660 #define BFD_QNT_CORE_GREG 9
11661 #define BFD_QNT_CORE_FPREG 10
11662
11663 static bool
11664 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11665 {
11666 /* Every GREG section has a STATUS section before it. Store the
11667 tid from the previous call to pass down to the next gregs
11668 function. */
11669 static long tid = 1;
11670
11671 switch (note->type)
11672 {
11673 case BFD_QNT_CORE_INFO:
11674 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11675 case BFD_QNT_CORE_STATUS:
11676 return elfcore_grok_nto_status (abfd, note, &tid);
11677 case BFD_QNT_CORE_GREG:
11678 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11679 case BFD_QNT_CORE_FPREG:
11680 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11681 default:
11682 return true;
11683 }
11684 }
11685
11686 static bool
11687 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11688 {
11689 char *name;
11690 asection *sect;
11691 size_t len;
11692
11693 /* Use note name as section name. */
11694 len = note->namesz;
11695 name = (char *) bfd_alloc (abfd, len);
11696 if (name == NULL)
11697 return false;
11698 memcpy (name, note->namedata, len);
11699 name[len - 1] = '\0';
11700
11701 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11702 if (sect == NULL)
11703 return false;
11704
11705 sect->size = note->descsz;
11706 sect->filepos = note->descpos;
11707 sect->alignment_power = 1;
11708
11709 return true;
11710 }
11711
11712 /* Function: elfcore_write_note
11713
11714 Inputs:
11715 buffer to hold note, and current size of buffer
11716 name of note
11717 type of note
11718 data for note
11719 size of data for note
11720
11721 Writes note to end of buffer. ELF64 notes are written exactly as
11722 for ELF32, despite the current (as of 2006) ELF gabi specifying
11723 that they ought to have 8-byte namesz and descsz field, and have
11724 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11725
11726 Return:
11727 Pointer to realloc'd buffer, *BUFSIZ updated. */
11728
11729 char *
11730 elfcore_write_note (bfd *abfd,
11731 char *buf,
11732 int *bufsiz,
11733 const char *name,
11734 int type,
11735 const void *input,
11736 int size)
11737 {
11738 Elf_External_Note *xnp;
11739 size_t namesz;
11740 size_t newspace;
11741 char *dest;
11742
11743 namesz = 0;
11744 if (name != NULL)
11745 namesz = strlen (name) + 1;
11746
11747 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11748
11749 buf = (char *) realloc (buf, *bufsiz + newspace);
11750 if (buf == NULL)
11751 return buf;
11752 dest = buf + *bufsiz;
11753 *bufsiz += newspace;
11754 xnp = (Elf_External_Note *) dest;
11755 H_PUT_32 (abfd, namesz, xnp->namesz);
11756 H_PUT_32 (abfd, size, xnp->descsz);
11757 H_PUT_32 (abfd, type, xnp->type);
11758 dest = xnp->name;
11759 if (name != NULL)
11760 {
11761 memcpy (dest, name, namesz);
11762 dest += namesz;
11763 while (namesz & 3)
11764 {
11765 *dest++ = '\0';
11766 ++namesz;
11767 }
11768 }
11769 memcpy (dest, input, size);
11770 dest += size;
11771 while (size & 3)
11772 {
11773 *dest++ = '\0';
11774 ++size;
11775 }
11776 return buf;
11777 }
11778
11779 /* gcc-8 warns (*) on all the strncpy calls in this function about
11780 possible string truncation. The "truncation" is not a bug. We
11781 have an external representation of structs with fields that are not
11782 necessarily NULL terminated and corresponding internal
11783 representation fields that are one larger so that they can always
11784 be NULL terminated.
11785 gcc versions between 4.2 and 4.6 do not allow pragma control of
11786 diagnostics inside functions, giving a hard error if you try to use
11787 the finer control available with later versions.
11788 gcc prior to 4.2 warns about diagnostic push and pop.
11789 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11790 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11791 (*) Depending on your system header files! */
11792 #if GCC_VERSION >= 8000
11793 # pragma GCC diagnostic push
11794 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11795 #endif
11796 char *
11797 elfcore_write_prpsinfo (bfd *abfd,
11798 char *buf,
11799 int *bufsiz,
11800 const char *fname,
11801 const char *psargs)
11802 {
11803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11804
11805 if (bed->elf_backend_write_core_note != NULL)
11806 {
11807 char *ret;
11808 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11809 NT_PRPSINFO, fname, psargs);
11810 if (ret != NULL)
11811 return ret;
11812 }
11813
11814 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11815 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11816 if (bed->s->elfclass == ELFCLASS32)
11817 {
11818 # if defined (HAVE_PSINFO32_T)
11819 psinfo32_t data;
11820 int note_type = NT_PSINFO;
11821 # else
11822 prpsinfo32_t data;
11823 int note_type = NT_PRPSINFO;
11824 # endif
11825
11826 memset (&data, 0, sizeof (data));
11827 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11828 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11829 return elfcore_write_note (abfd, buf, bufsiz,
11830 "CORE", note_type, &data, sizeof (data));
11831 }
11832 else
11833 # endif
11834 {
11835 # if defined (HAVE_PSINFO_T)
11836 psinfo_t data;
11837 int note_type = NT_PSINFO;
11838 # else
11839 prpsinfo_t data;
11840 int note_type = NT_PRPSINFO;
11841 # endif
11842
11843 memset (&data, 0, sizeof (data));
11844 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11845 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11846 return elfcore_write_note (abfd, buf, bufsiz,
11847 "CORE", note_type, &data, sizeof (data));
11848 }
11849 #endif /* PSINFO_T or PRPSINFO_T */
11850
11851 free (buf);
11852 return NULL;
11853 }
11854 #if GCC_VERSION >= 8000
11855 # pragma GCC diagnostic pop
11856 #endif
11857
11858 char *
11859 elfcore_write_linux_prpsinfo32
11860 (bfd *abfd, char *buf, int *bufsiz,
11861 const struct elf_internal_linux_prpsinfo *prpsinfo)
11862 {
11863 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11864 {
11865 struct elf_external_linux_prpsinfo32_ugid16 data;
11866
11867 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11868 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11869 &data, sizeof (data));
11870 }
11871 else
11872 {
11873 struct elf_external_linux_prpsinfo32_ugid32 data;
11874
11875 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11876 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11877 &data, sizeof (data));
11878 }
11879 }
11880
11881 char *
11882 elfcore_write_linux_prpsinfo64
11883 (bfd *abfd, char *buf, int *bufsiz,
11884 const struct elf_internal_linux_prpsinfo *prpsinfo)
11885 {
11886 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11887 {
11888 struct elf_external_linux_prpsinfo64_ugid16 data;
11889
11890 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11891 return elfcore_write_note (abfd, buf, bufsiz,
11892 "CORE", NT_PRPSINFO, &data, sizeof (data));
11893 }
11894 else
11895 {
11896 struct elf_external_linux_prpsinfo64_ugid32 data;
11897
11898 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11899 return elfcore_write_note (abfd, buf, bufsiz,
11900 "CORE", NT_PRPSINFO, &data, sizeof (data));
11901 }
11902 }
11903
11904 char *
11905 elfcore_write_prstatus (bfd *abfd,
11906 char *buf,
11907 int *bufsiz,
11908 long pid,
11909 int cursig,
11910 const void *gregs)
11911 {
11912 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11913
11914 if (bed->elf_backend_write_core_note != NULL)
11915 {
11916 char *ret;
11917 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11918 NT_PRSTATUS,
11919 pid, cursig, gregs);
11920 if (ret != NULL)
11921 return ret;
11922 }
11923
11924 #if defined (HAVE_PRSTATUS_T)
11925 #if defined (HAVE_PRSTATUS32_T)
11926 if (bed->s->elfclass == ELFCLASS32)
11927 {
11928 prstatus32_t prstat;
11929
11930 memset (&prstat, 0, sizeof (prstat));
11931 prstat.pr_pid = pid;
11932 prstat.pr_cursig = cursig;
11933 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11934 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11935 NT_PRSTATUS, &prstat, sizeof (prstat));
11936 }
11937 else
11938 #endif
11939 {
11940 prstatus_t prstat;
11941
11942 memset (&prstat, 0, sizeof (prstat));
11943 prstat.pr_pid = pid;
11944 prstat.pr_cursig = cursig;
11945 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11946 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11947 NT_PRSTATUS, &prstat, sizeof (prstat));
11948 }
11949 #endif /* HAVE_PRSTATUS_T */
11950
11951 free (buf);
11952 return NULL;
11953 }
11954
11955 #if defined (HAVE_LWPSTATUS_T)
11956 char *
11957 elfcore_write_lwpstatus (bfd *abfd,
11958 char *buf,
11959 int *bufsiz,
11960 long pid,
11961 int cursig,
11962 const void *gregs)
11963 {
11964 lwpstatus_t lwpstat;
11965 const char *note_name = "CORE";
11966
11967 memset (&lwpstat, 0, sizeof (lwpstat));
11968 lwpstat.pr_lwpid = pid >> 16;
11969 lwpstat.pr_cursig = cursig;
11970 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11971 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11972 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11973 #if !defined(gregs)
11974 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11975 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11976 #else
11977 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11978 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11979 #endif
11980 #endif
11981 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11982 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11983 }
11984 #endif /* HAVE_LWPSTATUS_T */
11985
11986 #if defined (HAVE_PSTATUS_T)
11987 char *
11988 elfcore_write_pstatus (bfd *abfd,
11989 char *buf,
11990 int *bufsiz,
11991 long pid,
11992 int cursig ATTRIBUTE_UNUSED,
11993 const void *gregs ATTRIBUTE_UNUSED)
11994 {
11995 const char *note_name = "CORE";
11996 #if defined (HAVE_PSTATUS32_T)
11997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11998
11999 if (bed->s->elfclass == ELFCLASS32)
12000 {
12001 pstatus32_t pstat;
12002
12003 memset (&pstat, 0, sizeof (pstat));
12004 pstat.pr_pid = pid & 0xffff;
12005 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12006 NT_PSTATUS, &pstat, sizeof (pstat));
12007 return buf;
12008 }
12009 else
12010 #endif
12011 {
12012 pstatus_t pstat;
12013
12014 memset (&pstat, 0, sizeof (pstat));
12015 pstat.pr_pid = pid & 0xffff;
12016 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12017 NT_PSTATUS, &pstat, sizeof (pstat));
12018 return buf;
12019 }
12020 }
12021 #endif /* HAVE_PSTATUS_T */
12022
12023 char *
12024 elfcore_write_prfpreg (bfd *abfd,
12025 char *buf,
12026 int *bufsiz,
12027 const void *fpregs,
12028 int size)
12029 {
12030 const char *note_name = "CORE";
12031 return elfcore_write_note (abfd, buf, bufsiz,
12032 note_name, NT_FPREGSET, fpregs, size);
12033 }
12034
12035 char *
12036 elfcore_write_prxfpreg (bfd *abfd,
12037 char *buf,
12038 int *bufsiz,
12039 const void *xfpregs,
12040 int size)
12041 {
12042 char *note_name = "LINUX";
12043 return elfcore_write_note (abfd, buf, bufsiz,
12044 note_name, NT_PRXFPREG, xfpregs, size);
12045 }
12046
12047 char *
12048 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12049 const void *xfpregs, int size)
12050 {
12051 char *note_name;
12052 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12053 note_name = "FreeBSD";
12054 else
12055 note_name = "LINUX";
12056 return elfcore_write_note (abfd, buf, bufsiz,
12057 note_name, NT_X86_XSTATE, xfpregs, size);
12058 }
12059
12060 char *
12061 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12062 const void *regs, int size)
12063 {
12064 char *note_name = "FreeBSD";
12065 return elfcore_write_note (abfd, buf, bufsiz,
12066 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12067 }
12068
12069 char *
12070 elfcore_write_ppc_vmx (bfd *abfd,
12071 char *buf,
12072 int *bufsiz,
12073 const void *ppc_vmx,
12074 int size)
12075 {
12076 char *note_name = "LINUX";
12077 return elfcore_write_note (abfd, buf, bufsiz,
12078 note_name, NT_PPC_VMX, ppc_vmx, size);
12079 }
12080
12081 char *
12082 elfcore_write_ppc_vsx (bfd *abfd,
12083 char *buf,
12084 int *bufsiz,
12085 const void *ppc_vsx,
12086 int size)
12087 {
12088 char *note_name = "LINUX";
12089 return elfcore_write_note (abfd, buf, bufsiz,
12090 note_name, NT_PPC_VSX, ppc_vsx, size);
12091 }
12092
12093 char *
12094 elfcore_write_ppc_tar (bfd *abfd,
12095 char *buf,
12096 int *bufsiz,
12097 const void *ppc_tar,
12098 int size)
12099 {
12100 char *note_name = "LINUX";
12101 return elfcore_write_note (abfd, buf, bufsiz,
12102 note_name, NT_PPC_TAR, ppc_tar, size);
12103 }
12104
12105 char *
12106 elfcore_write_ppc_ppr (bfd *abfd,
12107 char *buf,
12108 int *bufsiz,
12109 const void *ppc_ppr,
12110 int size)
12111 {
12112 char *note_name = "LINUX";
12113 return elfcore_write_note (abfd, buf, bufsiz,
12114 note_name, NT_PPC_PPR, ppc_ppr, size);
12115 }
12116
12117 char *
12118 elfcore_write_ppc_dscr (bfd *abfd,
12119 char *buf,
12120 int *bufsiz,
12121 const void *ppc_dscr,
12122 int size)
12123 {
12124 char *note_name = "LINUX";
12125 return elfcore_write_note (abfd, buf, bufsiz,
12126 note_name, NT_PPC_DSCR, ppc_dscr, size);
12127 }
12128
12129 char *
12130 elfcore_write_ppc_ebb (bfd *abfd,
12131 char *buf,
12132 int *bufsiz,
12133 const void *ppc_ebb,
12134 int size)
12135 {
12136 char *note_name = "LINUX";
12137 return elfcore_write_note (abfd, buf, bufsiz,
12138 note_name, NT_PPC_EBB, ppc_ebb, size);
12139 }
12140
12141 char *
12142 elfcore_write_ppc_pmu (bfd *abfd,
12143 char *buf,
12144 int *bufsiz,
12145 const void *ppc_pmu,
12146 int size)
12147 {
12148 char *note_name = "LINUX";
12149 return elfcore_write_note (abfd, buf, bufsiz,
12150 note_name, NT_PPC_PMU, ppc_pmu, size);
12151 }
12152
12153 char *
12154 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12155 char *buf,
12156 int *bufsiz,
12157 const void *ppc_tm_cgpr,
12158 int size)
12159 {
12160 char *note_name = "LINUX";
12161 return elfcore_write_note (abfd, buf, bufsiz,
12162 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12163 }
12164
12165 char *
12166 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12167 char *buf,
12168 int *bufsiz,
12169 const void *ppc_tm_cfpr,
12170 int size)
12171 {
12172 char *note_name = "LINUX";
12173 return elfcore_write_note (abfd, buf, bufsiz,
12174 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12175 }
12176
12177 char *
12178 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12179 char *buf,
12180 int *bufsiz,
12181 const void *ppc_tm_cvmx,
12182 int size)
12183 {
12184 char *note_name = "LINUX";
12185 return elfcore_write_note (abfd, buf, bufsiz,
12186 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12187 }
12188
12189 char *
12190 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12191 char *buf,
12192 int *bufsiz,
12193 const void *ppc_tm_cvsx,
12194 int size)
12195 {
12196 char *note_name = "LINUX";
12197 return elfcore_write_note (abfd, buf, bufsiz,
12198 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12199 }
12200
12201 char *
12202 elfcore_write_ppc_tm_spr (bfd *abfd,
12203 char *buf,
12204 int *bufsiz,
12205 const void *ppc_tm_spr,
12206 int size)
12207 {
12208 char *note_name = "LINUX";
12209 return elfcore_write_note (abfd, buf, bufsiz,
12210 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12211 }
12212
12213 char *
12214 elfcore_write_ppc_tm_ctar (bfd *abfd,
12215 char *buf,
12216 int *bufsiz,
12217 const void *ppc_tm_ctar,
12218 int size)
12219 {
12220 char *note_name = "LINUX";
12221 return elfcore_write_note (abfd, buf, bufsiz,
12222 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12223 }
12224
12225 char *
12226 elfcore_write_ppc_tm_cppr (bfd *abfd,
12227 char *buf,
12228 int *bufsiz,
12229 const void *ppc_tm_cppr,
12230 int size)
12231 {
12232 char *note_name = "LINUX";
12233 return elfcore_write_note (abfd, buf, bufsiz,
12234 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12235 }
12236
12237 char *
12238 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12239 char *buf,
12240 int *bufsiz,
12241 const void *ppc_tm_cdscr,
12242 int size)
12243 {
12244 char *note_name = "LINUX";
12245 return elfcore_write_note (abfd, buf, bufsiz,
12246 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12247 }
12248
12249 static char *
12250 elfcore_write_s390_high_gprs (bfd *abfd,
12251 char *buf,
12252 int *bufsiz,
12253 const void *s390_high_gprs,
12254 int size)
12255 {
12256 char *note_name = "LINUX";
12257 return elfcore_write_note (abfd, buf, bufsiz,
12258 note_name, NT_S390_HIGH_GPRS,
12259 s390_high_gprs, size);
12260 }
12261
12262 char *
12263 elfcore_write_s390_timer (bfd *abfd,
12264 char *buf,
12265 int *bufsiz,
12266 const void *s390_timer,
12267 int size)
12268 {
12269 char *note_name = "LINUX";
12270 return elfcore_write_note (abfd, buf, bufsiz,
12271 note_name, NT_S390_TIMER, s390_timer, size);
12272 }
12273
12274 char *
12275 elfcore_write_s390_todcmp (bfd *abfd,
12276 char *buf,
12277 int *bufsiz,
12278 const void *s390_todcmp,
12279 int size)
12280 {
12281 char *note_name = "LINUX";
12282 return elfcore_write_note (abfd, buf, bufsiz,
12283 note_name, NT_S390_TODCMP, s390_todcmp, size);
12284 }
12285
12286 char *
12287 elfcore_write_s390_todpreg (bfd *abfd,
12288 char *buf,
12289 int *bufsiz,
12290 const void *s390_todpreg,
12291 int size)
12292 {
12293 char *note_name = "LINUX";
12294 return elfcore_write_note (abfd, buf, bufsiz,
12295 note_name, NT_S390_TODPREG, s390_todpreg, size);
12296 }
12297
12298 char *
12299 elfcore_write_s390_ctrs (bfd *abfd,
12300 char *buf,
12301 int *bufsiz,
12302 const void *s390_ctrs,
12303 int size)
12304 {
12305 char *note_name = "LINUX";
12306 return elfcore_write_note (abfd, buf, bufsiz,
12307 note_name, NT_S390_CTRS, s390_ctrs, size);
12308 }
12309
12310 char *
12311 elfcore_write_s390_prefix (bfd *abfd,
12312 char *buf,
12313 int *bufsiz,
12314 const void *s390_prefix,
12315 int size)
12316 {
12317 char *note_name = "LINUX";
12318 return elfcore_write_note (abfd, buf, bufsiz,
12319 note_name, NT_S390_PREFIX, s390_prefix, size);
12320 }
12321
12322 char *
12323 elfcore_write_s390_last_break (bfd *abfd,
12324 char *buf,
12325 int *bufsiz,
12326 const void *s390_last_break,
12327 int size)
12328 {
12329 char *note_name = "LINUX";
12330 return elfcore_write_note (abfd, buf, bufsiz,
12331 note_name, NT_S390_LAST_BREAK,
12332 s390_last_break, size);
12333 }
12334
12335 char *
12336 elfcore_write_s390_system_call (bfd *abfd,
12337 char *buf,
12338 int *bufsiz,
12339 const void *s390_system_call,
12340 int size)
12341 {
12342 char *note_name = "LINUX";
12343 return elfcore_write_note (abfd, buf, bufsiz,
12344 note_name, NT_S390_SYSTEM_CALL,
12345 s390_system_call, size);
12346 }
12347
12348 char *
12349 elfcore_write_s390_tdb (bfd *abfd,
12350 char *buf,
12351 int *bufsiz,
12352 const void *s390_tdb,
12353 int size)
12354 {
12355 char *note_name = "LINUX";
12356 return elfcore_write_note (abfd, buf, bufsiz,
12357 note_name, NT_S390_TDB, s390_tdb, size);
12358 }
12359
12360 char *
12361 elfcore_write_s390_vxrs_low (bfd *abfd,
12362 char *buf,
12363 int *bufsiz,
12364 const void *s390_vxrs_low,
12365 int size)
12366 {
12367 char *note_name = "LINUX";
12368 return elfcore_write_note (abfd, buf, bufsiz,
12369 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12370 }
12371
12372 char *
12373 elfcore_write_s390_vxrs_high (bfd *abfd,
12374 char *buf,
12375 int *bufsiz,
12376 const void *s390_vxrs_high,
12377 int size)
12378 {
12379 char *note_name = "LINUX";
12380 return elfcore_write_note (abfd, buf, bufsiz,
12381 note_name, NT_S390_VXRS_HIGH,
12382 s390_vxrs_high, size);
12383 }
12384
12385 char *
12386 elfcore_write_s390_gs_cb (bfd *abfd,
12387 char *buf,
12388 int *bufsiz,
12389 const void *s390_gs_cb,
12390 int size)
12391 {
12392 char *note_name = "LINUX";
12393 return elfcore_write_note (abfd, buf, bufsiz,
12394 note_name, NT_S390_GS_CB,
12395 s390_gs_cb, size);
12396 }
12397
12398 char *
12399 elfcore_write_s390_gs_bc (bfd *abfd,
12400 char *buf,
12401 int *bufsiz,
12402 const void *s390_gs_bc,
12403 int size)
12404 {
12405 char *note_name = "LINUX";
12406 return elfcore_write_note (abfd, buf, bufsiz,
12407 note_name, NT_S390_GS_BC,
12408 s390_gs_bc, size);
12409 }
12410
12411 char *
12412 elfcore_write_arm_vfp (bfd *abfd,
12413 char *buf,
12414 int *bufsiz,
12415 const void *arm_vfp,
12416 int size)
12417 {
12418 char *note_name = "LINUX";
12419 return elfcore_write_note (abfd, buf, bufsiz,
12420 note_name, NT_ARM_VFP, arm_vfp, size);
12421 }
12422
12423 char *
12424 elfcore_write_aarch_tls (bfd *abfd,
12425 char *buf,
12426 int *bufsiz,
12427 const void *aarch_tls,
12428 int size)
12429 {
12430 char *note_name = "LINUX";
12431 return elfcore_write_note (abfd, buf, bufsiz,
12432 note_name, NT_ARM_TLS, aarch_tls, size);
12433 }
12434
12435 char *
12436 elfcore_write_aarch_hw_break (bfd *abfd,
12437 char *buf,
12438 int *bufsiz,
12439 const void *aarch_hw_break,
12440 int size)
12441 {
12442 char *note_name = "LINUX";
12443 return elfcore_write_note (abfd, buf, bufsiz,
12444 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12445 }
12446
12447 char *
12448 elfcore_write_aarch_hw_watch (bfd *abfd,
12449 char *buf,
12450 int *bufsiz,
12451 const void *aarch_hw_watch,
12452 int size)
12453 {
12454 char *note_name = "LINUX";
12455 return elfcore_write_note (abfd, buf, bufsiz,
12456 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12457 }
12458
12459 char *
12460 elfcore_write_aarch_sve (bfd *abfd,
12461 char *buf,
12462 int *bufsiz,
12463 const void *aarch_sve,
12464 int size)
12465 {
12466 char *note_name = "LINUX";
12467 return elfcore_write_note (abfd, buf, bufsiz,
12468 note_name, NT_ARM_SVE, aarch_sve, size);
12469 }
12470
12471 char *
12472 elfcore_write_aarch_pauth (bfd *abfd,
12473 char *buf,
12474 int *bufsiz,
12475 const void *aarch_pauth,
12476 int size)
12477 {
12478 char *note_name = "LINUX";
12479 return elfcore_write_note (abfd, buf, bufsiz,
12480 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12481 }
12482
12483 char *
12484 elfcore_write_aarch_mte (bfd *abfd,
12485 char *buf,
12486 int *bufsiz,
12487 const void *aarch_mte,
12488 int size)
12489 {
12490 char *note_name = "LINUX";
12491 return elfcore_write_note (abfd, buf, bufsiz,
12492 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12493 aarch_mte,
12494 size);
12495 }
12496
12497 char *
12498 elfcore_write_arc_v2 (bfd *abfd,
12499 char *buf,
12500 int *bufsiz,
12501 const void *arc_v2,
12502 int size)
12503 {
12504 char *note_name = "LINUX";
12505 return elfcore_write_note (abfd, buf, bufsiz,
12506 note_name, NT_ARC_V2, arc_v2, size);
12507 }
12508
12509 char *
12510 elfcore_write_loongarch_cpucfg (bfd *abfd,
12511 char *buf,
12512 int *bufsiz,
12513 const void *loongarch_cpucfg,
12514 int size)
12515 {
12516 char *note_name = "LINUX";
12517 return elfcore_write_note (abfd, buf, bufsiz,
12518 note_name, NT_LARCH_CPUCFG,
12519 loongarch_cpucfg, size);
12520 }
12521
12522 char *
12523 elfcore_write_loongarch_lbt (bfd *abfd,
12524 char *buf,
12525 int *bufsiz,
12526 const void *loongarch_lbt,
12527 int size)
12528 {
12529 char *note_name = "LINUX";
12530 return elfcore_write_note (abfd, buf, bufsiz,
12531 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12532 }
12533
12534 char *
12535 elfcore_write_loongarch_lsx (bfd *abfd,
12536 char *buf,
12537 int *bufsiz,
12538 const void *loongarch_lsx,
12539 int size)
12540 {
12541 char *note_name = "LINUX";
12542 return elfcore_write_note (abfd, buf, bufsiz,
12543 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12544 }
12545
12546 char *
12547 elfcore_write_loongarch_lasx (bfd *abfd,
12548 char *buf,
12549 int *bufsiz,
12550 const void *loongarch_lasx,
12551 int size)
12552 {
12553 char *note_name = "LINUX";
12554 return elfcore_write_note (abfd, buf, bufsiz,
12555 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12556 }
12557
12558 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12559 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12560 written into. Return a pointer to the new start of the note buffer, to
12561 replace BUF which may no longer be valid. */
12562
12563 char *
12564 elfcore_write_riscv_csr (bfd *abfd,
12565 char *buf,
12566 int *bufsiz,
12567 const void *csrs,
12568 int size)
12569 {
12570 const char *note_name = "GDB";
12571 return elfcore_write_note (abfd, buf, bufsiz,
12572 note_name, NT_RISCV_CSR, csrs, size);
12573 }
12574
12575 /* Write the target description (a string) pointed to by TDESC, length
12576 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12577 note is being written into. Return a pointer to the new start of the
12578 note buffer, to replace BUF which may no longer be valid. */
12579
12580 char *
12581 elfcore_write_gdb_tdesc (bfd *abfd,
12582 char *buf,
12583 int *bufsiz,
12584 const void *tdesc,
12585 int size)
12586 {
12587 const char *note_name = "GDB";
12588 return elfcore_write_note (abfd, buf, bufsiz,
12589 note_name, NT_GDB_TDESC, tdesc, size);
12590 }
12591
12592 char *
12593 elfcore_write_register_note (bfd *abfd,
12594 char *buf,
12595 int *bufsiz,
12596 const char *section,
12597 const void *data,
12598 int size)
12599 {
12600 if (strcmp (section, ".reg2") == 0)
12601 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12602 if (strcmp (section, ".reg-xfp") == 0)
12603 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12604 if (strcmp (section, ".reg-xstate") == 0)
12605 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12606 if (strcmp (section, ".reg-x86-segbases") == 0)
12607 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12608 if (strcmp (section, ".reg-ppc-vmx") == 0)
12609 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12610 if (strcmp (section, ".reg-ppc-vsx") == 0)
12611 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12612 if (strcmp (section, ".reg-ppc-tar") == 0)
12613 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12614 if (strcmp (section, ".reg-ppc-ppr") == 0)
12615 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12616 if (strcmp (section, ".reg-ppc-dscr") == 0)
12617 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12618 if (strcmp (section, ".reg-ppc-ebb") == 0)
12619 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12620 if (strcmp (section, ".reg-ppc-pmu") == 0)
12621 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12622 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12623 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12624 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12625 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12626 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12627 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12628 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12629 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12630 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12631 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12632 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12633 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12634 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12635 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12636 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12637 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12638 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12639 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12640 if (strcmp (section, ".reg-s390-timer") == 0)
12641 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12642 if (strcmp (section, ".reg-s390-todcmp") == 0)
12643 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12644 if (strcmp (section, ".reg-s390-todpreg") == 0)
12645 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12646 if (strcmp (section, ".reg-s390-ctrs") == 0)
12647 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12648 if (strcmp (section, ".reg-s390-prefix") == 0)
12649 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12650 if (strcmp (section, ".reg-s390-last-break") == 0)
12651 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12652 if (strcmp (section, ".reg-s390-system-call") == 0)
12653 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12654 if (strcmp (section, ".reg-s390-tdb") == 0)
12655 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12656 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12657 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12658 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12659 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12660 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12661 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12662 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12663 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12664 if (strcmp (section, ".reg-arm-vfp") == 0)
12665 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12666 if (strcmp (section, ".reg-aarch-tls") == 0)
12667 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12668 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12669 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12670 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12671 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12672 if (strcmp (section, ".reg-aarch-sve") == 0)
12673 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12674 if (strcmp (section, ".reg-aarch-pauth") == 0)
12675 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12676 if (strcmp (section, ".reg-aarch-mte") == 0)
12677 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12678 if (strcmp (section, ".reg-arc-v2") == 0)
12679 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12680 if (strcmp (section, ".gdb-tdesc") == 0)
12681 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12682 if (strcmp (section, ".reg-riscv-csr") == 0)
12683 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12684 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12685 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12686 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12687 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12688 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12689 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12690 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12691 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12692 return NULL;
12693 }
12694
12695 char *
12696 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12697 const void *buf, int bufsiz)
12698 {
12699 return elfcore_write_note (obfd, note_data, note_size,
12700 "CORE", NT_FILE, buf, bufsiz);
12701 }
12702
12703 static bool
12704 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12705 size_t align)
12706 {
12707 char *p;
12708
12709 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12710 gABI specifies that PT_NOTE alignment should be aligned to 4
12711 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12712 align is less than 4, we use 4 byte alignment. */
12713 if (align < 4)
12714 align = 4;
12715 if (align != 4 && align != 8)
12716 return false;
12717
12718 p = buf;
12719 while (p < buf + size)
12720 {
12721 Elf_External_Note *xnp = (Elf_External_Note *) p;
12722 Elf_Internal_Note in;
12723
12724 if (offsetof (Elf_External_Note, name) > buf - p + size)
12725 return false;
12726
12727 in.type = H_GET_32 (abfd, xnp->type);
12728
12729 in.namesz = H_GET_32 (abfd, xnp->namesz);
12730 in.namedata = xnp->name;
12731 if (in.namesz > buf - in.namedata + size)
12732 return false;
12733
12734 in.descsz = H_GET_32 (abfd, xnp->descsz);
12735 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12736 in.descpos = offset + (in.descdata - buf);
12737 if (in.descsz != 0
12738 && (in.descdata >= buf + size
12739 || in.descsz > buf - in.descdata + size))
12740 return false;
12741
12742 switch (bfd_get_format (abfd))
12743 {
12744 default:
12745 return true;
12746
12747 case bfd_core:
12748 {
12749 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12750 struct
12751 {
12752 const char * string;
12753 size_t len;
12754 bool (*func) (bfd *, Elf_Internal_Note *);
12755 }
12756 grokers[] =
12757 {
12758 GROKER_ELEMENT ("", elfcore_grok_note),
12759 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12760 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12761 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12762 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12763 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12764 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12765 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12766 };
12767 #undef GROKER_ELEMENT
12768 int i;
12769
12770 for (i = ARRAY_SIZE (grokers); i--;)
12771 {
12772 if (in.namesz >= grokers[i].len
12773 && strncmp (in.namedata, grokers[i].string,
12774 grokers[i].len) == 0)
12775 {
12776 if (! grokers[i].func (abfd, & in))
12777 return false;
12778 break;
12779 }
12780 }
12781 break;
12782 }
12783
12784 case bfd_object:
12785 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12786 {
12787 if (! elfobj_grok_gnu_note (abfd, &in))
12788 return false;
12789 }
12790 else if (in.namesz == sizeof "stapsdt"
12791 && strcmp (in.namedata, "stapsdt") == 0)
12792 {
12793 if (! elfobj_grok_stapsdt_note (abfd, &in))
12794 return false;
12795 }
12796 break;
12797 }
12798
12799 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12800 }
12801
12802 return true;
12803 }
12804
12805 bool
12806 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12807 size_t align)
12808 {
12809 char *buf;
12810
12811 if (size == 0 || (size + 1) == 0)
12812 return true;
12813
12814 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12815 return false;
12816
12817 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12818 if (buf == NULL)
12819 return false;
12820
12821 /* PR 17512: file: ec08f814
12822 0-termintate the buffer so that string searches will not overflow. */
12823 buf[size] = 0;
12824
12825 if (!elf_parse_notes (abfd, buf, size, offset, align))
12826 {
12827 free (buf);
12828 return false;
12829 }
12830
12831 free (buf);
12832 return true;
12833 }
12834 \f
12835 /* Providing external access to the ELF program header table. */
12836
12837 /* Return an upper bound on the number of bytes required to store a
12838 copy of ABFD's program header table entries. Return -1 if an error
12839 occurs; bfd_get_error will return an appropriate code. */
12840
12841 long
12842 bfd_get_elf_phdr_upper_bound (bfd *abfd)
12843 {
12844 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12845 {
12846 bfd_set_error (bfd_error_wrong_format);
12847 return -1;
12848 }
12849
12850 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12851 }
12852
12853 /* Copy ABFD's program header table entries to *PHDRS. The entries
12854 will be stored as an array of Elf_Internal_Phdr structures, as
12855 defined in include/elf/internal.h. To find out how large the
12856 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12857
12858 Return the number of program header table entries read, or -1 if an
12859 error occurs; bfd_get_error will return an appropriate code. */
12860
12861 int
12862 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12863 {
12864 int num_phdrs;
12865
12866 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12867 {
12868 bfd_set_error (bfd_error_wrong_format);
12869 return -1;
12870 }
12871
12872 num_phdrs = elf_elfheader (abfd)->e_phnum;
12873 if (num_phdrs != 0)
12874 memcpy (phdrs, elf_tdata (abfd)->phdr,
12875 num_phdrs * sizeof (Elf_Internal_Phdr));
12876
12877 return num_phdrs;
12878 }
12879
12880 enum elf_reloc_type_class
12881 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12882 const asection *rel_sec ATTRIBUTE_UNUSED,
12883 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12884 {
12885 return reloc_class_normal;
12886 }
12887
12888 /* For RELA architectures, return the relocation value for a
12889 relocation against a local symbol. */
12890
12891 bfd_vma
12892 _bfd_elf_rela_local_sym (bfd *abfd,
12893 Elf_Internal_Sym *sym,
12894 asection **psec,
12895 Elf_Internal_Rela *rel)
12896 {
12897 asection *sec = *psec;
12898 bfd_vma relocation;
12899
12900 relocation = (sec->output_section->vma
12901 + sec->output_offset
12902 + sym->st_value);
12903 if ((sec->flags & SEC_MERGE)
12904 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12905 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12906 {
12907 rel->r_addend =
12908 _bfd_merged_section_offset (abfd, psec,
12909 elf_section_data (sec)->sec_info,
12910 sym->st_value + rel->r_addend);
12911 if (sec != *psec)
12912 {
12913 /* If we have changed the section, and our original section is
12914 marked with SEC_EXCLUDE, it means that the original
12915 SEC_MERGE section has been completely subsumed in some
12916 other SEC_MERGE section. In this case, we need to leave
12917 some info around for --emit-relocs. */
12918 if ((sec->flags & SEC_EXCLUDE) != 0)
12919 sec->kept_section = *psec;
12920 sec = *psec;
12921 }
12922 rel->r_addend -= relocation;
12923 rel->r_addend += sec->output_section->vma + sec->output_offset;
12924 }
12925 return relocation;
12926 }
12927
12928 bfd_vma
12929 _bfd_elf_rel_local_sym (bfd *abfd,
12930 Elf_Internal_Sym *sym,
12931 asection **psec,
12932 bfd_vma addend)
12933 {
12934 asection *sec = *psec;
12935
12936 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12937 return sym->st_value + addend;
12938
12939 return _bfd_merged_section_offset (abfd, psec,
12940 elf_section_data (sec)->sec_info,
12941 sym->st_value + addend);
12942 }
12943
12944 /* Adjust an address within a section. Given OFFSET within SEC, return
12945 the new offset within the section, based upon changes made to the
12946 section. Returns -1 if the offset is now invalid.
12947 The offset (in abnd out) is in target sized bytes, however big a
12948 byte may be. */
12949
12950 bfd_vma
12951 _bfd_elf_section_offset (bfd *abfd,
12952 struct bfd_link_info *info,
12953 asection *sec,
12954 bfd_vma offset)
12955 {
12956 switch (sec->sec_info_type)
12957 {
12958 case SEC_INFO_TYPE_STABS:
12959 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12960 offset);
12961 case SEC_INFO_TYPE_EH_FRAME:
12962 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12963
12964 default:
12965 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12966 {
12967 /* Reverse the offset. */
12968 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12969 bfd_size_type address_size = bed->s->arch_size / 8;
12970
12971 /* address_size and sec->size are in octets. Convert
12972 to bytes before subtracting the original offset. */
12973 offset = ((sec->size - address_size)
12974 / bfd_octets_per_byte (abfd, sec) - offset);
12975 }
12976 return offset;
12977 }
12978 }
12979 \f
12980 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12981 reconstruct an ELF file by reading the segments out of remote memory
12982 based on the ELF file header at EHDR_VMA and the ELF program headers it
12983 points to. If not null, *LOADBASEP is filled in with the difference
12984 between the VMAs from which the segments were read, and the VMAs the
12985 file headers (and hence BFD's idea of each section's VMA) put them at.
12986
12987 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12988 remote memory at target address VMA into the local buffer at MYADDR; it
12989 should return zero on success or an `errno' code on failure. TEMPL must
12990 be a BFD for an ELF target with the word size and byte order found in
12991 the remote memory. */
12992
12993 bfd *
12994 bfd_elf_bfd_from_remote_memory
12995 (bfd *templ,
12996 bfd_vma ehdr_vma,
12997 bfd_size_type size,
12998 bfd_vma *loadbasep,
12999 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
13000 {
13001 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
13002 (templ, ehdr_vma, size, loadbasep, target_read_memory);
13003 }
13004 \f
13005 long
13006 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13007 long symcount ATTRIBUTE_UNUSED,
13008 asymbol **syms ATTRIBUTE_UNUSED,
13009 long dynsymcount,
13010 asymbol **dynsyms,
13011 asymbol **ret)
13012 {
13013 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13014 asection *relplt;
13015 asymbol *s;
13016 const char *relplt_name;
13017 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13018 arelent *p;
13019 long count, i, n;
13020 size_t size;
13021 Elf_Internal_Shdr *hdr;
13022 char *names;
13023 asection *plt;
13024
13025 *ret = NULL;
13026
13027 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13028 return 0;
13029
13030 if (dynsymcount <= 0)
13031 return 0;
13032
13033 if (!bed->plt_sym_val)
13034 return 0;
13035
13036 relplt_name = bed->relplt_name;
13037 if (relplt_name == NULL)
13038 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13039 relplt = bfd_get_section_by_name (abfd, relplt_name);
13040 if (relplt == NULL)
13041 return 0;
13042
13043 hdr = &elf_section_data (relplt)->this_hdr;
13044 if (hdr->sh_link != elf_dynsymtab (abfd)
13045 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13046 return 0;
13047
13048 plt = bfd_get_section_by_name (abfd, ".plt");
13049 if (plt == NULL)
13050 return 0;
13051
13052 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13053 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13054 return -1;
13055
13056 count = relplt->size / hdr->sh_entsize;
13057 size = count * sizeof (asymbol);
13058 p = relplt->relocation;
13059 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13060 {
13061 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13062 if (p->addend != 0)
13063 {
13064 #ifdef BFD64
13065 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13066 #else
13067 size += sizeof ("+0x") - 1 + 8;
13068 #endif
13069 }
13070 }
13071
13072 s = *ret = (asymbol *) bfd_malloc (size);
13073 if (s == NULL)
13074 return -1;
13075
13076 names = (char *) (s + count);
13077 p = relplt->relocation;
13078 n = 0;
13079 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13080 {
13081 size_t len;
13082 bfd_vma addr;
13083
13084 addr = bed->plt_sym_val (i, plt, p);
13085 if (addr == (bfd_vma) -1)
13086 continue;
13087
13088 *s = **p->sym_ptr_ptr;
13089 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13090 we are defining a symbol, ensure one of them is set. */
13091 if ((s->flags & BSF_LOCAL) == 0)
13092 s->flags |= BSF_GLOBAL;
13093 s->flags |= BSF_SYNTHETIC;
13094 s->section = plt;
13095 s->value = addr - plt->vma;
13096 s->name = names;
13097 s->udata.p = NULL;
13098 len = strlen ((*p->sym_ptr_ptr)->name);
13099 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13100 names += len;
13101 if (p->addend != 0)
13102 {
13103 char buf[30], *a;
13104
13105 memcpy (names, "+0x", sizeof ("+0x") - 1);
13106 names += sizeof ("+0x") - 1;
13107 bfd_sprintf_vma (abfd, buf, p->addend);
13108 for (a = buf; *a == '0'; ++a)
13109 ;
13110 len = strlen (a);
13111 memcpy (names, a, len);
13112 names += len;
13113 }
13114 memcpy (names, "@plt", sizeof ("@plt"));
13115 names += sizeof ("@plt");
13116 ++s, ++n;
13117 }
13118
13119 return n;
13120 }
13121
13122 /* It is only used by x86-64 so far.
13123 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13124 but current usage would allow all of _bfd_std_section to be zero. */
13125 static const asymbol lcomm_sym
13126 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13127 asection _bfd_elf_large_com_section
13128 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13129 "LARGE_COMMON", 0, SEC_IS_COMMON);
13130
13131 bool
13132 _bfd_elf_final_write_processing (bfd *abfd)
13133 {
13134 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13135
13136 i_ehdrp = elf_elfheader (abfd);
13137
13138 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13139 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13140
13141 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13142 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13143 or STB_GNU_UNIQUE binding. */
13144 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13145 {
13146 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13147 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13148 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13149 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13150 {
13151 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13152 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13153 "and FreeBSD targets"));
13154 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13155 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13156 "only by GNU and FreeBSD targets"));
13157 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13158 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13159 "only by GNU and FreeBSD targets"));
13160 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13161 _bfd_error_handler (_("GNU_RETAIN section is supported "
13162 "only by GNU and FreeBSD targets"));
13163 bfd_set_error (bfd_error_sorry);
13164 return false;
13165 }
13166 }
13167 return true;
13168 }
13169
13170
13171 /* Return TRUE for ELF symbol types that represent functions.
13172 This is the default version of this function, which is sufficient for
13173 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13174
13175 bool
13176 _bfd_elf_is_function_type (unsigned int type)
13177 {
13178 return (type == STT_FUNC
13179 || type == STT_GNU_IFUNC);
13180 }
13181
13182 /* If the ELF symbol SYM might be a function in SEC, return the
13183 function size and set *CODE_OFF to the function's entry point,
13184 otherwise return zero. */
13185
13186 bfd_size_type
13187 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13188 bfd_vma *code_off)
13189 {
13190 bfd_size_type size;
13191 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13192
13193 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13194 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13195 || sym->section != sec)
13196 return 0;
13197
13198 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13199
13200 /* In theory we should check that the symbol's type satisfies
13201 _bfd_elf_is_function_type(), but there are some function-like
13202 symbols which would fail this test. (eg _start). Instead
13203 we check for hidden, local, notype symbols with zero size.
13204 This type of symbol is generated by the annobin plugin for gcc
13205 and clang, and should not be considered to be a function symbol. */
13206 if (size == 0
13207 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13208 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13209 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13210 return 0;
13211
13212 *code_off = sym->value;
13213 /* Do not return 0 for the function's size. */
13214 return size ? size : 1;
13215 }
13216
13217 /* Set to non-zero to enable some debug messages. */
13218 #define DEBUG_SECONDARY_RELOCS 0
13219
13220 /* An internal-to-the-bfd-library only section type
13221 used to indicate a cached secondary reloc section. */
13222 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13223
13224 /* Create a BFD section to hold a secondary reloc section. */
13225
13226 bool
13227 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13228 Elf_Internal_Shdr *hdr,
13229 const char * name,
13230 unsigned int shindex)
13231 {
13232 /* We only support RELA secondary relocs. */
13233 if (hdr->sh_type != SHT_RELA)
13234 return false;
13235
13236 #if DEBUG_SECONDARY_RELOCS
13237 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13238 #endif
13239 hdr->sh_type = SHT_SECONDARY_RELOC;
13240 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13241 }
13242
13243 /* Read in any secondary relocs associated with SEC. */
13244
13245 bool
13246 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13247 asection * sec,
13248 asymbol ** symbols,
13249 bool dynamic)
13250 {
13251 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13252 asection * relsec;
13253 bool result = true;
13254 bfd_vma (*r_sym) (bfd_vma);
13255
13256 #if BFD_DEFAULT_TARGET_SIZE > 32
13257 if (bfd_arch_bits_per_address (abfd) != 32)
13258 r_sym = elf64_r_sym;
13259 else
13260 #endif
13261 r_sym = elf32_r_sym;
13262
13263 if (!elf_section_data (sec)->has_secondary_relocs)
13264 return true;
13265
13266 /* Discover if there are any secondary reloc sections
13267 associated with SEC. */
13268 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13269 {
13270 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13271
13272 if (hdr->sh_type == SHT_SECONDARY_RELOC
13273 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13274 && (hdr->sh_entsize == ebd->s->sizeof_rel
13275 || hdr->sh_entsize == ebd->s->sizeof_rela))
13276 {
13277 bfd_byte * native_relocs;
13278 bfd_byte * native_reloc;
13279 arelent * internal_relocs;
13280 arelent * internal_reloc;
13281 unsigned int i;
13282 unsigned int entsize;
13283 unsigned int symcount;
13284 unsigned int reloc_count;
13285 size_t amt;
13286
13287 if (ebd->elf_info_to_howto == NULL)
13288 return false;
13289
13290 #if DEBUG_SECONDARY_RELOCS
13291 fprintf (stderr, "read secondary relocs for %s from %s\n",
13292 sec->name, relsec->name);
13293 #endif
13294 entsize = hdr->sh_entsize;
13295
13296 native_relocs = bfd_malloc (hdr->sh_size);
13297 if (native_relocs == NULL)
13298 {
13299 result = false;
13300 continue;
13301 }
13302
13303 reloc_count = NUM_SHDR_ENTRIES (hdr);
13304 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13305 {
13306 free (native_relocs);
13307 bfd_set_error (bfd_error_file_too_big);
13308 result = false;
13309 continue;
13310 }
13311
13312 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13313 if (internal_relocs == NULL)
13314 {
13315 free (native_relocs);
13316 result = false;
13317 continue;
13318 }
13319
13320 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13321 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13322 != hdr->sh_size))
13323 {
13324 free (native_relocs);
13325 /* The internal_relocs will be freed when
13326 the memory for the bfd is released. */
13327 result = false;
13328 continue;
13329 }
13330
13331 if (dynamic)
13332 symcount = bfd_get_dynamic_symcount (abfd);
13333 else
13334 symcount = bfd_get_symcount (abfd);
13335
13336 for (i = 0, internal_reloc = internal_relocs,
13337 native_reloc = native_relocs;
13338 i < reloc_count;
13339 i++, internal_reloc++, native_reloc += entsize)
13340 {
13341 bool res;
13342 Elf_Internal_Rela rela;
13343
13344 if (entsize == ebd->s->sizeof_rel)
13345 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13346 else /* entsize == ebd->s->sizeof_rela */
13347 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13348
13349 /* The address of an ELF reloc is section relative for an object
13350 file, and absolute for an executable file or shared library.
13351 The address of a normal BFD reloc is always section relative,
13352 and the address of a dynamic reloc is absolute.. */
13353 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13354 internal_reloc->address = rela.r_offset;
13355 else
13356 internal_reloc->address = rela.r_offset - sec->vma;
13357
13358 if (r_sym (rela.r_info) == STN_UNDEF)
13359 {
13360 /* FIXME: This and the error case below mean that we
13361 have a symbol on relocs that is not elf_symbol_type. */
13362 internal_reloc->sym_ptr_ptr =
13363 bfd_abs_section_ptr->symbol_ptr_ptr;
13364 }
13365 else if (r_sym (rela.r_info) > symcount)
13366 {
13367 _bfd_error_handler
13368 /* xgettext:c-format */
13369 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13370 abfd, sec, i, (long) r_sym (rela.r_info));
13371 bfd_set_error (bfd_error_bad_value);
13372 internal_reloc->sym_ptr_ptr =
13373 bfd_abs_section_ptr->symbol_ptr_ptr;
13374 result = false;
13375 }
13376 else
13377 {
13378 asymbol **ps;
13379
13380 ps = symbols + r_sym (rela.r_info) - 1;
13381 internal_reloc->sym_ptr_ptr = ps;
13382 /* Make sure that this symbol is not removed by strip. */
13383 (*ps)->flags |= BSF_KEEP;
13384 }
13385
13386 internal_reloc->addend = rela.r_addend;
13387
13388 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13389 if (! res || internal_reloc->howto == NULL)
13390 {
13391 #if DEBUG_SECONDARY_RELOCS
13392 fprintf (stderr,
13393 "there is no howto associated with reloc %lx\n",
13394 rela.r_info);
13395 #endif
13396 result = false;
13397 }
13398 }
13399
13400 free (native_relocs);
13401 /* Store the internal relocs. */
13402 elf_section_data (relsec)->sec_info = internal_relocs;
13403 }
13404 }
13405
13406 return result;
13407 }
13408
13409 /* Set the ELF section header fields of an output secondary reloc section. */
13410
13411 bool
13412 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13413 bfd *obfd ATTRIBUTE_UNUSED,
13414 const Elf_Internal_Shdr *isection,
13415 Elf_Internal_Shdr *osection)
13416 {
13417 asection * isec;
13418 asection * osec;
13419 struct bfd_elf_section_data * esd;
13420
13421 if (isection == NULL)
13422 return false;
13423
13424 if (isection->sh_type != SHT_SECONDARY_RELOC)
13425 return true;
13426
13427 isec = isection->bfd_section;
13428 if (isec == NULL)
13429 return false;
13430
13431 osec = osection->bfd_section;
13432 if (osec == NULL)
13433 return false;
13434
13435 esd = elf_section_data (osec);
13436 BFD_ASSERT (esd->sec_info == NULL);
13437 esd->sec_info = elf_section_data (isec)->sec_info;
13438 osection->sh_type = SHT_RELA;
13439 osection->sh_link = elf_onesymtab (obfd);
13440 if (osection->sh_link == 0)
13441 {
13442 /* There is no symbol table - we are hosed... */
13443 _bfd_error_handler
13444 /* xgettext:c-format */
13445 (_("%pB(%pA): link section cannot be set"
13446 " because the output file does not have a symbol table"),
13447 obfd, osec);
13448 bfd_set_error (bfd_error_bad_value);
13449 return false;
13450 }
13451
13452 /* Find the output section that corresponds to the isection's
13453 sh_info link. */
13454 if (isection->sh_info == 0
13455 || isection->sh_info >= elf_numsections (ibfd))
13456 {
13457 _bfd_error_handler
13458 /* xgettext:c-format */
13459 (_("%pB(%pA): info section index is invalid"),
13460 obfd, osec);
13461 bfd_set_error (bfd_error_bad_value);
13462 return false;
13463 }
13464
13465 isection = elf_elfsections (ibfd)[isection->sh_info];
13466
13467 if (isection == NULL
13468 || isection->bfd_section == NULL
13469 || isection->bfd_section->output_section == NULL)
13470 {
13471 _bfd_error_handler
13472 /* xgettext:c-format */
13473 (_("%pB(%pA): info section index cannot be set"
13474 " because the section is not in the output"),
13475 obfd, osec);
13476 bfd_set_error (bfd_error_bad_value);
13477 return false;
13478 }
13479
13480 esd = elf_section_data (isection->bfd_section->output_section);
13481 BFD_ASSERT (esd != NULL);
13482 osection->sh_info = esd->this_idx;
13483 esd->has_secondary_relocs = true;
13484 #if DEBUG_SECONDARY_RELOCS
13485 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13486 osec->name, osection->sh_link, osection->sh_info);
13487 fprintf (stderr, "mark section %s as having secondary relocs\n",
13488 bfd_section_name (isection->bfd_section->output_section));
13489 #endif
13490
13491 return true;
13492 }
13493
13494 /* Write out a secondary reloc section.
13495
13496 FIXME: Currently this function can result in a serious performance penalty
13497 for files with secondary relocs and lots of sections. The proper way to
13498 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13499 relocs together and then to have this function just walk that chain. */
13500
13501 bool
13502 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13503 {
13504 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13505 bfd_vma addr_offset;
13506 asection * relsec;
13507 bfd_vma (*r_info) (bfd_vma, bfd_vma);
13508 bool result = true;
13509
13510 if (sec == NULL)
13511 return false;
13512
13513 #if BFD_DEFAULT_TARGET_SIZE > 32
13514 if (bfd_arch_bits_per_address (abfd) != 32)
13515 r_info = elf64_r_info;
13516 else
13517 #endif
13518 r_info = elf32_r_info;
13519
13520 /* The address of an ELF reloc is section relative for an object
13521 file, and absolute for an executable file or shared library.
13522 The address of a BFD reloc is always section relative. */
13523 addr_offset = 0;
13524 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13525 addr_offset = sec->vma;
13526
13527 /* Discover if there are any secondary reloc sections
13528 associated with SEC. */
13529 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13530 {
13531 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13532 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13533
13534 if (hdr->sh_type == SHT_RELA
13535 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13536 {
13537 asymbol * last_sym;
13538 int last_sym_idx;
13539 size_t reloc_count;
13540 size_t idx;
13541 bfd_size_type entsize;
13542 arelent * src_irel;
13543 bfd_byte * dst_rela;
13544
13545 if (hdr->contents != NULL)
13546 {
13547 _bfd_error_handler
13548 /* xgettext:c-format */
13549 (_("%pB(%pA): error: secondary reloc section processed twice"),
13550 abfd, relsec);
13551 bfd_set_error (bfd_error_bad_value);
13552 result = false;
13553 continue;
13554 }
13555
13556 entsize = hdr->sh_entsize;
13557 if (entsize == 0)
13558 {
13559 _bfd_error_handler
13560 /* xgettext:c-format */
13561 (_("%pB(%pA): error: secondary reloc section"
13562 " has zero sized entries"),
13563 abfd, relsec);
13564 bfd_set_error (bfd_error_bad_value);
13565 result = false;
13566 continue;
13567 }
13568 else if (entsize != ebd->s->sizeof_rel
13569 && entsize != ebd->s->sizeof_rela)
13570 {
13571 _bfd_error_handler
13572 /* xgettext:c-format */
13573 (_("%pB(%pA): error: secondary reloc section"
13574 " has non-standard sized entries"),
13575 abfd, relsec);
13576 bfd_set_error (bfd_error_bad_value);
13577 result = false;
13578 continue;
13579 }
13580
13581 reloc_count = hdr->sh_size / entsize;
13582 hdr->sh_size = entsize * reloc_count;
13583 if (reloc_count == 0)
13584 {
13585 _bfd_error_handler
13586 /* xgettext:c-format */
13587 (_("%pB(%pA): error: secondary reloc section is empty!"),
13588 abfd, relsec);
13589 bfd_set_error (bfd_error_bad_value);
13590 result = false;
13591 continue;
13592 }
13593
13594 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13595 if (hdr->contents == NULL)
13596 continue;
13597
13598 #if DEBUG_SECONDARY_RELOCS
13599 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13600 reloc_count, sec->name, relsec->name);
13601 #endif
13602 last_sym = NULL;
13603 last_sym_idx = 0;
13604 dst_rela = hdr->contents;
13605 src_irel = (arelent *) esd->sec_info;
13606 if (src_irel == NULL)
13607 {
13608 _bfd_error_handler
13609 /* xgettext:c-format */
13610 (_("%pB(%pA): error: internal relocs missing"
13611 " for secondary reloc section"),
13612 abfd, relsec);
13613 bfd_set_error (bfd_error_bad_value);
13614 result = false;
13615 continue;
13616 }
13617
13618 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13619 {
13620 Elf_Internal_Rela src_rela;
13621 arelent *ptr;
13622 asymbol *sym;
13623 int n;
13624
13625 ptr = src_irel + idx;
13626 if (ptr == NULL)
13627 {
13628 _bfd_error_handler
13629 /* xgettext:c-format */
13630 (_("%pB(%pA): error: reloc table entry %zu is empty"),
13631 abfd, relsec, idx);
13632 bfd_set_error (bfd_error_bad_value);
13633 result = false;
13634 break;
13635 }
13636
13637 if (ptr->sym_ptr_ptr == NULL)
13638 {
13639 /* FIXME: Is this an error ? */
13640 n = 0;
13641 }
13642 else
13643 {
13644 sym = *ptr->sym_ptr_ptr;
13645
13646 if (sym == last_sym)
13647 n = last_sym_idx;
13648 else
13649 {
13650 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13651 if (n < 0)
13652 {
13653 _bfd_error_handler
13654 /* xgettext:c-format */
13655 (_("%pB(%pA): error: secondary reloc %zu"
13656 " references a missing symbol"),
13657 abfd, relsec, idx);
13658 bfd_set_error (bfd_error_bad_value);
13659 result = false;
13660 n = 0;
13661 }
13662
13663 last_sym = sym;
13664 last_sym_idx = n;
13665 }
13666
13667 if (sym->the_bfd != NULL
13668 && sym->the_bfd->xvec != abfd->xvec
13669 && ! _bfd_elf_validate_reloc (abfd, ptr))
13670 {
13671 _bfd_error_handler
13672 /* xgettext:c-format */
13673 (_("%pB(%pA): error: secondary reloc %zu"
13674 " references a deleted symbol"),
13675 abfd, relsec, idx);
13676 bfd_set_error (bfd_error_bad_value);
13677 result = false;
13678 n = 0;
13679 }
13680 }
13681
13682 src_rela.r_offset = ptr->address + addr_offset;
13683 if (ptr->howto == NULL)
13684 {
13685 _bfd_error_handler
13686 /* xgettext:c-format */
13687 (_("%pB(%pA): error: secondary reloc %zu"
13688 " is of an unknown type"),
13689 abfd, relsec, idx);
13690 bfd_set_error (bfd_error_bad_value);
13691 result = false;
13692 src_rela.r_info = r_info (0, 0);
13693 }
13694 else
13695 src_rela.r_info = r_info (n, ptr->howto->type);
13696 src_rela.r_addend = ptr->addend;
13697
13698 if (entsize == ebd->s->sizeof_rel)
13699 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13700 else /* entsize == ebd->s->sizeof_rela */
13701 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13702 }
13703 }
13704 }
13705
13706 return result;
13707 }