]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf.c
d7109f14039509d4e87e596877aae93063b20f6c
[thirdparty/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2
3 Copyright (C) 1993-2023 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22
23 /*
24 SECTION
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
34
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include <limits.h>
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
47
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
51
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
58
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
62
63 /* Swap in a Verdef structure. */
64
65 void
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
69 {
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
77 }
78
79 /* Swap out a Verdef structure. */
80
81 void
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
85 {
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93 }
94
95 /* Swap in a Verdaux structure. */
96
97 void
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
101 {
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
104 }
105
106 /* Swap out a Verdaux structure. */
107
108 void
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
112 {
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115 }
116
117 /* Swap in a Verneed structure. */
118
119 void
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
123 {
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
129 }
130
131 /* Swap out a Verneed structure. */
132
133 void
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
137 {
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143 }
144
145 /* Swap in a Vernaux structure. */
146
147 void
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
151 {
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
157 }
158
159 /* Swap out a Vernaux structure. */
160
161 void
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
165 {
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171 }
172
173 /* Swap in a Versym structure. */
174
175 void
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
179 {
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181 }
182
183 /* Swap out a Versym structure. */
184
185 void
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
189 {
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191 }
192
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
195
196 unsigned long
197 bfd_elf_hash (const char *namearg)
198 {
199 uint32_t h = 0;
200
201 for (const unsigned char *name = (const unsigned char *) namearg;
202 *name; name++)
203 {
204 h = (h << 4) + *name;
205 h ^= (h >> 24) & 0xf0;
206 }
207 return h & 0x0fffffff;
208 }
209
210 /* DT_GNU_HASH hash function. Do not change this function; you will
211 cause invalid hash tables to be generated. */
212
213 unsigned long
214 bfd_elf_gnu_hash (const char *namearg)
215 {
216 uint32_t h = 5381;
217
218 for (const unsigned char *name = (const unsigned char *) namearg;
219 *name; name++)
220 h = (h << 5) + h + *name;
221 return h;
222 }
223
224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
225 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
226 bool
227 bfd_elf_allocate_object (bfd *abfd,
228 size_t object_size,
229 enum elf_target_id object_id)
230 {
231 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
232 abfd->tdata.any = bfd_zalloc (abfd, object_size);
233 if (abfd->tdata.any == NULL)
234 return false;
235
236 elf_object_id (abfd) = object_id;
237 if (abfd->direction != read_direction)
238 {
239 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
240 if (o == NULL)
241 return false;
242 elf_tdata (abfd)->o = o;
243 elf_program_header_size (abfd) = (bfd_size_type) -1;
244 }
245 return true;
246 }
247
248
249 bool
250 bfd_elf_make_object (bfd *abfd)
251 {
252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
253 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
254 bed->target_id);
255 }
256
257 bool
258 bfd_elf_mkcorefile (bfd *abfd)
259 {
260 /* I think this can be done just like an object file. */
261 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
262 return false;
263 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
264 return elf_tdata (abfd)->core != NULL;
265 }
266
267 char *
268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
269 {
270 Elf_Internal_Shdr **i_shdrp;
271 bfd_byte *shstrtab = NULL;
272 file_ptr offset;
273 bfd_size_type shstrtabsize;
274
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0
277 || shindex >= elf_numsections (abfd)
278 || i_shdrp[shindex] == 0)
279 return NULL;
280
281 shstrtab = i_shdrp[shindex]->contents;
282 if (shstrtab == NULL)
283 {
284 /* No cached one, attempt to read, and cache what we read. */
285 offset = i_shdrp[shindex]->sh_offset;
286 shstrtabsize = i_shdrp[shindex]->sh_size;
287
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize + 1 <= 1
291 || bfd_seek (abfd, offset, SEEK_SET) != 0
292 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
293 shstrtabsize)) == NULL)
294 {
295 /* Once we've failed to read it, make sure we don't keep
296 trying. Otherwise, we'll keep allocating space for
297 the string table over and over. */
298 i_shdrp[shindex]->sh_size = 0;
299 }
300 else
301 shstrtab[shstrtabsize] = '\0';
302 i_shdrp[shindex]->contents = shstrtab;
303 }
304 return (char *) shstrtab;
305 }
306
307 char *
308 bfd_elf_string_from_elf_section (bfd *abfd,
309 unsigned int shindex,
310 unsigned int strindex)
311 {
312 Elf_Internal_Shdr *hdr;
313
314 if (strindex == 0)
315 return "";
316
317 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
318 return NULL;
319
320 hdr = elf_elfsections (abfd)[shindex];
321
322 if (hdr->contents == NULL)
323 {
324 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
325 {
326 /* PR 17512: file: f057ec89. */
327 /* xgettext:c-format */
328 _bfd_error_handler (_("%pB: attempt to load strings from"
329 " a non-string section (number %d)"),
330 abfd, shindex);
331 return NULL;
332 }
333
334 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
335 return NULL;
336 }
337 else
338 {
339 /* PR 24273: The string section's contents may have already
340 been loaded elsewhere, eg because a corrupt file has the
341 string section index in the ELF header pointing at a group
342 section. So be paranoid, and test that the last byte of
343 the section is zero. */
344 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
345 return NULL;
346 }
347
348 if (strindex >= hdr->sh_size)
349 {
350 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
351 _bfd_error_handler
352 /* xgettext:c-format */
353 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
354 abfd, strindex, (uint64_t) hdr->sh_size,
355 (shindex == shstrndx && strindex == hdr->sh_name
356 ? ".shstrtab"
357 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358 return NULL;
359 }
360
361 return ((char *) hdr->contents) + strindex;
362 }
363
364 /* Read and convert symbols to internal format.
365 SYMCOUNT specifies the number of symbols to read, starting from
366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367 are non-NULL, they are used to store the internal symbols, external
368 symbols, and symbol section index extensions, respectively.
369 Returns a pointer to the internal symbol buffer (malloced if necessary)
370 or NULL if there were no symbols or some kind of problem. */
371
372 Elf_Internal_Sym *
373 bfd_elf_get_elf_syms (bfd *ibfd,
374 Elf_Internal_Shdr *symtab_hdr,
375 size_t symcount,
376 size_t symoffset,
377 Elf_Internal_Sym *intsym_buf,
378 void *extsym_buf,
379 Elf_External_Sym_Shndx *extshndx_buf)
380 {
381 Elf_Internal_Shdr *shndx_hdr;
382 void *alloc_ext;
383 const bfd_byte *esym;
384 Elf_External_Sym_Shndx *alloc_extshndx;
385 Elf_External_Sym_Shndx *shndx;
386 Elf_Internal_Sym *alloc_intsym;
387 Elf_Internal_Sym *isym;
388 Elf_Internal_Sym *isymend;
389 const struct elf_backend_data *bed;
390 size_t extsym_size;
391 size_t amt;
392 file_ptr pos;
393
394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395 abort ();
396
397 if (symcount == 0)
398 return intsym_buf;
399
400 if (elf_use_dt_symtab_p (ibfd))
401 {
402 /* Use dynamic symbol table. */
403 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
404 {
405 bfd_set_error (bfd_error_invalid_operation);
406 return NULL;
407 }
408 return elf_tdata (ibfd)->dt_symtab + symoffset;
409 }
410
411 /* Normal syms might have section extension entries. */
412 shndx_hdr = NULL;
413 if (elf_symtab_shndx_list (ibfd) != NULL)
414 {
415 elf_section_list * entry;
416 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417
418 /* Find an index section that is linked to this symtab section. */
419 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420 {
421 /* PR 20063. */
422 if (entry->hdr.sh_link >= elf_numsections (ibfd))
423 continue;
424
425 if (sections[entry->hdr.sh_link] == symtab_hdr)
426 {
427 shndx_hdr = & entry->hdr;
428 break;
429 };
430 }
431
432 if (shndx_hdr == NULL)
433 {
434 if (symtab_hdr == &elf_symtab_hdr (ibfd))
435 /* Not really accurate, but this was how the old code used
436 to work. */
437 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
438 /* Otherwise we do nothing. The assumption is that
439 the index table will not be needed. */
440 }
441 }
442
443 /* Read the symbols. */
444 alloc_ext = NULL;
445 alloc_extshndx = NULL;
446 alloc_intsym = NULL;
447 bed = get_elf_backend_data (ibfd);
448 extsym_size = bed->s->sizeof_sym;
449 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
450 {
451 bfd_set_error (bfd_error_file_too_big);
452 intsym_buf = NULL;
453 goto out;
454 }
455 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456 if (extsym_buf == NULL)
457 {
458 alloc_ext = bfd_malloc (amt);
459 extsym_buf = alloc_ext;
460 }
461 if (extsym_buf == NULL
462 || bfd_seek (ibfd, pos, SEEK_SET) != 0
463 || bfd_read (extsym_buf, amt, ibfd) != amt)
464 {
465 intsym_buf = NULL;
466 goto out;
467 }
468
469 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
470 extshndx_buf = NULL;
471 else
472 {
473 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
474 {
475 bfd_set_error (bfd_error_file_too_big);
476 intsym_buf = NULL;
477 goto out;
478 }
479 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
480 if (extshndx_buf == NULL)
481 {
482 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
483 extshndx_buf = alloc_extshndx;
484 }
485 if (extshndx_buf == NULL
486 || bfd_seek (ibfd, pos, SEEK_SET) != 0
487 || bfd_read (extshndx_buf, amt, ibfd) != amt)
488 {
489 intsym_buf = NULL;
490 goto out;
491 }
492 }
493
494 if (intsym_buf == NULL)
495 {
496 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
497 {
498 bfd_set_error (bfd_error_file_too_big);
499 goto out;
500 }
501 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
502 intsym_buf = alloc_intsym;
503 if (intsym_buf == NULL)
504 goto out;
505 }
506
507 /* Convert the symbols to internal form. */
508 isymend = intsym_buf + symcount;
509 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
510 shndx = extshndx_buf;
511 isym < isymend;
512 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
513 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
514 {
515 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
516 /* xgettext:c-format */
517 _bfd_error_handler (_("%pB symbol number %lu references"
518 " nonexistent SHT_SYMTAB_SHNDX section"),
519 ibfd, (unsigned long) symoffset);
520 free (alloc_intsym);
521 intsym_buf = NULL;
522 goto out;
523 }
524
525 out:
526 free (alloc_ext);
527 free (alloc_extshndx);
528
529 return intsym_buf;
530 }
531
532 /* Look up a symbol name. */
533 const char *
534 bfd_elf_sym_name (bfd *abfd,
535 Elf_Internal_Shdr *symtab_hdr,
536 Elf_Internal_Sym *isym,
537 asection *sym_sec)
538 {
539 const char *name;
540 unsigned int iname = isym->st_name;
541 unsigned int shindex = symtab_hdr->sh_link;
542
543 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
544 /* Check for a bogus st_shndx to avoid crashing. */
545 && isym->st_shndx < elf_numsections (abfd))
546 {
547 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
548 shindex = elf_elfheader (abfd)->e_shstrndx;
549 }
550
551 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
552 if (name == NULL)
553 name = "(null)";
554 else if (sym_sec && *name == '\0')
555 name = bfd_section_name (sym_sec);
556
557 return name;
558 }
559
560 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
561 sections. The first element is the flags, the rest are section
562 pointers. */
563
564 typedef union elf_internal_group {
565 Elf_Internal_Shdr *shdr;
566 unsigned int flags;
567 } Elf_Internal_Group;
568
569 /* Return the name of the group signature symbol. Why isn't the
570 signature just a string? */
571
572 static const char *
573 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
574 {
575 Elf_Internal_Shdr *hdr;
576 unsigned char esym[sizeof (Elf64_External_Sym)];
577 Elf_External_Sym_Shndx eshndx;
578 Elf_Internal_Sym isym;
579
580 /* First we need to ensure the symbol table is available. Make sure
581 that it is a symbol table section. */
582 if (ghdr->sh_link >= elf_numsections (abfd))
583 return NULL;
584 hdr = elf_elfsections (abfd) [ghdr->sh_link];
585 if (hdr->sh_type != SHT_SYMTAB
586 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
587 return NULL;
588
589 /* Go read the symbol. */
590 hdr = &elf_tdata (abfd)->symtab_hdr;
591 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
592 &isym, esym, &eshndx) == NULL)
593 return NULL;
594
595 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
596 }
597
598 /* Set next_in_group list pointer, and group name for NEWSECT. */
599
600 static bool
601 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
602 {
603 unsigned int num_group = elf_tdata (abfd)->num_group;
604
605 /* If num_group is zero, read in all SHT_GROUP sections. The count
606 is set to -1 if there are no SHT_GROUP sections. */
607 if (num_group == 0)
608 {
609 unsigned int i, shnum;
610
611 /* First count the number of groups. If we have a SHT_GROUP
612 section with just a flag word (ie. sh_size is 4), ignore it. */
613 shnum = elf_numsections (abfd);
614 num_group = 0;
615
616 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
617 ( (shdr)->sh_type == SHT_GROUP \
618 && (shdr)->sh_size >= minsize \
619 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
620 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
621
622 for (i = 0; i < shnum; i++)
623 {
624 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
625
626 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
627 num_group += 1;
628 }
629
630 if (num_group == 0)
631 {
632 num_group = (unsigned) -1;
633 elf_tdata (abfd)->num_group = num_group;
634 elf_tdata (abfd)->group_sect_ptr = NULL;
635 }
636 else
637 {
638 /* We keep a list of elf section headers for group sections,
639 so we can find them quickly. */
640 size_t amt;
641
642 elf_tdata (abfd)->num_group = num_group;
643 amt = num_group * sizeof (Elf_Internal_Shdr *);
644 elf_tdata (abfd)->group_sect_ptr
645 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
646 if (elf_tdata (abfd)->group_sect_ptr == NULL)
647 return false;
648 num_group = 0;
649
650 for (i = 0; i < shnum; i++)
651 {
652 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
653
654 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
655 {
656 unsigned char *src;
657 Elf_Internal_Group *dest;
658
659 /* Make sure the group section has a BFD section
660 attached to it. */
661 if (!bfd_section_from_shdr (abfd, i))
662 return false;
663
664 /* Add to list of sections. */
665 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
666 num_group += 1;
667
668 /* Read the raw contents. */
669 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
670 shdr->contents = NULL;
671 if (_bfd_mul_overflow (shdr->sh_size,
672 sizeof (*dest) / 4, &amt)
673 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
674 || !(shdr->contents
675 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
676 {
677 _bfd_error_handler
678 /* xgettext:c-format */
679 (_("%pB: invalid size field in group section"
680 " header: %#" PRIx64 ""),
681 abfd, (uint64_t) shdr->sh_size);
682 bfd_set_error (bfd_error_bad_value);
683 -- num_group;
684 continue;
685 }
686
687 /* Translate raw contents, a flag word followed by an
688 array of elf section indices all in target byte order,
689 to the flag word followed by an array of elf section
690 pointers. */
691 src = shdr->contents + shdr->sh_size;
692 dest = (Elf_Internal_Group *) (shdr->contents + amt);
693
694 while (1)
695 {
696 unsigned int idx;
697
698 src -= 4;
699 --dest;
700 idx = H_GET_32 (abfd, src);
701 if (src == shdr->contents)
702 {
703 dest->shdr = NULL;
704 dest->flags = idx;
705 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
706 shdr->bfd_section->flags
707 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
708 break;
709 }
710 if (idx < shnum)
711 {
712 dest->shdr = elf_elfsections (abfd)[idx];
713 /* PR binutils/23199: All sections in a
714 section group should be marked with
715 SHF_GROUP. But some tools generate
716 broken objects without SHF_GROUP. Fix
717 them up here. */
718 dest->shdr->sh_flags |= SHF_GROUP;
719 }
720 if (idx >= shnum
721 || dest->shdr->sh_type == SHT_GROUP)
722 {
723 _bfd_error_handler
724 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
725 abfd, i);
726 dest->shdr = NULL;
727 }
728 }
729 }
730 }
731
732 /* PR 17510: Corrupt binaries might contain invalid groups. */
733 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
734 {
735 elf_tdata (abfd)->num_group = num_group;
736
737 /* If all groups are invalid then fail. */
738 if (num_group == 0)
739 {
740 elf_tdata (abfd)->group_sect_ptr = NULL;
741 elf_tdata (abfd)->num_group = num_group = -1;
742 _bfd_error_handler
743 (_("%pB: no valid group sections found"), abfd);
744 bfd_set_error (bfd_error_bad_value);
745 }
746 }
747 }
748 }
749
750 if (num_group != (unsigned) -1)
751 {
752 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
753 unsigned int j;
754
755 for (j = 0; j < num_group; j++)
756 {
757 /* Begin search from previous found group. */
758 unsigned i = (j + search_offset) % num_group;
759
760 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
761 Elf_Internal_Group *idx;
762 bfd_size_type n_elt;
763
764 if (shdr == NULL)
765 continue;
766
767 idx = (Elf_Internal_Group *) shdr->contents;
768 if (idx == NULL || shdr->sh_size < 4)
769 {
770 /* See PR 21957 for a reproducer. */
771 /* xgettext:c-format */
772 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
773 abfd, shdr->bfd_section);
774 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
775 bfd_set_error (bfd_error_bad_value);
776 return false;
777 }
778 n_elt = shdr->sh_size / 4;
779
780 /* Look through this group's sections to see if current
781 section is a member. */
782 while (--n_elt != 0)
783 if ((++idx)->shdr == hdr)
784 {
785 asection *s = NULL;
786
787 /* We are a member of this group. Go looking through
788 other members to see if any others are linked via
789 next_in_group. */
790 idx = (Elf_Internal_Group *) shdr->contents;
791 n_elt = shdr->sh_size / 4;
792 while (--n_elt != 0)
793 if ((++idx)->shdr != NULL
794 && (s = idx->shdr->bfd_section) != NULL
795 && elf_next_in_group (s) != NULL)
796 break;
797 if (n_elt != 0)
798 {
799 /* Snarf the group name from other member, and
800 insert current section in circular list. */
801 elf_group_name (newsect) = elf_group_name (s);
802 elf_next_in_group (newsect) = elf_next_in_group (s);
803 elf_next_in_group (s) = newsect;
804 }
805 else
806 {
807 const char *gname;
808
809 gname = group_signature (abfd, shdr);
810 if (gname == NULL)
811 return false;
812 elf_group_name (newsect) = gname;
813
814 /* Start a circular list with one element. */
815 elf_next_in_group (newsect) = newsect;
816 }
817
818 /* If the group section has been created, point to the
819 new member. */
820 if (shdr->bfd_section != NULL)
821 elf_next_in_group (shdr->bfd_section) = newsect;
822
823 elf_tdata (abfd)->group_search_offset = i;
824 j = num_group - 1;
825 break;
826 }
827 }
828 }
829
830 if (elf_group_name (newsect) == NULL)
831 {
832 /* xgettext:c-format */
833 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
834 abfd, newsect);
835 /* PR 29532: Return true here, even though the group info has not been
836 read. Separate debug info files can have empty group sections, but
837 we do not want this to prevent them from being loaded as otherwise
838 GDB will not be able to use them. */
839 return true;
840 }
841 return true;
842 }
843
844 bool
845 _bfd_elf_setup_sections (bfd *abfd)
846 {
847 unsigned int i;
848 unsigned int num_group = elf_tdata (abfd)->num_group;
849 bool result = true;
850 asection *s;
851
852 /* Process SHF_LINK_ORDER. */
853 for (s = abfd->sections; s != NULL; s = s->next)
854 {
855 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
856 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
857 {
858 unsigned int elfsec = this_hdr->sh_link;
859 /* An sh_link value of 0 is now allowed. It indicates that linked
860 to section has already been discarded, but that the current
861 section has been retained for some other reason. This linking
862 section is still a candidate for later garbage collection
863 however. */
864 if (elfsec == 0)
865 {
866 elf_linked_to_section (s) = NULL;
867 }
868 else
869 {
870 asection *linksec = NULL;
871
872 if (elfsec < elf_numsections (abfd))
873 {
874 this_hdr = elf_elfsections (abfd)[elfsec];
875 linksec = this_hdr->bfd_section;
876 }
877
878 /* PR 1991, 2008:
879 Some strip/objcopy may leave an incorrect value in
880 sh_link. We don't want to proceed. */
881 if (linksec == NULL)
882 {
883 _bfd_error_handler
884 /* xgettext:c-format */
885 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
886 s->owner, elfsec, s);
887 result = false;
888 }
889
890 elf_linked_to_section (s) = linksec;
891 }
892 }
893 else if (this_hdr->sh_type == SHT_GROUP
894 && elf_next_in_group (s) == NULL)
895 {
896 _bfd_error_handler
897 /* xgettext:c-format */
898 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
899 abfd, elf_section_data (s)->this_idx);
900 result = false;
901 }
902 }
903
904 /* Process section groups. */
905 if (num_group == (unsigned) -1)
906 return result;
907
908 for (i = 0; i < num_group; i++)
909 {
910 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
911 Elf_Internal_Group *idx;
912 unsigned int n_elt;
913
914 /* PR binutils/18758: Beware of corrupt binaries with invalid
915 group data. */
916 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
917 {
918 _bfd_error_handler
919 /* xgettext:c-format */
920 (_("%pB: section group entry number %u is corrupt"),
921 abfd, i);
922 result = false;
923 continue;
924 }
925
926 idx = (Elf_Internal_Group *) shdr->contents;
927 n_elt = shdr->sh_size / 4;
928
929 while (--n_elt != 0)
930 {
931 ++ idx;
932
933 if (idx->shdr == NULL)
934 continue;
935 else if (idx->shdr->bfd_section)
936 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
937 else if (idx->shdr->sh_type != SHT_RELA
938 && idx->shdr->sh_type != SHT_REL)
939 {
940 /* There are some unknown sections in the group. */
941 _bfd_error_handler
942 /* xgettext:c-format */
943 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
944 abfd,
945 idx->shdr->sh_type,
946 bfd_elf_string_from_elf_section (abfd,
947 (elf_elfheader (abfd)
948 ->e_shstrndx),
949 idx->shdr->sh_name),
950 shdr->bfd_section);
951 result = false;
952 }
953 }
954 }
955
956 return result;
957 }
958
959 bool
960 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
961 {
962 return elf_next_in_group (sec) != NULL;
963 }
964
965 const char *
966 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
967 {
968 if (elf_sec_group (sec) != NULL)
969 return elf_group_name (sec);
970 return NULL;
971 }
972
973 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
974
975 struct lto_section
976 {
977 int16_t major_version;
978 int16_t minor_version;
979 unsigned char slim_object;
980
981 /* Flags is a private field that is not defined publicly. */
982 uint16_t flags;
983 };
984
985 /* Make a BFD section from an ELF section. We store a pointer to the
986 BFD section in the bfd_section field of the header. */
987
988 bool
989 _bfd_elf_make_section_from_shdr (bfd *abfd,
990 Elf_Internal_Shdr *hdr,
991 const char *name,
992 int shindex)
993 {
994 asection *newsect;
995 flagword flags;
996 const struct elf_backend_data *bed;
997 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
998
999 if (hdr->bfd_section != NULL)
1000 return true;
1001
1002 newsect = bfd_make_section_anyway (abfd, name);
1003 if (newsect == NULL)
1004 return false;
1005
1006 hdr->bfd_section = newsect;
1007 elf_section_data (newsect)->this_hdr = *hdr;
1008 elf_section_data (newsect)->this_idx = shindex;
1009
1010 /* Always use the real type/flags. */
1011 elf_section_type (newsect) = hdr->sh_type;
1012 elf_section_flags (newsect) = hdr->sh_flags;
1013
1014 newsect->filepos = hdr->sh_offset;
1015
1016 flags = SEC_NO_FLAGS;
1017 if (hdr->sh_type != SHT_NOBITS)
1018 flags |= SEC_HAS_CONTENTS;
1019 if (hdr->sh_type == SHT_GROUP)
1020 flags |= SEC_GROUP;
1021 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1022 {
1023 flags |= SEC_ALLOC;
1024 if (hdr->sh_type != SHT_NOBITS)
1025 flags |= SEC_LOAD;
1026 }
1027 if ((hdr->sh_flags & SHF_WRITE) == 0)
1028 flags |= SEC_READONLY;
1029 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1030 flags |= SEC_CODE;
1031 else if ((flags & SEC_LOAD) != 0)
1032 flags |= SEC_DATA;
1033 if ((hdr->sh_flags & SHF_MERGE) != 0)
1034 {
1035 flags |= SEC_MERGE;
1036 newsect->entsize = hdr->sh_entsize;
1037 }
1038 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1039 flags |= SEC_STRINGS;
1040 if (hdr->sh_flags & SHF_GROUP)
1041 if (!setup_group (abfd, hdr, newsect))
1042 return false;
1043 if ((hdr->sh_flags & SHF_TLS) != 0)
1044 flags |= SEC_THREAD_LOCAL;
1045 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1046 flags |= SEC_EXCLUDE;
1047
1048 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1049 {
1050 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1051 but binutils as of 2019-07-23 did not set the EI_OSABI header
1052 byte. */
1053 case ELFOSABI_GNU:
1054 case ELFOSABI_FREEBSD:
1055 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1056 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1057 /* Fall through */
1058 case ELFOSABI_NONE:
1059 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1060 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1061 break;
1062 }
1063
1064 if ((flags & SEC_ALLOC) == 0)
1065 {
1066 /* The debugging sections appear to be recognized only by name,
1067 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1068 if (name [0] == '.')
1069 {
1070 if (startswith (name, ".debug")
1071 || startswith (name, ".gnu.debuglto_.debug_")
1072 || startswith (name, ".gnu.linkonce.wi.")
1073 || startswith (name, ".zdebug"))
1074 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1075 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1076 || startswith (name, ".note.gnu"))
1077 {
1078 flags |= SEC_ELF_OCTETS;
1079 opb = 1;
1080 }
1081 else if (startswith (name, ".line")
1082 || startswith (name, ".stab")
1083 || strcmp (name, ".gdb_index") == 0)
1084 flags |= SEC_DEBUGGING;
1085 }
1086 }
1087
1088 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1089 || !bfd_set_section_size (newsect, hdr->sh_size)
1090 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1091 & -hdr->sh_addralign)))
1092 return false;
1093
1094 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1095 only link a single copy of the section. This is used to support
1096 g++. g++ will emit each template expansion in its own section.
1097 The symbols will be defined as weak, so that multiple definitions
1098 are permitted. The GNU linker extension is to actually discard
1099 all but one of the sections. */
1100 if (startswith (name, ".gnu.linkonce")
1101 && elf_next_in_group (newsect) == NULL)
1102 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1103
1104 if (!bfd_set_section_flags (newsect, flags))
1105 return false;
1106
1107 bed = get_elf_backend_data (abfd);
1108 if (bed->elf_backend_section_flags)
1109 if (!bed->elf_backend_section_flags (hdr))
1110 return false;
1111
1112 /* We do not parse the PT_NOTE segments as we are interested even in the
1113 separate debug info files which may have the segments offsets corrupted.
1114 PT_NOTEs from the core files are currently not parsed using BFD. */
1115 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1116 {
1117 bfd_byte *contents;
1118
1119 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1120 return false;
1121
1122 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1123 hdr->sh_offset, hdr->sh_addralign);
1124 free (contents);
1125 }
1126
1127 if ((newsect->flags & SEC_ALLOC) != 0)
1128 {
1129 Elf_Internal_Phdr *phdr;
1130 unsigned int i, nload;
1131
1132 /* Some ELF linkers produce binaries with all the program header
1133 p_paddr fields zero. If we have such a binary with more than
1134 one PT_LOAD header, then leave the section lma equal to vma
1135 so that we don't create sections with overlapping lma. */
1136 phdr = elf_tdata (abfd)->phdr;
1137 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1138 if (phdr->p_paddr != 0)
1139 break;
1140 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1141 ++nload;
1142 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1143 return true;
1144
1145 phdr = elf_tdata (abfd)->phdr;
1146 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1147 {
1148 if (((phdr->p_type == PT_LOAD
1149 && (hdr->sh_flags & SHF_TLS) == 0)
1150 || phdr->p_type == PT_TLS)
1151 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1152 {
1153 if ((newsect->flags & SEC_LOAD) == 0)
1154 newsect->lma = (phdr->p_paddr
1155 + hdr->sh_addr - phdr->p_vaddr) / opb;
1156 else
1157 /* We used to use the same adjustment for SEC_LOAD
1158 sections, but that doesn't work if the segment
1159 is packed with code from multiple VMAs.
1160 Instead we calculate the section LMA based on
1161 the segment LMA. It is assumed that the
1162 segment will contain sections with contiguous
1163 LMAs, even if the VMAs are not. */
1164 newsect->lma = (phdr->p_paddr
1165 + hdr->sh_offset - phdr->p_offset) / opb;
1166
1167 /* With contiguous segments, we can't tell from file
1168 offsets whether a section with zero size should
1169 be placed at the end of one segment or the
1170 beginning of the next. Decide based on vaddr. */
1171 if (hdr->sh_addr >= phdr->p_vaddr
1172 && (hdr->sh_addr + hdr->sh_size
1173 <= phdr->p_vaddr + phdr->p_memsz))
1174 break;
1175 }
1176 }
1177 }
1178
1179 /* Compress/decompress DWARF debug sections with names: .debug_*,
1180 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1181 if ((newsect->flags & SEC_DEBUGGING) != 0
1182 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1183 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1184 {
1185 enum { nothing, compress, decompress } action = nothing;
1186 int compression_header_size;
1187 bfd_size_type uncompressed_size;
1188 unsigned int uncompressed_align_power;
1189 enum compression_type ch_type = ch_none;
1190 bool compressed
1191 = bfd_is_section_compressed_info (abfd, newsect,
1192 &compression_header_size,
1193 &uncompressed_size,
1194 &uncompressed_align_power,
1195 &ch_type);
1196
1197 /* Should we decompress? */
1198 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1199 action = decompress;
1200
1201 /* Should we compress? Or convert to a different compression? */
1202 else if ((abfd->flags & BFD_COMPRESS) != 0
1203 && newsect->size != 0
1204 && compression_header_size >= 0
1205 && uncompressed_size > 0)
1206 {
1207 if (!compressed)
1208 action = compress;
1209 else
1210 {
1211 enum compression_type new_ch_type = ch_none;
1212 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1213 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1214 ? ch_compress_zstd : ch_compress_zlib);
1215 if (new_ch_type != ch_type)
1216 action = compress;
1217 }
1218 }
1219
1220 if (action == compress)
1221 {
1222 if (!bfd_init_section_compress_status (abfd, newsect))
1223 {
1224 _bfd_error_handler
1225 /* xgettext:c-format */
1226 (_("%pB: unable to compress section %s"), abfd, name);
1227 return false;
1228 }
1229 }
1230 else if (action == decompress)
1231 {
1232 if (!bfd_init_section_decompress_status (abfd, newsect))
1233 {
1234 _bfd_error_handler
1235 /* xgettext:c-format */
1236 (_("%pB: unable to decompress section %s"), abfd, name);
1237 return false;
1238 }
1239 #ifndef HAVE_ZSTD
1240 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1241 {
1242 _bfd_error_handler
1243 /* xgettext:c-format */
1244 (_ ("%pB: section %s is compressed with zstd, but BFD "
1245 "is not built with zstd support"),
1246 abfd, name);
1247 newsect->compress_status = COMPRESS_SECTION_NONE;
1248 return false;
1249 }
1250 #endif
1251 if (abfd->is_linker_input
1252 && name[1] == 'z')
1253 {
1254 /* Rename section from .zdebug_* to .debug_* so that ld
1255 scripts will see this section as a debug section. */
1256 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1257 if (new_name == NULL)
1258 return false;
1259 bfd_rename_section (newsect, new_name);
1260 }
1261 }
1262 }
1263
1264 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1265 section. */
1266 if (startswith (name, ".gnu.lto_.lto."))
1267 {
1268 struct lto_section lsection;
1269 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1270 sizeof (struct lto_section)))
1271 abfd->lto_slim_object = lsection.slim_object;
1272 }
1273
1274 return true;
1275 }
1276
1277 const char *const bfd_elf_section_type_names[] =
1278 {
1279 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1280 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1281 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1282 };
1283
1284 /* ELF relocs are against symbols. If we are producing relocatable
1285 output, and the reloc is against an external symbol, and nothing
1286 has given us any additional addend, the resulting reloc will also
1287 be against the same symbol. In such a case, we don't want to
1288 change anything about the way the reloc is handled, since it will
1289 all be done at final link time. Rather than put special case code
1290 into bfd_perform_relocation, all the reloc types use this howto
1291 function, or should call this function for relocatable output. */
1292
1293 bfd_reloc_status_type
1294 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1295 arelent *reloc_entry,
1296 asymbol *symbol,
1297 void *data ATTRIBUTE_UNUSED,
1298 asection *input_section,
1299 bfd *output_bfd,
1300 char **error_message ATTRIBUTE_UNUSED)
1301 {
1302 if (output_bfd != NULL
1303 && (symbol->flags & BSF_SECTION_SYM) == 0
1304 && (! reloc_entry->howto->partial_inplace
1305 || reloc_entry->addend == 0))
1306 {
1307 reloc_entry->address += input_section->output_offset;
1308 return bfd_reloc_ok;
1309 }
1310
1311 /* In some cases the relocation should be treated as output section
1312 relative, as when linking ELF DWARF into PE COFF. Many ELF
1313 targets lack section relative relocations and instead use
1314 ordinary absolute relocations for references between DWARF
1315 sections. That is arguably a bug in those targets but it happens
1316 to work for the usual case of linking to non-loaded ELF debug
1317 sections with VMAs forced to zero. PE COFF on the other hand
1318 doesn't allow a section VMA of zero. */
1319 if (output_bfd == NULL
1320 && !reloc_entry->howto->pc_relative
1321 && (symbol->section->flags & SEC_DEBUGGING) != 0
1322 && (input_section->flags & SEC_DEBUGGING) != 0)
1323 reloc_entry->addend -= symbol->section->output_section->vma;
1324
1325 return bfd_reloc_continue;
1326 }
1327 \f
1328 /* Returns TRUE if section A matches section B.
1329 Names, addresses and links may be different, but everything else
1330 should be the same. */
1331
1332 static bool
1333 section_match (const Elf_Internal_Shdr * a,
1334 const Elf_Internal_Shdr * b)
1335 {
1336 if (a->sh_type != b->sh_type
1337 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1338 || a->sh_addralign != b->sh_addralign
1339 || a->sh_entsize != b->sh_entsize)
1340 return false;
1341 if (a->sh_type == SHT_SYMTAB
1342 || a->sh_type == SHT_STRTAB)
1343 return true;
1344 return a->sh_size == b->sh_size;
1345 }
1346
1347 /* Find a section in OBFD that has the same characteristics
1348 as IHEADER. Return the index of this section or SHN_UNDEF if
1349 none can be found. Check's section HINT first, as this is likely
1350 to be the correct section. */
1351
1352 static unsigned int
1353 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1354 const unsigned int hint)
1355 {
1356 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1357 unsigned int i;
1358
1359 BFD_ASSERT (iheader != NULL);
1360
1361 /* See PR 20922 for a reproducer of the NULL test. */
1362 if (hint < elf_numsections (obfd)
1363 && oheaders[hint] != NULL
1364 && section_match (oheaders[hint], iheader))
1365 return hint;
1366
1367 for (i = 1; i < elf_numsections (obfd); i++)
1368 {
1369 Elf_Internal_Shdr * oheader = oheaders[i];
1370
1371 if (oheader == NULL)
1372 continue;
1373 if (section_match (oheader, iheader))
1374 /* FIXME: Do we care if there is a potential for
1375 multiple matches ? */
1376 return i;
1377 }
1378
1379 return SHN_UNDEF;
1380 }
1381
1382 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1383 Processor specific section, based upon a matching input section.
1384 Returns TRUE upon success, FALSE otherwise. */
1385
1386 static bool
1387 copy_special_section_fields (const bfd *ibfd,
1388 bfd *obfd,
1389 const Elf_Internal_Shdr *iheader,
1390 Elf_Internal_Shdr *oheader,
1391 const unsigned int secnum)
1392 {
1393 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1394 const Elf_Internal_Shdr **iheaders
1395 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1396 bool changed = false;
1397 unsigned int sh_link;
1398
1399 if (oheader->sh_type == SHT_NOBITS)
1400 {
1401 /* This is a feature for objcopy --only-keep-debug:
1402 When a section's type is changed to NOBITS, we preserve
1403 the sh_link and sh_info fields so that they can be
1404 matched up with the original.
1405
1406 Note: Strictly speaking these assignments are wrong.
1407 The sh_link and sh_info fields should point to the
1408 relevent sections in the output BFD, which may not be in
1409 the same location as they were in the input BFD. But
1410 the whole point of this action is to preserve the
1411 original values of the sh_link and sh_info fields, so
1412 that they can be matched up with the section headers in
1413 the original file. So strictly speaking we may be
1414 creating an invalid ELF file, but it is only for a file
1415 that just contains debug info and only for sections
1416 without any contents. */
1417 if (oheader->sh_link == 0)
1418 oheader->sh_link = iheader->sh_link;
1419 if (oheader->sh_info == 0)
1420 oheader->sh_info = iheader->sh_info;
1421 return true;
1422 }
1423
1424 /* Allow the target a chance to decide how these fields should be set. */
1425 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1426 iheader, oheader))
1427 return true;
1428
1429 /* We have an iheader which might match oheader, and which has non-zero
1430 sh_info and/or sh_link fields. Attempt to follow those links and find
1431 the section in the output bfd which corresponds to the linked section
1432 in the input bfd. */
1433 if (iheader->sh_link != SHN_UNDEF)
1434 {
1435 /* See PR 20931 for a reproducer. */
1436 if (iheader->sh_link >= elf_numsections (ibfd))
1437 {
1438 _bfd_error_handler
1439 /* xgettext:c-format */
1440 (_("%pB: invalid sh_link field (%d) in section number %d"),
1441 ibfd, iheader->sh_link, secnum);
1442 return false;
1443 }
1444
1445 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1446 if (sh_link != SHN_UNDEF)
1447 {
1448 oheader->sh_link = sh_link;
1449 changed = true;
1450 }
1451 else
1452 /* FIXME: Should we install iheader->sh_link
1453 if we could not find a match ? */
1454 _bfd_error_handler
1455 /* xgettext:c-format */
1456 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1457 }
1458
1459 if (iheader->sh_info)
1460 {
1461 /* The sh_info field can hold arbitrary information, but if the
1462 SHF_LINK_INFO flag is set then it should be interpreted as a
1463 section index. */
1464 if (iheader->sh_flags & SHF_INFO_LINK)
1465 {
1466 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1467 iheader->sh_info);
1468 if (sh_link != SHN_UNDEF)
1469 oheader->sh_flags |= SHF_INFO_LINK;
1470 }
1471 else
1472 /* No idea what it means - just copy it. */
1473 sh_link = iheader->sh_info;
1474
1475 if (sh_link != SHN_UNDEF)
1476 {
1477 oheader->sh_info = sh_link;
1478 changed = true;
1479 }
1480 else
1481 _bfd_error_handler
1482 /* xgettext:c-format */
1483 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1484 }
1485
1486 return changed;
1487 }
1488
1489 /* Copy the program header and other data from one object module to
1490 another. */
1491
1492 bool
1493 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1494 {
1495 const Elf_Internal_Shdr **iheaders
1496 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1497 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1498 const struct elf_backend_data *bed;
1499 unsigned int i;
1500
1501 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1502 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1503 return true;
1504
1505 if (!elf_flags_init (obfd))
1506 {
1507 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1508 elf_flags_init (obfd) = true;
1509 }
1510
1511 elf_gp (obfd) = elf_gp (ibfd);
1512
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1515 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1516
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1519 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1520 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1521
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1524
1525 if (iheaders == NULL || oheaders == NULL)
1526 return true;
1527
1528 bed = get_elf_backend_data (obfd);
1529
1530 /* Possibly copy other fields in the section header. */
1531 for (i = 1; i < elf_numsections (obfd); i++)
1532 {
1533 unsigned int j;
1534 Elf_Internal_Shdr * oheader = oheaders[i];
1535
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
1539 if (oheader == NULL
1540 || (oheader->sh_type != SHT_NOBITS
1541 && oheader->sh_type < SHT_LOOS))
1542 continue;
1543
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader->sh_size == 0
1547 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1548 continue;
1549
1550 /* Scan for the matching section in the input bfd.
1551 First we try for a direct mapping between the input and
1552 output sections. */
1553 for (j = 1; j < elf_numsections (ibfd); j++)
1554 {
1555 const Elf_Internal_Shdr * iheader = iheaders[j];
1556
1557 if (iheader == NULL)
1558 continue;
1559
1560 if (oheader->bfd_section != NULL
1561 && iheader->bfd_section != NULL
1562 && iheader->bfd_section->output_section != NULL
1563 && iheader->bfd_section->output_section == oheader->bfd_section)
1564 {
1565 /* We have found a connection from the input section to
1566 the output section. Attempt to copy the header fields.
1567 If this fails then do not try any further sections -
1568 there should only be a one-to-one mapping between
1569 input and output. */
1570 if (!copy_special_section_fields (ibfd, obfd,
1571 iheader, oheader, i))
1572 j = elf_numsections (ibfd);
1573 break;
1574 }
1575 }
1576
1577 if (j < elf_numsections (ibfd))
1578 continue;
1579
1580 /* That failed. So try to deduce the corresponding input section.
1581 Unfortunately we cannot compare names as the output string table
1582 is empty, so instead we check size, address and type. */
1583 for (j = 1; j < elf_numsections (ibfd); j++)
1584 {
1585 const Elf_Internal_Shdr * iheader = iheaders[j];
1586
1587 if (iheader == NULL)
1588 continue;
1589
1590 /* Try matching fields in the input section's header.
1591 Since --only-keep-debug turns all non-debug sections into
1592 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1593 input type. */
1594 if ((oheader->sh_type == SHT_NOBITS
1595 || iheader->sh_type == oheader->sh_type)
1596 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1597 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1598 && iheader->sh_addralign == oheader->sh_addralign
1599 && iheader->sh_entsize == oheader->sh_entsize
1600 && iheader->sh_size == oheader->sh_size
1601 && iheader->sh_addr == oheader->sh_addr
1602 && (iheader->sh_info != oheader->sh_info
1603 || iheader->sh_link != oheader->sh_link))
1604 {
1605 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1606 break;
1607 }
1608 }
1609
1610 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1611 {
1612 /* Final attempt. Call the backend copy function
1613 with a NULL input section. */
1614 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1615 NULL, oheader);
1616 }
1617 }
1618
1619 return true;
1620 }
1621
1622 static const char *
1623 get_segment_type (unsigned int p_type)
1624 {
1625 const char *pt;
1626 switch (p_type)
1627 {
1628 case PT_NULL: pt = "NULL"; break;
1629 case PT_LOAD: pt = "LOAD"; break;
1630 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1631 case PT_INTERP: pt = "INTERP"; break;
1632 case PT_NOTE: pt = "NOTE"; break;
1633 case PT_SHLIB: pt = "SHLIB"; break;
1634 case PT_PHDR: pt = "PHDR"; break;
1635 case PT_TLS: pt = "TLS"; break;
1636 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1637 case PT_GNU_STACK: pt = "STACK"; break;
1638 case PT_GNU_RELRO: pt = "RELRO"; break;
1639 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1640 default: pt = NULL; break;
1641 }
1642 return pt;
1643 }
1644
1645 /* Print out the program headers. */
1646
1647 bool
1648 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1649 {
1650 FILE *f = (FILE *) farg;
1651 Elf_Internal_Phdr *p;
1652 asection *s;
1653 bfd_byte *dynbuf = NULL;
1654
1655 p = elf_tdata (abfd)->phdr;
1656 if (p != NULL)
1657 {
1658 unsigned int i, c;
1659
1660 fprintf (f, _("\nProgram Header:\n"));
1661 c = elf_elfheader (abfd)->e_phnum;
1662 for (i = 0; i < c; i++, p++)
1663 {
1664 const char *pt = get_segment_type (p->p_type);
1665 char buf[20];
1666
1667 if (pt == NULL)
1668 {
1669 sprintf (buf, "0x%lx", p->p_type);
1670 pt = buf;
1671 }
1672 fprintf (f, "%8s off 0x", pt);
1673 bfd_fprintf_vma (abfd, f, p->p_offset);
1674 fprintf (f, " vaddr 0x");
1675 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1676 fprintf (f, " paddr 0x");
1677 bfd_fprintf_vma (abfd, f, p->p_paddr);
1678 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1679 fprintf (f, " filesz 0x");
1680 bfd_fprintf_vma (abfd, f, p->p_filesz);
1681 fprintf (f, " memsz 0x");
1682 bfd_fprintf_vma (abfd, f, p->p_memsz);
1683 fprintf (f, " flags %c%c%c",
1684 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1685 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1686 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1687 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1688 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1689 fprintf (f, "\n");
1690 }
1691 }
1692
1693 s = bfd_get_section_by_name (abfd, ".dynamic");
1694 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1695 {
1696 unsigned int elfsec;
1697 unsigned long shlink;
1698 bfd_byte *extdyn, *extdynend;
1699 size_t extdynsize;
1700 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1701
1702 fprintf (f, _("\nDynamic Section:\n"));
1703
1704 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1705 goto error_return;
1706
1707 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1708 if (elfsec == SHN_BAD)
1709 goto error_return;
1710 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1711
1712 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1713 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1714
1715 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1716 (size_t) (extdynend - extdyn) >= extdynsize;
1717 extdyn += extdynsize)
1718 {
1719 Elf_Internal_Dyn dyn;
1720 const char *name = "";
1721 char ab[20];
1722 bool stringp;
1723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1724
1725 (*swap_dyn_in) (abfd, extdyn, &dyn);
1726
1727 if (dyn.d_tag == DT_NULL)
1728 break;
1729
1730 stringp = false;
1731 switch (dyn.d_tag)
1732 {
1733 default:
1734 if (bed->elf_backend_get_target_dtag)
1735 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1736
1737 if (!strcmp (name, ""))
1738 {
1739 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1740 name = ab;
1741 }
1742 break;
1743
1744 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1745 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1746 case DT_PLTGOT: name = "PLTGOT"; break;
1747 case DT_HASH: name = "HASH"; break;
1748 case DT_STRTAB: name = "STRTAB"; break;
1749 case DT_SYMTAB: name = "SYMTAB"; break;
1750 case DT_RELA: name = "RELA"; break;
1751 case DT_RELASZ: name = "RELASZ"; break;
1752 case DT_RELAENT: name = "RELAENT"; break;
1753 case DT_STRSZ: name = "STRSZ"; break;
1754 case DT_SYMENT: name = "SYMENT"; break;
1755 case DT_INIT: name = "INIT"; break;
1756 case DT_FINI: name = "FINI"; break;
1757 case DT_SONAME: name = "SONAME"; stringp = true; break;
1758 case DT_RPATH: name = "RPATH"; stringp = true; break;
1759 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1760 case DT_REL: name = "REL"; break;
1761 case DT_RELSZ: name = "RELSZ"; break;
1762 case DT_RELENT: name = "RELENT"; break;
1763 case DT_RELR: name = "RELR"; break;
1764 case DT_RELRSZ: name = "RELRSZ"; break;
1765 case DT_RELRENT: name = "RELRENT"; break;
1766 case DT_PLTREL: name = "PLTREL"; break;
1767 case DT_DEBUG: name = "DEBUG"; break;
1768 case DT_TEXTREL: name = "TEXTREL"; break;
1769 case DT_JMPREL: name = "JMPREL"; break;
1770 case DT_BIND_NOW: name = "BIND_NOW"; break;
1771 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1772 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1773 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1774 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1775 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1776 case DT_FLAGS: name = "FLAGS"; break;
1777 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1778 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1779 case DT_CHECKSUM: name = "CHECKSUM"; break;
1780 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1781 case DT_MOVEENT: name = "MOVEENT"; break;
1782 case DT_MOVESZ: name = "MOVESZ"; break;
1783 case DT_FEATURE: name = "FEATURE"; break;
1784 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1785 case DT_SYMINSZ: name = "SYMINSZ"; break;
1786 case DT_SYMINENT: name = "SYMINENT"; break;
1787 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1788 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1789 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1790 case DT_PLTPAD: name = "PLTPAD"; break;
1791 case DT_MOVETAB: name = "MOVETAB"; break;
1792 case DT_SYMINFO: name = "SYMINFO"; break;
1793 case DT_RELACOUNT: name = "RELACOUNT"; break;
1794 case DT_RELCOUNT: name = "RELCOUNT"; break;
1795 case DT_FLAGS_1: name = "FLAGS_1"; break;
1796 case DT_VERSYM: name = "VERSYM"; break;
1797 case DT_VERDEF: name = "VERDEF"; break;
1798 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1799 case DT_VERNEED: name = "VERNEED"; break;
1800 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1801 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1802 case DT_USED: name = "USED"; break;
1803 case DT_FILTER: name = "FILTER"; stringp = true; break;
1804 case DT_GNU_HASH: name = "GNU_HASH"; break;
1805 }
1806
1807 fprintf (f, " %-20s ", name);
1808 if (! stringp)
1809 {
1810 fprintf (f, "0x");
1811 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1812 }
1813 else
1814 {
1815 const char *string;
1816 unsigned int tagv = dyn.d_un.d_val;
1817
1818 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1819 if (string == NULL)
1820 goto error_return;
1821 fprintf (f, "%s", string);
1822 }
1823 fprintf (f, "\n");
1824 }
1825
1826 free (dynbuf);
1827 dynbuf = NULL;
1828 }
1829
1830 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1831 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1832 {
1833 if (! _bfd_elf_slurp_version_tables (abfd, false))
1834 return false;
1835 }
1836
1837 if (elf_dynverdef (abfd) != 0)
1838 {
1839 Elf_Internal_Verdef *t;
1840
1841 fprintf (f, _("\nVersion definitions:\n"));
1842 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1843 {
1844 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1845 t->vd_flags, t->vd_hash,
1846 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1847 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1848 {
1849 Elf_Internal_Verdaux *a;
1850
1851 fprintf (f, "\t");
1852 for (a = t->vd_auxptr->vda_nextptr;
1853 a != NULL;
1854 a = a->vda_nextptr)
1855 fprintf (f, "%s ",
1856 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1857 fprintf (f, "\n");
1858 }
1859 }
1860 }
1861
1862 if (elf_dynverref (abfd) != 0)
1863 {
1864 Elf_Internal_Verneed *t;
1865
1866 fprintf (f, _("\nVersion References:\n"));
1867 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1868 {
1869 Elf_Internal_Vernaux *a;
1870
1871 fprintf (f, _(" required from %s:\n"),
1872 t->vn_filename ? t->vn_filename : "<corrupt>");
1873 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1874 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1875 a->vna_flags, a->vna_other,
1876 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1877 }
1878 }
1879
1880 return true;
1881
1882 error_return:
1883 free (dynbuf);
1884 return false;
1885 }
1886
1887 /* Find the file offset corresponding to VMA by using the program
1888 headers. */
1889
1890 static file_ptr
1891 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1892 size_t size, size_t *max_size_p)
1893 {
1894 Elf_Internal_Phdr *seg;
1895 size_t i;
1896
1897 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1898 if (seg->p_type == PT_LOAD
1899 && vma >= (seg->p_vaddr & -seg->p_align)
1900 && vma + size <= seg->p_vaddr + seg->p_filesz)
1901 {
1902 if (max_size_p)
1903 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1904 return vma - seg->p_vaddr + seg->p_offset;
1905 }
1906
1907 if (max_size_p)
1908 *max_size_p = 0;
1909 bfd_set_error (bfd_error_invalid_operation);
1910 return (file_ptr) -1;
1911 }
1912
1913 /* Convert hash table to internal form. */
1914
1915 static bfd_vma *
1916 get_hash_table_data (bfd *abfd, bfd_size_type number,
1917 unsigned int ent_size, bfd_size_type filesize)
1918 {
1919 unsigned char *e_data = NULL;
1920 bfd_vma *i_data = NULL;
1921 bfd_size_type size;
1922
1923 if (ent_size != 4 && ent_size != 8)
1924 return NULL;
1925
1926 if ((size_t) number != number)
1927 {
1928 bfd_set_error (bfd_error_file_too_big);
1929 return NULL;
1930 }
1931
1932 size = ent_size * number;
1933 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1934 attempting to allocate memory when the read is bound to fail. */
1935 if (size > filesize
1936 || number >= ~(size_t) 0 / ent_size
1937 || number >= ~(size_t) 0 / sizeof (*i_data))
1938 {
1939 bfd_set_error (bfd_error_file_too_big);
1940 return NULL;
1941 }
1942
1943 e_data = _bfd_malloc_and_read (abfd, size, size);
1944 if (e_data == NULL)
1945 return NULL;
1946
1947 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1948 if (i_data == NULL)
1949 {
1950 free (e_data);
1951 return NULL;
1952 }
1953
1954 if (ent_size == 4)
1955 while (number--)
1956 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1957 else
1958 while (number--)
1959 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1960
1961 free (e_data);
1962 return i_data;
1963 }
1964
1965 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1966 support DT_MIPS_XHASH? */
1967 #define DT_MIPS_XHASH 0x70000036
1968
1969 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1970
1971 bool
1972 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1973 Elf_Internal_Phdr *phdrs, size_t phnum,
1974 bfd_size_type filesize)
1975 {
1976 bfd_byte *extdyn, *extdynend;
1977 size_t extdynsize;
1978 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1979 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1980 Elf_Internal_Sym *);
1981 Elf_Internal_Dyn dyn;
1982 bfd_vma dt_hash = 0;
1983 bfd_vma dt_gnu_hash = 0;
1984 bfd_vma dt_mips_xhash = 0;
1985 bfd_vma dt_strtab = 0;
1986 bfd_vma dt_symtab = 0;
1987 size_t dt_strsz = 0;
1988 bfd_vma dt_versym = 0;
1989 bfd_vma dt_verdef = 0;
1990 bfd_vma dt_verneed = 0;
1991 bfd_byte *dynbuf = NULL;
1992 char *strbuf = NULL;
1993 bfd_vma *gnubuckets = NULL;
1994 bfd_vma *gnuchains = NULL;
1995 bfd_vma *mipsxlat = NULL;
1996 file_ptr saved_filepos, filepos;
1997 bool res = false;
1998 size_t amt;
1999 bfd_byte *esymbuf = NULL, *esym;
2000 bfd_size_type symcount;
2001 Elf_Internal_Sym *isymbuf = NULL;
2002 Elf_Internal_Sym *isym, *isymend;
2003 bfd_byte *versym = NULL;
2004 bfd_byte *verdef = NULL;
2005 bfd_byte *verneed = NULL;
2006 size_t verdef_size = 0;
2007 size_t verneed_size = 0;
2008 size_t extsym_size;
2009 const struct elf_backend_data *bed;
2010
2011 /* Return TRUE if symbol table is bad. */
2012 if (elf_bad_symtab (abfd))
2013 return true;
2014
2015 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
2016 if (elf_tdata (abfd)->dt_strtab != NULL)
2017 return true;
2018
2019 bed = get_elf_backend_data (abfd);
2020
2021 /* Save file position for elf_object_p. */
2022 saved_filepos = bfd_tell (abfd);
2023
2024 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
2025 goto error_return;
2026
2027 dynbuf = _bfd_malloc_and_read (abfd, phdr->p_filesz, phdr->p_filesz);
2028 if (dynbuf == NULL)
2029 goto error_return;
2030
2031 extsym_size = bed->s->sizeof_sym;
2032 extdynsize = bed->s->sizeof_dyn;
2033 swap_dyn_in = bed->s->swap_dyn_in;
2034
2035 extdyn = dynbuf;
2036 if (phdr->p_filesz < extdynsize)
2037 goto error_return;
2038 extdynend = extdyn + phdr->p_filesz;
2039 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
2040 {
2041 swap_dyn_in (abfd, extdyn, &dyn);
2042
2043 if (dyn.d_tag == DT_NULL)
2044 break;
2045
2046 switch (dyn.d_tag)
2047 {
2048 case DT_HASH:
2049 dt_hash = dyn.d_un.d_val;
2050 break;
2051 case DT_GNU_HASH:
2052 if (bed->elf_machine_code != EM_MIPS
2053 && bed->elf_machine_code != EM_MIPS_RS3_LE)
2054 dt_gnu_hash = dyn.d_un.d_val;
2055 break;
2056 case DT_STRTAB:
2057 dt_strtab = dyn.d_un.d_val;
2058 break;
2059 case DT_SYMTAB:
2060 dt_symtab = dyn.d_un.d_val;
2061 break;
2062 case DT_STRSZ:
2063 dt_strsz = dyn.d_un.d_val;
2064 break;
2065 case DT_SYMENT:
2066 if (dyn.d_un.d_val != extsym_size)
2067 goto error_return;
2068 break;
2069 case DT_VERSYM:
2070 dt_versym = dyn.d_un.d_val;
2071 break;
2072 case DT_VERDEF:
2073 dt_verdef = dyn.d_un.d_val;
2074 break;
2075 case DT_VERNEED:
2076 dt_verneed = dyn.d_un.d_val;
2077 break;
2078 default:
2079 if (dyn.d_tag == DT_MIPS_XHASH
2080 && (bed->elf_machine_code == EM_MIPS
2081 || bed->elf_machine_code == EM_MIPS_RS3_LE))
2082 {
2083 dt_gnu_hash = dyn.d_un.d_val;
2084 dt_mips_xhash = dyn.d_un.d_val;
2085 }
2086 break;
2087 }
2088 }
2089
2090 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2091 segment. */
2092 if ((!dt_hash && !dt_gnu_hash)
2093 || !dt_strtab
2094 || !dt_symtab
2095 || !dt_strsz)
2096 goto error_return;
2097
2098 /* Get dynamic string table. */
2099 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
2100 if (filepos == (file_ptr) -1
2101 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2102 goto error_return;
2103
2104 /* Dynamic string table must be valid until ABFD is closed. */
2105 strbuf = (char *) _bfd_alloc_and_read (abfd, dt_strsz, dt_strsz);
2106 if (strbuf == NULL)
2107 goto error_return;
2108
2109 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2110 DT_HASH since it is simpler than DT_GNU_HASH. */
2111 if (dt_hash)
2112 {
2113 unsigned char nb[16];
2114 unsigned int hash_ent_size;
2115
2116 switch (bed->elf_machine_code)
2117 {
2118 case EM_ALPHA:
2119 case EM_S390:
2120 case EM_S390_OLD:
2121 if (bed->s->elfclass == ELFCLASS64)
2122 {
2123 hash_ent_size = 8;
2124 break;
2125 }
2126 /* FALLTHROUGH */
2127 default:
2128 hash_ent_size = 4;
2129 break;
2130 }
2131
2132 filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
2133 NULL);
2134 if (filepos == (file_ptr) -1
2135 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2136 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2137 goto error_return;
2138
2139 /* The number of dynamic symbol table entries equals the number
2140 of chains. */
2141 if (hash_ent_size == 8)
2142 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2143 else
2144 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2145 }
2146 else
2147 {
2148 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2149 bindings are in hash table. Since in dynamic symbol table,
2150 all symbols with STB_LOCAL binding are placed before symbols
2151 with other bindings and all undefined symbols are placed
2152 before defined ones, the highest symbol index in DT_GNU_HASH
2153 is the highest dynamic symbol table index. */
2154 unsigned char nb[16];
2155 bfd_vma ngnubuckets;
2156 bfd_vma gnusymidx;
2157 size_t i, ngnuchains;
2158 bfd_vma maxchain = 0xffffffff, bitmaskwords;
2159 bfd_vma buckets_vma;
2160
2161 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2162 sizeof (nb), NULL);
2163 if (filepos == (file_ptr) -1
2164 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2165 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2166 goto error_return;
2167
2168 ngnubuckets = bfd_get_32 (abfd, nb);
2169 gnusymidx = bfd_get_32 (abfd, nb + 4);
2170 bitmaskwords = bfd_get_32 (abfd, nb + 8);
2171 buckets_vma = dt_gnu_hash + 16;
2172 if (bed->s->elfclass == ELFCLASS32)
2173 buckets_vma += bitmaskwords * 4;
2174 else
2175 buckets_vma += bitmaskwords * 8;
2176 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2177 if (filepos == (file_ptr) -1
2178 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2179 goto error_return;
2180
2181 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2182 if (gnubuckets == NULL)
2183 goto error_return;
2184
2185 for (i = 0; i < ngnubuckets; i++)
2186 if (gnubuckets[i] != 0)
2187 {
2188 if (gnubuckets[i] < gnusymidx)
2189 goto error_return;
2190
2191 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2192 maxchain = gnubuckets[i];
2193 }
2194
2195 if (maxchain == 0xffffffff)
2196 {
2197 symcount = 0;
2198 goto empty_gnu_hash;
2199 }
2200
2201 maxchain -= gnusymidx;
2202 filepos = offset_from_vma (phdrs, phnum,
2203 (buckets_vma +
2204 4 * (ngnubuckets + maxchain)),
2205 4, NULL);
2206 if (filepos == (file_ptr) -1
2207 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2208 goto error_return;
2209
2210 do
2211 {
2212 if (bfd_read (nb, 4, abfd) != 4)
2213 goto error_return;
2214 ++maxchain;
2215 if (maxchain == 0)
2216 goto error_return;
2217 }
2218 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2219
2220 filepos = offset_from_vma (phdrs, phnum,
2221 (buckets_vma + 4 * ngnubuckets),
2222 4, NULL);
2223 if (filepos == (file_ptr) -1
2224 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2225 goto error_return;
2226
2227 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2228 if (gnubuckets == NULL)
2229 goto error_return;
2230 ngnuchains = maxchain;
2231
2232 if (dt_mips_xhash)
2233 {
2234 filepos = offset_from_vma (phdrs, phnum,
2235 (buckets_vma
2236 + 4 * (ngnubuckets + maxchain)),
2237 4, NULL);
2238 if (filepos == (file_ptr) -1
2239 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2240 goto error_return;
2241
2242 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2243 if (mipsxlat == NULL)
2244 goto error_return;
2245 }
2246
2247 symcount = 0;
2248 for (i = 0; i < ngnubuckets; ++i)
2249 if (gnubuckets[i] != 0)
2250 {
2251 bfd_vma si = gnubuckets[i];
2252 bfd_vma off = si - gnusymidx;
2253 do
2254 {
2255 if (mipsxlat)
2256 {
2257 if (mipsxlat[off] >= symcount)
2258 symcount = mipsxlat[off] + 1;
2259 }
2260 else
2261 {
2262 if (si >= symcount)
2263 symcount = si + 1;
2264 }
2265 si++;
2266 }
2267 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2268 }
2269 }
2270
2271 /* Swap in dynamic symbol table. */
2272 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2273 {
2274 bfd_set_error (bfd_error_file_too_big);
2275 goto error_return;
2276 }
2277
2278 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2279 if (filepos == (file_ptr) -1
2280 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2281 goto error_return;
2282 esymbuf = _bfd_malloc_and_read (abfd, amt, amt);
2283 if (esymbuf == NULL)
2284 goto error_return;
2285
2286 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2287 {
2288 bfd_set_error (bfd_error_file_too_big);
2289 goto error_return;
2290 }
2291
2292 /* Dynamic symbol table must be valid until ABFD is closed. */
2293 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2294 if (isymbuf == NULL)
2295 goto error_return;
2296
2297 swap_symbol_in = bed->s->swap_symbol_in;
2298
2299 /* Convert the symbols to internal form. */
2300 isymend = isymbuf + symcount;
2301 for (esym = esymbuf, isym = isymbuf;
2302 isym < isymend;
2303 esym += extsym_size, isym++)
2304 if (!swap_symbol_in (abfd, esym, NULL, isym)
2305 || isym->st_name >= dt_strsz)
2306 {
2307 bfd_set_error (bfd_error_invalid_operation);
2308 goto error_return;
2309 }
2310
2311 if (dt_versym)
2312 {
2313 /* Swap in DT_VERSYM. */
2314 if (_bfd_mul_overflow (symcount, 2, &amt))
2315 {
2316 bfd_set_error (bfd_error_file_too_big);
2317 goto error_return;
2318 }
2319
2320 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2321 if (filepos == (file_ptr) -1
2322 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2323 goto error_return;
2324
2325 /* DT_VERSYM info must be valid until ABFD is closed. */
2326 versym = _bfd_alloc_and_read (abfd, amt, amt);
2327
2328 if (dt_verdef)
2329 {
2330 /* Read in DT_VERDEF. */
2331 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2332 0, &verdef_size);
2333 if (filepos == (file_ptr) -1
2334 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2335 goto error_return;
2336
2337 /* DT_VERDEF info must be valid until ABFD is closed. */
2338 verdef = _bfd_alloc_and_read (abfd, verdef_size,
2339 verdef_size);
2340 }
2341
2342 if (dt_verneed)
2343 {
2344 /* Read in DT_VERNEED. */
2345 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2346 0, &verneed_size);
2347 if (filepos == (file_ptr) -1
2348 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2349 goto error_return;
2350
2351 /* DT_VERNEED info must be valid until ABFD is closed. */
2352 verneed = _bfd_alloc_and_read (abfd, verneed_size,
2353 verneed_size);
2354 }
2355 }
2356
2357 empty_gnu_hash:
2358 elf_tdata (abfd)->dt_strtab = strbuf;
2359 elf_tdata (abfd)->dt_symtab = isymbuf;
2360 elf_tdata (abfd)->dt_symtab_count = symcount;
2361 elf_tdata (abfd)->dt_versym = versym;
2362 elf_tdata (abfd)->dt_verdef = verdef;
2363 elf_tdata (abfd)->dt_verneed = verneed;
2364 elf_tdata (abfd)->dt_verdef_count
2365 = verdef_size / sizeof (Elf_External_Verdef);
2366 elf_tdata (abfd)->dt_verneed_count
2367 = verneed_size / sizeof (Elf_External_Verneed);
2368
2369 res = true;
2370
2371 error_return:
2372 /* Restore file position for elf_object_p. */
2373 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2374 res = false;
2375 free (dynbuf);
2376 free (esymbuf);
2377 free (gnubuckets);
2378 free (gnuchains);
2379 free (mipsxlat);
2380 return res;
2381 }
2382
2383 /* Reconstruct section from dynamic symbol. */
2384
2385 asection *
2386 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2387 Elf_Internal_Sym *isym)
2388 {
2389 asection *sec;
2390 flagword flags;
2391
2392 if (!elf_use_dt_symtab_p (abfd))
2393 return NULL;
2394
2395 flags = SEC_ALLOC | SEC_LOAD;
2396 switch (ELF_ST_TYPE (isym->st_info))
2397 {
2398 case STT_FUNC:
2399 case STT_GNU_IFUNC:
2400 sec = bfd_get_section_by_name (abfd, ".text");
2401 if (sec == NULL)
2402 sec = bfd_make_section_with_flags (abfd,
2403 ".text",
2404 flags | SEC_CODE);
2405 break;
2406 case STT_COMMON:
2407 sec = bfd_com_section_ptr;
2408 break;
2409 case STT_OBJECT:
2410 sec = bfd_get_section_by_name (abfd, ".data");
2411 if (sec == NULL)
2412 sec = bfd_make_section_with_flags (abfd,
2413 ".data",
2414 flags | SEC_DATA);
2415 break;
2416 case STT_TLS:
2417 sec = bfd_get_section_by_name (abfd, ".tdata");
2418 if (sec == NULL)
2419 sec = bfd_make_section_with_flags (abfd,
2420 ".tdata",
2421 (flags
2422 | SEC_DATA
2423 | SEC_THREAD_LOCAL));
2424 break;
2425 default:
2426 sec = bfd_abs_section_ptr;
2427 break;
2428 }
2429
2430 return sec;
2431 }
2432
2433 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2434 and return symbol version for symbol version itself. */
2435
2436 const char *
2437 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2438 bool base_p,
2439 bool *hidden)
2440 {
2441 const char *version_string = NULL;
2442 if ((elf_dynversym (abfd) != 0
2443 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2444 || (elf_tdata (abfd)->dt_versym != NULL
2445 && (elf_tdata (abfd)->dt_verdef != NULL
2446 || elf_tdata (abfd)->dt_verneed != NULL)))
2447 {
2448 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2449
2450 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2451 vernum &= VERSYM_VERSION;
2452
2453 if (vernum == 0)
2454 version_string = "";
2455 else if (vernum == 1
2456 && (vernum > elf_tdata (abfd)->cverdefs
2457 || (elf_tdata (abfd)->verdef[0].vd_flags
2458 == VER_FLG_BASE)))
2459 version_string = base_p ? "Base" : "";
2460 else if (vernum <= elf_tdata (abfd)->cverdefs)
2461 {
2462 const char *nodename
2463 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2464 version_string = "";
2465 if (base_p
2466 || nodename == NULL
2467 || symbol->name == NULL
2468 || strcmp (symbol->name, nodename) != 0)
2469 version_string = nodename;
2470 }
2471 else
2472 {
2473 Elf_Internal_Verneed *t;
2474
2475 version_string = _("<corrupt>");
2476 for (t = elf_tdata (abfd)->verref;
2477 t != NULL;
2478 t = t->vn_nextref)
2479 {
2480 Elf_Internal_Vernaux *a;
2481
2482 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2483 {
2484 if (a->vna_other == vernum)
2485 {
2486 *hidden = true;
2487 version_string = a->vna_nodename;
2488 break;
2489 }
2490 }
2491 }
2492 }
2493 }
2494 return version_string;
2495 }
2496
2497 /* Display ELF-specific fields of a symbol. */
2498
2499 void
2500 bfd_elf_print_symbol (bfd *abfd,
2501 void *filep,
2502 asymbol *symbol,
2503 bfd_print_symbol_type how)
2504 {
2505 FILE *file = (FILE *) filep;
2506 switch (how)
2507 {
2508 case bfd_print_symbol_name:
2509 fprintf (file, "%s", symbol->name);
2510 break;
2511 case bfd_print_symbol_more:
2512 fprintf (file, "elf ");
2513 bfd_fprintf_vma (abfd, file, symbol->value);
2514 fprintf (file, " %x", symbol->flags);
2515 break;
2516 case bfd_print_symbol_all:
2517 {
2518 const char *section_name;
2519 const char *name = NULL;
2520 const struct elf_backend_data *bed;
2521 unsigned char st_other;
2522 bfd_vma val;
2523 const char *version_string;
2524 bool hidden;
2525
2526 section_name = symbol->section ? symbol->section->name : "(*none*)";
2527
2528 bed = get_elf_backend_data (abfd);
2529 if (bed->elf_backend_print_symbol_all)
2530 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2531
2532 if (name == NULL)
2533 {
2534 name = symbol->name;
2535 bfd_print_symbol_vandf (abfd, file, symbol);
2536 }
2537
2538 fprintf (file, " %s\t", section_name);
2539 /* Print the "other" value for a symbol. For common symbols,
2540 we've already printed the size; now print the alignment.
2541 For other symbols, we have no specified alignment, and
2542 we've printed the address; now print the size. */
2543 if (symbol->section && bfd_is_com_section (symbol->section))
2544 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2545 else
2546 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2547 bfd_fprintf_vma (abfd, file, val);
2548
2549 /* If we have version information, print it. */
2550 version_string = _bfd_elf_get_symbol_version_string (abfd,
2551 symbol,
2552 true,
2553 &hidden);
2554 if (version_string)
2555 {
2556 if (!hidden)
2557 fprintf (file, " %-11s", version_string);
2558 else
2559 {
2560 int i;
2561
2562 fprintf (file, " (%s)", version_string);
2563 for (i = 10 - strlen (version_string); i > 0; --i)
2564 putc (' ', file);
2565 }
2566 }
2567
2568 /* If the st_other field is not zero, print it. */
2569 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2570
2571 switch (st_other)
2572 {
2573 case 0: break;
2574 case STV_INTERNAL: fprintf (file, " .internal"); break;
2575 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2576 case STV_PROTECTED: fprintf (file, " .protected"); break;
2577 default:
2578 /* Some other non-defined flags are also present, so print
2579 everything hex. */
2580 fprintf (file, " 0x%02x", (unsigned int) st_other);
2581 }
2582
2583 fprintf (file, " %s", name);
2584 }
2585 break;
2586 }
2587 }
2588 \f
2589 /* ELF .o/exec file reading */
2590
2591 /* Create a new bfd section from an ELF section header. */
2592
2593 bool
2594 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2595 {
2596 Elf_Internal_Shdr *hdr;
2597 Elf_Internal_Ehdr *ehdr;
2598 const struct elf_backend_data *bed;
2599 const char *name;
2600 bool ret = true;
2601
2602 if (shindex >= elf_numsections (abfd))
2603 return false;
2604
2605 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2606 sh_link or sh_info. Detect this here, by refusing to load a
2607 section that we are already in the process of loading. */
2608 if (elf_tdata (abfd)->being_created[shindex])
2609 {
2610 _bfd_error_handler
2611 (_("%pB: warning: loop in section dependencies detected"), abfd);
2612 return false;
2613 }
2614 elf_tdata (abfd)->being_created[shindex] = true;
2615
2616 hdr = elf_elfsections (abfd)[shindex];
2617 ehdr = elf_elfheader (abfd);
2618 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2619 hdr->sh_name);
2620 if (name == NULL)
2621 goto fail;
2622
2623 bed = get_elf_backend_data (abfd);
2624 switch (hdr->sh_type)
2625 {
2626 case SHT_NULL:
2627 /* Inactive section. Throw it away. */
2628 goto success;
2629
2630 case SHT_PROGBITS: /* Normal section with contents. */
2631 case SHT_NOBITS: /* .bss section. */
2632 case SHT_HASH: /* .hash section. */
2633 case SHT_NOTE: /* .note section. */
2634 case SHT_INIT_ARRAY: /* .init_array section. */
2635 case SHT_FINI_ARRAY: /* .fini_array section. */
2636 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2637 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2638 case SHT_GNU_HASH: /* .gnu.hash section. */
2639 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2640 goto success;
2641
2642 case SHT_DYNAMIC: /* Dynamic linking information. */
2643 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2644 goto fail;
2645
2646 if (hdr->sh_link > elf_numsections (abfd))
2647 {
2648 /* PR 10478: Accept Solaris binaries with a sh_link field
2649 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2650 switch (bfd_get_arch (abfd))
2651 {
2652 case bfd_arch_i386:
2653 case bfd_arch_sparc:
2654 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2655 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2656 break;
2657 /* Otherwise fall through. */
2658 default:
2659 goto fail;
2660 }
2661 }
2662 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2663 goto fail;
2664 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2665 {
2666 Elf_Internal_Shdr *dynsymhdr;
2667
2668 /* The shared libraries distributed with hpux11 have a bogus
2669 sh_link field for the ".dynamic" section. Find the
2670 string table for the ".dynsym" section instead. */
2671 if (elf_dynsymtab (abfd) != 0)
2672 {
2673 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2674 hdr->sh_link = dynsymhdr->sh_link;
2675 }
2676 else
2677 {
2678 unsigned int i, num_sec;
2679
2680 num_sec = elf_numsections (abfd);
2681 for (i = 1; i < num_sec; i++)
2682 {
2683 dynsymhdr = elf_elfsections (abfd)[i];
2684 if (dynsymhdr->sh_type == SHT_DYNSYM)
2685 {
2686 hdr->sh_link = dynsymhdr->sh_link;
2687 break;
2688 }
2689 }
2690 }
2691 }
2692 goto success;
2693
2694 case SHT_SYMTAB: /* A symbol table. */
2695 if (elf_onesymtab (abfd) == shindex)
2696 goto success;
2697
2698 if (hdr->sh_entsize != bed->s->sizeof_sym)
2699 goto fail;
2700
2701 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2702 {
2703 if (hdr->sh_size != 0)
2704 goto fail;
2705 /* Some assemblers erroneously set sh_info to one with a
2706 zero sh_size. ld sees this as a global symbol count
2707 of (unsigned) -1. Fix it here. */
2708 hdr->sh_info = 0;
2709 goto success;
2710 }
2711
2712 /* PR 18854: A binary might contain more than one symbol table.
2713 Unusual, but possible. Warn, but continue. */
2714 if (elf_onesymtab (abfd) != 0)
2715 {
2716 _bfd_error_handler
2717 /* xgettext:c-format */
2718 (_("%pB: warning: multiple symbol tables detected"
2719 " - ignoring the table in section %u"),
2720 abfd, shindex);
2721 goto success;
2722 }
2723 elf_onesymtab (abfd) = shindex;
2724 elf_symtab_hdr (abfd) = *hdr;
2725 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2726 abfd->flags |= HAS_SYMS;
2727
2728 /* Sometimes a shared object will map in the symbol table. If
2729 SHF_ALLOC is set, and this is a shared object, then we also
2730 treat this section as a BFD section. We can not base the
2731 decision purely on SHF_ALLOC, because that flag is sometimes
2732 set in a relocatable object file, which would confuse the
2733 linker. */
2734 if ((hdr->sh_flags & SHF_ALLOC) != 0
2735 && (abfd->flags & DYNAMIC) != 0
2736 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2737 shindex))
2738 goto fail;
2739
2740 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2741 can't read symbols without that section loaded as well. It
2742 is most likely specified by the next section header. */
2743 {
2744 elf_section_list * entry;
2745 unsigned int i, num_sec;
2746
2747 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2748 if (entry->hdr.sh_link == shindex)
2749 goto success;
2750
2751 num_sec = elf_numsections (abfd);
2752 for (i = shindex + 1; i < num_sec; i++)
2753 {
2754 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2755
2756 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2757 && hdr2->sh_link == shindex)
2758 break;
2759 }
2760
2761 if (i == num_sec)
2762 for (i = 1; i < shindex; i++)
2763 {
2764 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2765
2766 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2767 && hdr2->sh_link == shindex)
2768 break;
2769 }
2770
2771 if (i != shindex)
2772 ret = bfd_section_from_shdr (abfd, i);
2773 /* else FIXME: we have failed to find the symbol table.
2774 Should we issue an error? */
2775 goto success;
2776 }
2777
2778 case SHT_DYNSYM: /* A dynamic symbol table. */
2779 if (elf_dynsymtab (abfd) == shindex)
2780 goto success;
2781
2782 if (hdr->sh_entsize != bed->s->sizeof_sym)
2783 goto fail;
2784
2785 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2786 {
2787 if (hdr->sh_size != 0)
2788 goto fail;
2789
2790 /* Some linkers erroneously set sh_info to one with a
2791 zero sh_size. ld sees this as a global symbol count
2792 of (unsigned) -1. Fix it here. */
2793 hdr->sh_info = 0;
2794 goto success;
2795 }
2796
2797 /* PR 18854: A binary might contain more than one dynamic symbol table.
2798 Unusual, but possible. Warn, but continue. */
2799 if (elf_dynsymtab (abfd) != 0)
2800 {
2801 _bfd_error_handler
2802 /* xgettext:c-format */
2803 (_("%pB: warning: multiple dynamic symbol tables detected"
2804 " - ignoring the table in section %u"),
2805 abfd, shindex);
2806 goto success;
2807 }
2808 elf_dynsymtab (abfd) = shindex;
2809 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2810 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2811 abfd->flags |= HAS_SYMS;
2812
2813 /* Besides being a symbol table, we also treat this as a regular
2814 section, so that objcopy can handle it. */
2815 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2816 goto success;
2817
2818 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2819 {
2820 elf_section_list * entry;
2821
2822 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2823 if (entry->ndx == shindex)
2824 goto success;
2825
2826 entry = bfd_alloc (abfd, sizeof (*entry));
2827 if (entry == NULL)
2828 goto fail;
2829 entry->ndx = shindex;
2830 entry->hdr = * hdr;
2831 entry->next = elf_symtab_shndx_list (abfd);
2832 elf_symtab_shndx_list (abfd) = entry;
2833 elf_elfsections (abfd)[shindex] = & entry->hdr;
2834 goto success;
2835 }
2836
2837 case SHT_STRTAB: /* A string table. */
2838 if (hdr->bfd_section != NULL)
2839 goto success;
2840
2841 if (ehdr->e_shstrndx == shindex)
2842 {
2843 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2844 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2845 goto success;
2846 }
2847
2848 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2849 {
2850 symtab_strtab:
2851 elf_tdata (abfd)->strtab_hdr = *hdr;
2852 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2853 goto success;
2854 }
2855
2856 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2857 {
2858 dynsymtab_strtab:
2859 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2860 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2861 elf_elfsections (abfd)[shindex] = hdr;
2862 /* We also treat this as a regular section, so that objcopy
2863 can handle it. */
2864 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2865 shindex);
2866 goto success;
2867 }
2868
2869 /* If the string table isn't one of the above, then treat it as a
2870 regular section. We need to scan all the headers to be sure,
2871 just in case this strtab section appeared before the above. */
2872 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2873 {
2874 unsigned int i, num_sec;
2875
2876 num_sec = elf_numsections (abfd);
2877 for (i = 1; i < num_sec; i++)
2878 {
2879 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2880 if (hdr2->sh_link == shindex)
2881 {
2882 /* Prevent endless recursion on broken objects. */
2883 if (i == shindex)
2884 goto fail;
2885 if (! bfd_section_from_shdr (abfd, i))
2886 goto fail;
2887 if (elf_onesymtab (abfd) == i)
2888 goto symtab_strtab;
2889 if (elf_dynsymtab (abfd) == i)
2890 goto dynsymtab_strtab;
2891 }
2892 }
2893 }
2894 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2895 goto success;
2896
2897 case SHT_REL:
2898 case SHT_RELA:
2899 case SHT_RELR:
2900 /* *These* do a lot of work -- but build no sections! */
2901 {
2902 asection *target_sect;
2903 Elf_Internal_Shdr *hdr2, **p_hdr;
2904 unsigned int num_sec = elf_numsections (abfd);
2905 struct bfd_elf_section_data *esdt;
2906 bfd_size_type size;
2907
2908 if (hdr->sh_type == SHT_REL)
2909 size = bed->s->sizeof_rel;
2910 else if (hdr->sh_type == SHT_RELA)
2911 size = bed->s->sizeof_rela;
2912 else
2913 size = bed->s->arch_size / 8;
2914 if (hdr->sh_entsize != size)
2915 goto fail;
2916
2917 /* Check for a bogus link to avoid crashing. */
2918 if (hdr->sh_link >= num_sec)
2919 {
2920 _bfd_error_handler
2921 /* xgettext:c-format */
2922 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2923 abfd, hdr->sh_link, name, shindex);
2924 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2925 goto success;
2926 }
2927
2928 /* Get the symbol table. */
2929 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2930 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2931 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2932 goto fail;
2933
2934 /* If this is an alloc section in an executable or shared
2935 library, or the reloc section does not use the main symbol
2936 table we don't treat it as a reloc section. BFD can't
2937 adequately represent such a section, so at least for now,
2938 we don't try. We just present it as a normal section. We
2939 also can't use it as a reloc section if it points to the
2940 null section, an invalid section, another reloc section, or
2941 its sh_link points to the null section. */
2942 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2943 && (hdr->sh_flags & SHF_ALLOC) != 0)
2944 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2945 || hdr->sh_type == SHT_RELR
2946 || hdr->sh_link == SHN_UNDEF
2947 || hdr->sh_link != elf_onesymtab (abfd)
2948 || hdr->sh_info == SHN_UNDEF
2949 || hdr->sh_info >= num_sec
2950 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2951 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2952 {
2953 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2954 goto success;
2955 }
2956
2957 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2958 goto fail;
2959
2960 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2961 if (target_sect == NULL)
2962 goto fail;
2963
2964 esdt = elf_section_data (target_sect);
2965 if (hdr->sh_type == SHT_RELA)
2966 p_hdr = &esdt->rela.hdr;
2967 else
2968 p_hdr = &esdt->rel.hdr;
2969
2970 /* PR 17512: file: 0b4f81b7.
2971 Also see PR 24456, for a file which deliberately has two reloc
2972 sections. */
2973 if (*p_hdr != NULL)
2974 {
2975 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2976 {
2977 _bfd_error_handler
2978 /* xgettext:c-format */
2979 (_("%pB: warning: secondary relocation section '%s' "
2980 "for section %pA found - ignoring"),
2981 abfd, name, target_sect);
2982 }
2983 else
2984 esdt->has_secondary_relocs = true;
2985 goto success;
2986 }
2987
2988 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2989 if (hdr2 == NULL)
2990 goto fail;
2991 *hdr2 = *hdr;
2992 *p_hdr = hdr2;
2993 elf_elfsections (abfd)[shindex] = hdr2;
2994 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2995 * bed->s->int_rels_per_ext_rel);
2996 target_sect->flags |= SEC_RELOC;
2997 target_sect->relocation = NULL;
2998 target_sect->rel_filepos = hdr->sh_offset;
2999 /* In the section to which the relocations apply, mark whether
3000 its relocations are of the REL or RELA variety. */
3001 if (hdr->sh_size != 0)
3002 {
3003 if (hdr->sh_type == SHT_RELA)
3004 target_sect->use_rela_p = 1;
3005 }
3006 abfd->flags |= HAS_RELOC;
3007 goto success;
3008 }
3009
3010 case SHT_GNU_verdef:
3011 if (hdr->sh_info != 0)
3012 elf_dynverdef (abfd) = shindex;
3013 elf_tdata (abfd)->dynverdef_hdr = *hdr;
3014 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3015 goto success;
3016
3017 case SHT_GNU_versym:
3018 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
3019 goto fail;
3020
3021 elf_dynversym (abfd) = shindex;
3022 elf_tdata (abfd)->dynversym_hdr = *hdr;
3023 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3024 goto success;
3025
3026 case SHT_GNU_verneed:
3027 if (hdr->sh_info != 0)
3028 elf_dynverref (abfd) = shindex;
3029 elf_tdata (abfd)->dynverref_hdr = *hdr;
3030 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3031 goto success;
3032
3033 case SHT_SHLIB:
3034 goto success;
3035
3036 case SHT_GROUP:
3037 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
3038 goto fail;
3039
3040 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3041 goto fail;
3042
3043 goto success;
3044
3045 default:
3046 /* Possibly an attributes section. */
3047 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
3048 || hdr->sh_type == bed->obj_attrs_section_type)
3049 {
3050 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3051 goto fail;
3052 _bfd_elf_parse_attributes (abfd, hdr);
3053 goto success;
3054 }
3055
3056 /* Check for any processor-specific section types. */
3057 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
3058 goto success;
3059
3060 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
3061 {
3062 if ((hdr->sh_flags & SHF_ALLOC) != 0)
3063 /* FIXME: How to properly handle allocated section reserved
3064 for applications? */
3065 _bfd_error_handler
3066 /* xgettext:c-format */
3067 (_("%pB: unknown type [%#x] section `%s'"),
3068 abfd, hdr->sh_type, name);
3069 else
3070 {
3071 /* Allow sections reserved for applications. */
3072 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3073 goto success;
3074 }
3075 }
3076 else if (hdr->sh_type >= SHT_LOPROC
3077 && hdr->sh_type <= SHT_HIPROC)
3078 /* FIXME: We should handle this section. */
3079 _bfd_error_handler
3080 /* xgettext:c-format */
3081 (_("%pB: unknown type [%#x] section `%s'"),
3082 abfd, hdr->sh_type, name);
3083 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
3084 {
3085 /* Unrecognised OS-specific sections. */
3086 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3087 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3088 required to correctly process the section and the file should
3089 be rejected with an error message. */
3090 _bfd_error_handler
3091 /* xgettext:c-format */
3092 (_("%pB: unknown type [%#x] section `%s'"),
3093 abfd, hdr->sh_type, name);
3094 else
3095 {
3096 /* Otherwise it should be processed. */
3097 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3098 goto success;
3099 }
3100 }
3101 else
3102 /* FIXME: We should handle this section. */
3103 _bfd_error_handler
3104 /* xgettext:c-format */
3105 (_("%pB: unknown type [%#x] section `%s'"),
3106 abfd, hdr->sh_type, name);
3107
3108 goto fail;
3109 }
3110
3111 fail:
3112 ret = false;
3113 success:
3114 elf_tdata (abfd)->being_created[shindex] = false;
3115 return ret;
3116 }
3117
3118 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3119
3120 Elf_Internal_Sym *
3121 bfd_sym_from_r_symndx (struct sym_cache *cache,
3122 bfd *abfd,
3123 unsigned long r_symndx)
3124 {
3125 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3126
3127 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3128 {
3129 Elf_Internal_Shdr *symtab_hdr;
3130 unsigned char esym[sizeof (Elf64_External_Sym)];
3131 Elf_External_Sym_Shndx eshndx;
3132
3133 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3134 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3135 &cache->sym[ent], esym, &eshndx) == NULL)
3136 return NULL;
3137
3138 if (cache->abfd != abfd)
3139 {
3140 memset (cache->indx, -1, sizeof (cache->indx));
3141 cache->abfd = abfd;
3142 }
3143 cache->indx[ent] = r_symndx;
3144 }
3145
3146 return &cache->sym[ent];
3147 }
3148
3149 /* Given an ELF section number, retrieve the corresponding BFD
3150 section. */
3151
3152 asection *
3153 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3154 {
3155 if (sec_index >= elf_numsections (abfd))
3156 return NULL;
3157 return elf_elfsections (abfd)[sec_index]->bfd_section;
3158 }
3159
3160 static const struct bfd_elf_special_section special_sections_b[] =
3161 {
3162 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3163 { NULL, 0, 0, 0, 0 }
3164 };
3165
3166 static const struct bfd_elf_special_section special_sections_c[] =
3167 {
3168 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3169 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
3170 { NULL, 0, 0, 0, 0 }
3171 };
3172
3173 static const struct bfd_elf_special_section special_sections_d[] =
3174 {
3175 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3176 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3177 /* There are more DWARF sections than these, but they needn't be added here
3178 unless you have to cope with broken compilers that don't emit section
3179 attributes or you want to help the user writing assembler. */
3180 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
3181 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
3182 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
3183 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
3184 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3185 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3186 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3187 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3188 { NULL, 0, 0, 0, 0 }
3189 };
3190
3191 static const struct bfd_elf_special_section special_sections_f[] =
3192 {
3193 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3194 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3195 { NULL, 0 , 0, 0, 0 }
3196 };
3197
3198 static const struct bfd_elf_special_section special_sections_g[] =
3199 {
3200 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3201 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3202 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3203 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3204 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3205 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3206 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3207 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3208 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3209 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3210 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3211 { NULL, 0, 0, 0, 0 }
3212 };
3213
3214 static const struct bfd_elf_special_section special_sections_h[] =
3215 {
3216 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3217 { NULL, 0, 0, 0, 0 }
3218 };
3219
3220 static const struct bfd_elf_special_section special_sections_i[] =
3221 {
3222 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3223 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3224 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3225 { NULL, 0, 0, 0, 0 }
3226 };
3227
3228 static const struct bfd_elf_special_section special_sections_l[] =
3229 {
3230 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3231 { NULL, 0, 0, 0, 0 }
3232 };
3233
3234 static const struct bfd_elf_special_section special_sections_n[] =
3235 {
3236 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3237 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3238 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3239 { NULL, 0, 0, 0, 0 }
3240 };
3241
3242 static const struct bfd_elf_special_section special_sections_p[] =
3243 {
3244 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3245 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3246 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3247 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3248 { NULL, 0, 0, 0, 0 }
3249 };
3250
3251 static const struct bfd_elf_special_section special_sections_r[] =
3252 {
3253 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3254 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3255 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3256 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3257 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3258 { NULL, 0, 0, 0, 0 }
3259 };
3260
3261 static const struct bfd_elf_special_section special_sections_s[] =
3262 {
3263 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3264 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3265 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3266 /* See struct bfd_elf_special_section declaration for the semantics of
3267 this special case where .prefix_length != strlen (.prefix). */
3268 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3269 { NULL, 0, 0, 0, 0 }
3270 };
3271
3272 static const struct bfd_elf_special_section special_sections_t[] =
3273 {
3274 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3275 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3276 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3277 { NULL, 0, 0, 0, 0 }
3278 };
3279
3280 static const struct bfd_elf_special_section special_sections_z[] =
3281 {
3282 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3283 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3284 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3285 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3286 { NULL, 0, 0, 0, 0 }
3287 };
3288
3289 static const struct bfd_elf_special_section * const special_sections[] =
3290 {
3291 special_sections_b, /* 'b' */
3292 special_sections_c, /* 'c' */
3293 special_sections_d, /* 'd' */
3294 NULL, /* 'e' */
3295 special_sections_f, /* 'f' */
3296 special_sections_g, /* 'g' */
3297 special_sections_h, /* 'h' */
3298 special_sections_i, /* 'i' */
3299 NULL, /* 'j' */
3300 NULL, /* 'k' */
3301 special_sections_l, /* 'l' */
3302 NULL, /* 'm' */
3303 special_sections_n, /* 'n' */
3304 NULL, /* 'o' */
3305 special_sections_p, /* 'p' */
3306 NULL, /* 'q' */
3307 special_sections_r, /* 'r' */
3308 special_sections_s, /* 's' */
3309 special_sections_t, /* 't' */
3310 NULL, /* 'u' */
3311 NULL, /* 'v' */
3312 NULL, /* 'w' */
3313 NULL, /* 'x' */
3314 NULL, /* 'y' */
3315 special_sections_z /* 'z' */
3316 };
3317
3318 const struct bfd_elf_special_section *
3319 _bfd_elf_get_special_section (const char *name,
3320 const struct bfd_elf_special_section *spec,
3321 unsigned int rela)
3322 {
3323 int i;
3324 int len;
3325
3326 len = strlen (name);
3327
3328 for (i = 0; spec[i].prefix != NULL; i++)
3329 {
3330 int suffix_len;
3331 int prefix_len = spec[i].prefix_length;
3332
3333 if (len < prefix_len)
3334 continue;
3335 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3336 continue;
3337
3338 suffix_len = spec[i].suffix_length;
3339 if (suffix_len <= 0)
3340 {
3341 if (name[prefix_len] != 0)
3342 {
3343 if (suffix_len == 0)
3344 continue;
3345 if (name[prefix_len] != '.'
3346 && (suffix_len == -2
3347 || (rela && spec[i].type == SHT_REL)))
3348 continue;
3349 }
3350 }
3351 else
3352 {
3353 if (len < prefix_len + suffix_len)
3354 continue;
3355 if (memcmp (name + len - suffix_len,
3356 spec[i].prefix + prefix_len,
3357 suffix_len) != 0)
3358 continue;
3359 }
3360 return &spec[i];
3361 }
3362
3363 return NULL;
3364 }
3365
3366 const struct bfd_elf_special_section *
3367 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3368 {
3369 int i;
3370 const struct bfd_elf_special_section *spec;
3371 const struct elf_backend_data *bed;
3372
3373 /* See if this is one of the special sections. */
3374 if (sec->name == NULL)
3375 return NULL;
3376
3377 bed = get_elf_backend_data (abfd);
3378 spec = bed->special_sections;
3379 if (spec)
3380 {
3381 spec = _bfd_elf_get_special_section (sec->name,
3382 bed->special_sections,
3383 sec->use_rela_p);
3384 if (spec != NULL)
3385 return spec;
3386 }
3387
3388 if (sec->name[0] != '.')
3389 return NULL;
3390
3391 i = sec->name[1] - 'b';
3392 if (i < 0 || i > 'z' - 'b')
3393 return NULL;
3394
3395 spec = special_sections[i];
3396
3397 if (spec == NULL)
3398 return NULL;
3399
3400 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3401 }
3402
3403 bool
3404 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3405 {
3406 struct bfd_elf_section_data *sdata;
3407 const struct elf_backend_data *bed;
3408 const struct bfd_elf_special_section *ssect;
3409
3410 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3411 if (sdata == NULL)
3412 {
3413 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3414 sizeof (*sdata));
3415 if (sdata == NULL)
3416 return false;
3417 sec->used_by_bfd = sdata;
3418 }
3419
3420 /* Indicate whether or not this section should use RELA relocations. */
3421 bed = get_elf_backend_data (abfd);
3422 sec->use_rela_p = bed->default_use_rela_p;
3423
3424 /* Set up ELF section type and flags for newly created sections, if
3425 there is an ABI mandated section. */
3426 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3427 if (ssect != NULL)
3428 {
3429 elf_section_type (sec) = ssect->type;
3430 elf_section_flags (sec) = ssect->attr;
3431 }
3432
3433 return _bfd_generic_new_section_hook (abfd, sec);
3434 }
3435
3436 /* Create a new bfd section from an ELF program header.
3437
3438 Since program segments have no names, we generate a synthetic name
3439 of the form segment<NUM>, where NUM is generally the index in the
3440 program header table. For segments that are split (see below) we
3441 generate the names segment<NUM>a and segment<NUM>b.
3442
3443 Note that some program segments may have a file size that is different than
3444 (less than) the memory size. All this means is that at execution the
3445 system must allocate the amount of memory specified by the memory size,
3446 but only initialize it with the first "file size" bytes read from the
3447 file. This would occur for example, with program segments consisting
3448 of combined data+bss.
3449
3450 To handle the above situation, this routine generates TWO bfd sections
3451 for the single program segment. The first has the length specified by
3452 the file size of the segment, and the second has the length specified
3453 by the difference between the two sizes. In effect, the segment is split
3454 into its initialized and uninitialized parts. */
3455
3456 bool
3457 _bfd_elf_make_section_from_phdr (bfd *abfd,
3458 Elf_Internal_Phdr *hdr,
3459 int hdr_index,
3460 const char *type_name)
3461 {
3462 asection *newsect;
3463 char *name;
3464 char namebuf[64];
3465 size_t len;
3466 int split;
3467 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3468
3469 split = ((hdr->p_memsz > 0)
3470 && (hdr->p_filesz > 0)
3471 && (hdr->p_memsz > hdr->p_filesz));
3472
3473 if (hdr->p_filesz > 0)
3474 {
3475 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3476 len = strlen (namebuf) + 1;
3477 name = (char *) bfd_alloc (abfd, len);
3478 if (!name)
3479 return false;
3480 memcpy (name, namebuf, len);
3481 newsect = bfd_make_section (abfd, name);
3482 if (newsect == NULL)
3483 return false;
3484 newsect->vma = hdr->p_vaddr / opb;
3485 newsect->lma = hdr->p_paddr / opb;
3486 newsect->size = hdr->p_filesz;
3487 newsect->filepos = hdr->p_offset;
3488 newsect->flags |= SEC_HAS_CONTENTS;
3489 newsect->alignment_power = bfd_log2 (hdr->p_align);
3490 if (hdr->p_type == PT_LOAD)
3491 {
3492 newsect->flags |= SEC_ALLOC;
3493 newsect->flags |= SEC_LOAD;
3494 if (hdr->p_flags & PF_X)
3495 {
3496 /* FIXME: all we known is that it has execute PERMISSION,
3497 may be data. */
3498 newsect->flags |= SEC_CODE;
3499 }
3500 }
3501 if (!(hdr->p_flags & PF_W))
3502 {
3503 newsect->flags |= SEC_READONLY;
3504 }
3505 }
3506
3507 if (hdr->p_memsz > hdr->p_filesz)
3508 {
3509 bfd_vma align;
3510
3511 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3512 len = strlen (namebuf) + 1;
3513 name = (char *) bfd_alloc (abfd, len);
3514 if (!name)
3515 return false;
3516 memcpy (name, namebuf, len);
3517 newsect = bfd_make_section (abfd, name);
3518 if (newsect == NULL)
3519 return false;
3520 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3521 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3522 newsect->size = hdr->p_memsz - hdr->p_filesz;
3523 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3524 align = newsect->vma & -newsect->vma;
3525 if (align == 0 || align > hdr->p_align)
3526 align = hdr->p_align;
3527 newsect->alignment_power = bfd_log2 (align);
3528 if (hdr->p_type == PT_LOAD)
3529 {
3530 newsect->flags |= SEC_ALLOC;
3531 if (hdr->p_flags & PF_X)
3532 newsect->flags |= SEC_CODE;
3533 }
3534 if (!(hdr->p_flags & PF_W))
3535 newsect->flags |= SEC_READONLY;
3536 }
3537
3538 return true;
3539 }
3540
3541 static bool
3542 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3543 {
3544 /* The return value is ignored. Build-ids are considered optional. */
3545 if (templ->xvec->flavour == bfd_target_elf_flavour)
3546 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3547 (templ, offset);
3548 return false;
3549 }
3550
3551 bool
3552 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3553 {
3554 const struct elf_backend_data *bed;
3555
3556 switch (hdr->p_type)
3557 {
3558 case PT_NULL:
3559 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3560
3561 case PT_LOAD:
3562 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3563 return false;
3564 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3565 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3566 return true;
3567
3568 case PT_DYNAMIC:
3569 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3570
3571 case PT_INTERP:
3572 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3573
3574 case PT_NOTE:
3575 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3576 return false;
3577 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3578 hdr->p_align))
3579 return false;
3580 return true;
3581
3582 case PT_SHLIB:
3583 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3584
3585 case PT_PHDR:
3586 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3587
3588 case PT_GNU_EH_FRAME:
3589 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3590 "eh_frame_hdr");
3591
3592 case PT_GNU_STACK:
3593 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3594
3595 case PT_GNU_RELRO:
3596 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3597
3598 case PT_GNU_SFRAME:
3599 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3600 "sframe");
3601
3602 default:
3603 /* Check for any processor-specific program segment types. */
3604 bed = get_elf_backend_data (abfd);
3605 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3606 }
3607 }
3608
3609 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3610 REL or RELA. */
3611
3612 Elf_Internal_Shdr *
3613 _bfd_elf_single_rel_hdr (asection *sec)
3614 {
3615 if (elf_section_data (sec)->rel.hdr)
3616 {
3617 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3618 return elf_section_data (sec)->rel.hdr;
3619 }
3620 else
3621 return elf_section_data (sec)->rela.hdr;
3622 }
3623
3624 static bool
3625 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3626 Elf_Internal_Shdr *rel_hdr,
3627 const char *sec_name,
3628 bool use_rela_p)
3629 {
3630 char *name = (char *) bfd_alloc (abfd,
3631 sizeof ".rela" + strlen (sec_name));
3632 if (name == NULL)
3633 return false;
3634
3635 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3636 rel_hdr->sh_name =
3637 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3638 false);
3639 if (rel_hdr->sh_name == (unsigned int) -1)
3640 return false;
3641
3642 return true;
3643 }
3644
3645 /* Allocate and initialize a section-header for a new reloc section,
3646 containing relocations against ASECT. It is stored in RELDATA. If
3647 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3648 relocations. */
3649
3650 static bool
3651 _bfd_elf_init_reloc_shdr (bfd *abfd,
3652 struct bfd_elf_section_reloc_data *reldata,
3653 const char *sec_name,
3654 bool use_rela_p,
3655 bool delay_st_name_p)
3656 {
3657 Elf_Internal_Shdr *rel_hdr;
3658 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3659
3660 BFD_ASSERT (reldata->hdr == NULL);
3661 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3662 reldata->hdr = rel_hdr;
3663
3664 if (delay_st_name_p)
3665 rel_hdr->sh_name = (unsigned int) -1;
3666 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3667 use_rela_p))
3668 return false;
3669 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3670 rel_hdr->sh_entsize = (use_rela_p
3671 ? bed->s->sizeof_rela
3672 : bed->s->sizeof_rel);
3673 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3674 rel_hdr->sh_flags = 0;
3675 rel_hdr->sh_addr = 0;
3676 rel_hdr->sh_size = 0;
3677 rel_hdr->sh_offset = 0;
3678
3679 return true;
3680 }
3681
3682 /* Return the default section type based on the passed in section flags. */
3683
3684 int
3685 bfd_elf_get_default_section_type (flagword flags)
3686 {
3687 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3688 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3689 return SHT_NOBITS;
3690 return SHT_PROGBITS;
3691 }
3692
3693 struct fake_section_arg
3694 {
3695 struct bfd_link_info *link_info;
3696 bool failed;
3697 };
3698
3699 /* Set up an ELF internal section header for a section. */
3700
3701 static void
3702 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3703 {
3704 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3705 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3706 struct bfd_elf_section_data *esd = elf_section_data (asect);
3707 Elf_Internal_Shdr *this_hdr;
3708 unsigned int sh_type;
3709 const char *name = asect->name;
3710 bool delay_st_name_p = false;
3711 bfd_vma mask;
3712
3713 if (arg->failed)
3714 {
3715 /* We already failed; just get out of the bfd_map_over_sections
3716 loop. */
3717 return;
3718 }
3719
3720 this_hdr = &esd->this_hdr;
3721
3722 /* ld: compress DWARF debug sections with names: .debug_*. */
3723 if (arg->link_info
3724 && (abfd->flags & BFD_COMPRESS) != 0
3725 && (asect->flags & SEC_DEBUGGING) != 0
3726 && name[1] == 'd'
3727 && name[6] == '_')
3728 {
3729 /* If this section will be compressed, delay adding section
3730 name to section name section after it is compressed in
3731 _bfd_elf_assign_file_positions_for_non_load. */
3732 delay_st_name_p = true;
3733 }
3734
3735 if (delay_st_name_p)
3736 this_hdr->sh_name = (unsigned int) -1;
3737 else
3738 {
3739 this_hdr->sh_name
3740 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3741 name, false);
3742 if (this_hdr->sh_name == (unsigned int) -1)
3743 {
3744 arg->failed = true;
3745 return;
3746 }
3747 }
3748
3749 /* Don't clear sh_flags. Assembler may set additional bits. */
3750
3751 if ((asect->flags & SEC_ALLOC) != 0
3752 || asect->user_set_vma)
3753 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3754 else
3755 this_hdr->sh_addr = 0;
3756
3757 this_hdr->sh_offset = 0;
3758 this_hdr->sh_size = asect->size;
3759 this_hdr->sh_link = 0;
3760 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3761 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3762 {
3763 _bfd_error_handler
3764 /* xgettext:c-format */
3765 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3766 abfd, asect->alignment_power, asect);
3767 arg->failed = true;
3768 return;
3769 }
3770 /* Set sh_addralign to the highest power of two given by alignment
3771 consistent with the section VMA. Linker scripts can force VMA. */
3772 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3773 this_hdr->sh_addralign = mask & -mask;
3774 /* The sh_entsize and sh_info fields may have been set already by
3775 copy_private_section_data. */
3776
3777 this_hdr->bfd_section = asect;
3778 this_hdr->contents = NULL;
3779
3780 /* If the section type is unspecified, we set it based on
3781 asect->flags. */
3782 if (asect->type != 0)
3783 sh_type = asect->type;
3784 else if ((asect->flags & SEC_GROUP) != 0)
3785 sh_type = SHT_GROUP;
3786 else
3787 sh_type = bfd_elf_get_default_section_type (asect->flags);
3788
3789 if (this_hdr->sh_type == SHT_NULL)
3790 this_hdr->sh_type = sh_type;
3791 else if (this_hdr->sh_type == SHT_NOBITS
3792 && sh_type == SHT_PROGBITS
3793 && (asect->flags & SEC_ALLOC) != 0)
3794 {
3795 /* Warn if we are changing a NOBITS section to PROGBITS, but
3796 allow the link to proceed. This can happen when users link
3797 non-bss input sections to bss output sections, or emit data
3798 to a bss output section via a linker script. */
3799 _bfd_error_handler
3800 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3801 this_hdr->sh_type = sh_type;
3802 }
3803
3804 switch (this_hdr->sh_type)
3805 {
3806 default:
3807 break;
3808
3809 case SHT_STRTAB:
3810 case SHT_NOTE:
3811 case SHT_NOBITS:
3812 case SHT_PROGBITS:
3813 break;
3814
3815 case SHT_INIT_ARRAY:
3816 case SHT_FINI_ARRAY:
3817 case SHT_PREINIT_ARRAY:
3818 this_hdr->sh_entsize = bed->s->arch_size / 8;
3819 break;
3820
3821 case SHT_HASH:
3822 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3823 break;
3824
3825 case SHT_DYNSYM:
3826 this_hdr->sh_entsize = bed->s->sizeof_sym;
3827 break;
3828
3829 case SHT_DYNAMIC:
3830 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3831 break;
3832
3833 case SHT_RELA:
3834 if (get_elf_backend_data (abfd)->may_use_rela_p)
3835 this_hdr->sh_entsize = bed->s->sizeof_rela;
3836 break;
3837
3838 case SHT_REL:
3839 if (get_elf_backend_data (abfd)->may_use_rel_p)
3840 this_hdr->sh_entsize = bed->s->sizeof_rel;
3841 break;
3842
3843 case SHT_GNU_versym:
3844 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3845 break;
3846
3847 case SHT_GNU_verdef:
3848 this_hdr->sh_entsize = 0;
3849 /* objcopy or strip will copy over sh_info, but may not set
3850 cverdefs. The linker will set cverdefs, but sh_info will be
3851 zero. */
3852 if (this_hdr->sh_info == 0)
3853 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3854 else
3855 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3856 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3857 break;
3858
3859 case SHT_GNU_verneed:
3860 this_hdr->sh_entsize = 0;
3861 /* objcopy or strip will copy over sh_info, but may not set
3862 cverrefs. The linker will set cverrefs, but sh_info will be
3863 zero. */
3864 if (this_hdr->sh_info == 0)
3865 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3866 else
3867 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3868 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3869 break;
3870
3871 case SHT_GROUP:
3872 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3873 break;
3874
3875 case SHT_GNU_HASH:
3876 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3877 break;
3878 }
3879
3880 if ((asect->flags & SEC_ALLOC) != 0)
3881 this_hdr->sh_flags |= SHF_ALLOC;
3882 if ((asect->flags & SEC_READONLY) == 0)
3883 this_hdr->sh_flags |= SHF_WRITE;
3884 if ((asect->flags & SEC_CODE) != 0)
3885 this_hdr->sh_flags |= SHF_EXECINSTR;
3886 if ((asect->flags & SEC_MERGE) != 0)
3887 {
3888 this_hdr->sh_flags |= SHF_MERGE;
3889 this_hdr->sh_entsize = asect->entsize;
3890 }
3891 if ((asect->flags & SEC_STRINGS) != 0)
3892 this_hdr->sh_flags |= SHF_STRINGS;
3893 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3894 this_hdr->sh_flags |= SHF_GROUP;
3895 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3896 {
3897 this_hdr->sh_flags |= SHF_TLS;
3898 if (asect->size == 0
3899 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3900 {
3901 struct bfd_link_order *o = asect->map_tail.link_order;
3902
3903 this_hdr->sh_size = 0;
3904 if (o != NULL)
3905 {
3906 this_hdr->sh_size = o->offset + o->size;
3907 if (this_hdr->sh_size != 0)
3908 this_hdr->sh_type = SHT_NOBITS;
3909 }
3910 }
3911 }
3912 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3913 this_hdr->sh_flags |= SHF_EXCLUDE;
3914
3915 /* If the section has relocs, set up a section header for the
3916 SHT_REL[A] section. If two relocation sections are required for
3917 this section, it is up to the processor-specific back-end to
3918 create the other. */
3919 if ((asect->flags & SEC_RELOC) != 0)
3920 {
3921 /* When doing a relocatable link, create both REL and RELA sections if
3922 needed. */
3923 if (arg->link_info
3924 /* Do the normal setup if we wouldn't create any sections here. */
3925 && esd->rel.count + esd->rela.count > 0
3926 && (bfd_link_relocatable (arg->link_info)
3927 || arg->link_info->emitrelocations))
3928 {
3929 if (esd->rel.count && esd->rel.hdr == NULL
3930 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3931 false, delay_st_name_p))
3932 {
3933 arg->failed = true;
3934 return;
3935 }
3936 if (esd->rela.count && esd->rela.hdr == NULL
3937 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3938 true, delay_st_name_p))
3939 {
3940 arg->failed = true;
3941 return;
3942 }
3943 }
3944 else if (!_bfd_elf_init_reloc_shdr (abfd,
3945 (asect->use_rela_p
3946 ? &esd->rela : &esd->rel),
3947 name,
3948 asect->use_rela_p,
3949 delay_st_name_p))
3950 {
3951 arg->failed = true;
3952 return;
3953 }
3954 }
3955
3956 /* Check for processor-specific section types. */
3957 sh_type = this_hdr->sh_type;
3958 if (bed->elf_backend_fake_sections
3959 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3960 {
3961 arg->failed = true;
3962 return;
3963 }
3964
3965 if (sh_type == SHT_NOBITS && asect->size != 0)
3966 {
3967 /* Don't change the header type from NOBITS if we are being
3968 called for objcopy --only-keep-debug. */
3969 this_hdr->sh_type = sh_type;
3970 }
3971 }
3972
3973 /* Fill in the contents of a SHT_GROUP section. Called from
3974 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3975 when ELF targets use the generic linker, ld. Called for ld -r
3976 from bfd_elf_final_link. */
3977
3978 void
3979 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3980 {
3981 bool *failedptr = (bool *) failedptrarg;
3982 asection *elt, *first;
3983 unsigned char *loc;
3984 bool gas;
3985
3986 /* Ignore linker created group section. See elfNN_ia64_object_p in
3987 elfxx-ia64.c. */
3988 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3989 || sec->size == 0
3990 || *failedptr)
3991 return;
3992
3993 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3994 {
3995 unsigned long symindx = 0;
3996
3997 /* elf_group_id will have been set up by objcopy and the
3998 generic linker. */
3999 if (elf_group_id (sec) != NULL)
4000 symindx = elf_group_id (sec)->udata.i;
4001
4002 if (symindx == 0)
4003 {
4004 /* If called from the assembler, swap_out_syms will have set up
4005 elf_section_syms.
4006 PR 25699: A corrupt input file could contain bogus group info. */
4007 if (sec->index >= elf_num_section_syms (abfd)
4008 || elf_section_syms (abfd)[sec->index] == NULL)
4009 {
4010 *failedptr = true;
4011 return;
4012 }
4013 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
4014 }
4015 elf_section_data (sec)->this_hdr.sh_info = symindx;
4016 }
4017 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
4018 {
4019 /* The ELF backend linker sets sh_info to -2 when the group
4020 signature symbol is global, and thus the index can't be
4021 set until all local symbols are output. */
4022 asection *igroup;
4023 struct bfd_elf_section_data *sec_data;
4024 unsigned long symndx;
4025 unsigned long extsymoff;
4026 struct elf_link_hash_entry *h;
4027
4028 /* The point of this little dance to the first SHF_GROUP section
4029 then back to the SHT_GROUP section is that this gets us to
4030 the SHT_GROUP in the input object. */
4031 igroup = elf_sec_group (elf_next_in_group (sec));
4032 sec_data = elf_section_data (igroup);
4033 symndx = sec_data->this_hdr.sh_info;
4034 extsymoff = 0;
4035 if (!elf_bad_symtab (igroup->owner))
4036 {
4037 Elf_Internal_Shdr *symtab_hdr;
4038
4039 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
4040 extsymoff = symtab_hdr->sh_info;
4041 }
4042 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
4043 while (h->root.type == bfd_link_hash_indirect
4044 || h->root.type == bfd_link_hash_warning)
4045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4046
4047 elf_section_data (sec)->this_hdr.sh_info = h->indx;
4048 }
4049
4050 /* The contents won't be allocated for "ld -r" or objcopy. */
4051 gas = true;
4052 if (sec->contents == NULL)
4053 {
4054 gas = false;
4055 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
4056
4057 /* Arrange for the section to be written out. */
4058 elf_section_data (sec)->this_hdr.contents = sec->contents;
4059 if (sec->contents == NULL)
4060 {
4061 *failedptr = true;
4062 return;
4063 }
4064 }
4065
4066 loc = sec->contents + sec->size;
4067
4068 /* Get the pointer to the first section in the group that gas
4069 squirreled away here. objcopy arranges for this to be set to the
4070 start of the input section group. */
4071 first = elt = elf_next_in_group (sec);
4072
4073 /* First element is a flag word. Rest of section is elf section
4074 indices for all the sections of the group. Write them backwards
4075 just to keep the group in the same order as given in .section
4076 directives, not that it matters. */
4077 while (elt != NULL)
4078 {
4079 asection *s;
4080
4081 s = elt;
4082 if (!gas)
4083 s = s->output_section;
4084 if (s != NULL
4085 && !bfd_is_abs_section (s))
4086 {
4087 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4088 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4089
4090 if (elf_sec->rel.hdr != NULL
4091 && (gas
4092 || (input_elf_sec->rel.hdr != NULL
4093 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4094 {
4095 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4096 loc -= 4;
4097 if (loc == sec->contents)
4098 break;
4099 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4100 }
4101 if (elf_sec->rela.hdr != NULL
4102 && (gas
4103 || (input_elf_sec->rela.hdr != NULL
4104 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4105 {
4106 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4107 loc -= 4;
4108 if (loc == sec->contents)
4109 break;
4110 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4111 }
4112 loc -= 4;
4113 if (loc == sec->contents)
4114 break;
4115 H_PUT_32 (abfd, elf_sec->this_idx, loc);
4116 }
4117 elt = elf_next_in_group (elt);
4118 if (elt == first)
4119 break;
4120 }
4121
4122 /* We should always get here with loc == sec->contents + 4, but it is
4123 possible to craft bogus SHT_GROUP sections that will cause segfaults
4124 in objcopy without checking loc here and in the loop above. */
4125 if (loc == sec->contents)
4126 BFD_ASSERT (0);
4127 else
4128 {
4129 loc -= 4;
4130 if (loc != sec->contents)
4131 {
4132 BFD_ASSERT (0);
4133 memset (sec->contents + 4, 0, loc - sec->contents);
4134 loc = sec->contents;
4135 }
4136 }
4137
4138 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4139 }
4140
4141 /* Given NAME, the name of a relocation section stripped of its
4142 .rel/.rela prefix, return the section in ABFD to which the
4143 relocations apply. */
4144
4145 asection *
4146 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4147 {
4148 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4149 section likely apply to .got.plt or .got section. */
4150 if (get_elf_backend_data (abfd)->want_got_plt
4151 && strcmp (name, ".plt") == 0)
4152 {
4153 asection *sec;
4154
4155 name = ".got.plt";
4156 sec = bfd_get_section_by_name (abfd, name);
4157 if (sec != NULL)
4158 return sec;
4159 name = ".got";
4160 }
4161
4162 return bfd_get_section_by_name (abfd, name);
4163 }
4164
4165 /* Return the section to which RELOC_SEC applies. */
4166
4167 static asection *
4168 elf_get_reloc_section (asection *reloc_sec)
4169 {
4170 const char *name;
4171 unsigned int type;
4172 bfd *abfd;
4173 const struct elf_backend_data *bed;
4174
4175 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4176 if (type != SHT_REL && type != SHT_RELA)
4177 return NULL;
4178
4179 /* We look up the section the relocs apply to by name. */
4180 name = reloc_sec->name;
4181 if (!startswith (name, ".rel"))
4182 return NULL;
4183 name += 4;
4184 if (type == SHT_RELA && *name++ != 'a')
4185 return NULL;
4186
4187 abfd = reloc_sec->owner;
4188 bed = get_elf_backend_data (abfd);
4189 return bed->get_reloc_section (abfd, name);
4190 }
4191
4192 /* Assign all ELF section numbers. The dummy first section is handled here
4193 too. The link/info pointers for the standard section types are filled
4194 in here too, while we're at it. LINK_INFO will be 0 when arriving
4195 here for gas, objcopy, and when using the generic ELF linker. */
4196
4197 static bool
4198 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4199 {
4200 struct elf_obj_tdata *t = elf_tdata (abfd);
4201 asection *sec;
4202 unsigned int section_number;
4203 Elf_Internal_Shdr **i_shdrp;
4204 struct bfd_elf_section_data *d;
4205 bool need_symtab;
4206 size_t amt;
4207
4208 section_number = 1;
4209
4210 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4211
4212 /* SHT_GROUP sections are in relocatable files only. */
4213 if (link_info == NULL || !link_info->resolve_section_groups)
4214 {
4215 size_t reloc_count = 0;
4216
4217 /* Put SHT_GROUP sections first. */
4218 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4219 {
4220 d = elf_section_data (sec);
4221
4222 if (d->this_hdr.sh_type == SHT_GROUP)
4223 {
4224 if (sec->flags & SEC_LINKER_CREATED)
4225 {
4226 /* Remove the linker created SHT_GROUP sections. */
4227 bfd_section_list_remove (abfd, sec);
4228 abfd->section_count--;
4229 }
4230 else
4231 d->this_idx = section_number++;
4232 }
4233
4234 /* Count relocations. */
4235 reloc_count += sec->reloc_count;
4236 }
4237
4238 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4239 if (reloc_count == 0)
4240 abfd->flags &= ~HAS_RELOC;
4241 else
4242 abfd->flags |= HAS_RELOC;
4243 }
4244
4245 for (sec = abfd->sections; sec; sec = sec->next)
4246 {
4247 d = elf_section_data (sec);
4248
4249 if (d->this_hdr.sh_type != SHT_GROUP)
4250 d->this_idx = section_number++;
4251 if (d->this_hdr.sh_name != (unsigned int) -1)
4252 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4253 if (d->rel.hdr)
4254 {
4255 d->rel.idx = section_number++;
4256 if (d->rel.hdr->sh_name != (unsigned int) -1)
4257 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4258 }
4259 else
4260 d->rel.idx = 0;
4261
4262 if (d->rela.hdr)
4263 {
4264 d->rela.idx = section_number++;
4265 if (d->rela.hdr->sh_name != (unsigned int) -1)
4266 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4267 }
4268 else
4269 d->rela.idx = 0;
4270 }
4271
4272 need_symtab = (bfd_get_symcount (abfd) > 0
4273 || (link_info == NULL
4274 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4275 == HAS_RELOC)));
4276 if (need_symtab)
4277 {
4278 elf_onesymtab (abfd) = section_number++;
4279 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4280 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4281 {
4282 elf_section_list *entry;
4283
4284 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4285
4286 entry = bfd_zalloc (abfd, sizeof (*entry));
4287 entry->ndx = section_number++;
4288 elf_symtab_shndx_list (abfd) = entry;
4289 entry->hdr.sh_name
4290 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4291 ".symtab_shndx", false);
4292 if (entry->hdr.sh_name == (unsigned int) -1)
4293 return false;
4294 }
4295 elf_strtab_sec (abfd) = section_number++;
4296 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4297 }
4298
4299 elf_shstrtab_sec (abfd) = section_number++;
4300 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4301 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4302
4303 if (section_number >= SHN_LORESERVE)
4304 {
4305 /* xgettext:c-format */
4306 _bfd_error_handler (_("%pB: too many sections: %u"),
4307 abfd, section_number);
4308 return false;
4309 }
4310
4311 elf_numsections (abfd) = section_number;
4312 elf_elfheader (abfd)->e_shnum = section_number;
4313
4314 /* Set up the list of section header pointers, in agreement with the
4315 indices. */
4316 amt = section_number * sizeof (Elf_Internal_Shdr *);
4317 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4318 if (i_shdrp == NULL)
4319 return false;
4320
4321 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4322 sizeof (Elf_Internal_Shdr));
4323 if (i_shdrp[0] == NULL)
4324 {
4325 bfd_release (abfd, i_shdrp);
4326 return false;
4327 }
4328
4329 elf_elfsections (abfd) = i_shdrp;
4330
4331 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4332 if (need_symtab)
4333 {
4334 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4335 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4336 {
4337 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4338 BFD_ASSERT (entry != NULL);
4339 i_shdrp[entry->ndx] = & entry->hdr;
4340 entry->hdr.sh_link = elf_onesymtab (abfd);
4341 }
4342 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4343 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4344 }
4345
4346 for (sec = abfd->sections; sec; sec = sec->next)
4347 {
4348 asection *s;
4349
4350 d = elf_section_data (sec);
4351
4352 i_shdrp[d->this_idx] = &d->this_hdr;
4353 if (d->rel.idx != 0)
4354 i_shdrp[d->rel.idx] = d->rel.hdr;
4355 if (d->rela.idx != 0)
4356 i_shdrp[d->rela.idx] = d->rela.hdr;
4357
4358 /* Fill in the sh_link and sh_info fields while we're at it. */
4359
4360 /* sh_link of a reloc section is the section index of the symbol
4361 table. sh_info is the section index of the section to which
4362 the relocation entries apply. */
4363 if (d->rel.idx != 0)
4364 {
4365 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4366 d->rel.hdr->sh_info = d->this_idx;
4367 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4368 }
4369 if (d->rela.idx != 0)
4370 {
4371 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4372 d->rela.hdr->sh_info = d->this_idx;
4373 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4374 }
4375
4376 /* We need to set up sh_link for SHF_LINK_ORDER. */
4377 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4378 {
4379 s = elf_linked_to_section (sec);
4380 /* We can now have a NULL linked section pointer.
4381 This happens when the sh_link field is 0, which is done
4382 when a linked to section is discarded but the linking
4383 section has been retained for some reason. */
4384 if (s)
4385 {
4386 /* Check discarded linkonce section. */
4387 if (discarded_section (s))
4388 {
4389 asection *kept;
4390 _bfd_error_handler
4391 /* xgettext:c-format */
4392 (_("%pB: sh_link of section `%pA' points to"
4393 " discarded section `%pA' of `%pB'"),
4394 abfd, d->this_hdr.bfd_section, s, s->owner);
4395 /* Point to the kept section if it has the same
4396 size as the discarded one. */
4397 kept = _bfd_elf_check_kept_section (s, link_info);
4398 if (kept == NULL)
4399 {
4400 bfd_set_error (bfd_error_bad_value);
4401 return false;
4402 }
4403 s = kept;
4404 }
4405 /* Handle objcopy. */
4406 else if (s->output_section == NULL)
4407 {
4408 _bfd_error_handler
4409 /* xgettext:c-format */
4410 (_("%pB: sh_link of section `%pA' points to"
4411 " removed section `%pA' of `%pB'"),
4412 abfd, d->this_hdr.bfd_section, s, s->owner);
4413 bfd_set_error (bfd_error_bad_value);
4414 return false;
4415 }
4416 s = s->output_section;
4417 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4418 }
4419 }
4420
4421 switch (d->this_hdr.sh_type)
4422 {
4423 case SHT_REL:
4424 case SHT_RELA:
4425 /* sh_link is the section index of the symbol table.
4426 sh_info is the section index of the section to which the
4427 relocation entries apply. */
4428 if (d->this_hdr.sh_link == 0)
4429 {
4430 /* FIXME maybe: If this is a reloc section which we are
4431 treating as a normal section then we likely should
4432 not be assuming its sh_link is .dynsym or .symtab. */
4433 if ((sec->flags & SEC_ALLOC) != 0)
4434 {
4435 s = bfd_get_section_by_name (abfd, ".dynsym");
4436 if (s != NULL)
4437 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4438 }
4439 else
4440 d->this_hdr.sh_link = elf_onesymtab (abfd);
4441 }
4442
4443 s = elf_get_reloc_section (sec);
4444 if (s != NULL)
4445 {
4446 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4447 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4448 }
4449 break;
4450
4451 case SHT_STRTAB:
4452 /* We assume that a section named .stab*str is a stabs
4453 string section. We look for a section with the same name
4454 but without the trailing ``str'', and set its sh_link
4455 field to point to this section. */
4456 if (startswith (sec->name, ".stab")
4457 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4458 {
4459 size_t len;
4460 char *alc;
4461
4462 len = strlen (sec->name);
4463 alc = (char *) bfd_malloc (len - 2);
4464 if (alc == NULL)
4465 return false;
4466 memcpy (alc, sec->name, len - 3);
4467 alc[len - 3] = '\0';
4468 s = bfd_get_section_by_name (abfd, alc);
4469 free (alc);
4470 if (s != NULL)
4471 {
4472 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4473
4474 /* This is a .stab section. */
4475 elf_section_data (s)->this_hdr.sh_entsize = 12;
4476 }
4477 }
4478 break;
4479
4480 case SHT_DYNAMIC:
4481 case SHT_DYNSYM:
4482 case SHT_GNU_verneed:
4483 case SHT_GNU_verdef:
4484 /* sh_link is the section header index of the string table
4485 used for the dynamic entries, or the symbol table, or the
4486 version strings. */
4487 s = bfd_get_section_by_name (abfd, ".dynstr");
4488 if (s != NULL)
4489 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4490 break;
4491
4492 case SHT_GNU_LIBLIST:
4493 /* sh_link is the section header index of the prelink library
4494 list used for the dynamic entries, or the symbol table, or
4495 the version strings. */
4496 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4497 ? ".dynstr" : ".gnu.libstr"));
4498 if (s != NULL)
4499 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4500 break;
4501
4502 case SHT_HASH:
4503 case SHT_GNU_HASH:
4504 case SHT_GNU_versym:
4505 /* sh_link is the section header index of the symbol table
4506 this hash table or version table is for. */
4507 s = bfd_get_section_by_name (abfd, ".dynsym");
4508 if (s != NULL)
4509 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4510 break;
4511
4512 case SHT_GROUP:
4513 d->this_hdr.sh_link = elf_onesymtab (abfd);
4514 }
4515 }
4516
4517 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4518 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4519 debug section name from .debug_* to .zdebug_* if needed. */
4520
4521 return true;
4522 }
4523
4524 static bool
4525 sym_is_global (bfd *abfd, asymbol *sym)
4526 {
4527 /* If the backend has a special mapping, use it. */
4528 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4529 if (bed->elf_backend_sym_is_global)
4530 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4531
4532 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4533 || bfd_is_und_section (bfd_asymbol_section (sym))
4534 || bfd_is_com_section (bfd_asymbol_section (sym)));
4535 }
4536
4537 /* Filter global symbols of ABFD to include in the import library. All
4538 SYMCOUNT symbols of ABFD can be examined from their pointers in
4539 SYMS. Pointers of symbols to keep should be stored contiguously at
4540 the beginning of that array.
4541
4542 Returns the number of symbols to keep. */
4543
4544 unsigned int
4545 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4546 asymbol **syms, long symcount)
4547 {
4548 long src_count, dst_count = 0;
4549
4550 for (src_count = 0; src_count < symcount; src_count++)
4551 {
4552 asymbol *sym = syms[src_count];
4553 char *name = (char *) bfd_asymbol_name (sym);
4554 struct bfd_link_hash_entry *h;
4555
4556 if (!sym_is_global (abfd, sym))
4557 continue;
4558
4559 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4560 if (h == NULL)
4561 continue;
4562 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4563 continue;
4564 if (h->linker_def || h->ldscript_def)
4565 continue;
4566
4567 syms[dst_count++] = sym;
4568 }
4569
4570 syms[dst_count] = NULL;
4571
4572 return dst_count;
4573 }
4574
4575 /* Don't output section symbols for sections that are not going to be
4576 output, that are duplicates or there is no BFD section. */
4577
4578 static bool
4579 ignore_section_sym (bfd *abfd, asymbol *sym)
4580 {
4581 elf_symbol_type *type_ptr;
4582
4583 if (sym == NULL)
4584 return false;
4585
4586 if ((sym->flags & BSF_SECTION_SYM) == 0)
4587 return false;
4588
4589 /* Ignore the section symbol if it isn't used. */
4590 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4591 return true;
4592
4593 if (sym->section == NULL)
4594 return true;
4595
4596 type_ptr = elf_symbol_from (sym);
4597 return ((type_ptr != NULL
4598 && type_ptr->internal_elf_sym.st_shndx != 0
4599 && bfd_is_abs_section (sym->section))
4600 || !(sym->section->owner == abfd
4601 || (sym->section->output_section != NULL
4602 && sym->section->output_section->owner == abfd
4603 && sym->section->output_offset == 0)
4604 || bfd_is_abs_section (sym->section)));
4605 }
4606
4607 /* Map symbol from it's internal number to the external number, moving
4608 all local symbols to be at the head of the list. */
4609
4610 static bool
4611 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4612 {
4613 unsigned int symcount = bfd_get_symcount (abfd);
4614 asymbol **syms = bfd_get_outsymbols (abfd);
4615 asymbol **sect_syms;
4616 unsigned int num_locals = 0;
4617 unsigned int num_globals = 0;
4618 unsigned int num_locals2 = 0;
4619 unsigned int num_globals2 = 0;
4620 unsigned int max_index = 0;
4621 unsigned int idx;
4622 asection *asect;
4623 asymbol **new_syms;
4624 size_t amt;
4625
4626 #ifdef DEBUG
4627 fprintf (stderr, "elf_map_symbols\n");
4628 fflush (stderr);
4629 #endif
4630
4631 for (asect = abfd->sections; asect; asect = asect->next)
4632 {
4633 if (max_index < asect->index)
4634 max_index = asect->index;
4635 }
4636
4637 max_index++;
4638 amt = max_index * sizeof (asymbol *);
4639 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4640 if (sect_syms == NULL)
4641 return false;
4642 elf_section_syms (abfd) = sect_syms;
4643 elf_num_section_syms (abfd) = max_index;
4644
4645 /* Init sect_syms entries for any section symbols we have already
4646 decided to output. */
4647 for (idx = 0; idx < symcount; idx++)
4648 {
4649 asymbol *sym = syms[idx];
4650
4651 if ((sym->flags & BSF_SECTION_SYM) != 0
4652 && sym->value == 0
4653 && !ignore_section_sym (abfd, sym)
4654 && !bfd_is_abs_section (sym->section))
4655 {
4656 asection *sec = sym->section;
4657
4658 if (sec->owner != abfd)
4659 sec = sec->output_section;
4660
4661 sect_syms[sec->index] = syms[idx];
4662 }
4663 }
4664
4665 /* Classify all of the symbols. */
4666 for (idx = 0; idx < symcount; idx++)
4667 {
4668 if (sym_is_global (abfd, syms[idx]))
4669 num_globals++;
4670 else if (!ignore_section_sym (abfd, syms[idx]))
4671 num_locals++;
4672 }
4673
4674 /* We will be adding a section symbol for each normal BFD section. Most
4675 sections will already have a section symbol in outsymbols, but
4676 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4677 at least in that case. */
4678 for (asect = abfd->sections; asect; asect = asect->next)
4679 {
4680 asymbol *sym = asect->symbol;
4681 /* Don't include ignored section symbols. */
4682 if (!ignore_section_sym (abfd, sym)
4683 && sect_syms[asect->index] == NULL)
4684 {
4685 if (!sym_is_global (abfd, asect->symbol))
4686 num_locals++;
4687 else
4688 num_globals++;
4689 }
4690 }
4691
4692 /* Now sort the symbols so the local symbols are first. */
4693 amt = (num_locals + num_globals) * sizeof (asymbol *);
4694 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4695 if (new_syms == NULL)
4696 return false;
4697
4698 for (idx = 0; idx < symcount; idx++)
4699 {
4700 asymbol *sym = syms[idx];
4701 unsigned int i;
4702
4703 if (sym_is_global (abfd, sym))
4704 i = num_locals + num_globals2++;
4705 /* Don't include ignored section symbols. */
4706 else if (!ignore_section_sym (abfd, sym))
4707 i = num_locals2++;
4708 else
4709 continue;
4710 new_syms[i] = sym;
4711 sym->udata.i = i + 1;
4712 }
4713 for (asect = abfd->sections; asect; asect = asect->next)
4714 {
4715 asymbol *sym = asect->symbol;
4716 if (!ignore_section_sym (abfd, sym)
4717 && sect_syms[asect->index] == NULL)
4718 {
4719 unsigned int i;
4720
4721 sect_syms[asect->index] = sym;
4722 if (!sym_is_global (abfd, sym))
4723 i = num_locals2++;
4724 else
4725 i = num_locals + num_globals2++;
4726 new_syms[i] = sym;
4727 sym->udata.i = i + 1;
4728 }
4729 }
4730
4731 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4732
4733 *pnum_locals = num_locals;
4734 return true;
4735 }
4736
4737 /* Align to the maximum file alignment that could be required for any
4738 ELF data structure. */
4739
4740 static inline file_ptr
4741 align_file_position (file_ptr off, int align)
4742 {
4743 return (off + align - 1) & ~(align - 1);
4744 }
4745
4746 /* Assign a file position to a section, optionally aligning to the
4747 required section alignment. */
4748
4749 file_ptr
4750 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4751 file_ptr offset,
4752 bool align)
4753 {
4754 if (align && i_shdrp->sh_addralign > 1)
4755 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4756 i_shdrp->sh_offset = offset;
4757 if (i_shdrp->bfd_section != NULL)
4758 i_shdrp->bfd_section->filepos = offset;
4759 if (i_shdrp->sh_type != SHT_NOBITS)
4760 offset += i_shdrp->sh_size;
4761 return offset;
4762 }
4763
4764 /* Compute the file positions we are going to put the sections at, and
4765 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4766 is not NULL, this is being called by the ELF backend linker. */
4767
4768 bool
4769 _bfd_elf_compute_section_file_positions (bfd *abfd,
4770 struct bfd_link_info *link_info)
4771 {
4772 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4773 struct fake_section_arg fsargs;
4774 bool failed;
4775 struct elf_strtab_hash *strtab = NULL;
4776 Elf_Internal_Shdr *shstrtab_hdr;
4777 bool need_symtab;
4778
4779 if (abfd->output_has_begun)
4780 return true;
4781
4782 /* Do any elf backend specific processing first. */
4783 if (bed->elf_backend_begin_write_processing)
4784 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4785
4786 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4787 return false;
4788
4789 fsargs.failed = false;
4790 fsargs.link_info = link_info;
4791 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4792 if (fsargs.failed)
4793 return false;
4794
4795 if (!assign_section_numbers (abfd, link_info))
4796 return false;
4797
4798 /* The backend linker builds symbol table information itself. */
4799 need_symtab = (link_info == NULL
4800 && (bfd_get_symcount (abfd) > 0
4801 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4802 == HAS_RELOC)));
4803 if (need_symtab)
4804 {
4805 /* Non-zero if doing a relocatable link. */
4806 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4807
4808 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4809 return false;
4810 }
4811
4812 failed = false;
4813 if (link_info == NULL)
4814 {
4815 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4816 if (failed)
4817 goto err_free_strtab;
4818 }
4819
4820 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4821 /* sh_name was set in init_file_header. */
4822 shstrtab_hdr->sh_type = SHT_STRTAB;
4823 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4824 shstrtab_hdr->sh_addr = 0;
4825 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4826 shstrtab_hdr->sh_entsize = 0;
4827 shstrtab_hdr->sh_link = 0;
4828 shstrtab_hdr->sh_info = 0;
4829 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4830 shstrtab_hdr->sh_addralign = 1;
4831
4832 if (!assign_file_positions_except_relocs (abfd, link_info))
4833 goto err_free_strtab;
4834
4835 if (strtab != NULL)
4836 {
4837 file_ptr off;
4838 Elf_Internal_Shdr *hdr;
4839
4840 off = elf_next_file_pos (abfd);
4841
4842 hdr = & elf_symtab_hdr (abfd);
4843 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4844
4845 if (elf_symtab_shndx_list (abfd) != NULL)
4846 {
4847 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4848 if (hdr->sh_size != 0)
4849 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4850 /* FIXME: What about other symtab_shndx sections in the list ? */
4851 }
4852
4853 hdr = &elf_tdata (abfd)->strtab_hdr;
4854 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4855
4856 elf_next_file_pos (abfd) = off;
4857
4858 /* Now that we know where the .strtab section goes, write it
4859 out. */
4860 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4861 || ! _bfd_elf_strtab_emit (abfd, strtab))
4862 goto err_free_strtab;
4863 _bfd_elf_strtab_free (strtab);
4864 }
4865
4866 abfd->output_has_begun = true;
4867 return true;
4868
4869 err_free_strtab:
4870 if (strtab != NULL)
4871 _bfd_elf_strtab_free (strtab);
4872 return false;
4873 }
4874
4875 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4876 function effectively returns whether --eh-frame-hdr is given on the
4877 command line. After size_dynamic_sections the result reflects
4878 whether .eh_frame_hdr will actually be output (sizing isn't done
4879 until ldemul_after_allocation). */
4880
4881 static asection *
4882 elf_eh_frame_hdr (const struct bfd_link_info *info)
4883 {
4884 if (info != NULL && is_elf_hash_table (info->hash))
4885 return elf_hash_table (info)->eh_info.hdr_sec;
4886 return NULL;
4887 }
4888
4889 /* Make an initial estimate of the size of the program header. If we
4890 get the number wrong here, we'll redo section placement. */
4891
4892 static bfd_size_type
4893 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4894 {
4895 size_t segs;
4896 asection *s;
4897 const struct elf_backend_data *bed;
4898
4899 /* Assume we will need exactly two PT_LOAD segments: one for text
4900 and one for data. */
4901 segs = 2;
4902
4903 s = bfd_get_section_by_name (abfd, ".interp");
4904 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4905 {
4906 /* If we have a loadable interpreter section, we need a
4907 PT_INTERP segment. In this case, assume we also need a
4908 PT_PHDR segment, although that may not be true for all
4909 targets. */
4910 segs += 2;
4911 }
4912
4913 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4914 {
4915 /* We need a PT_DYNAMIC segment. */
4916 ++segs;
4917 }
4918
4919 if (info != NULL && info->relro)
4920 {
4921 /* We need a PT_GNU_RELRO segment. */
4922 ++segs;
4923 }
4924
4925 if (elf_eh_frame_hdr (info))
4926 {
4927 /* We need a PT_GNU_EH_FRAME segment. */
4928 ++segs;
4929 }
4930
4931 if (elf_stack_flags (abfd))
4932 {
4933 /* We need a PT_GNU_STACK segment. */
4934 ++segs;
4935 }
4936
4937 if (elf_sframe (abfd))
4938 {
4939 /* We need a PT_GNU_SFRAME segment. */
4940 ++segs;
4941 }
4942
4943 s = bfd_get_section_by_name (abfd,
4944 NOTE_GNU_PROPERTY_SECTION_NAME);
4945 if (s != NULL && s->size != 0)
4946 {
4947 /* We need a PT_GNU_PROPERTY segment. */
4948 ++segs;
4949 }
4950
4951 for (s = abfd->sections; s != NULL; s = s->next)
4952 {
4953 if ((s->flags & SEC_LOAD) != 0
4954 && elf_section_type (s) == SHT_NOTE)
4955 {
4956 unsigned int alignment_power;
4957 /* We need a PT_NOTE segment. */
4958 ++segs;
4959 /* Try to create just one PT_NOTE segment for all adjacent
4960 loadable SHT_NOTE sections. gABI requires that within a
4961 PT_NOTE segment (and also inside of each SHT_NOTE section)
4962 each note should have the same alignment. So we check
4963 whether the sections are correctly aligned. */
4964 alignment_power = s->alignment_power;
4965 while (s->next != NULL
4966 && s->next->alignment_power == alignment_power
4967 && (s->next->flags & SEC_LOAD) != 0
4968 && elf_section_type (s->next) == SHT_NOTE)
4969 s = s->next;
4970 }
4971 }
4972
4973 for (s = abfd->sections; s != NULL; s = s->next)
4974 {
4975 if (s->flags & SEC_THREAD_LOCAL)
4976 {
4977 /* We need a PT_TLS segment. */
4978 ++segs;
4979 break;
4980 }
4981 }
4982
4983 bed = get_elf_backend_data (abfd);
4984
4985 if ((abfd->flags & D_PAGED) != 0
4986 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4987 {
4988 /* Add a PT_GNU_MBIND segment for each mbind section. */
4989 bfd_vma commonpagesize;
4990 unsigned int page_align_power;
4991
4992 if (info != NULL)
4993 commonpagesize = info->commonpagesize;
4994 else
4995 commonpagesize = bed->commonpagesize;
4996 page_align_power = bfd_log2 (commonpagesize);
4997 for (s = abfd->sections; s != NULL; s = s->next)
4998 if (elf_section_flags (s) & SHF_GNU_MBIND)
4999 {
5000 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
5001 {
5002 _bfd_error_handler
5003 /* xgettext:c-format */
5004 (_("%pB: GNU_MBIND section `%pA' has invalid "
5005 "sh_info field: %d"),
5006 abfd, s, elf_section_data (s)->this_hdr.sh_info);
5007 continue;
5008 }
5009 /* Align mbind section to page size. */
5010 if (s->alignment_power < page_align_power)
5011 s->alignment_power = page_align_power;
5012 segs ++;
5013 }
5014 }
5015
5016 /* Let the backend count up any program headers it might need. */
5017 if (bed->elf_backend_additional_program_headers)
5018 {
5019 int a;
5020
5021 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
5022 if (a == -1)
5023 abort ();
5024 segs += a;
5025 }
5026
5027 return segs * bed->s->sizeof_phdr;
5028 }
5029
5030 /* Find the segment that contains the output_section of section. */
5031
5032 Elf_Internal_Phdr *
5033 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
5034 {
5035 struct elf_segment_map *m;
5036 Elf_Internal_Phdr *p;
5037
5038 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
5039 m != NULL;
5040 m = m->next, p++)
5041 {
5042 int i;
5043
5044 for (i = m->count - 1; i >= 0; i--)
5045 if (m->sections[i] == section)
5046 return p;
5047 }
5048
5049 return NULL;
5050 }
5051
5052 /* Create a mapping from a set of sections to a program segment. */
5053
5054 static struct elf_segment_map *
5055 make_mapping (bfd *abfd,
5056 asection **sections,
5057 unsigned int from,
5058 unsigned int to,
5059 bool phdr)
5060 {
5061 struct elf_segment_map *m;
5062 unsigned int i;
5063 asection **hdrpp;
5064 size_t amt;
5065
5066 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5067 amt += (to - from) * sizeof (asection *);
5068 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5069 if (m == NULL)
5070 return NULL;
5071 m->next = NULL;
5072 m->p_type = PT_LOAD;
5073 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5074 m->sections[i - from] = *hdrpp;
5075 m->count = to - from;
5076
5077 if (from == 0 && phdr)
5078 {
5079 /* Include the headers in the first PT_LOAD segment. */
5080 m->includes_filehdr = 1;
5081 m->includes_phdrs = 1;
5082 }
5083
5084 return m;
5085 }
5086
5087 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5088 on failure. */
5089
5090 struct elf_segment_map *
5091 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5092 {
5093 struct elf_segment_map *m;
5094
5095 m = (struct elf_segment_map *) bfd_zalloc (abfd,
5096 sizeof (struct elf_segment_map));
5097 if (m == NULL)
5098 return NULL;
5099 m->next = NULL;
5100 m->p_type = PT_DYNAMIC;
5101 m->count = 1;
5102 m->sections[0] = dynsec;
5103
5104 return m;
5105 }
5106
5107 /* Possibly add or remove segments from the segment map. */
5108
5109 static bool
5110 elf_modify_segment_map (bfd *abfd,
5111 struct bfd_link_info *info,
5112 bool remove_empty_load)
5113 {
5114 struct elf_segment_map **m;
5115 const struct elf_backend_data *bed;
5116
5117 /* The placement algorithm assumes that non allocated sections are
5118 not in PT_LOAD segments. We ensure this here by removing such
5119 sections from the segment map. We also remove excluded
5120 sections. Finally, any PT_LOAD segment without sections is
5121 removed. */
5122 m = &elf_seg_map (abfd);
5123 while (*m)
5124 {
5125 unsigned int i, new_count;
5126
5127 for (new_count = 0, i = 0; i < (*m)->count; i++)
5128 {
5129 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5130 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5131 || (*m)->p_type != PT_LOAD))
5132 {
5133 (*m)->sections[new_count] = (*m)->sections[i];
5134 new_count++;
5135 }
5136 }
5137 (*m)->count = new_count;
5138
5139 if (remove_empty_load
5140 && (*m)->p_type == PT_LOAD
5141 && (*m)->count == 0
5142 && !(*m)->includes_phdrs)
5143 *m = (*m)->next;
5144 else
5145 m = &(*m)->next;
5146 }
5147
5148 bed = get_elf_backend_data (abfd);
5149 if (bed->elf_backend_modify_segment_map != NULL)
5150 {
5151 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5152 return false;
5153 }
5154
5155 return true;
5156 }
5157
5158 #define IS_TBSS(s) \
5159 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5160
5161 /* Set up a mapping from BFD sections to program segments. Update
5162 NEED_LAYOUT if the section layout is changed. */
5163
5164 bool
5165 _bfd_elf_map_sections_to_segments (bfd *abfd,
5166 struct bfd_link_info *info,
5167 bool *need_layout)
5168 {
5169 unsigned int count;
5170 struct elf_segment_map *m;
5171 asection **sections = NULL;
5172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5173 bool no_user_phdrs;
5174
5175 no_user_phdrs = elf_seg_map (abfd) == NULL;
5176
5177 if (info != NULL)
5178 {
5179 info->user_phdrs = !no_user_phdrs;
5180
5181 /* Size the relative relocations if DT_RELR is enabled. */
5182 if (info->enable_dt_relr
5183 && need_layout != NULL
5184 && bed->size_relative_relocs
5185 && !bed->size_relative_relocs (info, need_layout))
5186 info->callbacks->einfo
5187 (_("%F%P: failed to size relative relocations\n"));
5188 }
5189
5190 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5191 {
5192 asection *s;
5193 unsigned int i;
5194 struct elf_segment_map *mfirst;
5195 struct elf_segment_map **pm;
5196 asection *last_hdr;
5197 bfd_vma last_size;
5198 unsigned int hdr_index;
5199 bfd_vma maxpagesize;
5200 asection **hdrpp;
5201 bool phdr_in_segment;
5202 bool writable;
5203 bool executable;
5204 unsigned int tls_count = 0;
5205 asection *first_tls = NULL;
5206 asection *first_mbind = NULL;
5207 asection *dynsec, *eh_frame_hdr;
5208 asection *sframe;
5209 size_t amt;
5210 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5211 bfd_size_type phdr_size; /* Octets/bytes. */
5212 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5213
5214 /* Select the allocated sections, and sort them. */
5215
5216 amt = bfd_count_sections (abfd) * sizeof (asection *);
5217 sections = (asection **) bfd_malloc (amt);
5218 if (sections == NULL)
5219 goto error_return;
5220
5221 /* Calculate top address, avoiding undefined behaviour of shift
5222 left operator when shift count is equal to size of type
5223 being shifted. */
5224 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5225 addr_mask = (addr_mask << 1) + 1;
5226
5227 i = 0;
5228 for (s = abfd->sections; s != NULL; s = s->next)
5229 {
5230 if ((s->flags & SEC_ALLOC) != 0)
5231 {
5232 /* target_index is unused until bfd_elf_final_link
5233 starts output of section symbols. Use it to make
5234 qsort stable. */
5235 s->target_index = i;
5236 sections[i] = s;
5237 ++i;
5238 /* A wrapping section potentially clashes with header. */
5239 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5240 wrap_to = (s->lma + s->size / opb) & addr_mask;
5241 }
5242 }
5243 BFD_ASSERT (i <= bfd_count_sections (abfd));
5244 count = i;
5245
5246 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5247
5248 phdr_size = elf_program_header_size (abfd);
5249 if (phdr_size == (bfd_size_type) -1)
5250 phdr_size = get_program_header_size (abfd, info);
5251 phdr_size += bed->s->sizeof_ehdr;
5252 /* phdr_size is compared to LMA values which are in bytes. */
5253 phdr_size /= opb;
5254 if (info != NULL)
5255 maxpagesize = info->maxpagesize;
5256 else
5257 maxpagesize = bed->maxpagesize;
5258 if (maxpagesize == 0)
5259 maxpagesize = 1;
5260 phdr_in_segment = info != NULL && info->load_phdrs;
5261 if (count != 0
5262 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5263 >= (phdr_size & (maxpagesize - 1))))
5264 /* For compatibility with old scripts that may not be using
5265 SIZEOF_HEADERS, add headers when it looks like space has
5266 been left for them. */
5267 phdr_in_segment = true;
5268
5269 /* Build the mapping. */
5270 mfirst = NULL;
5271 pm = &mfirst;
5272
5273 /* If we have a .interp section, then create a PT_PHDR segment for
5274 the program headers and a PT_INTERP segment for the .interp
5275 section. */
5276 s = bfd_get_section_by_name (abfd, ".interp");
5277 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5278 {
5279 amt = sizeof (struct elf_segment_map);
5280 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5281 if (m == NULL)
5282 goto error_return;
5283 m->next = NULL;
5284 m->p_type = PT_PHDR;
5285 m->p_flags = PF_R;
5286 m->p_flags_valid = 1;
5287 m->includes_phdrs = 1;
5288 phdr_in_segment = true;
5289 *pm = m;
5290 pm = &m->next;
5291
5292 amt = sizeof (struct elf_segment_map);
5293 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5294 if (m == NULL)
5295 goto error_return;
5296 m->next = NULL;
5297 m->p_type = PT_INTERP;
5298 m->count = 1;
5299 m->sections[0] = s;
5300
5301 *pm = m;
5302 pm = &m->next;
5303 }
5304
5305 /* Look through the sections. We put sections in the same program
5306 segment when the start of the second section can be placed within
5307 a few bytes of the end of the first section. */
5308 last_hdr = NULL;
5309 last_size = 0;
5310 hdr_index = 0;
5311 writable = false;
5312 executable = false;
5313 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5314 if (dynsec != NULL
5315 && (dynsec->flags & SEC_LOAD) == 0)
5316 dynsec = NULL;
5317
5318 if ((abfd->flags & D_PAGED) == 0)
5319 phdr_in_segment = false;
5320
5321 /* Deal with -Ttext or something similar such that the first section
5322 is not adjacent to the program headers. This is an
5323 approximation, since at this point we don't know exactly how many
5324 program headers we will need. */
5325 if (phdr_in_segment && count > 0)
5326 {
5327 bfd_vma phdr_lma; /* Bytes. */
5328 bool separate_phdr = false;
5329
5330 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5331 if (info != NULL
5332 && info->separate_code
5333 && (sections[0]->flags & SEC_CODE) != 0)
5334 {
5335 /* If data sections should be separate from code and
5336 thus not executable, and the first section is
5337 executable then put the file and program headers in
5338 their own PT_LOAD. */
5339 separate_phdr = true;
5340 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5341 == (sections[0]->lma & addr_mask & -maxpagesize)))
5342 {
5343 /* The file and program headers are currently on the
5344 same page as the first section. Put them on the
5345 previous page if we can. */
5346 if (phdr_lma >= maxpagesize)
5347 phdr_lma -= maxpagesize;
5348 else
5349 separate_phdr = false;
5350 }
5351 }
5352 if ((sections[0]->lma & addr_mask) < phdr_lma
5353 || (sections[0]->lma & addr_mask) < phdr_size)
5354 /* If file and program headers would be placed at the end
5355 of memory then it's probably better to omit them. */
5356 phdr_in_segment = false;
5357 else if (phdr_lma < wrap_to)
5358 /* If a section wraps around to where we'll be placing
5359 file and program headers, then the headers will be
5360 overwritten. */
5361 phdr_in_segment = false;
5362 else if (separate_phdr)
5363 {
5364 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5365 if (m == NULL)
5366 goto error_return;
5367 m->p_paddr = phdr_lma * opb;
5368 m->p_vaddr_offset
5369 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5370 m->p_paddr_valid = 1;
5371 *pm = m;
5372 pm = &m->next;
5373 phdr_in_segment = false;
5374 }
5375 }
5376
5377 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5378 {
5379 asection *hdr;
5380 bool new_segment;
5381
5382 hdr = *hdrpp;
5383
5384 /* See if this section and the last one will fit in the same
5385 segment. */
5386
5387 if (last_hdr == NULL)
5388 {
5389 /* If we don't have a segment yet, then we don't need a new
5390 one (we build the last one after this loop). */
5391 new_segment = false;
5392 }
5393 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5394 {
5395 /* If this section has a different relation between the
5396 virtual address and the load address, then we need a new
5397 segment. */
5398 new_segment = true;
5399 }
5400 else if (hdr->lma < last_hdr->lma + last_size
5401 || last_hdr->lma + last_size < last_hdr->lma)
5402 {
5403 /* If this section has a load address that makes it overlap
5404 the previous section, then we need a new segment. */
5405 new_segment = true;
5406 }
5407 else if ((abfd->flags & D_PAGED) != 0
5408 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5409 == (hdr->lma & -maxpagesize)))
5410 {
5411 /* If we are demand paged then we can't map two disk
5412 pages onto the same memory page. */
5413 new_segment = false;
5414 }
5415 /* In the next test we have to be careful when last_hdr->lma is close
5416 to the end of the address space. If the aligned address wraps
5417 around to the start of the address space, then there are no more
5418 pages left in memory and it is OK to assume that the current
5419 section can be included in the current segment. */
5420 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5421 + maxpagesize > last_hdr->lma)
5422 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5423 + maxpagesize <= hdr->lma))
5424 {
5425 /* If putting this section in this segment would force us to
5426 skip a page in the segment, then we need a new segment. */
5427 new_segment = true;
5428 }
5429 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5430 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5431 {
5432 /* We don't want to put a loaded section after a
5433 nonloaded (ie. bss style) section in the same segment
5434 as that will force the non-loaded section to be loaded.
5435 Consider .tbss sections as loaded for this purpose. */
5436 new_segment = true;
5437 }
5438 else if ((abfd->flags & D_PAGED) == 0)
5439 {
5440 /* If the file is not demand paged, which means that we
5441 don't require the sections to be correctly aligned in the
5442 file, then there is no other reason for a new segment. */
5443 new_segment = false;
5444 }
5445 else if (info != NULL
5446 && info->separate_code
5447 && executable != ((hdr->flags & SEC_CODE) != 0))
5448 {
5449 new_segment = true;
5450 }
5451 else if (! writable
5452 && (hdr->flags & SEC_READONLY) == 0)
5453 {
5454 /* We don't want to put a writable section in a read only
5455 segment. */
5456 new_segment = true;
5457 }
5458 else
5459 {
5460 /* Otherwise, we can use the same segment. */
5461 new_segment = false;
5462 }
5463
5464 /* Allow interested parties a chance to override our decision. */
5465 if (last_hdr != NULL
5466 && info != NULL
5467 && info->callbacks->override_segment_assignment != NULL)
5468 new_segment
5469 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5470 last_hdr,
5471 new_segment);
5472
5473 if (! new_segment)
5474 {
5475 if ((hdr->flags & SEC_READONLY) == 0)
5476 writable = true;
5477 if ((hdr->flags & SEC_CODE) != 0)
5478 executable = true;
5479 last_hdr = hdr;
5480 /* .tbss sections effectively have zero size. */
5481 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5482 continue;
5483 }
5484
5485 /* We need a new program segment. We must create a new program
5486 header holding all the sections from hdr_index until hdr. */
5487
5488 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5489 if (m == NULL)
5490 goto error_return;
5491
5492 *pm = m;
5493 pm = &m->next;
5494
5495 if ((hdr->flags & SEC_READONLY) == 0)
5496 writable = true;
5497 else
5498 writable = false;
5499
5500 if ((hdr->flags & SEC_CODE) == 0)
5501 executable = false;
5502 else
5503 executable = true;
5504
5505 last_hdr = hdr;
5506 /* .tbss sections effectively have zero size. */
5507 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5508 hdr_index = i;
5509 phdr_in_segment = false;
5510 }
5511
5512 /* Create a final PT_LOAD program segment, but not if it's just
5513 for .tbss. */
5514 if (last_hdr != NULL
5515 && (i - hdr_index != 1
5516 || !IS_TBSS (last_hdr)))
5517 {
5518 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5519 if (m == NULL)
5520 goto error_return;
5521
5522 *pm = m;
5523 pm = &m->next;
5524 }
5525
5526 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5527 if (dynsec != NULL)
5528 {
5529 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5530 if (m == NULL)
5531 goto error_return;
5532 *pm = m;
5533 pm = &m->next;
5534 }
5535
5536 /* For each batch of consecutive loadable SHT_NOTE sections,
5537 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5538 because if we link together nonloadable .note sections and
5539 loadable .note sections, we will generate two .note sections
5540 in the output file. */
5541 for (s = abfd->sections; s != NULL; s = s->next)
5542 {
5543 if ((s->flags & SEC_LOAD) != 0
5544 && elf_section_type (s) == SHT_NOTE)
5545 {
5546 asection *s2;
5547 unsigned int alignment_power = s->alignment_power;
5548
5549 count = 1;
5550 for (s2 = s; s2->next != NULL; s2 = s2->next)
5551 {
5552 if (s2->next->alignment_power == alignment_power
5553 && (s2->next->flags & SEC_LOAD) != 0
5554 && elf_section_type (s2->next) == SHT_NOTE
5555 && align_power (s2->lma + s2->size / opb,
5556 alignment_power)
5557 == s2->next->lma)
5558 count++;
5559 else
5560 break;
5561 }
5562 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5563 amt += count * sizeof (asection *);
5564 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5565 if (m == NULL)
5566 goto error_return;
5567 m->next = NULL;
5568 m->p_type = PT_NOTE;
5569 m->count = count;
5570 while (count > 1)
5571 {
5572 m->sections[m->count - count--] = s;
5573 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5574 s = s->next;
5575 }
5576 m->sections[m->count - 1] = s;
5577 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5578 *pm = m;
5579 pm = &m->next;
5580 }
5581 if (s->flags & SEC_THREAD_LOCAL)
5582 {
5583 if (! tls_count)
5584 first_tls = s;
5585 tls_count++;
5586 }
5587 if (first_mbind == NULL
5588 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5589 first_mbind = s;
5590 }
5591
5592 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5593 if (tls_count > 0)
5594 {
5595 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5596 amt += tls_count * sizeof (asection *);
5597 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5598 if (m == NULL)
5599 goto error_return;
5600 m->next = NULL;
5601 m->p_type = PT_TLS;
5602 m->count = tls_count;
5603 /* Mandated PF_R. */
5604 m->p_flags = PF_R;
5605 m->p_flags_valid = 1;
5606 s = first_tls;
5607 for (i = 0; i < tls_count; ++i)
5608 {
5609 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5610 {
5611 _bfd_error_handler
5612 (_("%pB: TLS sections are not adjacent:"), abfd);
5613 s = first_tls;
5614 i = 0;
5615 while (i < tls_count)
5616 {
5617 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5618 {
5619 _bfd_error_handler (_(" TLS: %pA"), s);
5620 i++;
5621 }
5622 else
5623 _bfd_error_handler (_(" non-TLS: %pA"), s);
5624 s = s->next;
5625 }
5626 bfd_set_error (bfd_error_bad_value);
5627 goto error_return;
5628 }
5629 m->sections[i] = s;
5630 s = s->next;
5631 }
5632
5633 *pm = m;
5634 pm = &m->next;
5635 }
5636
5637 if (first_mbind
5638 && (abfd->flags & D_PAGED) != 0
5639 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5640 for (s = first_mbind; s != NULL; s = s->next)
5641 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5642 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5643 {
5644 /* Mandated PF_R. */
5645 unsigned long p_flags = PF_R;
5646 if ((s->flags & SEC_READONLY) == 0)
5647 p_flags |= PF_W;
5648 if ((s->flags & SEC_CODE) != 0)
5649 p_flags |= PF_X;
5650
5651 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5652 m = bfd_zalloc (abfd, amt);
5653 if (m == NULL)
5654 goto error_return;
5655 m->next = NULL;
5656 m->p_type = (PT_GNU_MBIND_LO
5657 + elf_section_data (s)->this_hdr.sh_info);
5658 m->count = 1;
5659 m->p_flags_valid = 1;
5660 m->sections[0] = s;
5661 m->p_flags = p_flags;
5662
5663 *pm = m;
5664 pm = &m->next;
5665 }
5666
5667 s = bfd_get_section_by_name (abfd,
5668 NOTE_GNU_PROPERTY_SECTION_NAME);
5669 if (s != NULL && s->size != 0)
5670 {
5671 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5672 m = bfd_zalloc (abfd, amt);
5673 if (m == NULL)
5674 goto error_return;
5675 m->next = NULL;
5676 m->p_type = PT_GNU_PROPERTY;
5677 m->count = 1;
5678 m->p_flags_valid = 1;
5679 m->sections[0] = s;
5680 m->p_flags = PF_R;
5681 *pm = m;
5682 pm = &m->next;
5683 }
5684
5685 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5686 segment. */
5687 eh_frame_hdr = elf_eh_frame_hdr (info);
5688 if (eh_frame_hdr != NULL
5689 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5690 {
5691 amt = sizeof (struct elf_segment_map);
5692 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5693 if (m == NULL)
5694 goto error_return;
5695 m->next = NULL;
5696 m->p_type = PT_GNU_EH_FRAME;
5697 m->count = 1;
5698 m->sections[0] = eh_frame_hdr->output_section;
5699
5700 *pm = m;
5701 pm = &m->next;
5702 }
5703
5704 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5705 segment. */
5706 sframe = elf_sframe (abfd);
5707 if (sframe != NULL
5708 && (sframe->output_section->flags & SEC_LOAD) != 0
5709 && sframe->size != 0)
5710 {
5711 amt = sizeof (struct elf_segment_map);
5712 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5713 if (m == NULL)
5714 goto error_return;
5715 m->next = NULL;
5716 m->p_type = PT_GNU_SFRAME;
5717 m->count = 1;
5718 m->sections[0] = sframe->output_section;
5719
5720 *pm = m;
5721 pm = &m->next;
5722 }
5723
5724 if (elf_stack_flags (abfd))
5725 {
5726 amt = sizeof (struct elf_segment_map);
5727 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5728 if (m == NULL)
5729 goto error_return;
5730 m->next = NULL;
5731 m->p_type = PT_GNU_STACK;
5732 m->p_flags = elf_stack_flags (abfd);
5733 m->p_align = bed->stack_align;
5734 m->p_flags_valid = 1;
5735 m->p_align_valid = m->p_align != 0;
5736 if (info->stacksize > 0)
5737 {
5738 m->p_size = info->stacksize;
5739 m->p_size_valid = 1;
5740 }
5741
5742 *pm = m;
5743 pm = &m->next;
5744 }
5745
5746 if (info != NULL && info->relro)
5747 {
5748 for (m = mfirst; m != NULL; m = m->next)
5749 {
5750 if (m->p_type == PT_LOAD
5751 && m->count != 0
5752 && m->sections[0]->vma >= info->relro_start
5753 && m->sections[0]->vma < info->relro_end)
5754 {
5755 i = m->count;
5756 while (--i != (unsigned) -1)
5757 {
5758 if (m->sections[i]->size > 0
5759 && (m->sections[i]->flags & SEC_LOAD) != 0
5760 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5761 break;
5762 }
5763
5764 if (i != (unsigned) -1)
5765 break;
5766 }
5767 }
5768
5769 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5770 if (m != NULL)
5771 {
5772 amt = sizeof (struct elf_segment_map);
5773 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5774 if (m == NULL)
5775 goto error_return;
5776 m->next = NULL;
5777 m->p_type = PT_GNU_RELRO;
5778 *pm = m;
5779 pm = &m->next;
5780 }
5781 }
5782
5783 free (sections);
5784 elf_seg_map (abfd) = mfirst;
5785 }
5786
5787 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5788 return false;
5789
5790 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5791 ++count;
5792 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5793
5794 return true;
5795
5796 error_return:
5797 free (sections);
5798 return false;
5799 }
5800
5801 /* Sort sections by address. */
5802
5803 static int
5804 elf_sort_sections (const void *arg1, const void *arg2)
5805 {
5806 const asection *sec1 = *(const asection **) arg1;
5807 const asection *sec2 = *(const asection **) arg2;
5808 bfd_size_type size1, size2;
5809
5810 /* Sort by LMA first, since this is the address used to
5811 place the section into a segment. */
5812 if (sec1->lma < sec2->lma)
5813 return -1;
5814 else if (sec1->lma > sec2->lma)
5815 return 1;
5816
5817 /* Then sort by VMA. Normally the LMA and the VMA will be
5818 the same, and this will do nothing. */
5819 if (sec1->vma < sec2->vma)
5820 return -1;
5821 else if (sec1->vma > sec2->vma)
5822 return 1;
5823
5824 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5825
5826 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5827 && (x)->size != 0)
5828
5829 if (TOEND (sec1))
5830 {
5831 if (!TOEND (sec2))
5832 return 1;
5833 }
5834 else if (TOEND (sec2))
5835 return -1;
5836
5837 #undef TOEND
5838
5839 /* Sort by size, to put zero sized sections
5840 before others at the same address. */
5841
5842 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5843 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5844
5845 if (size1 < size2)
5846 return -1;
5847 if (size1 > size2)
5848 return 1;
5849
5850 return sec1->target_index - sec2->target_index;
5851 }
5852
5853 /* This qsort comparison functions sorts PT_LOAD segments first and
5854 by p_paddr, for assign_file_positions_for_load_sections. */
5855
5856 static int
5857 elf_sort_segments (const void *arg1, const void *arg2)
5858 {
5859 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5860 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5861
5862 if (m1->p_type != m2->p_type)
5863 {
5864 if (m1->p_type == PT_NULL)
5865 return 1;
5866 if (m2->p_type == PT_NULL)
5867 return -1;
5868 return m1->p_type < m2->p_type ? -1 : 1;
5869 }
5870 if (m1->includes_filehdr != m2->includes_filehdr)
5871 return m1->includes_filehdr ? -1 : 1;
5872 if (m1->no_sort_lma != m2->no_sort_lma)
5873 return m1->no_sort_lma ? -1 : 1;
5874 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5875 {
5876 bfd_vma lma1, lma2; /* Octets. */
5877 lma1 = 0;
5878 if (m1->p_paddr_valid)
5879 lma1 = m1->p_paddr;
5880 else if (m1->count != 0)
5881 {
5882 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5883 m1->sections[0]);
5884 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5885 }
5886 lma2 = 0;
5887 if (m2->p_paddr_valid)
5888 lma2 = m2->p_paddr;
5889 else if (m2->count != 0)
5890 {
5891 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5892 m2->sections[0]);
5893 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5894 }
5895 if (lma1 != lma2)
5896 return lma1 < lma2 ? -1 : 1;
5897 }
5898 if (m1->idx != m2->idx)
5899 return m1->idx < m2->idx ? -1 : 1;
5900 return 0;
5901 }
5902
5903 /* Ian Lance Taylor writes:
5904
5905 We shouldn't be using % with a negative signed number. That's just
5906 not good. We have to make sure either that the number is not
5907 negative, or that the number has an unsigned type. When the types
5908 are all the same size they wind up as unsigned. When file_ptr is a
5909 larger signed type, the arithmetic winds up as signed long long,
5910 which is wrong.
5911
5912 What we're trying to say here is something like ``increase OFF by
5913 the least amount that will cause it to be equal to the VMA modulo
5914 the page size.'' */
5915 /* In other words, something like:
5916
5917 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5918 off_offset = off % bed->maxpagesize;
5919 if (vma_offset < off_offset)
5920 adjustment = vma_offset + bed->maxpagesize - off_offset;
5921 else
5922 adjustment = vma_offset - off_offset;
5923
5924 which can be collapsed into the expression below. */
5925
5926 static file_ptr
5927 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5928 {
5929 /* PR binutils/16199: Handle an alignment of zero. */
5930 if (maxpagesize == 0)
5931 maxpagesize = 1;
5932 return ((vma - off) % maxpagesize);
5933 }
5934
5935 static void
5936 print_segment_map (const struct elf_segment_map *m)
5937 {
5938 unsigned int j;
5939 const char *pt = get_segment_type (m->p_type);
5940 char buf[32];
5941
5942 if (pt == NULL)
5943 {
5944 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5945 sprintf (buf, "LOPROC+%7.7x",
5946 (unsigned int) (m->p_type - PT_LOPROC));
5947 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5948 sprintf (buf, "LOOS+%7.7x",
5949 (unsigned int) (m->p_type - PT_LOOS));
5950 else
5951 snprintf (buf, sizeof (buf), "%8.8x",
5952 (unsigned int) m->p_type);
5953 pt = buf;
5954 }
5955 fflush (stdout);
5956 fprintf (stderr, "%s:", pt);
5957 for (j = 0; j < m->count; j++)
5958 fprintf (stderr, " %s", m->sections [j]->name);
5959 putc ('\n',stderr);
5960 fflush (stderr);
5961 }
5962
5963 /* Assign file positions to the sections based on the mapping from
5964 sections to segments. This function also sets up some fields in
5965 the file header. */
5966
5967 static bool
5968 assign_file_positions_for_load_sections (bfd *abfd,
5969 struct bfd_link_info *link_info)
5970 {
5971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5972 struct elf_segment_map *m;
5973 struct elf_segment_map *phdr_load_seg;
5974 Elf_Internal_Phdr *phdrs;
5975 Elf_Internal_Phdr *p;
5976 file_ptr off; /* Octets. */
5977 bfd_size_type maxpagesize;
5978 unsigned int alloc, actual;
5979 unsigned int i, j;
5980 struct elf_segment_map **sorted_seg_map;
5981 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5982
5983 if (link_info == NULL
5984 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5985 return false;
5986
5987 alloc = 0;
5988 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5989 m->idx = alloc++;
5990
5991 if (alloc)
5992 {
5993 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5994 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5995 }
5996 else
5997 {
5998 /* PR binutils/12467. */
5999 elf_elfheader (abfd)->e_phoff = 0;
6000 elf_elfheader (abfd)->e_phentsize = 0;
6001 }
6002
6003 elf_elfheader (abfd)->e_phnum = alloc;
6004
6005 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
6006 {
6007 actual = alloc;
6008 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
6009 }
6010 else
6011 {
6012 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6013 BFD_ASSERT (elf_program_header_size (abfd)
6014 == actual * bed->s->sizeof_phdr);
6015 BFD_ASSERT (actual >= alloc);
6016 }
6017
6018 if (alloc == 0)
6019 {
6020 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
6021 return true;
6022 }
6023
6024 /* We're writing the size in elf_program_header_size (abfd),
6025 see assign_file_positions_except_relocs, so make sure we have
6026 that amount allocated, with trailing space cleared.
6027 The variable alloc contains the computed need, while
6028 elf_program_header_size (abfd) contains the size used for the
6029 layout.
6030 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6031 where the layout is forced to according to a larger size in the
6032 last iterations for the testcase ld-elf/header. */
6033 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
6034 + alloc * sizeof (*sorted_seg_map)));
6035 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
6036 elf_tdata (abfd)->phdr = phdrs;
6037 if (phdrs == NULL)
6038 return false;
6039
6040 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
6041 {
6042 sorted_seg_map[j] = m;
6043 /* If elf_segment_map is not from map_sections_to_segments, the
6044 sections may not be correctly ordered. NOTE: sorting should
6045 not be done to the PT_NOTE section of a corefile, which may
6046 contain several pseudo-sections artificially created by bfd.
6047 Sorting these pseudo-sections breaks things badly. */
6048 if (m->count > 1
6049 && !(elf_elfheader (abfd)->e_type == ET_CORE
6050 && m->p_type == PT_NOTE))
6051 {
6052 for (i = 0; i < m->count; i++)
6053 m->sections[i]->target_index = i;
6054 qsort (m->sections, (size_t) m->count, sizeof (asection *),
6055 elf_sort_sections);
6056 }
6057 }
6058 if (alloc > 1)
6059 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
6060 elf_sort_segments);
6061
6062 maxpagesize = 1;
6063 if ((abfd->flags & D_PAGED) != 0)
6064 {
6065 if (link_info != NULL)
6066 maxpagesize = link_info->maxpagesize;
6067 else
6068 maxpagesize = bed->maxpagesize;
6069 }
6070
6071 /* Sections must map to file offsets past the ELF file header. */
6072 off = bed->s->sizeof_ehdr;
6073 /* And if one of the PT_LOAD headers doesn't include the program
6074 headers then we'll be mapping program headers in the usual
6075 position after the ELF file header. */
6076 phdr_load_seg = NULL;
6077 for (j = 0; j < alloc; j++)
6078 {
6079 m = sorted_seg_map[j];
6080 if (m->p_type != PT_LOAD)
6081 break;
6082 if (m->includes_phdrs)
6083 {
6084 phdr_load_seg = m;
6085 break;
6086 }
6087 }
6088 if (phdr_load_seg == NULL)
6089 off += actual * bed->s->sizeof_phdr;
6090
6091 for (j = 0; j < alloc; j++)
6092 {
6093 asection **secpp;
6094 bfd_vma off_adjust; /* Octets. */
6095 bool no_contents;
6096 bfd_size_type p_align;
6097 bool p_align_p;
6098
6099 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6100 number of sections with contents contributing to both p_filesz
6101 and p_memsz, followed by a number of sections with no contents
6102 that just contribute to p_memsz. In this loop, OFF tracks next
6103 available file offset for PT_LOAD and PT_NOTE segments. */
6104 m = sorted_seg_map[j];
6105 p = phdrs + m->idx;
6106 p->p_type = m->p_type;
6107 p->p_flags = m->p_flags;
6108 p_align = bed->p_align;
6109 p_align_p = false;
6110
6111 if (m->count == 0)
6112 p->p_vaddr = m->p_vaddr_offset * opb;
6113 else
6114 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6115
6116 if (m->p_paddr_valid)
6117 p->p_paddr = m->p_paddr;
6118 else if (m->count == 0)
6119 p->p_paddr = 0;
6120 else
6121 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6122
6123 if (p->p_type == PT_LOAD
6124 && (abfd->flags & D_PAGED) != 0)
6125 {
6126 /* p_align in demand paged PT_LOAD segments effectively stores
6127 the maximum page size. When copying an executable with
6128 objcopy, we set m->p_align from the input file. Use this
6129 value for maxpagesize rather than bed->maxpagesize, which
6130 may be different. Note that we use maxpagesize for PT_TLS
6131 segment alignment later in this function, so we are relying
6132 on at least one PT_LOAD segment appearing before a PT_TLS
6133 segment. */
6134 if (m->p_align_valid)
6135 maxpagesize = m->p_align;
6136 else if (p_align != 0
6137 && (link_info == NULL
6138 || !link_info->maxpagesize_is_set))
6139 /* Set p_align to the default p_align value while laying
6140 out segments aligning to the maximum page size or the
6141 largest section alignment. The run-time loader can
6142 align segments to the default p_align value or the
6143 maximum page size, depending on system page size. */
6144 p_align_p = true;
6145
6146 p->p_align = maxpagesize;
6147 }
6148 else if (m->p_align_valid)
6149 p->p_align = m->p_align;
6150 else if (m->count == 0)
6151 p->p_align = 1 << bed->s->log_file_align;
6152
6153 if (m == phdr_load_seg)
6154 {
6155 if (!m->includes_filehdr)
6156 p->p_offset = off;
6157 off += actual * bed->s->sizeof_phdr;
6158 }
6159
6160 no_contents = false;
6161 off_adjust = 0;
6162 if (p->p_type == PT_LOAD
6163 && m->count > 0)
6164 {
6165 bfd_size_type align; /* Bytes. */
6166 unsigned int align_power = 0;
6167
6168 if (m->p_align_valid)
6169 align = p->p_align;
6170 else
6171 {
6172 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6173 {
6174 unsigned int secalign;
6175
6176 secalign = bfd_section_alignment (*secpp);
6177 if (secalign > align_power)
6178 align_power = secalign;
6179 }
6180 align = (bfd_size_type) 1 << align_power;
6181 if (align < maxpagesize)
6182 {
6183 /* If a section requires alignment higher than the
6184 default p_align value, don't set p_align to the
6185 default p_align value. */
6186 if (align > p_align)
6187 p_align_p = false;
6188 align = maxpagesize;
6189 }
6190 else
6191 {
6192 /* If a section requires alignment higher than the
6193 maximum page size, set p_align to the section
6194 alignment. */
6195 p_align_p = true;
6196 p_align = align;
6197 }
6198 }
6199
6200 for (i = 0; i < m->count; i++)
6201 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6202 /* If we aren't making room for this section, then
6203 it must be SHT_NOBITS regardless of what we've
6204 set via struct bfd_elf_special_section. */
6205 elf_section_type (m->sections[i]) = SHT_NOBITS;
6206
6207 /* Find out whether this segment contains any loadable
6208 sections. */
6209 no_contents = true;
6210 for (i = 0; i < m->count; i++)
6211 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6212 {
6213 no_contents = false;
6214 break;
6215 }
6216
6217 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6218
6219 /* Broken hardware and/or kernel require that files do not
6220 map the same page with different permissions on some hppa
6221 processors. */
6222 if (j != 0
6223 && (abfd->flags & D_PAGED) != 0
6224 && bed->no_page_alias
6225 && (off & (maxpagesize - 1)) != 0
6226 && ((off & -maxpagesize)
6227 == ((off + off_adjust) & -maxpagesize)))
6228 off_adjust += maxpagesize;
6229 off += off_adjust;
6230 if (no_contents)
6231 {
6232 /* We shouldn't need to align the segment on disk since
6233 the segment doesn't need file space, but the gABI
6234 arguably requires the alignment and glibc ld.so
6235 checks it. So to comply with the alignment
6236 requirement but not waste file space, we adjust
6237 p_offset for just this segment. (OFF_ADJUST is
6238 subtracted from OFF later.) This may put p_offset
6239 past the end of file, but that shouldn't matter. */
6240 }
6241 else
6242 off_adjust = 0;
6243 }
6244 /* Make sure the .dynamic section is the first section in the
6245 PT_DYNAMIC segment. */
6246 else if (p->p_type == PT_DYNAMIC
6247 && m->count > 1
6248 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6249 {
6250 _bfd_error_handler
6251 (_("%pB: The first section in the PT_DYNAMIC segment"
6252 " is not the .dynamic section"),
6253 abfd);
6254 bfd_set_error (bfd_error_bad_value);
6255 return false;
6256 }
6257 /* Set the note section type to SHT_NOTE. */
6258 else if (p->p_type == PT_NOTE)
6259 for (i = 0; i < m->count; i++)
6260 elf_section_type (m->sections[i]) = SHT_NOTE;
6261
6262 if (m->includes_filehdr)
6263 {
6264 if (!m->p_flags_valid)
6265 p->p_flags |= PF_R;
6266 p->p_filesz = bed->s->sizeof_ehdr;
6267 p->p_memsz = bed->s->sizeof_ehdr;
6268 if (p->p_type == PT_LOAD)
6269 {
6270 if (m->count > 0)
6271 {
6272 if (p->p_vaddr < (bfd_vma) off
6273 || (!m->p_paddr_valid
6274 && p->p_paddr < (bfd_vma) off))
6275 {
6276 _bfd_error_handler
6277 (_("%pB: not enough room for program headers,"
6278 " try linking with -N"),
6279 abfd);
6280 bfd_set_error (bfd_error_bad_value);
6281 return false;
6282 }
6283 p->p_vaddr -= off;
6284 if (!m->p_paddr_valid)
6285 p->p_paddr -= off;
6286 }
6287 }
6288 else if (sorted_seg_map[0]->includes_filehdr)
6289 {
6290 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6291 p->p_vaddr = filehdr->p_vaddr;
6292 if (!m->p_paddr_valid)
6293 p->p_paddr = filehdr->p_paddr;
6294 }
6295 }
6296
6297 if (m->includes_phdrs)
6298 {
6299 if (!m->p_flags_valid)
6300 p->p_flags |= PF_R;
6301 p->p_filesz += actual * bed->s->sizeof_phdr;
6302 p->p_memsz += actual * bed->s->sizeof_phdr;
6303 if (!m->includes_filehdr)
6304 {
6305 if (p->p_type == PT_LOAD)
6306 {
6307 elf_elfheader (abfd)->e_phoff = p->p_offset;
6308 if (m->count > 0)
6309 {
6310 p->p_vaddr -= off - p->p_offset;
6311 if (!m->p_paddr_valid)
6312 p->p_paddr -= off - p->p_offset;
6313 }
6314 }
6315 else if (phdr_load_seg != NULL)
6316 {
6317 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6318 bfd_vma phdr_off = 0; /* Octets. */
6319 if (phdr_load_seg->includes_filehdr)
6320 phdr_off = bed->s->sizeof_ehdr;
6321 p->p_vaddr = phdr->p_vaddr + phdr_off;
6322 if (!m->p_paddr_valid)
6323 p->p_paddr = phdr->p_paddr + phdr_off;
6324 p->p_offset = phdr->p_offset + phdr_off;
6325 }
6326 else
6327 p->p_offset = bed->s->sizeof_ehdr;
6328 }
6329 }
6330
6331 if (p->p_type == PT_LOAD
6332 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6333 {
6334 if (!m->includes_filehdr && !m->includes_phdrs)
6335 {
6336 p->p_offset = off;
6337 if (no_contents)
6338 {
6339 /* Put meaningless p_offset for PT_LOAD segments
6340 without file contents somewhere within the first
6341 page, in an attempt to not point past EOF. */
6342 bfd_size_type align = maxpagesize;
6343 if (align < p->p_align)
6344 align = p->p_align;
6345 if (align < 1)
6346 align = 1;
6347 p->p_offset = off % align;
6348 }
6349 }
6350 else
6351 {
6352 file_ptr adjust; /* Octets. */
6353
6354 adjust = off - (p->p_offset + p->p_filesz);
6355 if (!no_contents)
6356 p->p_filesz += adjust;
6357 p->p_memsz += adjust;
6358 }
6359 }
6360
6361 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6362 maps. Set filepos for sections in PT_LOAD segments, and in
6363 core files, for sections in PT_NOTE segments.
6364 assign_file_positions_for_non_load_sections will set filepos
6365 for other sections and update p_filesz for other segments. */
6366 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6367 {
6368 asection *sec;
6369 bfd_size_type align;
6370 Elf_Internal_Shdr *this_hdr;
6371
6372 sec = *secpp;
6373 this_hdr = &elf_section_data (sec)->this_hdr;
6374 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6375
6376 if ((p->p_type == PT_LOAD
6377 || p->p_type == PT_TLS)
6378 && (this_hdr->sh_type != SHT_NOBITS
6379 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6380 && ((this_hdr->sh_flags & SHF_TLS) == 0
6381 || p->p_type == PT_TLS))))
6382 {
6383 bfd_vma p_start = p->p_paddr; /* Octets. */
6384 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6385 bfd_vma s_start = sec->lma * opb; /* Octets. */
6386 bfd_vma adjust = s_start - p_end; /* Octets. */
6387
6388 if (adjust != 0
6389 && (s_start < p_end
6390 || p_end < p_start))
6391 {
6392 _bfd_error_handler
6393 /* xgettext:c-format */
6394 (_("%pB: section %pA lma %#" PRIx64
6395 " adjusted to %#" PRIx64),
6396 abfd, sec, (uint64_t) s_start / opb,
6397 (uint64_t) p_end / opb);
6398 adjust = 0;
6399 sec->lma = p_end / opb;
6400 }
6401 p->p_memsz += adjust;
6402
6403 if (p->p_type == PT_LOAD)
6404 {
6405 if (this_hdr->sh_type != SHT_NOBITS)
6406 {
6407 off_adjust = 0;
6408 if (p->p_filesz + adjust < p->p_memsz)
6409 {
6410 /* We have a PROGBITS section following NOBITS ones.
6411 Allocate file space for the NOBITS section(s).
6412 We don't need to write out the zeros, posix
6413 fseek past the end of data already written
6414 followed by a write at that location is
6415 guaranteed to result in zeros being read
6416 from the gap. */
6417 adjust = p->p_memsz - p->p_filesz;
6418 }
6419 }
6420 /* We only adjust sh_offset in SHT_NOBITS sections
6421 as would seem proper for their address when the
6422 section is first in the segment. sh_offset
6423 doesn't really have any significance for
6424 SHT_NOBITS anyway, apart from a notional position
6425 relative to other sections. Historically we
6426 didn't bother with adjusting sh_offset and some
6427 programs depend on it not being adjusted. See
6428 pr12921 and pr25662. */
6429 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6430 {
6431 off += adjust;
6432 if (this_hdr->sh_type == SHT_NOBITS)
6433 off_adjust += adjust;
6434 }
6435 }
6436 if (this_hdr->sh_type != SHT_NOBITS)
6437 p->p_filesz += adjust;
6438 }
6439
6440 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6441 {
6442 /* The section at i == 0 is the one that actually contains
6443 everything. */
6444 if (i == 0)
6445 {
6446 this_hdr->sh_offset = sec->filepos = off;
6447 off += this_hdr->sh_size;
6448 p->p_filesz = this_hdr->sh_size;
6449 p->p_memsz = 0;
6450 p->p_align = 1;
6451 }
6452 else
6453 {
6454 /* The rest are fake sections that shouldn't be written. */
6455 sec->filepos = 0;
6456 sec->size = 0;
6457 sec->flags = 0;
6458 continue;
6459 }
6460 }
6461 else
6462 {
6463 if (p->p_type == PT_LOAD)
6464 {
6465 this_hdr->sh_offset = sec->filepos = off;
6466 if (this_hdr->sh_type != SHT_NOBITS)
6467 off += this_hdr->sh_size;
6468 }
6469 else if (this_hdr->sh_type == SHT_NOBITS
6470 && (this_hdr->sh_flags & SHF_TLS) != 0
6471 && this_hdr->sh_offset == 0)
6472 {
6473 /* This is a .tbss section that didn't get a PT_LOAD.
6474 (See _bfd_elf_map_sections_to_segments "Create a
6475 final PT_LOAD".) Set sh_offset to the value it
6476 would have if we had created a zero p_filesz and
6477 p_memsz PT_LOAD header for the section. This
6478 also makes the PT_TLS header have the same
6479 p_offset value. */
6480 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6481 off, align);
6482 this_hdr->sh_offset = sec->filepos = off + adjust;
6483 }
6484
6485 if (this_hdr->sh_type != SHT_NOBITS)
6486 {
6487 p->p_filesz += this_hdr->sh_size;
6488 /* A load section without SHF_ALLOC is something like
6489 a note section in a PT_NOTE segment. These take
6490 file space but are not loaded into memory. */
6491 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6492 p->p_memsz += this_hdr->sh_size;
6493 }
6494 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6495 {
6496 if (p->p_type == PT_TLS)
6497 p->p_memsz += this_hdr->sh_size;
6498
6499 /* .tbss is special. It doesn't contribute to p_memsz of
6500 normal segments. */
6501 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6502 p->p_memsz += this_hdr->sh_size;
6503 }
6504
6505 if (align > p->p_align
6506 && !m->p_align_valid
6507 && (p->p_type != PT_LOAD
6508 || (abfd->flags & D_PAGED) == 0))
6509 p->p_align = align;
6510 }
6511
6512 if (!m->p_flags_valid)
6513 {
6514 p->p_flags |= PF_R;
6515 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6516 p->p_flags |= PF_X;
6517 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6518 p->p_flags |= PF_W;
6519 }
6520 }
6521
6522 off -= off_adjust;
6523
6524 /* PR ld/20815 - Check that the program header segment, if
6525 present, will be loaded into memory. */
6526 if (p->p_type == PT_PHDR
6527 && phdr_load_seg == NULL
6528 && !(bed->elf_backend_allow_non_load_phdr != NULL
6529 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6530 {
6531 /* The fix for this error is usually to edit the linker script being
6532 used and set up the program headers manually. Either that or
6533 leave room for the headers at the start of the SECTIONS. */
6534 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6535 " by LOAD segment"),
6536 abfd);
6537 if (link_info == NULL)
6538 return false;
6539 /* Arrange for the linker to exit with an error, deleting
6540 the output file unless --noinhibit-exec is given. */
6541 link_info->callbacks->info ("%X");
6542 }
6543
6544 /* Check that all sections are in a PT_LOAD segment.
6545 Don't check funky gdb generated core files. */
6546 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6547 {
6548 bool check_vma = true;
6549
6550 for (i = 1; i < m->count; i++)
6551 if (m->sections[i]->vma == m->sections[i - 1]->vma
6552 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6553 ->this_hdr), p) != 0
6554 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6555 ->this_hdr), p) != 0)
6556 {
6557 /* Looks like we have overlays packed into the segment. */
6558 check_vma = false;
6559 break;
6560 }
6561
6562 for (i = 0; i < m->count; i++)
6563 {
6564 Elf_Internal_Shdr *this_hdr;
6565 asection *sec;
6566
6567 sec = m->sections[i];
6568 this_hdr = &(elf_section_data(sec)->this_hdr);
6569 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6570 && !ELF_TBSS_SPECIAL (this_hdr, p))
6571 {
6572 _bfd_error_handler
6573 /* xgettext:c-format */
6574 (_("%pB: section `%pA' can't be allocated in segment %d"),
6575 abfd, sec, j);
6576 print_segment_map (m);
6577 }
6578 }
6579
6580 if (p_align_p)
6581 p->p_align = p_align;
6582 }
6583 }
6584
6585 elf_next_file_pos (abfd) = off;
6586
6587 if (link_info != NULL
6588 && phdr_load_seg != NULL
6589 && phdr_load_seg->includes_filehdr)
6590 {
6591 /* There is a segment that contains both the file headers and the
6592 program headers, so provide a symbol __ehdr_start pointing there.
6593 A program can use this to examine itself robustly. */
6594
6595 struct elf_link_hash_entry *hash
6596 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6597 false, false, true);
6598 /* If the symbol was referenced and not defined, define it. */
6599 if (hash != NULL
6600 && (hash->root.type == bfd_link_hash_new
6601 || hash->root.type == bfd_link_hash_undefined
6602 || hash->root.type == bfd_link_hash_undefweak
6603 || hash->root.type == bfd_link_hash_common))
6604 {
6605 asection *s = NULL;
6606 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6607
6608 if (phdr_load_seg->count != 0)
6609 /* The segment contains sections, so use the first one. */
6610 s = phdr_load_seg->sections[0];
6611 else
6612 /* Use the first (i.e. lowest-addressed) section in any segment. */
6613 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6614 if (m->p_type == PT_LOAD && m->count != 0)
6615 {
6616 s = m->sections[0];
6617 break;
6618 }
6619
6620 if (s != NULL)
6621 {
6622 hash->root.u.def.value = filehdr_vaddr - s->vma;
6623 hash->root.u.def.section = s;
6624 }
6625 else
6626 {
6627 hash->root.u.def.value = filehdr_vaddr;
6628 hash->root.u.def.section = bfd_abs_section_ptr;
6629 }
6630
6631 hash->root.type = bfd_link_hash_defined;
6632 hash->def_regular = 1;
6633 hash->non_elf = 0;
6634 }
6635 }
6636
6637 return true;
6638 }
6639
6640 /* Determine if a bfd is a debuginfo file. Unfortunately there
6641 is no defined method for detecting such files, so we have to
6642 use heuristics instead. */
6643
6644 bool
6645 is_debuginfo_file (bfd *abfd)
6646 {
6647 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6648 return false;
6649
6650 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6651 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6652 Elf_Internal_Shdr **headerp;
6653
6654 for (headerp = start_headers; headerp < end_headers; headerp ++)
6655 {
6656 Elf_Internal_Shdr *header = * headerp;
6657
6658 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6659 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6660 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6661 && header->sh_type != SHT_NOBITS
6662 && header->sh_type != SHT_NOTE)
6663 return false;
6664 }
6665
6666 return true;
6667 }
6668
6669 /* Assign file positions for other sections, except for compressed debug
6670 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6671
6672 static bool
6673 assign_file_positions_for_non_load_sections (bfd *abfd,
6674 struct bfd_link_info *link_info)
6675 {
6676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6677 Elf_Internal_Shdr **i_shdrpp;
6678 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6679 Elf_Internal_Phdr *phdrs;
6680 Elf_Internal_Phdr *p;
6681 struct elf_segment_map *m;
6682 file_ptr off;
6683 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6684 bfd_vma maxpagesize;
6685
6686 if (link_info != NULL)
6687 maxpagesize = link_info->maxpagesize;
6688 else
6689 maxpagesize = bed->maxpagesize;
6690 i_shdrpp = elf_elfsections (abfd);
6691 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6692 off = elf_next_file_pos (abfd);
6693 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6694 {
6695 Elf_Internal_Shdr *hdr;
6696 bfd_vma align;
6697
6698 hdr = *hdrpp;
6699 if (hdr->bfd_section != NULL
6700 && (hdr->bfd_section->filepos != 0
6701 || (hdr->sh_type == SHT_NOBITS
6702 && hdr->contents == NULL)))
6703 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6704 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6705 {
6706 if (hdr->sh_size != 0
6707 /* PR 24717 - debuginfo files are known to be not strictly
6708 compliant with the ELF standard. In particular they often
6709 have .note.gnu.property sections that are outside of any
6710 loadable segment. This is not a problem for such files,
6711 so do not warn about them. */
6712 && ! is_debuginfo_file (abfd))
6713 _bfd_error_handler
6714 /* xgettext:c-format */
6715 (_("%pB: warning: allocated section `%s' not in segment"),
6716 abfd,
6717 (hdr->bfd_section == NULL
6718 ? "*unknown*"
6719 : hdr->bfd_section->name));
6720 /* We don't need to page align empty sections. */
6721 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6722 align = maxpagesize;
6723 else
6724 align = hdr->sh_addralign & -hdr->sh_addralign;
6725 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6726 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6727 false);
6728 }
6729 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6730 && hdr->bfd_section == NULL)
6731 /* We don't know the offset of these sections yet:
6732 their size has not been decided. */
6733 || (abfd->is_linker_output
6734 && hdr->bfd_section != NULL
6735 && (hdr->sh_name == -1u
6736 || bfd_section_is_ctf (hdr->bfd_section)))
6737 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6738 || (elf_symtab_shndx_list (abfd) != NULL
6739 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6740 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6741 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6742 hdr->sh_offset = -1;
6743 else
6744 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6745 }
6746 elf_next_file_pos (abfd) = off;
6747
6748 /* Now that we have set the section file positions, we can set up
6749 the file positions for the non PT_LOAD segments. */
6750 phdrs = elf_tdata (abfd)->phdr;
6751 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6752 {
6753 if (p->p_type == PT_GNU_RELRO)
6754 {
6755 bfd_vma start, end; /* Bytes. */
6756 bool ok;
6757
6758 if (link_info != NULL)
6759 {
6760 /* During linking the range of the RELRO segment is passed
6761 in link_info. Note that there may be padding between
6762 relro_start and the first RELRO section. */
6763 start = link_info->relro_start;
6764 end = link_info->relro_end;
6765 }
6766 else if (m->count != 0)
6767 {
6768 if (!m->p_size_valid)
6769 abort ();
6770 start = m->sections[0]->vma;
6771 end = start + m->p_size / opb;
6772 }
6773 else
6774 {
6775 start = 0;
6776 end = 0;
6777 }
6778
6779 ok = false;
6780 if (start < end)
6781 {
6782 struct elf_segment_map *lm;
6783 const Elf_Internal_Phdr *lp;
6784 unsigned int i;
6785
6786 /* Find a LOAD segment containing a section in the RELRO
6787 segment. */
6788 for (lm = elf_seg_map (abfd), lp = phdrs;
6789 lm != NULL;
6790 lm = lm->next, lp++)
6791 {
6792 if (lp->p_type == PT_LOAD
6793 && lm->count != 0
6794 && (lm->sections[lm->count - 1]->vma
6795 + (!IS_TBSS (lm->sections[lm->count - 1])
6796 ? lm->sections[lm->count - 1]->size / opb
6797 : 0)) > start
6798 && lm->sections[0]->vma < end)
6799 break;
6800 }
6801
6802 if (lm != NULL)
6803 {
6804 /* Find the section starting the RELRO segment. */
6805 for (i = 0; i < lm->count; i++)
6806 {
6807 asection *s = lm->sections[i];
6808 if (s->vma >= start
6809 && s->vma < end
6810 && s->size != 0)
6811 break;
6812 }
6813
6814 if (i < lm->count)
6815 {
6816 p->p_vaddr = lm->sections[i]->vma * opb;
6817 p->p_paddr = lm->sections[i]->lma * opb;
6818 p->p_offset = lm->sections[i]->filepos;
6819 p->p_memsz = end * opb - p->p_vaddr;
6820 p->p_filesz = p->p_memsz;
6821
6822 /* The RELRO segment typically ends a few bytes
6823 into .got.plt but other layouts are possible.
6824 In cases where the end does not match any
6825 loaded section (for instance is in file
6826 padding), trim p_filesz back to correspond to
6827 the end of loaded section contents. */
6828 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6829 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6830
6831 /* Preserve the alignment and flags if they are
6832 valid. The gold linker generates RW/4 for
6833 the PT_GNU_RELRO section. It is better for
6834 objcopy/strip to honor these attributes
6835 otherwise gdb will choke when using separate
6836 debug files. */
6837 if (!m->p_align_valid)
6838 p->p_align = 1;
6839 if (!m->p_flags_valid)
6840 p->p_flags = PF_R;
6841 ok = true;
6842 }
6843 }
6844 }
6845
6846 if (!ok)
6847 {
6848 if (link_info != NULL)
6849 _bfd_error_handler
6850 (_("%pB: warning: unable to allocate any sections"
6851 " to PT_GNU_RELRO segment"),
6852 abfd);
6853 memset (p, 0, sizeof *p);
6854 }
6855 }
6856 else if (p->p_type == PT_GNU_STACK)
6857 {
6858 if (m->p_size_valid)
6859 p->p_memsz = m->p_size;
6860 }
6861 else if (m->count != 0)
6862 {
6863 unsigned int i;
6864
6865 if (p->p_type != PT_LOAD
6866 && (p->p_type != PT_NOTE
6867 || bfd_get_format (abfd) != bfd_core))
6868 {
6869 /* A user specified segment layout may include a PHDR
6870 segment that overlaps with a LOAD segment... */
6871 if (p->p_type == PT_PHDR)
6872 {
6873 m->count = 0;
6874 continue;
6875 }
6876
6877 if (m->includes_filehdr || m->includes_phdrs)
6878 {
6879 /* PR 17512: file: 2195325e. */
6880 _bfd_error_handler
6881 (_("%pB: error: non-load segment %d includes file header "
6882 "and/or program header"),
6883 abfd, (int) (p - phdrs));
6884 return false;
6885 }
6886
6887 p->p_filesz = 0;
6888 p->p_offset = m->sections[0]->filepos;
6889 for (i = m->count; i-- != 0;)
6890 {
6891 asection *sect = m->sections[i];
6892 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6893 if (hdr->sh_type != SHT_NOBITS)
6894 {
6895 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6896 /* NB: p_memsz of the loadable PT_NOTE segment
6897 should be the same as p_filesz. */
6898 if (p->p_type == PT_NOTE
6899 && (hdr->sh_flags & SHF_ALLOC) != 0)
6900 p->p_memsz = p->p_filesz;
6901 break;
6902 }
6903 }
6904 }
6905 }
6906 }
6907
6908 return true;
6909 }
6910
6911 static elf_section_list *
6912 find_section_in_list (unsigned int i, elf_section_list * list)
6913 {
6914 for (;list != NULL; list = list->next)
6915 if (list->ndx == i)
6916 break;
6917 return list;
6918 }
6919
6920 /* Work out the file positions of all the sections. This is called by
6921 _bfd_elf_compute_section_file_positions. All the section sizes and
6922 VMAs must be known before this is called.
6923
6924 Reloc sections come in two flavours: Those processed specially as
6925 "side-channel" data attached to a section to which they apply, and
6926 those that bfd doesn't process as relocations. The latter sort are
6927 stored in a normal bfd section by bfd_section_from_shdr. We don't
6928 consider the former sort here, unless they form part of the loadable
6929 image. Reloc sections not assigned here (and compressed debugging
6930 sections and CTF sections which nothing else in the file can rely
6931 upon) will be handled later by assign_file_positions_for_relocs.
6932
6933 We also don't set the positions of the .symtab and .strtab here. */
6934
6935 static bool
6936 assign_file_positions_except_relocs (bfd *abfd,
6937 struct bfd_link_info *link_info)
6938 {
6939 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6940 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6941 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6942 unsigned int alloc;
6943
6944 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6945 && bfd_get_format (abfd) != bfd_core)
6946 {
6947 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6948 unsigned int num_sec = elf_numsections (abfd);
6949 Elf_Internal_Shdr **hdrpp;
6950 unsigned int i;
6951 file_ptr off;
6952
6953 /* Start after the ELF header. */
6954 off = i_ehdrp->e_ehsize;
6955
6956 /* We are not creating an executable, which means that we are
6957 not creating a program header, and that the actual order of
6958 the sections in the file is unimportant. */
6959 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6960 {
6961 Elf_Internal_Shdr *hdr;
6962
6963 hdr = *hdrpp;
6964 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6965 && hdr->bfd_section == NULL)
6966 /* Do not assign offsets for these sections yet: we don't know
6967 their sizes. */
6968 || (abfd->is_linker_output
6969 && hdr->bfd_section != NULL
6970 && (hdr->sh_name == -1u
6971 || bfd_section_is_ctf (hdr->bfd_section)))
6972 || i == elf_onesymtab (abfd)
6973 || (elf_symtab_shndx_list (abfd) != NULL
6974 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6975 || i == elf_strtab_sec (abfd)
6976 || i == elf_shstrtab_sec (abfd))
6977 {
6978 hdr->sh_offset = -1;
6979 }
6980 else
6981 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6982 }
6983
6984 elf_next_file_pos (abfd) = off;
6985 elf_program_header_size (abfd) = 0;
6986 }
6987 else
6988 {
6989 /* Assign file positions for the loaded sections based on the
6990 assignment of sections to segments. */
6991 if (!assign_file_positions_for_load_sections (abfd, link_info))
6992 return false;
6993
6994 /* And for non-load sections. */
6995 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6996 return false;
6997 }
6998
6999 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
7000 return false;
7001
7002 /* Write out the program headers. */
7003 alloc = i_ehdrp->e_phnum;
7004 if (alloc != 0)
7005 {
7006 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
7007 {
7008 /* Memory resident segments with non-zero size and RWX
7009 permissions are a security risk, so we generate a warning
7010 here if we are creating any. */
7011 unsigned int i;
7012
7013 for (i = 0; i < alloc; i++)
7014 {
7015 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
7016
7017 if (phdr->p_memsz == 0)
7018 continue;
7019
7020 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
7021 _bfd_error_handler (_("warning: %pB has a TLS segment"
7022 " with execute permission"),
7023 abfd);
7024 else if (phdr->p_type == PT_LOAD
7025 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
7026 == (PF_R | PF_W | PF_X)))
7027 _bfd_error_handler (_("warning: %pB has a LOAD segment"
7028 " with RWX permissions"),
7029 abfd);
7030 }
7031 }
7032
7033 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7034 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7035 return false;
7036 }
7037
7038 return true;
7039 }
7040
7041 bool
7042 _bfd_elf_init_file_header (bfd *abfd,
7043 struct bfd_link_info *info ATTRIBUTE_UNUSED)
7044 {
7045 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
7046 struct elf_strtab_hash *shstrtab;
7047 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7048
7049 i_ehdrp = elf_elfheader (abfd);
7050
7051 shstrtab = _bfd_elf_strtab_init ();
7052 if (shstrtab == NULL)
7053 return false;
7054
7055 elf_shstrtab (abfd) = shstrtab;
7056
7057 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7058 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7059 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7060 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7061
7062 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7063 i_ehdrp->e_ident[EI_DATA] =
7064 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7065 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7066
7067 if ((abfd->flags & DYNAMIC) != 0)
7068 i_ehdrp->e_type = ET_DYN;
7069 else if ((abfd->flags & EXEC_P) != 0)
7070 i_ehdrp->e_type = ET_EXEC;
7071 else if (bfd_get_format (abfd) == bfd_core)
7072 i_ehdrp->e_type = ET_CORE;
7073 else
7074 i_ehdrp->e_type = ET_REL;
7075
7076 switch (bfd_get_arch (abfd))
7077 {
7078 case bfd_arch_unknown:
7079 i_ehdrp->e_machine = EM_NONE;
7080 break;
7081
7082 /* There used to be a long list of cases here, each one setting
7083 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7084 in the corresponding bfd definition. To avoid duplication,
7085 the switch was removed. Machines that need special handling
7086 can generally do it in elf_backend_final_write_processing(),
7087 unless they need the information earlier than the final write.
7088 Such need can generally be supplied by replacing the tests for
7089 e_machine with the conditions used to determine it. */
7090 default:
7091 i_ehdrp->e_machine = bed->elf_machine_code;
7092 }
7093
7094 i_ehdrp->e_version = bed->s->ev_current;
7095 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7096
7097 /* No program header, for now. */
7098 i_ehdrp->e_phoff = 0;
7099 i_ehdrp->e_phentsize = 0;
7100 i_ehdrp->e_phnum = 0;
7101
7102 /* Each bfd section is section header entry. */
7103 i_ehdrp->e_entry = bfd_get_start_address (abfd);
7104 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7105
7106 elf_tdata (abfd)->symtab_hdr.sh_name =
7107 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7108 elf_tdata (abfd)->strtab_hdr.sh_name =
7109 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7110 elf_tdata (abfd)->shstrtab_hdr.sh_name =
7111 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7112 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7113 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7114 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7115 return false;
7116
7117 return true;
7118 }
7119
7120 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7121
7122 FIXME: We used to have code here to sort the PT_LOAD segments into
7123 ascending order, as per the ELF spec. But this breaks some programs,
7124 including the Linux kernel. But really either the spec should be
7125 changed or the programs updated. */
7126
7127 bool
7128 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7129 {
7130 if (link_info != NULL && bfd_link_pie (link_info))
7131 {
7132 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7133 unsigned int num_segments = i_ehdrp->e_phnum;
7134 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7135 Elf_Internal_Phdr *segment = tdata->phdr;
7136 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7137
7138 /* Find the lowest p_vaddr in PT_LOAD segments. */
7139 bfd_vma p_vaddr = (bfd_vma) -1;
7140 for (; segment < end_segment; segment++)
7141 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7142 p_vaddr = segment->p_vaddr;
7143
7144 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7145 segments is non-zero. */
7146 if (p_vaddr)
7147 i_ehdrp->e_type = ET_EXEC;
7148 }
7149 return true;
7150 }
7151
7152 /* Assign file positions for all the reloc sections which are not part
7153 of the loadable file image, and the file position of section headers. */
7154
7155 static bool
7156 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7157 {
7158 file_ptr off;
7159 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7160 Elf_Internal_Shdr *shdrp;
7161 Elf_Internal_Ehdr *i_ehdrp;
7162 const struct elf_backend_data *bed;
7163
7164 /* Skip non-load sections without section header. */
7165 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7166 return true;
7167
7168 off = elf_next_file_pos (abfd);
7169
7170 shdrpp = elf_elfsections (abfd);
7171 end_shdrpp = shdrpp + elf_numsections (abfd);
7172 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7173 {
7174 shdrp = *shdrpp;
7175 if (shdrp->sh_offset == -1)
7176 {
7177 asection *sec = shdrp->bfd_section;
7178 if (sec == NULL
7179 || shdrp->sh_type == SHT_REL
7180 || shdrp->sh_type == SHT_RELA)
7181 ;
7182 else if (bfd_section_is_ctf (sec))
7183 {
7184 /* Update section size and contents. */
7185 shdrp->sh_size = sec->size;
7186 shdrp->contents = sec->contents;
7187 }
7188 else if (shdrp->sh_name == -1u)
7189 {
7190 const char *name = sec->name;
7191 struct bfd_elf_section_data *d;
7192
7193 /* Compress DWARF debug sections. */
7194 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7195 return false;
7196
7197 if (sec->compress_status == COMPRESS_SECTION_DONE
7198 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7199 && name[1] == 'd')
7200 {
7201 /* If section is compressed with zlib-gnu, convert
7202 section name from .debug_* to .zdebug_*. */
7203 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7204 if (new_name == NULL)
7205 return false;
7206 name = new_name;
7207 }
7208 /* Add section name to section name section. */
7209 shdrp->sh_name
7210 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7211 name, false);
7212 d = elf_section_data (sec);
7213
7214 /* Add reloc section name to section name section. */
7215 if (d->rel.hdr
7216 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7217 name, false))
7218 return false;
7219 if (d->rela.hdr
7220 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7221 name, true))
7222 return false;
7223
7224 /* Update section size and contents. */
7225 shdrp->sh_size = sec->size;
7226 shdrp->contents = sec->contents;
7227 sec->contents = NULL;
7228 }
7229
7230 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7231 }
7232 }
7233
7234 /* Place section name section after DWARF debug sections have been
7235 compressed. */
7236 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7237 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7238 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7239 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7240
7241 /* Place the section headers. */
7242 i_ehdrp = elf_elfheader (abfd);
7243 bed = get_elf_backend_data (abfd);
7244 off = align_file_position (off, 1 << bed->s->log_file_align);
7245 i_ehdrp->e_shoff = off;
7246 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7247 elf_next_file_pos (abfd) = off;
7248
7249 return true;
7250 }
7251
7252 bool
7253 _bfd_elf_write_object_contents (bfd *abfd)
7254 {
7255 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7256 Elf_Internal_Shdr **i_shdrp;
7257 bool failed;
7258 unsigned int count, num_sec;
7259 struct elf_obj_tdata *t;
7260
7261 if (! abfd->output_has_begun
7262 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7263 return false;
7264 /* Do not rewrite ELF data when the BFD has been opened for update.
7265 abfd->output_has_begun was set to TRUE on opening, so creation of
7266 new sections, and modification of existing section sizes was
7267 restricted. This means the ELF header, program headers and
7268 section headers can't have changed. If the contents of any
7269 sections has been modified, then those changes have already been
7270 written to the BFD. */
7271 else if (abfd->direction == both_direction)
7272 {
7273 BFD_ASSERT (abfd->output_has_begun);
7274 return true;
7275 }
7276
7277 i_shdrp = elf_elfsections (abfd);
7278
7279 failed = false;
7280 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7281 if (failed)
7282 return false;
7283
7284 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7285 return false;
7286
7287 /* After writing the headers, we need to write the sections too... */
7288 num_sec = elf_numsections (abfd);
7289 for (count = 1; count < num_sec; count++)
7290 {
7291 /* Don't set the sh_name field without section header. */
7292 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7293 i_shdrp[count]->sh_name
7294 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7295 i_shdrp[count]->sh_name);
7296 if (bed->elf_backend_section_processing)
7297 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7298 return false;
7299 if (i_shdrp[count]->contents)
7300 {
7301 bfd_size_type amt = i_shdrp[count]->sh_size;
7302
7303 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7304 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7305 return false;
7306 }
7307 }
7308
7309 /* Write out the section header names. */
7310 t = elf_tdata (abfd);
7311 if (elf_shstrtab (abfd) != NULL
7312 && t->shstrtab_hdr.sh_offset != -1
7313 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7314 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7315 return false;
7316
7317 if (!(*bed->elf_backend_final_write_processing) (abfd))
7318 return false;
7319
7320 if (!bed->s->write_shdrs_and_ehdr (abfd))
7321 return false;
7322
7323 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7324 if (t->o->build_id.after_write_object_contents != NULL
7325 && !(*t->o->build_id.after_write_object_contents) (abfd))
7326 return false;
7327 if (t->o->package_metadata.after_write_object_contents != NULL
7328 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7329 return false;
7330
7331 return true;
7332 }
7333
7334 bool
7335 _bfd_elf_write_corefile_contents (bfd *abfd)
7336 {
7337 /* Hopefully this can be done just like an object file. */
7338 return _bfd_elf_write_object_contents (abfd);
7339 }
7340
7341 /* Given a section, search the header to find them. */
7342
7343 unsigned int
7344 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7345 {
7346 const struct elf_backend_data *bed;
7347 unsigned int sec_index;
7348
7349 if (elf_section_data (asect) != NULL
7350 && elf_section_data (asect)->this_idx != 0)
7351 return elf_section_data (asect)->this_idx;
7352
7353 if (bfd_is_abs_section (asect))
7354 sec_index = SHN_ABS;
7355 else if (bfd_is_com_section (asect))
7356 sec_index = SHN_COMMON;
7357 else if (bfd_is_und_section (asect))
7358 sec_index = SHN_UNDEF;
7359 else
7360 sec_index = SHN_BAD;
7361
7362 bed = get_elf_backend_data (abfd);
7363 if (bed->elf_backend_section_from_bfd_section)
7364 {
7365 int retval = sec_index;
7366
7367 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7368 return retval;
7369 }
7370
7371 if (sec_index == SHN_BAD)
7372 bfd_set_error (bfd_error_nonrepresentable_section);
7373
7374 return sec_index;
7375 }
7376
7377 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7378 on error. */
7379
7380 int
7381 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7382 {
7383 asymbol *asym_ptr = *asym_ptr_ptr;
7384 int idx;
7385 flagword flags = asym_ptr->flags;
7386
7387 /* When gas creates relocations against local labels, it creates its
7388 own symbol for the section, but does put the symbol into the
7389 symbol chain, so udata is 0. When the linker is generating
7390 relocatable output, this section symbol may be for one of the
7391 input sections rather than the output section. */
7392 if (asym_ptr->udata.i == 0
7393 && (flags & BSF_SECTION_SYM)
7394 && asym_ptr->section)
7395 {
7396 asection *sec;
7397
7398 sec = asym_ptr->section;
7399 if (sec->owner != abfd && sec->output_section != NULL)
7400 sec = sec->output_section;
7401 if (sec->owner == abfd
7402 && sec->index < elf_num_section_syms (abfd)
7403 && elf_section_syms (abfd)[sec->index] != NULL)
7404 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7405 }
7406
7407 idx = asym_ptr->udata.i;
7408
7409 if (idx == 0)
7410 {
7411 /* This case can occur when using --strip-symbol on a symbol
7412 which is used in a relocation entry. */
7413 _bfd_error_handler
7414 /* xgettext:c-format */
7415 (_("%pB: symbol `%s' required but not present"),
7416 abfd, bfd_asymbol_name (asym_ptr));
7417 bfd_set_error (bfd_error_no_symbols);
7418 return -1;
7419 }
7420
7421 #if DEBUG & 4
7422 {
7423 fprintf (stderr,
7424 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7425 " flags = 0x%.8x\n",
7426 (long) asym_ptr, asym_ptr->name, idx, flags);
7427 fflush (stderr);
7428 }
7429 #endif
7430
7431 return idx;
7432 }
7433
7434 static inline bfd_vma
7435 segment_size (Elf_Internal_Phdr *segment)
7436 {
7437 return (segment->p_memsz > segment->p_filesz
7438 ? segment->p_memsz : segment->p_filesz);
7439 }
7440
7441
7442 /* Returns the end address of the segment + 1. */
7443 static inline bfd_vma
7444 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7445 {
7446 return start + segment_size (segment);
7447 }
7448
7449 static inline bfd_size_type
7450 section_size (asection *section, Elf_Internal_Phdr *segment)
7451 {
7452 if ((section->flags & SEC_HAS_CONTENTS) != 0
7453 || (section->flags & SEC_THREAD_LOCAL) == 0
7454 || segment->p_type == PT_TLS)
7455 return section->size;
7456 return 0;
7457 }
7458
7459 /* Returns TRUE if the given section is contained within the given
7460 segment. LMA addresses are compared against PADDR when
7461 USE_VADDR is false, VMA against VADDR when true. */
7462 static bool
7463 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7464 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7465 bool use_vaddr)
7466 {
7467 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7468 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7469 bfd_vma octet;
7470 if (_bfd_mul_overflow (addr, opb, &octet))
7471 return false;
7472 /* The third and fourth lines below are testing that the section end
7473 address is within the segment. It's written this way to avoid
7474 overflow. Add seg_addr + section_size to both sides of the
7475 inequality to make it obvious. */
7476 return (octet >= seg_addr
7477 && segment_size (segment) >= section_size (section, segment)
7478 && (octet - seg_addr
7479 <= segment_size (segment) - section_size (section, segment)));
7480 }
7481
7482 /* Handle PT_NOTE segment. */
7483 static bool
7484 is_note (asection *s, Elf_Internal_Phdr *p)
7485 {
7486 return (p->p_type == PT_NOTE
7487 && elf_section_type (s) == SHT_NOTE
7488 && (ufile_ptr) s->filepos >= p->p_offset
7489 && p->p_filesz >= s->size
7490 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7491 }
7492
7493 /* Rewrite program header information. */
7494
7495 static bool
7496 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7497 {
7498 Elf_Internal_Ehdr *iehdr;
7499 struct elf_segment_map *map;
7500 struct elf_segment_map *map_first;
7501 struct elf_segment_map **pointer_to_map;
7502 Elf_Internal_Phdr *segment;
7503 asection *section;
7504 unsigned int i;
7505 unsigned int num_segments;
7506 bool phdr_included = false;
7507 bool p_paddr_valid;
7508 struct elf_segment_map *phdr_adjust_seg = NULL;
7509 unsigned int phdr_adjust_num = 0;
7510 const struct elf_backend_data *bed;
7511 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7512
7513 bed = get_elf_backend_data (ibfd);
7514 iehdr = elf_elfheader (ibfd);
7515
7516 map_first = NULL;
7517 pointer_to_map = &map_first;
7518
7519 num_segments = elf_elfheader (ibfd)->e_phnum;
7520
7521 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7522 linker, which generates a PT_INTERP section with p_vaddr and
7523 p_memsz set to 0. */
7524 #define IS_SOLARIS_PT_INTERP(p, s) \
7525 (p->p_vaddr == 0 \
7526 && p->p_paddr == 0 \
7527 && p->p_memsz == 0 \
7528 && p->p_filesz > 0 \
7529 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7530 && s->size > 0 \
7531 && (bfd_vma) s->filepos >= p->p_offset \
7532 && ((bfd_vma) s->filepos + s->size \
7533 <= p->p_offset + p->p_filesz))
7534
7535 /* Decide if the given section should be included in the given segment.
7536 A section will be included if:
7537 1. It is within the address space of the segment -- we use the LMA
7538 if that is set for the segment and the VMA otherwise,
7539 2. It is an allocated section or a NOTE section in a PT_NOTE
7540 segment.
7541 3. There is an output section associated with it,
7542 4. The section has not already been allocated to a previous segment.
7543 5. PT_GNU_STACK segments do not include any sections.
7544 6. PT_TLS segment includes only SHF_TLS sections.
7545 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7546 8. PT_DYNAMIC should not contain empty sections at the beginning
7547 (with the possible exception of .dynamic). */
7548 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7549 (((is_contained_by (section, segment, segment->p_paddr, \
7550 segment->p_vaddr, opb, \
7551 bed->want_p_paddr_set_to_zero) \
7552 && (section->flags & SEC_ALLOC) != 0) \
7553 || is_note (section, segment)) \
7554 && segment->p_type != PT_GNU_STACK \
7555 && (segment->p_type != PT_TLS \
7556 || (section->flags & SEC_THREAD_LOCAL)) \
7557 && (segment->p_type == PT_LOAD \
7558 || segment->p_type == PT_TLS \
7559 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7560 && (segment->p_type != PT_DYNAMIC \
7561 || section_size (section, segment) > 0 \
7562 || (segment->p_paddr \
7563 ? segment->p_paddr != section->lma * (opb) \
7564 : segment->p_vaddr != section->vma * (opb)) \
7565 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7566 && (segment->p_type != PT_LOAD || !section->segment_mark))
7567
7568 /* If the output section of a section in the input segment is NULL,
7569 it is removed from the corresponding output segment. */
7570 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7571 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7572 && section->output_section != NULL)
7573
7574 /* Returns TRUE iff seg1 starts after the end of seg2. */
7575 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7576 (seg1->field >= segment_end (seg2, seg2->field))
7577
7578 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7579 their VMA address ranges and their LMA address ranges overlap.
7580 It is possible to have overlapping VMA ranges without overlapping LMA
7581 ranges. RedBoot images for example can have both .data and .bss mapped
7582 to the same VMA range, but with the .data section mapped to a different
7583 LMA. */
7584 #define SEGMENT_OVERLAPS(seg1, seg2) \
7585 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7586 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7587 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7588 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7589
7590 /* Initialise the segment mark field, and discard stupid alignment. */
7591 for (section = ibfd->sections; section != NULL; section = section->next)
7592 {
7593 asection *o = section->output_section;
7594 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7595 o->alignment_power = 0;
7596 section->segment_mark = false;
7597 }
7598
7599 /* The Solaris linker creates program headers in which all the
7600 p_paddr fields are zero. When we try to objcopy or strip such a
7601 file, we get confused. Check for this case, and if we find it
7602 don't set the p_paddr_valid fields. */
7603 p_paddr_valid = false;
7604 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7605 i < num_segments;
7606 i++, segment++)
7607 if (segment->p_paddr != 0)
7608 {
7609 p_paddr_valid = true;
7610 break;
7611 }
7612
7613 /* Scan through the segments specified in the program header
7614 of the input BFD. For this first scan we look for overlaps
7615 in the loadable segments. These can be created by weird
7616 parameters to objcopy. Also, fix some solaris weirdness. */
7617 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7618 i < num_segments;
7619 i++, segment++)
7620 {
7621 unsigned int j;
7622 Elf_Internal_Phdr *segment2;
7623
7624 if (segment->p_type == PT_INTERP)
7625 for (section = ibfd->sections; section; section = section->next)
7626 if (IS_SOLARIS_PT_INTERP (segment, section))
7627 {
7628 /* Mininal change so that the normal section to segment
7629 assignment code will work. */
7630 segment->p_vaddr = section->vma * opb;
7631 break;
7632 }
7633
7634 if (segment->p_type != PT_LOAD)
7635 {
7636 /* Remove PT_GNU_RELRO segment. */
7637 if (segment->p_type == PT_GNU_RELRO)
7638 segment->p_type = PT_NULL;
7639 continue;
7640 }
7641
7642 /* Determine if this segment overlaps any previous segments. */
7643 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7644 {
7645 bfd_signed_vma extra_length;
7646
7647 if (segment2->p_type != PT_LOAD
7648 || !SEGMENT_OVERLAPS (segment, segment2))
7649 continue;
7650
7651 /* Merge the two segments together. */
7652 if (segment2->p_vaddr < segment->p_vaddr)
7653 {
7654 /* Extend SEGMENT2 to include SEGMENT and then delete
7655 SEGMENT. */
7656 extra_length = (segment_end (segment, segment->p_vaddr)
7657 - segment_end (segment2, segment2->p_vaddr));
7658
7659 if (extra_length > 0)
7660 {
7661 segment2->p_memsz += extra_length;
7662 segment2->p_filesz += extra_length;
7663 }
7664
7665 segment->p_type = PT_NULL;
7666
7667 /* Since we have deleted P we must restart the outer loop. */
7668 i = 0;
7669 segment = elf_tdata (ibfd)->phdr;
7670 break;
7671 }
7672 else
7673 {
7674 /* Extend SEGMENT to include SEGMENT2 and then delete
7675 SEGMENT2. */
7676 extra_length = (segment_end (segment2, segment2->p_vaddr)
7677 - segment_end (segment, segment->p_vaddr));
7678
7679 if (extra_length > 0)
7680 {
7681 segment->p_memsz += extra_length;
7682 segment->p_filesz += extra_length;
7683 }
7684
7685 segment2->p_type = PT_NULL;
7686 }
7687 }
7688 }
7689
7690 /* The second scan attempts to assign sections to segments. */
7691 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7692 i < num_segments;
7693 i++, segment++)
7694 {
7695 unsigned int section_count;
7696 asection **sections;
7697 asection *output_section;
7698 unsigned int isec;
7699 asection *matching_lma;
7700 asection *suggested_lma;
7701 unsigned int j;
7702 size_t amt;
7703 asection *first_section;
7704
7705 if (segment->p_type == PT_NULL)
7706 continue;
7707
7708 first_section = NULL;
7709 /* Compute how many sections might be placed into this segment. */
7710 for (section = ibfd->sections, section_count = 0;
7711 section != NULL;
7712 section = section->next)
7713 {
7714 /* Find the first section in the input segment, which may be
7715 removed from the corresponding output segment. */
7716 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7717 {
7718 if (first_section == NULL)
7719 first_section = section;
7720 if (section->output_section != NULL)
7721 ++section_count;
7722 }
7723 }
7724
7725 /* Allocate a segment map big enough to contain
7726 all of the sections we have selected. */
7727 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7728 amt += section_count * sizeof (asection *);
7729 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7730 if (map == NULL)
7731 return false;
7732
7733 /* Initialise the fields of the segment map. Default to
7734 using the physical address of the segment in the input BFD. */
7735 map->next = NULL;
7736 map->p_type = segment->p_type;
7737 map->p_flags = segment->p_flags;
7738 map->p_flags_valid = 1;
7739
7740 if (map->p_type == PT_LOAD
7741 && (ibfd->flags & D_PAGED) != 0
7742 && maxpagesize > 1
7743 && segment->p_align > 1)
7744 {
7745 map->p_align = segment->p_align;
7746 if (segment->p_align > maxpagesize)
7747 map->p_align = maxpagesize;
7748 map->p_align_valid = 1;
7749 }
7750
7751 /* If the first section in the input segment is removed, there is
7752 no need to preserve segment physical address in the corresponding
7753 output segment. */
7754 if (!first_section || first_section->output_section != NULL)
7755 {
7756 map->p_paddr = segment->p_paddr;
7757 map->p_paddr_valid = p_paddr_valid;
7758 }
7759
7760 /* Determine if this segment contains the ELF file header
7761 and if it contains the program headers themselves. */
7762 map->includes_filehdr = (segment->p_offset == 0
7763 && segment->p_filesz >= iehdr->e_ehsize);
7764 map->includes_phdrs = 0;
7765
7766 if (!phdr_included || segment->p_type != PT_LOAD)
7767 {
7768 map->includes_phdrs =
7769 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7770 && (segment->p_offset + segment->p_filesz
7771 >= ((bfd_vma) iehdr->e_phoff
7772 + iehdr->e_phnum * iehdr->e_phentsize)));
7773
7774 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7775 phdr_included = true;
7776 }
7777
7778 if (section_count == 0)
7779 {
7780 /* Special segments, such as the PT_PHDR segment, may contain
7781 no sections, but ordinary, loadable segments should contain
7782 something. They are allowed by the ELF spec however, so only
7783 a warning is produced.
7784 Don't warn if an empty PT_LOAD contains the program headers.
7785 There is however the valid use case of embedded systems which
7786 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7787 flash memory with zeros. No warning is shown for that case. */
7788 if (segment->p_type == PT_LOAD
7789 && !map->includes_phdrs
7790 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7791 /* xgettext:c-format */
7792 _bfd_error_handler
7793 (_("%pB: warning: empty loadable segment detected"
7794 " at vaddr=%#" PRIx64 ", is this intentional?"),
7795 ibfd, (uint64_t) segment->p_vaddr);
7796
7797 map->p_vaddr_offset = segment->p_vaddr / opb;
7798 map->count = 0;
7799 *pointer_to_map = map;
7800 pointer_to_map = &map->next;
7801
7802 continue;
7803 }
7804
7805 /* Now scan the sections in the input BFD again and attempt
7806 to add their corresponding output sections to the segment map.
7807 The problem here is how to handle an output section which has
7808 been moved (ie had its LMA changed). There are four possibilities:
7809
7810 1. None of the sections have been moved.
7811 In this case we can continue to use the segment LMA from the
7812 input BFD.
7813
7814 2. All of the sections have been moved by the same amount.
7815 In this case we can change the segment's LMA to match the LMA
7816 of the first section.
7817
7818 3. Some of the sections have been moved, others have not.
7819 In this case those sections which have not been moved can be
7820 placed in the current segment which will have to have its size,
7821 and possibly its LMA changed, and a new segment or segments will
7822 have to be created to contain the other sections.
7823
7824 4. The sections have been moved, but not by the same amount.
7825 In this case we can change the segment's LMA to match the LMA
7826 of the first section and we will have to create a new segment
7827 or segments to contain the other sections.
7828
7829 In order to save time, we allocate an array to hold the section
7830 pointers that we are interested in. As these sections get assigned
7831 to a segment, they are removed from this array. */
7832
7833 amt = section_count * sizeof (asection *);
7834 sections = (asection **) bfd_malloc (amt);
7835 if (sections == NULL)
7836 return false;
7837
7838 /* Step One: Scan for segment vs section LMA conflicts.
7839 Also add the sections to the section array allocated above.
7840 Also add the sections to the current segment. In the common
7841 case, where the sections have not been moved, this means that
7842 we have completely filled the segment, and there is nothing
7843 more to do. */
7844 isec = 0;
7845 matching_lma = NULL;
7846 suggested_lma = NULL;
7847
7848 for (section = first_section, j = 0;
7849 section != NULL;
7850 section = section->next)
7851 {
7852 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7853 {
7854 output_section = section->output_section;
7855
7856 sections[j++] = section;
7857
7858 /* The Solaris native linker always sets p_paddr to 0.
7859 We try to catch that case here, and set it to the
7860 correct value. Note - some backends require that
7861 p_paddr be left as zero. */
7862 if (!p_paddr_valid
7863 && segment->p_vaddr != 0
7864 && !bed->want_p_paddr_set_to_zero
7865 && isec == 0
7866 && output_section->lma != 0
7867 && (align_power (segment->p_vaddr
7868 + (map->includes_filehdr
7869 ? iehdr->e_ehsize : 0)
7870 + (map->includes_phdrs
7871 ? iehdr->e_phnum * iehdr->e_phentsize
7872 : 0),
7873 output_section->alignment_power * opb)
7874 == (output_section->vma * opb)))
7875 map->p_paddr = segment->p_vaddr;
7876
7877 /* Match up the physical address of the segment with the
7878 LMA address of the output section. */
7879 if (is_contained_by (output_section, segment, map->p_paddr,
7880 0, opb, false)
7881 || is_note (section, segment))
7882 {
7883 if (matching_lma == NULL
7884 || output_section->lma < matching_lma->lma)
7885 matching_lma = output_section;
7886
7887 /* We assume that if the section fits within the segment
7888 then it does not overlap any other section within that
7889 segment. */
7890 map->sections[isec++] = output_section;
7891 }
7892 else if (suggested_lma == NULL)
7893 suggested_lma = output_section;
7894
7895 if (j == section_count)
7896 break;
7897 }
7898 }
7899
7900 BFD_ASSERT (j == section_count);
7901
7902 /* Step Two: Adjust the physical address of the current segment,
7903 if necessary. */
7904 if (isec == section_count)
7905 {
7906 /* All of the sections fitted within the segment as currently
7907 specified. This is the default case. Add the segment to
7908 the list of built segments and carry on to process the next
7909 program header in the input BFD. */
7910 map->count = section_count;
7911 *pointer_to_map = map;
7912 pointer_to_map = &map->next;
7913
7914 if (p_paddr_valid
7915 && !bed->want_p_paddr_set_to_zero)
7916 {
7917 bfd_vma hdr_size = 0;
7918 if (map->includes_filehdr)
7919 hdr_size = iehdr->e_ehsize;
7920 if (map->includes_phdrs)
7921 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7922
7923 /* Account for padding before the first section in the
7924 segment. */
7925 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7926 - matching_lma->lma);
7927 }
7928
7929 free (sections);
7930 continue;
7931 }
7932 else
7933 {
7934 /* Change the current segment's physical address to match
7935 the LMA of the first section that fitted, or if no
7936 section fitted, the first section. */
7937 if (matching_lma == NULL)
7938 matching_lma = suggested_lma;
7939
7940 map->p_paddr = matching_lma->lma * opb;
7941
7942 /* Offset the segment physical address from the lma
7943 to allow for space taken up by elf headers. */
7944 if (map->includes_phdrs)
7945 {
7946 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7947
7948 /* iehdr->e_phnum is just an estimate of the number
7949 of program headers that we will need. Make a note
7950 here of the number we used and the segment we chose
7951 to hold these headers, so that we can adjust the
7952 offset when we know the correct value. */
7953 phdr_adjust_num = iehdr->e_phnum;
7954 phdr_adjust_seg = map;
7955 }
7956
7957 if (map->includes_filehdr)
7958 {
7959 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7960 map->p_paddr -= iehdr->e_ehsize;
7961 /* We've subtracted off the size of headers from the
7962 first section lma, but there may have been some
7963 alignment padding before that section too. Try to
7964 account for that by adjusting the segment lma down to
7965 the same alignment. */
7966 if (segment->p_align != 0 && segment->p_align < align)
7967 align = segment->p_align;
7968 map->p_paddr &= -(align * opb);
7969 }
7970 }
7971
7972 /* Step Three: Loop over the sections again, this time assigning
7973 those that fit to the current segment and removing them from the
7974 sections array; but making sure not to leave large gaps. Once all
7975 possible sections have been assigned to the current segment it is
7976 added to the list of built segments and if sections still remain
7977 to be assigned, a new segment is constructed before repeating
7978 the loop. */
7979 isec = 0;
7980 do
7981 {
7982 map->count = 0;
7983 suggested_lma = NULL;
7984
7985 /* Fill the current segment with sections that fit. */
7986 for (j = 0; j < section_count; j++)
7987 {
7988 section = sections[j];
7989
7990 if (section == NULL)
7991 continue;
7992
7993 output_section = section->output_section;
7994
7995 BFD_ASSERT (output_section != NULL);
7996
7997 if (is_contained_by (output_section, segment, map->p_paddr,
7998 0, opb, false)
7999 || is_note (section, segment))
8000 {
8001 if (map->count == 0)
8002 {
8003 /* If the first section in a segment does not start at
8004 the beginning of the segment, then something is
8005 wrong. */
8006 if (align_power (map->p_paddr
8007 + (map->includes_filehdr
8008 ? iehdr->e_ehsize : 0)
8009 + (map->includes_phdrs
8010 ? iehdr->e_phnum * iehdr->e_phentsize
8011 : 0),
8012 output_section->alignment_power * opb)
8013 != output_section->lma * opb)
8014 goto sorry;
8015 }
8016 else
8017 {
8018 asection *prev_sec;
8019
8020 prev_sec = map->sections[map->count - 1];
8021
8022 /* If the gap between the end of the previous section
8023 and the start of this section is more than
8024 maxpagesize then we need to start a new segment. */
8025 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
8026 maxpagesize)
8027 < BFD_ALIGN (output_section->lma, maxpagesize))
8028 || (prev_sec->lma + prev_sec->size
8029 > output_section->lma))
8030 {
8031 if (suggested_lma == NULL)
8032 suggested_lma = output_section;
8033
8034 continue;
8035 }
8036 }
8037
8038 map->sections[map->count++] = output_section;
8039 ++isec;
8040 sections[j] = NULL;
8041 if (segment->p_type == PT_LOAD)
8042 section->segment_mark = true;
8043 }
8044 else if (suggested_lma == NULL)
8045 suggested_lma = output_section;
8046 }
8047
8048 /* PR 23932. A corrupt input file may contain sections that cannot
8049 be assigned to any segment - because for example they have a
8050 negative size - or segments that do not contain any sections.
8051 But there are also valid reasons why a segment can be empty.
8052 So allow a count of zero. */
8053
8054 /* Add the current segment to the list of built segments. */
8055 *pointer_to_map = map;
8056 pointer_to_map = &map->next;
8057
8058 if (isec < section_count)
8059 {
8060 /* We still have not allocated all of the sections to
8061 segments. Create a new segment here, initialise it
8062 and carry on looping. */
8063 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8064 amt += section_count * sizeof (asection *);
8065 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8066 if (map == NULL)
8067 {
8068 free (sections);
8069 return false;
8070 }
8071
8072 /* Initialise the fields of the segment map. Set the physical
8073 physical address to the LMA of the first section that has
8074 not yet been assigned. */
8075 map->next = NULL;
8076 map->p_type = segment->p_type;
8077 map->p_flags = segment->p_flags;
8078 map->p_flags_valid = 1;
8079 map->p_paddr = suggested_lma->lma * opb;
8080 map->p_paddr_valid = p_paddr_valid;
8081 map->includes_filehdr = 0;
8082 map->includes_phdrs = 0;
8083 }
8084
8085 continue;
8086 sorry:
8087 bfd_set_error (bfd_error_sorry);
8088 free (sections);
8089 return false;
8090 }
8091 while (isec < section_count);
8092
8093 free (sections);
8094 }
8095
8096 elf_seg_map (obfd) = map_first;
8097
8098 /* If we had to estimate the number of program headers that were
8099 going to be needed, then check our estimate now and adjust
8100 the offset if necessary. */
8101 if (phdr_adjust_seg != NULL)
8102 {
8103 unsigned int count;
8104
8105 for (count = 0, map = map_first; map != NULL; map = map->next)
8106 count++;
8107
8108 if (count > phdr_adjust_num)
8109 phdr_adjust_seg->p_paddr
8110 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8111
8112 for (map = map_first; map != NULL; map = map->next)
8113 if (map->p_type == PT_PHDR)
8114 {
8115 bfd_vma adjust
8116 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8117 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8118 break;
8119 }
8120 }
8121
8122 #undef IS_SOLARIS_PT_INTERP
8123 #undef IS_SECTION_IN_INPUT_SEGMENT
8124 #undef INCLUDE_SECTION_IN_SEGMENT
8125 #undef SEGMENT_AFTER_SEGMENT
8126 #undef SEGMENT_OVERLAPS
8127 return true;
8128 }
8129
8130 /* Return true if p_align in the ELF program header in ABFD is valid. */
8131
8132 static bool
8133 elf_is_p_align_valid (bfd *abfd)
8134 {
8135 unsigned int i;
8136 Elf_Internal_Phdr *segment;
8137 unsigned int num_segments;
8138 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8139 bfd_size_type maxpagesize = bed->maxpagesize;
8140 bfd_size_type p_align = bed->p_align;
8141
8142 /* Return true if the default p_align value isn't set or the maximum
8143 page size is the same as the minimum page size. */
8144 if (p_align == 0 || maxpagesize == bed->minpagesize)
8145 return true;
8146
8147 /* When the default p_align value is set, p_align may be set to the
8148 default p_align value while segments are aligned to the maximum
8149 page size. In this case, the input p_align will be ignored and
8150 the maximum page size will be used to align the output segments. */
8151 segment = elf_tdata (abfd)->phdr;
8152 num_segments = elf_elfheader (abfd)->e_phnum;
8153 for (i = 0; i < num_segments; i++, segment++)
8154 if (segment->p_type == PT_LOAD
8155 && (segment->p_align != p_align
8156 || vma_page_aligned_bias (segment->p_vaddr,
8157 segment->p_offset,
8158 maxpagesize) != 0))
8159 return true;
8160
8161 return false;
8162 }
8163
8164 /* Copy ELF program header information. */
8165
8166 static bool
8167 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8168 {
8169 Elf_Internal_Ehdr *iehdr;
8170 struct elf_segment_map *map;
8171 struct elf_segment_map *map_first;
8172 struct elf_segment_map **pointer_to_map;
8173 Elf_Internal_Phdr *segment;
8174 unsigned int i;
8175 unsigned int num_segments;
8176 bool phdr_included = false;
8177 bool p_paddr_valid;
8178 bool p_palign_valid;
8179 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8180
8181 iehdr = elf_elfheader (ibfd);
8182
8183 map_first = NULL;
8184 pointer_to_map = &map_first;
8185
8186 /* If all the segment p_paddr fields are zero, don't set
8187 map->p_paddr_valid. */
8188 p_paddr_valid = false;
8189 num_segments = elf_elfheader (ibfd)->e_phnum;
8190 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8191 i < num_segments;
8192 i++, segment++)
8193 if (segment->p_paddr != 0)
8194 {
8195 p_paddr_valid = true;
8196 break;
8197 }
8198
8199 p_palign_valid = elf_is_p_align_valid (ibfd);
8200
8201 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8202 i < num_segments;
8203 i++, segment++)
8204 {
8205 asection *section;
8206 unsigned int section_count;
8207 size_t amt;
8208 Elf_Internal_Shdr *this_hdr;
8209 asection *first_section = NULL;
8210 asection *lowest_section;
8211
8212 /* Compute how many sections are in this segment. */
8213 for (section = ibfd->sections, section_count = 0;
8214 section != NULL;
8215 section = section->next)
8216 {
8217 this_hdr = &(elf_section_data(section)->this_hdr);
8218 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8219 {
8220 if (first_section == NULL)
8221 first_section = section;
8222 section_count++;
8223 }
8224 }
8225
8226 /* Allocate a segment map big enough to contain
8227 all of the sections we have selected. */
8228 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8229 amt += section_count * sizeof (asection *);
8230 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8231 if (map == NULL)
8232 return false;
8233
8234 /* Initialize the fields of the output segment map with the
8235 input segment. */
8236 map->next = NULL;
8237 map->p_type = segment->p_type;
8238 map->p_flags = segment->p_flags;
8239 map->p_flags_valid = 1;
8240 map->p_paddr = segment->p_paddr;
8241 map->p_paddr_valid = p_paddr_valid;
8242 map->p_align = segment->p_align;
8243 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8244 map->p_align_valid = (map->p_type == PT_GNU_STACK
8245 || p_palign_valid);
8246 map->p_vaddr_offset = 0;
8247
8248 if (map->p_type == PT_GNU_RELRO
8249 || map->p_type == PT_GNU_STACK)
8250 {
8251 /* The PT_GNU_RELRO segment may contain the first a few
8252 bytes in the .got.plt section even if the whole .got.plt
8253 section isn't in the PT_GNU_RELRO segment. We won't
8254 change the size of the PT_GNU_RELRO segment.
8255 Similarly, PT_GNU_STACK size is significant on uclinux
8256 systems. */
8257 map->p_size = segment->p_memsz;
8258 map->p_size_valid = 1;
8259 }
8260
8261 /* Determine if this segment contains the ELF file header
8262 and if it contains the program headers themselves. */
8263 map->includes_filehdr = (segment->p_offset == 0
8264 && segment->p_filesz >= iehdr->e_ehsize);
8265
8266 map->includes_phdrs = 0;
8267 if (! phdr_included || segment->p_type != PT_LOAD)
8268 {
8269 map->includes_phdrs =
8270 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8271 && (segment->p_offset + segment->p_filesz
8272 >= ((bfd_vma) iehdr->e_phoff
8273 + iehdr->e_phnum * iehdr->e_phentsize)));
8274
8275 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8276 phdr_included = true;
8277 }
8278
8279 lowest_section = NULL;
8280 if (section_count != 0)
8281 {
8282 unsigned int isec = 0;
8283
8284 for (section = first_section;
8285 section != NULL;
8286 section = section->next)
8287 {
8288 this_hdr = &(elf_section_data(section)->this_hdr);
8289 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8290 {
8291 map->sections[isec++] = section->output_section;
8292 if ((section->flags & SEC_ALLOC) != 0)
8293 {
8294 bfd_vma seg_off;
8295
8296 if (lowest_section == NULL
8297 || section->lma < lowest_section->lma)
8298 lowest_section = section;
8299
8300 /* Section lmas are set up from PT_LOAD header
8301 p_paddr in _bfd_elf_make_section_from_shdr.
8302 If this header has a p_paddr that disagrees
8303 with the section lma, flag the p_paddr as
8304 invalid. */
8305 if ((section->flags & SEC_LOAD) != 0)
8306 seg_off = this_hdr->sh_offset - segment->p_offset;
8307 else
8308 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8309 if (section->lma * opb - segment->p_paddr != seg_off)
8310 map->p_paddr_valid = false;
8311 }
8312 if (isec == section_count)
8313 break;
8314 }
8315 }
8316 }
8317
8318 if (section_count == 0)
8319 map->p_vaddr_offset = segment->p_vaddr / opb;
8320 else if (map->p_paddr_valid)
8321 {
8322 /* Account for padding before the first section in the segment. */
8323 bfd_vma hdr_size = 0;
8324 if (map->includes_filehdr)
8325 hdr_size = iehdr->e_ehsize;
8326 if (map->includes_phdrs)
8327 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8328
8329 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8330 - (lowest_section ? lowest_section->lma : 0));
8331 }
8332
8333 map->count = section_count;
8334 *pointer_to_map = map;
8335 pointer_to_map = &map->next;
8336 }
8337
8338 elf_seg_map (obfd) = map_first;
8339 return true;
8340 }
8341
8342 /* Copy private BFD data. This copies or rewrites ELF program header
8343 information. */
8344
8345 static bool
8346 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8347 {
8348 bfd_vma maxpagesize;
8349
8350 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8351 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8352 return true;
8353
8354 if (elf_tdata (ibfd)->phdr == NULL)
8355 return true;
8356
8357 if (ibfd->xvec == obfd->xvec)
8358 {
8359 /* Check to see if any sections in the input BFD
8360 covered by ELF program header have changed. */
8361 Elf_Internal_Phdr *segment;
8362 asection *section, *osec;
8363 unsigned int i, num_segments;
8364 Elf_Internal_Shdr *this_hdr;
8365 const struct elf_backend_data *bed;
8366
8367 bed = get_elf_backend_data (ibfd);
8368
8369 /* Regenerate the segment map if p_paddr is set to 0. */
8370 if (bed->want_p_paddr_set_to_zero)
8371 goto rewrite;
8372
8373 /* Initialize the segment mark field. */
8374 for (section = obfd->sections; section != NULL;
8375 section = section->next)
8376 section->segment_mark = false;
8377
8378 num_segments = elf_elfheader (ibfd)->e_phnum;
8379 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8380 i < num_segments;
8381 i++, segment++)
8382 {
8383 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8384 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8385 which severly confuses things, so always regenerate the segment
8386 map in this case. */
8387 if (segment->p_paddr == 0
8388 && segment->p_memsz == 0
8389 && (segment->p_type == PT_INTERP
8390 || segment->p_type == PT_DYNAMIC))
8391 goto rewrite;
8392
8393 for (section = ibfd->sections;
8394 section != NULL; section = section->next)
8395 {
8396 /* We mark the output section so that we know it comes
8397 from the input BFD. */
8398 osec = section->output_section;
8399 if (osec)
8400 osec->segment_mark = true;
8401
8402 /* Check if this section is covered by the segment. */
8403 this_hdr = &(elf_section_data(section)->this_hdr);
8404 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8405 {
8406 /* FIXME: Check if its output section is changed or
8407 removed. What else do we need to check? */
8408 if (osec == NULL
8409 || section->flags != osec->flags
8410 || section->lma != osec->lma
8411 || section->vma != osec->vma
8412 || section->size != osec->size
8413 || section->rawsize != osec->rawsize
8414 || section->alignment_power != osec->alignment_power)
8415 goto rewrite;
8416 }
8417 }
8418 }
8419
8420 /* Check to see if any output section do not come from the
8421 input BFD. */
8422 for (section = obfd->sections; section != NULL;
8423 section = section->next)
8424 {
8425 if (!section->segment_mark)
8426 goto rewrite;
8427 else
8428 section->segment_mark = false;
8429 }
8430
8431 return copy_elf_program_header (ibfd, obfd);
8432 }
8433
8434 rewrite:
8435 maxpagesize = 0;
8436 if (ibfd->xvec == obfd->xvec)
8437 {
8438 /* When rewriting program header, set the output maxpagesize to
8439 the maximum alignment of input PT_LOAD segments. */
8440 Elf_Internal_Phdr *segment;
8441 unsigned int i;
8442 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8443
8444 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8445 i < num_segments;
8446 i++, segment++)
8447 if (segment->p_type == PT_LOAD
8448 && maxpagesize < segment->p_align)
8449 {
8450 /* PR 17512: file: f17299af. */
8451 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8452 /* xgettext:c-format */
8453 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8454 PRIx64 " is too large"),
8455 ibfd, (uint64_t) segment->p_align);
8456 else
8457 maxpagesize = segment->p_align;
8458 }
8459 }
8460 if (maxpagesize == 0)
8461 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8462
8463 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8464 }
8465
8466 /* Initialize private output section information from input section. */
8467
8468 bool
8469 _bfd_elf_init_private_section_data (bfd *ibfd,
8470 asection *isec,
8471 bfd *obfd,
8472 asection *osec,
8473 struct bfd_link_info *link_info)
8474
8475 {
8476 Elf_Internal_Shdr *ihdr, *ohdr;
8477 bool final_link = (link_info != NULL
8478 && !bfd_link_relocatable (link_info));
8479
8480 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8481 || obfd->xvec->flavour != bfd_target_elf_flavour)
8482 return true;
8483
8484 BFD_ASSERT (elf_section_data (osec) != NULL);
8485
8486 /* If this is a known ABI section, ELF section type and flags may
8487 have been set up when OSEC was created. For normal sections we
8488 allow the user to override the type and flags other than
8489 SHF_MASKOS and SHF_MASKPROC. */
8490 if (elf_section_type (osec) == SHT_PROGBITS
8491 || elf_section_type (osec) == SHT_NOTE
8492 || elf_section_type (osec) == SHT_NOBITS)
8493 elf_section_type (osec) = SHT_NULL;
8494 /* For objcopy and relocatable link, copy the ELF section type from
8495 the input file if the BFD section flags are the same. (If they
8496 are different the user may be doing something like
8497 "objcopy --set-section-flags .text=alloc,data".) For a final
8498 link allow some flags that the linker clears to differ. */
8499 if (elf_section_type (osec) == SHT_NULL
8500 && (osec->flags == isec->flags
8501 || (final_link
8502 && ((osec->flags ^ isec->flags)
8503 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8504 elf_section_type (osec) = elf_section_type (isec);
8505
8506 /* FIXME: Is this correct for all OS/PROC specific flags? */
8507 elf_section_flags (osec) = (elf_section_flags (isec)
8508 & (SHF_MASKOS | SHF_MASKPROC));
8509
8510 /* Copy sh_info from input for mbind section. */
8511 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8512 && elf_section_flags (isec) & SHF_GNU_MBIND)
8513 elf_section_data (osec)->this_hdr.sh_info
8514 = elf_section_data (isec)->this_hdr.sh_info;
8515
8516 /* Set things up for objcopy and relocatable link. The output
8517 SHT_GROUP section will have its elf_next_in_group pointing back
8518 to the input group members. Ignore linker created group section.
8519 See elfNN_ia64_object_p in elfxx-ia64.c. */
8520 if ((link_info == NULL
8521 || !link_info->resolve_section_groups)
8522 && (elf_sec_group (isec) == NULL
8523 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8524 {
8525 if (elf_section_flags (isec) & SHF_GROUP)
8526 elf_section_flags (osec) |= SHF_GROUP;
8527 elf_next_in_group (osec) = elf_next_in_group (isec);
8528 elf_section_data (osec)->group = elf_section_data (isec)->group;
8529 }
8530
8531 /* If not decompress, preserve SHF_COMPRESSED. */
8532 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8533 elf_section_flags (osec) |= (elf_section_flags (isec)
8534 & SHF_COMPRESSED);
8535
8536 ihdr = &elf_section_data (isec)->this_hdr;
8537
8538 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8539 don't use the output section of the linked-to section since it
8540 may be NULL at this point. */
8541 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8542 {
8543 ohdr = &elf_section_data (osec)->this_hdr;
8544 ohdr->sh_flags |= SHF_LINK_ORDER;
8545 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8546 }
8547
8548 osec->use_rela_p = isec->use_rela_p;
8549
8550 return true;
8551 }
8552
8553 /* Copy private section information. This copies over the entsize
8554 field, and sometimes the info field. */
8555
8556 bool
8557 _bfd_elf_copy_private_section_data (bfd *ibfd,
8558 asection *isec,
8559 bfd *obfd,
8560 asection *osec)
8561 {
8562 Elf_Internal_Shdr *ihdr, *ohdr;
8563
8564 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8565 || obfd->xvec->flavour != bfd_target_elf_flavour)
8566 return true;
8567
8568 ihdr = &elf_section_data (isec)->this_hdr;
8569 ohdr = &elf_section_data (osec)->this_hdr;
8570
8571 ohdr->sh_entsize = ihdr->sh_entsize;
8572
8573 if (ihdr->sh_type == SHT_SYMTAB
8574 || ihdr->sh_type == SHT_DYNSYM
8575 || ihdr->sh_type == SHT_GNU_verneed
8576 || ihdr->sh_type == SHT_GNU_verdef)
8577 ohdr->sh_info = ihdr->sh_info;
8578
8579 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8580 NULL);
8581 }
8582
8583 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8584 necessary if we are removing either the SHT_GROUP section or any of
8585 the group member sections. DISCARDED is the value that a section's
8586 output_section has if the section will be discarded, NULL when this
8587 function is called from objcopy, bfd_abs_section_ptr when called
8588 from the linker. */
8589
8590 bool
8591 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8592 {
8593 asection *isec;
8594
8595 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8596 if (elf_section_type (isec) == SHT_GROUP)
8597 {
8598 asection *first = elf_next_in_group (isec);
8599 asection *s = first;
8600 bfd_size_type removed = 0;
8601
8602 while (s != NULL)
8603 {
8604 /* If this member section is being output but the
8605 SHT_GROUP section is not, then clear the group info
8606 set up by _bfd_elf_copy_private_section_data. */
8607 if (s->output_section != discarded
8608 && isec->output_section == discarded)
8609 {
8610 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8611 elf_group_name (s->output_section) = NULL;
8612 }
8613 else
8614 {
8615 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8616 if (s->output_section == discarded
8617 && isec->output_section != discarded)
8618 {
8619 /* Conversely, if the member section is not being
8620 output but the SHT_GROUP section is, then adjust
8621 its size. */
8622 removed += 4;
8623 if (elf_sec->rel.hdr != NULL
8624 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8625 removed += 4;
8626 if (elf_sec->rela.hdr != NULL
8627 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8628 removed += 4;
8629 }
8630 else
8631 {
8632 /* Also adjust for zero-sized relocation member
8633 section. */
8634 if (elf_sec->rel.hdr != NULL
8635 && elf_sec->rel.hdr->sh_size == 0)
8636 removed += 4;
8637 if (elf_sec->rela.hdr != NULL
8638 && elf_sec->rela.hdr->sh_size == 0)
8639 removed += 4;
8640 }
8641 }
8642 s = elf_next_in_group (s);
8643 if (s == first)
8644 break;
8645 }
8646 if (removed != 0)
8647 {
8648 if (discarded != NULL)
8649 {
8650 /* If we've been called for ld -r, then we need to
8651 adjust the input section size. */
8652 if (isec->rawsize == 0)
8653 isec->rawsize = isec->size;
8654 isec->size = isec->rawsize - removed;
8655 if (isec->size <= 4)
8656 {
8657 isec->size = 0;
8658 isec->flags |= SEC_EXCLUDE;
8659 }
8660 }
8661 else if (isec->output_section != NULL)
8662 {
8663 /* Adjust the output section size when called from
8664 objcopy. */
8665 isec->output_section->size -= removed;
8666 if (isec->output_section->size <= 4)
8667 {
8668 isec->output_section->size = 0;
8669 isec->output_section->flags |= SEC_EXCLUDE;
8670 }
8671 }
8672 }
8673 }
8674
8675 return true;
8676 }
8677
8678 /* Copy private header information. */
8679
8680 bool
8681 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8682 {
8683 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8684 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8685 return true;
8686
8687 /* Copy over private BFD data if it has not already been copied.
8688 This must be done here, rather than in the copy_private_bfd_data
8689 entry point, because the latter is called after the section
8690 contents have been set, which means that the program headers have
8691 already been worked out. */
8692 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8693 {
8694 if (! copy_private_bfd_data (ibfd, obfd))
8695 return false;
8696 }
8697
8698 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8699 }
8700
8701 /* Copy private symbol information. If this symbol is in a section
8702 which we did not map into a BFD section, try to map the section
8703 index correctly. We use special macro definitions for the mapped
8704 section indices; these definitions are interpreted by the
8705 swap_out_syms function. */
8706
8707 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8708 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8709 #define MAP_STRTAB (SHN_HIOS + 3)
8710 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8711 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8712
8713 bool
8714 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8715 asymbol *isymarg,
8716 bfd *obfd,
8717 asymbol *osymarg)
8718 {
8719 elf_symbol_type *isym, *osym;
8720
8721 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8722 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8723 return true;
8724
8725 isym = elf_symbol_from (isymarg);
8726 osym = elf_symbol_from (osymarg);
8727
8728 if (isym != NULL
8729 && isym->internal_elf_sym.st_shndx != 0
8730 && osym != NULL
8731 && bfd_is_abs_section (isym->symbol.section))
8732 {
8733 unsigned int shndx;
8734
8735 shndx = isym->internal_elf_sym.st_shndx;
8736 if (shndx == elf_onesymtab (ibfd))
8737 shndx = MAP_ONESYMTAB;
8738 else if (shndx == elf_dynsymtab (ibfd))
8739 shndx = MAP_DYNSYMTAB;
8740 else if (shndx == elf_strtab_sec (ibfd))
8741 shndx = MAP_STRTAB;
8742 else if (shndx == elf_shstrtab_sec (ibfd))
8743 shndx = MAP_SHSTRTAB;
8744 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8745 shndx = MAP_SYM_SHNDX;
8746 osym->internal_elf_sym.st_shndx = shndx;
8747 }
8748
8749 return true;
8750 }
8751
8752 /* Swap out the symbols. */
8753
8754 static bool
8755 swap_out_syms (bfd *abfd,
8756 struct elf_strtab_hash **sttp,
8757 int relocatable_p,
8758 struct bfd_link_info *info)
8759 {
8760 const struct elf_backend_data *bed;
8761 unsigned int symcount;
8762 asymbol **syms;
8763 struct elf_strtab_hash *stt;
8764 Elf_Internal_Shdr *symtab_hdr;
8765 Elf_Internal_Shdr *symtab_shndx_hdr;
8766 Elf_Internal_Shdr *symstrtab_hdr;
8767 struct elf_sym_strtab *symstrtab;
8768 bfd_byte *outbound_syms;
8769 bfd_byte *outbound_shndx;
8770 unsigned long outbound_syms_index;
8771 unsigned int idx;
8772 unsigned int num_locals;
8773 size_t amt;
8774 bool name_local_sections;
8775
8776 if (!elf_map_symbols (abfd, &num_locals))
8777 return false;
8778
8779 /* Dump out the symtabs. */
8780 stt = _bfd_elf_strtab_init ();
8781 if (stt == NULL)
8782 return false;
8783
8784 bed = get_elf_backend_data (abfd);
8785 symcount = bfd_get_symcount (abfd);
8786 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8787 symtab_hdr->sh_type = SHT_SYMTAB;
8788 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8789 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8790 symtab_hdr->sh_info = num_locals + 1;
8791 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8792
8793 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8794 symstrtab_hdr->sh_type = SHT_STRTAB;
8795
8796 /* Allocate buffer to swap out the .strtab section. */
8797 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8798 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8799 {
8800 bfd_set_error (bfd_error_no_memory);
8801 _bfd_elf_strtab_free (stt);
8802 return false;
8803 }
8804
8805 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8806 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8807 {
8808 error_no_mem:
8809 bfd_set_error (bfd_error_no_memory);
8810 error_return:
8811 free (symstrtab);
8812 _bfd_elf_strtab_free (stt);
8813 return false;
8814 }
8815 symtab_hdr->contents = outbound_syms;
8816 outbound_syms_index = 0;
8817
8818 outbound_shndx = NULL;
8819
8820 if (elf_symtab_shndx_list (abfd))
8821 {
8822 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8823 if (symtab_shndx_hdr->sh_name != 0)
8824 {
8825 if (_bfd_mul_overflow (symcount + 1,
8826 sizeof (Elf_External_Sym_Shndx), &amt))
8827 goto error_no_mem;
8828 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8829 if (outbound_shndx == NULL)
8830 goto error_return;
8831
8832 symtab_shndx_hdr->contents = outbound_shndx;
8833 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8834 symtab_shndx_hdr->sh_size = amt;
8835 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8836 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8837 }
8838 /* FIXME: What about any other headers in the list ? */
8839 }
8840
8841 /* Now generate the data (for "contents"). */
8842 {
8843 /* Fill in zeroth symbol and swap it out. */
8844 Elf_Internal_Sym sym;
8845 sym.st_name = 0;
8846 sym.st_value = 0;
8847 sym.st_size = 0;
8848 sym.st_info = 0;
8849 sym.st_other = 0;
8850 sym.st_shndx = SHN_UNDEF;
8851 sym.st_target_internal = 0;
8852 symstrtab[0].sym = sym;
8853 symstrtab[0].dest_index = outbound_syms_index;
8854 outbound_syms_index++;
8855 }
8856
8857 name_local_sections
8858 = (bed->elf_backend_name_local_section_symbols
8859 && bed->elf_backend_name_local_section_symbols (abfd));
8860
8861 syms = bfd_get_outsymbols (abfd);
8862 for (idx = 0; idx < symcount;)
8863 {
8864 Elf_Internal_Sym sym;
8865 bfd_vma value = syms[idx]->value;
8866 elf_symbol_type *type_ptr;
8867 flagword flags = syms[idx]->flags;
8868 int type;
8869
8870 if (!name_local_sections
8871 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8872 {
8873 /* Local section symbols have no name. */
8874 sym.st_name = (unsigned long) -1;
8875 }
8876 else
8877 {
8878 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8879 to get the final offset for st_name. */
8880 sym.st_name
8881 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8882 false);
8883 if (sym.st_name == (unsigned long) -1)
8884 goto error_return;
8885 }
8886
8887 type_ptr = elf_symbol_from (syms[idx]);
8888
8889 if ((flags & BSF_SECTION_SYM) == 0
8890 && bfd_is_com_section (syms[idx]->section))
8891 {
8892 /* ELF common symbols put the alignment into the `value' field,
8893 and the size into the `size' field. This is backwards from
8894 how BFD handles it, so reverse it here. */
8895 sym.st_size = value;
8896 if (type_ptr == NULL
8897 || type_ptr->internal_elf_sym.st_value == 0)
8898 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8899 else
8900 sym.st_value = type_ptr->internal_elf_sym.st_value;
8901 sym.st_shndx = _bfd_elf_section_from_bfd_section
8902 (abfd, syms[idx]->section);
8903 }
8904 else
8905 {
8906 asection *sec = syms[idx]->section;
8907 unsigned int shndx;
8908
8909 if (sec->output_section)
8910 {
8911 value += sec->output_offset;
8912 sec = sec->output_section;
8913 }
8914
8915 /* Don't add in the section vma for relocatable output. */
8916 if (! relocatable_p)
8917 value += sec->vma;
8918 sym.st_value = value;
8919 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8920
8921 if (bfd_is_abs_section (sec)
8922 && type_ptr != NULL
8923 && type_ptr->internal_elf_sym.st_shndx != 0)
8924 {
8925 /* This symbol is in a real ELF section which we did
8926 not create as a BFD section. Undo the mapping done
8927 by copy_private_symbol_data. */
8928 shndx = type_ptr->internal_elf_sym.st_shndx;
8929 switch (shndx)
8930 {
8931 case MAP_ONESYMTAB:
8932 shndx = elf_onesymtab (abfd);
8933 break;
8934 case MAP_DYNSYMTAB:
8935 shndx = elf_dynsymtab (abfd);
8936 break;
8937 case MAP_STRTAB:
8938 shndx = elf_strtab_sec (abfd);
8939 break;
8940 case MAP_SHSTRTAB:
8941 shndx = elf_shstrtab_sec (abfd);
8942 break;
8943 case MAP_SYM_SHNDX:
8944 if (elf_symtab_shndx_list (abfd))
8945 shndx = elf_symtab_shndx_list (abfd)->ndx;
8946 break;
8947 case SHN_COMMON:
8948 case SHN_ABS:
8949 shndx = SHN_ABS;
8950 break;
8951 default:
8952 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8953 {
8954 if (bed->symbol_section_index)
8955 shndx = bed->symbol_section_index (abfd, type_ptr);
8956 /* Otherwise just leave the index alone. */
8957 }
8958 else
8959 {
8960 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8961 _bfd_error_handler (_("%pB: \
8962 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8963 abfd, shndx);
8964 shndx = SHN_ABS;
8965 }
8966 break;
8967 }
8968 }
8969 else
8970 {
8971 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8972
8973 if (shndx == SHN_BAD)
8974 {
8975 asection *sec2;
8976
8977 /* Writing this would be a hell of a lot easier if
8978 we had some decent documentation on bfd, and
8979 knew what to expect of the library, and what to
8980 demand of applications. For example, it
8981 appears that `objcopy' might not set the
8982 section of a symbol to be a section that is
8983 actually in the output file. */
8984 sec2 = bfd_get_section_by_name (abfd, sec->name);
8985 if (sec2 != NULL)
8986 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8987 if (shndx == SHN_BAD)
8988 {
8989 /* xgettext:c-format */
8990 _bfd_error_handler
8991 (_("unable to find equivalent output section"
8992 " for symbol '%s' from section '%s'"),
8993 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8994 sec->name);
8995 bfd_set_error (bfd_error_invalid_operation);
8996 goto error_return;
8997 }
8998 }
8999 }
9000
9001 sym.st_shndx = shndx;
9002 }
9003
9004 if ((flags & BSF_THREAD_LOCAL) != 0)
9005 type = STT_TLS;
9006 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
9007 type = STT_GNU_IFUNC;
9008 else if ((flags & BSF_FUNCTION) != 0)
9009 type = STT_FUNC;
9010 else if ((flags & BSF_OBJECT) != 0)
9011 type = STT_OBJECT;
9012 else if ((flags & BSF_RELC) != 0)
9013 type = STT_RELC;
9014 else if ((flags & BSF_SRELC) != 0)
9015 type = STT_SRELC;
9016 else
9017 type = STT_NOTYPE;
9018
9019 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
9020 type = STT_TLS;
9021
9022 /* Processor-specific types. */
9023 if (type_ptr != NULL
9024 && bed->elf_backend_get_symbol_type)
9025 type = ((*bed->elf_backend_get_symbol_type)
9026 (&type_ptr->internal_elf_sym, type));
9027
9028 if (flags & BSF_SECTION_SYM)
9029 {
9030 if (flags & BSF_GLOBAL)
9031 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9032 else
9033 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9034 }
9035 else if (bfd_is_com_section (syms[idx]->section))
9036 {
9037 if (type != STT_TLS)
9038 {
9039 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9040 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9041 ? STT_COMMON : STT_OBJECT);
9042 else
9043 type = ((flags & BSF_ELF_COMMON) != 0
9044 ? STT_COMMON : STT_OBJECT);
9045 }
9046 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9047 }
9048 else if (bfd_is_und_section (syms[idx]->section))
9049 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9050 ? STB_WEAK
9051 : STB_GLOBAL),
9052 type);
9053 else if (flags & BSF_FILE)
9054 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9055 else
9056 {
9057 int bind = STB_LOCAL;
9058
9059 if (flags & BSF_LOCAL)
9060 bind = STB_LOCAL;
9061 else if (flags & BSF_GNU_UNIQUE)
9062 bind = STB_GNU_UNIQUE;
9063 else if (flags & BSF_WEAK)
9064 bind = STB_WEAK;
9065 else if (flags & BSF_GLOBAL)
9066 bind = STB_GLOBAL;
9067
9068 sym.st_info = ELF_ST_INFO (bind, type);
9069 }
9070
9071 if (type_ptr != NULL)
9072 {
9073 sym.st_other = type_ptr->internal_elf_sym.st_other;
9074 sym.st_target_internal
9075 = type_ptr->internal_elf_sym.st_target_internal;
9076 }
9077 else
9078 {
9079 sym.st_other = 0;
9080 sym.st_target_internal = 0;
9081 }
9082
9083 idx++;
9084 symstrtab[idx].sym = sym;
9085 symstrtab[idx].dest_index = outbound_syms_index;
9086
9087 outbound_syms_index++;
9088 }
9089
9090 /* Finalize the .strtab section. */
9091 _bfd_elf_strtab_finalize (stt);
9092
9093 /* Swap out the .strtab section. */
9094 for (idx = 0; idx <= symcount; idx++)
9095 {
9096 struct elf_sym_strtab *elfsym = &symstrtab[idx];
9097 if (elfsym->sym.st_name == (unsigned long) -1)
9098 elfsym->sym.st_name = 0;
9099 else
9100 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9101 elfsym->sym.st_name);
9102 if (info && info->callbacks->ctf_new_symbol)
9103 info->callbacks->ctf_new_symbol (elfsym->dest_index,
9104 &elfsym->sym);
9105
9106 /* Inform the linker of the addition of this symbol. */
9107
9108 bed->s->swap_symbol_out (abfd, &elfsym->sym,
9109 (outbound_syms
9110 + (elfsym->dest_index
9111 * bed->s->sizeof_sym)),
9112 NPTR_ADD (outbound_shndx,
9113 (elfsym->dest_index
9114 * sizeof (Elf_External_Sym_Shndx))));
9115 }
9116 free (symstrtab);
9117
9118 *sttp = stt;
9119 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9120 symstrtab_hdr->sh_type = SHT_STRTAB;
9121 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9122 symstrtab_hdr->sh_addr = 0;
9123 symstrtab_hdr->sh_entsize = 0;
9124 symstrtab_hdr->sh_link = 0;
9125 symstrtab_hdr->sh_info = 0;
9126 symstrtab_hdr->sh_addralign = 1;
9127
9128 return true;
9129 }
9130
9131 /* Return the number of bytes required to hold the symtab vector.
9132
9133 Note that we base it on the count plus 1, since we will null terminate
9134 the vector allocated based on this size. However, the ELF symbol table
9135 always has a dummy entry as symbol #0, so it ends up even. */
9136
9137 long
9138 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9139 {
9140 bfd_size_type symcount;
9141 long symtab_size;
9142 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9143
9144 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9145 if (symcount > LONG_MAX / sizeof (asymbol *))
9146 {
9147 bfd_set_error (bfd_error_file_too_big);
9148 return -1;
9149 }
9150 symtab_size = symcount * (sizeof (asymbol *));
9151 if (symcount == 0)
9152 symtab_size = sizeof (asymbol *);
9153 else if (!bfd_write_p (abfd))
9154 {
9155 ufile_ptr filesize = bfd_get_file_size (abfd);
9156
9157 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9158 {
9159 bfd_set_error (bfd_error_file_truncated);
9160 return -1;
9161 }
9162 }
9163
9164 return symtab_size;
9165 }
9166
9167 long
9168 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9169 {
9170 bfd_size_type symcount;
9171 long symtab_size;
9172 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9173
9174 if (elf_dynsymtab (abfd) == 0)
9175 {
9176 /* Check if there is dynamic symbol table. */
9177 symcount = elf_tdata (abfd)->dt_symtab_count;
9178 if (symcount)
9179 goto compute_symtab_size;
9180
9181 bfd_set_error (bfd_error_invalid_operation);
9182 return -1;
9183 }
9184
9185 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9186 if (symcount > LONG_MAX / sizeof (asymbol *))
9187 {
9188 bfd_set_error (bfd_error_file_too_big);
9189 return -1;
9190 }
9191
9192 compute_symtab_size:
9193 symtab_size = symcount * (sizeof (asymbol *));
9194 if (symcount == 0)
9195 symtab_size = sizeof (asymbol *);
9196 else if (!bfd_write_p (abfd))
9197 {
9198 ufile_ptr filesize = bfd_get_file_size (abfd);
9199
9200 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9201 {
9202 bfd_set_error (bfd_error_file_truncated);
9203 return -1;
9204 }
9205 }
9206
9207 return symtab_size;
9208 }
9209
9210 long
9211 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9212 {
9213 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9214 {
9215 /* Sanity check reloc section size. */
9216 ufile_ptr filesize = bfd_get_file_size (abfd);
9217
9218 if (filesize != 0)
9219 {
9220 struct bfd_elf_section_data *d = elf_section_data (asect);
9221 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9222 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9223
9224 if (rel_size + rela_size > filesize
9225 || rel_size + rela_size < rel_size)
9226 {
9227 bfd_set_error (bfd_error_file_truncated);
9228 return -1;
9229 }
9230 }
9231 }
9232
9233 #if SIZEOF_LONG == SIZEOF_INT
9234 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9235 {
9236 bfd_set_error (bfd_error_file_too_big);
9237 return -1;
9238 }
9239 #endif
9240 return (asect->reloc_count + 1L) * sizeof (arelent *);
9241 }
9242
9243 /* Canonicalize the relocs. */
9244
9245 long
9246 _bfd_elf_canonicalize_reloc (bfd *abfd,
9247 sec_ptr section,
9248 arelent **relptr,
9249 asymbol **symbols)
9250 {
9251 arelent *tblptr;
9252 unsigned int i;
9253 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9254
9255 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9256 return -1;
9257
9258 tblptr = section->relocation;
9259 for (i = 0; i < section->reloc_count; i++)
9260 *relptr++ = tblptr++;
9261
9262 *relptr = NULL;
9263
9264 return section->reloc_count;
9265 }
9266
9267 long
9268 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9269 {
9270 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9271 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9272
9273 if (symcount >= 0)
9274 abfd->symcount = symcount;
9275 return symcount;
9276 }
9277
9278 long
9279 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9280 asymbol **allocation)
9281 {
9282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9283 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9284
9285 if (symcount >= 0)
9286 abfd->dynsymcount = symcount;
9287 return symcount;
9288 }
9289
9290 /* Return the size required for the dynamic reloc entries. Any loadable
9291 section that was actually installed in the BFD, and has type SHT_REL
9292 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9293 dynamic reloc section. */
9294
9295 long
9296 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9297 {
9298 bfd_size_type count, ext_rel_size;
9299 asection *s;
9300
9301 if (elf_dynsymtab (abfd) == 0)
9302 {
9303 bfd_set_error (bfd_error_invalid_operation);
9304 return -1;
9305 }
9306
9307 count = 1;
9308 ext_rel_size = 0;
9309 for (s = abfd->sections; s != NULL; s = s->next)
9310 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9311 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9312 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9313 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9314 {
9315 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9316 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9317 {
9318 bfd_set_error (bfd_error_file_truncated);
9319 return -1;
9320 }
9321 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9322 if (count > LONG_MAX / sizeof (arelent *))
9323 {
9324 bfd_set_error (bfd_error_file_too_big);
9325 return -1;
9326 }
9327 }
9328 if (count > 1 && !bfd_write_p (abfd))
9329 {
9330 /* Sanity check reloc section sizes. */
9331 ufile_ptr filesize = bfd_get_file_size (abfd);
9332 if (filesize != 0 && ext_rel_size > filesize)
9333 {
9334 bfd_set_error (bfd_error_file_truncated);
9335 return -1;
9336 }
9337 }
9338 return count * sizeof (arelent *);
9339 }
9340
9341 /* Canonicalize the dynamic relocation entries. Note that we return the
9342 dynamic relocations as a single block, although they are actually
9343 associated with particular sections; the interface, which was
9344 designed for SunOS style shared libraries, expects that there is only
9345 one set of dynamic relocs. Any loadable section that was actually
9346 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9347 dynamic symbol table, is considered to be a dynamic reloc section. */
9348
9349 long
9350 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9351 arelent **storage,
9352 asymbol **syms)
9353 {
9354 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9355 asection *s;
9356 long ret;
9357
9358 if (elf_dynsymtab (abfd) == 0)
9359 {
9360 bfd_set_error (bfd_error_invalid_operation);
9361 return -1;
9362 }
9363
9364 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9365 ret = 0;
9366 for (s = abfd->sections; s != NULL; s = s->next)
9367 {
9368 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9369 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9370 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9371 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9372 {
9373 arelent *p;
9374 long count, i;
9375
9376 if (! (*slurp_relocs) (abfd, s, syms, true))
9377 return -1;
9378 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9379 p = s->relocation;
9380 for (i = 0; i < count; i++)
9381 *storage++ = p++;
9382 ret += count;
9383 }
9384 }
9385
9386 *storage = NULL;
9387
9388 return ret;
9389 }
9390 \f
9391 /* Read in the version information. */
9392
9393 bool
9394 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9395 {
9396 bfd_byte *contents = NULL;
9397 unsigned int freeidx = 0;
9398 size_t amt;
9399
9400 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9401 {
9402 Elf_Internal_Shdr *hdr;
9403 Elf_External_Verneed *everneed;
9404 Elf_Internal_Verneed *iverneed;
9405 unsigned int i;
9406 bfd_byte *contents_end;
9407 size_t verneed_count;
9408 size_t verneed_size;
9409
9410 if (elf_tdata (abfd)->dt_verneed != NULL)
9411 {
9412 hdr = NULL;
9413 contents = elf_tdata (abfd)->dt_verneed;
9414 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9415 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9416 }
9417 else
9418 {
9419 hdr = &elf_tdata (abfd)->dynverref_hdr;
9420
9421 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9422 {
9423 error_return_bad_verref:
9424 _bfd_error_handler
9425 (_("%pB: .gnu.version_r invalid entry"), abfd);
9426 bfd_set_error (bfd_error_bad_value);
9427 error_return_verref:
9428 elf_tdata (abfd)->verref = NULL;
9429 elf_tdata (abfd)->cverrefs = 0;
9430 goto error_return;
9431 }
9432
9433 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9434 goto error_return_verref;
9435 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9436 if (contents == NULL)
9437 goto error_return_verref;
9438
9439 verneed_size = hdr->sh_size;
9440 verneed_count = hdr->sh_info;
9441 }
9442
9443 if (_bfd_mul_overflow (verneed_count,
9444 sizeof (Elf_Internal_Verneed), &amt))
9445 {
9446 bfd_set_error (bfd_error_file_too_big);
9447 goto error_return_verref;
9448 }
9449 if (amt == 0)
9450 goto error_return_verref;
9451 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9452 if (elf_tdata (abfd)->verref == NULL)
9453 goto error_return_verref;
9454
9455 BFD_ASSERT (sizeof (Elf_External_Verneed)
9456 == sizeof (Elf_External_Vernaux));
9457 contents_end = (contents + verneed_size
9458 - sizeof (Elf_External_Verneed));
9459 everneed = (Elf_External_Verneed *) contents;
9460 iverneed = elf_tdata (abfd)->verref;
9461 for (i = 0; i < verneed_count; i++, iverneed++)
9462 {
9463 Elf_External_Vernaux *evernaux;
9464 Elf_Internal_Vernaux *ivernaux;
9465 unsigned int j;
9466
9467 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9468
9469 iverneed->vn_bfd = abfd;
9470
9471 if (elf_use_dt_symtab_p (abfd))
9472 iverneed->vn_filename
9473 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9474 else
9475 iverneed->vn_filename
9476 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9477 iverneed->vn_file);
9478 if (iverneed->vn_filename == NULL)
9479 goto error_return_bad_verref;
9480
9481 if (iverneed->vn_cnt == 0)
9482 iverneed->vn_auxptr = NULL;
9483 else
9484 {
9485 if (_bfd_mul_overflow (iverneed->vn_cnt,
9486 sizeof (Elf_Internal_Vernaux), &amt))
9487 {
9488 bfd_set_error (bfd_error_file_too_big);
9489 goto error_return_verref;
9490 }
9491 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9492 bfd_alloc (abfd, amt);
9493 if (iverneed->vn_auxptr == NULL)
9494 goto error_return_verref;
9495 }
9496
9497 if (iverneed->vn_aux
9498 > (size_t) (contents_end - (bfd_byte *) everneed))
9499 goto error_return_bad_verref;
9500
9501 evernaux = ((Elf_External_Vernaux *)
9502 ((bfd_byte *) everneed + iverneed->vn_aux));
9503 ivernaux = iverneed->vn_auxptr;
9504 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9505 {
9506 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9507
9508 if (elf_use_dt_symtab_p (abfd))
9509 ivernaux->vna_nodename
9510 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9511 else
9512 ivernaux->vna_nodename
9513 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9514 ivernaux->vna_name);
9515 if (ivernaux->vna_nodename == NULL)
9516 goto error_return_bad_verref;
9517
9518 if (ivernaux->vna_other > freeidx)
9519 freeidx = ivernaux->vna_other;
9520
9521 ivernaux->vna_nextptr = NULL;
9522 if (ivernaux->vna_next == 0)
9523 {
9524 iverneed->vn_cnt = j + 1;
9525 break;
9526 }
9527 if (j + 1 < iverneed->vn_cnt)
9528 ivernaux->vna_nextptr = ivernaux + 1;
9529
9530 if (ivernaux->vna_next
9531 > (size_t) (contents_end - (bfd_byte *) evernaux))
9532 goto error_return_bad_verref;
9533
9534 evernaux = ((Elf_External_Vernaux *)
9535 ((bfd_byte *) evernaux + ivernaux->vna_next));
9536 }
9537
9538 iverneed->vn_nextref = NULL;
9539 if (iverneed->vn_next == 0)
9540 break;
9541 if (i + 1 < hdr->sh_info)
9542 iverneed->vn_nextref = iverneed + 1;
9543
9544 if (iverneed->vn_next
9545 > (size_t) (contents_end - (bfd_byte *) everneed))
9546 goto error_return_bad_verref;
9547
9548 everneed = ((Elf_External_Verneed *)
9549 ((bfd_byte *) everneed + iverneed->vn_next));
9550 }
9551 elf_tdata (abfd)->cverrefs = i;
9552
9553 if (elf_tdata (abfd)->dt_verneed == NULL)
9554 free (contents);
9555 contents = NULL;
9556 }
9557
9558 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9559 {
9560 Elf_Internal_Shdr *hdr;
9561 Elf_External_Verdef *everdef;
9562 Elf_Internal_Verdef *iverdef;
9563 Elf_Internal_Verdef *iverdefarr;
9564 Elf_Internal_Verdef iverdefmem;
9565 unsigned int i;
9566 unsigned int maxidx;
9567 bfd_byte *contents_end_def, *contents_end_aux;
9568 size_t verdef_count;
9569 size_t verdef_size;
9570
9571 if (elf_tdata (abfd)->dt_verdef != NULL)
9572 {
9573 hdr = NULL;
9574 contents = elf_tdata (abfd)->dt_verdef;
9575 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9576 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9577 }
9578 else
9579 {
9580 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9581
9582 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9583 {
9584 error_return_bad_verdef:
9585 _bfd_error_handler
9586 (_("%pB: .gnu.version_d invalid entry"), abfd);
9587 bfd_set_error (bfd_error_bad_value);
9588 error_return_verdef:
9589 elf_tdata (abfd)->verdef = NULL;
9590 elf_tdata (abfd)->cverdefs = 0;
9591 goto error_return;
9592 }
9593
9594 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9595 goto error_return_verdef;
9596 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9597 if (contents == NULL)
9598 goto error_return_verdef;
9599
9600 BFD_ASSERT (sizeof (Elf_External_Verdef)
9601 >= sizeof (Elf_External_Verdaux));
9602
9603 verdef_count = hdr->sh_info;
9604 verdef_size = hdr->sh_size;
9605 }
9606
9607 contents_end_def = (contents + verdef_size
9608 - sizeof (Elf_External_Verdef));
9609 contents_end_aux = (contents + verdef_size
9610 - sizeof (Elf_External_Verdaux));
9611
9612 /* We know the number of entries in the section but not the maximum
9613 index. Therefore we have to run through all entries and find
9614 the maximum. */
9615 everdef = (Elf_External_Verdef *) contents;
9616 maxidx = 0;
9617 for (i = 0; i < verdef_count; ++i)
9618 {
9619 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9620
9621 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9622 goto error_return_bad_verdef;
9623 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9624 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9625
9626 if (iverdefmem.vd_next == 0)
9627 break;
9628
9629 if (iverdefmem.vd_next
9630 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9631 goto error_return_bad_verdef;
9632
9633 everdef = ((Elf_External_Verdef *)
9634 ((bfd_byte *) everdef + iverdefmem.vd_next));
9635 }
9636
9637 if (default_imported_symver)
9638 {
9639 if (freeidx > maxidx)
9640 maxidx = ++freeidx;
9641 else
9642 freeidx = ++maxidx;
9643 }
9644 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9645 {
9646 bfd_set_error (bfd_error_file_too_big);
9647 goto error_return_verdef;
9648 }
9649
9650 if (amt == 0)
9651 goto error_return_verdef;
9652 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9653 if (elf_tdata (abfd)->verdef == NULL)
9654 goto error_return_verdef;
9655
9656 elf_tdata (abfd)->cverdefs = maxidx;
9657
9658 everdef = (Elf_External_Verdef *) contents;
9659 iverdefarr = elf_tdata (abfd)->verdef;
9660 for (i = 0; i < verdef_count; ++i)
9661 {
9662 Elf_External_Verdaux *everdaux;
9663 Elf_Internal_Verdaux *iverdaux;
9664 unsigned int j;
9665
9666 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9667
9668 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9669 goto error_return_bad_verdef;
9670
9671 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9672 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9673
9674 iverdef->vd_bfd = abfd;
9675
9676 if (iverdef->vd_cnt == 0)
9677 iverdef->vd_auxptr = NULL;
9678 else
9679 {
9680 if (_bfd_mul_overflow (iverdef->vd_cnt,
9681 sizeof (Elf_Internal_Verdaux), &amt))
9682 {
9683 bfd_set_error (bfd_error_file_too_big);
9684 goto error_return_verdef;
9685 }
9686 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9687 bfd_alloc (abfd, amt);
9688 if (iverdef->vd_auxptr == NULL)
9689 goto error_return_verdef;
9690 }
9691
9692 if (iverdef->vd_aux
9693 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9694 goto error_return_bad_verdef;
9695
9696 everdaux = ((Elf_External_Verdaux *)
9697 ((bfd_byte *) everdef + iverdef->vd_aux));
9698 iverdaux = iverdef->vd_auxptr;
9699 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9700 {
9701 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9702
9703 if (elf_use_dt_symtab_p (abfd))
9704 iverdaux->vda_nodename
9705 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9706 else
9707 iverdaux->vda_nodename
9708 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9709 iverdaux->vda_name);
9710 if (iverdaux->vda_nodename == NULL)
9711 goto error_return_bad_verdef;
9712
9713 iverdaux->vda_nextptr = NULL;
9714 if (iverdaux->vda_next == 0)
9715 {
9716 iverdef->vd_cnt = j + 1;
9717 break;
9718 }
9719 if (j + 1 < iverdef->vd_cnt)
9720 iverdaux->vda_nextptr = iverdaux + 1;
9721
9722 if (iverdaux->vda_next
9723 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9724 goto error_return_bad_verdef;
9725
9726 everdaux = ((Elf_External_Verdaux *)
9727 ((bfd_byte *) everdaux + iverdaux->vda_next));
9728 }
9729
9730 iverdef->vd_nodename = NULL;
9731 if (iverdef->vd_cnt)
9732 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9733
9734 iverdef->vd_nextdef = NULL;
9735 if (iverdef->vd_next == 0)
9736 break;
9737 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9738 iverdef->vd_nextdef = iverdef + 1;
9739
9740 everdef = ((Elf_External_Verdef *)
9741 ((bfd_byte *) everdef + iverdef->vd_next));
9742 }
9743
9744 if (elf_tdata (abfd)->dt_verdef == NULL)
9745 free (contents);
9746 contents = NULL;
9747 }
9748 else if (default_imported_symver)
9749 {
9750 if (freeidx < 3)
9751 freeidx = 3;
9752 else
9753 freeidx++;
9754
9755 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9756 {
9757 bfd_set_error (bfd_error_file_too_big);
9758 goto error_return;
9759 }
9760 if (amt == 0)
9761 goto error_return;
9762 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9763 if (elf_tdata (abfd)->verdef == NULL)
9764 goto error_return;
9765
9766 elf_tdata (abfd)->cverdefs = freeidx;
9767 }
9768
9769 /* Create a default version based on the soname. */
9770 if (default_imported_symver)
9771 {
9772 Elf_Internal_Verdef *iverdef;
9773 Elf_Internal_Verdaux *iverdaux;
9774
9775 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9776
9777 iverdef->vd_version = VER_DEF_CURRENT;
9778 iverdef->vd_flags = 0;
9779 iverdef->vd_ndx = freeidx;
9780 iverdef->vd_cnt = 1;
9781
9782 iverdef->vd_bfd = abfd;
9783
9784 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9785 if (iverdef->vd_nodename == NULL)
9786 goto error_return_verdef;
9787 iverdef->vd_nextdef = NULL;
9788 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9789 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9790 if (iverdef->vd_auxptr == NULL)
9791 goto error_return_verdef;
9792
9793 iverdaux = iverdef->vd_auxptr;
9794 iverdaux->vda_nodename = iverdef->vd_nodename;
9795 }
9796
9797 return true;
9798
9799 error_return:
9800 free (contents);
9801 return false;
9802 }
9803 \f
9804 asymbol *
9805 _bfd_elf_make_empty_symbol (bfd *abfd)
9806 {
9807 elf_symbol_type *newsym;
9808
9809 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9810 if (!newsym)
9811 return NULL;
9812 newsym->symbol.the_bfd = abfd;
9813 return &newsym->symbol;
9814 }
9815
9816 void
9817 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9818 asymbol *symbol,
9819 symbol_info *ret)
9820 {
9821 bfd_symbol_info (symbol, ret);
9822 }
9823
9824 /* Return whether a symbol name implies a local symbol. Most targets
9825 use this function for the is_local_label_name entry point, but some
9826 override it. */
9827
9828 bool
9829 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9830 const char *name)
9831 {
9832 /* Normal local symbols start with ``.L''. */
9833 if (name[0] == '.' && name[1] == 'L')
9834 return true;
9835
9836 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9837 DWARF debugging symbols starting with ``..''. */
9838 if (name[0] == '.' && name[1] == '.')
9839 return true;
9840
9841 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9842 emitting DWARF debugging output. I suspect this is actually a
9843 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9844 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9845 underscore to be emitted on some ELF targets). For ease of use,
9846 we treat such symbols as local. */
9847 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9848 return true;
9849
9850 /* Treat assembler generated fake symbols, dollar local labels and
9851 forward-backward labels (aka local labels) as locals.
9852 These labels have the form:
9853
9854 L0^A.* (fake symbols)
9855
9856 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9857
9858 Versions which start with .L will have already been matched above,
9859 so we only need to match the rest. */
9860 if (name[0] == 'L' && ISDIGIT (name[1]))
9861 {
9862 bool ret = false;
9863 const char * p;
9864 char c;
9865
9866 for (p = name + 2; (c = *p); p++)
9867 {
9868 if (c == 1 || c == 2)
9869 {
9870 if (c == 1 && p == name + 2)
9871 /* A fake symbol. */
9872 return true;
9873
9874 /* FIXME: We are being paranoid here and treating symbols like
9875 L0^Bfoo as if there were non-local, on the grounds that the
9876 assembler will never generate them. But can any symbol
9877 containing an ASCII value in the range 1-31 ever be anything
9878 other than some kind of local ? */
9879 ret = true;
9880 }
9881
9882 if (! ISDIGIT (c))
9883 {
9884 ret = false;
9885 break;
9886 }
9887 }
9888 return ret;
9889 }
9890
9891 return false;
9892 }
9893
9894 alent *
9895 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9896 asymbol *symbol ATTRIBUTE_UNUSED)
9897 {
9898 abort ();
9899 return NULL;
9900 }
9901
9902 bool
9903 _bfd_elf_set_arch_mach (bfd *abfd,
9904 enum bfd_architecture arch,
9905 unsigned long machine)
9906 {
9907 /* If this isn't the right architecture for this backend, and this
9908 isn't the generic backend, fail. */
9909 if (arch != get_elf_backend_data (abfd)->arch
9910 && arch != bfd_arch_unknown
9911 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9912 return false;
9913
9914 return bfd_default_set_arch_mach (abfd, arch, machine);
9915 }
9916
9917 /* Find the nearest line to a particular section and offset,
9918 for error reporting. */
9919
9920 bool
9921 _bfd_elf_find_nearest_line (bfd *abfd,
9922 asymbol **symbols,
9923 asection *section,
9924 bfd_vma offset,
9925 const char **filename_ptr,
9926 const char **functionname_ptr,
9927 unsigned int *line_ptr,
9928 unsigned int *discriminator_ptr)
9929 {
9930 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9931 offset, filename_ptr,
9932 functionname_ptr, line_ptr,
9933 discriminator_ptr);
9934 }
9935
9936 /* Find the nearest line to a particular section and offset,
9937 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9938 can be optionally specified. */
9939
9940 bool
9941 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9942 const char *alt_filename,
9943 asymbol **symbols,
9944 asection *section,
9945 bfd_vma offset,
9946 const char **filename_ptr,
9947 const char **functionname_ptr,
9948 unsigned int *line_ptr,
9949 unsigned int *discriminator_ptr)
9950 {
9951 bool found;
9952
9953 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9954 section, offset, filename_ptr,
9955 functionname_ptr, line_ptr,
9956 discriminator_ptr,
9957 dwarf_debug_sections,
9958 &elf_tdata (abfd)->dwarf2_find_line_info))
9959 return true;
9960
9961 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9962 filename_ptr, functionname_ptr, line_ptr))
9963 {
9964 if (!*functionname_ptr)
9965 _bfd_elf_find_function (abfd, symbols, section, offset,
9966 *filename_ptr ? NULL : filename_ptr,
9967 functionname_ptr);
9968 return true;
9969 }
9970
9971 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9972 &found, filename_ptr,
9973 functionname_ptr, line_ptr,
9974 &elf_tdata (abfd)->line_info))
9975 return false;
9976 if (found && (*functionname_ptr || *line_ptr))
9977 return true;
9978
9979 if (symbols == NULL)
9980 return false;
9981
9982 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9983 filename_ptr, functionname_ptr))
9984 return false;
9985
9986 *line_ptr = 0;
9987 return true;
9988 }
9989
9990 /* Find the line for a symbol. */
9991
9992 bool
9993 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9994 const char **filename_ptr, unsigned int *line_ptr)
9995 {
9996 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9997 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9998 filename_ptr, NULL, line_ptr, NULL,
9999 dwarf_debug_sections,
10000 &tdata->dwarf2_find_line_info);
10001 }
10002
10003 /* After a call to bfd_find_nearest_line, successive calls to
10004 bfd_find_inliner_info can be used to get source information about
10005 each level of function inlining that terminated at the address
10006 passed to bfd_find_nearest_line. Currently this is only supported
10007 for DWARF2 with appropriate DWARF3 extensions. */
10008
10009 bool
10010 _bfd_elf_find_inliner_info (bfd *abfd,
10011 const char **filename_ptr,
10012 const char **functionname_ptr,
10013 unsigned int *line_ptr)
10014 {
10015 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10016 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10017 functionname_ptr, line_ptr,
10018 &tdata->dwarf2_find_line_info);
10019 }
10020
10021 int
10022 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10023 {
10024 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10025 int ret = bed->s->sizeof_ehdr;
10026
10027 if (!bfd_link_relocatable (info))
10028 {
10029 bfd_size_type phdr_size = elf_program_header_size (abfd);
10030
10031 if (phdr_size == (bfd_size_type) -1)
10032 {
10033 struct elf_segment_map *m;
10034
10035 phdr_size = 0;
10036 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10037 phdr_size += bed->s->sizeof_phdr;
10038
10039 if (phdr_size == 0)
10040 phdr_size = get_program_header_size (abfd, info);
10041 }
10042
10043 elf_program_header_size (abfd) = phdr_size;
10044 ret += phdr_size;
10045 }
10046
10047 return ret;
10048 }
10049
10050 bool
10051 _bfd_elf_set_section_contents (bfd *abfd,
10052 sec_ptr section,
10053 const void *location,
10054 file_ptr offset,
10055 bfd_size_type count)
10056 {
10057 Elf_Internal_Shdr *hdr;
10058
10059 if (! abfd->output_has_begun
10060 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10061 return false;
10062
10063 if (!count)
10064 return true;
10065
10066 hdr = &elf_section_data (section)->this_hdr;
10067 if (hdr->sh_offset == (file_ptr) -1)
10068 {
10069 unsigned char *contents;
10070
10071 if (bfd_section_is_ctf (section))
10072 /* Nothing to do with this section: the contents are generated
10073 later. */
10074 return true;
10075
10076 if ((offset + count) > hdr->sh_size)
10077 {
10078 _bfd_error_handler
10079 (_("%pB:%pA: error: attempting to write"
10080 " over the end of the section"),
10081 abfd, section);
10082
10083 bfd_set_error (bfd_error_invalid_operation);
10084 return false;
10085 }
10086
10087 contents = hdr->contents;
10088 if (contents == NULL)
10089 {
10090 _bfd_error_handler
10091 (_("%pB:%pA: error: attempting to write"
10092 " section into an empty buffer"),
10093 abfd, section);
10094
10095 bfd_set_error (bfd_error_invalid_operation);
10096 return false;
10097 }
10098
10099 memcpy (contents + offset, location, count);
10100 return true;
10101 }
10102
10103 return _bfd_generic_set_section_contents (abfd, section,
10104 location, offset, count);
10105 }
10106
10107 bool
10108 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10109 arelent *cache_ptr ATTRIBUTE_UNUSED,
10110 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10111 {
10112 abort ();
10113 return false;
10114 }
10115
10116 /* Try to convert a non-ELF reloc into an ELF one. */
10117
10118 bool
10119 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10120 {
10121 /* Check whether we really have an ELF howto. */
10122
10123 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10124 {
10125 bfd_reloc_code_real_type code;
10126 reloc_howto_type *howto;
10127
10128 /* Alien reloc: Try to determine its type to replace it with an
10129 equivalent ELF reloc. */
10130
10131 if (areloc->howto->pc_relative)
10132 {
10133 switch (areloc->howto->bitsize)
10134 {
10135 case 8:
10136 code = BFD_RELOC_8_PCREL;
10137 break;
10138 case 12:
10139 code = BFD_RELOC_12_PCREL;
10140 break;
10141 case 16:
10142 code = BFD_RELOC_16_PCREL;
10143 break;
10144 case 24:
10145 code = BFD_RELOC_24_PCREL;
10146 break;
10147 case 32:
10148 code = BFD_RELOC_32_PCREL;
10149 break;
10150 case 64:
10151 code = BFD_RELOC_64_PCREL;
10152 break;
10153 default:
10154 goto fail;
10155 }
10156
10157 howto = bfd_reloc_type_lookup (abfd, code);
10158
10159 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10160 {
10161 if (howto->pcrel_offset)
10162 areloc->addend += areloc->address;
10163 else
10164 areloc->addend -= areloc->address; /* addend is unsigned!! */
10165 }
10166 }
10167 else
10168 {
10169 switch (areloc->howto->bitsize)
10170 {
10171 case 8:
10172 code = BFD_RELOC_8;
10173 break;
10174 case 14:
10175 code = BFD_RELOC_14;
10176 break;
10177 case 16:
10178 code = BFD_RELOC_16;
10179 break;
10180 case 26:
10181 code = BFD_RELOC_26;
10182 break;
10183 case 32:
10184 code = BFD_RELOC_32;
10185 break;
10186 case 64:
10187 code = BFD_RELOC_64;
10188 break;
10189 default:
10190 goto fail;
10191 }
10192
10193 howto = bfd_reloc_type_lookup (abfd, code);
10194 }
10195
10196 if (howto)
10197 areloc->howto = howto;
10198 else
10199 goto fail;
10200 }
10201
10202 return true;
10203
10204 fail:
10205 /* xgettext:c-format */
10206 _bfd_error_handler (_("%pB: %s unsupported"),
10207 abfd, areloc->howto->name);
10208 bfd_set_error (bfd_error_sorry);
10209 return false;
10210 }
10211
10212 bool
10213 _bfd_elf_free_cached_info (bfd *abfd)
10214 {
10215 struct elf_obj_tdata *tdata;
10216
10217 if ((bfd_get_format (abfd) == bfd_object
10218 || bfd_get_format (abfd) == bfd_core)
10219 && (tdata = elf_tdata (abfd)) != NULL)
10220 {
10221 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10222 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10223 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10224 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10225 _bfd_stab_cleanup (abfd, &tdata->line_info);
10226 }
10227
10228 return _bfd_generic_bfd_free_cached_info (abfd);
10229 }
10230
10231 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10232 in the relocation's offset. Thus we cannot allow any sort of sanity
10233 range-checking to interfere. There is nothing else to do in processing
10234 this reloc. */
10235
10236 bfd_reloc_status_type
10237 _bfd_elf_rel_vtable_reloc_fn
10238 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10239 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10240 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10241 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10242 {
10243 return bfd_reloc_ok;
10244 }
10245 \f
10246 /* Elf core file support. Much of this only works on native
10247 toolchains, since we rely on knowing the
10248 machine-dependent procfs structure in order to pick
10249 out details about the corefile. */
10250
10251 #ifdef HAVE_SYS_PROCFS_H
10252 # include <sys/procfs.h>
10253 #endif
10254
10255 /* Return a PID that identifies a "thread" for threaded cores, or the
10256 PID of the main process for non-threaded cores. */
10257
10258 static int
10259 elfcore_make_pid (bfd *abfd)
10260 {
10261 int pid;
10262
10263 pid = elf_tdata (abfd)->core->lwpid;
10264 if (pid == 0)
10265 pid = elf_tdata (abfd)->core->pid;
10266
10267 return pid;
10268 }
10269
10270 /* If there isn't a section called NAME, make one, using data from
10271 SECT. Note, this function will generate a reference to NAME, so
10272 you shouldn't deallocate or overwrite it. */
10273
10274 static bool
10275 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10276 {
10277 asection *sect2;
10278
10279 if (bfd_get_section_by_name (abfd, name) != NULL)
10280 return true;
10281
10282 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10283 if (sect2 == NULL)
10284 return false;
10285
10286 sect2->size = sect->size;
10287 sect2->filepos = sect->filepos;
10288 sect2->alignment_power = sect->alignment_power;
10289 return true;
10290 }
10291
10292 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10293 actually creates up to two pseudosections:
10294 - For the single-threaded case, a section named NAME, unless
10295 such a section already exists.
10296 - For the multi-threaded case, a section named "NAME/PID", where
10297 PID is elfcore_make_pid (abfd).
10298 Both pseudosections have identical contents. */
10299 bool
10300 _bfd_elfcore_make_pseudosection (bfd *abfd,
10301 char *name,
10302 size_t size,
10303 ufile_ptr filepos)
10304 {
10305 char buf[100];
10306 char *threaded_name;
10307 size_t len;
10308 asection *sect;
10309
10310 /* Build the section name. */
10311
10312 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10313 len = strlen (buf) + 1;
10314 threaded_name = (char *) bfd_alloc (abfd, len);
10315 if (threaded_name == NULL)
10316 return false;
10317 memcpy (threaded_name, buf, len);
10318
10319 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10320 SEC_HAS_CONTENTS);
10321 if (sect == NULL)
10322 return false;
10323 sect->size = size;
10324 sect->filepos = filepos;
10325 sect->alignment_power = 2;
10326
10327 return elfcore_maybe_make_sect (abfd, name, sect);
10328 }
10329
10330 static bool
10331 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10332 size_t offs)
10333 {
10334 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10335 SEC_HAS_CONTENTS);
10336
10337 if (sect == NULL)
10338 return false;
10339
10340 sect->size = note->descsz - offs;
10341 sect->filepos = note->descpos + offs;
10342 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10343
10344 return true;
10345 }
10346
10347 /* prstatus_t exists on:
10348 solaris 2.5+
10349 linux 2.[01] + glibc
10350 unixware 4.2
10351 */
10352
10353 #if defined (HAVE_PRSTATUS_T)
10354
10355 static bool
10356 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10357 {
10358 size_t size;
10359 int offset;
10360
10361 if (note->descsz == sizeof (prstatus_t))
10362 {
10363 prstatus_t prstat;
10364
10365 size = sizeof (prstat.pr_reg);
10366 offset = offsetof (prstatus_t, pr_reg);
10367 memcpy (&prstat, note->descdata, sizeof (prstat));
10368
10369 /* Do not overwrite the core signal if it
10370 has already been set by another thread. */
10371 if (elf_tdata (abfd)->core->signal == 0)
10372 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10373 if (elf_tdata (abfd)->core->pid == 0)
10374 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10375
10376 /* pr_who exists on:
10377 solaris 2.5+
10378 unixware 4.2
10379 pr_who doesn't exist on:
10380 linux 2.[01]
10381 */
10382 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10383 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10384 #else
10385 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10386 #endif
10387 }
10388 #if defined (HAVE_PRSTATUS32_T)
10389 else if (note->descsz == sizeof (prstatus32_t))
10390 {
10391 /* 64-bit host, 32-bit corefile */
10392 prstatus32_t prstat;
10393
10394 size = sizeof (prstat.pr_reg);
10395 offset = offsetof (prstatus32_t, pr_reg);
10396 memcpy (&prstat, note->descdata, sizeof (prstat));
10397
10398 /* Do not overwrite the core signal if it
10399 has already been set by another thread. */
10400 if (elf_tdata (abfd)->core->signal == 0)
10401 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10402 if (elf_tdata (abfd)->core->pid == 0)
10403 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10404
10405 /* pr_who exists on:
10406 solaris 2.5+
10407 unixware 4.2
10408 pr_who doesn't exist on:
10409 linux 2.[01]
10410 */
10411 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10412 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10413 #else
10414 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10415 #endif
10416 }
10417 #endif /* HAVE_PRSTATUS32_T */
10418 else
10419 {
10420 /* Fail - we don't know how to handle any other
10421 note size (ie. data object type). */
10422 return true;
10423 }
10424
10425 /* Make a ".reg/999" section and a ".reg" section. */
10426 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10427 size, note->descpos + offset);
10428 }
10429 #endif /* defined (HAVE_PRSTATUS_T) */
10430
10431 /* Create a pseudosection containing the exact contents of NOTE. */
10432 static bool
10433 elfcore_make_note_pseudosection (bfd *abfd,
10434 char *name,
10435 Elf_Internal_Note *note)
10436 {
10437 return _bfd_elfcore_make_pseudosection (abfd, name,
10438 note->descsz, note->descpos);
10439 }
10440
10441 /* There isn't a consistent prfpregset_t across platforms,
10442 but it doesn't matter, because we don't have to pick this
10443 data structure apart. */
10444
10445 static bool
10446 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10447 {
10448 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10449 }
10450
10451 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10452 type of NT_PRXFPREG. Just include the whole note's contents
10453 literally. */
10454
10455 static bool
10456 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10457 {
10458 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10459 }
10460
10461 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10462 with a note type of NT_X86_XSTATE. Just include the whole note's
10463 contents literally. */
10464
10465 static bool
10466 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10467 {
10468 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10469 }
10470
10471 static bool
10472 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10473 {
10474 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10475 }
10476
10477 static bool
10478 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10479 {
10480 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10481 }
10482
10483 static bool
10484 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10485 {
10486 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10487 }
10488
10489 static bool
10490 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10491 {
10492 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10493 }
10494
10495 static bool
10496 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10497 {
10498 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10499 }
10500
10501 static bool
10502 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10503 {
10504 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10505 }
10506
10507 static bool
10508 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10509 {
10510 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10511 }
10512
10513 static bool
10514 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10515 {
10516 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10517 }
10518
10519 static bool
10520 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10521 {
10522 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10523 }
10524
10525 static bool
10526 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10527 {
10528 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10529 }
10530
10531 static bool
10532 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10533 {
10534 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10535 }
10536
10537 static bool
10538 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10539 {
10540 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10541 }
10542
10543 static bool
10544 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10545 {
10546 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10547 }
10548
10549 static bool
10550 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10551 {
10552 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10553 }
10554
10555 static bool
10556 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10557 {
10558 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10559 }
10560
10561 static bool
10562 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10563 {
10564 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10565 }
10566
10567 static bool
10568 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10569 {
10570 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10571 }
10572
10573 static bool
10574 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10575 {
10576 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10577 }
10578
10579 static bool
10580 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10581 {
10582 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10583 }
10584
10585 static bool
10586 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10587 {
10588 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10589 }
10590
10591 static bool
10592 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10593 {
10594 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10595 }
10596
10597 static bool
10598 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10599 {
10600 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10601 }
10602
10603 static bool
10604 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10605 {
10606 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10607 }
10608
10609 static bool
10610 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10611 {
10612 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10613 }
10614
10615 static bool
10616 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10617 {
10618 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10619 }
10620
10621 static bool
10622 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10623 {
10624 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10625 }
10626
10627 static bool
10628 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10629 {
10630 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10631 }
10632
10633 static bool
10634 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10635 {
10636 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10637 }
10638
10639 static bool
10640 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10641 {
10642 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10643 }
10644
10645 static bool
10646 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10647 {
10648 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10649 }
10650
10651 static bool
10652 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10653 {
10654 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10655 }
10656
10657 static bool
10658 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10659 {
10660 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10661 }
10662
10663 static bool
10664 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10665 {
10666 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10667 }
10668
10669 static bool
10670 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10671 {
10672 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10673 }
10674
10675 static bool
10676 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10677 {
10678 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10679 note);
10680 }
10681
10682 static bool
10683 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10684 {
10685 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10686 }
10687
10688 static bool
10689 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10690 {
10691 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10692 }
10693
10694 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10695 successful, otherwise return FALSE. */
10696
10697 static bool
10698 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10699 {
10700 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10701 }
10702
10703 static bool
10704 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10705 {
10706 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10707 }
10708
10709 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10710 successful otherwise, return FALSE. */
10711
10712 static bool
10713 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10714 {
10715 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10716 }
10717
10718 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10719 successful otherwise, return FALSE. */
10720
10721 static bool
10722 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10723 {
10724 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10725 }
10726
10727 static bool
10728 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10729 {
10730 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10731 }
10732
10733 static bool
10734 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10735 {
10736 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10737 }
10738
10739 static bool
10740 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10741 {
10742 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10743 }
10744
10745 static bool
10746 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10747 {
10748 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10749 }
10750
10751 #if defined (HAVE_PRPSINFO_T)
10752 typedef prpsinfo_t elfcore_psinfo_t;
10753 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10754 typedef prpsinfo32_t elfcore_psinfo32_t;
10755 #endif
10756 #endif
10757
10758 #if defined (HAVE_PSINFO_T)
10759 typedef psinfo_t elfcore_psinfo_t;
10760 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10761 typedef psinfo32_t elfcore_psinfo32_t;
10762 #endif
10763 #endif
10764
10765 /* return a malloc'ed copy of a string at START which is at
10766 most MAX bytes long, possibly without a terminating '\0'.
10767 the copy will always have a terminating '\0'. */
10768
10769 char *
10770 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10771 {
10772 char *dups;
10773 char *end = (char *) memchr (start, '\0', max);
10774 size_t len;
10775
10776 if (end == NULL)
10777 len = max;
10778 else
10779 len = end - start;
10780
10781 dups = (char *) bfd_alloc (abfd, len + 1);
10782 if (dups == NULL)
10783 return NULL;
10784
10785 memcpy (dups, start, len);
10786 dups[len] = '\0';
10787
10788 return dups;
10789 }
10790
10791 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10792 static bool
10793 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10794 {
10795 if (note->descsz == sizeof (elfcore_psinfo_t))
10796 {
10797 elfcore_psinfo_t psinfo;
10798
10799 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10800
10801 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10802 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10803 #endif
10804 elf_tdata (abfd)->core->program
10805 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10806 sizeof (psinfo.pr_fname));
10807
10808 elf_tdata (abfd)->core->command
10809 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10810 sizeof (psinfo.pr_psargs));
10811 }
10812 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10813 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10814 {
10815 /* 64-bit host, 32-bit corefile */
10816 elfcore_psinfo32_t psinfo;
10817
10818 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10819
10820 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10821 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10822 #endif
10823 elf_tdata (abfd)->core->program
10824 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10825 sizeof (psinfo.pr_fname));
10826
10827 elf_tdata (abfd)->core->command
10828 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10829 sizeof (psinfo.pr_psargs));
10830 }
10831 #endif
10832
10833 else
10834 {
10835 /* Fail - we don't know how to handle any other
10836 note size (ie. data object type). */
10837 return true;
10838 }
10839
10840 /* Note that for some reason, a spurious space is tacked
10841 onto the end of the args in some (at least one anyway)
10842 implementations, so strip it off if it exists. */
10843
10844 {
10845 char *command = elf_tdata (abfd)->core->command;
10846 int n = strlen (command);
10847
10848 if (0 < n && command[n - 1] == ' ')
10849 command[n - 1] = '\0';
10850 }
10851
10852 return true;
10853 }
10854 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10855
10856 #if defined (HAVE_PSTATUS_T)
10857 static bool
10858 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10859 {
10860 if (note->descsz == sizeof (pstatus_t)
10861 #if defined (HAVE_PXSTATUS_T)
10862 || note->descsz == sizeof (pxstatus_t)
10863 #endif
10864 )
10865 {
10866 pstatus_t pstat;
10867
10868 memcpy (&pstat, note->descdata, sizeof (pstat));
10869
10870 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10871 }
10872 #if defined (HAVE_PSTATUS32_T)
10873 else if (note->descsz == sizeof (pstatus32_t))
10874 {
10875 /* 64-bit host, 32-bit corefile */
10876 pstatus32_t pstat;
10877
10878 memcpy (&pstat, note->descdata, sizeof (pstat));
10879
10880 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10881 }
10882 #endif
10883 /* Could grab some more details from the "representative"
10884 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10885 NT_LWPSTATUS note, presumably. */
10886
10887 return true;
10888 }
10889 #endif /* defined (HAVE_PSTATUS_T) */
10890
10891 #if defined (HAVE_LWPSTATUS_T)
10892 static bool
10893 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10894 {
10895 lwpstatus_t lwpstat;
10896 char buf[100];
10897 char *name;
10898 size_t len;
10899 asection *sect;
10900
10901 if (note->descsz != sizeof (lwpstat)
10902 #if defined (HAVE_LWPXSTATUS_T)
10903 && note->descsz != sizeof (lwpxstatus_t)
10904 #endif
10905 )
10906 return true;
10907
10908 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10909
10910 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10911 /* Do not overwrite the core signal if it has already been set by
10912 another thread. */
10913 if (elf_tdata (abfd)->core->signal == 0)
10914 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10915
10916 /* Make a ".reg/999" section. */
10917
10918 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10919 len = strlen (buf) + 1;
10920 name = bfd_alloc (abfd, len);
10921 if (name == NULL)
10922 return false;
10923 memcpy (name, buf, len);
10924
10925 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10926 if (sect == NULL)
10927 return false;
10928
10929 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10930 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10931 sect->filepos = note->descpos
10932 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10933 #endif
10934
10935 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10936 sect->size = sizeof (lwpstat.pr_reg);
10937 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10938 #endif
10939
10940 sect->alignment_power = 2;
10941
10942 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10943 return false;
10944
10945 /* Make a ".reg2/999" section */
10946
10947 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10948 len = strlen (buf) + 1;
10949 name = bfd_alloc (abfd, len);
10950 if (name == NULL)
10951 return false;
10952 memcpy (name, buf, len);
10953
10954 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10955 if (sect == NULL)
10956 return false;
10957
10958 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10959 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10960 sect->filepos = note->descpos
10961 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10962 #endif
10963
10964 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10965 sect->size = sizeof (lwpstat.pr_fpreg);
10966 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10967 #endif
10968
10969 sect->alignment_power = 2;
10970
10971 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10972 }
10973 #endif /* defined (HAVE_LWPSTATUS_T) */
10974
10975 /* These constants, and the structure offsets used below, are defined by
10976 Cygwin's core_dump.h */
10977 #define NOTE_INFO_PROCESS 1
10978 #define NOTE_INFO_THREAD 2
10979 #define NOTE_INFO_MODULE 3
10980 #define NOTE_INFO_MODULE64 4
10981
10982 static bool
10983 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10984 {
10985 char buf[30];
10986 char *name;
10987 size_t len;
10988 unsigned int name_size;
10989 asection *sect;
10990 unsigned int type;
10991 int is_active_thread;
10992 bfd_vma base_addr;
10993
10994 if (note->descsz < 4)
10995 return true;
10996
10997 if (! startswith (note->namedata, "win32"))
10998 return true;
10999
11000 type = bfd_get_32 (abfd, note->descdata);
11001
11002 struct
11003 {
11004 const char *type_name;
11005 unsigned long min_size;
11006 } size_check[] =
11007 {
11008 { "NOTE_INFO_PROCESS", 12 },
11009 { "NOTE_INFO_THREAD", 12 },
11010 { "NOTE_INFO_MODULE", 12 },
11011 { "NOTE_INFO_MODULE64", 16 },
11012 };
11013
11014 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11015 return true;
11016
11017 if (note->descsz < size_check[type - 1].min_size)
11018 {
11019 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11020 " is too small"),
11021 abfd, size_check[type - 1].type_name, note->descsz);
11022 return true;
11023 }
11024
11025 switch (type)
11026 {
11027 case NOTE_INFO_PROCESS:
11028 /* FIXME: need to add ->core->command. */
11029 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11030 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11031 break;
11032
11033 case NOTE_INFO_THREAD:
11034 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11035 structure. */
11036 /* thread_info.tid */
11037 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11038
11039 len = strlen (buf) + 1;
11040 name = (char *) bfd_alloc (abfd, len);
11041 if (name == NULL)
11042 return false;
11043
11044 memcpy (name, buf, len);
11045
11046 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11047 if (sect == NULL)
11048 return false;
11049
11050 /* sizeof (thread_info.thread_context) */
11051 sect->size = note->descsz - 12;
11052 /* offsetof (thread_info.thread_context) */
11053 sect->filepos = note->descpos + 12;
11054 sect->alignment_power = 2;
11055
11056 /* thread_info.is_active_thread */
11057 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11058
11059 if (is_active_thread)
11060 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11061 return false;
11062 break;
11063
11064 case NOTE_INFO_MODULE:
11065 case NOTE_INFO_MODULE64:
11066 /* Make a ".module/xxxxxxxx" section. */
11067 if (type == NOTE_INFO_MODULE)
11068 {
11069 /* module_info.base_address */
11070 base_addr = bfd_get_32 (abfd, note->descdata + 4);
11071 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11072 /* module_info.module_name_size */
11073 name_size = bfd_get_32 (abfd, note->descdata + 8);
11074 }
11075 else /* NOTE_INFO_MODULE64 */
11076 {
11077 /* module_info.base_address */
11078 base_addr = bfd_get_64 (abfd, note->descdata + 4);
11079 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11080 /* module_info.module_name_size */
11081 name_size = bfd_get_32 (abfd, note->descdata + 12);
11082 }
11083
11084 len = strlen (buf) + 1;
11085 name = (char *) bfd_alloc (abfd, len);
11086 if (name == NULL)
11087 return false;
11088
11089 memcpy (name, buf, len);
11090
11091 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11092
11093 if (sect == NULL)
11094 return false;
11095
11096 if (note->descsz < 12 + name_size)
11097 {
11098 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11099 " is too small to contain a name of size %u"),
11100 abfd, note->descsz, name_size);
11101 return true;
11102 }
11103
11104 sect->size = note->descsz;
11105 sect->filepos = note->descpos;
11106 sect->alignment_power = 2;
11107 break;
11108
11109 default:
11110 return true;
11111 }
11112
11113 return true;
11114 }
11115
11116 static bool
11117 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11118 {
11119 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11120
11121 switch (note->type)
11122 {
11123 default:
11124 return true;
11125
11126 case NT_PRSTATUS:
11127 if (bed->elf_backend_grok_prstatus)
11128 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11129 return true;
11130 #if defined (HAVE_PRSTATUS_T)
11131 return elfcore_grok_prstatus (abfd, note);
11132 #else
11133 return true;
11134 #endif
11135
11136 #if defined (HAVE_PSTATUS_T)
11137 case NT_PSTATUS:
11138 return elfcore_grok_pstatus (abfd, note);
11139 #endif
11140
11141 #if defined (HAVE_LWPSTATUS_T)
11142 case NT_LWPSTATUS:
11143 return elfcore_grok_lwpstatus (abfd, note);
11144 #endif
11145
11146 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11147 return elfcore_grok_prfpreg (abfd, note);
11148
11149 case NT_WIN32PSTATUS:
11150 return elfcore_grok_win32pstatus (abfd, note);
11151
11152 case NT_PRXFPREG: /* Linux SSE extension */
11153 if (note->namesz == 6
11154 && strcmp (note->namedata, "LINUX") == 0)
11155 return elfcore_grok_prxfpreg (abfd, note);
11156 else
11157 return true;
11158
11159 case NT_X86_XSTATE: /* Linux XSAVE extension */
11160 if (note->namesz == 6
11161 && strcmp (note->namedata, "LINUX") == 0)
11162 return elfcore_grok_xstatereg (abfd, note);
11163 else
11164 return true;
11165
11166 case NT_PPC_VMX:
11167 if (note->namesz == 6
11168 && strcmp (note->namedata, "LINUX") == 0)
11169 return elfcore_grok_ppc_vmx (abfd, note);
11170 else
11171 return true;
11172
11173 case NT_PPC_VSX:
11174 if (note->namesz == 6
11175 && strcmp (note->namedata, "LINUX") == 0)
11176 return elfcore_grok_ppc_vsx (abfd, note);
11177 else
11178 return true;
11179
11180 case NT_PPC_TAR:
11181 if (note->namesz == 6
11182 && strcmp (note->namedata, "LINUX") == 0)
11183 return elfcore_grok_ppc_tar (abfd, note);
11184 else
11185 return true;
11186
11187 case NT_PPC_PPR:
11188 if (note->namesz == 6
11189 && strcmp (note->namedata, "LINUX") == 0)
11190 return elfcore_grok_ppc_ppr (abfd, note);
11191 else
11192 return true;
11193
11194 case NT_PPC_DSCR:
11195 if (note->namesz == 6
11196 && strcmp (note->namedata, "LINUX") == 0)
11197 return elfcore_grok_ppc_dscr (abfd, note);
11198 else
11199 return true;
11200
11201 case NT_PPC_EBB:
11202 if (note->namesz == 6
11203 && strcmp (note->namedata, "LINUX") == 0)
11204 return elfcore_grok_ppc_ebb (abfd, note);
11205 else
11206 return true;
11207
11208 case NT_PPC_PMU:
11209 if (note->namesz == 6
11210 && strcmp (note->namedata, "LINUX") == 0)
11211 return elfcore_grok_ppc_pmu (abfd, note);
11212 else
11213 return true;
11214
11215 case NT_PPC_TM_CGPR:
11216 if (note->namesz == 6
11217 && strcmp (note->namedata, "LINUX") == 0)
11218 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11219 else
11220 return true;
11221
11222 case NT_PPC_TM_CFPR:
11223 if (note->namesz == 6
11224 && strcmp (note->namedata, "LINUX") == 0)
11225 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11226 else
11227 return true;
11228
11229 case NT_PPC_TM_CVMX:
11230 if (note->namesz == 6
11231 && strcmp (note->namedata, "LINUX") == 0)
11232 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11233 else
11234 return true;
11235
11236 case NT_PPC_TM_CVSX:
11237 if (note->namesz == 6
11238 && strcmp (note->namedata, "LINUX") == 0)
11239 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11240 else
11241 return true;
11242
11243 case NT_PPC_TM_SPR:
11244 if (note->namesz == 6
11245 && strcmp (note->namedata, "LINUX") == 0)
11246 return elfcore_grok_ppc_tm_spr (abfd, note);
11247 else
11248 return true;
11249
11250 case NT_PPC_TM_CTAR:
11251 if (note->namesz == 6
11252 && strcmp (note->namedata, "LINUX") == 0)
11253 return elfcore_grok_ppc_tm_ctar (abfd, note);
11254 else
11255 return true;
11256
11257 case NT_PPC_TM_CPPR:
11258 if (note->namesz == 6
11259 && strcmp (note->namedata, "LINUX") == 0)
11260 return elfcore_grok_ppc_tm_cppr (abfd, note);
11261 else
11262 return true;
11263
11264 case NT_PPC_TM_CDSCR:
11265 if (note->namesz == 6
11266 && strcmp (note->namedata, "LINUX") == 0)
11267 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11268 else
11269 return true;
11270
11271 case NT_S390_HIGH_GPRS:
11272 if (note->namesz == 6
11273 && strcmp (note->namedata, "LINUX") == 0)
11274 return elfcore_grok_s390_high_gprs (abfd, note);
11275 else
11276 return true;
11277
11278 case NT_S390_TIMER:
11279 if (note->namesz == 6
11280 && strcmp (note->namedata, "LINUX") == 0)
11281 return elfcore_grok_s390_timer (abfd, note);
11282 else
11283 return true;
11284
11285 case NT_S390_TODCMP:
11286 if (note->namesz == 6
11287 && strcmp (note->namedata, "LINUX") == 0)
11288 return elfcore_grok_s390_todcmp (abfd, note);
11289 else
11290 return true;
11291
11292 case NT_S390_TODPREG:
11293 if (note->namesz == 6
11294 && strcmp (note->namedata, "LINUX") == 0)
11295 return elfcore_grok_s390_todpreg (abfd, note);
11296 else
11297 return true;
11298
11299 case NT_S390_CTRS:
11300 if (note->namesz == 6
11301 && strcmp (note->namedata, "LINUX") == 0)
11302 return elfcore_grok_s390_ctrs (abfd, note);
11303 else
11304 return true;
11305
11306 case NT_S390_PREFIX:
11307 if (note->namesz == 6
11308 && strcmp (note->namedata, "LINUX") == 0)
11309 return elfcore_grok_s390_prefix (abfd, note);
11310 else
11311 return true;
11312
11313 case NT_S390_LAST_BREAK:
11314 if (note->namesz == 6
11315 && strcmp (note->namedata, "LINUX") == 0)
11316 return elfcore_grok_s390_last_break (abfd, note);
11317 else
11318 return true;
11319
11320 case NT_S390_SYSTEM_CALL:
11321 if (note->namesz == 6
11322 && strcmp (note->namedata, "LINUX") == 0)
11323 return elfcore_grok_s390_system_call (abfd, note);
11324 else
11325 return true;
11326
11327 case NT_S390_TDB:
11328 if (note->namesz == 6
11329 && strcmp (note->namedata, "LINUX") == 0)
11330 return elfcore_grok_s390_tdb (abfd, note);
11331 else
11332 return true;
11333
11334 case NT_S390_VXRS_LOW:
11335 if (note->namesz == 6
11336 && strcmp (note->namedata, "LINUX") == 0)
11337 return elfcore_grok_s390_vxrs_low (abfd, note);
11338 else
11339 return true;
11340
11341 case NT_S390_VXRS_HIGH:
11342 if (note->namesz == 6
11343 && strcmp (note->namedata, "LINUX") == 0)
11344 return elfcore_grok_s390_vxrs_high (abfd, note);
11345 else
11346 return true;
11347
11348 case NT_S390_GS_CB:
11349 if (note->namesz == 6
11350 && strcmp (note->namedata, "LINUX") == 0)
11351 return elfcore_grok_s390_gs_cb (abfd, note);
11352 else
11353 return true;
11354
11355 case NT_S390_GS_BC:
11356 if (note->namesz == 6
11357 && strcmp (note->namedata, "LINUX") == 0)
11358 return elfcore_grok_s390_gs_bc (abfd, note);
11359 else
11360 return true;
11361
11362 case NT_ARC_V2:
11363 if (note->namesz == 6
11364 && strcmp (note->namedata, "LINUX") == 0)
11365 return elfcore_grok_arc_v2 (abfd, note);
11366 else
11367 return true;
11368
11369 case NT_ARM_VFP:
11370 if (note->namesz == 6
11371 && strcmp (note->namedata, "LINUX") == 0)
11372 return elfcore_grok_arm_vfp (abfd, note);
11373 else
11374 return true;
11375
11376 case NT_ARM_TLS:
11377 if (note->namesz == 6
11378 && strcmp (note->namedata, "LINUX") == 0)
11379 return elfcore_grok_aarch_tls (abfd, note);
11380 else
11381 return true;
11382
11383 case NT_ARM_HW_BREAK:
11384 if (note->namesz == 6
11385 && strcmp (note->namedata, "LINUX") == 0)
11386 return elfcore_grok_aarch_hw_break (abfd, note);
11387 else
11388 return true;
11389
11390 case NT_ARM_HW_WATCH:
11391 if (note->namesz == 6
11392 && strcmp (note->namedata, "LINUX") == 0)
11393 return elfcore_grok_aarch_hw_watch (abfd, note);
11394 else
11395 return true;
11396
11397 case NT_ARM_SVE:
11398 if (note->namesz == 6
11399 && strcmp (note->namedata, "LINUX") == 0)
11400 return elfcore_grok_aarch_sve (abfd, note);
11401 else
11402 return true;
11403
11404 case NT_ARM_PAC_MASK:
11405 if (note->namesz == 6
11406 && strcmp (note->namedata, "LINUX") == 0)
11407 return elfcore_grok_aarch_pauth (abfd, note);
11408 else
11409 return true;
11410
11411 case NT_ARM_TAGGED_ADDR_CTRL:
11412 if (note->namesz == 6
11413 && strcmp (note->namedata, "LINUX") == 0)
11414 return elfcore_grok_aarch_mte (abfd, note);
11415 else
11416 return true;
11417
11418 case NT_ARM_SSVE:
11419 if (note->namesz == 6
11420 && strcmp (note->namedata, "LINUX") == 0)
11421 return elfcore_grok_aarch_ssve (abfd, note);
11422 else
11423 return true;
11424
11425 case NT_ARM_ZA:
11426 if (note->namesz == 6
11427 && strcmp (note->namedata, "LINUX") == 0)
11428 return elfcore_grok_aarch_za (abfd, note);
11429 else
11430 return true;
11431
11432 case NT_ARM_ZT:
11433 if (note->namesz == 6
11434 && strcmp (note->namedata, "LINUX") == 0)
11435 return elfcore_grok_aarch_zt (abfd, note);
11436 else
11437 return true;
11438
11439 case NT_GDB_TDESC:
11440 if (note->namesz == 4
11441 && strcmp (note->namedata, "GDB") == 0)
11442 return elfcore_grok_gdb_tdesc (abfd, note);
11443 else
11444 return true;
11445
11446 case NT_RISCV_CSR:
11447 if (note->namesz == 4
11448 && strcmp (note->namedata, "GDB") == 0)
11449 return elfcore_grok_riscv_csr (abfd, note);
11450 else
11451 return true;
11452
11453 case NT_LARCH_CPUCFG:
11454 if (note->namesz == 6
11455 && strcmp (note->namedata, "LINUX") == 0)
11456 return elfcore_grok_loongarch_cpucfg (abfd, note);
11457 else
11458 return true;
11459
11460 case NT_LARCH_LBT:
11461 if (note->namesz == 6
11462 && strcmp (note->namedata, "LINUX") == 0)
11463 return elfcore_grok_loongarch_lbt (abfd, note);
11464 else
11465 return true;
11466
11467 case NT_LARCH_LSX:
11468 if (note->namesz == 6
11469 && strcmp (note->namedata, "LINUX") == 0)
11470 return elfcore_grok_loongarch_lsx (abfd, note);
11471 else
11472 return true;
11473
11474 case NT_LARCH_LASX:
11475 if (note->namesz == 6
11476 && strcmp (note->namedata, "LINUX") == 0)
11477 return elfcore_grok_loongarch_lasx (abfd, note);
11478 else
11479 return true;
11480
11481 case NT_PRPSINFO:
11482 case NT_PSINFO:
11483 if (bed->elf_backend_grok_psinfo)
11484 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11485 return true;
11486 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11487 return elfcore_grok_psinfo (abfd, note);
11488 #else
11489 return true;
11490 #endif
11491
11492 case NT_AUXV:
11493 return elfcore_make_auxv_note_section (abfd, note, 0);
11494
11495 case NT_FILE:
11496 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11497 note);
11498
11499 case NT_SIGINFO:
11500 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11501 note);
11502
11503 }
11504 }
11505
11506 static bool
11507 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11508 {
11509 struct bfd_build_id* build_id;
11510
11511 if (note->descsz == 0)
11512 return false;
11513
11514 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11515 if (build_id == NULL)
11516 return false;
11517
11518 build_id->size = note->descsz;
11519 memcpy (build_id->data, note->descdata, note->descsz);
11520 abfd->build_id = build_id;
11521
11522 return true;
11523 }
11524
11525 static bool
11526 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11527 {
11528 switch (note->type)
11529 {
11530 default:
11531 return true;
11532
11533 case NT_GNU_PROPERTY_TYPE_0:
11534 return _bfd_elf_parse_gnu_properties (abfd, note);
11535
11536 case NT_GNU_BUILD_ID:
11537 return elfobj_grok_gnu_build_id (abfd, note);
11538 }
11539 }
11540
11541 static bool
11542 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11543 {
11544 struct sdt_note *cur =
11545 (struct sdt_note *) bfd_alloc (abfd,
11546 sizeof (struct sdt_note) + note->descsz);
11547
11548 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11549 cur->size = (bfd_size_type) note->descsz;
11550 memcpy (cur->data, note->descdata, note->descsz);
11551
11552 elf_tdata (abfd)->sdt_note_head = cur;
11553
11554 return true;
11555 }
11556
11557 static bool
11558 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11559 {
11560 switch (note->type)
11561 {
11562 case NT_STAPSDT:
11563 return elfobj_grok_stapsdt_note_1 (abfd, note);
11564
11565 default:
11566 return true;
11567 }
11568 }
11569
11570 static bool
11571 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11572 {
11573 size_t offset;
11574
11575 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11576 {
11577 case ELFCLASS32:
11578 if (note->descsz < 108)
11579 return false;
11580 break;
11581
11582 case ELFCLASS64:
11583 if (note->descsz < 120)
11584 return false;
11585 break;
11586
11587 default:
11588 return false;
11589 }
11590
11591 /* Check for version 1 in pr_version. */
11592 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11593 return false;
11594
11595 offset = 4;
11596
11597 /* Skip over pr_psinfosz. */
11598 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11599 offset += 4;
11600 else
11601 {
11602 offset += 4; /* Padding before pr_psinfosz. */
11603 offset += 8;
11604 }
11605
11606 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11607 elf_tdata (abfd)->core->program
11608 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11609 offset += 17;
11610
11611 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11612 elf_tdata (abfd)->core->command
11613 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11614 offset += 81;
11615
11616 /* Padding before pr_pid. */
11617 offset += 2;
11618
11619 /* The pr_pid field was added in version "1a". */
11620 if (note->descsz < offset + 4)
11621 return true;
11622
11623 elf_tdata (abfd)->core->pid
11624 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11625
11626 return true;
11627 }
11628
11629 static bool
11630 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11631 {
11632 size_t offset;
11633 size_t size;
11634 size_t min_size;
11635
11636 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11637 Also compute minimum size of this note. */
11638 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11639 {
11640 case ELFCLASS32:
11641 offset = 4 + 4;
11642 min_size = offset + (4 * 2) + 4 + 4 + 4;
11643 break;
11644
11645 case ELFCLASS64:
11646 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11647 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11648 break;
11649
11650 default:
11651 return false;
11652 }
11653
11654 if (note->descsz < min_size)
11655 return false;
11656
11657 /* Check for version 1 in pr_version. */
11658 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11659 return false;
11660
11661 /* Extract size of pr_reg from pr_gregsetsz. */
11662 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11663 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11664 {
11665 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11666 offset += 4 * 2;
11667 }
11668 else
11669 {
11670 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11671 offset += 8 * 2;
11672 }
11673
11674 /* Skip over pr_osreldate. */
11675 offset += 4;
11676
11677 /* Read signal from pr_cursig. */
11678 if (elf_tdata (abfd)->core->signal == 0)
11679 elf_tdata (abfd)->core->signal
11680 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11681 offset += 4;
11682
11683 /* Read TID from pr_pid. */
11684 elf_tdata (abfd)->core->lwpid
11685 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11686 offset += 4;
11687
11688 /* Padding before pr_reg. */
11689 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11690 offset += 4;
11691
11692 /* Make sure that there is enough data remaining in the note. */
11693 if ((note->descsz - offset) < size)
11694 return false;
11695
11696 /* Make a ".reg/999" section and a ".reg" section. */
11697 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11698 size, note->descpos + offset);
11699 }
11700
11701 static bool
11702 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11703 {
11704 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11705
11706 switch (note->type)
11707 {
11708 case NT_PRSTATUS:
11709 if (bed->elf_backend_grok_freebsd_prstatus)
11710 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11711 return true;
11712 return elfcore_grok_freebsd_prstatus (abfd, note);
11713
11714 case NT_FPREGSET:
11715 return elfcore_grok_prfpreg (abfd, note);
11716
11717 case NT_PRPSINFO:
11718 return elfcore_grok_freebsd_psinfo (abfd, note);
11719
11720 case NT_FREEBSD_THRMISC:
11721 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11722
11723 case NT_FREEBSD_PROCSTAT_PROC:
11724 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11725 note);
11726
11727 case NT_FREEBSD_PROCSTAT_FILES:
11728 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11729 note);
11730
11731 case NT_FREEBSD_PROCSTAT_VMMAP:
11732 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11733 note);
11734
11735 case NT_FREEBSD_PROCSTAT_AUXV:
11736 return elfcore_make_auxv_note_section (abfd, note, 4);
11737
11738 case NT_FREEBSD_X86_SEGBASES:
11739 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11740
11741 case NT_X86_XSTATE:
11742 return elfcore_grok_xstatereg (abfd, note);
11743
11744 case NT_FREEBSD_PTLWPINFO:
11745 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11746 note);
11747
11748 case NT_ARM_TLS:
11749 return elfcore_grok_aarch_tls (abfd, note);
11750
11751 case NT_ARM_VFP:
11752 return elfcore_grok_arm_vfp (abfd, note);
11753
11754 default:
11755 return true;
11756 }
11757 }
11758
11759 static bool
11760 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11761 {
11762 char *cp;
11763
11764 cp = strchr (note->namedata, '@');
11765 if (cp != NULL)
11766 {
11767 *lwpidp = atoi(cp + 1);
11768 return true;
11769 }
11770 return false;
11771 }
11772
11773 static bool
11774 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11775 {
11776 if (note->descsz <= 0x7c + 31)
11777 return false;
11778
11779 /* Signal number at offset 0x08. */
11780 elf_tdata (abfd)->core->signal
11781 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11782
11783 /* Process ID at offset 0x50. */
11784 elf_tdata (abfd)->core->pid
11785 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11786
11787 /* Command name at 0x7c (max 32 bytes, including nul). */
11788 elf_tdata (abfd)->core->command
11789 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11790
11791 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11792 note);
11793 }
11794
11795 static bool
11796 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11797 {
11798 int lwp;
11799
11800 if (elfcore_netbsd_get_lwpid (note, &lwp))
11801 elf_tdata (abfd)->core->lwpid = lwp;
11802
11803 switch (note->type)
11804 {
11805 case NT_NETBSDCORE_PROCINFO:
11806 /* NetBSD-specific core "procinfo". Note that we expect to
11807 find this note before any of the others, which is fine,
11808 since the kernel writes this note out first when it
11809 creates a core file. */
11810 return elfcore_grok_netbsd_procinfo (abfd, note);
11811 case NT_NETBSDCORE_AUXV:
11812 /* NetBSD-specific Elf Auxiliary Vector data. */
11813 return elfcore_make_auxv_note_section (abfd, note, 4);
11814 case NT_NETBSDCORE_LWPSTATUS:
11815 return elfcore_make_note_pseudosection (abfd,
11816 ".note.netbsdcore.lwpstatus",
11817 note);
11818 default:
11819 break;
11820 }
11821
11822 /* As of March 2020 there are no other machine-independent notes
11823 defined for NetBSD core files. If the note type is less
11824 than the start of the machine-dependent note types, we don't
11825 understand it. */
11826
11827 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11828 return true;
11829
11830
11831 switch (bfd_get_arch (abfd))
11832 {
11833 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11834 PT_GETFPREGS == mach+2. */
11835
11836 case bfd_arch_aarch64:
11837 case bfd_arch_alpha:
11838 case bfd_arch_sparc:
11839 switch (note->type)
11840 {
11841 case NT_NETBSDCORE_FIRSTMACH+0:
11842 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11843
11844 case NT_NETBSDCORE_FIRSTMACH+2:
11845 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11846
11847 default:
11848 return true;
11849 }
11850
11851 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11852 There's also old PT___GETREGS40 == mach + 1 for old reg
11853 structure which lacks GBR. */
11854
11855 case bfd_arch_sh:
11856 switch (note->type)
11857 {
11858 case NT_NETBSDCORE_FIRSTMACH+3:
11859 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11860
11861 case NT_NETBSDCORE_FIRSTMACH+5:
11862 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11863
11864 default:
11865 return true;
11866 }
11867
11868 /* On all other arch's, PT_GETREGS == mach+1 and
11869 PT_GETFPREGS == mach+3. */
11870
11871 default:
11872 switch (note->type)
11873 {
11874 case NT_NETBSDCORE_FIRSTMACH+1:
11875 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11876
11877 case NT_NETBSDCORE_FIRSTMACH+3:
11878 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11879
11880 default:
11881 return true;
11882 }
11883 }
11884 /* NOTREACHED */
11885 }
11886
11887 static bool
11888 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11889 {
11890 if (note->descsz <= 0x48 + 31)
11891 return false;
11892
11893 /* Signal number at offset 0x08. */
11894 elf_tdata (abfd)->core->signal
11895 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11896
11897 /* Process ID at offset 0x20. */
11898 elf_tdata (abfd)->core->pid
11899 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11900
11901 /* Command name at 0x48 (max 32 bytes, including nul). */
11902 elf_tdata (abfd)->core->command
11903 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11904
11905 return true;
11906 }
11907
11908 /* Processes Solaris's process status note.
11909 sig_off ~ offsetof(prstatus_t, pr_cursig)
11910 pid_off ~ offsetof(prstatus_t, pr_pid)
11911 lwpid_off ~ offsetof(prstatus_t, pr_who)
11912 gregset_size ~ sizeof(gregset_t)
11913 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11914
11915 static bool
11916 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11917 int pid_off, int lwpid_off, size_t gregset_size,
11918 size_t gregset_offset)
11919 {
11920 asection *sect = NULL;
11921 elf_tdata (abfd)->core->signal
11922 = bfd_get_16 (abfd, note->descdata + sig_off);
11923 elf_tdata (abfd)->core->pid
11924 = bfd_get_32 (abfd, note->descdata + pid_off);
11925 elf_tdata (abfd)->core->lwpid
11926 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11927
11928 sect = bfd_get_section_by_name (abfd, ".reg");
11929 if (sect != NULL)
11930 sect->size = gregset_size;
11931
11932 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11933 note->descpos + gregset_offset);
11934 }
11935
11936 /* Gets program and arguments from a core.
11937 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11938 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11939
11940 static bool
11941 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11942 int prog_off, int comm_off)
11943 {
11944 elf_tdata (abfd)->core->program
11945 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11946 elf_tdata (abfd)->core->command
11947 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11948
11949 return true;
11950 }
11951
11952 /* Processes Solaris's LWP status note.
11953 gregset_size ~ sizeof(gregset_t)
11954 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11955 fpregset_size ~ sizeof(fpregset_t)
11956 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11957
11958 static bool
11959 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11960 size_t gregset_size, int gregset_off,
11961 size_t fpregset_size, int fpregset_off)
11962 {
11963 asection *sect = NULL;
11964 char reg2_section_name[16] = { 0 };
11965
11966 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11967 elf_tdata (abfd)->core->lwpid);
11968
11969 /* offsetof(lwpstatus_t, pr_lwpid) */
11970 elf_tdata (abfd)->core->lwpid
11971 = bfd_get_32 (abfd, note->descdata + 4);
11972 /* offsetof(lwpstatus_t, pr_cursig) */
11973 elf_tdata (abfd)->core->signal
11974 = bfd_get_16 (abfd, note->descdata + 12);
11975
11976 sect = bfd_get_section_by_name (abfd, ".reg");
11977 if (sect != NULL)
11978 sect->size = gregset_size;
11979 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11980 note->descpos + gregset_off))
11981 return false;
11982
11983 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11984 if (sect != NULL)
11985 {
11986 sect->size = fpregset_size;
11987 sect->filepos = note->descpos + fpregset_off;
11988 sect->alignment_power = 2;
11989 }
11990 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11991 note->descpos + fpregset_off))
11992 return false;
11993
11994 return true;
11995 }
11996
11997 static bool
11998 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11999 {
12000 if (note == NULL)
12001 return false;
12002
12003 /* core files are identified as 32- or 64-bit, SPARC or x86,
12004 by the size of the descsz which matches the sizeof()
12005 the type appropriate for that note type (e.g., prstatus_t for
12006 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12007 on Solaris. The core file bitness may differ from the bitness of
12008 gdb itself, so fixed values are used instead of sizeof().
12009 Appropriate fixed offsets are also used to obtain data from
12010 the note. */
12011
12012 switch ((int) note->type)
12013 {
12014 case SOLARIS_NT_PRSTATUS:
12015 switch (note->descsz)
12016 {
12017 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12018 return elfcore_grok_solaris_prstatus(abfd, note,
12019 136, 216, 308, 152, 356);
12020 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12021 return elfcore_grok_solaris_prstatus(abfd, note,
12022 264, 360, 520, 304, 600);
12023 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12024 return elfcore_grok_solaris_prstatus(abfd, note,
12025 136, 216, 308, 76, 356);
12026 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12027 return elfcore_grok_solaris_prstatus(abfd, note,
12028 264, 360, 520, 224, 600);
12029 default:
12030 return true;
12031 }
12032
12033 case SOLARIS_NT_PSINFO:
12034 case SOLARIS_NT_PRPSINFO:
12035 switch (note->descsz)
12036 {
12037 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12038 return elfcore_grok_solaris_info(abfd, note, 84, 100);
12039 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12040 return elfcore_grok_solaris_info(abfd, note, 120, 136);
12041 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12042 return elfcore_grok_solaris_info(abfd, note, 88, 104);
12043 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12044 return elfcore_grok_solaris_info(abfd, note, 136, 152);
12045 default:
12046 return true;
12047 }
12048
12049 case SOLARIS_NT_LWPSTATUS:
12050 switch (note->descsz)
12051 {
12052 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12053 return elfcore_grok_solaris_lwpstatus(abfd, note,
12054 152, 344, 400, 496);
12055 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12056 return elfcore_grok_solaris_lwpstatus(abfd, note,
12057 304, 544, 544, 848);
12058 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12059 return elfcore_grok_solaris_lwpstatus(abfd, note,
12060 76, 344, 380, 420);
12061 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12062 return elfcore_grok_solaris_lwpstatus(abfd, note,
12063 224, 544, 528, 768);
12064 default:
12065 return true;
12066 }
12067
12068 case SOLARIS_NT_LWPSINFO:
12069 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12070 if (note->descsz == 128 || note->descsz == 152)
12071 elf_tdata (abfd)->core->lwpid =
12072 bfd_get_32 (abfd, note->descdata + 4);
12073 break;
12074
12075 default:
12076 break;
12077 }
12078
12079 return true;
12080 }
12081
12082 /* For name starting with "CORE" this may be either a Solaris
12083 core file or a gdb-generated core file. Do Solaris-specific
12084 processing on selected note types first with
12085 elfcore_grok_solaris_note(), then process the note
12086 in elfcore_grok_note(). */
12087
12088 static bool
12089 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12090 {
12091 if (!elfcore_grok_solaris_note_impl (abfd, note))
12092 return false;
12093
12094 return elfcore_grok_note (abfd, note);
12095 }
12096
12097 static bool
12098 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12099 {
12100 if (note->type == NT_OPENBSD_PROCINFO)
12101 return elfcore_grok_openbsd_procinfo (abfd, note);
12102
12103 if (note->type == NT_OPENBSD_REGS)
12104 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12105
12106 if (note->type == NT_OPENBSD_FPREGS)
12107 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12108
12109 if (note->type == NT_OPENBSD_XFPREGS)
12110 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12111
12112 if (note->type == NT_OPENBSD_AUXV)
12113 return elfcore_make_auxv_note_section (abfd, note, 0);
12114
12115 if (note->type == NT_OPENBSD_WCOOKIE)
12116 {
12117 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12118 SEC_HAS_CONTENTS);
12119
12120 if (sect == NULL)
12121 return false;
12122 sect->size = note->descsz;
12123 sect->filepos = note->descpos;
12124 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12125
12126 return true;
12127 }
12128
12129 return true;
12130 }
12131
12132 static bool
12133 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12134 {
12135 void *ddata = note->descdata;
12136 char buf[100];
12137 char *name;
12138 asection *sect;
12139 short sig;
12140 unsigned flags;
12141
12142 if (note->descsz < 16)
12143 return false;
12144
12145 /* nto_procfs_status 'pid' field is at offset 0. */
12146 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12147
12148 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12149 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12150
12151 /* nto_procfs_status 'flags' field is at offset 8. */
12152 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12153
12154 /* nto_procfs_status 'what' field is at offset 14. */
12155 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12156 {
12157 elf_tdata (abfd)->core->signal = sig;
12158 elf_tdata (abfd)->core->lwpid = *tid;
12159 }
12160
12161 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12162 do not come from signals so we make sure we set the current
12163 thread just in case. */
12164 if (flags & 0x00000080)
12165 elf_tdata (abfd)->core->lwpid = *tid;
12166
12167 /* Make a ".qnx_core_status/%d" section. */
12168 sprintf (buf, ".qnx_core_status/%ld", *tid);
12169
12170 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12171 if (name == NULL)
12172 return false;
12173 strcpy (name, buf);
12174
12175 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12176 if (sect == NULL)
12177 return false;
12178
12179 sect->size = note->descsz;
12180 sect->filepos = note->descpos;
12181 sect->alignment_power = 2;
12182
12183 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12184 }
12185
12186 static bool
12187 elfcore_grok_nto_regs (bfd *abfd,
12188 Elf_Internal_Note *note,
12189 long tid,
12190 char *base)
12191 {
12192 char buf[100];
12193 char *name;
12194 asection *sect;
12195
12196 /* Make a "(base)/%d" section. */
12197 sprintf (buf, "%s/%ld", base, tid);
12198
12199 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12200 if (name == NULL)
12201 return false;
12202 strcpy (name, buf);
12203
12204 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12205 if (sect == NULL)
12206 return false;
12207
12208 sect->size = note->descsz;
12209 sect->filepos = note->descpos;
12210 sect->alignment_power = 2;
12211
12212 /* This is the current thread. */
12213 if (elf_tdata (abfd)->core->lwpid == tid)
12214 return elfcore_maybe_make_sect (abfd, base, sect);
12215
12216 return true;
12217 }
12218
12219 static bool
12220 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12221 {
12222 /* Every GREG section has a STATUS section before it. Store the
12223 tid from the previous call to pass down to the next gregs
12224 function. */
12225 static long tid = 1;
12226
12227 switch (note->type)
12228 {
12229 case QNT_CORE_INFO:
12230 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12231 case QNT_CORE_STATUS:
12232 return elfcore_grok_nto_status (abfd, note, &tid);
12233 case QNT_CORE_GREG:
12234 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12235 case QNT_CORE_FPREG:
12236 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12237 default:
12238 return true;
12239 }
12240 }
12241
12242 static bool
12243 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12244 {
12245 char *name;
12246 asection *sect;
12247 size_t len;
12248
12249 /* Use note name as section name. */
12250 len = note->namesz;
12251 name = (char *) bfd_alloc (abfd, len);
12252 if (name == NULL)
12253 return false;
12254 memcpy (name, note->namedata, len);
12255 name[len - 1] = '\0';
12256
12257 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12258 if (sect == NULL)
12259 return false;
12260
12261 sect->size = note->descsz;
12262 sect->filepos = note->descpos;
12263 sect->alignment_power = 1;
12264
12265 return true;
12266 }
12267
12268 /* Function: elfcore_write_note
12269
12270 Inputs:
12271 buffer to hold note, and current size of buffer
12272 name of note
12273 type of note
12274 data for note
12275 size of data for note
12276
12277 Writes note to end of buffer. ELF64 notes are written exactly as
12278 for ELF32, despite the current (as of 2006) ELF gabi specifying
12279 that they ought to have 8-byte namesz and descsz field, and have
12280 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12281
12282 Return:
12283 Pointer to realloc'd buffer, *BUFSIZ updated. */
12284
12285 char *
12286 elfcore_write_note (bfd *abfd,
12287 char *buf,
12288 int *bufsiz,
12289 const char *name,
12290 int type,
12291 const void *input,
12292 int size)
12293 {
12294 Elf_External_Note *xnp;
12295 size_t namesz;
12296 size_t newspace;
12297 char *dest;
12298
12299 namesz = 0;
12300 if (name != NULL)
12301 namesz = strlen (name) + 1;
12302
12303 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12304
12305 buf = (char *) realloc (buf, *bufsiz + newspace);
12306 if (buf == NULL)
12307 return buf;
12308 dest = buf + *bufsiz;
12309 *bufsiz += newspace;
12310 xnp = (Elf_External_Note *) dest;
12311 H_PUT_32 (abfd, namesz, xnp->namesz);
12312 H_PUT_32 (abfd, size, xnp->descsz);
12313 H_PUT_32 (abfd, type, xnp->type);
12314 dest = xnp->name;
12315 if (name != NULL)
12316 {
12317 memcpy (dest, name, namesz);
12318 dest += namesz;
12319 while (namesz & 3)
12320 {
12321 *dest++ = '\0';
12322 ++namesz;
12323 }
12324 }
12325 memcpy (dest, input, size);
12326 dest += size;
12327 while (size & 3)
12328 {
12329 *dest++ = '\0';
12330 ++size;
12331 }
12332 return buf;
12333 }
12334
12335 /* gcc-8 warns (*) on all the strncpy calls in this function about
12336 possible string truncation. The "truncation" is not a bug. We
12337 have an external representation of structs with fields that are not
12338 necessarily NULL terminated and corresponding internal
12339 representation fields that are one larger so that they can always
12340 be NULL terminated.
12341 gcc versions between 4.2 and 4.6 do not allow pragma control of
12342 diagnostics inside functions, giving a hard error if you try to use
12343 the finer control available with later versions.
12344 gcc prior to 4.2 warns about diagnostic push and pop.
12345 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12346 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12347 (*) Depending on your system header files! */
12348 #if GCC_VERSION >= 8000
12349 # pragma GCC diagnostic push
12350 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12351 #endif
12352 char *
12353 elfcore_write_prpsinfo (bfd *abfd,
12354 char *buf,
12355 int *bufsiz,
12356 const char *fname,
12357 const char *psargs)
12358 {
12359 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12360
12361 if (bed->elf_backend_write_core_note != NULL)
12362 {
12363 char *ret;
12364 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12365 NT_PRPSINFO, fname, psargs);
12366 if (ret != NULL)
12367 return ret;
12368 }
12369
12370 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12371 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12372 if (bed->s->elfclass == ELFCLASS32)
12373 {
12374 # if defined (HAVE_PSINFO32_T)
12375 psinfo32_t data;
12376 int note_type = NT_PSINFO;
12377 # else
12378 prpsinfo32_t data;
12379 int note_type = NT_PRPSINFO;
12380 # endif
12381
12382 memset (&data, 0, sizeof (data));
12383 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12384 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12385 return elfcore_write_note (abfd, buf, bufsiz,
12386 "CORE", note_type, &data, sizeof (data));
12387 }
12388 else
12389 # endif
12390 {
12391 # if defined (HAVE_PSINFO_T)
12392 psinfo_t data;
12393 int note_type = NT_PSINFO;
12394 # else
12395 prpsinfo_t data;
12396 int note_type = NT_PRPSINFO;
12397 # endif
12398
12399 memset (&data, 0, sizeof (data));
12400 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12401 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12402 return elfcore_write_note (abfd, buf, bufsiz,
12403 "CORE", note_type, &data, sizeof (data));
12404 }
12405 #endif /* PSINFO_T or PRPSINFO_T */
12406
12407 free (buf);
12408 return NULL;
12409 }
12410 #if GCC_VERSION >= 8000
12411 # pragma GCC diagnostic pop
12412 #endif
12413
12414 char *
12415 elfcore_write_linux_prpsinfo32
12416 (bfd *abfd, char *buf, int *bufsiz,
12417 const struct elf_internal_linux_prpsinfo *prpsinfo)
12418 {
12419 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12420 {
12421 struct elf_external_linux_prpsinfo32_ugid16 data;
12422
12423 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12424 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12425 &data, sizeof (data));
12426 }
12427 else
12428 {
12429 struct elf_external_linux_prpsinfo32_ugid32 data;
12430
12431 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12432 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12433 &data, sizeof (data));
12434 }
12435 }
12436
12437 char *
12438 elfcore_write_linux_prpsinfo64
12439 (bfd *abfd, char *buf, int *bufsiz,
12440 const struct elf_internal_linux_prpsinfo *prpsinfo)
12441 {
12442 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12443 {
12444 struct elf_external_linux_prpsinfo64_ugid16 data;
12445
12446 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12447 return elfcore_write_note (abfd, buf, bufsiz,
12448 "CORE", NT_PRPSINFO, &data, sizeof (data));
12449 }
12450 else
12451 {
12452 struct elf_external_linux_prpsinfo64_ugid32 data;
12453
12454 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12455 return elfcore_write_note (abfd, buf, bufsiz,
12456 "CORE", NT_PRPSINFO, &data, sizeof (data));
12457 }
12458 }
12459
12460 char *
12461 elfcore_write_prstatus (bfd *abfd,
12462 char *buf,
12463 int *bufsiz,
12464 long pid,
12465 int cursig,
12466 const void *gregs)
12467 {
12468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12469
12470 if (bed->elf_backend_write_core_note != NULL)
12471 {
12472 char *ret;
12473 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12474 NT_PRSTATUS,
12475 pid, cursig, gregs);
12476 if (ret != NULL)
12477 return ret;
12478 }
12479
12480 #if defined (HAVE_PRSTATUS_T)
12481 #if defined (HAVE_PRSTATUS32_T)
12482 if (bed->s->elfclass == ELFCLASS32)
12483 {
12484 prstatus32_t prstat;
12485
12486 memset (&prstat, 0, sizeof (prstat));
12487 prstat.pr_pid = pid;
12488 prstat.pr_cursig = cursig;
12489 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12490 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12491 NT_PRSTATUS, &prstat, sizeof (prstat));
12492 }
12493 else
12494 #endif
12495 {
12496 prstatus_t prstat;
12497
12498 memset (&prstat, 0, sizeof (prstat));
12499 prstat.pr_pid = pid;
12500 prstat.pr_cursig = cursig;
12501 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12502 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12503 NT_PRSTATUS, &prstat, sizeof (prstat));
12504 }
12505 #endif /* HAVE_PRSTATUS_T */
12506
12507 free (buf);
12508 return NULL;
12509 }
12510
12511 #if defined (HAVE_LWPSTATUS_T)
12512 char *
12513 elfcore_write_lwpstatus (bfd *abfd,
12514 char *buf,
12515 int *bufsiz,
12516 long pid,
12517 int cursig,
12518 const void *gregs)
12519 {
12520 lwpstatus_t lwpstat;
12521 const char *note_name = "CORE";
12522
12523 memset (&lwpstat, 0, sizeof (lwpstat));
12524 lwpstat.pr_lwpid = pid >> 16;
12525 lwpstat.pr_cursig = cursig;
12526 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12527 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12528 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12529 #if !defined(gregs)
12530 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12531 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12532 #else
12533 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12534 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12535 #endif
12536 #endif
12537 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12538 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12539 }
12540 #endif /* HAVE_LWPSTATUS_T */
12541
12542 #if defined (HAVE_PSTATUS_T)
12543 char *
12544 elfcore_write_pstatus (bfd *abfd,
12545 char *buf,
12546 int *bufsiz,
12547 long pid,
12548 int cursig ATTRIBUTE_UNUSED,
12549 const void *gregs ATTRIBUTE_UNUSED)
12550 {
12551 const char *note_name = "CORE";
12552 #if defined (HAVE_PSTATUS32_T)
12553 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12554
12555 if (bed->s->elfclass == ELFCLASS32)
12556 {
12557 pstatus32_t pstat;
12558
12559 memset (&pstat, 0, sizeof (pstat));
12560 pstat.pr_pid = pid & 0xffff;
12561 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12562 NT_PSTATUS, &pstat, sizeof (pstat));
12563 return buf;
12564 }
12565 else
12566 #endif
12567 {
12568 pstatus_t pstat;
12569
12570 memset (&pstat, 0, sizeof (pstat));
12571 pstat.pr_pid = pid & 0xffff;
12572 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12573 NT_PSTATUS, &pstat, sizeof (pstat));
12574 return buf;
12575 }
12576 }
12577 #endif /* HAVE_PSTATUS_T */
12578
12579 char *
12580 elfcore_write_prfpreg (bfd *abfd,
12581 char *buf,
12582 int *bufsiz,
12583 const void *fpregs,
12584 int size)
12585 {
12586 const char *note_name = "CORE";
12587 return elfcore_write_note (abfd, buf, bufsiz,
12588 note_name, NT_FPREGSET, fpregs, size);
12589 }
12590
12591 char *
12592 elfcore_write_prxfpreg (bfd *abfd,
12593 char *buf,
12594 int *bufsiz,
12595 const void *xfpregs,
12596 int size)
12597 {
12598 char *note_name = "LINUX";
12599 return elfcore_write_note (abfd, buf, bufsiz,
12600 note_name, NT_PRXFPREG, xfpregs, size);
12601 }
12602
12603 char *
12604 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12605 const void *xfpregs, int size)
12606 {
12607 char *note_name;
12608 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12609 note_name = "FreeBSD";
12610 else
12611 note_name = "LINUX";
12612 return elfcore_write_note (abfd, buf, bufsiz,
12613 note_name, NT_X86_XSTATE, xfpregs, size);
12614 }
12615
12616 char *
12617 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12618 const void *regs, int size)
12619 {
12620 char *note_name = "FreeBSD";
12621 return elfcore_write_note (abfd, buf, bufsiz,
12622 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12623 }
12624
12625 char *
12626 elfcore_write_ppc_vmx (bfd *abfd,
12627 char *buf,
12628 int *bufsiz,
12629 const void *ppc_vmx,
12630 int size)
12631 {
12632 char *note_name = "LINUX";
12633 return elfcore_write_note (abfd, buf, bufsiz,
12634 note_name, NT_PPC_VMX, ppc_vmx, size);
12635 }
12636
12637 char *
12638 elfcore_write_ppc_vsx (bfd *abfd,
12639 char *buf,
12640 int *bufsiz,
12641 const void *ppc_vsx,
12642 int size)
12643 {
12644 char *note_name = "LINUX";
12645 return elfcore_write_note (abfd, buf, bufsiz,
12646 note_name, NT_PPC_VSX, ppc_vsx, size);
12647 }
12648
12649 char *
12650 elfcore_write_ppc_tar (bfd *abfd,
12651 char *buf,
12652 int *bufsiz,
12653 const void *ppc_tar,
12654 int size)
12655 {
12656 char *note_name = "LINUX";
12657 return elfcore_write_note (abfd, buf, bufsiz,
12658 note_name, NT_PPC_TAR, ppc_tar, size);
12659 }
12660
12661 char *
12662 elfcore_write_ppc_ppr (bfd *abfd,
12663 char *buf,
12664 int *bufsiz,
12665 const void *ppc_ppr,
12666 int size)
12667 {
12668 char *note_name = "LINUX";
12669 return elfcore_write_note (abfd, buf, bufsiz,
12670 note_name, NT_PPC_PPR, ppc_ppr, size);
12671 }
12672
12673 char *
12674 elfcore_write_ppc_dscr (bfd *abfd,
12675 char *buf,
12676 int *bufsiz,
12677 const void *ppc_dscr,
12678 int size)
12679 {
12680 char *note_name = "LINUX";
12681 return elfcore_write_note (abfd, buf, bufsiz,
12682 note_name, NT_PPC_DSCR, ppc_dscr, size);
12683 }
12684
12685 char *
12686 elfcore_write_ppc_ebb (bfd *abfd,
12687 char *buf,
12688 int *bufsiz,
12689 const void *ppc_ebb,
12690 int size)
12691 {
12692 char *note_name = "LINUX";
12693 return elfcore_write_note (abfd, buf, bufsiz,
12694 note_name, NT_PPC_EBB, ppc_ebb, size);
12695 }
12696
12697 char *
12698 elfcore_write_ppc_pmu (bfd *abfd,
12699 char *buf,
12700 int *bufsiz,
12701 const void *ppc_pmu,
12702 int size)
12703 {
12704 char *note_name = "LINUX";
12705 return elfcore_write_note (abfd, buf, bufsiz,
12706 note_name, NT_PPC_PMU, ppc_pmu, size);
12707 }
12708
12709 char *
12710 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12711 char *buf,
12712 int *bufsiz,
12713 const void *ppc_tm_cgpr,
12714 int size)
12715 {
12716 char *note_name = "LINUX";
12717 return elfcore_write_note (abfd, buf, bufsiz,
12718 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12719 }
12720
12721 char *
12722 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12723 char *buf,
12724 int *bufsiz,
12725 const void *ppc_tm_cfpr,
12726 int size)
12727 {
12728 char *note_name = "LINUX";
12729 return elfcore_write_note (abfd, buf, bufsiz,
12730 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12731 }
12732
12733 char *
12734 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12735 char *buf,
12736 int *bufsiz,
12737 const void *ppc_tm_cvmx,
12738 int size)
12739 {
12740 char *note_name = "LINUX";
12741 return elfcore_write_note (abfd, buf, bufsiz,
12742 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12743 }
12744
12745 char *
12746 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12747 char *buf,
12748 int *bufsiz,
12749 const void *ppc_tm_cvsx,
12750 int size)
12751 {
12752 char *note_name = "LINUX";
12753 return elfcore_write_note (abfd, buf, bufsiz,
12754 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12755 }
12756
12757 char *
12758 elfcore_write_ppc_tm_spr (bfd *abfd,
12759 char *buf,
12760 int *bufsiz,
12761 const void *ppc_tm_spr,
12762 int size)
12763 {
12764 char *note_name = "LINUX";
12765 return elfcore_write_note (abfd, buf, bufsiz,
12766 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12767 }
12768
12769 char *
12770 elfcore_write_ppc_tm_ctar (bfd *abfd,
12771 char *buf,
12772 int *bufsiz,
12773 const void *ppc_tm_ctar,
12774 int size)
12775 {
12776 char *note_name = "LINUX";
12777 return elfcore_write_note (abfd, buf, bufsiz,
12778 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12779 }
12780
12781 char *
12782 elfcore_write_ppc_tm_cppr (bfd *abfd,
12783 char *buf,
12784 int *bufsiz,
12785 const void *ppc_tm_cppr,
12786 int size)
12787 {
12788 char *note_name = "LINUX";
12789 return elfcore_write_note (abfd, buf, bufsiz,
12790 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12791 }
12792
12793 char *
12794 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12795 char *buf,
12796 int *bufsiz,
12797 const void *ppc_tm_cdscr,
12798 int size)
12799 {
12800 char *note_name = "LINUX";
12801 return elfcore_write_note (abfd, buf, bufsiz,
12802 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12803 }
12804
12805 static char *
12806 elfcore_write_s390_high_gprs (bfd *abfd,
12807 char *buf,
12808 int *bufsiz,
12809 const void *s390_high_gprs,
12810 int size)
12811 {
12812 char *note_name = "LINUX";
12813 return elfcore_write_note (abfd, buf, bufsiz,
12814 note_name, NT_S390_HIGH_GPRS,
12815 s390_high_gprs, size);
12816 }
12817
12818 char *
12819 elfcore_write_s390_timer (bfd *abfd,
12820 char *buf,
12821 int *bufsiz,
12822 const void *s390_timer,
12823 int size)
12824 {
12825 char *note_name = "LINUX";
12826 return elfcore_write_note (abfd, buf, bufsiz,
12827 note_name, NT_S390_TIMER, s390_timer, size);
12828 }
12829
12830 char *
12831 elfcore_write_s390_todcmp (bfd *abfd,
12832 char *buf,
12833 int *bufsiz,
12834 const void *s390_todcmp,
12835 int size)
12836 {
12837 char *note_name = "LINUX";
12838 return elfcore_write_note (abfd, buf, bufsiz,
12839 note_name, NT_S390_TODCMP, s390_todcmp, size);
12840 }
12841
12842 char *
12843 elfcore_write_s390_todpreg (bfd *abfd,
12844 char *buf,
12845 int *bufsiz,
12846 const void *s390_todpreg,
12847 int size)
12848 {
12849 char *note_name = "LINUX";
12850 return elfcore_write_note (abfd, buf, bufsiz,
12851 note_name, NT_S390_TODPREG, s390_todpreg, size);
12852 }
12853
12854 char *
12855 elfcore_write_s390_ctrs (bfd *abfd,
12856 char *buf,
12857 int *bufsiz,
12858 const void *s390_ctrs,
12859 int size)
12860 {
12861 char *note_name = "LINUX";
12862 return elfcore_write_note (abfd, buf, bufsiz,
12863 note_name, NT_S390_CTRS, s390_ctrs, size);
12864 }
12865
12866 char *
12867 elfcore_write_s390_prefix (bfd *abfd,
12868 char *buf,
12869 int *bufsiz,
12870 const void *s390_prefix,
12871 int size)
12872 {
12873 char *note_name = "LINUX";
12874 return elfcore_write_note (abfd, buf, bufsiz,
12875 note_name, NT_S390_PREFIX, s390_prefix, size);
12876 }
12877
12878 char *
12879 elfcore_write_s390_last_break (bfd *abfd,
12880 char *buf,
12881 int *bufsiz,
12882 const void *s390_last_break,
12883 int size)
12884 {
12885 char *note_name = "LINUX";
12886 return elfcore_write_note (abfd, buf, bufsiz,
12887 note_name, NT_S390_LAST_BREAK,
12888 s390_last_break, size);
12889 }
12890
12891 char *
12892 elfcore_write_s390_system_call (bfd *abfd,
12893 char *buf,
12894 int *bufsiz,
12895 const void *s390_system_call,
12896 int size)
12897 {
12898 char *note_name = "LINUX";
12899 return elfcore_write_note (abfd, buf, bufsiz,
12900 note_name, NT_S390_SYSTEM_CALL,
12901 s390_system_call, size);
12902 }
12903
12904 char *
12905 elfcore_write_s390_tdb (bfd *abfd,
12906 char *buf,
12907 int *bufsiz,
12908 const void *s390_tdb,
12909 int size)
12910 {
12911 char *note_name = "LINUX";
12912 return elfcore_write_note (abfd, buf, bufsiz,
12913 note_name, NT_S390_TDB, s390_tdb, size);
12914 }
12915
12916 char *
12917 elfcore_write_s390_vxrs_low (bfd *abfd,
12918 char *buf,
12919 int *bufsiz,
12920 const void *s390_vxrs_low,
12921 int size)
12922 {
12923 char *note_name = "LINUX";
12924 return elfcore_write_note (abfd, buf, bufsiz,
12925 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12926 }
12927
12928 char *
12929 elfcore_write_s390_vxrs_high (bfd *abfd,
12930 char *buf,
12931 int *bufsiz,
12932 const void *s390_vxrs_high,
12933 int size)
12934 {
12935 char *note_name = "LINUX";
12936 return elfcore_write_note (abfd, buf, bufsiz,
12937 note_name, NT_S390_VXRS_HIGH,
12938 s390_vxrs_high, size);
12939 }
12940
12941 char *
12942 elfcore_write_s390_gs_cb (bfd *abfd,
12943 char *buf,
12944 int *bufsiz,
12945 const void *s390_gs_cb,
12946 int size)
12947 {
12948 char *note_name = "LINUX";
12949 return elfcore_write_note (abfd, buf, bufsiz,
12950 note_name, NT_S390_GS_CB,
12951 s390_gs_cb, size);
12952 }
12953
12954 char *
12955 elfcore_write_s390_gs_bc (bfd *abfd,
12956 char *buf,
12957 int *bufsiz,
12958 const void *s390_gs_bc,
12959 int size)
12960 {
12961 char *note_name = "LINUX";
12962 return elfcore_write_note (abfd, buf, bufsiz,
12963 note_name, NT_S390_GS_BC,
12964 s390_gs_bc, size);
12965 }
12966
12967 char *
12968 elfcore_write_arm_vfp (bfd *abfd,
12969 char *buf,
12970 int *bufsiz,
12971 const void *arm_vfp,
12972 int size)
12973 {
12974 char *note_name = "LINUX";
12975 return elfcore_write_note (abfd, buf, bufsiz,
12976 note_name, NT_ARM_VFP, arm_vfp, size);
12977 }
12978
12979 char *
12980 elfcore_write_aarch_tls (bfd *abfd,
12981 char *buf,
12982 int *bufsiz,
12983 const void *aarch_tls,
12984 int size)
12985 {
12986 char *note_name = "LINUX";
12987 return elfcore_write_note (abfd, buf, bufsiz,
12988 note_name, NT_ARM_TLS, aarch_tls, size);
12989 }
12990
12991 char *
12992 elfcore_write_aarch_hw_break (bfd *abfd,
12993 char *buf,
12994 int *bufsiz,
12995 const void *aarch_hw_break,
12996 int size)
12997 {
12998 char *note_name = "LINUX";
12999 return elfcore_write_note (abfd, buf, bufsiz,
13000 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
13001 }
13002
13003 char *
13004 elfcore_write_aarch_hw_watch (bfd *abfd,
13005 char *buf,
13006 int *bufsiz,
13007 const void *aarch_hw_watch,
13008 int size)
13009 {
13010 char *note_name = "LINUX";
13011 return elfcore_write_note (abfd, buf, bufsiz,
13012 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13013 }
13014
13015 char *
13016 elfcore_write_aarch_sve (bfd *abfd,
13017 char *buf,
13018 int *bufsiz,
13019 const void *aarch_sve,
13020 int size)
13021 {
13022 char *note_name = "LINUX";
13023 return elfcore_write_note (abfd, buf, bufsiz,
13024 note_name, NT_ARM_SVE, aarch_sve, size);
13025 }
13026
13027 char *
13028 elfcore_write_aarch_pauth (bfd *abfd,
13029 char *buf,
13030 int *bufsiz,
13031 const void *aarch_pauth,
13032 int size)
13033 {
13034 char *note_name = "LINUX";
13035 return elfcore_write_note (abfd, buf, bufsiz,
13036 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13037 }
13038
13039 char *
13040 elfcore_write_aarch_mte (bfd *abfd,
13041 char *buf,
13042 int *bufsiz,
13043 const void *aarch_mte,
13044 int size)
13045 {
13046 char *note_name = "LINUX";
13047 return elfcore_write_note (abfd, buf, bufsiz,
13048 note_name, NT_ARM_TAGGED_ADDR_CTRL,
13049 aarch_mte,
13050 size);
13051 }
13052
13053 char *
13054 elfcore_write_aarch_ssve (bfd *abfd,
13055 char *buf,
13056 int *bufsiz,
13057 const void *aarch_ssve,
13058 int size)
13059 {
13060 char *note_name = "LINUX";
13061 return elfcore_write_note (abfd, buf, bufsiz,
13062 note_name, NT_ARM_SSVE,
13063 aarch_ssve,
13064 size);
13065 }
13066
13067 char *
13068 elfcore_write_aarch_za (bfd *abfd,
13069 char *buf,
13070 int *bufsiz,
13071 const void *aarch_za,
13072 int size)
13073 {
13074 char *note_name = "LINUX";
13075 return elfcore_write_note (abfd, buf, bufsiz,
13076 note_name, NT_ARM_ZA,
13077 aarch_za,
13078 size);
13079 }
13080
13081 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13082 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13083 written into. Return a pointer to the new start of the note buffer, to
13084 replace BUF which may no longer be valid. */
13085
13086 char *
13087 elfcore_write_aarch_zt (bfd *abfd,
13088 char *buf,
13089 int *bufsiz,
13090 const void *aarch_zt,
13091 int size)
13092 {
13093 char *note_name = "LINUX";
13094 return elfcore_write_note (abfd, buf, bufsiz,
13095 note_name, NT_ARM_ZT,
13096 aarch_zt,
13097 size);
13098 }
13099
13100 char *
13101 elfcore_write_arc_v2 (bfd *abfd,
13102 char *buf,
13103 int *bufsiz,
13104 const void *arc_v2,
13105 int size)
13106 {
13107 char *note_name = "LINUX";
13108 return elfcore_write_note (abfd, buf, bufsiz,
13109 note_name, NT_ARC_V2, arc_v2, size);
13110 }
13111
13112 char *
13113 elfcore_write_loongarch_cpucfg (bfd *abfd,
13114 char *buf,
13115 int *bufsiz,
13116 const void *loongarch_cpucfg,
13117 int size)
13118 {
13119 char *note_name = "LINUX";
13120 return elfcore_write_note (abfd, buf, bufsiz,
13121 note_name, NT_LARCH_CPUCFG,
13122 loongarch_cpucfg, size);
13123 }
13124
13125 char *
13126 elfcore_write_loongarch_lbt (bfd *abfd,
13127 char *buf,
13128 int *bufsiz,
13129 const void *loongarch_lbt,
13130 int size)
13131 {
13132 char *note_name = "LINUX";
13133 return elfcore_write_note (abfd, buf, bufsiz,
13134 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13135 }
13136
13137 char *
13138 elfcore_write_loongarch_lsx (bfd *abfd,
13139 char *buf,
13140 int *bufsiz,
13141 const void *loongarch_lsx,
13142 int size)
13143 {
13144 char *note_name = "LINUX";
13145 return elfcore_write_note (abfd, buf, bufsiz,
13146 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13147 }
13148
13149 char *
13150 elfcore_write_loongarch_lasx (bfd *abfd,
13151 char *buf,
13152 int *bufsiz,
13153 const void *loongarch_lasx,
13154 int size)
13155 {
13156 char *note_name = "LINUX";
13157 return elfcore_write_note (abfd, buf, bufsiz,
13158 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13159 }
13160
13161 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13162 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13163 written into. Return a pointer to the new start of the note buffer, to
13164 replace BUF which may no longer be valid. */
13165
13166 char *
13167 elfcore_write_riscv_csr (bfd *abfd,
13168 char *buf,
13169 int *bufsiz,
13170 const void *csrs,
13171 int size)
13172 {
13173 const char *note_name = "GDB";
13174 return elfcore_write_note (abfd, buf, bufsiz,
13175 note_name, NT_RISCV_CSR, csrs, size);
13176 }
13177
13178 /* Write the target description (a string) pointed to by TDESC, length
13179 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13180 note is being written into. Return a pointer to the new start of the
13181 note buffer, to replace BUF which may no longer be valid. */
13182
13183 char *
13184 elfcore_write_gdb_tdesc (bfd *abfd,
13185 char *buf,
13186 int *bufsiz,
13187 const void *tdesc,
13188 int size)
13189 {
13190 const char *note_name = "GDB";
13191 return elfcore_write_note (abfd, buf, bufsiz,
13192 note_name, NT_GDB_TDESC, tdesc, size);
13193 }
13194
13195 char *
13196 elfcore_write_register_note (bfd *abfd,
13197 char *buf,
13198 int *bufsiz,
13199 const char *section,
13200 const void *data,
13201 int size)
13202 {
13203 if (strcmp (section, ".reg2") == 0)
13204 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13205 if (strcmp (section, ".reg-xfp") == 0)
13206 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13207 if (strcmp (section, ".reg-xstate") == 0)
13208 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13209 if (strcmp (section, ".reg-x86-segbases") == 0)
13210 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13211 if (strcmp (section, ".reg-ppc-vmx") == 0)
13212 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13213 if (strcmp (section, ".reg-ppc-vsx") == 0)
13214 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13215 if (strcmp (section, ".reg-ppc-tar") == 0)
13216 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13217 if (strcmp (section, ".reg-ppc-ppr") == 0)
13218 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13219 if (strcmp (section, ".reg-ppc-dscr") == 0)
13220 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13221 if (strcmp (section, ".reg-ppc-ebb") == 0)
13222 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13223 if (strcmp (section, ".reg-ppc-pmu") == 0)
13224 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13225 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13226 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13227 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13228 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13229 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13230 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13231 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13232 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13233 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13234 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13235 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13236 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13237 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13238 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13239 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13240 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13241 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13242 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13243 if (strcmp (section, ".reg-s390-timer") == 0)
13244 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13245 if (strcmp (section, ".reg-s390-todcmp") == 0)
13246 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13247 if (strcmp (section, ".reg-s390-todpreg") == 0)
13248 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13249 if (strcmp (section, ".reg-s390-ctrs") == 0)
13250 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13251 if (strcmp (section, ".reg-s390-prefix") == 0)
13252 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13253 if (strcmp (section, ".reg-s390-last-break") == 0)
13254 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13255 if (strcmp (section, ".reg-s390-system-call") == 0)
13256 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13257 if (strcmp (section, ".reg-s390-tdb") == 0)
13258 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13259 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13260 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13261 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13262 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13263 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13264 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13265 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13266 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13267 if (strcmp (section, ".reg-arm-vfp") == 0)
13268 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13269 if (strcmp (section, ".reg-aarch-tls") == 0)
13270 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13271 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13272 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13273 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13274 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13275 if (strcmp (section, ".reg-aarch-sve") == 0)
13276 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13277 if (strcmp (section, ".reg-aarch-pauth") == 0)
13278 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13279 if (strcmp (section, ".reg-aarch-mte") == 0)
13280 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13281 if (strcmp (section, ".reg-aarch-ssve") == 0)
13282 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13283 if (strcmp (section, ".reg-aarch-za") == 0)
13284 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13285 if (strcmp (section, ".reg-aarch-zt") == 0)
13286 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13287 if (strcmp (section, ".reg-arc-v2") == 0)
13288 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13289 if (strcmp (section, ".gdb-tdesc") == 0)
13290 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13291 if (strcmp (section, ".reg-riscv-csr") == 0)
13292 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13293 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13294 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13295 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13296 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13297 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13298 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13299 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13300 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13301 return NULL;
13302 }
13303
13304 char *
13305 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13306 const void *buf, int bufsiz)
13307 {
13308 return elfcore_write_note (obfd, note_data, note_size,
13309 "CORE", NT_FILE, buf, bufsiz);
13310 }
13311
13312 static bool
13313 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13314 size_t align)
13315 {
13316 char *p;
13317
13318 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13319 gABI specifies that PT_NOTE alignment should be aligned to 4
13320 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13321 align is less than 4, we use 4 byte alignment. */
13322 if (align < 4)
13323 align = 4;
13324 if (align != 4 && align != 8)
13325 return false;
13326
13327 p = buf;
13328 while (p < buf + size)
13329 {
13330 Elf_External_Note *xnp = (Elf_External_Note *) p;
13331 Elf_Internal_Note in;
13332
13333 if (offsetof (Elf_External_Note, name) > buf - p + size)
13334 return false;
13335
13336 in.type = H_GET_32 (abfd, xnp->type);
13337
13338 in.namesz = H_GET_32 (abfd, xnp->namesz);
13339 in.namedata = xnp->name;
13340 if (in.namesz > buf - in.namedata + size)
13341 return false;
13342
13343 in.descsz = H_GET_32 (abfd, xnp->descsz);
13344 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13345 in.descpos = offset + (in.descdata - buf);
13346 if (in.descsz != 0
13347 && (in.descdata >= buf + size
13348 || in.descsz > buf - in.descdata + size))
13349 return false;
13350
13351 switch (bfd_get_format (abfd))
13352 {
13353 default:
13354 return true;
13355
13356 case bfd_core:
13357 {
13358 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13359 struct
13360 {
13361 const char * string;
13362 size_t len;
13363 bool (*func) (bfd *, Elf_Internal_Note *);
13364 }
13365 grokers[] =
13366 {
13367 GROKER_ELEMENT ("", elfcore_grok_note),
13368 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13369 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13370 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13371 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13372 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13373 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13374 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13375 };
13376 #undef GROKER_ELEMENT
13377 int i;
13378
13379 for (i = ARRAY_SIZE (grokers); i--;)
13380 {
13381 if (in.namesz >= grokers[i].len
13382 && strncmp (in.namedata, grokers[i].string,
13383 grokers[i].len) == 0)
13384 {
13385 if (! grokers[i].func (abfd, & in))
13386 return false;
13387 break;
13388 }
13389 }
13390 break;
13391 }
13392
13393 case bfd_object:
13394 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13395 {
13396 if (! elfobj_grok_gnu_note (abfd, &in))
13397 return false;
13398 }
13399 else if (in.namesz == sizeof "stapsdt"
13400 && strcmp (in.namedata, "stapsdt") == 0)
13401 {
13402 if (! elfobj_grok_stapsdt_note (abfd, &in))
13403 return false;
13404 }
13405 break;
13406 }
13407
13408 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13409 }
13410
13411 return true;
13412 }
13413
13414 bool
13415 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13416 size_t align)
13417 {
13418 char *buf;
13419
13420 if (size == 0 || (size + 1) == 0)
13421 return true;
13422
13423 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13424 return false;
13425
13426 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13427 if (buf == NULL)
13428 return false;
13429
13430 /* PR 17512: file: ec08f814
13431 0-termintate the buffer so that string searches will not overflow. */
13432 buf[size] = 0;
13433
13434 if (!elf_parse_notes (abfd, buf, size, offset, align))
13435 {
13436 free (buf);
13437 return false;
13438 }
13439
13440 free (buf);
13441 return true;
13442 }
13443 \f
13444 /* Providing external access to the ELF program header table. */
13445
13446 /* Return an upper bound on the number of bytes required to store a
13447 copy of ABFD's program header table entries. Return -1 if an error
13448 occurs; bfd_get_error will return an appropriate code. */
13449
13450 long
13451 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13452 {
13453 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13454 {
13455 bfd_set_error (bfd_error_wrong_format);
13456 return -1;
13457 }
13458
13459 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13460 }
13461
13462 /* Copy ABFD's program header table entries to *PHDRS. The entries
13463 will be stored as an array of Elf_Internal_Phdr structures, as
13464 defined in include/elf/internal.h. To find out how large the
13465 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13466
13467 Return the number of program header table entries read, or -1 if an
13468 error occurs; bfd_get_error will return an appropriate code. */
13469
13470 int
13471 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13472 {
13473 int num_phdrs;
13474
13475 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13476 {
13477 bfd_set_error (bfd_error_wrong_format);
13478 return -1;
13479 }
13480
13481 num_phdrs = elf_elfheader (abfd)->e_phnum;
13482 if (num_phdrs != 0)
13483 memcpy (phdrs, elf_tdata (abfd)->phdr,
13484 num_phdrs * sizeof (Elf_Internal_Phdr));
13485
13486 return num_phdrs;
13487 }
13488
13489 enum elf_reloc_type_class
13490 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13491 const asection *rel_sec ATTRIBUTE_UNUSED,
13492 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13493 {
13494 return reloc_class_normal;
13495 }
13496
13497 /* For RELA architectures, return the relocation value for a
13498 relocation against a local symbol. */
13499
13500 bfd_vma
13501 _bfd_elf_rela_local_sym (bfd *abfd,
13502 Elf_Internal_Sym *sym,
13503 asection **psec,
13504 Elf_Internal_Rela *rel)
13505 {
13506 asection *sec = *psec;
13507 bfd_vma relocation;
13508
13509 relocation = (sec->output_section->vma
13510 + sec->output_offset
13511 + sym->st_value);
13512 if ((sec->flags & SEC_MERGE)
13513 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13514 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13515 {
13516 rel->r_addend =
13517 _bfd_merged_section_offset (abfd, psec,
13518 elf_section_data (sec)->sec_info,
13519 sym->st_value + rel->r_addend);
13520 if (sec != *psec)
13521 {
13522 /* If we have changed the section, and our original section is
13523 marked with SEC_EXCLUDE, it means that the original
13524 SEC_MERGE section has been completely subsumed in some
13525 other SEC_MERGE section. In this case, we need to leave
13526 some info around for --emit-relocs. */
13527 if ((sec->flags & SEC_EXCLUDE) != 0)
13528 sec->kept_section = *psec;
13529 sec = *psec;
13530 }
13531 rel->r_addend -= relocation;
13532 rel->r_addend += sec->output_section->vma + sec->output_offset;
13533 }
13534 return relocation;
13535 }
13536
13537 bfd_vma
13538 _bfd_elf_rel_local_sym (bfd *abfd,
13539 Elf_Internal_Sym *sym,
13540 asection **psec,
13541 bfd_vma addend)
13542 {
13543 asection *sec = *psec;
13544
13545 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13546 return sym->st_value + addend;
13547
13548 return _bfd_merged_section_offset (abfd, psec,
13549 elf_section_data (sec)->sec_info,
13550 sym->st_value + addend);
13551 }
13552
13553 /* Adjust an address within a section. Given OFFSET within SEC, return
13554 the new offset within the section, based upon changes made to the
13555 section. Returns -1 if the offset is now invalid.
13556 The offset (in abnd out) is in target sized bytes, however big a
13557 byte may be. */
13558
13559 bfd_vma
13560 _bfd_elf_section_offset (bfd *abfd,
13561 struct bfd_link_info *info,
13562 asection *sec,
13563 bfd_vma offset)
13564 {
13565 switch (sec->sec_info_type)
13566 {
13567 case SEC_INFO_TYPE_STABS:
13568 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13569 offset);
13570 case SEC_INFO_TYPE_EH_FRAME:
13571 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13572
13573 default:
13574 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13575 {
13576 /* Reverse the offset. */
13577 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13578 bfd_size_type address_size = bed->s->arch_size / 8;
13579
13580 /* address_size and sec->size are in octets. Convert
13581 to bytes before subtracting the original offset. */
13582 offset = ((sec->size - address_size)
13583 / bfd_octets_per_byte (abfd, sec) - offset);
13584 }
13585 return offset;
13586 }
13587 }
13588 \f
13589 long
13590 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13591 long symcount ATTRIBUTE_UNUSED,
13592 asymbol **syms ATTRIBUTE_UNUSED,
13593 long dynsymcount,
13594 asymbol **dynsyms,
13595 asymbol **ret)
13596 {
13597 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13598 asection *relplt;
13599 asymbol *s;
13600 const char *relplt_name;
13601 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13602 arelent *p;
13603 long count, i, n;
13604 size_t size;
13605 Elf_Internal_Shdr *hdr;
13606 char *names;
13607 asection *plt;
13608
13609 *ret = NULL;
13610
13611 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13612 return 0;
13613
13614 if (dynsymcount <= 0)
13615 return 0;
13616
13617 if (!bed->plt_sym_val)
13618 return 0;
13619
13620 relplt_name = bed->relplt_name;
13621 if (relplt_name == NULL)
13622 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13623 relplt = bfd_get_section_by_name (abfd, relplt_name);
13624 if (relplt == NULL)
13625 return 0;
13626
13627 hdr = &elf_section_data (relplt)->this_hdr;
13628 if (hdr->sh_link != elf_dynsymtab (abfd)
13629 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13630 return 0;
13631
13632 plt = bfd_get_section_by_name (abfd, ".plt");
13633 if (plt == NULL)
13634 return 0;
13635
13636 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13637 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13638 return -1;
13639
13640 count = NUM_SHDR_ENTRIES (hdr);
13641 size = count * sizeof (asymbol);
13642 p = relplt->relocation;
13643 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13644 {
13645 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13646 if (p->addend != 0)
13647 {
13648 #ifdef BFD64
13649 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13650 #else
13651 size += sizeof ("+0x") - 1 + 8;
13652 #endif
13653 }
13654 }
13655
13656 s = *ret = (asymbol *) bfd_malloc (size);
13657 if (s == NULL)
13658 return -1;
13659
13660 names = (char *) (s + count);
13661 p = relplt->relocation;
13662 n = 0;
13663 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13664 {
13665 size_t len;
13666 bfd_vma addr;
13667
13668 addr = bed->plt_sym_val (i, plt, p);
13669 if (addr == (bfd_vma) -1)
13670 continue;
13671
13672 *s = **p->sym_ptr_ptr;
13673 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13674 we are defining a symbol, ensure one of them is set. */
13675 if ((s->flags & BSF_LOCAL) == 0)
13676 s->flags |= BSF_GLOBAL;
13677 s->flags |= BSF_SYNTHETIC;
13678 s->section = plt;
13679 s->value = addr - plt->vma;
13680 s->name = names;
13681 s->udata.p = NULL;
13682 len = strlen ((*p->sym_ptr_ptr)->name);
13683 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13684 names += len;
13685 if (p->addend != 0)
13686 {
13687 char buf[30], *a;
13688
13689 memcpy (names, "+0x", sizeof ("+0x") - 1);
13690 names += sizeof ("+0x") - 1;
13691 bfd_sprintf_vma (abfd, buf, p->addend);
13692 for (a = buf; *a == '0'; ++a)
13693 ;
13694 len = strlen (a);
13695 memcpy (names, a, len);
13696 names += len;
13697 }
13698 memcpy (names, "@plt", sizeof ("@plt"));
13699 names += sizeof ("@plt");
13700 ++s, ++n;
13701 }
13702
13703 return n;
13704 }
13705
13706 /* It is only used by x86-64 so far.
13707 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13708 but current usage would allow all of _bfd_std_section to be zero. */
13709 static const asymbol lcomm_sym
13710 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13711 asection _bfd_elf_large_com_section
13712 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13713 "LARGE_COMMON", 0, SEC_IS_COMMON);
13714
13715 bool
13716 _bfd_elf_final_write_processing (bfd *abfd)
13717 {
13718 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13719
13720 i_ehdrp = elf_elfheader (abfd);
13721
13722 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13723 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13724
13725 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13726 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13727 or STB_GNU_UNIQUE binding. */
13728 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13729 {
13730 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13731 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13732 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13733 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13734 {
13735 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13736 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13737 "and FreeBSD targets"));
13738 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13739 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13740 "only by GNU and FreeBSD targets"));
13741 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13742 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13743 "only by GNU and FreeBSD targets"));
13744 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13745 _bfd_error_handler (_("GNU_RETAIN section is supported "
13746 "only by GNU and FreeBSD targets"));
13747 bfd_set_error (bfd_error_sorry);
13748 return false;
13749 }
13750 }
13751 return true;
13752 }
13753
13754
13755 /* Return TRUE for ELF symbol types that represent functions.
13756 This is the default version of this function, which is sufficient for
13757 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13758
13759 bool
13760 _bfd_elf_is_function_type (unsigned int type)
13761 {
13762 return (type == STT_FUNC
13763 || type == STT_GNU_IFUNC);
13764 }
13765
13766 /* If the ELF symbol SYM might be a function in SEC, return the
13767 function size and set *CODE_OFF to the function's entry point,
13768 otherwise return zero. */
13769
13770 bfd_size_type
13771 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13772 bfd_vma *code_off)
13773 {
13774 bfd_size_type size;
13775 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13776
13777 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13778 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13779 || sym->section != sec)
13780 return 0;
13781
13782 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13783
13784 /* In theory we should check that the symbol's type satisfies
13785 _bfd_elf_is_function_type(), but there are some function-like
13786 symbols which would fail this test. (eg _start). Instead
13787 we check for hidden, local, notype symbols with zero size.
13788 This type of symbol is generated by the annobin plugin for gcc
13789 and clang, and should not be considered to be a function symbol. */
13790 if (size == 0
13791 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13792 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13793 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13794 return 0;
13795
13796 *code_off = sym->value;
13797 /* Do not return 0 for the function's size. */
13798 return size ? size : 1;
13799 }
13800
13801 /* Set to non-zero to enable some debug messages. */
13802 #define DEBUG_SECONDARY_RELOCS 0
13803
13804 /* An internal-to-the-bfd-library only section type
13805 used to indicate a cached secondary reloc section. */
13806 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13807
13808 /* Create a BFD section to hold a secondary reloc section. */
13809
13810 bool
13811 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13812 Elf_Internal_Shdr *hdr,
13813 const char * name,
13814 unsigned int shindex)
13815 {
13816 /* We only support RELA secondary relocs. */
13817 if (hdr->sh_type != SHT_RELA)
13818 return false;
13819
13820 #if DEBUG_SECONDARY_RELOCS
13821 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13822 #endif
13823 hdr->sh_type = SHT_SECONDARY_RELOC;
13824 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13825 }
13826
13827 /* Read in any secondary relocs associated with SEC. */
13828
13829 bool
13830 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13831 asection * sec,
13832 asymbol ** symbols,
13833 bool dynamic)
13834 {
13835 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13836 asection * relsec;
13837 bool result = true;
13838 bfd_vma (*r_sym) (bfd_vma);
13839 ufile_ptr filesize;
13840
13841 #if BFD_DEFAULT_TARGET_SIZE > 32
13842 if (bfd_arch_bits_per_address (abfd) != 32)
13843 r_sym = elf64_r_sym;
13844 else
13845 #endif
13846 r_sym = elf32_r_sym;
13847
13848 if (!elf_section_data (sec)->has_secondary_relocs)
13849 return true;
13850
13851 /* Discover if there are any secondary reloc sections
13852 associated with SEC. */
13853 filesize = bfd_get_file_size (abfd);
13854 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13855 {
13856 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13857
13858 if (hdr->sh_type == SHT_SECONDARY_RELOC
13859 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13860 && (hdr->sh_entsize == ebd->s->sizeof_rel
13861 || hdr->sh_entsize == ebd->s->sizeof_rela))
13862 {
13863 bfd_byte * native_relocs;
13864 bfd_byte * native_reloc;
13865 arelent * internal_relocs;
13866 arelent * internal_reloc;
13867 size_t i;
13868 unsigned int entsize;
13869 unsigned int symcount;
13870 bfd_size_type reloc_count;
13871 size_t amt;
13872
13873 if (ebd->elf_info_to_howto == NULL)
13874 return false;
13875
13876 #if DEBUG_SECONDARY_RELOCS
13877 fprintf (stderr, "read secondary relocs for %s from %s\n",
13878 sec->name, relsec->name);
13879 #endif
13880 entsize = hdr->sh_entsize;
13881
13882 if (filesize != 0
13883 && ((ufile_ptr) hdr->sh_offset > filesize
13884 || hdr->sh_size > filesize - hdr->sh_offset))
13885 {
13886 bfd_set_error (bfd_error_file_truncated);
13887 result = false;
13888 continue;
13889 }
13890
13891 native_relocs = bfd_malloc (hdr->sh_size);
13892 if (native_relocs == NULL)
13893 {
13894 result = false;
13895 continue;
13896 }
13897
13898 reloc_count = NUM_SHDR_ENTRIES (hdr);
13899 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13900 {
13901 free (native_relocs);
13902 bfd_set_error (bfd_error_file_too_big);
13903 result = false;
13904 continue;
13905 }
13906
13907 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13908 if (internal_relocs == NULL)
13909 {
13910 free (native_relocs);
13911 result = false;
13912 continue;
13913 }
13914
13915 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13916 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13917 {
13918 free (native_relocs);
13919 /* The internal_relocs will be freed when
13920 the memory for the bfd is released. */
13921 result = false;
13922 continue;
13923 }
13924
13925 if (dynamic)
13926 symcount = bfd_get_dynamic_symcount (abfd);
13927 else
13928 symcount = bfd_get_symcount (abfd);
13929
13930 for (i = 0, internal_reloc = internal_relocs,
13931 native_reloc = native_relocs;
13932 i < reloc_count;
13933 i++, internal_reloc++, native_reloc += entsize)
13934 {
13935 bool res;
13936 Elf_Internal_Rela rela;
13937
13938 if (entsize == ebd->s->sizeof_rel)
13939 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13940 else /* entsize == ebd->s->sizeof_rela */
13941 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13942
13943 /* The address of an ELF reloc is section relative for an object
13944 file, and absolute for an executable file or shared library.
13945 The address of a normal BFD reloc is always section relative,
13946 and the address of a dynamic reloc is absolute.. */
13947 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13948 internal_reloc->address = rela.r_offset;
13949 else
13950 internal_reloc->address = rela.r_offset - sec->vma;
13951
13952 if (r_sym (rela.r_info) == STN_UNDEF)
13953 {
13954 /* FIXME: This and the error case below mean that we
13955 have a symbol on relocs that is not elf_symbol_type. */
13956 internal_reloc->sym_ptr_ptr =
13957 bfd_abs_section_ptr->symbol_ptr_ptr;
13958 }
13959 else if (r_sym (rela.r_info) > symcount)
13960 {
13961 _bfd_error_handler
13962 /* xgettext:c-format */
13963 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13964 abfd, sec, i, (long) r_sym (rela.r_info));
13965 bfd_set_error (bfd_error_bad_value);
13966 internal_reloc->sym_ptr_ptr =
13967 bfd_abs_section_ptr->symbol_ptr_ptr;
13968 result = false;
13969 }
13970 else
13971 {
13972 asymbol **ps;
13973
13974 ps = symbols + r_sym (rela.r_info) - 1;
13975 internal_reloc->sym_ptr_ptr = ps;
13976 /* Make sure that this symbol is not removed by strip. */
13977 (*ps)->flags |= BSF_KEEP;
13978 }
13979
13980 internal_reloc->addend = rela.r_addend;
13981
13982 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13983 if (! res || internal_reloc->howto == NULL)
13984 {
13985 #if DEBUG_SECONDARY_RELOCS
13986 fprintf (stderr,
13987 "there is no howto associated with reloc %lx\n",
13988 rela.r_info);
13989 #endif
13990 result = false;
13991 }
13992 }
13993
13994 free (native_relocs);
13995 /* Store the internal relocs. */
13996 elf_section_data (relsec)->sec_info = internal_relocs;
13997 }
13998 }
13999
14000 return result;
14001 }
14002
14003 /* Set the ELF section header fields of an output secondary reloc section. */
14004
14005 bool
14006 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14007 bfd *obfd ATTRIBUTE_UNUSED,
14008 const Elf_Internal_Shdr *isection,
14009 Elf_Internal_Shdr *osection)
14010 {
14011 asection * isec;
14012 asection * osec;
14013 struct bfd_elf_section_data * esd;
14014
14015 if (isection == NULL)
14016 return false;
14017
14018 if (isection->sh_type != SHT_SECONDARY_RELOC)
14019 return true;
14020
14021 isec = isection->bfd_section;
14022 if (isec == NULL)
14023 return false;
14024
14025 osec = osection->bfd_section;
14026 if (osec == NULL)
14027 return false;
14028
14029 esd = elf_section_data (osec);
14030 BFD_ASSERT (esd->sec_info == NULL);
14031 esd->sec_info = elf_section_data (isec)->sec_info;
14032 osection->sh_type = SHT_RELA;
14033 osection->sh_link = elf_onesymtab (obfd);
14034 if (osection->sh_link == 0)
14035 {
14036 /* There is no symbol table - we are hosed... */
14037 _bfd_error_handler
14038 /* xgettext:c-format */
14039 (_("%pB(%pA): link section cannot be set"
14040 " because the output file does not have a symbol table"),
14041 obfd, osec);
14042 bfd_set_error (bfd_error_bad_value);
14043 return false;
14044 }
14045
14046 /* Find the output section that corresponds to the isection's
14047 sh_info link. */
14048 if (isection->sh_info == 0
14049 || isection->sh_info >= elf_numsections (ibfd))
14050 {
14051 _bfd_error_handler
14052 /* xgettext:c-format */
14053 (_("%pB(%pA): info section index is invalid"),
14054 obfd, osec);
14055 bfd_set_error (bfd_error_bad_value);
14056 return false;
14057 }
14058
14059 isection = elf_elfsections (ibfd)[isection->sh_info];
14060
14061 if (isection == NULL
14062 || isection->bfd_section == NULL
14063 || isection->bfd_section->output_section == NULL)
14064 {
14065 _bfd_error_handler
14066 /* xgettext:c-format */
14067 (_("%pB(%pA): info section index cannot be set"
14068 " because the section is not in the output"),
14069 obfd, osec);
14070 bfd_set_error (bfd_error_bad_value);
14071 return false;
14072 }
14073
14074 esd = elf_section_data (isection->bfd_section->output_section);
14075 BFD_ASSERT (esd != NULL);
14076 osection->sh_info = esd->this_idx;
14077 esd->has_secondary_relocs = true;
14078 #if DEBUG_SECONDARY_RELOCS
14079 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14080 osec->name, osection->sh_link, osection->sh_info);
14081 fprintf (stderr, "mark section %s as having secondary relocs\n",
14082 bfd_section_name (isection->bfd_section->output_section));
14083 #endif
14084
14085 return true;
14086 }
14087
14088 /* Write out a secondary reloc section.
14089
14090 FIXME: Currently this function can result in a serious performance penalty
14091 for files with secondary relocs and lots of sections. The proper way to
14092 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14093 relocs together and then to have this function just walk that chain. */
14094
14095 bool
14096 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14097 {
14098 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14099 bfd_vma addr_offset;
14100 asection * relsec;
14101 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14102 bool result = true;
14103
14104 if (sec == NULL)
14105 return false;
14106
14107 #if BFD_DEFAULT_TARGET_SIZE > 32
14108 if (bfd_arch_bits_per_address (abfd) != 32)
14109 r_info = elf64_r_info;
14110 else
14111 #endif
14112 r_info = elf32_r_info;
14113
14114 /* The address of an ELF reloc is section relative for an object
14115 file, and absolute for an executable file or shared library.
14116 The address of a BFD reloc is always section relative. */
14117 addr_offset = 0;
14118 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14119 addr_offset = sec->vma;
14120
14121 /* Discover if there are any secondary reloc sections
14122 associated with SEC. */
14123 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14124 {
14125 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14126 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14127
14128 if (hdr->sh_type == SHT_RELA
14129 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14130 {
14131 asymbol * last_sym;
14132 int last_sym_idx;
14133 size_t reloc_count;
14134 size_t idx;
14135 bfd_size_type entsize;
14136 arelent * src_irel;
14137 bfd_byte * dst_rela;
14138
14139 if (hdr->contents != NULL)
14140 {
14141 _bfd_error_handler
14142 /* xgettext:c-format */
14143 (_("%pB(%pA): error: secondary reloc section processed twice"),
14144 abfd, relsec);
14145 bfd_set_error (bfd_error_bad_value);
14146 result = false;
14147 continue;
14148 }
14149
14150 entsize = hdr->sh_entsize;
14151 if (entsize == 0)
14152 {
14153 _bfd_error_handler
14154 /* xgettext:c-format */
14155 (_("%pB(%pA): error: secondary reloc section"
14156 " has zero sized entries"),
14157 abfd, relsec);
14158 bfd_set_error (bfd_error_bad_value);
14159 result = false;
14160 continue;
14161 }
14162 else if (entsize != ebd->s->sizeof_rel
14163 && entsize != ebd->s->sizeof_rela)
14164 {
14165 _bfd_error_handler
14166 /* xgettext:c-format */
14167 (_("%pB(%pA): error: secondary reloc section"
14168 " has non-standard sized entries"),
14169 abfd, relsec);
14170 bfd_set_error (bfd_error_bad_value);
14171 result = false;
14172 continue;
14173 }
14174
14175 reloc_count = hdr->sh_size / entsize;
14176 hdr->sh_size = entsize * reloc_count;
14177 if (reloc_count == 0)
14178 {
14179 _bfd_error_handler
14180 /* xgettext:c-format */
14181 (_("%pB(%pA): error: secondary reloc section is empty!"),
14182 abfd, relsec);
14183 bfd_set_error (bfd_error_bad_value);
14184 result = false;
14185 continue;
14186 }
14187
14188 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14189 if (hdr->contents == NULL)
14190 continue;
14191
14192 #if DEBUG_SECONDARY_RELOCS
14193 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14194 reloc_count, sec->name, relsec->name);
14195 #endif
14196 last_sym = NULL;
14197 last_sym_idx = 0;
14198 dst_rela = hdr->contents;
14199 src_irel = (arelent *) esd->sec_info;
14200 if (src_irel == NULL)
14201 {
14202 _bfd_error_handler
14203 /* xgettext:c-format */
14204 (_("%pB(%pA): error: internal relocs missing"
14205 " for secondary reloc section"),
14206 abfd, relsec);
14207 bfd_set_error (bfd_error_bad_value);
14208 result = false;
14209 continue;
14210 }
14211
14212 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14213 {
14214 Elf_Internal_Rela src_rela;
14215 arelent *ptr;
14216 asymbol *sym;
14217 int n;
14218
14219 ptr = src_irel + idx;
14220 if (ptr == NULL)
14221 {
14222 _bfd_error_handler
14223 /* xgettext:c-format */
14224 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14225 abfd, relsec, idx);
14226 bfd_set_error (bfd_error_bad_value);
14227 result = false;
14228 break;
14229 }
14230
14231 if (ptr->sym_ptr_ptr == NULL)
14232 {
14233 /* FIXME: Is this an error ? */
14234 n = 0;
14235 }
14236 else
14237 {
14238 sym = *ptr->sym_ptr_ptr;
14239
14240 if (sym == last_sym)
14241 n = last_sym_idx;
14242 else
14243 {
14244 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14245 if (n < 0)
14246 {
14247 _bfd_error_handler
14248 /* xgettext:c-format */
14249 (_("%pB(%pA): error: secondary reloc %zu"
14250 " references a missing symbol"),
14251 abfd, relsec, idx);
14252 bfd_set_error (bfd_error_bad_value);
14253 result = false;
14254 n = 0;
14255 }
14256
14257 last_sym = sym;
14258 last_sym_idx = n;
14259 }
14260
14261 if (sym->the_bfd != NULL
14262 && sym->the_bfd->xvec != abfd->xvec
14263 && ! _bfd_elf_validate_reloc (abfd, ptr))
14264 {
14265 _bfd_error_handler
14266 /* xgettext:c-format */
14267 (_("%pB(%pA): error: secondary reloc %zu"
14268 " references a deleted symbol"),
14269 abfd, relsec, idx);
14270 bfd_set_error (bfd_error_bad_value);
14271 result = false;
14272 n = 0;
14273 }
14274 }
14275
14276 src_rela.r_offset = ptr->address + addr_offset;
14277 if (ptr->howto == NULL)
14278 {
14279 _bfd_error_handler
14280 /* xgettext:c-format */
14281 (_("%pB(%pA): error: secondary reloc %zu"
14282 " is of an unknown type"),
14283 abfd, relsec, idx);
14284 bfd_set_error (bfd_error_bad_value);
14285 result = false;
14286 src_rela.r_info = r_info (0, 0);
14287 }
14288 else
14289 src_rela.r_info = r_info (n, ptr->howto->type);
14290 src_rela.r_addend = ptr->addend;
14291
14292 if (entsize == ebd->s->sizeof_rel)
14293 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14294 else /* entsize == ebd->s->sizeof_rela */
14295 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14296 }
14297 }
14298 }
14299
14300 return result;
14301 }