d496c1b2983b0ebaf9c8a6082ce42ec2a6a869d4
[binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2
3 Copyright (C) 1993-2022 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22
23 /*
24 SECTION
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
34
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include <limits.h>
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
47
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
51
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
58
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
62
63 /* Swap in a Verdef structure. */
64
65 void
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
69 {
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
77 }
78
79 /* Swap out a Verdef structure. */
80
81 void
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
85 {
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93 }
94
95 /* Swap in a Verdaux structure. */
96
97 void
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
101 {
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
104 }
105
106 /* Swap out a Verdaux structure. */
107
108 void
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
112 {
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115 }
116
117 /* Swap in a Verneed structure. */
118
119 void
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
123 {
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
129 }
130
131 /* Swap out a Verneed structure. */
132
133 void
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
137 {
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143 }
144
145 /* Swap in a Vernaux structure. */
146
147 void
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
151 {
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
157 }
158
159 /* Swap out a Vernaux structure. */
160
161 void
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
165 {
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171 }
172
173 /* Swap in a Versym structure. */
174
175 void
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
179 {
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181 }
182
183 /* Swap out a Versym structure. */
184
185 void
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
189 {
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191 }
192
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
195
196 unsigned long
197 bfd_elf_hash (const char *namearg)
198 {
199 const unsigned char *name = (const unsigned char *) namearg;
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
203
204 while ((ch = *name++) != '\0')
205 {
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
208 {
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
213 }
214 }
215 return h & 0xffffffff;
216 }
217
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
220
221 unsigned long
222 bfd_elf_gnu_hash (const char *namearg)
223 {
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
227
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
231 }
232
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bool
236 bfd_elf_allocate_object (bfd *abfd,
237 size_t object_size,
238 enum elf_target_id object_id)
239 {
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
243 return false;
244
245 elf_object_id (abfd) = object_id;
246 if (abfd->direction != read_direction)
247 {
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
250 return false;
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 }
254 return true;
255 }
256
257
258 bool
259 bfd_elf_make_object (bfd *abfd)
260 {
261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
263 bed->target_id);
264 }
265
266 bool
267 bfd_elf_mkcorefile (bfd *abfd)
268 {
269 /* I think this can be done just like an object file. */
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271 return false;
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
274 }
275
276 char *
277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
278 {
279 Elf_Internal_Shdr **i_shdrp;
280 bfd_byte *shstrtab = NULL;
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
283
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
288 return NULL;
289
290 shstrtab = i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
292 {
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
296
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize + 1 <= 1
300 || bfd_seek (abfd, offset, SEEK_SET) != 0
301 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
302 shstrtabsize)) == NULL)
303 {
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp[shindex]->sh_size = 0;
308 }
309 else
310 shstrtab[shstrtabsize] = '\0';
311 i_shdrp[shindex]->contents = shstrtab;
312 }
313 return (char *) shstrtab;
314 }
315
316 char *
317 bfd_elf_string_from_elf_section (bfd *abfd,
318 unsigned int shindex,
319 unsigned int strindex)
320 {
321 Elf_Internal_Shdr *hdr;
322
323 if (strindex == 0)
324 return "";
325
326 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
327 return NULL;
328
329 hdr = elf_elfsections (abfd)[shindex];
330
331 if (hdr->contents == NULL)
332 {
333 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
334 {
335 /* PR 17512: file: f057ec89. */
336 /* xgettext:c-format */
337 _bfd_error_handler (_("%pB: attempt to load strings from"
338 " a non-string section (number %d)"),
339 abfd, shindex);
340 return NULL;
341 }
342
343 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
344 return NULL;
345 }
346 else
347 {
348 /* PR 24273: The string section's contents may have already
349 been loaded elsewhere, eg because a corrupt file has the
350 string section index in the ELF header pointing at a group
351 section. So be paranoid, and test that the last byte of
352 the section is zero. */
353 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
354 return NULL;
355 }
356
357 if (strindex >= hdr->sh_size)
358 {
359 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
360 _bfd_error_handler
361 /* xgettext:c-format */
362 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
363 abfd, strindex, (uint64_t) hdr->sh_size,
364 (shindex == shstrndx && strindex == hdr->sh_name
365 ? ".shstrtab"
366 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
367 return NULL;
368 }
369
370 return ((char *) hdr->contents) + strindex;
371 }
372
373 /* Read and convert symbols to internal format.
374 SYMCOUNT specifies the number of symbols to read, starting from
375 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 are non-NULL, they are used to store the internal symbols, external
377 symbols, and symbol section index extensions, respectively.
378 Returns a pointer to the internal symbol buffer (malloced if necessary)
379 or NULL if there were no symbols or some kind of problem. */
380
381 Elf_Internal_Sym *
382 bfd_elf_get_elf_syms (bfd *ibfd,
383 Elf_Internal_Shdr *symtab_hdr,
384 size_t symcount,
385 size_t symoffset,
386 Elf_Internal_Sym *intsym_buf,
387 void *extsym_buf,
388 Elf_External_Sym_Shndx *extshndx_buf)
389 {
390 Elf_Internal_Shdr *shndx_hdr;
391 void *alloc_ext;
392 const bfd_byte *esym;
393 Elf_External_Sym_Shndx *alloc_extshndx;
394 Elf_External_Sym_Shndx *shndx;
395 Elf_Internal_Sym *alloc_intsym;
396 Elf_Internal_Sym *isym;
397 Elf_Internal_Sym *isymend;
398 const struct elf_backend_data *bed;
399 size_t extsym_size;
400 size_t amt;
401 file_ptr pos;
402
403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
404 abort ();
405
406 if (symcount == 0)
407 return intsym_buf;
408
409 /* Normal syms might have section extension entries. */
410 shndx_hdr = NULL;
411 if (elf_symtab_shndx_list (ibfd) != NULL)
412 {
413 elf_section_list * entry;
414 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
415
416 /* Find an index section that is linked to this symtab section. */
417 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
418 {
419 /* PR 20063. */
420 if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 continue;
422
423 if (sections[entry->hdr.sh_link] == symtab_hdr)
424 {
425 shndx_hdr = & entry->hdr;
426 break;
427 };
428 }
429
430 if (shndx_hdr == NULL)
431 {
432 if (symtab_hdr == &elf_symtab_hdr (ibfd))
433 /* Not really accurate, but this was how the old code used
434 to work. */
435 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
436 /* Otherwise we do nothing. The assumption is that
437 the index table will not be needed. */
438 }
439 }
440
441 /* Read the symbols. */
442 alloc_ext = NULL;
443 alloc_extshndx = NULL;
444 alloc_intsym = NULL;
445 bed = get_elf_backend_data (ibfd);
446 extsym_size = bed->s->sizeof_sym;
447 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
448 {
449 bfd_set_error (bfd_error_file_too_big);
450 intsym_buf = NULL;
451 goto out;
452 }
453 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
454 if (extsym_buf == NULL)
455 {
456 alloc_ext = bfd_malloc (amt);
457 extsym_buf = alloc_ext;
458 }
459 if (extsym_buf == NULL
460 || bfd_seek (ibfd, pos, SEEK_SET) != 0
461 || bfd_bread (extsym_buf, amt, ibfd) != amt)
462 {
463 intsym_buf = NULL;
464 goto out;
465 }
466
467 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
468 extshndx_buf = NULL;
469 else
470 {
471 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
472 {
473 bfd_set_error (bfd_error_file_too_big);
474 intsym_buf = NULL;
475 goto out;
476 }
477 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
478 if (extshndx_buf == NULL)
479 {
480 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
481 extshndx_buf = alloc_extshndx;
482 }
483 if (extshndx_buf == NULL
484 || bfd_seek (ibfd, pos, SEEK_SET) != 0
485 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
486 {
487 intsym_buf = NULL;
488 goto out;
489 }
490 }
491
492 if (intsym_buf == NULL)
493 {
494 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
495 {
496 bfd_set_error (bfd_error_file_too_big);
497 goto out;
498 }
499 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
500 intsym_buf = alloc_intsym;
501 if (intsym_buf == NULL)
502 goto out;
503 }
504
505 /* Convert the symbols to internal form. */
506 isymend = intsym_buf + symcount;
507 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
508 shndx = extshndx_buf;
509 isym < isymend;
510 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
511 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
512 {
513 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
514 /* xgettext:c-format */
515 _bfd_error_handler (_("%pB symbol number %lu references"
516 " nonexistent SHT_SYMTAB_SHNDX section"),
517 ibfd, (unsigned long) symoffset);
518 free (alloc_intsym);
519 intsym_buf = NULL;
520 goto out;
521 }
522
523 out:
524 free (alloc_ext);
525 free (alloc_extshndx);
526
527 return intsym_buf;
528 }
529
530 /* Look up a symbol name. */
531 const char *
532 bfd_elf_sym_name (bfd *abfd,
533 Elf_Internal_Shdr *symtab_hdr,
534 Elf_Internal_Sym *isym,
535 asection *sym_sec)
536 {
537 const char *name;
538 unsigned int iname = isym->st_name;
539 unsigned int shindex = symtab_hdr->sh_link;
540
541 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
542 /* Check for a bogus st_shndx to avoid crashing. */
543 && isym->st_shndx < elf_numsections (abfd))
544 {
545 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
546 shindex = elf_elfheader (abfd)->e_shstrndx;
547 }
548
549 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
550 if (name == NULL)
551 name = "(null)";
552 else if (sym_sec && *name == '\0')
553 name = bfd_section_name (sym_sec);
554
555 return name;
556 }
557
558 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
559 sections. The first element is the flags, the rest are section
560 pointers. */
561
562 typedef union elf_internal_group {
563 Elf_Internal_Shdr *shdr;
564 unsigned int flags;
565 } Elf_Internal_Group;
566
567 /* Return the name of the group signature symbol. Why isn't the
568 signature just a string? */
569
570 static const char *
571 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
572 {
573 Elf_Internal_Shdr *hdr;
574 unsigned char esym[sizeof (Elf64_External_Sym)];
575 Elf_External_Sym_Shndx eshndx;
576 Elf_Internal_Sym isym;
577
578 /* First we need to ensure the symbol table is available. Make sure
579 that it is a symbol table section. */
580 if (ghdr->sh_link >= elf_numsections (abfd))
581 return NULL;
582 hdr = elf_elfsections (abfd) [ghdr->sh_link];
583 if (hdr->sh_type != SHT_SYMTAB
584 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
585 return NULL;
586
587 /* Go read the symbol. */
588 hdr = &elf_tdata (abfd)->symtab_hdr;
589 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
590 &isym, esym, &eshndx) == NULL)
591 return NULL;
592
593 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
594 }
595
596 /* Set next_in_group list pointer, and group name for NEWSECT. */
597
598 static bool
599 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
600 {
601 unsigned int num_group = elf_tdata (abfd)->num_group;
602
603 /* If num_group is zero, read in all SHT_GROUP sections. The count
604 is set to -1 if there are no SHT_GROUP sections. */
605 if (num_group == 0)
606 {
607 unsigned int i, shnum;
608
609 /* First count the number of groups. If we have a SHT_GROUP
610 section with just a flag word (ie. sh_size is 4), ignore it. */
611 shnum = elf_numsections (abfd);
612 num_group = 0;
613
614 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
615 ( (shdr)->sh_type == SHT_GROUP \
616 && (shdr)->sh_size >= minsize \
617 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
618 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
619
620 for (i = 0; i < shnum; i++)
621 {
622 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
623
624 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
625 num_group += 1;
626 }
627
628 if (num_group == 0)
629 {
630 num_group = (unsigned) -1;
631 elf_tdata (abfd)->num_group = num_group;
632 elf_tdata (abfd)->group_sect_ptr = NULL;
633 }
634 else
635 {
636 /* We keep a list of elf section headers for group sections,
637 so we can find them quickly. */
638 size_t amt;
639
640 elf_tdata (abfd)->num_group = num_group;
641 amt = num_group * sizeof (Elf_Internal_Shdr *);
642 elf_tdata (abfd)->group_sect_ptr
643 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
644 if (elf_tdata (abfd)->group_sect_ptr == NULL)
645 return false;
646 num_group = 0;
647
648 for (i = 0; i < shnum; i++)
649 {
650 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
651
652 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
653 {
654 unsigned char *src;
655 Elf_Internal_Group *dest;
656
657 /* Make sure the group section has a BFD section
658 attached to it. */
659 if (!bfd_section_from_shdr (abfd, i))
660 return false;
661
662 /* Add to list of sections. */
663 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
664 num_group += 1;
665
666 /* Read the raw contents. */
667 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
668 shdr->contents = NULL;
669 if (_bfd_mul_overflow (shdr->sh_size,
670 sizeof (*dest) / 4, &amt)
671 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
672 || !(shdr->contents
673 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
674 {
675 _bfd_error_handler
676 /* xgettext:c-format */
677 (_("%pB: invalid size field in group section"
678 " header: %#" PRIx64 ""),
679 abfd, (uint64_t) shdr->sh_size);
680 bfd_set_error (bfd_error_bad_value);
681 -- num_group;
682 continue;
683 }
684
685 /* Translate raw contents, a flag word followed by an
686 array of elf section indices all in target byte order,
687 to the flag word followed by an array of elf section
688 pointers. */
689 src = shdr->contents + shdr->sh_size;
690 dest = (Elf_Internal_Group *) (shdr->contents + amt);
691
692 while (1)
693 {
694 unsigned int idx;
695
696 src -= 4;
697 --dest;
698 idx = H_GET_32 (abfd, src);
699 if (src == shdr->contents)
700 {
701 dest->shdr = NULL;
702 dest->flags = idx;
703 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
704 shdr->bfd_section->flags
705 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
706 break;
707 }
708 if (idx < shnum)
709 {
710 dest->shdr = elf_elfsections (abfd)[idx];
711 /* PR binutils/23199: All sections in a
712 section group should be marked with
713 SHF_GROUP. But some tools generate
714 broken objects without SHF_GROUP. Fix
715 them up here. */
716 dest->shdr->sh_flags |= SHF_GROUP;
717 }
718 if (idx >= shnum
719 || dest->shdr->sh_type == SHT_GROUP)
720 {
721 _bfd_error_handler
722 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
723 abfd, i);
724 dest->shdr = NULL;
725 }
726 }
727 }
728 }
729
730 /* PR 17510: Corrupt binaries might contain invalid groups. */
731 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
732 {
733 elf_tdata (abfd)->num_group = num_group;
734
735 /* If all groups are invalid then fail. */
736 if (num_group == 0)
737 {
738 elf_tdata (abfd)->group_sect_ptr = NULL;
739 elf_tdata (abfd)->num_group = num_group = -1;
740 _bfd_error_handler
741 (_("%pB: no valid group sections found"), abfd);
742 bfd_set_error (bfd_error_bad_value);
743 }
744 }
745 }
746 }
747
748 if (num_group != (unsigned) -1)
749 {
750 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
751 unsigned int j;
752
753 for (j = 0; j < num_group; j++)
754 {
755 /* Begin search from previous found group. */
756 unsigned i = (j + search_offset) % num_group;
757
758 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
759 Elf_Internal_Group *idx;
760 bfd_size_type n_elt;
761
762 if (shdr == NULL)
763 continue;
764
765 idx = (Elf_Internal_Group *) shdr->contents;
766 if (idx == NULL || shdr->sh_size < 4)
767 {
768 /* See PR 21957 for a reproducer. */
769 /* xgettext:c-format */
770 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
771 abfd, shdr->bfd_section);
772 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
773 bfd_set_error (bfd_error_bad_value);
774 return false;
775 }
776 n_elt = shdr->sh_size / 4;
777
778 /* Look through this group's sections to see if current
779 section is a member. */
780 while (--n_elt != 0)
781 if ((++idx)->shdr == hdr)
782 {
783 asection *s = NULL;
784
785 /* We are a member of this group. Go looking through
786 other members to see if any others are linked via
787 next_in_group. */
788 idx = (Elf_Internal_Group *) shdr->contents;
789 n_elt = shdr->sh_size / 4;
790 while (--n_elt != 0)
791 if ((++idx)->shdr != NULL
792 && (s = idx->shdr->bfd_section) != NULL
793 && elf_next_in_group (s) != NULL)
794 break;
795 if (n_elt != 0)
796 {
797 /* Snarf the group name from other member, and
798 insert current section in circular list. */
799 elf_group_name (newsect) = elf_group_name (s);
800 elf_next_in_group (newsect) = elf_next_in_group (s);
801 elf_next_in_group (s) = newsect;
802 }
803 else
804 {
805 const char *gname;
806
807 gname = group_signature (abfd, shdr);
808 if (gname == NULL)
809 return false;
810 elf_group_name (newsect) = gname;
811
812 /* Start a circular list with one element. */
813 elf_next_in_group (newsect) = newsect;
814 }
815
816 /* If the group section has been created, point to the
817 new member. */
818 if (shdr->bfd_section != NULL)
819 elf_next_in_group (shdr->bfd_section) = newsect;
820
821 elf_tdata (abfd)->group_search_offset = i;
822 j = num_group - 1;
823 break;
824 }
825 }
826 }
827
828 if (elf_group_name (newsect) == NULL)
829 {
830 /* xgettext:c-format */
831 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
832 abfd, newsect);
833 /* PR 29532: Return true here, even though the group info has not been
834 read. Separate debug info files can have empty group sections, but
835 we do not want this to prevent them from being loaded as otherwise
836 GDB will not be able to use them. */
837 return true;
838 }
839 return true;
840 }
841
842 bool
843 _bfd_elf_setup_sections (bfd *abfd)
844 {
845 unsigned int i;
846 unsigned int num_group = elf_tdata (abfd)->num_group;
847 bool result = true;
848 asection *s;
849
850 /* Process SHF_LINK_ORDER. */
851 for (s = abfd->sections; s != NULL; s = s->next)
852 {
853 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
854 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
855 {
856 unsigned int elfsec = this_hdr->sh_link;
857 /* An sh_link value of 0 is now allowed. It indicates that linked
858 to section has already been discarded, but that the current
859 section has been retained for some other reason. This linking
860 section is still a candidate for later garbage collection
861 however. */
862 if (elfsec == 0)
863 {
864 elf_linked_to_section (s) = NULL;
865 }
866 else
867 {
868 asection *linksec = NULL;
869
870 if (elfsec < elf_numsections (abfd))
871 {
872 this_hdr = elf_elfsections (abfd)[elfsec];
873 linksec = this_hdr->bfd_section;
874 }
875
876 /* PR 1991, 2008:
877 Some strip/objcopy may leave an incorrect value in
878 sh_link. We don't want to proceed. */
879 if (linksec == NULL)
880 {
881 _bfd_error_handler
882 /* xgettext:c-format */
883 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
884 s->owner, elfsec, s);
885 result = false;
886 }
887
888 elf_linked_to_section (s) = linksec;
889 }
890 }
891 else if (this_hdr->sh_type == SHT_GROUP
892 && elf_next_in_group (s) == NULL)
893 {
894 _bfd_error_handler
895 /* xgettext:c-format */
896 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
897 abfd, elf_section_data (s)->this_idx);
898 result = false;
899 }
900 }
901
902 /* Process section groups. */
903 if (num_group == (unsigned) -1)
904 return result;
905
906 for (i = 0; i < num_group; i++)
907 {
908 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
909 Elf_Internal_Group *idx;
910 unsigned int n_elt;
911
912 /* PR binutils/18758: Beware of corrupt binaries with invalid
913 group data. */
914 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
915 {
916 _bfd_error_handler
917 /* xgettext:c-format */
918 (_("%pB: section group entry number %u is corrupt"),
919 abfd, i);
920 result = false;
921 continue;
922 }
923
924 idx = (Elf_Internal_Group *) shdr->contents;
925 n_elt = shdr->sh_size / 4;
926
927 while (--n_elt != 0)
928 {
929 ++ idx;
930
931 if (idx->shdr == NULL)
932 continue;
933 else if (idx->shdr->bfd_section)
934 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
935 else if (idx->shdr->sh_type != SHT_RELA
936 && idx->shdr->sh_type != SHT_REL)
937 {
938 /* There are some unknown sections in the group. */
939 _bfd_error_handler
940 /* xgettext:c-format */
941 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
942 abfd,
943 idx->shdr->sh_type,
944 bfd_elf_string_from_elf_section (abfd,
945 (elf_elfheader (abfd)
946 ->e_shstrndx),
947 idx->shdr->sh_name),
948 shdr->bfd_section);
949 result = false;
950 }
951 }
952 }
953
954 return result;
955 }
956
957 bool
958 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959 {
960 return elf_next_in_group (sec) != NULL;
961 }
962
963 const char *
964 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
965 {
966 if (elf_sec_group (sec) != NULL)
967 return elf_group_name (sec);
968 return NULL;
969 }
970
971 static char *
972 convert_debug_to_zdebug (bfd *abfd, const char *name)
973 {
974 unsigned int len = strlen (name);
975 char *new_name = bfd_alloc (abfd, len + 2);
976 if (new_name == NULL)
977 return NULL;
978 new_name[0] = '.';
979 new_name[1] = 'z';
980 memcpy (new_name + 2, name + 1, len);
981 return new_name;
982 }
983
984 static char *
985 convert_zdebug_to_debug (bfd *abfd, const char *name)
986 {
987 unsigned int len = strlen (name);
988 char *new_name = bfd_alloc (abfd, len);
989 if (new_name == NULL)
990 return NULL;
991 new_name[0] = '.';
992 memcpy (new_name + 1, name + 2, len - 1);
993 return new_name;
994 }
995
996 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
997
998 struct lto_section
999 {
1000 int16_t major_version;
1001 int16_t minor_version;
1002 unsigned char slim_object;
1003
1004 /* Flags is a private field that is not defined publicly. */
1005 uint16_t flags;
1006 };
1007
1008 /* Make a BFD section from an ELF section. We store a pointer to the
1009 BFD section in the bfd_section field of the header. */
1010
1011 bool
1012 _bfd_elf_make_section_from_shdr (bfd *abfd,
1013 Elf_Internal_Shdr *hdr,
1014 const char *name,
1015 int shindex)
1016 {
1017 asection *newsect;
1018 flagword flags;
1019 const struct elf_backend_data *bed;
1020 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
1021
1022 if (hdr->bfd_section != NULL)
1023 return true;
1024
1025 newsect = bfd_make_section_anyway (abfd, name);
1026 if (newsect == NULL)
1027 return false;
1028
1029 hdr->bfd_section = newsect;
1030 elf_section_data (newsect)->this_hdr = *hdr;
1031 elf_section_data (newsect)->this_idx = shindex;
1032
1033 /* Always use the real type/flags. */
1034 elf_section_type (newsect) = hdr->sh_type;
1035 elf_section_flags (newsect) = hdr->sh_flags;
1036
1037 newsect->filepos = hdr->sh_offset;
1038
1039 flags = SEC_NO_FLAGS;
1040 if (hdr->sh_type != SHT_NOBITS)
1041 flags |= SEC_HAS_CONTENTS;
1042 if (hdr->sh_type == SHT_GROUP)
1043 flags |= SEC_GROUP;
1044 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1045 {
1046 flags |= SEC_ALLOC;
1047 if (hdr->sh_type != SHT_NOBITS)
1048 flags |= SEC_LOAD;
1049 }
1050 if ((hdr->sh_flags & SHF_WRITE) == 0)
1051 flags |= SEC_READONLY;
1052 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1053 flags |= SEC_CODE;
1054 else if ((flags & SEC_LOAD) != 0)
1055 flags |= SEC_DATA;
1056 if ((hdr->sh_flags & SHF_MERGE) != 0)
1057 {
1058 flags |= SEC_MERGE;
1059 newsect->entsize = hdr->sh_entsize;
1060 }
1061 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1062 flags |= SEC_STRINGS;
1063 if (hdr->sh_flags & SHF_GROUP)
1064 if (!setup_group (abfd, hdr, newsect))
1065 return false;
1066 if ((hdr->sh_flags & SHF_TLS) != 0)
1067 flags |= SEC_THREAD_LOCAL;
1068 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1069 flags |= SEC_EXCLUDE;
1070
1071 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1072 {
1073 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1074 but binutils as of 2019-07-23 did not set the EI_OSABI header
1075 byte. */
1076 case ELFOSABI_GNU:
1077 case ELFOSABI_FREEBSD:
1078 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1079 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1080 /* Fall through */
1081 case ELFOSABI_NONE:
1082 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1083 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1084 break;
1085 }
1086
1087 if ((flags & SEC_ALLOC) == 0)
1088 {
1089 /* The debugging sections appear to be recognized only by name,
1090 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1091 if (name [0] == '.')
1092 {
1093 if (startswith (name, ".debug")
1094 || startswith (name, ".gnu.debuglto_.debug_")
1095 || startswith (name, ".gnu.linkonce.wi.")
1096 || startswith (name, ".zdebug"))
1097 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1098 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1099 || startswith (name, ".note.gnu"))
1100 {
1101 flags |= SEC_ELF_OCTETS;
1102 opb = 1;
1103 }
1104 else if (startswith (name, ".line")
1105 || startswith (name, ".stab")
1106 || strcmp (name, ".gdb_index") == 0)
1107 flags |= SEC_DEBUGGING;
1108 }
1109 }
1110
1111 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1112 || !bfd_set_section_size (newsect, hdr->sh_size)
1113 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1114 & -hdr->sh_addralign)))
1115 return false;
1116
1117 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1118 only link a single copy of the section. This is used to support
1119 g++. g++ will emit each template expansion in its own section.
1120 The symbols will be defined as weak, so that multiple definitions
1121 are permitted. The GNU linker extension is to actually discard
1122 all but one of the sections. */
1123 if (startswith (name, ".gnu.linkonce")
1124 && elf_next_in_group (newsect) == NULL)
1125 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1126
1127 if (!bfd_set_section_flags (newsect, flags))
1128 return false;
1129
1130 bed = get_elf_backend_data (abfd);
1131 if (bed->elf_backend_section_flags)
1132 if (!bed->elf_backend_section_flags (hdr))
1133 return false;
1134
1135 /* We do not parse the PT_NOTE segments as we are interested even in the
1136 separate debug info files which may have the segments offsets corrupted.
1137 PT_NOTEs from the core files are currently not parsed using BFD. */
1138 if (hdr->sh_type == SHT_NOTE)
1139 {
1140 bfd_byte *contents;
1141
1142 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1143 return false;
1144
1145 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1146 hdr->sh_offset, hdr->sh_addralign);
1147 free (contents);
1148 }
1149
1150 if ((newsect->flags & SEC_ALLOC) != 0)
1151 {
1152 Elf_Internal_Phdr *phdr;
1153 unsigned int i, nload;
1154
1155 /* Some ELF linkers produce binaries with all the program header
1156 p_paddr fields zero. If we have such a binary with more than
1157 one PT_LOAD header, then leave the section lma equal to vma
1158 so that we don't create sections with overlapping lma. */
1159 phdr = elf_tdata (abfd)->phdr;
1160 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1161 if (phdr->p_paddr != 0)
1162 break;
1163 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1164 ++nload;
1165 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1166 return true;
1167
1168 phdr = elf_tdata (abfd)->phdr;
1169 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1170 {
1171 if (((phdr->p_type == PT_LOAD
1172 && (hdr->sh_flags & SHF_TLS) == 0)
1173 || phdr->p_type == PT_TLS)
1174 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1175 {
1176 if ((newsect->flags & SEC_LOAD) == 0)
1177 newsect->lma = (phdr->p_paddr
1178 + hdr->sh_addr - phdr->p_vaddr) / opb;
1179 else
1180 /* We used to use the same adjustment for SEC_LOAD
1181 sections, but that doesn't work if the segment
1182 is packed with code from multiple VMAs.
1183 Instead we calculate the section LMA based on
1184 the segment LMA. It is assumed that the
1185 segment will contain sections with contiguous
1186 LMAs, even if the VMAs are not. */
1187 newsect->lma = (phdr->p_paddr
1188 + hdr->sh_offset - phdr->p_offset) / opb;
1189
1190 /* With contiguous segments, we can't tell from file
1191 offsets whether a section with zero size should
1192 be placed at the end of one segment or the
1193 beginning of the next. Decide based on vaddr. */
1194 if (hdr->sh_addr >= phdr->p_vaddr
1195 && (hdr->sh_addr + hdr->sh_size
1196 <= phdr->p_vaddr + phdr->p_memsz))
1197 break;
1198 }
1199 }
1200 }
1201
1202 /* Compress/decompress DWARF debug sections with names: .debug_* and
1203 .zdebug_*, after the section flags is set. */
1204 if ((newsect->flags & SEC_DEBUGGING) != 0
1205 && (newsect->flags & SEC_HAS_CONTENTS) != 0
1206 && ((name[1] == 'd' && name[6] == '_')
1207 || (name[1] == 'z' && name[7] == '_')))
1208 {
1209 enum { nothing, compress, decompress } action = nothing;
1210 int compression_header_size;
1211 bfd_size_type uncompressed_size;
1212 unsigned int uncompressed_align_power;
1213 bool compressed
1214 = bfd_is_section_compressed_with_header (abfd, newsect,
1215 &compression_header_size,
1216 &uncompressed_size,
1217 &uncompressed_align_power);
1218 if (compressed)
1219 {
1220 /* Compressed section. Check if we should decompress. */
1221 if ((abfd->flags & BFD_DECOMPRESS))
1222 action = decompress;
1223 }
1224
1225 /* Compress the uncompressed section or convert from/to .zdebug*
1226 section. Check if we should compress. */
1227 if (action == nothing)
1228 {
1229 if (newsect->size != 0
1230 && (abfd->flags & BFD_COMPRESS)
1231 && compression_header_size >= 0
1232 && uncompressed_size > 0
1233 && (!compressed
1234 || ((compression_header_size > 0)
1235 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1236 action = compress;
1237 else
1238 return true;
1239 }
1240
1241 if (action == compress)
1242 {
1243 if (!bfd_init_section_compress_status (abfd, newsect))
1244 {
1245 _bfd_error_handler
1246 /* xgettext:c-format */
1247 (_("%pB: unable to initialize compress status for section %s"),
1248 abfd, name);
1249 return false;
1250 }
1251 }
1252 else
1253 {
1254 if (!bfd_init_section_decompress_status (abfd, newsect))
1255 {
1256 _bfd_error_handler
1257 /* xgettext:c-format */
1258 (_("%pB: unable to initialize decompress status"
1259 " for section %s"),
1260 abfd, name);
1261 return false;
1262 }
1263 #ifndef HAVE_ZSTD
1264 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1265 {
1266 _bfd_error_handler
1267 /* xgettext:c-format */
1268 (_ ("%pB: section %s is compressed with zstd, but BFD "
1269 "is not built with zstd support"),
1270 abfd, name);
1271 newsect->compress_status = COMPRESS_SECTION_NONE;
1272 return false;
1273 }
1274 #endif
1275 }
1276
1277 if (abfd->is_linker_input)
1278 {
1279 if (name[1] == 'z'
1280 && (action == decompress
1281 || (action == compress
1282 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1283 {
1284 /* Convert section name from .zdebug_* to .debug_* so
1285 that linker will consider this section as a debug
1286 section. */
1287 char *new_name = convert_zdebug_to_debug (abfd, name);
1288 if (new_name == NULL)
1289 return false;
1290 bfd_rename_section (newsect, new_name);
1291 }
1292 }
1293 else
1294 /* For objdump, don't rename the section. For objcopy, delay
1295 section rename to elf_fake_sections. */
1296 newsect->flags |= SEC_ELF_RENAME;
1297 }
1298
1299 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1300 section. */
1301 if (startswith (name, ".gnu.lto_.lto."))
1302 {
1303 struct lto_section lsection;
1304 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1305 sizeof (struct lto_section)))
1306 abfd->lto_slim_object = lsection.slim_object;
1307 }
1308
1309 return true;
1310 }
1311
1312 const char *const bfd_elf_section_type_names[] =
1313 {
1314 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1315 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1316 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1317 };
1318
1319 /* ELF relocs are against symbols. If we are producing relocatable
1320 output, and the reloc is against an external symbol, and nothing
1321 has given us any additional addend, the resulting reloc will also
1322 be against the same symbol. In such a case, we don't want to
1323 change anything about the way the reloc is handled, since it will
1324 all be done at final link time. Rather than put special case code
1325 into bfd_perform_relocation, all the reloc types use this howto
1326 function, or should call this function for relocatable output. */
1327
1328 bfd_reloc_status_type
1329 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1330 arelent *reloc_entry,
1331 asymbol *symbol,
1332 void *data ATTRIBUTE_UNUSED,
1333 asection *input_section,
1334 bfd *output_bfd,
1335 char **error_message ATTRIBUTE_UNUSED)
1336 {
1337 if (output_bfd != NULL
1338 && (symbol->flags & BSF_SECTION_SYM) == 0
1339 && (! reloc_entry->howto->partial_inplace
1340 || reloc_entry->addend == 0))
1341 {
1342 reloc_entry->address += input_section->output_offset;
1343 return bfd_reloc_ok;
1344 }
1345
1346 /* In some cases the relocation should be treated as output section
1347 relative, as when linking ELF DWARF into PE COFF. Many ELF
1348 targets lack section relative relocations and instead use
1349 ordinary absolute relocations for references between DWARF
1350 sections. That is arguably a bug in those targets but it happens
1351 to work for the usual case of linking to non-loaded ELF debug
1352 sections with VMAs forced to zero. PE COFF on the other hand
1353 doesn't allow a section VMA of zero. */
1354 if (output_bfd == NULL
1355 && !reloc_entry->howto->pc_relative
1356 && (symbol->section->flags & SEC_DEBUGGING) != 0
1357 && (input_section->flags & SEC_DEBUGGING) != 0)
1358 reloc_entry->addend -= symbol->section->output_section->vma;
1359
1360 return bfd_reloc_continue;
1361 }
1362 \f
1363 /* Returns TRUE if section A matches section B.
1364 Names, addresses and links may be different, but everything else
1365 should be the same. */
1366
1367 static bool
1368 section_match (const Elf_Internal_Shdr * a,
1369 const Elf_Internal_Shdr * b)
1370 {
1371 if (a->sh_type != b->sh_type
1372 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1373 || a->sh_addralign != b->sh_addralign
1374 || a->sh_entsize != b->sh_entsize)
1375 return false;
1376 if (a->sh_type == SHT_SYMTAB
1377 || a->sh_type == SHT_STRTAB)
1378 return true;
1379 return a->sh_size == b->sh_size;
1380 }
1381
1382 /* Find a section in OBFD that has the same characteristics
1383 as IHEADER. Return the index of this section or SHN_UNDEF if
1384 none can be found. Check's section HINT first, as this is likely
1385 to be the correct section. */
1386
1387 static unsigned int
1388 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1389 const unsigned int hint)
1390 {
1391 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1392 unsigned int i;
1393
1394 BFD_ASSERT (iheader != NULL);
1395
1396 /* See PR 20922 for a reproducer of the NULL test. */
1397 if (hint < elf_numsections (obfd)
1398 && oheaders[hint] != NULL
1399 && section_match (oheaders[hint], iheader))
1400 return hint;
1401
1402 for (i = 1; i < elf_numsections (obfd); i++)
1403 {
1404 Elf_Internal_Shdr * oheader = oheaders[i];
1405
1406 if (oheader == NULL)
1407 continue;
1408 if (section_match (oheader, iheader))
1409 /* FIXME: Do we care if there is a potential for
1410 multiple matches ? */
1411 return i;
1412 }
1413
1414 return SHN_UNDEF;
1415 }
1416
1417 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1418 Processor specific section, based upon a matching input section.
1419 Returns TRUE upon success, FALSE otherwise. */
1420
1421 static bool
1422 copy_special_section_fields (const bfd *ibfd,
1423 bfd *obfd,
1424 const Elf_Internal_Shdr *iheader,
1425 Elf_Internal_Shdr *oheader,
1426 const unsigned int secnum)
1427 {
1428 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1429 const Elf_Internal_Shdr **iheaders
1430 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1431 bool changed = false;
1432 unsigned int sh_link;
1433
1434 if (oheader->sh_type == SHT_NOBITS)
1435 {
1436 /* This is a feature for objcopy --only-keep-debug:
1437 When a section's type is changed to NOBITS, we preserve
1438 the sh_link and sh_info fields so that they can be
1439 matched up with the original.
1440
1441 Note: Strictly speaking these assignments are wrong.
1442 The sh_link and sh_info fields should point to the
1443 relevent sections in the output BFD, which may not be in
1444 the same location as they were in the input BFD. But
1445 the whole point of this action is to preserve the
1446 original values of the sh_link and sh_info fields, so
1447 that they can be matched up with the section headers in
1448 the original file. So strictly speaking we may be
1449 creating an invalid ELF file, but it is only for a file
1450 that just contains debug info and only for sections
1451 without any contents. */
1452 if (oheader->sh_link == 0)
1453 oheader->sh_link = iheader->sh_link;
1454 if (oheader->sh_info == 0)
1455 oheader->sh_info = iheader->sh_info;
1456 return true;
1457 }
1458
1459 /* Allow the target a chance to decide how these fields should be set. */
1460 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1461 iheader, oheader))
1462 return true;
1463
1464 /* We have an iheader which might match oheader, and which has non-zero
1465 sh_info and/or sh_link fields. Attempt to follow those links and find
1466 the section in the output bfd which corresponds to the linked section
1467 in the input bfd. */
1468 if (iheader->sh_link != SHN_UNDEF)
1469 {
1470 /* See PR 20931 for a reproducer. */
1471 if (iheader->sh_link >= elf_numsections (ibfd))
1472 {
1473 _bfd_error_handler
1474 /* xgettext:c-format */
1475 (_("%pB: invalid sh_link field (%d) in section number %d"),
1476 ibfd, iheader->sh_link, secnum);
1477 return false;
1478 }
1479
1480 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1481 if (sh_link != SHN_UNDEF)
1482 {
1483 oheader->sh_link = sh_link;
1484 changed = true;
1485 }
1486 else
1487 /* FIXME: Should we install iheader->sh_link
1488 if we could not find a match ? */
1489 _bfd_error_handler
1490 /* xgettext:c-format */
1491 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1492 }
1493
1494 if (iheader->sh_info)
1495 {
1496 /* The sh_info field can hold arbitrary information, but if the
1497 SHF_LINK_INFO flag is set then it should be interpreted as a
1498 section index. */
1499 if (iheader->sh_flags & SHF_INFO_LINK)
1500 {
1501 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1502 iheader->sh_info);
1503 if (sh_link != SHN_UNDEF)
1504 oheader->sh_flags |= SHF_INFO_LINK;
1505 }
1506 else
1507 /* No idea what it means - just copy it. */
1508 sh_link = iheader->sh_info;
1509
1510 if (sh_link != SHN_UNDEF)
1511 {
1512 oheader->sh_info = sh_link;
1513 changed = true;
1514 }
1515 else
1516 _bfd_error_handler
1517 /* xgettext:c-format */
1518 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1519 }
1520
1521 return changed;
1522 }
1523
1524 /* Copy the program header and other data from one object module to
1525 another. */
1526
1527 bool
1528 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1529 {
1530 const Elf_Internal_Shdr **iheaders
1531 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1532 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1533 const struct elf_backend_data *bed;
1534 unsigned int i;
1535
1536 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1537 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1538 return true;
1539
1540 if (!elf_flags_init (obfd))
1541 {
1542 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1543 elf_flags_init (obfd) = true;
1544 }
1545
1546 elf_gp (obfd) = elf_gp (ibfd);
1547
1548 /* Also copy the EI_OSABI field. */
1549 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1550 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1551
1552 /* If set, copy the EI_ABIVERSION field. */
1553 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1554 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1555 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1556
1557 /* Copy object attributes. */
1558 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1559
1560 if (iheaders == NULL || oheaders == NULL)
1561 return true;
1562
1563 bed = get_elf_backend_data (obfd);
1564
1565 /* Possibly copy other fields in the section header. */
1566 for (i = 1; i < elf_numsections (obfd); i++)
1567 {
1568 unsigned int j;
1569 Elf_Internal_Shdr * oheader = oheaders[i];
1570
1571 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1572 because of a special case need for generating separate debug info
1573 files. See below for more details. */
1574 if (oheader == NULL
1575 || (oheader->sh_type != SHT_NOBITS
1576 && oheader->sh_type < SHT_LOOS))
1577 continue;
1578
1579 /* Ignore empty sections, and sections whose
1580 fields have already been initialised. */
1581 if (oheader->sh_size == 0
1582 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1583 continue;
1584
1585 /* Scan for the matching section in the input bfd.
1586 First we try for a direct mapping between the input and
1587 output sections. */
1588 for (j = 1; j < elf_numsections (ibfd); j++)
1589 {
1590 const Elf_Internal_Shdr * iheader = iheaders[j];
1591
1592 if (iheader == NULL)
1593 continue;
1594
1595 if (oheader->bfd_section != NULL
1596 && iheader->bfd_section != NULL
1597 && iheader->bfd_section->output_section != NULL
1598 && iheader->bfd_section->output_section == oheader->bfd_section)
1599 {
1600 /* We have found a connection from the input section to
1601 the output section. Attempt to copy the header fields.
1602 If this fails then do not try any further sections -
1603 there should only be a one-to-one mapping between
1604 input and output. */
1605 if (!copy_special_section_fields (ibfd, obfd,
1606 iheader, oheader, i))
1607 j = elf_numsections (ibfd);
1608 break;
1609 }
1610 }
1611
1612 if (j < elf_numsections (ibfd))
1613 continue;
1614
1615 /* That failed. So try to deduce the corresponding input section.
1616 Unfortunately we cannot compare names as the output string table
1617 is empty, so instead we check size, address and type. */
1618 for (j = 1; j < elf_numsections (ibfd); j++)
1619 {
1620 const Elf_Internal_Shdr * iheader = iheaders[j];
1621
1622 if (iheader == NULL)
1623 continue;
1624
1625 /* Try matching fields in the input section's header.
1626 Since --only-keep-debug turns all non-debug sections into
1627 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1628 input type. */
1629 if ((oheader->sh_type == SHT_NOBITS
1630 || iheader->sh_type == oheader->sh_type)
1631 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1632 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1633 && iheader->sh_addralign == oheader->sh_addralign
1634 && iheader->sh_entsize == oheader->sh_entsize
1635 && iheader->sh_size == oheader->sh_size
1636 && iheader->sh_addr == oheader->sh_addr
1637 && (iheader->sh_info != oheader->sh_info
1638 || iheader->sh_link != oheader->sh_link))
1639 {
1640 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1641 break;
1642 }
1643 }
1644
1645 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1646 {
1647 /* Final attempt. Call the backend copy function
1648 with a NULL input section. */
1649 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1650 NULL, oheader);
1651 }
1652 }
1653
1654 return true;
1655 }
1656
1657 static const char *
1658 get_segment_type (unsigned int p_type)
1659 {
1660 const char *pt;
1661 switch (p_type)
1662 {
1663 case PT_NULL: pt = "NULL"; break;
1664 case PT_LOAD: pt = "LOAD"; break;
1665 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1666 case PT_INTERP: pt = "INTERP"; break;
1667 case PT_NOTE: pt = "NOTE"; break;
1668 case PT_SHLIB: pt = "SHLIB"; break;
1669 case PT_PHDR: pt = "PHDR"; break;
1670 case PT_TLS: pt = "TLS"; break;
1671 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1672 case PT_GNU_STACK: pt = "STACK"; break;
1673 case PT_GNU_RELRO: pt = "RELRO"; break;
1674 default: pt = NULL; break;
1675 }
1676 return pt;
1677 }
1678
1679 /* Print out the program headers. */
1680
1681 bool
1682 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1683 {
1684 FILE *f = (FILE *) farg;
1685 Elf_Internal_Phdr *p;
1686 asection *s;
1687 bfd_byte *dynbuf = NULL;
1688
1689 p = elf_tdata (abfd)->phdr;
1690 if (p != NULL)
1691 {
1692 unsigned int i, c;
1693
1694 fprintf (f, _("\nProgram Header:\n"));
1695 c = elf_elfheader (abfd)->e_phnum;
1696 for (i = 0; i < c; i++, p++)
1697 {
1698 const char *pt = get_segment_type (p->p_type);
1699 char buf[20];
1700
1701 if (pt == NULL)
1702 {
1703 sprintf (buf, "0x%lx", p->p_type);
1704 pt = buf;
1705 }
1706 fprintf (f, "%8s off 0x", pt);
1707 bfd_fprintf_vma (abfd, f, p->p_offset);
1708 fprintf (f, " vaddr 0x");
1709 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1710 fprintf (f, " paddr 0x");
1711 bfd_fprintf_vma (abfd, f, p->p_paddr);
1712 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1713 fprintf (f, " filesz 0x");
1714 bfd_fprintf_vma (abfd, f, p->p_filesz);
1715 fprintf (f, " memsz 0x");
1716 bfd_fprintf_vma (abfd, f, p->p_memsz);
1717 fprintf (f, " flags %c%c%c",
1718 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1719 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1720 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1721 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1722 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1723 fprintf (f, "\n");
1724 }
1725 }
1726
1727 s = bfd_get_section_by_name (abfd, ".dynamic");
1728 if (s != NULL)
1729 {
1730 unsigned int elfsec;
1731 unsigned long shlink;
1732 bfd_byte *extdyn, *extdynend;
1733 size_t extdynsize;
1734 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1735
1736 fprintf (f, _("\nDynamic Section:\n"));
1737
1738 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1739 goto error_return;
1740
1741 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1742 if (elfsec == SHN_BAD)
1743 goto error_return;
1744 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1745
1746 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1747 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1748
1749 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1750 (size_t) (extdynend - extdyn) >= extdynsize;
1751 extdyn += extdynsize)
1752 {
1753 Elf_Internal_Dyn dyn;
1754 const char *name = "";
1755 char ab[20];
1756 bool stringp;
1757 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1758
1759 (*swap_dyn_in) (abfd, extdyn, &dyn);
1760
1761 if (dyn.d_tag == DT_NULL)
1762 break;
1763
1764 stringp = false;
1765 switch (dyn.d_tag)
1766 {
1767 default:
1768 if (bed->elf_backend_get_target_dtag)
1769 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1770
1771 if (!strcmp (name, ""))
1772 {
1773 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1774 name = ab;
1775 }
1776 break;
1777
1778 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1779 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1780 case DT_PLTGOT: name = "PLTGOT"; break;
1781 case DT_HASH: name = "HASH"; break;
1782 case DT_STRTAB: name = "STRTAB"; break;
1783 case DT_SYMTAB: name = "SYMTAB"; break;
1784 case DT_RELA: name = "RELA"; break;
1785 case DT_RELASZ: name = "RELASZ"; break;
1786 case DT_RELAENT: name = "RELAENT"; break;
1787 case DT_STRSZ: name = "STRSZ"; break;
1788 case DT_SYMENT: name = "SYMENT"; break;
1789 case DT_INIT: name = "INIT"; break;
1790 case DT_FINI: name = "FINI"; break;
1791 case DT_SONAME: name = "SONAME"; stringp = true; break;
1792 case DT_RPATH: name = "RPATH"; stringp = true; break;
1793 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1794 case DT_REL: name = "REL"; break;
1795 case DT_RELSZ: name = "RELSZ"; break;
1796 case DT_RELENT: name = "RELENT"; break;
1797 case DT_RELR: name = "RELR"; break;
1798 case DT_RELRSZ: name = "RELRSZ"; break;
1799 case DT_RELRENT: name = "RELRENT"; break;
1800 case DT_PLTREL: name = "PLTREL"; break;
1801 case DT_DEBUG: name = "DEBUG"; break;
1802 case DT_TEXTREL: name = "TEXTREL"; break;
1803 case DT_JMPREL: name = "JMPREL"; break;
1804 case DT_BIND_NOW: name = "BIND_NOW"; break;
1805 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1806 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1807 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1808 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1809 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1810 case DT_FLAGS: name = "FLAGS"; break;
1811 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1812 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1813 case DT_CHECKSUM: name = "CHECKSUM"; break;
1814 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1815 case DT_MOVEENT: name = "MOVEENT"; break;
1816 case DT_MOVESZ: name = "MOVESZ"; break;
1817 case DT_FEATURE: name = "FEATURE"; break;
1818 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1819 case DT_SYMINSZ: name = "SYMINSZ"; break;
1820 case DT_SYMINENT: name = "SYMINENT"; break;
1821 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1822 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1823 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1824 case DT_PLTPAD: name = "PLTPAD"; break;
1825 case DT_MOVETAB: name = "MOVETAB"; break;
1826 case DT_SYMINFO: name = "SYMINFO"; break;
1827 case DT_RELACOUNT: name = "RELACOUNT"; break;
1828 case DT_RELCOUNT: name = "RELCOUNT"; break;
1829 case DT_FLAGS_1: name = "FLAGS_1"; break;
1830 case DT_VERSYM: name = "VERSYM"; break;
1831 case DT_VERDEF: name = "VERDEF"; break;
1832 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1833 case DT_VERNEED: name = "VERNEED"; break;
1834 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1835 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1836 case DT_USED: name = "USED"; break;
1837 case DT_FILTER: name = "FILTER"; stringp = true; break;
1838 case DT_GNU_HASH: name = "GNU_HASH"; break;
1839 }
1840
1841 fprintf (f, " %-20s ", name);
1842 if (! stringp)
1843 {
1844 fprintf (f, "0x");
1845 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1846 }
1847 else
1848 {
1849 const char *string;
1850 unsigned int tagv = dyn.d_un.d_val;
1851
1852 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1853 if (string == NULL)
1854 goto error_return;
1855 fprintf (f, "%s", string);
1856 }
1857 fprintf (f, "\n");
1858 }
1859
1860 free (dynbuf);
1861 dynbuf = NULL;
1862 }
1863
1864 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1865 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1866 {
1867 if (! _bfd_elf_slurp_version_tables (abfd, false))
1868 return false;
1869 }
1870
1871 if (elf_dynverdef (abfd) != 0)
1872 {
1873 Elf_Internal_Verdef *t;
1874
1875 fprintf (f, _("\nVersion definitions:\n"));
1876 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1877 {
1878 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1879 t->vd_flags, t->vd_hash,
1880 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1881 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1882 {
1883 Elf_Internal_Verdaux *a;
1884
1885 fprintf (f, "\t");
1886 for (a = t->vd_auxptr->vda_nextptr;
1887 a != NULL;
1888 a = a->vda_nextptr)
1889 fprintf (f, "%s ",
1890 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1891 fprintf (f, "\n");
1892 }
1893 }
1894 }
1895
1896 if (elf_dynverref (abfd) != 0)
1897 {
1898 Elf_Internal_Verneed *t;
1899
1900 fprintf (f, _("\nVersion References:\n"));
1901 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1902 {
1903 Elf_Internal_Vernaux *a;
1904
1905 fprintf (f, _(" required from %s:\n"),
1906 t->vn_filename ? t->vn_filename : "<corrupt>");
1907 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1908 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1909 a->vna_flags, a->vna_other,
1910 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1911 }
1912 }
1913
1914 return true;
1915
1916 error_return:
1917 free (dynbuf);
1918 return false;
1919 }
1920
1921 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1922 and return symbol version for symbol version itself. */
1923
1924 const char *
1925 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1926 bool base_p,
1927 bool *hidden)
1928 {
1929 const char *version_string = NULL;
1930 if (elf_dynversym (abfd) != 0
1931 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1932 {
1933 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1934
1935 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1936 vernum &= VERSYM_VERSION;
1937
1938 if (vernum == 0)
1939 version_string = "";
1940 else if (vernum == 1
1941 && (vernum > elf_tdata (abfd)->cverdefs
1942 || (elf_tdata (abfd)->verdef[0].vd_flags
1943 == VER_FLG_BASE)))
1944 version_string = base_p ? "Base" : "";
1945 else if (vernum <= elf_tdata (abfd)->cverdefs)
1946 {
1947 const char *nodename
1948 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1949 version_string = "";
1950 if (base_p
1951 || nodename == NULL
1952 || symbol->name == NULL
1953 || strcmp (symbol->name, nodename) != 0)
1954 version_string = nodename;
1955 }
1956 else
1957 {
1958 Elf_Internal_Verneed *t;
1959
1960 version_string = _("<corrupt>");
1961 for (t = elf_tdata (abfd)->verref;
1962 t != NULL;
1963 t = t->vn_nextref)
1964 {
1965 Elf_Internal_Vernaux *a;
1966
1967 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1968 {
1969 if (a->vna_other == vernum)
1970 {
1971 *hidden = true;
1972 version_string = a->vna_nodename;
1973 break;
1974 }
1975 }
1976 }
1977 }
1978 }
1979 return version_string;
1980 }
1981
1982 /* Display ELF-specific fields of a symbol. */
1983
1984 void
1985 bfd_elf_print_symbol (bfd *abfd,
1986 void *filep,
1987 asymbol *symbol,
1988 bfd_print_symbol_type how)
1989 {
1990 FILE *file = (FILE *) filep;
1991 switch (how)
1992 {
1993 case bfd_print_symbol_name:
1994 fprintf (file, "%s", symbol->name);
1995 break;
1996 case bfd_print_symbol_more:
1997 fprintf (file, "elf ");
1998 bfd_fprintf_vma (abfd, file, symbol->value);
1999 fprintf (file, " %x", symbol->flags);
2000 break;
2001 case bfd_print_symbol_all:
2002 {
2003 const char *section_name;
2004 const char *name = NULL;
2005 const struct elf_backend_data *bed;
2006 unsigned char st_other;
2007 bfd_vma val;
2008 const char *version_string;
2009 bool hidden;
2010
2011 section_name = symbol->section ? symbol->section->name : "(*none*)";
2012
2013 bed = get_elf_backend_data (abfd);
2014 if (bed->elf_backend_print_symbol_all)
2015 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2016
2017 if (name == NULL)
2018 {
2019 name = symbol->name;
2020 bfd_print_symbol_vandf (abfd, file, symbol);
2021 }
2022
2023 fprintf (file, " %s\t", section_name);
2024 /* Print the "other" value for a symbol. For common symbols,
2025 we've already printed the size; now print the alignment.
2026 For other symbols, we have no specified alignment, and
2027 we've printed the address; now print the size. */
2028 if (symbol->section && bfd_is_com_section (symbol->section))
2029 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2030 else
2031 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2032 bfd_fprintf_vma (abfd, file, val);
2033
2034 /* If we have version information, print it. */
2035 version_string = _bfd_elf_get_symbol_version_string (abfd,
2036 symbol,
2037 true,
2038 &hidden);
2039 if (version_string)
2040 {
2041 if (!hidden)
2042 fprintf (file, " %-11s", version_string);
2043 else
2044 {
2045 int i;
2046
2047 fprintf (file, " (%s)", version_string);
2048 for (i = 10 - strlen (version_string); i > 0; --i)
2049 putc (' ', file);
2050 }
2051 }
2052
2053 /* If the st_other field is not zero, print it. */
2054 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2055
2056 switch (st_other)
2057 {
2058 case 0: break;
2059 case STV_INTERNAL: fprintf (file, " .internal"); break;
2060 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2061 case STV_PROTECTED: fprintf (file, " .protected"); break;
2062 default:
2063 /* Some other non-defined flags are also present, so print
2064 everything hex. */
2065 fprintf (file, " 0x%02x", (unsigned int) st_other);
2066 }
2067
2068 fprintf (file, " %s", name);
2069 }
2070 break;
2071 }
2072 }
2073 \f
2074 /* ELF .o/exec file reading */
2075
2076 /* Create a new bfd section from an ELF section header. */
2077
2078 bool
2079 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2080 {
2081 Elf_Internal_Shdr *hdr;
2082 Elf_Internal_Ehdr *ehdr;
2083 const struct elf_backend_data *bed;
2084 const char *name;
2085 bool ret = true;
2086
2087 if (shindex >= elf_numsections (abfd))
2088 return false;
2089
2090 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2091 sh_link or sh_info. Detect this here, by refusing to load a
2092 section that we are already in the process of loading. */
2093 if (elf_tdata (abfd)->being_created[shindex])
2094 {
2095 _bfd_error_handler
2096 (_("%pB: warning: loop in section dependencies detected"), abfd);
2097 return false;
2098 }
2099 elf_tdata (abfd)->being_created[shindex] = true;
2100
2101 hdr = elf_elfsections (abfd)[shindex];
2102 ehdr = elf_elfheader (abfd);
2103 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2104 hdr->sh_name);
2105 if (name == NULL)
2106 goto fail;
2107
2108 bed = get_elf_backend_data (abfd);
2109 switch (hdr->sh_type)
2110 {
2111 case SHT_NULL:
2112 /* Inactive section. Throw it away. */
2113 goto success;
2114
2115 case SHT_PROGBITS: /* Normal section with contents. */
2116 case SHT_NOBITS: /* .bss section. */
2117 case SHT_HASH: /* .hash section. */
2118 case SHT_NOTE: /* .note section. */
2119 case SHT_INIT_ARRAY: /* .init_array section. */
2120 case SHT_FINI_ARRAY: /* .fini_array section. */
2121 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2122 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2123 case SHT_GNU_HASH: /* .gnu.hash section. */
2124 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2125 goto success;
2126
2127 case SHT_DYNAMIC: /* Dynamic linking information. */
2128 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2129 goto fail;
2130
2131 if (hdr->sh_link > elf_numsections (abfd))
2132 {
2133 /* PR 10478: Accept Solaris binaries with a sh_link field
2134 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2135 switch (bfd_get_arch (abfd))
2136 {
2137 case bfd_arch_i386:
2138 case bfd_arch_sparc:
2139 if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2140 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2141 break;
2142 /* Otherwise fall through. */
2143 default:
2144 goto fail;
2145 }
2146 }
2147 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2148 goto fail;
2149 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2150 {
2151 Elf_Internal_Shdr *dynsymhdr;
2152
2153 /* The shared libraries distributed with hpux11 have a bogus
2154 sh_link field for the ".dynamic" section. Find the
2155 string table for the ".dynsym" section instead. */
2156 if (elf_dynsymtab (abfd) != 0)
2157 {
2158 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2159 hdr->sh_link = dynsymhdr->sh_link;
2160 }
2161 else
2162 {
2163 unsigned int i, num_sec;
2164
2165 num_sec = elf_numsections (abfd);
2166 for (i = 1; i < num_sec; i++)
2167 {
2168 dynsymhdr = elf_elfsections (abfd)[i];
2169 if (dynsymhdr->sh_type == SHT_DYNSYM)
2170 {
2171 hdr->sh_link = dynsymhdr->sh_link;
2172 break;
2173 }
2174 }
2175 }
2176 }
2177 goto success;
2178
2179 case SHT_SYMTAB: /* A symbol table. */
2180 if (elf_onesymtab (abfd) == shindex)
2181 goto success;
2182
2183 if (hdr->sh_entsize != bed->s->sizeof_sym)
2184 goto fail;
2185
2186 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2187 {
2188 if (hdr->sh_size != 0)
2189 goto fail;
2190 /* Some assemblers erroneously set sh_info to one with a
2191 zero sh_size. ld sees this as a global symbol count
2192 of (unsigned) -1. Fix it here. */
2193 hdr->sh_info = 0;
2194 goto success;
2195 }
2196
2197 /* PR 18854: A binary might contain more than one symbol table.
2198 Unusual, but possible. Warn, but continue. */
2199 if (elf_onesymtab (abfd) != 0)
2200 {
2201 _bfd_error_handler
2202 /* xgettext:c-format */
2203 (_("%pB: warning: multiple symbol tables detected"
2204 " - ignoring the table in section %u"),
2205 abfd, shindex);
2206 goto success;
2207 }
2208 elf_onesymtab (abfd) = shindex;
2209 elf_symtab_hdr (abfd) = *hdr;
2210 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2211 abfd->flags |= HAS_SYMS;
2212
2213 /* Sometimes a shared object will map in the symbol table. If
2214 SHF_ALLOC is set, and this is a shared object, then we also
2215 treat this section as a BFD section. We can not base the
2216 decision purely on SHF_ALLOC, because that flag is sometimes
2217 set in a relocatable object file, which would confuse the
2218 linker. */
2219 if ((hdr->sh_flags & SHF_ALLOC) != 0
2220 && (abfd->flags & DYNAMIC) != 0
2221 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2222 shindex))
2223 goto fail;
2224
2225 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2226 can't read symbols without that section loaded as well. It
2227 is most likely specified by the next section header. */
2228 {
2229 elf_section_list * entry;
2230 unsigned int i, num_sec;
2231
2232 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2233 if (entry->hdr.sh_link == shindex)
2234 goto success;
2235
2236 num_sec = elf_numsections (abfd);
2237 for (i = shindex + 1; i < num_sec; i++)
2238 {
2239 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2240
2241 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2242 && hdr2->sh_link == shindex)
2243 break;
2244 }
2245
2246 if (i == num_sec)
2247 for (i = 1; i < shindex; i++)
2248 {
2249 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2250
2251 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2252 && hdr2->sh_link == shindex)
2253 break;
2254 }
2255
2256 if (i != shindex)
2257 ret = bfd_section_from_shdr (abfd, i);
2258 /* else FIXME: we have failed to find the symbol table.
2259 Should we issue an error? */
2260 goto success;
2261 }
2262
2263 case SHT_DYNSYM: /* A dynamic symbol table. */
2264 if (elf_dynsymtab (abfd) == shindex)
2265 goto success;
2266
2267 if (hdr->sh_entsize != bed->s->sizeof_sym)
2268 goto fail;
2269
2270 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2271 {
2272 if (hdr->sh_size != 0)
2273 goto fail;
2274
2275 /* Some linkers erroneously set sh_info to one with a
2276 zero sh_size. ld sees this as a global symbol count
2277 of (unsigned) -1. Fix it here. */
2278 hdr->sh_info = 0;
2279 goto success;
2280 }
2281
2282 /* PR 18854: A binary might contain more than one dynamic symbol table.
2283 Unusual, but possible. Warn, but continue. */
2284 if (elf_dynsymtab (abfd) != 0)
2285 {
2286 _bfd_error_handler
2287 /* xgettext:c-format */
2288 (_("%pB: warning: multiple dynamic symbol tables detected"
2289 " - ignoring the table in section %u"),
2290 abfd, shindex);
2291 goto success;
2292 }
2293 elf_dynsymtab (abfd) = shindex;
2294 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2295 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2296 abfd->flags |= HAS_SYMS;
2297
2298 /* Besides being a symbol table, we also treat this as a regular
2299 section, so that objcopy can handle it. */
2300 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2301 goto success;
2302
2303 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2304 {
2305 elf_section_list * entry;
2306
2307 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2308 if (entry->ndx == shindex)
2309 goto success;
2310
2311 entry = bfd_alloc (abfd, sizeof (*entry));
2312 if (entry == NULL)
2313 goto fail;
2314 entry->ndx = shindex;
2315 entry->hdr = * hdr;
2316 entry->next = elf_symtab_shndx_list (abfd);
2317 elf_symtab_shndx_list (abfd) = entry;
2318 elf_elfsections (abfd)[shindex] = & entry->hdr;
2319 goto success;
2320 }
2321
2322 case SHT_STRTAB: /* A string table. */
2323 if (hdr->bfd_section != NULL)
2324 goto success;
2325
2326 if (ehdr->e_shstrndx == shindex)
2327 {
2328 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2329 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2330 goto success;
2331 }
2332
2333 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2334 {
2335 symtab_strtab:
2336 elf_tdata (abfd)->strtab_hdr = *hdr;
2337 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2338 goto success;
2339 }
2340
2341 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2342 {
2343 dynsymtab_strtab:
2344 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2345 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2346 elf_elfsections (abfd)[shindex] = hdr;
2347 /* We also treat this as a regular section, so that objcopy
2348 can handle it. */
2349 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2350 shindex);
2351 goto success;
2352 }
2353
2354 /* If the string table isn't one of the above, then treat it as a
2355 regular section. We need to scan all the headers to be sure,
2356 just in case this strtab section appeared before the above. */
2357 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2358 {
2359 unsigned int i, num_sec;
2360
2361 num_sec = elf_numsections (abfd);
2362 for (i = 1; i < num_sec; i++)
2363 {
2364 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2365 if (hdr2->sh_link == shindex)
2366 {
2367 /* Prevent endless recursion on broken objects. */
2368 if (i == shindex)
2369 goto fail;
2370 if (! bfd_section_from_shdr (abfd, i))
2371 goto fail;
2372 if (elf_onesymtab (abfd) == i)
2373 goto symtab_strtab;
2374 if (elf_dynsymtab (abfd) == i)
2375 goto dynsymtab_strtab;
2376 }
2377 }
2378 }
2379 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2380 goto success;
2381
2382 case SHT_REL:
2383 case SHT_RELA:
2384 case SHT_RELR:
2385 /* *These* do a lot of work -- but build no sections! */
2386 {
2387 asection *target_sect;
2388 Elf_Internal_Shdr *hdr2, **p_hdr;
2389 unsigned int num_sec = elf_numsections (abfd);
2390 struct bfd_elf_section_data *esdt;
2391 bfd_size_type size;
2392
2393 if (hdr->sh_type == SHT_REL)
2394 size = bed->s->sizeof_rel;
2395 else if (hdr->sh_type == SHT_RELA)
2396 size = bed->s->sizeof_rela;
2397 else
2398 size = bed->s->arch_size / 8;
2399 if (hdr->sh_entsize != size)
2400 goto fail;
2401
2402 /* Check for a bogus link to avoid crashing. */
2403 if (hdr->sh_link >= num_sec)
2404 {
2405 _bfd_error_handler
2406 /* xgettext:c-format */
2407 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2408 abfd, hdr->sh_link, name, shindex);
2409 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2410 goto success;
2411 }
2412
2413 /* Get the symbol table. */
2414 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2415 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2416 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2417 goto fail;
2418
2419 /* If this is an alloc section in an executable or shared
2420 library, or the reloc section does not use the main symbol
2421 table we don't treat it as a reloc section. BFD can't
2422 adequately represent such a section, so at least for now,
2423 we don't try. We just present it as a normal section. We
2424 also can't use it as a reloc section if it points to the
2425 null section, an invalid section, another reloc section, or
2426 its sh_link points to the null section. */
2427 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2428 && (hdr->sh_flags & SHF_ALLOC) != 0)
2429 || hdr->sh_type == SHT_RELR
2430 || hdr->sh_link == SHN_UNDEF
2431 || hdr->sh_link != elf_onesymtab (abfd)
2432 || hdr->sh_info == SHN_UNDEF
2433 || hdr->sh_info >= num_sec
2434 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2435 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2436 {
2437 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2438 goto success;
2439 }
2440
2441 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2442 goto fail;
2443
2444 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2445 if (target_sect == NULL)
2446 goto fail;
2447
2448 esdt = elf_section_data (target_sect);
2449 if (hdr->sh_type == SHT_RELA)
2450 p_hdr = &esdt->rela.hdr;
2451 else
2452 p_hdr = &esdt->rel.hdr;
2453
2454 /* PR 17512: file: 0b4f81b7.
2455 Also see PR 24456, for a file which deliberately has two reloc
2456 sections. */
2457 if (*p_hdr != NULL)
2458 {
2459 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2460 {
2461 _bfd_error_handler
2462 /* xgettext:c-format */
2463 (_("%pB: warning: secondary relocation section '%s' "
2464 "for section %pA found - ignoring"),
2465 abfd, name, target_sect);
2466 }
2467 else
2468 esdt->has_secondary_relocs = true;
2469 goto success;
2470 }
2471
2472 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2473 if (hdr2 == NULL)
2474 goto fail;
2475 *hdr2 = *hdr;
2476 *p_hdr = hdr2;
2477 elf_elfsections (abfd)[shindex] = hdr2;
2478 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2479 * bed->s->int_rels_per_ext_rel);
2480 target_sect->flags |= SEC_RELOC;
2481 target_sect->relocation = NULL;
2482 target_sect->rel_filepos = hdr->sh_offset;
2483 /* In the section to which the relocations apply, mark whether
2484 its relocations are of the REL or RELA variety. */
2485 if (hdr->sh_size != 0)
2486 {
2487 if (hdr->sh_type == SHT_RELA)
2488 target_sect->use_rela_p = 1;
2489 }
2490 abfd->flags |= HAS_RELOC;
2491 goto success;
2492 }
2493
2494 case SHT_GNU_verdef:
2495 if (hdr->sh_info != 0)
2496 elf_dynverdef (abfd) = shindex;
2497 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2498 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2499 goto success;
2500
2501 case SHT_GNU_versym:
2502 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2503 goto fail;
2504
2505 elf_dynversym (abfd) = shindex;
2506 elf_tdata (abfd)->dynversym_hdr = *hdr;
2507 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2508 goto success;
2509
2510 case SHT_GNU_verneed:
2511 if (hdr->sh_info != 0)
2512 elf_dynverref (abfd) = shindex;
2513 elf_tdata (abfd)->dynverref_hdr = *hdr;
2514 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2515 goto success;
2516
2517 case SHT_SHLIB:
2518 goto success;
2519
2520 case SHT_GROUP:
2521 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2522 goto fail;
2523
2524 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2525 goto fail;
2526
2527 goto success;
2528
2529 default:
2530 /* Possibly an attributes section. */
2531 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2532 || hdr->sh_type == bed->obj_attrs_section_type)
2533 {
2534 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2535 goto fail;
2536 _bfd_elf_parse_attributes (abfd, hdr);
2537 goto success;
2538 }
2539
2540 /* Check for any processor-specific section types. */
2541 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2542 goto success;
2543
2544 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2545 {
2546 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2547 /* FIXME: How to properly handle allocated section reserved
2548 for applications? */
2549 _bfd_error_handler
2550 /* xgettext:c-format */
2551 (_("%pB: unknown type [%#x] section `%s'"),
2552 abfd, hdr->sh_type, name);
2553 else
2554 {
2555 /* Allow sections reserved for applications. */
2556 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2557 goto success;
2558 }
2559 }
2560 else if (hdr->sh_type >= SHT_LOPROC
2561 && hdr->sh_type <= SHT_HIPROC)
2562 /* FIXME: We should handle this section. */
2563 _bfd_error_handler
2564 /* xgettext:c-format */
2565 (_("%pB: unknown type [%#x] section `%s'"),
2566 abfd, hdr->sh_type, name);
2567 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2568 {
2569 /* Unrecognised OS-specific sections. */
2570 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2571 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2572 required to correctly process the section and the file should
2573 be rejected with an error message. */
2574 _bfd_error_handler
2575 /* xgettext:c-format */
2576 (_("%pB: unknown type [%#x] section `%s'"),
2577 abfd, hdr->sh_type, name);
2578 else
2579 {
2580 /* Otherwise it should be processed. */
2581 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2582 goto success;
2583 }
2584 }
2585 else
2586 /* FIXME: We should handle this section. */
2587 _bfd_error_handler
2588 /* xgettext:c-format */
2589 (_("%pB: unknown type [%#x] section `%s'"),
2590 abfd, hdr->sh_type, name);
2591
2592 goto fail;
2593 }
2594
2595 fail:
2596 ret = false;
2597 success:
2598 elf_tdata (abfd)->being_created[shindex] = false;
2599 return ret;
2600 }
2601
2602 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2603
2604 Elf_Internal_Sym *
2605 bfd_sym_from_r_symndx (struct sym_cache *cache,
2606 bfd *abfd,
2607 unsigned long r_symndx)
2608 {
2609 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2610
2611 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2612 {
2613 Elf_Internal_Shdr *symtab_hdr;
2614 unsigned char esym[sizeof (Elf64_External_Sym)];
2615 Elf_External_Sym_Shndx eshndx;
2616
2617 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2618 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2619 &cache->sym[ent], esym, &eshndx) == NULL)
2620 return NULL;
2621
2622 if (cache->abfd != abfd)
2623 {
2624 memset (cache->indx, -1, sizeof (cache->indx));
2625 cache->abfd = abfd;
2626 }
2627 cache->indx[ent] = r_symndx;
2628 }
2629
2630 return &cache->sym[ent];
2631 }
2632
2633 /* Given an ELF section number, retrieve the corresponding BFD
2634 section. */
2635
2636 asection *
2637 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2638 {
2639 if (sec_index >= elf_numsections (abfd))
2640 return NULL;
2641 return elf_elfsections (abfd)[sec_index]->bfd_section;
2642 }
2643
2644 static const struct bfd_elf_special_section special_sections_b[] =
2645 {
2646 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2647 { NULL, 0, 0, 0, 0 }
2648 };
2649
2650 static const struct bfd_elf_special_section special_sections_c[] =
2651 {
2652 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2653 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2654 { NULL, 0, 0, 0, 0 }
2655 };
2656
2657 static const struct bfd_elf_special_section special_sections_d[] =
2658 {
2659 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2660 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2661 /* There are more DWARF sections than these, but they needn't be added here
2662 unless you have to cope with broken compilers that don't emit section
2663 attributes or you want to help the user writing assembler. */
2664 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2665 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2666 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2667 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2668 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2669 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2670 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2671 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2672 { NULL, 0, 0, 0, 0 }
2673 };
2674
2675 static const struct bfd_elf_special_section special_sections_f[] =
2676 {
2677 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2678 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2679 { NULL, 0 , 0, 0, 0 }
2680 };
2681
2682 static const struct bfd_elf_special_section special_sections_g[] =
2683 {
2684 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2685 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2686 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2687 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2688 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2689 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2690 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2691 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2692 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2693 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2694 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2695 { NULL, 0, 0, 0, 0 }
2696 };
2697
2698 static const struct bfd_elf_special_section special_sections_h[] =
2699 {
2700 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2701 { NULL, 0, 0, 0, 0 }
2702 };
2703
2704 static const struct bfd_elf_special_section special_sections_i[] =
2705 {
2706 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2707 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2708 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2709 { NULL, 0, 0, 0, 0 }
2710 };
2711
2712 static const struct bfd_elf_special_section special_sections_l[] =
2713 {
2714 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2715 { NULL, 0, 0, 0, 0 }
2716 };
2717
2718 static const struct bfd_elf_special_section special_sections_n[] =
2719 {
2720 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2721 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2722 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2723 { NULL, 0, 0, 0, 0 }
2724 };
2725
2726 static const struct bfd_elf_special_section special_sections_p[] =
2727 {
2728 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2729 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2730 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2731 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2732 { NULL, 0, 0, 0, 0 }
2733 };
2734
2735 static const struct bfd_elf_special_section special_sections_r[] =
2736 {
2737 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2738 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2739 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2740 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2741 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2742 { NULL, 0, 0, 0, 0 }
2743 };
2744
2745 static const struct bfd_elf_special_section special_sections_s[] =
2746 {
2747 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2748 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2749 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2750 /* See struct bfd_elf_special_section declaration for the semantics of
2751 this special case where .prefix_length != strlen (.prefix). */
2752 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2753 { NULL, 0, 0, 0, 0 }
2754 };
2755
2756 static const struct bfd_elf_special_section special_sections_t[] =
2757 {
2758 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2759 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2760 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2761 { NULL, 0, 0, 0, 0 }
2762 };
2763
2764 static const struct bfd_elf_special_section special_sections_z[] =
2765 {
2766 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2767 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2768 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2769 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2770 { NULL, 0, 0, 0, 0 }
2771 };
2772
2773 static const struct bfd_elf_special_section * const special_sections[] =
2774 {
2775 special_sections_b, /* 'b' */
2776 special_sections_c, /* 'c' */
2777 special_sections_d, /* 'd' */
2778 NULL, /* 'e' */
2779 special_sections_f, /* 'f' */
2780 special_sections_g, /* 'g' */
2781 special_sections_h, /* 'h' */
2782 special_sections_i, /* 'i' */
2783 NULL, /* 'j' */
2784 NULL, /* 'k' */
2785 special_sections_l, /* 'l' */
2786 NULL, /* 'm' */
2787 special_sections_n, /* 'n' */
2788 NULL, /* 'o' */
2789 special_sections_p, /* 'p' */
2790 NULL, /* 'q' */
2791 special_sections_r, /* 'r' */
2792 special_sections_s, /* 's' */
2793 special_sections_t, /* 't' */
2794 NULL, /* 'u' */
2795 NULL, /* 'v' */
2796 NULL, /* 'w' */
2797 NULL, /* 'x' */
2798 NULL, /* 'y' */
2799 special_sections_z /* 'z' */
2800 };
2801
2802 const struct bfd_elf_special_section *
2803 _bfd_elf_get_special_section (const char *name,
2804 const struct bfd_elf_special_section *spec,
2805 unsigned int rela)
2806 {
2807 int i;
2808 int len;
2809
2810 len = strlen (name);
2811
2812 for (i = 0; spec[i].prefix != NULL; i++)
2813 {
2814 int suffix_len;
2815 int prefix_len = spec[i].prefix_length;
2816
2817 if (len < prefix_len)
2818 continue;
2819 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2820 continue;
2821
2822 suffix_len = spec[i].suffix_length;
2823 if (suffix_len <= 0)
2824 {
2825 if (name[prefix_len] != 0)
2826 {
2827 if (suffix_len == 0)
2828 continue;
2829 if (name[prefix_len] != '.'
2830 && (suffix_len == -2
2831 || (rela && spec[i].type == SHT_REL)))
2832 continue;
2833 }
2834 }
2835 else
2836 {
2837 if (len < prefix_len + suffix_len)
2838 continue;
2839 if (memcmp (name + len - suffix_len,
2840 spec[i].prefix + prefix_len,
2841 suffix_len) != 0)
2842 continue;
2843 }
2844 return &spec[i];
2845 }
2846
2847 return NULL;
2848 }
2849
2850 const struct bfd_elf_special_section *
2851 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2852 {
2853 int i;
2854 const struct bfd_elf_special_section *spec;
2855 const struct elf_backend_data *bed;
2856
2857 /* See if this is one of the special sections. */
2858 if (sec->name == NULL)
2859 return NULL;
2860
2861 bed = get_elf_backend_data (abfd);
2862 spec = bed->special_sections;
2863 if (spec)
2864 {
2865 spec = _bfd_elf_get_special_section (sec->name,
2866 bed->special_sections,
2867 sec->use_rela_p);
2868 if (spec != NULL)
2869 return spec;
2870 }
2871
2872 if (sec->name[0] != '.')
2873 return NULL;
2874
2875 i = sec->name[1] - 'b';
2876 if (i < 0 || i > 'z' - 'b')
2877 return NULL;
2878
2879 spec = special_sections[i];
2880
2881 if (spec == NULL)
2882 return NULL;
2883
2884 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2885 }
2886
2887 bool
2888 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2889 {
2890 struct bfd_elf_section_data *sdata;
2891 const struct elf_backend_data *bed;
2892 const struct bfd_elf_special_section *ssect;
2893
2894 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2895 if (sdata == NULL)
2896 {
2897 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2898 sizeof (*sdata));
2899 if (sdata == NULL)
2900 return false;
2901 sec->used_by_bfd = sdata;
2902 }
2903
2904 /* Indicate whether or not this section should use RELA relocations. */
2905 bed = get_elf_backend_data (abfd);
2906 sec->use_rela_p = bed->default_use_rela_p;
2907
2908 /* Set up ELF section type and flags for newly created sections, if
2909 there is an ABI mandated section. */
2910 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2911 if (ssect != NULL)
2912 {
2913 elf_section_type (sec) = ssect->type;
2914 elf_section_flags (sec) = ssect->attr;
2915 }
2916
2917 return _bfd_generic_new_section_hook (abfd, sec);
2918 }
2919
2920 /* Create a new bfd section from an ELF program header.
2921
2922 Since program segments have no names, we generate a synthetic name
2923 of the form segment<NUM>, where NUM is generally the index in the
2924 program header table. For segments that are split (see below) we
2925 generate the names segment<NUM>a and segment<NUM>b.
2926
2927 Note that some program segments may have a file size that is different than
2928 (less than) the memory size. All this means is that at execution the
2929 system must allocate the amount of memory specified by the memory size,
2930 but only initialize it with the first "file size" bytes read from the
2931 file. This would occur for example, with program segments consisting
2932 of combined data+bss.
2933
2934 To handle the above situation, this routine generates TWO bfd sections
2935 for the single program segment. The first has the length specified by
2936 the file size of the segment, and the second has the length specified
2937 by the difference between the two sizes. In effect, the segment is split
2938 into its initialized and uninitialized parts. */
2939
2940 bool
2941 _bfd_elf_make_section_from_phdr (bfd *abfd,
2942 Elf_Internal_Phdr *hdr,
2943 int hdr_index,
2944 const char *type_name)
2945 {
2946 asection *newsect;
2947 char *name;
2948 char namebuf[64];
2949 size_t len;
2950 int split;
2951 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2952
2953 split = ((hdr->p_memsz > 0)
2954 && (hdr->p_filesz > 0)
2955 && (hdr->p_memsz > hdr->p_filesz));
2956
2957 if (hdr->p_filesz > 0)
2958 {
2959 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2960 len = strlen (namebuf) + 1;
2961 name = (char *) bfd_alloc (abfd, len);
2962 if (!name)
2963 return false;
2964 memcpy (name, namebuf, len);
2965 newsect = bfd_make_section (abfd, name);
2966 if (newsect == NULL)
2967 return false;
2968 newsect->vma = hdr->p_vaddr / opb;
2969 newsect->lma = hdr->p_paddr / opb;
2970 newsect->size = hdr->p_filesz;
2971 newsect->filepos = hdr->p_offset;
2972 newsect->flags |= SEC_HAS_CONTENTS;
2973 newsect->alignment_power = bfd_log2 (hdr->p_align);
2974 if (hdr->p_type == PT_LOAD)
2975 {
2976 newsect->flags |= SEC_ALLOC;
2977 newsect->flags |= SEC_LOAD;
2978 if (hdr->p_flags & PF_X)
2979 {
2980 /* FIXME: all we known is that it has execute PERMISSION,
2981 may be data. */
2982 newsect->flags |= SEC_CODE;
2983 }
2984 }
2985 if (!(hdr->p_flags & PF_W))
2986 {
2987 newsect->flags |= SEC_READONLY;
2988 }
2989 }
2990
2991 if (hdr->p_memsz > hdr->p_filesz)
2992 {
2993 bfd_vma align;
2994
2995 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2996 len = strlen (namebuf) + 1;
2997 name = (char *) bfd_alloc (abfd, len);
2998 if (!name)
2999 return false;
3000 memcpy (name, namebuf, len);
3001 newsect = bfd_make_section (abfd, name);
3002 if (newsect == NULL)
3003 return false;
3004 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3005 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3006 newsect->size = hdr->p_memsz - hdr->p_filesz;
3007 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3008 align = newsect->vma & -newsect->vma;
3009 if (align == 0 || align > hdr->p_align)
3010 align = hdr->p_align;
3011 newsect->alignment_power = bfd_log2 (align);
3012 if (hdr->p_type == PT_LOAD)
3013 {
3014 newsect->flags |= SEC_ALLOC;
3015 if (hdr->p_flags & PF_X)
3016 newsect->flags |= SEC_CODE;
3017 }
3018 if (!(hdr->p_flags & PF_W))
3019 newsect->flags |= SEC_READONLY;
3020 }
3021
3022 return true;
3023 }
3024
3025 static bool
3026 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3027 {
3028 /* The return value is ignored. Build-ids are considered optional. */
3029 if (templ->xvec->flavour == bfd_target_elf_flavour)
3030 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3031 (templ, offset);
3032 return false;
3033 }
3034
3035 bool
3036 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3037 {
3038 const struct elf_backend_data *bed;
3039
3040 switch (hdr->p_type)
3041 {
3042 case PT_NULL:
3043 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3044
3045 case PT_LOAD:
3046 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3047 return false;
3048 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3049 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3050 return true;
3051
3052 case PT_DYNAMIC:
3053 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3054
3055 case PT_INTERP:
3056 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3057
3058 case PT_NOTE:
3059 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3060 return false;
3061 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3062 hdr->p_align))
3063 return false;
3064 return true;
3065
3066 case PT_SHLIB:
3067 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3068
3069 case PT_PHDR:
3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3071
3072 case PT_GNU_EH_FRAME:
3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3074 "eh_frame_hdr");
3075
3076 case PT_GNU_STACK:
3077 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3078
3079 case PT_GNU_RELRO:
3080 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3081
3082 default:
3083 /* Check for any processor-specific program segment types. */
3084 bed = get_elf_backend_data (abfd);
3085 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3086 }
3087 }
3088
3089 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3090 REL or RELA. */
3091
3092 Elf_Internal_Shdr *
3093 _bfd_elf_single_rel_hdr (asection *sec)
3094 {
3095 if (elf_section_data (sec)->rel.hdr)
3096 {
3097 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3098 return elf_section_data (sec)->rel.hdr;
3099 }
3100 else
3101 return elf_section_data (sec)->rela.hdr;
3102 }
3103
3104 static bool
3105 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3106 Elf_Internal_Shdr *rel_hdr,
3107 const char *sec_name,
3108 bool use_rela_p)
3109 {
3110 char *name = (char *) bfd_alloc (abfd,
3111 sizeof ".rela" + strlen (sec_name));
3112 if (name == NULL)
3113 return false;
3114
3115 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3116 rel_hdr->sh_name =
3117 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3118 false);
3119 if (rel_hdr->sh_name == (unsigned int) -1)
3120 return false;
3121
3122 return true;
3123 }
3124
3125 /* Allocate and initialize a section-header for a new reloc section,
3126 containing relocations against ASECT. It is stored in RELDATA. If
3127 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3128 relocations. */
3129
3130 static bool
3131 _bfd_elf_init_reloc_shdr (bfd *abfd,
3132 struct bfd_elf_section_reloc_data *reldata,
3133 const char *sec_name,
3134 bool use_rela_p,
3135 bool delay_st_name_p)
3136 {
3137 Elf_Internal_Shdr *rel_hdr;
3138 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3139
3140 BFD_ASSERT (reldata->hdr == NULL);
3141 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3142 reldata->hdr = rel_hdr;
3143
3144 if (delay_st_name_p)
3145 rel_hdr->sh_name = (unsigned int) -1;
3146 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3147 use_rela_p))
3148 return false;
3149 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3150 rel_hdr->sh_entsize = (use_rela_p
3151 ? bed->s->sizeof_rela
3152 : bed->s->sizeof_rel);
3153 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3154 rel_hdr->sh_flags = 0;
3155 rel_hdr->sh_addr = 0;
3156 rel_hdr->sh_size = 0;
3157 rel_hdr->sh_offset = 0;
3158
3159 return true;
3160 }
3161
3162 /* Return the default section type based on the passed in section flags. */
3163
3164 int
3165 bfd_elf_get_default_section_type (flagword flags)
3166 {
3167 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3168 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3169 return SHT_NOBITS;
3170 return SHT_PROGBITS;
3171 }
3172
3173 struct fake_section_arg
3174 {
3175 struct bfd_link_info *link_info;
3176 bool failed;
3177 };
3178
3179 /* Set up an ELF internal section header for a section. */
3180
3181 static void
3182 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3183 {
3184 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3186 struct bfd_elf_section_data *esd = elf_section_data (asect);
3187 Elf_Internal_Shdr *this_hdr;
3188 unsigned int sh_type;
3189 const char *name = asect->name;
3190 bool delay_st_name_p = false;
3191 bfd_vma mask;
3192
3193 if (arg->failed)
3194 {
3195 /* We already failed; just get out of the bfd_map_over_sections
3196 loop. */
3197 return;
3198 }
3199
3200 this_hdr = &esd->this_hdr;
3201
3202 if (arg->link_info)
3203 {
3204 /* ld: compress DWARF debug sections with names: .debug_*. */
3205 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3206 && (asect->flags & SEC_DEBUGGING)
3207 && name[1] == 'd'
3208 && name[6] == '_')
3209 {
3210 /* Set SEC_ELF_COMPRESS to indicate this section should be
3211 compressed. */
3212 asect->flags |= SEC_ELF_COMPRESS;
3213 /* If this section will be compressed, delay adding section
3214 name to section name section after it is compressed in
3215 _bfd_elf_assign_file_positions_for_non_load. */
3216 delay_st_name_p = true;
3217 }
3218 }
3219 else if ((asect->flags & SEC_ELF_RENAME))
3220 {
3221 /* objcopy: rename output DWARF debug section. */
3222 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3223 {
3224 /* When we decompress or compress with SHF_COMPRESSED,
3225 convert section name from .zdebug_* to .debug_* if
3226 needed. */
3227 if (name[1] == 'z')
3228 {
3229 char *new_name = convert_zdebug_to_debug (abfd, name);
3230 if (new_name == NULL)
3231 {
3232 arg->failed = true;
3233 return;
3234 }
3235 name = new_name;
3236 }
3237 }
3238 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3239 {
3240 /* PR binutils/18087: Compression does not always make a
3241 section smaller. So only rename the section when
3242 compression has actually taken place. If input section
3243 name is .zdebug_*, we should never compress it again. */
3244 char *new_name = convert_debug_to_zdebug (abfd, name);
3245 if (new_name == NULL)
3246 {
3247 arg->failed = true;
3248 return;
3249 }
3250 BFD_ASSERT (name[1] != 'z');
3251 name = new_name;
3252 }
3253 }
3254
3255 if (delay_st_name_p)
3256 this_hdr->sh_name = (unsigned int) -1;
3257 else
3258 {
3259 this_hdr->sh_name
3260 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3261 name, false);
3262 if (this_hdr->sh_name == (unsigned int) -1)
3263 {
3264 arg->failed = true;
3265 return;
3266 }
3267 }
3268
3269 /* Don't clear sh_flags. Assembler may set additional bits. */
3270
3271 if ((asect->flags & SEC_ALLOC) != 0
3272 || asect->user_set_vma)
3273 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3274 else
3275 this_hdr->sh_addr = 0;
3276
3277 this_hdr->sh_offset = 0;
3278 this_hdr->sh_size = asect->size;
3279 this_hdr->sh_link = 0;
3280 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3281 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3282 {
3283 _bfd_error_handler
3284 /* xgettext:c-format */
3285 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3286 abfd, asect->alignment_power, asect);
3287 arg->failed = true;
3288 return;
3289 }
3290 /* Set sh_addralign to the highest power of two given by alignment
3291 consistent with the section VMA. Linker scripts can force VMA. */
3292 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3293 this_hdr->sh_addralign = mask & -mask;
3294 /* The sh_entsize and sh_info fields may have been set already by
3295 copy_private_section_data. */
3296
3297 this_hdr->bfd_section = asect;
3298 this_hdr->contents = NULL;
3299
3300 /* If the section type is unspecified, we set it based on
3301 asect->flags. */
3302 if (asect->type != 0)
3303 sh_type = asect->type;
3304 else if ((asect->flags & SEC_GROUP) != 0)
3305 sh_type = SHT_GROUP;
3306 else
3307 sh_type = bfd_elf_get_default_section_type (asect->flags);
3308
3309 if (this_hdr->sh_type == SHT_NULL)
3310 this_hdr->sh_type = sh_type;
3311 else if (this_hdr->sh_type == SHT_NOBITS
3312 && sh_type == SHT_PROGBITS
3313 && (asect->flags & SEC_ALLOC) != 0)
3314 {
3315 /* Warn if we are changing a NOBITS section to PROGBITS, but
3316 allow the link to proceed. This can happen when users link
3317 non-bss input sections to bss output sections, or emit data
3318 to a bss output section via a linker script. */
3319 _bfd_error_handler
3320 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3321 this_hdr->sh_type = sh_type;
3322 }
3323
3324 switch (this_hdr->sh_type)
3325 {
3326 default:
3327 break;
3328
3329 case SHT_STRTAB:
3330 case SHT_NOTE:
3331 case SHT_NOBITS:
3332 case SHT_PROGBITS:
3333 break;
3334
3335 case SHT_INIT_ARRAY:
3336 case SHT_FINI_ARRAY:
3337 case SHT_PREINIT_ARRAY:
3338 this_hdr->sh_entsize = bed->s->arch_size / 8;
3339 break;
3340
3341 case SHT_HASH:
3342 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3343 break;
3344
3345 case SHT_DYNSYM:
3346 this_hdr->sh_entsize = bed->s->sizeof_sym;
3347 break;
3348
3349 case SHT_DYNAMIC:
3350 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3351 break;
3352
3353 case SHT_RELA:
3354 if (get_elf_backend_data (abfd)->may_use_rela_p)
3355 this_hdr->sh_entsize = bed->s->sizeof_rela;
3356 break;
3357
3358 case SHT_REL:
3359 if (get_elf_backend_data (abfd)->may_use_rel_p)
3360 this_hdr->sh_entsize = bed->s->sizeof_rel;
3361 break;
3362
3363 case SHT_GNU_versym:
3364 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3365 break;
3366
3367 case SHT_GNU_verdef:
3368 this_hdr->sh_entsize = 0;
3369 /* objcopy or strip will copy over sh_info, but may not set
3370 cverdefs. The linker will set cverdefs, but sh_info will be
3371 zero. */
3372 if (this_hdr->sh_info == 0)
3373 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3374 else
3375 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3376 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3377 break;
3378
3379 case SHT_GNU_verneed:
3380 this_hdr->sh_entsize = 0;
3381 /* objcopy or strip will copy over sh_info, but may not set
3382 cverrefs. The linker will set cverrefs, but sh_info will be
3383 zero. */
3384 if (this_hdr->sh_info == 0)
3385 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3386 else
3387 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3388 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3389 break;
3390
3391 case SHT_GROUP:
3392 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3393 break;
3394
3395 case SHT_GNU_HASH:
3396 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3397 break;
3398 }
3399
3400 if ((asect->flags & SEC_ALLOC) != 0)
3401 this_hdr->sh_flags |= SHF_ALLOC;
3402 if ((asect->flags & SEC_READONLY) == 0)
3403 this_hdr->sh_flags |= SHF_WRITE;
3404 if ((asect->flags & SEC_CODE) != 0)
3405 this_hdr->sh_flags |= SHF_EXECINSTR;
3406 if ((asect->flags & SEC_MERGE) != 0)
3407 {
3408 this_hdr->sh_flags |= SHF_MERGE;
3409 this_hdr->sh_entsize = asect->entsize;
3410 }
3411 if ((asect->flags & SEC_STRINGS) != 0)
3412 this_hdr->sh_flags |= SHF_STRINGS;
3413 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3414 this_hdr->sh_flags |= SHF_GROUP;
3415 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3416 {
3417 this_hdr->sh_flags |= SHF_TLS;
3418 if (asect->size == 0
3419 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3420 {
3421 struct bfd_link_order *o = asect->map_tail.link_order;
3422
3423 this_hdr->sh_size = 0;
3424 if (o != NULL)
3425 {
3426 this_hdr->sh_size = o->offset + o->size;
3427 if (this_hdr->sh_size != 0)
3428 this_hdr->sh_type = SHT_NOBITS;
3429 }
3430 }
3431 }
3432 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3433 this_hdr->sh_flags |= SHF_EXCLUDE;
3434
3435 /* If the section has relocs, set up a section header for the
3436 SHT_REL[A] section. If two relocation sections are required for
3437 this section, it is up to the processor-specific back-end to
3438 create the other. */
3439 if ((asect->flags & SEC_RELOC) != 0)
3440 {
3441 /* When doing a relocatable link, create both REL and RELA sections if
3442 needed. */
3443 if (arg->link_info
3444 /* Do the normal setup if we wouldn't create any sections here. */
3445 && esd->rel.count + esd->rela.count > 0
3446 && (bfd_link_relocatable (arg->link_info)
3447 || arg->link_info->emitrelocations))
3448 {
3449 if (esd->rel.count && esd->rel.hdr == NULL
3450 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3451 false, delay_st_name_p))
3452 {
3453 arg->failed = true;
3454 return;
3455 }
3456 if (esd->rela.count && esd->rela.hdr == NULL
3457 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3458 true, delay_st_name_p))
3459 {
3460 arg->failed = true;
3461 return;
3462 }
3463 }
3464 else if (!_bfd_elf_init_reloc_shdr (abfd,
3465 (asect->use_rela_p
3466 ? &esd->rela : &esd->rel),
3467 name,
3468 asect->use_rela_p,
3469 delay_st_name_p))
3470 {
3471 arg->failed = true;
3472 return;
3473 }
3474 }
3475
3476 /* Check for processor-specific section types. */
3477 sh_type = this_hdr->sh_type;
3478 if (bed->elf_backend_fake_sections
3479 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3480 {
3481 arg->failed = true;
3482 return;
3483 }
3484
3485 if (sh_type == SHT_NOBITS && asect->size != 0)
3486 {
3487 /* Don't change the header type from NOBITS if we are being
3488 called for objcopy --only-keep-debug. */
3489 this_hdr->sh_type = sh_type;
3490 }
3491 }
3492
3493 /* Fill in the contents of a SHT_GROUP section. Called from
3494 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3495 when ELF targets use the generic linker, ld. Called for ld -r
3496 from bfd_elf_final_link. */
3497
3498 void
3499 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3500 {
3501 bool *failedptr = (bool *) failedptrarg;
3502 asection *elt, *first;
3503 unsigned char *loc;
3504 bool gas;
3505
3506 /* Ignore linker created group section. See elfNN_ia64_object_p in
3507 elfxx-ia64.c. */
3508 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3509 || sec->size == 0
3510 || *failedptr)
3511 return;
3512
3513 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3514 {
3515 unsigned long symindx = 0;
3516
3517 /* elf_group_id will have been set up by objcopy and the
3518 generic linker. */
3519 if (elf_group_id (sec) != NULL)
3520 symindx = elf_group_id (sec)->udata.i;
3521
3522 if (symindx == 0)
3523 {
3524 /* If called from the assembler, swap_out_syms will have set up
3525 elf_section_syms.
3526 PR 25699: A corrupt input file could contain bogus group info. */
3527 if (sec->index >= elf_num_section_syms (abfd)
3528 || elf_section_syms (abfd)[sec->index] == NULL)
3529 {
3530 *failedptr = true;
3531 return;
3532 }
3533 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3534 }
3535 elf_section_data (sec)->this_hdr.sh_info = symindx;
3536 }
3537 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3538 {
3539 /* The ELF backend linker sets sh_info to -2 when the group
3540 signature symbol is global, and thus the index can't be
3541 set until all local symbols are output. */
3542 asection *igroup;
3543 struct bfd_elf_section_data *sec_data;
3544 unsigned long symndx;
3545 unsigned long extsymoff;
3546 struct elf_link_hash_entry *h;
3547
3548 /* The point of this little dance to the first SHF_GROUP section
3549 then back to the SHT_GROUP section is that this gets us to
3550 the SHT_GROUP in the input object. */
3551 igroup = elf_sec_group (elf_next_in_group (sec));
3552 sec_data = elf_section_data (igroup);
3553 symndx = sec_data->this_hdr.sh_info;
3554 extsymoff = 0;
3555 if (!elf_bad_symtab (igroup->owner))
3556 {
3557 Elf_Internal_Shdr *symtab_hdr;
3558
3559 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3560 extsymoff = symtab_hdr->sh_info;
3561 }
3562 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3563 while (h->root.type == bfd_link_hash_indirect
3564 || h->root.type == bfd_link_hash_warning)
3565 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3566
3567 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3568 }
3569
3570 /* The contents won't be allocated for "ld -r" or objcopy. */
3571 gas = true;
3572 if (sec->contents == NULL)
3573 {
3574 gas = false;
3575 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3576
3577 /* Arrange for the section to be written out. */
3578 elf_section_data (sec)->this_hdr.contents = sec->contents;
3579 if (sec->contents == NULL)
3580 {
3581 *failedptr = true;
3582 return;
3583 }
3584 }
3585
3586 loc = sec->contents + sec->size;
3587
3588 /* Get the pointer to the first section in the group that gas
3589 squirreled away here. objcopy arranges for this to be set to the
3590 start of the input section group. */
3591 first = elt = elf_next_in_group (sec);
3592
3593 /* First element is a flag word. Rest of section is elf section
3594 indices for all the sections of the group. Write them backwards
3595 just to keep the group in the same order as given in .section
3596 directives, not that it matters. */
3597 while (elt != NULL)
3598 {
3599 asection *s;
3600
3601 s = elt;
3602 if (!gas)
3603 s = s->output_section;
3604 if (s != NULL
3605 && !bfd_is_abs_section (s))
3606 {
3607 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3608 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3609
3610 if (elf_sec->rel.hdr != NULL
3611 && (gas
3612 || (input_elf_sec->rel.hdr != NULL
3613 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3614 {
3615 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3616 loc -= 4;
3617 if (loc == sec->contents)
3618 break;
3619 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3620 }
3621 if (elf_sec->rela.hdr != NULL
3622 && (gas
3623 || (input_elf_sec->rela.hdr != NULL
3624 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3625 {
3626 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3627 loc -= 4;
3628 if (loc == sec->contents)
3629 break;
3630 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3631 }
3632 loc -= 4;
3633 if (loc == sec->contents)
3634 break;
3635 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3636 }
3637 elt = elf_next_in_group (elt);
3638 if (elt == first)
3639 break;
3640 }
3641
3642 /* We should always get here with loc == sec->contents + 4, but it is
3643 possible to craft bogus SHT_GROUP sections that will cause segfaults
3644 in objcopy without checking loc here and in the loop above. */
3645 if (loc == sec->contents)
3646 BFD_ASSERT (0);
3647 else
3648 {
3649 loc -= 4;
3650 if (loc != sec->contents)
3651 {
3652 BFD_ASSERT (0);
3653 memset (sec->contents + 4, 0, loc - sec->contents);
3654 loc = sec->contents;
3655 }
3656 }
3657
3658 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3659 }
3660
3661 /* Given NAME, the name of a relocation section stripped of its
3662 .rel/.rela prefix, return the section in ABFD to which the
3663 relocations apply. */
3664
3665 asection *
3666 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3667 {
3668 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3669 section likely apply to .got.plt or .got section. */
3670 if (get_elf_backend_data (abfd)->want_got_plt
3671 && strcmp (name, ".plt") == 0)
3672 {
3673 asection *sec;
3674
3675 name = ".got.plt";
3676 sec = bfd_get_section_by_name (abfd, name);
3677 if (sec != NULL)
3678 return sec;
3679 name = ".got";
3680 }
3681
3682 return bfd_get_section_by_name (abfd, name);
3683 }
3684
3685 /* Return the section to which RELOC_SEC applies. */
3686
3687 static asection *
3688 elf_get_reloc_section (asection *reloc_sec)
3689 {
3690 const char *name;
3691 unsigned int type;
3692 bfd *abfd;
3693 const struct elf_backend_data *bed;
3694
3695 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3696 if (type != SHT_REL && type != SHT_RELA)
3697 return NULL;
3698
3699 /* We look up the section the relocs apply to by name. */
3700 name = reloc_sec->name;
3701 if (!startswith (name, ".rel"))
3702 return NULL;
3703 name += 4;
3704 if (type == SHT_RELA && *name++ != 'a')
3705 return NULL;
3706
3707 abfd = reloc_sec->owner;
3708 bed = get_elf_backend_data (abfd);
3709 return bed->get_reloc_section (abfd, name);
3710 }
3711
3712 /* Assign all ELF section numbers. The dummy first section is handled here
3713 too. The link/info pointers for the standard section types are filled
3714 in here too, while we're at it. LINK_INFO will be 0 when arriving
3715 here for gas, objcopy, and when using the generic ELF linker. */
3716
3717 static bool
3718 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3719 {
3720 struct elf_obj_tdata *t = elf_tdata (abfd);
3721 asection *sec;
3722 unsigned int section_number;
3723 Elf_Internal_Shdr **i_shdrp;
3724 struct bfd_elf_section_data *d;
3725 bool need_symtab;
3726 size_t amt;
3727
3728 section_number = 1;
3729
3730 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3731
3732 /* SHT_GROUP sections are in relocatable files only. */
3733 if (link_info == NULL || !link_info->resolve_section_groups)
3734 {
3735 size_t reloc_count = 0;
3736
3737 /* Put SHT_GROUP sections first. */
3738 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3739 {
3740 d = elf_section_data (sec);
3741
3742 if (d->this_hdr.sh_type == SHT_GROUP)
3743 {
3744 if (sec->flags & SEC_LINKER_CREATED)
3745 {
3746 /* Remove the linker created SHT_GROUP sections. */
3747 bfd_section_list_remove (abfd, sec);
3748 abfd->section_count--;
3749 }
3750 else
3751 d->this_idx = section_number++;
3752 }
3753
3754 /* Count relocations. */
3755 reloc_count += sec->reloc_count;
3756 }
3757
3758 /* Set/clear HAS_RELOC depending on whether there are relocations. */
3759 if (reloc_count == 0)
3760 abfd->flags &= ~HAS_RELOC;
3761 else
3762 abfd->flags |= HAS_RELOC;
3763 }
3764
3765 for (sec = abfd->sections; sec; sec = sec->next)
3766 {
3767 d = elf_section_data (sec);
3768
3769 if (d->this_hdr.sh_type != SHT_GROUP)
3770 d->this_idx = section_number++;
3771 if (d->this_hdr.sh_name != (unsigned int) -1)
3772 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3773 if (d->rel.hdr)
3774 {
3775 d->rel.idx = section_number++;
3776 if (d->rel.hdr->sh_name != (unsigned int) -1)
3777 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3778 }
3779 else
3780 d->rel.idx = 0;
3781
3782 if (d->rela.hdr)
3783 {
3784 d->rela.idx = section_number++;
3785 if (d->rela.hdr->sh_name != (unsigned int) -1)
3786 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3787 }
3788 else
3789 d->rela.idx = 0;
3790 }
3791
3792 need_symtab = (bfd_get_symcount (abfd) > 0
3793 || (link_info == NULL
3794 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3795 == HAS_RELOC)));
3796 if (need_symtab)
3797 {
3798 elf_onesymtab (abfd) = section_number++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3800 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3801 {
3802 elf_section_list *entry;
3803
3804 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3805
3806 entry = bfd_zalloc (abfd, sizeof (*entry));
3807 entry->ndx = section_number++;
3808 elf_symtab_shndx_list (abfd) = entry;
3809 entry->hdr.sh_name
3810 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3811 ".symtab_shndx", false);
3812 if (entry->hdr.sh_name == (unsigned int) -1)
3813 return false;
3814 }
3815 elf_strtab_sec (abfd) = section_number++;
3816 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3817 }
3818
3819 elf_shstrtab_sec (abfd) = section_number++;
3820 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3821 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3822
3823 if (section_number >= SHN_LORESERVE)
3824 {
3825 /* xgettext:c-format */
3826 _bfd_error_handler (_("%pB: too many sections: %u"),
3827 abfd, section_number);
3828 return false;
3829 }
3830
3831 elf_numsections (abfd) = section_number;
3832 elf_elfheader (abfd)->e_shnum = section_number;
3833
3834 /* Set up the list of section header pointers, in agreement with the
3835 indices. */
3836 amt = section_number * sizeof (Elf_Internal_Shdr *);
3837 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3838 if (i_shdrp == NULL)
3839 return false;
3840
3841 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3842 sizeof (Elf_Internal_Shdr));
3843 if (i_shdrp[0] == NULL)
3844 {
3845 bfd_release (abfd, i_shdrp);
3846 return false;
3847 }
3848
3849 elf_elfsections (abfd) = i_shdrp;
3850
3851 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3852 if (need_symtab)
3853 {
3854 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3855 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3856 {
3857 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3858 BFD_ASSERT (entry != NULL);
3859 i_shdrp[entry->ndx] = & entry->hdr;
3860 entry->hdr.sh_link = elf_onesymtab (abfd);
3861 }
3862 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3863 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3864 }
3865
3866 for (sec = abfd->sections; sec; sec = sec->next)
3867 {
3868 asection *s;
3869
3870 d = elf_section_data (sec);
3871
3872 i_shdrp[d->this_idx] = &d->this_hdr;
3873 if (d->rel.idx != 0)
3874 i_shdrp[d->rel.idx] = d->rel.hdr;
3875 if (d->rela.idx != 0)
3876 i_shdrp[d->rela.idx] = d->rela.hdr;
3877
3878 /* Fill in the sh_link and sh_info fields while we're at it. */
3879
3880 /* sh_link of a reloc section is the section index of the symbol
3881 table. sh_info is the section index of the section to which
3882 the relocation entries apply. */
3883 if (d->rel.idx != 0)
3884 {
3885 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3886 d->rel.hdr->sh_info = d->this_idx;
3887 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3888 }
3889 if (d->rela.idx != 0)
3890 {
3891 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3892 d->rela.hdr->sh_info = d->this_idx;
3893 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3894 }
3895
3896 /* We need to set up sh_link for SHF_LINK_ORDER. */
3897 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3898 {
3899 s = elf_linked_to_section (sec);
3900 /* We can now have a NULL linked section pointer.
3901 This happens when the sh_link field is 0, which is done
3902 when a linked to section is discarded but the linking
3903 section has been retained for some reason. */
3904 if (s)
3905 {
3906 /* Check discarded linkonce section. */
3907 if (discarded_section (s))
3908 {
3909 asection *kept;
3910 _bfd_error_handler
3911 /* xgettext:c-format */
3912 (_("%pB: sh_link of section `%pA' points to"
3913 " discarded section `%pA' of `%pB'"),
3914 abfd, d->this_hdr.bfd_section, s, s->owner);
3915 /* Point to the kept section if it has the same
3916 size as the discarded one. */
3917 kept = _bfd_elf_check_kept_section (s, link_info);
3918 if (kept == NULL)
3919 {
3920 bfd_set_error (bfd_error_bad_value);
3921 return false;
3922 }
3923 s = kept;
3924 }
3925 /* Handle objcopy. */
3926 else if (s->output_section == NULL)
3927 {
3928 _bfd_error_handler
3929 /* xgettext:c-format */
3930 (_("%pB: sh_link of section `%pA' points to"
3931 " removed section `%pA' of `%pB'"),
3932 abfd, d->this_hdr.bfd_section, s, s->owner);
3933 bfd_set_error (bfd_error_bad_value);
3934 return false;
3935 }
3936 s = s->output_section;
3937 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3938 }
3939 }
3940
3941 switch (d->this_hdr.sh_type)
3942 {
3943 case SHT_REL:
3944 case SHT_RELA:
3945 /* A reloc section which we are treating as a normal BFD
3946 section. sh_link is the section index of the symbol
3947 table. sh_info is the section index of the section to
3948 which the relocation entries apply. We assume that an
3949 allocated reloc section uses the dynamic symbol table
3950 if there is one. Otherwise we guess the normal symbol
3951 table. FIXME: How can we be sure? */
3952 if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
3953 {
3954 s = bfd_get_section_by_name (abfd, ".dynsym");
3955 if (s != NULL)
3956 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3957 }
3958 if (d->this_hdr.sh_link == 0)
3959 d->this_hdr.sh_link = elf_onesymtab (abfd);
3960
3961 s = elf_get_reloc_section (sec);
3962 if (s != NULL)
3963 {
3964 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3965 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3966 }
3967 break;
3968
3969 case SHT_STRTAB:
3970 /* We assume that a section named .stab*str is a stabs
3971 string section. We look for a section with the same name
3972 but without the trailing ``str'', and set its sh_link
3973 field to point to this section. */
3974 if (startswith (sec->name, ".stab")
3975 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3976 {
3977 size_t len;
3978 char *alc;
3979
3980 len = strlen (sec->name);
3981 alc = (char *) bfd_malloc (len - 2);
3982 if (alc == NULL)
3983 return false;
3984 memcpy (alc, sec->name, len - 3);
3985 alc[len - 3] = '\0';
3986 s = bfd_get_section_by_name (abfd, alc);
3987 free (alc);
3988 if (s != NULL)
3989 {
3990 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3991
3992 /* This is a .stab section. */
3993 elf_section_data (s)->this_hdr.sh_entsize = 12;
3994 }
3995 }
3996 break;
3997
3998 case SHT_DYNAMIC:
3999 case SHT_DYNSYM:
4000 case SHT_GNU_verneed:
4001 case SHT_GNU_verdef:
4002 /* sh_link is the section header index of the string table
4003 used for the dynamic entries, or the symbol table, or the
4004 version strings. */
4005 s = bfd_get_section_by_name (abfd, ".dynstr");
4006 if (s != NULL)
4007 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4008 break;
4009
4010 case SHT_GNU_LIBLIST:
4011 /* sh_link is the section header index of the prelink library
4012 list used for the dynamic entries, or the symbol table, or
4013 the version strings. */
4014 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4015 ? ".dynstr" : ".gnu.libstr"));
4016 if (s != NULL)
4017 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4018 break;
4019
4020 case SHT_HASH:
4021 case SHT_GNU_HASH:
4022 case SHT_GNU_versym:
4023 /* sh_link is the section header index of the symbol table
4024 this hash table or version table is for. */
4025 s = bfd_get_section_by_name (abfd, ".dynsym");
4026 if (s != NULL)
4027 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4028 break;
4029
4030 case SHT_GROUP:
4031 d->this_hdr.sh_link = elf_onesymtab (abfd);
4032 }
4033 }
4034
4035 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4036 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4037 debug section name from .debug_* to .zdebug_* if needed. */
4038
4039 return true;
4040 }
4041
4042 static bool
4043 sym_is_global (bfd *abfd, asymbol *sym)
4044 {
4045 /* If the backend has a special mapping, use it. */
4046 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4047 if (bed->elf_backend_sym_is_global)
4048 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4049
4050 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4051 || bfd_is_und_section (bfd_asymbol_section (sym))
4052 || bfd_is_com_section (bfd_asymbol_section (sym)));
4053 }
4054
4055 /* Filter global symbols of ABFD to include in the import library. All
4056 SYMCOUNT symbols of ABFD can be examined from their pointers in
4057 SYMS. Pointers of symbols to keep should be stored contiguously at
4058 the beginning of that array.
4059
4060 Returns the number of symbols to keep. */
4061
4062 unsigned int
4063 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4064 asymbol **syms, long symcount)
4065 {
4066 long src_count, dst_count = 0;
4067
4068 for (src_count = 0; src_count < symcount; src_count++)
4069 {
4070 asymbol *sym = syms[src_count];
4071 char *name = (char *) bfd_asymbol_name (sym);
4072 struct bfd_link_hash_entry *h;
4073
4074 if (!sym_is_global (abfd, sym))
4075 continue;
4076
4077 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4078 if (h == NULL)
4079 continue;
4080 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4081 continue;
4082 if (h->linker_def || h->ldscript_def)
4083 continue;
4084
4085 syms[dst_count++] = sym;
4086 }
4087
4088 syms[dst_count] = NULL;
4089
4090 return dst_count;
4091 }
4092
4093 /* Don't output section symbols for sections that are not going to be
4094 output, that are duplicates or there is no BFD section. */
4095
4096 static bool
4097 ignore_section_sym (bfd *abfd, asymbol *sym)
4098 {
4099 elf_symbol_type *type_ptr;
4100
4101 if (sym == NULL)
4102 return false;
4103
4104 if ((sym->flags & BSF_SECTION_SYM) == 0)
4105 return false;
4106
4107 /* Ignore the section symbol if it isn't used. */
4108 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4109 return true;
4110
4111 if (sym->section == NULL)
4112 return true;
4113
4114 type_ptr = elf_symbol_from (sym);
4115 return ((type_ptr != NULL
4116 && type_ptr->internal_elf_sym.st_shndx != 0
4117 && bfd_is_abs_section (sym->section))
4118 || !(sym->section->owner == abfd
4119 || (sym->section->output_section != NULL
4120 && sym->section->output_section->owner == abfd
4121 && sym->section->output_offset == 0)
4122 || bfd_is_abs_section (sym->section)));
4123 }
4124
4125 /* Map symbol from it's internal number to the external number, moving
4126 all local symbols to be at the head of the list. */
4127
4128 static bool
4129 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4130 {
4131 unsigned int symcount = bfd_get_symcount (abfd);
4132 asymbol **syms = bfd_get_outsymbols (abfd);
4133 asymbol **sect_syms;
4134 unsigned int num_locals = 0;
4135 unsigned int num_globals = 0;
4136 unsigned int num_locals2 = 0;
4137 unsigned int num_globals2 = 0;
4138 unsigned int max_index = 0;
4139 unsigned int idx;
4140 asection *asect;
4141 asymbol **new_syms;
4142 size_t amt;
4143
4144 #ifdef DEBUG
4145 fprintf (stderr, "elf_map_symbols\n");
4146 fflush (stderr);
4147 #endif
4148
4149 for (asect = abfd->sections; asect; asect = asect->next)
4150 {
4151 if (max_index < asect->index)
4152 max_index = asect->index;
4153 }
4154
4155 max_index++;
4156 amt = max_index * sizeof (asymbol *);
4157 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4158 if (sect_syms == NULL)
4159 return false;
4160 elf_section_syms (abfd) = sect_syms;
4161 elf_num_section_syms (abfd) = max_index;
4162
4163 /* Init sect_syms entries for any section symbols we have already
4164 decided to output. */
4165 for (idx = 0; idx < symcount; idx++)
4166 {
4167 asymbol *sym = syms[idx];
4168
4169 if ((sym->flags & BSF_SECTION_SYM) != 0
4170 && sym->value == 0
4171 && !ignore_section_sym (abfd, sym)
4172 && !bfd_is_abs_section (sym->section))
4173 {
4174 asection *sec = sym->section;
4175
4176 if (sec->owner != abfd)
4177 sec = sec->output_section;
4178
4179 sect_syms[sec->index] = syms[idx];
4180 }
4181 }
4182
4183 /* Classify all of the symbols. */
4184 for (idx = 0; idx < symcount; idx++)
4185 {
4186 if (sym_is_global (abfd, syms[idx]))
4187 num_globals++;
4188 else if (!ignore_section_sym (abfd, syms[idx]))
4189 num_locals++;
4190 }
4191
4192 /* We will be adding a section symbol for each normal BFD section. Most
4193 sections will already have a section symbol in outsymbols, but
4194 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4195 at least in that case. */
4196 for (asect = abfd->sections; asect; asect = asect->next)
4197 {
4198 asymbol *sym = asect->symbol;
4199 /* Don't include ignored section symbols. */
4200 if (!ignore_section_sym (abfd, sym)
4201 && sect_syms[asect->index] == NULL)
4202 {
4203 if (!sym_is_global (abfd, asect->symbol))
4204 num_locals++;
4205 else
4206 num_globals++;
4207 }
4208 }
4209
4210 /* Now sort the symbols so the local symbols are first. */
4211 amt = (num_locals + num_globals) * sizeof (asymbol *);
4212 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4213 if (new_syms == NULL)
4214 return false;
4215
4216 for (idx = 0; idx < symcount; idx++)
4217 {
4218 asymbol *sym = syms[idx];
4219 unsigned int i;
4220
4221 if (sym_is_global (abfd, sym))
4222 i = num_locals + num_globals2++;
4223 /* Don't include ignored section symbols. */
4224 else if (!ignore_section_sym (abfd, sym))
4225 i = num_locals2++;
4226 else
4227 continue;
4228 new_syms[i] = sym;
4229 sym->udata.i = i + 1;
4230 }
4231 for (asect = abfd->sections; asect; asect = asect->next)
4232 {
4233 asymbol *sym = asect->symbol;
4234 if (!ignore_section_sym (abfd, sym)
4235 && sect_syms[asect->index] == NULL)
4236 {
4237 unsigned int i;
4238
4239 sect_syms[asect->index] = sym;
4240 if (!sym_is_global (abfd, sym))
4241 i = num_locals2++;
4242 else
4243 i = num_locals + num_globals2++;
4244 new_syms[i] = sym;
4245 sym->udata.i = i + 1;
4246 }
4247 }
4248
4249 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4250
4251 *pnum_locals = num_locals;
4252 return true;
4253 }
4254
4255 /* Align to the maximum file alignment that could be required for any
4256 ELF data structure. */
4257
4258 static inline file_ptr
4259 align_file_position (file_ptr off, int align)
4260 {
4261 return (off + align - 1) & ~(align - 1);
4262 }
4263
4264 /* Assign a file position to a section, optionally aligning to the
4265 required section alignment. */
4266
4267 file_ptr
4268 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4269 file_ptr offset,
4270 bool align)
4271 {
4272 if (align && i_shdrp->sh_addralign > 1)
4273 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4274 i_shdrp->sh_offset = offset;
4275 if (i_shdrp->bfd_section != NULL)
4276 i_shdrp->bfd_section->filepos = offset;
4277 if (i_shdrp->sh_type != SHT_NOBITS)
4278 offset += i_shdrp->sh_size;
4279 return offset;
4280 }
4281
4282 /* Compute the file positions we are going to put the sections at, and
4283 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4284 is not NULL, this is being called by the ELF backend linker. */
4285
4286 bool
4287 _bfd_elf_compute_section_file_positions (bfd *abfd,
4288 struct bfd_link_info *link_info)
4289 {
4290 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4291 struct fake_section_arg fsargs;
4292 bool failed;
4293 struct elf_strtab_hash *strtab = NULL;
4294 Elf_Internal_Shdr *shstrtab_hdr;
4295 bool need_symtab;
4296
4297 if (abfd->output_has_begun)
4298 return true;
4299
4300 /* Do any elf backend specific processing first. */
4301 if (bed->elf_backend_begin_write_processing)
4302 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4303
4304 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4305 return false;
4306
4307 fsargs.failed = false;
4308 fsargs.link_info = link_info;
4309 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4310 if (fsargs.failed)
4311 return false;
4312
4313 if (!assign_section_numbers (abfd, link_info))
4314 return false;
4315
4316 /* The backend linker builds symbol table information itself. */
4317 need_symtab = (link_info == NULL
4318 && (bfd_get_symcount (abfd) > 0
4319 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4320 == HAS_RELOC)));
4321 if (need_symtab)
4322 {
4323 /* Non-zero if doing a relocatable link. */
4324 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4325
4326 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4327 return false;
4328 }
4329
4330 failed = false;
4331 if (link_info == NULL)
4332 {
4333 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4334 if (failed)
4335 return false;
4336 }
4337
4338 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4339 /* sh_name was set in init_file_header. */
4340 shstrtab_hdr->sh_type = SHT_STRTAB;
4341 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4342 shstrtab_hdr->sh_addr = 0;
4343 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4344 shstrtab_hdr->sh_entsize = 0;
4345 shstrtab_hdr->sh_link = 0;
4346 shstrtab_hdr->sh_info = 0;
4347 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4348 shstrtab_hdr->sh_addralign = 1;
4349
4350 if (!assign_file_positions_except_relocs (abfd, link_info))
4351 return false;
4352
4353 if (need_symtab)
4354 {
4355 file_ptr off;
4356 Elf_Internal_Shdr *hdr;
4357
4358 off = elf_next_file_pos (abfd);
4359
4360 hdr = & elf_symtab_hdr (abfd);
4361 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4362
4363 if (elf_symtab_shndx_list (abfd) != NULL)
4364 {
4365 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4366 if (hdr->sh_size != 0)
4367 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4368 /* FIXME: What about other symtab_shndx sections in the list ? */
4369 }
4370
4371 hdr = &elf_tdata (abfd)->strtab_hdr;
4372 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4373
4374 elf_next_file_pos (abfd) = off;
4375
4376 /* Now that we know where the .strtab section goes, write it
4377 out. */
4378 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4379 || ! _bfd_elf_strtab_emit (abfd, strtab))
4380 return false;
4381 _bfd_elf_strtab_free (strtab);
4382 }
4383
4384 abfd->output_has_begun = true;
4385
4386 return true;
4387 }
4388
4389 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4390 function effectively returns whether --eh-frame-hdr is given on the
4391 command line. After size_dynamic_sections the result reflects
4392 whether .eh_frame_hdr will actually be output (sizing isn't done
4393 until ldemul_after_allocation). */
4394
4395 static asection *
4396 elf_eh_frame_hdr (const struct bfd_link_info *info)
4397 {
4398 if (info != NULL && is_elf_hash_table (info->hash))
4399 return elf_hash_table (info)->eh_info.hdr_sec;
4400 return NULL;
4401 }
4402
4403 /* Make an initial estimate of the size of the program header. If we
4404 get the number wrong here, we'll redo section placement. */
4405
4406 static bfd_size_type
4407 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4408 {
4409 size_t segs;
4410 asection *s;
4411 const struct elf_backend_data *bed;
4412
4413 /* Assume we will need exactly two PT_LOAD segments: one for text
4414 and one for data. */
4415 segs = 2;
4416
4417 s = bfd_get_section_by_name (abfd, ".interp");
4418 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4419 {
4420 /* If we have a loadable interpreter section, we need a
4421 PT_INTERP segment. In this case, assume we also need a
4422 PT_PHDR segment, although that may not be true for all
4423 targets. */
4424 segs += 2;
4425 }
4426
4427 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4428 {
4429 /* We need a PT_DYNAMIC segment. */
4430 ++segs;
4431 }
4432
4433 if (info != NULL && info->relro)
4434 {
4435 /* We need a PT_GNU_RELRO segment. */
4436 ++segs;
4437 }
4438
4439 if (elf_eh_frame_hdr (info))
4440 {
4441 /* We need a PT_GNU_EH_FRAME segment. */
4442 ++segs;
4443 }
4444
4445 if (elf_stack_flags (abfd))
4446 {
4447 /* We need a PT_GNU_STACK segment. */
4448 ++segs;
4449 }
4450
4451 s = bfd_get_section_by_name (abfd,
4452 NOTE_GNU_PROPERTY_SECTION_NAME);
4453 if (s != NULL && s->size != 0)
4454 {
4455 /* We need a PT_GNU_PROPERTY segment. */
4456 ++segs;
4457 }
4458
4459 for (s = abfd->sections; s != NULL; s = s->next)
4460 {
4461 if ((s->flags & SEC_LOAD) != 0
4462 && elf_section_type (s) == SHT_NOTE)
4463 {
4464 unsigned int alignment_power;
4465 /* We need a PT_NOTE segment. */
4466 ++segs;
4467 /* Try to create just one PT_NOTE segment for all adjacent
4468 loadable SHT_NOTE sections. gABI requires that within a
4469 PT_NOTE segment (and also inside of each SHT_NOTE section)
4470 each note should have the same alignment. So we check
4471 whether the sections are correctly aligned. */
4472 alignment_power = s->alignment_power;
4473 while (s->next != NULL
4474 && s->next->alignment_power == alignment_power
4475 && (s->next->flags & SEC_LOAD) != 0
4476 && elf_section_type (s->next) == SHT_NOTE)
4477 s = s->next;
4478 }
4479 }
4480
4481 for (s = abfd->sections; s != NULL; s = s->next)
4482 {
4483 if (s->flags & SEC_THREAD_LOCAL)
4484 {
4485 /* We need a PT_TLS segment. */
4486 ++segs;
4487 break;
4488 }
4489 }
4490
4491 bed = get_elf_backend_data (abfd);
4492
4493 if ((abfd->flags & D_PAGED) != 0
4494 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4495 {
4496 /* Add a PT_GNU_MBIND segment for each mbind section. */
4497 bfd_vma commonpagesize;
4498 unsigned int page_align_power;
4499
4500 if (info != NULL)
4501 commonpagesize = info->commonpagesize;
4502 else
4503 commonpagesize = bed->commonpagesize;
4504 page_align_power = bfd_log2 (commonpagesize);
4505 for (s = abfd->sections; s != NULL; s = s->next)
4506 if (elf_section_flags (s) & SHF_GNU_MBIND)
4507 {
4508 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4509 {
4510 _bfd_error_handler
4511 /* xgettext:c-format */
4512 (_("%pB: GNU_MBIND section `%pA' has invalid "
4513 "sh_info field: %d"),
4514 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4515 continue;
4516 }
4517 /* Align mbind section to page size. */
4518 if (s->alignment_power < page_align_power)
4519 s->alignment_power = page_align_power;
4520 segs ++;
4521 }
4522 }
4523
4524 /* Let the backend count up any program headers it might need. */
4525 if (bed->elf_backend_additional_program_headers)
4526 {
4527 int a;
4528
4529 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4530 if (a == -1)
4531 abort ();
4532 segs += a;
4533 }
4534
4535 return segs * bed->s->sizeof_phdr;
4536 }
4537
4538 /* Find the segment that contains the output_section of section. */
4539
4540 Elf_Internal_Phdr *
4541 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4542 {
4543 struct elf_segment_map *m;
4544 Elf_Internal_Phdr *p;
4545
4546 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4547 m != NULL;
4548 m = m->next, p++)
4549 {
4550 int i;
4551
4552 for (i = m->count - 1; i >= 0; i--)
4553 if (m->sections[i] == section)
4554 return p;
4555 }
4556
4557 return NULL;
4558 }
4559
4560 /* Create a mapping from a set of sections to a program segment. */
4561
4562 static struct elf_segment_map *
4563 make_mapping (bfd *abfd,
4564 asection **sections,
4565 unsigned int from,
4566 unsigned int to,
4567 bool phdr)
4568 {
4569 struct elf_segment_map *m;
4570 unsigned int i;
4571 asection **hdrpp;
4572 size_t amt;
4573
4574 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4575 amt += (to - from) * sizeof (asection *);
4576 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4577 if (m == NULL)
4578 return NULL;
4579 m->next = NULL;
4580 m->p_type = PT_LOAD;
4581 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4582 m->sections[i - from] = *hdrpp;
4583 m->count = to - from;
4584
4585 if (from == 0 && phdr)
4586 {
4587 /* Include the headers in the first PT_LOAD segment. */
4588 m->includes_filehdr = 1;
4589 m->includes_phdrs = 1;
4590 }
4591
4592 return m;
4593 }
4594
4595 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4596 on failure. */
4597
4598 struct elf_segment_map *
4599 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4600 {
4601 struct elf_segment_map *m;
4602
4603 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4604 sizeof (struct elf_segment_map));
4605 if (m == NULL)
4606 return NULL;
4607 m->next = NULL;
4608 m->p_type = PT_DYNAMIC;
4609 m->count = 1;
4610 m->sections[0] = dynsec;
4611
4612 return m;
4613 }
4614
4615 /* Possibly add or remove segments from the segment map. */
4616
4617 static bool
4618 elf_modify_segment_map (bfd *abfd,
4619 struct bfd_link_info *info,
4620 bool remove_empty_load)
4621 {
4622 struct elf_segment_map **m;
4623 const struct elf_backend_data *bed;
4624
4625 /* The placement algorithm assumes that non allocated sections are
4626 not in PT_LOAD segments. We ensure this here by removing such
4627 sections from the segment map. We also remove excluded
4628 sections. Finally, any PT_LOAD segment without sections is
4629 removed. */
4630 m = &elf_seg_map (abfd);
4631 while (*m)
4632 {
4633 unsigned int i, new_count;
4634
4635 for (new_count = 0, i = 0; i < (*m)->count; i++)
4636 {
4637 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4638 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4639 || (*m)->p_type != PT_LOAD))
4640 {
4641 (*m)->sections[new_count] = (*m)->sections[i];
4642 new_count++;
4643 }
4644 }
4645 (*m)->count = new_count;
4646
4647 if (remove_empty_load
4648 && (*m)->p_type == PT_LOAD
4649 && (*m)->count == 0
4650 && !(*m)->includes_phdrs)
4651 *m = (*m)->next;
4652 else
4653 m = &(*m)->next;
4654 }
4655
4656 bed = get_elf_backend_data (abfd);
4657 if (bed->elf_backend_modify_segment_map != NULL)
4658 {
4659 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4660 return false;
4661 }
4662
4663 return true;
4664 }
4665
4666 #define IS_TBSS(s) \
4667 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4668
4669 /* Set up a mapping from BFD sections to program segments. Update
4670 NEED_LAYOUT if the section layout is changed. */
4671
4672 bool
4673 _bfd_elf_map_sections_to_segments (bfd *abfd,
4674 struct bfd_link_info *info,
4675 bool *need_layout)
4676 {
4677 unsigned int count;
4678 struct elf_segment_map *m;
4679 asection **sections = NULL;
4680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4681 bool no_user_phdrs;
4682
4683 no_user_phdrs = elf_seg_map (abfd) == NULL;
4684
4685 if (info != NULL)
4686 {
4687 info->user_phdrs = !no_user_phdrs;
4688
4689 /* Size the relative relocations if DT_RELR is enabled. */
4690 if (info->enable_dt_relr
4691 && need_layout != NULL
4692 && bed->size_relative_relocs
4693 && !bed->size_relative_relocs (info, need_layout))
4694 info->callbacks->einfo
4695 (_("%F%P: failed to size relative relocations\n"));
4696 }
4697
4698 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4699 {
4700 asection *s;
4701 unsigned int i;
4702 struct elf_segment_map *mfirst;
4703 struct elf_segment_map **pm;
4704 asection *last_hdr;
4705 bfd_vma last_size;
4706 unsigned int hdr_index;
4707 bfd_vma maxpagesize;
4708 asection **hdrpp;
4709 bool phdr_in_segment;
4710 bool writable;
4711 bool executable;
4712 unsigned int tls_count = 0;
4713 asection *first_tls = NULL;
4714 asection *first_mbind = NULL;
4715 asection *dynsec, *eh_frame_hdr;
4716 size_t amt;
4717 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4718 bfd_size_type phdr_size; /* Octets/bytes. */
4719 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4720
4721 /* Select the allocated sections, and sort them. */
4722
4723 amt = bfd_count_sections (abfd) * sizeof (asection *);
4724 sections = (asection **) bfd_malloc (amt);
4725 if (sections == NULL)
4726 goto error_return;
4727
4728 /* Calculate top address, avoiding undefined behaviour of shift
4729 left operator when shift count is equal to size of type
4730 being shifted. */
4731 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4732 addr_mask = (addr_mask << 1) + 1;
4733
4734 i = 0;
4735 for (s = abfd->sections; s != NULL; s = s->next)
4736 {
4737 if ((s->flags & SEC_ALLOC) != 0)
4738 {
4739 /* target_index is unused until bfd_elf_final_link
4740 starts output of section symbols. Use it to make
4741 qsort stable. */
4742 s->target_index = i;
4743 sections[i] = s;
4744 ++i;
4745 /* A wrapping section potentially clashes with header. */
4746 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4747 wrap_to = (s->lma + s->size / opb) & addr_mask;
4748 }
4749 }
4750 BFD_ASSERT (i <= bfd_count_sections (abfd));
4751 count = i;
4752
4753 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4754
4755 phdr_size = elf_program_header_size (abfd);
4756 if (phdr_size == (bfd_size_type) -1)
4757 phdr_size = get_program_header_size (abfd, info);
4758 phdr_size += bed->s->sizeof_ehdr;
4759 /* phdr_size is compared to LMA values which are in bytes. */
4760 phdr_size /= opb;
4761 if (info != NULL)
4762 maxpagesize = info->maxpagesize;
4763 else
4764 maxpagesize = bed->maxpagesize;
4765 if (maxpagesize == 0)
4766 maxpagesize = 1;
4767 phdr_in_segment = info != NULL && info->load_phdrs;
4768 if (count != 0
4769 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4770 >= (phdr_size & (maxpagesize - 1))))
4771 /* For compatibility with old scripts that may not be using
4772 SIZEOF_HEADERS, add headers when it looks like space has
4773 been left for them. */
4774 phdr_in_segment = true;
4775
4776 /* Build the mapping. */
4777 mfirst = NULL;
4778 pm = &mfirst;
4779
4780 /* If we have a .interp section, then create a PT_PHDR segment for
4781 the program headers and a PT_INTERP segment for the .interp
4782 section. */
4783 s = bfd_get_section_by_name (abfd, ".interp");
4784 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4785 {
4786 amt = sizeof (struct elf_segment_map);
4787 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4788 if (m == NULL)
4789 goto error_return;
4790 m->next = NULL;
4791 m->p_type = PT_PHDR;
4792 m->p_flags = PF_R;
4793 m->p_flags_valid = 1;
4794 m->includes_phdrs = 1;
4795 phdr_in_segment = true;
4796 *pm = m;
4797 pm = &m->next;
4798
4799 amt = sizeof (struct elf_segment_map);
4800 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4801 if (m == NULL)
4802 goto error_return;
4803 m->next = NULL;
4804 m->p_type = PT_INTERP;
4805 m->count = 1;
4806 m->sections[0] = s;
4807
4808 *pm = m;
4809 pm = &m->next;
4810 }
4811
4812 /* Look through the sections. We put sections in the same program
4813 segment when the start of the second section can be placed within
4814 a few bytes of the end of the first section. */
4815 last_hdr = NULL;
4816 last_size = 0;
4817 hdr_index = 0;
4818 writable = false;
4819 executable = false;
4820 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4821 if (dynsec != NULL
4822 && (dynsec->flags & SEC_LOAD) == 0)
4823 dynsec = NULL;
4824
4825 if ((abfd->flags & D_PAGED) == 0)
4826 phdr_in_segment = false;
4827
4828 /* Deal with -Ttext or something similar such that the first section
4829 is not adjacent to the program headers. This is an
4830 approximation, since at this point we don't know exactly how many
4831 program headers we will need. */
4832 if (phdr_in_segment && count > 0)
4833 {
4834 bfd_vma phdr_lma; /* Bytes. */
4835 bool separate_phdr = false;
4836
4837 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4838 if (info != NULL
4839 && info->separate_code
4840 && (sections[0]->flags & SEC_CODE) != 0)
4841 {
4842 /* If data sections should be separate from code and
4843 thus not executable, and the first section is
4844 executable then put the file and program headers in
4845 their own PT_LOAD. */
4846 separate_phdr = true;
4847 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4848 == (sections[0]->lma & addr_mask & -maxpagesize)))
4849 {
4850 /* The file and program headers are currently on the
4851 same page as the first section. Put them on the
4852 previous page if we can. */
4853 if (phdr_lma >= maxpagesize)
4854 phdr_lma -= maxpagesize;
4855 else
4856 separate_phdr = false;
4857 }
4858 }
4859 if ((sections[0]->lma & addr_mask) < phdr_lma
4860 || (sections[0]->lma & addr_mask) < phdr_size)
4861 /* If file and program headers would be placed at the end
4862 of memory then it's probably better to omit them. */
4863 phdr_in_segment = false;
4864 else if (phdr_lma < wrap_to)
4865 /* If a section wraps around to where we'll be placing
4866 file and program headers, then the headers will be
4867 overwritten. */
4868 phdr_in_segment = false;
4869 else if (separate_phdr)
4870 {
4871 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4872 if (m == NULL)
4873 goto error_return;
4874 m->p_paddr = phdr_lma * opb;
4875 m->p_vaddr_offset
4876 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4877 m->p_paddr_valid = 1;
4878 *pm = m;
4879 pm = &m->next;
4880 phdr_in_segment = false;
4881 }
4882 }
4883
4884 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4885 {
4886 asection *hdr;
4887 bool new_segment;
4888
4889 hdr = *hdrpp;
4890
4891 /* See if this section and the last one will fit in the same
4892 segment. */
4893
4894 if (last_hdr == NULL)
4895 {
4896 /* If we don't have a segment yet, then we don't need a new
4897 one (we build the last one after this loop). */
4898 new_segment = false;
4899 }
4900 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4901 {
4902 /* If this section has a different relation between the
4903 virtual address and the load address, then we need a new
4904 segment. */
4905 new_segment = true;
4906 }
4907 else if (hdr->lma < last_hdr->lma + last_size
4908 || last_hdr->lma + last_size < last_hdr->lma)
4909 {
4910 /* If this section has a load address that makes it overlap
4911 the previous section, then we need a new segment. */
4912 new_segment = true;
4913 }
4914 else if ((abfd->flags & D_PAGED) != 0
4915 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4916 == (hdr->lma & -maxpagesize)))
4917 {
4918 /* If we are demand paged then we can't map two disk
4919 pages onto the same memory page. */
4920 new_segment = false;
4921 }
4922 /* In the next test we have to be careful when last_hdr->lma is close
4923 to the end of the address space. If the aligned address wraps
4924 around to the start of the address space, then there are no more
4925 pages left in memory and it is OK to assume that the current
4926 section can be included in the current segment. */
4927 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4928 + maxpagesize > last_hdr->lma)
4929 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4930 + maxpagesize <= hdr->lma))
4931 {
4932 /* If putting this section in this segment would force us to
4933 skip a page in the segment, then we need a new segment. */
4934 new_segment = true;
4935 }
4936 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4937 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4938 {
4939 /* We don't want to put a loaded section after a
4940 nonloaded (ie. bss style) section in the same segment
4941 as that will force the non-loaded section to be loaded.
4942 Consider .tbss sections as loaded for this purpose. */
4943 new_segment = true;
4944 }
4945 else if ((abfd->flags & D_PAGED) == 0)
4946 {
4947 /* If the file is not demand paged, which means that we
4948 don't require the sections to be correctly aligned in the
4949 file, then there is no other reason for a new segment. */
4950 new_segment = false;
4951 }
4952 else if (info != NULL
4953 && info->separate_code
4954 && executable != ((hdr->flags & SEC_CODE) != 0))
4955 {
4956 new_segment = true;
4957 }
4958 else if (! writable
4959 && (hdr->flags & SEC_READONLY) == 0)
4960 {
4961 /* We don't want to put a writable section in a read only
4962 segment. */
4963 new_segment = true;
4964 }
4965 else
4966 {
4967 /* Otherwise, we can use the same segment. */
4968 new_segment = false;
4969 }
4970
4971 /* Allow interested parties a chance to override our decision. */
4972 if (last_hdr != NULL
4973 && info != NULL
4974 && info->callbacks->override_segment_assignment != NULL)
4975 new_segment
4976 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4977 last_hdr,
4978 new_segment);
4979
4980 if (! new_segment)
4981 {
4982 if ((hdr->flags & SEC_READONLY) == 0)
4983 writable = true;
4984 if ((hdr->flags & SEC_CODE) != 0)
4985 executable = true;
4986 last_hdr = hdr;
4987 /* .tbss sections effectively have zero size. */
4988 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4989 continue;
4990 }
4991
4992 /* We need a new program segment. We must create a new program
4993 header holding all the sections from hdr_index until hdr. */
4994
4995 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4996 if (m == NULL)
4997 goto error_return;
4998
4999 *pm = m;
5000 pm = &m->next;
5001
5002 if ((hdr->flags & SEC_READONLY) == 0)
5003 writable = true;
5004 else
5005 writable = false;
5006
5007 if ((hdr->flags & SEC_CODE) == 0)
5008 executable = false;
5009 else
5010 executable = true;
5011
5012 last_hdr = hdr;
5013 /* .tbss sections effectively have zero size. */
5014 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5015 hdr_index = i;
5016 phdr_in_segment = false;
5017 }
5018
5019 /* Create a final PT_LOAD program segment, but not if it's just
5020 for .tbss. */
5021 if (last_hdr != NULL
5022 && (i - hdr_index != 1
5023 || !IS_TBSS (last_hdr)))
5024 {
5025 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5026 if (m == NULL)
5027 goto error_return;
5028
5029 *pm = m;
5030 pm = &m->next;
5031 }
5032
5033 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5034 if (dynsec != NULL)
5035 {
5036 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5037 if (m == NULL)
5038 goto error_return;
5039 *pm = m;
5040 pm = &m->next;
5041 }
5042
5043 /* For each batch of consecutive loadable SHT_NOTE sections,
5044 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5045 because if we link together nonloadable .note sections and
5046 loadable .note sections, we will generate two .note sections
5047 in the output file. */
5048 for (s = abfd->sections; s != NULL; s = s->next)
5049 {
5050 if ((s->flags & SEC_LOAD) != 0
5051 && elf_section_type (s) == SHT_NOTE)
5052 {
5053 asection *s2;
5054 unsigned int alignment_power = s->alignment_power;
5055
5056 count = 1;
5057 for (s2 = s; s2->next != NULL; s2 = s2->next)
5058 {
5059 if (s2->next->alignment_power == alignment_power
5060 && (s2->next->flags & SEC_LOAD) != 0
5061 && elf_section_type (s2->next) == SHT_NOTE
5062 && align_power (s2->lma + s2->size / opb,
5063 alignment_power)
5064 == s2->next->lma)
5065 count++;
5066 else
5067 break;
5068 }
5069 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5070 amt += count * sizeof (asection *);
5071 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5072 if (m == NULL)
5073 goto error_return;
5074 m->next = NULL;
5075 m->p_type = PT_NOTE;
5076 m->count = count;
5077 while (count > 1)
5078 {
5079 m->sections[m->count - count--] = s;
5080 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5081 s = s->next;
5082 }
5083 m->sections[m->count - 1] = s;
5084 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5085 *pm = m;
5086 pm = &m->next;
5087 }
5088 if (s->flags & SEC_THREAD_LOCAL)
5089 {
5090 if (! tls_count)
5091 first_tls = s;
5092 tls_count++;
5093 }
5094 if (first_mbind == NULL
5095 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5096 first_mbind = s;
5097 }
5098
5099 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5100 if (tls_count > 0)
5101 {
5102 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5103 amt += tls_count * sizeof (asection *);
5104 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5105 if (m == NULL)
5106 goto error_return;
5107 m->next = NULL;
5108 m->p_type = PT_TLS;
5109 m->count = tls_count;
5110 /* Mandated PF_R. */
5111 m->p_flags = PF_R;
5112 m->p_flags_valid = 1;
5113 s = first_tls;
5114 for (i = 0; i < tls_count; ++i)
5115 {
5116 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5117 {
5118 _bfd_error_handler
5119 (_("%pB: TLS sections are not adjacent:"), abfd);
5120 s = first_tls;
5121 i = 0;
5122 while (i < tls_count)
5123 {
5124 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5125 {
5126 _bfd_error_handler (_(" TLS: %pA"), s);
5127 i++;
5128 }
5129 else
5130 _bfd_error_handler (_(" non-TLS: %pA"), s);
5131 s = s->next;
5132 }
5133 bfd_set_error (bfd_error_bad_value);
5134 goto error_return;
5135 }
5136 m->sections[i] = s;
5137 s = s->next;
5138 }
5139
5140 *pm = m;
5141 pm = &m->next;
5142 }
5143
5144 if (first_mbind
5145 && (abfd->flags & D_PAGED) != 0
5146 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5147 for (s = first_mbind; s != NULL; s = s->next)
5148 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5149 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5150 {
5151 /* Mandated PF_R. */
5152 unsigned long p_flags = PF_R;
5153 if ((s->flags & SEC_READONLY) == 0)
5154 p_flags |= PF_W;
5155 if ((s->flags & SEC_CODE) != 0)
5156 p_flags |= PF_X;
5157
5158 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5159 m = bfd_zalloc (abfd, amt);
5160 if (m == NULL)
5161 goto error_return;
5162 m->next = NULL;
5163 m->p_type = (PT_GNU_MBIND_LO
5164 + elf_section_data (s)->this_hdr.sh_info);
5165 m->count = 1;
5166 m->p_flags_valid = 1;
5167 m->sections[0] = s;
5168 m->p_flags = p_flags;
5169
5170 *pm = m;
5171 pm = &m->next;
5172 }
5173
5174 s = bfd_get_section_by_name (abfd,
5175 NOTE_GNU_PROPERTY_SECTION_NAME);
5176 if (s != NULL && s->size != 0)
5177 {
5178 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5179 m = bfd_zalloc (abfd, amt);
5180 if (m == NULL)
5181 goto error_return;
5182 m->next = NULL;
5183 m->p_type = PT_GNU_PROPERTY;
5184 m->count = 1;
5185 m->p_flags_valid = 1;
5186 m->sections[0] = s;
5187 m->p_flags = PF_R;
5188 *pm = m;
5189 pm = &m->next;
5190 }
5191
5192 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5193 segment. */
5194 eh_frame_hdr = elf_eh_frame_hdr (info);
5195 if (eh_frame_hdr != NULL
5196 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5197 {
5198 amt = sizeof (struct elf_segment_map);
5199 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5200 if (m == NULL)
5201 goto error_return;
5202 m->next = NULL;
5203 m->p_type = PT_GNU_EH_FRAME;
5204 m->count = 1;
5205 m->sections[0] = eh_frame_hdr->output_section;
5206
5207 *pm = m;
5208 pm = &m->next;
5209 }
5210
5211 if (elf_stack_flags (abfd))
5212 {
5213 amt = sizeof (struct elf_segment_map);
5214 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5215 if (m == NULL)
5216 goto error_return;
5217 m->next = NULL;
5218 m->p_type = PT_GNU_STACK;
5219 m->p_flags = elf_stack_flags (abfd);
5220 m->p_align = bed->stack_align;
5221 m->p_flags_valid = 1;
5222 m->p_align_valid = m->p_align != 0;
5223 if (info->stacksize > 0)
5224 {
5225 m->p_size = info->stacksize;
5226 m->p_size_valid = 1;
5227 }
5228
5229 *pm = m;
5230 pm = &m->next;
5231 }
5232
5233 if (info != NULL && info->relro)
5234 {
5235 for (m = mfirst; m != NULL; m = m->next)
5236 {
5237 if (m->p_type == PT_LOAD
5238 && m->count != 0
5239 && m->sections[0]->vma >= info->relro_start
5240 && m->sections[0]->vma < info->relro_end)
5241 {
5242 i = m->count;
5243 while (--i != (unsigned) -1)
5244 {
5245 if (m->sections[i]->size > 0
5246 && (m->sections[i]->flags & SEC_LOAD) != 0
5247 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5248 break;
5249 }
5250
5251 if (i != (unsigned) -1)
5252 break;
5253 }
5254 }
5255
5256 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5257 if (m != NULL)
5258 {
5259 amt = sizeof (struct elf_segment_map);
5260 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5261 if (m == NULL)
5262 goto error_return;
5263 m->next = NULL;
5264 m->p_type = PT_GNU_RELRO;
5265 *pm = m;
5266 pm = &m->next;
5267 }
5268 }
5269
5270 free (sections);
5271 elf_seg_map (abfd) = mfirst;
5272 }
5273
5274 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5275 return false;
5276
5277 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5278 ++count;
5279 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5280
5281 return true;
5282
5283 error_return:
5284 free (sections);
5285 return false;
5286 }
5287
5288 /* Sort sections by address. */
5289
5290 static int
5291 elf_sort_sections (const void *arg1, const void *arg2)
5292 {
5293 const asection *sec1 = *(const asection **) arg1;
5294 const asection *sec2 = *(const asection **) arg2;
5295 bfd_size_type size1, size2;
5296
5297 /* Sort by LMA first, since this is the address used to
5298 place the section into a segment. */
5299 if (sec1->lma < sec2->lma)
5300 return -1;
5301 else if (sec1->lma > sec2->lma)
5302 return 1;
5303
5304 /* Then sort by VMA. Normally the LMA and the VMA will be
5305 the same, and this will do nothing. */
5306 if (sec1->vma < sec2->vma)
5307 return -1;
5308 else if (sec1->vma > sec2->vma)
5309 return 1;
5310
5311 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5312
5313 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5314 && (x)->size != 0)
5315
5316 if (TOEND (sec1))
5317 {
5318 if (!TOEND (sec2))
5319 return 1;
5320 }
5321 else if (TOEND (sec2))
5322 return -1;
5323
5324 #undef TOEND
5325
5326 /* Sort by size, to put zero sized sections
5327 before others at the same address. */
5328
5329 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5330 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5331
5332 if (size1 < size2)
5333 return -1;
5334 if (size1 > size2)
5335 return 1;
5336
5337 return sec1->target_index - sec2->target_index;
5338 }
5339
5340 /* This qsort comparison functions sorts PT_LOAD segments first and
5341 by p_paddr, for assign_file_positions_for_load_sections. */
5342
5343 static int
5344 elf_sort_segments (const void *arg1, const void *arg2)
5345 {
5346 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5347 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5348
5349 if (m1->p_type != m2->p_type)
5350 {
5351 if (m1->p_type == PT_NULL)
5352 return 1;
5353 if (m2->p_type == PT_NULL)
5354 return -1;
5355 return m1->p_type < m2->p_type ? -1 : 1;
5356 }
5357 if (m1->includes_filehdr != m2->includes_filehdr)
5358 return m1->includes_filehdr ? -1 : 1;
5359 if (m1->no_sort_lma != m2->no_sort_lma)
5360 return m1->no_sort_lma ? -1 : 1;
5361 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5362 {
5363 bfd_vma lma1, lma2; /* Octets. */
5364 lma1 = 0;
5365 if (m1->p_paddr_valid)
5366 lma1 = m1->p_paddr;
5367 else if (m1->count != 0)
5368 {
5369 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5370 m1->sections[0]);
5371 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5372 }
5373 lma2 = 0;
5374 if (m2->p_paddr_valid)
5375 lma2 = m2->p_paddr;
5376 else if (m2->count != 0)
5377 {
5378 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5379 m2->sections[0]);
5380 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5381 }
5382 if (lma1 != lma2)
5383 return lma1 < lma2 ? -1 : 1;
5384 }
5385 if (m1->idx != m2->idx)
5386 return m1->idx < m2->idx ? -1 : 1;
5387 return 0;
5388 }
5389
5390 /* Ian Lance Taylor writes:
5391
5392 We shouldn't be using % with a negative signed number. That's just
5393 not good. We have to make sure either that the number is not
5394 negative, or that the number has an unsigned type. When the types
5395 are all the same size they wind up as unsigned. When file_ptr is a
5396 larger signed type, the arithmetic winds up as signed long long,
5397 which is wrong.
5398
5399 What we're trying to say here is something like ``increase OFF by
5400 the least amount that will cause it to be equal to the VMA modulo
5401 the page size.'' */
5402 /* In other words, something like:
5403
5404 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5405 off_offset = off % bed->maxpagesize;
5406 if (vma_offset < off_offset)
5407 adjustment = vma_offset + bed->maxpagesize - off_offset;
5408 else
5409 adjustment = vma_offset - off_offset;
5410
5411 which can be collapsed into the expression below. */
5412
5413 static file_ptr
5414 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5415 {
5416 /* PR binutils/16199: Handle an alignment of zero. */
5417 if (maxpagesize == 0)
5418 maxpagesize = 1;
5419 return ((vma - off) % maxpagesize);
5420 }
5421
5422 static void
5423 print_segment_map (const struct elf_segment_map *m)
5424 {
5425 unsigned int j;
5426 const char *pt = get_segment_type (m->p_type);
5427 char buf[32];
5428
5429 if (pt == NULL)
5430 {
5431 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5432 sprintf (buf, "LOPROC+%7.7x",
5433 (unsigned int) (m->p_type - PT_LOPROC));
5434 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5435 sprintf (buf, "LOOS+%7.7x",
5436 (unsigned int) (m->p_type - PT_LOOS));
5437 else
5438 snprintf (buf, sizeof (buf), "%8.8x",
5439 (unsigned int) m->p_type);
5440 pt = buf;
5441 }
5442 fflush (stdout);
5443 fprintf (stderr, "%s:", pt);
5444 for (j = 0; j < m->count; j++)
5445 fprintf (stderr, " %s", m->sections [j]->name);
5446 putc ('\n',stderr);
5447 fflush (stderr);
5448 }
5449
5450 static bool
5451 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5452 {
5453 void *buf;
5454 bool ret;
5455
5456 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5457 return false;
5458 buf = bfd_zmalloc (len);
5459 if (buf == NULL)
5460 return false;
5461 ret = bfd_bwrite (buf, len, abfd) == len;
5462 free (buf);
5463 return ret;
5464 }
5465
5466 /* Assign file positions to the sections based on the mapping from
5467 sections to segments. This function also sets up some fields in
5468 the file header. */
5469
5470 static bool
5471 assign_file_positions_for_load_sections (bfd *abfd,
5472 struct bfd_link_info *link_info)
5473 {
5474 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5475 struct elf_segment_map *m;
5476 struct elf_segment_map *phdr_load_seg;
5477 Elf_Internal_Phdr *phdrs;
5478 Elf_Internal_Phdr *p;
5479 file_ptr off; /* Octets. */
5480 bfd_size_type maxpagesize;
5481 unsigned int alloc, actual;
5482 unsigned int i, j;
5483 struct elf_segment_map **sorted_seg_map;
5484 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5485
5486 if (link_info == NULL
5487 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5488 return false;
5489
5490 alloc = 0;
5491 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5492 m->idx = alloc++;
5493
5494 if (alloc)
5495 {
5496 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5497 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5498 }
5499 else
5500 {
5501 /* PR binutils/12467. */
5502 elf_elfheader (abfd)->e_phoff = 0;
5503 elf_elfheader (abfd)->e_phentsize = 0;
5504 }
5505
5506 elf_elfheader (abfd)->e_phnum = alloc;
5507
5508 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5509 {
5510 actual = alloc;
5511 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5512 }
5513 else
5514 {
5515 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5516 BFD_ASSERT (elf_program_header_size (abfd)
5517 == actual * bed->s->sizeof_phdr);
5518 BFD_ASSERT (actual >= alloc);
5519 }
5520
5521 if (alloc == 0)
5522 {
5523 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5524 return true;
5525 }
5526
5527 /* We're writing the size in elf_program_header_size (abfd),
5528 see assign_file_positions_except_relocs, so make sure we have
5529 that amount allocated, with trailing space cleared.
5530 The variable alloc contains the computed need, while
5531 elf_program_header_size (abfd) contains the size used for the
5532 layout.
5533 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5534 where the layout is forced to according to a larger size in the
5535 last iterations for the testcase ld-elf/header. */
5536 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5537 + alloc * sizeof (*sorted_seg_map)));
5538 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5539 elf_tdata (abfd)->phdr = phdrs;
5540 if (phdrs == NULL)
5541 return false;
5542
5543 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5544 {
5545 sorted_seg_map[j] = m;
5546 /* If elf_segment_map is not from map_sections_to_segments, the
5547 sections may not be correctly ordered. NOTE: sorting should
5548 not be done to the PT_NOTE section of a corefile, which may
5549 contain several pseudo-sections artificially created by bfd.
5550 Sorting these pseudo-sections breaks things badly. */
5551 if (m->count > 1
5552 && !(elf_elfheader (abfd)->e_type == ET_CORE
5553 && m->p_type == PT_NOTE))
5554 {
5555 for (i = 0; i < m->count; i++)
5556 m->sections[i]->target_index = i;
5557 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5558 elf_sort_sections);
5559 }
5560 }
5561 if (alloc > 1)
5562 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5563 elf_sort_segments);
5564
5565 maxpagesize = 1;
5566 if ((abfd->flags & D_PAGED) != 0)
5567 {
5568 if (link_info != NULL)
5569 maxpagesize = link_info->maxpagesize;
5570 else
5571 maxpagesize = bed->maxpagesize;
5572 }
5573
5574 /* Sections must map to file offsets past the ELF file header. */
5575 off = bed->s->sizeof_ehdr;
5576 /* And if one of the PT_LOAD headers doesn't include the program
5577 headers then we'll be mapping program headers in the usual
5578 position after the ELF file header. */
5579 phdr_load_seg = NULL;
5580 for (j = 0; j < alloc; j++)
5581 {
5582 m = sorted_seg_map[j];
5583 if (m->p_type != PT_LOAD)
5584 break;
5585 if (m->includes_phdrs)
5586 {
5587 phdr_load_seg = m;
5588 break;
5589 }
5590 }
5591 if (phdr_load_seg == NULL)
5592 off += actual * bed->s->sizeof_phdr;
5593
5594 for (j = 0; j < alloc; j++)
5595 {
5596 asection **secpp;
5597 bfd_vma off_adjust; /* Octets. */
5598 bool no_contents;
5599 bfd_size_type p_align;
5600 bool p_align_p;
5601
5602 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5603 number of sections with contents contributing to both p_filesz
5604 and p_memsz, followed by a number of sections with no contents
5605 that just contribute to p_memsz. In this loop, OFF tracks next
5606 available file offset for PT_LOAD and PT_NOTE segments. */
5607 m = sorted_seg_map[j];
5608 p = phdrs + m->idx;
5609 p->p_type = m->p_type;
5610 p->p_flags = m->p_flags;
5611 p_align = bed->p_align;
5612 p_align_p = false;
5613
5614 if (m->count == 0)
5615 p->p_vaddr = m->p_vaddr_offset * opb;
5616 else
5617 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5618
5619 if (m->p_paddr_valid)
5620 p->p_paddr = m->p_paddr;
5621 else if (m->count == 0)
5622 p->p_paddr = 0;
5623 else
5624 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5625
5626 if (p->p_type == PT_LOAD
5627 && (abfd->flags & D_PAGED) != 0)
5628 {
5629 /* p_align in demand paged PT_LOAD segments effectively stores
5630 the maximum page size. When copying an executable with
5631 objcopy, we set m->p_align from the input file. Use this
5632 value for maxpagesize rather than bed->maxpagesize, which
5633 may be different. Note that we use maxpagesize for PT_TLS
5634 segment alignment later in this function, so we are relying
5635 on at least one PT_LOAD segment appearing before a PT_TLS
5636 segment. */
5637 if (m->p_align_valid)
5638 maxpagesize = m->p_align;
5639 else if (p_align != 0
5640 && (link_info == NULL
5641 || !link_info->maxpagesize_is_set))
5642 /* Set p_align to the default p_align value while laying
5643 out segments aligning to the maximum page size or the
5644 largest section alignment. The run-time loader can
5645 align segments to the default p_align value or the
5646 maximum page size, depending on system page size. */
5647 p_align_p = true;
5648
5649 p->p_align = maxpagesize;
5650 }
5651 else if (m->p_align_valid)
5652 p->p_align = m->p_align;
5653 else if (m->count == 0)
5654 p->p_align = 1 << bed->s->log_file_align;
5655
5656 if (m == phdr_load_seg)
5657 {
5658 if (!m->includes_filehdr)
5659 p->p_offset = off;
5660 off += actual * bed->s->sizeof_phdr;
5661 }
5662
5663 no_contents = false;
5664 off_adjust = 0;
5665 if (p->p_type == PT_LOAD
5666 && m->count > 0)
5667 {
5668 bfd_size_type align; /* Bytes. */
5669 unsigned int align_power = 0;
5670
5671 if (m->p_align_valid)
5672 align = p->p_align;
5673 else
5674 {
5675 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5676 {
5677 unsigned int secalign;
5678
5679 secalign = bfd_section_alignment (*secpp);
5680 if (secalign > align_power)
5681 align_power = secalign;
5682 }
5683 align = (bfd_size_type) 1 << align_power;
5684 if (align < maxpagesize)
5685 {
5686 /* If a section requires alignment higher than the
5687 default p_align value, don't set p_align to the
5688 default p_align value. */
5689 if (align > p_align)
5690 p_align_p = false;
5691 align = maxpagesize;
5692 }
5693 else
5694 {
5695 /* If a section requires alignment higher than the
5696 maximum page size, set p_align to the section
5697 alignment. */
5698 p_align_p = true;
5699 p_align = align;
5700 }
5701 }
5702
5703 for (i = 0; i < m->count; i++)
5704 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5705 /* If we aren't making room for this section, then
5706 it must be SHT_NOBITS regardless of what we've
5707 set via struct bfd_elf_special_section. */
5708 elf_section_type (m->sections[i]) = SHT_NOBITS;
5709
5710 /* Find out whether this segment contains any loadable
5711 sections. */
5712 no_contents = true;
5713 for (i = 0; i < m->count; i++)
5714 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5715 {
5716 no_contents = false;
5717 break;
5718 }
5719
5720 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5721
5722 /* Broken hardware and/or kernel require that files do not
5723 map the same page with different permissions on some hppa
5724 processors. */
5725 if (j != 0
5726 && (abfd->flags & D_PAGED) != 0
5727 && bed->no_page_alias
5728 && (off & (maxpagesize - 1)) != 0
5729 && ((off & -maxpagesize)
5730 == ((off + off_adjust) & -maxpagesize)))
5731 off_adjust += maxpagesize;
5732 off += off_adjust;
5733 if (no_contents)
5734 {
5735 /* We shouldn't need to align the segment on disk since
5736 the segment doesn't need file space, but the gABI
5737 arguably requires the alignment and glibc ld.so
5738 checks it. So to comply with the alignment
5739 requirement but not waste file space, we adjust
5740 p_offset for just this segment. (OFF_ADJUST is
5741 subtracted from OFF later.) This may put p_offset
5742 past the end of file, but that shouldn't matter. */
5743 }
5744 else
5745 off_adjust = 0;
5746 }
5747 /* Make sure the .dynamic section is the first section in the
5748 PT_DYNAMIC segment. */
5749 else if (p->p_type == PT_DYNAMIC
5750 && m->count > 1
5751 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5752 {
5753 _bfd_error_handler
5754 (_("%pB: The first section in the PT_DYNAMIC segment"
5755 " is not the .dynamic section"),
5756 abfd);
5757 bfd_set_error (bfd_error_bad_value);
5758 return false;
5759 }
5760 /* Set the note section type to SHT_NOTE. */
5761 else if (p->p_type == PT_NOTE)
5762 for (i = 0; i < m->count; i++)
5763 elf_section_type (m->sections[i]) = SHT_NOTE;
5764
5765 if (m->includes_filehdr)
5766 {
5767 if (!m->p_flags_valid)
5768 p->p_flags |= PF_R;
5769 p->p_filesz = bed->s->sizeof_ehdr;
5770 p->p_memsz = bed->s->sizeof_ehdr;
5771 if (p->p_type == PT_LOAD)
5772 {
5773 if (m->count > 0)
5774 {
5775 if (p->p_vaddr < (bfd_vma) off
5776 || (!m->p_paddr_valid
5777 && p->p_paddr < (bfd_vma) off))
5778 {
5779 _bfd_error_handler
5780 (_("%pB: not enough room for program headers,"
5781 " try linking with -N"),
5782 abfd);
5783 bfd_set_error (bfd_error_bad_value);
5784 return false;
5785 }
5786 p->p_vaddr -= off;
5787 if (!m->p_paddr_valid)
5788 p->p_paddr -= off;
5789 }
5790 }
5791 else if (sorted_seg_map[0]->includes_filehdr)
5792 {
5793 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5794 p->p_vaddr = filehdr->p_vaddr;
5795 if (!m->p_paddr_valid)
5796 p->p_paddr = filehdr->p_paddr;
5797 }
5798 }
5799
5800 if (m->includes_phdrs)
5801 {
5802 if (!m->p_flags_valid)
5803 p->p_flags |= PF_R;
5804 p->p_filesz += actual * bed->s->sizeof_phdr;
5805 p->p_memsz += actual * bed->s->sizeof_phdr;
5806 if (!m->includes_filehdr)
5807 {
5808 if (p->p_type == PT_LOAD)
5809 {
5810 elf_elfheader (abfd)->e_phoff = p->p_offset;
5811 if (m->count > 0)
5812 {
5813 p->p_vaddr -= off - p->p_offset;
5814 if (!m->p_paddr_valid)
5815 p->p_paddr -= off - p->p_offset;
5816 }
5817 }
5818 else if (phdr_load_seg != NULL)
5819 {
5820 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5821 bfd_vma phdr_off = 0; /* Octets. */
5822 if (phdr_load_seg->includes_filehdr)
5823 phdr_off = bed->s->sizeof_ehdr;
5824 p->p_vaddr = phdr->p_vaddr + phdr_off;
5825 if (!m->p_paddr_valid)
5826 p->p_paddr = phdr->p_paddr + phdr_off;
5827 p->p_offset = phdr->p_offset + phdr_off;
5828 }
5829 else
5830 p->p_offset = bed->s->sizeof_ehdr;
5831 }
5832 }
5833
5834 if (p->p_type == PT_LOAD
5835 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5836 {
5837 if (!m->includes_filehdr && !m->includes_phdrs)
5838 {
5839 p->p_offset = off;
5840 if (no_contents)
5841 {
5842 /* Put meaningless p_offset for PT_LOAD segments
5843 without file contents somewhere within the first
5844 page, in an attempt to not point past EOF. */
5845 bfd_size_type align = maxpagesize;
5846 if (align < p->p_align)
5847 align = p->p_align;
5848 if (align < 1)
5849 align = 1;
5850 p->p_offset = off % align;
5851 }
5852 }
5853 else
5854 {
5855 file_ptr adjust; /* Octets. */
5856
5857 adjust = off - (p->p_offset + p->p_filesz);
5858 if (!no_contents)
5859 p->p_filesz += adjust;
5860 p->p_memsz += adjust;
5861 }
5862 }
5863
5864 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5865 maps. Set filepos for sections in PT_LOAD segments, and in
5866 core files, for sections in PT_NOTE segments.
5867 assign_file_positions_for_non_load_sections will set filepos
5868 for other sections and update p_filesz for other segments. */
5869 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5870 {
5871 asection *sec;
5872 bfd_size_type align;
5873 Elf_Internal_Shdr *this_hdr;
5874
5875 sec = *secpp;
5876 this_hdr = &elf_section_data (sec)->this_hdr;
5877 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5878
5879 if ((p->p_type == PT_LOAD
5880 || p->p_type == PT_TLS)
5881 && (this_hdr->sh_type != SHT_NOBITS
5882 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5883 && ((this_hdr->sh_flags & SHF_TLS) == 0
5884 || p->p_type == PT_TLS))))
5885 {
5886 bfd_vma p_start = p->p_paddr; /* Octets. */
5887 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5888 bfd_vma s_start = sec->lma * opb; /* Octets. */
5889 bfd_vma adjust = s_start - p_end; /* Octets. */
5890
5891 if (adjust != 0
5892 && (s_start < p_end
5893 || p_end < p_start))
5894 {
5895 _bfd_error_handler
5896 /* xgettext:c-format */
5897 (_("%pB: section %pA lma %#" PRIx64
5898 " adjusted to %#" PRIx64),
5899 abfd, sec, (uint64_t) s_start / opb,
5900 (uint64_t) p_end / opb);
5901 adjust = 0;
5902 sec->lma = p_end / opb;
5903 }
5904 p->p_memsz += adjust;
5905
5906 if (p->p_type == PT_LOAD)
5907 {
5908 if (this_hdr->sh_type != SHT_NOBITS)
5909 {
5910 off_adjust = 0;
5911 if (p->p_filesz + adjust < p->p_memsz)
5912 {
5913 /* We have a PROGBITS section following NOBITS ones.
5914 Allocate file space for the NOBITS section(s) and
5915 zero it. */
5916 adjust = p->p_memsz - p->p_filesz;
5917 if (!write_zeros (abfd, off, adjust))
5918 return false;
5919 }
5920 }
5921 /* We only adjust sh_offset in SHT_NOBITS sections
5922 as would seem proper for their address when the
5923 section is first in the segment. sh_offset
5924 doesn't really have any significance for
5925 SHT_NOBITS anyway, apart from a notional position
5926 relative to other sections. Historically we
5927 didn't bother with adjusting sh_offset and some
5928 programs depend on it not being adjusted. See
5929 pr12921 and pr25662. */
5930 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5931 {
5932 off += adjust;
5933 if (this_hdr->sh_type == SHT_NOBITS)
5934 off_adjust += adjust;
5935 }
5936 }
5937 if (this_hdr->sh_type != SHT_NOBITS)
5938 p->p_filesz += adjust;
5939 }
5940
5941 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5942 {
5943 /* The section at i == 0 is the one that actually contains
5944 everything. */
5945 if (i == 0)
5946 {
5947 this_hdr->sh_offset = sec->filepos = off;
5948 off += this_hdr->sh_size;
5949 p->p_filesz = this_hdr->sh_size;
5950 p->p_memsz = 0;
5951 p->p_align = 1;
5952 }
5953 else
5954 {
5955 /* The rest are fake sections that shouldn't be written. */
5956 sec->filepos = 0;
5957 sec->size = 0;
5958 sec->flags = 0;
5959 continue;
5960 }
5961 }
5962 else
5963 {
5964 if (p->p_type == PT_LOAD)
5965 {
5966 this_hdr->sh_offset = sec->filepos = off;
5967 if (this_hdr->sh_type != SHT_NOBITS)
5968 off += this_hdr->sh_size;
5969 }
5970 else if (this_hdr->sh_type == SHT_NOBITS
5971 && (this_hdr->sh_flags & SHF_TLS) != 0
5972 && this_hdr->sh_offset == 0)
5973 {
5974 /* This is a .tbss section that didn't get a PT_LOAD.
5975 (See _bfd_elf_map_sections_to_segments "Create a
5976 final PT_LOAD".) Set sh_offset to the value it
5977 would have if we had created a zero p_filesz and
5978 p_memsz PT_LOAD header for the section. This
5979 also makes the PT_TLS header have the same
5980 p_offset value. */
5981 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5982 off, align);
5983 this_hdr->sh_offset = sec->filepos = off + adjust;
5984 }
5985
5986 if (this_hdr->sh_type != SHT_NOBITS)
5987 {
5988 p->p_filesz += this_hdr->sh_size;
5989 /* A load section without SHF_ALLOC is something like
5990 a note section in a PT_NOTE segment. These take
5991 file space but are not loaded into memory. */
5992 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5993 p->p_memsz += this_hdr->sh_size;
5994 }
5995 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5996 {
5997 if (p->p_type == PT_TLS)
5998 p->p_memsz += this_hdr->sh_size;
5999
6000 /* .tbss is special. It doesn't contribute to p_memsz of
6001 normal segments. */
6002 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6003 p->p_memsz += this_hdr->sh_size;
6004 }
6005
6006 if (align > p->p_align
6007 && !m->p_align_valid
6008 && (p->p_type != PT_LOAD
6009 || (abfd->flags & D_PAGED) == 0))
6010 p->p_align = align;
6011 }
6012
6013 if (!m->p_flags_valid)
6014 {
6015 p->p_flags |= PF_R;
6016 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6017 p->p_flags |= PF_X;
6018 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6019 p->p_flags |= PF_W;
6020 }
6021 }
6022
6023 off -= off_adjust;
6024
6025 /* PR ld/20815 - Check that the program header segment, if
6026 present, will be loaded into memory. */
6027 if (p->p_type == PT_PHDR
6028 && phdr_load_seg == NULL
6029 && !(bed->elf_backend_allow_non_load_phdr != NULL
6030 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6031 {
6032 /* The fix for this error is usually to edit the linker script being
6033 used and set up the program headers manually. Either that or
6034 leave room for the headers at the start of the SECTIONS. */
6035 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6036 " by LOAD segment"),
6037 abfd);
6038 if (link_info == NULL)
6039 return false;
6040 /* Arrange for the linker to exit with an error, deleting
6041 the output file unless --noinhibit-exec is given. */
6042 link_info->callbacks->info ("%X");
6043 }
6044
6045 /* Check that all sections are in a PT_LOAD segment.
6046 Don't check funky gdb generated core files. */
6047 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6048 {
6049 bool check_vma = true;
6050
6051 for (i = 1; i < m->count; i++)
6052 if (m->sections[i]->vma == m->sections[i - 1]->vma
6053 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6054 ->this_hdr), p) != 0
6055 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6056 ->this_hdr), p) != 0)
6057 {
6058 /* Looks like we have overlays packed into the segment. */
6059 check_vma = false;
6060 break;
6061 }
6062
6063 for (i = 0; i < m->count; i++)
6064 {
6065 Elf_Internal_Shdr *this_hdr;
6066 asection *sec;
6067
6068 sec = m->sections[i];
6069 this_hdr = &(elf_section_data(sec)->this_hdr);
6070 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6071 && !ELF_TBSS_SPECIAL (this_hdr, p))
6072 {
6073 _bfd_error_handler
6074 /* xgettext:c-format */
6075 (_("%pB: section `%pA' can't be allocated in segment %d"),
6076 abfd, sec, j);
6077 print_segment_map (m);
6078 }
6079 }
6080
6081 if (p_align_p)
6082 p->p_align = p_align;
6083 }
6084 }
6085
6086 elf_next_file_pos (abfd) = off;
6087
6088 if (link_info != NULL
6089 && phdr_load_seg != NULL
6090 && phdr_load_seg->includes_filehdr)
6091 {
6092 /* There is a segment that contains both the file headers and the
6093 program headers, so provide a symbol __ehdr_start pointing there.
6094 A program can use this to examine itself robustly. */
6095
6096 struct elf_link_hash_entry *hash
6097 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6098 false, false, true);
6099 /* If the symbol was referenced and not defined, define it. */
6100 if (hash != NULL
6101 && (hash->root.type == bfd_link_hash_new
6102 || hash->root.type == bfd_link_hash_undefined
6103 || hash->root.type == bfd_link_hash_undefweak
6104 || hash->root.type == bfd_link_hash_common))
6105 {
6106 asection *s = NULL;
6107 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6108
6109 if (phdr_load_seg->count != 0)
6110 /* The segment contains sections, so use the first one. */
6111 s = phdr_load_seg->sections[0];
6112 else
6113 /* Use the first (i.e. lowest-addressed) section in any segment. */
6114 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6115 if (m->p_type == PT_LOAD && m->count != 0)
6116 {
6117 s = m->sections[0];
6118 break;
6119 }
6120
6121 if (s != NULL)
6122 {
6123 hash->root.u.def.value = filehdr_vaddr - s->vma;
6124 hash->root.u.def.section = s;
6125 }
6126 else
6127 {
6128 hash->root.u.def.value = filehdr_vaddr;
6129 hash->root.u.def.section = bfd_abs_section_ptr;
6130 }
6131
6132 hash->root.type = bfd_link_hash_defined;
6133 hash->def_regular = 1;
6134 hash->non_elf = 0;
6135 }
6136 }
6137
6138 return true;
6139 }
6140
6141 /* Determine if a bfd is a debuginfo file. Unfortunately there
6142 is no defined method for detecting such files, so we have to
6143 use heuristics instead. */
6144
6145 bool
6146 is_debuginfo_file (bfd *abfd)
6147 {
6148 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6149 return false;
6150
6151 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6152 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6153 Elf_Internal_Shdr **headerp;
6154
6155 for (headerp = start_headers; headerp < end_headers; headerp ++)
6156 {
6157 Elf_Internal_Shdr *header = * headerp;
6158
6159 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6160 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6161 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6162 && header->sh_type != SHT_NOBITS
6163 && header->sh_type != SHT_NOTE)
6164 return false;
6165 }
6166
6167 return true;
6168 }
6169
6170 /* Assign file positions for other sections, except for compressed debug
6171 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6172
6173 static bool
6174 assign_file_positions_for_non_load_sections (bfd *abfd,
6175 struct bfd_link_info *link_info)
6176 {
6177 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6178 Elf_Internal_Shdr **i_shdrpp;
6179 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6180 Elf_Internal_Phdr *phdrs;
6181 Elf_Internal_Phdr *p;
6182 struct elf_segment_map *m;
6183 file_ptr off;
6184 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6185 bfd_vma maxpagesize;
6186
6187 if (link_info != NULL)
6188 maxpagesize = link_info->maxpagesize;
6189 else
6190 maxpagesize = bed->maxpagesize;
6191 i_shdrpp = elf_elfsections (abfd);
6192 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6193 off = elf_next_file_pos (abfd);
6194 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6195 {
6196 Elf_Internal_Shdr *hdr;
6197 bfd_vma align;
6198
6199 hdr = *hdrpp;
6200 if (hdr->bfd_section != NULL
6201 && (hdr->bfd_section->filepos != 0
6202 || (hdr->sh_type == SHT_NOBITS
6203 && hdr->contents == NULL)))
6204 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6205 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6206 {
6207 if (hdr->sh_size != 0
6208 /* PR 24717 - debuginfo files are known to be not strictly
6209 compliant with the ELF standard. In particular they often
6210 have .note.gnu.property sections that are outside of any
6211 loadable segment. This is not a problem for such files,
6212 so do not warn about them. */
6213 && ! is_debuginfo_file (abfd))
6214 _bfd_error_handler
6215 /* xgettext:c-format */
6216 (_("%pB: warning: allocated section `%s' not in segment"),
6217 abfd,
6218 (hdr->bfd_section == NULL
6219 ? "*unknown*"
6220 : hdr->bfd_section->name));
6221 /* We don't need to page align empty sections. */
6222 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6223 align = maxpagesize;
6224 else
6225 align = hdr->sh_addralign & -hdr->sh_addralign;
6226 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6227 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6228 false);
6229 }
6230 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6231 && hdr->bfd_section == NULL)
6232 /* We don't know the offset of these sections yet:
6233 their size has not been decided. */
6234 || (hdr->bfd_section != NULL
6235 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6236 || (bfd_section_is_ctf (hdr->bfd_section)
6237 && abfd->is_linker_output)))
6238 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6239 || (elf_symtab_shndx_list (abfd) != NULL
6240 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6241 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6242 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6243 hdr->sh_offset = -1;
6244 else
6245 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6246 }
6247 elf_next_file_pos (abfd) = off;
6248
6249 /* Now that we have set the section file positions, we can set up
6250 the file positions for the non PT_LOAD segments. */
6251 phdrs = elf_tdata (abfd)->phdr;
6252 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6253 {
6254 if (p->p_type == PT_GNU_RELRO)
6255 {
6256 bfd_vma start, end; /* Bytes. */
6257 bool ok;
6258
6259 if (link_info != NULL)
6260 {
6261 /* During linking the range of the RELRO segment is passed
6262 in link_info. Note that there may be padding between
6263 relro_start and the first RELRO section. */
6264 start = link_info->relro_start;
6265 end = link_info->relro_end;
6266 }
6267 else if (m->count != 0)
6268 {
6269 if (!m->p_size_valid)
6270 abort ();
6271 start = m->sections[0]->vma;
6272 end = start + m->p_size / opb;
6273 }
6274 else
6275 {
6276 start = 0;
6277 end = 0;
6278 }
6279
6280 ok = false;
6281 if (start < end)
6282 {
6283 struct elf_segment_map *lm;
6284 const Elf_Internal_Phdr *lp;
6285 unsigned int i;
6286
6287 /* Find a LOAD segment containing a section in the RELRO
6288 segment. */
6289 for (lm = elf_seg_map (abfd), lp = phdrs;
6290 lm != NULL;
6291 lm = lm->next, lp++)
6292 {
6293 if (lp->p_type == PT_LOAD
6294 && lm->count != 0
6295 && (lm->sections[lm->count - 1]->vma
6296 + (!IS_TBSS (lm->sections[lm->count - 1])
6297 ? lm->sections[lm->count - 1]->size / opb
6298 : 0)) > start
6299 && lm->sections[0]->vma < end)
6300 break;
6301 }
6302
6303 if (lm != NULL)
6304 {
6305 /* Find the section starting the RELRO segment. */
6306 for (i = 0; i < lm->count; i++)
6307 {
6308 asection *s = lm->sections[i];
6309 if (s->vma >= start
6310 && s->vma < end
6311 && s->size != 0)
6312 break;
6313 }
6314
6315 if (i < lm->count)
6316 {
6317 p->p_vaddr = lm->sections[i]->vma * opb;
6318 p->p_paddr = lm->sections[i]->lma * opb;
6319 p->p_offset = lm->sections[i]->filepos;
6320 p->p_memsz = end * opb - p->p_vaddr;
6321 p->p_filesz = p->p_memsz;
6322
6323 /* The RELRO segment typically ends a few bytes
6324 into .got.plt but other layouts are possible.
6325 In cases where the end does not match any
6326 loaded section (for instance is in file
6327 padding), trim p_filesz back to correspond to
6328 the end of loaded section contents. */
6329 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6330 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6331
6332 /* Preserve the alignment and flags if they are
6333 valid. The gold linker generates RW/4 for
6334 the PT_GNU_RELRO section. It is better for
6335 objcopy/strip to honor these attributes
6336 otherwise gdb will choke when using separate
6337 debug files. */
6338 if (!m->p_align_valid)
6339 p->p_align = 1;
6340 if (!m->p_flags_valid)
6341 p->p_flags = PF_R;
6342 ok = true;
6343 }
6344 }
6345 }
6346
6347 if (!ok)
6348 {
6349 if (link_info != NULL)
6350 _bfd_error_handler
6351 (_("%pB: warning: unable to allocate any sections"
6352 " to PT_GNU_RELRO segment"),
6353 abfd);
6354 memset (p, 0, sizeof *p);
6355 }
6356 }
6357 else if (p->p_type == PT_GNU_STACK)
6358 {
6359 if (m->p_size_valid)
6360 p->p_memsz = m->p_size;
6361 }
6362 else if (m->count != 0)
6363 {
6364 unsigned int i;
6365
6366 if (p->p_type != PT_LOAD
6367 && (p->p_type != PT_NOTE
6368 || bfd_get_format (abfd) != bfd_core))
6369 {
6370 /* A user specified segment layout may include a PHDR
6371 segment that overlaps with a LOAD segment... */
6372 if (p->p_type == PT_PHDR)
6373 {
6374 m->count = 0;
6375 continue;
6376 }
6377
6378 if (m->includes_filehdr || m->includes_phdrs)
6379 {
6380 /* PR 17512: file: 2195325e. */
6381 _bfd_error_handler
6382 (_("%pB: error: non-load segment %d includes file header "
6383 "and/or program header"),
6384 abfd, (int) (p - phdrs));
6385 return false;
6386 }
6387
6388 p->p_filesz = 0;
6389 p->p_offset = m->sections[0]->filepos;
6390 for (i = m->count; i-- != 0;)
6391 {
6392 asection *sect = m->sections[i];
6393 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6394 if (hdr->sh_type != SHT_NOBITS)
6395 {
6396 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6397 /* NB: p_memsz of the loadable PT_NOTE segment
6398 should be the same as p_filesz. */
6399 if (p->p_type == PT_NOTE
6400 && (hdr->sh_flags & SHF_ALLOC) != 0)
6401 p->p_memsz = p->p_filesz;
6402 break;
6403 }
6404 }
6405 }
6406 }
6407 }
6408
6409 return true;
6410 }
6411
6412 static elf_section_list *
6413 find_section_in_list (unsigned int i, elf_section_list * list)
6414 {
6415 for (;list != NULL; list = list->next)
6416 if (list->ndx == i)
6417 break;
6418 return list;
6419 }
6420
6421 /* Work out the file positions of all the sections. This is called by
6422 _bfd_elf_compute_section_file_positions. All the section sizes and
6423 VMAs must be known before this is called.
6424
6425 Reloc sections come in two flavours: Those processed specially as
6426 "side-channel" data attached to a section to which they apply, and
6427 those that bfd doesn't process as relocations. The latter sort are
6428 stored in a normal bfd section by bfd_section_from_shdr. We don't
6429 consider the former sort here, unless they form part of the loadable
6430 image. Reloc sections not assigned here (and compressed debugging
6431 sections and CTF sections which nothing else in the file can rely
6432 upon) will be handled later by assign_file_positions_for_relocs.
6433
6434 We also don't set the positions of the .symtab and .strtab here. */
6435
6436 static bool
6437 assign_file_positions_except_relocs (bfd *abfd,
6438 struct bfd_link_info *link_info)
6439 {
6440 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6441 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6442 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6443 unsigned int alloc;
6444
6445 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6446 && bfd_get_format (abfd) != bfd_core)
6447 {
6448 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6449 unsigned int num_sec = elf_numsections (abfd);
6450 Elf_Internal_Shdr **hdrpp;
6451 unsigned int i;
6452 file_ptr off;
6453
6454 /* Start after the ELF header. */
6455 off = i_ehdrp->e_ehsize;
6456
6457 /* We are not creating an executable, which means that we are
6458 not creating a program header, and that the actual order of
6459 the sections in the file is unimportant. */
6460 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6461 {
6462 Elf_Internal_Shdr *hdr;
6463
6464 hdr = *hdrpp;
6465 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6466 && hdr->bfd_section == NULL)
6467 /* Do not assign offsets for these sections yet: we don't know
6468 their sizes. */
6469 || (hdr->bfd_section != NULL
6470 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6471 || (bfd_section_is_ctf (hdr->bfd_section)
6472 && abfd->is_linker_output)))
6473 || i == elf_onesymtab (abfd)
6474 || (elf_symtab_shndx_list (abfd) != NULL
6475 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6476 || i == elf_strtab_sec (abfd)
6477 || i == elf_shstrtab_sec (abfd))
6478 {
6479 hdr->sh_offset = -1;
6480 }
6481 else
6482 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6483 }
6484
6485 elf_next_file_pos (abfd) = off;
6486 elf_program_header_size (abfd) = 0;
6487 }
6488 else
6489 {
6490 /* Assign file positions for the loaded sections based on the
6491 assignment of sections to segments. */
6492 if (!assign_file_positions_for_load_sections (abfd, link_info))
6493 return false;
6494
6495 /* And for non-load sections. */
6496 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6497 return false;
6498 }
6499
6500 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6501 return false;
6502
6503 /* Write out the program headers. */
6504 alloc = i_ehdrp->e_phnum;
6505 if (alloc != 0)
6506 {
6507 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6508 {
6509 /* Memory resident segments with non-zero size and RWX
6510 permissions are a security risk, so we generate a warning
6511 here if we are creating any. */
6512 unsigned int i;
6513
6514 for (i = 0; i < alloc; i++)
6515 {
6516 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6517
6518 if (phdr->p_memsz == 0)
6519 continue;
6520
6521 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6522 _bfd_error_handler (_("warning: %pB has a TLS segment"
6523 " with execute permission"),
6524 abfd);
6525 else if (phdr->p_type == PT_LOAD
6526 && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6527 == (PF_R | PF_W | PF_X)))
6528 _bfd_error_handler (_("warning: %pB has a LOAD segment"
6529 " with RWX permissions"),
6530 abfd);
6531 }
6532 }
6533
6534 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6535 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6536 return false;
6537 }
6538
6539 return true;
6540 }
6541
6542 bool
6543 _bfd_elf_init_file_header (bfd *abfd,
6544 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6545 {
6546 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6547 struct elf_strtab_hash *shstrtab;
6548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6549
6550 i_ehdrp = elf_elfheader (abfd);
6551
6552 shstrtab = _bfd_elf_strtab_init ();
6553 if (shstrtab == NULL)
6554 return false;
6555
6556 elf_shstrtab (abfd) = shstrtab;
6557
6558 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6559 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6560 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6561 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6562
6563 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6564 i_ehdrp->e_ident[EI_DATA] =
6565 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6566 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6567
6568 if ((abfd->flags & DYNAMIC) != 0)
6569 i_ehdrp->e_type = ET_DYN;
6570 else if ((abfd->flags & EXEC_P) != 0)
6571 i_ehdrp->e_type = ET_EXEC;
6572 else if (bfd_get_format (abfd) == bfd_core)
6573 i_ehdrp->e_type = ET_CORE;
6574 else
6575 i_ehdrp->e_type = ET_REL;
6576
6577 switch (bfd_get_arch (abfd))
6578 {
6579 case bfd_arch_unknown:
6580 i_ehdrp->e_machine = EM_NONE;
6581 break;
6582
6583 /* There used to be a long list of cases here, each one setting
6584 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6585 in the corresponding bfd definition. To avoid duplication,
6586 the switch was removed. Machines that need special handling
6587 can generally do it in elf_backend_final_write_processing(),
6588 unless they need the information earlier than the final write.
6589 Such need can generally be supplied by replacing the tests for
6590 e_machine with the conditions used to determine it. */
6591 default:
6592 i_ehdrp->e_machine = bed->elf_machine_code;
6593 }
6594
6595 i_ehdrp->e_version = bed->s->ev_current;
6596 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6597
6598 /* No program header, for now. */
6599 i_ehdrp->e_phoff = 0;
6600 i_ehdrp->e_phentsize = 0;
6601 i_ehdrp->e_phnum = 0;
6602
6603 /* Each bfd section is section header entry. */
6604 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6605 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6606
6607 elf_tdata (abfd)->symtab_hdr.sh_name =
6608 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6609 elf_tdata (abfd)->strtab_hdr.sh_name =
6610 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6611 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6612 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6613 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6614 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6615 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6616 return false;
6617
6618 return true;
6619 }
6620
6621 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6622
6623 FIXME: We used to have code here to sort the PT_LOAD segments into
6624 ascending order, as per the ELF spec. But this breaks some programs,
6625 including the Linux kernel. But really either the spec should be
6626 changed or the programs updated. */
6627
6628 bool
6629 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6630 {
6631 if (link_info != NULL && bfd_link_pie (link_info))
6632 {
6633 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6634 unsigned int num_segments = i_ehdrp->e_phnum;
6635 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6636 Elf_Internal_Phdr *segment = tdata->phdr;
6637 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6638
6639 /* Find the lowest p_vaddr in PT_LOAD segments. */
6640 bfd_vma p_vaddr = (bfd_vma) -1;
6641 for (; segment < end_segment; segment++)
6642 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6643 p_vaddr = segment->p_vaddr;
6644
6645 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6646 segments is non-zero. */
6647 if (p_vaddr)
6648 i_ehdrp->e_type = ET_EXEC;
6649 }
6650 return true;
6651 }
6652
6653 /* Assign file positions for all the reloc sections which are not part
6654 of the loadable file image, and the file position of section headers. */
6655
6656 static bool
6657 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6658 {
6659 file_ptr off;
6660 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6661 Elf_Internal_Shdr *shdrp;
6662 Elf_Internal_Ehdr *i_ehdrp;
6663 const struct elf_backend_data *bed;
6664
6665 off = elf_next_file_pos (abfd);
6666
6667 shdrpp = elf_elfsections (abfd);
6668 end_shdrpp = shdrpp + elf_numsections (abfd);
6669 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6670 {
6671 shdrp = *shdrpp;
6672 if (shdrp->sh_offset == -1)
6673 {
6674 asection *sec = shdrp->bfd_section;
6675 bool is_rel = (shdrp->sh_type == SHT_REL
6676 || shdrp->sh_type == SHT_RELA);
6677 bool is_ctf = sec && bfd_section_is_ctf (sec);
6678 if (is_rel
6679 || is_ctf
6680 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6681 {
6682 if (!is_rel && !is_ctf)
6683 {
6684 const char *name = sec->name;
6685 struct bfd_elf_section_data *d;
6686
6687 /* Compress DWARF debug sections. */
6688 if (!bfd_compress_section (abfd, sec,
6689 shdrp->contents))
6690 return false;
6691
6692 if (sec->compress_status == COMPRESS_SECTION_DONE
6693 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6694 {
6695 /* If section is compressed with zlib-gnu, convert
6696 section name from .debug_* to .zdebug_*. */
6697 char *new_name
6698 = convert_debug_to_zdebug (abfd, name);
6699 if (new_name == NULL)
6700 return false;
6701 name = new_name;
6702 }
6703 /* Add section name to section name section. */
6704 if (shdrp->sh_name != (unsigned int) -1)
6705 abort ();
6706 shdrp->sh_name
6707 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6708 name, false);
6709 d = elf_section_data (sec);
6710
6711 /* Add reloc section name to section name section. */
6712 if (d->rel.hdr
6713 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6714 name, false))
6715 return false;
6716 if (d->rela.hdr
6717 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6718 name, true))
6719 return false;
6720
6721 /* Update section size and contents. */
6722 shdrp->sh_size = sec->size;
6723 shdrp->contents = sec->contents;
6724 shdrp->bfd_section->contents = NULL;
6725 }
6726 else if (is_ctf)
6727 {
6728 /* Update section size and contents. */
6729 shdrp->sh_size = sec->size;
6730 shdrp->contents = sec->contents;
6731 }
6732
6733 off = _bfd_elf_assign_file_position_for_section (shdrp, off,
6734 true);
6735 }
6736 }
6737 }
6738
6739 /* Place section name section after DWARF debug sections have been
6740 compressed. */
6741 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6742 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6743 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6744 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6745
6746 /* Place the section headers. */
6747 i_ehdrp = elf_elfheader (abfd);
6748 bed = get_elf_backend_data (abfd);
6749 off = align_file_position (off, 1 << bed->s->log_file_align);
6750 i_ehdrp->e_shoff = off;
6751 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6752 elf_next_file_pos (abfd) = off;
6753
6754 return true;
6755 }
6756
6757 bool
6758 _bfd_elf_write_object_contents (bfd *abfd)
6759 {
6760 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6761 Elf_Internal_Shdr **i_shdrp;
6762 bool failed;
6763 unsigned int count, num_sec;
6764 struct elf_obj_tdata *t;
6765
6766 if (! abfd->output_has_begun
6767 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6768 return false;
6769 /* Do not rewrite ELF data when the BFD has been opened for update.
6770 abfd->output_has_begun was set to TRUE on opening, so creation of
6771 new sections, and modification of existing section sizes was
6772 restricted. This means the ELF header, program headers and
6773 section headers can't have changed. If the contents of any
6774 sections has been modified, then those changes have already been
6775 written to the BFD. */
6776 else if (abfd->direction == both_direction)
6777 {
6778 BFD_ASSERT (abfd->output_has_begun);
6779 return true;
6780 }
6781
6782 i_shdrp = elf_elfsections (abfd);
6783
6784 failed = false;
6785 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6786 if (failed)
6787 return false;
6788
6789 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6790 return false;
6791
6792 /* After writing the headers, we need to write the sections too... */
6793 num_sec = elf_numsections (abfd);
6794 for (count = 1; count < num_sec; count++)
6795 {
6796 i_shdrp[count]->sh_name
6797 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6798 i_shdrp[count]->sh_name);
6799 if (bed->elf_backend_section_processing)
6800 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6801 return false;
6802 if (i_shdrp[count]->contents)
6803 {
6804 bfd_size_type amt = i_shdrp[count]->sh_size;
6805
6806 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6807 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6808 return false;
6809 }
6810 }
6811
6812 /* Write out the section header names. */
6813 t = elf_tdata (abfd);
6814 if (elf_shstrtab (abfd) != NULL
6815 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6816 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6817 return false;
6818
6819 if (!(*bed->elf_backend_final_write_processing) (abfd))
6820 return false;
6821
6822 if (!bed->s->write_shdrs_and_ehdr (abfd))
6823 return false;
6824
6825 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6826 if (t->o->build_id.after_write_object_contents != NULL
6827 && !(*t->o->build_id.after_write_object_contents) (abfd))
6828 return false;
6829 if (t->o->package_metadata.after_write_object_contents != NULL
6830 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6831 return false;
6832
6833 return true;
6834 }
6835
6836 bool
6837 _bfd_elf_write_corefile_contents (bfd *abfd)
6838 {
6839 /* Hopefully this can be done just like an object file. */
6840 return _bfd_elf_write_object_contents (abfd);
6841 }
6842
6843 /* Given a section, search the header to find them. */
6844
6845 unsigned int
6846 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6847 {
6848 const struct elf_backend_data *bed;
6849 unsigned int sec_index;
6850
6851 if (elf_section_data (asect) != NULL
6852 && elf_section_data (asect)->this_idx != 0)
6853 return elf_section_data (asect)->this_idx;
6854
6855 if (bfd_is_abs_section (asect))
6856 sec_index = SHN_ABS;
6857 else if (bfd_is_com_section (asect))
6858 sec_index = SHN_COMMON;
6859 else if (bfd_is_und_section (asect))
6860 sec_index = SHN_UNDEF;
6861 else
6862 sec_index = SHN_BAD;
6863
6864 bed = get_elf_backend_data (abfd);
6865 if (bed->elf_backend_section_from_bfd_section)
6866 {
6867 int retval = sec_index;
6868
6869 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6870 return retval;
6871 }
6872
6873 if (sec_index == SHN_BAD)
6874 bfd_set_error (bfd_error_nonrepresentable_section);
6875
6876 return sec_index;
6877 }
6878
6879 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6880 on error. */
6881
6882 int
6883 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6884 {
6885 asymbol *asym_ptr = *asym_ptr_ptr;
6886 int idx;
6887 flagword flags = asym_ptr->flags;
6888
6889 /* When gas creates relocations against local labels, it creates its
6890 own symbol for the section, but does put the symbol into the
6891 symbol chain, so udata is 0. When the linker is generating
6892 relocatable output, this section symbol may be for one of the
6893 input sections rather than the output section. */
6894 if (asym_ptr->udata.i == 0
6895 && (flags & BSF_SECTION_SYM)
6896 && asym_ptr->section)
6897 {
6898 asection *sec;
6899
6900 sec = asym_ptr->section;
6901 if (sec->owner != abfd && sec->output_section != NULL)
6902 sec = sec->output_section;
6903 if (sec->owner == abfd
6904 && sec->index < elf_num_section_syms (abfd)
6905 && elf_section_syms (abfd)[sec->index] != NULL)
6906 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
6907 }
6908
6909 idx = asym_ptr->udata.i;
6910
6911 if (idx == 0)
6912 {
6913 /* This case can occur when using --strip-symbol on a symbol
6914 which is used in a relocation entry. */
6915 _bfd_error_handler
6916 /* xgettext:c-format */
6917 (_("%pB: symbol `%s' required but not present"),
6918 abfd, bfd_asymbol_name (asym_ptr));
6919 bfd_set_error (bfd_error_no_symbols);
6920 return -1;
6921 }
6922
6923 #if DEBUG & 4
6924 {
6925 fprintf (stderr,
6926 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6927 " flags = 0x%.8x\n",
6928 (long) asym_ptr, asym_ptr->name, idx, flags);
6929 fflush (stderr);
6930 }
6931 #endif
6932
6933 return idx;
6934 }
6935
6936 static inline bfd_vma
6937 segment_size (Elf_Internal_Phdr *segment)
6938 {
6939 return (segment->p_memsz > segment->p_filesz
6940 ? segment->p_memsz : segment->p_filesz);
6941 }
6942
6943
6944 /* Returns the end address of the segment + 1. */
6945 static inline bfd_vma
6946 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6947 {
6948 return start + segment_size (segment);
6949 }
6950
6951 static inline bfd_size_type
6952 section_size (asection *section, Elf_Internal_Phdr *segment)
6953 {
6954 if ((section->flags & SEC_HAS_CONTENTS) != 0
6955 || (section->flags & SEC_THREAD_LOCAL) == 0
6956 || segment->p_type == PT_TLS)
6957 return section->size;
6958 return 0;
6959 }
6960
6961 /* Returns TRUE if the given section is contained within the given
6962 segment. LMA addresses are compared against PADDR when
6963 bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true. */
6964 static bool
6965 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6966 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
6967 const struct elf_backend_data *bed)
6968 {
6969 bfd_vma seg_addr = !bed->want_p_paddr_set_to_zero ? paddr : vaddr;
6970 bfd_vma addr = !bed->want_p_paddr_set_to_zero ? section->lma : section->vma;
6971 bfd_vma octet;
6972 if (_bfd_mul_overflow (addr, opb, &octet))
6973 return false;
6974 /* The third and fourth lines below are testing that the section end
6975 address is within the segment. It's written this way to avoid
6976 overflow. Add seg_addr + section_size to both sides of the
6977 inequality to make it obvious. */
6978 return (octet >= seg_addr
6979 && segment_size (segment) >= section_size (section, segment)
6980 && (octet - seg_addr
6981 <= segment_size (segment) - section_size (section, segment)));
6982 }
6983
6984 /* Handle PT_NOTE segment. */
6985 static bool
6986 is_note (asection *s, Elf_Internal_Phdr *p)
6987 {
6988 return (p->p_type == PT_NOTE
6989 && elf_section_type (s) == SHT_NOTE
6990 && (ufile_ptr) s->filepos >= p->p_offset
6991 && p->p_filesz >= s->size
6992 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
6993 }
6994
6995 /* Rewrite program header information. */
6996
6997 static bool
6998 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6999 {
7000 Elf_Internal_Ehdr *iehdr;
7001 struct elf_segment_map *map;
7002 struct elf_segment_map *map_first;
7003 struct elf_segment_map **pointer_to_map;
7004 Elf_Internal_Phdr *segment;
7005 asection *section;
7006 unsigned int i;
7007 unsigned int num_segments;
7008 bool phdr_included = false;
7009 bool p_paddr_valid;
7010 struct elf_segment_map *phdr_adjust_seg = NULL;
7011 unsigned int phdr_adjust_num = 0;
7012 const struct elf_backend_data *bed;
7013 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7014
7015 bed = get_elf_backend_data (ibfd);
7016 iehdr = elf_elfheader (ibfd);
7017
7018 map_first = NULL;
7019 pointer_to_map = &map_first;
7020
7021 num_segments = elf_elfheader (ibfd)->e_phnum;
7022
7023 /* The complicated case when p_vaddr is 0 is to handle the Solaris
7024 linker, which generates a PT_INTERP section with p_vaddr and
7025 p_memsz set to 0. */
7026 #define IS_SOLARIS_PT_INTERP(p, s) \
7027 (p->p_vaddr == 0 \
7028 && p->p_paddr == 0 \
7029 && p->p_memsz == 0 \
7030 && p->p_filesz > 0 \
7031 && (s->flags & SEC_HAS_CONTENTS) != 0 \
7032 && s->size > 0 \
7033 && (bfd_vma) s->filepos >= p->p_offset \
7034 && ((bfd_vma) s->filepos + s->size \
7035 <= p->p_offset + p->p_filesz))
7036
7037 /* Decide if the given section should be included in the given segment.
7038 A section will be included if:
7039 1. It is within the address space of the segment -- we use the LMA
7040 if that is set for the segment and the VMA otherwise,
7041 2. It is an allocated section or a NOTE section in a PT_NOTE
7042 segment.
7043 3. There is an output section associated with it,
7044 4. The section has not already been allocated to a previous segment.
7045 5. PT_GNU_STACK segments do not include any sections.
7046 6. PT_TLS segment includes only SHF_TLS sections.
7047 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7048 8. PT_DYNAMIC should not contain empty sections at the beginning
7049 (with the possible exception of .dynamic). */
7050 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7051 (((is_contained_by (section, segment, segment->p_paddr, \
7052 segment->p_vaddr, opb, bed) \
7053 && (section->flags & SEC_ALLOC) != 0) \
7054 || is_note (section, segment)) \
7055 && segment->p_type != PT_GNU_STACK \
7056 && (segment->p_type != PT_TLS \
7057 || (section->flags & SEC_THREAD_LOCAL)) \
7058 && (segment->p_type == PT_LOAD \
7059 || segment->p_type == PT_TLS \
7060 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7061 && (segment->p_type != PT_DYNAMIC \
7062 || section_size (section, segment) > 0 \
7063 || (segment->p_paddr \
7064 ? segment->p_paddr != section->lma * (opb) \
7065 : segment->p_vaddr != section->vma * (opb)) \
7066 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7067 && (segment->p_type != PT_LOAD || !section->segment_mark))
7068
7069 /* If the output section of a section in the input segment is NULL,
7070 it is removed from the corresponding output segment. */
7071 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7072 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7073 && section->output_section != NULL)
7074
7075 /* Returns TRUE iff seg1 starts after the end of seg2. */
7076 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7077 (seg1->field >= segment_end (seg2, seg2->field))
7078
7079 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7080 their VMA address ranges and their LMA address ranges overlap.
7081 It is possible to have overlapping VMA ranges without overlapping LMA
7082 ranges. RedBoot images for example can have both .data and .bss mapped
7083 to the same VMA range, but with the .data section mapped to a different
7084 LMA. */
7085 #define SEGMENT_OVERLAPS(seg1, seg2) \
7086 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7087 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7088 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7089 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7090
7091 /* Initialise the segment mark field, and discard stupid alignment. */
7092 for (section = ibfd->sections; section != NULL; section = section->next)
7093 {
7094 asection *o = section->output_section;
7095 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7096 o->alignment_power = 0;
7097 section->segment_mark = false;
7098 }
7099
7100 /* The Solaris linker creates program headers in which all the
7101 p_paddr fields are zero. When we try to objcopy or strip such a
7102 file, we get confused. Check for this case, and if we find it
7103 don't set the p_paddr_valid fields. */
7104 p_paddr_valid = false;
7105 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7106 i < num_segments;
7107 i++, segment++)
7108 if (segment->p_paddr != 0)
7109 {
7110 p_paddr_valid = true;
7111 break;
7112 }
7113
7114 /* Scan through the segments specified in the program header
7115 of the input BFD. For this first scan we look for overlaps
7116 in the loadable segments. These can be created by weird
7117 parameters to objcopy. Also, fix some solaris weirdness. */
7118 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7119 i < num_segments;
7120 i++, segment++)
7121 {
7122 unsigned int j;
7123 Elf_Internal_Phdr *segment2;
7124
7125 if (segment->p_type == PT_INTERP)
7126 for (section = ibfd->sections; section; section = section->next)
7127 if (IS_SOLARIS_PT_INTERP (segment, section))
7128 {
7129 /* Mininal change so that the normal section to segment
7130 assignment code will work. */
7131 segment->p_vaddr = section->vma * opb;
7132 break;
7133 }
7134
7135 if (segment->p_type != PT_LOAD)
7136 {
7137 /* Remove PT_GNU_RELRO segment. */
7138 if (segment->p_type == PT_GNU_RELRO)
7139 segment->p_type = PT_NULL;
7140 continue;
7141 }
7142
7143 /* Determine if this segment overlaps any previous segments. */
7144 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7145 {
7146 bfd_signed_vma extra_length;
7147
7148 if (segment2->p_type != PT_LOAD
7149 || !SEGMENT_OVERLAPS (segment, segment2))
7150 continue;
7151
7152 /* Merge the two segments together. */
7153 if (segment2->p_vaddr < segment->p_vaddr)
7154 {
7155 /* Extend SEGMENT2 to include SEGMENT and then delete
7156 SEGMENT. */
7157 extra_length = (segment_end (segment, segment->p_vaddr)
7158 - segment_end (segment2, segment2->p_vaddr));
7159
7160 if (extra_length > 0)
7161 {
7162 segment2->p_memsz += extra_length;
7163 segment2->p_filesz += extra_length;
7164 }
7165
7166 segment->p_type = PT_NULL;
7167
7168 /* Since we have deleted P we must restart the outer loop. */
7169 i = 0;
7170 segment = elf_tdata (ibfd)->phdr;
7171 break;
7172 }
7173 else
7174 {
7175 /* Extend SEGMENT to include SEGMENT2 and then delete
7176 SEGMENT2. */
7177 extra_length = (segment_end (segment2, segment2->p_vaddr)
7178 - segment_end (segment, segment->p_vaddr));
7179
7180 if (extra_length > 0)
7181 {
7182 segment->p_memsz += extra_length;
7183 segment->p_filesz += extra_length;
7184 }
7185
7186 segment2->p_type = PT_NULL;
7187 }
7188 }
7189 }
7190
7191 /* The second scan attempts to assign sections to segments. */
7192 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7193 i < num_segments;
7194 i++, segment++)
7195 {
7196 unsigned int section_count;
7197 asection **sections;
7198 asection *output_section;
7199 unsigned int isec;
7200 asection *matching_lma;
7201 asection *suggested_lma;
7202 unsigned int j;
7203 size_t amt;
7204 asection *first_section;
7205
7206 if (segment->p_type == PT_NULL)
7207 continue;
7208
7209 first_section = NULL;
7210 /* Compute how many sections might be placed into this segment. */
7211 for (section = ibfd->sections, section_count = 0;
7212 section != NULL;
7213 section = section->next)
7214 {
7215 /* Find the first section in the input segment, which may be
7216 removed from the corresponding output segment. */
7217 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7218 {
7219 if (first_section == NULL)
7220 first_section = section;
7221 if (section->output_section != NULL)
7222 ++section_count;
7223 }
7224 }
7225
7226 /* Allocate a segment map big enough to contain
7227 all of the sections we have selected. */
7228 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7229 amt += section_count * sizeof (asection *);
7230 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7231 if (map == NULL)
7232 return false;
7233
7234 /* Initialise the fields of the segment map. Default to
7235 using the physical address of the segment in the input BFD. */
7236 map->next = NULL;
7237 map->p_type = segment->p_type;
7238 map->p_flags = segment->p_flags;
7239 map->p_flags_valid = 1;
7240
7241 if (map->p_type == PT_LOAD
7242 && (ibfd->flags & D_PAGED) != 0
7243 && maxpagesize > 1
7244 && segment->p_align > 1)
7245 {
7246 map->p_align = segment->p_align;
7247 if (segment->p_align > maxpagesize)
7248 map->p_align = maxpagesize;
7249 map->p_align_valid = 1;
7250 }
7251
7252 /* If the first section in the input segment is removed, there is
7253 no need to preserve segment physical address in the corresponding
7254 output segment. */
7255 if (!first_section || first_section->output_section != NULL)
7256 {
7257 map->p_paddr = segment->p_paddr;
7258 map->p_paddr_valid = p_paddr_valid;
7259 }
7260
7261 /* Determine if this segment contains the ELF file header
7262 and if it contains the program headers themselves. */
7263 map->includes_filehdr = (segment->p_offset == 0
7264 && segment->p_filesz >= iehdr->e_ehsize);
7265 map->includes_phdrs = 0;
7266
7267 if (!phdr_included || segment->p_type != PT_LOAD)
7268 {
7269 map->includes_phdrs =
7270 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7271 && (segment->p_offset + segment->p_filesz
7272 >= ((bfd_vma) iehdr->e_phoff
7273 + iehdr->e_phnum * iehdr->e_phentsize)));
7274
7275 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7276 phdr_included = true;
7277 }
7278
7279 if (section_count == 0)
7280 {
7281 /* Special segments, such as the PT_PHDR segment, may contain
7282 no sections, but ordinary, loadable segments should contain
7283 something. They are allowed by the ELF spec however, so only
7284 a warning is produced.
7285 There is however the valid use case of embedded systems which
7286 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7287 flash memory with zeros. No warning is shown for that case. */
7288 if (segment->p_type == PT_LOAD
7289 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7290 /* xgettext:c-format */
7291 _bfd_error_handler
7292 (_("%pB: warning: empty loadable segment detected"
7293 " at vaddr=%#" PRIx64 ", is this intentional?"),
7294 ibfd, (uint64_t) segment->p_vaddr);
7295
7296 map->p_vaddr_offset = segment->p_vaddr / opb;
7297 map->count = 0;
7298 *pointer_to_map = map;
7299 pointer_to_map = &map->next;
7300
7301 continue;
7302 }
7303
7304 /* Now scan the sections in the input BFD again and attempt
7305 to add their corresponding output sections to the segment map.
7306 The problem here is how to handle an output section which has
7307 been moved (ie had its LMA changed). There are four possibilities:
7308
7309 1. None of the sections have been moved.
7310 In this case we can continue to use the segment LMA from the
7311 input BFD.
7312
7313 2. All of the sections have been moved by the same amount.
7314 In this case we can change the segment's LMA to match the LMA
7315 of the first section.
7316
7317 3. Some of the sections have been moved, others have not.
7318 In this case those sections which have not been moved can be
7319 placed in the current segment which will have to have its size,
7320 and possibly its LMA changed, and a new segment or segments will
7321 have to be created to contain the other sections.
7322
7323 4. The sections have been moved, but not by the same amount.
7324 In this case we can change the segment's LMA to match the LMA
7325 of the first section and we will have to create a new segment
7326 or segments to contain the other sections.
7327
7328 In order to save time, we allocate an array to hold the section
7329 pointers that we are interested in. As these sections get assigned
7330 to a segment, they are removed from this array. */
7331
7332 amt = section_count * sizeof (asection *);
7333 sections = (asection **) bfd_malloc (amt);
7334 if (sections == NULL)
7335 return false;
7336
7337 /* Step One: Scan for segment vs section LMA conflicts.
7338 Also add the sections to the section array allocated above.
7339 Also add the sections to the current segment. In the common
7340 case, where the sections have not been moved, this means that
7341 we have completely filled the segment, and there is nothing
7342 more to do. */
7343 isec = 0;
7344 matching_lma = NULL;
7345 suggested_lma = NULL;
7346
7347 for (section = first_section, j = 0;
7348 section != NULL;
7349 section = section->next)
7350 {
7351 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7352 {
7353 output_section = section->output_section;
7354
7355 sections[j++] = section;
7356
7357 /* The Solaris native linker always sets p_paddr to 0.
7358 We try to catch that case here, and set it to the
7359 correct value. Note - some backends require that
7360 p_paddr be left as zero. */
7361 if (!p_paddr_valid
7362 && segment->p_vaddr != 0
7363 && !bed->want_p_paddr_set_to_zero
7364 && isec == 0
7365 && output_section->lma != 0
7366 && (align_power (segment->p_vaddr
7367 + (map->includes_filehdr
7368 ? iehdr->e_ehsize : 0)
7369 + (map->includes_phdrs
7370 ? iehdr->e_phnum * iehdr->e_phentsize
7371 : 0),
7372 output_section->alignment_power * opb)
7373 == (output_section->vma * opb)))
7374 map->p_paddr = segment->p_vaddr;
7375
7376 /* Match up the physical address of the segment with the
7377 LMA address of the output section. */
7378 if (is_contained_by (output_section, segment, map->p_paddr,
7379 map->p_paddr + map->p_vaddr_offset, opb, bed)
7380 || is_note (section, segment))
7381 {
7382 if (matching_lma == NULL
7383 || output_section->lma < matching_lma->lma)
7384 matching_lma = output_section;
7385
7386 /* We assume that if the section fits within the segment
7387 then it does not overlap any other section within that
7388 segment. */
7389 map->sections[isec++] = output_section;
7390 }
7391 else if (suggested_lma == NULL)
7392 suggested_lma = output_section;
7393
7394 if (j == section_count)
7395 break;
7396 }
7397 }
7398
7399 BFD_ASSERT (j == section_count);
7400
7401 /* Step Two: Adjust the physical address of the current segment,
7402 if necessary. */
7403 if (isec == section_count)
7404 {
7405 /* All of the sections fitted within the segment as currently
7406 specified. This is the default case. Add the segment to
7407 the list of built segments and carry on to process the next
7408 program header in the input BFD. */
7409 map->count = section_count;
7410 *pointer_to_map = map;
7411 pointer_to_map = &map->next;
7412
7413 if (p_paddr_valid
7414 && !bed->want_p_paddr_set_to_zero)
7415 {
7416 bfd_vma hdr_size = 0;
7417 if (map->includes_filehdr)
7418 hdr_size = iehdr->e_ehsize;
7419 if (map->includes_phdrs)
7420 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7421
7422 /* Account for padding before the first section in the
7423 segment. */
7424 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7425 - matching_lma->lma);
7426 }
7427
7428 free (sections);
7429 continue;
7430 }
7431 else
7432 {
7433 /* Change the current segment's physical address to match
7434 the LMA of the first section that fitted, or if no
7435 section fitted, the first section. */
7436 if (matching_lma == NULL)
7437 matching_lma = suggested_lma;
7438
7439 map->p_paddr = matching_lma->lma * opb;
7440
7441 /* Offset the segment physical address from the lma
7442 to allow for space taken up by elf headers. */
7443 if (map->includes_phdrs)
7444 {
7445 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7446
7447 /* iehdr->e_phnum is just an estimate of the number
7448 of program headers that we will need. Make a note
7449 here of the number we used and the segment we chose
7450 to hold these headers, so that we can adjust the
7451 offset when we know the correct value. */
7452 phdr_adjust_num = iehdr->e_phnum;
7453 phdr_adjust_seg = map;
7454 }
7455
7456 if (map->includes_filehdr)
7457 {
7458 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7459 map->p_paddr -= iehdr->e_ehsize;
7460 /* We've subtracted off the size of headers from the
7461 first section lma, but there may have been some
7462 alignment padding before that section too. Try to
7463 account for that by adjusting the segment lma down to
7464 the same alignment. */
7465 if (segment->p_align != 0 && segment->p_align < align)
7466 align = segment->p_align;
7467 map->p_paddr &= -(align * opb);
7468 }
7469 }
7470
7471 /* Step Three: Loop over the sections again, this time assigning
7472 those that fit to the current segment and removing them from the
7473 sections array; but making sure not to leave large gaps. Once all
7474 possible sections have been assigned to the current segment it is
7475 added to the list of built segments and if sections still remain
7476 to be assigned, a new segment is constructed before repeating
7477 the loop. */
7478 isec = 0;
7479 do
7480 {
7481 map->count = 0;
7482 suggested_lma = NULL;
7483
7484 /* Fill the current segment with sections that fit. */
7485 for (j = 0; j < section_count; j++)
7486 {
7487 section = sections[j];
7488
7489 if (section == NULL)
7490 continue;
7491
7492 output_section = section->output_section;
7493
7494 BFD_ASSERT (output_section != NULL);
7495
7496 if (is_contained_by (output_section, segment, map->p_paddr,
7497 map->p_paddr + map->p_vaddr_offset, opb, bed)
7498 || is_note (section, segment))
7499 {
7500 if (map->count == 0)
7501 {
7502 /* If the first section in a segment does not start at
7503 the beginning of the segment, then something is
7504 wrong. */
7505 if (align_power (map->p_paddr
7506 + (map->includes_filehdr
7507 ? iehdr->e_ehsize : 0)
7508 + (map->includes_phdrs
7509 ? iehdr->e_phnum * iehdr->e_phentsize
7510 : 0),
7511 output_section->alignment_power * opb)
7512 != output_section->lma * opb)
7513 goto sorry;
7514 }
7515 else
7516 {
7517 asection *prev_sec;
7518
7519 prev_sec = map->sections[map->count - 1];
7520
7521 /* If the gap between the end of the previous section
7522 and the start of this section is more than
7523 maxpagesize then we need to start a new segment. */
7524 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7525 maxpagesize)
7526 < BFD_ALIGN (output_section->lma, maxpagesize))
7527 || (prev_sec->lma + prev_sec->size
7528 > output_section->lma))
7529 {
7530 if (suggested_lma == NULL)
7531 suggested_lma = output_section;
7532
7533 continue;
7534 }
7535 }
7536
7537 map->sections[map->count++] = output_section;
7538 ++isec;
7539 sections[j] = NULL;
7540 if (segment->p_type == PT_LOAD)
7541 section->segment_mark = true;
7542 }
7543 else if (suggested_lma == NULL)
7544 suggested_lma = output_section;
7545 }
7546
7547 /* PR 23932. A corrupt input file may contain sections that cannot
7548 be assigned to any segment - because for example they have a
7549 negative size - or segments that do not contain any sections.
7550 But there are also valid reasons why a segment can be empty.
7551 So allow a count of zero. */
7552
7553 /* Add the current segment to the list of built segments. */
7554 *pointer_to_map = map;
7555 pointer_to_map = &map->next;
7556
7557 if (isec < section_count)
7558 {
7559 /* We still have not allocated all of the sections to
7560 segments. Create a new segment here, initialise it
7561 and carry on looping. */
7562 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7563 amt += section_count * sizeof (asection *);
7564 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7565 if (map == NULL)
7566 {
7567 free (sections);
7568 return false;
7569 }
7570
7571 /* Initialise the fields of the segment map. Set the physical
7572 physical address to the LMA of the first section that has
7573 not yet been assigned. */
7574 map->next = NULL;
7575 map->p_type = segment->p_type;
7576 map->p_flags = segment->p_flags;
7577 map->p_flags_valid = 1;
7578 map->p_paddr = suggested_lma->lma * opb;
7579 map->p_paddr_valid = p_paddr_valid;
7580 map->includes_filehdr = 0;
7581 map->includes_phdrs = 0;
7582 }
7583
7584 continue;
7585 sorry:
7586 bfd_set_error (bfd_error_sorry);
7587 free (sections);
7588 return false;
7589 }
7590 while (isec < section_count);
7591
7592 free (sections);
7593 }
7594
7595 elf_seg_map (obfd) = map_first;
7596
7597 /* If we had to estimate the number of program headers that were
7598 going to be needed, then check our estimate now and adjust
7599 the offset if necessary. */
7600 if (phdr_adjust_seg != NULL)
7601 {
7602 unsigned int count;
7603
7604 for (count = 0, map = map_first; map != NULL; map = map->next)
7605 count++;
7606
7607 if (count > phdr_adjust_num)
7608 phdr_adjust_seg->p_paddr
7609 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7610
7611 for (map = map_first; map != NULL; map = map->next)
7612 if (map->p_type == PT_PHDR)
7613 {
7614 bfd_vma adjust
7615 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7616 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7617 break;
7618 }
7619 }
7620
7621 #undef IS_SOLARIS_PT_INTERP
7622 #undef IS_SECTION_IN_INPUT_SEGMENT
7623 #undef INCLUDE_SECTION_IN_SEGMENT
7624 #undef SEGMENT_AFTER_SEGMENT
7625 #undef SEGMENT_OVERLAPS
7626 return true;
7627 }
7628
7629 /* Return true if p_align in the ELF program header in ABFD is valid. */
7630
7631 static bool
7632 elf_is_p_align_valid (bfd *abfd)
7633 {
7634 unsigned int i;
7635 Elf_Internal_Phdr *segment;
7636 unsigned int num_segments;
7637 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7638 bfd_size_type maxpagesize = bed->maxpagesize;
7639 bfd_size_type p_align = bed->p_align;
7640
7641 /* Return true if the default p_align value isn't set or the maximum
7642 page size is the same as the minimum page size. */
7643 if (p_align == 0 || maxpagesize == bed->minpagesize)
7644 return true;
7645
7646 /* When the default p_align value is set, p_align may be set to the
7647 default p_align value while segments are aligned to the maximum
7648 page size. In this case, the input p_align will be ignored and
7649 the maximum page size will be used to align the output segments. */
7650 segment = elf_tdata (abfd)->phdr;
7651 num_segments = elf_elfheader (abfd)->e_phnum;
7652 for (i = 0; i < num_segments; i++, segment++)
7653 if (segment->p_type == PT_LOAD
7654 && (segment->p_align != p_align
7655 || vma_page_aligned_bias (segment->p_vaddr,
7656 segment->p_offset,
7657 maxpagesize) != 0))
7658 return true;
7659
7660 return false;
7661 }
7662
7663 /* Copy ELF program header information. */
7664
7665 static bool
7666 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7667 {
7668 Elf_Internal_Ehdr *iehdr;
7669 struct elf_segment_map *map;
7670 struct elf_segment_map *map_first;
7671 struct elf_segment_map **pointer_to_map;
7672 Elf_Internal_Phdr *segment;
7673 unsigned int i;
7674 unsigned int num_segments;
7675 bool phdr_included = false;
7676 bool p_paddr_valid;
7677 bool p_palign_valid;
7678 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7679
7680 iehdr = elf_elfheader (ibfd);
7681
7682 map_first = NULL;
7683 pointer_to_map = &map_first;
7684
7685 /* If all the segment p_paddr fields are zero, don't set
7686 map->p_paddr_valid. */
7687 p_paddr_valid = false;
7688 num_segments = elf_elfheader (ibfd)->e_phnum;
7689 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7690 i < num_segments;
7691 i++, segment++)
7692 if (segment->p_paddr != 0)
7693 {
7694 p_paddr_valid = true;
7695 break;
7696 }
7697
7698 p_palign_valid = elf_is_p_align_valid (ibfd);
7699
7700 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7701 i < num_segments;
7702 i++, segment++)
7703 {
7704 asection *section;
7705 unsigned int section_count;
7706 size_t amt;
7707 Elf_Internal_Shdr *this_hdr;
7708 asection *first_section = NULL;
7709 asection *lowest_section;
7710
7711 /* Compute how many sections are in this segment. */
7712 for (section = ibfd->sections, section_count = 0;
7713 section != NULL;
7714 section = section->next)
7715 {
7716 this_hdr = &(elf_section_data(section)->this_hdr);
7717 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7718 {
7719 if (first_section == NULL)
7720 first_section = section;
7721 section_count++;
7722 }
7723 }
7724
7725 /* Allocate a segment map big enough to contain
7726 all of the sections we have selected. */
7727 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7728 amt += section_count * sizeof (asection *);
7729 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7730 if (map == NULL)
7731 return false;
7732
7733 /* Initialize the fields of the output segment map with the
7734 input segment. */
7735 map->next = NULL;
7736 map->p_type = segment->p_type;
7737 map->p_flags = segment->p_flags;
7738 map->p_flags_valid = 1;
7739 map->p_paddr = segment->p_paddr;
7740 map->p_paddr_valid = p_paddr_valid;
7741 map->p_align = segment->p_align;
7742 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7743 map->p_align_valid = (map->p_type == PT_GNU_STACK
7744 || p_palign_valid);
7745 map->p_vaddr_offset = 0;
7746
7747 if (map->p_type == PT_GNU_RELRO
7748 || map->p_type == PT_GNU_STACK)
7749 {
7750 /* The PT_GNU_RELRO segment may contain the first a few
7751 bytes in the .got.plt section even if the whole .got.plt
7752 section isn't in the PT_GNU_RELRO segment. We won't
7753 change the size of the PT_GNU_RELRO segment.
7754 Similarly, PT_GNU_STACK size is significant on uclinux
7755 systems. */
7756 map->p_size = segment->p_memsz;
7757 map->p_size_valid = 1;
7758 }
7759
7760 /* Determine if this segment contains the ELF file header
7761 and if it contains the program headers themselves. */
7762 map->includes_filehdr = (segment->p_offset == 0
7763 && segment->p_filesz >= iehdr->e_ehsize);
7764
7765 map->includes_phdrs = 0;
7766 if (! phdr_included || segment->p_type != PT_LOAD)
7767 {
7768 map->includes_phdrs =
7769 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7770 && (segment->p_offset + segment->p_filesz
7771 >= ((bfd_vma) iehdr->e_phoff
7772 + iehdr->e_phnum * iehdr->e_phentsize)));
7773
7774 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7775 phdr_included = true;
7776 }
7777
7778 lowest_section = NULL;
7779 if (section_count != 0)
7780 {
7781 unsigned int isec = 0;
7782
7783 for (section = first_section;
7784 section != NULL;
7785 section = section->next)
7786 {
7787 this_hdr = &(elf_section_data(section)->this_hdr);
7788 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7789 {
7790 map->sections[isec++] = section->output_section;
7791 if ((section->flags & SEC_ALLOC) != 0)
7792 {
7793 bfd_vma seg_off;
7794
7795 if (lowest_section == NULL
7796 || section->lma < lowest_section->lma)
7797 lowest_section = section;
7798
7799 /* Section lmas are set up from PT_LOAD header
7800 p_paddr in _bfd_elf_make_section_from_shdr.
7801 If this header has a p_paddr that disagrees
7802 with the section lma, flag the p_paddr as
7803 invalid. */
7804 if ((section->flags & SEC_LOAD) != 0)
7805 seg_off = this_hdr->sh_offset - segment->p_offset;
7806 else
7807 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7808 if (section->lma * opb - segment->p_paddr != seg_off)
7809 map->p_paddr_valid = false;
7810 }
7811 if (isec == section_count)
7812 break;
7813 }
7814 }
7815 }
7816
7817 if (section_count == 0)
7818 map->p_vaddr_offset = segment->p_vaddr / opb;
7819 else if (map->p_paddr_valid)
7820 {
7821 /* Account for padding before the first section in the segment. */
7822 bfd_vma hdr_size = 0;
7823 if (map->includes_filehdr)
7824 hdr_size = iehdr->e_ehsize;
7825 if (map->includes_phdrs)
7826 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7827
7828 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7829 - (lowest_section ? lowest_section->lma : 0));
7830 }
7831
7832 map->count = section_count;
7833 *pointer_to_map = map;
7834 pointer_to_map = &map->next;
7835 }
7836
7837 elf_seg_map (obfd) = map_first;
7838 return true;
7839 }
7840
7841 /* Copy private BFD data. This copies or rewrites ELF program header
7842 information. */
7843
7844 static bool
7845 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7846 {
7847 bfd_vma maxpagesize;
7848
7849 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7850 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7851 return true;
7852
7853 if (elf_tdata (ibfd)->phdr == NULL)
7854 return true;
7855
7856 if (ibfd->xvec == obfd->xvec)
7857 {
7858 /* Check to see if any sections in the input BFD
7859 covered by ELF program header have changed. */
7860 Elf_Internal_Phdr *segment;
7861 asection *section, *osec;
7862 unsigned int i, num_segments;
7863 Elf_Internal_Shdr *this_hdr;
7864 const struct elf_backend_data *bed;
7865
7866 bed = get_elf_backend_data (ibfd);
7867
7868 /* Regenerate the segment map if p_paddr is set to 0. */
7869 if (bed->want_p_paddr_set_to_zero)
7870 goto rewrite;
7871
7872 /* Initialize the segment mark field. */
7873 for (section = obfd->sections; section != NULL;
7874 section = section->next)
7875 section->segment_mark = false;
7876
7877 num_segments = elf_elfheader (ibfd)->e_phnum;
7878 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7879 i < num_segments;
7880 i++, segment++)
7881 {
7882 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7883 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7884 which severly confuses things, so always regenerate the segment
7885 map in this case. */
7886 if (segment->p_paddr == 0
7887 && segment->p_memsz == 0
7888 && (segment->p_type == PT_INTERP
7889 || segment->p_type == PT_DYNAMIC))
7890 goto rewrite;
7891
7892 for (section = ibfd->sections;
7893 section != NULL; section = section->next)
7894 {
7895 /* We mark the output section so that we know it comes
7896 from the input BFD. */
7897 osec = section->output_section;
7898 if (osec)
7899 osec->segment_mark = true;
7900
7901 /* Check if this section is covered by the segment. */
7902 this_hdr = &(elf_section_data(section)->this_hdr);
7903 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7904 {
7905 /* FIXME: Check if its output section is changed or
7906 removed. What else do we need to check? */
7907 if (osec == NULL
7908 || section->flags != osec->flags
7909 || section->lma != osec->lma
7910 || section->vma != osec->vma
7911 || section->size != osec->size
7912 || section->rawsize != osec->rawsize
7913 || section->alignment_power != osec->alignment_power)
7914 goto rewrite;
7915 }
7916 }
7917 }
7918
7919 /* Check to see if any output section do not come from the
7920 input BFD. */
7921 for (section = obfd->sections; section != NULL;
7922 section = section->next)
7923 {
7924 if (!section->segment_mark)
7925 goto rewrite;
7926 else
7927 section->segment_mark = false;
7928 }
7929
7930 return copy_elf_program_header (ibfd, obfd);
7931 }
7932
7933 rewrite:
7934 maxpagesize = 0;
7935 if (ibfd->xvec == obfd->xvec)
7936 {
7937 /* When rewriting program header, set the output maxpagesize to
7938 the maximum alignment of input PT_LOAD segments. */
7939 Elf_Internal_Phdr *segment;
7940 unsigned int i;
7941 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7942
7943 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7944 i < num_segments;
7945 i++, segment++)
7946 if (segment->p_type == PT_LOAD
7947 && maxpagesize < segment->p_align)
7948 {
7949 /* PR 17512: file: f17299af. */
7950 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7951 /* xgettext:c-format */
7952 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7953 PRIx64 " is too large"),
7954 ibfd, (uint64_t) segment->p_align);
7955 else
7956 maxpagesize = segment->p_align;
7957 }
7958 }
7959 if (maxpagesize == 0)
7960 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7961
7962 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7963 }
7964
7965 /* Initialize private output section information from input section. */
7966
7967 bool
7968 _bfd_elf_init_private_section_data (bfd *ibfd,
7969 asection *isec,
7970 bfd *obfd,
7971 asection *osec,
7972 struct bfd_link_info *link_info)
7973
7974 {
7975 Elf_Internal_Shdr *ihdr, *ohdr;
7976 bool final_link = (link_info != NULL
7977 && !bfd_link_relocatable (link_info));
7978
7979 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7980 || obfd->xvec->flavour != bfd_target_elf_flavour)
7981 return true;
7982
7983 BFD_ASSERT (elf_section_data (osec) != NULL);
7984
7985 /* If this is a known ABI section, ELF section type and flags may
7986 have been set up when OSEC was created. For normal sections we
7987 allow the user to override the type and flags other than
7988 SHF_MASKOS and SHF_MASKPROC. */
7989 if (elf_section_type (osec) == SHT_PROGBITS
7990 || elf_section_type (osec) == SHT_NOTE
7991 || elf_section_type (osec) == SHT_NOBITS)
7992 elf_section_type (osec) = SHT_NULL;
7993 /* For objcopy and relocatable link, copy the ELF section type from
7994 the input file if the BFD section flags are the same. (If they
7995 are different the user may be doing something like
7996 "objcopy --set-section-flags .text=alloc,data".) For a final
7997 link allow some flags that the linker clears to differ. */
7998 if (elf_section_type (osec) == SHT_NULL
7999 && (osec->flags == isec->flags
8000 || (final_link
8001 && ((osec->flags ^ isec->flags)
8002 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8003 elf_section_type (osec) = elf_section_type (isec);
8004
8005 /* FIXME: Is this correct for all OS/PROC specific flags? */
8006 elf_section_flags (osec) = (elf_section_flags (isec)
8007 & (SHF_MASKOS | SHF_MASKPROC));
8008
8009 /* Copy sh_info from input for mbind section. */
8010 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8011 && elf_section_flags (isec) & SHF_GNU_MBIND)
8012 elf_section_data (osec)->this_hdr.sh_info
8013 = elf_section_data (isec)->this_hdr.sh_info;
8014
8015 /* Set things up for objcopy and relocatable link. The output
8016 SHT_GROUP section will have its elf_next_in_group pointing back
8017 to the input group members. Ignore linker created group section.
8018 See elfNN_ia64_object_p in elfxx-ia64.c. */
8019 if ((link_info == NULL
8020 || !link_info->resolve_section_groups)
8021 && (elf_sec_group (isec) == NULL
8022 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8023 {
8024 if (elf_section_flags (isec) & SHF_GROUP)
8025 elf_section_flags (osec) |= SHF_GROUP;
8026 elf_next_in_group (osec) = elf_next_in_group (isec);
8027 elf_section_data (osec)->group = elf_section_data (isec)->group;
8028 }
8029
8030 /* If not decompress, preserve SHF_COMPRESSED. */
8031 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8032 elf_section_flags (osec) |= (elf_section_flags (isec)
8033 & SHF_COMPRESSED);
8034
8035 ihdr = &elf_section_data (isec)->this_hdr;
8036
8037 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8038 don't use the output section of the linked-to section since it
8039 may be NULL at this point. */
8040 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8041 {
8042 ohdr = &elf_section_data (osec)->this_hdr;
8043 ohdr->sh_flags |= SHF_LINK_ORDER;
8044 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8045 }
8046
8047 osec->use_rela_p = isec->use_rela_p;
8048
8049 return true;
8050 }
8051
8052 /* Copy private section information. This copies over the entsize
8053 field, and sometimes the info field. */
8054
8055 bool
8056 _bfd_elf_copy_private_section_data (bfd *ibfd,
8057 asection *isec,
8058 bfd *obfd,
8059 asection *osec)
8060 {
8061 Elf_Internal_Shdr *ihdr, *ohdr;
8062
8063 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8064 || obfd->xvec->flavour != bfd_target_elf_flavour)
8065 return true;
8066
8067 ihdr = &elf_section_data (isec)->this_hdr;
8068 ohdr = &elf_section_data (osec)->this_hdr;
8069
8070 ohdr->sh_entsize = ihdr->sh_entsize;
8071
8072 if (ihdr->sh_type == SHT_SYMTAB
8073 || ihdr->sh_type == SHT_DYNSYM
8074 || ihdr->sh_type == SHT_GNU_verneed
8075 || ihdr->sh_type == SHT_GNU_verdef)
8076 ohdr->sh_info = ihdr->sh_info;
8077
8078 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8079 NULL);
8080 }
8081
8082 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8083 necessary if we are removing either the SHT_GROUP section or any of
8084 the group member sections. DISCARDED is the value that a section's
8085 output_section has if the section will be discarded, NULL when this
8086 function is called from objcopy, bfd_abs_section_ptr when called
8087 from the linker. */
8088
8089 bool
8090 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8091 {
8092 asection *isec;
8093
8094 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8095 if (elf_section_type (isec) == SHT_GROUP)
8096 {
8097 asection *first = elf_next_in_group (isec);
8098 asection *s = first;
8099 bfd_size_type removed = 0;
8100
8101 while (s != NULL)
8102 {
8103 /* If this member section is being output but the
8104 SHT_GROUP section is not, then clear the group info
8105 set up by _bfd_elf_copy_private_section_data. */
8106 if (s->output_section != discarded
8107 && isec->output_section == discarded)
8108 {
8109 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8110 elf_group_name (s->output_section) = NULL;
8111 }
8112 else
8113 {
8114 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8115 if (s->output_section == discarded
8116 && isec->output_section != discarded)
8117 {
8118 /* Conversely, if the member section is not being
8119 output but the SHT_GROUP section is, then adjust
8120 its size. */
8121 removed += 4;
8122 if (elf_sec->rel.hdr != NULL
8123 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8124 removed += 4;
8125 if (elf_sec->rela.hdr != NULL
8126 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8127 removed += 4;
8128 }
8129 else
8130 {
8131 /* Also adjust for zero-sized relocation member
8132 section. */
8133 if (elf_sec->rel.hdr != NULL
8134 && elf_sec->rel.hdr->sh_size == 0)
8135 removed += 4;
8136 if (elf_sec->rela.hdr != NULL
8137 && elf_sec->rela.hdr->sh_size == 0)
8138 removed += 4;
8139 }
8140 }
8141 s = elf_next_in_group (s);
8142 if (s == first)
8143 break;
8144 }
8145 if (removed != 0)
8146 {
8147 if (discarded != NULL)
8148 {
8149 /* If we've been called for ld -r, then we need to
8150 adjust the input section size. */
8151 if (isec->rawsize == 0)
8152 isec->rawsize = isec->size;
8153 isec->size = isec->rawsize - removed;
8154 if (isec->size <= 4)
8155 {
8156 isec->size = 0;
8157 isec->flags |= SEC_EXCLUDE;
8158 }
8159 }
8160 else if (isec->output_section != NULL)
8161 {
8162 /* Adjust the output section size when called from
8163 objcopy. */
8164 isec->output_section->size -= removed;
8165 if (isec->output_section->size <= 4)
8166 {
8167 isec->output_section->size = 0;
8168 isec->output_section->flags |= SEC_EXCLUDE;
8169 }
8170 }
8171 }
8172 }
8173
8174 return true;
8175 }
8176
8177 /* Copy private header information. */
8178
8179 bool
8180 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8181 {
8182 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8183 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8184 return true;
8185
8186 /* Copy over private BFD data if it has not already been copied.
8187 This must be done here, rather than in the copy_private_bfd_data
8188 entry point, because the latter is called after the section
8189 contents have been set, which means that the program headers have
8190 already been worked out. */
8191 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8192 {
8193 if (! copy_private_bfd_data (ibfd, obfd))
8194 return false;
8195 }
8196
8197 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8198 }
8199
8200 /* Copy private symbol information. If this symbol is in a section
8201 which we did not map into a BFD section, try to map the section
8202 index correctly. We use special macro definitions for the mapped
8203 section indices; these definitions are interpreted by the
8204 swap_out_syms function. */
8205
8206 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8207 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8208 #define MAP_STRTAB (SHN_HIOS + 3)
8209 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8210 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8211
8212 bool
8213 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8214 asymbol *isymarg,
8215 bfd *obfd,
8216 asymbol *osymarg)
8217 {
8218 elf_symbol_type *isym, *osym;
8219
8220 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8221 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8222 return true;
8223
8224 isym = elf_symbol_from (isymarg);
8225 osym = elf_symbol_from (osymarg);
8226
8227 if (isym != NULL
8228 && isym->internal_elf_sym.st_shndx != 0
8229 && osym != NULL
8230 && bfd_is_abs_section (isym->symbol.section))
8231 {
8232 unsigned int shndx;
8233
8234 shndx = isym->internal_elf_sym.st_shndx;
8235 if (shndx == elf_onesymtab (ibfd))
8236 shndx = MAP_ONESYMTAB;
8237 else if (shndx == elf_dynsymtab (ibfd))
8238 shndx = MAP_DYNSYMTAB;
8239 else if (shndx == elf_strtab_sec (ibfd))
8240 shndx = MAP_STRTAB;
8241 else if (shndx == elf_shstrtab_sec (ibfd))
8242 shndx = MAP_SHSTRTAB;
8243 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8244 shndx = MAP_SYM_SHNDX;
8245 osym->internal_elf_sym.st_shndx = shndx;
8246 }
8247
8248 return true;
8249 }
8250
8251 /* Swap out the symbols. */
8252
8253 static bool
8254 swap_out_syms (bfd *abfd,
8255 struct elf_strtab_hash **sttp,
8256 int relocatable_p,
8257 struct bfd_link_info *info)
8258 {
8259 const struct elf_backend_data *bed;
8260 unsigned int symcount;
8261 asymbol **syms;
8262 struct elf_strtab_hash *stt;
8263 Elf_Internal_Shdr *symtab_hdr;
8264 Elf_Internal_Shdr *symtab_shndx_hdr;
8265 Elf_Internal_Shdr *symstrtab_hdr;
8266 struct elf_sym_strtab *symstrtab;
8267 bfd_byte *outbound_syms;
8268 bfd_byte *outbound_shndx;
8269 unsigned long outbound_syms_index;
8270 unsigned int idx;
8271 unsigned int num_locals;
8272 size_t amt;
8273 bool name_local_sections;
8274
8275 if (!elf_map_symbols (abfd, &num_locals))
8276 return false;
8277
8278 /* Dump out the symtabs. */
8279 stt = _bfd_elf_strtab_init ();
8280 if (stt == NULL)
8281 return false;
8282
8283 bed = get_elf_backend_data (abfd);
8284 symcount = bfd_get_symcount (abfd);
8285 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8286 symtab_hdr->sh_type = SHT_SYMTAB;
8287 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8288 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8289 symtab_hdr->sh_info = num_locals + 1;
8290 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8291
8292 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8293 symstrtab_hdr->sh_type = SHT_STRTAB;
8294
8295 /* Allocate buffer to swap out the .strtab section. */
8296 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8297 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8298 {
8299 bfd_set_error (bfd_error_no_memory);
8300 _bfd_elf_strtab_free (stt);
8301 return false;
8302 }
8303
8304 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8305 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8306 {
8307 error_no_mem:
8308 bfd_set_error (bfd_error_no_memory);
8309 error_return:
8310 free (symstrtab);
8311 _bfd_elf_strtab_free (stt);
8312 return false;
8313 }
8314 symtab_hdr->contents = outbound_syms;
8315 outbound_syms_index = 0;
8316
8317 outbound_shndx = NULL;
8318
8319 if (elf_symtab_shndx_list (abfd))
8320 {
8321 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8322 if (symtab_shndx_hdr->sh_name != 0)
8323 {
8324 if (_bfd_mul_overflow (symcount + 1,
8325 sizeof (Elf_External_Sym_Shndx), &amt))
8326 goto error_no_mem;
8327 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8328 if (outbound_shndx == NULL)
8329 goto error_return;
8330
8331 symtab_shndx_hdr->contents = outbound_shndx;
8332 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8333 symtab_shndx_hdr->sh_size = amt;
8334 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8335 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8336 }
8337 /* FIXME: What about any other headers in the list ? */
8338 }
8339
8340 /* Now generate the data (for "contents"). */
8341 {
8342 /* Fill in zeroth symbol and swap it out. */
8343 Elf_Internal_Sym sym;
8344 sym.st_name = 0;
8345 sym.st_value = 0;
8346 sym.st_size = 0;
8347 sym.st_info = 0;
8348 sym.st_other = 0;
8349 sym.st_shndx = SHN_UNDEF;
8350 sym.st_target_internal = 0;
8351 symstrtab[0].sym = sym;
8352 symstrtab[0].dest_index = outbound_syms_index;
8353 outbound_syms_index++;
8354 }
8355
8356 name_local_sections
8357 = (bed->elf_backend_name_local_section_symbols
8358 && bed->elf_backend_name_local_section_symbols (abfd));
8359
8360 syms = bfd_get_outsymbols (abfd);
8361 for (idx = 0; idx < symcount;)
8362 {
8363 Elf_Internal_Sym sym;
8364 bfd_vma value = syms[idx]->value;
8365 elf_symbol_type *type_ptr;
8366 flagword flags = syms[idx]->flags;
8367 int type;
8368
8369 if (!name_local_sections
8370 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8371 {
8372 /* Local section symbols have no name. */
8373 sym.st_name = (unsigned long) -1;
8374 }
8375 else
8376 {
8377 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8378 to get the final offset for st_name. */
8379 sym.st_name
8380 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8381 false);
8382 if (sym.st_name == (unsigned long) -1)
8383 goto error_return;
8384 }
8385
8386 type_ptr = elf_symbol_from (syms[idx]);
8387
8388 if ((flags & BSF_SECTION_SYM) == 0
8389 && bfd_is_com_section (syms[idx]->section))
8390 {
8391 /* ELF common symbols put the alignment into the `value' field,
8392 and the size into the `size' field. This is backwards from
8393 how BFD handles it, so reverse it here. */
8394 sym.st_size = value;
8395 if (type_ptr == NULL
8396 || type_ptr->internal_elf_sym.st_value == 0)
8397 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8398 else
8399 sym.st_value = type_ptr->internal_elf_sym.st_value;
8400 sym.st_shndx = _bfd_elf_section_from_bfd_section
8401 (abfd, syms[idx]->section);
8402 }
8403 else
8404 {
8405 asection *sec = syms[idx]->section;
8406 unsigned int shndx;
8407
8408 if (sec->output_section)
8409 {
8410 value += sec->output_offset;
8411 sec = sec->output_section;
8412 }
8413
8414 /* Don't add in the section vma for relocatable output. */
8415 if (! relocatable_p)
8416 value += sec->vma;
8417 sym.st_value = value;
8418 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8419
8420 if (bfd_is_abs_section (sec)
8421 && type_ptr != NULL
8422 && type_ptr->internal_elf_sym.st_shndx != 0)
8423 {
8424 /* This symbol is in a real ELF section which we did
8425 not create as a BFD section. Undo the mapping done
8426 by copy_private_symbol_data. */
8427 shndx = type_ptr->internal_elf_sym.st_shndx;
8428 switch (shndx)
8429 {
8430 case MAP_ONESYMTAB:
8431 shndx = elf_onesymtab (abfd);
8432 break;
8433 case MAP_DYNSYMTAB:
8434 shndx = elf_dynsymtab (abfd);
8435 break;
8436 case MAP_STRTAB:
8437 shndx = elf_strtab_sec (abfd);
8438 break;
8439 case MAP_SHSTRTAB:
8440 shndx = elf_shstrtab_sec (abfd);
8441 break;
8442 case MAP_SYM_SHNDX:
8443 if (elf_symtab_shndx_list (abfd))
8444 shndx = elf_symtab_shndx_list (abfd)->ndx;
8445 break;
8446 case SHN_COMMON:
8447 case SHN_ABS:
8448 shndx = SHN_ABS;
8449 break;
8450 default:
8451 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8452 {
8453 if (bed->symbol_section_index)
8454 shndx = bed->symbol_section_index (abfd, type_ptr);
8455 /* Otherwise just leave the index alone. */
8456 }
8457 else
8458 {
8459 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8460 _bfd_error_handler (_("%pB: \
8461 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8462 abfd, shndx);
8463 shndx = SHN_ABS;
8464 }
8465 break;
8466 }
8467 }
8468 else
8469 {
8470 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8471
8472 if (shndx == SHN_BAD)
8473 {
8474 asection *sec2;
8475
8476 /* Writing this would be a hell of a lot easier if
8477 we had some decent documentation on bfd, and
8478 knew what to expect of the library, and what to
8479 demand of applications. For example, it
8480 appears that `objcopy' might not set the
8481 section of a symbol to be a section that is
8482 actually in the output file. */
8483 sec2 = bfd_get_section_by_name (abfd, sec->name);
8484 if (sec2 != NULL)
8485 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8486 if (shndx == SHN_BAD)
8487 {
8488 /* xgettext:c-format */
8489 _bfd_error_handler
8490 (_("unable to find equivalent output section"
8491 " for symbol '%s' from section '%s'"),
8492 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8493 sec->name);
8494 bfd_set_error (bfd_error_invalid_operation);
8495 goto error_return;
8496 }
8497 }
8498 }
8499
8500 sym.st_shndx = shndx;
8501 }
8502
8503 if ((flags & BSF_THREAD_LOCAL) != 0)
8504 type = STT_TLS;
8505 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8506 type = STT_GNU_IFUNC;
8507 else if ((flags & BSF_FUNCTION) != 0)
8508 type = STT_FUNC;
8509 else if ((flags & BSF_OBJECT) != 0)
8510 type = STT_OBJECT;
8511 else if ((flags & BSF_RELC) != 0)
8512 type = STT_RELC;
8513 else if ((flags & BSF_SRELC) != 0)
8514 type = STT_SRELC;
8515 else
8516 type = STT_NOTYPE;
8517
8518 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8519 type = STT_TLS;
8520
8521 /* Processor-specific types. */
8522 if (type_ptr != NULL
8523 && bed->elf_backend_get_symbol_type)
8524 type = ((*bed->elf_backend_get_symbol_type)
8525 (&type_ptr->internal_elf_sym, type));
8526
8527 if (flags & BSF_SECTION_SYM)
8528 {
8529 if (flags & BSF_GLOBAL)
8530 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8531 else
8532 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8533 }
8534 else if (bfd_is_com_section (syms[idx]->section))
8535 {
8536 if (type != STT_TLS)
8537 {
8538 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8539 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8540 ? STT_COMMON : STT_OBJECT);
8541 else
8542 type = ((flags & BSF_ELF_COMMON) != 0
8543 ? STT_COMMON : STT_OBJECT);
8544 }
8545 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8546 }
8547 else if (bfd_is_und_section (syms[idx]->section))
8548 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8549 ? STB_WEAK
8550 : STB_GLOBAL),
8551 type);
8552 else if (flags & BSF_FILE)
8553 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8554 else
8555 {
8556 int bind = STB_LOCAL;
8557
8558 if (flags & BSF_LOCAL)
8559 bind = STB_LOCAL;
8560 else if (flags & BSF_GNU_UNIQUE)
8561 bind = STB_GNU_UNIQUE;
8562 else if (flags & BSF_WEAK)
8563 bind = STB_WEAK;
8564 else if (flags & BSF_GLOBAL)
8565 bind = STB_GLOBAL;
8566
8567 sym.st_info = ELF_ST_INFO (bind, type);
8568 }
8569
8570 if (type_ptr != NULL)
8571 {
8572 sym.st_other = type_ptr->internal_elf_sym.st_other;
8573 sym.st_target_internal
8574 = type_ptr->internal_elf_sym.st_target_internal;
8575 }
8576 else
8577 {
8578 sym.st_other = 0;
8579 sym.st_target_internal = 0;
8580 }
8581
8582 idx++;
8583 symstrtab[idx].sym = sym;
8584 symstrtab[idx].dest_index = outbound_syms_index;
8585
8586 outbound_syms_index++;
8587 }
8588
8589 /* Finalize the .strtab section. */
8590 _bfd_elf_strtab_finalize (stt);
8591
8592 /* Swap out the .strtab section. */
8593 for (idx = 0; idx <= symcount; idx++)
8594 {
8595 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8596 if (elfsym->sym.st_name == (unsigned long) -1)
8597 elfsym->sym.st_name = 0;
8598 else
8599 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8600 elfsym->sym.st_name);
8601 if (info && info->callbacks->ctf_new_symbol)
8602 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8603 &elfsym->sym);
8604
8605 /* Inform the linker of the addition of this symbol. */
8606
8607 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8608 (outbound_syms
8609 + (elfsym->dest_index
8610 * bed->s->sizeof_sym)),
8611 NPTR_ADD (outbound_shndx,
8612 (elfsym->dest_index
8613 * sizeof (Elf_External_Sym_Shndx))));
8614 }
8615 free (symstrtab);
8616
8617 *sttp = stt;
8618 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8619 symstrtab_hdr->sh_type = SHT_STRTAB;
8620 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8621 symstrtab_hdr->sh_addr = 0;
8622 symstrtab_hdr->sh_entsize = 0;
8623 symstrtab_hdr->sh_link = 0;
8624 symstrtab_hdr->sh_info = 0;
8625 symstrtab_hdr->sh_addralign = 1;
8626
8627 return true;
8628 }
8629
8630 /* Return the number of bytes required to hold the symtab vector.
8631
8632 Note that we base it on the count plus 1, since we will null terminate
8633 the vector allocated based on this size. However, the ELF symbol table
8634 always has a dummy entry as symbol #0, so it ends up even. */
8635
8636 long
8637 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8638 {
8639 bfd_size_type symcount;
8640 long symtab_size;
8641 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8642
8643 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8644 if (symcount > LONG_MAX / sizeof (asymbol *))
8645 {
8646 bfd_set_error (bfd_error_file_too_big);
8647 return -1;
8648 }
8649 symtab_size = symcount * (sizeof (asymbol *));
8650 if (symcount == 0)
8651 symtab_size = sizeof (asymbol *);
8652 else if (!bfd_write_p (abfd))
8653 {
8654 ufile_ptr filesize = bfd_get_file_size (abfd);
8655
8656 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8657 {
8658 bfd_set_error (bfd_error_file_truncated);
8659 return -1;
8660 }
8661 }
8662
8663 return symtab_size;
8664 }
8665
8666 long
8667 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8668 {
8669 bfd_size_type symcount;
8670 long symtab_size;
8671 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8672
8673 if (elf_dynsymtab (abfd) == 0)
8674 {
8675 bfd_set_error (bfd_error_invalid_operation);
8676 return -1;
8677 }
8678
8679 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8680 if (symcount > LONG_MAX / sizeof (asymbol *))
8681 {
8682 bfd_set_error (bfd_error_file_too_big);
8683 return -1;
8684 }
8685 symtab_size = symcount * (sizeof (asymbol *));
8686 if (symcount == 0)
8687 symtab_size = sizeof (asymbol *);
8688 else if (!bfd_write_p (abfd))
8689 {
8690 ufile_ptr filesize = bfd_get_file_size (abfd);
8691
8692 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8693 {
8694 bfd_set_error (bfd_error_file_truncated);
8695 return -1;
8696 }
8697 }
8698
8699 return symtab_size;
8700 }
8701
8702 long
8703 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8704 {
8705 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8706 {
8707 /* Sanity check reloc section size. */
8708 struct bfd_elf_section_data *d = elf_section_data (asect);
8709 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8710 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8711 ufile_ptr filesize = bfd_get_file_size (abfd);
8712
8713 if (filesize != 0 && ext_rel_size > filesize)
8714 {
8715 bfd_set_error (bfd_error_file_truncated);
8716 return -1;
8717 }
8718 }
8719
8720 #if SIZEOF_LONG == SIZEOF_INT
8721 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8722 {
8723 bfd_set_error (bfd_error_file_too_big);
8724 return -1;
8725 }
8726 #endif
8727 return (asect->reloc_count + 1L) * sizeof (arelent *);
8728 }
8729
8730 /* Canonicalize the relocs. */
8731
8732 long
8733 _bfd_elf_canonicalize_reloc (bfd *abfd,
8734 sec_ptr section,
8735 arelent **relptr,
8736 asymbol **symbols)
8737 {
8738 arelent *tblptr;
8739 unsigned int i;
8740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8741
8742 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8743 return -1;
8744
8745 tblptr = section->relocation;
8746 for (i = 0; i < section->reloc_count; i++)
8747 *relptr++ = tblptr++;
8748
8749 *relptr = NULL;
8750
8751 return section->reloc_count;
8752 }
8753
8754 long
8755 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8756 {
8757 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8758 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8759
8760 if (symcount >= 0)
8761 abfd->symcount = symcount;
8762 return symcount;
8763 }
8764
8765 long
8766 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8767 asymbol **allocation)
8768 {
8769 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8770 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8771
8772 if (symcount >= 0)
8773 abfd->dynsymcount = symcount;
8774 return symcount;
8775 }
8776
8777 /* Return the size required for the dynamic reloc entries. Any loadable
8778 section that was actually installed in the BFD, and has type SHT_REL
8779 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8780 dynamic reloc section. */
8781
8782 long
8783 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8784 {
8785 bfd_size_type count, ext_rel_size;
8786 asection *s;
8787
8788 if (elf_dynsymtab (abfd) == 0)
8789 {
8790 bfd_set_error (bfd_error_invalid_operation);
8791 return -1;
8792 }
8793
8794 count = 1;
8795 ext_rel_size = 0;
8796 for (s = abfd->sections; s != NULL; s = s->next)
8797 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8798 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8799 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8800 {
8801 ext_rel_size += s->size;
8802 if (ext_rel_size < s->size)
8803 {
8804 bfd_set_error (bfd_error_file_truncated);
8805 return -1;
8806 }
8807 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8808 if (count > LONG_MAX / sizeof (arelent *))
8809 {
8810 bfd_set_error (bfd_error_file_too_big);
8811 return -1;
8812 }
8813 }
8814 if (count > 1 && !bfd_write_p (abfd))
8815 {
8816 /* Sanity check reloc section sizes. */
8817 ufile_ptr filesize = bfd_get_file_size (abfd);
8818 if (filesize != 0 && ext_rel_size > filesize)
8819 {
8820 bfd_set_error (bfd_error_file_truncated);
8821 return -1;
8822 }
8823 }
8824 return count * sizeof (arelent *);
8825 }
8826
8827 /* Canonicalize the dynamic relocation entries. Note that we return the
8828 dynamic relocations as a single block, although they are actually
8829 associated with particular sections; the interface, which was
8830 designed for SunOS style shared libraries, expects that there is only
8831 one set of dynamic relocs. Any loadable section that was actually
8832 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8833 dynamic symbol table, is considered to be a dynamic reloc section. */
8834
8835 long
8836 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8837 arelent **storage,
8838 asymbol **syms)
8839 {
8840 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8841 asection *s;
8842 long ret;
8843
8844 if (elf_dynsymtab (abfd) == 0)
8845 {
8846 bfd_set_error (bfd_error_invalid_operation);
8847 return -1;
8848 }
8849
8850 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8851 ret = 0;
8852 for (s = abfd->sections; s != NULL; s = s->next)
8853 {
8854 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8855 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8856 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8857 {
8858 arelent *p;
8859 long count, i;
8860
8861 if (! (*slurp_relocs) (abfd, s, syms, true))
8862 return -1;
8863 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8864 p = s->relocation;
8865 for (i = 0; i < count; i++)
8866 *storage++ = p++;
8867 ret += count;
8868 }
8869 }
8870
8871 *storage = NULL;
8872
8873 return ret;
8874 }
8875 \f
8876 /* Read in the version information. */
8877
8878 bool
8879 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8880 {
8881 bfd_byte *contents = NULL;
8882 unsigned int freeidx = 0;
8883 size_t amt;
8884
8885 if (elf_dynverref (abfd) != 0)
8886 {
8887 Elf_Internal_Shdr *hdr;
8888 Elf_External_Verneed *everneed;
8889 Elf_Internal_Verneed *iverneed;
8890 unsigned int i;
8891 bfd_byte *contents_end;
8892
8893 hdr = &elf_tdata (abfd)->dynverref_hdr;
8894
8895 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8896 {
8897 error_return_bad_verref:
8898 _bfd_error_handler
8899 (_("%pB: .gnu.version_r invalid entry"), abfd);
8900 bfd_set_error (bfd_error_bad_value);
8901 error_return_verref:
8902 elf_tdata (abfd)->verref = NULL;
8903 elf_tdata (abfd)->cverrefs = 0;
8904 goto error_return;
8905 }
8906
8907 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8908 goto error_return_verref;
8909 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8910 if (contents == NULL)
8911 goto error_return_verref;
8912
8913 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8914 {
8915 bfd_set_error (bfd_error_file_too_big);
8916 goto error_return_verref;
8917 }
8918 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
8919 if (elf_tdata (abfd)->verref == NULL)
8920 goto error_return_verref;
8921
8922 BFD_ASSERT (sizeof (Elf_External_Verneed)
8923 == sizeof (Elf_External_Vernaux));
8924 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8925 everneed = (Elf_External_Verneed *) contents;
8926 iverneed = elf_tdata (abfd)->verref;
8927 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8928 {
8929 Elf_External_Vernaux *evernaux;
8930 Elf_Internal_Vernaux *ivernaux;
8931 unsigned int j;
8932
8933 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8934
8935 iverneed->vn_bfd = abfd;
8936
8937 iverneed->vn_filename =
8938 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8939 iverneed->vn_file);
8940 if (iverneed->vn_filename == NULL)
8941 goto error_return_bad_verref;
8942
8943 if (iverneed->vn_cnt == 0)
8944 iverneed->vn_auxptr = NULL;
8945 else
8946 {
8947 if (_bfd_mul_overflow (iverneed->vn_cnt,
8948 sizeof (Elf_Internal_Vernaux), &amt))
8949 {
8950 bfd_set_error (bfd_error_file_too_big);
8951 goto error_return_verref;
8952 }
8953 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8954 bfd_alloc (abfd, amt);
8955 if (iverneed->vn_auxptr == NULL)
8956 goto error_return_verref;
8957 }
8958
8959 if (iverneed->vn_aux
8960 > (size_t) (contents_end - (bfd_byte *) everneed))
8961 goto error_return_bad_verref;
8962
8963 evernaux = ((Elf_External_Vernaux *)
8964 ((bfd_byte *) everneed + iverneed->vn_aux));
8965 ivernaux = iverneed->vn_auxptr;
8966 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8967 {
8968 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8969
8970 ivernaux->vna_nodename =
8971 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8972 ivernaux->vna_name);
8973 if (ivernaux->vna_nodename == NULL)
8974 goto error_return_bad_verref;
8975
8976 if (ivernaux->vna_other > freeidx)
8977 freeidx = ivernaux->vna_other;
8978
8979 ivernaux->vna_nextptr = NULL;
8980 if (ivernaux->vna_next == 0)
8981 {
8982 iverneed->vn_cnt = j + 1;
8983 break;
8984 }
8985 if (j + 1 < iverneed->vn_cnt)
8986 ivernaux->vna_nextptr = ivernaux + 1;
8987
8988 if (ivernaux->vna_next
8989 > (size_t) (contents_end - (bfd_byte *) evernaux))
8990 goto error_return_bad_verref;
8991
8992 evernaux = ((Elf_External_Vernaux *)
8993 ((bfd_byte *) evernaux + ivernaux->vna_next));
8994 }
8995
8996 iverneed->vn_nextref = NULL;
8997 if (iverneed->vn_next == 0)
8998 break;
8999 if (i + 1 < hdr->sh_info)
9000 iverneed->vn_nextref = iverneed + 1;
9001
9002 if (iverneed->vn_next
9003 > (size_t) (contents_end - (bfd_byte *) everneed))
9004 goto error_return_bad_verref;
9005
9006 everneed = ((Elf_External_Verneed *)
9007 ((bfd_byte *) everneed + iverneed->vn_next));
9008 }
9009 elf_tdata (abfd)->cverrefs = i;
9010
9011 free (contents);
9012 contents = NULL;
9013 }
9014
9015 if (elf_dynverdef (abfd) != 0)
9016 {
9017 Elf_Internal_Shdr *hdr;
9018 Elf_External_Verdef *everdef;
9019 Elf_Internal_Verdef *iverdef;
9020 Elf_Internal_Verdef *iverdefarr;
9021 Elf_Internal_Verdef iverdefmem;
9022 unsigned int i;
9023 unsigned int maxidx;
9024 bfd_byte *contents_end_def, *contents_end_aux;
9025
9026 hdr = &elf_tdata (abfd)->dynverdef_hdr;
9027
9028 if (hdr->sh_size < sizeof (Elf_External_Verdef))
9029 {
9030 error_return_bad_verdef:
9031 _bfd_error_handler
9032 (_("%pB: .gnu.version_d invalid entry"), abfd);
9033 bfd_set_error (bfd_error_bad_value);
9034 error_return_verdef:
9035 elf_tdata (abfd)->verdef = NULL;
9036 elf_tdata (abfd)->cverdefs = 0;
9037 goto error_return;
9038 }
9039
9040 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9041 goto error_return_verdef;
9042 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9043 if (contents == NULL)
9044 goto error_return_verdef;
9045
9046 BFD_ASSERT (sizeof (Elf_External_Verdef)
9047 >= sizeof (Elf_External_Verdaux));
9048 contents_end_def = contents + hdr->sh_size
9049 - sizeof (Elf_External_Verdef);
9050 contents_end_aux = contents + hdr->sh_size
9051 - sizeof (Elf_External_Verdaux);
9052
9053 /* We know the number of entries in the section but not the maximum
9054 index. Therefore we have to run through all entries and find
9055 the maximum. */
9056 everdef = (Elf_External_Verdef *) contents;
9057 maxidx = 0;
9058 for (i = 0; i < hdr->sh_info; ++i)
9059 {
9060 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9061
9062 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9063 goto error_return_bad_verdef;
9064 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9065 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9066
9067 if (iverdefmem.vd_next == 0)
9068 break;
9069
9070 if (iverdefmem.vd_next
9071 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9072 goto error_return_bad_verdef;
9073
9074 everdef = ((Elf_External_Verdef *)
9075 ((bfd_byte *) everdef + iverdefmem.vd_next));
9076 }
9077
9078 if (default_imported_symver)
9079 {
9080 if (freeidx > maxidx)
9081 maxidx = ++freeidx;
9082 else
9083 freeidx = ++maxidx;
9084 }
9085 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9086 {
9087 bfd_set_error (bfd_error_file_too_big);
9088 goto error_return_verdef;
9089 }
9090 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9091 if (elf_tdata (abfd)->verdef == NULL)
9092 goto error_return_verdef;
9093
9094 elf_tdata (abfd)->cverdefs = maxidx;
9095
9096 everdef = (Elf_External_Verdef *) contents;
9097 iverdefarr = elf_tdata (abfd)->verdef;
9098 for (i = 0; i < hdr->sh_info; i++)
9099 {
9100 Elf_External_Verdaux *everdaux;
9101 Elf_Internal_Verdaux *iverdaux;
9102 unsigned int j;
9103
9104 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9105
9106 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9107 goto error_return_bad_verdef;
9108
9109 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9110 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9111
9112 iverdef->vd_bfd = abfd;
9113
9114 if (iverdef->vd_cnt == 0)
9115 iverdef->vd_auxptr = NULL;
9116 else
9117 {
9118 if (_bfd_mul_overflow (iverdef->vd_cnt,
9119 sizeof (Elf_Internal_Verdaux), &amt))
9120 {
9121 bfd_set_error (bfd_error_file_too_big);
9122 goto error_return_verdef;
9123 }
9124 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9125 bfd_alloc (abfd, amt);
9126 if (iverdef->vd_auxptr == NULL)
9127 goto error_return_verdef;
9128 }
9129
9130 if (iverdef->vd_aux
9131 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9132 goto error_return_bad_verdef;
9133
9134 everdaux = ((Elf_External_Verdaux *)
9135 ((bfd_byte *) everdef + iverdef->vd_aux));
9136 iverdaux = iverdef->vd_auxptr;
9137 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9138 {
9139 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9140
9141 iverdaux->vda_nodename =
9142 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9143 iverdaux->vda_name);
9144 if (iverdaux->vda_nodename == NULL)
9145 goto error_return_bad_verdef;
9146
9147 iverdaux->vda_nextptr = NULL;
9148 if (iverdaux->vda_next == 0)
9149 {
9150 iverdef->vd_cnt = j + 1;
9151 break;
9152 }
9153 if (j + 1 < iverdef->vd_cnt)
9154 iverdaux->vda_nextptr = iverdaux + 1;
9155
9156 if (iverdaux->vda_next
9157 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9158 goto error_return_bad_verdef;
9159
9160 everdaux = ((Elf_External_Verdaux *)
9161 ((bfd_byte *) everdaux + iverdaux->vda_next));
9162 }
9163
9164 iverdef->vd_nodename = NULL;
9165 if (iverdef->vd_cnt)
9166 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9167
9168 iverdef->vd_nextdef = NULL;
9169 if (iverdef->vd_next == 0)
9170 break;
9171 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9172 iverdef->vd_nextdef = iverdef + 1;
9173
9174 everdef = ((Elf_External_Verdef *)
9175 ((bfd_byte *) everdef + iverdef->vd_next));
9176 }
9177
9178 free (contents);
9179 contents = NULL;
9180 }
9181 else if (default_imported_symver)
9182 {
9183 if (freeidx < 3)
9184 freeidx = 3;
9185 else
9186 freeidx++;
9187
9188 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9189 {
9190 bfd_set_error (bfd_error_file_too_big);
9191 goto error_return;
9192 }
9193 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9194 if (elf_tdata (abfd)->verdef == NULL)
9195 goto error_return;
9196
9197 elf_tdata (abfd)->cverdefs = freeidx;
9198 }
9199
9200 /* Create a default version based on the soname. */
9201 if (default_imported_symver)
9202 {
9203 Elf_Internal_Verdef *iverdef;
9204 Elf_Internal_Verdaux *iverdaux;
9205
9206 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9207
9208 iverdef->vd_version = VER_DEF_CURRENT;
9209 iverdef->vd_flags = 0;
9210 iverdef->vd_ndx = freeidx;
9211 iverdef->vd_cnt = 1;
9212
9213 iverdef->vd_bfd = abfd;
9214
9215 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9216 if (iverdef->vd_nodename == NULL)
9217 goto error_return_verdef;
9218 iverdef->vd_nextdef = NULL;
9219 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9220 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9221 if (iverdef->vd_auxptr == NULL)
9222 goto error_return_verdef;
9223
9224 iverdaux = iverdef->vd_auxptr;
9225 iverdaux->vda_nodename = iverdef->vd_nodename;
9226 }
9227
9228 return true;
9229
9230 error_return:
9231 free (contents);
9232 return false;
9233 }
9234 \f
9235 asymbol *
9236 _bfd_elf_make_empty_symbol (bfd *abfd)
9237 {
9238 elf_symbol_type *newsym;
9239
9240 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9241 if (!newsym)
9242 return NULL;
9243 newsym->symbol.the_bfd = abfd;
9244 return &newsym->symbol;
9245 }
9246
9247 void
9248 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9249 asymbol *symbol,
9250 symbol_info *ret)
9251 {
9252 bfd_symbol_info (symbol, ret);
9253 }
9254
9255 /* Return whether a symbol name implies a local symbol. Most targets
9256 use this function for the is_local_label_name entry point, but some
9257 override it. */
9258
9259 bool
9260 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9261 const char *name)
9262 {
9263 /* Normal local symbols start with ``.L''. */
9264 if (name[0] == '.' && name[1] == 'L')
9265 return true;
9266
9267 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9268 DWARF debugging symbols starting with ``..''. */
9269 if (name[0] == '.' && name[1] == '.')
9270 return true;
9271
9272 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9273 emitting DWARF debugging output. I suspect this is actually a
9274 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9275 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9276 underscore to be emitted on some ELF targets). For ease of use,
9277 we treat such symbols as local. */
9278 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9279 return true;
9280
9281 /* Treat assembler generated fake symbols, dollar local labels and
9282 forward-backward labels (aka local labels) as locals.
9283 These labels have the form:
9284
9285 L0^A.* (fake symbols)
9286
9287 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9288
9289 Versions which start with .L will have already been matched above,
9290 so we only need to match the rest. */
9291 if (name[0] == 'L' && ISDIGIT (name[1]))
9292 {
9293 bool ret = false;
9294 const char * p;
9295 char c;
9296
9297 for (p = name + 2; (c = *p); p++)
9298 {
9299 if (c == 1 || c == 2)
9300 {
9301 if (c == 1 && p == name + 2)
9302 /* A fake symbol. */
9303 return true;
9304
9305 /* FIXME: We are being paranoid here and treating symbols like
9306 L0^Bfoo as if there were non-local, on the grounds that the
9307 assembler will never generate them. But can any symbol
9308 containing an ASCII value in the range 1-31 ever be anything
9309 other than some kind of local ? */
9310 ret = true;
9311 }
9312
9313 if (! ISDIGIT (c))
9314 {
9315 ret = false;
9316 break;
9317 }
9318 }
9319 return ret;
9320 }
9321
9322 return false;
9323 }
9324
9325 alent *
9326 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9327 asymbol *symbol ATTRIBUTE_UNUSED)
9328 {
9329 abort ();
9330 return NULL;
9331 }
9332
9333 bool
9334 _bfd_elf_set_arch_mach (bfd *abfd,
9335 enum bfd_architecture arch,
9336 unsigned long machine)
9337 {
9338 /* If this isn't the right architecture for this backend, and this
9339 isn't the generic backend, fail. */
9340 if (arch != get_elf_backend_data (abfd)->arch
9341 && arch != bfd_arch_unknown
9342 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9343 return false;
9344
9345 return bfd_default_set_arch_mach (abfd, arch, machine);
9346 }
9347
9348 /* Find the nearest line to a particular section and offset,
9349 for error reporting. */
9350
9351 bool
9352 _bfd_elf_find_nearest_line (bfd *abfd,
9353 asymbol **symbols,
9354 asection *section,
9355 bfd_vma offset,
9356 const char **filename_ptr,
9357 const char **functionname_ptr,
9358 unsigned int *line_ptr,
9359 unsigned int *discriminator_ptr)
9360 {
9361 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9362 offset, filename_ptr,
9363 functionname_ptr, line_ptr,
9364 discriminator_ptr);
9365 }
9366
9367 /* Find the nearest line to a particular section and offset,
9368 for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9369 can be optionally specified. */
9370
9371 bool
9372 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9373 const char *alt_filename,
9374 asymbol **symbols,
9375 asection *section,
9376 bfd_vma offset,
9377 const char **filename_ptr,
9378 const char **functionname_ptr,
9379 unsigned int *line_ptr,
9380 unsigned int *discriminator_ptr)
9381 {
9382 bool found;
9383
9384 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9385 section, offset, filename_ptr,
9386 functionname_ptr, line_ptr,
9387 discriminator_ptr,
9388 dwarf_debug_sections,
9389 &elf_tdata (abfd)->dwarf2_find_line_info))
9390 return true;
9391
9392 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9393 filename_ptr, functionname_ptr, line_ptr))
9394 {
9395 if (!*functionname_ptr)
9396 _bfd_elf_find_function (abfd, symbols, section, offset,
9397 *filename_ptr ? NULL : filename_ptr,
9398 functionname_ptr);
9399 return true;
9400 }
9401
9402 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9403 &found, filename_ptr,
9404 functionname_ptr, line_ptr,
9405 &elf_tdata (abfd)->line_info))
9406 return false;
9407 if (found && (*functionname_ptr || *line_ptr))
9408 return true;
9409
9410 if (symbols == NULL)
9411 return false;
9412
9413 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9414 filename_ptr, functionname_ptr))
9415 return false;
9416
9417 *line_ptr = 0;
9418 return true;
9419 }
9420
9421 /* Find the line for a symbol. */
9422
9423 bool
9424 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9425 const char **filename_ptr, unsigned int *line_ptr)
9426 {
9427 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9428 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9429 filename_ptr, NULL, line_ptr, NULL,
9430 dwarf_debug_sections,
9431 &tdata->dwarf2_find_line_info);
9432 }
9433
9434 /* After a call to bfd_find_nearest_line, successive calls to
9435 bfd_find_inliner_info can be used to get source information about
9436 each level of function inlining that terminated at the address
9437 passed to bfd_find_nearest_line. Currently this is only supported
9438 for DWARF2 with appropriate DWARF3 extensions. */
9439
9440 bool
9441 _bfd_elf_find_inliner_info (bfd *abfd,
9442 const char **filename_ptr,
9443 const char **functionname_ptr,
9444 unsigned int *line_ptr)
9445 {
9446 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9447 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9448 functionname_ptr, line_ptr,
9449 &tdata->dwarf2_find_line_info);
9450 }
9451
9452 int
9453 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9454 {
9455 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9456 int ret = bed->s->sizeof_ehdr;
9457
9458 if (!bfd_link_relocatable (info))
9459 {
9460 bfd_size_type phdr_size = elf_program_header_size (abfd);
9461
9462 if (phdr_size == (bfd_size_type) -1)
9463 {
9464 struct elf_segment_map *m;
9465
9466 phdr_size = 0;
9467 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9468 phdr_size += bed->s->sizeof_phdr;
9469
9470 if (phdr_size == 0)
9471 phdr_size = get_program_header_size (abfd, info);
9472 }
9473
9474 elf_program_header_size (abfd) = phdr_size;
9475 ret += phdr_size;
9476 }
9477
9478 return ret;
9479 }
9480
9481 bool
9482 _bfd_elf_set_section_contents (bfd *abfd,
9483 sec_ptr section,
9484 const void *location,
9485 file_ptr offset,
9486 bfd_size_type count)
9487 {
9488 Elf_Internal_Shdr *hdr;
9489
9490 if (! abfd->output_has_begun
9491 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9492 return false;
9493
9494 if (!count)
9495 return true;
9496
9497 hdr = &elf_section_data (section)->this_hdr;
9498 if (hdr->sh_offset == (file_ptr) -1)
9499 {
9500 unsigned char *contents;
9501
9502 if (bfd_section_is_ctf (section))
9503 /* Nothing to do with this section: the contents are generated
9504 later. */
9505 return true;
9506
9507 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9508 {
9509 _bfd_error_handler
9510 (_("%pB:%pA: error: attempting to write"
9511 " into an unallocated compressed section"),
9512 abfd, section);
9513 bfd_set_error (bfd_error_invalid_operation);
9514 return false;
9515 }
9516
9517 if ((offset + count) > hdr->sh_size)
9518 {
9519 _bfd_error_handler
9520 (_("%pB:%pA: error: attempting to write"
9521 " over the end of the section"),
9522 abfd, section);
9523
9524 bfd_set_error (bfd_error_invalid_operation);
9525 return false;
9526 }
9527
9528 contents = hdr->contents;
9529 if (contents == NULL)
9530 {
9531 _bfd_error_handler
9532 (_("%pB:%pA: error: attempting to write"
9533 " section into an empty buffer"),
9534 abfd, section);
9535
9536 bfd_set_error (bfd_error_invalid_operation);
9537 return false;
9538 }
9539
9540 memcpy (contents + offset, location, count);
9541 return true;
9542 }
9543
9544 return _bfd_generic_set_section_contents (abfd, section,
9545 location, offset, count);
9546 }
9547
9548 bool
9549 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9550 arelent *cache_ptr ATTRIBUTE_UNUSED,
9551 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9552 {
9553 abort ();
9554 return false;
9555 }
9556
9557 /* Try to convert a non-ELF reloc into an ELF one. */
9558
9559 bool
9560 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9561 {
9562 /* Check whether we really have an ELF howto. */
9563
9564 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9565 {
9566 bfd_reloc_code_real_type code;
9567 reloc_howto_type *howto;
9568
9569 /* Alien reloc: Try to determine its type to replace it with an
9570 equivalent ELF reloc. */
9571
9572 if (areloc->howto->pc_relative)
9573 {
9574 switch (areloc->howto->bitsize)
9575 {
9576 case 8:
9577 code = BFD_RELOC_8_PCREL;
9578 break;
9579 case 12:
9580 code = BFD_RELOC_12_PCREL;
9581 break;
9582 case 16:
9583 code = BFD_RELOC_16_PCREL;
9584 break;
9585 case 24:
9586 code = BFD_RELOC_24_PCREL;
9587 break;
9588 case 32:
9589 code = BFD_RELOC_32_PCREL;
9590 break;
9591 case 64:
9592 code = BFD_RELOC_64_PCREL;
9593 break;
9594 default:
9595 goto fail;
9596 }
9597
9598 howto = bfd_reloc_type_lookup (abfd, code);
9599
9600 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9601 {
9602 if (howto->pcrel_offset)
9603 areloc->addend += areloc->address;
9604 else
9605 areloc->addend -= areloc->address; /* addend is unsigned!! */
9606 }
9607 }
9608 else
9609 {
9610 switch (areloc->howto->bitsize)
9611 {
9612 case 8:
9613 code = BFD_RELOC_8;
9614 break;
9615 case 14:
9616 code = BFD_RELOC_14;
9617 break;
9618 case 16:
9619 code = BFD_RELOC_16;
9620 break;
9621 case 26:
9622 code = BFD_RELOC_26;
9623 break;
9624 case 32:
9625 code = BFD_RELOC_32;
9626 break;
9627 case 64:
9628 code = BFD_RELOC_64;
9629 break;
9630 default:
9631 goto fail;
9632 }
9633
9634 howto = bfd_reloc_type_lookup (abfd, code);
9635 }
9636
9637 if (howto)
9638 areloc->howto = howto;
9639 else
9640 goto fail;
9641 }
9642
9643 return true;
9644
9645 fail:
9646 /* xgettext:c-format */
9647 _bfd_error_handler (_("%pB: %s unsupported"),
9648 abfd, areloc->howto->name);
9649 bfd_set_error (bfd_error_sorry);
9650 return false;
9651 }
9652
9653 bool
9654 _bfd_elf_close_and_cleanup (bfd *abfd)
9655 {
9656 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9657 if (tdata != NULL
9658 && (bfd_get_format (abfd) == bfd_object
9659 || bfd_get_format (abfd) == bfd_core))
9660 {
9661 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9662 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9663 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9664 _bfd_stab_cleanup (abfd, &tdata->line_info);
9665 }
9666
9667 return _bfd_generic_close_and_cleanup (abfd);
9668 }
9669
9670 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9671 in the relocation's offset. Thus we cannot allow any sort of sanity
9672 range-checking to interfere. There is nothing else to do in processing
9673 this reloc. */
9674
9675 bfd_reloc_status_type
9676 _bfd_elf_rel_vtable_reloc_fn
9677 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9678 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9679 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9680 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9681 {
9682 return bfd_reloc_ok;
9683 }
9684 \f
9685 /* Elf core file support. Much of this only works on native
9686 toolchains, since we rely on knowing the
9687 machine-dependent procfs structure in order to pick
9688 out details about the corefile. */
9689
9690 #ifdef HAVE_SYS_PROCFS_H
9691 # include <sys/procfs.h>
9692 #endif
9693
9694 /* Return a PID that identifies a "thread" for threaded cores, or the
9695 PID of the main process for non-threaded cores. */
9696
9697 static int
9698 elfcore_make_pid (bfd *abfd)
9699 {
9700 int pid;
9701
9702 pid = elf_tdata (abfd)->core->lwpid;
9703 if (pid == 0)
9704 pid = elf_tdata (abfd)->core->pid;
9705
9706 return pid;
9707 }
9708
9709 /* If there isn't a section called NAME, make one, using data from
9710 SECT. Note, this function will generate a reference to NAME, so
9711 you shouldn't deallocate or overwrite it. */
9712
9713 static bool
9714 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9715 {
9716 asection *sect2;
9717
9718 if (bfd_get_section_by_name (abfd, name) != NULL)
9719 return true;
9720
9721 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9722 if (sect2 == NULL)
9723 return false;
9724
9725 sect2->size = sect->size;
9726 sect2->filepos = sect->filepos;
9727 sect2->alignment_power = sect->alignment_power;
9728 return true;
9729 }
9730
9731 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9732 actually creates up to two pseudosections:
9733 - For the single-threaded case, a section named NAME, unless
9734 such a section already exists.
9735 - For the multi-threaded case, a section named "NAME/PID", where
9736 PID is elfcore_make_pid (abfd).
9737 Both pseudosections have identical contents. */
9738 bool
9739 _bfd_elfcore_make_pseudosection (bfd *abfd,
9740 char *name,
9741 size_t size,
9742 ufile_ptr filepos)
9743 {
9744 char buf[100];
9745 char *threaded_name;
9746 size_t len;
9747 asection *sect;
9748
9749 /* Build the section name. */
9750
9751 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9752 len = strlen (buf) + 1;
9753 threaded_name = (char *) bfd_alloc (abfd, len);
9754 if (threaded_name == NULL)
9755 return false;
9756 memcpy (threaded_name, buf, len);
9757
9758 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9759 SEC_HAS_CONTENTS);
9760 if (sect == NULL)
9761 return false;
9762 sect->size = size;
9763 sect->filepos = filepos;
9764 sect->alignment_power = 2;
9765
9766 return elfcore_maybe_make_sect (abfd, name, sect);
9767 }
9768
9769 static bool
9770 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9771 size_t offs)
9772 {
9773 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9774 SEC_HAS_CONTENTS);
9775
9776 if (sect == NULL)
9777 return false;
9778
9779 sect->size = note->descsz - offs;
9780 sect->filepos = note->descpos + offs;
9781 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9782
9783 return true;
9784 }
9785
9786 /* prstatus_t exists on:
9787 solaris 2.5+
9788 linux 2.[01] + glibc
9789 unixware 4.2
9790 */
9791
9792 #if defined (HAVE_PRSTATUS_T)
9793
9794 static bool
9795 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9796 {
9797 size_t size;
9798 int offset;
9799
9800 if (note->descsz == sizeof (prstatus_t))
9801 {
9802 prstatus_t prstat;
9803
9804 size = sizeof (prstat.pr_reg);
9805 offset = offsetof (prstatus_t, pr_reg);
9806 memcpy (&prstat, note->descdata, sizeof (prstat));
9807
9808 /* Do not overwrite the core signal if it
9809 has already been set by another thread. */
9810 if (elf_tdata (abfd)->core->signal == 0)
9811 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9812 if (elf_tdata (abfd)->core->pid == 0)
9813 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9814
9815 /* pr_who exists on:
9816 solaris 2.5+
9817 unixware 4.2
9818 pr_who doesn't exist on:
9819 linux 2.[01]
9820 */
9821 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9822 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9823 #else
9824 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9825 #endif
9826 }
9827 #if defined (HAVE_PRSTATUS32_T)
9828 else if (note->descsz == sizeof (prstatus32_t))
9829 {
9830 /* 64-bit host, 32-bit corefile */
9831 prstatus32_t prstat;
9832
9833 size = sizeof (prstat.pr_reg);
9834 offset = offsetof (prstatus32_t, pr_reg);
9835 memcpy (&prstat, note->descdata, sizeof (prstat));
9836
9837 /* Do not overwrite the core signal if it
9838 has already been set by another thread. */
9839 if (elf_tdata (abfd)->core->signal == 0)
9840 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9841 if (elf_tdata (abfd)->core->pid == 0)
9842 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9843
9844 /* pr_who exists on:
9845 solaris 2.5+
9846 unixware 4.2
9847 pr_who doesn't exist on:
9848 linux 2.[01]
9849 */
9850 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9851 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9852 #else
9853 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9854 #endif
9855 }
9856 #endif /* HAVE_PRSTATUS32_T */
9857 else
9858 {
9859 /* Fail - we don't know how to handle any other
9860 note size (ie. data object type). */
9861 return true;
9862 }
9863
9864 /* Make a ".reg/999" section and a ".reg" section. */
9865 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9866 size, note->descpos + offset);
9867 }
9868 #endif /* defined (HAVE_PRSTATUS_T) */
9869
9870 /* Create a pseudosection containing the exact contents of NOTE. */
9871 static bool
9872 elfcore_make_note_pseudosection (bfd *abfd,
9873 char *name,
9874 Elf_Internal_Note *note)
9875 {
9876 return _bfd_elfcore_make_pseudosection (abfd, name,
9877 note->descsz, note->descpos);
9878 }
9879
9880 /* There isn't a consistent prfpregset_t across platforms,
9881 but it doesn't matter, because we don't have to pick this
9882 data structure apart. */
9883
9884 static bool
9885 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9886 {
9887 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9888 }
9889
9890 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9891 type of NT_PRXFPREG. Just include the whole note's contents
9892 literally. */
9893
9894 static bool
9895 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9896 {
9897 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9898 }
9899
9900 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9901 with a note type of NT_X86_XSTATE. Just include the whole note's
9902 contents literally. */
9903
9904 static bool
9905 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9906 {
9907 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9908 }
9909
9910 static bool
9911 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9912 {
9913 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9914 }
9915
9916 static bool
9917 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9918 {
9919 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9920 }
9921
9922 static bool
9923 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9924 {
9925 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9926 }
9927
9928 static bool
9929 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9930 {
9931 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9932 }
9933
9934 static bool
9935 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9936 {
9937 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9938 }
9939
9940 static bool
9941 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9942 {
9943 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9944 }
9945
9946 static bool
9947 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9948 {
9949 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9950 }
9951
9952 static bool
9953 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9954 {
9955 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9956 }
9957
9958 static bool
9959 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9960 {
9961 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9962 }
9963
9964 static bool
9965 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9966 {
9967 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9968 }
9969
9970 static bool
9971 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9972 {
9973 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9974 }
9975
9976 static bool
9977 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9978 {
9979 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9980 }
9981
9982 static bool
9983 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9984 {
9985 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9986 }
9987
9988 static bool
9989 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9990 {
9991 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9992 }
9993
9994 static bool
9995 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9996 {
9997 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9998 }
9999
10000 static bool
10001 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10002 {
10003 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
10004 }
10005
10006 static bool
10007 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10008 {
10009 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
10010 }
10011
10012 static bool
10013 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10014 {
10015 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
10016 }
10017
10018 static bool
10019 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10020 {
10021 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
10022 }
10023
10024 static bool
10025 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10026 {
10027 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
10028 }
10029
10030 static bool
10031 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10032 {
10033 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
10034 }
10035
10036 static bool
10037 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10038 {
10039 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
10040 }
10041
10042 static bool
10043 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10044 {
10045 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
10046 }
10047
10048 static bool
10049 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10050 {
10051 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10052 }
10053
10054 static bool
10055 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10056 {
10057 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10058 }
10059
10060 static bool
10061 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10062 {
10063 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10064 }
10065
10066 static bool
10067 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10068 {
10069 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10070 }
10071
10072 static bool
10073 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10074 {
10075 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10076 }
10077
10078 static bool
10079 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10080 {
10081 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10082 }
10083
10084 static bool
10085 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10086 {
10087 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10088 }
10089
10090 static bool
10091 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10092 {
10093 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10094 }
10095
10096 static bool
10097 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10098 {
10099 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10100 }
10101
10102 static bool
10103 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10104 {
10105 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10106 }
10107
10108 static bool
10109 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10110 {
10111 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10112 }
10113
10114 static bool
10115 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10116 {
10117 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10118 note);
10119 }
10120
10121 static bool
10122 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10123 {
10124 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10125 }
10126
10127 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10128 successful otherwise, return FALSE. */
10129
10130 static bool
10131 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10132 {
10133 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10134 }
10135
10136 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10137 successful otherwise, return FALSE. */
10138
10139 static bool
10140 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10141 {
10142 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10143 }
10144
10145 static bool
10146 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10147 {
10148 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10149 }
10150
10151 static bool
10152 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10153 {
10154 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10155 }
10156
10157 static bool
10158 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10159 {
10160 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10161 }
10162
10163 static bool
10164 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10165 {
10166 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10167 }
10168
10169 #if defined (HAVE_PRPSINFO_T)
10170 typedef prpsinfo_t elfcore_psinfo_t;
10171 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10172 typedef prpsinfo32_t elfcore_psinfo32_t;
10173 #endif
10174 #endif
10175
10176 #if defined (HAVE_PSINFO_T)
10177 typedef psinfo_t elfcore_psinfo_t;
10178 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10179 typedef psinfo32_t elfcore_psinfo32_t;
10180 #endif
10181 #endif
10182
10183 /* return a malloc'ed copy of a string at START which is at
10184 most MAX bytes long, possibly without a terminating '\0'.
10185 the copy will always have a terminating '\0'. */
10186
10187 char *
10188 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10189 {
10190 char *dups;
10191 char *end = (char *) memchr (start, '\0', max);
10192 size_t len;
10193
10194 if (end == NULL)
10195 len = max;
10196 else
10197 len = end - start;
10198
10199 dups = (char *) bfd_alloc (abfd, len + 1);
10200 if (dups == NULL)
10201 return NULL;
10202
10203 memcpy (dups, start, len);
10204 dups[len] = '\0';
10205
10206 return dups;
10207 }
10208
10209 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10210 static bool
10211 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10212 {
10213 if (note->descsz == sizeof (elfcore_psinfo_t))
10214 {
10215 elfcore_psinfo_t psinfo;
10216
10217 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10218
10219 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10220 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10221 #endif
10222 elf_tdata (abfd)->core->program
10223 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10224 sizeof (psinfo.pr_fname));
10225
10226 elf_tdata (abfd)->core->command
10227 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10228 sizeof (psinfo.pr_psargs));
10229 }
10230 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10231 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10232 {
10233 /* 64-bit host, 32-bit corefile */
10234 elfcore_psinfo32_t psinfo;
10235
10236 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10237
10238 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10239 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10240 #endif
10241 elf_tdata (abfd)->core->program
10242 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10243 sizeof (psinfo.pr_fname));
10244
10245 elf_tdata (abfd)->core->command
10246 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10247 sizeof (psinfo.pr_psargs));
10248 }
10249 #endif
10250
10251 else
10252 {
10253 /* Fail - we don't know how to handle any other
10254 note size (ie. data object type). */
10255 return true;
10256 }
10257
10258 /* Note that for some reason, a spurious space is tacked
10259 onto the end of the args in some (at least one anyway)
10260 implementations, so strip it off if it exists. */
10261
10262 {
10263 char *command = elf_tdata (abfd)->core->command;
10264 int n = strlen (command);
10265
10266 if (0 < n && command[n - 1] == ' ')
10267 command[n - 1] = '\0';
10268 }
10269
10270 return true;
10271 }
10272 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10273
10274 #if defined (HAVE_PSTATUS_T)
10275 static bool
10276 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10277 {
10278 if (note->descsz == sizeof (pstatus_t)
10279 #if defined (HAVE_PXSTATUS_T)
10280 || note->descsz == sizeof (pxstatus_t)
10281 #endif
10282 )
10283 {
10284 pstatus_t pstat;
10285
10286 memcpy (&pstat, note->descdata, sizeof (pstat));
10287
10288 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10289 }
10290 #if defined (HAVE_PSTATUS32_T)
10291 else if (note->descsz == sizeof (pstatus32_t))
10292 {
10293 /* 64-bit host, 32-bit corefile */
10294 pstatus32_t pstat;
10295
10296 memcpy (&pstat, note->descdata, sizeof (pstat));
10297
10298 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10299 }
10300 #endif
10301 /* Could grab some more details from the "representative"
10302 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10303 NT_LWPSTATUS note, presumably. */
10304
10305 return true;
10306 }
10307 #endif /* defined (HAVE_PSTATUS_T) */
10308
10309 #if defined (HAVE_LWPSTATUS_T)
10310 static bool
10311 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10312 {
10313 lwpstatus_t lwpstat;
10314 char buf[100];
10315 char *name;
10316 size_t len;
10317 asection *sect;
10318
10319 if (note->descsz != sizeof (lwpstat)
10320 #if defined (HAVE_LWPXSTATUS_T)
10321 && note->descsz != sizeof (lwpxstatus_t)
10322 #endif
10323 )
10324 return true;
10325
10326 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10327
10328 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10329 /* Do not overwrite the core signal if it has already been set by
10330 another thread. */
10331 if (elf_tdata (abfd)->core->signal == 0)
10332 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10333
10334 /* Make a ".reg/999" section. */
10335
10336 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10337 len = strlen (buf) + 1;
10338 name = bfd_alloc (abfd, len);
10339 if (name == NULL)
10340 return false;
10341 memcpy (name, buf, len);
10342
10343 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10344 if (sect == NULL)
10345 return false;
10346
10347 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10348 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10349 sect->filepos = note->descpos
10350 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10351 #endif
10352
10353 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10354 sect->size = sizeof (lwpstat.pr_reg);
10355 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10356 #endif
10357
10358 sect->alignment_power = 2;
10359
10360 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10361 return false;
10362
10363 /* Make a ".reg2/999" section */
10364
10365 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10366 len = strlen (buf) + 1;
10367 name = bfd_alloc (abfd, len);
10368 if (name == NULL)
10369 return false;
10370 memcpy (name, buf, len);
10371
10372 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10373 if (sect == NULL)
10374 return false;
10375
10376 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10377 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10378 sect->filepos = note->descpos
10379 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10380 #endif
10381
10382 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10383 sect->size = sizeof (lwpstat.pr_fpreg);
10384 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10385 #endif
10386
10387 sect->alignment_power = 2;
10388
10389 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10390 }
10391 #endif /* defined (HAVE_LWPSTATUS_T) */
10392
10393 /* These constants, and the structure offsets used below, are defined by
10394 Cygwin's core_dump.h */
10395 #define NOTE_INFO_PROCESS 1
10396 #define NOTE_INFO_THREAD 2
10397 #define NOTE_INFO_MODULE 3
10398 #define NOTE_INFO_MODULE64 4
10399
10400 static bool
10401 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10402 {
10403 char buf[30];
10404 char *name;
10405 size_t len;
10406 unsigned int name_size;
10407 asection *sect;
10408 unsigned int type;
10409 int is_active_thread;
10410 bfd_vma base_addr;
10411
10412 if (note->descsz < 4)
10413 return true;
10414
10415 if (! startswith (note->namedata, "win32"))
10416 return true;
10417
10418 type = bfd_get_32 (abfd, note->descdata);
10419
10420 struct
10421 {
10422 const char *type_name;
10423 unsigned long min_size;
10424 } size_check[] =
10425 {
10426 { "NOTE_INFO_PROCESS", 12 },
10427 { "NOTE_INFO_THREAD", 12 },
10428 { "NOTE_INFO_MODULE", 12 },
10429 { "NOTE_INFO_MODULE64", 16 },
10430 };
10431
10432 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10433 return true;
10434
10435 if (note->descsz < size_check[type - 1].min_size)
10436 {
10437 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10438 " is too small"),
10439 abfd, size_check[type - 1].type_name, note->descsz);
10440 return true;
10441 }
10442
10443 switch (type)
10444 {
10445 case NOTE_INFO_PROCESS:
10446 /* FIXME: need to add ->core->command. */
10447 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10448 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10449 break;
10450
10451 case NOTE_INFO_THREAD:
10452 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10453 structure. */
10454 /* thread_info.tid */
10455 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10456
10457 len = strlen (buf) + 1;
10458 name = (char *) bfd_alloc (abfd, len);
10459 if (name == NULL)
10460 return false;
10461
10462 memcpy (name, buf, len);
10463
10464 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10465 if (sect == NULL)
10466 return false;
10467
10468 /* sizeof (thread_info.thread_context) */
10469 sect->size = note->descsz - 12;
10470 /* offsetof (thread_info.thread_context) */
10471 sect->filepos = note->descpos + 12;
10472 sect->alignment_power = 2;
10473
10474 /* thread_info.is_active_thread */
10475 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10476
10477 if (is_active_thread)
10478 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10479 return false;
10480 break;
10481
10482 case NOTE_INFO_MODULE:
10483 case NOTE_INFO_MODULE64:
10484 /* Make a ".module/xxxxxxxx" section. */
10485 if (type == NOTE_INFO_MODULE)
10486 {
10487 /* module_info.base_address */
10488 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10489 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10490 /* module_info.module_name_size */
10491 name_size = bfd_get_32 (abfd, note->descdata + 8);
10492 }
10493 else /* NOTE_INFO_MODULE64 */
10494 {
10495 /* module_info.base_address */
10496 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10497 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10498 /* module_info.module_name_size */
10499 name_size = bfd_get_32 (abfd, note->descdata + 12);
10500 }
10501
10502 len = strlen (buf) + 1;
10503 name = (char *) bfd_alloc (abfd, len);
10504 if (name == NULL)
10505 return false;
10506
10507 memcpy (name, buf, len);
10508
10509 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10510
10511 if (sect == NULL)
10512 return false;
10513
10514 if (note->descsz < 12 + name_size)
10515 {
10516 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10517 " is too small to contain a name of size %u"),
10518 abfd, note->descsz, name_size);
10519 return true;
10520 }
10521
10522 sect->size = note->descsz;
10523 sect->filepos = note->descpos;
10524 sect->alignment_power = 2;
10525 break;
10526
10527 default:
10528 return true;
10529 }
10530
10531 return true;
10532 }
10533
10534 static bool
10535 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10536 {
10537 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10538
10539 switch (note->type)
10540 {
10541 default:
10542 return true;
10543
10544 case NT_PRSTATUS:
10545 if (bed->elf_backend_grok_prstatus)
10546 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10547 return true;
10548 #if defined (HAVE_PRSTATUS_T)
10549 return elfcore_grok_prstatus (abfd, note);
10550 #else
10551 return true;
10552 #endif
10553
10554 #if defined (HAVE_PSTATUS_T)
10555 case NT_PSTATUS:
10556 return elfcore_grok_pstatus (abfd, note);
10557 #endif
10558
10559 #if defined (HAVE_LWPSTATUS_T)
10560 case NT_LWPSTATUS:
10561 return elfcore_grok_lwpstatus (abfd, note);
10562 #endif
10563
10564 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10565 return elfcore_grok_prfpreg (abfd, note);
10566
10567 case NT_WIN32PSTATUS:
10568 return elfcore_grok_win32pstatus (abfd, note);
10569
10570 case NT_PRXFPREG: /* Linux SSE extension */
10571 if (note->namesz == 6
10572 && strcmp (note->namedata, "LINUX") == 0)
10573 return elfcore_grok_prxfpreg (abfd, note);
10574 else
10575 return true;
10576
10577 case NT_X86_XSTATE: /* Linux XSAVE extension */
10578 if (note->namesz == 6
10579 && strcmp (note->namedata, "LINUX") == 0)
10580 return elfcore_grok_xstatereg (abfd, note);
10581 else
10582 return true;
10583
10584 case NT_PPC_VMX:
10585 if (note->namesz == 6
10586 && strcmp (note->namedata, "LINUX") == 0)
10587 return elfcore_grok_ppc_vmx (abfd, note);
10588 else
10589 return true;
10590
10591 case NT_PPC_VSX:
10592 if (note->namesz == 6
10593 && strcmp (note->namedata, "LINUX") == 0)
10594 return elfcore_grok_ppc_vsx (abfd, note);
10595 else
10596 return true;
10597
10598 case NT_PPC_TAR:
10599 if (note->namesz == 6
10600 && strcmp (note->namedata, "LINUX") == 0)
10601 return elfcore_grok_ppc_tar (abfd, note);
10602 else
10603 return true;
10604
10605 case NT_PPC_PPR:
10606 if (note->namesz == 6
10607 && strcmp (note->namedata, "LINUX") == 0)
10608 return elfcore_grok_ppc_ppr (abfd, note);
10609 else
10610 return true;
10611
10612 case NT_PPC_DSCR:
10613 if (note->namesz == 6
10614 && strcmp (note->namedata, "LINUX") == 0)
10615 return elfcore_grok_ppc_dscr (abfd, note);
10616 else
10617 return true;
10618
10619 case NT_PPC_EBB:
10620 if (note->namesz == 6
10621 && strcmp (note->namedata, "LINUX") == 0)
10622 return elfcore_grok_ppc_ebb (abfd, note);
10623 else
10624 return true;
10625
10626 case NT_PPC_PMU:
10627 if (note->namesz == 6
10628 && strcmp (note->namedata, "LINUX") == 0)
10629 return elfcore_grok_ppc_pmu (abfd, note);
10630 else
10631 return true;
10632
10633 case NT_PPC_TM_CGPR:
10634 if (note->namesz == 6
10635 && strcmp (note->namedata, "LINUX") == 0)
10636 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10637 else
10638 return true;
10639
10640 case NT_PPC_TM_CFPR:
10641 if (note->namesz == 6
10642 && strcmp (note->namedata, "LINUX") == 0)
10643 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10644 else
10645 return true;
10646
10647 case NT_PPC_TM_CVMX:
10648 if (note->namesz == 6
10649 && strcmp (note->namedata, "LINUX") == 0)
10650 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10651 else
10652 return true;
10653
10654 case NT_PPC_TM_CVSX:
10655 if (note->namesz == 6
10656 && strcmp (note->namedata, "LINUX") == 0)
10657 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10658 else
10659 return true;
10660
10661 case NT_PPC_TM_SPR:
10662 if (note->namesz == 6
10663 && strcmp (note->namedata, "LINUX") == 0)
10664 return elfcore_grok_ppc_tm_spr (abfd, note);
10665 else
10666 return true;
10667
10668 case NT_PPC_TM_CTAR:
10669 if (note->namesz == 6
10670 && strcmp (note->namedata, "LINUX") == 0)
10671 return elfcore_grok_ppc_tm_ctar (abfd, note);
10672 else
10673 return true;
10674
10675 case NT_PPC_TM_CPPR:
10676 if (note->namesz == 6
10677 && strcmp (note->namedata, "LINUX") == 0)
10678 return elfcore_grok_ppc_tm_cppr (abfd, note);
10679 else
10680 return true;
10681
10682 case NT_PPC_TM_CDSCR:
10683 if (note->namesz == 6
10684 && strcmp (note->namedata, "LINUX") == 0)
10685 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10686 else
10687 return true;
10688
10689 case NT_S390_HIGH_GPRS:
10690 if (note->namesz == 6
10691 && strcmp (note->namedata, "LINUX") == 0)
10692 return elfcore_grok_s390_high_gprs (abfd, note);
10693 else
10694 return true;
10695
10696 case NT_S390_TIMER:
10697 if (note->namesz == 6
10698 && strcmp (note->namedata, "LINUX") == 0)
10699 return elfcore_grok_s390_timer (abfd, note);
10700 else
10701 return true;
10702
10703 case NT_S390_TODCMP:
10704 if (note->namesz == 6
10705 && strcmp (note->namedata, "LINUX") == 0)
10706 return elfcore_grok_s390_todcmp (abfd, note);
10707 else
10708 return true;
10709
10710 case NT_S390_TODPREG:
10711 if (note->namesz == 6
10712 && strcmp (note->namedata, "LINUX") == 0)
10713 return elfcore_grok_s390_todpreg (abfd, note);
10714 else
10715 return true;
10716
10717 case NT_S390_CTRS:
10718 if (note->namesz == 6
10719 && strcmp (note->namedata, "LINUX") == 0)
10720 return elfcore_grok_s390_ctrs (abfd, note);
10721 else
10722 return true;
10723
10724 case NT_S390_PREFIX:
10725 if (note->namesz == 6
10726 && strcmp (note->namedata, "LINUX") == 0)
10727 return elfcore_grok_s390_prefix (abfd, note);
10728 else
10729 return true;
10730
10731 case NT_S390_LAST_BREAK:
10732 if (note->namesz == 6
10733 && strcmp (note->namedata, "LINUX") == 0)
10734 return elfcore_grok_s390_last_break (abfd, note);
10735 else
10736 return true;
10737
10738 case NT_S390_SYSTEM_CALL:
10739 if (note->namesz == 6
10740 && strcmp (note->namedata, "LINUX") == 0)
10741 return elfcore_grok_s390_system_call (abfd, note);
10742 else
10743 return true;
10744
10745 case NT_S390_TDB:
10746 if (note->namesz == 6
10747 && strcmp (note->namedata, "LINUX") == 0)
10748 return elfcore_grok_s390_tdb (abfd, note);
10749 else
10750 return true;
10751
10752 case NT_S390_VXRS_LOW:
10753 if (note->namesz == 6
10754 && strcmp (note->namedata, "LINUX") == 0)
10755 return elfcore_grok_s390_vxrs_low (abfd, note);
10756 else
10757 return true;
10758
10759 case NT_S390_VXRS_HIGH:
10760 if (note->namesz == 6
10761 && strcmp (note->namedata, "LINUX") == 0)
10762 return elfcore_grok_s390_vxrs_high (abfd, note);
10763 else
10764 return true;
10765
10766 case NT_S390_GS_CB:
10767 if (note->namesz == 6
10768 && strcmp (note->namedata, "LINUX") == 0)
10769 return elfcore_grok_s390_gs_cb (abfd, note);
10770 else
10771 return true;
10772
10773 case NT_S390_GS_BC:
10774 if (note->namesz == 6
10775 && strcmp (note->namedata, "LINUX") == 0)
10776 return elfcore_grok_s390_gs_bc (abfd, note);
10777 else
10778 return true;
10779
10780 case NT_ARC_V2:
10781 if (note->namesz == 6
10782 && strcmp (note->namedata, "LINUX") == 0)
10783 return elfcore_grok_arc_v2 (abfd, note);
10784 else
10785 return true;
10786
10787 case NT_ARM_VFP:
10788 if (note->namesz == 6
10789 && strcmp (note->namedata, "LINUX") == 0)
10790 return elfcore_grok_arm_vfp (abfd, note);
10791 else
10792 return true;
10793
10794 case NT_ARM_TLS:
10795 if (note->namesz == 6
10796 && strcmp (note->namedata, "LINUX") == 0)
10797 return elfcore_grok_aarch_tls (abfd, note);
10798 else
10799 return true;
10800
10801 case NT_ARM_HW_BREAK:
10802 if (note->namesz == 6
10803 && strcmp (note->namedata, "LINUX") == 0)
10804 return elfcore_grok_aarch_hw_break (abfd, note);
10805 else
10806 return true;
10807
10808 case NT_ARM_HW_WATCH:
10809 if (note->namesz == 6
10810 && strcmp (note->namedata, "LINUX") == 0)
10811 return elfcore_grok_aarch_hw_watch (abfd, note);
10812 else
10813 return true;
10814
10815 case NT_ARM_SVE:
10816 if (note->namesz == 6
10817 && strcmp (note->namedata, "LINUX") == 0)
10818 return elfcore_grok_aarch_sve (abfd, note);
10819 else
10820 return true;
10821
10822 case NT_ARM_PAC_MASK:
10823 if (note->namesz == 6
10824 && strcmp (note->namedata, "LINUX") == 0)
10825 return elfcore_grok_aarch_pauth (abfd, note);
10826 else
10827 return true;
10828
10829 case NT_ARM_TAGGED_ADDR_CTRL:
10830 if (note->namesz == 6
10831 && strcmp (note->namedata, "LINUX") == 0)
10832 return elfcore_grok_aarch_mte (abfd, note);
10833 else
10834 return true;
10835
10836 case NT_GDB_TDESC:
10837 if (note->namesz == 4
10838 && strcmp (note->namedata, "GDB") == 0)
10839 return elfcore_grok_gdb_tdesc (abfd, note);
10840 else
10841 return true;
10842
10843 case NT_RISCV_CSR:
10844 if (note->namesz == 4
10845 && strcmp (note->namedata, "GDB") == 0)
10846 return elfcore_grok_riscv_csr (abfd, note);
10847 else
10848 return true;
10849
10850 case NT_LARCH_CPUCFG:
10851 if (note->namesz == 6
10852 && strcmp (note->namedata, "LINUX") == 0)
10853 return elfcore_grok_loongarch_cpucfg (abfd, note);
10854 else
10855 return true;
10856
10857 case NT_LARCH_LBT:
10858 if (note->namesz == 6
10859 && strcmp (note->namedata, "LINUX") == 0)
10860 return elfcore_grok_loongarch_lbt (abfd, note);
10861 else
10862 return true;
10863
10864 case NT_LARCH_LSX:
10865 if (note->namesz == 6
10866 && strcmp (note->namedata, "LINUX") == 0)
10867 return elfcore_grok_loongarch_lsx (abfd, note);
10868 else
10869 return true;
10870
10871 case NT_LARCH_LASX:
10872 if (note->namesz == 6
10873 && strcmp (note->namedata, "LINUX") == 0)
10874 return elfcore_grok_loongarch_lasx (abfd, note);
10875 else
10876 return true;
10877
10878 case NT_PRPSINFO:
10879 case NT_PSINFO:
10880 if (bed->elf_backend_grok_psinfo)
10881 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10882 return true;
10883 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10884 return elfcore_grok_psinfo (abfd, note);
10885 #else
10886 return true;
10887 #endif
10888
10889 case NT_AUXV:
10890 return elfcore_make_auxv_note_section (abfd, note, 0);
10891
10892 case NT_FILE:
10893 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10894 note);
10895
10896 case NT_SIGINFO:
10897 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10898 note);
10899
10900 }
10901 }
10902
10903 static bool
10904 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10905 {
10906 struct bfd_build_id* build_id;
10907
10908 if (note->descsz == 0)
10909 return false;
10910
10911 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10912 if (build_id == NULL)
10913 return false;
10914
10915 build_id->size = note->descsz;
10916 memcpy (build_id->data, note->descdata, note->descsz);
10917 abfd->build_id = build_id;
10918
10919 return true;
10920 }
10921
10922 static bool
10923 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10924 {
10925 switch (note->type)
10926 {
10927 default:
10928 return true;
10929
10930 case NT_GNU_PROPERTY_TYPE_0:
10931 return _bfd_elf_parse_gnu_properties (abfd, note);
10932
10933 case NT_GNU_BUILD_ID:
10934 return elfobj_grok_gnu_build_id (abfd, note);
10935 }
10936 }
10937
10938 static bool
10939 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10940 {
10941 struct sdt_note *cur =
10942 (struct sdt_note *) bfd_alloc (abfd,
10943 sizeof (struct sdt_note) + note->descsz);
10944
10945 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10946 cur->size = (bfd_size_type) note->descsz;
10947 memcpy (cur->data, note->descdata, note->descsz);
10948
10949 elf_tdata (abfd)->sdt_note_head = cur;
10950
10951 return true;
10952 }
10953
10954 static bool
10955 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10956 {
10957 switch (note->type)
10958 {
10959 case NT_STAPSDT:
10960 return elfobj_grok_stapsdt_note_1 (abfd, note);
10961
10962 default:
10963 return true;
10964 }
10965 }
10966
10967 static bool
10968 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10969 {
10970 size_t offset;
10971
10972 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10973 {
10974 case ELFCLASS32:
10975 if (note->descsz < 108)
10976 return false;
10977 break;
10978
10979 case ELFCLASS64:
10980 if (note->descsz < 120)
10981 return false;
10982 break;
10983
10984 default:
10985 return false;
10986 }
10987
10988 /* Check for version 1 in pr_version. */
10989 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10990 return false;
10991
10992 offset = 4;
10993
10994 /* Skip over pr_psinfosz. */
10995 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10996 offset += 4;
10997 else
10998 {
10999 offset += 4; /* Padding before pr_psinfosz. */
11000 offset += 8;
11001 }
11002
11003 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
11004 elf_tdata (abfd)->core->program
11005 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11006 offset += 17;
11007
11008 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
11009 elf_tdata (abfd)->core->command
11010 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11011 offset += 81;
11012
11013 /* Padding before pr_pid. */
11014 offset += 2;
11015
11016 /* The pr_pid field was added in version "1a". */
11017 if (note->descsz < offset + 4)
11018 return true;
11019
11020 elf_tdata (abfd)->core->pid
11021 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11022
11023 return true;
11024 }
11025
11026 static bool
11027 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11028 {
11029 size_t offset;
11030 size_t size;
11031 size_t min_size;
11032
11033 /* Compute offset of pr_getregsz, skipping over pr_statussz.
11034 Also compute minimum size of this note. */
11035 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11036 {
11037 case ELFCLASS32:
11038 offset = 4 + 4;
11039 min_size = offset + (4 * 2) + 4 + 4 + 4;
11040 break;
11041
11042 case ELFCLASS64:
11043 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
11044 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11045 break;
11046
11047 default:
11048 return false;
11049 }
11050
11051 if (note->descsz < min_size)
11052 return false;
11053
11054 /* Check for version 1 in pr_version. */
11055 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11056 return false;
11057
11058 /* Extract size of pr_reg from pr_gregsetsz. */
11059 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11060 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11061 {
11062 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11063 offset += 4 * 2;
11064 }
11065 else
11066 {
11067 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11068 offset += 8 * 2;
11069 }
11070
11071 /* Skip over pr_osreldate. */
11072 offset += 4;
11073
11074 /* Read signal from pr_cursig. */
11075 if (elf_tdata (abfd)->core->signal == 0)
11076 elf_tdata (abfd)->core->signal
11077 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11078 offset += 4;
11079
11080 /* Read TID from pr_pid. */
11081 elf_tdata (abfd)->core->lwpid
11082 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11083 offset += 4;
11084
11085 /* Padding before pr_reg. */
11086 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11087 offset += 4;
11088
11089 /* Make sure that there is enough data remaining in the note. */
11090 if ((note->descsz - offset) < size)
11091 return false;
11092
11093 /* Make a ".reg/999" section and a ".reg" section. */
11094 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11095 size, note->descpos + offset);
11096 }
11097
11098 static bool
11099 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11100 {
11101 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11102
11103 switch (note->type)
11104 {
11105 case NT_PRSTATUS:
11106 if (bed->elf_backend_grok_freebsd_prstatus)
11107 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11108 return true;
11109 return elfcore_grok_freebsd_prstatus (abfd, note);
11110
11111 case NT_FPREGSET:
11112 return elfcore_grok_prfpreg (abfd, note);
11113
11114 case NT_PRPSINFO:
11115 return elfcore_grok_freebsd_psinfo (abfd, note);
11116
11117 case NT_FREEBSD_THRMISC:
11118 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11119
11120 case NT_FREEBSD_PROCSTAT_PROC:
11121 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11122 note);
11123
11124 case NT_FREEBSD_PROCSTAT_FILES:
11125 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11126 note);
11127
11128 case NT_FREEBSD_PROCSTAT_VMMAP:
11129 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11130 note);
11131
11132 case NT_FREEBSD_PROCSTAT_AUXV:
11133 return elfcore_make_auxv_note_section (abfd, note, 4);
11134
11135 case NT_FREEBSD_X86_SEGBASES:
11136 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11137
11138 case NT_X86_XSTATE:
11139 return elfcore_grok_xstatereg (abfd, note);
11140
11141 case NT_FREEBSD_PTLWPINFO:
11142 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11143 note);
11144
11145 case NT_ARM_TLS:
11146 return elfcore_grok_aarch_tls (abfd, note);
11147
11148 case NT_ARM_VFP:
11149 return elfcore_grok_arm_vfp (abfd, note);
11150
11151 default:
11152 return true;
11153 }
11154 }
11155
11156 static bool
11157 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11158 {
11159 char *cp;
11160
11161 cp = strchr (note->namedata, '@');
11162 if (cp != NULL)
11163 {
11164 *lwpidp = atoi(cp + 1);
11165 return true;
11166 }
11167 return false;
11168 }
11169
11170 static bool
11171 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11172 {
11173 if (note->descsz <= 0x7c + 31)
11174 return false;
11175
11176 /* Signal number at offset 0x08. */
11177 elf_tdata (abfd)->core->signal
11178 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11179
11180 /* Process ID at offset 0x50. */
11181 elf_tdata (abfd)->core->pid
11182 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11183
11184 /* Command name at 0x7c (max 32 bytes, including nul). */
11185 elf_tdata (abfd)->core->command
11186 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11187
11188 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11189 note);
11190 }
11191
11192 static bool
11193 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11194 {
11195 int lwp;
11196
11197 if (elfcore_netbsd_get_lwpid (note, &lwp))
11198 elf_tdata (abfd)->core->lwpid = lwp;
11199
11200 switch (note->type)
11201 {
11202 case NT_NETBSDCORE_PROCINFO:
11203 /* NetBSD-specific core "procinfo". Note that we expect to
11204 find this note before any of the others, which is fine,
11205 since the kernel writes this note out first when it
11206 creates a core file. */
11207 return elfcore_grok_netbsd_procinfo (abfd, note);
11208 case NT_NETBSDCORE_AUXV:
11209 /* NetBSD-specific Elf Auxiliary Vector data. */
11210 return elfcore_make_auxv_note_section (abfd, note, 4);
11211 case NT_NETBSDCORE_LWPSTATUS:
11212 return elfcore_make_note_pseudosection (abfd,
11213 ".note.netbsdcore.lwpstatus",
11214 note);
11215 default:
11216 break;
11217 }
11218
11219 /* As of March 2020 there are no other machine-independent notes
11220 defined for NetBSD core files. If the note type is less
11221 than the start of the machine-dependent note types, we don't
11222 understand it. */
11223
11224 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11225 return true;
11226
11227
11228 switch (bfd_get_arch (abfd))
11229 {
11230 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11231 PT_GETFPREGS == mach+2. */
11232
11233 case bfd_arch_aarch64:
11234 case bfd_arch_alpha:
11235 case bfd_arch_sparc:
11236 switch (note->type)
11237 {
11238 case NT_NETBSDCORE_FIRSTMACH+0:
11239 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11240
11241 case NT_NETBSDCORE_FIRSTMACH+2:
11242 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11243
11244 default:
11245 return true;
11246 }
11247
11248 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11249 There's also old PT___GETREGS40 == mach + 1 for old reg
11250 structure which lacks GBR. */
11251
11252 case bfd_arch_sh:
11253 switch (note->type)
11254 {
11255 case NT_NETBSDCORE_FIRSTMACH+3:
11256 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11257
11258 case NT_NETBSDCORE_FIRSTMACH+5:
11259 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11260
11261 default:
11262 return true;
11263 }
11264
11265 /* On all other arch's, PT_GETREGS == mach+1 and
11266 PT_GETFPREGS == mach+3. */
11267
11268 default:
11269 switch (note->type)
11270 {
11271 case NT_NETBSDCORE_FIRSTMACH+1:
11272 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11273
11274 case NT_NETBSDCORE_FIRSTMACH+3:
11275 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11276
11277 default:
11278 return true;
11279 }
11280 }
11281 /* NOTREACHED */
11282 }
11283
11284 static bool
11285 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11286 {
11287 if (note->descsz <= 0x48 + 31)
11288 return false;
11289
11290 /* Signal number at offset 0x08. */
11291 elf_tdata (abfd)->core->signal
11292 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11293
11294 /* Process ID at offset 0x20. */
11295 elf_tdata (abfd)->core->pid
11296 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11297
11298 /* Command name at 0x48 (max 32 bytes, including nul). */
11299 elf_tdata (abfd)->core->command
11300 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11301
11302 return true;
11303 }
11304
11305 /* Processes Solaris's process status note.
11306 sig_off ~ offsetof(prstatus_t, pr_cursig)
11307 pid_off ~ offsetof(prstatus_t, pr_pid)
11308 lwpid_off ~ offsetof(prstatus_t, pr_who)
11309 gregset_size ~ sizeof(gregset_t)
11310 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11311
11312 static bool
11313 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11314 int pid_off, int lwpid_off, size_t gregset_size,
11315 size_t gregset_offset)
11316 {
11317 asection *sect = NULL;
11318 elf_tdata (abfd)->core->signal
11319 = bfd_get_16 (abfd, note->descdata + sig_off);
11320 elf_tdata (abfd)->core->pid
11321 = bfd_get_32 (abfd, note->descdata + pid_off);
11322 elf_tdata (abfd)->core->lwpid
11323 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11324
11325 sect = bfd_get_section_by_name (abfd, ".reg");
11326 if (sect != NULL)
11327 sect->size = gregset_size;
11328
11329 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11330 note->descpos + gregset_offset);
11331 }
11332
11333 /* Gets program and arguments from a core.
11334 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11335 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11336
11337 static bool
11338 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11339 int prog_off, int comm_off)
11340 {
11341 elf_tdata (abfd)->core->program
11342 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11343 elf_tdata (abfd)->core->command
11344 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11345
11346 return true;
11347 }
11348
11349 /* Processes Solaris's LWP status note.
11350 gregset_size ~ sizeof(gregset_t)
11351 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11352 fpregset_size ~ sizeof(fpregset_t)
11353 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11354
11355 static bool
11356 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11357 size_t gregset_size, int gregset_off,
11358 size_t fpregset_size, int fpregset_off)
11359 {
11360 asection *sect = NULL;
11361 char reg2_section_name[16] = { 0 };
11362
11363 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11364 elf_tdata (abfd)->core->lwpid);
11365
11366 /* offsetof(lwpstatus_t, pr_lwpid) */
11367 elf_tdata (abfd)->core->lwpid
11368 = bfd_get_32 (abfd, note->descdata + 4);
11369 /* offsetof(lwpstatus_t, pr_cursig) */
11370 elf_tdata (abfd)->core->signal
11371 = bfd_get_16 (abfd, note->descdata + 12);
11372
11373 sect = bfd_get_section_by_name (abfd, ".reg");
11374 if (sect != NULL)
11375 sect->size = gregset_size;
11376 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11377 note->descpos + gregset_off))
11378 return false;
11379
11380 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11381 if (sect != NULL)
11382 {
11383 sect->size = fpregset_size;
11384 sect->filepos = note->descpos + fpregset_off;
11385 sect->alignment_power = 2;
11386 }
11387 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11388 note->descpos + fpregset_off))
11389 return false;
11390
11391 return true;
11392 }
11393
11394 static bool
11395 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11396 {
11397 if (note == NULL)
11398 return false;
11399
11400 /* core files are identified as 32- or 64-bit, SPARC or x86,
11401 by the size of the descsz which matches the sizeof()
11402 the type appropriate for that note type (e.g., prstatus_t for
11403 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11404 on Solaris. The core file bitness may differ from the bitness of
11405 gdb itself, so fixed values are used instead of sizeof().
11406 Appropriate fixed offsets are also used to obtain data from
11407 the note. */
11408
11409 switch ((int) note->type)
11410 {
11411 case SOLARIS_NT_PRSTATUS:
11412 switch (note->descsz)
11413 {
11414 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11415 return elfcore_grok_solaris_prstatus(abfd, note,
11416 136, 216, 308, 152, 356);
11417 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11418 return elfcore_grok_solaris_prstatus(abfd, note,
11419 264, 360, 520, 304, 600);
11420 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11421 return elfcore_grok_solaris_prstatus(abfd, note,
11422 136, 216, 308, 76, 356);
11423 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11424 return elfcore_grok_solaris_prstatus(abfd, note,
11425 264, 360, 520, 224, 600);
11426 default:
11427 return true;
11428 }
11429
11430 case SOLARIS_NT_PSINFO:
11431 case SOLARIS_NT_PRPSINFO:
11432 switch (note->descsz)
11433 {
11434 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11435 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11436 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11437 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11438 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11439 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11440 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11441 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11442 default:
11443 return true;
11444 }
11445
11446 case SOLARIS_NT_LWPSTATUS:
11447 switch (note->descsz)
11448 {
11449 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11450 return elfcore_grok_solaris_lwpstatus(abfd, note,
11451 152, 344, 400, 496);
11452 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11453 return elfcore_grok_solaris_lwpstatus(abfd, note,
11454 304, 544, 544, 848);
11455 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11456 return elfcore_grok_solaris_lwpstatus(abfd, note,
11457 76, 344, 380, 420);
11458 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11459 return elfcore_grok_solaris_lwpstatus(abfd, note,
11460 224, 544, 528, 768);
11461 default:
11462 return true;
11463 }
11464
11465 case SOLARIS_NT_LWPSINFO:
11466 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11467 if (note->descsz == 128 || note->descsz == 152)
11468 elf_tdata (abfd)->core->lwpid =
11469 bfd_get_32 (abfd, note->descdata + 4);
11470 break;
11471
11472 default:
11473 break;
11474 }
11475
11476 return true;
11477 }
11478
11479 /* For name starting with "CORE" this may be either a Solaris
11480 core file or a gdb-generated core file. Do Solaris-specific
11481 processing on selected note types first with
11482 elfcore_grok_solaris_note(), then process the note
11483 in elfcore_grok_note(). */
11484
11485 static bool
11486 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11487 {
11488 if (!elfcore_grok_solaris_note_impl (abfd, note))
11489 return false;
11490
11491 return elfcore_grok_note (abfd, note);
11492 }
11493
11494 static bool
11495 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11496 {
11497 if (note->type == NT_OPENBSD_PROCINFO)
11498 return elfcore_grok_openbsd_procinfo (abfd, note);
11499
11500 if (note->type == NT_OPENBSD_REGS)
11501 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11502
11503 if (note->type == NT_OPENBSD_FPREGS)
11504 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11505
11506 if (note->type == NT_OPENBSD_XFPREGS)
11507 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11508
11509 if (note->type == NT_OPENBSD_AUXV)
11510 return elfcore_make_auxv_note_section (abfd, note, 0);
11511
11512 if (note->type == NT_OPENBSD_WCOOKIE)
11513 {
11514 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11515 SEC_HAS_CONTENTS);
11516
11517 if (sect == NULL)
11518 return false;
11519 sect->size = note->descsz;
11520 sect->filepos = note->descpos;
11521 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11522
11523 return true;
11524 }
11525
11526 return true;
11527 }
11528
11529 static bool
11530 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11531 {
11532 void *ddata = note->descdata;
11533 char buf[100];
11534 char *name;
11535 asection *sect;
11536 short sig;
11537 unsigned flags;
11538
11539 if (note->descsz < 16)
11540 return false;
11541
11542 /* nto_procfs_status 'pid' field is at offset 0. */
11543 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11544
11545 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11546 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11547
11548 /* nto_procfs_status 'flags' field is at offset 8. */
11549 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11550
11551 /* nto_procfs_status 'what' field is at offset 14. */
11552 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11553 {
11554 elf_tdata (abfd)->core->signal = sig;
11555 elf_tdata (abfd)->core->lwpid = *tid;
11556 }
11557
11558 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11559 do not come from signals so we make sure we set the current
11560 thread just in case. */
11561 if (flags & 0x00000080)
11562 elf_tdata (abfd)->core->lwpid = *tid;
11563
11564 /* Make a ".qnx_core_status/%d" section. */
11565 sprintf (buf, ".qnx_core_status/%ld", *tid);
11566
11567 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11568 if (name == NULL)
11569 return false;
11570 strcpy (name, buf);
11571
11572 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11573 if (sect == NULL)
11574 return false;
11575
11576 sect->size = note->descsz;
11577 sect->filepos = note->descpos;
11578 sect->alignment_power = 2;
11579
11580 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11581 }
11582
11583 static bool
11584 elfcore_grok_nto_regs (bfd *abfd,
11585 Elf_Internal_Note *note,
11586 long tid,
11587 char *base)
11588 {
11589 char buf[100];
11590 char *name;
11591 asection *sect;
11592
11593 /* Make a "(base)/%d" section. */
11594 sprintf (buf, "%s/%ld", base, tid);
11595
11596 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11597 if (name == NULL)
11598 return false;
11599 strcpy (name, buf);
11600
11601 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11602 if (sect == NULL)
11603 return false;
11604
11605 sect->size = note->descsz;
11606 sect->filepos = note->descpos;
11607 sect->alignment_power = 2;
11608
11609 /* This is the current thread. */
11610 if (elf_tdata (abfd)->core->lwpid == tid)
11611 return elfcore_maybe_make_sect (abfd, base, sect);
11612
11613 return true;
11614 }
11615
11616 #define BFD_QNT_CORE_INFO 7
11617 #define BFD_QNT_CORE_STATUS 8
11618 #define BFD_QNT_CORE_GREG 9
11619 #define BFD_QNT_CORE_FPREG 10
11620
11621 static bool
11622 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11623 {
11624 /* Every GREG section has a STATUS section before it. Store the
11625 tid from the previous call to pass down to the next gregs
11626 function. */
11627 static long tid = 1;
11628
11629 switch (note->type)
11630 {
11631 case BFD_QNT_CORE_INFO:
11632 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11633 case BFD_QNT_CORE_STATUS:
11634 return elfcore_grok_nto_status (abfd, note, &tid);
11635 case BFD_QNT_CORE_GREG:
11636 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11637 case BFD_QNT_CORE_FPREG:
11638 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11639 default:
11640 return true;
11641 }
11642 }
11643
11644 static bool
11645 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11646 {
11647 char *name;
11648 asection *sect;
11649 size_t len;
11650
11651 /* Use note name as section name. */
11652 len = note->namesz;
11653 name = (char *) bfd_alloc (abfd, len);
11654 if (name == NULL)
11655 return false;
11656 memcpy (name, note->namedata, len);
11657 name[len - 1] = '\0';
11658
11659 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11660 if (sect == NULL)
11661 return false;
11662
11663 sect->size = note->descsz;
11664 sect->filepos = note->descpos;
11665 sect->alignment_power = 1;
11666
11667 return true;
11668 }
11669
11670 /* Function: elfcore_write_note
11671
11672 Inputs:
11673 buffer to hold note, and current size of buffer
11674 name of note
11675 type of note
11676 data for note
11677 size of data for note
11678
11679 Writes note to end of buffer. ELF64 notes are written exactly as
11680 for ELF32, despite the current (as of 2006) ELF gabi specifying
11681 that they ought to have 8-byte namesz and descsz field, and have
11682 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11683
11684 Return:
11685 Pointer to realloc'd buffer, *BUFSIZ updated. */
11686
11687 char *
11688 elfcore_write_note (bfd *abfd,
11689 char *buf,
11690 int *bufsiz,
11691 const char *name,
11692 int type,
11693 const void *input,
11694 int size)
11695 {
11696 Elf_External_Note *xnp;
11697 size_t namesz;
11698 size_t newspace;
11699 char *dest;
11700
11701 namesz = 0;
11702 if (name != NULL)
11703 namesz = strlen (name) + 1;
11704
11705 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11706
11707 buf = (char *) realloc (buf, *bufsiz + newspace);
11708 if (buf == NULL)
11709 return buf;
11710 dest = buf + *bufsiz;
11711 *bufsiz += newspace;
11712 xnp = (Elf_External_Note *) dest;
11713 H_PUT_32 (abfd, namesz, xnp->namesz);
11714 H_PUT_32 (abfd, size, xnp->descsz);
11715 H_PUT_32 (abfd, type, xnp->type);
11716 dest = xnp->name;
11717 if (name != NULL)
11718 {
11719 memcpy (dest, name, namesz);
11720 dest += namesz;
11721 while (namesz & 3)
11722 {
11723 *dest++ = '\0';
11724 ++namesz;
11725 }
11726 }
11727 memcpy (dest, input, size);
11728 dest += size;
11729 while (size & 3)
11730 {
11731 *dest++ = '\0';
11732 ++size;
11733 }
11734 return buf;
11735 }
11736
11737 /* gcc-8 warns (*) on all the strncpy calls in this function about
11738 possible string truncation. The "truncation" is not a bug. We
11739 have an external representation of structs with fields that are not
11740 necessarily NULL terminated and corresponding internal
11741 representation fields that are one larger so that they can always
11742 be NULL terminated.
11743 gcc versions between 4.2 and 4.6 do not allow pragma control of
11744 diagnostics inside functions, giving a hard error if you try to use
11745 the finer control available with later versions.
11746 gcc prior to 4.2 warns about diagnostic push and pop.
11747 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11748 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11749 (*) Depending on your system header files! */
11750 #if GCC_VERSION >= 8000
11751 # pragma GCC diagnostic push
11752 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11753 #endif
11754 char *
11755 elfcore_write_prpsinfo (bfd *abfd,
11756 char *buf,
11757 int *bufsiz,
11758 const char *fname,
11759 const char *psargs)
11760 {
11761 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11762
11763 if (bed->elf_backend_write_core_note != NULL)
11764 {
11765 char *ret;
11766 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11767 NT_PRPSINFO, fname, psargs);
11768 if (ret != NULL)
11769 return ret;
11770 }
11771
11772 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11773 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11774 if (bed->s->elfclass == ELFCLASS32)
11775 {
11776 # if defined (HAVE_PSINFO32_T)
11777 psinfo32_t data;
11778 int note_type = NT_PSINFO;
11779 # else
11780 prpsinfo32_t data;
11781 int note_type = NT_PRPSINFO;
11782 # endif
11783
11784 memset (&data, 0, sizeof (data));
11785 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11786 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11787 return elfcore_write_note (abfd, buf, bufsiz,
11788 "CORE", note_type, &data, sizeof (data));
11789 }
11790 else
11791 # endif
11792 {
11793 # if defined (HAVE_PSINFO_T)
11794 psinfo_t data;
11795 int note_type = NT_PSINFO;
11796 # else
11797 prpsinfo_t data;
11798 int note_type = NT_PRPSINFO;
11799 # endif
11800
11801 memset (&data, 0, sizeof (data));
11802 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11803 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11804 return elfcore_write_note (abfd, buf, bufsiz,
11805 "CORE", note_type, &data, sizeof (data));
11806 }
11807 #endif /* PSINFO_T or PRPSINFO_T */
11808
11809 free (buf);
11810 return NULL;
11811 }
11812 #if GCC_VERSION >= 8000
11813 # pragma GCC diagnostic pop
11814 #endif
11815
11816 char *
11817 elfcore_write_linux_prpsinfo32
11818 (bfd *abfd, char *buf, int *bufsiz,
11819 const struct elf_internal_linux_prpsinfo *prpsinfo)
11820 {
11821 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11822 {
11823 struct elf_external_linux_prpsinfo32_ugid16 data;
11824
11825 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11826 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11827 &data, sizeof (data));
11828 }
11829 else
11830 {
11831 struct elf_external_linux_prpsinfo32_ugid32 data;
11832
11833 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11834 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11835 &data, sizeof (data));
11836 }
11837 }
11838
11839 char *
11840 elfcore_write_linux_prpsinfo64
11841 (bfd *abfd, char *buf, int *bufsiz,
11842 const struct elf_internal_linux_prpsinfo *prpsinfo)
11843 {
11844 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11845 {
11846 struct elf_external_linux_prpsinfo64_ugid16 data;
11847
11848 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11849 return elfcore_write_note (abfd, buf, bufsiz,
11850 "CORE", NT_PRPSINFO, &data, sizeof (data));
11851 }
11852 else
11853 {
11854 struct elf_external_linux_prpsinfo64_ugid32 data;
11855
11856 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11857 return elfcore_write_note (abfd, buf, bufsiz,
11858 "CORE", NT_PRPSINFO, &data, sizeof (data));
11859 }
11860 }
11861
11862 char *
11863 elfcore_write_prstatus (bfd *abfd,
11864 char *buf,
11865 int *bufsiz,
11866 long pid,
11867 int cursig,
11868 const void *gregs)
11869 {
11870 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11871
11872 if (bed->elf_backend_write_core_note != NULL)
11873 {
11874 char *ret;
11875 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11876 NT_PRSTATUS,
11877 pid, cursig, gregs);
11878 if (ret != NULL)
11879 return ret;
11880 }
11881
11882 #if defined (HAVE_PRSTATUS_T)
11883 #if defined (HAVE_PRSTATUS32_T)
11884 if (bed->s->elfclass == ELFCLASS32)
11885 {
11886 prstatus32_t prstat;
11887
11888 memset (&prstat, 0, sizeof (prstat));
11889 prstat.pr_pid = pid;
11890 prstat.pr_cursig = cursig;
11891 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11892 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11893 NT_PRSTATUS, &prstat, sizeof (prstat));
11894 }
11895 else
11896 #endif
11897 {
11898 prstatus_t prstat;
11899
11900 memset (&prstat, 0, sizeof (prstat));
11901 prstat.pr_pid = pid;
11902 prstat.pr_cursig = cursig;
11903 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11904 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11905 NT_PRSTATUS, &prstat, sizeof (prstat));
11906 }
11907 #endif /* HAVE_PRSTATUS_T */
11908
11909 free (buf);
11910 return NULL;
11911 }
11912
11913 #if defined (HAVE_LWPSTATUS_T)
11914 char *
11915 elfcore_write_lwpstatus (bfd *abfd,
11916 char *buf,
11917 int *bufsiz,
11918 long pid,
11919 int cursig,
11920 const void *gregs)
11921 {
11922 lwpstatus_t lwpstat;
11923 const char *note_name = "CORE";
11924
11925 memset (&lwpstat, 0, sizeof (lwpstat));
11926 lwpstat.pr_lwpid = pid >> 16;
11927 lwpstat.pr_cursig = cursig;
11928 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11929 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11930 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11931 #if !defined(gregs)
11932 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11933 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11934 #else
11935 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11936 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11937 #endif
11938 #endif
11939 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11940 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11941 }
11942 #endif /* HAVE_LWPSTATUS_T */
11943
11944 #if defined (HAVE_PSTATUS_T)
11945 char *
11946 elfcore_write_pstatus (bfd *abfd,
11947 char *buf,
11948 int *bufsiz,
11949 long pid,
11950 int cursig ATTRIBUTE_UNUSED,
11951 const void *gregs ATTRIBUTE_UNUSED)
11952 {
11953 const char *note_name = "CORE";
11954 #if defined (HAVE_PSTATUS32_T)
11955 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11956
11957 if (bed->s->elfclass == ELFCLASS32)
11958 {
11959 pstatus32_t pstat;
11960
11961 memset (&pstat, 0, sizeof (pstat));
11962 pstat.pr_pid = pid & 0xffff;
11963 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11964 NT_PSTATUS, &pstat, sizeof (pstat));
11965 return buf;
11966 }
11967 else
11968 #endif
11969 {
11970 pstatus_t pstat;
11971
11972 memset (&pstat, 0, sizeof (pstat));
11973 pstat.pr_pid = pid & 0xffff;
11974 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11975 NT_PSTATUS, &pstat, sizeof (pstat));
11976 return buf;
11977 }
11978 }
11979 #endif /* HAVE_PSTATUS_T */
11980
11981 char *
11982 elfcore_write_prfpreg (bfd *abfd,
11983 char *buf,
11984 int *bufsiz,
11985 const void *fpregs,
11986 int size)
11987 {
11988 const char *note_name = "CORE";
11989 return elfcore_write_note (abfd, buf, bufsiz,
11990 note_name, NT_FPREGSET, fpregs, size);
11991 }
11992
11993 char *
11994 elfcore_write_prxfpreg (bfd *abfd,
11995 char *buf,
11996 int *bufsiz,
11997 const void *xfpregs,
11998 int size)
11999 {
12000 char *note_name = "LINUX";
12001 return elfcore_write_note (abfd, buf, bufsiz,
12002 note_name, NT_PRXFPREG, xfpregs, size);
12003 }
12004
12005 char *
12006 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12007 const void *xfpregs, int size)
12008 {
12009 char *note_name;
12010 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12011 note_name = "FreeBSD";
12012 else
12013 note_name = "LINUX";
12014 return elfcore_write_note (abfd, buf, bufsiz,
12015 note_name, NT_X86_XSTATE, xfpregs, size);
12016 }
12017
12018 char *
12019 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12020 const void *regs, int size)
12021 {
12022 char *note_name = "FreeBSD";
12023 return elfcore_write_note (abfd, buf, bufsiz,
12024 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
12025 }
12026
12027 char *
12028 elfcore_write_ppc_vmx (bfd *abfd,
12029 char *buf,
12030 int *bufsiz,
12031 const void *ppc_vmx,
12032 int size)
12033 {
12034 char *note_name = "LINUX";
12035 return elfcore_write_note (abfd, buf, bufsiz,
12036 note_name, NT_PPC_VMX, ppc_vmx, size);
12037 }
12038
12039 char *
12040 elfcore_write_ppc_vsx (bfd *abfd,
12041 char *buf,
12042 int *bufsiz,
12043 const void *ppc_vsx,
12044 int size)
12045 {
12046 char *note_name = "LINUX";
12047 return elfcore_write_note (abfd, buf, bufsiz,
12048 note_name, NT_PPC_VSX, ppc_vsx, size);
12049 }
12050
12051 char *
12052 elfcore_write_ppc_tar (bfd *abfd,
12053 char *buf,
12054 int *bufsiz,
12055 const void *ppc_tar,
12056 int size)
12057 {
12058 char *note_name = "LINUX";
12059 return elfcore_write_note (abfd, buf, bufsiz,
12060 note_name, NT_PPC_TAR, ppc_tar, size);
12061 }
12062
12063 char *
12064 elfcore_write_ppc_ppr (bfd *abfd,
12065 char *buf,
12066 int *bufsiz,
12067 const void *ppc_ppr,
12068 int size)
12069 {
12070 char *note_name = "LINUX";
12071 return elfcore_write_note (abfd, buf, bufsiz,
12072 note_name, NT_PPC_PPR, ppc_ppr, size);
12073 }
12074
12075 char *
12076 elfcore_write_ppc_dscr (bfd *abfd,
12077 char *buf,
12078 int *bufsiz,
12079 const void *ppc_dscr,
12080 int size)
12081 {
12082 char *note_name = "LINUX";
12083 return elfcore_write_note (abfd, buf, bufsiz,
12084 note_name, NT_PPC_DSCR, ppc_dscr, size);
12085 }
12086
12087 char *
12088 elfcore_write_ppc_ebb (bfd *abfd,
12089 char *buf,
12090 int *bufsiz,
12091 const void *ppc_ebb,
12092 int size)
12093 {
12094 char *note_name = "LINUX";
12095 return elfcore_write_note (abfd, buf, bufsiz,
12096 note_name, NT_PPC_EBB, ppc_ebb, size);
12097 }
12098
12099 char *
12100 elfcore_write_ppc_pmu (bfd *abfd,
12101 char *buf,
12102 int *bufsiz,
12103 const void *ppc_pmu,
12104 int size)
12105 {
12106 char *note_name = "LINUX";
12107 return elfcore_write_note (abfd, buf, bufsiz,
12108 note_name, NT_PPC_PMU, ppc_pmu, size);
12109 }
12110
12111 char *
12112 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12113 char *buf,
12114 int *bufsiz,
12115 const void *ppc_tm_cgpr,
12116 int size)
12117 {
12118 char *note_name = "LINUX";
12119 return elfcore_write_note (abfd, buf, bufsiz,
12120 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12121 }
12122
12123 char *
12124 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12125 char *buf,
12126 int *bufsiz,
12127 const void *ppc_tm_cfpr,
12128 int size)
12129 {
12130 char *note_name = "LINUX";
12131 return elfcore_write_note (abfd, buf, bufsiz,
12132 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12133 }
12134
12135 char *
12136 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12137 char *buf,
12138 int *bufsiz,
12139 const void *ppc_tm_cvmx,
12140 int size)
12141 {
12142 char *note_name = "LINUX";
12143 return elfcore_write_note (abfd, buf, bufsiz,
12144 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12145 }
12146
12147 char *
12148 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12149 char *buf,
12150 int *bufsiz,
12151 const void *ppc_tm_cvsx,
12152 int size)
12153 {
12154 char *note_name = "LINUX";
12155 return elfcore_write_note (abfd, buf, bufsiz,
12156 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12157 }
12158
12159 char *
12160 elfcore_write_ppc_tm_spr (bfd *abfd,
12161 char *buf,
12162 int *bufsiz,
12163 const void *ppc_tm_spr,
12164 int size)
12165 {
12166 char *note_name = "LINUX";
12167 return elfcore_write_note (abfd, buf, bufsiz,
12168 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12169 }
12170
12171 char *
12172 elfcore_write_ppc_tm_ctar (bfd *abfd,
12173 char *buf,
12174 int *bufsiz,
12175 const void *ppc_tm_ctar,
12176 int size)
12177 {
12178 char *note_name = "LINUX";
12179 return elfcore_write_note (abfd, buf, bufsiz,
12180 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12181 }
12182
12183 char *
12184 elfcore_write_ppc_tm_cppr (bfd *abfd,
12185 char *buf,
12186 int *bufsiz,
12187 const void *ppc_tm_cppr,
12188 int size)
12189 {
12190 char *note_name = "LINUX";
12191 return elfcore_write_note (abfd, buf, bufsiz,
12192 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12193 }
12194
12195 char *
12196 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12197 char *buf,
12198 int *bufsiz,
12199 const void *ppc_tm_cdscr,
12200 int size)
12201 {
12202 char *note_name = "LINUX";
12203 return elfcore_write_note (abfd, buf, bufsiz,
12204 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12205 }
12206
12207 static char *
12208 elfcore_write_s390_high_gprs (bfd *abfd,
12209 char *buf,
12210 int *bufsiz,
12211 const void *s390_high_gprs,
12212 int size)
12213 {
12214 char *note_name = "LINUX";
12215 return elfcore_write_note (abfd, buf, bufsiz,
12216 note_name, NT_S390_HIGH_GPRS,
12217 s390_high_gprs, size);
12218 }
12219
12220 char *
12221 elfcore_write_s390_timer (bfd *abfd,
12222 char *buf,
12223 int *bufsiz,
12224 const void *s390_timer,
12225 int size)
12226 {
12227 char *note_name = "LINUX";
12228 return elfcore_write_note (abfd, buf, bufsiz,
12229 note_name, NT_S390_TIMER, s390_timer, size);
12230 }
12231
12232 char *
12233 elfcore_write_s390_todcmp (bfd *abfd,
12234 char *buf,
12235 int *bufsiz,
12236 const void *s390_todcmp,
12237 int size)
12238 {
12239 char *note_name = "LINUX";
12240 return elfcore_write_note (abfd, buf, bufsiz,
12241 note_name, NT_S390_TODCMP, s390_todcmp, size);
12242 }
12243
12244 char *
12245 elfcore_write_s390_todpreg (bfd *abfd,
12246 char *buf,
12247 int *bufsiz,
12248 const void *s390_todpreg,
12249 int size)
12250 {
12251 char *note_name = "LINUX";
12252 return elfcore_write_note (abfd, buf, bufsiz,
12253 note_name, NT_S390_TODPREG, s390_todpreg, size);
12254 }
12255
12256 char *
12257 elfcore_write_s390_ctrs (bfd *abfd,
12258 char *buf,
12259 int *bufsiz,
12260 const void *s390_ctrs,
12261 int size)
12262 {
12263 char *note_name = "LINUX";
12264 return elfcore_write_note (abfd, buf, bufsiz,
12265 note_name, NT_S390_CTRS, s390_ctrs, size);
12266 }
12267
12268 char *
12269 elfcore_write_s390_prefix (bfd *abfd,
12270 char *buf,
12271 int *bufsiz,
12272 const void *s390_prefix,
12273 int size)
12274 {
12275 char *note_name = "LINUX";
12276 return elfcore_write_note (abfd, buf, bufsiz,
12277 note_name, NT_S390_PREFIX, s390_prefix, size);
12278 }
12279
12280 char *
12281 elfcore_write_s390_last_break (bfd *abfd,
12282 char *buf,
12283 int *bufsiz,
12284 const void *s390_last_break,
12285 int size)
12286 {
12287 char *note_name = "LINUX";
12288 return elfcore_write_note (abfd, buf, bufsiz,
12289 note_name, NT_S390_LAST_BREAK,
12290 s390_last_break, size);
12291 }
12292
12293 char *
12294 elfcore_write_s390_system_call (bfd *abfd,
12295 char *buf,
12296 int *bufsiz,
12297 const void *s390_system_call,
12298 int size)
12299 {
12300 char *note_name = "LINUX";
12301 return elfcore_write_note (abfd, buf, bufsiz,
12302 note_name, NT_S390_SYSTEM_CALL,
12303 s390_system_call, size);
12304 }
12305
12306 char *
12307 elfcore_write_s390_tdb (bfd *abfd,
12308 char *buf,
12309 int *bufsiz,
12310 const void *s390_tdb,
12311 int size)
12312 {
12313 char *note_name = "LINUX";
12314 return elfcore_write_note (abfd, buf, bufsiz,
12315 note_name, NT_S390_TDB, s390_tdb, size);
12316 }
12317
12318 char *
12319 elfcore_write_s390_vxrs_low (bfd *abfd,
12320 char *buf,
12321 int *bufsiz,
12322 const void *s390_vxrs_low,
12323 int size)
12324 {
12325 char *note_name = "LINUX";
12326 return elfcore_write_note (abfd, buf, bufsiz,
12327 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12328 }
12329
12330 char *
12331 elfcore_write_s390_vxrs_high (bfd *abfd,
12332 char *buf,
12333 int *bufsiz,
12334 const void *s390_vxrs_high,
12335 int size)
12336 {
12337 char *note_name = "LINUX";
12338 return elfcore_write_note (abfd, buf, bufsiz,
12339 note_name, NT_S390_VXRS_HIGH,
12340 s390_vxrs_high, size);
12341 }
12342
12343 char *
12344 elfcore_write_s390_gs_cb (bfd *abfd,
12345 char *buf,
12346 int *bufsiz,
12347 const void *s390_gs_cb,
12348 int size)
12349 {
12350 char *note_name = "LINUX";
12351 return elfcore_write_note (abfd, buf, bufsiz,
12352 note_name, NT_S390_GS_CB,
12353 s390_gs_cb, size);
12354 }
12355
12356 char *
12357 elfcore_write_s390_gs_bc (bfd *abfd,
12358 char *buf,
12359 int *bufsiz,
12360 const void *s390_gs_bc,
12361 int size)
12362 {
12363 char *note_name = "LINUX";
12364 return elfcore_write_note (abfd, buf, bufsiz,
12365 note_name, NT_S390_GS_BC,
12366 s390_gs_bc, size);
12367 }
12368
12369 char *
12370 elfcore_write_arm_vfp (bfd *abfd,
12371 char *buf,
12372 int *bufsiz,
12373 const void *arm_vfp,
12374 int size)
12375 {
12376 char *note_name = "LINUX";
12377 return elfcore_write_note (abfd, buf, bufsiz,
12378 note_name, NT_ARM_VFP, arm_vfp, size);
12379 }
12380
12381 char *
12382 elfcore_write_aarch_tls (bfd *abfd,
12383 char *buf,
12384 int *bufsiz,
12385 const void *aarch_tls,
12386 int size)
12387 {
12388 char *note_name = "LINUX";
12389 return elfcore_write_note (abfd, buf, bufsiz,
12390 note_name, NT_ARM_TLS, aarch_tls, size);
12391 }
12392
12393 char *
12394 elfcore_write_aarch_hw_break (bfd *abfd,
12395 char *buf,
12396 int *bufsiz,
12397 const void *aarch_hw_break,
12398 int size)
12399 {
12400 char *note_name = "LINUX";
12401 return elfcore_write_note (abfd, buf, bufsiz,
12402 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12403 }
12404
12405 char *
12406 elfcore_write_aarch_hw_watch (bfd *abfd,
12407 char *buf,
12408 int *bufsiz,
12409 const void *aarch_hw_watch,
12410 int size)
12411 {
12412 char *note_name = "LINUX";
12413 return elfcore_write_note (abfd, buf, bufsiz,
12414 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12415 }
12416
12417 char *
12418 elfcore_write_aarch_sve (bfd *abfd,
12419 char *buf,
12420 int *bufsiz,
12421 const void *aarch_sve,
12422 int size)
12423 {
12424 char *note_name = "LINUX";
12425 return elfcore_write_note (abfd, buf, bufsiz,
12426 note_name, NT_ARM_SVE, aarch_sve, size);
12427 }
12428
12429 char *
12430 elfcore_write_aarch_pauth (bfd *abfd,
12431 char *buf,
12432 int *bufsiz,
12433 const void *aarch_pauth,
12434 int size)
12435 {
12436 char *note_name = "LINUX";
12437 return elfcore_write_note (abfd, buf, bufsiz,
12438 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12439 }
12440
12441 char *
12442 elfcore_write_aarch_mte (bfd *abfd,
12443 char *buf,
12444 int *bufsiz,
12445 const void *aarch_mte,
12446 int size)
12447 {
12448 char *note_name = "LINUX";
12449 return elfcore_write_note (abfd, buf, bufsiz,
12450 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12451 aarch_mte,
12452 size);
12453 }
12454
12455 char *
12456 elfcore_write_arc_v2 (bfd *abfd,
12457 char *buf,
12458 int *bufsiz,
12459 const void *arc_v2,
12460 int size)
12461 {
12462 char *note_name = "LINUX";
12463 return elfcore_write_note (abfd, buf, bufsiz,
12464 note_name, NT_ARC_V2, arc_v2, size);
12465 }
12466
12467 char *
12468 elfcore_write_loongarch_cpucfg (bfd *abfd,
12469 char *buf,
12470 int *bufsiz,
12471 const void *loongarch_cpucfg,
12472 int size)
12473 {
12474 char *note_name = "LINUX";
12475 return elfcore_write_note (abfd, buf, bufsiz,
12476 note_name, NT_LARCH_CPUCFG,
12477 loongarch_cpucfg, size);
12478 }
12479
12480 char *
12481 elfcore_write_loongarch_lbt (bfd *abfd,
12482 char *buf,
12483 int *bufsiz,
12484 const void *loongarch_lbt,
12485 int size)
12486 {
12487 char *note_name = "LINUX";
12488 return elfcore_write_note (abfd, buf, bufsiz,
12489 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12490 }
12491
12492 char *
12493 elfcore_write_loongarch_lsx (bfd *abfd,
12494 char *buf,
12495 int *bufsiz,
12496 const void *loongarch_lsx,
12497 int size)
12498 {
12499 char *note_name = "LINUX";
12500 return elfcore_write_note (abfd, buf, bufsiz,
12501 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12502 }
12503
12504 char *
12505 elfcore_write_loongarch_lasx (bfd *abfd,
12506 char *buf,
12507 int *bufsiz,
12508 const void *loongarch_lasx,
12509 int size)
12510 {
12511 char *note_name = "LINUX";
12512 return elfcore_write_note (abfd, buf, bufsiz,
12513 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12514 }
12515
12516 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12517 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12518 written into. Return a pointer to the new start of the note buffer, to
12519 replace BUF which may no longer be valid. */
12520
12521 char *
12522 elfcore_write_riscv_csr (bfd *abfd,
12523 char *buf,
12524 int *bufsiz,
12525 const void *csrs,
12526 int size)
12527 {
12528 const char *note_name = "GDB";
12529 return elfcore_write_note (abfd, buf, bufsiz,
12530 note_name, NT_RISCV_CSR, csrs, size);
12531 }
12532
12533 /* Write the target description (a string) pointed to by TDESC, length
12534 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12535 note is being written into. Return a pointer to the new start of the
12536 note buffer, to replace BUF which may no longer be valid. */
12537
12538 char *
12539 elfcore_write_gdb_tdesc (bfd *abfd,
12540 char *buf,
12541 int *bufsiz,
12542 const void *tdesc,
12543 int size)
12544 {
12545 const char *note_name = "GDB";
12546 return elfcore_write_note (abfd, buf, bufsiz,
12547 note_name, NT_GDB_TDESC, tdesc, size);
12548 }
12549
12550 char *
12551 elfcore_write_register_note (bfd *abfd,
12552 char *buf,
12553 int *bufsiz,
12554 const char *section,
12555 const void *data,
12556 int size)
12557 {
12558 if (strcmp (section, ".reg2") == 0)
12559 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12560 if (strcmp (section, ".reg-xfp") == 0)
12561 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12562 if (strcmp (section, ".reg-xstate") == 0)
12563 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12564 if (strcmp (section, ".reg-x86-segbases") == 0)
12565 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12566 if (strcmp (section, ".reg-ppc-vmx") == 0)
12567 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12568 if (strcmp (section, ".reg-ppc-vsx") == 0)
12569 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12570 if (strcmp (section, ".reg-ppc-tar") == 0)
12571 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12572 if (strcmp (section, ".reg-ppc-ppr") == 0)
12573 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12574 if (strcmp (section, ".reg-ppc-dscr") == 0)
12575 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12576 if (strcmp (section, ".reg-ppc-ebb") == 0)
12577 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12578 if (strcmp (section, ".reg-ppc-pmu") == 0)
12579 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12580 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12581 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12582 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12583 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12584 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12585 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12586 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12587 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12588 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12589 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12590 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12591 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12592 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12593 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12594 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12595 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12596 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12597 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12598 if (strcmp (section, ".reg-s390-timer") == 0)
12599 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12600 if (strcmp (section, ".reg-s390-todcmp") == 0)
12601 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12602 if (strcmp (section, ".reg-s390-todpreg") == 0)
12603 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12604 if (strcmp (section, ".reg-s390-ctrs") == 0)
12605 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12606 if (strcmp (section, ".reg-s390-prefix") == 0)
12607 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12608 if (strcmp (section, ".reg-s390-last-break") == 0)
12609 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12610 if (strcmp (section, ".reg-s390-system-call") == 0)
12611 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12612 if (strcmp (section, ".reg-s390-tdb") == 0)
12613 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12614 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12615 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12616 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12617 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12618 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12619 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12620 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12621 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12622 if (strcmp (section, ".reg-arm-vfp") == 0)
12623 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12624 if (strcmp (section, ".reg-aarch-tls") == 0)
12625 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12626 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12627 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12628 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12629 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12630 if (strcmp (section, ".reg-aarch-sve") == 0)
12631 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12632 if (strcmp (section, ".reg-aarch-pauth") == 0)
12633 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12634 if (strcmp (section, ".reg-aarch-mte") == 0)
12635 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12636 if (strcmp (section, ".reg-arc-v2") == 0)
12637 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12638 if (strcmp (section, ".gdb-tdesc") == 0)
12639 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12640 if (strcmp (section, ".reg-riscv-csr") == 0)
12641 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12642 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12643 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12644 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12645 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12646 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12647 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12648 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12649 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12650 return NULL;
12651 }
12652
12653 char *
12654 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12655 const void *buf, int bufsiz)
12656 {
12657 return elfcore_write_note (obfd, note_data, note_size,
12658 "CORE", NT_FILE, buf, bufsiz);
12659 }
12660
12661 static bool
12662 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12663 size_t align)
12664 {
12665 char *p;
12666
12667 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12668 gABI specifies that PT_NOTE alignment should be aligned to 4
12669 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12670 align is less than 4, we use 4 byte alignment. */
12671 if (align < 4)
12672 align = 4;
12673 if (align != 4 && align != 8)
12674 return false;
12675
12676 p = buf;
12677 while (p < buf + size)
12678 {
12679 Elf_External_Note *xnp = (Elf_External_Note *) p;
12680 Elf_Internal_Note in;
12681
12682 if (offsetof (Elf_External_Note, name) > buf - p + size)
12683 return false;
12684
12685 in.type = H_GET_32 (abfd, xnp->type);
12686
12687 in.namesz = H_GET_32 (abfd, xnp->namesz);
12688 in.namedata = xnp->name;
12689 if (in.namesz > buf - in.namedata + size)
12690 return false;
12691
12692 in.descsz = H_GET_32 (abfd, xnp->descsz);
12693 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12694 in.descpos = offset + (in.descdata - buf);
12695 if (in.descsz != 0
12696 && (in.descdata >= buf + size
12697 || in.descsz > buf - in.descdata + size))
12698 return false;
12699
12700 switch (bfd_get_format (abfd))
12701 {
12702 default:
12703 return true;
12704
12705 case bfd_core:
12706 {
12707 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12708 struct
12709 {
12710 const char * string;
12711 size_t len;
12712 bool (*func) (bfd *, Elf_Internal_Note *);
12713 }
12714 grokers[] =
12715 {
12716 GROKER_ELEMENT ("", elfcore_grok_note),
12717 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12718 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12719 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12720 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12721 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12722 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12723 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12724 };
12725 #undef GROKER_ELEMENT
12726 int i;
12727
12728 for (i = ARRAY_SIZE (grokers); i--;)
12729 {
12730 if (in.namesz >= grokers[i].len
12731 && strncmp (in.namedata, grokers[i].string,
12732 grokers[i].len) == 0)
12733 {
12734 if (! grokers[i].func (abfd, & in))
12735 return false;
12736 break;
12737 }
12738 }
12739 break;
12740 }
12741
12742 case bfd_object:
12743 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12744 {
12745 if (! elfobj_grok_gnu_note (abfd, &in))
12746 return false;
12747 }
12748 else if (in.namesz == sizeof "stapsdt"
12749 && strcmp (in.namedata, "stapsdt") == 0)
12750 {
12751 if (! elfobj_grok_stapsdt_note (abfd, &in))
12752 return false;
12753 }
12754 break;
12755 }
12756
12757 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12758 }
12759
12760 return true;
12761 }
12762
12763 bool
12764 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12765 size_t align)
12766 {
12767 char *buf;
12768
12769 if (size == 0 || (size + 1) == 0)
12770 return true;
12771
12772 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12773 return false;
12774
12775 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12776 if (buf == NULL)
12777 return false;
12778
12779 /* PR 17512: file: ec08f814
12780 0-termintate the buffer so that string searches will not overflow. */
12781 buf[size] = 0;
12782
12783 if (!elf_parse_notes (abfd, buf, size, offset, align))
12784 {
12785 free (buf);
12786 return false;
12787 }
12788
12789 free (buf);
12790 return true;
12791 }
12792 \f
12793 /* Providing external access to the ELF program header table. */
12794
12795 /* Return an upper bound on the number of bytes required to store a
12796 copy of ABFD's program header table entries. Return -1 if an error
12797 occurs; bfd_get_error will return an appropriate code. */
12798
12799 long
12800 bfd_get_elf_phdr_upper_bound (bfd *abfd)
12801 {
12802 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12803 {
12804 bfd_set_error (bfd_error_wrong_format);
12805 return -1;
12806 }
12807
12808 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12809 }
12810
12811 /* Copy ABFD's program header table entries to *PHDRS. The entries
12812 will be stored as an array of Elf_Internal_Phdr structures, as
12813 defined in include/elf/internal.h. To find out how large the
12814 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12815
12816 Return the number of program header table entries read, or -1 if an
12817 error occurs; bfd_get_error will return an appropriate code. */
12818
12819 int
12820 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12821 {
12822 int num_phdrs;
12823
12824 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12825 {
12826 bfd_set_error (bfd_error_wrong_format);
12827 return -1;
12828 }
12829
12830 num_phdrs = elf_elfheader (abfd)->e_phnum;
12831 if (num_phdrs != 0)
12832 memcpy (phdrs, elf_tdata (abfd)->phdr,
12833 num_phdrs * sizeof (Elf_Internal_Phdr));
12834
12835 return num_phdrs;
12836 }
12837
12838 enum elf_reloc_type_class
12839 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12840 const asection *rel_sec ATTRIBUTE_UNUSED,
12841 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12842 {
12843 return reloc_class_normal;
12844 }
12845
12846 /* For RELA architectures, return the relocation value for a
12847 relocation against a local symbol. */
12848
12849 bfd_vma
12850 _bfd_elf_rela_local_sym (bfd *abfd,
12851 Elf_Internal_Sym *sym,
12852 asection **psec,
12853 Elf_Internal_Rela *rel)
12854 {
12855 asection *sec = *psec;
12856 bfd_vma relocation;
12857
12858 relocation = (sec->output_section->vma
12859 + sec->output_offset
12860 + sym->st_value);
12861 if ((sec->flags & SEC_MERGE)
12862 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12863 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12864 {
12865 rel->r_addend =
12866 _bfd_merged_section_offset (abfd, psec,
12867 elf_section_data (sec)->sec_info,
12868 sym->st_value + rel->r_addend);
12869 if (sec != *psec)
12870 {
12871 /* If we have changed the section, and our original section is
12872 marked with SEC_EXCLUDE, it means that the original
12873 SEC_MERGE section has been completely subsumed in some
12874 other SEC_MERGE section. In this case, we need to leave
12875 some info around for --emit-relocs. */
12876 if ((sec->flags & SEC_EXCLUDE) != 0)
12877 sec->kept_section = *psec;
12878 sec = *psec;
12879 }
12880 rel->r_addend -= relocation;
12881 rel->r_addend += sec->output_section->vma + sec->output_offset;
12882 }
12883 return relocation;
12884 }
12885
12886 bfd_vma
12887 _bfd_elf_rel_local_sym (bfd *abfd,
12888 Elf_Internal_Sym *sym,
12889 asection **psec,
12890 bfd_vma addend)
12891 {
12892 asection *sec = *psec;
12893
12894 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12895 return sym->st_value + addend;
12896
12897 return _bfd_merged_section_offset (abfd, psec,
12898 elf_section_data (sec)->sec_info,
12899 sym->st_value + addend);
12900 }
12901
12902 /* Adjust an address within a section. Given OFFSET within SEC, return
12903 the new offset within the section, based upon changes made to the
12904 section. Returns -1 if the offset is now invalid.
12905 The offset (in abnd out) is in target sized bytes, however big a
12906 byte may be. */
12907
12908 bfd_vma
12909 _bfd_elf_section_offset (bfd *abfd,
12910 struct bfd_link_info *info,
12911 asection *sec,
12912 bfd_vma offset)
12913 {
12914 switch (sec->sec_info_type)
12915 {
12916 case SEC_INFO_TYPE_STABS:
12917 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12918 offset);
12919 case SEC_INFO_TYPE_EH_FRAME:
12920 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12921
12922 default:
12923 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12924 {
12925 /* Reverse the offset. */
12926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12927 bfd_size_type address_size = bed->s->arch_size / 8;
12928
12929 /* address_size and sec->size are in octets. Convert
12930 to bytes before subtracting the original offset. */
12931 offset = ((sec->size - address_size)
12932 / bfd_octets_per_byte (abfd, sec) - offset);
12933 }
12934 return offset;
12935 }
12936 }
12937 \f
12938 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12939 reconstruct an ELF file by reading the segments out of remote memory
12940 based on the ELF file header at EHDR_VMA and the ELF program headers it
12941 points to. If not null, *LOADBASEP is filled in with the difference
12942 between the VMAs from which the segments were read, and the VMAs the
12943 file headers (and hence BFD's idea of each section's VMA) put them at.
12944
12945 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12946 remote memory at target address VMA into the local buffer at MYADDR; it
12947 should return zero on success or an `errno' code on failure. TEMPL must
12948 be a BFD for an ELF target with the word size and byte order found in
12949 the remote memory. */
12950
12951 bfd *
12952 bfd_elf_bfd_from_remote_memory
12953 (bfd *templ,
12954 bfd_vma ehdr_vma,
12955 bfd_size_type size,
12956 bfd_vma *loadbasep,
12957 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12958 {
12959 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12960 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12961 }
12962 \f
12963 long
12964 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12965 long symcount ATTRIBUTE_UNUSED,
12966 asymbol **syms ATTRIBUTE_UNUSED,
12967 long dynsymcount,
12968 asymbol **dynsyms,
12969 asymbol **ret)
12970 {
12971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12972 asection *relplt;
12973 asymbol *s;
12974 const char *relplt_name;
12975 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12976 arelent *p;
12977 long count, i, n;
12978 size_t size;
12979 Elf_Internal_Shdr *hdr;
12980 char *names;
12981 asection *plt;
12982
12983 *ret = NULL;
12984
12985 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12986 return 0;
12987
12988 if (dynsymcount <= 0)
12989 return 0;
12990
12991 if (!bed->plt_sym_val)
12992 return 0;
12993
12994 relplt_name = bed->relplt_name;
12995 if (relplt_name == NULL)
12996 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12997 relplt = bfd_get_section_by_name (abfd, relplt_name);
12998 if (relplt == NULL)
12999 return 0;
13000
13001 hdr = &elf_section_data (relplt)->this_hdr;
13002 if (hdr->sh_link != elf_dynsymtab (abfd)
13003 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13004 return 0;
13005
13006 plt = bfd_get_section_by_name (abfd, ".plt");
13007 if (plt == NULL)
13008 return 0;
13009
13010 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13011 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13012 return -1;
13013
13014 count = relplt->size / hdr->sh_entsize;
13015 size = count * sizeof (asymbol);
13016 p = relplt->relocation;
13017 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13018 {
13019 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13020 if (p->addend != 0)
13021 {
13022 #ifdef BFD64
13023 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13024 #else
13025 size += sizeof ("+0x") - 1 + 8;
13026 #endif
13027 }
13028 }
13029
13030 s = *ret = (asymbol *) bfd_malloc (size);
13031 if (s == NULL)
13032 return -1;
13033
13034 names = (char *) (s + count);
13035 p = relplt->relocation;
13036 n = 0;
13037 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13038 {
13039 size_t len;
13040 bfd_vma addr;
13041
13042 addr = bed->plt_sym_val (i, plt, p);
13043 if (addr == (bfd_vma) -1)
13044 continue;
13045
13046 *s = **p->sym_ptr_ptr;
13047 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13048 we are defining a symbol, ensure one of them is set. */
13049 if ((s->flags & BSF_LOCAL) == 0)
13050 s->flags |= BSF_GLOBAL;
13051 s->flags |= BSF_SYNTHETIC;
13052 s->section = plt;
13053 s->value = addr - plt->vma;
13054 s->name = names;
13055 s->udata.p = NULL;
13056 len = strlen ((*p->sym_ptr_ptr)->name);
13057 memcpy (names, (*p->sym_ptr_ptr)->name, len);
13058 names += len;
13059 if (p->addend != 0)
13060 {
13061 char buf[30], *a;
13062
13063 memcpy (names, "+0x", sizeof ("+0x") - 1);
13064 names += sizeof ("+0x") - 1;
13065 bfd_sprintf_vma (abfd, buf, p->addend);
13066 for (a = buf; *a == '0'; ++a)
13067 ;
13068 len = strlen (a);
13069 memcpy (names, a, len);
13070 names += len;
13071 }
13072 memcpy (names, "@plt", sizeof ("@plt"));
13073 names += sizeof ("@plt");
13074 ++s, ++n;
13075 }
13076
13077 return n;
13078 }
13079
13080 /* It is only used by x86-64 so far.
13081 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13082 but current usage would allow all of _bfd_std_section to be zero. */
13083 static const asymbol lcomm_sym
13084 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13085 asection _bfd_elf_large_com_section
13086 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13087 "LARGE_COMMON", 0, SEC_IS_COMMON);
13088
13089 bool
13090 _bfd_elf_final_write_processing (bfd *abfd)
13091 {
13092 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13093
13094 i_ehdrp = elf_elfheader (abfd);
13095
13096 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13097 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13098
13099 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13100 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13101 or STB_GNU_UNIQUE binding. */
13102 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13103 {
13104 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13105 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13106 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13107 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13108 {
13109 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13110 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13111 "and FreeBSD targets"));
13112 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13113 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13114 "only by GNU and FreeBSD targets"));
13115 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13116 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13117 "only by GNU and FreeBSD targets"));
13118 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13119 _bfd_error_handler (_("GNU_RETAIN section is supported "
13120 "only by GNU and FreeBSD targets"));
13121 bfd_set_error (bfd_error_sorry);
13122 return false;
13123 }
13124 }
13125 return true;
13126 }
13127
13128
13129 /* Return TRUE for ELF symbol types that represent functions.
13130 This is the default version of this function, which is sufficient for
13131 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13132
13133 bool
13134 _bfd_elf_is_function_type (unsigned int type)
13135 {
13136 return (type == STT_FUNC
13137 || type == STT_GNU_IFUNC);
13138 }
13139
13140 /* If the ELF symbol SYM might be a function in SEC, return the
13141 function size and set *CODE_OFF to the function's entry point,
13142 otherwise return zero. */
13143
13144 bfd_size_type
13145 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13146 bfd_vma *code_off)
13147 {
13148 bfd_size_type size;
13149 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13150
13151 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13152 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13153 || sym->section != sec)
13154 return 0;
13155
13156 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13157
13158 /* In theory we should check that the symbol's type satisfies
13159 _bfd_elf_is_function_type(), but there are some function-like
13160 symbols which would fail this test. (eg _start). Instead
13161 we check for hidden, local, notype symbols with zero size.
13162 This type of symbol is generated by the annobin plugin for gcc
13163 and clang, and should not be considered to be a function symbol. */
13164 if (size == 0
13165 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13166 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13167 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13168 return 0;
13169
13170 *code_off = sym->value;
13171 /* Do not return 0 for the function's size. */
13172 return size ? size : 1;
13173 }
13174
13175 /* Set to non-zero to enable some debug messages. */
13176 #define DEBUG_SECONDARY_RELOCS 0
13177
13178 /* An internal-to-the-bfd-library only section type
13179 used to indicate a cached secondary reloc section. */
13180 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13181
13182 /* Create a BFD section to hold a secondary reloc section. */
13183
13184 bool
13185 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13186 Elf_Internal_Shdr *hdr,
13187 const char * name,
13188 unsigned int shindex)
13189 {
13190 /* We only support RELA secondary relocs. */
13191 if (hdr->sh_type != SHT_RELA)
13192 return false;
13193
13194 #if DEBUG_SECONDARY_RELOCS
13195 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13196 #endif
13197 hdr->sh_type = SHT_SECONDARY_RELOC;
13198 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13199 }
13200
13201 /* Read in any secondary relocs associated with SEC. */
13202
13203 bool
13204 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13205 asection * sec,
13206 asymbol ** symbols,
13207 bool dynamic)
13208 {
13209 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13210 asection * relsec;
13211 bool result = true;
13212 bfd_vma (*r_sym) (bfd_vma);
13213
13214 #if BFD_DEFAULT_TARGET_SIZE > 32
13215 if (bfd_arch_bits_per_address (abfd) != 32)
13216 r_sym = elf64_r_sym;
13217 else
13218 #endif
13219 r_sym = elf32_r_sym;
13220
13221 if (!elf_section_data (sec)->has_secondary_relocs)
13222 return true;
13223
13224 /* Discover if there are any secondary reloc sections
13225 associated with SEC. */
13226 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13227 {
13228 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13229
13230 if (hdr->sh_type == SHT_SECONDARY_RELOC
13231 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13232 && (hdr->sh_entsize == ebd->s->sizeof_rel
13233 || hdr->sh_entsize == ebd->s->sizeof_rela))
13234 {
13235 bfd_byte * native_relocs;
13236 bfd_byte * native_reloc;
13237 arelent * internal_relocs;
13238 arelent * internal_reloc;
13239 unsigned int i;
13240 unsigned int entsize;
13241 unsigned int symcount;
13242 unsigned int reloc_count;
13243 size_t amt;
13244
13245 if (ebd->elf_info_to_howto == NULL)
13246 return false;
13247
13248 #if DEBUG_SECONDARY_RELOCS
13249 fprintf (stderr, "read secondary relocs for %s from %s\n",
13250 sec->name, relsec->name);
13251 #endif
13252 entsize = hdr->sh_entsize;
13253
13254 native_relocs = bfd_malloc (hdr->sh_size);
13255 if (native_relocs == NULL)
13256 {
13257 result = false;
13258 continue;
13259 }
13260
13261 reloc_count = NUM_SHDR_ENTRIES (hdr);
13262 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13263 {
13264 free (native_relocs);
13265 bfd_set_error (bfd_error_file_too_big);
13266 result = false;
13267 continue;
13268 }
13269
13270 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13271 if (internal_relocs == NULL)
13272 {
13273 free (native_relocs);
13274 result = false;
13275 continue;
13276 }
13277
13278 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13279 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13280 != hdr->sh_size))
13281 {
13282 free (native_relocs);
13283 /* The internal_relocs will be freed when
13284 the memory for the bfd is released. */
13285 result = false;
13286 continue;
13287 }
13288
13289 if (dynamic)
13290 symcount = bfd_get_dynamic_symcount (abfd);
13291 else
13292 symcount = bfd_get_symcount (abfd);
13293
13294 for (i = 0, internal_reloc = internal_relocs,
13295 native_reloc = native_relocs;
13296 i < reloc_count;
13297 i++, internal_reloc++, native_reloc += entsize)
13298 {
13299 bool res;
13300 Elf_Internal_Rela rela;
13301
13302 if (entsize == ebd->s->sizeof_rel)
13303 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13304 else /* entsize == ebd->s->sizeof_rela */
13305 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13306
13307 /* The address of an ELF reloc is section relative for an object
13308 file, and absolute for an executable file or shared library.
13309 The address of a normal BFD reloc is always section relative,
13310 and the address of a dynamic reloc is absolute.. */
13311 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13312 internal_reloc->address = rela.r_offset;
13313 else
13314 internal_reloc->address = rela.r_offset - sec->vma;
13315
13316 if (r_sym (rela.r_info) == STN_UNDEF)
13317 {
13318 /* FIXME: This and the error case below mean that we
13319 have a symbol on relocs that is not elf_symbol_type. */
13320 internal_reloc->sym_ptr_ptr =
13321 bfd_abs_section_ptr->symbol_ptr_ptr;
13322 }
13323 else if (r_sym (rela.r_info) > symcount)
13324 {
13325 _bfd_error_handler
13326 /* xgettext:c-format */
13327 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13328 abfd, sec, i, (long) r_sym (rela.r_info));
13329 bfd_set_error (bfd_error_bad_value);
13330 internal_reloc->sym_ptr_ptr =
13331 bfd_abs_section_ptr->symbol_ptr_ptr;
13332 result = false;
13333 }
13334 else
13335 {
13336 asymbol **ps;
13337
13338 ps = symbols + r_sym (rela.r_info) - 1;
13339 internal_reloc->sym_ptr_ptr = ps;
13340 /* Make sure that this symbol is not removed by strip. */
13341 (*ps)->flags |= BSF_KEEP;
13342 }
13343
13344 internal_reloc->addend = rela.r_addend;
13345
13346 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13347 if (! res || internal_reloc->howto == NULL)
13348 {
13349 #if DEBUG_SECONDARY_RELOCS
13350 fprintf (stderr,
13351 "there is no howto associated with reloc %lx\n",
13352 rela.r_info);
13353 #endif
13354 result = false;
13355 }
13356 }
13357
13358 free (native_relocs);
13359 /* Store the internal relocs. */
13360 elf_section_data (relsec)->sec_info = internal_relocs;
13361 }
13362 }
13363
13364 return result;
13365 }
13366
13367 /* Set the ELF section header fields of an output secondary reloc section. */
13368
13369 bool
13370 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13371 bfd *obfd ATTRIBUTE_UNUSED,
13372 const Elf_Internal_Shdr *isection,
13373 Elf_Internal_Shdr *osection)
13374 {
13375 asection * isec;
13376 asection * osec;
13377 struct bfd_elf_section_data * esd;
13378
13379 if (isection == NULL)
13380 return false;
13381
13382 if (isection->sh_type != SHT_SECONDARY_RELOC)
13383 return true;
13384
13385 isec = isection->bfd_section;
13386 if (isec == NULL)
13387 return false;
13388
13389 osec = osection->bfd_section;
13390 if (osec == NULL)
13391 return false;
13392
13393 esd = elf_section_data (osec);
13394 BFD_ASSERT (esd->sec_info == NULL);
13395 esd->sec_info = elf_section_data (isec)->sec_info;
13396 osection->sh_type = SHT_RELA;
13397 osection->sh_link = elf_onesymtab (obfd);
13398 if (osection->sh_link == 0)
13399 {
13400 /* There is no symbol table - we are hosed... */
13401 _bfd_error_handler
13402 /* xgettext:c-format */
13403 (_("%pB(%pA): link section cannot be set"
13404 " because the output file does not have a symbol table"),
13405 obfd, osec);
13406 bfd_set_error (bfd_error_bad_value);
13407 return false;
13408 }
13409
13410 /* Find the output section that corresponds to the isection's
13411 sh_info link. */
13412 if (isection->sh_info == 0
13413 || isection->sh_info >= elf_numsections (ibfd))
13414 {
13415 _bfd_error_handler
13416 /* xgettext:c-format */
13417 (_("%pB(%pA): info section index is invalid"),
13418 obfd, osec);
13419 bfd_set_error (bfd_error_bad_value);
13420 return false;
13421 }
13422
13423 isection = elf_elfsections (ibfd)[isection->sh_info];
13424
13425 if (isection == NULL
13426 || isection->bfd_section == NULL
13427 || isection->bfd_section->output_section == NULL)
13428 {
13429 _bfd_error_handler
13430 /* xgettext:c-format */
13431 (_("%pB(%pA): info section index cannot be set"
13432 " because the section is not in the output"),
13433 obfd, osec);
13434 bfd_set_error (bfd_error_bad_value);
13435 return false;
13436 }
13437
13438 esd = elf_section_data (isection->bfd_section->output_section);
13439 BFD_ASSERT (esd != NULL);
13440 osection->sh_info = esd->this_idx;
13441 esd->has_secondary_relocs = true;
13442 #if DEBUG_SECONDARY_RELOCS
13443 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13444 osec->name, osection->sh_link, osection->sh_info);
13445 fprintf (stderr, "mark section %s as having secondary relocs\n",
13446 bfd_section_name (isection->bfd_section->output_section));
13447 #endif
13448
13449 return true;
13450 }
13451
13452 /* Write out a secondary reloc section.
13453
13454 FIXME: Currently this function can result in a serious performance penalty
13455 for files with secondary relocs and lots of sections. The proper way to
13456 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13457 relocs together and then to have this function just walk that chain. */
13458
13459 bool
13460 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13461 {
13462 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13463 bfd_vma addr_offset;
13464 asection * relsec;
13465 bfd_vma (*r_info) (bfd_vma, bfd_vma);
13466 bool result = true;
13467
13468 if (sec == NULL)
13469 return false;
13470
13471 #if BFD_DEFAULT_TARGET_SIZE > 32
13472 if (bfd_arch_bits_per_address (abfd) != 32)
13473 r_info = elf64_r_info;
13474 else
13475 #endif
13476 r_info = elf32_r_info;
13477
13478 /* The address of an ELF reloc is section relative for an object
13479 file, and absolute for an executable file or shared library.
13480 The address of a BFD reloc is always section relative. */
13481 addr_offset = 0;
13482 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13483 addr_offset = sec->vma;
13484
13485 /* Discover if there are any secondary reloc sections
13486 associated with SEC. */
13487 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13488 {
13489 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13490 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13491
13492 if (hdr->sh_type == SHT_RELA
13493 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13494 {
13495 asymbol * last_sym;
13496 int last_sym_idx;
13497 size_t reloc_count;
13498 size_t idx;
13499 bfd_size_type entsize;
13500 arelent * src_irel;
13501 bfd_byte * dst_rela;
13502
13503 if (hdr->contents != NULL)
13504 {
13505 _bfd_error_handler
13506 /* xgettext:c-format */
13507 (_("%pB(%pA): error: secondary reloc section processed twice"),
13508 abfd, relsec);
13509 bfd_set_error (bfd_error_bad_value);
13510 result = false;
13511 continue;
13512 }
13513
13514 entsize = hdr->sh_entsize;
13515 if (entsize == 0)
13516 {
13517 _bfd_error_handler
13518 /* xgettext:c-format */
13519 (_("%pB(%pA): error: secondary reloc section"
13520 " has zero sized entries"),
13521 abfd, relsec);
13522 bfd_set_error (bfd_error_bad_value);
13523 result = false;
13524 continue;
13525 }
13526 else if (entsize != ebd->s->sizeof_rel
13527 && entsize != ebd->s->sizeof_rela)
13528 {
13529 _bfd_error_handler
13530 /* xgettext:c-format */
13531 (_("%pB(%pA): error: secondary reloc section"
13532 " has non-standard sized entries"),
13533 abfd, relsec);
13534 bfd_set_error (bfd_error_bad_value);
13535 result = false;
13536 continue;
13537 }
13538
13539 reloc_count = hdr->sh_size / entsize;
13540 hdr->sh_size = entsize * reloc_count;
13541 if (reloc_count == 0)
13542 {
13543 _bfd_error_handler
13544 /* xgettext:c-format */
13545 (_("%pB(%pA): error: secondary reloc section is empty!"),
13546 abfd, relsec);
13547 bfd_set_error (bfd_error_bad_value);
13548 result = false;
13549 continue;
13550 }
13551
13552 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13553 if (hdr->contents == NULL)
13554 continue;
13555
13556 #if DEBUG_SECONDARY_RELOCS
13557 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13558 reloc_count, sec->name, relsec->name);
13559 #endif
13560 last_sym = NULL;
13561 last_sym_idx = 0;
13562 dst_rela = hdr->contents;
13563 src_irel = (arelent *) esd->sec_info;
13564 if (src_irel == NULL)
13565 {
13566 _bfd_error_handler
13567 /* xgettext:c-format */
13568 (_("%pB(%pA): error: internal relocs missing"
13569 " for secondary reloc section"),
13570 abfd, relsec);
13571 bfd_set_error (bfd_error_bad_value);
13572 result = false;
13573 continue;
13574 }
13575
13576 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13577 {
13578 Elf_Internal_Rela src_rela;
13579 arelent *ptr;
13580 asymbol *sym;
13581 int n;
13582
13583 ptr = src_irel + idx;
13584 if (ptr == NULL)
13585 {
13586 _bfd_error_handler
13587 /* xgettext:c-format */
13588 (_("%pB(%pA): error: reloc table entry %zu is empty"),
13589 abfd, relsec, idx);
13590 bfd_set_error (bfd_error_bad_value);
13591 result = false;
13592 break;
13593 }
13594
13595 if (ptr->sym_ptr_ptr == NULL)
13596 {
13597 /* FIXME: Is this an error ? */
13598 n = 0;
13599 }
13600 else
13601 {
13602 sym = *ptr->sym_ptr_ptr;
13603
13604 if (sym == last_sym)
13605 n = last_sym_idx;
13606 else
13607 {
13608 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13609 if (n < 0)
13610 {
13611 _bfd_error_handler
13612 /* xgettext:c-format */
13613 (_("%pB(%pA): error: secondary reloc %zu"
13614 " references a missing symbol"),
13615 abfd, relsec, idx);
13616 bfd_set_error (bfd_error_bad_value);
13617 result = false;
13618 n = 0;
13619 }
13620
13621 last_sym = sym;
13622 last_sym_idx = n;
13623 }
13624
13625 if (sym->the_bfd != NULL
13626 && sym->the_bfd->xvec != abfd->xvec
13627 && ! _bfd_elf_validate_reloc (abfd, ptr))
13628 {
13629 _bfd_error_handler
13630 /* xgettext:c-format */
13631 (_("%pB(%pA): error: secondary reloc %zu"
13632 " references a deleted symbol"),
13633 abfd, relsec, idx);
13634 bfd_set_error (bfd_error_bad_value);
13635 result = false;
13636 n = 0;
13637 }
13638 }
13639
13640 src_rela.r_offset = ptr->address + addr_offset;
13641 if (ptr->howto == NULL)
13642 {
13643 _bfd_error_handler
13644 /* xgettext:c-format */
13645 (_("%pB(%pA): error: secondary reloc %zu"
13646 " is of an unknown type"),
13647 abfd, relsec, idx);
13648 bfd_set_error (bfd_error_bad_value);
13649 result = false;
13650 src_rela.r_info = r_info (0, 0);
13651 }
13652 else
13653 src_rela.r_info = r_info (n, ptr->howto->type);
13654 src_rela.r_addend = ptr->addend;
13655
13656 if (entsize == ebd->s->sizeof_rel)
13657 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13658 else /* entsize == ebd->s->sizeof_rela */
13659 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13660 }
13661 }
13662 }
13663
13664 return result;
13665 }