elf.c revision 1.19.2.1 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_get_file_size (abfd) > 0 /* not a character device */
292 && shstrtabsize > bfd_get_file_size (abfd))
293 || bfd_seek (abfd, offset, SEEK_SET) != 0
294 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
295 shstrtabsize)) == NULL)
296 {
297 /* Once we've failed to read it, make sure we don't keep
298 trying. Otherwise, we'll keep allocating space for
299 the string table over and over. */
300 i_shdrp[shindex]->sh_size = 0;
301 }
302 else
303 shstrtab[shstrtabsize] = '\0';
304 i_shdrp[shindex]->contents = shstrtab;
305 }
306 return (char *) shstrtab;
307 }
308
309 char *
310 bfd_elf_string_from_elf_section (bfd *abfd,
311 unsigned int shindex,
312 unsigned int strindex)
313 {
314 Elf_Internal_Shdr *hdr;
315
316 if (strindex == 0)
317 return "";
318
319 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
320 return NULL;
321
322 hdr = elf_elfsections (abfd)[shindex];
323
324 if (hdr->contents == NULL)
325 {
326 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
327 {
328 /* PR 17512: file: f057ec89. */
329 /* xgettext:c-format */
330 _bfd_error_handler (_("%pB: attempt to load strings from"
331 " a non-string section (number %d)"),
332 abfd, shindex);
333 return NULL;
334 }
335
336 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
337 return NULL;
338 }
339 else
340 {
341 /* PR 24273: The string section's contents may have already
342 been loaded elsewhere, eg because a corrupt file has the
343 string section index in the ELF header pointing at a group
344 section. So be paranoid, and test that the last byte of
345 the section is zero. */
346 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
347 return NULL;
348 }
349
350 if (strindex >= hdr->sh_size)
351 {
352 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
353 _bfd_error_handler
354 /* xgettext:c-format */
355 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
356 abfd, strindex, (uint64_t) hdr->sh_size,
357 (shindex == shstrndx && strindex == hdr->sh_name
358 ? ".shstrtab"
359 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
360 return NULL;
361 }
362
363 return ((char *) hdr->contents) + strindex;
364 }
365
366 /* Read and convert symbols to internal format.
367 SYMCOUNT specifies the number of symbols to read, starting from
368 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
369 are non-NULL, they are used to store the internal symbols, external
370 symbols, and symbol section index extensions, respectively.
371 Returns a pointer to the internal symbol buffer (malloced if necessary)
372 or NULL if there were no symbols or some kind of problem. */
373
374 Elf_Internal_Sym *
375 bfd_elf_get_elf_syms (bfd *ibfd,
376 Elf_Internal_Shdr *symtab_hdr,
377 size_t symcount,
378 size_t symoffset,
379 Elf_Internal_Sym *intsym_buf,
380 void *extsym_buf,
381 Elf_External_Sym_Shndx *extshndx_buf)
382 {
383 Elf_Internal_Shdr *shndx_hdr;
384 void *alloc_ext;
385 const bfd_byte *esym;
386 Elf_External_Sym_Shndx *alloc_extshndx;
387 Elf_External_Sym_Shndx *shndx;
388 Elf_Internal_Sym *alloc_intsym;
389 Elf_Internal_Sym *isym;
390 Elf_Internal_Sym *isymend;
391 const struct elf_backend_data *bed;
392 size_t extsym_size;
393 size_t amt;
394 file_ptr pos;
395
396 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
397 abort ();
398
399 if (symcount == 0)
400 return intsym_buf;
401
402 if (elf_use_dt_symtab_p (ibfd))
403 {
404 /* Use dynamic symbol table. */
405 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
406 {
407 bfd_set_error (bfd_error_invalid_operation);
408 return NULL;
409 }
410 return elf_tdata (ibfd)->dt_symtab + symoffset;
411 }
412
413 /* Normal syms might have section extension entries. */
414 shndx_hdr = NULL;
415 if (elf_symtab_shndx_list (ibfd) != NULL)
416 {
417 elf_section_list * entry;
418 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
419
420 /* Find an index section that is linked to this symtab section. */
421 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
422 {
423 /* PR 20063. */
424 if (entry->hdr.sh_link >= elf_numsections (ibfd))
425 continue;
426
427 if (sections[entry->hdr.sh_link] == symtab_hdr)
428 {
429 shndx_hdr = & entry->hdr;
430 break;
431 };
432 }
433
434 if (shndx_hdr == NULL)
435 {
436 if (symtab_hdr == &elf_symtab_hdr (ibfd))
437 /* Not really accurate, but this was how the old code used
438 to work. */
439 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
440 /* Otherwise we do nothing. The assumption is that
441 the index table will not be needed. */
442 }
443 }
444
445 /* Read the symbols. */
446 alloc_ext = NULL;
447 alloc_extshndx = NULL;
448 alloc_intsym = NULL;
449 bed = get_elf_backend_data (ibfd);
450 extsym_size = bed->s->sizeof_sym;
451 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
452 {
453 bfd_set_error (bfd_error_file_too_big);
454 intsym_buf = NULL;
455 goto out;
456 }
457 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
458 if (extsym_buf == NULL)
459 {
460 alloc_ext = bfd_malloc (amt);
461 extsym_buf = alloc_ext;
462 }
463 if (extsym_buf == NULL
464 || bfd_seek (ibfd, pos, SEEK_SET) != 0
465 || bfd_read (extsym_buf, amt, ibfd) != amt)
466 {
467 intsym_buf = NULL;
468 goto out;
469 }
470
471 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
472 extshndx_buf = NULL;
473 else
474 {
475 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
476 {
477 bfd_set_error (bfd_error_file_too_big);
478 intsym_buf = NULL;
479 goto out;
480 }
481 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
482 if (extshndx_buf == NULL)
483 {
484 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
485 extshndx_buf = alloc_extshndx;
486 }
487 if (extshndx_buf == NULL
488 || bfd_seek (ibfd, pos, SEEK_SET) != 0
489 || bfd_read (extshndx_buf, amt, ibfd) != amt)
490 {
491 intsym_buf = NULL;
492 goto out;
493 }
494 }
495
496 if (intsym_buf == NULL)
497 {
498 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
499 {
500 bfd_set_error (bfd_error_file_too_big);
501 goto out;
502 }
503 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
504 intsym_buf = alloc_intsym;
505 if (intsym_buf == NULL)
506 goto out;
507 }
508
509 /* Convert the symbols to internal form. */
510 isymend = intsym_buf + symcount;
511 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
512 shndx = extshndx_buf;
513 isym < isymend;
514 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
515 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
516 {
517 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
518 /* xgettext:c-format */
519 _bfd_error_handler (_("%pB symbol number %lu references"
520 " nonexistent SHT_SYMTAB_SHNDX section"),
521 ibfd, (unsigned long) symoffset);
522 free (alloc_intsym);
523 intsym_buf = NULL;
524 goto out;
525 }
526
527 out:
528 free (alloc_ext);
529 free (alloc_extshndx);
530
531 return intsym_buf;
532 }
533
534 /* Look up a symbol name. */
535 const char *
536 bfd_elf_sym_name (bfd *abfd,
537 Elf_Internal_Shdr *symtab_hdr,
538 Elf_Internal_Sym *isym,
539 asection *sym_sec)
540 {
541 const char *name;
542 unsigned int iname = isym->st_name;
543 unsigned int shindex = symtab_hdr->sh_link;
544
545 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
546 /* Check for a bogus st_shndx to avoid crashing. */
547 && isym->st_shndx < elf_numsections (abfd))
548 {
549 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
550 shindex = elf_elfheader (abfd)->e_shstrndx;
551 }
552
553 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
554 if (name == NULL)
555 name = "(null)";
556 else if (sym_sec && *name == '\0')
557 name = bfd_section_name (sym_sec);
558
559 return name;
560 }
561
562 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
563 sections. The first element is the flags, the rest are section
564 pointers. */
565
566 typedef union elf_internal_group {
567 Elf_Internal_Shdr *shdr;
568 unsigned int flags;
569 } Elf_Internal_Group;
570
571 /* Return the name of the group signature symbol. Why isn't the
572 signature just a string? */
573
574 static const char *
575 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
576 {
577 Elf_Internal_Shdr *hdr;
578 unsigned char esym[sizeof (Elf64_External_Sym)];
579 Elf_External_Sym_Shndx eshndx;
580 Elf_Internal_Sym isym;
581
582 /* First we need to ensure the symbol table is available. Make sure
583 that it is a symbol table section. */
584 if (ghdr->sh_link >= elf_numsections (abfd))
585 return NULL;
586 hdr = elf_elfsections (abfd) [ghdr->sh_link];
587 if (hdr->sh_type != SHT_SYMTAB
588 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
589 return NULL;
590
591 /* Go read the symbol. */
592 hdr = &elf_tdata (abfd)->symtab_hdr;
593 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
594 &isym, esym, &eshndx) == NULL)
595 return NULL;
596
597 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
598 }
599
600 /* Set next_in_group list pointer, and group name for NEWSECT. */
601
602 static bool
603 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
604 {
605 unsigned int num_group = elf_tdata (abfd)->num_group;
606
607 /* If num_group is zero, read in all SHT_GROUP sections. The count
608 is set to -1 if there are no SHT_GROUP sections. */
609 if (num_group == 0)
610 {
611 unsigned int i, shnum;
612
613 /* First count the number of groups. If we have a SHT_GROUP
614 section with just a flag word (ie. sh_size is 4), ignore it. */
615 shnum = elf_numsections (abfd);
616 num_group = 0;
617
618 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
619 ( (shdr)->sh_type == SHT_GROUP \
620 && (shdr)->sh_size >= minsize \
621 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
622 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
623
624 for (i = 0; i < shnum; i++)
625 {
626 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
627
628 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
629 num_group += 1;
630 }
631
632 if (num_group == 0)
633 {
634 num_group = (unsigned) -1;
635 elf_tdata (abfd)->num_group = num_group;
636 elf_tdata (abfd)->group_sect_ptr = NULL;
637 }
638 else
639 {
640 /* We keep a list of elf section headers for group sections,
641 so we can find them quickly. */
642 size_t amt;
643
644 elf_tdata (abfd)->num_group = num_group;
645 amt = num_group * sizeof (Elf_Internal_Shdr *);
646 elf_tdata (abfd)->group_sect_ptr
647 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
648 if (elf_tdata (abfd)->group_sect_ptr == NULL)
649 return false;
650 num_group = 0;
651
652 for (i = 0; i < shnum; i++)
653 {
654 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
655
656 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
657 {
658 unsigned char *src;
659 Elf_Internal_Group *dest;
660
661 /* Make sure the group section has a BFD section
662 attached to it. */
663 if (!bfd_section_from_shdr (abfd, i))
664 return false;
665
666 /* Add to list of sections. */
667 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
668 num_group += 1;
669
670 /* Read the raw contents. */
671 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
672 shdr->contents = NULL;
673 if (_bfd_mul_overflow (shdr->sh_size,
674 sizeof (*dest) / 4, &amt)
675 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
676 || !(shdr->contents
677 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
678 {
679 _bfd_error_handler
680 /* xgettext:c-format */
681 (_("%pB: invalid size field in group section"
682 " header: %#" PRIx64 ""),
683 abfd, (uint64_t) shdr->sh_size);
684 bfd_set_error (bfd_error_bad_value);
685 -- num_group;
686 continue;
687 }
688
689 /* Translate raw contents, a flag word followed by an
690 array of elf section indices all in target byte order,
691 to the flag word followed by an array of elf section
692 pointers. */
693 src = shdr->contents + shdr->sh_size;
694 dest = (Elf_Internal_Group *) (shdr->contents + amt);
695
696 while (1)
697 {
698 unsigned int idx;
699
700 src -= 4;
701 --dest;
702 idx = H_GET_32 (abfd, src);
703 if (src == shdr->contents)
704 {
705 dest->shdr = NULL;
706 dest->flags = idx;
707 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
708 shdr->bfd_section->flags
709 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
710 break;
711 }
712 if (idx < shnum)
713 {
714 dest->shdr = elf_elfsections (abfd)[idx];
715 /* PR binutils/23199: All sections in a
716 section group should be marked with
717 SHF_GROUP. But some tools generate
718 broken objects without SHF_GROUP. Fix
719 them up here. */
720 dest->shdr->sh_flags |= SHF_GROUP;
721 }
722 if (idx >= shnum
723 || dest->shdr->sh_type == SHT_GROUP)
724 {
725 _bfd_error_handler
726 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
727 abfd, i);
728 dest->shdr = NULL;
729 }
730 }
731 }
732 }
733
734 /* PR 17510: Corrupt binaries might contain invalid groups. */
735 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
736 {
737 elf_tdata (abfd)->num_group = num_group;
738
739 /* If all groups are invalid then fail. */
740 if (num_group == 0)
741 {
742 elf_tdata (abfd)->group_sect_ptr = NULL;
743 elf_tdata (abfd)->num_group = num_group = -1;
744 _bfd_error_handler
745 (_("%pB: no valid group sections found"), abfd);
746 bfd_set_error (bfd_error_bad_value);
747 }
748 }
749 }
750 }
751
752 if (num_group != (unsigned) -1)
753 {
754 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
755 unsigned int j;
756
757 for (j = 0; j < num_group; j++)
758 {
759 /* Begin search from previous found group. */
760 unsigned i = (j + search_offset) % num_group;
761
762 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
763 Elf_Internal_Group *idx;
764 bfd_size_type n_elt;
765
766 if (shdr == NULL)
767 continue;
768
769 idx = (Elf_Internal_Group *) shdr->contents;
770 if (idx == NULL || shdr->sh_size < 4)
771 {
772 /* See PR 21957 for a reproducer. */
773 /* xgettext:c-format */
774 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
775 abfd, shdr->bfd_section);
776 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
777 bfd_set_error (bfd_error_bad_value);
778 return false;
779 }
780 n_elt = shdr->sh_size / 4;
781
782 /* Look through this group's sections to see if current
783 section is a member. */
784 while (--n_elt != 0)
785 if ((++idx)->shdr == hdr)
786 {
787 asection *s = NULL;
788
789 /* We are a member of this group. Go looking through
790 other members to see if any others are linked via
791 next_in_group. */
792 idx = (Elf_Internal_Group *) shdr->contents;
793 n_elt = shdr->sh_size / 4;
794 while (--n_elt != 0)
795 if ((++idx)->shdr != NULL
796 && (s = idx->shdr->bfd_section) != NULL
797 && elf_next_in_group (s) != NULL)
798 break;
799 if (n_elt != 0)
800 {
801 /* Snarf the group name from other member, and
802 insert current section in circular list. */
803 elf_group_name (newsect) = elf_group_name (s);
804 elf_next_in_group (newsect) = elf_next_in_group (s);
805 elf_next_in_group (s) = newsect;
806 }
807 else
808 {
809 const char *gname;
810
811 gname = group_signature (abfd, shdr);
812 if (gname == NULL)
813 return false;
814 elf_group_name (newsect) = gname;
815
816 /* Start a circular list with one element. */
817 elf_next_in_group (newsect) = newsect;
818 }
819
820 /* If the group section has been created, point to the
821 new member. */
822 if (shdr->bfd_section != NULL)
823 elf_next_in_group (shdr->bfd_section) = newsect;
824
825 elf_tdata (abfd)->group_search_offset = i;
826 j = num_group - 1;
827 break;
828 }
829 }
830 }
831
832 if (elf_group_name (newsect) == NULL)
833 {
834 /* xgettext:c-format */
835 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
836 abfd, newsect);
837 /* PR 29532: Return true here, even though the group info has not been
838 read. Separate debug info files can have empty group sections, but
839 we do not want this to prevent them from being loaded as otherwise
840 GDB will not be able to use them. */
841 return true;
842 }
843 return true;
844 }
845
846 bool
847 _bfd_elf_setup_sections (bfd *abfd)
848 {
849 unsigned int i;
850 unsigned int num_group = elf_tdata (abfd)->num_group;
851 bool result = true;
852 asection *s;
853
854 /* Process SHF_LINK_ORDER. */
855 for (s = abfd->sections; s != NULL; s = s->next)
856 {
857 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
858 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
859 {
860 unsigned int elfsec = this_hdr->sh_link;
861 /* An sh_link value of 0 is now allowed. It indicates that linked
862 to section has already been discarded, but that the current
863 section has been retained for some other reason. This linking
864 section is still a candidate for later garbage collection
865 however. */
866 if (elfsec == 0)
867 {
868 elf_linked_to_section (s) = NULL;
869 }
870 else
871 {
872 asection *linksec = NULL;
873
874 if (elfsec < elf_numsections (abfd))
875 {
876 this_hdr = elf_elfsections (abfd)[elfsec];
877 linksec = this_hdr->bfd_section;
878 }
879
880 /* PR 1991, 2008:
881 Some strip/objcopy may leave an incorrect value in
882 sh_link. We don't want to proceed. */
883 if (linksec == NULL)
884 {
885 _bfd_error_handler
886 /* xgettext:c-format */
887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
888 s->owner, elfsec, s);
889 result = false;
890 }
891
892 elf_linked_to_section (s) = linksec;
893 }
894 }
895 else if (this_hdr->sh_type == SHT_GROUP
896 && elf_next_in_group (s) == NULL)
897 {
898 _bfd_error_handler
899 /* xgettext:c-format */
900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
901 abfd, elf_section_data (s)->this_idx);
902 result = false;
903 }
904 }
905
906 /* Process section groups. */
907 if (num_group == (unsigned) -1)
908 return result;
909
910 for (i = 0; i < num_group; i++)
911 {
912 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
913 Elf_Internal_Group *idx;
914 unsigned int n_elt;
915
916 /* PR binutils/18758: Beware of corrupt binaries with invalid
917 group data. */
918 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
919 {
920 _bfd_error_handler
921 /* xgettext:c-format */
922 (_("%pB: section group entry number %u is corrupt"),
923 abfd, i);
924 result = false;
925 continue;
926 }
927
928 idx = (Elf_Internal_Group *) shdr->contents;
929 n_elt = shdr->sh_size / 4;
930
931 while (--n_elt != 0)
932 {
933 ++ idx;
934
935 if (idx->shdr == NULL)
936 continue;
937 else if (idx->shdr->bfd_section)
938 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
939 else if (idx->shdr->sh_type != SHT_RELA
940 && idx->shdr->sh_type != SHT_REL)
941 {
942 /* There are some unknown sections in the group. */
943 _bfd_error_handler
944 /* xgettext:c-format */
945 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
946 abfd,
947 idx->shdr->sh_type,
948 bfd_elf_string_from_elf_section (abfd,
949 (elf_elfheader (abfd)
950 ->e_shstrndx),
951 idx->shdr->sh_name),
952 shdr->bfd_section);
953 result = false;
954 }
955 }
956 }
957
958 return result;
959 }
960
961 bool
962 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
963 {
964 return elf_next_in_group (sec) != NULL;
965 }
966
967 const char *
968 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
969 {
970 if (elf_sec_group (sec) != NULL)
971 return elf_group_name (sec);
972 return NULL;
973 }
974
975 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
976
977 struct lto_section
978 {
979 int16_t major_version;
980 int16_t minor_version;
981 unsigned char slim_object;
982
983 /* Flags is a private field that is not defined publicly. */
984 uint16_t flags;
985 };
986
987 /* Make a BFD section from an ELF section. We store a pointer to the
988 BFD section in the bfd_section field of the header. */
989
990 bool
991 _bfd_elf_make_section_from_shdr (bfd *abfd,
992 Elf_Internal_Shdr *hdr,
993 const char *name,
994 int shindex)
995 {
996 asection *newsect;
997 flagword flags;
998 const struct elf_backend_data *bed;
999 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
1000
1001 if (hdr->bfd_section != NULL)
1002 return true;
1003
1004 newsect = bfd_make_section_anyway (abfd, name);
1005 if (newsect == NULL)
1006 return false;
1007
1008 hdr->bfd_section = newsect;
1009 elf_section_data (newsect)->this_hdr = *hdr;
1010 elf_section_data (newsect)->this_idx = shindex;
1011
1012 /* Always use the real type/flags. */
1013 elf_section_type (newsect) = hdr->sh_type;
1014 elf_section_flags (newsect) = hdr->sh_flags;
1015
1016 newsect->filepos = hdr->sh_offset;
1017
1018 flags = SEC_NO_FLAGS;
1019 if (hdr->sh_type != SHT_NOBITS)
1020 flags |= SEC_HAS_CONTENTS;
1021 if (hdr->sh_type == SHT_GROUP)
1022 flags |= SEC_GROUP;
1023 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1024 {
1025 flags |= SEC_ALLOC;
1026 if (hdr->sh_type != SHT_NOBITS)
1027 flags |= SEC_LOAD;
1028 }
1029 if ((hdr->sh_flags & SHF_WRITE) == 0)
1030 flags |= SEC_READONLY;
1031 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1032 flags |= SEC_CODE;
1033 else if ((flags & SEC_LOAD) != 0)
1034 flags |= SEC_DATA;
1035 if ((hdr->sh_flags & SHF_MERGE) != 0)
1036 {
1037 flags |= SEC_MERGE;
1038 newsect->entsize = hdr->sh_entsize;
1039 }
1040 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1041 flags |= SEC_STRINGS;
1042 if (hdr->sh_flags & SHF_GROUP)
1043 if (!setup_group (abfd, hdr, newsect))
1044 return false;
1045 if ((hdr->sh_flags & SHF_TLS) != 0)
1046 flags |= SEC_THREAD_LOCAL;
1047 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1048 flags |= SEC_EXCLUDE;
1049
1050 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1051 {
1052 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1053 but binutils as of 2019-07-23 did not set the EI_OSABI header
1054 byte. */
1055 case ELFOSABI_GNU:
1056 case ELFOSABI_FREEBSD:
1057 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1058 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1059 /* Fall through */
1060 case ELFOSABI_NONE:
1061 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1062 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1063 break;
1064 }
1065
1066 if ((flags & SEC_ALLOC) == 0)
1067 {
1068 /* The debugging sections appear to be recognized only by name,
1069 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1070 if (name [0] == '.')
1071 {
1072 if (startswith (name, ".debug")
1073 || startswith (name, ".gnu.debuglto_.debug_")
1074 || startswith (name, ".gnu.linkonce.wi.")
1075 || startswith (name, ".zdebug"))
1076 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1077 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1078 || startswith (name, ".note.gnu"))
1079 {
1080 flags |= SEC_ELF_OCTETS;
1081 opb = 1;
1082 }
1083 else if (startswith (name, ".line")
1084 || startswith (name, ".stab")
1085 || strcmp (name, ".gdb_index") == 0)
1086 flags |= SEC_DEBUGGING;
1087 }
1088 }
1089
1090 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1091 || !bfd_set_section_size (newsect, hdr->sh_size)
1092 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1093 & -hdr->sh_addralign)))
1094 return false;
1095
1096 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1097 only link a single copy of the section. This is used to support
1098 g++. g++ will emit each template expansion in its own section.
1099 The symbols will be defined as weak, so that multiple definitions
1100 are permitted. The GNU linker extension is to actually discard
1101 all but one of the sections. */
1102 if (startswith (name, ".gnu.linkonce")
1103 && elf_next_in_group (newsect) == NULL)
1104 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1105
1106 if (!bfd_set_section_flags (newsect, flags))
1107 return false;
1108
1109 bed = get_elf_backend_data (abfd);
1110 if (bed->elf_backend_section_flags)
1111 if (!bed->elf_backend_section_flags (hdr))
1112 return false;
1113
1114 /* We do not parse the PT_NOTE segments as we are interested even in the
1115 separate debug info files which may have the segments offsets corrupted.
1116 PT_NOTEs from the core files are currently not parsed using BFD. */
1117 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1118 {
1119 bfd_byte *contents;
1120
1121 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1122 return false;
1123
1124 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1125 hdr->sh_offset, hdr->sh_addralign);
1126 free (contents);
1127 }
1128
1129 if ((newsect->flags & SEC_ALLOC) != 0)
1130 {
1131 Elf_Internal_Phdr *phdr;
1132 unsigned int i, nload;
1133
1134 /* Some ELF linkers produce binaries with all the program header
1135 p_paddr fields zero. If we have such a binary with more than
1136 one PT_LOAD header, then leave the section lma equal to vma
1137 so that we don't create sections with overlapping lma. */
1138 phdr = elf_tdata (abfd)->phdr;
1139 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1140 if (phdr->p_paddr != 0)
1141 break;
1142 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1143 ++nload;
1144 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1145 return true;
1146
1147 phdr = elf_tdata (abfd)->phdr;
1148 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1149 {
1150 if (((phdr->p_type == PT_LOAD
1151 && (hdr->sh_flags & SHF_TLS) == 0)
1152 || phdr->p_type == PT_TLS)
1153 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1154 {
1155 if ((newsect->flags & SEC_LOAD) == 0)
1156 newsect->lma = (phdr->p_paddr
1157 + hdr->sh_addr - phdr->p_vaddr) / opb;
1158 else
1159 /* We used to use the same adjustment for SEC_LOAD
1160 sections, but that doesn't work if the segment
1161 is packed with code from multiple VMAs.
1162 Instead we calculate the section LMA based on
1163 the segment LMA. It is assumed that the
1164 segment will contain sections with contiguous
1165 LMAs, even if the VMAs are not. */
1166 newsect->lma = (phdr->p_paddr
1167 + hdr->sh_offset - phdr->p_offset) / opb;
1168
1169 /* With contiguous segments, we can't tell from file
1170 offsets whether a section with zero size should
1171 be placed at the end of one segment or the
1172 beginning of the next. Decide based on vaddr. */
1173 if (hdr->sh_addr >= phdr->p_vaddr
1174 && (hdr->sh_addr + hdr->sh_size
1175 <= phdr->p_vaddr + phdr->p_memsz))
1176 break;
1177 }
1178 }
1179 }
1180
1181 /* Compress/decompress DWARF debug sections with names: .debug_*,
1182 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1183 if ((newsect->flags & SEC_DEBUGGING) != 0
1184 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1185 && (newsect->flags & SEC_ELF_OCTETS) != 0)
1186 {
1187 enum { nothing, compress, decompress } action = nothing;
1188 int compression_header_size;
1189 bfd_size_type uncompressed_size;
1190 unsigned int uncompressed_align_power;
1191 enum compression_type ch_type = ch_none;
1192 bool compressed
1193 = bfd_is_section_compressed_info (abfd, newsect,
1194 &compression_header_size,
1195 &uncompressed_size,
1196 &uncompressed_align_power,
1197 &ch_type);
1198
1199 /* Should we decompress? */
1200 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1201 action = decompress;
1202
1203 /* Should we compress? Or convert to a different compression? */
1204 else if ((abfd->flags & BFD_COMPRESS) != 0
1205 && newsect->size != 0
1206 && compression_header_size >= 0
1207 && uncompressed_size > 0)
1208 {
1209 if (!compressed)
1210 action = compress;
1211 else
1212 {
1213 enum compression_type new_ch_type = ch_none;
1214 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1215 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1216 ? ch_compress_zstd : ch_compress_zlib);
1217 if (new_ch_type != ch_type)
1218 action = compress;
1219 }
1220 }
1221
1222 if (action == compress)
1223 {
1224 if (!bfd_init_section_compress_status (abfd, newsect))
1225 {
1226 _bfd_error_handler
1227 /* xgettext:c-format */
1228 (_("%pB: unable to compress section %s"), abfd, name);
1229 return false;
1230 }
1231 }
1232 else if (action == decompress)
1233 {
1234 if (!bfd_init_section_decompress_status (abfd, newsect))
1235 {
1236 _bfd_error_handler
1237 /* xgettext:c-format */
1238 (_("%pB: unable to decompress section %s"), abfd, name);
1239 return false;
1240 }
1241 #ifndef HAVE_ZSTD
1242 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1243 {
1244 _bfd_error_handler
1245 /* xgettext:c-format */
1246 (_ ("%pB: section %s is compressed with zstd, but BFD "
1247 "is not built with zstd support"),
1248 abfd, name);
1249 newsect->compress_status = COMPRESS_SECTION_NONE;
1250 return false;
1251 }
1252 #endif
1253 if (abfd->is_linker_input
1254 && name[1] == 'z')
1255 {
1256 /* Rename section from .zdebug_* to .debug_* so that ld
1257 scripts will see this section as a debug section. */
1258 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1259 if (new_name == NULL)
1260 return false;
1261 bfd_rename_section (newsect, new_name);
1262 }
1263 }
1264 }
1265
1266 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1267 section. */
1268 if (startswith (name, ".gnu.lto_.lto."))
1269 {
1270 struct lto_section lsection;
1271 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1272 sizeof (struct lto_section)))
1273 abfd->lto_slim_object = lsection.slim_object;
1274 }
1275
1276 return true;
1277 }
1278
1279 const char *const bfd_elf_section_type_names[] =
1280 {
1281 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1282 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1283 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1284 };
1285
1286 /* ELF relocs are against symbols. If we are producing relocatable
1287 output, and the reloc is against an external symbol, and nothing
1288 has given us any additional addend, the resulting reloc will also
1289 be against the same symbol. In such a case, we don't want to
1290 change anything about the way the reloc is handled, since it will
1291 all be done at final link time. Rather than put special case code
1292 into bfd_perform_relocation, all the reloc types use this howto
1293 function, or should call this function for relocatable output. */
1294
1295 bfd_reloc_status_type
1296 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1297 arelent *reloc_entry,
1298 asymbol *symbol,
1299 void *data ATTRIBUTE_UNUSED,
1300 asection *input_section,
1301 bfd *output_bfd,
1302 char **error_message ATTRIBUTE_UNUSED)
1303 {
1304 if (output_bfd != NULL
1305 && (symbol->flags & BSF_SECTION_SYM) == 0
1306 && (! reloc_entry->howto->partial_inplace
1307 || reloc_entry->addend == 0))
1308 {
1309 reloc_entry->address += input_section->output_offset;
1310 return bfd_reloc_ok;
1311 }
1312
1313 /* In some cases the relocation should be treated as output section
1314 relative, as when linking ELF DWARF into PE COFF. Many ELF
1315 targets lack section relative relocations and instead use
1316 ordinary absolute relocations for references between DWARF
1317 sections. That is arguably a bug in those targets but it happens
1318 to work for the usual case of linking to non-loaded ELF debug
1319 sections with VMAs forced to zero. PE COFF on the other hand
1320 doesn't allow a section VMA of zero. */
1321 if (output_bfd == NULL
1322 && !reloc_entry->howto->pc_relative
1323 && (symbol->section->flags & SEC_DEBUGGING) != 0
1324 && (input_section->flags & SEC_DEBUGGING) != 0)
1325 reloc_entry->addend -= symbol->section->output_section->vma;
1326
1327 return bfd_reloc_continue;
1328 }
1329
1330 /* Returns TRUE if section A matches section B.
1332 Names, addresses and links may be different, but everything else
1333 should be the same. */
1334
1335 static bool
1336 section_match (const Elf_Internal_Shdr * a,
1337 const Elf_Internal_Shdr * b)
1338 {
1339 if (a->sh_type != b->sh_type
1340 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1341 || a->sh_addralign != b->sh_addralign
1342 || a->sh_entsize != b->sh_entsize)
1343 return false;
1344 if (a->sh_type == SHT_SYMTAB
1345 || a->sh_type == SHT_STRTAB)
1346 return true;
1347 return a->sh_size == b->sh_size;
1348 }
1349
1350 /* Find a section in OBFD that has the same characteristics
1351 as IHEADER. Return the index of this section or SHN_UNDEF if
1352 none can be found. Check's section HINT first, as this is likely
1353 to be the correct section. */
1354
1355 static unsigned int
1356 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1357 const unsigned int hint)
1358 {
1359 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1360 unsigned int i;
1361
1362 BFD_ASSERT (iheader != NULL);
1363
1364 /* See PR 20922 for a reproducer of the NULL test. */
1365 if (hint < elf_numsections (obfd)
1366 && oheaders[hint] != NULL
1367 && section_match (oheaders[hint], iheader))
1368 return hint;
1369
1370 for (i = 1; i < elf_numsections (obfd); i++)
1371 {
1372 Elf_Internal_Shdr * oheader = oheaders[i];
1373
1374 if (oheader == NULL)
1375 continue;
1376 if (section_match (oheader, iheader))
1377 /* FIXME: Do we care if there is a potential for
1378 multiple matches ? */
1379 return i;
1380 }
1381
1382 return SHN_UNDEF;
1383 }
1384
1385 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1386 Processor specific section, based upon a matching input section.
1387 Returns TRUE upon success, FALSE otherwise. */
1388
1389 static bool
1390 copy_special_section_fields (const bfd *ibfd,
1391 bfd *obfd,
1392 const Elf_Internal_Shdr *iheader,
1393 Elf_Internal_Shdr *oheader,
1394 const unsigned int secnum)
1395 {
1396 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1397 const Elf_Internal_Shdr **iheaders
1398 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1399 bool changed = false;
1400 unsigned int sh_link;
1401
1402 if (oheader->sh_type == SHT_NOBITS)
1403 {
1404 /* This is a feature for objcopy --only-keep-debug:
1405 When a section's type is changed to NOBITS, we preserve
1406 the sh_link and sh_info fields so that they can be
1407 matched up with the original.
1408
1409 Note: Strictly speaking these assignments are wrong.
1410 The sh_link and sh_info fields should point to the
1411 relevent sections in the output BFD, which may not be in
1412 the same location as they were in the input BFD. But
1413 the whole point of this action is to preserve the
1414 original values of the sh_link and sh_info fields, so
1415 that they can be matched up with the section headers in
1416 the original file. So strictly speaking we may be
1417 creating an invalid ELF file, but it is only for a file
1418 that just contains debug info and only for sections
1419 without any contents. */
1420 if (oheader->sh_link == 0)
1421 oheader->sh_link = iheader->sh_link;
1422 if (oheader->sh_info == 0)
1423 oheader->sh_info = iheader->sh_info;
1424 return true;
1425 }
1426
1427 /* Allow the target a chance to decide how these fields should be set. */
1428 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1429 iheader, oheader))
1430 return true;
1431
1432 /* We have an iheader which might match oheader, and which has non-zero
1433 sh_info and/or sh_link fields. Attempt to follow those links and find
1434 the section in the output bfd which corresponds to the linked section
1435 in the input bfd. */
1436 if (iheader->sh_link != SHN_UNDEF)
1437 {
1438 /* See PR 20931 for a reproducer. */
1439 if (iheader->sh_link >= elf_numsections (ibfd))
1440 {
1441 _bfd_error_handler
1442 /* xgettext:c-format */
1443 (_("%pB: invalid sh_link field (%d) in section number %d"),
1444 ibfd, iheader->sh_link, secnum);
1445 return false;
1446 }
1447
1448 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1449 if (sh_link != SHN_UNDEF)
1450 {
1451 oheader->sh_link = sh_link;
1452 changed = true;
1453 }
1454 else
1455 /* FIXME: Should we install iheader->sh_link
1456 if we could not find a match ? */
1457 _bfd_error_handler
1458 /* xgettext:c-format */
1459 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1460 }
1461
1462 if (iheader->sh_info)
1463 {
1464 /* The sh_info field can hold arbitrary information, but if the
1465 SHF_LINK_INFO flag is set then it should be interpreted as a
1466 section index. */
1467 if (iheader->sh_flags & SHF_INFO_LINK)
1468 {
1469 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1470 iheader->sh_info);
1471 if (sh_link != SHN_UNDEF)
1472 oheader->sh_flags |= SHF_INFO_LINK;
1473 }
1474 else
1475 /* No idea what it means - just copy it. */
1476 sh_link = iheader->sh_info;
1477
1478 if (sh_link != SHN_UNDEF)
1479 {
1480 oheader->sh_info = sh_link;
1481 changed = true;
1482 }
1483 else
1484 _bfd_error_handler
1485 /* xgettext:c-format */
1486 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1487 }
1488
1489 return changed;
1490 }
1491
1492 /* Copy the program header and other data from one object module to
1493 another. */
1494
1495 bool
1496 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1497 {
1498 const Elf_Internal_Shdr **iheaders
1499 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1500 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1501 const struct elf_backend_data *bed;
1502 unsigned int i;
1503
1504 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1505 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1506 return true;
1507
1508 if (!elf_flags_init (obfd))
1509 {
1510 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1511 elf_flags_init (obfd) = true;
1512 }
1513
1514 elf_gp (obfd) = elf_gp (ibfd);
1515
1516 /* Also copy the EI_OSABI field. */
1517 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1518 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1519
1520 /* If set, copy the EI_ABIVERSION field. */
1521 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1522 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1523 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1524
1525 /* Copy object attributes. */
1526 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1527
1528 if (iheaders == NULL || oheaders == NULL)
1529 return true;
1530
1531 bed = get_elf_backend_data (obfd);
1532
1533 /* Possibly copy other fields in the section header. */
1534 for (i = 1; i < elf_numsections (obfd); i++)
1535 {
1536 unsigned int j;
1537 Elf_Internal_Shdr * oheader = oheaders[i];
1538
1539 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1540 because of a special case need for generating separate debug info
1541 files. See below for more details. */
1542 if (oheader == NULL
1543 || (oheader->sh_type != SHT_NOBITS
1544 && oheader->sh_type < SHT_LOOS))
1545 continue;
1546
1547 /* Ignore empty sections, and sections whose
1548 fields have already been initialised. */
1549 if (oheader->sh_size == 0
1550 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1551 continue;
1552
1553 /* Scan for the matching section in the input bfd.
1554 First we try for a direct mapping between the input and
1555 output sections. */
1556 for (j = 1; j < elf_numsections (ibfd); j++)
1557 {
1558 const Elf_Internal_Shdr * iheader = iheaders[j];
1559
1560 if (iheader == NULL)
1561 continue;
1562
1563 if (oheader->bfd_section != NULL
1564 && iheader->bfd_section != NULL
1565 && iheader->bfd_section->output_section != NULL
1566 && iheader->bfd_section->output_section == oheader->bfd_section)
1567 {
1568 /* We have found a connection from the input section to
1569 the output section. Attempt to copy the header fields.
1570 If this fails then do not try any further sections -
1571 there should only be a one-to-one mapping between
1572 input and output. */
1573 if (!copy_special_section_fields (ibfd, obfd,
1574 iheader, oheader, i))
1575 j = elf_numsections (ibfd);
1576 break;
1577 }
1578 }
1579
1580 if (j < elf_numsections (ibfd))
1581 continue;
1582
1583 /* That failed. So try to deduce the corresponding input section.
1584 Unfortunately we cannot compare names as the output string table
1585 is empty, so instead we check size, address and type. */
1586 for (j = 1; j < elf_numsections (ibfd); j++)
1587 {
1588 const Elf_Internal_Shdr * iheader = iheaders[j];
1589
1590 if (iheader == NULL)
1591 continue;
1592
1593 /* Try matching fields in the input section's header.
1594 Since --only-keep-debug turns all non-debug sections into
1595 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1596 input type. */
1597 if ((oheader->sh_type == SHT_NOBITS
1598 || iheader->sh_type == oheader->sh_type)
1599 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1600 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1601 && iheader->sh_addralign == oheader->sh_addralign
1602 && iheader->sh_entsize == oheader->sh_entsize
1603 && iheader->sh_size == oheader->sh_size
1604 && iheader->sh_addr == oheader->sh_addr
1605 && (iheader->sh_info != oheader->sh_info
1606 || iheader->sh_link != oheader->sh_link))
1607 {
1608 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1609 break;
1610 }
1611 }
1612
1613 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1614 {
1615 /* Final attempt. Call the backend copy function
1616 with a NULL input section. */
1617 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1618 NULL, oheader);
1619 }
1620 }
1621
1622 return true;
1623 }
1624
1625 static const char *
1626 get_segment_type (unsigned int p_type)
1627 {
1628 const char *pt;
1629 switch (p_type)
1630 {
1631 case PT_NULL: pt = "NULL"; break;
1632 case PT_LOAD: pt = "LOAD"; break;
1633 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1634 case PT_INTERP: pt = "INTERP"; break;
1635 case PT_NOTE: pt = "NOTE"; break;
1636 case PT_SHLIB: pt = "SHLIB"; break;
1637 case PT_PHDR: pt = "PHDR"; break;
1638 case PT_TLS: pt = "TLS"; break;
1639 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1640 case PT_GNU_STACK: pt = "STACK"; break;
1641 case PT_GNU_RELRO: pt = "RELRO"; break;
1642 case PT_GNU_SFRAME: pt = "SFRAME"; break;
1643 default: pt = NULL; break;
1644 }
1645 return pt;
1646 }
1647
1648 /* Print out the program headers. */
1649
1650 bool
1651 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1652 {
1653 FILE *f = (FILE *) farg;
1654 Elf_Internal_Phdr *p;
1655 asection *s;
1656 bfd_byte *dynbuf = NULL;
1657
1658 p = elf_tdata (abfd)->phdr;
1659 if (p != NULL)
1660 {
1661 unsigned int i, c;
1662
1663 fprintf (f, _("\nProgram Header:\n"));
1664 c = elf_elfheader (abfd)->e_phnum;
1665 for (i = 0; i < c; i++, p++)
1666 {
1667 const char *pt = get_segment_type (p->p_type);
1668 char buf[20];
1669
1670 if (pt == NULL)
1671 {
1672 sprintf (buf, "0x%lx", p->p_type);
1673 pt = buf;
1674 }
1675 fprintf (f, "%8s off 0x", pt);
1676 bfd_fprintf_vma (abfd, f, p->p_offset);
1677 fprintf (f, " vaddr 0x");
1678 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1679 fprintf (f, " paddr 0x");
1680 bfd_fprintf_vma (abfd, f, p->p_paddr);
1681 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1682 fprintf (f, " filesz 0x");
1683 bfd_fprintf_vma (abfd, f, p->p_filesz);
1684 fprintf (f, " memsz 0x");
1685 bfd_fprintf_vma (abfd, f, p->p_memsz);
1686 fprintf (f, " flags %c%c%c",
1687 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1688 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1689 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1690 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1691 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1692 fprintf (f, "\n");
1693 }
1694 }
1695
1696 s = bfd_get_section_by_name (abfd, ".dynamic");
1697 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1698 {
1699 unsigned int elfsec;
1700 unsigned long shlink;
1701 bfd_byte *extdyn, *extdynend;
1702 size_t extdynsize;
1703 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1704
1705 fprintf (f, _("\nDynamic Section:\n"));
1706
1707 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1708 goto error_return;
1709
1710 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1711 if (elfsec == SHN_BAD)
1712 goto error_return;
1713 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1714
1715 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1716 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1717
1718 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1719 (size_t) (extdynend - extdyn) >= extdynsize;
1720 extdyn += extdynsize)
1721 {
1722 Elf_Internal_Dyn dyn;
1723 const char *name = "";
1724 char ab[20];
1725 bool stringp;
1726 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1727
1728 (*swap_dyn_in) (abfd, extdyn, &dyn);
1729
1730 if (dyn.d_tag == DT_NULL)
1731 break;
1732
1733 stringp = false;
1734 switch (dyn.d_tag)
1735 {
1736 default:
1737 if (bed->elf_backend_get_target_dtag)
1738 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1739
1740 if (!strcmp (name, ""))
1741 {
1742 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1743 name = ab;
1744 }
1745 break;
1746
1747 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1748 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1749 case DT_PLTGOT: name = "PLTGOT"; break;
1750 case DT_HASH: name = "HASH"; break;
1751 case DT_STRTAB: name = "STRTAB"; break;
1752 case DT_SYMTAB: name = "SYMTAB"; break;
1753 case DT_RELA: name = "RELA"; break;
1754 case DT_RELASZ: name = "RELASZ"; break;
1755 case DT_RELAENT: name = "RELAENT"; break;
1756 case DT_STRSZ: name = "STRSZ"; break;
1757 case DT_SYMENT: name = "SYMENT"; break;
1758 case DT_INIT: name = "INIT"; break;
1759 case DT_FINI: name = "FINI"; break;
1760 case DT_SONAME: name = "SONAME"; stringp = true; break;
1761 case DT_RPATH: name = "RPATH"; stringp = true; break;
1762 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1763 case DT_REL: name = "REL"; break;
1764 case DT_RELSZ: name = "RELSZ"; break;
1765 case DT_RELENT: name = "RELENT"; break;
1766 case DT_RELR: name = "RELR"; break;
1767 case DT_RELRSZ: name = "RELRSZ"; break;
1768 case DT_RELRENT: name = "RELRENT"; break;
1769 case DT_PLTREL: name = "PLTREL"; break;
1770 case DT_DEBUG: name = "DEBUG"; break;
1771 case DT_TEXTREL: name = "TEXTREL"; break;
1772 case DT_JMPREL: name = "JMPREL"; break;
1773 case DT_BIND_NOW: name = "BIND_NOW"; break;
1774 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1775 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1776 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1777 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1778 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1779 case DT_FLAGS: name = "FLAGS"; break;
1780 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1781 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1782 case DT_CHECKSUM: name = "CHECKSUM"; break;
1783 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1784 case DT_MOVEENT: name = "MOVEENT"; break;
1785 case DT_MOVESZ: name = "MOVESZ"; break;
1786 case DT_FEATURE: name = "FEATURE"; break;
1787 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1788 case DT_SYMINSZ: name = "SYMINSZ"; break;
1789 case DT_SYMINENT: name = "SYMINENT"; break;
1790 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1791 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1792 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1793 case DT_PLTPAD: name = "PLTPAD"; break;
1794 case DT_MOVETAB: name = "MOVETAB"; break;
1795 case DT_SYMINFO: name = "SYMINFO"; break;
1796 case DT_RELACOUNT: name = "RELACOUNT"; break;
1797 case DT_RELCOUNT: name = "RELCOUNT"; break;
1798 case DT_FLAGS_1: name = "FLAGS_1"; break;
1799 case DT_VERSYM: name = "VERSYM"; break;
1800 case DT_VERDEF: name = "VERDEF"; break;
1801 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1802 case DT_VERNEED: name = "VERNEED"; break;
1803 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1804 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1805 case DT_USED: name = "USED"; break;
1806 case DT_FILTER: name = "FILTER"; stringp = true; break;
1807 case DT_GNU_HASH: name = "GNU_HASH"; break;
1808 }
1809
1810 fprintf (f, " %-20s ", name);
1811 if (! stringp)
1812 {
1813 fprintf (f, "0x");
1814 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1815 }
1816 else
1817 {
1818 const char *string;
1819 unsigned int tagv = dyn.d_un.d_val;
1820
1821 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1822 if (string == NULL)
1823 goto error_return;
1824 fprintf (f, "%s", string);
1825 }
1826 fprintf (f, "\n");
1827 }
1828
1829 free (dynbuf);
1830 dynbuf = NULL;
1831 }
1832
1833 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1834 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1835 {
1836 if (! _bfd_elf_slurp_version_tables (abfd, false))
1837 return false;
1838 }
1839
1840 if (elf_dynverdef (abfd) != 0)
1841 {
1842 Elf_Internal_Verdef *t;
1843
1844 fprintf (f, _("\nVersion definitions:\n"));
1845 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1846 {
1847 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1848 t->vd_flags, t->vd_hash,
1849 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1850 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1851 {
1852 Elf_Internal_Verdaux *a;
1853
1854 fprintf (f, "\t");
1855 for (a = t->vd_auxptr->vda_nextptr;
1856 a != NULL;
1857 a = a->vda_nextptr)
1858 fprintf (f, "%s ",
1859 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1860 fprintf (f, "\n");
1861 }
1862 }
1863 }
1864
1865 if (elf_dynverref (abfd) != 0)
1866 {
1867 Elf_Internal_Verneed *t;
1868
1869 fprintf (f, _("\nVersion References:\n"));
1870 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1871 {
1872 Elf_Internal_Vernaux *a;
1873
1874 fprintf (f, _(" required from %s:\n"),
1875 t->vn_filename ? t->vn_filename : "<corrupt>");
1876 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1877 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1878 a->vna_flags, a->vna_other,
1879 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1880 }
1881 }
1882
1883 return true;
1884
1885 error_return:
1886 free (dynbuf);
1887 return false;
1888 }
1889
1890 /* Find the file offset corresponding to VMA by using the program
1891 headers. */
1892
1893 static file_ptr
1894 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1895 size_t size, size_t *max_size_p)
1896 {
1897 Elf_Internal_Phdr *seg;
1898 size_t i;
1899
1900 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1901 if (seg->p_type == PT_LOAD
1902 && vma >= (seg->p_vaddr & -seg->p_align)
1903 && vma + size <= seg->p_vaddr + seg->p_filesz)
1904 {
1905 if (max_size_p)
1906 *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1907 return vma - seg->p_vaddr + seg->p_offset;
1908 }
1909
1910 if (max_size_p)
1911 *max_size_p = 0;
1912 bfd_set_error (bfd_error_invalid_operation);
1913 return (file_ptr) -1;
1914 }
1915
1916 /* Convert hash table to internal form. */
1917
1918 static bfd_vma *
1919 get_hash_table_data (bfd *abfd, bfd_size_type number,
1920 unsigned int ent_size, bfd_size_type filesize)
1921 {
1922 unsigned char *e_data = NULL;
1923 bfd_vma *i_data = NULL;
1924 bfd_size_type size;
1925
1926 if (ent_size != 4 && ent_size != 8)
1927 return NULL;
1928
1929 if ((size_t) number != number)
1930 {
1931 bfd_set_error (bfd_error_file_too_big);
1932 return NULL;
1933 }
1934
1935 size = ent_size * number;
1936 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1937 attempting to allocate memory when the read is bound to fail. */
1938 if (size > filesize
1939 || number >= ~(size_t) 0 / ent_size
1940 || number >= ~(size_t) 0 / sizeof (*i_data))
1941 {
1942 bfd_set_error (bfd_error_file_too_big);
1943 return NULL;
1944 }
1945
1946 e_data = _bfd_malloc_and_read (abfd, size, size);
1947 if (e_data == NULL)
1948 return NULL;
1949
1950 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1951 if (i_data == NULL)
1952 {
1953 free (e_data);
1954 return NULL;
1955 }
1956
1957 if (ent_size == 4)
1958 while (number--)
1959 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1960 else
1961 while (number--)
1962 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1963
1964 free (e_data);
1965 return i_data;
1966 }
1967
1968 /* Address of .MIPS.xhash section. FIXME: What is the best way to
1969 support DT_MIPS_XHASH? */
1970 #define DT_MIPS_XHASH 0x70000036
1971
1972 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */
1973
1974 bool
1975 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1976 Elf_Internal_Phdr *phdrs, size_t phnum,
1977 bfd_size_type filesize)
1978 {
1979 bfd_byte *extdyn, *extdynend;
1980 size_t extdynsize;
1981 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1982 bool (*swap_symbol_in) (bfd *, const void *, const void *,
1983 Elf_Internal_Sym *);
1984 Elf_Internal_Dyn dyn;
1985 bfd_vma dt_hash = 0;
1986 bfd_vma dt_gnu_hash = 0;
1987 bfd_vma dt_mips_xhash = 0;
1988 bfd_vma dt_strtab = 0;
1989 bfd_vma dt_symtab = 0;
1990 size_t dt_strsz = 0;
1991 bfd_vma dt_versym = 0;
1992 bfd_vma dt_verdef = 0;
1993 bfd_vma dt_verneed = 0;
1994 bfd_byte *dynbuf = NULL;
1995 char *strbuf = NULL;
1996 bfd_vma *gnubuckets = NULL;
1997 bfd_vma *gnuchains = NULL;
1998 bfd_vma *mipsxlat = NULL;
1999 file_ptr saved_filepos, filepos;
2000 bool res = false;
2001 size_t amt;
2002 bfd_byte *esymbuf = NULL, *esym;
2003 bfd_size_type symcount;
2004 Elf_Internal_Sym *isymbuf = NULL;
2005 Elf_Internal_Sym *isym, *isymend;
2006 bfd_byte *versym = NULL;
2007 bfd_byte *verdef = NULL;
2008 bfd_byte *verneed = NULL;
2009 size_t verdef_size = 0;
2010 size_t verneed_size = 0;
2011 size_t extsym_size;
2012 const struct elf_backend_data *bed;
2013
2014 /* Return TRUE if symbol table is bad. */
2015 if (elf_bad_symtab (abfd))
2016 return true;
2017
2018 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */
2019 if (elf_tdata (abfd)->dt_strtab != NULL)
2020 return true;
2021
2022 bed = get_elf_backend_data (abfd);
2023
2024 /* Save file position for elf_object_p. */
2025 saved_filepos = bfd_tell (abfd);
2026
2027 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
2028 goto error_return;
2029
2030 dynbuf = _bfd_malloc_and_read (abfd, phdr->p_filesz, phdr->p_filesz);
2031 if (dynbuf == NULL)
2032 goto error_return;
2033
2034 extsym_size = bed->s->sizeof_sym;
2035 extdynsize = bed->s->sizeof_dyn;
2036 swap_dyn_in = bed->s->swap_dyn_in;
2037
2038 extdyn = dynbuf;
2039 if (phdr->p_filesz < extdynsize)
2040 goto error_return;
2041 extdynend = extdyn + phdr->p_filesz;
2042 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
2043 {
2044 swap_dyn_in (abfd, extdyn, &dyn);
2045
2046 if (dyn.d_tag == DT_NULL)
2047 break;
2048
2049 switch (dyn.d_tag)
2050 {
2051 case DT_HASH:
2052 dt_hash = dyn.d_un.d_val;
2053 break;
2054 case DT_GNU_HASH:
2055 if (bed->elf_machine_code != EM_MIPS
2056 && bed->elf_machine_code != EM_MIPS_RS3_LE)
2057 dt_gnu_hash = dyn.d_un.d_val;
2058 break;
2059 case DT_STRTAB:
2060 dt_strtab = dyn.d_un.d_val;
2061 break;
2062 case DT_SYMTAB:
2063 dt_symtab = dyn.d_un.d_val;
2064 break;
2065 case DT_STRSZ:
2066 dt_strsz = dyn.d_un.d_val;
2067 break;
2068 case DT_SYMENT:
2069 if (dyn.d_un.d_val != extsym_size)
2070 goto error_return;
2071 break;
2072 case DT_VERSYM:
2073 dt_versym = dyn.d_un.d_val;
2074 break;
2075 case DT_VERDEF:
2076 dt_verdef = dyn.d_un.d_val;
2077 break;
2078 case DT_VERNEED:
2079 dt_verneed = dyn.d_un.d_val;
2080 break;
2081 default:
2082 if (dyn.d_tag == DT_MIPS_XHASH
2083 && (bed->elf_machine_code == EM_MIPS
2084 || bed->elf_machine_code == EM_MIPS_RS3_LE))
2085 {
2086 dt_gnu_hash = dyn.d_un.d_val;
2087 dt_mips_xhash = dyn.d_un.d_val;
2088 }
2089 break;
2090 }
2091 }
2092
2093 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
2094 segment. */
2095 if ((!dt_hash && !dt_gnu_hash)
2096 || !dt_strtab
2097 || !dt_symtab
2098 || !dt_strsz)
2099 goto error_return;
2100
2101 /* Get dynamic string table. */
2102 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
2103 if (filepos == (file_ptr) -1
2104 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2105 goto error_return;
2106
2107 /* Dynamic string table must be valid until ABFD is closed. */
2108 strbuf = (char *) _bfd_alloc_and_read (abfd, dt_strsz + 1, dt_strsz);
2109 if (strbuf == NULL)
2110 goto error_return;
2111 /* Since this is a string table, make sure that it is terminated. */
2112 strbuf[dt_strsz] = 0;
2113
2114 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer
2115 DT_HASH since it is simpler than DT_GNU_HASH. */
2116 if (dt_hash)
2117 {
2118 unsigned char nb[16];
2119 unsigned int hash_ent_size;
2120
2121 switch (bed->elf_machine_code)
2122 {
2123 case EM_ALPHA:
2124 case EM_S390:
2125 case EM_S390_OLD:
2126 if (bed->s->elfclass == ELFCLASS64)
2127 {
2128 hash_ent_size = 8;
2129 break;
2130 }
2131 /* FALLTHROUGH */
2132 default:
2133 hash_ent_size = 4;
2134 break;
2135 }
2136
2137 filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
2138 NULL);
2139 if (filepos == (file_ptr) -1
2140 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2141 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2142 goto error_return;
2143
2144 /* The number of dynamic symbol table entries equals the number
2145 of chains. */
2146 if (hash_ent_size == 8)
2147 symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2148 else
2149 symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2150 }
2151 else
2152 {
2153 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2154 bindings are in hash table. Since in dynamic symbol table,
2155 all symbols with STB_LOCAL binding are placed before symbols
2156 with other bindings and all undefined symbols are placed
2157 before defined ones, the highest symbol index in DT_GNU_HASH
2158 is the highest dynamic symbol table index. */
2159 unsigned char nb[16];
2160 bfd_vma ngnubuckets;
2161 bfd_vma gnusymidx;
2162 size_t i, ngnuchains;
2163 bfd_vma maxchain = 0xffffffff, bitmaskwords;
2164 bfd_vma buckets_vma;
2165
2166 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2167 sizeof (nb), NULL);
2168 if (filepos == (file_ptr) -1
2169 || bfd_seek (abfd, filepos, SEEK_SET) != 0
2170 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2171 goto error_return;
2172
2173 ngnubuckets = bfd_get_32 (abfd, nb);
2174 gnusymidx = bfd_get_32 (abfd, nb + 4);
2175 bitmaskwords = bfd_get_32 (abfd, nb + 8);
2176 buckets_vma = dt_gnu_hash + 16;
2177 if (bed->s->elfclass == ELFCLASS32)
2178 buckets_vma += bitmaskwords * 4;
2179 else
2180 buckets_vma += bitmaskwords * 8;
2181 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2182 if (filepos == (file_ptr) -1
2183 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2184 goto error_return;
2185
2186 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2187 if (gnubuckets == NULL)
2188 goto error_return;
2189
2190 for (i = 0; i < ngnubuckets; i++)
2191 if (gnubuckets[i] != 0)
2192 {
2193 if (gnubuckets[i] < gnusymidx)
2194 goto error_return;
2195
2196 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2197 maxchain = gnubuckets[i];
2198 }
2199
2200 if (maxchain == 0xffffffff)
2201 {
2202 symcount = 0;
2203 goto empty_gnu_hash;
2204 }
2205
2206 maxchain -= gnusymidx;
2207 filepos = offset_from_vma (phdrs, phnum,
2208 (buckets_vma +
2209 4 * (ngnubuckets + maxchain)),
2210 4, NULL);
2211 if (filepos == (file_ptr) -1
2212 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2213 goto error_return;
2214
2215 do
2216 {
2217 if (bfd_read (nb, 4, abfd) != 4)
2218 goto error_return;
2219 ++maxchain;
2220 if (maxchain == 0)
2221 goto error_return;
2222 }
2223 while ((bfd_get_32 (abfd, nb) & 1) == 0);
2224
2225 filepos = offset_from_vma (phdrs, phnum,
2226 (buckets_vma + 4 * ngnubuckets),
2227 4, NULL);
2228 if (filepos == (file_ptr) -1
2229 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2230 goto error_return;
2231
2232 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2233 if (gnuchains == NULL)
2234 goto error_return;
2235 ngnuchains = maxchain;
2236
2237 if (dt_mips_xhash)
2238 {
2239 filepos = offset_from_vma (phdrs, phnum,
2240 (buckets_vma
2241 + 4 * (ngnubuckets + maxchain)),
2242 4, NULL);
2243 if (filepos == (file_ptr) -1
2244 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2245 goto error_return;
2246
2247 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2248 if (mipsxlat == NULL)
2249 goto error_return;
2250 }
2251
2252 symcount = 0;
2253 for (i = 0; i < ngnubuckets; ++i)
2254 if (gnubuckets[i] != 0)
2255 {
2256 bfd_vma si = gnubuckets[i];
2257 bfd_vma off = si - gnusymidx;
2258 do
2259 {
2260 if (mipsxlat)
2261 {
2262 if (mipsxlat[off] >= symcount)
2263 symcount = mipsxlat[off] + 1;
2264 }
2265 else
2266 {
2267 if (si >= symcount)
2268 symcount = si + 1;
2269 }
2270 si++;
2271 }
2272 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2273 }
2274 }
2275
2276 /* Swap in dynamic symbol table. */
2277 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2278 {
2279 bfd_set_error (bfd_error_file_too_big);
2280 goto error_return;
2281 }
2282
2283 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2284 if (filepos == (file_ptr) -1
2285 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2286 goto error_return;
2287 esymbuf = _bfd_malloc_and_read (abfd, amt, amt);
2288 if (esymbuf == NULL)
2289 goto error_return;
2290
2291 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2292 {
2293 bfd_set_error (bfd_error_file_too_big);
2294 goto error_return;
2295 }
2296
2297 /* Dynamic symbol table must be valid until ABFD is closed. */
2298 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2299 if (isymbuf == NULL)
2300 goto error_return;
2301
2302 swap_symbol_in = bed->s->swap_symbol_in;
2303
2304 /* Convert the symbols to internal form. */
2305 isymend = isymbuf + symcount;
2306 for (esym = esymbuf, isym = isymbuf;
2307 isym < isymend;
2308 esym += extsym_size, isym++)
2309 if (!swap_symbol_in (abfd, esym, NULL, isym)
2310 || isym->st_name >= dt_strsz)
2311 {
2312 bfd_set_error (bfd_error_invalid_operation);
2313 goto error_return;
2314 }
2315
2316 if (dt_versym)
2317 {
2318 /* Swap in DT_VERSYM. */
2319 if (_bfd_mul_overflow (symcount, 2, &amt))
2320 {
2321 bfd_set_error (bfd_error_file_too_big);
2322 goto error_return;
2323 }
2324
2325 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2326 if (filepos == (file_ptr) -1
2327 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2328 goto error_return;
2329
2330 /* DT_VERSYM info must be valid until ABFD is closed. */
2331 versym = _bfd_alloc_and_read (abfd, amt, amt);
2332
2333 if (dt_verdef)
2334 {
2335 /* Read in DT_VERDEF. */
2336 filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2337 0, &verdef_size);
2338 if (filepos == (file_ptr) -1
2339 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2340 goto error_return;
2341
2342 /* DT_VERDEF info must be valid until ABFD is closed. */
2343 verdef = _bfd_alloc_and_read (abfd, verdef_size,
2344 verdef_size);
2345 }
2346
2347 if (dt_verneed)
2348 {
2349 /* Read in DT_VERNEED. */
2350 filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2351 0, &verneed_size);
2352 if (filepos == (file_ptr) -1
2353 || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2354 goto error_return;
2355
2356 /* DT_VERNEED info must be valid until ABFD is closed. */
2357 verneed = _bfd_alloc_and_read (abfd, verneed_size,
2358 verneed_size);
2359 }
2360 }
2361
2362 empty_gnu_hash:
2363 elf_tdata (abfd)->dt_strtab = strbuf;
2364 elf_tdata (abfd)->dt_strsz = dt_strsz;
2365 elf_tdata (abfd)->dt_symtab = isymbuf;
2366 elf_tdata (abfd)->dt_symtab_count = symcount;
2367 elf_tdata (abfd)->dt_versym = versym;
2368 elf_tdata (abfd)->dt_verdef = verdef;
2369 elf_tdata (abfd)->dt_verneed = verneed;
2370 elf_tdata (abfd)->dt_verdef_count
2371 = verdef_size / sizeof (Elf_External_Verdef);
2372 elf_tdata (abfd)->dt_verneed_count
2373 = verneed_size / sizeof (Elf_External_Verneed);
2374
2375 res = true;
2376
2377 error_return:
2378 /* Restore file position for elf_object_p. */
2379 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2380 res = false;
2381 free (dynbuf);
2382 free (esymbuf);
2383 free (gnubuckets);
2384 free (gnuchains);
2385 free (mipsxlat);
2386 return res;
2387 }
2388
2389 /* Reconstruct section from dynamic symbol. */
2390
2391 asection *
2392 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2393 Elf_Internal_Sym *isym)
2394 {
2395 asection *sec;
2396 flagword flags;
2397
2398 if (!elf_use_dt_symtab_p (abfd))
2399 return NULL;
2400
2401 flags = SEC_ALLOC | SEC_LOAD;
2402 switch (ELF_ST_TYPE (isym->st_info))
2403 {
2404 case STT_FUNC:
2405 case STT_GNU_IFUNC:
2406 sec = bfd_get_section_by_name (abfd, ".text");
2407 if (sec == NULL)
2408 sec = bfd_make_section_with_flags (abfd,
2409 ".text",
2410 flags | SEC_CODE);
2411 break;
2412 case STT_COMMON:
2413 sec = bfd_com_section_ptr;
2414 break;
2415 case STT_OBJECT:
2416 sec = bfd_get_section_by_name (abfd, ".data");
2417 if (sec == NULL)
2418 sec = bfd_make_section_with_flags (abfd,
2419 ".data",
2420 flags | SEC_DATA);
2421 break;
2422 case STT_TLS:
2423 sec = bfd_get_section_by_name (abfd, ".tdata");
2424 if (sec == NULL)
2425 sec = bfd_make_section_with_flags (abfd,
2426 ".tdata",
2427 (flags
2428 | SEC_DATA
2429 | SEC_THREAD_LOCAL));
2430 break;
2431 default:
2432 sec = bfd_abs_section_ptr;
2433 break;
2434 }
2435
2436 return sec;
2437 }
2438
2439 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2440 and return symbol version for symbol version itself. */
2441
2442 const char *
2443 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2444 bool base_p,
2445 bool *hidden)
2446 {
2447 const char *version_string = NULL;
2448 if ((elf_dynversym (abfd) != 0
2449 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2450 || (elf_tdata (abfd)->dt_versym != NULL
2451 && (elf_tdata (abfd)->dt_verdef != NULL
2452 || elf_tdata (abfd)->dt_verneed != NULL)))
2453 {
2454 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2455
2456 *hidden = (vernum & VERSYM_HIDDEN) != 0;
2457 vernum &= VERSYM_VERSION;
2458
2459 if (vernum == 0)
2460 version_string = "";
2461 else if (vernum == 1
2462 && (vernum > elf_tdata (abfd)->cverdefs
2463 || (elf_tdata (abfd)->verdef[0].vd_flags
2464 == VER_FLG_BASE)))
2465 version_string = base_p ? "Base" : "";
2466 else if (vernum <= elf_tdata (abfd)->cverdefs)
2467 {
2468 const char *nodename
2469 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2470 version_string = "";
2471 if (base_p
2472 || nodename == NULL
2473 || symbol->name == NULL
2474 || strcmp (symbol->name, nodename) != 0)
2475 version_string = nodename;
2476 }
2477 else
2478 {
2479 Elf_Internal_Verneed *t;
2480
2481 version_string = _("<corrupt>");
2482 for (t = elf_tdata (abfd)->verref;
2483 t != NULL;
2484 t = t->vn_nextref)
2485 {
2486 Elf_Internal_Vernaux *a;
2487
2488 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2489 {
2490 if (a->vna_other == vernum)
2491 {
2492 *hidden = true;
2493 version_string = a->vna_nodename;
2494 break;
2495 }
2496 }
2497 }
2498 }
2499 }
2500 return version_string;
2501 }
2502
2503 /* Display ELF-specific fields of a symbol. */
2504
2505 void
2506 bfd_elf_print_symbol (bfd *abfd,
2507 void *filep,
2508 asymbol *symbol,
2509 bfd_print_symbol_type how)
2510 {
2511 FILE *file = (FILE *) filep;
2512 switch (how)
2513 {
2514 case bfd_print_symbol_name:
2515 fprintf (file, "%s", symbol->name);
2516 break;
2517 case bfd_print_symbol_more:
2518 fprintf (file, "elf ");
2519 bfd_fprintf_vma (abfd, file, symbol->value);
2520 fprintf (file, " %x", symbol->flags);
2521 break;
2522 case bfd_print_symbol_all:
2523 {
2524 const char *section_name;
2525 const char *name = NULL;
2526 const struct elf_backend_data *bed;
2527 unsigned char st_other;
2528 bfd_vma val;
2529 const char *version_string;
2530 bool hidden;
2531
2532 section_name = symbol->section ? symbol->section->name : "(*none*)";
2533
2534 bed = get_elf_backend_data (abfd);
2535 if (bed->elf_backend_print_symbol_all)
2536 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2537
2538 if (name == NULL)
2539 {
2540 name = symbol->name;
2541 bfd_print_symbol_vandf (abfd, file, symbol);
2542 }
2543
2544 fprintf (file, " %s\t", section_name);
2545 /* Print the "other" value for a symbol. For common symbols,
2546 we've already printed the size; now print the alignment.
2547 For other symbols, we have no specified alignment, and
2548 we've printed the address; now print the size. */
2549 if (symbol->section && bfd_is_com_section (symbol->section))
2550 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2551 else
2552 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2553 bfd_fprintf_vma (abfd, file, val);
2554
2555 /* If we have version information, print it. */
2556 version_string = _bfd_elf_get_symbol_version_string (abfd,
2557 symbol,
2558 true,
2559 &hidden);
2560 if (version_string)
2561 {
2562 if (!hidden)
2563 fprintf (file, " %-11s", version_string);
2564 else
2565 {
2566 int i;
2567
2568 fprintf (file, " (%s)", version_string);
2569 for (i = 10 - strlen (version_string); i > 0; --i)
2570 putc (' ', file);
2571 }
2572 }
2573
2574 /* If the st_other field is not zero, print it. */
2575 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2576
2577 switch (st_other)
2578 {
2579 case 0: break;
2580 case STV_INTERNAL: fprintf (file, " .internal"); break;
2581 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2582 case STV_PROTECTED: fprintf (file, " .protected"); break;
2583 default:
2584 /* Some other non-defined flags are also present, so print
2585 everything hex. */
2586 fprintf (file, " 0x%02x", (unsigned int) st_other);
2587 }
2588
2589 fprintf (file, " %s", name);
2590 }
2591 break;
2592 }
2593 }
2594
2595 /* ELF .o/exec file reading */
2597
2598 /* Create a new bfd section from an ELF section header. */
2599
2600 bool
2601 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2602 {
2603 Elf_Internal_Shdr *hdr;
2604 Elf_Internal_Ehdr *ehdr;
2605 const struct elf_backend_data *bed;
2606 const char *name;
2607 bool ret = true;
2608
2609 if (shindex >= elf_numsections (abfd))
2610 return false;
2611
2612 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2613 sh_link or sh_info. Detect this here, by refusing to load a
2614 section that we are already in the process of loading. */
2615 if (elf_tdata (abfd)->being_created[shindex])
2616 {
2617 _bfd_error_handler
2618 (_("%pB: warning: loop in section dependencies detected"), abfd);
2619 return false;
2620 }
2621 elf_tdata (abfd)->being_created[shindex] = true;
2622
2623 hdr = elf_elfsections (abfd)[shindex];
2624 ehdr = elf_elfheader (abfd);
2625 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2626 hdr->sh_name);
2627 if (name == NULL)
2628 goto fail;
2629
2630 bed = get_elf_backend_data (abfd);
2631 switch (hdr->sh_type)
2632 {
2633 case SHT_NULL:
2634 /* Inactive section. Throw it away. */
2635 goto success;
2636
2637 case SHT_PROGBITS: /* Normal section with contents. */
2638 case SHT_NOBITS: /* .bss section. */
2639 case SHT_HASH: /* .hash section. */
2640 case SHT_NOTE: /* .note section. */
2641 case SHT_INIT_ARRAY: /* .init_array section. */
2642 case SHT_FINI_ARRAY: /* .fini_array section. */
2643 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2644 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2645 case SHT_GNU_HASH: /* .gnu.hash section. */
2646 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2647 goto success;
2648
2649 case SHT_DYNAMIC: /* Dynamic linking information. */
2650 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2651 goto fail;
2652
2653 if (hdr->sh_link > elf_numsections (abfd))
2654 {
2655 /* PR 10478: Accept Solaris binaries with a sh_link field
2656 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2657 switch (bfd_get_arch (abfd))
2658 {
2659 case bfd_arch_i386:
2660 case bfd_arch_sparc:
2661 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2662 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2663 break;
2664 /* Otherwise fall through. */
2665 default:
2666 goto fail;
2667 }
2668 }
2669 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2670 goto fail;
2671 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2672 {
2673 Elf_Internal_Shdr *dynsymhdr;
2674
2675 /* The shared libraries distributed with hpux11 have a bogus
2676 sh_link field for the ".dynamic" section. Find the
2677 string table for the ".dynsym" section instead. */
2678 if (elf_dynsymtab (abfd) != 0)
2679 {
2680 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2681 hdr->sh_link = dynsymhdr->sh_link;
2682 }
2683 else
2684 {
2685 unsigned int i, num_sec;
2686
2687 num_sec = elf_numsections (abfd);
2688 for (i = 1; i < num_sec; i++)
2689 {
2690 dynsymhdr = elf_elfsections (abfd)[i];
2691 if (dynsymhdr->sh_type == SHT_DYNSYM)
2692 {
2693 hdr->sh_link = dynsymhdr->sh_link;
2694 break;
2695 }
2696 }
2697 }
2698 }
2699 goto success;
2700
2701 case SHT_SYMTAB: /* A symbol table. */
2702 if (elf_onesymtab (abfd) == shindex)
2703 goto success;
2704
2705 if (hdr->sh_entsize != bed->s->sizeof_sym)
2706 goto fail;
2707
2708 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2709 {
2710 if (hdr->sh_size != 0)
2711 goto fail;
2712 /* Some assemblers erroneously set sh_info to one with a
2713 zero sh_size. ld sees this as a global symbol count
2714 of (unsigned) -1. Fix it here. */
2715 hdr->sh_info = 0;
2716 goto success;
2717 }
2718
2719 /* PR 18854: A binary might contain more than one symbol table.
2720 Unusual, but possible. Warn, but continue. */
2721 if (elf_onesymtab (abfd) != 0)
2722 {
2723 _bfd_error_handler
2724 /* xgettext:c-format */
2725 (_("%pB: warning: multiple symbol tables detected"
2726 " - ignoring the table in section %u"),
2727 abfd, shindex);
2728 goto success;
2729 }
2730 elf_onesymtab (abfd) = shindex;
2731 elf_symtab_hdr (abfd) = *hdr;
2732 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2733 abfd->flags |= HAS_SYMS;
2734
2735 /* Sometimes a shared object will map in the symbol table. If
2736 SHF_ALLOC is set, and this is a shared object, then we also
2737 treat this section as a BFD section. We can not base the
2738 decision purely on SHF_ALLOC, because that flag is sometimes
2739 set in a relocatable object file, which would confuse the
2740 linker. */
2741 if ((hdr->sh_flags & SHF_ALLOC) != 0
2742 && (abfd->flags & DYNAMIC) != 0
2743 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2744 shindex))
2745 goto fail;
2746
2747 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2748 can't read symbols without that section loaded as well. It
2749 is most likely specified by the next section header. */
2750 {
2751 elf_section_list * entry;
2752 unsigned int i, num_sec;
2753
2754 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2755 if (entry->hdr.sh_link == shindex)
2756 goto success;
2757
2758 num_sec = elf_numsections (abfd);
2759 for (i = shindex + 1; i < num_sec; i++)
2760 {
2761 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2762
2763 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2764 && hdr2->sh_link == shindex)
2765 break;
2766 }
2767
2768 if (i == num_sec)
2769 for (i = 1; i < shindex; i++)
2770 {
2771 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2772
2773 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2774 && hdr2->sh_link == shindex)
2775 break;
2776 }
2777
2778 if (i != shindex)
2779 ret = bfd_section_from_shdr (abfd, i);
2780 /* else FIXME: we have failed to find the symbol table.
2781 Should we issue an error? */
2782 goto success;
2783 }
2784
2785 case SHT_DYNSYM: /* A dynamic symbol table. */
2786 if (elf_dynsymtab (abfd) == shindex)
2787 goto success;
2788
2789 if (hdr->sh_entsize != bed->s->sizeof_sym)
2790 goto fail;
2791
2792 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2793 {
2794 if (hdr->sh_size != 0)
2795 goto fail;
2796
2797 /* Some linkers erroneously set sh_info to one with a
2798 zero sh_size. ld sees this as a global symbol count
2799 of (unsigned) -1. Fix it here. */
2800 hdr->sh_info = 0;
2801 goto success;
2802 }
2803
2804 /* PR 18854: A binary might contain more than one dynamic symbol table.
2805 Unusual, but possible. Warn, but continue. */
2806 if (elf_dynsymtab (abfd) != 0)
2807 {
2808 _bfd_error_handler
2809 /* xgettext:c-format */
2810 (_("%pB: warning: multiple dynamic symbol tables detected"
2811 " - ignoring the table in section %u"),
2812 abfd, shindex);
2813 goto success;
2814 }
2815 elf_dynsymtab (abfd) = shindex;
2816 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2817 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2818 abfd->flags |= HAS_SYMS;
2819
2820 /* Besides being a symbol table, we also treat this as a regular
2821 section, so that objcopy can handle it. */
2822 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2823 goto success;
2824
2825 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2826 {
2827 elf_section_list * entry;
2828
2829 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2830 if (entry->ndx == shindex)
2831 goto success;
2832
2833 entry = bfd_alloc (abfd, sizeof (*entry));
2834 if (entry == NULL)
2835 goto fail;
2836 entry->ndx = shindex;
2837 entry->hdr = * hdr;
2838 entry->next = elf_symtab_shndx_list (abfd);
2839 elf_symtab_shndx_list (abfd) = entry;
2840 elf_elfsections (abfd)[shindex] = & entry->hdr;
2841 goto success;
2842 }
2843
2844 case SHT_STRTAB: /* A string table. */
2845 if (hdr->bfd_section != NULL)
2846 goto success;
2847
2848 if (ehdr->e_shstrndx == shindex)
2849 {
2850 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2851 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2852 goto success;
2853 }
2854
2855 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2856 {
2857 symtab_strtab:
2858 elf_tdata (abfd)->strtab_hdr = *hdr;
2859 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2860 goto success;
2861 }
2862
2863 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2864 {
2865 dynsymtab_strtab:
2866 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2867 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2868 elf_elfsections (abfd)[shindex] = hdr;
2869 /* We also treat this as a regular section, so that objcopy
2870 can handle it. */
2871 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2872 shindex);
2873 goto success;
2874 }
2875
2876 /* If the string table isn't one of the above, then treat it as a
2877 regular section. We need to scan all the headers to be sure,
2878 just in case this strtab section appeared before the above. */
2879 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2880 {
2881 unsigned int i, num_sec;
2882
2883 num_sec = elf_numsections (abfd);
2884 for (i = 1; i < num_sec; i++)
2885 {
2886 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2887 if (hdr2->sh_link == shindex)
2888 {
2889 /* Prevent endless recursion on broken objects. */
2890 if (i == shindex)
2891 goto fail;
2892 if (! bfd_section_from_shdr (abfd, i))
2893 goto fail;
2894 if (elf_onesymtab (abfd) == i)
2895 goto symtab_strtab;
2896 if (elf_dynsymtab (abfd) == i)
2897 goto dynsymtab_strtab;
2898 }
2899 }
2900 }
2901 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2902 goto success;
2903
2904 case SHT_REL:
2905 case SHT_RELA:
2906 case SHT_RELR:
2907 /* *These* do a lot of work -- but build no sections! */
2908 {
2909 asection *target_sect;
2910 Elf_Internal_Shdr *hdr2, **p_hdr;
2911 unsigned int num_sec = elf_numsections (abfd);
2912 struct bfd_elf_section_data *esdt;
2913 bfd_size_type size;
2914
2915 if (hdr->sh_type == SHT_REL)
2916 size = bed->s->sizeof_rel;
2917 else if (hdr->sh_type == SHT_RELA)
2918 size = bed->s->sizeof_rela;
2919 else
2920 size = bed->s->arch_size / 8;
2921 if (hdr->sh_entsize != size)
2922 goto fail;
2923
2924 /* Check for a bogus link to avoid crashing. */
2925 if (hdr->sh_link >= num_sec)
2926 {
2927 _bfd_error_handler
2928 /* xgettext:c-format */
2929 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2930 abfd, hdr->sh_link, name, shindex);
2931 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2932 goto success;
2933 }
2934
2935 /* Get the symbol table. */
2936 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2937 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2938 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2939 goto fail;
2940
2941 /* If this is an alloc section in an executable or shared
2942 library, or the reloc section does not use the main symbol
2943 table we don't treat it as a reloc section. BFD can't
2944 adequately represent such a section, so at least for now,
2945 we don't try. We just present it as a normal section. We
2946 also can't use it as a reloc section if it points to the
2947 null section, an invalid section, another reloc section, or
2948 its sh_link points to the null section. */
2949 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2950 && (hdr->sh_flags & SHF_ALLOC) != 0)
2951 || (hdr->sh_flags & SHF_COMPRESSED) != 0
2952 || hdr->sh_type == SHT_RELR
2953 || hdr->sh_link == SHN_UNDEF
2954 || hdr->sh_link != elf_onesymtab (abfd)
2955 || hdr->sh_info == SHN_UNDEF
2956 || hdr->sh_info >= num_sec
2957 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2958 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2959 {
2960 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2961 goto success;
2962 }
2963
2964 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2965 goto fail;
2966
2967 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2968 if (target_sect == NULL)
2969 goto fail;
2970
2971 esdt = elf_section_data (target_sect);
2972 if (hdr->sh_type == SHT_RELA)
2973 p_hdr = &esdt->rela.hdr;
2974 else
2975 p_hdr = &esdt->rel.hdr;
2976
2977 /* PR 17512: file: 0b4f81b7.
2978 Also see PR 24456, for a file which deliberately has two reloc
2979 sections. */
2980 if (*p_hdr != NULL)
2981 {
2982 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2983 {
2984 _bfd_error_handler
2985 /* xgettext:c-format */
2986 (_("%pB: warning: secondary relocation section '%s' "
2987 "for section %pA found - ignoring"),
2988 abfd, name, target_sect);
2989 }
2990 else
2991 esdt->has_secondary_relocs = true;
2992 goto success;
2993 }
2994
2995 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2996 if (hdr2 == NULL)
2997 goto fail;
2998 *hdr2 = *hdr;
2999 *p_hdr = hdr2;
3000 elf_elfsections (abfd)[shindex] = hdr2;
3001 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
3002 * bed->s->int_rels_per_ext_rel);
3003 target_sect->flags |= SEC_RELOC;
3004 target_sect->relocation = NULL;
3005 target_sect->rel_filepos = hdr->sh_offset;
3006 /* In the section to which the relocations apply, mark whether
3007 its relocations are of the REL or RELA variety. */
3008 if (hdr->sh_size != 0)
3009 {
3010 if (hdr->sh_type == SHT_RELA)
3011 target_sect->use_rela_p = 1;
3012 }
3013 abfd->flags |= HAS_RELOC;
3014 goto success;
3015 }
3016
3017 case SHT_GNU_verdef:
3018 if (hdr->sh_info != 0)
3019 elf_dynverdef (abfd) = shindex;
3020 elf_tdata (abfd)->dynverdef_hdr = *hdr;
3021 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3022 goto success;
3023
3024 case SHT_GNU_versym:
3025 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
3026 goto fail;
3027
3028 elf_dynversym (abfd) = shindex;
3029 elf_tdata (abfd)->dynversym_hdr = *hdr;
3030 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3031 goto success;
3032
3033 case SHT_GNU_verneed:
3034 if (hdr->sh_info != 0)
3035 elf_dynverref (abfd) = shindex;
3036 elf_tdata (abfd)->dynverref_hdr = *hdr;
3037 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3038 goto success;
3039
3040 case SHT_SHLIB:
3041 goto success;
3042
3043 case SHT_GROUP:
3044 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
3045 goto fail;
3046
3047 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3048 goto fail;
3049
3050 goto success;
3051
3052 default:
3053 /* Possibly an attributes section. */
3054 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
3055 || hdr->sh_type == bed->obj_attrs_section_type)
3056 {
3057 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3058 goto fail;
3059 _bfd_elf_parse_attributes (abfd, hdr);
3060 goto success;
3061 }
3062
3063 /* Check for any processor-specific section types. */
3064 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
3065 goto success;
3066
3067 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
3068 {
3069 if ((hdr->sh_flags & SHF_ALLOC) != 0)
3070 /* FIXME: How to properly handle allocated section reserved
3071 for applications? */
3072 _bfd_error_handler
3073 /* xgettext:c-format */
3074 (_("%pB: unknown type [%#x] section `%s'"),
3075 abfd, hdr->sh_type, name);
3076 else
3077 {
3078 /* Allow sections reserved for applications. */
3079 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3080 goto success;
3081 }
3082 }
3083 else if (hdr->sh_type >= SHT_LOPROC
3084 && hdr->sh_type <= SHT_HIPROC)
3085 /* FIXME: We should handle this section. */
3086 _bfd_error_handler
3087 /* xgettext:c-format */
3088 (_("%pB: unknown type [%#x] section `%s'"),
3089 abfd, hdr->sh_type, name);
3090 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
3091 {
3092 /* Unrecognised OS-specific sections. */
3093 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3094 /* SHF_OS_NONCONFORMING indicates that special knowledge is
3095 required to correctly process the section and the file should
3096 be rejected with an error message. */
3097 _bfd_error_handler
3098 /* xgettext:c-format */
3099 (_("%pB: unknown type [%#x] section `%s'"),
3100 abfd, hdr->sh_type, name);
3101 else
3102 {
3103 /* Otherwise it should be processed. */
3104 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3105 goto success;
3106 }
3107 }
3108 else
3109 /* FIXME: We should handle this section. */
3110 _bfd_error_handler
3111 /* xgettext:c-format */
3112 (_("%pB: unknown type [%#x] section `%s'"),
3113 abfd, hdr->sh_type, name);
3114
3115 goto fail;
3116 }
3117
3118 fail:
3119 ret = false;
3120 success:
3121 elf_tdata (abfd)->being_created[shindex] = false;
3122 return ret;
3123 }
3124
3125 /* Return the local symbol specified by ABFD, R_SYMNDX. */
3126
3127 Elf_Internal_Sym *
3128 bfd_sym_from_r_symndx (struct sym_cache *cache,
3129 bfd *abfd,
3130 unsigned long r_symndx)
3131 {
3132 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3133
3134 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3135 {
3136 Elf_Internal_Shdr *symtab_hdr;
3137 unsigned char esym[sizeof (Elf64_External_Sym)];
3138 Elf_External_Sym_Shndx eshndx;
3139
3140 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3141 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3142 &cache->sym[ent], esym, &eshndx) == NULL)
3143 return NULL;
3144
3145 if (cache->abfd != abfd)
3146 {
3147 memset (cache->indx, -1, sizeof (cache->indx));
3148 cache->abfd = abfd;
3149 }
3150 cache->indx[ent] = r_symndx;
3151 }
3152
3153 return &cache->sym[ent];
3154 }
3155
3156 /* Given an ELF section number, retrieve the corresponding BFD
3157 section. */
3158
3159 asection *
3160 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3161 {
3162 if (sec_index >= elf_numsections (abfd))
3163 return NULL;
3164 return elf_elfsections (abfd)[sec_index]->bfd_section;
3165 }
3166
3167 static const struct bfd_elf_special_section special_sections_b[] =
3168 {
3169 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3170 { NULL, 0, 0, 0, 0 }
3171 };
3172
3173 static const struct bfd_elf_special_section special_sections_c[] =
3174 {
3175 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3176 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
3177 { NULL, 0, 0, 0, 0 }
3178 };
3179
3180 static const struct bfd_elf_special_section special_sections_d[] =
3181 {
3182 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3183 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3184 /* There are more DWARF sections than these, but they needn't be added here
3185 unless you have to cope with broken compilers that don't emit section
3186 attributes or you want to help the user writing assembler. */
3187 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
3188 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
3189 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
3190 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
3191 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3192 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
3193 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
3194 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
3195 { NULL, 0, 0, 0, 0 }
3196 };
3197
3198 static const struct bfd_elf_special_section special_sections_f[] =
3199 {
3200 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3201 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3202 { NULL, 0 , 0, 0, 0 }
3203 };
3204
3205 static const struct bfd_elf_special_section special_sections_g[] =
3206 {
3207 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3208 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3209 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3210 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
3211 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3212 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
3213 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
3214 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
3215 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
3216 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
3217 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
3218 { NULL, 0, 0, 0, 0 }
3219 };
3220
3221 static const struct bfd_elf_special_section special_sections_h[] =
3222 {
3223 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
3224 { NULL, 0, 0, 0, 0 }
3225 };
3226
3227 static const struct bfd_elf_special_section special_sections_i[] =
3228 {
3229 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3230 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3231 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
3232 { NULL, 0, 0, 0, 0 }
3233 };
3234
3235 static const struct bfd_elf_special_section special_sections_l[] =
3236 {
3237 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3238 { NULL, 0, 0, 0, 0 }
3239 };
3240
3241 static const struct bfd_elf_special_section special_sections_n[] =
3242 {
3243 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3244 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
3245 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
3246 { NULL, 0, 0, 0, 0 }
3247 };
3248
3249 static const struct bfd_elf_special_section special_sections_p[] =
3250 {
3251 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3252 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3253 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3254 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3255 { NULL, 0, 0, 0, 0 }
3256 };
3257
3258 static const struct bfd_elf_special_section special_sections_r[] =
3259 {
3260 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3261 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3262 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3263 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
3264 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
3265 { NULL, 0, 0, 0, 0 }
3266 };
3267
3268 static const struct bfd_elf_special_section special_sections_s[] =
3269 {
3270 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3271 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
3272 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
3273 /* See struct bfd_elf_special_section declaration for the semantics of
3274 this special case where .prefix_length != strlen (.prefix). */
3275 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
3276 { NULL, 0, 0, 0, 0 }
3277 };
3278
3279 static const struct bfd_elf_special_section special_sections_t[] =
3280 {
3281 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3282 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3283 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3284 { NULL, 0, 0, 0, 0 }
3285 };
3286
3287 static const struct bfd_elf_special_section special_sections_z[] =
3288 {
3289 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
3290 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
3291 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
3292 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3293 { NULL, 0, 0, 0, 0 }
3294 };
3295
3296 static const struct bfd_elf_special_section * const special_sections[] =
3297 {
3298 special_sections_b, /* 'b' */
3299 special_sections_c, /* 'c' */
3300 special_sections_d, /* 'd' */
3301 NULL, /* 'e' */
3302 special_sections_f, /* 'f' */
3303 special_sections_g, /* 'g' */
3304 special_sections_h, /* 'h' */
3305 special_sections_i, /* 'i' */
3306 NULL, /* 'j' */
3307 NULL, /* 'k' */
3308 special_sections_l, /* 'l' */
3309 NULL, /* 'm' */
3310 special_sections_n, /* 'n' */
3311 NULL, /* 'o' */
3312 special_sections_p, /* 'p' */
3313 NULL, /* 'q' */
3314 special_sections_r, /* 'r' */
3315 special_sections_s, /* 's' */
3316 special_sections_t, /* 't' */
3317 NULL, /* 'u' */
3318 NULL, /* 'v' */
3319 NULL, /* 'w' */
3320 NULL, /* 'x' */
3321 NULL, /* 'y' */
3322 special_sections_z /* 'z' */
3323 };
3324
3325 const struct bfd_elf_special_section *
3326 _bfd_elf_get_special_section (const char *name,
3327 const struct bfd_elf_special_section *spec,
3328 unsigned int rela)
3329 {
3330 int i;
3331 int len;
3332
3333 len = strlen (name);
3334
3335 for (i = 0; spec[i].prefix != NULL; i++)
3336 {
3337 int suffix_len;
3338 int prefix_len = spec[i].prefix_length;
3339
3340 if (len < prefix_len)
3341 continue;
3342 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3343 continue;
3344
3345 suffix_len = spec[i].suffix_length;
3346 if (suffix_len <= 0)
3347 {
3348 if (name[prefix_len] != 0)
3349 {
3350 if (suffix_len == 0)
3351 continue;
3352 if (name[prefix_len] != '.'
3353 && (suffix_len == -2
3354 || (rela && spec[i].type == SHT_REL)))
3355 continue;
3356 }
3357 }
3358 else
3359 {
3360 if (len < prefix_len + suffix_len)
3361 continue;
3362 if (memcmp (name + len - suffix_len,
3363 spec[i].prefix + prefix_len,
3364 suffix_len) != 0)
3365 continue;
3366 }
3367 return &spec[i];
3368 }
3369
3370 return NULL;
3371 }
3372
3373 const struct bfd_elf_special_section *
3374 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3375 {
3376 int i;
3377 const struct bfd_elf_special_section *spec;
3378 const struct elf_backend_data *bed;
3379
3380 /* See if this is one of the special sections. */
3381 if (sec->name == NULL)
3382 return NULL;
3383
3384 bed = get_elf_backend_data (abfd);
3385 spec = bed->special_sections;
3386 if (spec)
3387 {
3388 spec = _bfd_elf_get_special_section (sec->name,
3389 bed->special_sections,
3390 sec->use_rela_p);
3391 if (spec != NULL)
3392 return spec;
3393 }
3394
3395 if (sec->name[0] != '.')
3396 return NULL;
3397
3398 i = sec->name[1] - 'b';
3399 if (i < 0 || i > 'z' - 'b')
3400 return NULL;
3401
3402 spec = special_sections[i];
3403
3404 if (spec == NULL)
3405 return NULL;
3406
3407 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3408 }
3409
3410 bool
3411 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3412 {
3413 struct bfd_elf_section_data *sdata;
3414 const struct elf_backend_data *bed;
3415 const struct bfd_elf_special_section *ssect;
3416
3417 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3418 if (sdata == NULL)
3419 {
3420 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3421 sizeof (*sdata));
3422 if (sdata == NULL)
3423 return false;
3424 sec->used_by_bfd = sdata;
3425 }
3426
3427 /* Indicate whether or not this section should use RELA relocations. */
3428 bed = get_elf_backend_data (abfd);
3429 sec->use_rela_p = bed->default_use_rela_p;
3430
3431 /* Set up ELF section type and flags for newly created sections, if
3432 there is an ABI mandated section. */
3433 ssect = (*bed->get_sec_type_attr) (abfd, sec);
3434 if (ssect != NULL)
3435 {
3436 elf_section_type (sec) = ssect->type;
3437 elf_section_flags (sec) = ssect->attr;
3438 }
3439
3440 return _bfd_generic_new_section_hook (abfd, sec);
3441 }
3442
3443 /* Create a new bfd section from an ELF program header.
3444
3445 Since program segments have no names, we generate a synthetic name
3446 of the form segment<NUM>, where NUM is generally the index in the
3447 program header table. For segments that are split (see below) we
3448 generate the names segment<NUM>a and segment<NUM>b.
3449
3450 Note that some program segments may have a file size that is different than
3451 (less than) the memory size. All this means is that at execution the
3452 system must allocate the amount of memory specified by the memory size,
3453 but only initialize it with the first "file size" bytes read from the
3454 file. This would occur for example, with program segments consisting
3455 of combined data+bss.
3456
3457 To handle the above situation, this routine generates TWO bfd sections
3458 for the single program segment. The first has the length specified by
3459 the file size of the segment, and the second has the length specified
3460 by the difference between the two sizes. In effect, the segment is split
3461 into its initialized and uninitialized parts. */
3462
3463 bool
3464 _bfd_elf_make_section_from_phdr (bfd *abfd,
3465 Elf_Internal_Phdr *hdr,
3466 int hdr_index,
3467 const char *type_name)
3468 {
3469 asection *newsect;
3470 char *name;
3471 char namebuf[64];
3472 size_t len;
3473 int split;
3474 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3475
3476 split = ((hdr->p_memsz > 0)
3477 && (hdr->p_filesz > 0)
3478 && (hdr->p_memsz > hdr->p_filesz));
3479
3480 if (hdr->p_filesz > 0)
3481 {
3482 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3483 len = strlen (namebuf) + 1;
3484 name = (char *) bfd_alloc (abfd, len);
3485 if (!name)
3486 return false;
3487 memcpy (name, namebuf, len);
3488 newsect = bfd_make_section (abfd, name);
3489 if (newsect == NULL)
3490 return false;
3491 newsect->vma = hdr->p_vaddr / opb;
3492 newsect->lma = hdr->p_paddr / opb;
3493 newsect->size = hdr->p_filesz;
3494 newsect->filepos = hdr->p_offset;
3495 newsect->flags |= SEC_HAS_CONTENTS;
3496 newsect->alignment_power = bfd_log2 (hdr->p_align);
3497 if (hdr->p_type == PT_LOAD)
3498 {
3499 newsect->flags |= SEC_ALLOC;
3500 newsect->flags |= SEC_LOAD;
3501 if (hdr->p_flags & PF_X)
3502 {
3503 /* FIXME: all we known is that it has execute PERMISSION,
3504 may be data. */
3505 newsect->flags |= SEC_CODE;
3506 }
3507 }
3508 if (!(hdr->p_flags & PF_W))
3509 {
3510 newsect->flags |= SEC_READONLY;
3511 }
3512 }
3513
3514 if (hdr->p_memsz > hdr->p_filesz)
3515 {
3516 bfd_vma align;
3517
3518 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3519 len = strlen (namebuf) + 1;
3520 name = (char *) bfd_alloc (abfd, len);
3521 if (!name)
3522 return false;
3523 memcpy (name, namebuf, len);
3524 newsect = bfd_make_section (abfd, name);
3525 if (newsect == NULL)
3526 return false;
3527 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3528 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3529 newsect->size = hdr->p_memsz - hdr->p_filesz;
3530 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3531 align = newsect->vma & -newsect->vma;
3532 if (align == 0 || align > hdr->p_align)
3533 align = hdr->p_align;
3534 newsect->alignment_power = bfd_log2 (align);
3535 if (hdr->p_type == PT_LOAD)
3536 {
3537 newsect->flags |= SEC_ALLOC;
3538 if (hdr->p_flags & PF_X)
3539 newsect->flags |= SEC_CODE;
3540 }
3541 if (!(hdr->p_flags & PF_W))
3542 newsect->flags |= SEC_READONLY;
3543 }
3544
3545 return true;
3546 }
3547
3548 static bool
3549 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3550 {
3551 /* The return value is ignored. Build-ids are considered optional. */
3552 if (templ->xvec->flavour == bfd_target_elf_flavour)
3553 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3554 (templ, offset);
3555 return false;
3556 }
3557
3558 bool
3559 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3560 {
3561 const struct elf_backend_data *bed;
3562
3563 switch (hdr->p_type)
3564 {
3565 case PT_NULL:
3566 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3567
3568 case PT_LOAD:
3569 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3570 return false;
3571 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3572 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3573 return true;
3574
3575 case PT_DYNAMIC:
3576 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3577
3578 case PT_INTERP:
3579 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3580
3581 case PT_NOTE:
3582 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3583 return false;
3584 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3585 hdr->p_align))
3586 return false;
3587 return true;
3588
3589 case PT_SHLIB:
3590 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3591
3592 case PT_PHDR:
3593 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3594
3595 case PT_GNU_EH_FRAME:
3596 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3597 "eh_frame_hdr");
3598
3599 case PT_GNU_STACK:
3600 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3601
3602 case PT_GNU_RELRO:
3603 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3604
3605 case PT_GNU_SFRAME:
3606 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3607 "sframe");
3608
3609 default:
3610 /* Check for any processor-specific program segment types. */
3611 bed = get_elf_backend_data (abfd);
3612 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3613 }
3614 }
3615
3616 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3617 REL or RELA. */
3618
3619 Elf_Internal_Shdr *
3620 _bfd_elf_single_rel_hdr (asection *sec)
3621 {
3622 if (elf_section_data (sec)->rel.hdr)
3623 {
3624 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3625 return elf_section_data (sec)->rel.hdr;
3626 }
3627 else
3628 return elf_section_data (sec)->rela.hdr;
3629 }
3630
3631 static bool
3632 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3633 Elf_Internal_Shdr *rel_hdr,
3634 const char *sec_name,
3635 bool use_rela_p)
3636 {
3637 char *name = (char *) bfd_alloc (abfd,
3638 sizeof ".rela" + strlen (sec_name));
3639 if (name == NULL)
3640 return false;
3641
3642 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3643 rel_hdr->sh_name =
3644 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3645 false);
3646 if (rel_hdr->sh_name == (unsigned int) -1)
3647 return false;
3648
3649 return true;
3650 }
3651
3652 /* Allocate and initialize a section-header for a new reloc section,
3653 containing relocations against ASECT. It is stored in RELDATA. If
3654 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3655 relocations. */
3656
3657 static bool
3658 _bfd_elf_init_reloc_shdr (bfd *abfd,
3659 struct bfd_elf_section_reloc_data *reldata,
3660 const char *sec_name,
3661 bool use_rela_p,
3662 bool delay_st_name_p)
3663 {
3664 Elf_Internal_Shdr *rel_hdr;
3665 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3666
3667 BFD_ASSERT (reldata->hdr == NULL);
3668 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3669 if (rel_hdr == NULL)
3670 return false;
3671 reldata->hdr = rel_hdr;
3672
3673 if (delay_st_name_p)
3674 rel_hdr->sh_name = (unsigned int) -1;
3675 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3676 use_rela_p))
3677 return false;
3678 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3679 rel_hdr->sh_entsize = (use_rela_p
3680 ? bed->s->sizeof_rela
3681 : bed->s->sizeof_rel);
3682 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3683 rel_hdr->sh_flags = 0;
3684 rel_hdr->sh_addr = 0;
3685 rel_hdr->sh_size = 0;
3686 rel_hdr->sh_offset = 0;
3687
3688 return true;
3689 }
3690
3691 /* Return the default section type based on the passed in section flags. */
3692
3693 int
3694 bfd_elf_get_default_section_type (flagword flags)
3695 {
3696 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3697 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3698 return SHT_NOBITS;
3699 return SHT_PROGBITS;
3700 }
3701
3702 struct fake_section_arg
3703 {
3704 struct bfd_link_info *link_info;
3705 bool failed;
3706 };
3707
3708 /* Set up an ELF internal section header for a section. */
3709
3710 static void
3711 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3712 {
3713 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3714 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3715 struct bfd_elf_section_data *esd = elf_section_data (asect);
3716 Elf_Internal_Shdr *this_hdr;
3717 unsigned int sh_type;
3718 const char *name = asect->name;
3719 bool delay_st_name_p = false;
3720 bfd_vma mask;
3721
3722 if (arg->failed)
3723 {
3724 /* We already failed; just get out of the bfd_map_over_sections
3725 loop. */
3726 return;
3727 }
3728
3729 this_hdr = &esd->this_hdr;
3730
3731 /* ld: compress DWARF debug sections with names: .debug_*. */
3732 if (arg->link_info
3733 && (abfd->flags & BFD_COMPRESS) != 0
3734 && (asect->flags & SEC_DEBUGGING) != 0
3735 && name[1] == 'd'
3736 && name[6] == '_')
3737 {
3738 /* If this section will be compressed, delay adding section
3739 name to section name section after it is compressed in
3740 _bfd_elf_assign_file_positions_for_non_load. */
3741 delay_st_name_p = true;
3742 }
3743
3744 if (delay_st_name_p)
3745 this_hdr->sh_name = (unsigned int) -1;
3746 else
3747 {
3748 this_hdr->sh_name
3749 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3750 name, false);
3751 if (this_hdr->sh_name == (unsigned int) -1)
3752 {
3753 arg->failed = true;
3754 return;
3755 }
3756 }
3757
3758 /* Don't clear sh_flags. Assembler may set additional bits. */
3759
3760 if ((asect->flags & SEC_ALLOC) != 0
3761 || asect->user_set_vma)
3762 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3763 else
3764 this_hdr->sh_addr = 0;
3765
3766 this_hdr->sh_offset = 0;
3767 this_hdr->sh_size = asect->size;
3768 this_hdr->sh_link = 0;
3769 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3770 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3771 {
3772 _bfd_error_handler
3773 /* xgettext:c-format */
3774 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3775 abfd, asect->alignment_power, asect);
3776 arg->failed = true;
3777 return;
3778 }
3779 /* Set sh_addralign to the highest power of two given by alignment
3780 consistent with the section VMA. Linker scripts can force VMA. */
3781 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3782 this_hdr->sh_addralign = mask & -mask;
3783 /* The sh_entsize and sh_info fields may have been set already by
3784 copy_private_section_data. */
3785
3786 this_hdr->bfd_section = asect;
3787 this_hdr->contents = NULL;
3788
3789 /* If the section type is unspecified, we set it based on
3790 asect->flags. */
3791 if (asect->type != 0)
3792 sh_type = asect->type;
3793 else if ((asect->flags & SEC_GROUP) != 0)
3794 sh_type = SHT_GROUP;
3795 else
3796 sh_type = bfd_elf_get_default_section_type (asect->flags);
3797
3798 if (this_hdr->sh_type == SHT_NULL)
3799 this_hdr->sh_type = sh_type;
3800 else if (this_hdr->sh_type == SHT_NOBITS
3801 && sh_type == SHT_PROGBITS
3802 && (asect->flags & SEC_ALLOC) != 0)
3803 {
3804 /* Warn if we are changing a NOBITS section to PROGBITS, but
3805 allow the link to proceed. This can happen when users link
3806 non-bss input sections to bss output sections, or emit data
3807 to a bss output section via a linker script. */
3808 _bfd_error_handler
3809 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3810 this_hdr->sh_type = sh_type;
3811 }
3812
3813 switch (this_hdr->sh_type)
3814 {
3815 default:
3816 break;
3817
3818 case SHT_STRTAB:
3819 case SHT_NOTE:
3820 case SHT_NOBITS:
3821 case SHT_PROGBITS:
3822 break;
3823
3824 case SHT_INIT_ARRAY:
3825 case SHT_FINI_ARRAY:
3826 case SHT_PREINIT_ARRAY:
3827 this_hdr->sh_entsize = bed->s->arch_size / 8;
3828 break;
3829
3830 case SHT_HASH:
3831 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3832 break;
3833
3834 case SHT_DYNSYM:
3835 this_hdr->sh_entsize = bed->s->sizeof_sym;
3836 break;
3837
3838 case SHT_DYNAMIC:
3839 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3840 break;
3841
3842 case SHT_RELA:
3843 if (get_elf_backend_data (abfd)->may_use_rela_p)
3844 this_hdr->sh_entsize = bed->s->sizeof_rela;
3845 break;
3846
3847 case SHT_REL:
3848 if (get_elf_backend_data (abfd)->may_use_rel_p)
3849 this_hdr->sh_entsize = bed->s->sizeof_rel;
3850 break;
3851
3852 case SHT_GNU_versym:
3853 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3854 break;
3855
3856 case SHT_GNU_verdef:
3857 this_hdr->sh_entsize = 0;
3858 /* objcopy or strip will copy over sh_info, but may not set
3859 cverdefs. The linker will set cverdefs, but sh_info will be
3860 zero. */
3861 if (this_hdr->sh_info == 0)
3862 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3863 else
3864 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3865 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3866 break;
3867
3868 case SHT_GNU_verneed:
3869 this_hdr->sh_entsize = 0;
3870 /* objcopy or strip will copy over sh_info, but may not set
3871 cverrefs. The linker will set cverrefs, but sh_info will be
3872 zero. */
3873 if (this_hdr->sh_info == 0)
3874 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3875 else
3876 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3877 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3878 break;
3879
3880 case SHT_GROUP:
3881 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3882 break;
3883
3884 case SHT_GNU_HASH:
3885 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3886 break;
3887 }
3888
3889 if ((asect->flags & SEC_ALLOC) != 0)
3890 this_hdr->sh_flags |= SHF_ALLOC;
3891 if ((asect->flags & SEC_READONLY) == 0)
3892 this_hdr->sh_flags |= SHF_WRITE;
3893 if ((asect->flags & SEC_CODE) != 0)
3894 this_hdr->sh_flags |= SHF_EXECINSTR;
3895 if ((asect->flags & SEC_MERGE) != 0)
3896 {
3897 this_hdr->sh_flags |= SHF_MERGE;
3898 this_hdr->sh_entsize = asect->entsize;
3899 }
3900 if ((asect->flags & SEC_STRINGS) != 0)
3901 this_hdr->sh_flags |= SHF_STRINGS;
3902 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3903 this_hdr->sh_flags |= SHF_GROUP;
3904 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3905 {
3906 this_hdr->sh_flags |= SHF_TLS;
3907 if (asect->size == 0
3908 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3909 {
3910 struct bfd_link_order *o = asect->map_tail.link_order;
3911
3912 this_hdr->sh_size = 0;
3913 if (o != NULL)
3914 {
3915 this_hdr->sh_size = o->offset + o->size;
3916 if (this_hdr->sh_size != 0)
3917 this_hdr->sh_type = SHT_NOBITS;
3918 }
3919 }
3920 }
3921 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3922 this_hdr->sh_flags |= SHF_EXCLUDE;
3923
3924 /* If the section has relocs, set up a section header for the
3925 SHT_REL[A] section. If two relocation sections are required for
3926 this section, it is up to the processor-specific back-end to
3927 create the other. */
3928 if ((asect->flags & SEC_RELOC) != 0)
3929 {
3930 /* When doing a relocatable link, create both REL and RELA sections if
3931 needed. */
3932 if (arg->link_info
3933 /* Do the normal setup if we wouldn't create any sections here. */
3934 && esd->rel.count + esd->rela.count > 0
3935 && (bfd_link_relocatable (arg->link_info)
3936 || arg->link_info->emitrelocations))
3937 {
3938 if (esd->rel.count && esd->rel.hdr == NULL
3939 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3940 false, delay_st_name_p))
3941 {
3942 arg->failed = true;
3943 return;
3944 }
3945 if (esd->rela.count && esd->rela.hdr == NULL
3946 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3947 true, delay_st_name_p))
3948 {
3949 arg->failed = true;
3950 return;
3951 }
3952 }
3953 else if (!_bfd_elf_init_reloc_shdr (abfd,
3954 (asect->use_rela_p
3955 ? &esd->rela : &esd->rel),
3956 name,
3957 asect->use_rela_p,
3958 delay_st_name_p))
3959 {
3960 arg->failed = true;
3961 return;
3962 }
3963 }
3964
3965 /* Check for processor-specific section types. */
3966 sh_type = this_hdr->sh_type;
3967 if (bed->elf_backend_fake_sections
3968 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3969 {
3970 arg->failed = true;
3971 return;
3972 }
3973
3974 if (sh_type == SHT_NOBITS && asect->size != 0)
3975 {
3976 /* Don't change the header type from NOBITS if we are being
3977 called for objcopy --only-keep-debug. */
3978 this_hdr->sh_type = sh_type;
3979 }
3980 }
3981
3982 /* Fill in the contents of a SHT_GROUP section. Called from
3983 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3984 when ELF targets use the generic linker, ld. Called for ld -r
3985 from bfd_elf_final_link. */
3986
3987 void
3988 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3989 {
3990 bool *failedptr = (bool *) failedptrarg;
3991 asection *elt, *first;
3992 unsigned char *loc;
3993 bool gas;
3994
3995 /* Ignore linker created group section. See elfNN_ia64_object_p in
3996 elfxx-ia64.c. */
3997 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3998 || sec->size == 0
3999 || *failedptr)
4000 return;
4001
4002 if (elf_section_data (sec)->this_hdr.sh_info == 0)
4003 {
4004 unsigned long symindx = 0;
4005
4006 /* elf_group_id will have been set up by objcopy and the
4007 generic linker. */
4008 if (elf_group_id (sec) != NULL)
4009 symindx = elf_group_id (sec)->udata.i;
4010
4011 if (symindx == 0)
4012 {
4013 /* If called from the assembler, swap_out_syms will have set up
4014 elf_section_syms.
4015 PR 25699: A corrupt input file could contain bogus group info. */
4016 if (sec->index >= elf_num_section_syms (abfd)
4017 || elf_section_syms (abfd)[sec->index] == NULL)
4018 {
4019 *failedptr = true;
4020 return;
4021 }
4022 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
4023 }
4024 elf_section_data (sec)->this_hdr.sh_info = symindx;
4025 }
4026 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
4027 {
4028 /* The ELF backend linker sets sh_info to -2 when the group
4029 signature symbol is global, and thus the index can't be
4030 set until all local symbols are output. */
4031 asection *igroup;
4032 struct bfd_elf_section_data *sec_data;
4033 unsigned long symndx;
4034 unsigned long extsymoff;
4035 struct elf_link_hash_entry *h;
4036
4037 /* The point of this little dance to the first SHF_GROUP section
4038 then back to the SHT_GROUP section is that this gets us to
4039 the SHT_GROUP in the input object. */
4040 igroup = elf_sec_group (elf_next_in_group (sec));
4041 sec_data = elf_section_data (igroup);
4042 symndx = sec_data->this_hdr.sh_info;
4043 extsymoff = 0;
4044 if (!elf_bad_symtab (igroup->owner))
4045 {
4046 Elf_Internal_Shdr *symtab_hdr;
4047
4048 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
4049 extsymoff = symtab_hdr->sh_info;
4050 }
4051 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
4052 while (h->root.type == bfd_link_hash_indirect
4053 || h->root.type == bfd_link_hash_warning)
4054 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4055
4056 elf_section_data (sec)->this_hdr.sh_info = h->indx;
4057 }
4058
4059 /* The contents won't be allocated for "ld -r" or objcopy. */
4060 gas = true;
4061 if (sec->contents == NULL)
4062 {
4063 gas = false;
4064 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
4065
4066 /* Arrange for the section to be written out. */
4067 elf_section_data (sec)->this_hdr.contents = sec->contents;
4068 if (sec->contents == NULL)
4069 {
4070 *failedptr = true;
4071 return;
4072 }
4073 }
4074
4075 loc = sec->contents + sec->size;
4076
4077 /* Get the pointer to the first section in the group that gas
4078 squirreled away here. objcopy arranges for this to be set to the
4079 start of the input section group. */
4080 first = elt = elf_next_in_group (sec);
4081
4082 /* First element is a flag word. Rest of section is elf section
4083 indices for all the sections of the group. Write them backwards
4084 just to keep the group in the same order as given in .section
4085 directives, not that it matters. */
4086 while (elt != NULL)
4087 {
4088 asection *s;
4089
4090 s = elt;
4091 if (!gas)
4092 s = s->output_section;
4093 if (s != NULL
4094 && !bfd_is_abs_section (s))
4095 {
4096 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4097 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4098
4099 if (elf_sec->rel.hdr != NULL
4100 && (gas
4101 || (input_elf_sec->rel.hdr != NULL
4102 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4103 {
4104 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4105 loc -= 4;
4106 if (loc == sec->contents)
4107 break;
4108 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4109 }
4110 if (elf_sec->rela.hdr != NULL
4111 && (gas
4112 || (input_elf_sec->rela.hdr != NULL
4113 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4114 {
4115 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4116 loc -= 4;
4117 if (loc == sec->contents)
4118 break;
4119 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4120 }
4121 loc -= 4;
4122 if (loc == sec->contents)
4123 break;
4124 H_PUT_32 (abfd, elf_sec->this_idx, loc);
4125 }
4126 elt = elf_next_in_group (elt);
4127 if (elt == first)
4128 break;
4129 }
4130
4131 /* We should always get here with loc == sec->contents + 4, but it is
4132 possible to craft bogus SHT_GROUP sections that will cause segfaults
4133 in objcopy without checking loc here and in the loop above. */
4134 if (loc == sec->contents)
4135 BFD_ASSERT (0);
4136 else
4137 {
4138 loc -= 4;
4139 if (loc != sec->contents)
4140 {
4141 BFD_ASSERT (0);
4142 memset (sec->contents + 4, 0, loc - sec->contents);
4143 loc = sec->contents;
4144 }
4145 }
4146
4147 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4148 }
4149
4150 /* Given NAME, the name of a relocation section stripped of its
4151 .rel/.rela prefix, return the section in ABFD to which the
4152 relocations apply. */
4153
4154 asection *
4155 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4156 {
4157 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4158 section likely apply to .got.plt or .got section. */
4159 if (get_elf_backend_data (abfd)->want_got_plt
4160 && strcmp (name, ".plt") == 0)
4161 {
4162 asection *sec;
4163
4164 name = ".got.plt";
4165 sec = bfd_get_section_by_name (abfd, name);
4166 if (sec != NULL)
4167 return sec;
4168 name = ".got";
4169 }
4170
4171 return bfd_get_section_by_name (abfd, name);
4172 }
4173
4174 /* Return the section to which RELOC_SEC applies. */
4175
4176 static asection *
4177 elf_get_reloc_section (asection *reloc_sec)
4178 {
4179 const char *name;
4180 unsigned int type;
4181 bfd *abfd;
4182 const struct elf_backend_data *bed;
4183
4184 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4185 if (type != SHT_REL && type != SHT_RELA)
4186 return NULL;
4187
4188 /* We look up the section the relocs apply to by name. */
4189 name = reloc_sec->name;
4190 if (!startswith (name, ".rel"))
4191 return NULL;
4192 name += 4;
4193 if (type == SHT_RELA && *name++ != 'a')
4194 return NULL;
4195
4196 abfd = reloc_sec->owner;
4197 bed = get_elf_backend_data (abfd);
4198 return bed->get_reloc_section (abfd, name);
4199 }
4200
4201 /* Assign all ELF section numbers. The dummy first section is handled here
4202 too. The link/info pointers for the standard section types are filled
4203 in here too, while we're at it. LINK_INFO will be 0 when arriving
4204 here for gas, objcopy, and when using the generic ELF linker. */
4205
4206 static bool
4207 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4208 {
4209 struct elf_obj_tdata *t = elf_tdata (abfd);
4210 asection *sec;
4211 unsigned int section_number;
4212 Elf_Internal_Shdr **i_shdrp;
4213 struct bfd_elf_section_data *d;
4214 bool need_symtab;
4215 size_t amt;
4216
4217 section_number = 1;
4218
4219 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4220
4221 /* SHT_GROUP sections are in relocatable files only. */
4222 if (link_info == NULL || !link_info->resolve_section_groups)
4223 {
4224 size_t reloc_count = 0;
4225
4226 /* Put SHT_GROUP sections first. */
4227 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4228 {
4229 d = elf_section_data (sec);
4230
4231 if (d->this_hdr.sh_type == SHT_GROUP)
4232 {
4233 if (sec->flags & SEC_LINKER_CREATED)
4234 {
4235 /* Remove the linker created SHT_GROUP sections. */
4236 bfd_section_list_remove (abfd, sec);
4237 abfd->section_count--;
4238 }
4239 else
4240 d->this_idx = section_number++;
4241 }
4242
4243 /* Count relocations. */
4244 reloc_count += sec->reloc_count;
4245 }
4246
4247 /* Set/clear HAS_RELOC depending on whether there are relocations. */
4248 if (reloc_count == 0)
4249 abfd->flags &= ~HAS_RELOC;
4250 else
4251 abfd->flags |= HAS_RELOC;
4252 }
4253
4254 for (sec = abfd->sections; sec; sec = sec->next)
4255 {
4256 d = elf_section_data (sec);
4257
4258 if (d->this_hdr.sh_type != SHT_GROUP)
4259 d->this_idx = section_number++;
4260 if (d->this_hdr.sh_name != (unsigned int) -1)
4261 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4262 if (d->rel.hdr)
4263 {
4264 d->rel.idx = section_number++;
4265 if (d->rel.hdr->sh_name != (unsigned int) -1)
4266 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4267 }
4268 else
4269 d->rel.idx = 0;
4270
4271 if (d->rela.hdr)
4272 {
4273 d->rela.idx = section_number++;
4274 if (d->rela.hdr->sh_name != (unsigned int) -1)
4275 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4276 }
4277 else
4278 d->rela.idx = 0;
4279 }
4280
4281 need_symtab = (bfd_get_symcount (abfd) > 0
4282 || (link_info == NULL
4283 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4284 == HAS_RELOC)));
4285 if (need_symtab)
4286 {
4287 elf_onesymtab (abfd) = section_number++;
4288 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4289 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4290 {
4291 elf_section_list *entry;
4292
4293 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4294
4295 entry = bfd_zalloc (abfd, sizeof (*entry));
4296 entry->ndx = section_number++;
4297 elf_symtab_shndx_list (abfd) = entry;
4298 entry->hdr.sh_name
4299 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4300 ".symtab_shndx", false);
4301 if (entry->hdr.sh_name == (unsigned int) -1)
4302 return false;
4303 }
4304 elf_strtab_sec (abfd) = section_number++;
4305 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4306 }
4307
4308 elf_shstrtab_sec (abfd) = section_number++;
4309 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4310 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4311
4312 if (section_number >= SHN_LORESERVE)
4313 {
4314 /* xgettext:c-format */
4315 _bfd_error_handler (_("%pB: too many sections: %u"),
4316 abfd, section_number);
4317 return false;
4318 }
4319
4320 elf_numsections (abfd) = section_number;
4321 elf_elfheader (abfd)->e_shnum = section_number;
4322
4323 /* Set up the list of section header pointers, in agreement with the
4324 indices. */
4325 amt = section_number * sizeof (Elf_Internal_Shdr *);
4326 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4327 if (i_shdrp == NULL)
4328 return false;
4329
4330 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4331 sizeof (Elf_Internal_Shdr));
4332 if (i_shdrp[0] == NULL)
4333 {
4334 bfd_release (abfd, i_shdrp);
4335 return false;
4336 }
4337
4338 elf_elfsections (abfd) = i_shdrp;
4339
4340 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4341 if (need_symtab)
4342 {
4343 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4344 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4345 {
4346 elf_section_list * entry = elf_symtab_shndx_list (abfd);
4347 BFD_ASSERT (entry != NULL);
4348 i_shdrp[entry->ndx] = & entry->hdr;
4349 entry->hdr.sh_link = elf_onesymtab (abfd);
4350 }
4351 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4352 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4353 }
4354
4355 for (sec = abfd->sections; sec; sec = sec->next)
4356 {
4357 asection *s;
4358
4359 d = elf_section_data (sec);
4360
4361 i_shdrp[d->this_idx] = &d->this_hdr;
4362 if (d->rel.idx != 0)
4363 i_shdrp[d->rel.idx] = d->rel.hdr;
4364 if (d->rela.idx != 0)
4365 i_shdrp[d->rela.idx] = d->rela.hdr;
4366
4367 /* Fill in the sh_link and sh_info fields while we're at it. */
4368
4369 /* sh_link of a reloc section is the section index of the symbol
4370 table. sh_info is the section index of the section to which
4371 the relocation entries apply. */
4372 if (d->rel.idx != 0)
4373 {
4374 d->rel.hdr->sh_link = elf_onesymtab (abfd);
4375 d->rel.hdr->sh_info = d->this_idx;
4376 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4377 }
4378 if (d->rela.idx != 0)
4379 {
4380 d->rela.hdr->sh_link = elf_onesymtab (abfd);
4381 d->rela.hdr->sh_info = d->this_idx;
4382 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4383 }
4384
4385 /* We need to set up sh_link for SHF_LINK_ORDER. */
4386 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4387 {
4388 s = elf_linked_to_section (sec);
4389 /* We can now have a NULL linked section pointer.
4390 This happens when the sh_link field is 0, which is done
4391 when a linked to section is discarded but the linking
4392 section has been retained for some reason. */
4393 if (s)
4394 {
4395 /* Check discarded linkonce section. */
4396 if (discarded_section (s))
4397 {
4398 asection *kept;
4399 _bfd_error_handler
4400 /* xgettext:c-format */
4401 (_("%pB: sh_link of section `%pA' points to"
4402 " discarded section `%pA' of `%pB'"),
4403 abfd, d->this_hdr.bfd_section, s, s->owner);
4404 /* Point to the kept section if it has the same
4405 size as the discarded one. */
4406 kept = _bfd_elf_check_kept_section (s, link_info);
4407 if (kept == NULL)
4408 {
4409 bfd_set_error (bfd_error_bad_value);
4410 return false;
4411 }
4412 s = kept;
4413 }
4414 /* Handle objcopy. */
4415 else if (s->output_section == NULL)
4416 {
4417 _bfd_error_handler
4418 /* xgettext:c-format */
4419 (_("%pB: sh_link of section `%pA' points to"
4420 " removed section `%pA' of `%pB'"),
4421 abfd, d->this_hdr.bfd_section, s, s->owner);
4422 bfd_set_error (bfd_error_bad_value);
4423 return false;
4424 }
4425 s = s->output_section;
4426 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4427 }
4428 }
4429
4430 switch (d->this_hdr.sh_type)
4431 {
4432 case SHT_REL:
4433 case SHT_RELA:
4434 /* sh_link is the section index of the symbol table.
4435 sh_info is the section index of the section to which the
4436 relocation entries apply. */
4437 if (d->this_hdr.sh_link == 0)
4438 {
4439 /* FIXME maybe: If this is a reloc section which we are
4440 treating as a normal section then we likely should
4441 not be assuming its sh_link is .dynsym or .symtab. */
4442 if ((sec->flags & SEC_ALLOC) != 0)
4443 {
4444 s = bfd_get_section_by_name (abfd, ".dynsym");
4445 if (s != NULL)
4446 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4447 }
4448 else
4449 d->this_hdr.sh_link = elf_onesymtab (abfd);
4450 }
4451
4452 s = elf_get_reloc_section (sec);
4453 if (s != NULL)
4454 {
4455 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4456 d->this_hdr.sh_flags |= SHF_INFO_LINK;
4457 }
4458 break;
4459
4460 case SHT_STRTAB:
4461 /* We assume that a section named .stab*str is a stabs
4462 string section. We look for a section with the same name
4463 but without the trailing ``str'', and set its sh_link
4464 field to point to this section. */
4465 if (startswith (sec->name, ".stab")
4466 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
4467 {
4468 size_t len;
4469 char *alc;
4470
4471 len = strlen (sec->name);
4472 alc = (char *) bfd_malloc (len - 2);
4473 if (alc == NULL)
4474 return false;
4475 memcpy (alc, sec->name, len - 3);
4476 alc[len - 3] = '\0';
4477 s = bfd_get_section_by_name (abfd, alc);
4478 free (alc);
4479 if (s != NULL)
4480 {
4481 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4482
4483 /* This is a .stab section. */
4484 elf_section_data (s)->this_hdr.sh_entsize = 12;
4485 }
4486 }
4487 break;
4488
4489 case SHT_DYNAMIC:
4490 case SHT_DYNSYM:
4491 case SHT_GNU_verneed:
4492 case SHT_GNU_verdef:
4493 /* sh_link is the section header index of the string table
4494 used for the dynamic entries, or the symbol table, or the
4495 version strings. */
4496 s = bfd_get_section_by_name (abfd, ".dynstr");
4497 if (s != NULL)
4498 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4499 break;
4500
4501 case SHT_GNU_LIBLIST:
4502 /* sh_link is the section header index of the prelink library
4503 list used for the dynamic entries, or the symbol table, or
4504 the version strings. */
4505 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4506 ? ".dynstr" : ".gnu.libstr"));
4507 if (s != NULL)
4508 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4509 break;
4510
4511 case SHT_HASH:
4512 case SHT_GNU_HASH:
4513 case SHT_GNU_versym:
4514 /* sh_link is the section header index of the symbol table
4515 this hash table or version table is for. */
4516 s = bfd_get_section_by_name (abfd, ".dynsym");
4517 if (s != NULL)
4518 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4519 break;
4520
4521 case SHT_GROUP:
4522 d->this_hdr.sh_link = elf_onesymtab (abfd);
4523 }
4524 }
4525
4526 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4527 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4528 debug section name from .debug_* to .zdebug_* if needed. */
4529
4530 return true;
4531 }
4532
4533 static bool
4534 sym_is_global (bfd *abfd, asymbol *sym)
4535 {
4536 /* If the backend has a special mapping, use it. */
4537 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4538 if (bed->elf_backend_sym_is_global)
4539 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4540
4541 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4542 || bfd_is_und_section (bfd_asymbol_section (sym))
4543 || bfd_is_com_section (bfd_asymbol_section (sym)));
4544 }
4545
4546 /* Filter global symbols of ABFD to include in the import library. All
4547 SYMCOUNT symbols of ABFD can be examined from their pointers in
4548 SYMS. Pointers of symbols to keep should be stored contiguously at
4549 the beginning of that array.
4550
4551 Returns the number of symbols to keep. */
4552
4553 unsigned int
4554 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4555 asymbol **syms, long symcount)
4556 {
4557 long src_count, dst_count = 0;
4558
4559 for (src_count = 0; src_count < symcount; src_count++)
4560 {
4561 asymbol *sym = syms[src_count];
4562 char *name = (char *) bfd_asymbol_name (sym);
4563 struct bfd_link_hash_entry *h;
4564
4565 if (!sym_is_global (abfd, sym))
4566 continue;
4567
4568 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4569 if (h == NULL)
4570 continue;
4571 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4572 continue;
4573 if (h->linker_def || h->ldscript_def)
4574 continue;
4575
4576 syms[dst_count++] = sym;
4577 }
4578
4579 syms[dst_count] = NULL;
4580
4581 return dst_count;
4582 }
4583
4584 /* Don't output section symbols for sections that are not going to be
4585 output, that are duplicates or there is no BFD section. */
4586
4587 static bool
4588 ignore_section_sym (bfd *abfd, asymbol *sym)
4589 {
4590 elf_symbol_type *type_ptr;
4591
4592 if (sym == NULL)
4593 return false;
4594
4595 if ((sym->flags & BSF_SECTION_SYM) == 0)
4596 return false;
4597
4598 /* Ignore the section symbol if it isn't used. */
4599 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4600 return true;
4601
4602 if (sym->section == NULL)
4603 return true;
4604
4605 type_ptr = elf_symbol_from (sym);
4606 return ((type_ptr != NULL
4607 && type_ptr->internal_elf_sym.st_shndx != 0
4608 && bfd_is_abs_section (sym->section))
4609 || !(sym->section->owner == abfd
4610 || (sym->section->output_section != NULL
4611 && sym->section->output_section->owner == abfd
4612 && sym->section->output_offset == 0)
4613 || bfd_is_abs_section (sym->section)));
4614 }
4615
4616 /* Map symbol from it's internal number to the external number, moving
4617 all local symbols to be at the head of the list. */
4618
4619 static bool
4620 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4621 {
4622 unsigned int symcount = bfd_get_symcount (abfd);
4623 asymbol **syms = bfd_get_outsymbols (abfd);
4624 asymbol **sect_syms;
4625 unsigned int num_locals = 0;
4626 unsigned int num_globals = 0;
4627 unsigned int num_locals2 = 0;
4628 unsigned int num_globals2 = 0;
4629 unsigned int max_index = 0;
4630 unsigned int idx;
4631 asection *asect;
4632 asymbol **new_syms;
4633 size_t amt;
4634
4635 #ifdef DEBUG
4636 fprintf (stderr, "elf_map_symbols\n");
4637 fflush (stderr);
4638 #endif
4639
4640 for (asect = abfd->sections; asect; asect = asect->next)
4641 {
4642 if (max_index < asect->index)
4643 max_index = asect->index;
4644 }
4645
4646 max_index++;
4647 amt = max_index * sizeof (asymbol *);
4648 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4649 if (sect_syms == NULL)
4650 return false;
4651 elf_section_syms (abfd) = sect_syms;
4652 elf_num_section_syms (abfd) = max_index;
4653
4654 /* Init sect_syms entries for any section symbols we have already
4655 decided to output. */
4656 for (idx = 0; idx < symcount; idx++)
4657 {
4658 asymbol *sym = syms[idx];
4659
4660 if ((sym->flags & BSF_SECTION_SYM) != 0
4661 && sym->value == 0
4662 && !ignore_section_sym (abfd, sym)
4663 && !bfd_is_abs_section (sym->section))
4664 {
4665 asection *sec = sym->section;
4666
4667 if (sec->owner != abfd)
4668 sec = sec->output_section;
4669
4670 sect_syms[sec->index] = syms[idx];
4671 }
4672 }
4673
4674 /* Classify all of the symbols. */
4675 for (idx = 0; idx < symcount; idx++)
4676 {
4677 if (sym_is_global (abfd, syms[idx]))
4678 num_globals++;
4679 else if (!ignore_section_sym (abfd, syms[idx]))
4680 num_locals++;
4681 }
4682
4683 /* We will be adding a section symbol for each normal BFD section. Most
4684 sections will already have a section symbol in outsymbols, but
4685 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4686 at least in that case. */
4687 for (asect = abfd->sections; asect; asect = asect->next)
4688 {
4689 asymbol *sym = asect->symbol;
4690 /* Don't include ignored section symbols. */
4691 if (!ignore_section_sym (abfd, sym)
4692 && sect_syms[asect->index] == NULL)
4693 {
4694 if (!sym_is_global (abfd, asect->symbol))
4695 num_locals++;
4696 else
4697 num_globals++;
4698 }
4699 }
4700
4701 /* Now sort the symbols so the local symbols are first. */
4702 amt = (num_locals + num_globals) * sizeof (asymbol *);
4703 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4704 if (new_syms == NULL)
4705 return false;
4706
4707 for (idx = 0; idx < symcount; idx++)
4708 {
4709 asymbol *sym = syms[idx];
4710 unsigned int i;
4711
4712 if (sym_is_global (abfd, sym))
4713 i = num_locals + num_globals2++;
4714 /* Don't include ignored section symbols. */
4715 else if (!ignore_section_sym (abfd, sym))
4716 i = num_locals2++;
4717 else
4718 continue;
4719 new_syms[i] = sym;
4720 sym->udata.i = i + 1;
4721 }
4722 for (asect = abfd->sections; asect; asect = asect->next)
4723 {
4724 asymbol *sym = asect->symbol;
4725 if (!ignore_section_sym (abfd, sym)
4726 && sect_syms[asect->index] == NULL)
4727 {
4728 unsigned int i;
4729
4730 sect_syms[asect->index] = sym;
4731 if (!sym_is_global (abfd, sym))
4732 i = num_locals2++;
4733 else
4734 i = num_locals + num_globals2++;
4735 new_syms[i] = sym;
4736 sym->udata.i = i + 1;
4737 }
4738 }
4739
4740 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4741
4742 *pnum_locals = num_locals;
4743 return true;
4744 }
4745
4746 /* Align to the maximum file alignment that could be required for any
4747 ELF data structure. */
4748
4749 static inline file_ptr
4750 align_file_position (file_ptr off, int align)
4751 {
4752 return (off + align - 1) & ~(align - 1);
4753 }
4754
4755 /* Assign a file position to a section, optionally aligning to the
4756 required section alignment. */
4757
4758 file_ptr
4759 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4760 file_ptr offset,
4761 bool align)
4762 {
4763 if (align && i_shdrp->sh_addralign > 1)
4764 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4765 i_shdrp->sh_offset = offset;
4766 if (i_shdrp->bfd_section != NULL)
4767 i_shdrp->bfd_section->filepos = offset;
4768 if (i_shdrp->sh_type != SHT_NOBITS)
4769 offset += i_shdrp->sh_size;
4770 return offset;
4771 }
4772
4773 /* Compute the file positions we are going to put the sections at, and
4774 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4775 is not NULL, this is being called by the ELF backend linker. */
4776
4777 bool
4778 _bfd_elf_compute_section_file_positions (bfd *abfd,
4779 struct bfd_link_info *link_info)
4780 {
4781 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4782 struct fake_section_arg fsargs;
4783 bool failed;
4784 struct elf_strtab_hash *strtab = NULL;
4785 Elf_Internal_Shdr *shstrtab_hdr;
4786 bool need_symtab;
4787
4788 if (abfd->output_has_begun)
4789 return true;
4790
4791 /* Do any elf backend specific processing first. */
4792 if (bed->elf_backend_begin_write_processing)
4793 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4794
4795 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4796 return false;
4797
4798 fsargs.failed = false;
4799 fsargs.link_info = link_info;
4800 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4801 if (fsargs.failed)
4802 return false;
4803
4804 if (!assign_section_numbers (abfd, link_info))
4805 return false;
4806
4807 /* The backend linker builds symbol table information itself. */
4808 need_symtab = (link_info == NULL
4809 && (bfd_get_symcount (abfd) > 0
4810 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4811 == HAS_RELOC)));
4812 if (need_symtab)
4813 {
4814 /* Non-zero if doing a relocatable link. */
4815 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4816
4817 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4818 return false;
4819 }
4820
4821 failed = false;
4822 if (link_info == NULL)
4823 {
4824 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4825 if (failed)
4826 goto err_free_strtab;
4827 }
4828
4829 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4830 /* sh_name was set in init_file_header. */
4831 shstrtab_hdr->sh_type = SHT_STRTAB;
4832 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4833 shstrtab_hdr->sh_addr = 0;
4834 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4835 shstrtab_hdr->sh_entsize = 0;
4836 shstrtab_hdr->sh_link = 0;
4837 shstrtab_hdr->sh_info = 0;
4838 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4839 shstrtab_hdr->sh_addralign = 1;
4840
4841 if (!assign_file_positions_except_relocs (abfd, link_info))
4842 goto err_free_strtab;
4843
4844 if (strtab != NULL)
4845 {
4846 file_ptr off;
4847 Elf_Internal_Shdr *hdr;
4848
4849 off = elf_next_file_pos (abfd);
4850
4851 hdr = & elf_symtab_hdr (abfd);
4852 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4853
4854 if (elf_symtab_shndx_list (abfd) != NULL)
4855 {
4856 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4857 if (hdr->sh_size != 0)
4858 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4859 /* FIXME: What about other symtab_shndx sections in the list ? */
4860 }
4861
4862 hdr = &elf_tdata (abfd)->strtab_hdr;
4863 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4864
4865 elf_next_file_pos (abfd) = off;
4866
4867 /* Now that we know where the .strtab section goes, write it
4868 out. */
4869 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4870 || ! _bfd_elf_strtab_emit (abfd, strtab))
4871 goto err_free_strtab;
4872 _bfd_elf_strtab_free (strtab);
4873 }
4874
4875 abfd->output_has_begun = true;
4876 return true;
4877
4878 err_free_strtab:
4879 if (strtab != NULL)
4880 _bfd_elf_strtab_free (strtab);
4881 return false;
4882 }
4883
4884 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4885 function effectively returns whether --eh-frame-hdr is given on the
4886 command line. After size_dynamic_sections the result reflects
4887 whether .eh_frame_hdr will actually be output (sizing isn't done
4888 until ldemul_after_allocation). */
4889
4890 static asection *
4891 elf_eh_frame_hdr (const struct bfd_link_info *info)
4892 {
4893 if (info != NULL && is_elf_hash_table (info->hash))
4894 return elf_hash_table (info)->eh_info.hdr_sec;
4895 return NULL;
4896 }
4897
4898 /* Make an initial estimate of the size of the program header. If we
4899 get the number wrong here, we'll redo section placement. */
4900
4901 static bfd_size_type
4902 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4903 {
4904 size_t segs;
4905 asection *s, *s2;
4906 const struct elf_backend_data *bed;
4907
4908 /* Assume we will need exactly two PT_LOAD segments: one for text
4909 and one for data. */
4910 segs = 2;
4911
4912 s = bfd_get_section_by_name (abfd, ".interp");
4913 s2 = bfd_get_section_by_name (abfd, ".dynamic");
4914 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4915 {
4916 ++segs;
4917 }
4918
4919 if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
4920 {
4921 /* We need a PT_DYNAMIC segment. */
4922 ++segs;
4923 }
4924
4925 if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
4926 (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
4927 {
4928 /*
4929 * If either a PT_INTERP or PT_DYNAMIC segment is created,
4930 * also create a PT_PHDR segment.
4931 */
4932 ++segs;
4933 }
4934
4935 if (info != NULL && info->relro)
4936 {
4937 /* We need a PT_GNU_RELRO segment. */
4938 ++segs;
4939 }
4940
4941 if (elf_eh_frame_hdr (info))
4942 {
4943 /* We need a PT_GNU_EH_FRAME segment. */
4944 ++segs;
4945 }
4946
4947 if (elf_stack_flags (abfd))
4948 {
4949 /* We need a PT_GNU_STACK segment. */
4950 ++segs;
4951 }
4952
4953 if (elf_sframe (abfd))
4954 {
4955 /* We need a PT_GNU_SFRAME segment. */
4956 ++segs;
4957 }
4958
4959 s = bfd_get_section_by_name (abfd,
4960 NOTE_GNU_PROPERTY_SECTION_NAME);
4961 if (s != NULL && s->size != 0)
4962 {
4963 /* We need a PT_GNU_PROPERTY segment. */
4964 ++segs;
4965 }
4966
4967 for (s = abfd->sections; s != NULL; s = s->next)
4968 {
4969 if ((s->flags & SEC_LOAD) != 0
4970 && elf_section_type (s) == SHT_NOTE)
4971 {
4972 unsigned int alignment_power;
4973 /* We need a PT_NOTE segment. */
4974 ++segs;
4975 /* Try to create just one PT_NOTE segment for all adjacent
4976 loadable SHT_NOTE sections. gABI requires that within a
4977 PT_NOTE segment (and also inside of each SHT_NOTE section)
4978 each note should have the same alignment. So we check
4979 whether the sections are correctly aligned. */
4980 alignment_power = s->alignment_power;
4981 while (s->next != NULL
4982 && s->next->alignment_power == alignment_power
4983 && (s->next->flags & SEC_LOAD) != 0
4984 && elf_section_type (s->next) == SHT_NOTE)
4985 s = s->next;
4986 }
4987 }
4988
4989 for (s = abfd->sections; s != NULL; s = s->next)
4990 {
4991 if (s->flags & SEC_THREAD_LOCAL)
4992 {
4993 /* We need a PT_TLS segment. */
4994 ++segs;
4995 break;
4996 }
4997 }
4998
4999 bed = get_elf_backend_data (abfd);
5000
5001 if ((abfd->flags & D_PAGED) != 0
5002 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5003 {
5004 /* Add a PT_GNU_MBIND segment for each mbind section. */
5005 bfd_vma commonpagesize;
5006 unsigned int page_align_power;
5007
5008 if (info != NULL)
5009 commonpagesize = info->commonpagesize;
5010 else
5011 commonpagesize = bed->commonpagesize;
5012 page_align_power = bfd_log2 (commonpagesize);
5013 for (s = abfd->sections; s != NULL; s = s->next)
5014 if (elf_section_flags (s) & SHF_GNU_MBIND)
5015 {
5016 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
5017 {
5018 _bfd_error_handler
5019 /* xgettext:c-format */
5020 (_("%pB: GNU_MBIND section `%pA' has invalid "
5021 "sh_info field: %d"),
5022 abfd, s, elf_section_data (s)->this_hdr.sh_info);
5023 continue;
5024 }
5025 /* Align mbind section to page size. */
5026 if (s->alignment_power < page_align_power)
5027 s->alignment_power = page_align_power;
5028 segs ++;
5029 }
5030 }
5031
5032 /* Let the backend count up any program headers it might need. */
5033 if (bed->elf_backend_additional_program_headers)
5034 {
5035 int a;
5036
5037 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
5038 if (a == -1)
5039 abort ();
5040 segs += a;
5041 }
5042
5043 return segs * bed->s->sizeof_phdr;
5044 }
5045
5046 /* Find the segment that contains the output_section of section. */
5047
5048 Elf_Internal_Phdr *
5049 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
5050 {
5051 struct elf_segment_map *m;
5052 Elf_Internal_Phdr *p;
5053
5054 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
5055 m != NULL;
5056 m = m->next, p++)
5057 {
5058 int i;
5059
5060 for (i = m->count - 1; i >= 0; i--)
5061 if (m->sections[i] == section)
5062 return p;
5063 }
5064
5065 return NULL;
5066 }
5067
5068 /* Create a mapping from a set of sections to a program segment. */
5069
5070 static struct elf_segment_map *
5071 make_mapping (bfd *abfd,
5072 asection **sections,
5073 unsigned int from,
5074 unsigned int to,
5075 bool phdr)
5076 {
5077 struct elf_segment_map *m;
5078 unsigned int i;
5079 asection **hdrpp;
5080 size_t amt;
5081
5082 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5083 amt += (to - from) * sizeof (asection *);
5084 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5085 if (m == NULL)
5086 return NULL;
5087 m->next = NULL;
5088 m->p_type = PT_LOAD;
5089 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5090 m->sections[i - from] = *hdrpp;
5091 m->count = to - from;
5092
5093 if (from == 0 && phdr)
5094 {
5095 /* Include the headers in the first PT_LOAD segment. */
5096 m->includes_filehdr = 1;
5097 m->includes_phdrs = 1;
5098 }
5099
5100 return m;
5101 }
5102
5103 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
5104 on failure. */
5105
5106 struct elf_segment_map *
5107 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5108 {
5109 struct elf_segment_map *m;
5110
5111 m = (struct elf_segment_map *) bfd_zalloc (abfd,
5112 sizeof (struct elf_segment_map));
5113 if (m == NULL)
5114 return NULL;
5115 m->next = NULL;
5116 m->p_type = PT_DYNAMIC;
5117 m->count = 1;
5118 m->sections[0] = dynsec;
5119
5120 return m;
5121 }
5122
5123 /* Possibly add or remove segments from the segment map. */
5124
5125 static bool
5126 elf_modify_segment_map (bfd *abfd,
5127 struct bfd_link_info *info,
5128 bool remove_empty_load)
5129 {
5130 struct elf_segment_map **m;
5131 const struct elf_backend_data *bed;
5132
5133 /* The placement algorithm assumes that non allocated sections are
5134 not in PT_LOAD segments. We ensure this here by removing such
5135 sections from the segment map. We also remove excluded
5136 sections. Finally, any PT_LOAD segment without sections is
5137 removed. */
5138 m = &elf_seg_map (abfd);
5139 while (*m)
5140 {
5141 unsigned int i, new_count;
5142
5143 for (new_count = 0, i = 0; i < (*m)->count; i++)
5144 {
5145 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5146 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5147 || (*m)->p_type != PT_LOAD))
5148 {
5149 (*m)->sections[new_count] = (*m)->sections[i];
5150 new_count++;
5151 }
5152 }
5153 (*m)->count = new_count;
5154
5155 if (remove_empty_load
5156 && (*m)->p_type == PT_LOAD
5157 && (*m)->count == 0
5158 && !(*m)->includes_phdrs)
5159 *m = (*m)->next;
5160 else
5161 m = &(*m)->next;
5162 }
5163
5164 bed = get_elf_backend_data (abfd);
5165 if (bed->elf_backend_modify_segment_map != NULL)
5166 {
5167 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5168 return false;
5169 }
5170
5171 return true;
5172 }
5173
5174 #define IS_TBSS(s) \
5175 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5176
5177 /* Set up a mapping from BFD sections to program segments. Update
5178 NEED_LAYOUT if the section layout is changed. */
5179
5180 bool
5181 _bfd_elf_map_sections_to_segments (bfd *abfd,
5182 struct bfd_link_info *info,
5183 bool *need_layout)
5184 {
5185 unsigned int count;
5186 struct elf_segment_map *m;
5187 asection **sections = NULL;
5188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5189 bool no_user_phdrs;
5190
5191 no_user_phdrs = elf_seg_map (abfd) == NULL;
5192
5193 if (info != NULL)
5194 {
5195 info->user_phdrs = !no_user_phdrs;
5196
5197 /* Size the relative relocations if DT_RELR is enabled. */
5198 if (info->enable_dt_relr
5199 && need_layout != NULL
5200 && bed->size_relative_relocs
5201 && !bed->size_relative_relocs (info, need_layout))
5202 info->callbacks->einfo
5203 (_("%F%P: failed to size relative relocations\n"));
5204 }
5205
5206 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5207 {
5208 asection *s;
5209 unsigned int i;
5210 struct elf_segment_map *mfirst;
5211 struct elf_segment_map **pm;
5212 asection *last_hdr;
5213 bfd_vma last_size;
5214 unsigned int hdr_index;
5215 bfd_vma maxpagesize;
5216 asection **hdrpp;
5217 bool phdr_in_segment;
5218 bool writable;
5219 bool executable;
5220 unsigned int tls_count = 0;
5221 asection *first_tls = NULL;
5222 asection *first_mbind = NULL;
5223 asection *dynsec, *eh_frame_hdr;
5224 asection *sframe;
5225 size_t amt;
5226 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
5227 bfd_size_type phdr_size; /* Octets/bytes. */
5228 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5229
5230 /* Select the allocated sections, and sort them. */
5231
5232 amt = bfd_count_sections (abfd) * sizeof (asection *);
5233 sections = (asection **) bfd_malloc (amt);
5234 if (sections == NULL)
5235 goto error_return;
5236
5237 /* Calculate top address, avoiding undefined behaviour of shift
5238 left operator when shift count is equal to size of type
5239 being shifted. */
5240 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5241 addr_mask = (addr_mask << 1) + 1;
5242
5243 i = 0;
5244 for (s = abfd->sections; s != NULL; s = s->next)
5245 {
5246 if ((s->flags & SEC_ALLOC) != 0)
5247 {
5248 /* target_index is unused until bfd_elf_final_link
5249 starts output of section symbols. Use it to make
5250 qsort stable. */
5251 s->target_index = i;
5252 sections[i] = s;
5253 ++i;
5254 /* A wrapping section potentially clashes with header. */
5255 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5256 wrap_to = (s->lma + s->size / opb) & addr_mask;
5257 }
5258 }
5259 BFD_ASSERT (i <= bfd_count_sections (abfd));
5260 count = i;
5261
5262 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5263
5264 phdr_size = elf_program_header_size (abfd);
5265 if (phdr_size == (bfd_size_type) -1)
5266 phdr_size = get_program_header_size (abfd, info);
5267 phdr_size += bed->s->sizeof_ehdr;
5268 /* phdr_size is compared to LMA values which are in bytes. */
5269 phdr_size /= opb;
5270 if (info != NULL)
5271 maxpagesize = info->maxpagesize;
5272 else
5273 maxpagesize = bed->maxpagesize;
5274 if (maxpagesize == 0)
5275 maxpagesize = 1;
5276 phdr_in_segment = info != NULL && info->load_phdrs;
5277 if (count != 0
5278 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5279 >= (phdr_size & (maxpagesize - 1))))
5280 /* For compatibility with old scripts that may not be using
5281 SIZEOF_HEADERS, add headers when it looks like space has
5282 been left for them. */
5283 phdr_in_segment = true;
5284
5285 /* Build the mapping. */
5286 mfirst = NULL;
5287 pm = &mfirst;
5288
5289 /* If we have a .interp section, then create a PT_PHDR segment for
5290 the program headers and a PT_INTERP segment for the .interp
5291 section. */
5292 s = bfd_get_section_by_name (abfd, ".interp");
5293 if (s != NULL && (s->flags & SEC_LOAD) == 0)
5294 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5295 s = NULL;
5296 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5297 if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
5298 dynsec = NULL;
5299
5300 if (s != NULL || dynsec != NULL)
5301 {
5302 amt = sizeof (struct elf_segment_map);
5303 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5304 if (m == NULL)
5305 goto error_return;
5306 m->next = NULL;
5307 m->p_type = PT_PHDR;
5308 m->p_flags = PF_R;
5309 m->p_flags_valid = 1;
5310 m->includes_phdrs = 1;
5311 phdr_in_segment = true;
5312 *pm = m;
5313 pm = &m->next;
5314 }
5315
5316 if (s != NULL)
5317 {
5318 amt = sizeof (struct elf_segment_map);
5319 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5320 if (m == NULL)
5321 goto error_return;
5322 m->next = NULL;
5323 m->p_type = PT_INTERP;
5324 m->count = 1;
5325 m->sections[0] = s;
5326
5327 *pm = m;
5328 pm = &m->next;
5329 }
5330
5331 /* Look through the sections. We put sections in the same program
5332 segment when the start of the second section can be placed within
5333 a few bytes of the end of the first section. */
5334 last_hdr = NULL;
5335 last_size = 0;
5336 hdr_index = 0;
5337 writable = false;
5338 executable = false;
5339
5340 if ((abfd->flags & D_PAGED) == 0)
5341 phdr_in_segment = false;
5342
5343 /* Deal with -Ttext or something similar such that the first section
5344 is not adjacent to the program headers. This is an
5345 approximation, since at this point we don't know exactly how many
5346 program headers we will need. */
5347 if (phdr_in_segment && count > 0)
5348 {
5349 bfd_vma phdr_lma; /* Bytes. */
5350 bool separate_phdr = false;
5351
5352 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5353 if (info != NULL
5354 && info->separate_code
5355 && (sections[0]->flags & SEC_CODE) != 0)
5356 {
5357 /* If data sections should be separate from code and
5358 thus not executable, and the first section is
5359 executable then put the file and program headers in
5360 their own PT_LOAD. */
5361 separate_phdr = true;
5362 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5363 == (sections[0]->lma & addr_mask & -maxpagesize)))
5364 {
5365 /* The file and program headers are currently on the
5366 same page as the first section. Put them on the
5367 previous page if we can. */
5368 if (phdr_lma >= maxpagesize)
5369 phdr_lma -= maxpagesize;
5370 else
5371 separate_phdr = false;
5372 }
5373 }
5374 if ((sections[0]->lma & addr_mask) < phdr_lma
5375 || (sections[0]->lma & addr_mask) < phdr_size)
5376 /* If file and program headers would be placed at the end
5377 of memory then it's probably better to omit them. */
5378 phdr_in_segment = false;
5379 else if (phdr_lma < wrap_to)
5380 /* If a section wraps around to where we'll be placing
5381 file and program headers, then the headers will be
5382 overwritten. */
5383 phdr_in_segment = false;
5384 else if (separate_phdr)
5385 {
5386 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5387 if (m == NULL)
5388 goto error_return;
5389 m->p_paddr = phdr_lma * opb;
5390 m->p_vaddr_offset
5391 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5392 m->p_paddr_valid = 1;
5393 *pm = m;
5394 pm = &m->next;
5395 phdr_in_segment = false;
5396 }
5397 }
5398
5399 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5400 {
5401 asection *hdr;
5402 bool new_segment;
5403
5404 hdr = *hdrpp;
5405
5406 /* See if this section and the last one will fit in the same
5407 segment. */
5408
5409 if (last_hdr == NULL)
5410 {
5411 /* If we don't have a segment yet, then we don't need a new
5412 one (we build the last one after this loop). */
5413 new_segment = false;
5414 }
5415 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5416 {
5417 /* If this section has a different relation between the
5418 virtual address and the load address, then we need a new
5419 segment. */
5420 new_segment = true;
5421 }
5422 else if (hdr->lma < last_hdr->lma + last_size
5423 || last_hdr->lma + last_size < last_hdr->lma)
5424 {
5425 /* If this section has a load address that makes it overlap
5426 the previous section, then we need a new segment. */
5427 new_segment = true;
5428 }
5429 else if ((abfd->flags & D_PAGED) != 0
5430 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5431 == (hdr->lma & -maxpagesize)))
5432 {
5433 /* If we are demand paged then we can't map two disk
5434 pages onto the same memory page. */
5435 new_segment = false;
5436 }
5437 /* In the next test we have to be careful when last_hdr->lma is close
5438 to the end of the address space. If the aligned address wraps
5439 around to the start of the address space, then there are no more
5440 pages left in memory and it is OK to assume that the current
5441 section can be included in the current segment. */
5442 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5443 + maxpagesize > last_hdr->lma)
5444 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5445 + maxpagesize <= hdr->lma))
5446 {
5447 /* If putting this section in this segment would force us to
5448 skip a page in the segment, then we need a new segment. */
5449 new_segment = true;
5450 }
5451 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5452 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5453 {
5454 /* We don't want to put a loaded section after a
5455 nonloaded (ie. bss style) section in the same segment
5456 as that will force the non-loaded section to be loaded.
5457 Consider .tbss sections as loaded for this purpose. */
5458 new_segment = true;
5459 }
5460 else if ((abfd->flags & D_PAGED) == 0)
5461 {
5462 /* If the file is not demand paged, which means that we
5463 don't require the sections to be correctly aligned in the
5464 file, then there is no other reason for a new segment. */
5465 new_segment = false;
5466 }
5467 else if (info != NULL
5468 && info->separate_code
5469 && executable != ((hdr->flags & SEC_CODE) != 0))
5470 {
5471 new_segment = true;
5472 }
5473 else if (! writable
5474 && (hdr->flags & SEC_READONLY) == 0)
5475 {
5476 /* We don't want to put a writable section in a read only
5477 segment. */
5478 new_segment = true;
5479 }
5480 else
5481 {
5482 /* Otherwise, we can use the same segment. */
5483 new_segment = false;
5484 }
5485
5486 /* Allow interested parties a chance to override our decision. */
5487 if (last_hdr != NULL
5488 && info != NULL
5489 && info->callbacks->override_segment_assignment != NULL)
5490 new_segment
5491 = info->callbacks->override_segment_assignment (info, abfd, hdr,
5492 last_hdr,
5493 new_segment);
5494
5495 if (! new_segment)
5496 {
5497 if ((hdr->flags & SEC_READONLY) == 0)
5498 writable = true;
5499 if ((hdr->flags & SEC_CODE) != 0)
5500 executable = true;
5501 last_hdr = hdr;
5502 /* .tbss sections effectively have zero size. */
5503 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5504 continue;
5505 }
5506
5507 /* We need a new program segment. We must create a new program
5508 header holding all the sections from hdr_index until hdr. */
5509
5510 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5511 if (m == NULL)
5512 goto error_return;
5513
5514 *pm = m;
5515 pm = &m->next;
5516
5517 if ((hdr->flags & SEC_READONLY) == 0)
5518 writable = true;
5519 else
5520 writable = false;
5521
5522 if ((hdr->flags & SEC_CODE) == 0)
5523 executable = false;
5524 else
5525 executable = true;
5526
5527 last_hdr = hdr;
5528 /* .tbss sections effectively have zero size. */
5529 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5530 hdr_index = i;
5531 phdr_in_segment = false;
5532 }
5533
5534 /* Create a final PT_LOAD program segment, but not if it's just
5535 for .tbss. */
5536 if (last_hdr != NULL
5537 && (i - hdr_index != 1
5538 || !IS_TBSS (last_hdr)))
5539 {
5540 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5541 if (m == NULL)
5542 goto error_return;
5543
5544 *pm = m;
5545 pm = &m->next;
5546 }
5547
5548 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5549 if (dynsec != NULL)
5550 {
5551 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5552 if (m == NULL)
5553 goto error_return;
5554 *pm = m;
5555 pm = &m->next;
5556 }
5557
5558 /* For each batch of consecutive loadable SHT_NOTE sections,
5559 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5560 because if we link together nonloadable .note sections and
5561 loadable .note sections, we will generate two .note sections
5562 in the output file. */
5563 for (s = abfd->sections; s != NULL; s = s->next)
5564 {
5565 if ((s->flags & SEC_LOAD) != 0
5566 && elf_section_type (s) == SHT_NOTE)
5567 {
5568 asection *s2;
5569 unsigned int alignment_power = s->alignment_power;
5570
5571 count = 1;
5572 for (s2 = s; s2->next != NULL; s2 = s2->next)
5573 {
5574 if (s2->next->alignment_power == alignment_power
5575 && (s2->next->flags & SEC_LOAD) != 0
5576 && elf_section_type (s2->next) == SHT_NOTE
5577 && align_power (s2->lma + s2->size / opb,
5578 alignment_power)
5579 == s2->next->lma)
5580 count++;
5581 else
5582 break;
5583 }
5584 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5585 amt += count * sizeof (asection *);
5586 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5587 if (m == NULL)
5588 goto error_return;
5589 m->next = NULL;
5590 m->p_type = PT_NOTE;
5591 m->count = count;
5592 while (count > 1)
5593 {
5594 m->sections[m->count - count--] = s;
5595 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5596 s = s->next;
5597 }
5598 m->sections[m->count - 1] = s;
5599 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5600 *pm = m;
5601 pm = &m->next;
5602 }
5603 if (s->flags & SEC_THREAD_LOCAL)
5604 {
5605 if (! tls_count)
5606 first_tls = s;
5607 tls_count++;
5608 }
5609 if (first_mbind == NULL
5610 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5611 first_mbind = s;
5612 }
5613
5614 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5615 if (tls_count > 0)
5616 {
5617 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5618 amt += tls_count * sizeof (asection *);
5619 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5620 if (m == NULL)
5621 goto error_return;
5622 m->next = NULL;
5623 m->p_type = PT_TLS;
5624 m->count = tls_count;
5625 /* Mandated PF_R. */
5626 m->p_flags = PF_R;
5627 m->p_flags_valid = 1;
5628 s = first_tls;
5629 for (i = 0; i < tls_count; ++i)
5630 {
5631 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5632 {
5633 _bfd_error_handler
5634 (_("%pB: TLS sections are not adjacent:"), abfd);
5635 s = first_tls;
5636 i = 0;
5637 while (i < tls_count)
5638 {
5639 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5640 {
5641 _bfd_error_handler (_(" TLS: %pA"), s);
5642 i++;
5643 }
5644 else
5645 _bfd_error_handler (_(" non-TLS: %pA"), s);
5646 s = s->next;
5647 }
5648 bfd_set_error (bfd_error_bad_value);
5649 goto error_return;
5650 }
5651 m->sections[i] = s;
5652 s = s->next;
5653 }
5654
5655 *pm = m;
5656 pm = &m->next;
5657 }
5658
5659 if (first_mbind
5660 && (abfd->flags & D_PAGED) != 0
5661 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5662 for (s = first_mbind; s != NULL; s = s->next)
5663 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5664 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5665 {
5666 /* Mandated PF_R. */
5667 unsigned long p_flags = PF_R;
5668 if ((s->flags & SEC_READONLY) == 0)
5669 p_flags |= PF_W;
5670 if ((s->flags & SEC_CODE) != 0)
5671 p_flags |= PF_X;
5672
5673 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5674 m = bfd_zalloc (abfd, amt);
5675 if (m == NULL)
5676 goto error_return;
5677 m->next = NULL;
5678 m->p_type = (PT_GNU_MBIND_LO
5679 + elf_section_data (s)->this_hdr.sh_info);
5680 m->count = 1;
5681 m->p_flags_valid = 1;
5682 m->sections[0] = s;
5683 m->p_flags = p_flags;
5684
5685 *pm = m;
5686 pm = &m->next;
5687 }
5688
5689 s = bfd_get_section_by_name (abfd,
5690 NOTE_GNU_PROPERTY_SECTION_NAME);
5691 if (s != NULL && s->size != 0)
5692 {
5693 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5694 m = bfd_zalloc (abfd, amt);
5695 if (m == NULL)
5696 goto error_return;
5697 m->next = NULL;
5698 m->p_type = PT_GNU_PROPERTY;
5699 m->count = 1;
5700 m->p_flags_valid = 1;
5701 m->sections[0] = s;
5702 m->p_flags = PF_R;
5703 *pm = m;
5704 pm = &m->next;
5705 }
5706
5707 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5708 segment. */
5709 eh_frame_hdr = elf_eh_frame_hdr (info);
5710 if (eh_frame_hdr != NULL
5711 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5712 {
5713 amt = sizeof (struct elf_segment_map);
5714 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5715 if (m == NULL)
5716 goto error_return;
5717 m->next = NULL;
5718 m->p_type = PT_GNU_EH_FRAME;
5719 m->count = 1;
5720 m->sections[0] = eh_frame_hdr->output_section;
5721
5722 *pm = m;
5723 pm = &m->next;
5724 }
5725
5726 /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5727 segment. */
5728 sframe = elf_sframe (abfd);
5729 if (sframe != NULL
5730 && (sframe->output_section->flags & SEC_LOAD) != 0
5731 && sframe->size != 0)
5732 {
5733 amt = sizeof (struct elf_segment_map);
5734 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5735 if (m == NULL)
5736 goto error_return;
5737 m->next = NULL;
5738 m->p_type = PT_GNU_SFRAME;
5739 m->count = 1;
5740 m->sections[0] = sframe->output_section;
5741
5742 *pm = m;
5743 pm = &m->next;
5744 }
5745
5746 if (elf_stack_flags (abfd))
5747 {
5748 amt = sizeof (struct elf_segment_map);
5749 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5750 if (m == NULL)
5751 goto error_return;
5752 m->next = NULL;
5753 m->p_type = PT_GNU_STACK;
5754 m->p_flags = elf_stack_flags (abfd);
5755 m->p_align = bed->stack_align;
5756 m->p_flags_valid = 1;
5757 m->p_align_valid = m->p_align != 0;
5758 if (info->stacksize > 0)
5759 {
5760 m->p_size = info->stacksize;
5761 m->p_size_valid = 1;
5762 }
5763
5764 *pm = m;
5765 pm = &m->next;
5766 }
5767
5768 if (info != NULL && info->relro)
5769 {
5770 for (m = mfirst; m != NULL; m = m->next)
5771 {
5772 if (m->p_type == PT_LOAD
5773 && m->count != 0
5774 && m->sections[0]->vma >= info->relro_start
5775 && m->sections[0]->vma < info->relro_end)
5776 {
5777 i = m->count;
5778 while (--i != (unsigned) -1)
5779 {
5780 if (m->sections[i]->size > 0
5781 && (m->sections[i]->flags & SEC_LOAD) != 0
5782 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5783 break;
5784 }
5785
5786 if (i != (unsigned) -1)
5787 break;
5788 }
5789 }
5790
5791 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5792 if (m != NULL)
5793 {
5794 amt = sizeof (struct elf_segment_map);
5795 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5796 if (m == NULL)
5797 goto error_return;
5798 m->next = NULL;
5799 m->p_type = PT_GNU_RELRO;
5800 *pm = m;
5801 pm = &m->next;
5802 }
5803 }
5804
5805 free (sections);
5806 elf_seg_map (abfd) = mfirst;
5807 }
5808
5809 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5810 return false;
5811
5812 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5813 ++count;
5814 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5815
5816 return true;
5817
5818 error_return:
5819 free (sections);
5820 return false;
5821 }
5822
5823 /* Sort sections by address. */
5824
5825 static int
5826 elf_sort_sections (const void *arg1, const void *arg2)
5827 {
5828 const asection *sec1 = *(const asection **) arg1;
5829 const asection *sec2 = *(const asection **) arg2;
5830 bfd_size_type size1, size2;
5831
5832 /* Sort by LMA first, since this is the address used to
5833 place the section into a segment. */
5834 if (sec1->lma < sec2->lma)
5835 return -1;
5836 else if (sec1->lma > sec2->lma)
5837 return 1;
5838
5839 /* Then sort by VMA. Normally the LMA and the VMA will be
5840 the same, and this will do nothing. */
5841 if (sec1->vma < sec2->vma)
5842 return -1;
5843 else if (sec1->vma > sec2->vma)
5844 return 1;
5845
5846 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5847
5848 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5849 && (x)->size != 0)
5850
5851 if (TOEND (sec1))
5852 {
5853 if (!TOEND (sec2))
5854 return 1;
5855 }
5856 else if (TOEND (sec2))
5857 return -1;
5858
5859 #undef TOEND
5860
5861 /* Sort by size, to put zero sized sections
5862 before others at the same address. */
5863
5864 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5865 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5866
5867 if (size1 < size2)
5868 return -1;
5869 if (size1 > size2)
5870 return 1;
5871
5872 return sec1->target_index - sec2->target_index;
5873 }
5874
5875 /* This qsort comparison functions sorts PT_LOAD segments first and
5876 by p_paddr, for assign_file_positions_for_load_sections. */
5877
5878 static int
5879 elf_sort_segments (const void *arg1, const void *arg2)
5880 {
5881 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5882 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5883
5884 if (m1->p_type != m2->p_type)
5885 {
5886 if (m1->p_type == PT_NULL)
5887 return 1;
5888 if (m2->p_type == PT_NULL)
5889 return -1;
5890 return m1->p_type < m2->p_type ? -1 : 1;
5891 }
5892 if (m1->includes_filehdr != m2->includes_filehdr)
5893 return m1->includes_filehdr ? -1 : 1;
5894 if (m1->no_sort_lma != m2->no_sort_lma)
5895 return m1->no_sort_lma ? -1 : 1;
5896 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5897 {
5898 bfd_vma lma1, lma2; /* Octets. */
5899 lma1 = 0;
5900 if (m1->p_paddr_valid)
5901 lma1 = m1->p_paddr;
5902 else if (m1->count != 0)
5903 {
5904 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5905 m1->sections[0]);
5906 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5907 }
5908 lma2 = 0;
5909 if (m2->p_paddr_valid)
5910 lma2 = m2->p_paddr;
5911 else if (m2->count != 0)
5912 {
5913 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5914 m2->sections[0]);
5915 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5916 }
5917 if (lma1 != lma2)
5918 return lma1 < lma2 ? -1 : 1;
5919 }
5920 if (m1->idx != m2->idx)
5921 return m1->idx < m2->idx ? -1 : 1;
5922 return 0;
5923 }
5924
5925 /* Ian Lance Taylor writes:
5926
5927 We shouldn't be using % with a negative signed number. That's just
5928 not good. We have to make sure either that the number is not
5929 negative, or that the number has an unsigned type. When the types
5930 are all the same size they wind up as unsigned. When file_ptr is a
5931 larger signed type, the arithmetic winds up as signed long long,
5932 which is wrong.
5933
5934 What we're trying to say here is something like ``increase OFF by
5935 the least amount that will cause it to be equal to the VMA modulo
5936 the page size.'' */
5937 /* In other words, something like:
5938
5939 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5940 off_offset = off % bed->maxpagesize;
5941 if (vma_offset < off_offset)
5942 adjustment = vma_offset + bed->maxpagesize - off_offset;
5943 else
5944 adjustment = vma_offset - off_offset;
5945
5946 which can be collapsed into the expression below. */
5947
5948 static file_ptr
5949 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5950 {
5951 /* PR binutils/16199: Handle an alignment of zero. */
5952 if (maxpagesize == 0)
5953 maxpagesize = 1;
5954 return ((vma - off) % maxpagesize);
5955 }
5956
5957 static void
5958 print_segment_map (const struct elf_segment_map *m)
5959 {
5960 unsigned int j;
5961 const char *pt = get_segment_type (m->p_type);
5962 char buf[32];
5963
5964 if (pt == NULL)
5965 {
5966 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5967 sprintf (buf, "LOPROC+%7.7x",
5968 (unsigned int) (m->p_type - PT_LOPROC));
5969 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5970 sprintf (buf, "LOOS+%7.7x",
5971 (unsigned int) (m->p_type - PT_LOOS));
5972 else
5973 snprintf (buf, sizeof (buf), "%8.8x",
5974 (unsigned int) m->p_type);
5975 pt = buf;
5976 }
5977 fflush (stdout);
5978 fprintf (stderr, "%s:", pt);
5979 for (j = 0; j < m->count; j++)
5980 fprintf (stderr, " %s", m->sections [j]->name);
5981 putc ('\n',stderr);
5982 fflush (stderr);
5983 }
5984
5985 /* Assign file positions to the sections based on the mapping from
5986 sections to segments. This function also sets up some fields in
5987 the file header. */
5988
5989 static bool
5990 assign_file_positions_for_load_sections (bfd *abfd,
5991 struct bfd_link_info *link_info)
5992 {
5993 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5994 struct elf_segment_map *m;
5995 struct elf_segment_map *phdr_load_seg;
5996 Elf_Internal_Phdr *phdrs;
5997 Elf_Internal_Phdr *p;
5998 file_ptr off; /* Octets. */
5999 bfd_size_type maxpagesize;
6000 unsigned int alloc, actual;
6001 unsigned int i, j;
6002 struct elf_segment_map **sorted_seg_map;
6003 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6004
6005 if (link_info == NULL
6006 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
6007 return false;
6008
6009 alloc = 0;
6010 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6011 m->idx = alloc++;
6012
6013 if (alloc)
6014 {
6015 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
6016 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
6017 }
6018 else
6019 {
6020 /* PR binutils/12467. */
6021 elf_elfheader (abfd)->e_phoff = 0;
6022 elf_elfheader (abfd)->e_phentsize = 0;
6023 }
6024
6025 elf_elfheader (abfd)->e_phnum = alloc;
6026
6027 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
6028 {
6029 actual = alloc;
6030 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
6031 }
6032 else
6033 {
6034 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6035 BFD_ASSERT (elf_program_header_size (abfd)
6036 == actual * bed->s->sizeof_phdr);
6037 BFD_ASSERT (actual >= alloc);
6038 }
6039
6040 if (alloc == 0)
6041 {
6042 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
6043 return true;
6044 }
6045
6046 /* We're writing the size in elf_program_header_size (abfd),
6047 see assign_file_positions_except_relocs, so make sure we have
6048 that amount allocated, with trailing space cleared.
6049 The variable alloc contains the computed need, while
6050 elf_program_header_size (abfd) contains the size used for the
6051 layout.
6052 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
6053 where the layout is forced to according to a larger size in the
6054 last iterations for the testcase ld-elf/header. */
6055 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
6056 + alloc * sizeof (*sorted_seg_map)));
6057 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
6058 elf_tdata (abfd)->phdr = phdrs;
6059 if (phdrs == NULL)
6060 return false;
6061
6062 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
6063 {
6064 sorted_seg_map[j] = m;
6065 /* If elf_segment_map is not from map_sections_to_segments, the
6066 sections may not be correctly ordered. NOTE: sorting should
6067 not be done to the PT_NOTE section of a corefile, which may
6068 contain several pseudo-sections artificially created by bfd.
6069 Sorting these pseudo-sections breaks things badly. */
6070 if (m->count > 1
6071 && !(elf_elfheader (abfd)->e_type == ET_CORE
6072 && m->p_type == PT_NOTE))
6073 {
6074 for (i = 0; i < m->count; i++)
6075 m->sections[i]->target_index = i;
6076 qsort (m->sections, (size_t) m->count, sizeof (asection *),
6077 elf_sort_sections);
6078 }
6079 }
6080 if (alloc > 1)
6081 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
6082 elf_sort_segments);
6083
6084 maxpagesize = 1;
6085 if ((abfd->flags & D_PAGED) != 0)
6086 {
6087 if (link_info != NULL)
6088 maxpagesize = link_info->maxpagesize;
6089 else
6090 maxpagesize = bed->maxpagesize;
6091 }
6092
6093 /* Sections must map to file offsets past the ELF file header. */
6094 off = bed->s->sizeof_ehdr;
6095 /* And if one of the PT_LOAD headers doesn't include the program
6096 headers then we'll be mapping program headers in the usual
6097 position after the ELF file header. */
6098 phdr_load_seg = NULL;
6099 for (j = 0; j < alloc; j++)
6100 {
6101 m = sorted_seg_map[j];
6102 if (m->p_type != PT_LOAD)
6103 break;
6104 if (m->includes_phdrs)
6105 {
6106 phdr_load_seg = m;
6107 break;
6108 }
6109 }
6110 if (phdr_load_seg == NULL)
6111 off += actual * bed->s->sizeof_phdr;
6112
6113 for (j = 0; j < alloc; j++)
6114 {
6115 asection **secpp;
6116 bfd_vma off_adjust; /* Octets. */
6117 bool no_contents;
6118 bfd_size_type p_align;
6119 bool p_align_p;
6120
6121 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6122 number of sections with contents contributing to both p_filesz
6123 and p_memsz, followed by a number of sections with no contents
6124 that just contribute to p_memsz. In this loop, OFF tracks next
6125 available file offset for PT_LOAD and PT_NOTE segments. */
6126 m = sorted_seg_map[j];
6127 p = phdrs + m->idx;
6128 p->p_type = m->p_type;
6129 p->p_flags = m->p_flags;
6130 p_align = bed->p_align;
6131 p_align_p = false;
6132
6133 if (m->count == 0)
6134 p->p_vaddr = m->p_vaddr_offset * opb;
6135 else
6136 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6137
6138 if (m->p_paddr_valid)
6139 p->p_paddr = m->p_paddr;
6140 else if (m->count == 0)
6141 p->p_paddr = 0;
6142 else
6143 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6144
6145 if (p->p_type == PT_LOAD
6146 && (abfd->flags & D_PAGED) != 0)
6147 {
6148 /* p_align in demand paged PT_LOAD segments effectively stores
6149 the maximum page size. When copying an executable with
6150 objcopy, we set m->p_align from the input file. Use this
6151 value for maxpagesize rather than bed->maxpagesize, which
6152 may be different. Note that we use maxpagesize for PT_TLS
6153 segment alignment later in this function, so we are relying
6154 on at least one PT_LOAD segment appearing before a PT_TLS
6155 segment. */
6156 if (m->p_align_valid)
6157 maxpagesize = m->p_align;
6158 else if (p_align != 0
6159 && (link_info == NULL
6160 || !link_info->maxpagesize_is_set))
6161 /* Set p_align to the default p_align value while laying
6162 out segments aligning to the maximum page size or the
6163 largest section alignment. The run-time loader can
6164 align segments to the default p_align value or the
6165 maximum page size, depending on system page size. */
6166 p_align_p = true;
6167
6168 p->p_align = maxpagesize;
6169 }
6170 else if (m->p_align_valid)
6171 p->p_align = m->p_align;
6172 else if (m->count == 0)
6173 p->p_align = 1 << bed->s->log_file_align;
6174
6175 if (m == phdr_load_seg)
6176 {
6177 if (!m->includes_filehdr)
6178 p->p_offset = off;
6179 off += actual * bed->s->sizeof_phdr;
6180 }
6181
6182 no_contents = false;
6183 off_adjust = 0;
6184 if (p->p_type == PT_LOAD
6185 && m->count > 0)
6186 {
6187 bfd_size_type align; /* Bytes. */
6188 unsigned int align_power = 0;
6189
6190 if (m->p_align_valid)
6191 align = p->p_align;
6192 else
6193 {
6194 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6195 {
6196 unsigned int secalign;
6197
6198 secalign = bfd_section_alignment (*secpp);
6199 if (secalign > align_power)
6200 align_power = secalign;
6201 }
6202 align = (bfd_size_type) 1 << align_power;
6203 if (align < maxpagesize)
6204 {
6205 /* If a section requires alignment higher than the
6206 default p_align value, don't set p_align to the
6207 default p_align value. */
6208 if (align > p_align)
6209 p_align_p = false;
6210 align = maxpagesize;
6211 }
6212 else
6213 {
6214 /* If a section requires alignment higher than the
6215 maximum page size, set p_align to the section
6216 alignment. */
6217 p_align_p = true;
6218 p_align = align;
6219 }
6220 }
6221
6222 for (i = 0; i < m->count; i++)
6223 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6224 /* If we aren't making room for this section, then
6225 it must be SHT_NOBITS regardless of what we've
6226 set via struct bfd_elf_special_section. */
6227 elf_section_type (m->sections[i]) = SHT_NOBITS;
6228
6229 /* Find out whether this segment contains any loadable
6230 sections. */
6231 no_contents = true;
6232 for (i = 0; i < m->count; i++)
6233 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6234 {
6235 no_contents = false;
6236 break;
6237 }
6238
6239 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6240
6241 /* Broken hardware and/or kernel require that files do not
6242 map the same page with different permissions on some hppa
6243 processors. */
6244 if (j != 0
6245 && (abfd->flags & D_PAGED) != 0
6246 && bed->no_page_alias
6247 && (off & (maxpagesize - 1)) != 0
6248 && ((off & -maxpagesize)
6249 == ((off + off_adjust) & -maxpagesize)))
6250 off_adjust += maxpagesize;
6251 off += off_adjust;
6252 if (no_contents)
6253 {
6254 /* We shouldn't need to align the segment on disk since
6255 the segment doesn't need file space, but the gABI
6256 arguably requires the alignment and glibc ld.so
6257 checks it. So to comply with the alignment
6258 requirement but not waste file space, we adjust
6259 p_offset for just this segment. (OFF_ADJUST is
6260 subtracted from OFF later.) This may put p_offset
6261 past the end of file, but that shouldn't matter. */
6262 }
6263 else
6264 off_adjust = 0;
6265 }
6266 /* Make sure the .dynamic section is the first section in the
6267 PT_DYNAMIC segment. */
6268 else if (p->p_type == PT_DYNAMIC
6269 && m->count > 1
6270 && strcmp (m->sections[0]->name, ".dynamic") != 0)
6271 {
6272 _bfd_error_handler
6273 (_("%pB: The first section in the PT_DYNAMIC segment"
6274 " is not the .dynamic section"),
6275 abfd);
6276 bfd_set_error (bfd_error_bad_value);
6277 return false;
6278 }
6279 /* Set the note section type to SHT_NOTE. */
6280 else if (p->p_type == PT_NOTE)
6281 for (i = 0; i < m->count; i++)
6282 elf_section_type (m->sections[i]) = SHT_NOTE;
6283
6284 if (m->includes_filehdr)
6285 {
6286 if (!m->p_flags_valid)
6287 p->p_flags |= PF_R;
6288 p->p_filesz = bed->s->sizeof_ehdr;
6289 p->p_memsz = bed->s->sizeof_ehdr;
6290 if (p->p_type == PT_LOAD)
6291 {
6292 if (m->count > 0)
6293 {
6294 if (p->p_vaddr < (bfd_vma) off
6295 || (!m->p_paddr_valid
6296 && p->p_paddr < (bfd_vma) off))
6297 {
6298 _bfd_error_handler
6299 (_("%pB: not enough room for program headers,"
6300 " try linking with -N"),
6301 abfd);
6302 bfd_set_error (bfd_error_bad_value);
6303 return false;
6304 }
6305 p->p_vaddr -= off;
6306 if (!m->p_paddr_valid)
6307 p->p_paddr -= off;
6308 }
6309 }
6310 else if (sorted_seg_map[0]->includes_filehdr)
6311 {
6312 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6313 p->p_vaddr = filehdr->p_vaddr;
6314 if (!m->p_paddr_valid)
6315 p->p_paddr = filehdr->p_paddr;
6316 }
6317 }
6318
6319 if (m->includes_phdrs)
6320 {
6321 if (!m->p_flags_valid)
6322 p->p_flags |= PF_R;
6323 p->p_filesz += actual * bed->s->sizeof_phdr;
6324 p->p_memsz += actual * bed->s->sizeof_phdr;
6325 if (!m->includes_filehdr)
6326 {
6327 if (p->p_type == PT_LOAD)
6328 {
6329 elf_elfheader (abfd)->e_phoff = p->p_offset;
6330 if (m->count > 0)
6331 {
6332 p->p_vaddr -= off - p->p_offset;
6333 if (!m->p_paddr_valid)
6334 p->p_paddr -= off - p->p_offset;
6335 }
6336 }
6337 else if (phdr_load_seg != NULL)
6338 {
6339 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6340 bfd_vma phdr_off = 0; /* Octets. */
6341 if (phdr_load_seg->includes_filehdr)
6342 phdr_off = bed->s->sizeof_ehdr;
6343 p->p_vaddr = phdr->p_vaddr + phdr_off;
6344 if (!m->p_paddr_valid)
6345 p->p_paddr = phdr->p_paddr + phdr_off;
6346 p->p_offset = phdr->p_offset + phdr_off;
6347 }
6348 else
6349 p->p_offset = bed->s->sizeof_ehdr;
6350 }
6351 }
6352
6353 if (p->p_type == PT_LOAD
6354 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6355 {
6356 if (!m->includes_filehdr && !m->includes_phdrs)
6357 {
6358 p->p_offset = off;
6359 if (no_contents)
6360 {
6361 /* Put meaningless p_offset for PT_LOAD segments
6362 without file contents somewhere within the first
6363 page, in an attempt to not point past EOF. */
6364 bfd_size_type align = maxpagesize;
6365 if (align < p->p_align)
6366 align = p->p_align;
6367 if (align < 1)
6368 align = 1;
6369 p->p_offset = off % align;
6370 }
6371 }
6372 else
6373 {
6374 file_ptr adjust; /* Octets. */
6375
6376 adjust = off - (p->p_offset + p->p_filesz);
6377 if (!no_contents)
6378 p->p_filesz += adjust;
6379 p->p_memsz += adjust;
6380 }
6381 }
6382
6383 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6384 maps. Set filepos for sections in PT_LOAD segments, and in
6385 core files, for sections in PT_NOTE segments.
6386 assign_file_positions_for_non_load_sections will set filepos
6387 for other sections and update p_filesz for other segments. */
6388 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6389 {
6390 asection *sec;
6391 bfd_size_type align;
6392 Elf_Internal_Shdr *this_hdr;
6393
6394 sec = *secpp;
6395 this_hdr = &elf_section_data (sec)->this_hdr;
6396 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6397
6398 if ((p->p_type == PT_LOAD
6399 || p->p_type == PT_TLS)
6400 && (this_hdr->sh_type != SHT_NOBITS
6401 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6402 && ((this_hdr->sh_flags & SHF_TLS) == 0
6403 || p->p_type == PT_TLS))))
6404 {
6405 bfd_vma p_start = p->p_paddr; /* Octets. */
6406 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
6407 bfd_vma s_start = sec->lma * opb; /* Octets. */
6408 bfd_vma adjust = s_start - p_end; /* Octets. */
6409
6410 if (adjust != 0
6411 && (s_start < p_end
6412 || p_end < p_start))
6413 {
6414 _bfd_error_handler
6415 /* xgettext:c-format */
6416 (_("%pB: section %pA lma %#" PRIx64
6417 " adjusted to %#" PRIx64),
6418 abfd, sec, (uint64_t) s_start / opb,
6419 (uint64_t) p_end / opb);
6420 adjust = 0;
6421 sec->lma = p_end / opb;
6422 }
6423 p->p_memsz += adjust;
6424
6425 if (p->p_type == PT_LOAD)
6426 {
6427 if (this_hdr->sh_type != SHT_NOBITS)
6428 {
6429 off_adjust = 0;
6430 if (p->p_filesz + adjust < p->p_memsz)
6431 {
6432 /* We have a PROGBITS section following NOBITS ones.
6433 Allocate file space for the NOBITS section(s).
6434 We don't need to write out the zeros, posix
6435 fseek past the end of data already written
6436 followed by a write at that location is
6437 guaranteed to result in zeros being read
6438 from the gap. */
6439 adjust = p->p_memsz - p->p_filesz;
6440 }
6441 }
6442 /* We only adjust sh_offset in SHT_NOBITS sections
6443 as would seem proper for their address when the
6444 section is first in the segment. sh_offset
6445 doesn't really have any significance for
6446 SHT_NOBITS anyway, apart from a notional position
6447 relative to other sections. Historically we
6448 didn't bother with adjusting sh_offset and some
6449 programs depend on it not being adjusted. See
6450 pr12921 and pr25662. */
6451 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6452 {
6453 off += adjust;
6454 if (this_hdr->sh_type == SHT_NOBITS)
6455 off_adjust += adjust;
6456 }
6457 }
6458 if (this_hdr->sh_type != SHT_NOBITS)
6459 p->p_filesz += adjust;
6460 }
6461
6462 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6463 {
6464 /* The section at i == 0 is the one that actually contains
6465 everything. */
6466 if (i == 0)
6467 {
6468 this_hdr->sh_offset = sec->filepos = off;
6469 off += this_hdr->sh_size;
6470 p->p_filesz = this_hdr->sh_size;
6471 p->p_memsz = 0;
6472 p->p_align = 1;
6473 }
6474 else
6475 {
6476 /* The rest are fake sections that shouldn't be written. */
6477 sec->filepos = 0;
6478 sec->size = 0;
6479 sec->flags = 0;
6480 continue;
6481 }
6482 }
6483 else
6484 {
6485 if (p->p_type == PT_LOAD)
6486 {
6487 this_hdr->sh_offset = sec->filepos = off;
6488 if (this_hdr->sh_type != SHT_NOBITS)
6489 off += this_hdr->sh_size;
6490 }
6491 else if (this_hdr->sh_type == SHT_NOBITS
6492 && (this_hdr->sh_flags & SHF_TLS) != 0
6493 && this_hdr->sh_offset == 0)
6494 {
6495 /* This is a .tbss section that didn't get a PT_LOAD.
6496 (See _bfd_elf_map_sections_to_segments "Create a
6497 final PT_LOAD".) Set sh_offset to the value it
6498 would have if we had created a zero p_filesz and
6499 p_memsz PT_LOAD header for the section. This
6500 also makes the PT_TLS header have the same
6501 p_offset value. */
6502 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6503 off, align);
6504 this_hdr->sh_offset = sec->filepos = off + adjust;
6505 }
6506
6507 if (this_hdr->sh_type != SHT_NOBITS)
6508 {
6509 p->p_filesz += this_hdr->sh_size;
6510 /* A load section without SHF_ALLOC is something like
6511 a note section in a PT_NOTE segment. These take
6512 file space but are not loaded into memory. */
6513 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6514 p->p_memsz += this_hdr->sh_size;
6515 }
6516 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6517 {
6518 if (p->p_type == PT_TLS)
6519 p->p_memsz += this_hdr->sh_size;
6520
6521 /* .tbss is special. It doesn't contribute to p_memsz of
6522 normal segments. */
6523 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6524 p->p_memsz += this_hdr->sh_size;
6525 }
6526
6527 if (align > p->p_align
6528 && !m->p_align_valid
6529 && (p->p_type != PT_LOAD
6530 || (abfd->flags & D_PAGED) == 0))
6531 p->p_align = align;
6532 }
6533
6534 if (!m->p_flags_valid)
6535 {
6536 p->p_flags |= PF_R;
6537 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6538 p->p_flags |= PF_X;
6539 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6540 p->p_flags |= PF_W;
6541 }
6542 }
6543
6544 off -= off_adjust;
6545
6546 /* PR ld/20815 - Check that the program header segment, if
6547 present, will be loaded into memory. */
6548 if (p->p_type == PT_PHDR
6549 && phdr_load_seg == NULL
6550 && !(bed->elf_backend_allow_non_load_phdr != NULL
6551 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6552 {
6553 /* The fix for this error is usually to edit the linker script being
6554 used and set up the program headers manually. Either that or
6555 leave room for the headers at the start of the SECTIONS. */
6556 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6557 " by LOAD segment"),
6558 abfd);
6559 if (link_info == NULL)
6560 return false;
6561 /* Arrange for the linker to exit with an error, deleting
6562 the output file unless --noinhibit-exec is given. */
6563 link_info->callbacks->info ("%X");
6564 }
6565
6566 /* Check that all sections are in a PT_LOAD segment.
6567 Don't check funky gdb generated core files. */
6568 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6569 {
6570 bool check_vma = true;
6571
6572 for (i = 1; i < m->count; i++)
6573 if (m->sections[i]->vma == m->sections[i - 1]->vma
6574 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6575 ->this_hdr), p) != 0
6576 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6577 ->this_hdr), p) != 0)
6578 {
6579 /* Looks like we have overlays packed into the segment. */
6580 check_vma = false;
6581 break;
6582 }
6583
6584 for (i = 0; i < m->count; i++)
6585 {
6586 Elf_Internal_Shdr *this_hdr;
6587 asection *sec;
6588
6589 sec = m->sections[i];
6590 this_hdr = &(elf_section_data(sec)->this_hdr);
6591 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6592 && !ELF_TBSS_SPECIAL (this_hdr, p))
6593 {
6594 _bfd_error_handler
6595 /* xgettext:c-format */
6596 (_("%pB: section `%pA' can't be allocated in segment %d"),
6597 abfd, sec, j);
6598 print_segment_map (m);
6599 }
6600 }
6601
6602 if (p_align_p)
6603 p->p_align = p_align;
6604 }
6605 }
6606
6607 elf_next_file_pos (abfd) = off;
6608
6609 if (link_info != NULL
6610 && phdr_load_seg != NULL
6611 && phdr_load_seg->includes_filehdr)
6612 {
6613 /* There is a segment that contains both the file headers and the
6614 program headers, so provide a symbol __ehdr_start pointing there.
6615 A program can use this to examine itself robustly. */
6616
6617 struct elf_link_hash_entry *hash
6618 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6619 false, false, true);
6620 /* If the symbol was referenced and not defined, define it. */
6621 if (hash != NULL
6622 && (hash->root.type == bfd_link_hash_new
6623 || hash->root.type == bfd_link_hash_undefined
6624 || hash->root.type == bfd_link_hash_undefweak
6625 || hash->root.type == bfd_link_hash_common))
6626 {
6627 asection *s = NULL;
6628 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6629
6630 if (phdr_load_seg->count != 0)
6631 /* The segment contains sections, so use the first one. */
6632 s = phdr_load_seg->sections[0];
6633 else
6634 /* Use the first (i.e. lowest-addressed) section in any segment. */
6635 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6636 if (m->p_type == PT_LOAD && m->count != 0)
6637 {
6638 s = m->sections[0];
6639 break;
6640 }
6641
6642 if (s != NULL)
6643 {
6644 hash->root.u.def.value = filehdr_vaddr - s->vma;
6645 hash->root.u.def.section = s;
6646 }
6647 else
6648 {
6649 hash->root.u.def.value = filehdr_vaddr;
6650 hash->root.u.def.section = bfd_abs_section_ptr;
6651 }
6652
6653 hash->root.type = bfd_link_hash_defined;
6654 hash->def_regular = 1;
6655 hash->non_elf = 0;
6656 }
6657 }
6658
6659 return true;
6660 }
6661
6662 /* Determine if a bfd is a debuginfo file. Unfortunately there
6663 is no defined method for detecting such files, so we have to
6664 use heuristics instead. */
6665
6666 bool
6667 is_debuginfo_file (bfd *abfd)
6668 {
6669 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6670 return false;
6671
6672 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6673 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6674 Elf_Internal_Shdr **headerp;
6675
6676 for (headerp = start_headers; headerp < end_headers; headerp ++)
6677 {
6678 Elf_Internal_Shdr *header = * headerp;
6679
6680 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6681 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6682 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6683 && header->sh_type != SHT_NOBITS
6684 && header->sh_type != SHT_NOTE)
6685 return false;
6686 }
6687
6688 return true;
6689 }
6690
6691 /* Assign file positions for other sections, except for compressed debug
6692 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6693
6694 static bool
6695 assign_file_positions_for_non_load_sections (bfd *abfd,
6696 struct bfd_link_info *link_info)
6697 {
6698 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6699 Elf_Internal_Shdr **i_shdrpp;
6700 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6701 Elf_Internal_Phdr *phdrs;
6702 Elf_Internal_Phdr *p;
6703 struct elf_segment_map *m;
6704 file_ptr off;
6705 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6706 bfd_vma maxpagesize;
6707
6708 if (link_info != NULL)
6709 maxpagesize = link_info->maxpagesize;
6710 else
6711 maxpagesize = bed->maxpagesize;
6712 i_shdrpp = elf_elfsections (abfd);
6713 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6714 off = elf_next_file_pos (abfd);
6715 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6716 {
6717 Elf_Internal_Shdr *hdr;
6718 bfd_vma align;
6719
6720 hdr = *hdrpp;
6721 if (hdr->bfd_section != NULL
6722 && (hdr->bfd_section->filepos != 0
6723 || (hdr->sh_type == SHT_NOBITS
6724 && hdr->contents == NULL)))
6725 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6726 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6727 {
6728 if (hdr->sh_size != 0
6729 /* PR 24717 - debuginfo files are known to be not strictly
6730 compliant with the ELF standard. In particular they often
6731 have .note.gnu.property sections that are outside of any
6732 loadable segment. This is not a problem for such files,
6733 so do not warn about them. */
6734 && ! is_debuginfo_file (abfd))
6735 _bfd_error_handler
6736 /* xgettext:c-format */
6737 (_("%pB: warning: allocated section `%s' not in segment"),
6738 abfd,
6739 (hdr->bfd_section == NULL
6740 ? "*unknown*"
6741 : hdr->bfd_section->name));
6742 /* We don't need to page align empty sections. */
6743 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6744 align = maxpagesize;
6745 else
6746 align = hdr->sh_addralign & -hdr->sh_addralign;
6747 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6748 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6749 false);
6750 }
6751 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6752 && hdr->bfd_section == NULL)
6753 /* We don't know the offset of these sections yet:
6754 their size has not been decided. */
6755 || (abfd->is_linker_output
6756 && hdr->bfd_section != NULL
6757 && (hdr->sh_name == -1u
6758 || bfd_section_is_ctf (hdr->bfd_section)))
6759 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6760 || (elf_symtab_shndx_list (abfd) != NULL
6761 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6762 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6763 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6764 hdr->sh_offset = -1;
6765 else
6766 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6767 }
6768 elf_next_file_pos (abfd) = off;
6769
6770 /* Now that we have set the section file positions, we can set up
6771 the file positions for the non PT_LOAD segments. */
6772 phdrs = elf_tdata (abfd)->phdr;
6773 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6774 {
6775 if (p->p_type == PT_GNU_RELRO)
6776 {
6777 bfd_vma start, end; /* Bytes. */
6778 bool ok;
6779
6780 if (link_info != NULL)
6781 {
6782 /* During linking the range of the RELRO segment is passed
6783 in link_info. Note that there may be padding between
6784 relro_start and the first RELRO section. */
6785 start = link_info->relro_start;
6786 end = link_info->relro_end;
6787 }
6788 else if (m->count != 0)
6789 {
6790 if (!m->p_size_valid)
6791 abort ();
6792 start = m->sections[0]->vma;
6793 end = start + m->p_size / opb;
6794 }
6795 else
6796 {
6797 start = 0;
6798 end = 0;
6799 }
6800
6801 ok = false;
6802 if (start < end)
6803 {
6804 struct elf_segment_map *lm;
6805 const Elf_Internal_Phdr *lp;
6806 unsigned int i;
6807
6808 /* Find a LOAD segment containing a section in the RELRO
6809 segment. */
6810 for (lm = elf_seg_map (abfd), lp = phdrs;
6811 lm != NULL;
6812 lm = lm->next, lp++)
6813 {
6814 if (lp->p_type == PT_LOAD
6815 && lm->count != 0
6816 && (lm->sections[lm->count - 1]->vma
6817 + (!IS_TBSS (lm->sections[lm->count - 1])
6818 ? lm->sections[lm->count - 1]->size / opb
6819 : 0)) > start
6820 && lm->sections[0]->vma < end)
6821 break;
6822 }
6823
6824 if (lm != NULL)
6825 {
6826 /* Find the section starting the RELRO segment. */
6827 for (i = 0; i < lm->count; i++)
6828 {
6829 asection *s = lm->sections[i];
6830 if (s->vma >= start
6831 && s->vma < end
6832 && s->size != 0)
6833 break;
6834 }
6835
6836 if (i < lm->count)
6837 {
6838 p->p_vaddr = lm->sections[i]->vma * opb;
6839 p->p_paddr = lm->sections[i]->lma * opb;
6840 p->p_offset = lm->sections[i]->filepos;
6841 p->p_memsz = end * opb - p->p_vaddr;
6842 p->p_filesz = p->p_memsz;
6843
6844 /* The RELRO segment typically ends a few bytes
6845 into .got.plt but other layouts are possible.
6846 In cases where the end does not match any
6847 loaded section (for instance is in file
6848 padding), trim p_filesz back to correspond to
6849 the end of loaded section contents. */
6850 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6851 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6852
6853 /* Preserve the alignment and flags if they are
6854 valid. The gold linker generates RW/4 for
6855 the PT_GNU_RELRO section. It is better for
6856 objcopy/strip to honor these attributes
6857 otherwise gdb will choke when using separate
6858 debug files. */
6859 if (!m->p_align_valid)
6860 p->p_align = 1;
6861 if (!m->p_flags_valid)
6862 p->p_flags = PF_R;
6863 ok = true;
6864 }
6865 }
6866 }
6867
6868 if (!ok)
6869 {
6870 if (link_info != NULL)
6871 _bfd_error_handler
6872 (_("%pB: warning: unable to allocate any sections"
6873 " to PT_GNU_RELRO segment"),
6874 abfd);
6875 memset (p, 0, sizeof *p);
6876 }
6877 }
6878 else if (p->p_type == PT_GNU_STACK)
6879 {
6880 if (m->p_size_valid)
6881 p->p_memsz = m->p_size;
6882 }
6883 else if (m->count != 0)
6884 {
6885 unsigned int i;
6886
6887 if (p->p_type != PT_LOAD
6888 && (p->p_type != PT_NOTE
6889 || bfd_get_format (abfd) != bfd_core))
6890 {
6891 /* A user specified segment layout may include a PHDR
6892 segment that overlaps with a LOAD segment... */
6893 if (p->p_type == PT_PHDR)
6894 {
6895 m->count = 0;
6896 continue;
6897 }
6898
6899 if (m->includes_filehdr || m->includes_phdrs)
6900 {
6901 /* PR 17512: file: 2195325e. */
6902 _bfd_error_handler
6903 (_("%pB: error: non-load segment %d includes file header "
6904 "and/or program header"),
6905 abfd, (int) (p - phdrs));
6906 return false;
6907 }
6908
6909 p->p_filesz = 0;
6910 p->p_offset = m->sections[0]->filepos;
6911 for (i = m->count; i-- != 0;)
6912 {
6913 asection *sect = m->sections[i];
6914 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6915 if (hdr->sh_type != SHT_NOBITS)
6916 {
6917 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6918 /* NB: p_memsz of the loadable PT_NOTE segment
6919 should be the same as p_filesz. */
6920 if (p->p_type == PT_NOTE
6921 && (hdr->sh_flags & SHF_ALLOC) != 0)
6922 p->p_memsz = p->p_filesz;
6923 break;
6924 }
6925 }
6926 }
6927 }
6928 }
6929
6930 return true;
6931 }
6932
6933 static elf_section_list *
6934 find_section_in_list (unsigned int i, elf_section_list * list)
6935 {
6936 for (;list != NULL; list = list->next)
6937 if (list->ndx == i)
6938 break;
6939 return list;
6940 }
6941
6942 /* Work out the file positions of all the sections. This is called by
6943 _bfd_elf_compute_section_file_positions. All the section sizes and
6944 VMAs must be known before this is called.
6945
6946 Reloc sections come in two flavours: Those processed specially as
6947 "side-channel" data attached to a section to which they apply, and
6948 those that bfd doesn't process as relocations. The latter sort are
6949 stored in a normal bfd section by bfd_section_from_shdr. We don't
6950 consider the former sort here, unless they form part of the loadable
6951 image. Reloc sections not assigned here (and compressed debugging
6952 sections and CTF sections which nothing else in the file can rely
6953 upon) will be handled later by assign_file_positions_for_relocs.
6954
6955 We also don't set the positions of the .symtab and .strtab here. */
6956
6957 static bool
6958 assign_file_positions_except_relocs (bfd *abfd,
6959 struct bfd_link_info *link_info)
6960 {
6961 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6962 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6963 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6964 unsigned int alloc;
6965
6966 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6967 && bfd_get_format (abfd) != bfd_core)
6968 {
6969 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6970 unsigned int num_sec = elf_numsections (abfd);
6971 Elf_Internal_Shdr **hdrpp;
6972 unsigned int i;
6973 file_ptr off;
6974
6975 /* Start after the ELF header. */
6976 off = i_ehdrp->e_ehsize;
6977
6978 /* We are not creating an executable, which means that we are
6979 not creating a program header, and that the actual order of
6980 the sections in the file is unimportant. */
6981 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6982 {
6983 Elf_Internal_Shdr *hdr;
6984
6985 hdr = *hdrpp;
6986 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6987 && hdr->bfd_section == NULL)
6988 /* Do not assign offsets for these sections yet: we don't know
6989 their sizes. */
6990 || (abfd->is_linker_output
6991 && hdr->bfd_section != NULL
6992 && (hdr->sh_name == -1u
6993 || bfd_section_is_ctf (hdr->bfd_section)))
6994 || i == elf_onesymtab (abfd)
6995 || (elf_symtab_shndx_list (abfd) != NULL
6996 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6997 || i == elf_strtab_sec (abfd)
6998 || i == elf_shstrtab_sec (abfd))
6999 {
7000 hdr->sh_offset = -1;
7001 }
7002 else
7003 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
7004 }
7005
7006 elf_next_file_pos (abfd) = off;
7007 elf_program_header_size (abfd) = 0;
7008 }
7009 else
7010 {
7011 /* Assign file positions for the loaded sections based on the
7012 assignment of sections to segments. */
7013 if (!assign_file_positions_for_load_sections (abfd, link_info))
7014 return false;
7015
7016 /* And for non-load sections. */
7017 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
7018 return false;
7019 }
7020
7021 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
7022 return false;
7023
7024 /* Write out the program headers. */
7025 alloc = i_ehdrp->e_phnum;
7026 if (alloc != 0)
7027 {
7028 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
7029 {
7030 bool warned_tls = false;
7031 bool warned_rwx = false;
7032
7033 /* Memory resident segments with non-zero size and RWX
7034 permissions are a security risk, so we generate a warning
7035 here if we are creating any. */
7036 unsigned int i;
7037
7038 for (i = 0; i < alloc; i++)
7039 {
7040 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
7041
7042 if (phdr->p_memsz == 0)
7043 continue;
7044
7045 if (! warned_tls
7046 && phdr->p_type == PT_TLS
7047 && (phdr->p_flags & PF_X))
7048 {
7049 if (link_info->warn_is_error_for_rwx_segments)
7050 {
7051 _bfd_error_handler (_("\
7052 error: %pB has a TLS segment with execute permission"),
7053 abfd);
7054 return false;
7055 }
7056
7057 _bfd_error_handler (_("\
7058 warning: %pB has a TLS segment with execute permission"),
7059 abfd);
7060 if (warned_rwx)
7061 break;
7062
7063 warned_tls = true;
7064 }
7065 else if (! warned_rwx
7066 && phdr->p_type == PT_LOAD
7067 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
7068 == (PF_R | PF_W | PF_X)))
7069 {
7070 if (link_info->warn_is_error_for_rwx_segments)
7071 {
7072 _bfd_error_handler (_("\
7073 error: %pB has a LOAD segment with RWX permissions"),
7074 abfd);
7075 return false;
7076 }
7077
7078 _bfd_error_handler (_("\
7079 warning: %pB has a LOAD segment with RWX permissions"),
7080 abfd);
7081 if (warned_tls)
7082 break;
7083
7084 warned_rwx = true;
7085 }
7086 }
7087 }
7088
7089 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7090 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7091 return false;
7092 }
7093
7094 return true;
7095 }
7096
7097 bool
7098 _bfd_elf_init_file_header (bfd *abfd,
7099 struct bfd_link_info *info ATTRIBUTE_UNUSED)
7100 {
7101 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
7102 struct elf_strtab_hash *shstrtab;
7103 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7104
7105 i_ehdrp = elf_elfheader (abfd);
7106
7107 shstrtab = _bfd_elf_strtab_init ();
7108 if (shstrtab == NULL)
7109 return false;
7110
7111 elf_shstrtab (abfd) = shstrtab;
7112
7113 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7114 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7115 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7116 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7117
7118 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7119 i_ehdrp->e_ident[EI_DATA] =
7120 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7121 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7122
7123 if ((abfd->flags & DYNAMIC) != 0)
7124 i_ehdrp->e_type = ET_DYN;
7125 else if ((abfd->flags & EXEC_P) != 0)
7126 i_ehdrp->e_type = ET_EXEC;
7127 else if (bfd_get_format (abfd) == bfd_core)
7128 i_ehdrp->e_type = ET_CORE;
7129 else
7130 i_ehdrp->e_type = ET_REL;
7131
7132 switch (bfd_get_arch (abfd))
7133 {
7134 case bfd_arch_unknown:
7135 i_ehdrp->e_machine = EM_NONE;
7136 break;
7137
7138 /* There used to be a long list of cases here, each one setting
7139 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7140 in the corresponding bfd definition. To avoid duplication,
7141 the switch was removed. Machines that need special handling
7142 can generally do it in elf_backend_final_write_processing(),
7143 unless they need the information earlier than the final write.
7144 Such need can generally be supplied by replacing the tests for
7145 e_machine with the conditions used to determine it. */
7146 default:
7147 i_ehdrp->e_machine = bed->elf_machine_code;
7148 }
7149
7150 i_ehdrp->e_version = bed->s->ev_current;
7151 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7152
7153 /* No program header, for now. */
7154 i_ehdrp->e_phoff = 0;
7155 i_ehdrp->e_phentsize = 0;
7156 i_ehdrp->e_phnum = 0;
7157
7158 /* Each bfd section is section header entry. */
7159 i_ehdrp->e_entry = bfd_get_start_address (abfd);
7160 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7161
7162 elf_tdata (abfd)->symtab_hdr.sh_name =
7163 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7164 elf_tdata (abfd)->strtab_hdr.sh_name =
7165 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7166 elf_tdata (abfd)->shstrtab_hdr.sh_name =
7167 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7168 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7169 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7170 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7171 return false;
7172
7173 return true;
7174 }
7175
7176 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7177
7178 FIXME: We used to have code here to sort the PT_LOAD segments into
7179 ascending order, as per the ELF spec. But this breaks some programs,
7180 including the Linux kernel. But really either the spec should be
7181 changed or the programs updated. */
7182
7183 bool
7184 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7185 {
7186 if (link_info != NULL && bfd_link_pie (link_info))
7187 {
7188 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7189 unsigned int num_segments = i_ehdrp->e_phnum;
7190 struct elf_obj_tdata *tdata = elf_tdata (obfd);
7191 Elf_Internal_Phdr *segment = tdata->phdr;
7192 Elf_Internal_Phdr *end_segment = &segment[num_segments];
7193
7194 /* Find the lowest p_vaddr in PT_LOAD segments. */
7195 bfd_vma p_vaddr = (bfd_vma) -1;
7196 for (; segment < end_segment; segment++)
7197 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7198 p_vaddr = segment->p_vaddr;
7199
7200 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7201 segments is non-zero. */
7202 if (p_vaddr)
7203 i_ehdrp->e_type = ET_EXEC;
7204 }
7205 return true;
7206 }
7207
7208 /* Assign file positions for all the reloc sections which are not part
7209 of the loadable file image, and the file position of section headers. */
7210
7211 static bool
7212 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7213 {
7214 file_ptr off;
7215 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7216 Elf_Internal_Shdr *shdrp;
7217 Elf_Internal_Ehdr *i_ehdrp;
7218 const struct elf_backend_data *bed;
7219
7220 /* Skip non-load sections without section header. */
7221 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7222 return true;
7223
7224 off = elf_next_file_pos (abfd);
7225
7226 shdrpp = elf_elfsections (abfd);
7227 end_shdrpp = shdrpp + elf_numsections (abfd);
7228 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7229 {
7230 shdrp = *shdrpp;
7231 if (shdrp->sh_offset == -1)
7232 {
7233 asection *sec = shdrp->bfd_section;
7234 if (sec == NULL
7235 || shdrp->sh_type == SHT_REL
7236 || shdrp->sh_type == SHT_RELA)
7237 ;
7238 else if (bfd_section_is_ctf (sec))
7239 {
7240 /* Update section size and contents. */
7241 shdrp->sh_size = sec->size;
7242 shdrp->contents = sec->contents;
7243 }
7244 else if (shdrp->sh_name == -1u)
7245 {
7246 const char *name = sec->name;
7247 struct bfd_elf_section_data *d;
7248
7249 /* Compress DWARF debug sections. */
7250 if (!bfd_compress_section (abfd, sec, shdrp->contents))
7251 return false;
7252
7253 if (sec->compress_status == COMPRESS_SECTION_DONE
7254 && (abfd->flags & BFD_COMPRESS_GABI) == 0
7255 && name[1] == 'd')
7256 {
7257 /* If section is compressed with zlib-gnu, convert
7258 section name from .debug_* to .zdebug_*. */
7259 char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7260 if (new_name == NULL)
7261 return false;
7262 name = new_name;
7263 }
7264 /* Add section name to section name section. */
7265 shdrp->sh_name
7266 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7267 name, false);
7268 d = elf_section_data (sec);
7269
7270 /* Add reloc section name to section name section. */
7271 if (d->rel.hdr
7272 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7273 name, false))
7274 return false;
7275 if (d->rela.hdr
7276 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7277 name, true))
7278 return false;
7279
7280 /* Update section size and contents. */
7281 shdrp->sh_size = sec->size;
7282 shdrp->contents = sec->contents;
7283 sec->contents = NULL;
7284 }
7285
7286 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7287 }
7288 }
7289
7290 /* Place section name section after DWARF debug sections have been
7291 compressed. */
7292 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7293 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7294 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7295 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
7296
7297 /* Place the section headers. */
7298 i_ehdrp = elf_elfheader (abfd);
7299 bed = get_elf_backend_data (abfd);
7300 off = align_file_position (off, 1 << bed->s->log_file_align);
7301 i_ehdrp->e_shoff = off;
7302 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7303 elf_next_file_pos (abfd) = off;
7304
7305 return true;
7306 }
7307
7308 bool
7309 _bfd_elf_write_object_contents (bfd *abfd)
7310 {
7311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7312 Elf_Internal_Shdr **i_shdrp;
7313 bool failed;
7314 unsigned int count, num_sec;
7315 struct elf_obj_tdata *t;
7316
7317 if (! abfd->output_has_begun
7318 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7319 return false;
7320 /* Do not rewrite ELF data when the BFD has been opened for update.
7321 abfd->output_has_begun was set to TRUE on opening, so creation of
7322 new sections, and modification of existing section sizes was
7323 restricted. This means the ELF header, program headers and
7324 section headers can't have changed. If the contents of any
7325 sections has been modified, then those changes have already been
7326 written to the BFD. */
7327 else if (abfd->direction == both_direction)
7328 {
7329 BFD_ASSERT (abfd->output_has_begun);
7330 return true;
7331 }
7332
7333 i_shdrp = elf_elfsections (abfd);
7334
7335 failed = false;
7336 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7337 if (failed)
7338 return false;
7339
7340 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7341 return false;
7342
7343 /* After writing the headers, we need to write the sections too... */
7344 num_sec = elf_numsections (abfd);
7345 for (count = 1; count < num_sec; count++)
7346 {
7347 /* Don't set the sh_name field without section header. */
7348 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7349 i_shdrp[count]->sh_name
7350 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7351 i_shdrp[count]->sh_name);
7352 if (bed->elf_backend_section_processing)
7353 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7354 return false;
7355 if (i_shdrp[count]->contents)
7356 {
7357 bfd_size_type amt = i_shdrp[count]->sh_size;
7358
7359 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7360 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7361 return false;
7362 }
7363 }
7364
7365 /* Write out the section header names. */
7366 t = elf_tdata (abfd);
7367 if (elf_shstrtab (abfd) != NULL
7368 && t->shstrtab_hdr.sh_offset != -1
7369 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7370 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7371 return false;
7372
7373 if (!(*bed->elf_backend_final_write_processing) (abfd))
7374 return false;
7375
7376 if (!bed->s->write_shdrs_and_ehdr (abfd))
7377 return false;
7378
7379 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
7380 if (t->o->build_id.after_write_object_contents != NULL
7381 && !(*t->o->build_id.after_write_object_contents) (abfd))
7382 return false;
7383 if (t->o->package_metadata.after_write_object_contents != NULL
7384 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7385 return false;
7386
7387 return true;
7388 }
7389
7390 bool
7391 _bfd_elf_write_corefile_contents (bfd *abfd)
7392 {
7393 /* Hopefully this can be done just like an object file. */
7394 return _bfd_elf_write_object_contents (abfd);
7395 }
7396
7397 /* Given a section, search the header to find them. */
7398
7399 unsigned int
7400 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7401 {
7402 const struct elf_backend_data *bed;
7403 unsigned int sec_index;
7404
7405 if (elf_section_data (asect) != NULL
7406 && elf_section_data (asect)->this_idx != 0)
7407 return elf_section_data (asect)->this_idx;
7408
7409 if (bfd_is_abs_section (asect))
7410 sec_index = SHN_ABS;
7411 else if (bfd_is_com_section (asect))
7412 sec_index = SHN_COMMON;
7413 else if (bfd_is_und_section (asect))
7414 sec_index = SHN_UNDEF;
7415 else
7416 sec_index = SHN_BAD;
7417
7418 bed = get_elf_backend_data (abfd);
7419 if (bed->elf_backend_section_from_bfd_section)
7420 {
7421 int retval = sec_index;
7422
7423 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7424 return retval;
7425 }
7426
7427 if (sec_index == SHN_BAD)
7428 bfd_set_error (bfd_error_nonrepresentable_section);
7429
7430 return sec_index;
7431 }
7432
7433 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
7434 on error. */
7435
7436 int
7437 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7438 {
7439 asymbol *asym_ptr = *asym_ptr_ptr;
7440 int idx;
7441 flagword flags = asym_ptr->flags;
7442
7443 /* When gas creates relocations against local labels, it creates its
7444 own symbol for the section, but does put the symbol into the
7445 symbol chain, so udata is 0. When the linker is generating
7446 relocatable output, this section symbol may be for one of the
7447 input sections rather than the output section. */
7448 if (asym_ptr->udata.i == 0
7449 && (flags & BSF_SECTION_SYM)
7450 && asym_ptr->section)
7451 {
7452 asection *sec;
7453
7454 sec = asym_ptr->section;
7455 if (sec->owner != abfd && sec->output_section != NULL)
7456 sec = sec->output_section;
7457 if (sec->owner == abfd
7458 && sec->index < elf_num_section_syms (abfd)
7459 && elf_section_syms (abfd)[sec->index] != NULL)
7460 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7461 }
7462
7463 idx = asym_ptr->udata.i;
7464
7465 if (idx == 0)
7466 {
7467 /* This case can occur when using --strip-symbol on a symbol
7468 which is used in a relocation entry. */
7469 _bfd_error_handler
7470 /* xgettext:c-format */
7471 (_("%pB: symbol `%s' required but not present"),
7472 abfd, bfd_asymbol_name (asym_ptr));
7473 bfd_set_error (bfd_error_no_symbols);
7474 return -1;
7475 }
7476
7477 #if DEBUG & 4
7478 {
7479 fprintf (stderr,
7480 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7481 " flags = 0x%.8x\n",
7482 (long) asym_ptr, asym_ptr->name, idx, flags);
7483 fflush (stderr);
7484 }
7485 #endif
7486
7487 return idx;
7488 }
7489
7490 static inline bfd_vma
7491 segment_size (Elf_Internal_Phdr *segment)
7492 {
7493 return (segment->p_memsz > segment->p_filesz
7494 ? segment->p_memsz : segment->p_filesz);
7495 }
7496
7497
7498 /* Returns the end address of the segment + 1. */
7499 static inline bfd_vma
7500 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7501 {
7502 return start + segment_size (segment);
7503 }
7504
7505 static inline bfd_size_type
7506 section_size (asection *section, Elf_Internal_Phdr *segment)
7507 {
7508 if ((section->flags & SEC_HAS_CONTENTS) != 0
7509 || (section->flags & SEC_THREAD_LOCAL) == 0
7510 || segment->p_type == PT_TLS)
7511 return section->size;
7512 return 0;
7513 }
7514
7515 /* Returns TRUE if the given section is contained within the given
7516 segment. LMA addresses are compared against PADDR when
7517 USE_VADDR is false, VMA against VADDR when true. */
7518 static bool
7519 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7520 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7521 bool use_vaddr)
7522 {
7523 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7524 bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7525 bfd_vma octet;
7526 if (_bfd_mul_overflow (addr, opb, &octet))
7527 return false;
7528 /* The third and fourth lines below are testing that the section end
7529 address is within the segment. It's written this way to avoid
7530 overflow. Add seg_addr + section_size to both sides of the
7531 inequality to make it obvious. */
7532 return (octet >= seg_addr
7533 && segment_size (segment) >= section_size (section, segment)
7534 && (octet - seg_addr
7535 <= segment_size (segment) - section_size (section, segment)));
7536 }
7537
7538 /* Handle PT_NOTE segment. */
7539 static bool
7540 is_note (asection *s, Elf_Internal_Phdr *p)
7541 {
7542 return (p->p_type == PT_NOTE
7543 && elf_section_type (s) == SHT_NOTE
7544 && (ufile_ptr) s->filepos >= p->p_offset
7545 && p->p_filesz >= s->size
7546 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7547 }
7548
7549 /* Rewrite program header information. */
7550
7551 static bool
7552 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7553 {
7554 Elf_Internal_Ehdr *iehdr;
7555 struct elf_segment_map *map;
7556 struct elf_segment_map *map_first;
7557 struct elf_segment_map **pointer_to_map;
7558 Elf_Internal_Phdr *segment;
7559 asection *section;
7560 unsigned int i;
7561 unsigned int num_segments;
7562 bool phdr_included = false;
7563 bool p_paddr_valid;
7564 struct elf_segment_map *phdr_adjust_seg = NULL;
7565 unsigned int phdr_adjust_num = 0;
7566 const struct elf_backend_data *bed;
7567 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7568
7569 bed = get_elf_backend_data (ibfd);
7570 iehdr = elf_elfheader (ibfd);
7571
7572 map_first = NULL;
7573 pointer_to_map = &map_first;
7574
7575 num_segments = elf_elfheader (ibfd)->e_phnum;
7576
7577 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7578 linker, which generates a PT_INTERP section with p_vaddr and
7579 p_memsz set to 0. */
7580 #define IS_SOLARIS_PT_INTERP(p, s) \
7581 (p->p_vaddr == 0 \
7582 && p->p_paddr == 0 \
7583 && p->p_memsz == 0 \
7584 && p->p_filesz > 0 \
7585 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7586 && s->size > 0 \
7587 && (bfd_vma) s->filepos >= p->p_offset \
7588 && ((bfd_vma) s->filepos + s->size \
7589 <= p->p_offset + p->p_filesz))
7590
7591 /* Decide if the given section should be included in the given segment.
7592 A section will be included if:
7593 1. It is within the address space of the segment -- we use the LMA
7594 if that is set for the segment and the VMA otherwise,
7595 2. It is an allocated section or a NOTE section in a PT_NOTE
7596 segment.
7597 3. There is an output section associated with it,
7598 4. The section has not already been allocated to a previous segment.
7599 5. PT_GNU_STACK segments do not include any sections.
7600 6. PT_TLS segment includes only SHF_TLS sections.
7601 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7602 8. PT_DYNAMIC should not contain empty sections at the beginning
7603 (with the possible exception of .dynamic). */
7604 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7605 (((is_contained_by (section, segment, segment->p_paddr, \
7606 segment->p_vaddr, opb, \
7607 bed->want_p_paddr_set_to_zero) \
7608 && (section->flags & SEC_ALLOC) != 0) \
7609 || is_note (section, segment)) \
7610 && segment->p_type != PT_GNU_STACK \
7611 && (segment->p_type != PT_TLS \
7612 || (section->flags & SEC_THREAD_LOCAL)) \
7613 && (segment->p_type == PT_LOAD \
7614 || segment->p_type == PT_TLS \
7615 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7616 && (segment->p_type != PT_DYNAMIC \
7617 || section_size (section, segment) > 0 \
7618 || (segment->p_paddr \
7619 ? segment->p_paddr != section->lma * (opb) \
7620 : segment->p_vaddr != section->vma * (opb)) \
7621 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7622 && (segment->p_type != PT_LOAD || !section->segment_mark))
7623
7624 /* If the output section of a section in the input segment is NULL,
7625 it is removed from the corresponding output segment. */
7626 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7627 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7628 && section->output_section != NULL)
7629
7630 /* Returns TRUE iff seg1 starts after the end of seg2. */
7631 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7632 (seg1->field >= segment_end (seg2, seg2->field))
7633
7634 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7635 their VMA address ranges and their LMA address ranges overlap.
7636 It is possible to have overlapping VMA ranges without overlapping LMA
7637 ranges. RedBoot images for example can have both .data and .bss mapped
7638 to the same VMA range, but with the .data section mapped to a different
7639 LMA. */
7640 #define SEGMENT_OVERLAPS(seg1, seg2) \
7641 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7642 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7643 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7644 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7645
7646 /* Initialise the segment mark field, and discard stupid alignment. */
7647 for (section = ibfd->sections; section != NULL; section = section->next)
7648 {
7649 asection *o = section->output_section;
7650 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7651 o->alignment_power = 0;
7652 section->segment_mark = false;
7653 }
7654
7655 /* The Solaris linker creates program headers in which all the
7656 p_paddr fields are zero. When we try to objcopy or strip such a
7657 file, we get confused. Check for this case, and if we find it
7658 don't set the p_paddr_valid fields. */
7659 p_paddr_valid = false;
7660 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7661 i < num_segments;
7662 i++, segment++)
7663 if (segment->p_paddr != 0)
7664 {
7665 p_paddr_valid = true;
7666 break;
7667 }
7668
7669 /* Scan through the segments specified in the program header
7670 of the input BFD. For this first scan we look for overlaps
7671 in the loadable segments. These can be created by weird
7672 parameters to objcopy. Also, fix some solaris weirdness. */
7673 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7674 i < num_segments;
7675 i++, segment++)
7676 {
7677 unsigned int j;
7678 Elf_Internal_Phdr *segment2;
7679
7680 if (segment->p_type == PT_INTERP)
7681 for (section = ibfd->sections; section; section = section->next)
7682 if (IS_SOLARIS_PT_INTERP (segment, section))
7683 {
7684 /* Mininal change so that the normal section to segment
7685 assignment code will work. */
7686 segment->p_vaddr = section->vma * opb;
7687 break;
7688 }
7689
7690 if (segment->p_type != PT_LOAD)
7691 {
7692 /* Remove PT_GNU_RELRO segment. */
7693 if (segment->p_type == PT_GNU_RELRO)
7694 segment->p_type = PT_NULL;
7695 continue;
7696 }
7697
7698 /* Determine if this segment overlaps any previous segments. */
7699 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7700 {
7701 bfd_signed_vma extra_length;
7702
7703 if (segment2->p_type != PT_LOAD
7704 || !SEGMENT_OVERLAPS (segment, segment2))
7705 continue;
7706
7707 /* Merge the two segments together. */
7708 if (segment2->p_vaddr < segment->p_vaddr)
7709 {
7710 /* Extend SEGMENT2 to include SEGMENT and then delete
7711 SEGMENT. */
7712 extra_length = (segment_end (segment, segment->p_vaddr)
7713 - segment_end (segment2, segment2->p_vaddr));
7714
7715 if (extra_length > 0)
7716 {
7717 segment2->p_memsz += extra_length;
7718 segment2->p_filesz += extra_length;
7719 }
7720
7721 segment->p_type = PT_NULL;
7722
7723 /* Since we have deleted P we must restart the outer loop. */
7724 i = 0;
7725 segment = elf_tdata (ibfd)->phdr;
7726 break;
7727 }
7728 else
7729 {
7730 /* Extend SEGMENT to include SEGMENT2 and then delete
7731 SEGMENT2. */
7732 extra_length = (segment_end (segment2, segment2->p_vaddr)
7733 - segment_end (segment, segment->p_vaddr));
7734
7735 if (extra_length > 0)
7736 {
7737 segment->p_memsz += extra_length;
7738 segment->p_filesz += extra_length;
7739 }
7740
7741 segment2->p_type = PT_NULL;
7742 }
7743 }
7744 }
7745
7746 /* The second scan attempts to assign sections to segments. */
7747 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7748 i < num_segments;
7749 i++, segment++)
7750 {
7751 unsigned int section_count;
7752 asection **sections;
7753 asection *output_section;
7754 unsigned int isec;
7755 asection *matching_lma;
7756 asection *suggested_lma;
7757 unsigned int j;
7758 size_t amt;
7759 asection *first_section;
7760
7761 if (segment->p_type == PT_NULL)
7762 continue;
7763
7764 first_section = NULL;
7765 /* Compute how many sections might be placed into this segment. */
7766 for (section = ibfd->sections, section_count = 0;
7767 section != NULL;
7768 section = section->next)
7769 {
7770 /* Find the first section in the input segment, which may be
7771 removed from the corresponding output segment. */
7772 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7773 {
7774 if (first_section == NULL)
7775 first_section = section;
7776 if (section->output_section != NULL)
7777 ++section_count;
7778 }
7779 }
7780
7781 /* Allocate a segment map big enough to contain
7782 all of the sections we have selected. */
7783 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7784 amt += section_count * sizeof (asection *);
7785 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7786 if (map == NULL)
7787 return false;
7788
7789 /* Initialise the fields of the segment map. Default to
7790 using the physical address of the segment in the input BFD. */
7791 map->next = NULL;
7792 map->p_type = segment->p_type;
7793 map->p_flags = segment->p_flags;
7794 map->p_flags_valid = 1;
7795
7796 if (map->p_type == PT_LOAD
7797 && (ibfd->flags & D_PAGED) != 0
7798 && maxpagesize > 1
7799 && segment->p_align > 1)
7800 {
7801 map->p_align = segment->p_align;
7802 if (segment->p_align > maxpagesize)
7803 map->p_align = maxpagesize;
7804 map->p_align_valid = 1;
7805 }
7806
7807 /* If the first section in the input segment is removed, there is
7808 no need to preserve segment physical address in the corresponding
7809 output segment. */
7810 if (!first_section || first_section->output_section != NULL)
7811 {
7812 map->p_paddr = segment->p_paddr;
7813 map->p_paddr_valid = p_paddr_valid;
7814 }
7815
7816 /* Determine if this segment contains the ELF file header
7817 and if it contains the program headers themselves. */
7818 map->includes_filehdr = (segment->p_offset == 0
7819 && segment->p_filesz >= iehdr->e_ehsize);
7820 map->includes_phdrs = 0;
7821
7822 if (!phdr_included || segment->p_type != PT_LOAD)
7823 {
7824 map->includes_phdrs =
7825 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7826 && (segment->p_offset + segment->p_filesz
7827 >= ((bfd_vma) iehdr->e_phoff
7828 + iehdr->e_phnum * iehdr->e_phentsize)));
7829
7830 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7831 phdr_included = true;
7832 }
7833
7834 if (section_count == 0)
7835 {
7836 /* Special segments, such as the PT_PHDR segment, may contain
7837 no sections, but ordinary, loadable segments should contain
7838 something. They are allowed by the ELF spec however, so only
7839 a warning is produced.
7840 Don't warn if an empty PT_LOAD contains the program headers.
7841 There is however the valid use case of embedded systems which
7842 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7843 flash memory with zeros. No warning is shown for that case. */
7844 if (segment->p_type == PT_LOAD
7845 && !map->includes_phdrs
7846 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7847 /* xgettext:c-format */
7848 _bfd_error_handler
7849 (_("%pB: warning: empty loadable segment detected"
7850 " at vaddr=%#" PRIx64 ", is this intentional?"),
7851 ibfd, (uint64_t) segment->p_vaddr);
7852
7853 map->p_vaddr_offset = segment->p_vaddr / opb;
7854 map->count = 0;
7855 *pointer_to_map = map;
7856 pointer_to_map = &map->next;
7857
7858 continue;
7859 }
7860
7861 /* Now scan the sections in the input BFD again and attempt
7862 to add their corresponding output sections to the segment map.
7863 The problem here is how to handle an output section which has
7864 been moved (ie had its LMA changed). There are four possibilities:
7865
7866 1. None of the sections have been moved.
7867 In this case we can continue to use the segment LMA from the
7868 input BFD.
7869
7870 2. All of the sections have been moved by the same amount.
7871 In this case we can change the segment's LMA to match the LMA
7872 of the first section.
7873
7874 3. Some of the sections have been moved, others have not.
7875 In this case those sections which have not been moved can be
7876 placed in the current segment which will have to have its size,
7877 and possibly its LMA changed, and a new segment or segments will
7878 have to be created to contain the other sections.
7879
7880 4. The sections have been moved, but not by the same amount.
7881 In this case we can change the segment's LMA to match the LMA
7882 of the first section and we will have to create a new segment
7883 or segments to contain the other sections.
7884
7885 In order to save time, we allocate an array to hold the section
7886 pointers that we are interested in. As these sections get assigned
7887 to a segment, they are removed from this array. */
7888
7889 amt = section_count * sizeof (asection *);
7890 sections = (asection **) bfd_malloc (amt);
7891 if (sections == NULL)
7892 return false;
7893
7894 /* Step One: Scan for segment vs section LMA conflicts.
7895 Also add the sections to the section array allocated above.
7896 Also add the sections to the current segment. In the common
7897 case, where the sections have not been moved, this means that
7898 we have completely filled the segment, and there is nothing
7899 more to do. */
7900 isec = 0;
7901 matching_lma = NULL;
7902 suggested_lma = NULL;
7903
7904 for (section = first_section, j = 0;
7905 section != NULL;
7906 section = section->next)
7907 {
7908 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7909 {
7910 output_section = section->output_section;
7911
7912 sections[j++] = section;
7913
7914 /* The Solaris native linker always sets p_paddr to 0.
7915 We try to catch that case here, and set it to the
7916 correct value. Note - some backends require that
7917 p_paddr be left as zero. */
7918 if (!p_paddr_valid
7919 && segment->p_vaddr != 0
7920 && !bed->want_p_paddr_set_to_zero
7921 && isec == 0
7922 && output_section->lma != 0
7923 && (align_power (segment->p_vaddr
7924 + (map->includes_filehdr
7925 ? iehdr->e_ehsize : 0)
7926 + (map->includes_phdrs
7927 ? iehdr->e_phnum * iehdr->e_phentsize
7928 : 0),
7929 output_section->alignment_power * opb)
7930 == (output_section->vma * opb)))
7931 map->p_paddr = segment->p_vaddr;
7932
7933 /* Match up the physical address of the segment with the
7934 LMA address of the output section. */
7935 if (is_contained_by (output_section, segment, map->p_paddr,
7936 0, opb, false)
7937 || is_note (section, segment))
7938 {
7939 if (matching_lma == NULL
7940 || output_section->lma < matching_lma->lma)
7941 matching_lma = output_section;
7942
7943 /* We assume that if the section fits within the segment
7944 then it does not overlap any other section within that
7945 segment. */
7946 map->sections[isec++] = output_section;
7947 }
7948 else if (suggested_lma == NULL)
7949 suggested_lma = output_section;
7950
7951 if (j == section_count)
7952 break;
7953 }
7954 }
7955
7956 BFD_ASSERT (j == section_count);
7957
7958 /* Step Two: Adjust the physical address of the current segment,
7959 if necessary. */
7960 if (isec == section_count)
7961 {
7962 /* All of the sections fitted within the segment as currently
7963 specified. This is the default case. Add the segment to
7964 the list of built segments and carry on to process the next
7965 program header in the input BFD. */
7966 map->count = section_count;
7967 *pointer_to_map = map;
7968 pointer_to_map = &map->next;
7969
7970 if (p_paddr_valid
7971 && !bed->want_p_paddr_set_to_zero)
7972 {
7973 bfd_vma hdr_size = 0;
7974 if (map->includes_filehdr)
7975 hdr_size = iehdr->e_ehsize;
7976 if (map->includes_phdrs)
7977 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7978
7979 /* Account for padding before the first section in the
7980 segment. */
7981 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7982 - matching_lma->lma);
7983 }
7984
7985 free (sections);
7986 continue;
7987 }
7988 else
7989 {
7990 /* Change the current segment's physical address to match
7991 the LMA of the first section that fitted, or if no
7992 section fitted, the first section. */
7993 if (matching_lma == NULL)
7994 matching_lma = suggested_lma;
7995
7996 map->p_paddr = matching_lma->lma * opb;
7997
7998 /* Offset the segment physical address from the lma
7999 to allow for space taken up by elf headers. */
8000 if (map->includes_phdrs)
8001 {
8002 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
8003
8004 /* iehdr->e_phnum is just an estimate of the number
8005 of program headers that we will need. Make a note
8006 here of the number we used and the segment we chose
8007 to hold these headers, so that we can adjust the
8008 offset when we know the correct value. */
8009 phdr_adjust_num = iehdr->e_phnum;
8010 phdr_adjust_seg = map;
8011 }
8012
8013 if (map->includes_filehdr)
8014 {
8015 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
8016 map->p_paddr -= iehdr->e_ehsize;
8017 /* We've subtracted off the size of headers from the
8018 first section lma, but there may have been some
8019 alignment padding before that section too. Try to
8020 account for that by adjusting the segment lma down to
8021 the same alignment. */
8022 if (segment->p_align != 0 && segment->p_align < align)
8023 align = segment->p_align;
8024 map->p_paddr &= -(align * opb);
8025 }
8026 }
8027
8028 /* Step Three: Loop over the sections again, this time assigning
8029 those that fit to the current segment and removing them from the
8030 sections array; but making sure not to leave large gaps. Once all
8031 possible sections have been assigned to the current segment it is
8032 added to the list of built segments and if sections still remain
8033 to be assigned, a new segment is constructed before repeating
8034 the loop. */
8035 isec = 0;
8036 do
8037 {
8038 map->count = 0;
8039 suggested_lma = NULL;
8040
8041 /* Fill the current segment with sections that fit. */
8042 for (j = 0; j < section_count; j++)
8043 {
8044 section = sections[j];
8045
8046 if (section == NULL)
8047 continue;
8048
8049 output_section = section->output_section;
8050
8051 BFD_ASSERT (output_section != NULL);
8052
8053 if (is_contained_by (output_section, segment, map->p_paddr,
8054 0, opb, false)
8055 || is_note (section, segment))
8056 {
8057 if (map->count == 0)
8058 {
8059 /* If the first section in a segment does not start at
8060 the beginning of the segment, then something is
8061 wrong. */
8062 if (align_power (map->p_paddr
8063 + (map->includes_filehdr
8064 ? iehdr->e_ehsize : 0)
8065 + (map->includes_phdrs
8066 ? iehdr->e_phnum * iehdr->e_phentsize
8067 : 0),
8068 output_section->alignment_power * opb)
8069 != output_section->lma * opb)
8070 goto sorry;
8071 }
8072 else
8073 {
8074 asection *prev_sec;
8075
8076 prev_sec = map->sections[map->count - 1];
8077
8078 /* If the gap between the end of the previous section
8079 and the start of this section is more than
8080 maxpagesize then we need to start a new segment. */
8081 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
8082 maxpagesize)
8083 < BFD_ALIGN (output_section->lma, maxpagesize))
8084 || (prev_sec->lma + prev_sec->size
8085 > output_section->lma))
8086 {
8087 if (suggested_lma == NULL)
8088 suggested_lma = output_section;
8089
8090 continue;
8091 }
8092 }
8093
8094 map->sections[map->count++] = output_section;
8095 ++isec;
8096 sections[j] = NULL;
8097 if (segment->p_type == PT_LOAD)
8098 section->segment_mark = true;
8099 }
8100 else if (suggested_lma == NULL)
8101 suggested_lma = output_section;
8102 }
8103
8104 /* PR 23932. A corrupt input file may contain sections that cannot
8105 be assigned to any segment - because for example they have a
8106 negative size - or segments that do not contain any sections.
8107 But there are also valid reasons why a segment can be empty.
8108 So allow a count of zero. */
8109
8110 /* Add the current segment to the list of built segments. */
8111 *pointer_to_map = map;
8112 pointer_to_map = &map->next;
8113
8114 if (isec < section_count)
8115 {
8116 /* We still have not allocated all of the sections to
8117 segments. Create a new segment here, initialise it
8118 and carry on looping. */
8119 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8120 amt += section_count * sizeof (asection *);
8121 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8122 if (map == NULL)
8123 {
8124 free (sections);
8125 return false;
8126 }
8127
8128 /* Initialise the fields of the segment map. Set the physical
8129 physical address to the LMA of the first section that has
8130 not yet been assigned. */
8131 map->next = NULL;
8132 map->p_type = segment->p_type;
8133 map->p_flags = segment->p_flags;
8134 map->p_flags_valid = 1;
8135 map->p_paddr = suggested_lma->lma * opb;
8136 map->p_paddr_valid = p_paddr_valid;
8137 map->includes_filehdr = 0;
8138 map->includes_phdrs = 0;
8139 }
8140
8141 continue;
8142 sorry:
8143 bfd_set_error (bfd_error_sorry);
8144 free (sections);
8145 return false;
8146 }
8147 while (isec < section_count);
8148
8149 free (sections);
8150 }
8151
8152 elf_seg_map (obfd) = map_first;
8153
8154 /* If we had to estimate the number of program headers that were
8155 going to be needed, then check our estimate now and adjust
8156 the offset if necessary. */
8157 if (phdr_adjust_seg != NULL)
8158 {
8159 unsigned int count;
8160
8161 for (count = 0, map = map_first; map != NULL; map = map->next)
8162 count++;
8163
8164 if (count > phdr_adjust_num)
8165 phdr_adjust_seg->p_paddr
8166 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8167
8168 for (map = map_first; map != NULL; map = map->next)
8169 if (map->p_type == PT_PHDR)
8170 {
8171 bfd_vma adjust
8172 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8173 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8174 break;
8175 }
8176 }
8177
8178 #undef IS_SOLARIS_PT_INTERP
8179 #undef IS_SECTION_IN_INPUT_SEGMENT
8180 #undef INCLUDE_SECTION_IN_SEGMENT
8181 #undef SEGMENT_AFTER_SEGMENT
8182 #undef SEGMENT_OVERLAPS
8183 return true;
8184 }
8185
8186 /* Return true if p_align in the ELF program header in ABFD is valid. */
8187
8188 static bool
8189 elf_is_p_align_valid (bfd *abfd)
8190 {
8191 unsigned int i;
8192 Elf_Internal_Phdr *segment;
8193 unsigned int num_segments;
8194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8195 bfd_size_type maxpagesize = bed->maxpagesize;
8196 bfd_size_type p_align = bed->p_align;
8197
8198 /* Return true if the default p_align value isn't set or the maximum
8199 page size is the same as the minimum page size. */
8200 if (p_align == 0 || maxpagesize == bed->minpagesize)
8201 return true;
8202
8203 /* When the default p_align value is set, p_align may be set to the
8204 default p_align value while segments are aligned to the maximum
8205 page size. In this case, the input p_align will be ignored and
8206 the maximum page size will be used to align the output segments. */
8207 segment = elf_tdata (abfd)->phdr;
8208 num_segments = elf_elfheader (abfd)->e_phnum;
8209 for (i = 0; i < num_segments; i++, segment++)
8210 if (segment->p_type == PT_LOAD
8211 && (segment->p_align != p_align
8212 || vma_page_aligned_bias (segment->p_vaddr,
8213 segment->p_offset,
8214 maxpagesize) != 0))
8215 return true;
8216
8217 return false;
8218 }
8219
8220 /* Copy ELF program header information. */
8221
8222 static bool
8223 copy_elf_program_header (bfd *ibfd, bfd *obfd)
8224 {
8225 Elf_Internal_Ehdr *iehdr;
8226 struct elf_segment_map *map;
8227 struct elf_segment_map *map_first;
8228 struct elf_segment_map **pointer_to_map;
8229 Elf_Internal_Phdr *segment;
8230 unsigned int i;
8231 unsigned int num_segments;
8232 bool phdr_included = false;
8233 bool p_paddr_valid;
8234 bool p_palign_valid;
8235 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8236
8237 iehdr = elf_elfheader (ibfd);
8238
8239 map_first = NULL;
8240 pointer_to_map = &map_first;
8241
8242 /* If all the segment p_paddr fields are zero, don't set
8243 map->p_paddr_valid. */
8244 p_paddr_valid = false;
8245 num_segments = elf_elfheader (ibfd)->e_phnum;
8246 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8247 i < num_segments;
8248 i++, segment++)
8249 if (segment->p_paddr != 0)
8250 {
8251 p_paddr_valid = true;
8252 break;
8253 }
8254
8255 p_palign_valid = elf_is_p_align_valid (ibfd);
8256
8257 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8258 i < num_segments;
8259 i++, segment++)
8260 {
8261 asection *section;
8262 unsigned int section_count;
8263 size_t amt;
8264 Elf_Internal_Shdr *this_hdr;
8265 asection *first_section = NULL;
8266 asection *lowest_section;
8267
8268 /* Compute how many sections are in this segment. */
8269 for (section = ibfd->sections, section_count = 0;
8270 section != NULL;
8271 section = section->next)
8272 {
8273 this_hdr = &(elf_section_data(section)->this_hdr);
8274 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8275 {
8276 if (first_section == NULL)
8277 first_section = section;
8278 section_count++;
8279 }
8280 }
8281
8282 /* Allocate a segment map big enough to contain
8283 all of the sections we have selected. */
8284 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8285 amt += section_count * sizeof (asection *);
8286 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8287 if (map == NULL)
8288 return false;
8289
8290 /* Initialize the fields of the output segment map with the
8291 input segment. */
8292 map->next = NULL;
8293 map->p_type = segment->p_type;
8294 map->p_flags = segment->p_flags;
8295 map->p_flags_valid = 1;
8296 map->p_paddr = segment->p_paddr;
8297 map->p_paddr_valid = p_paddr_valid;
8298 map->p_align = segment->p_align;
8299 /* Keep p_align of PT_GNU_STACK for stack alignment. */
8300 map->p_align_valid = (map->p_type == PT_GNU_STACK
8301 || p_palign_valid);
8302 map->p_vaddr_offset = 0;
8303
8304 if (map->p_type == PT_GNU_RELRO
8305 || map->p_type == PT_GNU_STACK)
8306 {
8307 /* The PT_GNU_RELRO segment may contain the first a few
8308 bytes in the .got.plt section even if the whole .got.plt
8309 section isn't in the PT_GNU_RELRO segment. We won't
8310 change the size of the PT_GNU_RELRO segment.
8311 Similarly, PT_GNU_STACK size is significant on uclinux
8312 systems. */
8313 map->p_size = segment->p_memsz;
8314 map->p_size_valid = 1;
8315 }
8316
8317 /* Determine if this segment contains the ELF file header
8318 and if it contains the program headers themselves. */
8319 map->includes_filehdr = (segment->p_offset == 0
8320 && segment->p_filesz >= iehdr->e_ehsize);
8321
8322 map->includes_phdrs = 0;
8323 if (! phdr_included || segment->p_type != PT_LOAD)
8324 {
8325 map->includes_phdrs =
8326 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8327 && (segment->p_offset + segment->p_filesz
8328 >= ((bfd_vma) iehdr->e_phoff
8329 + iehdr->e_phnum * iehdr->e_phentsize)));
8330
8331 if (segment->p_type == PT_LOAD && map->includes_phdrs)
8332 phdr_included = true;
8333 }
8334
8335 lowest_section = NULL;
8336 if (section_count != 0)
8337 {
8338 unsigned int isec = 0;
8339
8340 for (section = first_section;
8341 section != NULL;
8342 section = section->next)
8343 {
8344 this_hdr = &(elf_section_data(section)->this_hdr);
8345 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8346 {
8347 map->sections[isec++] = section->output_section;
8348 if ((section->flags & SEC_ALLOC) != 0)
8349 {
8350 bfd_vma seg_off;
8351
8352 if (lowest_section == NULL
8353 || section->lma < lowest_section->lma)
8354 lowest_section = section;
8355
8356 /* Section lmas are set up from PT_LOAD header
8357 p_paddr in _bfd_elf_make_section_from_shdr.
8358 If this header has a p_paddr that disagrees
8359 with the section lma, flag the p_paddr as
8360 invalid. */
8361 if ((section->flags & SEC_LOAD) != 0)
8362 seg_off = this_hdr->sh_offset - segment->p_offset;
8363 else
8364 seg_off = this_hdr->sh_addr - segment->p_vaddr;
8365 if (section->lma * opb - segment->p_paddr != seg_off)
8366 map->p_paddr_valid = false;
8367 }
8368 if (isec == section_count)
8369 break;
8370 }
8371 }
8372 }
8373
8374 if (section_count == 0)
8375 map->p_vaddr_offset = segment->p_vaddr / opb;
8376 else if (map->p_paddr_valid)
8377 {
8378 /* Account for padding before the first section in the segment. */
8379 bfd_vma hdr_size = 0;
8380 if (map->includes_filehdr)
8381 hdr_size = iehdr->e_ehsize;
8382 if (map->includes_phdrs)
8383 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8384
8385 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8386 - (lowest_section ? lowest_section->lma : 0));
8387 }
8388
8389 map->count = section_count;
8390 *pointer_to_map = map;
8391 pointer_to_map = &map->next;
8392 }
8393
8394 elf_seg_map (obfd) = map_first;
8395 return true;
8396 }
8397
8398 /* Copy private BFD data. This copies or rewrites ELF program header
8399 information. */
8400
8401 static bool
8402 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8403 {
8404 bfd_vma maxpagesize;
8405
8406 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8407 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8408 return true;
8409
8410 if (elf_tdata (ibfd)->phdr == NULL)
8411 return true;
8412
8413 if (ibfd->xvec == obfd->xvec)
8414 {
8415 /* Check to see if any sections in the input BFD
8416 covered by ELF program header have changed. */
8417 Elf_Internal_Phdr *segment;
8418 asection *section, *osec;
8419 unsigned int i, num_segments;
8420 Elf_Internal_Shdr *this_hdr;
8421 const struct elf_backend_data *bed;
8422
8423 bed = get_elf_backend_data (ibfd);
8424
8425 /* Regenerate the segment map if p_paddr is set to 0. */
8426 if (bed->want_p_paddr_set_to_zero)
8427 goto rewrite;
8428
8429 /* Initialize the segment mark field. */
8430 for (section = obfd->sections; section != NULL;
8431 section = section->next)
8432 section->segment_mark = false;
8433
8434 num_segments = elf_elfheader (ibfd)->e_phnum;
8435 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8436 i < num_segments;
8437 i++, segment++)
8438 {
8439 /* PR binutils/3535. The Solaris linker always sets the p_paddr
8440 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8441 which severly confuses things, so always regenerate the segment
8442 map in this case. */
8443 if (segment->p_paddr == 0
8444 && segment->p_memsz == 0
8445 && (segment->p_type == PT_INTERP
8446 || segment->p_type == PT_DYNAMIC))
8447 goto rewrite;
8448
8449 for (section = ibfd->sections;
8450 section != NULL; section = section->next)
8451 {
8452 /* We mark the output section so that we know it comes
8453 from the input BFD. */
8454 osec = section->output_section;
8455 if (osec)
8456 osec->segment_mark = true;
8457
8458 /* Check if this section is covered by the segment. */
8459 this_hdr = &(elf_section_data(section)->this_hdr);
8460 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8461 {
8462 /* FIXME: Check if its output section is changed or
8463 removed. What else do we need to check? */
8464 if (osec == NULL
8465 || section->flags != osec->flags
8466 || section->lma != osec->lma
8467 || section->vma != osec->vma
8468 || section->size != osec->size
8469 || section->rawsize != osec->rawsize
8470 || section->alignment_power != osec->alignment_power)
8471 goto rewrite;
8472 }
8473 }
8474 }
8475
8476 /* Check to see if any output section do not come from the
8477 input BFD. */
8478 for (section = obfd->sections; section != NULL;
8479 section = section->next)
8480 {
8481 if (!section->segment_mark)
8482 goto rewrite;
8483 else
8484 section->segment_mark = false;
8485 }
8486
8487 return copy_elf_program_header (ibfd, obfd);
8488 }
8489
8490 rewrite:
8491 maxpagesize = 0;
8492 if (ibfd->xvec == obfd->xvec)
8493 {
8494 /* When rewriting program header, set the output maxpagesize to
8495 the maximum alignment of input PT_LOAD segments. */
8496 Elf_Internal_Phdr *segment;
8497 unsigned int i;
8498 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8499
8500 for (i = 0, segment = elf_tdata (ibfd)->phdr;
8501 i < num_segments;
8502 i++, segment++)
8503 if (segment->p_type == PT_LOAD
8504 && maxpagesize < segment->p_align)
8505 {
8506 /* PR 17512: file: f17299af. */
8507 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8508 /* xgettext:c-format */
8509 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8510 PRIx64 " is too large"),
8511 ibfd, (uint64_t) segment->p_align);
8512 else
8513 maxpagesize = segment->p_align;
8514 }
8515 }
8516 if (maxpagesize == 0)
8517 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8518
8519 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8520 }
8521
8522 /* Initialize private output section information from input section. */
8523
8524 bool
8525 _bfd_elf_init_private_section_data (bfd *ibfd,
8526 asection *isec,
8527 bfd *obfd,
8528 asection *osec,
8529 struct bfd_link_info *link_info)
8530
8531 {
8532 Elf_Internal_Shdr *ihdr, *ohdr;
8533 bool final_link = (link_info != NULL
8534 && !bfd_link_relocatable (link_info));
8535
8536 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8537 || obfd->xvec->flavour != bfd_target_elf_flavour)
8538 return true;
8539
8540 BFD_ASSERT (elf_section_data (osec) != NULL);
8541
8542 /* If this is a known ABI section, ELF section type and flags may
8543 have been set up when OSEC was created. For normal sections we
8544 allow the user to override the type and flags other than
8545 SHF_MASKOS and SHF_MASKPROC. */
8546 if (elf_section_type (osec) == SHT_PROGBITS
8547 || elf_section_type (osec) == SHT_NOTE
8548 || elf_section_type (osec) == SHT_NOBITS)
8549 elf_section_type (osec) = SHT_NULL;
8550 /* For objcopy and relocatable link, copy the ELF section type from
8551 the input file if the BFD section flags are the same. (If they
8552 are different the user may be doing something like
8553 "objcopy --set-section-flags .text=alloc,data".) For a final
8554 link allow some flags that the linker clears to differ. */
8555 if (elf_section_type (osec) == SHT_NULL
8556 && (osec->flags == isec->flags
8557 || (final_link
8558 && ((osec->flags ^ isec->flags)
8559 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8560 elf_section_type (osec) = elf_section_type (isec);
8561
8562 /* FIXME: Is this correct for all OS/PROC specific flags? */
8563 elf_section_flags (osec) = (elf_section_flags (isec)
8564 & (SHF_MASKOS | SHF_MASKPROC));
8565
8566 /* Copy sh_info from input for mbind section. */
8567 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8568 && elf_section_flags (isec) & SHF_GNU_MBIND)
8569 elf_section_data (osec)->this_hdr.sh_info
8570 = elf_section_data (isec)->this_hdr.sh_info;
8571
8572 /* Set things up for objcopy and relocatable link. The output
8573 SHT_GROUP section will have its elf_next_in_group pointing back
8574 to the input group members. Ignore linker created group section.
8575 See elfNN_ia64_object_p in elfxx-ia64.c. */
8576 if ((link_info == NULL
8577 || !link_info->resolve_section_groups)
8578 && (elf_sec_group (isec) == NULL
8579 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8580 {
8581 if (elf_section_flags (isec) & SHF_GROUP)
8582 elf_section_flags (osec) |= SHF_GROUP;
8583 elf_next_in_group (osec) = elf_next_in_group (isec);
8584 elf_section_data (osec)->group = elf_section_data (isec)->group;
8585 }
8586
8587 /* If not decompress, preserve SHF_COMPRESSED. */
8588 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8589 elf_section_flags (osec) |= (elf_section_flags (isec)
8590 & SHF_COMPRESSED);
8591
8592 ihdr = &elf_section_data (isec)->this_hdr;
8593
8594 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8595 don't use the output section of the linked-to section since it
8596 may be NULL at this point. */
8597 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8598 {
8599 ohdr = &elf_section_data (osec)->this_hdr;
8600 ohdr->sh_flags |= SHF_LINK_ORDER;
8601 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8602 }
8603
8604 osec->use_rela_p = isec->use_rela_p;
8605
8606 return true;
8607 }
8608
8609 /* Copy private section information. This copies over the entsize
8610 field, and sometimes the info field. */
8611
8612 bool
8613 _bfd_elf_copy_private_section_data (bfd *ibfd,
8614 asection *isec,
8615 bfd *obfd,
8616 asection *osec)
8617 {
8618 Elf_Internal_Shdr *ihdr, *ohdr;
8619
8620 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8621 || obfd->xvec->flavour != bfd_target_elf_flavour)
8622 return true;
8623
8624 ihdr = &elf_section_data (isec)->this_hdr;
8625 ohdr = &elf_section_data (osec)->this_hdr;
8626
8627 ohdr->sh_entsize = ihdr->sh_entsize;
8628
8629 if (ihdr->sh_type == SHT_SYMTAB
8630 || ihdr->sh_type == SHT_DYNSYM
8631 || ihdr->sh_type == SHT_GNU_verneed
8632 || ihdr->sh_type == SHT_GNU_verdef)
8633 ohdr->sh_info = ihdr->sh_info;
8634
8635 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8636 NULL);
8637 }
8638
8639 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8640 necessary if we are removing either the SHT_GROUP section or any of
8641 the group member sections. DISCARDED is the value that a section's
8642 output_section has if the section will be discarded, NULL when this
8643 function is called from objcopy, bfd_abs_section_ptr when called
8644 from the linker. */
8645
8646 bool
8647 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8648 {
8649 asection *isec;
8650
8651 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8652 if (elf_section_type (isec) == SHT_GROUP)
8653 {
8654 asection *first = elf_next_in_group (isec);
8655 asection *s = first;
8656 bfd_size_type removed = 0;
8657
8658 while (s != NULL)
8659 {
8660 /* If this member section is being output but the
8661 SHT_GROUP section is not, then clear the group info
8662 set up by _bfd_elf_copy_private_section_data. */
8663 if (s->output_section != discarded
8664 && isec->output_section == discarded)
8665 {
8666 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8667 elf_group_name (s->output_section) = NULL;
8668 }
8669 else
8670 {
8671 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8672 if (s->output_section == discarded
8673 && isec->output_section != discarded)
8674 {
8675 /* Conversely, if the member section is not being
8676 output but the SHT_GROUP section is, then adjust
8677 its size. */
8678 removed += 4;
8679 if (elf_sec->rel.hdr != NULL
8680 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8681 removed += 4;
8682 if (elf_sec->rela.hdr != NULL
8683 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8684 removed += 4;
8685 }
8686 else
8687 {
8688 /* Also adjust for zero-sized relocation member
8689 section. */
8690 if (elf_sec->rel.hdr != NULL
8691 && elf_sec->rel.hdr->sh_size == 0)
8692 removed += 4;
8693 if (elf_sec->rela.hdr != NULL
8694 && elf_sec->rela.hdr->sh_size == 0)
8695 removed += 4;
8696 }
8697 }
8698 s = elf_next_in_group (s);
8699 if (s == first)
8700 break;
8701 }
8702 if (removed != 0)
8703 {
8704 if (discarded != NULL)
8705 {
8706 /* If we've been called for ld -r, then we need to
8707 adjust the input section size. */
8708 if (isec->rawsize == 0)
8709 isec->rawsize = isec->size;
8710 isec->size = isec->rawsize - removed;
8711 if (isec->size <= 4)
8712 {
8713 isec->size = 0;
8714 isec->flags |= SEC_EXCLUDE;
8715 }
8716 }
8717 else if (isec->output_section != NULL)
8718 {
8719 /* Adjust the output section size when called from
8720 objcopy. */
8721 isec->output_section->size -= removed;
8722 if (isec->output_section->size <= 4)
8723 {
8724 isec->output_section->size = 0;
8725 isec->output_section->flags |= SEC_EXCLUDE;
8726 }
8727 }
8728 }
8729 }
8730
8731 return true;
8732 }
8733
8734 /* Copy private header information. */
8735
8736 bool
8737 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8738 {
8739 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8740 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8741 return true;
8742
8743 /* Copy over private BFD data if it has not already been copied.
8744 This must be done here, rather than in the copy_private_bfd_data
8745 entry point, because the latter is called after the section
8746 contents have been set, which means that the program headers have
8747 already been worked out. */
8748 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8749 {
8750 if (! copy_private_bfd_data (ibfd, obfd))
8751 return false;
8752 }
8753
8754 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8755 }
8756
8757 /* Copy private symbol information. If this symbol is in a section
8758 which we did not map into a BFD section, try to map the section
8759 index correctly. We use special macro definitions for the mapped
8760 section indices; these definitions are interpreted by the
8761 swap_out_syms function. */
8762
8763 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8764 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8765 #define MAP_STRTAB (SHN_HIOS + 3)
8766 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8767 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8768
8769 bool
8770 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8771 asymbol *isymarg,
8772 bfd *obfd,
8773 asymbol *osymarg)
8774 {
8775 elf_symbol_type *isym, *osym;
8776
8777 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8778 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8779 return true;
8780
8781 isym = elf_symbol_from (isymarg);
8782 osym = elf_symbol_from (osymarg);
8783
8784 if (isym != NULL
8785 && isym->internal_elf_sym.st_shndx != 0
8786 && osym != NULL
8787 && bfd_is_abs_section (isym->symbol.section))
8788 {
8789 unsigned int shndx;
8790
8791 shndx = isym->internal_elf_sym.st_shndx;
8792 if (shndx == elf_onesymtab (ibfd))
8793 shndx = MAP_ONESYMTAB;
8794 else if (shndx == elf_dynsymtab (ibfd))
8795 shndx = MAP_DYNSYMTAB;
8796 else if (shndx == elf_strtab_sec (ibfd))
8797 shndx = MAP_STRTAB;
8798 else if (shndx == elf_shstrtab_sec (ibfd))
8799 shndx = MAP_SHSTRTAB;
8800 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8801 shndx = MAP_SYM_SHNDX;
8802 osym->internal_elf_sym.st_shndx = shndx;
8803 }
8804
8805 return true;
8806 }
8807
8808 /* Swap out the symbols. */
8809
8810 static bool
8811 swap_out_syms (bfd *abfd,
8812 struct elf_strtab_hash **sttp,
8813 int relocatable_p,
8814 struct bfd_link_info *info)
8815 {
8816 const struct elf_backend_data *bed;
8817 unsigned int symcount;
8818 asymbol **syms;
8819 struct elf_strtab_hash *stt;
8820 Elf_Internal_Shdr *symtab_hdr;
8821 Elf_Internal_Shdr *symtab_shndx_hdr;
8822 Elf_Internal_Shdr *symstrtab_hdr;
8823 struct elf_sym_strtab *symstrtab;
8824 bfd_byte *outbound_syms;
8825 bfd_byte *outbound_shndx;
8826 unsigned long outbound_syms_index;
8827 unsigned int idx;
8828 unsigned int num_locals;
8829 size_t amt;
8830 bool name_local_sections;
8831
8832 if (!elf_map_symbols (abfd, &num_locals))
8833 return false;
8834
8835 /* Dump out the symtabs. */
8836 stt = _bfd_elf_strtab_init ();
8837 if (stt == NULL)
8838 return false;
8839
8840 bed = get_elf_backend_data (abfd);
8841 symcount = bfd_get_symcount (abfd);
8842 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8843 symtab_hdr->sh_type = SHT_SYMTAB;
8844 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8845 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8846 symtab_hdr->sh_info = num_locals + 1;
8847 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8848
8849 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8850 symstrtab_hdr->sh_type = SHT_STRTAB;
8851
8852 /* Allocate buffer to swap out the .strtab section. */
8853 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8854 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8855 {
8856 bfd_set_error (bfd_error_no_memory);
8857 _bfd_elf_strtab_free (stt);
8858 return false;
8859 }
8860
8861 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8862 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8863 {
8864 error_no_mem:
8865 bfd_set_error (bfd_error_no_memory);
8866 error_return:
8867 free (symstrtab);
8868 _bfd_elf_strtab_free (stt);
8869 return false;
8870 }
8871 symtab_hdr->contents = outbound_syms;
8872 outbound_syms_index = 0;
8873
8874 outbound_shndx = NULL;
8875
8876 if (elf_symtab_shndx_list (abfd))
8877 {
8878 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8879 if (symtab_shndx_hdr->sh_name != 0)
8880 {
8881 if (_bfd_mul_overflow (symcount + 1,
8882 sizeof (Elf_External_Sym_Shndx), &amt))
8883 goto error_no_mem;
8884 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8885 if (outbound_shndx == NULL)
8886 goto error_return;
8887
8888 symtab_shndx_hdr->contents = outbound_shndx;
8889 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8890 symtab_shndx_hdr->sh_size = amt;
8891 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8892 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8893 }
8894 /* FIXME: What about any other headers in the list ? */
8895 }
8896
8897 /* Now generate the data (for "contents"). */
8898 {
8899 /* Fill in zeroth symbol and swap it out. */
8900 Elf_Internal_Sym sym;
8901 sym.st_name = 0;
8902 sym.st_value = 0;
8903 sym.st_size = 0;
8904 sym.st_info = 0;
8905 sym.st_other = 0;
8906 sym.st_shndx = SHN_UNDEF;
8907 sym.st_target_internal = 0;
8908 symstrtab[0].sym = sym;
8909 symstrtab[0].dest_index = outbound_syms_index;
8910 outbound_syms_index++;
8911 }
8912
8913 name_local_sections
8914 = (bed->elf_backend_name_local_section_symbols
8915 && bed->elf_backend_name_local_section_symbols (abfd));
8916
8917 syms = bfd_get_outsymbols (abfd);
8918 for (idx = 0; idx < symcount;)
8919 {
8920 Elf_Internal_Sym sym;
8921 bfd_vma value = syms[idx]->value;
8922 elf_symbol_type *type_ptr;
8923 flagword flags = syms[idx]->flags;
8924 int type;
8925
8926 if (!name_local_sections
8927 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8928 {
8929 /* Local section symbols have no name. */
8930 sym.st_name = (unsigned long) -1;
8931 }
8932 else
8933 {
8934 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8935 to get the final offset for st_name. */
8936 sym.st_name
8937 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8938 false);
8939 if (sym.st_name == (unsigned long) -1)
8940 goto error_return;
8941 }
8942
8943 type_ptr = elf_symbol_from (syms[idx]);
8944
8945 if ((flags & BSF_SECTION_SYM) == 0
8946 && bfd_is_com_section (syms[idx]->section))
8947 {
8948 /* ELF common symbols put the alignment into the `value' field,
8949 and the size into the `size' field. This is backwards from
8950 how BFD handles it, so reverse it here. */
8951 sym.st_size = value;
8952 if (type_ptr == NULL
8953 || type_ptr->internal_elf_sym.st_value == 0)
8954 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8955 else
8956 sym.st_value = type_ptr->internal_elf_sym.st_value;
8957 sym.st_shndx = _bfd_elf_section_from_bfd_section
8958 (abfd, syms[idx]->section);
8959 }
8960 else
8961 {
8962 asection *sec = syms[idx]->section;
8963 unsigned int shndx;
8964
8965 if (sec->output_section)
8966 {
8967 value += sec->output_offset;
8968 sec = sec->output_section;
8969 }
8970
8971 /* Don't add in the section vma for relocatable output. */
8972 if (! relocatable_p)
8973 value += sec->vma;
8974 sym.st_value = value;
8975 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8976
8977 if (bfd_is_abs_section (sec)
8978 && type_ptr != NULL
8979 && type_ptr->internal_elf_sym.st_shndx != 0)
8980 {
8981 /* This symbol is in a real ELF section which we did
8982 not create as a BFD section. Undo the mapping done
8983 by copy_private_symbol_data. */
8984 shndx = type_ptr->internal_elf_sym.st_shndx;
8985 switch (shndx)
8986 {
8987 case MAP_ONESYMTAB:
8988 shndx = elf_onesymtab (abfd);
8989 break;
8990 case MAP_DYNSYMTAB:
8991 shndx = elf_dynsymtab (abfd);
8992 break;
8993 case MAP_STRTAB:
8994 shndx = elf_strtab_sec (abfd);
8995 break;
8996 case MAP_SHSTRTAB:
8997 shndx = elf_shstrtab_sec (abfd);
8998 break;
8999 case MAP_SYM_SHNDX:
9000 if (elf_symtab_shndx_list (abfd))
9001 shndx = elf_symtab_shndx_list (abfd)->ndx;
9002 break;
9003 case SHN_COMMON:
9004 case SHN_ABS:
9005 shndx = SHN_ABS;
9006 break;
9007 default:
9008 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
9009 {
9010 if (bed->symbol_section_index)
9011 shndx = bed->symbol_section_index (abfd, type_ptr);
9012 /* Otherwise just leave the index alone. */
9013 }
9014 else
9015 {
9016 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
9017 _bfd_error_handler (_("%pB: \
9018 Unable to handle section index %x in ELF symbol. Using ABS instead."),
9019 abfd, shndx);
9020 shndx = SHN_ABS;
9021 }
9022 break;
9023 }
9024 }
9025 else
9026 {
9027 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9028
9029 if (shndx == SHN_BAD)
9030 {
9031 asection *sec2;
9032
9033 /* Writing this would be a hell of a lot easier if
9034 we had some decent documentation on bfd, and
9035 knew what to expect of the library, and what to
9036 demand of applications. For example, it
9037 appears that `objcopy' might not set the
9038 section of a symbol to be a section that is
9039 actually in the output file. */
9040 sec2 = bfd_get_section_by_name (abfd, sec->name);
9041 if (sec2 != NULL)
9042 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
9043 if (shndx == SHN_BAD)
9044 {
9045 /* xgettext:c-format */
9046 _bfd_error_handler
9047 (_("unable to find equivalent output section"
9048 " for symbol '%s' from section '%s'"),
9049 syms[idx]->name ? syms[idx]->name : "<Local sym>",
9050 sec->name);
9051 bfd_set_error (bfd_error_invalid_operation);
9052 goto error_return;
9053 }
9054 }
9055 }
9056
9057 sym.st_shndx = shndx;
9058 }
9059
9060 if ((flags & BSF_THREAD_LOCAL) != 0)
9061 type = STT_TLS;
9062 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
9063 type = STT_GNU_IFUNC;
9064 else if ((flags & BSF_FUNCTION) != 0)
9065 type = STT_FUNC;
9066 else if ((flags & BSF_OBJECT) != 0)
9067 type = STT_OBJECT;
9068 else if ((flags & BSF_RELC) != 0)
9069 type = STT_RELC;
9070 else if ((flags & BSF_SRELC) != 0)
9071 type = STT_SRELC;
9072 else
9073 type = STT_NOTYPE;
9074
9075 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
9076 type = STT_TLS;
9077
9078 /* Processor-specific types. */
9079 if (type_ptr != NULL
9080 && bed->elf_backend_get_symbol_type)
9081 type = ((*bed->elf_backend_get_symbol_type)
9082 (&type_ptr->internal_elf_sym, type));
9083
9084 if (flags & BSF_SECTION_SYM)
9085 {
9086 if (flags & BSF_GLOBAL)
9087 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9088 else
9089 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9090 }
9091 else if (bfd_is_com_section (syms[idx]->section))
9092 {
9093 if (type != STT_TLS)
9094 {
9095 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9096 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9097 ? STT_COMMON : STT_OBJECT);
9098 else
9099 type = ((flags & BSF_ELF_COMMON) != 0
9100 ? STT_COMMON : STT_OBJECT);
9101 }
9102 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9103 }
9104 else if (bfd_is_und_section (syms[idx]->section))
9105 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9106 ? STB_WEAK
9107 : STB_GLOBAL),
9108 type);
9109 else if (flags & BSF_FILE)
9110 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9111 else
9112 {
9113 int bind = STB_LOCAL;
9114
9115 if (flags & BSF_LOCAL)
9116 bind = STB_LOCAL;
9117 else if (flags & BSF_GNU_UNIQUE)
9118 bind = STB_GNU_UNIQUE;
9119 else if (flags & BSF_WEAK)
9120 bind = STB_WEAK;
9121 else if (flags & BSF_GLOBAL)
9122 bind = STB_GLOBAL;
9123
9124 sym.st_info = ELF_ST_INFO (bind, type);
9125 }
9126
9127 if (type_ptr != NULL)
9128 {
9129 sym.st_other = type_ptr->internal_elf_sym.st_other;
9130 sym.st_target_internal
9131 = type_ptr->internal_elf_sym.st_target_internal;
9132 }
9133 else
9134 {
9135 sym.st_other = 0;
9136 sym.st_target_internal = 0;
9137 }
9138
9139 idx++;
9140 symstrtab[idx].sym = sym;
9141 symstrtab[idx].dest_index = outbound_syms_index;
9142
9143 outbound_syms_index++;
9144 }
9145
9146 /* Finalize the .strtab section. */
9147 _bfd_elf_strtab_finalize (stt);
9148
9149 /* Swap out the .strtab section. */
9150 for (idx = 0; idx <= symcount; idx++)
9151 {
9152 struct elf_sym_strtab *elfsym = &symstrtab[idx];
9153 if (elfsym->sym.st_name == (unsigned long) -1)
9154 elfsym->sym.st_name = 0;
9155 else
9156 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9157 elfsym->sym.st_name);
9158 if (info && info->callbacks->ctf_new_symbol)
9159 info->callbacks->ctf_new_symbol (elfsym->dest_index,
9160 &elfsym->sym);
9161
9162 /* Inform the linker of the addition of this symbol. */
9163
9164 bed->s->swap_symbol_out (abfd, &elfsym->sym,
9165 (outbound_syms
9166 + (elfsym->dest_index
9167 * bed->s->sizeof_sym)),
9168 NPTR_ADD (outbound_shndx,
9169 (elfsym->dest_index
9170 * sizeof (Elf_External_Sym_Shndx))));
9171 }
9172 free (symstrtab);
9173
9174 *sttp = stt;
9175 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9176 symstrtab_hdr->sh_type = SHT_STRTAB;
9177 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
9178 symstrtab_hdr->sh_addr = 0;
9179 symstrtab_hdr->sh_entsize = 0;
9180 symstrtab_hdr->sh_link = 0;
9181 symstrtab_hdr->sh_info = 0;
9182 symstrtab_hdr->sh_addralign = 1;
9183
9184 return true;
9185 }
9186
9187 /* Return the number of bytes required to hold the symtab vector.
9188
9189 Note that we base it on the count plus 1, since we will null terminate
9190 the vector allocated based on this size. However, the ELF symbol table
9191 always has a dummy entry as symbol #0, so it ends up even. */
9192
9193 long
9194 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
9195 {
9196 bfd_size_type symcount;
9197 long symtab_size;
9198 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9199
9200 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9201 if (symcount > LONG_MAX / sizeof (asymbol *))
9202 {
9203 bfd_set_error (bfd_error_file_too_big);
9204 return -1;
9205 }
9206 symtab_size = symcount * (sizeof (asymbol *));
9207 if (symcount == 0)
9208 symtab_size = sizeof (asymbol *);
9209 else if (!bfd_write_p (abfd))
9210 {
9211 ufile_ptr filesize = bfd_get_file_size (abfd);
9212
9213 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9214 {
9215 bfd_set_error (bfd_error_file_truncated);
9216 return -1;
9217 }
9218 }
9219
9220 return symtab_size;
9221 }
9222
9223 long
9224 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9225 {
9226 bfd_size_type symcount;
9227 long symtab_size;
9228 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9229
9230 if (elf_dynsymtab (abfd) == 0)
9231 {
9232 /* Check if there is dynamic symbol table. */
9233 symcount = elf_tdata (abfd)->dt_symtab_count;
9234 if (symcount)
9235 goto compute_symtab_size;
9236
9237 bfd_set_error (bfd_error_invalid_operation);
9238 return -1;
9239 }
9240
9241 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9242 if (symcount > LONG_MAX / sizeof (asymbol *))
9243 {
9244 bfd_set_error (bfd_error_file_too_big);
9245 return -1;
9246 }
9247
9248 compute_symtab_size:
9249 symtab_size = symcount * (sizeof (asymbol *));
9250 if (symcount == 0)
9251 symtab_size = sizeof (asymbol *);
9252 else if (!bfd_write_p (abfd))
9253 {
9254 ufile_ptr filesize = bfd_get_file_size (abfd);
9255
9256 if (filesize != 0 && (unsigned long) symtab_size > filesize)
9257 {
9258 bfd_set_error (bfd_error_file_truncated);
9259 return -1;
9260 }
9261 }
9262
9263 return symtab_size;
9264 }
9265
9266 long
9267 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9268 {
9269 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9270 {
9271 /* Sanity check reloc section size. */
9272 ufile_ptr filesize = bfd_get_file_size (abfd);
9273
9274 if (filesize != 0)
9275 {
9276 struct bfd_elf_section_data *d = elf_section_data (asect);
9277 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9278 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9279
9280 if (rel_size + rela_size > filesize
9281 || rel_size + rela_size < rel_size)
9282 {
9283 bfd_set_error (bfd_error_file_truncated);
9284 return -1;
9285 }
9286 }
9287 }
9288
9289 #if SIZEOF_LONG == SIZEOF_INT
9290 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9291 {
9292 bfd_set_error (bfd_error_file_too_big);
9293 return -1;
9294 }
9295 #endif
9296 return (asect->reloc_count + 1L) * sizeof (arelent *);
9297 }
9298
9299 /* Canonicalize the relocs. */
9300
9301 long
9302 _bfd_elf_canonicalize_reloc (bfd *abfd,
9303 sec_ptr section,
9304 arelent **relptr,
9305 asymbol **symbols)
9306 {
9307 arelent *tblptr;
9308 unsigned int i;
9309 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9310
9311 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9312 return -1;
9313
9314 tblptr = section->relocation;
9315 for (i = 0; i < section->reloc_count; i++)
9316 *relptr++ = tblptr++;
9317
9318 *relptr = NULL;
9319
9320 return section->reloc_count;
9321 }
9322
9323 long
9324 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9325 {
9326 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9327 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9328
9329 if (symcount >= 0)
9330 abfd->symcount = symcount;
9331 return symcount;
9332 }
9333
9334 long
9335 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9336 asymbol **allocation)
9337 {
9338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9339 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9340
9341 if (symcount >= 0)
9342 abfd->dynsymcount = symcount;
9343 return symcount;
9344 }
9345
9346 /* Return the size required for the dynamic reloc entries. Any loadable
9347 section that was actually installed in the BFD, and has type SHT_REL
9348 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9349 dynamic reloc section. */
9350
9351 long
9352 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9353 {
9354 bfd_size_type count, ext_rel_size;
9355 asection *s;
9356
9357 if (elf_dynsymtab (abfd) == 0)
9358 {
9359 bfd_set_error (bfd_error_invalid_operation);
9360 return -1;
9361 }
9362
9363 count = 1;
9364 ext_rel_size = 0;
9365 for (s = abfd->sections; s != NULL; s = s->next)
9366 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9367 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9368 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9369 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9370 {
9371 ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9372 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9373 {
9374 bfd_set_error (bfd_error_file_truncated);
9375 return -1;
9376 }
9377 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9378 if (count > LONG_MAX / sizeof (arelent *))
9379 {
9380 bfd_set_error (bfd_error_file_too_big);
9381 return -1;
9382 }
9383 }
9384 if (count > 1 && !bfd_write_p (abfd))
9385 {
9386 /* Sanity check reloc section sizes. */
9387 ufile_ptr filesize = bfd_get_file_size (abfd);
9388 if (filesize != 0 && ext_rel_size > filesize)
9389 {
9390 bfd_set_error (bfd_error_file_truncated);
9391 return -1;
9392 }
9393 }
9394 return count * sizeof (arelent *);
9395 }
9396
9397 /* Canonicalize the dynamic relocation entries. Note that we return the
9398 dynamic relocations as a single block, although they are actually
9399 associated with particular sections; the interface, which was
9400 designed for SunOS style shared libraries, expects that there is only
9401 one set of dynamic relocs. Any loadable section that was actually
9402 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9403 dynamic symbol table, is considered to be a dynamic reloc section. */
9404
9405 long
9406 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9407 arelent **storage,
9408 asymbol **syms)
9409 {
9410 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9411 asection *s;
9412 long ret;
9413
9414 if (elf_dynsymtab (abfd) == 0)
9415 {
9416 bfd_set_error (bfd_error_invalid_operation);
9417 return -1;
9418 }
9419
9420 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9421 ret = 0;
9422 for (s = abfd->sections; s != NULL; s = s->next)
9423 {
9424 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9425 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9426 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9427 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9428 {
9429 arelent *p;
9430 long count, i;
9431
9432 if (! (*slurp_relocs) (abfd, s, syms, true))
9433 return -1;
9434 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9435 p = s->relocation;
9436 for (i = 0; i < count; i++)
9437 *storage++ = p++;
9438 ret += count;
9439 }
9440 }
9441
9442 *storage = NULL;
9443
9444 return ret;
9445 }
9446
9447 /* Read in the version information. */
9449
9450 bool
9451 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9452 {
9453 bfd_byte *contents = NULL;
9454 unsigned int freeidx = 0;
9455 size_t amt;
9456
9457 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9458 {
9459 Elf_Internal_Shdr *hdr;
9460 Elf_External_Verneed *everneed;
9461 Elf_Internal_Verneed *iverneed;
9462 unsigned int i;
9463 bfd_byte *contents_end;
9464 size_t verneed_count;
9465 size_t verneed_size;
9466
9467 if (elf_tdata (abfd)->dt_verneed != NULL)
9468 {
9469 hdr = NULL;
9470 contents = elf_tdata (abfd)->dt_verneed;
9471 verneed_count = elf_tdata (abfd)->dt_verneed_count;
9472 verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9473 }
9474 else
9475 {
9476 hdr = &elf_tdata (abfd)->dynverref_hdr;
9477
9478 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9479 {
9480 error_return_bad_verref:
9481 _bfd_error_handler
9482 (_("%pB: .gnu.version_r invalid entry"), abfd);
9483 bfd_set_error (bfd_error_bad_value);
9484 error_return_verref:
9485 elf_tdata (abfd)->verref = NULL;
9486 elf_tdata (abfd)->cverrefs = 0;
9487 goto error_return;
9488 }
9489
9490 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9491 goto error_return_verref;
9492 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9493 if (contents == NULL)
9494 goto error_return_verref;
9495
9496 verneed_size = hdr->sh_size;
9497 verneed_count = hdr->sh_info;
9498 }
9499
9500 if (_bfd_mul_overflow (verneed_count,
9501 sizeof (Elf_Internal_Verneed), &amt))
9502 {
9503 bfd_set_error (bfd_error_file_too_big);
9504 goto error_return_verref;
9505 }
9506 if (amt == 0)
9507 goto error_return_verref;
9508 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9509 if (elf_tdata (abfd)->verref == NULL)
9510 goto error_return_verref;
9511
9512 BFD_ASSERT (sizeof (Elf_External_Verneed)
9513 == sizeof (Elf_External_Vernaux));
9514 contents_end = (contents + verneed_size
9515 - sizeof (Elf_External_Verneed));
9516 everneed = (Elf_External_Verneed *) contents;
9517 iverneed = elf_tdata (abfd)->verref;
9518 for (i = 0; i < verneed_count; i++, iverneed++)
9519 {
9520 Elf_External_Vernaux *evernaux;
9521 Elf_Internal_Vernaux *ivernaux;
9522 unsigned int j;
9523
9524 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9525
9526 iverneed->vn_bfd = abfd;
9527
9528 if (elf_use_dt_symtab_p (abfd))
9529 {
9530 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9531 iverneed->vn_filename
9532 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9533 else
9534 iverneed->vn_filename = NULL;
9535 }
9536 else if (hdr == NULL)
9537 goto error_return_bad_verref;
9538 else
9539 iverneed->vn_filename
9540 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9541 iverneed->vn_file);
9542 if (iverneed->vn_filename == NULL)
9543 goto error_return_bad_verref;
9544
9545 if (iverneed->vn_cnt == 0)
9546 iverneed->vn_auxptr = NULL;
9547 else
9548 {
9549 if (_bfd_mul_overflow (iverneed->vn_cnt,
9550 sizeof (Elf_Internal_Vernaux), &amt))
9551 {
9552 bfd_set_error (bfd_error_file_too_big);
9553 goto error_return_verref;
9554 }
9555 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9556 bfd_alloc (abfd, amt);
9557 if (iverneed->vn_auxptr == NULL)
9558 goto error_return_verref;
9559 }
9560
9561 if (iverneed->vn_aux
9562 > (size_t) (contents_end - (bfd_byte *) everneed))
9563 goto error_return_bad_verref;
9564
9565 evernaux = ((Elf_External_Vernaux *)
9566 ((bfd_byte *) everneed + iverneed->vn_aux));
9567 ivernaux = iverneed->vn_auxptr;
9568 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9569 {
9570 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9571
9572 if (elf_use_dt_symtab_p (abfd))
9573 {
9574 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9575 ivernaux->vna_nodename
9576 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9577 else
9578 ivernaux->vna_nodename = NULL;
9579 }
9580 else if (hdr == NULL)
9581 goto error_return_bad_verref;
9582 else
9583 ivernaux->vna_nodename
9584 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9585 ivernaux->vna_name);
9586 if (ivernaux->vna_nodename == NULL)
9587 goto error_return_bad_verref;
9588
9589 if (ivernaux->vna_other > freeidx)
9590 freeidx = ivernaux->vna_other;
9591
9592 ivernaux->vna_nextptr = NULL;
9593 if (ivernaux->vna_next == 0)
9594 {
9595 iverneed->vn_cnt = j + 1;
9596 break;
9597 }
9598 if (j + 1 < iverneed->vn_cnt)
9599 ivernaux->vna_nextptr = ivernaux + 1;
9600
9601 if (ivernaux->vna_next
9602 > (size_t) (contents_end - (bfd_byte *) evernaux))
9603 goto error_return_bad_verref;
9604
9605 evernaux = ((Elf_External_Vernaux *)
9606 ((bfd_byte *) evernaux + ivernaux->vna_next));
9607 }
9608
9609 iverneed->vn_nextref = NULL;
9610 if (iverneed->vn_next == 0)
9611 break;
9612 if (hdr != NULL && (i + 1 < hdr->sh_info))
9613 iverneed->vn_nextref = iverneed + 1;
9614
9615 if (iverneed->vn_next
9616 > (size_t) (contents_end - (bfd_byte *) everneed))
9617 goto error_return_bad_verref;
9618
9619 everneed = ((Elf_External_Verneed *)
9620 ((bfd_byte *) everneed + iverneed->vn_next));
9621 }
9622 elf_tdata (abfd)->cverrefs = i;
9623
9624 if (contents != elf_tdata (abfd)->dt_verneed)
9625 free (contents);
9626 contents = NULL;
9627 }
9628
9629 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9630 {
9631 Elf_Internal_Shdr *hdr;
9632 Elf_External_Verdef *everdef;
9633 Elf_Internal_Verdef *iverdef;
9634 Elf_Internal_Verdef *iverdefarr;
9635 Elf_Internal_Verdef iverdefmem;
9636 unsigned int i;
9637 unsigned int maxidx;
9638 bfd_byte *contents_end_def, *contents_end_aux;
9639 size_t verdef_count;
9640 size_t verdef_size;
9641
9642 if (elf_tdata (abfd)->dt_verdef != NULL)
9643 {
9644 hdr = NULL;
9645 contents = elf_tdata (abfd)->dt_verdef;
9646 verdef_count = elf_tdata (abfd)->dt_verdef_count;
9647 verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9648 }
9649 else
9650 {
9651 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9652
9653 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9654 {
9655 error_return_bad_verdef:
9656 _bfd_error_handler
9657 (_("%pB: .gnu.version_d invalid entry"), abfd);
9658 bfd_set_error (bfd_error_bad_value);
9659 error_return_verdef:
9660 elf_tdata (abfd)->verdef = NULL;
9661 elf_tdata (abfd)->cverdefs = 0;
9662 goto error_return;
9663 }
9664
9665 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9666 goto error_return_verdef;
9667 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9668 if (contents == NULL)
9669 goto error_return_verdef;
9670
9671 BFD_ASSERT (sizeof (Elf_External_Verdef)
9672 >= sizeof (Elf_External_Verdaux));
9673
9674 verdef_count = hdr->sh_info;
9675 verdef_size = hdr->sh_size;
9676 }
9677
9678 contents_end_def = (contents + verdef_size
9679 - sizeof (Elf_External_Verdef));
9680 contents_end_aux = (contents + verdef_size
9681 - sizeof (Elf_External_Verdaux));
9682
9683 /* We know the number of entries in the section but not the maximum
9684 index. Therefore we have to run through all entries and find
9685 the maximum. */
9686 everdef = (Elf_External_Verdef *) contents;
9687 maxidx = 0;
9688 for (i = 0; i < verdef_count; ++i)
9689 {
9690 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9691
9692 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9693 goto error_return_bad_verdef;
9694 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9695 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9696
9697 if (iverdefmem.vd_next == 0)
9698 break;
9699
9700 if (iverdefmem.vd_next
9701 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9702 goto error_return_bad_verdef;
9703
9704 everdef = ((Elf_External_Verdef *)
9705 ((bfd_byte *) everdef + iverdefmem.vd_next));
9706 }
9707
9708 if (default_imported_symver)
9709 {
9710 if (freeidx > maxidx)
9711 maxidx = ++freeidx;
9712 else
9713 freeidx = ++maxidx;
9714 }
9715 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9716 {
9717 bfd_set_error (bfd_error_file_too_big);
9718 goto error_return_verdef;
9719 }
9720
9721 if (amt == 0)
9722 goto error_return_verdef;
9723 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9724 if (elf_tdata (abfd)->verdef == NULL)
9725 goto error_return_verdef;
9726
9727 elf_tdata (abfd)->cverdefs = maxidx;
9728
9729 everdef = (Elf_External_Verdef *) contents;
9730 iverdefarr = elf_tdata (abfd)->verdef;
9731 for (i = 0; i < verdef_count; ++i)
9732 {
9733 Elf_External_Verdaux *everdaux;
9734 Elf_Internal_Verdaux *iverdaux;
9735 unsigned int j;
9736
9737 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9738
9739 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9740 goto error_return_bad_verdef;
9741
9742 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9743 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9744
9745 iverdef->vd_bfd = abfd;
9746
9747 if (iverdef->vd_cnt == 0)
9748 iverdef->vd_auxptr = NULL;
9749 else
9750 {
9751 if (_bfd_mul_overflow (iverdef->vd_cnt,
9752 sizeof (Elf_Internal_Verdaux), &amt))
9753 {
9754 bfd_set_error (bfd_error_file_too_big);
9755 goto error_return_verdef;
9756 }
9757 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9758 bfd_alloc (abfd, amt);
9759 if (iverdef->vd_auxptr == NULL)
9760 goto error_return_verdef;
9761 }
9762
9763 if (iverdef->vd_aux
9764 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9765 goto error_return_bad_verdef;
9766
9767 everdaux = ((Elf_External_Verdaux *)
9768 ((bfd_byte *) everdef + iverdef->vd_aux));
9769 iverdaux = iverdef->vd_auxptr;
9770 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9771 {
9772 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9773
9774 if (elf_use_dt_symtab_p (abfd))
9775 {
9776 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9777 iverdaux->vda_nodename
9778 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9779 else
9780 iverdaux->vda_nodename = NULL;
9781 }
9782 else
9783 iverdaux->vda_nodename
9784 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9785 iverdaux->vda_name);
9786 if (iverdaux->vda_nodename == NULL)
9787 goto error_return_bad_verdef;
9788
9789 iverdaux->vda_nextptr = NULL;
9790 if (iverdaux->vda_next == 0)
9791 {
9792 iverdef->vd_cnt = j + 1;
9793 break;
9794 }
9795 if (j + 1 < iverdef->vd_cnt)
9796 iverdaux->vda_nextptr = iverdaux + 1;
9797
9798 if (iverdaux->vda_next
9799 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9800 goto error_return_bad_verdef;
9801
9802 everdaux = ((Elf_External_Verdaux *)
9803 ((bfd_byte *) everdaux + iverdaux->vda_next));
9804 }
9805
9806 iverdef->vd_nodename = NULL;
9807 if (iverdef->vd_cnt)
9808 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9809
9810 iverdef->vd_nextdef = NULL;
9811 if (iverdef->vd_next == 0)
9812 break;
9813 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9814 iverdef->vd_nextdef = iverdef + 1;
9815
9816 everdef = ((Elf_External_Verdef *)
9817 ((bfd_byte *) everdef + iverdef->vd_next));
9818 }
9819
9820 if (contents != elf_tdata (abfd)->dt_verdef)
9821 free (contents);
9822 contents = NULL;
9823 }
9824 else if (default_imported_symver)
9825 {
9826 if (freeidx < 3)
9827 freeidx = 3;
9828 else
9829 freeidx++;
9830
9831 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9832 {
9833 bfd_set_error (bfd_error_file_too_big);
9834 goto error_return;
9835 }
9836 if (amt == 0)
9837 goto error_return;
9838 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9839 if (elf_tdata (abfd)->verdef == NULL)
9840 goto error_return;
9841
9842 elf_tdata (abfd)->cverdefs = freeidx;
9843 }
9844
9845 /* Create a default version based on the soname. */
9846 if (default_imported_symver)
9847 {
9848 Elf_Internal_Verdef *iverdef;
9849 Elf_Internal_Verdaux *iverdaux;
9850
9851 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9852
9853 iverdef->vd_version = VER_DEF_CURRENT;
9854 iverdef->vd_flags = 0;
9855 iverdef->vd_ndx = freeidx;
9856 iverdef->vd_cnt = 1;
9857
9858 iverdef->vd_bfd = abfd;
9859
9860 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9861 if (iverdef->vd_nodename == NULL)
9862 goto error_return_verdef;
9863 iverdef->vd_nextdef = NULL;
9864 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9865 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9866 if (iverdef->vd_auxptr == NULL)
9867 goto error_return_verdef;
9868
9869 iverdaux = iverdef->vd_auxptr;
9870 iverdaux->vda_nodename = iverdef->vd_nodename;
9871 }
9872
9873 return true;
9874
9875 error_return:
9876 if (contents != elf_tdata (abfd)->dt_verneed
9877 && contents != elf_tdata (abfd)->dt_verdef)
9878 free (contents);
9879 return false;
9880 }
9881
9882 asymbol *
9884 _bfd_elf_make_empty_symbol (bfd *abfd)
9885 {
9886 elf_symbol_type *newsym;
9887
9888 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9889 if (!newsym)
9890 return NULL;
9891 newsym->symbol.the_bfd = abfd;
9892 return &newsym->symbol;
9893 }
9894
9895 void
9896 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9897 asymbol *symbol,
9898 symbol_info *ret)
9899 {
9900 bfd_symbol_info (symbol, ret);
9901 }
9902
9903 /* Return whether a symbol name implies a local symbol. Most targets
9904 use this function for the is_local_label_name entry point, but some
9905 override it. */
9906
9907 bool
9908 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9909 const char *name)
9910 {
9911 /* Normal local symbols start with ``.L''. */
9912 if (name[0] == '.' && name[1] == 'L')
9913 return true;
9914
9915 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9916 DWARF debugging symbols starting with ``..''. */
9917 if (name[0] == '.' && name[1] == '.')
9918 return true;
9919
9920 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9921 emitting DWARF debugging output. I suspect this is actually a
9922 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9923 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9924 underscore to be emitted on some ELF targets). For ease of use,
9925 we treat such symbols as local. */
9926 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9927 return true;
9928
9929 /* Treat assembler generated fake symbols, dollar local labels and
9930 forward-backward labels (aka local labels) as locals.
9931 These labels have the form:
9932
9933 L0^A.* (fake symbols)
9934
9935 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9936
9937 Versions which start with .L will have already been matched above,
9938 so we only need to match the rest. */
9939 if (name[0] == 'L' && ISDIGIT (name[1]))
9940 {
9941 bool ret = false;
9942 const char * p;
9943 char c;
9944
9945 for (p = name + 2; (c = *p); p++)
9946 {
9947 if (c == 1 || c == 2)
9948 {
9949 if (c == 1 && p == name + 2)
9950 /* A fake symbol. */
9951 return true;
9952
9953 /* FIXME: We are being paranoid here and treating symbols like
9954 L0^Bfoo as if there were non-local, on the grounds that the
9955 assembler will never generate them. But can any symbol
9956 containing an ASCII value in the range 1-31 ever be anything
9957 other than some kind of local ? */
9958 ret = true;
9959 }
9960
9961 if (! ISDIGIT (c))
9962 {
9963 ret = false;
9964 break;
9965 }
9966 }
9967 return ret;
9968 }
9969
9970 return false;
9971 }
9972
9973 alent *
9974 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9975 asymbol *symbol ATTRIBUTE_UNUSED)
9976 {
9977 abort ();
9978 return NULL;
9979 }
9980
9981 bool
9982 _bfd_elf_set_arch_mach (bfd *abfd,
9983 enum bfd_architecture arch,
9984 unsigned long machine)
9985 {
9986 /* If this isn't the right architecture for this backend, and this
9987 isn't the generic backend, fail. */
9988 if (arch != get_elf_backend_data (abfd)->arch
9989 && arch != bfd_arch_unknown
9990 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9991 return false;
9992
9993 return bfd_default_set_arch_mach (abfd, arch, machine);
9994 }
9995
9996 /* Find the nearest line to a particular section and offset,
9997 for error reporting. */
9998
9999 bool
10000 _bfd_elf_find_nearest_line (bfd *abfd,
10001 asymbol **symbols,
10002 asection *section,
10003 bfd_vma offset,
10004 const char **filename_ptr,
10005 const char **functionname_ptr,
10006 unsigned int *line_ptr,
10007 unsigned int *discriminator_ptr)
10008 {
10009 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
10010 offset, filename_ptr,
10011 functionname_ptr, line_ptr,
10012 discriminator_ptr);
10013 }
10014
10015 /* Find the nearest line to a particular section and offset,
10016 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
10017 can be optionally specified. */
10018
10019 bool
10020 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
10021 const char *alt_filename,
10022 asymbol **symbols,
10023 asection *section,
10024 bfd_vma offset,
10025 const char **filename_ptr,
10026 const char **functionname_ptr,
10027 unsigned int *line_ptr,
10028 unsigned int *discriminator_ptr)
10029 {
10030 bool found;
10031
10032 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
10033 section, offset, filename_ptr,
10034 functionname_ptr, line_ptr,
10035 discriminator_ptr,
10036 dwarf_debug_sections,
10037 &elf_tdata (abfd)->dwarf2_find_line_info))
10038 return true;
10039
10040 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
10041 filename_ptr, functionname_ptr, line_ptr))
10042 {
10043 if (!*functionname_ptr)
10044 _bfd_elf_find_function (abfd, symbols, section, offset,
10045 *filename_ptr ? NULL : filename_ptr,
10046 functionname_ptr);
10047 return true;
10048 }
10049
10050 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
10051 &found, filename_ptr,
10052 functionname_ptr, line_ptr,
10053 &elf_tdata (abfd)->line_info))
10054 return false;
10055 if (found && (*functionname_ptr || *line_ptr))
10056 return true;
10057
10058 if (symbols == NULL)
10059 return false;
10060
10061 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
10062 filename_ptr, functionname_ptr))
10063 return false;
10064
10065 *line_ptr = 0;
10066 return true;
10067 }
10068
10069 /* Find the line for a symbol. */
10070
10071 bool
10072 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
10073 const char **filename_ptr, unsigned int *line_ptr)
10074 {
10075 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10076 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
10077 filename_ptr, NULL, line_ptr, NULL,
10078 dwarf_debug_sections,
10079 &tdata->dwarf2_find_line_info);
10080 }
10081
10082 /* After a call to bfd_find_nearest_line, successive calls to
10083 bfd_find_inliner_info can be used to get source information about
10084 each level of function inlining that terminated at the address
10085 passed to bfd_find_nearest_line. Currently this is only supported
10086 for DWARF2 with appropriate DWARF3 extensions. */
10087
10088 bool
10089 _bfd_elf_find_inliner_info (bfd *abfd,
10090 const char **filename_ptr,
10091 const char **functionname_ptr,
10092 unsigned int *line_ptr)
10093 {
10094 struct elf_obj_tdata *tdata = elf_tdata (abfd);
10095 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10096 functionname_ptr, line_ptr,
10097 &tdata->dwarf2_find_line_info);
10098 }
10099
10100 int
10101 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10102 {
10103 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10104 int ret = bed->s->sizeof_ehdr;
10105
10106 if (!bfd_link_relocatable (info))
10107 {
10108 bfd_size_type phdr_size = elf_program_header_size (abfd);
10109
10110 if (phdr_size == (bfd_size_type) -1)
10111 {
10112 struct elf_segment_map *m;
10113
10114 phdr_size = 0;
10115 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10116 phdr_size += bed->s->sizeof_phdr;
10117
10118 if (phdr_size == 0)
10119 phdr_size = get_program_header_size (abfd, info);
10120 }
10121
10122 elf_program_header_size (abfd) = phdr_size;
10123 ret += phdr_size;
10124 }
10125
10126 return ret;
10127 }
10128
10129 bool
10130 _bfd_elf_set_section_contents (bfd *abfd,
10131 sec_ptr section,
10132 const void *location,
10133 file_ptr offset,
10134 bfd_size_type count)
10135 {
10136 Elf_Internal_Shdr *hdr;
10137
10138 if (! abfd->output_has_begun
10139 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10140 return false;
10141
10142 if (!count)
10143 return true;
10144
10145 hdr = &elf_section_data (section)->this_hdr;
10146 if (hdr->sh_offset == (file_ptr) -1)
10147 {
10148 unsigned char *contents;
10149
10150 if (bfd_section_is_ctf (section))
10151 /* Nothing to do with this section: the contents are generated
10152 later. */
10153 return true;
10154
10155 if ((offset + count) > hdr->sh_size)
10156 {
10157 _bfd_error_handler
10158 (_("%pB:%pA: error: attempting to write"
10159 " over the end of the section"),
10160 abfd, section);
10161
10162 bfd_set_error (bfd_error_invalid_operation);
10163 return false;
10164 }
10165
10166 contents = hdr->contents;
10167 if (contents == NULL)
10168 {
10169 _bfd_error_handler
10170 (_("%pB:%pA: error: attempting to write"
10171 " section into an empty buffer"),
10172 abfd, section);
10173
10174 bfd_set_error (bfd_error_invalid_operation);
10175 return false;
10176 }
10177
10178 memcpy (contents + offset, location, count);
10179 return true;
10180 }
10181
10182 return _bfd_generic_set_section_contents (abfd, section,
10183 location, offset, count);
10184 }
10185
10186 bool
10187 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10188 arelent *cache_ptr ATTRIBUTE_UNUSED,
10189 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10190 {
10191 abort ();
10192 return false;
10193 }
10194
10195 /* Try to convert a non-ELF reloc into an ELF one. */
10196
10197 bool
10198 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10199 {
10200 /* Check whether we really have an ELF howto. */
10201
10202 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10203 {
10204 bfd_reloc_code_real_type code;
10205 reloc_howto_type *howto;
10206
10207 /* Alien reloc: Try to determine its type to replace it with an
10208 equivalent ELF reloc. */
10209
10210 if (areloc->howto->pc_relative)
10211 {
10212 switch (areloc->howto->bitsize)
10213 {
10214 case 8:
10215 code = BFD_RELOC_8_PCREL;
10216 break;
10217 case 12:
10218 code = BFD_RELOC_12_PCREL;
10219 break;
10220 case 16:
10221 code = BFD_RELOC_16_PCREL;
10222 break;
10223 case 24:
10224 code = BFD_RELOC_24_PCREL;
10225 break;
10226 case 32:
10227 code = BFD_RELOC_32_PCREL;
10228 break;
10229 case 64:
10230 code = BFD_RELOC_64_PCREL;
10231 break;
10232 default:
10233 goto fail;
10234 }
10235
10236 howto = bfd_reloc_type_lookup (abfd, code);
10237
10238 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10239 {
10240 if (howto->pcrel_offset)
10241 areloc->addend += areloc->address;
10242 else
10243 areloc->addend -= areloc->address; /* addend is unsigned!! */
10244 }
10245 }
10246 else
10247 {
10248 switch (areloc->howto->bitsize)
10249 {
10250 case 8:
10251 code = BFD_RELOC_8;
10252 break;
10253 case 14:
10254 code = BFD_RELOC_14;
10255 break;
10256 case 16:
10257 code = BFD_RELOC_16;
10258 break;
10259 case 26:
10260 code = BFD_RELOC_26;
10261 break;
10262 case 32:
10263 code = BFD_RELOC_32;
10264 break;
10265 case 64:
10266 code = BFD_RELOC_64;
10267 break;
10268 default:
10269 goto fail;
10270 }
10271
10272 howto = bfd_reloc_type_lookup (abfd, code);
10273 }
10274
10275 if (howto)
10276 areloc->howto = howto;
10277 else
10278 goto fail;
10279 }
10280
10281 return true;
10282
10283 fail:
10284 /* xgettext:c-format */
10285 _bfd_error_handler (_("%pB: %s unsupported"),
10286 abfd, areloc->howto->name);
10287 bfd_set_error (bfd_error_sorry);
10288 return false;
10289 }
10290
10291 bool
10292 _bfd_elf_free_cached_info (bfd *abfd)
10293 {
10294 struct elf_obj_tdata *tdata;
10295
10296 if ((bfd_get_format (abfd) == bfd_object
10297 || bfd_get_format (abfd) == bfd_core)
10298 && (tdata = elf_tdata (abfd)) != NULL)
10299 {
10300 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10301 _bfd_elf_strtab_free (elf_shstrtab (abfd));
10302 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10303 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10304 _bfd_stab_cleanup (abfd, &tdata->line_info);
10305 }
10306
10307 return _bfd_generic_bfd_free_cached_info (abfd);
10308 }
10309
10310 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10311 in the relocation's offset. Thus we cannot allow any sort of sanity
10312 range-checking to interfere. There is nothing else to do in processing
10313 this reloc. */
10314
10315 bfd_reloc_status_type
10316 _bfd_elf_rel_vtable_reloc_fn
10317 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10318 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10319 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10320 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10321 {
10322 return bfd_reloc_ok;
10323 }
10324
10325 /* Elf core file support. Much of this only works on native
10327 toolchains, since we rely on knowing the
10328 machine-dependent procfs structure in order to pick
10329 out details about the corefile. */
10330
10331 #ifdef HAVE_SYS_PROCFS_H
10332 # include <sys/procfs.h>
10333 #endif
10334
10335 /* Return a PID that identifies a "thread" for threaded cores, or the
10336 PID of the main process for non-threaded cores. */
10337
10338 static int
10339 elfcore_make_pid (bfd *abfd)
10340 {
10341 int pid;
10342
10343 pid = elf_tdata (abfd)->core->lwpid;
10344 if (pid == 0)
10345 pid = elf_tdata (abfd)->core->pid;
10346
10347 return pid;
10348 }
10349
10350 /* If there isn't a section called NAME, make one, using data from
10351 SECT. Note, this function will generate a reference to NAME, so
10352 you shouldn't deallocate or overwrite it. */
10353
10354 static bool
10355 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10356 {
10357 asection *sect2;
10358
10359 if (bfd_get_section_by_name (abfd, name) != NULL)
10360 return true;
10361
10362 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10363 if (sect2 == NULL)
10364 return false;
10365
10366 sect2->size = sect->size;
10367 sect2->filepos = sect->filepos;
10368 sect2->alignment_power = sect->alignment_power;
10369 return true;
10370 }
10371
10372 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
10373 actually creates up to two pseudosections:
10374 - For the single-threaded case, a section named NAME, unless
10375 such a section already exists.
10376 - For the multi-threaded case, a section named "NAME/PID", where
10377 PID is elfcore_make_pid (abfd).
10378 Both pseudosections have identical contents. */
10379 bool
10380 _bfd_elfcore_make_pseudosection (bfd *abfd,
10381 char *name,
10382 size_t size,
10383 ufile_ptr filepos)
10384 {
10385 char buf[100];
10386 char *threaded_name;
10387 size_t len;
10388 asection *sect;
10389
10390 /* Build the section name. */
10391
10392 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10393 len = strlen (buf) + 1;
10394 threaded_name = (char *) bfd_alloc (abfd, len);
10395 if (threaded_name == NULL)
10396 return false;
10397 memcpy (threaded_name, buf, len);
10398
10399 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10400 SEC_HAS_CONTENTS);
10401 if (sect == NULL)
10402 return false;
10403 sect->size = size;
10404 sect->filepos = filepos;
10405 sect->alignment_power = 2;
10406
10407 return elfcore_maybe_make_sect (abfd, name, sect);
10408 }
10409
10410 static bool
10411 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10412 size_t offs)
10413 {
10414 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10415 SEC_HAS_CONTENTS);
10416
10417 if (sect == NULL)
10418 return false;
10419
10420 sect->size = note->descsz - offs;
10421 sect->filepos = note->descpos + offs;
10422 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10423
10424 return true;
10425 }
10426
10427 /* prstatus_t exists on:
10428 solaris 2.5+
10429 linux 2.[01] + glibc
10430 unixware 4.2
10431 */
10432
10433 #if defined (HAVE_PRSTATUS_T)
10434
10435 static bool
10436 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10437 {
10438 size_t size;
10439 int offset;
10440
10441 if (note->descsz == sizeof (prstatus_t))
10442 {
10443 prstatus_t prstat;
10444
10445 size = sizeof (prstat.pr_reg);
10446 offset = offsetof (prstatus_t, pr_reg);
10447 memcpy (&prstat, note->descdata, sizeof (prstat));
10448
10449 /* Do not overwrite the core signal if it
10450 has already been set by another thread. */
10451 if (elf_tdata (abfd)->core->signal == 0)
10452 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10453 if (elf_tdata (abfd)->core->pid == 0)
10454 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10455
10456 /* pr_who exists on:
10457 solaris 2.5+
10458 unixware 4.2
10459 pr_who doesn't exist on:
10460 linux 2.[01]
10461 */
10462 #if defined (HAVE_PRSTATUS_T_PR_WHO)
10463 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10464 #else
10465 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10466 #endif
10467 }
10468 #if defined (HAVE_PRSTATUS32_T)
10469 else if (note->descsz == sizeof (prstatus32_t))
10470 {
10471 /* 64-bit host, 32-bit corefile */
10472 prstatus32_t prstat;
10473
10474 size = sizeof (prstat.pr_reg);
10475 offset = offsetof (prstatus32_t, pr_reg);
10476 memcpy (&prstat, note->descdata, sizeof (prstat));
10477
10478 /* Do not overwrite the core signal if it
10479 has already been set by another thread. */
10480 if (elf_tdata (abfd)->core->signal == 0)
10481 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10482 if (elf_tdata (abfd)->core->pid == 0)
10483 elf_tdata (abfd)->core->pid = prstat.pr_pid;
10484
10485 /* pr_who exists on:
10486 solaris 2.5+
10487 unixware 4.2
10488 pr_who doesn't exist on:
10489 linux 2.[01]
10490 */
10491 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
10492 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10493 #else
10494 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10495 #endif
10496 }
10497 #endif /* HAVE_PRSTATUS32_T */
10498 else
10499 {
10500 /* Fail - we don't know how to handle any other
10501 note size (ie. data object type). */
10502 return true;
10503 }
10504
10505 /* Make a ".reg/999" section and a ".reg" section. */
10506 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10507 size, note->descpos + offset);
10508 }
10509 #endif /* defined (HAVE_PRSTATUS_T) */
10510
10511 /* Create a pseudosection containing the exact contents of NOTE. */
10512 static bool
10513 elfcore_make_note_pseudosection (bfd *abfd,
10514 char *name,
10515 Elf_Internal_Note *note)
10516 {
10517 return _bfd_elfcore_make_pseudosection (abfd, name,
10518 note->descsz, note->descpos);
10519 }
10520
10521 /* There isn't a consistent prfpregset_t across platforms,
10522 but it doesn't matter, because we don't have to pick this
10523 data structure apart. */
10524
10525 static bool
10526 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10527 {
10528 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10529 }
10530
10531 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10532 type of NT_PRXFPREG. Just include the whole note's contents
10533 literally. */
10534
10535 static bool
10536 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10537 {
10538 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10539 }
10540
10541 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10542 with a note type of NT_X86_XSTATE. Just include the whole note's
10543 contents literally. */
10544
10545 static bool
10546 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10547 {
10548 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
10549 }
10550
10551 static bool
10552 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10553 {
10554 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
10555 }
10556
10557 static bool
10558 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10559 {
10560 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
10561 }
10562
10563 static bool
10564 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10565 {
10566 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
10567 }
10568
10569 static bool
10570 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10571 {
10572 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
10573 }
10574
10575 static bool
10576 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10577 {
10578 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
10579 }
10580
10581 static bool
10582 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10583 {
10584 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
10585 }
10586
10587 static bool
10588 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10589 {
10590 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
10591 }
10592
10593 static bool
10594 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10595 {
10596 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
10597 }
10598
10599 static bool
10600 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10601 {
10602 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
10603 }
10604
10605 static bool
10606 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10607 {
10608 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
10609 }
10610
10611 static bool
10612 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10613 {
10614 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
10615 }
10616
10617 static bool
10618 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10619 {
10620 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
10621 }
10622
10623 static bool
10624 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10625 {
10626 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
10627 }
10628
10629 static bool
10630 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10631 {
10632 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
10633 }
10634
10635 static bool
10636 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10637 {
10638 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
10639 }
10640
10641 static bool
10642 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10643 {
10644 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10645 }
10646
10647 static bool
10648 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10649 {
10650 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10651 }
10652
10653 static bool
10654 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10655 {
10656 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10657 }
10658
10659 static bool
10660 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10661 {
10662 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10663 }
10664
10665 static bool
10666 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10667 {
10668 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10669 }
10670
10671 static bool
10672 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10673 {
10674 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10675 }
10676
10677 static bool
10678 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10679 {
10680 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10681 }
10682
10683 static bool
10684 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10685 {
10686 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10687 }
10688
10689 static bool
10690 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10691 {
10692 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10693 }
10694
10695 static bool
10696 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10697 {
10698 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10699 }
10700
10701 static bool
10702 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10703 {
10704 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10705 }
10706
10707 static bool
10708 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10709 {
10710 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10711 }
10712
10713 static bool
10714 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10715 {
10716 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10717 }
10718
10719 static bool
10720 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10721 {
10722 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10723 }
10724
10725 static bool
10726 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10727 {
10728 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10729 }
10730
10731 static bool
10732 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10733 {
10734 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10735 }
10736
10737 static bool
10738 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10739 {
10740 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10741 }
10742
10743 static bool
10744 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10745 {
10746 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10747 }
10748
10749 static bool
10750 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10751 {
10752 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10753 }
10754
10755 static bool
10756 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10757 {
10758 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10759 note);
10760 }
10761
10762 static bool
10763 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10764 {
10765 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
10766 }
10767
10768 static bool
10769 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10770 {
10771 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
10772 }
10773
10774 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if
10775 successful, otherwise return FALSE. */
10776
10777 static bool
10778 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10779 {
10780 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
10781 }
10782
10783 static bool
10784 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10785 {
10786 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10787 }
10788
10789 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10790 successful otherwise, return FALSE. */
10791
10792 static bool
10793 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10794 {
10795 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10796 }
10797
10798 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10799 successful otherwise, return FALSE. */
10800
10801 static bool
10802 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10803 {
10804 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10805 }
10806
10807 static bool
10808 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10809 {
10810 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10811 }
10812
10813 static bool
10814 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10815 {
10816 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10817 }
10818
10819 static bool
10820 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10821 {
10822 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10823 }
10824
10825 static bool
10826 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10827 {
10828 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10829 }
10830
10831 #if defined (HAVE_PRPSINFO_T)
10832 typedef prpsinfo_t elfcore_psinfo_t;
10833 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10834 typedef prpsinfo32_t elfcore_psinfo32_t;
10835 #endif
10836 #endif
10837
10838 #if defined (HAVE_PSINFO_T)
10839 typedef psinfo_t elfcore_psinfo_t;
10840 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10841 typedef psinfo32_t elfcore_psinfo32_t;
10842 #endif
10843 #endif
10844
10845 /* return a malloc'ed copy of a string at START which is at
10846 most MAX bytes long, possibly without a terminating '\0'.
10847 the copy will always have a terminating '\0'. */
10848
10849 char *
10850 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10851 {
10852 char *dups;
10853 char *end = (char *) memchr (start, '\0', max);
10854 size_t len;
10855
10856 if (end == NULL)
10857 len = max;
10858 else
10859 len = end - start;
10860
10861 dups = (char *) bfd_alloc (abfd, len + 1);
10862 if (dups == NULL)
10863 return NULL;
10864
10865 memcpy (dups, start, len);
10866 dups[len] = '\0';
10867
10868 return dups;
10869 }
10870
10871 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10872 static bool
10873 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10874 {
10875 if (note->descsz == sizeof (elfcore_psinfo_t))
10876 {
10877 elfcore_psinfo_t psinfo;
10878
10879 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10880
10881 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10882 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10883 #endif
10884 elf_tdata (abfd)->core->program
10885 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10886 sizeof (psinfo.pr_fname));
10887
10888 elf_tdata (abfd)->core->command
10889 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10890 sizeof (psinfo.pr_psargs));
10891 }
10892 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10893 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10894 {
10895 /* 64-bit host, 32-bit corefile */
10896 elfcore_psinfo32_t psinfo;
10897
10898 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10899
10900 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10901 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10902 #endif
10903 elf_tdata (abfd)->core->program
10904 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10905 sizeof (psinfo.pr_fname));
10906
10907 elf_tdata (abfd)->core->command
10908 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10909 sizeof (psinfo.pr_psargs));
10910 }
10911 #endif
10912
10913 else
10914 {
10915 /* Fail - we don't know how to handle any other
10916 note size (ie. data object type). */
10917 return true;
10918 }
10919
10920 /* Note that for some reason, a spurious space is tacked
10921 onto the end of the args in some (at least one anyway)
10922 implementations, so strip it off if it exists. */
10923
10924 {
10925 char *command = elf_tdata (abfd)->core->command;
10926 int n = strlen (command);
10927
10928 if (0 < n && command[n - 1] == ' ')
10929 command[n - 1] = '\0';
10930 }
10931
10932 return true;
10933 }
10934 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10935
10936 #if defined (HAVE_PSTATUS_T)
10937 static bool
10938 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10939 {
10940 if (note->descsz == sizeof (pstatus_t)
10941 #if defined (HAVE_PXSTATUS_T)
10942 || note->descsz == sizeof (pxstatus_t)
10943 #endif
10944 )
10945 {
10946 pstatus_t pstat;
10947
10948 memcpy (&pstat, note->descdata, sizeof (pstat));
10949
10950 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10951 }
10952 #if defined (HAVE_PSTATUS32_T)
10953 else if (note->descsz == sizeof (pstatus32_t))
10954 {
10955 /* 64-bit host, 32-bit corefile */
10956 pstatus32_t pstat;
10957
10958 memcpy (&pstat, note->descdata, sizeof (pstat));
10959
10960 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10961 }
10962 #endif
10963 /* Could grab some more details from the "representative"
10964 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10965 NT_LWPSTATUS note, presumably. */
10966
10967 return true;
10968 }
10969 #endif /* defined (HAVE_PSTATUS_T) */
10970
10971 #if defined (HAVE_LWPSTATUS_T)
10972 static bool
10973 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10974 {
10975 lwpstatus_t lwpstat;
10976 char buf[100];
10977 char *name;
10978 size_t len;
10979 asection *sect;
10980
10981 if (note->descsz != sizeof (lwpstat)
10982 #if defined (HAVE_LWPXSTATUS_T)
10983 && note->descsz != sizeof (lwpxstatus_t)
10984 #endif
10985 )
10986 return true;
10987
10988 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10989
10990 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10991 /* Do not overwrite the core signal if it has already been set by
10992 another thread. */
10993 if (elf_tdata (abfd)->core->signal == 0)
10994 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10995
10996 /* Make a ".reg/999" section. */
10997
10998 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10999 len = strlen (buf) + 1;
11000 name = bfd_alloc (abfd, len);
11001 if (name == NULL)
11002 return false;
11003 memcpy (name, buf, len);
11004
11005 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11006 if (sect == NULL)
11007 return false;
11008
11009 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11010 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
11011 sect->filepos = note->descpos
11012 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
11013 #endif
11014
11015 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11016 sect->size = sizeof (lwpstat.pr_reg);
11017 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
11018 #endif
11019
11020 sect->alignment_power = 2;
11021
11022 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
11023 return false;
11024
11025 /* Make a ".reg2/999" section */
11026
11027 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
11028 len = strlen (buf) + 1;
11029 name = bfd_alloc (abfd, len);
11030 if (name == NULL)
11031 return false;
11032 memcpy (name, buf, len);
11033
11034 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11035 if (sect == NULL)
11036 return false;
11037
11038 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11039 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11040 sect->filepos = note->descpos
11041 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11042 #endif
11043
11044 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11045 sect->size = sizeof (lwpstat.pr_fpreg);
11046 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11047 #endif
11048
11049 sect->alignment_power = 2;
11050
11051 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
11052 }
11053 #endif /* defined (HAVE_LWPSTATUS_T) */
11054
11055 /* These constants, and the structure offsets used below, are defined by
11056 Cygwin's core_dump.h */
11057 #define NOTE_INFO_PROCESS 1
11058 #define NOTE_INFO_THREAD 2
11059 #define NOTE_INFO_MODULE 3
11060 #define NOTE_INFO_MODULE64 4
11061
11062 static bool
11063 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11064 {
11065 char buf[30];
11066 char *name;
11067 size_t len;
11068 unsigned int name_size;
11069 asection *sect;
11070 unsigned int type;
11071 int is_active_thread;
11072 bfd_vma base_addr;
11073
11074 if (note->descsz < 4)
11075 return true;
11076
11077 if (! startswith (note->namedata, "win32"))
11078 return true;
11079
11080 type = bfd_get_32 (abfd, note->descdata);
11081
11082 struct
11083 {
11084 const char *type_name;
11085 unsigned long min_size;
11086 } size_check[] =
11087 {
11088 { "NOTE_INFO_PROCESS", 12 },
11089 { "NOTE_INFO_THREAD", 12 },
11090 { "NOTE_INFO_MODULE", 12 },
11091 { "NOTE_INFO_MODULE64", 16 },
11092 };
11093
11094 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11095 return true;
11096
11097 if (note->descsz < size_check[type - 1].min_size)
11098 {
11099 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11100 " is too small"),
11101 abfd, size_check[type - 1].type_name, note->descsz);
11102 return true;
11103 }
11104
11105 switch (type)
11106 {
11107 case NOTE_INFO_PROCESS:
11108 /* FIXME: need to add ->core->command. */
11109 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11110 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11111 break;
11112
11113 case NOTE_INFO_THREAD:
11114 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11115 structure. */
11116 /* thread_info.tid */
11117 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11118
11119 len = strlen (buf) + 1;
11120 name = (char *) bfd_alloc (abfd, len);
11121 if (name == NULL)
11122 return false;
11123
11124 memcpy (name, buf, len);
11125
11126 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11127 if (sect == NULL)
11128 return false;
11129
11130 /* sizeof (thread_info.thread_context) */
11131 sect->size = note->descsz - 12;
11132 /* offsetof (thread_info.thread_context) */
11133 sect->filepos = note->descpos + 12;
11134 sect->alignment_power = 2;
11135
11136 /* thread_info.is_active_thread */
11137 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11138
11139 if (is_active_thread)
11140 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
11141 return false;
11142 break;
11143
11144 case NOTE_INFO_MODULE:
11145 case NOTE_INFO_MODULE64:
11146 /* Make a ".module/xxxxxxxx" section. */
11147 if (type == NOTE_INFO_MODULE)
11148 {
11149 /* module_info.base_address */
11150 base_addr = bfd_get_32 (abfd, note->descdata + 4);
11151 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11152 /* module_info.module_name_size */
11153 name_size = bfd_get_32 (abfd, note->descdata + 8);
11154 }
11155 else /* NOTE_INFO_MODULE64 */
11156 {
11157 /* module_info.base_address */
11158 base_addr = bfd_get_64 (abfd, note->descdata + 4);
11159 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11160 /* module_info.module_name_size */
11161 name_size = bfd_get_32 (abfd, note->descdata + 12);
11162 }
11163
11164 len = strlen (buf) + 1;
11165 name = (char *) bfd_alloc (abfd, len);
11166 if (name == NULL)
11167 return false;
11168
11169 memcpy (name, buf, len);
11170
11171 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11172
11173 if (sect == NULL)
11174 return false;
11175
11176 if (note->descsz < 12 + name_size)
11177 {
11178 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11179 " is too small to contain a name of size %u"),
11180 abfd, note->descsz, name_size);
11181 return true;
11182 }
11183
11184 sect->size = note->descsz;
11185 sect->filepos = note->descpos;
11186 sect->alignment_power = 2;
11187 break;
11188
11189 default:
11190 return true;
11191 }
11192
11193 return true;
11194 }
11195
11196 static bool
11197 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11198 {
11199 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11200
11201 switch (note->type)
11202 {
11203 default:
11204 return true;
11205
11206 case NT_PRSTATUS:
11207 if (bed->elf_backend_grok_prstatus)
11208 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11209 return true;
11210 #if defined (HAVE_PRSTATUS_T)
11211 return elfcore_grok_prstatus (abfd, note);
11212 #else
11213 return true;
11214 #endif
11215
11216 #if defined (HAVE_PSTATUS_T)
11217 case NT_PSTATUS:
11218 return elfcore_grok_pstatus (abfd, note);
11219 #endif
11220
11221 #if defined (HAVE_LWPSTATUS_T)
11222 case NT_LWPSTATUS:
11223 return elfcore_grok_lwpstatus (abfd, note);
11224 #endif
11225
11226 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
11227 return elfcore_grok_prfpreg (abfd, note);
11228
11229 case NT_WIN32PSTATUS:
11230 return elfcore_grok_win32pstatus (abfd, note);
11231
11232 case NT_PRXFPREG: /* Linux SSE extension */
11233 if (note->namesz == 6
11234 && strcmp (note->namedata, "LINUX") == 0)
11235 return elfcore_grok_prxfpreg (abfd, note);
11236 else
11237 return true;
11238
11239 case NT_X86_XSTATE: /* Linux XSAVE extension */
11240 if (note->namesz == 6
11241 && strcmp (note->namedata, "LINUX") == 0)
11242 return elfcore_grok_xstatereg (abfd, note);
11243 else
11244 return true;
11245
11246 case NT_PPC_VMX:
11247 if (note->namesz == 6
11248 && strcmp (note->namedata, "LINUX") == 0)
11249 return elfcore_grok_ppc_vmx (abfd, note);
11250 else
11251 return true;
11252
11253 case NT_PPC_VSX:
11254 if (note->namesz == 6
11255 && strcmp (note->namedata, "LINUX") == 0)
11256 return elfcore_grok_ppc_vsx (abfd, note);
11257 else
11258 return true;
11259
11260 case NT_PPC_TAR:
11261 if (note->namesz == 6
11262 && strcmp (note->namedata, "LINUX") == 0)
11263 return elfcore_grok_ppc_tar (abfd, note);
11264 else
11265 return true;
11266
11267 case NT_PPC_PPR:
11268 if (note->namesz == 6
11269 && strcmp (note->namedata, "LINUX") == 0)
11270 return elfcore_grok_ppc_ppr (abfd, note);
11271 else
11272 return true;
11273
11274 case NT_PPC_DSCR:
11275 if (note->namesz == 6
11276 && strcmp (note->namedata, "LINUX") == 0)
11277 return elfcore_grok_ppc_dscr (abfd, note);
11278 else
11279 return true;
11280
11281 case NT_PPC_EBB:
11282 if (note->namesz == 6
11283 && strcmp (note->namedata, "LINUX") == 0)
11284 return elfcore_grok_ppc_ebb (abfd, note);
11285 else
11286 return true;
11287
11288 case NT_PPC_PMU:
11289 if (note->namesz == 6
11290 && strcmp (note->namedata, "LINUX") == 0)
11291 return elfcore_grok_ppc_pmu (abfd, note);
11292 else
11293 return true;
11294
11295 case NT_PPC_TM_CGPR:
11296 if (note->namesz == 6
11297 && strcmp (note->namedata, "LINUX") == 0)
11298 return elfcore_grok_ppc_tm_cgpr (abfd, note);
11299 else
11300 return true;
11301
11302 case NT_PPC_TM_CFPR:
11303 if (note->namesz == 6
11304 && strcmp (note->namedata, "LINUX") == 0)
11305 return elfcore_grok_ppc_tm_cfpr (abfd, note);
11306 else
11307 return true;
11308
11309 case NT_PPC_TM_CVMX:
11310 if (note->namesz == 6
11311 && strcmp (note->namedata, "LINUX") == 0)
11312 return elfcore_grok_ppc_tm_cvmx (abfd, note);
11313 else
11314 return true;
11315
11316 case NT_PPC_TM_CVSX:
11317 if (note->namesz == 6
11318 && strcmp (note->namedata, "LINUX") == 0)
11319 return elfcore_grok_ppc_tm_cvsx (abfd, note);
11320 else
11321 return true;
11322
11323 case NT_PPC_TM_SPR:
11324 if (note->namesz == 6
11325 && strcmp (note->namedata, "LINUX") == 0)
11326 return elfcore_grok_ppc_tm_spr (abfd, note);
11327 else
11328 return true;
11329
11330 case NT_PPC_TM_CTAR:
11331 if (note->namesz == 6
11332 && strcmp (note->namedata, "LINUX") == 0)
11333 return elfcore_grok_ppc_tm_ctar (abfd, note);
11334 else
11335 return true;
11336
11337 case NT_PPC_TM_CPPR:
11338 if (note->namesz == 6
11339 && strcmp (note->namedata, "LINUX") == 0)
11340 return elfcore_grok_ppc_tm_cppr (abfd, note);
11341 else
11342 return true;
11343
11344 case NT_PPC_TM_CDSCR:
11345 if (note->namesz == 6
11346 && strcmp (note->namedata, "LINUX") == 0)
11347 return elfcore_grok_ppc_tm_cdscr (abfd, note);
11348 else
11349 return true;
11350
11351 case NT_S390_HIGH_GPRS:
11352 if (note->namesz == 6
11353 && strcmp (note->namedata, "LINUX") == 0)
11354 return elfcore_grok_s390_high_gprs (abfd, note);
11355 else
11356 return true;
11357
11358 case NT_S390_TIMER:
11359 if (note->namesz == 6
11360 && strcmp (note->namedata, "LINUX") == 0)
11361 return elfcore_grok_s390_timer (abfd, note);
11362 else
11363 return true;
11364
11365 case NT_S390_TODCMP:
11366 if (note->namesz == 6
11367 && strcmp (note->namedata, "LINUX") == 0)
11368 return elfcore_grok_s390_todcmp (abfd, note);
11369 else
11370 return true;
11371
11372 case NT_S390_TODPREG:
11373 if (note->namesz == 6
11374 && strcmp (note->namedata, "LINUX") == 0)
11375 return elfcore_grok_s390_todpreg (abfd, note);
11376 else
11377 return true;
11378
11379 case NT_S390_CTRS:
11380 if (note->namesz == 6
11381 && strcmp (note->namedata, "LINUX") == 0)
11382 return elfcore_grok_s390_ctrs (abfd, note);
11383 else
11384 return true;
11385
11386 case NT_S390_PREFIX:
11387 if (note->namesz == 6
11388 && strcmp (note->namedata, "LINUX") == 0)
11389 return elfcore_grok_s390_prefix (abfd, note);
11390 else
11391 return true;
11392
11393 case NT_S390_LAST_BREAK:
11394 if (note->namesz == 6
11395 && strcmp (note->namedata, "LINUX") == 0)
11396 return elfcore_grok_s390_last_break (abfd, note);
11397 else
11398 return true;
11399
11400 case NT_S390_SYSTEM_CALL:
11401 if (note->namesz == 6
11402 && strcmp (note->namedata, "LINUX") == 0)
11403 return elfcore_grok_s390_system_call (abfd, note);
11404 else
11405 return true;
11406
11407 case NT_S390_TDB:
11408 if (note->namesz == 6
11409 && strcmp (note->namedata, "LINUX") == 0)
11410 return elfcore_grok_s390_tdb (abfd, note);
11411 else
11412 return true;
11413
11414 case NT_S390_VXRS_LOW:
11415 if (note->namesz == 6
11416 && strcmp (note->namedata, "LINUX") == 0)
11417 return elfcore_grok_s390_vxrs_low (abfd, note);
11418 else
11419 return true;
11420
11421 case NT_S390_VXRS_HIGH:
11422 if (note->namesz == 6
11423 && strcmp (note->namedata, "LINUX") == 0)
11424 return elfcore_grok_s390_vxrs_high (abfd, note);
11425 else
11426 return true;
11427
11428 case NT_S390_GS_CB:
11429 if (note->namesz == 6
11430 && strcmp (note->namedata, "LINUX") == 0)
11431 return elfcore_grok_s390_gs_cb (abfd, note);
11432 else
11433 return true;
11434
11435 case NT_S390_GS_BC:
11436 if (note->namesz == 6
11437 && strcmp (note->namedata, "LINUX") == 0)
11438 return elfcore_grok_s390_gs_bc (abfd, note);
11439 else
11440 return true;
11441
11442 case NT_ARC_V2:
11443 if (note->namesz == 6
11444 && strcmp (note->namedata, "LINUX") == 0)
11445 return elfcore_grok_arc_v2 (abfd, note);
11446 else
11447 return true;
11448
11449 case NT_ARM_VFP:
11450 if (note->namesz == 6
11451 && strcmp (note->namedata, "LINUX") == 0)
11452 return elfcore_grok_arm_vfp (abfd, note);
11453 else
11454 return true;
11455
11456 case NT_ARM_TLS:
11457 if (note->namesz == 6
11458 && strcmp (note->namedata, "LINUX") == 0)
11459 return elfcore_grok_aarch_tls (abfd, note);
11460 else
11461 return true;
11462
11463 case NT_ARM_HW_BREAK:
11464 if (note->namesz == 6
11465 && strcmp (note->namedata, "LINUX") == 0)
11466 return elfcore_grok_aarch_hw_break (abfd, note);
11467 else
11468 return true;
11469
11470 case NT_ARM_HW_WATCH:
11471 if (note->namesz == 6
11472 && strcmp (note->namedata, "LINUX") == 0)
11473 return elfcore_grok_aarch_hw_watch (abfd, note);
11474 else
11475 return true;
11476
11477 case NT_ARM_SVE:
11478 if (note->namesz == 6
11479 && strcmp (note->namedata, "LINUX") == 0)
11480 return elfcore_grok_aarch_sve (abfd, note);
11481 else
11482 return true;
11483
11484 case NT_ARM_PAC_MASK:
11485 if (note->namesz == 6
11486 && strcmp (note->namedata, "LINUX") == 0)
11487 return elfcore_grok_aarch_pauth (abfd, note);
11488 else
11489 return true;
11490
11491 case NT_ARM_TAGGED_ADDR_CTRL:
11492 if (note->namesz == 6
11493 && strcmp (note->namedata, "LINUX") == 0)
11494 return elfcore_grok_aarch_mte (abfd, note);
11495 else
11496 return true;
11497
11498 case NT_ARM_SSVE:
11499 if (note->namesz == 6
11500 && strcmp (note->namedata, "LINUX") == 0)
11501 return elfcore_grok_aarch_ssve (abfd, note);
11502 else
11503 return true;
11504
11505 case NT_ARM_ZA:
11506 if (note->namesz == 6
11507 && strcmp (note->namedata, "LINUX") == 0)
11508 return elfcore_grok_aarch_za (abfd, note);
11509 else
11510 return true;
11511
11512 case NT_ARM_ZT:
11513 if (note->namesz == 6
11514 && strcmp (note->namedata, "LINUX") == 0)
11515 return elfcore_grok_aarch_zt (abfd, note);
11516 else
11517 return true;
11518
11519 case NT_GDB_TDESC:
11520 if (note->namesz == 4
11521 && strcmp (note->namedata, "GDB") == 0)
11522 return elfcore_grok_gdb_tdesc (abfd, note);
11523 else
11524 return true;
11525
11526 case NT_RISCV_CSR:
11527 if (note->namesz == 4
11528 && strcmp (note->namedata, "GDB") == 0)
11529 return elfcore_grok_riscv_csr (abfd, note);
11530 else
11531 return true;
11532
11533 case NT_LARCH_CPUCFG:
11534 if (note->namesz == 6
11535 && strcmp (note->namedata, "LINUX") == 0)
11536 return elfcore_grok_loongarch_cpucfg (abfd, note);
11537 else
11538 return true;
11539
11540 case NT_LARCH_LBT:
11541 if (note->namesz == 6
11542 && strcmp (note->namedata, "LINUX") == 0)
11543 return elfcore_grok_loongarch_lbt (abfd, note);
11544 else
11545 return true;
11546
11547 case NT_LARCH_LSX:
11548 if (note->namesz == 6
11549 && strcmp (note->namedata, "LINUX") == 0)
11550 return elfcore_grok_loongarch_lsx (abfd, note);
11551 else
11552 return true;
11553
11554 case NT_LARCH_LASX:
11555 if (note->namesz == 6
11556 && strcmp (note->namedata, "LINUX") == 0)
11557 return elfcore_grok_loongarch_lasx (abfd, note);
11558 else
11559 return true;
11560
11561 case NT_PRPSINFO:
11562 case NT_PSINFO:
11563 if (bed->elf_backend_grok_psinfo)
11564 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11565 return true;
11566 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11567 return elfcore_grok_psinfo (abfd, note);
11568 #else
11569 return true;
11570 #endif
11571
11572 case NT_AUXV:
11573 return elfcore_make_auxv_note_section (abfd, note, 0);
11574
11575 case NT_FILE:
11576 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11577 note);
11578
11579 case NT_SIGINFO:
11580 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11581 note);
11582
11583 }
11584 }
11585
11586 static bool
11587 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11588 {
11589 struct bfd_build_id* build_id;
11590
11591 if (note->descsz == 0)
11592 return false;
11593
11594 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11595 if (build_id == NULL)
11596 return false;
11597
11598 build_id->size = note->descsz;
11599 memcpy (build_id->data, note->descdata, note->descsz);
11600 abfd->build_id = build_id;
11601
11602 return true;
11603 }
11604
11605 static bool
11606 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11607 {
11608 switch (note->type)
11609 {
11610 default:
11611 return true;
11612
11613 case NT_GNU_PROPERTY_TYPE_0:
11614 return _bfd_elf_parse_gnu_properties (abfd, note);
11615
11616 case NT_GNU_BUILD_ID:
11617 return elfobj_grok_gnu_build_id (abfd, note);
11618 }
11619 }
11620
11621 static bool
11622 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11623 {
11624 struct sdt_note *cur =
11625 (struct sdt_note *) bfd_alloc (abfd,
11626 sizeof (struct sdt_note) + note->descsz);
11627
11628 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11629 cur->size = (bfd_size_type) note->descsz;
11630 memcpy (cur->data, note->descdata, note->descsz);
11631
11632 elf_tdata (abfd)->sdt_note_head = cur;
11633
11634 return true;
11635 }
11636
11637 static bool
11638 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11639 {
11640 switch (note->type)
11641 {
11642 case NT_STAPSDT:
11643 return elfobj_grok_stapsdt_note_1 (abfd, note);
11644
11645 default:
11646 return true;
11647 }
11648 }
11649
11650 static bool
11651 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11652 {
11653 size_t offset;
11654
11655 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11656 {
11657 case ELFCLASS32:
11658 if (note->descsz < 108)
11659 return false;
11660 break;
11661
11662 case ELFCLASS64:
11663 if (note->descsz < 120)
11664 return false;
11665 break;
11666
11667 default:
11668 return false;
11669 }
11670
11671 /* Check for version 1 in pr_version. */
11672 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11673 return false;
11674
11675 offset = 4;
11676
11677 /* Skip over pr_psinfosz. */
11678 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11679 offset += 4;
11680 else
11681 {
11682 offset += 4; /* Padding before pr_psinfosz. */
11683 offset += 8;
11684 }
11685
11686 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11687 elf_tdata (abfd)->core->program
11688 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11689 offset += 17;
11690
11691 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11692 elf_tdata (abfd)->core->command
11693 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11694 offset += 81;
11695
11696 /* Padding before pr_pid. */
11697 offset += 2;
11698
11699 /* The pr_pid field was added in version "1a". */
11700 if (note->descsz < offset + 4)
11701 return true;
11702
11703 elf_tdata (abfd)->core->pid
11704 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11705
11706 return true;
11707 }
11708
11709 static bool
11710 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11711 {
11712 size_t offset;
11713 size_t size;
11714 size_t min_size;
11715
11716 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11717 Also compute minimum size of this note. */
11718 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11719 {
11720 case ELFCLASS32:
11721 offset = 4 + 4;
11722 min_size = offset + (4 * 2) + 4 + 4 + 4;
11723 break;
11724
11725 case ELFCLASS64:
11726 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11727 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11728 break;
11729
11730 default:
11731 return false;
11732 }
11733
11734 if (note->descsz < min_size)
11735 return false;
11736
11737 /* Check for version 1 in pr_version. */
11738 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11739 return false;
11740
11741 /* Extract size of pr_reg from pr_gregsetsz. */
11742 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11743 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11744 {
11745 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11746 offset += 4 * 2;
11747 }
11748 else
11749 {
11750 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11751 offset += 8 * 2;
11752 }
11753
11754 /* Skip over pr_osreldate. */
11755 offset += 4;
11756
11757 /* Read signal from pr_cursig. */
11758 if (elf_tdata (abfd)->core->signal == 0)
11759 elf_tdata (abfd)->core->signal
11760 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11761 offset += 4;
11762
11763 /* Read TID from pr_pid. */
11764 elf_tdata (abfd)->core->lwpid
11765 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11766 offset += 4;
11767
11768 /* Padding before pr_reg. */
11769 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11770 offset += 4;
11771
11772 /* Make sure that there is enough data remaining in the note. */
11773 if ((note->descsz - offset) < size)
11774 return false;
11775
11776 /* Make a ".reg/999" section and a ".reg" section. */
11777 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11778 size, note->descpos + offset);
11779 }
11780
11781 static bool
11782 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11783 {
11784 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11785
11786 switch (note->type)
11787 {
11788 case NT_PRSTATUS:
11789 if (bed->elf_backend_grok_freebsd_prstatus)
11790 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11791 return true;
11792 return elfcore_grok_freebsd_prstatus (abfd, note);
11793
11794 case NT_FPREGSET:
11795 return elfcore_grok_prfpreg (abfd, note);
11796
11797 case NT_PRPSINFO:
11798 return elfcore_grok_freebsd_psinfo (abfd, note);
11799
11800 case NT_FREEBSD_THRMISC:
11801 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11802
11803 case NT_FREEBSD_PROCSTAT_PROC:
11804 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11805 note);
11806
11807 case NT_FREEBSD_PROCSTAT_FILES:
11808 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11809 note);
11810
11811 case NT_FREEBSD_PROCSTAT_VMMAP:
11812 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11813 note);
11814
11815 case NT_FREEBSD_PROCSTAT_AUXV:
11816 return elfcore_make_auxv_note_section (abfd, note, 4);
11817
11818 case NT_FREEBSD_X86_SEGBASES:
11819 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11820
11821 case NT_X86_XSTATE:
11822 return elfcore_grok_xstatereg (abfd, note);
11823
11824 case NT_FREEBSD_PTLWPINFO:
11825 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11826 note);
11827
11828 case NT_ARM_TLS:
11829 return elfcore_grok_aarch_tls (abfd, note);
11830
11831 case NT_ARM_VFP:
11832 return elfcore_grok_arm_vfp (abfd, note);
11833
11834 default:
11835 return true;
11836 }
11837 }
11838
11839 static bool
11840 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11841 {
11842 char *cp;
11843
11844 cp = strchr (note->namedata, '@');
11845 if (cp != NULL)
11846 {
11847 *lwpidp = atoi(cp + 1);
11848 return true;
11849 }
11850 return false;
11851 }
11852
11853 static bool
11854 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11855 {
11856 if (note->descsz <= 0x7c + 31)
11857 return false;
11858
11859 /* Signal number at offset 0x08. */
11860 elf_tdata (abfd)->core->signal
11861 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11862
11863 /* Process ID at offset 0x50. */
11864 elf_tdata (abfd)->core->pid
11865 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11866
11867 /* Command name at 0x7c (max 32 bytes, including nul). */
11868 elf_tdata (abfd)->core->command
11869 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11870
11871 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11872 note);
11873 }
11874
11875 static bool
11876 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11877 {
11878 int lwp;
11879
11880 if (elfcore_netbsd_get_lwpid (note, &lwp))
11881 elf_tdata (abfd)->core->lwpid = lwp;
11882
11883 switch (note->type)
11884 {
11885 case NT_NETBSDCORE_PROCINFO:
11886 /* NetBSD-specific core "procinfo". Note that we expect to
11887 find this note before any of the others, which is fine,
11888 since the kernel writes this note out first when it
11889 creates a core file. */
11890 return elfcore_grok_netbsd_procinfo (abfd, note);
11891 case NT_NETBSDCORE_AUXV:
11892 /* NetBSD-specific Elf Auxiliary Vector data. */
11893 return elfcore_make_auxv_note_section (abfd, note, 0);
11894 case NT_NETBSDCORE_LWPSTATUS:
11895 return elfcore_make_note_pseudosection (abfd,
11896 ".note.netbsdcore.lwpstatus",
11897 note);
11898 default:
11899 break;
11900 }
11901
11902 /* As of March 2020 there are no other machine-independent notes
11903 defined for NetBSD core files. If the note type is less
11904 than the start of the machine-dependent note types, we don't
11905 understand it. */
11906
11907 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11908 return true;
11909
11910
11911 switch (bfd_get_arch (abfd))
11912 {
11913 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11914 PT_GETFPREGS == mach+2. */
11915
11916 case bfd_arch_aarch64:
11917 case bfd_arch_alpha:
11918 case bfd_arch_sparc:
11919 switch (note->type)
11920 {
11921 case NT_NETBSDCORE_FIRSTMACH+0:
11922 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11923
11924 case NT_NETBSDCORE_FIRSTMACH+2:
11925 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11926
11927 default:
11928 return true;
11929 }
11930
11931 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11932 There's also old PT___GETREGS40 == mach + 1 for old reg
11933 structure which lacks GBR. */
11934
11935 case bfd_arch_sh:
11936 switch (note->type)
11937 {
11938 case NT_NETBSDCORE_FIRSTMACH+3:
11939 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11940
11941 case NT_NETBSDCORE_FIRSTMACH+5:
11942 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11943
11944 default:
11945 return true;
11946 }
11947
11948 /* On all other arch's, PT_GETREGS == mach+1 and
11949 PT_GETFPREGS == mach+3. */
11950
11951 default:
11952 switch (note->type)
11953 {
11954 case NT_NETBSDCORE_FIRSTMACH+1:
11955 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11956
11957 case NT_NETBSDCORE_FIRSTMACH+3:
11958 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11959
11960 default:
11961 return true;
11962 }
11963 }
11964 /* NOTREACHED */
11965 }
11966
11967 static bool
11968 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11969 {
11970 if (note->descsz <= 0x48 + 31)
11971 return false;
11972
11973 /* Signal number at offset 0x08. */
11974 elf_tdata (abfd)->core->signal
11975 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11976
11977 /* Process ID at offset 0x20. */
11978 elf_tdata (abfd)->core->pid
11979 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11980
11981 /* Command name at 0x48 (max 32 bytes, including nul). */
11982 elf_tdata (abfd)->core->command
11983 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11984
11985 return true;
11986 }
11987
11988 /* Processes Solaris's process status note.
11989 sig_off ~ offsetof(prstatus_t, pr_cursig)
11990 pid_off ~ offsetof(prstatus_t, pr_pid)
11991 lwpid_off ~ offsetof(prstatus_t, pr_who)
11992 gregset_size ~ sizeof(gregset_t)
11993 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11994
11995 static bool
11996 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11997 int pid_off, int lwpid_off, size_t gregset_size,
11998 size_t gregset_offset)
11999 {
12000 asection *sect = NULL;
12001 elf_tdata (abfd)->core->signal
12002 = bfd_get_16 (abfd, note->descdata + sig_off);
12003 elf_tdata (abfd)->core->pid
12004 = bfd_get_32 (abfd, note->descdata + pid_off);
12005 elf_tdata (abfd)->core->lwpid
12006 = bfd_get_32 (abfd, note->descdata + lwpid_off);
12007
12008 sect = bfd_get_section_by_name (abfd, ".reg");
12009 if (sect != NULL)
12010 sect->size = gregset_size;
12011
12012 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12013 note->descpos + gregset_offset);
12014 }
12015
12016 /* Gets program and arguments from a core.
12017 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
12018 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
12019
12020 static bool
12021 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
12022 int prog_off, int comm_off)
12023 {
12024 elf_tdata (abfd)->core->program
12025 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
12026 elf_tdata (abfd)->core->command
12027 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
12028
12029 return true;
12030 }
12031
12032 /* Processes Solaris's LWP status note.
12033 gregset_size ~ sizeof(gregset_t)
12034 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
12035 fpregset_size ~ sizeof(fpregset_t)
12036 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
12037
12038 static bool
12039 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
12040 size_t gregset_size, int gregset_off,
12041 size_t fpregset_size, int fpregset_off)
12042 {
12043 asection *sect = NULL;
12044 char reg2_section_name[16] = { 0 };
12045
12046 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
12047 elf_tdata (abfd)->core->lwpid);
12048
12049 /* offsetof(lwpstatus_t, pr_lwpid) */
12050 elf_tdata (abfd)->core->lwpid
12051 = bfd_get_32 (abfd, note->descdata + 4);
12052 /* offsetof(lwpstatus_t, pr_cursig) */
12053 elf_tdata (abfd)->core->signal
12054 = bfd_get_16 (abfd, note->descdata + 12);
12055
12056 sect = bfd_get_section_by_name (abfd, ".reg");
12057 if (sect != NULL)
12058 sect->size = gregset_size;
12059 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
12060 note->descpos + gregset_off))
12061 return false;
12062
12063 sect = bfd_get_section_by_name (abfd, reg2_section_name);
12064 if (sect != NULL)
12065 {
12066 sect->size = fpregset_size;
12067 sect->filepos = note->descpos + fpregset_off;
12068 sect->alignment_power = 2;
12069 }
12070 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
12071 note->descpos + fpregset_off))
12072 return false;
12073
12074 return true;
12075 }
12076
12077 static bool
12078 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
12079 {
12080 if (note == NULL)
12081 return false;
12082
12083 /* core files are identified as 32- or 64-bit, SPARC or x86,
12084 by the size of the descsz which matches the sizeof()
12085 the type appropriate for that note type (e.g., prstatus_t for
12086 SOLARIS_NT_PRSTATUS) for the corresponding architecture
12087 on Solaris. The core file bitness may differ from the bitness of
12088 gdb itself, so fixed values are used instead of sizeof().
12089 Appropriate fixed offsets are also used to obtain data from
12090 the note. */
12091
12092 switch ((int) note->type)
12093 {
12094 case SOLARIS_NT_PRSTATUS:
12095 switch (note->descsz)
12096 {
12097 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
12098 return elfcore_grok_solaris_prstatus(abfd, note,
12099 136, 216, 308, 152, 356);
12100 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
12101 return elfcore_grok_solaris_prstatus(abfd, note,
12102 264, 360, 520, 304, 600);
12103 case 432: /* sizeof(prstatus_t) Intel 32-bit */
12104 return elfcore_grok_solaris_prstatus(abfd, note,
12105 136, 216, 308, 76, 356);
12106 case 824: /* sizeof(prstatus_t) Intel 64-bit */
12107 return elfcore_grok_solaris_prstatus(abfd, note,
12108 264, 360, 520, 224, 600);
12109 default:
12110 return true;
12111 }
12112
12113 case SOLARIS_NT_PSINFO:
12114 case SOLARIS_NT_PRPSINFO:
12115 switch (note->descsz)
12116 {
12117 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
12118 return elfcore_grok_solaris_info(abfd, note, 84, 100);
12119 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
12120 return elfcore_grok_solaris_info(abfd, note, 120, 136);
12121 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
12122 return elfcore_grok_solaris_info(abfd, note, 88, 104);
12123 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
12124 return elfcore_grok_solaris_info(abfd, note, 136, 152);
12125 default:
12126 return true;
12127 }
12128
12129 case SOLARIS_NT_LWPSTATUS:
12130 switch (note->descsz)
12131 {
12132 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
12133 return elfcore_grok_solaris_lwpstatus(abfd, note,
12134 152, 344, 400, 496);
12135 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
12136 return elfcore_grok_solaris_lwpstatus(abfd, note,
12137 304, 544, 544, 848);
12138 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
12139 return elfcore_grok_solaris_lwpstatus(abfd, note,
12140 76, 344, 380, 420);
12141 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
12142 return elfcore_grok_solaris_lwpstatus(abfd, note,
12143 224, 544, 528, 768);
12144 default:
12145 return true;
12146 }
12147
12148 case SOLARIS_NT_LWPSINFO:
12149 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
12150 if (note->descsz == 128 || note->descsz == 152)
12151 elf_tdata (abfd)->core->lwpid =
12152 bfd_get_32 (abfd, note->descdata + 4);
12153 break;
12154
12155 default:
12156 break;
12157 }
12158
12159 return true;
12160 }
12161
12162 /* For name starting with "CORE" this may be either a Solaris
12163 core file or a gdb-generated core file. Do Solaris-specific
12164 processing on selected note types first with
12165 elfcore_grok_solaris_note(), then process the note
12166 in elfcore_grok_note(). */
12167
12168 static bool
12169 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
12170 {
12171 if (!elfcore_grok_solaris_note_impl (abfd, note))
12172 return false;
12173
12174 return elfcore_grok_note (abfd, note);
12175 }
12176
12177 static bool
12178 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
12179 {
12180 if (note->type == NT_OPENBSD_PROCINFO)
12181 return elfcore_grok_openbsd_procinfo (abfd, note);
12182
12183 if (note->type == NT_OPENBSD_REGS)
12184 return elfcore_make_note_pseudosection (abfd, ".reg", note);
12185
12186 if (note->type == NT_OPENBSD_FPREGS)
12187 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
12188
12189 if (note->type == NT_OPENBSD_XFPREGS)
12190 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
12191
12192 if (note->type == NT_OPENBSD_AUXV)
12193 return elfcore_make_auxv_note_section (abfd, note, 0);
12194
12195 if (note->type == NT_OPENBSD_WCOOKIE)
12196 {
12197 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
12198 SEC_HAS_CONTENTS);
12199
12200 if (sect == NULL)
12201 return false;
12202 sect->size = note->descsz;
12203 sect->filepos = note->descpos;
12204 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
12205
12206 return true;
12207 }
12208
12209 return true;
12210 }
12211
12212 static bool
12213 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
12214 {
12215 void *ddata = note->descdata;
12216 char buf[100];
12217 char *name;
12218 asection *sect;
12219 short sig;
12220 unsigned flags;
12221
12222 if (note->descsz < 16)
12223 return false;
12224
12225 /* nto_procfs_status 'pid' field is at offset 0. */
12226 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
12227
12228 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
12229 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
12230
12231 /* nto_procfs_status 'flags' field is at offset 8. */
12232 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
12233
12234 /* nto_procfs_status 'what' field is at offset 14. */
12235 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
12236 {
12237 elf_tdata (abfd)->core->signal = sig;
12238 elf_tdata (abfd)->core->lwpid = *tid;
12239 }
12240
12241 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
12242 do not come from signals so we make sure we set the current
12243 thread just in case. */
12244 if (flags & 0x00000080)
12245 elf_tdata (abfd)->core->lwpid = *tid;
12246
12247 /* Make a ".qnx_core_status/%d" section. */
12248 sprintf (buf, ".qnx_core_status/%ld", *tid);
12249
12250 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12251 if (name == NULL)
12252 return false;
12253 strcpy (name, buf);
12254
12255 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12256 if (sect == NULL)
12257 return false;
12258
12259 sect->size = note->descsz;
12260 sect->filepos = note->descpos;
12261 sect->alignment_power = 2;
12262
12263 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
12264 }
12265
12266 static bool
12267 elfcore_grok_nto_regs (bfd *abfd,
12268 Elf_Internal_Note *note,
12269 long tid,
12270 char *base)
12271 {
12272 char buf[100];
12273 char *name;
12274 asection *sect;
12275
12276 /* Make a "(base)/%d" section. */
12277 sprintf (buf, "%s/%ld", base, tid);
12278
12279 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
12280 if (name == NULL)
12281 return false;
12282 strcpy (name, buf);
12283
12284 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12285 if (sect == NULL)
12286 return false;
12287
12288 sect->size = note->descsz;
12289 sect->filepos = note->descpos;
12290 sect->alignment_power = 2;
12291
12292 /* This is the current thread. */
12293 if (elf_tdata (abfd)->core->lwpid == tid)
12294 return elfcore_maybe_make_sect (abfd, base, sect);
12295
12296 return true;
12297 }
12298
12299 static bool
12300 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12301 {
12302 /* Every GREG section has a STATUS section before it. Store the
12303 tid from the previous call to pass down to the next gregs
12304 function. */
12305 static long tid = 1;
12306
12307 switch (note->type)
12308 {
12309 case QNT_CORE_INFO:
12310 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12311 case QNT_CORE_STATUS:
12312 return elfcore_grok_nto_status (abfd, note, &tid);
12313 case QNT_CORE_GREG:
12314 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
12315 case QNT_CORE_FPREG:
12316 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
12317 default:
12318 return true;
12319 }
12320 }
12321
12322 static bool
12323 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12324 {
12325 char *name;
12326 asection *sect;
12327 size_t len;
12328
12329 /* Use note name as section name. */
12330 len = note->namesz;
12331 name = (char *) bfd_alloc (abfd, len);
12332 if (name == NULL)
12333 return false;
12334 memcpy (name, note->namedata, len);
12335 name[len - 1] = '\0';
12336
12337 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12338 if (sect == NULL)
12339 return false;
12340
12341 sect->size = note->descsz;
12342 sect->filepos = note->descpos;
12343 sect->alignment_power = 1;
12344
12345 return true;
12346 }
12347
12348 /* Function: elfcore_write_note
12349
12350 Inputs:
12351 buffer to hold note, and current size of buffer
12352 name of note
12353 type of note
12354 data for note
12355 size of data for note
12356
12357 Writes note to end of buffer. ELF64 notes are written exactly as
12358 for ELF32, despite the current (as of 2006) ELF gabi specifying
12359 that they ought to have 8-byte namesz and descsz field, and have
12360 8-byte alignment. Other writers, eg. Linux kernel, do the same.
12361
12362 Return:
12363 Pointer to realloc'd buffer, *BUFSIZ updated. */
12364
12365 char *
12366 elfcore_write_note (bfd *abfd,
12367 char *buf,
12368 int *bufsiz,
12369 const char *name,
12370 int type,
12371 const void *input,
12372 int size)
12373 {
12374 Elf_External_Note *xnp;
12375 size_t namesz;
12376 size_t newspace;
12377 char *dest;
12378
12379 namesz = 0;
12380 if (name != NULL)
12381 namesz = strlen (name) + 1;
12382
12383 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12384
12385 buf = (char *) realloc (buf, *bufsiz + newspace);
12386 if (buf == NULL)
12387 return buf;
12388 dest = buf + *bufsiz;
12389 *bufsiz += newspace;
12390 xnp = (Elf_External_Note *) dest;
12391 H_PUT_32 (abfd, namesz, xnp->namesz);
12392 H_PUT_32 (abfd, size, xnp->descsz);
12393 H_PUT_32 (abfd, type, xnp->type);
12394 dest = xnp->name;
12395 if (name != NULL)
12396 {
12397 memcpy (dest, name, namesz);
12398 dest += namesz;
12399 while (namesz & 3)
12400 {
12401 *dest++ = '\0';
12402 ++namesz;
12403 }
12404 }
12405 memcpy (dest, input, size);
12406 dest += size;
12407 while (size & 3)
12408 {
12409 *dest++ = '\0';
12410 ++size;
12411 }
12412 return buf;
12413 }
12414
12415 /* gcc-8 warns (*) on all the strncpy calls in this function about
12416 possible string truncation. The "truncation" is not a bug. We
12417 have an external representation of structs with fields that are not
12418 necessarily NULL terminated and corresponding internal
12419 representation fields that are one larger so that they can always
12420 be NULL terminated.
12421 gcc versions between 4.2 and 4.6 do not allow pragma control of
12422 diagnostics inside functions, giving a hard error if you try to use
12423 the finer control available with later versions.
12424 gcc prior to 4.2 warns about diagnostic push and pop.
12425 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12426 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12427 (*) Depending on your system header files! */
12428 #if GCC_VERSION >= 8000
12429 # pragma GCC diagnostic push
12430 # pragma GCC diagnostic ignored "-Wstringop-truncation"
12431 #endif
12432 char *
12433 elfcore_write_prpsinfo (bfd *abfd,
12434 char *buf,
12435 int *bufsiz,
12436 const char *fname,
12437 const char *psargs)
12438 {
12439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12440
12441 if (bed->elf_backend_write_core_note != NULL)
12442 {
12443 char *ret;
12444 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12445 NT_PRPSINFO, fname, psargs);
12446 if (ret != NULL)
12447 return ret;
12448 }
12449
12450 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12451 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12452 if (bed->s->elfclass == ELFCLASS32)
12453 {
12454 # if defined (HAVE_PSINFO32_T)
12455 psinfo32_t data;
12456 int note_type = NT_PSINFO;
12457 # else
12458 prpsinfo32_t data;
12459 int note_type = NT_PRPSINFO;
12460 # endif
12461
12462 memset (&data, 0, sizeof (data));
12463 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12464 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12465 return elfcore_write_note (abfd, buf, bufsiz,
12466 "CORE", note_type, &data, sizeof (data));
12467 }
12468 else
12469 # endif
12470 {
12471 # if defined (HAVE_PSINFO_T)
12472 psinfo_t data;
12473 int note_type = NT_PSINFO;
12474 # else
12475 prpsinfo_t data;
12476 int note_type = NT_PRPSINFO;
12477 # endif
12478
12479 memset (&data, 0, sizeof (data));
12480 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12481 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12482 return elfcore_write_note (abfd, buf, bufsiz,
12483 "CORE", note_type, &data, sizeof (data));
12484 }
12485 #endif /* PSINFO_T or PRPSINFO_T */
12486
12487 free (buf);
12488 return NULL;
12489 }
12490 #if GCC_VERSION >= 8000
12491 # pragma GCC diagnostic pop
12492 #endif
12493
12494 char *
12495 elfcore_write_linux_prpsinfo32
12496 (bfd *abfd, char *buf, int *bufsiz,
12497 const struct elf_internal_linux_prpsinfo *prpsinfo)
12498 {
12499 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12500 {
12501 struct elf_external_linux_prpsinfo32_ugid16 data;
12502
12503 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12504 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12505 &data, sizeof (data));
12506 }
12507 else
12508 {
12509 struct elf_external_linux_prpsinfo32_ugid32 data;
12510
12511 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12512 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
12513 &data, sizeof (data));
12514 }
12515 }
12516
12517 char *
12518 elfcore_write_linux_prpsinfo64
12519 (bfd *abfd, char *buf, int *bufsiz,
12520 const struct elf_internal_linux_prpsinfo *prpsinfo)
12521 {
12522 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12523 {
12524 struct elf_external_linux_prpsinfo64_ugid16 data;
12525
12526 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12527 return elfcore_write_note (abfd, buf, bufsiz,
12528 "CORE", NT_PRPSINFO, &data, sizeof (data));
12529 }
12530 else
12531 {
12532 struct elf_external_linux_prpsinfo64_ugid32 data;
12533
12534 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12535 return elfcore_write_note (abfd, buf, bufsiz,
12536 "CORE", NT_PRPSINFO, &data, sizeof (data));
12537 }
12538 }
12539
12540 char *
12541 elfcore_write_prstatus (bfd *abfd,
12542 char *buf,
12543 int *bufsiz,
12544 long pid,
12545 int cursig,
12546 const void *gregs)
12547 {
12548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12549
12550 if (bed->elf_backend_write_core_note != NULL)
12551 {
12552 char *ret;
12553 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12554 NT_PRSTATUS,
12555 pid, cursig, gregs);
12556 if (ret != NULL)
12557 return ret;
12558 }
12559
12560 #if defined (HAVE_PRSTATUS_T)
12561 #if defined (HAVE_PRSTATUS32_T)
12562 if (bed->s->elfclass == ELFCLASS32)
12563 {
12564 prstatus32_t prstat;
12565
12566 memset (&prstat, 0, sizeof (prstat));
12567 prstat.pr_pid = pid;
12568 prstat.pr_cursig = cursig;
12569 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12570 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12571 NT_PRSTATUS, &prstat, sizeof (prstat));
12572 }
12573 else
12574 #endif
12575 {
12576 prstatus_t prstat;
12577
12578 memset (&prstat, 0, sizeof (prstat));
12579 prstat.pr_pid = pid;
12580 prstat.pr_cursig = cursig;
12581 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12582 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
12583 NT_PRSTATUS, &prstat, sizeof (prstat));
12584 }
12585 #endif /* HAVE_PRSTATUS_T */
12586
12587 free (buf);
12588 return NULL;
12589 }
12590
12591 #if defined (HAVE_LWPSTATUS_T)
12592 char *
12593 elfcore_write_lwpstatus (bfd *abfd,
12594 char *buf,
12595 int *bufsiz,
12596 long pid,
12597 int cursig,
12598 const void *gregs)
12599 {
12600 lwpstatus_t lwpstat;
12601 const char *note_name = "CORE";
12602
12603 memset (&lwpstat, 0, sizeof (lwpstat));
12604 lwpstat.pr_lwpid = pid >> 16;
12605 lwpstat.pr_cursig = cursig;
12606 #if defined (HAVE_LWPSTATUS_T_PR_REG)
12607 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12608 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12609 #if !defined(gregs)
12610 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12611 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12612 #else
12613 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12614 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12615 #endif
12616 #endif
12617 return elfcore_write_note (abfd, buf, bufsiz, note_name,
12618 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12619 }
12620 #endif /* HAVE_LWPSTATUS_T */
12621
12622 #if defined (HAVE_PSTATUS_T)
12623 char *
12624 elfcore_write_pstatus (bfd *abfd,
12625 char *buf,
12626 int *bufsiz,
12627 long pid,
12628 int cursig ATTRIBUTE_UNUSED,
12629 const void *gregs ATTRIBUTE_UNUSED)
12630 {
12631 const char *note_name = "CORE";
12632 #if defined (HAVE_PSTATUS32_T)
12633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12634
12635 if (bed->s->elfclass == ELFCLASS32)
12636 {
12637 pstatus32_t pstat;
12638
12639 memset (&pstat, 0, sizeof (pstat));
12640 pstat.pr_pid = pid & 0xffff;
12641 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12642 NT_PSTATUS, &pstat, sizeof (pstat));
12643 return buf;
12644 }
12645 else
12646 #endif
12647 {
12648 pstatus_t pstat;
12649
12650 memset (&pstat, 0, sizeof (pstat));
12651 pstat.pr_pid = pid & 0xffff;
12652 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
12653 NT_PSTATUS, &pstat, sizeof (pstat));
12654 return buf;
12655 }
12656 }
12657 #endif /* HAVE_PSTATUS_T */
12658
12659 char *
12660 elfcore_write_prfpreg (bfd *abfd,
12661 char *buf,
12662 int *bufsiz,
12663 const void *fpregs,
12664 int size)
12665 {
12666 const char *note_name = "CORE";
12667 return elfcore_write_note (abfd, buf, bufsiz,
12668 note_name, NT_FPREGSET, fpregs, size);
12669 }
12670
12671 char *
12672 elfcore_write_prxfpreg (bfd *abfd,
12673 char *buf,
12674 int *bufsiz,
12675 const void *xfpregs,
12676 int size)
12677 {
12678 char *note_name = "LINUX";
12679 return elfcore_write_note (abfd, buf, bufsiz,
12680 note_name, NT_PRXFPREG, xfpregs, size);
12681 }
12682
12683 char *
12684 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12685 const void *xfpregs, int size)
12686 {
12687 char *note_name;
12688 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12689 note_name = "FreeBSD";
12690 else
12691 note_name = "LINUX";
12692 return elfcore_write_note (abfd, buf, bufsiz,
12693 note_name, NT_X86_XSTATE, xfpregs, size);
12694 }
12695
12696 char *
12697 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12698 const void *regs, int size)
12699 {
12700 char *note_name = "FreeBSD";
12701 return elfcore_write_note (abfd, buf, bufsiz,
12702 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12703 }
12704
12705 char *
12706 elfcore_write_ppc_vmx (bfd *abfd,
12707 char *buf,
12708 int *bufsiz,
12709 const void *ppc_vmx,
12710 int size)
12711 {
12712 char *note_name = "LINUX";
12713 return elfcore_write_note (abfd, buf, bufsiz,
12714 note_name, NT_PPC_VMX, ppc_vmx, size);
12715 }
12716
12717 char *
12718 elfcore_write_ppc_vsx (bfd *abfd,
12719 char *buf,
12720 int *bufsiz,
12721 const void *ppc_vsx,
12722 int size)
12723 {
12724 char *note_name = "LINUX";
12725 return elfcore_write_note (abfd, buf, bufsiz,
12726 note_name, NT_PPC_VSX, ppc_vsx, size);
12727 }
12728
12729 char *
12730 elfcore_write_ppc_tar (bfd *abfd,
12731 char *buf,
12732 int *bufsiz,
12733 const void *ppc_tar,
12734 int size)
12735 {
12736 char *note_name = "LINUX";
12737 return elfcore_write_note (abfd, buf, bufsiz,
12738 note_name, NT_PPC_TAR, ppc_tar, size);
12739 }
12740
12741 char *
12742 elfcore_write_ppc_ppr (bfd *abfd,
12743 char *buf,
12744 int *bufsiz,
12745 const void *ppc_ppr,
12746 int size)
12747 {
12748 char *note_name = "LINUX";
12749 return elfcore_write_note (abfd, buf, bufsiz,
12750 note_name, NT_PPC_PPR, ppc_ppr, size);
12751 }
12752
12753 char *
12754 elfcore_write_ppc_dscr (bfd *abfd,
12755 char *buf,
12756 int *bufsiz,
12757 const void *ppc_dscr,
12758 int size)
12759 {
12760 char *note_name = "LINUX";
12761 return elfcore_write_note (abfd, buf, bufsiz,
12762 note_name, NT_PPC_DSCR, ppc_dscr, size);
12763 }
12764
12765 char *
12766 elfcore_write_ppc_ebb (bfd *abfd,
12767 char *buf,
12768 int *bufsiz,
12769 const void *ppc_ebb,
12770 int size)
12771 {
12772 char *note_name = "LINUX";
12773 return elfcore_write_note (abfd, buf, bufsiz,
12774 note_name, NT_PPC_EBB, ppc_ebb, size);
12775 }
12776
12777 char *
12778 elfcore_write_ppc_pmu (bfd *abfd,
12779 char *buf,
12780 int *bufsiz,
12781 const void *ppc_pmu,
12782 int size)
12783 {
12784 char *note_name = "LINUX";
12785 return elfcore_write_note (abfd, buf, bufsiz,
12786 note_name, NT_PPC_PMU, ppc_pmu, size);
12787 }
12788
12789 char *
12790 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12791 char *buf,
12792 int *bufsiz,
12793 const void *ppc_tm_cgpr,
12794 int size)
12795 {
12796 char *note_name = "LINUX";
12797 return elfcore_write_note (abfd, buf, bufsiz,
12798 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12799 }
12800
12801 char *
12802 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12803 char *buf,
12804 int *bufsiz,
12805 const void *ppc_tm_cfpr,
12806 int size)
12807 {
12808 char *note_name = "LINUX";
12809 return elfcore_write_note (abfd, buf, bufsiz,
12810 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12811 }
12812
12813 char *
12814 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12815 char *buf,
12816 int *bufsiz,
12817 const void *ppc_tm_cvmx,
12818 int size)
12819 {
12820 char *note_name = "LINUX";
12821 return elfcore_write_note (abfd, buf, bufsiz,
12822 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12823 }
12824
12825 char *
12826 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12827 char *buf,
12828 int *bufsiz,
12829 const void *ppc_tm_cvsx,
12830 int size)
12831 {
12832 char *note_name = "LINUX";
12833 return elfcore_write_note (abfd, buf, bufsiz,
12834 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12835 }
12836
12837 char *
12838 elfcore_write_ppc_tm_spr (bfd *abfd,
12839 char *buf,
12840 int *bufsiz,
12841 const void *ppc_tm_spr,
12842 int size)
12843 {
12844 char *note_name = "LINUX";
12845 return elfcore_write_note (abfd, buf, bufsiz,
12846 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12847 }
12848
12849 char *
12850 elfcore_write_ppc_tm_ctar (bfd *abfd,
12851 char *buf,
12852 int *bufsiz,
12853 const void *ppc_tm_ctar,
12854 int size)
12855 {
12856 char *note_name = "LINUX";
12857 return elfcore_write_note (abfd, buf, bufsiz,
12858 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12859 }
12860
12861 char *
12862 elfcore_write_ppc_tm_cppr (bfd *abfd,
12863 char *buf,
12864 int *bufsiz,
12865 const void *ppc_tm_cppr,
12866 int size)
12867 {
12868 char *note_name = "LINUX";
12869 return elfcore_write_note (abfd, buf, bufsiz,
12870 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12871 }
12872
12873 char *
12874 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12875 char *buf,
12876 int *bufsiz,
12877 const void *ppc_tm_cdscr,
12878 int size)
12879 {
12880 char *note_name = "LINUX";
12881 return elfcore_write_note (abfd, buf, bufsiz,
12882 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12883 }
12884
12885 static char *
12886 elfcore_write_s390_high_gprs (bfd *abfd,
12887 char *buf,
12888 int *bufsiz,
12889 const void *s390_high_gprs,
12890 int size)
12891 {
12892 char *note_name = "LINUX";
12893 return elfcore_write_note (abfd, buf, bufsiz,
12894 note_name, NT_S390_HIGH_GPRS,
12895 s390_high_gprs, size);
12896 }
12897
12898 char *
12899 elfcore_write_s390_timer (bfd *abfd,
12900 char *buf,
12901 int *bufsiz,
12902 const void *s390_timer,
12903 int size)
12904 {
12905 char *note_name = "LINUX";
12906 return elfcore_write_note (abfd, buf, bufsiz,
12907 note_name, NT_S390_TIMER, s390_timer, size);
12908 }
12909
12910 char *
12911 elfcore_write_s390_todcmp (bfd *abfd,
12912 char *buf,
12913 int *bufsiz,
12914 const void *s390_todcmp,
12915 int size)
12916 {
12917 char *note_name = "LINUX";
12918 return elfcore_write_note (abfd, buf, bufsiz,
12919 note_name, NT_S390_TODCMP, s390_todcmp, size);
12920 }
12921
12922 char *
12923 elfcore_write_s390_todpreg (bfd *abfd,
12924 char *buf,
12925 int *bufsiz,
12926 const void *s390_todpreg,
12927 int size)
12928 {
12929 char *note_name = "LINUX";
12930 return elfcore_write_note (abfd, buf, bufsiz,
12931 note_name, NT_S390_TODPREG, s390_todpreg, size);
12932 }
12933
12934 char *
12935 elfcore_write_s390_ctrs (bfd *abfd,
12936 char *buf,
12937 int *bufsiz,
12938 const void *s390_ctrs,
12939 int size)
12940 {
12941 char *note_name = "LINUX";
12942 return elfcore_write_note (abfd, buf, bufsiz,
12943 note_name, NT_S390_CTRS, s390_ctrs, size);
12944 }
12945
12946 char *
12947 elfcore_write_s390_prefix (bfd *abfd,
12948 char *buf,
12949 int *bufsiz,
12950 const void *s390_prefix,
12951 int size)
12952 {
12953 char *note_name = "LINUX";
12954 return elfcore_write_note (abfd, buf, bufsiz,
12955 note_name, NT_S390_PREFIX, s390_prefix, size);
12956 }
12957
12958 char *
12959 elfcore_write_s390_last_break (bfd *abfd,
12960 char *buf,
12961 int *bufsiz,
12962 const void *s390_last_break,
12963 int size)
12964 {
12965 char *note_name = "LINUX";
12966 return elfcore_write_note (abfd, buf, bufsiz,
12967 note_name, NT_S390_LAST_BREAK,
12968 s390_last_break, size);
12969 }
12970
12971 char *
12972 elfcore_write_s390_system_call (bfd *abfd,
12973 char *buf,
12974 int *bufsiz,
12975 const void *s390_system_call,
12976 int size)
12977 {
12978 char *note_name = "LINUX";
12979 return elfcore_write_note (abfd, buf, bufsiz,
12980 note_name, NT_S390_SYSTEM_CALL,
12981 s390_system_call, size);
12982 }
12983
12984 char *
12985 elfcore_write_s390_tdb (bfd *abfd,
12986 char *buf,
12987 int *bufsiz,
12988 const void *s390_tdb,
12989 int size)
12990 {
12991 char *note_name = "LINUX";
12992 return elfcore_write_note (abfd, buf, bufsiz,
12993 note_name, NT_S390_TDB, s390_tdb, size);
12994 }
12995
12996 char *
12997 elfcore_write_s390_vxrs_low (bfd *abfd,
12998 char *buf,
12999 int *bufsiz,
13000 const void *s390_vxrs_low,
13001 int size)
13002 {
13003 char *note_name = "LINUX";
13004 return elfcore_write_note (abfd, buf, bufsiz,
13005 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
13006 }
13007
13008 char *
13009 elfcore_write_s390_vxrs_high (bfd *abfd,
13010 char *buf,
13011 int *bufsiz,
13012 const void *s390_vxrs_high,
13013 int size)
13014 {
13015 char *note_name = "LINUX";
13016 return elfcore_write_note (abfd, buf, bufsiz,
13017 note_name, NT_S390_VXRS_HIGH,
13018 s390_vxrs_high, size);
13019 }
13020
13021 char *
13022 elfcore_write_s390_gs_cb (bfd *abfd,
13023 char *buf,
13024 int *bufsiz,
13025 const void *s390_gs_cb,
13026 int size)
13027 {
13028 char *note_name = "LINUX";
13029 return elfcore_write_note (abfd, buf, bufsiz,
13030 note_name, NT_S390_GS_CB,
13031 s390_gs_cb, size);
13032 }
13033
13034 char *
13035 elfcore_write_s390_gs_bc (bfd *abfd,
13036 char *buf,
13037 int *bufsiz,
13038 const void *s390_gs_bc,
13039 int size)
13040 {
13041 char *note_name = "LINUX";
13042 return elfcore_write_note (abfd, buf, bufsiz,
13043 note_name, NT_S390_GS_BC,
13044 s390_gs_bc, size);
13045 }
13046
13047 char *
13048 elfcore_write_arm_vfp (bfd *abfd,
13049 char *buf,
13050 int *bufsiz,
13051 const void *arm_vfp,
13052 int size)
13053 {
13054 char *note_name = "LINUX";
13055 return elfcore_write_note (abfd, buf, bufsiz,
13056 note_name, NT_ARM_VFP, arm_vfp, size);
13057 }
13058
13059 char *
13060 elfcore_write_aarch_tls (bfd *abfd,
13061 char *buf,
13062 int *bufsiz,
13063 const void *aarch_tls,
13064 int size)
13065 {
13066 char *note_name = "LINUX";
13067 return elfcore_write_note (abfd, buf, bufsiz,
13068 note_name, NT_ARM_TLS, aarch_tls, size);
13069 }
13070
13071 char *
13072 elfcore_write_aarch_hw_break (bfd *abfd,
13073 char *buf,
13074 int *bufsiz,
13075 const void *aarch_hw_break,
13076 int size)
13077 {
13078 char *note_name = "LINUX";
13079 return elfcore_write_note (abfd, buf, bufsiz,
13080 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
13081 }
13082
13083 char *
13084 elfcore_write_aarch_hw_watch (bfd *abfd,
13085 char *buf,
13086 int *bufsiz,
13087 const void *aarch_hw_watch,
13088 int size)
13089 {
13090 char *note_name = "LINUX";
13091 return elfcore_write_note (abfd, buf, bufsiz,
13092 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
13093 }
13094
13095 char *
13096 elfcore_write_aarch_sve (bfd *abfd,
13097 char *buf,
13098 int *bufsiz,
13099 const void *aarch_sve,
13100 int size)
13101 {
13102 char *note_name = "LINUX";
13103 return elfcore_write_note (abfd, buf, bufsiz,
13104 note_name, NT_ARM_SVE, aarch_sve, size);
13105 }
13106
13107 char *
13108 elfcore_write_aarch_pauth (bfd *abfd,
13109 char *buf,
13110 int *bufsiz,
13111 const void *aarch_pauth,
13112 int size)
13113 {
13114 char *note_name = "LINUX";
13115 return elfcore_write_note (abfd, buf, bufsiz,
13116 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
13117 }
13118
13119 char *
13120 elfcore_write_aarch_mte (bfd *abfd,
13121 char *buf,
13122 int *bufsiz,
13123 const void *aarch_mte,
13124 int size)
13125 {
13126 char *note_name = "LINUX";
13127 return elfcore_write_note (abfd, buf, bufsiz,
13128 note_name, NT_ARM_TAGGED_ADDR_CTRL,
13129 aarch_mte,
13130 size);
13131 }
13132
13133 char *
13134 elfcore_write_aarch_ssve (bfd *abfd,
13135 char *buf,
13136 int *bufsiz,
13137 const void *aarch_ssve,
13138 int size)
13139 {
13140 char *note_name = "LINUX";
13141 return elfcore_write_note (abfd, buf, bufsiz,
13142 note_name, NT_ARM_SSVE,
13143 aarch_ssve,
13144 size);
13145 }
13146
13147 char *
13148 elfcore_write_aarch_za (bfd *abfd,
13149 char *buf,
13150 int *bufsiz,
13151 const void *aarch_za,
13152 int size)
13153 {
13154 char *note_name = "LINUX";
13155 return elfcore_write_note (abfd, buf, bufsiz,
13156 note_name, NT_ARM_ZA,
13157 aarch_za,
13158 size);
13159 }
13160
13161 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
13162 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13163 written into. Return a pointer to the new start of the note buffer, to
13164 replace BUF which may no longer be valid. */
13165
13166 char *
13167 elfcore_write_aarch_zt (bfd *abfd,
13168 char *buf,
13169 int *bufsiz,
13170 const void *aarch_zt,
13171 int size)
13172 {
13173 char *note_name = "LINUX";
13174 return elfcore_write_note (abfd, buf, bufsiz,
13175 note_name, NT_ARM_ZT,
13176 aarch_zt,
13177 size);
13178 }
13179
13180 char *
13181 elfcore_write_arc_v2 (bfd *abfd,
13182 char *buf,
13183 int *bufsiz,
13184 const void *arc_v2,
13185 int size)
13186 {
13187 char *note_name = "LINUX";
13188 return elfcore_write_note (abfd, buf, bufsiz,
13189 note_name, NT_ARC_V2, arc_v2, size);
13190 }
13191
13192 char *
13193 elfcore_write_loongarch_cpucfg (bfd *abfd,
13194 char *buf,
13195 int *bufsiz,
13196 const void *loongarch_cpucfg,
13197 int size)
13198 {
13199 char *note_name = "LINUX";
13200 return elfcore_write_note (abfd, buf, bufsiz,
13201 note_name, NT_LARCH_CPUCFG,
13202 loongarch_cpucfg, size);
13203 }
13204
13205 char *
13206 elfcore_write_loongarch_lbt (bfd *abfd,
13207 char *buf,
13208 int *bufsiz,
13209 const void *loongarch_lbt,
13210 int size)
13211 {
13212 char *note_name = "LINUX";
13213 return elfcore_write_note (abfd, buf, bufsiz,
13214 note_name, NT_LARCH_LBT, loongarch_lbt, size);
13215 }
13216
13217 char *
13218 elfcore_write_loongarch_lsx (bfd *abfd,
13219 char *buf,
13220 int *bufsiz,
13221 const void *loongarch_lsx,
13222 int size)
13223 {
13224 char *note_name = "LINUX";
13225 return elfcore_write_note (abfd, buf, bufsiz,
13226 note_name, NT_LARCH_LSX, loongarch_lsx, size);
13227 }
13228
13229 char *
13230 elfcore_write_loongarch_lasx (bfd *abfd,
13231 char *buf,
13232 int *bufsiz,
13233 const void *loongarch_lasx,
13234 int size)
13235 {
13236 char *note_name = "LINUX";
13237 return elfcore_write_note (abfd, buf, bufsiz,
13238 note_name, NT_LARCH_LASX, loongarch_lasx, size);
13239 }
13240
13241 /* Write the buffer of csr values in CSRS (length SIZE) into the note
13242 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
13243 written into. Return a pointer to the new start of the note buffer, to
13244 replace BUF which may no longer be valid. */
13245
13246 char *
13247 elfcore_write_riscv_csr (bfd *abfd,
13248 char *buf,
13249 int *bufsiz,
13250 const void *csrs,
13251 int size)
13252 {
13253 const char *note_name = "GDB";
13254 return elfcore_write_note (abfd, buf, bufsiz,
13255 note_name, NT_RISCV_CSR, csrs, size);
13256 }
13257
13258 /* Write the target description (a string) pointed to by TDESC, length
13259 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
13260 note is being written into. Return a pointer to the new start of the
13261 note buffer, to replace BUF which may no longer be valid. */
13262
13263 char *
13264 elfcore_write_gdb_tdesc (bfd *abfd,
13265 char *buf,
13266 int *bufsiz,
13267 const void *tdesc,
13268 int size)
13269 {
13270 const char *note_name = "GDB";
13271 return elfcore_write_note (abfd, buf, bufsiz,
13272 note_name, NT_GDB_TDESC, tdesc, size);
13273 }
13274
13275 char *
13276 elfcore_write_register_note (bfd *abfd,
13277 char *buf,
13278 int *bufsiz,
13279 const char *section,
13280 const void *data,
13281 int size)
13282 {
13283 if (strcmp (section, ".reg2") == 0)
13284 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
13285 if (strcmp (section, ".reg-xfp") == 0)
13286 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
13287 if (strcmp (section, ".reg-xstate") == 0)
13288 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
13289 if (strcmp (section, ".reg-x86-segbases") == 0)
13290 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
13291 if (strcmp (section, ".reg-ppc-vmx") == 0)
13292 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
13293 if (strcmp (section, ".reg-ppc-vsx") == 0)
13294 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
13295 if (strcmp (section, ".reg-ppc-tar") == 0)
13296 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
13297 if (strcmp (section, ".reg-ppc-ppr") == 0)
13298 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
13299 if (strcmp (section, ".reg-ppc-dscr") == 0)
13300 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
13301 if (strcmp (section, ".reg-ppc-ebb") == 0)
13302 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
13303 if (strcmp (section, ".reg-ppc-pmu") == 0)
13304 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
13305 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
13306 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
13307 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
13308 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
13309 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
13310 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
13311 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
13312 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
13313 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
13314 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
13315 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
13316 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
13317 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
13318 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
13319 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
13320 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
13321 if (strcmp (section, ".reg-s390-high-gprs") == 0)
13322 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
13323 if (strcmp (section, ".reg-s390-timer") == 0)
13324 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
13325 if (strcmp (section, ".reg-s390-todcmp") == 0)
13326 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
13327 if (strcmp (section, ".reg-s390-todpreg") == 0)
13328 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
13329 if (strcmp (section, ".reg-s390-ctrs") == 0)
13330 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
13331 if (strcmp (section, ".reg-s390-prefix") == 0)
13332 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
13333 if (strcmp (section, ".reg-s390-last-break") == 0)
13334 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
13335 if (strcmp (section, ".reg-s390-system-call") == 0)
13336 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
13337 if (strcmp (section, ".reg-s390-tdb") == 0)
13338 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
13339 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
13340 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
13341 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
13342 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
13343 if (strcmp (section, ".reg-s390-gs-cb") == 0)
13344 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
13345 if (strcmp (section, ".reg-s390-gs-bc") == 0)
13346 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
13347 if (strcmp (section, ".reg-arm-vfp") == 0)
13348 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
13349 if (strcmp (section, ".reg-aarch-tls") == 0)
13350 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
13351 if (strcmp (section, ".reg-aarch-hw-break") == 0)
13352 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
13353 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
13354 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
13355 if (strcmp (section, ".reg-aarch-sve") == 0)
13356 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
13357 if (strcmp (section, ".reg-aarch-pauth") == 0)
13358 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
13359 if (strcmp (section, ".reg-aarch-mte") == 0)
13360 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
13361 if (strcmp (section, ".reg-aarch-ssve") == 0)
13362 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
13363 if (strcmp (section, ".reg-aarch-za") == 0)
13364 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
13365 if (strcmp (section, ".reg-aarch-zt") == 0)
13366 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
13367 if (strcmp (section, ".reg-arc-v2") == 0)
13368 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
13369 if (strcmp (section, ".gdb-tdesc") == 0)
13370 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
13371 if (strcmp (section, ".reg-riscv-csr") == 0)
13372 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
13373 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
13374 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
13375 if (strcmp (section, ".reg-loongarch-lbt") == 0)
13376 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
13377 if (strcmp (section, ".reg-loongarch-lsx") == 0)
13378 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
13379 if (strcmp (section, ".reg-loongarch-lasx") == 0)
13380 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
13381 return NULL;
13382 }
13383
13384 char *
13385 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13386 const void *buf, int bufsiz)
13387 {
13388 return elfcore_write_note (obfd, note_data, note_size,
13389 "CORE", NT_FILE, buf, bufsiz);
13390 }
13391
13392 static bool
13393 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13394 size_t align)
13395 {
13396 char *p;
13397
13398 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13399 gABI specifies that PT_NOTE alignment should be aligned to 4
13400 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
13401 align is less than 4, we use 4 byte alignment. */
13402 if (align < 4)
13403 align = 4;
13404 if (align != 4 && align != 8)
13405 return false;
13406
13407 p = buf;
13408 while (p < buf + size)
13409 {
13410 Elf_External_Note *xnp = (Elf_External_Note *) p;
13411 Elf_Internal_Note in;
13412
13413 if (offsetof (Elf_External_Note, name) > buf - p + size)
13414 return false;
13415
13416 in.type = H_GET_32 (abfd, xnp->type);
13417
13418 in.namesz = H_GET_32 (abfd, xnp->namesz);
13419 in.namedata = xnp->name;
13420 if (in.namesz > buf - in.namedata + size)
13421 return false;
13422
13423 in.descsz = H_GET_32 (abfd, xnp->descsz);
13424 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13425 in.descpos = offset + (in.descdata - buf);
13426 if (in.descsz != 0
13427 && (in.descdata >= buf + size
13428 || in.descsz > buf - in.descdata + size))
13429 return false;
13430
13431 switch (bfd_get_format (abfd))
13432 {
13433 default:
13434 return true;
13435
13436 case bfd_core:
13437 {
13438 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13439 struct
13440 {
13441 const char * string;
13442 size_t len;
13443 bool (*func) (bfd *, Elf_Internal_Note *);
13444 }
13445 grokers[] =
13446 {
13447 GROKER_ELEMENT ("", elfcore_grok_note),
13448 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
13449 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13450 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13451 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13452 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13453 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13454 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
13455 };
13456 #undef GROKER_ELEMENT
13457 int i;
13458
13459 for (i = ARRAY_SIZE (grokers); i--;)
13460 {
13461 if (in.namesz >= grokers[i].len
13462 && strncmp (in.namedata, grokers[i].string,
13463 grokers[i].len) == 0)
13464 {
13465 if (! grokers[i].func (abfd, & in))
13466 return false;
13467 break;
13468 }
13469 }
13470 break;
13471 }
13472
13473 case bfd_object:
13474 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13475 {
13476 if (! elfobj_grok_gnu_note (abfd, &in))
13477 return false;
13478 }
13479 else if (in.namesz == sizeof "stapsdt"
13480 && strcmp (in.namedata, "stapsdt") == 0)
13481 {
13482 if (! elfobj_grok_stapsdt_note (abfd, &in))
13483 return false;
13484 }
13485 break;
13486 }
13487
13488 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13489 }
13490
13491 return true;
13492 }
13493
13494 bool
13495 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13496 size_t align)
13497 {
13498 char *buf;
13499
13500 if (size == 0 || (size + 1) == 0)
13501 return true;
13502
13503 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13504 return false;
13505
13506 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13507 if (buf == NULL)
13508 return false;
13509
13510 /* PR 17512: file: ec08f814
13511 0-termintate the buffer so that string searches will not overflow. */
13512 buf[size] = 0;
13513
13514 if (!elf_parse_notes (abfd, buf, size, offset, align))
13515 {
13516 free (buf);
13517 return false;
13518 }
13519
13520 free (buf);
13521 return true;
13522 }
13523
13524 /* Providing external access to the ELF program header table. */
13526
13527 /* Return an upper bound on the number of bytes required to store a
13528 copy of ABFD's program header table entries. Return -1 if an error
13529 occurs; bfd_get_error will return an appropriate code. */
13530
13531 long
13532 bfd_get_elf_phdr_upper_bound (bfd *abfd)
13533 {
13534 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13535 {
13536 bfd_set_error (bfd_error_wrong_format);
13537 return -1;
13538 }
13539
13540 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13541 }
13542
13543 /* Copy ABFD's program header table entries to *PHDRS. The entries
13544 will be stored as an array of Elf_Internal_Phdr structures, as
13545 defined in include/elf/internal.h. To find out how large the
13546 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13547
13548 Return the number of program header table entries read, or -1 if an
13549 error occurs; bfd_get_error will return an appropriate code. */
13550
13551 int
13552 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13553 {
13554 int num_phdrs;
13555
13556 if (abfd->xvec->flavour != bfd_target_elf_flavour)
13557 {
13558 bfd_set_error (bfd_error_wrong_format);
13559 return -1;
13560 }
13561
13562 num_phdrs = elf_elfheader (abfd)->e_phnum;
13563 if (num_phdrs != 0)
13564 memcpy (phdrs, elf_tdata (abfd)->phdr,
13565 num_phdrs * sizeof (Elf_Internal_Phdr));
13566
13567 return num_phdrs;
13568 }
13569
13570 enum elf_reloc_type_class
13571 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13572 const asection *rel_sec ATTRIBUTE_UNUSED,
13573 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13574 {
13575 return reloc_class_normal;
13576 }
13577
13578 /* For RELA architectures, return the relocation value for a
13579 relocation against a local symbol. */
13580
13581 bfd_vma
13582 _bfd_elf_rela_local_sym (bfd *abfd,
13583 Elf_Internal_Sym *sym,
13584 asection **psec,
13585 Elf_Internal_Rela *rel)
13586 {
13587 asection *sec = *psec;
13588 bfd_vma relocation;
13589
13590 relocation = (sec->output_section->vma
13591 + sec->output_offset
13592 + sym->st_value);
13593 if ((sec->flags & SEC_MERGE)
13594 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13595 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13596 {
13597 rel->r_addend =
13598 _bfd_merged_section_offset (abfd, psec,
13599 elf_section_data (sec)->sec_info,
13600 sym->st_value + rel->r_addend);
13601 if (sec != *psec)
13602 {
13603 /* If we have changed the section, and our original section is
13604 marked with SEC_EXCLUDE, it means that the original
13605 SEC_MERGE section has been completely subsumed in some
13606 other SEC_MERGE section. In this case, we need to leave
13607 some info around for --emit-relocs. */
13608 if ((sec->flags & SEC_EXCLUDE) != 0)
13609 sec->kept_section = *psec;
13610 sec = *psec;
13611 }
13612 rel->r_addend -= relocation;
13613 rel->r_addend += sec->output_section->vma + sec->output_offset;
13614 }
13615 return relocation;
13616 }
13617
13618 bfd_vma
13619 _bfd_elf_rel_local_sym (bfd *abfd,
13620 Elf_Internal_Sym *sym,
13621 asection **psec,
13622 bfd_vma addend)
13623 {
13624 asection *sec = *psec;
13625
13626 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13627 return sym->st_value + addend;
13628
13629 return _bfd_merged_section_offset (abfd, psec,
13630 elf_section_data (sec)->sec_info,
13631 sym->st_value + addend);
13632 }
13633
13634 /* Adjust an address within a section. Given OFFSET within SEC, return
13635 the new offset within the section, based upon changes made to the
13636 section. Returns -1 if the offset is now invalid.
13637 The offset (in abnd out) is in target sized bytes, however big a
13638 byte may be. */
13639
13640 bfd_vma
13641 _bfd_elf_section_offset (bfd *abfd,
13642 struct bfd_link_info *info,
13643 asection *sec,
13644 bfd_vma offset)
13645 {
13646 switch (sec->sec_info_type)
13647 {
13648 case SEC_INFO_TYPE_STABS:
13649 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
13650 offset);
13651 case SEC_INFO_TYPE_EH_FRAME:
13652 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13653
13654 default:
13655 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13656 {
13657 /* Reverse the offset. */
13658 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13659 bfd_size_type address_size = bed->s->arch_size / 8;
13660
13661 /* address_size and sec->size are in octets. Convert
13662 to bytes before subtracting the original offset. */
13663 offset = ((sec->size - address_size)
13664 / bfd_octets_per_byte (abfd, sec) - offset);
13665 }
13666 return offset;
13667 }
13668 }
13669
13670 long
13672 _bfd_elf_get_synthetic_symtab (bfd *abfd,
13673 long symcount ATTRIBUTE_UNUSED,
13674 asymbol **syms ATTRIBUTE_UNUSED,
13675 long dynsymcount,
13676 asymbol **dynsyms,
13677 asymbol **ret)
13678 {
13679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13680 asection *relplt;
13681 asymbol *s;
13682 const char *relplt_name;
13683 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13684 arelent *p;
13685 long count, i, n;
13686 size_t size;
13687 Elf_Internal_Shdr *hdr;
13688 char *names;
13689 asection *plt;
13690
13691 *ret = NULL;
13692
13693 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13694 return 0;
13695
13696 if (dynsymcount <= 0)
13697 return 0;
13698
13699 if (!bed->plt_sym_val)
13700 return 0;
13701
13702 relplt_name = bed->relplt_name;
13703 if (relplt_name == NULL)
13704 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13705 relplt = bfd_get_section_by_name (abfd, relplt_name);
13706 if (relplt == NULL)
13707 return 0;
13708
13709 hdr = &elf_section_data (relplt)->this_hdr;
13710 if (hdr->sh_link != elf_dynsymtab (abfd)
13711 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13712 return 0;
13713
13714 plt = bfd_get_section_by_name (abfd, ".plt");
13715 if (plt == NULL)
13716 return 0;
13717
13718 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13719 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13720 return -1;
13721
13722 count = NUM_SHDR_ENTRIES (hdr);
13723 size = count * sizeof (asymbol);
13724 p = relplt->relocation;
13725 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13726 {
13727 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13728 if (p->addend != 0)
13729 {
13730 #ifdef BFD64
13731 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13732 #else
13733 size += sizeof ("+0x") - 1 + 8;
13734 #endif
13735 }
13736 }
13737
13738 s = *ret = (asymbol *) bfd_malloc (size);
13739 if (s == NULL)
13740 return -1;
13741
13742 names = (char *) (s + count);
13743 p = relplt->relocation;
13744 n = 0;
13745 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13746 {
13747 size_t len;
13748 bfd_vma addr;
13749
13750 addr = bed->plt_sym_val (i, plt, p);
13751 if (addr == (bfd_vma) -1)
13752 continue;
13753
13754 *s = **p->sym_ptr_ptr;
13755 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13756 we are defining a symbol, ensure one of them is set. */
13757 if ((s->flags & BSF_LOCAL) == 0)
13758 s->flags |= BSF_GLOBAL;
13759 s->flags |= BSF_SYNTHETIC;
13760 s->section = plt;
13761 s->value = addr - plt->vma;
13762 s->name = names;
13763 s->udata.p = NULL;
13764 len = strlen ((*p->sym_ptr_ptr)->name);
13765 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13766 names += len;
13767 if (p->addend != 0)
13768 {
13769 char buf[30], *a;
13770
13771 memcpy (names, "+0x", sizeof ("+0x") - 1);
13772 names += sizeof ("+0x") - 1;
13773 bfd_sprintf_vma (abfd, buf, p->addend);
13774 for (a = buf; *a == '0'; ++a)
13775 ;
13776 len = strlen (a);
13777 memcpy (names, a, len);
13778 names += len;
13779 }
13780 memcpy (names, "@plt", sizeof ("@plt"));
13781 names += sizeof ("@plt");
13782 ++s, ++n;
13783 }
13784
13785 return n;
13786 }
13787
13788 /* It is only used by x86-64 so far.
13789 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13790 but current usage would allow all of _bfd_std_section to be zero. */
13791 static const asymbol lcomm_sym
13792 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13793 asection _bfd_elf_large_com_section
13794 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13795 "LARGE_COMMON", 0, SEC_IS_COMMON);
13796
13797 bool
13798 _bfd_elf_final_write_processing (bfd *abfd)
13799 {
13800 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13801
13802 i_ehdrp = elf_elfheader (abfd);
13803
13804 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13805 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13806
13807 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13808 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13809 or STB_GNU_UNIQUE binding. */
13810 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13811 {
13812 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13813 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13814 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13815 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13816 {
13817 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13818 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13819 "and FreeBSD targets"));
13820 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13821 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13822 "only by GNU and FreeBSD targets"));
13823 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13824 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13825 "only by GNU and FreeBSD targets"));
13826 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13827 _bfd_error_handler (_("GNU_RETAIN section is supported "
13828 "only by GNU and FreeBSD targets"));
13829 bfd_set_error (bfd_error_sorry);
13830 return false;
13831 }
13832 }
13833 return true;
13834 }
13835
13836
13837 /* Return TRUE for ELF symbol types that represent functions.
13838 This is the default version of this function, which is sufficient for
13839 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13840
13841 bool
13842 _bfd_elf_is_function_type (unsigned int type)
13843 {
13844 return (type == STT_FUNC
13845 || type == STT_GNU_IFUNC);
13846 }
13847
13848 /* If the ELF symbol SYM might be a function in SEC, return the
13849 function size and set *CODE_OFF to the function's entry point,
13850 otherwise return zero. */
13851
13852 bfd_size_type
13853 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13854 bfd_vma *code_off)
13855 {
13856 bfd_size_type size;
13857 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13858
13859 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13860 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13861 || sym->section != sec)
13862 return 0;
13863
13864 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13865
13866 /* In theory we should check that the symbol's type satisfies
13867 _bfd_elf_is_function_type(), but there are some function-like
13868 symbols which would fail this test. (eg _start). Instead
13869 we check for hidden, local, notype symbols with zero size.
13870 This type of symbol is generated by the annobin plugin for gcc
13871 and clang, and should not be considered to be a function symbol. */
13872 if (size == 0
13873 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13874 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13875 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13876 return 0;
13877
13878 *code_off = sym->value;
13879 /* Do not return 0 for the function's size. */
13880 return size ? size : 1;
13881 }
13882
13883 /* Set to non-zero to enable some debug messages. */
13884 #define DEBUG_SECONDARY_RELOCS 0
13885
13886 /* An internal-to-the-bfd-library only section type
13887 used to indicate a cached secondary reloc section. */
13888 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13889
13890 /* Create a BFD section to hold a secondary reloc section. */
13891
13892 bool
13893 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13894 Elf_Internal_Shdr *hdr,
13895 const char * name,
13896 unsigned int shindex)
13897 {
13898 /* We only support RELA secondary relocs. */
13899 if (hdr->sh_type != SHT_RELA)
13900 return false;
13901
13902 #if DEBUG_SECONDARY_RELOCS
13903 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13904 #endif
13905 hdr->sh_type = SHT_SECONDARY_RELOC;
13906 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13907 }
13908
13909 /* Read in any secondary relocs associated with SEC. */
13910
13911 bool
13912 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13913 asection * sec,
13914 asymbol ** symbols,
13915 bool dynamic)
13916 {
13917 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13918 asection * relsec;
13919 bool result = true;
13920 bfd_vma (*r_sym) (bfd_vma);
13921 ufile_ptr filesize;
13922
13923 #if BFD_DEFAULT_TARGET_SIZE > 32
13924 if (bfd_arch_bits_per_address (abfd) != 32)
13925 r_sym = elf64_r_sym;
13926 else
13927 #endif
13928 r_sym = elf32_r_sym;
13929
13930 if (!elf_section_data (sec)->has_secondary_relocs)
13931 return true;
13932
13933 /* Discover if there are any secondary reloc sections
13934 associated with SEC. */
13935 filesize = bfd_get_file_size (abfd);
13936 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13937 {
13938 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13939
13940 if (hdr->sh_type == SHT_SECONDARY_RELOC
13941 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13942 && (hdr->sh_entsize == ebd->s->sizeof_rel
13943 || hdr->sh_entsize == ebd->s->sizeof_rela))
13944 {
13945 bfd_byte * native_relocs;
13946 bfd_byte * native_reloc;
13947 arelent * internal_relocs;
13948 arelent * internal_reloc;
13949 size_t i;
13950 unsigned int entsize;
13951 unsigned int symcount;
13952 bfd_size_type reloc_count;
13953 size_t amt;
13954
13955 if (ebd->elf_info_to_howto == NULL)
13956 return false;
13957
13958 #if DEBUG_SECONDARY_RELOCS
13959 fprintf (stderr, "read secondary relocs for %s from %s\n",
13960 sec->name, relsec->name);
13961 #endif
13962 entsize = hdr->sh_entsize;
13963
13964 if (filesize != 0
13965 && ((ufile_ptr) hdr->sh_offset > filesize
13966 || hdr->sh_size > filesize - hdr->sh_offset))
13967 {
13968 bfd_set_error (bfd_error_file_truncated);
13969 result = false;
13970 continue;
13971 }
13972
13973 native_relocs = bfd_malloc (hdr->sh_size);
13974 if (native_relocs == NULL)
13975 {
13976 result = false;
13977 continue;
13978 }
13979
13980 reloc_count = NUM_SHDR_ENTRIES (hdr);
13981 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13982 {
13983 free (native_relocs);
13984 bfd_set_error (bfd_error_file_too_big);
13985 result = false;
13986 continue;
13987 }
13988
13989 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13990 if (internal_relocs == NULL)
13991 {
13992 free (native_relocs);
13993 result = false;
13994 continue;
13995 }
13996
13997 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13998 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13999 {
14000 free (native_relocs);
14001 /* The internal_relocs will be freed when
14002 the memory for the bfd is released. */
14003 result = false;
14004 continue;
14005 }
14006
14007 if (dynamic)
14008 symcount = bfd_get_dynamic_symcount (abfd);
14009 else
14010 symcount = bfd_get_symcount (abfd);
14011
14012 for (i = 0, internal_reloc = internal_relocs,
14013 native_reloc = native_relocs;
14014 i < reloc_count;
14015 i++, internal_reloc++, native_reloc += entsize)
14016 {
14017 bool res;
14018 Elf_Internal_Rela rela;
14019
14020 if (entsize == ebd->s->sizeof_rel)
14021 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
14022 else /* entsize == ebd->s->sizeof_rela */
14023 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
14024
14025 /* The address of an ELF reloc is section relative for an object
14026 file, and absolute for an executable file or shared library.
14027 The address of a normal BFD reloc is always section relative,
14028 and the address of a dynamic reloc is absolute.. */
14029 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
14030 internal_reloc->address = rela.r_offset;
14031 else
14032 internal_reloc->address = rela.r_offset - sec->vma;
14033
14034 if (r_sym (rela.r_info) == STN_UNDEF)
14035 {
14036 /* FIXME: This and the error case below mean that we
14037 have a symbol on relocs that is not elf_symbol_type. */
14038 internal_reloc->sym_ptr_ptr =
14039 bfd_abs_section_ptr->symbol_ptr_ptr;
14040 }
14041 else if (r_sym (rela.r_info) > symcount)
14042 {
14043 _bfd_error_handler
14044 /* xgettext:c-format */
14045 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
14046 abfd, sec, i, (long) r_sym (rela.r_info));
14047 bfd_set_error (bfd_error_bad_value);
14048 internal_reloc->sym_ptr_ptr =
14049 bfd_abs_section_ptr->symbol_ptr_ptr;
14050 result = false;
14051 }
14052 else
14053 {
14054 asymbol **ps;
14055
14056 ps = symbols + r_sym (rela.r_info) - 1;
14057 internal_reloc->sym_ptr_ptr = ps;
14058 /* Make sure that this symbol is not removed by strip. */
14059 (*ps)->flags |= BSF_KEEP;
14060 }
14061
14062 internal_reloc->addend = rela.r_addend;
14063
14064 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
14065 if (! res || internal_reloc->howto == NULL)
14066 {
14067 #if DEBUG_SECONDARY_RELOCS
14068 fprintf (stderr,
14069 "there is no howto associated with reloc %lx\n",
14070 rela.r_info);
14071 #endif
14072 result = false;
14073 }
14074 }
14075
14076 free (native_relocs);
14077 /* Store the internal relocs. */
14078 elf_section_data (relsec)->sec_info = internal_relocs;
14079 }
14080 }
14081
14082 return result;
14083 }
14084
14085 /* Set the ELF section header fields of an output secondary reloc section. */
14086
14087 bool
14088 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
14089 bfd *obfd ATTRIBUTE_UNUSED,
14090 const Elf_Internal_Shdr *isection,
14091 Elf_Internal_Shdr *osection)
14092 {
14093 asection * isec;
14094 asection * osec;
14095 struct bfd_elf_section_data * esd;
14096
14097 if (isection == NULL)
14098 return false;
14099
14100 if (isection->sh_type != SHT_SECONDARY_RELOC)
14101 return true;
14102
14103 isec = isection->bfd_section;
14104 if (isec == NULL)
14105 return false;
14106
14107 osec = osection->bfd_section;
14108 if (osec == NULL)
14109 return false;
14110
14111 esd = elf_section_data (osec);
14112 BFD_ASSERT (esd->sec_info == NULL);
14113 esd->sec_info = elf_section_data (isec)->sec_info;
14114 osection->sh_type = SHT_RELA;
14115 osection->sh_link = elf_onesymtab (obfd);
14116 if (osection->sh_link == 0)
14117 {
14118 /* There is no symbol table - we are hosed... */
14119 _bfd_error_handler
14120 /* xgettext:c-format */
14121 (_("%pB(%pA): link section cannot be set"
14122 " because the output file does not have a symbol table"),
14123 obfd, osec);
14124 bfd_set_error (bfd_error_bad_value);
14125 return false;
14126 }
14127
14128 /* Find the output section that corresponds to the isection's
14129 sh_info link. */
14130 if (isection->sh_info == 0
14131 || isection->sh_info >= elf_numsections (ibfd))
14132 {
14133 _bfd_error_handler
14134 /* xgettext:c-format */
14135 (_("%pB(%pA): info section index is invalid"),
14136 obfd, osec);
14137 bfd_set_error (bfd_error_bad_value);
14138 return false;
14139 }
14140
14141 isection = elf_elfsections (ibfd)[isection->sh_info];
14142
14143 if (isection == NULL
14144 || isection->bfd_section == NULL
14145 || isection->bfd_section->output_section == NULL)
14146 {
14147 _bfd_error_handler
14148 /* xgettext:c-format */
14149 (_("%pB(%pA): info section index cannot be set"
14150 " because the section is not in the output"),
14151 obfd, osec);
14152 bfd_set_error (bfd_error_bad_value);
14153 return false;
14154 }
14155
14156 esd = elf_section_data (isection->bfd_section->output_section);
14157 BFD_ASSERT (esd != NULL);
14158 osection->sh_info = esd->this_idx;
14159 esd->has_secondary_relocs = true;
14160 #if DEBUG_SECONDARY_RELOCS
14161 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
14162 osec->name, osection->sh_link, osection->sh_info);
14163 fprintf (stderr, "mark section %s as having secondary relocs\n",
14164 bfd_section_name (isection->bfd_section->output_section));
14165 #endif
14166
14167 return true;
14168 }
14169
14170 /* Write out a secondary reloc section.
14171
14172 FIXME: Currently this function can result in a serious performance penalty
14173 for files with secondary relocs and lots of sections. The proper way to
14174 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
14175 relocs together and then to have this function just walk that chain. */
14176
14177 bool
14178 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
14179 {
14180 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
14181 bfd_vma addr_offset;
14182 asection * relsec;
14183 bfd_vma (*r_info) (bfd_vma, bfd_vma);
14184 bool result = true;
14185
14186 if (sec == NULL)
14187 return false;
14188
14189 #if BFD_DEFAULT_TARGET_SIZE > 32
14190 if (bfd_arch_bits_per_address (abfd) != 32)
14191 r_info = elf64_r_info;
14192 else
14193 #endif
14194 r_info = elf32_r_info;
14195
14196 /* The address of an ELF reloc is section relative for an object
14197 file, and absolute for an executable file or shared library.
14198 The address of a BFD reloc is always section relative. */
14199 addr_offset = 0;
14200 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
14201 addr_offset = sec->vma;
14202
14203 /* Discover if there are any secondary reloc sections
14204 associated with SEC. */
14205 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
14206 {
14207 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
14208 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
14209
14210 if (hdr->sh_type == SHT_RELA
14211 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
14212 {
14213 asymbol * last_sym;
14214 int last_sym_idx;
14215 size_t reloc_count;
14216 size_t idx;
14217 bfd_size_type entsize;
14218 arelent * src_irel;
14219 bfd_byte * dst_rela;
14220
14221 if (hdr->contents != NULL)
14222 {
14223 _bfd_error_handler
14224 /* xgettext:c-format */
14225 (_("%pB(%pA): error: secondary reloc section processed twice"),
14226 abfd, relsec);
14227 bfd_set_error (bfd_error_bad_value);
14228 result = false;
14229 continue;
14230 }
14231
14232 entsize = hdr->sh_entsize;
14233 if (entsize == 0)
14234 {
14235 _bfd_error_handler
14236 /* xgettext:c-format */
14237 (_("%pB(%pA): error: secondary reloc section"
14238 " has zero sized entries"),
14239 abfd, relsec);
14240 bfd_set_error (bfd_error_bad_value);
14241 result = false;
14242 continue;
14243 }
14244 else if (entsize != ebd->s->sizeof_rel
14245 && entsize != ebd->s->sizeof_rela)
14246 {
14247 _bfd_error_handler
14248 /* xgettext:c-format */
14249 (_("%pB(%pA): error: secondary reloc section"
14250 " has non-standard sized entries"),
14251 abfd, relsec);
14252 bfd_set_error (bfd_error_bad_value);
14253 result = false;
14254 continue;
14255 }
14256
14257 reloc_count = hdr->sh_size / entsize;
14258 hdr->sh_size = entsize * reloc_count;
14259 if (reloc_count == 0)
14260 {
14261 _bfd_error_handler
14262 /* xgettext:c-format */
14263 (_("%pB(%pA): error: secondary reloc section is empty!"),
14264 abfd, relsec);
14265 bfd_set_error (bfd_error_bad_value);
14266 result = false;
14267 continue;
14268 }
14269
14270 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14271 if (hdr->contents == NULL)
14272 continue;
14273
14274 #if DEBUG_SECONDARY_RELOCS
14275 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14276 reloc_count, sec->name, relsec->name);
14277 #endif
14278 last_sym = NULL;
14279 last_sym_idx = 0;
14280 dst_rela = hdr->contents;
14281 src_irel = (arelent *) esd->sec_info;
14282 if (src_irel == NULL)
14283 {
14284 _bfd_error_handler
14285 /* xgettext:c-format */
14286 (_("%pB(%pA): error: internal relocs missing"
14287 " for secondary reloc section"),
14288 abfd, relsec);
14289 bfd_set_error (bfd_error_bad_value);
14290 result = false;
14291 continue;
14292 }
14293
14294 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14295 {
14296 Elf_Internal_Rela src_rela;
14297 arelent *ptr;
14298 asymbol *sym;
14299 int n;
14300
14301 ptr = src_irel + idx;
14302 if (ptr == NULL)
14303 {
14304 _bfd_error_handler
14305 /* xgettext:c-format */
14306 (_("%pB(%pA): error: reloc table entry %zu is empty"),
14307 abfd, relsec, idx);
14308 bfd_set_error (bfd_error_bad_value);
14309 result = false;
14310 break;
14311 }
14312
14313 if (ptr->sym_ptr_ptr == NULL)
14314 {
14315 /* FIXME: Is this an error ? */
14316 n = 0;
14317 }
14318 else
14319 {
14320 sym = *ptr->sym_ptr_ptr;
14321
14322 if (sym == last_sym)
14323 n = last_sym_idx;
14324 else
14325 {
14326 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14327 if (n < 0)
14328 {
14329 _bfd_error_handler
14330 /* xgettext:c-format */
14331 (_("%pB(%pA): error: secondary reloc %zu"
14332 " references a missing symbol"),
14333 abfd, relsec, idx);
14334 bfd_set_error (bfd_error_bad_value);
14335 result = false;
14336 n = 0;
14337 }
14338
14339 last_sym = sym;
14340 last_sym_idx = n;
14341 }
14342
14343 if (sym->the_bfd != NULL
14344 && sym->the_bfd->xvec != abfd->xvec
14345 && ! _bfd_elf_validate_reloc (abfd, ptr))
14346 {
14347 _bfd_error_handler
14348 /* xgettext:c-format */
14349 (_("%pB(%pA): error: secondary reloc %zu"
14350 " references a deleted symbol"),
14351 abfd, relsec, idx);
14352 bfd_set_error (bfd_error_bad_value);
14353 result = false;
14354 n = 0;
14355 }
14356 }
14357
14358 src_rela.r_offset = ptr->address + addr_offset;
14359 if (ptr->howto == NULL)
14360 {
14361 _bfd_error_handler
14362 /* xgettext:c-format */
14363 (_("%pB(%pA): error: secondary reloc %zu"
14364 " is of an unknown type"),
14365 abfd, relsec, idx);
14366 bfd_set_error (bfd_error_bad_value);
14367 result = false;
14368 src_rela.r_info = r_info (0, 0);
14369 }
14370 else
14371 src_rela.r_info = r_info (n, ptr->howto->type);
14372 src_rela.r_addend = ptr->addend;
14373
14374 if (entsize == ebd->s->sizeof_rel)
14375 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14376 else /* entsize == ebd->s->sizeof_rela */
14377 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14378 }
14379 }
14380 }
14381
14382 return result;
14383 }
14384