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