Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2
3 Copyright (C) 1993-2021 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 to work. */
434 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
435 /* Otherwise we do nothing. The assumption is that
436 the index table will not be needed. */
437 }
438 }
439
440 /* Read the symbols. */
441 alloc_ext = NULL;
442 alloc_extshndx = NULL;
443 alloc_intsym = NULL;
444 bed = get_elf_backend_data (ibfd);
445 extsym_size = bed->s->sizeof_sym;
446 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
447 {
448 bfd_set_error (bfd_error_file_too_big);
449 intsym_buf = NULL;
450 goto out;
451 }
452 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
453 if (extsym_buf == NULL)
454 {
455 alloc_ext = bfd_malloc (amt);
456 extsym_buf = alloc_ext;
457 }
458 if (extsym_buf == NULL
459 || bfd_seek (ibfd, pos, SEEK_SET) != 0
460 || bfd_bread (extsym_buf, amt, ibfd) != amt)
461 {
462 intsym_buf = NULL;
463 goto out;
464 }
465
466 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
467 extshndx_buf = NULL;
468 else
469 {
470 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
471 {
472 bfd_set_error (bfd_error_file_too_big);
473 intsym_buf = NULL;
474 goto out;
475 }
476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
477 if (extshndx_buf == NULL)
478 {
479 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
480 extshndx_buf = alloc_extshndx;
481 }
482 if (extshndx_buf == NULL
483 || bfd_seek (ibfd, pos, SEEK_SET) != 0
484 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
485 {
486 intsym_buf = NULL;
487 goto out;
488 }
489 }
490
491 if (intsym_buf == NULL)
492 {
493 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
494 {
495 bfd_set_error (bfd_error_file_too_big);
496 goto out;
497 }
498 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
499 intsym_buf = alloc_intsym;
500 if (intsym_buf == NULL)
501 goto out;
502 }
503
504 /* Convert the symbols to internal form. */
505 isymend = intsym_buf + symcount;
506 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
507 shndx = extshndx_buf;
508 isym < isymend;
509 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
510 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
511 {
512 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
513 /* xgettext:c-format */
514 _bfd_error_handler (_("%pB symbol number %lu references"
515 " nonexistent SHT_SYMTAB_SHNDX section"),
516 ibfd, (unsigned long) symoffset);
517 free (alloc_intsym);
518 intsym_buf = NULL;
519 goto out;
520 }
521
522 out:
523 free (alloc_ext);
524 free (alloc_extshndx);
525
526 return intsym_buf;
527 }
528
529 /* Look up a symbol name. */
530 const char *
531 bfd_elf_sym_name (bfd *abfd,
532 Elf_Internal_Shdr *symtab_hdr,
533 Elf_Internal_Sym *isym,
534 asection *sym_sec)
535 {
536 const char *name;
537 unsigned int iname = isym->st_name;
538 unsigned int shindex = symtab_hdr->sh_link;
539
540 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
541 /* Check for a bogus st_shndx to avoid crashing. */
542 && isym->st_shndx < elf_numsections (abfd))
543 {
544 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
545 shindex = elf_elfheader (abfd)->e_shstrndx;
546 }
547
548 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
549 if (name == NULL)
550 name = "(null)";
551 else if (sym_sec && *name == '\0')
552 name = bfd_section_name (sym_sec);
553
554 return name;
555 }
556
557 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
558 sections. The first element is the flags, the rest are section
559 pointers. */
560
561 typedef union elf_internal_group {
562 Elf_Internal_Shdr *shdr;
563 unsigned int flags;
564 } Elf_Internal_Group;
565
566 /* Return the name of the group signature symbol. Why isn't the
567 signature just a string? */
568
569 static const char *
570 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
571 {
572 Elf_Internal_Shdr *hdr;
573 unsigned char esym[sizeof (Elf64_External_Sym)];
574 Elf_External_Sym_Shndx eshndx;
575 Elf_Internal_Sym isym;
576
577 /* First we need to ensure the symbol table is available. Make sure
578 that it is a symbol table section. */
579 if (ghdr->sh_link >= elf_numsections (abfd))
580 return NULL;
581 hdr = elf_elfsections (abfd) [ghdr->sh_link];
582 if (hdr->sh_type != SHT_SYMTAB
583 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
584 return NULL;
585
586 /* Go read the symbol. */
587 hdr = &elf_tdata (abfd)->symtab_hdr;
588 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
589 &isym, esym, &eshndx) == NULL)
590 return NULL;
591
592 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
593 }
594
595 /* Set next_in_group list pointer, and group name for NEWSECT. */
596
597 static bool
598 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
599 {
600 unsigned int num_group = elf_tdata (abfd)->num_group;
601
602 /* If num_group is zero, read in all SHT_GROUP sections. The count
603 is set to -1 if there are no SHT_GROUP sections. */
604 if (num_group == 0)
605 {
606 unsigned int i, shnum;
607
608 /* First count the number of groups. If we have a SHT_GROUP
609 section with just a flag word (ie. sh_size is 4), ignore it. */
610 shnum = elf_numsections (abfd);
611 num_group = 0;
612
613 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
614 ( (shdr)->sh_type == SHT_GROUP \
615 && (shdr)->sh_size >= minsize \
616 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
617 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
618
619 for (i = 0; i < shnum; i++)
620 {
621 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
622
623 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
624 num_group += 1;
625 }
626
627 if (num_group == 0)
628 {
629 num_group = (unsigned) -1;
630 elf_tdata (abfd)->num_group = num_group;
631 elf_tdata (abfd)->group_sect_ptr = NULL;
632 }
633 else
634 {
635 /* We keep a list of elf section headers for group sections,
636 so we can find them quickly. */
637 size_t amt;
638
639 elf_tdata (abfd)->num_group = num_group;
640 amt = num_group * sizeof (Elf_Internal_Shdr *);
641 elf_tdata (abfd)->group_sect_ptr
642 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
643 if (elf_tdata (abfd)->group_sect_ptr == NULL)
644 return false;
645 num_group = 0;
646
647 for (i = 0; i < shnum; i++)
648 {
649 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
650
651 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
652 {
653 unsigned char *src;
654 Elf_Internal_Group *dest;
655
656 /* Make sure the group section has a BFD section
657 attached to it. */
658 if (!bfd_section_from_shdr (abfd, i))
659 return false;
660
661 /* Add to list of sections. */
662 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
663 num_group += 1;
664
665 /* Read the raw contents. */
666 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
667 shdr->contents = NULL;
668 if (_bfd_mul_overflow (shdr->sh_size,
669 sizeof (*dest) / 4, &amt)
670 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
671 || !(shdr->contents
672 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
673 {
674 _bfd_error_handler
675 /* xgettext:c-format */
676 (_("%pB: invalid size field in group section"
677 " header: %#" PRIx64 ""),
678 abfd, (uint64_t) shdr->sh_size);
679 bfd_set_error (bfd_error_bad_value);
680 -- num_group;
681 continue;
682 }
683
684 /* Translate raw contents, a flag word followed by an
685 array of elf section indices all in target byte order,
686 to the flag word followed by an array of elf section
687 pointers. */
688 src = shdr->contents + shdr->sh_size;
689 dest = (Elf_Internal_Group *) (shdr->contents + amt);
690
691 while (1)
692 {
693 unsigned int idx;
694
695 src -= 4;
696 --dest;
697 idx = H_GET_32 (abfd, src);
698 if (src == shdr->contents)
699 {
700 dest->shdr = NULL;
701 dest->flags = idx;
702 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
703 shdr->bfd_section->flags
704 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
705 break;
706 }
707 if (idx < shnum)
708 {
709 dest->shdr = elf_elfsections (abfd)[idx];
710 /* PR binutils/23199: All sections in a
711 section group should be marked with
712 SHF_GROUP. But some tools generate
713 broken objects without SHF_GROUP. Fix
714 them up here. */
715 dest->shdr->sh_flags |= SHF_GROUP;
716 }
717 if (idx >= shnum
718 || dest->shdr->sh_type == SHT_GROUP)
719 {
720 _bfd_error_handler
721 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
722 abfd, i);
723 dest->shdr = NULL;
724 }
725 }
726 }
727 }
728
729 /* PR 17510: Corrupt binaries might contain invalid groups. */
730 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
731 {
732 elf_tdata (abfd)->num_group = num_group;
733
734 /* If all groups are invalid then fail. */
735 if (num_group == 0)
736 {
737 elf_tdata (abfd)->group_sect_ptr = NULL;
738 elf_tdata (abfd)->num_group = num_group = -1;
739 _bfd_error_handler
740 (_("%pB: no valid group sections found"), abfd);
741 bfd_set_error (bfd_error_bad_value);
742 }
743 }
744 }
745 }
746
747 if (num_group != (unsigned) -1)
748 {
749 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
750 unsigned int j;
751
752 for (j = 0; j < num_group; j++)
753 {
754 /* Begin search from previous found group. */
755 unsigned i = (j + search_offset) % num_group;
756
757 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
758 Elf_Internal_Group *idx;
759 bfd_size_type n_elt;
760
761 if (shdr == NULL)
762 continue;
763
764 idx = (Elf_Internal_Group *) shdr->contents;
765 if (idx == NULL || shdr->sh_size < 4)
766 {
767 /* See PR 21957 for a reproducer. */
768 /* xgettext:c-format */
769 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
770 abfd, shdr->bfd_section);
771 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
772 bfd_set_error (bfd_error_bad_value);
773 return false;
774 }
775 n_elt = shdr->sh_size / 4;
776
777 /* Look through this group's sections to see if current
778 section is a member. */
779 while (--n_elt != 0)
780 if ((++idx)->shdr == hdr)
781 {
782 asection *s = NULL;
783
784 /* We are a member of this group. Go looking through
785 other members to see if any others are linked via
786 next_in_group. */
787 idx = (Elf_Internal_Group *) shdr->contents;
788 n_elt = shdr->sh_size / 4;
789 while (--n_elt != 0)
790 if ((++idx)->shdr != NULL
791 && (s = idx->shdr->bfd_section) != NULL
792 && elf_next_in_group (s) != NULL)
793 break;
794 if (n_elt != 0)
795 {
796 /* Snarf the group name from other member, and
797 insert current section in circular list. */
798 elf_group_name (newsect) = elf_group_name (s);
799 elf_next_in_group (newsect) = elf_next_in_group (s);
800 elf_next_in_group (s) = newsect;
801 }
802 else
803 {
804 const char *gname;
805
806 gname = group_signature (abfd, shdr);
807 if (gname == NULL)
808 return false;
809 elf_group_name (newsect) = gname;
810
811 /* Start a circular list with one element. */
812 elf_next_in_group (newsect) = newsect;
813 }
814
815 /* If the group section has been created, point to the
816 new member. */
817 if (shdr->bfd_section != NULL)
818 elf_next_in_group (shdr->bfd_section) = newsect;
819
820 elf_tdata (abfd)->group_search_offset = i;
821 j = num_group - 1;
822 break;
823 }
824 }
825 }
826
827 if (elf_group_name (newsect) == NULL)
828 {
829 /* xgettext:c-format */
830 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
831 abfd, newsect);
832 return false;
833 }
834 return true;
835 }
836
837 bool
838 _bfd_elf_setup_sections (bfd *abfd)
839 {
840 unsigned int i;
841 unsigned int num_group = elf_tdata (abfd)->num_group;
842 bool result = true;
843 asection *s;
844
845 /* Process SHF_LINK_ORDER. */
846 for (s = abfd->sections; s != NULL; s = s->next)
847 {
848 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
849 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
850 {
851 unsigned int elfsec = this_hdr->sh_link;
852 /* An sh_link value of 0 is now allowed. It indicates that linked
853 to section has already been discarded, but that the current
854 section has been retained for some other reason. This linking
855 section is still a candidate for later garbage collection
856 however. */
857 if (elfsec == 0)
858 {
859 elf_linked_to_section (s) = NULL;
860 }
861 else
862 {
863 asection *linksec = NULL;
864
865 if (elfsec < elf_numsections (abfd))
866 {
867 this_hdr = elf_elfsections (abfd)[elfsec];
868 linksec = this_hdr->bfd_section;
869 }
870
871 /* PR 1991, 2008:
872 Some strip/objcopy may leave an incorrect value in
873 sh_link. We don't want to proceed. */
874 if (linksec == NULL)
875 {
876 _bfd_error_handler
877 /* xgettext:c-format */
878 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
879 s->owner, elfsec, s);
880 result = false;
881 }
882
883 elf_linked_to_section (s) = linksec;
884 }
885 }
886 else if (this_hdr->sh_type == SHT_GROUP
887 && elf_next_in_group (s) == NULL)
888 {
889 _bfd_error_handler
890 /* xgettext:c-format */
891 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
892 abfd, elf_section_data (s)->this_idx);
893 result = false;
894 }
895 }
896
897 /* Process section groups. */
898 if (num_group == (unsigned) -1)
899 return result;
900
901 for (i = 0; i < num_group; i++)
902 {
903 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
904 Elf_Internal_Group *idx;
905 unsigned int n_elt;
906
907 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
908 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
909 {
910 _bfd_error_handler
911 /* xgettext:c-format */
912 (_("%pB: section group entry number %u is corrupt"),
913 abfd, i);
914 result = false;
915 continue;
916 }
917
918 idx = (Elf_Internal_Group *) shdr->contents;
919 n_elt = shdr->sh_size / 4;
920
921 while (--n_elt != 0)
922 {
923 ++ idx;
924
925 if (idx->shdr == NULL)
926 continue;
927 else if (idx->shdr->bfd_section)
928 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
929 else if (idx->shdr->sh_type != SHT_RELA
930 && idx->shdr->sh_type != SHT_REL)
931 {
932 /* There are some unknown sections in the group. */
933 _bfd_error_handler
934 /* xgettext:c-format */
935 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
936 abfd,
937 idx->shdr->sh_type,
938 bfd_elf_string_from_elf_section (abfd,
939 (elf_elfheader (abfd)
940 ->e_shstrndx),
941 idx->shdr->sh_name),
942 shdr->bfd_section);
943 result = false;
944 }
945 }
946 }
947
948 return result;
949 }
950
951 bool
952 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
953 {
954 return elf_next_in_group (sec) != NULL;
955 }
956
957 const char *
958 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959 {
960 if (elf_sec_group (sec) != NULL)
961 return elf_group_name (sec);
962 return NULL;
963 }
964
965 static char *
966 convert_debug_to_zdebug (bfd *abfd, const char *name)
967 {
968 unsigned int len = strlen (name);
969 char *new_name = bfd_alloc (abfd, len + 2);
970 if (new_name == NULL)
971 return NULL;
972 new_name[0] = '.';
973 new_name[1] = 'z';
974 memcpy (new_name + 2, name + 1, len);
975 return new_name;
976 }
977
978 static char *
979 convert_zdebug_to_debug (bfd *abfd, const char *name)
980 {
981 unsigned int len = strlen (name);
982 char *new_name = bfd_alloc (abfd, len);
983 if (new_name == NULL)
984 return NULL;
985 new_name[0] = '.';
986 memcpy (new_name + 1, name + 2, len - 1);
987 return new_name;
988 }
989
990 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
991
992 struct lto_section
993 {
994 int16_t major_version;
995 int16_t minor_version;
996 unsigned char slim_object;
997
998 /* Flags is a private field that is not defined publicly. */
999 uint16_t flags;
1000 };
1001
1002 /* Make a BFD section from an ELF section. We store a pointer to the
1003 BFD section in the bfd_section field of the header. */
1004
1005 bool
1006 _bfd_elf_make_section_from_shdr (bfd *abfd,
1007 Elf_Internal_Shdr *hdr,
1008 const char *name,
1009 int shindex)
1010 {
1011 asection *newsect;
1012 flagword flags;
1013 const struct elf_backend_data *bed;
1014 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
1015
1016 if (hdr->bfd_section != NULL)
1017 return true;
1018
1019 newsect = bfd_make_section_anyway (abfd, name);
1020 if (newsect == NULL)
1021 return false;
1022
1023 hdr->bfd_section = newsect;
1024 elf_section_data (newsect)->this_hdr = *hdr;
1025 elf_section_data (newsect)->this_idx = shindex;
1026
1027 /* Always use the real type/flags. */
1028 elf_section_type (newsect) = hdr->sh_type;
1029 elf_section_flags (newsect) = hdr->sh_flags;
1030
1031 newsect->filepos = hdr->sh_offset;
1032
1033 flags = SEC_NO_FLAGS;
1034 if (hdr->sh_type != SHT_NOBITS)
1035 flags |= SEC_HAS_CONTENTS;
1036 if (hdr->sh_type == SHT_GROUP)
1037 flags |= SEC_GROUP;
1038 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1039 {
1040 flags |= SEC_ALLOC;
1041 if (hdr->sh_type != SHT_NOBITS)
1042 flags |= SEC_LOAD;
1043 }
1044 if ((hdr->sh_flags & SHF_WRITE) == 0)
1045 flags |= SEC_READONLY;
1046 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1047 flags |= SEC_CODE;
1048 else if ((flags & SEC_LOAD) != 0)
1049 flags |= SEC_DATA;
1050 if ((hdr->sh_flags & SHF_MERGE) != 0)
1051 {
1052 flags |= SEC_MERGE;
1053 newsect->entsize = hdr->sh_entsize;
1054 }
1055 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1056 flags |= SEC_STRINGS;
1057 if (hdr->sh_flags & SHF_GROUP)
1058 if (!setup_group (abfd, hdr, newsect))
1059 return false;
1060 if ((hdr->sh_flags & SHF_TLS) != 0)
1061 flags |= SEC_THREAD_LOCAL;
1062 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1063 flags |= SEC_EXCLUDE;
1064
1065 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1066 {
1067 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1068 but binutils as of 2019-07-23 did not set the EI_OSABI header
1069 byte. */
1070 case ELFOSABI_GNU:
1071 case ELFOSABI_FREEBSD:
1072 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1073 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1074 /* Fall through */
1075 case ELFOSABI_NONE:
1076 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1077 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1078 break;
1079 }
1080
1081 if ((flags & SEC_ALLOC) == 0)
1082 {
1083 /* The debugging sections appear to be recognized only by name,
1084 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1085 if (name [0] == '.')
1086 {
1087 if (startswith (name, ".debug")
1088 || startswith (name, ".gnu.debuglto_.debug_")
1089 || startswith (name, ".gnu.linkonce.wi.")
1090 || startswith (name, ".zdebug"))
1091 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1092 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1093 || startswith (name, ".note.gnu"))
1094 {
1095 flags |= SEC_ELF_OCTETS;
1096 opb = 1;
1097 }
1098 else if (startswith (name, ".line")
1099 || startswith (name, ".stab")
1100 || strcmp (name, ".gdb_index") == 0)
1101 flags |= SEC_DEBUGGING;
1102 }
1103 }
1104
1105 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1106 || !bfd_set_section_size (newsect, hdr->sh_size)
1107 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
1108 return false;
1109
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
1116 if (startswith (name, ".gnu.linkonce")
1117 && elf_next_in_group (newsect) == NULL)
1118 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1119
1120 if (!bfd_set_section_flags (newsect, flags))
1121 return false;
1122
1123 bed = get_elf_backend_data (abfd);
1124 if (bed->elf_backend_section_flags)
1125 if (!bed->elf_backend_section_flags (hdr))
1126 return false;
1127
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr->sh_type == SHT_NOTE)
1132 {
1133 bfd_byte *contents;
1134
1135 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1136 return false;
1137
1138 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1139 hdr->sh_offset, hdr->sh_addralign);
1140 free (contents);
1141 }
1142
1143 if ((newsect->flags & SEC_ALLOC) != 0)
1144 {
1145 Elf_Internal_Phdr *phdr;
1146 unsigned int i, nload;
1147
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr = elf_tdata (abfd)->phdr;
1153 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1154 if (phdr->p_paddr != 0)
1155 break;
1156 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1157 ++nload;
1158 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1159 return true;
1160
1161 phdr = elf_tdata (abfd)->phdr;
1162 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1163 {
1164 if (((phdr->p_type == PT_LOAD
1165 && (hdr->sh_flags & SHF_TLS) == 0)
1166 || phdr->p_type == PT_TLS)
1167 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1168 {
1169 if ((newsect->flags & SEC_LOAD) == 0)
1170 newsect->lma = (phdr->p_paddr
1171 + hdr->sh_addr - phdr->p_vaddr) / opb;
1172 else
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect->lma = (phdr->p_paddr
1181 + hdr->sh_offset - phdr->p_offset) / opb;
1182
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr->sh_addr >= phdr->p_vaddr
1188 && (hdr->sh_addr + hdr->sh_size
1189 <= phdr->p_vaddr + phdr->p_memsz))
1190 break;
1191 }
1192 }
1193 }
1194
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
1197 if ((newsect->flags & SEC_DEBUGGING)
1198 && ((name[1] == 'd' && name[6] == '_')
1199 || (name[1] == 'z' && name[7] == '_')))
1200 {
1201 enum { nothing, compress, decompress } action = nothing;
1202 int compression_header_size;
1203 bfd_size_type uncompressed_size;
1204 unsigned int uncompressed_align_power;
1205 bool compressed
1206 = bfd_is_section_compressed_with_header (abfd, newsect,
1207 &compression_header_size,
1208 &uncompressed_size,
1209 &uncompressed_align_power);
1210 if (compressed)
1211 {
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd->flags & BFD_DECOMPRESS))
1214 action = decompress;
1215 }
1216
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action == nothing)
1220 {
1221 if (newsect->size != 0
1222 && (abfd->flags & BFD_COMPRESS)
1223 && compression_header_size >= 0
1224 && uncompressed_size > 0
1225 && (!compressed
1226 || ((compression_header_size > 0)
1227 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1228 action = compress;
1229 else
1230 return true;
1231 }
1232
1233 if (action == compress)
1234 {
1235 if (!bfd_init_section_compress_status (abfd, newsect))
1236 {
1237 _bfd_error_handler
1238 /* xgettext:c-format */
1239 (_("%pB: unable to initialize compress status for section %s"),
1240 abfd, name);
1241 return false;
1242 }
1243 }
1244 else
1245 {
1246 if (!bfd_init_section_decompress_status (abfd, newsect))
1247 {
1248 _bfd_error_handler
1249 /* xgettext:c-format */
1250 (_("%pB: unable to initialize decompress status for section %s"),
1251 abfd, name);
1252 return false;
1253 }
1254 }
1255
1256 if (abfd->is_linker_input)
1257 {
1258 if (name[1] == 'z'
1259 && (action == decompress
1260 || (action == compress
1261 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1262 {
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1265 section. */
1266 char *new_name = convert_zdebug_to_debug (abfd, name);
1267 if (new_name == NULL)
1268 return false;
1269 bfd_rename_section (newsect, new_name);
1270 }
1271 }
1272 else
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect->flags |= SEC_ELF_RENAME;
1276 }
1277
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1279 section. */
1280 if (startswith (name, ".gnu.lto_.lto."))
1281 {
1282 struct lto_section lsection;
1283 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1284 sizeof (struct lto_section)))
1285 abfd->lto_slim_object = lsection.slim_object;
1286 }
1287
1288 return true;
1289 }
1290
1291 const char *const bfd_elf_section_type_names[] =
1292 {
1293 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1294 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1295 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1296 };
1297
1298 /* ELF relocs are against symbols. If we are producing relocatable
1299 output, and the reloc is against an external symbol, and nothing
1300 has given us any additional addend, the resulting reloc will also
1301 be against the same symbol. In such a case, we don't want to
1302 change anything about the way the reloc is handled, since it will
1303 all be done at final link time. Rather than put special case code
1304 into bfd_perform_relocation, all the reloc types use this howto
1305 function, or should call this function for relocatable output. */
1306
1307 bfd_reloc_status_type
1308 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1309 arelent *reloc_entry,
1310 asymbol *symbol,
1311 void *data ATTRIBUTE_UNUSED,
1312 asection *input_section,
1313 bfd *output_bfd,
1314 char **error_message ATTRIBUTE_UNUSED)
1315 {
1316 if (output_bfd != NULL
1317 && (symbol->flags & BSF_SECTION_SYM) == 0
1318 && (! reloc_entry->howto->partial_inplace
1319 || reloc_entry->addend == 0))
1320 {
1321 reloc_entry->address += input_section->output_offset;
1322 return bfd_reloc_ok;
1323 }
1324
1325 /* In some cases the relocation should be treated as output section
1326 relative, as when linking ELF DWARF into PE COFF. Many ELF
1327 targets lack section relative relocations and instead use
1328 ordinary absolute relocations for references between DWARF
1329 sections. That is arguably a bug in those targets but it happens
1330 to work for the usual case of linking to non-loaded ELF debug
1331 sections with VMAs forced to zero. PE COFF on the other hand
1332 doesn't allow a section VMA of zero. */
1333 if (output_bfd == NULL
1334 && !reloc_entry->howto->pc_relative
1335 && (symbol->section->flags & SEC_DEBUGGING) != 0
1336 && (input_section->flags & SEC_DEBUGGING) != 0)
1337 reloc_entry->addend -= symbol->section->output_section->vma;
1338
1339 return bfd_reloc_continue;
1340 }
1341 \f
1342 /* Returns TRUE if section A matches section B.
1343 Names, addresses and links may be different, but everything else
1344 should be the same. */
1345
1346 static bool
1347 section_match (const Elf_Internal_Shdr * a,
1348 const Elf_Internal_Shdr * b)
1349 {
1350 if (a->sh_type != b->sh_type
1351 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1352 || a->sh_addralign != b->sh_addralign
1353 || a->sh_entsize != b->sh_entsize)
1354 return false;
1355 if (a->sh_type == SHT_SYMTAB
1356 || a->sh_type == SHT_STRTAB)
1357 return true;
1358 return a->sh_size == b->sh_size;
1359 }
1360
1361 /* Find a section in OBFD that has the same characteristics
1362 as IHEADER. Return the index of this section or SHN_UNDEF if
1363 none can be found. Check's section HINT first, as this is likely
1364 to be the correct section. */
1365
1366 static unsigned int
1367 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1368 const unsigned int hint)
1369 {
1370 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1371 unsigned int i;
1372
1373 BFD_ASSERT (iheader != NULL);
1374
1375 /* See PR 20922 for a reproducer of the NULL test. */
1376 if (hint < elf_numsections (obfd)
1377 && oheaders[hint] != NULL
1378 && section_match (oheaders[hint], iheader))
1379 return hint;
1380
1381 for (i = 1; i < elf_numsections (obfd); i++)
1382 {
1383 Elf_Internal_Shdr * oheader = oheaders[i];
1384
1385 if (oheader == NULL)
1386 continue;
1387 if (section_match (oheader, iheader))
1388 /* FIXME: Do we care if there is a potential for
1389 multiple matches ? */
1390 return i;
1391 }
1392
1393 return SHN_UNDEF;
1394 }
1395
1396 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1397 Processor specific section, based upon a matching input section.
1398 Returns TRUE upon success, FALSE otherwise. */
1399
1400 static bool
1401 copy_special_section_fields (const bfd *ibfd,
1402 bfd *obfd,
1403 const Elf_Internal_Shdr *iheader,
1404 Elf_Internal_Shdr *oheader,
1405 const unsigned int secnum)
1406 {
1407 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1408 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1409 bool changed = false;
1410 unsigned int sh_link;
1411
1412 if (oheader->sh_type == SHT_NOBITS)
1413 {
1414 /* This is a feature for objcopy --only-keep-debug:
1415 When a section's type is changed to NOBITS, we preserve
1416 the sh_link and sh_info fields so that they can be
1417 matched up with the original.
1418
1419 Note: Strictly speaking these assignments are wrong.
1420 The sh_link and sh_info fields should point to the
1421 relevent sections in the output BFD, which may not be in
1422 the same location as they were in the input BFD. But
1423 the whole point of this action is to preserve the
1424 original values of the sh_link and sh_info fields, so
1425 that they can be matched up with the section headers in
1426 the original file. So strictly speaking we may be
1427 creating an invalid ELF file, but it is only for a file
1428 that just contains debug info and only for sections
1429 without any contents. */
1430 if (oheader->sh_link == 0)
1431 oheader->sh_link = iheader->sh_link;
1432 if (oheader->sh_info == 0)
1433 oheader->sh_info = iheader->sh_info;
1434 return true;
1435 }
1436
1437 /* Allow the target a chance to decide how these fields should be set. */
1438 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1439 iheader, oheader))
1440 return true;
1441
1442 /* We have an iheader which might match oheader, and which has non-zero
1443 sh_info and/or sh_link fields. Attempt to follow those links and find
1444 the section in the output bfd which corresponds to the linked section
1445 in the input bfd. */
1446 if (iheader->sh_link != SHN_UNDEF)
1447 {
1448 /* See PR 20931 for a reproducer. */
1449 if (iheader->sh_link >= elf_numsections (ibfd))
1450 {
1451 _bfd_error_handler
1452 /* xgettext:c-format */
1453 (_("%pB: invalid sh_link field (%d) in section number %d"),
1454 ibfd, iheader->sh_link, secnum);
1455 return false;
1456 }
1457
1458 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1459 if (sh_link != SHN_UNDEF)
1460 {
1461 oheader->sh_link = sh_link;
1462 changed = true;
1463 }
1464 else
1465 /* FIXME: Should we install iheader->sh_link
1466 if we could not find a match ? */
1467 _bfd_error_handler
1468 /* xgettext:c-format */
1469 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1470 }
1471
1472 if (iheader->sh_info)
1473 {
1474 /* The sh_info field can hold arbitrary information, but if the
1475 SHF_LINK_INFO flag is set then it should be interpreted as a
1476 section index. */
1477 if (iheader->sh_flags & SHF_INFO_LINK)
1478 {
1479 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1480 iheader->sh_info);
1481 if (sh_link != SHN_UNDEF)
1482 oheader->sh_flags |= SHF_INFO_LINK;
1483 }
1484 else
1485 /* No idea what it means - just copy it. */
1486 sh_link = iheader->sh_info;
1487
1488 if (sh_link != SHN_UNDEF)
1489 {
1490 oheader->sh_info = sh_link;
1491 changed = true;
1492 }
1493 else
1494 _bfd_error_handler
1495 /* xgettext:c-format */
1496 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1497 }
1498
1499 return changed;
1500 }
1501
1502 /* Copy the program header and other data from one object module to
1503 another. */
1504
1505 bool
1506 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1507 {
1508 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1509 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1510 const struct elf_backend_data *bed;
1511 unsigned int i;
1512
1513 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1514 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1515 return true;
1516
1517 if (!elf_flags_init (obfd))
1518 {
1519 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1520 elf_flags_init (obfd) = true;
1521 }
1522
1523 elf_gp (obfd) = elf_gp (ibfd);
1524
1525 /* Also copy the EI_OSABI field. */
1526 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1527 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1528
1529 /* If set, copy the EI_ABIVERSION field. */
1530 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1531 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1532 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1533
1534 /* Copy object attributes. */
1535 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1536
1537 if (iheaders == NULL || oheaders == NULL)
1538 return true;
1539
1540 bed = get_elf_backend_data (obfd);
1541
1542 /* Possibly copy other fields in the section header. */
1543 for (i = 1; i < elf_numsections (obfd); i++)
1544 {
1545 unsigned int j;
1546 Elf_Internal_Shdr * oheader = oheaders[i];
1547
1548 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1549 because of a special case need for generating separate debug info
1550 files. See below for more details. */
1551 if (oheader == NULL
1552 || (oheader->sh_type != SHT_NOBITS
1553 && oheader->sh_type < SHT_LOOS))
1554 continue;
1555
1556 /* Ignore empty sections, and sections whose
1557 fields have already been initialised. */
1558 if (oheader->sh_size == 0
1559 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1560 continue;
1561
1562 /* Scan for the matching section in the input bfd.
1563 First we try for a direct mapping between the input and output sections. */
1564 for (j = 1; j < elf_numsections (ibfd); j++)
1565 {
1566 const Elf_Internal_Shdr * iheader = iheaders[j];
1567
1568 if (iheader == NULL)
1569 continue;
1570
1571 if (oheader->bfd_section != NULL
1572 && iheader->bfd_section != NULL
1573 && iheader->bfd_section->output_section != NULL
1574 && iheader->bfd_section->output_section == oheader->bfd_section)
1575 {
1576 /* We have found a connection from the input section to the
1577 output section. Attempt to copy the header fields. If
1578 this fails then do not try any further sections - there
1579 should only be a one-to-one mapping between input and output. */
1580 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1581 j = elf_numsections (ibfd);
1582 break;
1583 }
1584 }
1585
1586 if (j < elf_numsections (ibfd))
1587 continue;
1588
1589 /* That failed. So try to deduce the corresponding input section.
1590 Unfortunately we cannot compare names as the output string table
1591 is empty, so instead we check size, address and type. */
1592 for (j = 1; j < elf_numsections (ibfd); j++)
1593 {
1594 const Elf_Internal_Shdr * iheader = iheaders[j];
1595
1596 if (iheader == NULL)
1597 continue;
1598
1599 /* Try matching fields in the input section's header.
1600 Since --only-keep-debug turns all non-debug sections into
1601 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1602 input type. */
1603 if ((oheader->sh_type == SHT_NOBITS
1604 || iheader->sh_type == oheader->sh_type)
1605 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1606 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1607 && iheader->sh_addralign == oheader->sh_addralign
1608 && iheader->sh_entsize == oheader->sh_entsize
1609 && iheader->sh_size == oheader->sh_size
1610 && iheader->sh_addr == oheader->sh_addr
1611 && (iheader->sh_info != oheader->sh_info
1612 || iheader->sh_link != oheader->sh_link))
1613 {
1614 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1615 break;
1616 }
1617 }
1618
1619 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1620 {
1621 /* Final attempt. Call the backend copy function
1622 with a NULL input section. */
1623 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1624 NULL, oheader);
1625 }
1626 }
1627
1628 return true;
1629 }
1630
1631 static const char *
1632 get_segment_type (unsigned int p_type)
1633 {
1634 const char *pt;
1635 switch (p_type)
1636 {
1637 case PT_NULL: pt = "NULL"; break;
1638 case PT_LOAD: pt = "LOAD"; break;
1639 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1640 case PT_INTERP: pt = "INTERP"; break;
1641 case PT_NOTE: pt = "NOTE"; break;
1642 case PT_SHLIB: pt = "SHLIB"; break;
1643 case PT_PHDR: pt = "PHDR"; break;
1644 case PT_TLS: pt = "TLS"; break;
1645 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1646 case PT_GNU_STACK: pt = "STACK"; break;
1647 case PT_GNU_RELRO: pt = "RELRO"; break;
1648 default: pt = NULL; break;
1649 }
1650 return pt;
1651 }
1652
1653 /* Print out the program headers. */
1654
1655 bool
1656 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1657 {
1658 FILE *f = (FILE *) farg;
1659 Elf_Internal_Phdr *p;
1660 asection *s;
1661 bfd_byte *dynbuf = NULL;
1662
1663 p = elf_tdata (abfd)->phdr;
1664 if (p != NULL)
1665 {
1666 unsigned int i, c;
1667
1668 fprintf (f, _("\nProgram Header:\n"));
1669 c = elf_elfheader (abfd)->e_phnum;
1670 for (i = 0; i < c; i++, p++)
1671 {
1672 const char *pt = get_segment_type (p->p_type);
1673 char buf[20];
1674
1675 if (pt == NULL)
1676 {
1677 sprintf (buf, "0x%lx", p->p_type);
1678 pt = buf;
1679 }
1680 fprintf (f, "%8s off 0x", pt);
1681 bfd_fprintf_vma (abfd, f, p->p_offset);
1682 fprintf (f, " vaddr 0x");
1683 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1684 fprintf (f, " paddr 0x");
1685 bfd_fprintf_vma (abfd, f, p->p_paddr);
1686 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1687 fprintf (f, " filesz 0x");
1688 bfd_fprintf_vma (abfd, f, p->p_filesz);
1689 fprintf (f, " memsz 0x");
1690 bfd_fprintf_vma (abfd, f, p->p_memsz);
1691 fprintf (f, " flags %c%c%c",
1692 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1693 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1694 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1695 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1696 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1697 fprintf (f, "\n");
1698 }
1699 }
1700
1701 s = bfd_get_section_by_name (abfd, ".dynamic");
1702 if (s != NULL)
1703 {
1704 unsigned int elfsec;
1705 unsigned long shlink;
1706 bfd_byte *extdyn, *extdynend;
1707 size_t extdynsize;
1708 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1709
1710 fprintf (f, _("\nDynamic Section:\n"));
1711
1712 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1713 goto error_return;
1714
1715 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1716 if (elfsec == SHN_BAD)
1717 goto error_return;
1718 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1719
1720 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1721 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1722
1723 extdyn = dynbuf;
1724 /* PR 17512: file: 6f427532. */
1725 if (s->size < extdynsize)
1726 goto error_return;
1727 extdynend = extdyn + s->size;
1728 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1729 Fix range check. */
1730 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1731 {
1732 Elf_Internal_Dyn dyn;
1733 const char *name = "";
1734 char ab[20];
1735 bool stringp;
1736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1737
1738 (*swap_dyn_in) (abfd, extdyn, &dyn);
1739
1740 if (dyn.d_tag == DT_NULL)
1741 break;
1742
1743 stringp = false;
1744 switch (dyn.d_tag)
1745 {
1746 default:
1747 if (bed->elf_backend_get_target_dtag)
1748 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1749
1750 if (!strcmp (name, ""))
1751 {
1752 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1753 name = ab;
1754 }
1755 break;
1756
1757 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1758 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1759 case DT_PLTGOT: name = "PLTGOT"; break;
1760 case DT_HASH: name = "HASH"; break;
1761 case DT_STRTAB: name = "STRTAB"; break;
1762 case DT_SYMTAB: name = "SYMTAB"; break;
1763 case DT_RELA: name = "RELA"; break;
1764 case DT_RELASZ: name = "RELASZ"; break;
1765 case DT_RELAENT: name = "RELAENT"; break;
1766 case DT_STRSZ: name = "STRSZ"; break;
1767 case DT_SYMENT: name = "SYMENT"; break;
1768 case DT_INIT: name = "INIT"; break;
1769 case DT_FINI: name = "FINI"; break;
1770 case DT_SONAME: name = "SONAME"; stringp = true; break;
1771 case DT_RPATH: name = "RPATH"; stringp = true; break;
1772 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1773 case DT_REL: name = "REL"; break;
1774 case DT_RELSZ: name = "RELSZ"; break;
1775 case DT_RELENT: name = "RELENT"; break;
1776 case DT_PLTREL: name = "PLTREL"; break;
1777 case DT_DEBUG: name = "DEBUG"; break;
1778 case DT_TEXTREL: name = "TEXTREL"; break;
1779 case DT_JMPREL: name = "JMPREL"; break;
1780 case DT_BIND_NOW: name = "BIND_NOW"; break;
1781 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1782 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1783 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1784 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1785 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1786 case DT_FLAGS: name = "FLAGS"; break;
1787 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1788 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1789 case DT_CHECKSUM: name = "CHECKSUM"; break;
1790 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1791 case DT_MOVEENT: name = "MOVEENT"; break;
1792 case DT_MOVESZ: name = "MOVESZ"; break;
1793 case DT_FEATURE: name = "FEATURE"; break;
1794 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1795 case DT_SYMINSZ: name = "SYMINSZ"; break;
1796 case DT_SYMINENT: name = "SYMINENT"; break;
1797 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1798 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1799 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1800 case DT_PLTPAD: name = "PLTPAD"; break;
1801 case DT_MOVETAB: name = "MOVETAB"; break;
1802 case DT_SYMINFO: name = "SYMINFO"; break;
1803 case DT_RELACOUNT: name = "RELACOUNT"; break;
1804 case DT_RELCOUNT: name = "RELCOUNT"; break;
1805 case DT_FLAGS_1: name = "FLAGS_1"; break;
1806 case DT_VERSYM: name = "VERSYM"; break;
1807 case DT_VERDEF: name = "VERDEF"; break;
1808 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1809 case DT_VERNEED: name = "VERNEED"; break;
1810 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1811 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1812 case DT_USED: name = "USED"; break;
1813 case DT_FILTER: name = "FILTER"; stringp = true; break;
1814 case DT_GNU_HASH: name = "GNU_HASH"; break;
1815 }
1816
1817 fprintf (f, " %-20s ", name);
1818 if (! stringp)
1819 {
1820 fprintf (f, "0x");
1821 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1822 }
1823 else
1824 {
1825 const char *string;
1826 unsigned int tagv = dyn.d_un.d_val;
1827
1828 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1829 if (string == NULL)
1830 goto error_return;
1831 fprintf (f, "%s", string);
1832 }
1833 fprintf (f, "\n");
1834 }
1835
1836 free (dynbuf);
1837 dynbuf = NULL;
1838 }
1839
1840 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1841 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1842 {
1843 if (! _bfd_elf_slurp_version_tables (abfd, false))
1844 return false;
1845 }
1846
1847 if (elf_dynverdef (abfd) != 0)
1848 {
1849 Elf_Internal_Verdef *t;
1850
1851 fprintf (f, _("\nVersion definitions:\n"));
1852 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1853 {
1854 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1855 t->vd_flags, t->vd_hash,
1856 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1857 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1858 {
1859 Elf_Internal_Verdaux *a;
1860
1861 fprintf (f, "\t");
1862 for (a = t->vd_auxptr->vda_nextptr;
1863 a != NULL;
1864 a = a->vda_nextptr)
1865 fprintf (f, "%s ",
1866 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1867 fprintf (f, "\n");
1868 }
1869 }
1870 }
1871
1872 if (elf_dynverref (abfd) != 0)
1873 {
1874 Elf_Internal_Verneed *t;
1875
1876 fprintf (f, _("\nVersion References:\n"));
1877 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1878 {
1879 Elf_Internal_Vernaux *a;
1880
1881 fprintf (f, _(" required from %s:\n"),
1882 t->vn_filename ? t->vn_filename : "<corrupt>");
1883 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1884 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1885 a->vna_flags, a->vna_other,
1886 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1887 }
1888 }
1889
1890 return true;
1891
1892 error_return:
1893 free (dynbuf);
1894 return false;
1895 }
1896
1897 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1898 and return symbol version for symbol version itself. */
1899
1900 const char *
1901 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1902 bool base_p,
1903 bool *hidden)
1904 {
1905 const char *version_string = NULL;
1906 if (elf_dynversym (abfd) != 0
1907 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1908 {
1909 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1910
1911 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1912 vernum &= VERSYM_VERSION;
1913
1914 if (vernum == 0)
1915 version_string = "";
1916 else if (vernum == 1
1917 && (vernum > elf_tdata (abfd)->cverdefs
1918 || (elf_tdata (abfd)->verdef[0].vd_flags
1919 == VER_FLG_BASE)))
1920 version_string = base_p ? "Base" : "";
1921 else if (vernum <= elf_tdata (abfd)->cverdefs)
1922 {
1923 const char *nodename
1924 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1925 version_string = "";
1926 if (base_p
1927 || nodename == NULL
1928 || symbol->name == NULL
1929 || strcmp (symbol->name, nodename) != 0)
1930 version_string = nodename;
1931 }
1932 else
1933 {
1934 Elf_Internal_Verneed *t;
1935
1936 version_string = _("<corrupt>");
1937 for (t = elf_tdata (abfd)->verref;
1938 t != NULL;
1939 t = t->vn_nextref)
1940 {
1941 Elf_Internal_Vernaux *a;
1942
1943 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1944 {
1945 if (a->vna_other == vernum)
1946 {
1947 version_string = a->vna_nodename;
1948 break;
1949 }
1950 }
1951 }
1952 }
1953 }
1954 return version_string;
1955 }
1956
1957 /* Display ELF-specific fields of a symbol. */
1958
1959 void
1960 bfd_elf_print_symbol (bfd *abfd,
1961 void *filep,
1962 asymbol *symbol,
1963 bfd_print_symbol_type how)
1964 {
1965 FILE *file = (FILE *) filep;
1966 switch (how)
1967 {
1968 case bfd_print_symbol_name:
1969 fprintf (file, "%s", symbol->name);
1970 break;
1971 case bfd_print_symbol_more:
1972 fprintf (file, "elf ");
1973 bfd_fprintf_vma (abfd, file, symbol->value);
1974 fprintf (file, " %x", symbol->flags);
1975 break;
1976 case bfd_print_symbol_all:
1977 {
1978 const char *section_name;
1979 const char *name = NULL;
1980 const struct elf_backend_data *bed;
1981 unsigned char st_other;
1982 bfd_vma val;
1983 const char *version_string;
1984 bool hidden;
1985
1986 section_name = symbol->section ? symbol->section->name : "(*none*)";
1987
1988 bed = get_elf_backend_data (abfd);
1989 if (bed->elf_backend_print_symbol_all)
1990 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1991
1992 if (name == NULL)
1993 {
1994 name = symbol->name;
1995 bfd_print_symbol_vandf (abfd, file, symbol);
1996 }
1997
1998 fprintf (file, " %s\t", section_name);
1999 /* Print the "other" value for a symbol. For common symbols,
2000 we've already printed the size; now print the alignment.
2001 For other symbols, we have no specified alignment, and
2002 we've printed the address; now print the size. */
2003 if (symbol->section && bfd_is_com_section (symbol->section))
2004 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2005 else
2006 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2007 bfd_fprintf_vma (abfd, file, val);
2008
2009 /* If we have version information, print it. */
2010 version_string = _bfd_elf_get_symbol_version_string (abfd,
2011 symbol,
2012 true,
2013 &hidden);
2014 if (version_string)
2015 {
2016 if (!hidden)
2017 fprintf (file, " %-11s", version_string);
2018 else
2019 {
2020 int i;
2021
2022 fprintf (file, " (%s)", version_string);
2023 for (i = 10 - strlen (version_string); i > 0; --i)
2024 putc (' ', file);
2025 }
2026 }
2027
2028 /* If the st_other field is not zero, print it. */
2029 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2030
2031 switch (st_other)
2032 {
2033 case 0: break;
2034 case STV_INTERNAL: fprintf (file, " .internal"); break;
2035 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2036 case STV_PROTECTED: fprintf (file, " .protected"); break;
2037 default:
2038 /* Some other non-defined flags are also present, so print
2039 everything hex. */
2040 fprintf (file, " 0x%02x", (unsigned int) st_other);
2041 }
2042
2043 fprintf (file, " %s", name);
2044 }
2045 break;
2046 }
2047 }
2048 \f
2049 /* ELF .o/exec file reading */
2050
2051 /* Create a new bfd section from an ELF section header. */
2052
2053 bool
2054 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2055 {
2056 Elf_Internal_Shdr *hdr;
2057 Elf_Internal_Ehdr *ehdr;
2058 const struct elf_backend_data *bed;
2059 const char *name;
2060 bool ret = true;
2061
2062 if (shindex >= elf_numsections (abfd))
2063 return false;
2064
2065 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2066 sh_link or sh_info. Detect this here, by refusing to load a
2067 section that we are already in the process of loading. */
2068 if (elf_tdata (abfd)->being_created[shindex])
2069 {
2070 _bfd_error_handler
2071 (_("%pB: warning: loop in section dependencies detected"), abfd);
2072 return false;
2073 }
2074 elf_tdata (abfd)->being_created[shindex] = true;
2075
2076 hdr = elf_elfsections (abfd)[shindex];
2077 ehdr = elf_elfheader (abfd);
2078 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2079 hdr->sh_name);
2080 if (name == NULL)
2081 goto fail;
2082
2083 bed = get_elf_backend_data (abfd);
2084 switch (hdr->sh_type)
2085 {
2086 case SHT_NULL:
2087 /* Inactive section. Throw it away. */
2088 goto success;
2089
2090 case SHT_PROGBITS: /* Normal section with contents. */
2091 case SHT_NOBITS: /* .bss section. */
2092 case SHT_HASH: /* .hash section. */
2093 case SHT_NOTE: /* .note section. */
2094 case SHT_INIT_ARRAY: /* .init_array section. */
2095 case SHT_FINI_ARRAY: /* .fini_array section. */
2096 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2097 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2098 case SHT_GNU_HASH: /* .gnu.hash section. */
2099 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2100 goto success;
2101
2102 case SHT_DYNAMIC: /* Dynamic linking information. */
2103 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2104 goto fail;
2105
2106 if (hdr->sh_link > elf_numsections (abfd))
2107 {
2108 /* PR 10478: Accept Solaris binaries with a sh_link
2109 field set to SHN_BEFORE or SHN_AFTER. */
2110 switch (bfd_get_arch (abfd))
2111 {
2112 case bfd_arch_i386:
2113 case bfd_arch_sparc:
2114 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2115 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2116 break;
2117 /* Otherwise fall through. */
2118 default:
2119 goto fail;
2120 }
2121 }
2122 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2123 goto fail;
2124 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2125 {
2126 Elf_Internal_Shdr *dynsymhdr;
2127
2128 /* The shared libraries distributed with hpux11 have a bogus
2129 sh_link field for the ".dynamic" section. Find the
2130 string table for the ".dynsym" section instead. */
2131 if (elf_dynsymtab (abfd) != 0)
2132 {
2133 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2134 hdr->sh_link = dynsymhdr->sh_link;
2135 }
2136 else
2137 {
2138 unsigned int i, num_sec;
2139
2140 num_sec = elf_numsections (abfd);
2141 for (i = 1; i < num_sec; i++)
2142 {
2143 dynsymhdr = elf_elfsections (abfd)[i];
2144 if (dynsymhdr->sh_type == SHT_DYNSYM)
2145 {
2146 hdr->sh_link = dynsymhdr->sh_link;
2147 break;
2148 }
2149 }
2150 }
2151 }
2152 goto success;
2153
2154 case SHT_SYMTAB: /* A symbol table. */
2155 if (elf_onesymtab (abfd) == shindex)
2156 goto success;
2157
2158 if (hdr->sh_entsize != bed->s->sizeof_sym)
2159 goto fail;
2160
2161 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2162 {
2163 if (hdr->sh_size != 0)
2164 goto fail;
2165 /* Some assemblers erroneously set sh_info to one with a
2166 zero sh_size. ld sees this as a global symbol count
2167 of (unsigned) -1. Fix it here. */
2168 hdr->sh_info = 0;
2169 goto success;
2170 }
2171
2172 /* PR 18854: A binary might contain more than one symbol table.
2173 Unusual, but possible. Warn, but continue. */
2174 if (elf_onesymtab (abfd) != 0)
2175 {
2176 _bfd_error_handler
2177 /* xgettext:c-format */
2178 (_("%pB: warning: multiple symbol tables detected"
2179 " - ignoring the table in section %u"),
2180 abfd, shindex);
2181 goto success;
2182 }
2183 elf_onesymtab (abfd) = shindex;
2184 elf_symtab_hdr (abfd) = *hdr;
2185 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2186 abfd->flags |= HAS_SYMS;
2187
2188 /* Sometimes a shared object will map in the symbol table. If
2189 SHF_ALLOC is set, and this is a shared object, then we also
2190 treat this section as a BFD section. We can not base the
2191 decision purely on SHF_ALLOC, because that flag is sometimes
2192 set in a relocatable object file, which would confuse the
2193 linker. */
2194 if ((hdr->sh_flags & SHF_ALLOC) != 0
2195 && (abfd->flags & DYNAMIC) != 0
2196 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2197 shindex))
2198 goto fail;
2199
2200 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2201 can't read symbols without that section loaded as well. It
2202 is most likely specified by the next section header. */
2203 {
2204 elf_section_list * entry;
2205 unsigned int i, num_sec;
2206
2207 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2208 if (entry->hdr.sh_link == shindex)
2209 goto success;
2210
2211 num_sec = elf_numsections (abfd);
2212 for (i = shindex + 1; i < num_sec; i++)
2213 {
2214 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2215
2216 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2217 && hdr2->sh_link == shindex)
2218 break;
2219 }
2220
2221 if (i == num_sec)
2222 for (i = 1; i < shindex; i++)
2223 {
2224 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2225
2226 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2227 && hdr2->sh_link == shindex)
2228 break;
2229 }
2230
2231 if (i != shindex)
2232 ret = bfd_section_from_shdr (abfd, i);
2233 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2234 goto success;
2235 }
2236
2237 case SHT_DYNSYM: /* A dynamic symbol table. */
2238 if (elf_dynsymtab (abfd) == shindex)
2239 goto success;
2240
2241 if (hdr->sh_entsize != bed->s->sizeof_sym)
2242 goto fail;
2243
2244 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2245 {
2246 if (hdr->sh_size != 0)
2247 goto fail;
2248
2249 /* Some linkers erroneously set sh_info to one with a
2250 zero sh_size. ld sees this as a global symbol count
2251 of (unsigned) -1. Fix it here. */
2252 hdr->sh_info = 0;
2253 goto success;
2254 }
2255
2256 /* PR 18854: A binary might contain more than one dynamic symbol table.
2257 Unusual, but possible. Warn, but continue. */
2258 if (elf_dynsymtab (abfd) != 0)
2259 {
2260 _bfd_error_handler
2261 /* xgettext:c-format */
2262 (_("%pB: warning: multiple dynamic symbol tables detected"
2263 " - ignoring the table in section %u"),
2264 abfd, shindex);
2265 goto success;
2266 }
2267 elf_dynsymtab (abfd) = shindex;
2268 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2269 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2270 abfd->flags |= HAS_SYMS;
2271
2272 /* Besides being a symbol table, we also treat this as a regular
2273 section, so that objcopy can handle it. */
2274 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2275 goto success;
2276
2277 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2278 {
2279 elf_section_list * entry;
2280
2281 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2282 if (entry->ndx == shindex)
2283 goto success;
2284
2285 entry = bfd_alloc (abfd, sizeof (*entry));
2286 if (entry == NULL)
2287 goto fail;
2288 entry->ndx = shindex;
2289 entry->hdr = * hdr;
2290 entry->next = elf_symtab_shndx_list (abfd);
2291 elf_symtab_shndx_list (abfd) = entry;
2292 elf_elfsections (abfd)[shindex] = & entry->hdr;
2293 goto success;
2294 }
2295
2296 case SHT_STRTAB: /* A string table. */
2297 if (hdr->bfd_section != NULL)
2298 goto success;
2299
2300 if (ehdr->e_shstrndx == shindex)
2301 {
2302 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2303 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2304 goto success;
2305 }
2306
2307 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2308 {
2309 symtab_strtab:
2310 elf_tdata (abfd)->strtab_hdr = *hdr;
2311 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2312 goto success;
2313 }
2314
2315 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2316 {
2317 dynsymtab_strtab:
2318 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2319 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2320 elf_elfsections (abfd)[shindex] = hdr;
2321 /* We also treat this as a regular section, so that objcopy
2322 can handle it. */
2323 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2324 shindex);
2325 goto success;
2326 }
2327
2328 /* If the string table isn't one of the above, then treat it as a
2329 regular section. We need to scan all the headers to be sure,
2330 just in case this strtab section appeared before the above. */
2331 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2332 {
2333 unsigned int i, num_sec;
2334
2335 num_sec = elf_numsections (abfd);
2336 for (i = 1; i < num_sec; i++)
2337 {
2338 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2339 if (hdr2->sh_link == shindex)
2340 {
2341 /* Prevent endless recursion on broken objects. */
2342 if (i == shindex)
2343 goto fail;
2344 if (! bfd_section_from_shdr (abfd, i))
2345 goto fail;
2346 if (elf_onesymtab (abfd) == i)
2347 goto symtab_strtab;
2348 if (elf_dynsymtab (abfd) == i)
2349 goto dynsymtab_strtab;
2350 }
2351 }
2352 }
2353 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2354 goto success;
2355
2356 case SHT_REL:
2357 case SHT_RELA:
2358 /* *These* do a lot of work -- but build no sections! */
2359 {
2360 asection *target_sect;
2361 Elf_Internal_Shdr *hdr2, **p_hdr;
2362 unsigned int num_sec = elf_numsections (abfd);
2363 struct bfd_elf_section_data *esdt;
2364
2365 if (hdr->sh_entsize
2366 != (bfd_size_type) (hdr->sh_type == SHT_REL
2367 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2368 goto fail;
2369
2370 /* Check for a bogus link to avoid crashing. */
2371 if (hdr->sh_link >= num_sec)
2372 {
2373 _bfd_error_handler
2374 /* xgettext:c-format */
2375 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2376 abfd, hdr->sh_link, name, shindex);
2377 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2378 shindex);
2379 goto success;
2380 }
2381
2382 /* For some incomprehensible reason Oracle distributes
2383 libraries for Solaris in which some of the objects have
2384 bogus sh_link fields. It would be nice if we could just
2385 reject them, but, unfortunately, some people need to use
2386 them. We scan through the section headers; if we find only
2387 one suitable symbol table, we clobber the sh_link to point
2388 to it. I hope this doesn't break anything.
2389
2390 Don't do it on executable nor shared library. */
2391 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2392 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2393 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2394 {
2395 unsigned int scan;
2396 int found;
2397
2398 found = 0;
2399 for (scan = 1; scan < num_sec; scan++)
2400 {
2401 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2402 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2403 {
2404 if (found != 0)
2405 {
2406 found = 0;
2407 break;
2408 }
2409 found = scan;
2410 }
2411 }
2412 if (found != 0)
2413 hdr->sh_link = found;
2414 }
2415
2416 /* Get the symbol table. */
2417 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2418 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2419 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2420 goto fail;
2421
2422 /* If this is an alloc section in an executable or shared
2423 library, or the reloc section does not use the main symbol
2424 table we don't treat it as a reloc section. BFD can't
2425 adequately represent such a section, so at least for now,
2426 we don't try. We just present it as a normal section. We
2427 also can't use it as a reloc section if it points to the
2428 null section, an invalid section, another reloc section, or
2429 its sh_link points to the null section. */
2430 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2431 && (hdr->sh_flags & SHF_ALLOC) != 0)
2432 || hdr->sh_link == SHN_UNDEF
2433 || hdr->sh_link != elf_onesymtab (abfd)
2434 || hdr->sh_info == SHN_UNDEF
2435 || hdr->sh_info >= num_sec
2436 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2437 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2438 {
2439 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2440 shindex);
2441 goto success;
2442 }
2443
2444 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2445 goto fail;
2446
2447 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2448 if (target_sect == NULL)
2449 goto fail;
2450
2451 esdt = elf_section_data (target_sect);
2452 if (hdr->sh_type == SHT_RELA)
2453 p_hdr = &esdt->rela.hdr;
2454 else
2455 p_hdr = &esdt->rel.hdr;
2456
2457 /* PR 17512: file: 0b4f81b7.
2458 Also see PR 24456, for a file which deliberately has two reloc
2459 sections. */
2460 if (*p_hdr != NULL)
2461 {
2462 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2463 {
2464 _bfd_error_handler
2465 /* xgettext:c-format */
2466 (_("%pB: warning: secondary relocation section '%s' "
2467 "for section %pA found - ignoring"),
2468 abfd, name, target_sect);
2469 }
2470 else
2471 esdt->has_secondary_relocs = true;
2472 goto success;
2473 }
2474
2475 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2476 if (hdr2 == NULL)
2477 goto fail;
2478 *hdr2 = *hdr;
2479 *p_hdr = hdr2;
2480 elf_elfsections (abfd)[shindex] = hdr2;
2481 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2482 * bed->s->int_rels_per_ext_rel);
2483 target_sect->flags |= SEC_RELOC;
2484 target_sect->relocation = NULL;
2485 target_sect->rel_filepos = hdr->sh_offset;
2486 /* In the section to which the relocations apply, mark whether
2487 its relocations are of the REL or RELA variety. */
2488 if (hdr->sh_size != 0)
2489 {
2490 if (hdr->sh_type == SHT_RELA)
2491 target_sect->use_rela_p = 1;
2492 }
2493 abfd->flags |= HAS_RELOC;
2494 goto success;
2495 }
2496
2497 case SHT_GNU_verdef:
2498 elf_dynverdef (abfd) = shindex;
2499 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2500 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2501 goto success;
2502
2503 case SHT_GNU_versym:
2504 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2505 goto fail;
2506
2507 elf_dynversym (abfd) = shindex;
2508 elf_tdata (abfd)->dynversym_hdr = *hdr;
2509 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2510 goto success;
2511
2512 case SHT_GNU_verneed:
2513 elf_dynverref (abfd) = shindex;
2514 elf_tdata (abfd)->dynverref_hdr = *hdr;
2515 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2516 goto success;
2517
2518 case SHT_SHLIB:
2519 goto success;
2520
2521 case SHT_GROUP:
2522 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2523 goto fail;
2524
2525 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2526 goto fail;
2527
2528 goto success;
2529
2530 default:
2531 /* Possibly an attributes section. */
2532 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2533 || hdr->sh_type == bed->obj_attrs_section_type)
2534 {
2535 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2536 goto fail;
2537 _bfd_elf_parse_attributes (abfd, hdr);
2538 goto success;
2539 }
2540
2541 /* Check for any processor-specific section types. */
2542 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2543 goto success;
2544
2545 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2546 {
2547 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2548 /* FIXME: How to properly handle allocated section reserved
2549 for applications? */
2550 _bfd_error_handler
2551 /* xgettext:c-format */
2552 (_("%pB: unknown type [%#x] section `%s'"),
2553 abfd, hdr->sh_type, name);
2554 else
2555 {
2556 /* Allow sections reserved for applications. */
2557 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2558 shindex);
2559 goto success;
2560 }
2561 }
2562 else if (hdr->sh_type >= SHT_LOPROC
2563 && hdr->sh_type <= SHT_HIPROC)
2564 /* FIXME: We should handle this section. */
2565 _bfd_error_handler
2566 /* xgettext:c-format */
2567 (_("%pB: unknown type [%#x] section `%s'"),
2568 abfd, hdr->sh_type, name);
2569 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2570 {
2571 /* Unrecognised OS-specific sections. */
2572 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2573 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2574 required to correctly process the section and the file should
2575 be rejected with an error message. */
2576 _bfd_error_handler
2577 /* xgettext:c-format */
2578 (_("%pB: unknown type [%#x] section `%s'"),
2579 abfd, hdr->sh_type, name);
2580 else
2581 {
2582 /* Otherwise it should be processed. */
2583 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2584 goto success;
2585 }
2586 }
2587 else
2588 /* FIXME: We should handle this section. */
2589 _bfd_error_handler
2590 /* xgettext:c-format */
2591 (_("%pB: unknown type [%#x] section `%s'"),
2592 abfd, hdr->sh_type, name);
2593
2594 goto fail;
2595 }
2596
2597 fail:
2598 ret = false;
2599 success:
2600 elf_tdata (abfd)->being_created[shindex] = false;
2601 return ret;
2602 }
2603
2604 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2605
2606 Elf_Internal_Sym *
2607 bfd_sym_from_r_symndx (struct sym_cache *cache,
2608 bfd *abfd,
2609 unsigned long r_symndx)
2610 {
2611 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2612
2613 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2614 {
2615 Elf_Internal_Shdr *symtab_hdr;
2616 unsigned char esym[sizeof (Elf64_External_Sym)];
2617 Elf_External_Sym_Shndx eshndx;
2618
2619 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2620 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2621 &cache->sym[ent], esym, &eshndx) == NULL)
2622 return NULL;
2623
2624 if (cache->abfd != abfd)
2625 {
2626 memset (cache->indx, -1, sizeof (cache->indx));
2627 cache->abfd = abfd;
2628 }
2629 cache->indx[ent] = r_symndx;
2630 }
2631
2632 return &cache->sym[ent];
2633 }
2634
2635 /* Given an ELF section number, retrieve the corresponding BFD
2636 section. */
2637
2638 asection *
2639 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2640 {
2641 if (sec_index >= elf_numsections (abfd))
2642 return NULL;
2643 return elf_elfsections (abfd)[sec_index]->bfd_section;
2644 }
2645
2646 static const struct bfd_elf_special_section special_sections_b[] =
2647 {
2648 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2649 { NULL, 0, 0, 0, 0 }
2650 };
2651
2652 static const struct bfd_elf_special_section special_sections_c[] =
2653 {
2654 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2655 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2656 { NULL, 0, 0, 0, 0 }
2657 };
2658
2659 static const struct bfd_elf_special_section special_sections_d[] =
2660 {
2661 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2662 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2663 /* There are more DWARF sections than these, but they needn't be added here
2664 unless you have to cope with broken compilers that don't emit section
2665 attributes or you want to help the user writing assembler. */
2666 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2667 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2668 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2669 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2670 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2671 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2672 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2673 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2674 { NULL, 0, 0, 0, 0 }
2675 };
2676
2677 static const struct bfd_elf_special_section special_sections_f[] =
2678 {
2679 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2680 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2681 { NULL, 0 , 0, 0, 0 }
2682 };
2683
2684 static const struct bfd_elf_special_section special_sections_g[] =
2685 {
2686 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2687 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2688 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2689 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2690 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2691 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2692 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2693 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2694 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2695 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2696 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2697 { NULL, 0, 0, 0, 0 }
2698 };
2699
2700 static const struct bfd_elf_special_section special_sections_h[] =
2701 {
2702 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2703 { NULL, 0, 0, 0, 0 }
2704 };
2705
2706 static const struct bfd_elf_special_section special_sections_i[] =
2707 {
2708 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2709 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2710 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2711 { NULL, 0, 0, 0, 0 }
2712 };
2713
2714 static const struct bfd_elf_special_section special_sections_l[] =
2715 {
2716 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2717 { NULL, 0, 0, 0, 0 }
2718 };
2719
2720 static const struct bfd_elf_special_section special_sections_n[] =
2721 {
2722 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2723 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2724 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2725 { NULL, 0, 0, 0, 0 }
2726 };
2727
2728 static const struct bfd_elf_special_section special_sections_p[] =
2729 {
2730 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2731 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2732 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2733 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2734 { NULL, 0, 0, 0, 0 }
2735 };
2736
2737 static const struct bfd_elf_special_section special_sections_r[] =
2738 {
2739 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2740 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2741 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2742 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2743 { NULL, 0, 0, 0, 0 }
2744 };
2745
2746 static const struct bfd_elf_special_section special_sections_s[] =
2747 {
2748 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2749 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2750 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2751 /* See struct bfd_elf_special_section declaration for the semantics of
2752 this special case where .prefix_length != strlen (.prefix). */
2753 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2754 { NULL, 0, 0, 0, 0 }
2755 };
2756
2757 static const struct bfd_elf_special_section special_sections_t[] =
2758 {
2759 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2760 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2761 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2762 { NULL, 0, 0, 0, 0 }
2763 };
2764
2765 static const struct bfd_elf_special_section special_sections_z[] =
2766 {
2767 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2768 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2769 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2770 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2771 { NULL, 0, 0, 0, 0 }
2772 };
2773
2774 static const struct bfd_elf_special_section * const special_sections[] =
2775 {
2776 special_sections_b, /* 'b' */
2777 special_sections_c, /* 'c' */
2778 special_sections_d, /* 'd' */
2779 NULL, /* 'e' */
2780 special_sections_f, /* 'f' */
2781 special_sections_g, /* 'g' */
2782 special_sections_h, /* 'h' */
2783 special_sections_i, /* 'i' */
2784 NULL, /* 'j' */
2785 NULL, /* 'k' */
2786 special_sections_l, /* 'l' */
2787 NULL, /* 'm' */
2788 special_sections_n, /* 'n' */
2789 NULL, /* 'o' */
2790 special_sections_p, /* 'p' */
2791 NULL, /* 'q' */
2792 special_sections_r, /* 'r' */
2793 special_sections_s, /* 's' */
2794 special_sections_t, /* 't' */
2795 NULL, /* 'u' */
2796 NULL, /* 'v' */
2797 NULL, /* 'w' */
2798 NULL, /* 'x' */
2799 NULL, /* 'y' */
2800 special_sections_z /* 'z' */
2801 };
2802
2803 const struct bfd_elf_special_section *
2804 _bfd_elf_get_special_section (const char *name,
2805 const struct bfd_elf_special_section *spec,
2806 unsigned int rela)
2807 {
2808 int i;
2809 int len;
2810
2811 len = strlen (name);
2812
2813 for (i = 0; spec[i].prefix != NULL; i++)
2814 {
2815 int suffix_len;
2816 int prefix_len = spec[i].prefix_length;
2817
2818 if (len < prefix_len)
2819 continue;
2820 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2821 continue;
2822
2823 suffix_len = spec[i].suffix_length;
2824 if (suffix_len <= 0)
2825 {
2826 if (name[prefix_len] != 0)
2827 {
2828 if (suffix_len == 0)
2829 continue;
2830 if (name[prefix_len] != '.'
2831 && (suffix_len == -2
2832 || (rela && spec[i].type == SHT_REL)))
2833 continue;
2834 }
2835 }
2836 else
2837 {
2838 if (len < prefix_len + suffix_len)
2839 continue;
2840 if (memcmp (name + len - suffix_len,
2841 spec[i].prefix + prefix_len,
2842 suffix_len) != 0)
2843 continue;
2844 }
2845 return &spec[i];
2846 }
2847
2848 return NULL;
2849 }
2850
2851 const struct bfd_elf_special_section *
2852 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2853 {
2854 int i;
2855 const struct bfd_elf_special_section *spec;
2856 const struct elf_backend_data *bed;
2857
2858 /* See if this is one of the special sections. */
2859 if (sec->name == NULL)
2860 return NULL;
2861
2862 bed = get_elf_backend_data (abfd);
2863 spec = bed->special_sections;
2864 if (spec)
2865 {
2866 spec = _bfd_elf_get_special_section (sec->name,
2867 bed->special_sections,
2868 sec->use_rela_p);
2869 if (spec != NULL)
2870 return spec;
2871 }
2872
2873 if (sec->name[0] != '.')
2874 return NULL;
2875
2876 i = sec->name[1] - 'b';
2877 if (i < 0 || i > 'z' - 'b')
2878 return NULL;
2879
2880 spec = special_sections[i];
2881
2882 if (spec == NULL)
2883 return NULL;
2884
2885 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2886 }
2887
2888 bool
2889 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2890 {
2891 struct bfd_elf_section_data *sdata;
2892 const struct elf_backend_data *bed;
2893 const struct bfd_elf_special_section *ssect;
2894
2895 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2896 if (sdata == NULL)
2897 {
2898 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2899 sizeof (*sdata));
2900 if (sdata == NULL)
2901 return false;
2902 sec->used_by_bfd = sdata;
2903 }
2904
2905 /* Indicate whether or not this section should use RELA relocations. */
2906 bed = get_elf_backend_data (abfd);
2907 sec->use_rela_p = bed->default_use_rela_p;
2908
2909 /* Set up ELF section type and flags for newly created sections, if
2910 there is an ABI mandated section. */
2911 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2912 if (ssect != NULL)
2913 {
2914 elf_section_type (sec) = ssect->type;
2915 elf_section_flags (sec) = ssect->attr;
2916 }
2917
2918 return _bfd_generic_new_section_hook (abfd, sec);
2919 }
2920
2921 /* Create a new bfd section from an ELF program header.
2922
2923 Since program segments have no names, we generate a synthetic name
2924 of the form segment<NUM>, where NUM is generally the index in the
2925 program header table. For segments that are split (see below) we
2926 generate the names segment<NUM>a and segment<NUM>b.
2927
2928 Note that some program segments may have a file size that is different than
2929 (less than) the memory size. All this means is that at execution the
2930 system must allocate the amount of memory specified by the memory size,
2931 but only initialize it with the first "file size" bytes read from the
2932 file. This would occur for example, with program segments consisting
2933 of combined data+bss.
2934
2935 To handle the above situation, this routine generates TWO bfd sections
2936 for the single program segment. The first has the length specified by
2937 the file size of the segment, and the second has the length specified
2938 by the difference between the two sizes. In effect, the segment is split
2939 into its initialized and uninitialized parts.
2940
2941 */
2942
2943 bool
2944 _bfd_elf_make_section_from_phdr (bfd *abfd,
2945 Elf_Internal_Phdr *hdr,
2946 int hdr_index,
2947 const char *type_name)
2948 {
2949 asection *newsect;
2950 char *name;
2951 char namebuf[64];
2952 size_t len;
2953 int split;
2954 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2955
2956 split = ((hdr->p_memsz > 0)
2957 && (hdr->p_filesz > 0)
2958 && (hdr->p_memsz > hdr->p_filesz));
2959
2960 if (hdr->p_filesz > 0)
2961 {
2962 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2963 len = strlen (namebuf) + 1;
2964 name = (char *) bfd_alloc (abfd, len);
2965 if (!name)
2966 return false;
2967 memcpy (name, namebuf, len);
2968 newsect = bfd_make_section (abfd, name);
2969 if (newsect == NULL)
2970 return false;
2971 newsect->vma = hdr->p_vaddr / opb;
2972 newsect->lma = hdr->p_paddr / opb;
2973 newsect->size = hdr->p_filesz;
2974 newsect->filepos = hdr->p_offset;
2975 newsect->flags |= SEC_HAS_CONTENTS;
2976 newsect->alignment_power = bfd_log2 (hdr->p_align);
2977 if (hdr->p_type == PT_LOAD)
2978 {
2979 newsect->flags |= SEC_ALLOC;
2980 newsect->flags |= SEC_LOAD;
2981 if (hdr->p_flags & PF_X)
2982 {
2983 /* FIXME: all we known is that it has execute PERMISSION,
2984 may be data. */
2985 newsect->flags |= SEC_CODE;
2986 }
2987 }
2988 if (!(hdr->p_flags & PF_W))
2989 {
2990 newsect->flags |= SEC_READONLY;
2991 }
2992 }
2993
2994 if (hdr->p_memsz > hdr->p_filesz)
2995 {
2996 bfd_vma align;
2997
2998 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2999 len = strlen (namebuf) + 1;
3000 name = (char *) bfd_alloc (abfd, len);
3001 if (!name)
3002 return false;
3003 memcpy (name, namebuf, len);
3004 newsect = bfd_make_section (abfd, name);
3005 if (newsect == NULL)
3006 return false;
3007 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3008 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3009 newsect->size = hdr->p_memsz - hdr->p_filesz;
3010 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3011 align = newsect->vma & -newsect->vma;
3012 if (align == 0 || align > hdr->p_align)
3013 align = hdr->p_align;
3014 newsect->alignment_power = bfd_log2 (align);
3015 if (hdr->p_type == PT_LOAD)
3016 {
3017 newsect->flags |= SEC_ALLOC;
3018 if (hdr->p_flags & PF_X)
3019 newsect->flags |= SEC_CODE;
3020 }
3021 if (!(hdr->p_flags & PF_W))
3022 newsect->flags |= SEC_READONLY;
3023 }
3024
3025 return true;
3026 }
3027
3028 static bool
3029 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3030 {
3031 /* The return value is ignored. Build-ids are considered optional. */
3032 if (templ->xvec->flavour == bfd_target_elf_flavour)
3033 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3034 (templ, offset);
3035 return false;
3036 }
3037
3038 bool
3039 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3040 {
3041 const struct elf_backend_data *bed;
3042
3043 switch (hdr->p_type)
3044 {
3045 case PT_NULL:
3046 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3047
3048 case PT_LOAD:
3049 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3050 return false;
3051 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3052 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3053 return true;
3054
3055 case PT_DYNAMIC:
3056 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3057
3058 case PT_INTERP:
3059 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3060
3061 case PT_NOTE:
3062 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3063 return false;
3064 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3065 hdr->p_align))
3066 return false;
3067 return true;
3068
3069 case PT_SHLIB:
3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3071
3072 case PT_PHDR:
3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3074
3075 case PT_GNU_EH_FRAME:
3076 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3077 "eh_frame_hdr");
3078
3079 case PT_GNU_STACK:
3080 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3081
3082 case PT_GNU_RELRO:
3083 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3084
3085 default:
3086 /* Check for any processor-specific program segment types. */
3087 bed = get_elf_backend_data (abfd);
3088 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3089 }
3090 }
3091
3092 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3093 REL or RELA. */
3094
3095 Elf_Internal_Shdr *
3096 _bfd_elf_single_rel_hdr (asection *sec)
3097 {
3098 if (elf_section_data (sec)->rel.hdr)
3099 {
3100 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3101 return elf_section_data (sec)->rel.hdr;
3102 }
3103 else
3104 return elf_section_data (sec)->rela.hdr;
3105 }
3106
3107 static bool
3108 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3109 Elf_Internal_Shdr *rel_hdr,
3110 const char *sec_name,
3111 bool use_rela_p)
3112 {
3113 char *name = (char *) bfd_alloc (abfd,
3114 sizeof ".rela" + strlen (sec_name));
3115 if (name == NULL)
3116 return false;
3117
3118 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3119 rel_hdr->sh_name =
3120 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3121 false);
3122 if (rel_hdr->sh_name == (unsigned int) -1)
3123 return false;
3124
3125 return true;
3126 }
3127
3128 /* Allocate and initialize a section-header for a new reloc section,
3129 containing relocations against ASECT. It is stored in RELDATA. If
3130 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3131 relocations. */
3132
3133 static bool
3134 _bfd_elf_init_reloc_shdr (bfd *abfd,
3135 struct bfd_elf_section_reloc_data *reldata,
3136 const char *sec_name,
3137 bool use_rela_p,
3138 bool delay_st_name_p)
3139 {
3140 Elf_Internal_Shdr *rel_hdr;
3141 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3142
3143 BFD_ASSERT (reldata->hdr == NULL);
3144 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3145 reldata->hdr = rel_hdr;
3146
3147 if (delay_st_name_p)
3148 rel_hdr->sh_name = (unsigned int) -1;
3149 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3150 use_rela_p))
3151 return false;
3152 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3153 rel_hdr->sh_entsize = (use_rela_p
3154 ? bed->s->sizeof_rela
3155 : bed->s->sizeof_rel);
3156 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3157 rel_hdr->sh_flags = 0;
3158 rel_hdr->sh_addr = 0;
3159 rel_hdr->sh_size = 0;
3160 rel_hdr->sh_offset = 0;
3161
3162 return true;
3163 }
3164
3165 /* Return the default section type based on the passed in section flags. */
3166
3167 int
3168 bfd_elf_get_default_section_type (flagword flags)
3169 {
3170 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3171 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3172 return SHT_NOBITS;
3173 return SHT_PROGBITS;
3174 }
3175
3176 struct fake_section_arg
3177 {
3178 struct bfd_link_info *link_info;
3179 bool failed;
3180 };
3181
3182 /* Set up an ELF internal section header for a section. */
3183
3184 static void
3185 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3186 {
3187 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3189 struct bfd_elf_section_data *esd = elf_section_data (asect);
3190 Elf_Internal_Shdr *this_hdr;
3191 unsigned int sh_type;
3192 const char *name = asect->name;
3193 bool delay_st_name_p = false;
3194 bfd_vma mask;
3195
3196 if (arg->failed)
3197 {
3198 /* We already failed; just get out of the bfd_map_over_sections
3199 loop. */
3200 return;
3201 }
3202
3203 this_hdr = &esd->this_hdr;
3204
3205 if (arg->link_info)
3206 {
3207 /* ld: compress DWARF debug sections with names: .debug_*. */
3208 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3209 && (asect->flags & SEC_DEBUGGING)
3210 && name[1] == 'd'
3211 && name[6] == '_')
3212 {
3213 /* Set SEC_ELF_COMPRESS to indicate this section should be
3214 compressed. */
3215 asect->flags |= SEC_ELF_COMPRESS;
3216 /* If this section will be compressed, delay adding section
3217 name to section name section after it is compressed in
3218 _bfd_elf_assign_file_positions_for_non_load. */
3219 delay_st_name_p = true;
3220 }
3221 }
3222 else if ((asect->flags & SEC_ELF_RENAME))
3223 {
3224 /* objcopy: rename output DWARF debug section. */
3225 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3226 {
3227 /* When we decompress or compress with SHF_COMPRESSED,
3228 convert section name from .zdebug_* to .debug_* if
3229 needed. */
3230 if (name[1] == 'z')
3231 {
3232 char *new_name = convert_zdebug_to_debug (abfd, name);
3233 if (new_name == NULL)
3234 {
3235 arg->failed = true;
3236 return;
3237 }
3238 name = new_name;
3239 }
3240 }
3241 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3242 {
3243 /* PR binutils/18087: Compression does not always make a
3244 section smaller. So only rename the section when
3245 compression has actually taken place. If input section
3246 name is .zdebug_*, we should never compress it again. */
3247 char *new_name = convert_debug_to_zdebug (abfd, name);
3248 if (new_name == NULL)
3249 {
3250 arg->failed = true;
3251 return;
3252 }
3253 BFD_ASSERT (name[1] != 'z');
3254 name = new_name;
3255 }
3256 }
3257
3258 if (delay_st_name_p)
3259 this_hdr->sh_name = (unsigned int) -1;
3260 else
3261 {
3262 this_hdr->sh_name
3263 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3264 name, false);
3265 if (this_hdr->sh_name == (unsigned int) -1)
3266 {
3267 arg->failed = true;
3268 return;
3269 }
3270 }
3271
3272 /* Don't clear sh_flags. Assembler may set additional bits. */
3273
3274 if ((asect->flags & SEC_ALLOC) != 0
3275 || asect->user_set_vma)
3276 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3277 else
3278 this_hdr->sh_addr = 0;
3279
3280 this_hdr->sh_offset = 0;
3281 this_hdr->sh_size = asect->size;
3282 this_hdr->sh_link = 0;
3283 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3284 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3285 {
3286 _bfd_error_handler
3287 /* xgettext:c-format */
3288 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3289 abfd, asect->alignment_power, asect);
3290 arg->failed = true;
3291 return;
3292 }
3293 /* Set sh_addralign to the highest power of two given by alignment
3294 consistent with the section VMA. Linker scripts can force VMA. */
3295 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3296 this_hdr->sh_addralign = mask & -mask;
3297 /* The sh_entsize and sh_info fields may have been set already by
3298 copy_private_section_data. */
3299
3300 this_hdr->bfd_section = asect;
3301 this_hdr->contents = NULL;
3302
3303 /* If the section type is unspecified, we set it based on
3304 asect->flags. */
3305 if ((asect->flags & SEC_GROUP) != 0)
3306 sh_type = SHT_GROUP;
3307 else
3308 sh_type = bfd_elf_get_default_section_type (asect->flags);
3309
3310 if (this_hdr->sh_type == SHT_NULL)
3311 this_hdr->sh_type = sh_type;
3312 else if (this_hdr->sh_type == SHT_NOBITS
3313 && sh_type == SHT_PROGBITS
3314 && (asect->flags & SEC_ALLOC) != 0)
3315 {
3316 /* Warn if we are changing a NOBITS section to PROGBITS, but
3317 allow the link to proceed. This can happen when users link
3318 non-bss input sections to bss output sections, or emit data
3319 to a bss output section via a linker script. */
3320 _bfd_error_handler
3321 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3322 this_hdr->sh_type = sh_type;
3323 }
3324
3325 switch (this_hdr->sh_type)
3326 {
3327 default:
3328 break;
3329
3330 case SHT_STRTAB:
3331 case SHT_NOTE:
3332 case SHT_NOBITS:
3333 case SHT_PROGBITS:
3334 break;
3335
3336 case SHT_INIT_ARRAY:
3337 case SHT_FINI_ARRAY:
3338 case SHT_PREINIT_ARRAY:
3339 this_hdr->sh_entsize = bed->s->arch_size / 8;
3340 break;
3341
3342 case SHT_HASH:
3343 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3344 break;
3345
3346 case SHT_DYNSYM:
3347 this_hdr->sh_entsize = bed->s->sizeof_sym;
3348 break;
3349
3350 case SHT_DYNAMIC:
3351 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3352 break;
3353
3354 case SHT_RELA:
3355 if (get_elf_backend_data (abfd)->may_use_rela_p)
3356 this_hdr->sh_entsize = bed->s->sizeof_rela;
3357 break;
3358
3359 case SHT_REL:
3360 if (get_elf_backend_data (abfd)->may_use_rel_p)
3361 this_hdr->sh_entsize = bed->s->sizeof_rel;
3362 break;
3363
3364 case SHT_GNU_versym:
3365 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3366 break;
3367
3368 case SHT_GNU_verdef:
3369 this_hdr->sh_entsize = 0;
3370 /* objcopy or strip will copy over sh_info, but may not set
3371 cverdefs. The linker will set cverdefs, but sh_info will be
3372 zero. */
3373 if (this_hdr->sh_info == 0)
3374 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3375 else
3376 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3377 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3378 break;
3379
3380 case SHT_GNU_verneed:
3381 this_hdr->sh_entsize = 0;
3382 /* objcopy or strip will copy over sh_info, but may not set
3383 cverrefs. The linker will set cverrefs, but sh_info will be
3384 zero. */
3385 if (this_hdr->sh_info == 0)
3386 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3387 else
3388 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3389 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3390 break;
3391
3392 case SHT_GROUP:
3393 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3394 break;
3395
3396 case SHT_GNU_HASH:
3397 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3398 break;
3399 }
3400
3401 if ((asect->flags & SEC_ALLOC) != 0)
3402 this_hdr->sh_flags |= SHF_ALLOC;
3403 if ((asect->flags & SEC_READONLY) == 0)
3404 this_hdr->sh_flags |= SHF_WRITE;
3405 if ((asect->flags & SEC_CODE) != 0)
3406 this_hdr->sh_flags |= SHF_EXECINSTR;
3407 if ((asect->flags & SEC_MERGE) != 0)
3408 {
3409 this_hdr->sh_flags |= SHF_MERGE;
3410 this_hdr->sh_entsize = asect->entsize;
3411 }
3412 if ((asect->flags & SEC_STRINGS) != 0)
3413 this_hdr->sh_flags |= SHF_STRINGS;
3414 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3415 this_hdr->sh_flags |= SHF_GROUP;
3416 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3417 {
3418 this_hdr->sh_flags |= SHF_TLS;
3419 if (asect->size == 0
3420 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3421 {
3422 struct bfd_link_order *o = asect->map_tail.link_order;
3423
3424 this_hdr->sh_size = 0;
3425 if (o != NULL)
3426 {
3427 this_hdr->sh_size = o->offset + o->size;
3428 if (this_hdr->sh_size != 0)
3429 this_hdr->sh_type = SHT_NOBITS;
3430 }
3431 }
3432 }
3433 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3434 this_hdr->sh_flags |= SHF_EXCLUDE;
3435
3436 /* If the section has relocs, set up a section header for the
3437 SHT_REL[A] section. If two relocation sections are required for
3438 this section, it is up to the processor-specific back-end to
3439 create the other. */
3440 if ((asect->flags & SEC_RELOC) != 0)
3441 {
3442 /* When doing a relocatable link, create both REL and RELA sections if
3443 needed. */
3444 if (arg->link_info
3445 /* Do the normal setup if we wouldn't create any sections here. */
3446 && esd->rel.count + esd->rela.count > 0
3447 && (bfd_link_relocatable (arg->link_info)
3448 || arg->link_info->emitrelocations))
3449 {
3450 if (esd->rel.count && esd->rel.hdr == NULL
3451 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3452 false, delay_st_name_p))
3453 {
3454 arg->failed = true;
3455 return;
3456 }
3457 if (esd->rela.count && esd->rela.hdr == NULL
3458 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3459 true, delay_st_name_p))
3460 {
3461 arg->failed = true;
3462 return;
3463 }
3464 }
3465 else if (!_bfd_elf_init_reloc_shdr (abfd,
3466 (asect->use_rela_p
3467 ? &esd->rela : &esd->rel),
3468 name,
3469 asect->use_rela_p,
3470 delay_st_name_p))
3471 {
3472 arg->failed = true;
3473 return;
3474 }
3475 }
3476
3477 /* Check for processor-specific section types. */
3478 sh_type = this_hdr->sh_type;
3479 if (bed->elf_backend_fake_sections
3480 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3481 {
3482 arg->failed = true;
3483 return;
3484 }
3485
3486 if (sh_type == SHT_NOBITS && asect->size != 0)
3487 {
3488 /* Don't change the header type from NOBITS if we are being
3489 called for objcopy --only-keep-debug. */
3490 this_hdr->sh_type = sh_type;
3491 }
3492 }
3493
3494 /* Fill in the contents of a SHT_GROUP section. Called from
3495 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3496 when ELF targets use the generic linker, ld. Called for ld -r
3497 from bfd_elf_final_link. */
3498
3499 void
3500 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3501 {
3502 bool *failedptr = (bool *) failedptrarg;
3503 asection *elt, *first;
3504 unsigned char *loc;
3505 bool gas;
3506
3507 /* Ignore linker created group section. See elfNN_ia64_object_p in
3508 elfxx-ia64.c. */
3509 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3510 || sec->size == 0
3511 || *failedptr)
3512 return;
3513
3514 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3515 {
3516 unsigned long symindx = 0;
3517
3518 /* elf_group_id will have been set up by objcopy and the
3519 generic linker. */
3520 if (elf_group_id (sec) != NULL)
3521 symindx = elf_group_id (sec)->udata.i;
3522
3523 if (symindx == 0)
3524 {
3525 /* If called from the assembler, swap_out_syms will have set up
3526 elf_section_syms.
3527 PR 25699: A corrupt input file could contain bogus group info. */
3528 if (elf_section_syms (abfd) == 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 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3618 }
3619 if (elf_sec->rela.hdr != NULL
3620 && (gas
3621 || (input_elf_sec->rela.hdr != NULL
3622 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3623 {
3624 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3625 loc -= 4;
3626 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3627 }
3628 loc -= 4;
3629 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3630 }
3631 elt = elf_next_in_group (elt);
3632 if (elt == first)
3633 break;
3634 }
3635
3636 loc -= 4;
3637 BFD_ASSERT (loc == sec->contents);
3638
3639 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3640 }
3641
3642 /* Given NAME, the name of a relocation section stripped of its
3643 .rel/.rela prefix, return the section in ABFD to which the
3644 relocations apply. */
3645
3646 asection *
3647 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3648 {
3649 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3650 section likely apply to .got.plt or .got section. */
3651 if (get_elf_backend_data (abfd)->want_got_plt
3652 && strcmp (name, ".plt") == 0)
3653 {
3654 asection *sec;
3655
3656 name = ".got.plt";
3657 sec = bfd_get_section_by_name (abfd, name);
3658 if (sec != NULL)
3659 return sec;
3660 name = ".got";
3661 }
3662
3663 return bfd_get_section_by_name (abfd, name);
3664 }
3665
3666 /* Return the section to which RELOC_SEC applies. */
3667
3668 static asection *
3669 elf_get_reloc_section (asection *reloc_sec)
3670 {
3671 const char *name;
3672 unsigned int type;
3673 bfd *abfd;
3674 const struct elf_backend_data *bed;
3675
3676 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3677 if (type != SHT_REL && type != SHT_RELA)
3678 return NULL;
3679
3680 /* We look up the section the relocs apply to by name. */
3681 name = reloc_sec->name;
3682 if (!startswith (name, ".rel"))
3683 return NULL;
3684 name += 4;
3685 if (type == SHT_RELA && *name++ != 'a')
3686 return NULL;
3687
3688 abfd = reloc_sec->owner;
3689 bed = get_elf_backend_data (abfd);
3690 return bed->get_reloc_section (abfd, name);
3691 }
3692
3693 /* Assign all ELF section numbers. The dummy first section is handled here
3694 too. The link/info pointers for the standard section types are filled
3695 in here too, while we're at it. LINK_INFO will be 0 when arriving
3696 here for objcopy, and when using the generic ELF linker. */
3697
3698 static bool
3699 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3700 {
3701 struct elf_obj_tdata *t = elf_tdata (abfd);
3702 asection *sec;
3703 unsigned int section_number;
3704 Elf_Internal_Shdr **i_shdrp;
3705 struct bfd_elf_section_data *d;
3706 bool need_symtab;
3707 size_t amt;
3708
3709 section_number = 1;
3710
3711 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3712
3713 /* SHT_GROUP sections are in relocatable files only. */
3714 if (link_info == NULL || !link_info->resolve_section_groups)
3715 {
3716 size_t reloc_count = 0;
3717
3718 /* Put SHT_GROUP sections first. */
3719 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3720 {
3721 d = elf_section_data (sec);
3722
3723 if (d->this_hdr.sh_type == SHT_GROUP)
3724 {
3725 if (sec->flags & SEC_LINKER_CREATED)
3726 {
3727 /* Remove the linker created SHT_GROUP sections. */
3728 bfd_section_list_remove (abfd, sec);
3729 abfd->section_count--;
3730 }
3731 else
3732 d->this_idx = section_number++;
3733 }
3734
3735 /* Count relocations. */
3736 reloc_count += sec->reloc_count;
3737 }
3738
3739 /* Clear HAS_RELOC if there are no relocations. */
3740 if (reloc_count == 0)
3741 abfd->flags &= ~HAS_RELOC;
3742 }
3743
3744 for (sec = abfd->sections; sec; sec = sec->next)
3745 {
3746 d = elf_section_data (sec);
3747
3748 if (d->this_hdr.sh_type != SHT_GROUP)
3749 d->this_idx = section_number++;
3750 if (d->this_hdr.sh_name != (unsigned int) -1)
3751 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3752 if (d->rel.hdr)
3753 {
3754 d->rel.idx = section_number++;
3755 if (d->rel.hdr->sh_name != (unsigned int) -1)
3756 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3757 }
3758 else
3759 d->rel.idx = 0;
3760
3761 if (d->rela.hdr)
3762 {
3763 d->rela.idx = section_number++;
3764 if (d->rela.hdr->sh_name != (unsigned int) -1)
3765 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3766 }
3767 else
3768 d->rela.idx = 0;
3769 }
3770
3771 need_symtab = (bfd_get_symcount (abfd) > 0
3772 || (link_info == NULL
3773 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3774 == HAS_RELOC)));
3775 if (need_symtab)
3776 {
3777 elf_onesymtab (abfd) = section_number++;
3778 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3779 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3780 {
3781 elf_section_list *entry;
3782
3783 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3784
3785 entry = bfd_zalloc (abfd, sizeof (*entry));
3786 entry->ndx = section_number++;
3787 elf_symtab_shndx_list (abfd) = entry;
3788 entry->hdr.sh_name
3789 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3790 ".symtab_shndx", false);
3791 if (entry->hdr.sh_name == (unsigned int) -1)
3792 return false;
3793 }
3794 elf_strtab_sec (abfd) = section_number++;
3795 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3796 }
3797
3798 elf_shstrtab_sec (abfd) = section_number++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3800 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3801
3802 if (section_number >= SHN_LORESERVE)
3803 {
3804 /* xgettext:c-format */
3805 _bfd_error_handler (_("%pB: too many sections: %u"),
3806 abfd, section_number);
3807 return false;
3808 }
3809
3810 elf_numsections (abfd) = section_number;
3811 elf_elfheader (abfd)->e_shnum = section_number;
3812
3813 /* Set up the list of section header pointers, in agreement with the
3814 indices. */
3815 amt = section_number * sizeof (Elf_Internal_Shdr *);
3816 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3817 if (i_shdrp == NULL)
3818 return false;
3819
3820 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3821 sizeof (Elf_Internal_Shdr));
3822 if (i_shdrp[0] == NULL)
3823 {
3824 bfd_release (abfd, i_shdrp);
3825 return false;
3826 }
3827
3828 elf_elfsections (abfd) = i_shdrp;
3829
3830 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3831 if (need_symtab)
3832 {
3833 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3834 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3835 {
3836 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3837 BFD_ASSERT (entry != NULL);
3838 i_shdrp[entry->ndx] = & entry->hdr;
3839 entry->hdr.sh_link = elf_onesymtab (abfd);
3840 }
3841 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3842 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3843 }
3844
3845 for (sec = abfd->sections; sec; sec = sec->next)
3846 {
3847 asection *s;
3848
3849 d = elf_section_data (sec);
3850
3851 i_shdrp[d->this_idx] = &d->this_hdr;
3852 if (d->rel.idx != 0)
3853 i_shdrp[d->rel.idx] = d->rel.hdr;
3854 if (d->rela.idx != 0)
3855 i_shdrp[d->rela.idx] = d->rela.hdr;
3856
3857 /* Fill in the sh_link and sh_info fields while we're at it. */
3858
3859 /* sh_link of a reloc section is the section index of the symbol
3860 table. sh_info is the section index of the section to which
3861 the relocation entries apply. */
3862 if (d->rel.idx != 0)
3863 {
3864 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3865 d->rel.hdr->sh_info = d->this_idx;
3866 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3867 }
3868 if (d->rela.idx != 0)
3869 {
3870 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3871 d->rela.hdr->sh_info = d->this_idx;
3872 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3873 }
3874
3875 /* We need to set up sh_link for SHF_LINK_ORDER. */
3876 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3877 {
3878 s = elf_linked_to_section (sec);
3879 /* We can now have a NULL linked section pointer.
3880 This happens when the sh_link field is 0, which is done
3881 when a linked to section is discarded but the linking
3882 section has been retained for some reason. */
3883 if (s)
3884 {
3885 /* Check discarded linkonce section. */
3886 if (discarded_section (s))
3887 {
3888 asection *kept;
3889 _bfd_error_handler
3890 /* xgettext:c-format */
3891 (_("%pB: sh_link of section `%pA' points to"
3892 " discarded section `%pA' of `%pB'"),
3893 abfd, d->this_hdr.bfd_section, s, s->owner);
3894 /* Point to the kept section if it has the same
3895 size as the discarded one. */
3896 kept = _bfd_elf_check_kept_section (s, link_info);
3897 if (kept == NULL)
3898 {
3899 bfd_set_error (bfd_error_bad_value);
3900 return false;
3901 }
3902 s = kept;
3903 }
3904 /* Handle objcopy. */
3905 else if (s->output_section == NULL)
3906 {
3907 _bfd_error_handler
3908 /* xgettext:c-format */
3909 (_("%pB: sh_link of section `%pA' points to"
3910 " removed section `%pA' of `%pB'"),
3911 abfd, d->this_hdr.bfd_section, s, s->owner);
3912 bfd_set_error (bfd_error_bad_value);
3913 return false;
3914 }
3915 s = s->output_section;
3916 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3917 }
3918 }
3919
3920 switch (d->this_hdr.sh_type)
3921 {
3922 case SHT_REL:
3923 case SHT_RELA:
3924 /* A reloc section which we are treating as a normal BFD
3925 section. sh_link is the section index of the symbol
3926 table. sh_info is the section index of the section to
3927 which the relocation entries apply. We assume that an
3928 allocated reloc section uses the dynamic symbol table.
3929 FIXME: How can we be sure? */
3930 s = bfd_get_section_by_name (abfd, ".dynsym");
3931 if (s != NULL)
3932 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3933
3934 s = elf_get_reloc_section (sec);
3935 if (s != NULL)
3936 {
3937 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3938 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3939 }
3940 break;
3941
3942 case SHT_STRTAB:
3943 /* We assume that a section named .stab*str is a stabs
3944 string section. We look for a section with the same name
3945 but without the trailing ``str'', and set its sh_link
3946 field to point to this section. */
3947 if (startswith (sec->name, ".stab")
3948 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3949 {
3950 size_t len;
3951 char *alc;
3952
3953 len = strlen (sec->name);
3954 alc = (char *) bfd_malloc (len - 2);
3955 if (alc == NULL)
3956 return false;
3957 memcpy (alc, sec->name, len - 3);
3958 alc[len - 3] = '\0';
3959 s = bfd_get_section_by_name (abfd, alc);
3960 free (alc);
3961 if (s != NULL)
3962 {
3963 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3964
3965 /* This is a .stab section. */
3966 elf_section_data (s)->this_hdr.sh_entsize = 12;
3967 }
3968 }
3969 break;
3970
3971 case SHT_DYNAMIC:
3972 case SHT_DYNSYM:
3973 case SHT_GNU_verneed:
3974 case SHT_GNU_verdef:
3975 /* sh_link is the section header index of the string table
3976 used for the dynamic entries, or the symbol table, or the
3977 version strings. */
3978 s = bfd_get_section_by_name (abfd, ".dynstr");
3979 if (s != NULL)
3980 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3981 break;
3982
3983 case SHT_GNU_LIBLIST:
3984 /* sh_link is the section header index of the prelink library
3985 list used for the dynamic entries, or the symbol table, or
3986 the version strings. */
3987 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3988 ? ".dynstr" : ".gnu.libstr");
3989 if (s != NULL)
3990 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3991 break;
3992
3993 case SHT_HASH:
3994 case SHT_GNU_HASH:
3995 case SHT_GNU_versym:
3996 /* sh_link is the section header index of the symbol table
3997 this hash table or version table is for. */
3998 s = bfd_get_section_by_name (abfd, ".dynsym");
3999 if (s != NULL)
4000 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4001 break;
4002
4003 case SHT_GROUP:
4004 d->this_hdr.sh_link = elf_onesymtab (abfd);
4005 }
4006 }
4007
4008 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4009 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4010 debug section name from .debug_* to .zdebug_* if needed. */
4011
4012 return true;
4013 }
4014
4015 static bool
4016 sym_is_global (bfd *abfd, asymbol *sym)
4017 {
4018 /* If the backend has a special mapping, use it. */
4019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4020 if (bed->elf_backend_sym_is_global)
4021 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4022
4023 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4024 || bfd_is_und_section (bfd_asymbol_section (sym))
4025 || bfd_is_com_section (bfd_asymbol_section (sym)));
4026 }
4027
4028 /* Filter global symbols of ABFD to include in the import library. All
4029 SYMCOUNT symbols of ABFD can be examined from their pointers in
4030 SYMS. Pointers of symbols to keep should be stored contiguously at
4031 the beginning of that array.
4032
4033 Returns the number of symbols to keep. */
4034
4035 unsigned int
4036 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4037 asymbol **syms, long symcount)
4038 {
4039 long src_count, dst_count = 0;
4040
4041 for (src_count = 0; src_count < symcount; src_count++)
4042 {
4043 asymbol *sym = syms[src_count];
4044 char *name = (char *) bfd_asymbol_name (sym);
4045 struct bfd_link_hash_entry *h;
4046
4047 if (!sym_is_global (abfd, sym))
4048 continue;
4049
4050 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4051 if (h == NULL)
4052 continue;
4053 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4054 continue;
4055 if (h->linker_def || h->ldscript_def)
4056 continue;
4057
4058 syms[dst_count++] = sym;
4059 }
4060
4061 syms[dst_count] = NULL;
4062
4063 return dst_count;
4064 }
4065
4066 /* Don't output section symbols for sections that are not going to be
4067 output, that are duplicates or there is no BFD section. */
4068
4069 static bool
4070 ignore_section_sym (bfd *abfd, asymbol *sym)
4071 {
4072 elf_symbol_type *type_ptr;
4073
4074 if (sym == NULL)
4075 return false;
4076
4077 if ((sym->flags & BSF_SECTION_SYM) == 0)
4078 return false;
4079
4080 /* Ignore the section symbol if it isn't used. */
4081 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4082 return true;
4083
4084 if (sym->section == NULL)
4085 return true;
4086
4087 type_ptr = elf_symbol_from (sym);
4088 return ((type_ptr != NULL
4089 && type_ptr->internal_elf_sym.st_shndx != 0
4090 && bfd_is_abs_section (sym->section))
4091 || !(sym->section->owner == abfd
4092 || (sym->section->output_section != NULL
4093 && sym->section->output_section->owner == abfd
4094 && sym->section->output_offset == 0)
4095 || bfd_is_abs_section (sym->section)));
4096 }
4097
4098 /* Map symbol from it's internal number to the external number, moving
4099 all local symbols to be at the head of the list. */
4100
4101 static bool
4102 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4103 {
4104 unsigned int symcount = bfd_get_symcount (abfd);
4105 asymbol **syms = bfd_get_outsymbols (abfd);
4106 asymbol **sect_syms;
4107 unsigned int num_locals = 0;
4108 unsigned int num_globals = 0;
4109 unsigned int num_locals2 = 0;
4110 unsigned int num_globals2 = 0;
4111 unsigned int max_index = 0;
4112 unsigned int idx;
4113 asection *asect;
4114 asymbol **new_syms;
4115 size_t amt;
4116
4117 #ifdef DEBUG
4118 fprintf (stderr, "elf_map_symbols\n");
4119 fflush (stderr);
4120 #endif
4121
4122 for (asect = abfd->sections; asect; asect = asect->next)
4123 {
4124 if (max_index < asect->index)
4125 max_index = asect->index;
4126 }
4127
4128 max_index++;
4129 amt = max_index * sizeof (asymbol *);
4130 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4131 if (sect_syms == NULL)
4132 return false;
4133 elf_section_syms (abfd) = sect_syms;
4134 elf_num_section_syms (abfd) = max_index;
4135
4136 /* Init sect_syms entries for any section symbols we have already
4137 decided to output. */
4138 for (idx = 0; idx < symcount; idx++)
4139 {
4140 asymbol *sym = syms[idx];
4141
4142 if ((sym->flags & BSF_SECTION_SYM) != 0
4143 && sym->value == 0
4144 && !ignore_section_sym (abfd, sym)
4145 && !bfd_is_abs_section (sym->section))
4146 {
4147 asection *sec = sym->section;
4148
4149 if (sec->owner != abfd)
4150 sec = sec->output_section;
4151
4152 sect_syms[sec->index] = syms[idx];
4153 }
4154 }
4155
4156 /* Classify all of the symbols. */
4157 for (idx = 0; idx < symcount; idx++)
4158 {
4159 if (sym_is_global (abfd, syms[idx]))
4160 num_globals++;
4161 else if (!ignore_section_sym (abfd, syms[idx]))
4162 num_locals++;
4163 }
4164
4165 /* We will be adding a section symbol for each normal BFD section. Most
4166 sections will already have a section symbol in outsymbols, but
4167 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4168 at least in that case. */
4169 for (asect = abfd->sections; asect; asect = asect->next)
4170 {
4171 asymbol *sym = asect->symbol;
4172 /* Don't include ignored section symbols. */
4173 if (!ignore_section_sym (abfd, sym)
4174 && sect_syms[asect->index] == NULL)
4175 {
4176 if (!sym_is_global (abfd, asect->symbol))
4177 num_locals++;
4178 else
4179 num_globals++;
4180 }
4181 }
4182
4183 /* Now sort the symbols so the local symbols are first. */
4184 amt = (num_locals + num_globals) * sizeof (asymbol *);
4185 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4186 if (new_syms == NULL)
4187 return false;
4188
4189 for (idx = 0; idx < symcount; idx++)
4190 {
4191 asymbol *sym = syms[idx];
4192 unsigned int i;
4193
4194 if (sym_is_global (abfd, sym))
4195 i = num_locals + num_globals2++;
4196 /* Don't include ignored section symbols. */
4197 else if (!ignore_section_sym (abfd, sym))
4198 i = num_locals2++;
4199 else
4200 continue;
4201 new_syms[i] = sym;
4202 sym->udata.i = i + 1;
4203 }
4204 for (asect = abfd->sections; asect; asect = asect->next)
4205 {
4206 asymbol *sym = asect->symbol;
4207 if (!ignore_section_sym (abfd, sym)
4208 && sect_syms[asect->index] == NULL)
4209 {
4210 unsigned int i;
4211
4212 sect_syms[asect->index] = sym;
4213 if (!sym_is_global (abfd, sym))
4214 i = num_locals2++;
4215 else
4216 i = num_locals + num_globals2++;
4217 new_syms[i] = sym;
4218 sym->udata.i = i + 1;
4219 }
4220 }
4221
4222 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4223
4224 *pnum_locals = num_locals;
4225 return true;
4226 }
4227
4228 /* Align to the maximum file alignment that could be required for any
4229 ELF data structure. */
4230
4231 static inline file_ptr
4232 align_file_position (file_ptr off, int align)
4233 {
4234 return (off + align - 1) & ~(align - 1);
4235 }
4236
4237 /* Assign a file position to a section, optionally aligning to the
4238 required section alignment. */
4239
4240 file_ptr
4241 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4242 file_ptr offset,
4243 bool align)
4244 {
4245 if (align && i_shdrp->sh_addralign > 1)
4246 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4247 i_shdrp->sh_offset = offset;
4248 if (i_shdrp->bfd_section != NULL)
4249 i_shdrp->bfd_section->filepos = offset;
4250 if (i_shdrp->sh_type != SHT_NOBITS)
4251 offset += i_shdrp->sh_size;
4252 return offset;
4253 }
4254
4255 /* Compute the file positions we are going to put the sections at, and
4256 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4257 is not NULL, this is being called by the ELF backend linker. */
4258
4259 bool
4260 _bfd_elf_compute_section_file_positions (bfd *abfd,
4261 struct bfd_link_info *link_info)
4262 {
4263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4264 struct fake_section_arg fsargs;
4265 bool failed;
4266 struct elf_strtab_hash *strtab = NULL;
4267 Elf_Internal_Shdr *shstrtab_hdr;
4268 bool need_symtab;
4269
4270 if (abfd->output_has_begun)
4271 return true;
4272
4273 /* Do any elf backend specific processing first. */
4274 if (bed->elf_backend_begin_write_processing)
4275 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4276
4277 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4278 return false;
4279
4280 fsargs.failed = false;
4281 fsargs.link_info = link_info;
4282 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4283 if (fsargs.failed)
4284 return false;
4285
4286 if (!assign_section_numbers (abfd, link_info))
4287 return false;
4288
4289 /* The backend linker builds symbol table information itself. */
4290 need_symtab = (link_info == NULL
4291 && (bfd_get_symcount (abfd) > 0
4292 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4293 == HAS_RELOC)));
4294 if (need_symtab)
4295 {
4296 /* Non-zero if doing a relocatable link. */
4297 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4298
4299 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4300 return false;
4301 }
4302
4303 failed = false;
4304 if (link_info == NULL)
4305 {
4306 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4307 if (failed)
4308 return false;
4309 }
4310
4311 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4312 /* sh_name was set in init_file_header. */
4313 shstrtab_hdr->sh_type = SHT_STRTAB;
4314 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4315 shstrtab_hdr->sh_addr = 0;
4316 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4317 shstrtab_hdr->sh_entsize = 0;
4318 shstrtab_hdr->sh_link = 0;
4319 shstrtab_hdr->sh_info = 0;
4320 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4321 shstrtab_hdr->sh_addralign = 1;
4322
4323 if (!assign_file_positions_except_relocs (abfd, link_info))
4324 return false;
4325
4326 if (need_symtab)
4327 {
4328 file_ptr off;
4329 Elf_Internal_Shdr *hdr;
4330
4331 off = elf_next_file_pos (abfd);
4332
4333 hdr = & elf_symtab_hdr (abfd);
4334 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4335
4336 if (elf_symtab_shndx_list (abfd) != NULL)
4337 {
4338 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4339 if (hdr->sh_size != 0)
4340 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4341 /* FIXME: What about other symtab_shndx sections in the list ? */
4342 }
4343
4344 hdr = &elf_tdata (abfd)->strtab_hdr;
4345 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4346
4347 elf_next_file_pos (abfd) = off;
4348
4349 /* Now that we know where the .strtab section goes, write it
4350 out. */
4351 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4352 || ! _bfd_elf_strtab_emit (abfd, strtab))
4353 return false;
4354 _bfd_elf_strtab_free (strtab);
4355 }
4356
4357 abfd->output_has_begun = true;
4358
4359 return true;
4360 }
4361
4362 /* Make an initial estimate of the size of the program header. If we
4363 get the number wrong here, we'll redo section placement. */
4364
4365 static bfd_size_type
4366 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4367 {
4368 size_t segs;
4369 asection *s;
4370 const struct elf_backend_data *bed;
4371
4372 /* Assume we will need exactly two PT_LOAD segments: one for text
4373 and one for data. */
4374 segs = 2;
4375
4376 s = bfd_get_section_by_name (abfd, ".interp");
4377 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4378 {
4379 /* If we have a loadable interpreter section, we need a
4380 PT_INTERP segment. In this case, assume we also need a
4381 PT_PHDR segment, although that may not be true for all
4382 targets. */
4383 segs += 2;
4384 }
4385
4386 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4387 {
4388 /* We need a PT_DYNAMIC segment. */
4389 ++segs;
4390 }
4391
4392 if (info != NULL && info->relro)
4393 {
4394 /* We need a PT_GNU_RELRO segment. */
4395 ++segs;
4396 }
4397
4398 if (elf_eh_frame_hdr (abfd))
4399 {
4400 /* We need a PT_GNU_EH_FRAME segment. */
4401 ++segs;
4402 }
4403
4404 if (elf_stack_flags (abfd))
4405 {
4406 /* We need a PT_GNU_STACK segment. */
4407 ++segs;
4408 }
4409
4410 s = bfd_get_section_by_name (abfd,
4411 NOTE_GNU_PROPERTY_SECTION_NAME);
4412 if (s != NULL && s->size != 0)
4413 {
4414 /* We need a PT_GNU_PROPERTY segment. */
4415 ++segs;
4416 }
4417
4418 for (s = abfd->sections; s != NULL; s = s->next)
4419 {
4420 if ((s->flags & SEC_LOAD) != 0
4421 && elf_section_type (s) == SHT_NOTE)
4422 {
4423 unsigned int alignment_power;
4424 /* We need a PT_NOTE segment. */
4425 ++segs;
4426 /* Try to create just one PT_NOTE segment for all adjacent
4427 loadable SHT_NOTE sections. gABI requires that within a
4428 PT_NOTE segment (and also inside of each SHT_NOTE section)
4429 each note should have the same alignment. So we check
4430 whether the sections are correctly aligned. */
4431 alignment_power = s->alignment_power;
4432 while (s->next != NULL
4433 && s->next->alignment_power == alignment_power
4434 && (s->next->flags & SEC_LOAD) != 0
4435 && elf_section_type (s->next) == SHT_NOTE)
4436 s = s->next;
4437 }
4438 }
4439
4440 for (s = abfd->sections; s != NULL; s = s->next)
4441 {
4442 if (s->flags & SEC_THREAD_LOCAL)
4443 {
4444 /* We need a PT_TLS segment. */
4445 ++segs;
4446 break;
4447 }
4448 }
4449
4450 bed = get_elf_backend_data (abfd);
4451
4452 if ((abfd->flags & D_PAGED) != 0
4453 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4454 {
4455 /* Add a PT_GNU_MBIND segment for each mbind section. */
4456 bfd_vma commonpagesize;
4457 unsigned int page_align_power;
4458
4459 if (info != NULL)
4460 commonpagesize = info->commonpagesize;
4461 else
4462 commonpagesize = bed->commonpagesize;
4463 page_align_power = bfd_log2 (commonpagesize);
4464 for (s = abfd->sections; s != NULL; s = s->next)
4465 if (elf_section_flags (s) & SHF_GNU_MBIND)
4466 {
4467 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4468 {
4469 _bfd_error_handler
4470 /* xgettext:c-format */
4471 (_("%pB: GNU_MBIND section `%pA' has invalid "
4472 "sh_info field: %d"),
4473 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4474 continue;
4475 }
4476 /* Align mbind section to page size. */
4477 if (s->alignment_power < page_align_power)
4478 s->alignment_power = page_align_power;
4479 segs ++;
4480 }
4481 }
4482
4483 /* Let the backend count up any program headers it might need. */
4484 if (bed->elf_backend_additional_program_headers)
4485 {
4486 int a;
4487
4488 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4489 if (a == -1)
4490 abort ();
4491 segs += a;
4492 }
4493
4494 return segs * bed->s->sizeof_phdr;
4495 }
4496
4497 /* Find the segment that contains the output_section of section. */
4498
4499 Elf_Internal_Phdr *
4500 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4501 {
4502 struct elf_segment_map *m;
4503 Elf_Internal_Phdr *p;
4504
4505 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4506 m != NULL;
4507 m = m->next, p++)
4508 {
4509 int i;
4510
4511 for (i = m->count - 1; i >= 0; i--)
4512 if (m->sections[i] == section)
4513 return p;
4514 }
4515
4516 return NULL;
4517 }
4518
4519 /* Create a mapping from a set of sections to a program segment. */
4520
4521 static struct elf_segment_map *
4522 make_mapping (bfd *abfd,
4523 asection **sections,
4524 unsigned int from,
4525 unsigned int to,
4526 bool phdr)
4527 {
4528 struct elf_segment_map *m;
4529 unsigned int i;
4530 asection **hdrpp;
4531 size_t amt;
4532
4533 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4534 amt += (to - from) * sizeof (asection *);
4535 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4536 if (m == NULL)
4537 return NULL;
4538 m->next = NULL;
4539 m->p_type = PT_LOAD;
4540 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4541 m->sections[i - from] = *hdrpp;
4542 m->count = to - from;
4543
4544 if (from == 0 && phdr)
4545 {
4546 /* Include the headers in the first PT_LOAD segment. */
4547 m->includes_filehdr = 1;
4548 m->includes_phdrs = 1;
4549 }
4550
4551 return m;
4552 }
4553
4554 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4555 on failure. */
4556
4557 struct elf_segment_map *
4558 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4559 {
4560 struct elf_segment_map *m;
4561
4562 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4563 sizeof (struct elf_segment_map));
4564 if (m == NULL)
4565 return NULL;
4566 m->next = NULL;
4567 m->p_type = PT_DYNAMIC;
4568 m->count = 1;
4569 m->sections[0] = dynsec;
4570
4571 return m;
4572 }
4573
4574 /* Possibly add or remove segments from the segment map. */
4575
4576 static bool
4577 elf_modify_segment_map (bfd *abfd,
4578 struct bfd_link_info *info,
4579 bool remove_empty_load)
4580 {
4581 struct elf_segment_map **m;
4582 const struct elf_backend_data *bed;
4583
4584 /* The placement algorithm assumes that non allocated sections are
4585 not in PT_LOAD segments. We ensure this here by removing such
4586 sections from the segment map. We also remove excluded
4587 sections. Finally, any PT_LOAD segment without sections is
4588 removed. */
4589 m = &elf_seg_map (abfd);
4590 while (*m)
4591 {
4592 unsigned int i, new_count;
4593
4594 for (new_count = 0, i = 0; i < (*m)->count; i++)
4595 {
4596 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4597 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4598 || (*m)->p_type != PT_LOAD))
4599 {
4600 (*m)->sections[new_count] = (*m)->sections[i];
4601 new_count++;
4602 }
4603 }
4604 (*m)->count = new_count;
4605
4606 if (remove_empty_load
4607 && (*m)->p_type == PT_LOAD
4608 && (*m)->count == 0
4609 && !(*m)->includes_phdrs)
4610 *m = (*m)->next;
4611 else
4612 m = &(*m)->next;
4613 }
4614
4615 bed = get_elf_backend_data (abfd);
4616 if (bed->elf_backend_modify_segment_map != NULL)
4617 {
4618 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4619 return false;
4620 }
4621
4622 return true;
4623 }
4624
4625 #define IS_TBSS(s) \
4626 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4627
4628 /* Set up a mapping from BFD sections to program segments. */
4629
4630 bool
4631 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4632 {
4633 unsigned int count;
4634 struct elf_segment_map *m;
4635 asection **sections = NULL;
4636 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4637 bool no_user_phdrs;
4638
4639 no_user_phdrs = elf_seg_map (abfd) == NULL;
4640
4641 if (info != NULL)
4642 info->user_phdrs = !no_user_phdrs;
4643
4644 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4645 {
4646 asection *s;
4647 unsigned int i;
4648 struct elf_segment_map *mfirst;
4649 struct elf_segment_map **pm;
4650 asection *last_hdr;
4651 bfd_vma last_size;
4652 unsigned int hdr_index;
4653 bfd_vma maxpagesize;
4654 asection **hdrpp;
4655 bool phdr_in_segment;
4656 bool writable;
4657 bool executable;
4658 unsigned int tls_count = 0;
4659 asection *first_tls = NULL;
4660 asection *first_mbind = NULL;
4661 asection *dynsec, *eh_frame_hdr;
4662 size_t amt;
4663 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4664 bfd_size_type phdr_size; /* Octets/bytes. */
4665 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4666
4667 /* Select the allocated sections, and sort them. */
4668
4669 amt = bfd_count_sections (abfd) * sizeof (asection *);
4670 sections = (asection **) bfd_malloc (amt);
4671 if (sections == NULL)
4672 goto error_return;
4673
4674 /* Calculate top address, avoiding undefined behaviour of shift
4675 left operator when shift count is equal to size of type
4676 being shifted. */
4677 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4678 addr_mask = (addr_mask << 1) + 1;
4679
4680 i = 0;
4681 for (s = abfd->sections; s != NULL; s = s->next)
4682 {
4683 if ((s->flags & SEC_ALLOC) != 0)
4684 {
4685 /* target_index is unused until bfd_elf_final_link
4686 starts output of section symbols. Use it to make
4687 qsort stable. */
4688 s->target_index = i;
4689 sections[i] = s;
4690 ++i;
4691 /* A wrapping section potentially clashes with header. */
4692 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4693 wrap_to = (s->lma + s->size / opb) & addr_mask;
4694 }
4695 }
4696 BFD_ASSERT (i <= bfd_count_sections (abfd));
4697 count = i;
4698
4699 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4700
4701 phdr_size = elf_program_header_size (abfd);
4702 if (phdr_size == (bfd_size_type) -1)
4703 phdr_size = get_program_header_size (abfd, info);
4704 phdr_size += bed->s->sizeof_ehdr;
4705 /* phdr_size is compared to LMA values which are in bytes. */
4706 phdr_size /= opb;
4707 if (info != NULL)
4708 maxpagesize = info->maxpagesize;
4709 else
4710 maxpagesize = bed->maxpagesize;
4711 if (maxpagesize == 0)
4712 maxpagesize = 1;
4713 phdr_in_segment = info != NULL && info->load_phdrs;
4714 if (count != 0
4715 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4716 >= (phdr_size & (maxpagesize - 1))))
4717 /* For compatibility with old scripts that may not be using
4718 SIZEOF_HEADERS, add headers when it looks like space has
4719 been left for them. */
4720 phdr_in_segment = true;
4721
4722 /* Build the mapping. */
4723 mfirst = NULL;
4724 pm = &mfirst;
4725
4726 /* If we have a .interp section, then create a PT_PHDR segment for
4727 the program headers and a PT_INTERP segment for the .interp
4728 section. */
4729 s = bfd_get_section_by_name (abfd, ".interp");
4730 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4731 {
4732 amt = sizeof (struct elf_segment_map);
4733 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4734 if (m == NULL)
4735 goto error_return;
4736 m->next = NULL;
4737 m->p_type = PT_PHDR;
4738 m->p_flags = PF_R;
4739 m->p_flags_valid = 1;
4740 m->includes_phdrs = 1;
4741 phdr_in_segment = true;
4742 *pm = m;
4743 pm = &m->next;
4744
4745 amt = sizeof (struct elf_segment_map);
4746 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4747 if (m == NULL)
4748 goto error_return;
4749 m->next = NULL;
4750 m->p_type = PT_INTERP;
4751 m->count = 1;
4752 m->sections[0] = s;
4753
4754 *pm = m;
4755 pm = &m->next;
4756 }
4757
4758 /* Look through the sections. We put sections in the same program
4759 segment when the start of the second section can be placed within
4760 a few bytes of the end of the first section. */
4761 last_hdr = NULL;
4762 last_size = 0;
4763 hdr_index = 0;
4764 writable = false;
4765 executable = false;
4766 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4767 if (dynsec != NULL
4768 && (dynsec->flags & SEC_LOAD) == 0)
4769 dynsec = NULL;
4770
4771 if ((abfd->flags & D_PAGED) == 0)
4772 phdr_in_segment = false;
4773
4774 /* Deal with -Ttext or something similar such that the first section
4775 is not adjacent to the program headers. This is an
4776 approximation, since at this point we don't know exactly how many
4777 program headers we will need. */
4778 if (phdr_in_segment && count > 0)
4779 {
4780 bfd_vma phdr_lma; /* Bytes. */
4781 bool separate_phdr = false;
4782
4783 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4784 if (info != NULL
4785 && info->separate_code
4786 && (sections[0]->flags & SEC_CODE) != 0)
4787 {
4788 /* If data sections should be separate from code and
4789 thus not executable, and the first section is
4790 executable then put the file and program headers in
4791 their own PT_LOAD. */
4792 separate_phdr = true;
4793 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4794 == (sections[0]->lma & addr_mask & -maxpagesize)))
4795 {
4796 /* The file and program headers are currently on the
4797 same page as the first section. Put them on the
4798 previous page if we can. */
4799 if (phdr_lma >= maxpagesize)
4800 phdr_lma -= maxpagesize;
4801 else
4802 separate_phdr = false;
4803 }
4804 }
4805 if ((sections[0]->lma & addr_mask) < phdr_lma
4806 || (sections[0]->lma & addr_mask) < phdr_size)
4807 /* If file and program headers would be placed at the end
4808 of memory then it's probably better to omit them. */
4809 phdr_in_segment = false;
4810 else if (phdr_lma < wrap_to)
4811 /* If a section wraps around to where we'll be placing
4812 file and program headers, then the headers will be
4813 overwritten. */
4814 phdr_in_segment = false;
4815 else if (separate_phdr)
4816 {
4817 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4818 if (m == NULL)
4819 goto error_return;
4820 m->p_paddr = phdr_lma * opb;
4821 m->p_vaddr_offset
4822 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4823 m->p_paddr_valid = 1;
4824 *pm = m;
4825 pm = &m->next;
4826 phdr_in_segment = false;
4827 }
4828 }
4829
4830 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4831 {
4832 asection *hdr;
4833 bool new_segment;
4834
4835 hdr = *hdrpp;
4836
4837 /* See if this section and the last one will fit in the same
4838 segment. */
4839
4840 if (last_hdr == NULL)
4841 {
4842 /* If we don't have a segment yet, then we don't need a new
4843 one (we build the last one after this loop). */
4844 new_segment = false;
4845 }
4846 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4847 {
4848 /* If this section has a different relation between the
4849 virtual address and the load address, then we need a new
4850 segment. */
4851 new_segment = true;
4852 }
4853 else if (hdr->lma < last_hdr->lma + last_size
4854 || last_hdr->lma + last_size < last_hdr->lma)
4855 {
4856 /* If this section has a load address that makes it overlap
4857 the previous section, then we need a new segment. */
4858 new_segment = true;
4859 }
4860 else if ((abfd->flags & D_PAGED) != 0
4861 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4862 == (hdr->lma & -maxpagesize)))
4863 {
4864 /* If we are demand paged then we can't map two disk
4865 pages onto the same memory page. */
4866 new_segment = false;
4867 }
4868 /* In the next test we have to be careful when last_hdr->lma is close
4869 to the end of the address space. If the aligned address wraps
4870 around to the start of the address space, then there are no more
4871 pages left in memory and it is OK to assume that the current
4872 section can be included in the current segment. */
4873 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4874 + maxpagesize > last_hdr->lma)
4875 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4876 + maxpagesize <= hdr->lma))
4877 {
4878 /* If putting this section in this segment would force us to
4879 skip a page in the segment, then we need a new segment. */
4880 new_segment = true;
4881 }
4882 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4883 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4884 {
4885 /* We don't want to put a loaded section after a
4886 nonloaded (ie. bss style) section in the same segment
4887 as that will force the non-loaded section to be loaded.
4888 Consider .tbss sections as loaded for this purpose. */
4889 new_segment = true;
4890 }
4891 else if ((abfd->flags & D_PAGED) == 0)
4892 {
4893 /* If the file is not demand paged, which means that we
4894 don't require the sections to be correctly aligned in the
4895 file, then there is no other reason for a new segment. */
4896 new_segment = false;
4897 }
4898 else if (info != NULL
4899 && info->separate_code
4900 && executable != ((hdr->flags & SEC_CODE) != 0))
4901 {
4902 new_segment = true;
4903 }
4904 else if (! writable
4905 && (hdr->flags & SEC_READONLY) == 0)
4906 {
4907 /* We don't want to put a writable section in a read only
4908 segment. */
4909 new_segment = true;
4910 }
4911 else
4912 {
4913 /* Otherwise, we can use the same segment. */
4914 new_segment = false;
4915 }
4916
4917 /* Allow interested parties a chance to override our decision. */
4918 if (last_hdr != NULL
4919 && info != NULL
4920 && info->callbacks->override_segment_assignment != NULL)
4921 new_segment
4922 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4923 last_hdr,
4924 new_segment);
4925
4926 if (! new_segment)
4927 {
4928 if ((hdr->flags & SEC_READONLY) == 0)
4929 writable = true;
4930 if ((hdr->flags & SEC_CODE) != 0)
4931 executable = true;
4932 last_hdr = hdr;
4933 /* .tbss sections effectively have zero size. */
4934 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4935 continue;
4936 }
4937
4938 /* We need a new program segment. We must create a new program
4939 header holding all the sections from hdr_index until hdr. */
4940
4941 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4942 if (m == NULL)
4943 goto error_return;
4944
4945 *pm = m;
4946 pm = &m->next;
4947
4948 if ((hdr->flags & SEC_READONLY) == 0)
4949 writable = true;
4950 else
4951 writable = false;
4952
4953 if ((hdr->flags & SEC_CODE) == 0)
4954 executable = false;
4955 else
4956 executable = true;
4957
4958 last_hdr = hdr;
4959 /* .tbss sections effectively have zero size. */
4960 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4961 hdr_index = i;
4962 phdr_in_segment = false;
4963 }
4964
4965 /* Create a final PT_LOAD program segment, but not if it's just
4966 for .tbss. */
4967 if (last_hdr != NULL
4968 && (i - hdr_index != 1
4969 || !IS_TBSS (last_hdr)))
4970 {
4971 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4972 if (m == NULL)
4973 goto error_return;
4974
4975 *pm = m;
4976 pm = &m->next;
4977 }
4978
4979 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4980 if (dynsec != NULL)
4981 {
4982 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4983 if (m == NULL)
4984 goto error_return;
4985 *pm = m;
4986 pm = &m->next;
4987 }
4988
4989 /* For each batch of consecutive loadable SHT_NOTE sections,
4990 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4991 because if we link together nonloadable .note sections and
4992 loadable .note sections, we will generate two .note sections
4993 in the output file. */
4994 for (s = abfd->sections; s != NULL; s = s->next)
4995 {
4996 if ((s->flags & SEC_LOAD) != 0
4997 && elf_section_type (s) == SHT_NOTE)
4998 {
4999 asection *s2;
5000 unsigned int alignment_power = s->alignment_power;
5001
5002 count = 1;
5003 for (s2 = s; s2->next != NULL; s2 = s2->next)
5004 {
5005 if (s2->next->alignment_power == alignment_power
5006 && (s2->next->flags & SEC_LOAD) != 0
5007 && elf_section_type (s2->next) == SHT_NOTE
5008 && align_power (s2->lma + s2->size / opb,
5009 alignment_power)
5010 == s2->next->lma)
5011 count++;
5012 else
5013 break;
5014 }
5015 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5016 amt += count * sizeof (asection *);
5017 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5018 if (m == NULL)
5019 goto error_return;
5020 m->next = NULL;
5021 m->p_type = PT_NOTE;
5022 m->count = count;
5023 while (count > 1)
5024 {
5025 m->sections[m->count - count--] = s;
5026 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5027 s = s->next;
5028 }
5029 m->sections[m->count - 1] = s;
5030 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5031 *pm = m;
5032 pm = &m->next;
5033 }
5034 if (s->flags & SEC_THREAD_LOCAL)
5035 {
5036 if (! tls_count)
5037 first_tls = s;
5038 tls_count++;
5039 }
5040 if (first_mbind == NULL
5041 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5042 first_mbind = s;
5043 }
5044
5045 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5046 if (tls_count > 0)
5047 {
5048 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5049 amt += tls_count * sizeof (asection *);
5050 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5051 if (m == NULL)
5052 goto error_return;
5053 m->next = NULL;
5054 m->p_type = PT_TLS;
5055 m->count = tls_count;
5056 /* Mandated PF_R. */
5057 m->p_flags = PF_R;
5058 m->p_flags_valid = 1;
5059 s = first_tls;
5060 for (i = 0; i < tls_count; ++i)
5061 {
5062 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5063 {
5064 _bfd_error_handler
5065 (_("%pB: TLS sections are not adjacent:"), abfd);
5066 s = first_tls;
5067 i = 0;
5068 while (i < tls_count)
5069 {
5070 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5071 {
5072 _bfd_error_handler (_(" TLS: %pA"), s);
5073 i++;
5074 }
5075 else
5076 _bfd_error_handler (_(" non-TLS: %pA"), s);
5077 s = s->next;
5078 }
5079 bfd_set_error (bfd_error_bad_value);
5080 goto error_return;
5081 }
5082 m->sections[i] = s;
5083 s = s->next;
5084 }
5085
5086 *pm = m;
5087 pm = &m->next;
5088 }
5089
5090 if (first_mbind
5091 && (abfd->flags & D_PAGED) != 0
5092 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5093 for (s = first_mbind; s != NULL; s = s->next)
5094 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5095 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5096 {
5097 /* Mandated PF_R. */
5098 unsigned long p_flags = PF_R;
5099 if ((s->flags & SEC_READONLY) == 0)
5100 p_flags |= PF_W;
5101 if ((s->flags & SEC_CODE) != 0)
5102 p_flags |= PF_X;
5103
5104 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5105 m = bfd_zalloc (abfd, amt);
5106 if (m == NULL)
5107 goto error_return;
5108 m->next = NULL;
5109 m->p_type = (PT_GNU_MBIND_LO
5110 + elf_section_data (s)->this_hdr.sh_info);
5111 m->count = 1;
5112 m->p_flags_valid = 1;
5113 m->sections[0] = s;
5114 m->p_flags = p_flags;
5115
5116 *pm = m;
5117 pm = &m->next;
5118 }
5119
5120 s = bfd_get_section_by_name (abfd,
5121 NOTE_GNU_PROPERTY_SECTION_NAME);
5122 if (s != NULL && s->size != 0)
5123 {
5124 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5125 m = bfd_zalloc (abfd, amt);
5126 if (m == NULL)
5127 goto error_return;
5128 m->next = NULL;
5129 m->p_type = PT_GNU_PROPERTY;
5130 m->count = 1;
5131 m->p_flags_valid = 1;
5132 m->sections[0] = s;
5133 m->p_flags = PF_R;
5134 *pm = m;
5135 pm = &m->next;
5136 }
5137
5138 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5139 segment. */
5140 eh_frame_hdr = elf_eh_frame_hdr (abfd);
5141 if (eh_frame_hdr != NULL
5142 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5143 {
5144 amt = sizeof (struct elf_segment_map);
5145 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5146 if (m == NULL)
5147 goto error_return;
5148 m->next = NULL;
5149 m->p_type = PT_GNU_EH_FRAME;
5150 m->count = 1;
5151 m->sections[0] = eh_frame_hdr->output_section;
5152
5153 *pm = m;
5154 pm = &m->next;
5155 }
5156
5157 if (elf_stack_flags (abfd))
5158 {
5159 amt = sizeof (struct elf_segment_map);
5160 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5161 if (m == NULL)
5162 goto error_return;
5163 m->next = NULL;
5164 m->p_type = PT_GNU_STACK;
5165 m->p_flags = elf_stack_flags (abfd);
5166 m->p_align = bed->stack_align;
5167 m->p_flags_valid = 1;
5168 m->p_align_valid = m->p_align != 0;
5169 if (info->stacksize > 0)
5170 {
5171 m->p_size = info->stacksize;
5172 m->p_size_valid = 1;
5173 }
5174
5175 *pm = m;
5176 pm = &m->next;
5177 }
5178
5179 if (info != NULL && info->relro)
5180 {
5181 for (m = mfirst; m != NULL; m = m->next)
5182 {
5183 if (m->p_type == PT_LOAD
5184 && m->count != 0
5185 && m->sections[0]->vma >= info->relro_start
5186 && m->sections[0]->vma < info->relro_end)
5187 {
5188 i = m->count;
5189 while (--i != (unsigned) -1)
5190 {
5191 if (m->sections[i]->size > 0
5192 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5193 == (SEC_LOAD | SEC_HAS_CONTENTS))
5194 break;
5195 }
5196
5197 if (i != (unsigned) -1)
5198 break;
5199 }
5200 }
5201
5202 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5203 if (m != NULL)
5204 {
5205 amt = sizeof (struct elf_segment_map);
5206 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5207 if (m == NULL)
5208 goto error_return;
5209 m->next = NULL;
5210 m->p_type = PT_GNU_RELRO;
5211 *pm = m;
5212 pm = &m->next;
5213 }
5214 }
5215
5216 free (sections);
5217 elf_seg_map (abfd) = mfirst;
5218 }
5219
5220 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5221 return false;
5222
5223 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5224 ++count;
5225 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5226
5227 return true;
5228
5229 error_return:
5230 free (sections);
5231 return false;
5232 }
5233
5234 /* Sort sections by address. */
5235
5236 static int
5237 elf_sort_sections (const void *arg1, const void *arg2)
5238 {
5239 const asection *sec1 = *(const asection **) arg1;
5240 const asection *sec2 = *(const asection **) arg2;
5241 bfd_size_type size1, size2;
5242
5243 /* Sort by LMA first, since this is the address used to
5244 place the section into a segment. */
5245 if (sec1->lma < sec2->lma)
5246 return -1;
5247 else if (sec1->lma > sec2->lma)
5248 return 1;
5249
5250 /* Then sort by VMA. Normally the LMA and the VMA will be
5251 the same, and this will do nothing. */
5252 if (sec1->vma < sec2->vma)
5253 return -1;
5254 else if (sec1->vma > sec2->vma)
5255 return 1;
5256
5257 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5258
5259 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5260 && (x)->size != 0)
5261
5262 if (TOEND (sec1))
5263 {
5264 if (!TOEND (sec2))
5265 return 1;
5266 }
5267 else if (TOEND (sec2))
5268 return -1;
5269
5270 #undef TOEND
5271
5272 /* Sort by size, to put zero sized sections
5273 before others at the same address. */
5274
5275 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5276 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5277
5278 if (size1 < size2)
5279 return -1;
5280 if (size1 > size2)
5281 return 1;
5282
5283 return sec1->target_index - sec2->target_index;
5284 }
5285
5286 /* This qsort comparison functions sorts PT_LOAD segments first and
5287 by p_paddr, for assign_file_positions_for_load_sections. */
5288
5289 static int
5290 elf_sort_segments (const void *arg1, const void *arg2)
5291 {
5292 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5293 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5294
5295 if (m1->p_type != m2->p_type)
5296 {
5297 if (m1->p_type == PT_NULL)
5298 return 1;
5299 if (m2->p_type == PT_NULL)
5300 return -1;
5301 return m1->p_type < m2->p_type ? -1 : 1;
5302 }
5303 if (m1->includes_filehdr != m2->includes_filehdr)
5304 return m1->includes_filehdr ? -1 : 1;
5305 if (m1->no_sort_lma != m2->no_sort_lma)
5306 return m1->no_sort_lma ? -1 : 1;
5307 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5308 {
5309 bfd_vma lma1, lma2; /* Octets. */
5310 lma1 = 0;
5311 if (m1->p_paddr_valid)
5312 lma1 = m1->p_paddr;
5313 else if (m1->count != 0)
5314 {
5315 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5316 m1->sections[0]);
5317 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5318 }
5319 lma2 = 0;
5320 if (m2->p_paddr_valid)
5321 lma2 = m2->p_paddr;
5322 else if (m2->count != 0)
5323 {
5324 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5325 m2->sections[0]);
5326 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5327 }
5328 if (lma1 != lma2)
5329 return lma1 < lma2 ? -1 : 1;
5330 }
5331 if (m1->idx != m2->idx)
5332 return m1->idx < m2->idx ? -1 : 1;
5333 return 0;
5334 }
5335
5336 /* Ian Lance Taylor writes:
5337
5338 We shouldn't be using % with a negative signed number. That's just
5339 not good. We have to make sure either that the number is not
5340 negative, or that the number has an unsigned type. When the types
5341 are all the same size they wind up as unsigned. When file_ptr is a
5342 larger signed type, the arithmetic winds up as signed long long,
5343 which is wrong.
5344
5345 What we're trying to say here is something like ``increase OFF by
5346 the least amount that will cause it to be equal to the VMA modulo
5347 the page size.'' */
5348 /* In other words, something like:
5349
5350 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5351 off_offset = off % bed->maxpagesize;
5352 if (vma_offset < off_offset)
5353 adjustment = vma_offset + bed->maxpagesize - off_offset;
5354 else
5355 adjustment = vma_offset - off_offset;
5356
5357 which can be collapsed into the expression below. */
5358
5359 static file_ptr
5360 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5361 {
5362 /* PR binutils/16199: Handle an alignment of zero. */
5363 if (maxpagesize == 0)
5364 maxpagesize = 1;
5365 return ((vma - off) % maxpagesize);
5366 }
5367
5368 static void
5369 print_segment_map (const struct elf_segment_map *m)
5370 {
5371 unsigned int j;
5372 const char *pt = get_segment_type (m->p_type);
5373 char buf[32];
5374
5375 if (pt == NULL)
5376 {
5377 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5378 sprintf (buf, "LOPROC+%7.7x",
5379 (unsigned int) (m->p_type - PT_LOPROC));
5380 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5381 sprintf (buf, "LOOS+%7.7x",
5382 (unsigned int) (m->p_type - PT_LOOS));
5383 else
5384 snprintf (buf, sizeof (buf), "%8.8x",
5385 (unsigned int) m->p_type);
5386 pt = buf;
5387 }
5388 fflush (stdout);
5389 fprintf (stderr, "%s:", pt);
5390 for (j = 0; j < m->count; j++)
5391 fprintf (stderr, " %s", m->sections [j]->name);
5392 putc ('\n',stderr);
5393 fflush (stderr);
5394 }
5395
5396 static bool
5397 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5398 {
5399 void *buf;
5400 bool ret;
5401
5402 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5403 return false;
5404 buf = bfd_zmalloc (len);
5405 if (buf == NULL)
5406 return false;
5407 ret = bfd_bwrite (buf, len, abfd) == len;
5408 free (buf);
5409 return ret;
5410 }
5411
5412 /* Assign file positions to the sections based on the mapping from
5413 sections to segments. This function also sets up some fields in
5414 the file header. */
5415
5416 static bool
5417 assign_file_positions_for_load_sections (bfd *abfd,
5418 struct bfd_link_info *link_info)
5419 {
5420 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5421 struct elf_segment_map *m;
5422 struct elf_segment_map *phdr_load_seg;
5423 Elf_Internal_Phdr *phdrs;
5424 Elf_Internal_Phdr *p;
5425 file_ptr off; /* Octets. */
5426 bfd_size_type maxpagesize;
5427 unsigned int alloc, actual;
5428 unsigned int i, j;
5429 struct elf_segment_map **sorted_seg_map;
5430 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5431
5432 if (link_info == NULL
5433 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5434 return false;
5435
5436 alloc = 0;
5437 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5438 m->idx = alloc++;
5439
5440 if (alloc)
5441 {
5442 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5443 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5444 }
5445 else
5446 {
5447 /* PR binutils/12467. */
5448 elf_elfheader (abfd)->e_phoff = 0;
5449 elf_elfheader (abfd)->e_phentsize = 0;
5450 }
5451
5452 elf_elfheader (abfd)->e_phnum = alloc;
5453
5454 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5455 {
5456 actual = alloc;
5457 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5458 }
5459 else
5460 {
5461 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5462 BFD_ASSERT (elf_program_header_size (abfd)
5463 == actual * bed->s->sizeof_phdr);
5464 BFD_ASSERT (actual >= alloc);
5465 }
5466
5467 if (alloc == 0)
5468 {
5469 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5470 return true;
5471 }
5472
5473 /* We're writing the size in elf_program_header_size (abfd),
5474 see assign_file_positions_except_relocs, so make sure we have
5475 that amount allocated, with trailing space cleared.
5476 The variable alloc contains the computed need, while
5477 elf_program_header_size (abfd) contains the size used for the
5478 layout.
5479 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5480 where the layout is forced to according to a larger size in the
5481 last iterations for the testcase ld-elf/header. */
5482 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5483 + alloc * sizeof (*sorted_seg_map)));
5484 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5485 elf_tdata (abfd)->phdr = phdrs;
5486 if (phdrs == NULL)
5487 return false;
5488
5489 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5490 {
5491 sorted_seg_map[j] = m;
5492 /* If elf_segment_map is not from map_sections_to_segments, the
5493 sections may not be correctly ordered. NOTE: sorting should
5494 not be done to the PT_NOTE section of a corefile, which may
5495 contain several pseudo-sections artificially created by bfd.
5496 Sorting these pseudo-sections breaks things badly. */
5497 if (m->count > 1
5498 && !(elf_elfheader (abfd)->e_type == ET_CORE
5499 && m->p_type == PT_NOTE))
5500 {
5501 for (i = 0; i < m->count; i++)
5502 m->sections[i]->target_index = i;
5503 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5504 elf_sort_sections);
5505 }
5506 }
5507 if (alloc > 1)
5508 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5509 elf_sort_segments);
5510
5511 maxpagesize = 1;
5512 if ((abfd->flags & D_PAGED) != 0)
5513 {
5514 if (link_info != NULL)
5515 maxpagesize = link_info->maxpagesize;
5516 else
5517 maxpagesize = bed->maxpagesize;
5518 }
5519
5520 /* Sections must map to file offsets past the ELF file header. */
5521 off = bed->s->sizeof_ehdr;
5522 /* And if one of the PT_LOAD headers doesn't include the program
5523 headers then we'll be mapping program headers in the usual
5524 position after the ELF file header. */
5525 phdr_load_seg = NULL;
5526 for (j = 0; j < alloc; j++)
5527 {
5528 m = sorted_seg_map[j];
5529 if (m->p_type != PT_LOAD)
5530 break;
5531 if (m->includes_phdrs)
5532 {
5533 phdr_load_seg = m;
5534 break;
5535 }
5536 }
5537 if (phdr_load_seg == NULL)
5538 off += actual * bed->s->sizeof_phdr;
5539
5540 for (j = 0; j < alloc; j++)
5541 {
5542 asection **secpp;
5543 bfd_vma off_adjust; /* Octets. */
5544 bool no_contents;
5545
5546 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5547 number of sections with contents contributing to both p_filesz
5548 and p_memsz, followed by a number of sections with no contents
5549 that just contribute to p_memsz. In this loop, OFF tracks next
5550 available file offset for PT_LOAD and PT_NOTE segments. */
5551 m = sorted_seg_map[j];
5552 p = phdrs + m->idx;
5553 p->p_type = m->p_type;
5554 p->p_flags = m->p_flags;
5555
5556 if (m->count == 0)
5557 p->p_vaddr = m->p_vaddr_offset * opb;
5558 else
5559 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5560
5561 if (m->p_paddr_valid)
5562 p->p_paddr = m->p_paddr;
5563 else if (m->count == 0)
5564 p->p_paddr = 0;
5565 else
5566 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5567
5568 if (p->p_type == PT_LOAD
5569 && (abfd->flags & D_PAGED) != 0)
5570 {
5571 /* p_align in demand paged PT_LOAD segments effectively stores
5572 the maximum page size. When copying an executable with
5573 objcopy, we set m->p_align from the input file. Use this
5574 value for maxpagesize rather than bed->maxpagesize, which
5575 may be different. Note that we use maxpagesize for PT_TLS
5576 segment alignment later in this function, so we are relying
5577 on at least one PT_LOAD segment appearing before a PT_TLS
5578 segment. */
5579 if (m->p_align_valid)
5580 maxpagesize = m->p_align;
5581
5582 p->p_align = maxpagesize;
5583 }
5584 else if (m->p_align_valid)
5585 p->p_align = m->p_align;
5586 else if (m->count == 0)
5587 p->p_align = 1 << bed->s->log_file_align;
5588
5589 if (m == phdr_load_seg)
5590 {
5591 if (!m->includes_filehdr)
5592 p->p_offset = off;
5593 off += actual * bed->s->sizeof_phdr;
5594 }
5595
5596 no_contents = false;
5597 off_adjust = 0;
5598 if (p->p_type == PT_LOAD
5599 && m->count > 0)
5600 {
5601 bfd_size_type align; /* Bytes. */
5602 unsigned int align_power = 0;
5603
5604 if (m->p_align_valid)
5605 align = p->p_align;
5606 else
5607 {
5608 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5609 {
5610 unsigned int secalign;
5611
5612 secalign = bfd_section_alignment (*secpp);
5613 if (secalign > align_power)
5614 align_power = secalign;
5615 }
5616 align = (bfd_size_type) 1 << align_power;
5617 if (align < maxpagesize)
5618 align = maxpagesize;
5619 }
5620
5621 for (i = 0; i < m->count; i++)
5622 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5623 /* If we aren't making room for this section, then
5624 it must be SHT_NOBITS regardless of what we've
5625 set via struct bfd_elf_special_section. */
5626 elf_section_type (m->sections[i]) = SHT_NOBITS;
5627
5628 /* Find out whether this segment contains any loadable
5629 sections. */
5630 no_contents = true;
5631 for (i = 0; i < m->count; i++)
5632 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5633 {
5634 no_contents = false;
5635 break;
5636 }
5637
5638 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5639
5640 /* Broken hardware and/or kernel require that files do not
5641 map the same page with different permissions on some hppa
5642 processors. */
5643 if (j != 0
5644 && (abfd->flags & D_PAGED) != 0
5645 && bed->no_page_alias
5646 && (off & (maxpagesize - 1)) != 0
5647 && ((off & -maxpagesize)
5648 == ((off + off_adjust) & -maxpagesize)))
5649 off_adjust += maxpagesize;
5650 off += off_adjust;
5651 if (no_contents)
5652 {
5653 /* We shouldn't need to align the segment on disk since
5654 the segment doesn't need file space, but the gABI
5655 arguably requires the alignment and glibc ld.so
5656 checks it. So to comply with the alignment
5657 requirement but not waste file space, we adjust
5658 p_offset for just this segment. (OFF_ADJUST is
5659 subtracted from OFF later.) This may put p_offset
5660 past the end of file, but that shouldn't matter. */
5661 }
5662 else
5663 off_adjust = 0;
5664 }
5665 /* Make sure the .dynamic section is the first section in the
5666 PT_DYNAMIC segment. */
5667 else if (p->p_type == PT_DYNAMIC
5668 && m->count > 1
5669 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5670 {
5671 _bfd_error_handler
5672 (_("%pB: The first section in the PT_DYNAMIC segment"
5673 " is not the .dynamic section"),
5674 abfd);
5675 bfd_set_error (bfd_error_bad_value);
5676 return false;
5677 }
5678 /* Set the note section type to SHT_NOTE. */
5679 else if (p->p_type == PT_NOTE)
5680 for (i = 0; i < m->count; i++)
5681 elf_section_type (m->sections[i]) = SHT_NOTE;
5682
5683 if (m->includes_filehdr)
5684 {
5685 if (!m->p_flags_valid)
5686 p->p_flags |= PF_R;
5687 p->p_filesz = bed->s->sizeof_ehdr;
5688 p->p_memsz = bed->s->sizeof_ehdr;
5689 if (p->p_type == PT_LOAD)
5690 {
5691 if (m->count > 0)
5692 {
5693 if (p->p_vaddr < (bfd_vma) off
5694 || (!m->p_paddr_valid
5695 && p->p_paddr < (bfd_vma) off))
5696 {
5697 _bfd_error_handler
5698 (_("%pB: not enough room for program headers,"
5699 " try linking with -N"),
5700 abfd);
5701 bfd_set_error (bfd_error_bad_value);
5702 return false;
5703 }
5704 p->p_vaddr -= off;
5705 if (!m->p_paddr_valid)
5706 p->p_paddr -= off;
5707 }
5708 }
5709 else if (sorted_seg_map[0]->includes_filehdr)
5710 {
5711 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5712 p->p_vaddr = filehdr->p_vaddr;
5713 if (!m->p_paddr_valid)
5714 p->p_paddr = filehdr->p_paddr;
5715 }
5716 }
5717
5718 if (m->includes_phdrs)
5719 {
5720 if (!m->p_flags_valid)
5721 p->p_flags |= PF_R;
5722 p->p_filesz += actual * bed->s->sizeof_phdr;
5723 p->p_memsz += actual * bed->s->sizeof_phdr;
5724 if (!m->includes_filehdr)
5725 {
5726 if (p->p_type == PT_LOAD)
5727 {
5728 elf_elfheader (abfd)->e_phoff = p->p_offset;
5729 if (m->count > 0)
5730 {
5731 p->p_vaddr -= off - p->p_offset;
5732 if (!m->p_paddr_valid)
5733 p->p_paddr -= off - p->p_offset;
5734 }
5735 }
5736 else if (phdr_load_seg != NULL)
5737 {
5738 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5739 bfd_vma phdr_off = 0; /* Octets. */
5740 if (phdr_load_seg->includes_filehdr)
5741 phdr_off = bed->s->sizeof_ehdr;
5742 p->p_vaddr = phdr->p_vaddr + phdr_off;
5743 if (!m->p_paddr_valid)
5744 p->p_paddr = phdr->p_paddr + phdr_off;
5745 p->p_offset = phdr->p_offset + phdr_off;
5746 }
5747 else
5748 p->p_offset = bed->s->sizeof_ehdr;
5749 }
5750 }
5751
5752 if (p->p_type == PT_LOAD
5753 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5754 {
5755 if (!m->includes_filehdr && !m->includes_phdrs)
5756 {
5757 p->p_offset = off;
5758 if (no_contents)
5759 {
5760 /* Put meaningless p_offset for PT_LOAD segments
5761 without file contents somewhere within the first
5762 page, in an attempt to not point past EOF. */
5763 bfd_size_type align = maxpagesize;
5764 if (align < p->p_align)
5765 align = p->p_align;
5766 if (align < 1)
5767 align = 1;
5768 p->p_offset = off % align;
5769 }
5770 }
5771 else
5772 {
5773 file_ptr adjust; /* Octets. */
5774
5775 adjust = off - (p->p_offset + p->p_filesz);
5776 if (!no_contents)
5777 p->p_filesz += adjust;
5778 p->p_memsz += adjust;
5779 }
5780 }
5781
5782 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5783 maps. Set filepos for sections in PT_LOAD segments, and in
5784 core files, for sections in PT_NOTE segments.
5785 assign_file_positions_for_non_load_sections will set filepos
5786 for other sections and update p_filesz for other segments. */
5787 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5788 {
5789 asection *sec;
5790 bfd_size_type align;
5791 Elf_Internal_Shdr *this_hdr;
5792
5793 sec = *secpp;
5794 this_hdr = &elf_section_data (sec)->this_hdr;
5795 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5796
5797 if ((p->p_type == PT_LOAD
5798 || p->p_type == PT_TLS)
5799 && (this_hdr->sh_type != SHT_NOBITS
5800 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5801 && ((this_hdr->sh_flags & SHF_TLS) == 0
5802 || p->p_type == PT_TLS))))
5803 {
5804 bfd_vma p_start = p->p_paddr; /* Octets. */
5805 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5806 bfd_vma s_start = sec->lma * opb; /* Octets. */
5807 bfd_vma adjust = s_start - p_end; /* Octets. */
5808
5809 if (adjust != 0
5810 && (s_start < p_end
5811 || p_end < p_start))
5812 {
5813 _bfd_error_handler
5814 /* xgettext:c-format */
5815 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5816 abfd, sec, (uint64_t) s_start / opb,
5817 (uint64_t) p_end / opb);
5818 adjust = 0;
5819 sec->lma = p_end / opb;
5820 }
5821 p->p_memsz += adjust;
5822
5823 if (p->p_type == PT_LOAD)
5824 {
5825 if (this_hdr->sh_type != SHT_NOBITS)
5826 {
5827 off_adjust = 0;
5828 if (p->p_filesz + adjust < p->p_memsz)
5829 {
5830 /* We have a PROGBITS section following NOBITS ones.
5831 Allocate file space for the NOBITS section(s) and
5832 zero it. */
5833 adjust = p->p_memsz - p->p_filesz;
5834 if (!write_zeros (abfd, off, adjust))
5835 return false;
5836 }
5837 }
5838 /* We only adjust sh_offset in SHT_NOBITS sections
5839 as would seem proper for their address when the
5840 section is first in the segment. sh_offset
5841 doesn't really have any significance for
5842 SHT_NOBITS anyway, apart from a notional position
5843 relative to other sections. Historically we
5844 didn't bother with adjusting sh_offset and some
5845 programs depend on it not being adjusted. See
5846 pr12921 and pr25662. */
5847 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5848 {
5849 off += adjust;
5850 if (this_hdr->sh_type == SHT_NOBITS)
5851 off_adjust += adjust;
5852 }
5853 }
5854 if (this_hdr->sh_type != SHT_NOBITS)
5855 p->p_filesz += adjust;
5856 }
5857
5858 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5859 {
5860 /* The section at i == 0 is the one that actually contains
5861 everything. */
5862 if (i == 0)
5863 {
5864 this_hdr->sh_offset = sec->filepos = off;
5865 off += this_hdr->sh_size;
5866 p->p_filesz = this_hdr->sh_size;
5867 p->p_memsz = 0;
5868 p->p_align = 1;
5869 }
5870 else
5871 {
5872 /* The rest are fake sections that shouldn't be written. */
5873 sec->filepos = 0;
5874 sec->size = 0;
5875 sec->flags = 0;
5876 continue;
5877 }
5878 }
5879 else
5880 {
5881 if (p->p_type == PT_LOAD)
5882 {
5883 this_hdr->sh_offset = sec->filepos = off;
5884 if (this_hdr->sh_type != SHT_NOBITS)
5885 off += this_hdr->sh_size;
5886 }
5887 else if (this_hdr->sh_type == SHT_NOBITS
5888 && (this_hdr->sh_flags & SHF_TLS) != 0
5889 && this_hdr->sh_offset == 0)
5890 {
5891 /* This is a .tbss section that didn't get a PT_LOAD.
5892 (See _bfd_elf_map_sections_to_segments "Create a
5893 final PT_LOAD".) Set sh_offset to the value it
5894 would have if we had created a zero p_filesz and
5895 p_memsz PT_LOAD header for the section. This
5896 also makes the PT_TLS header have the same
5897 p_offset value. */
5898 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5899 off, align);
5900 this_hdr->sh_offset = sec->filepos = off + adjust;
5901 }
5902
5903 if (this_hdr->sh_type != SHT_NOBITS)
5904 {
5905 p->p_filesz += this_hdr->sh_size;
5906 /* A load section without SHF_ALLOC is something like
5907 a note section in a PT_NOTE segment. These take
5908 file space but are not loaded into memory. */
5909 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5910 p->p_memsz += this_hdr->sh_size;
5911 }
5912 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5913 {
5914 if (p->p_type == PT_TLS)
5915 p->p_memsz += this_hdr->sh_size;
5916
5917 /* .tbss is special. It doesn't contribute to p_memsz of
5918 normal segments. */
5919 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5920 p->p_memsz += this_hdr->sh_size;
5921 }
5922
5923 if (align > p->p_align
5924 && !m->p_align_valid
5925 && (p->p_type != PT_LOAD
5926 || (abfd->flags & D_PAGED) == 0))
5927 p->p_align = align;
5928 }
5929
5930 if (!m->p_flags_valid)
5931 {
5932 p->p_flags |= PF_R;
5933 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5934 p->p_flags |= PF_X;
5935 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5936 p->p_flags |= PF_W;
5937 }
5938 }
5939
5940 off -= off_adjust;
5941
5942 /* PR ld/20815 - Check that the program header segment, if
5943 present, will be loaded into memory. */
5944 if (p->p_type == PT_PHDR
5945 && phdr_load_seg == NULL
5946 && !(bed->elf_backend_allow_non_load_phdr != NULL
5947 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5948 {
5949 /* The fix for this error is usually to edit the linker script being
5950 used and set up the program headers manually. Either that or
5951 leave room for the headers at the start of the SECTIONS. */
5952 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5953 " by LOAD segment"),
5954 abfd);
5955 if (link_info == NULL)
5956 return false;
5957 /* Arrange for the linker to exit with an error, deleting
5958 the output file unless --noinhibit-exec is given. */
5959 link_info->callbacks->info ("%X");
5960 }
5961
5962 /* Check that all sections are in a PT_LOAD segment.
5963 Don't check funky gdb generated core files. */
5964 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5965 {
5966 bool check_vma = true;
5967
5968 for (i = 1; i < m->count; i++)
5969 if (m->sections[i]->vma == m->sections[i - 1]->vma
5970 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5971 ->this_hdr), p) != 0
5972 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5973 ->this_hdr), p) != 0)
5974 {
5975 /* Looks like we have overlays packed into the segment. */
5976 check_vma = false;
5977 break;
5978 }
5979
5980 for (i = 0; i < m->count; i++)
5981 {
5982 Elf_Internal_Shdr *this_hdr;
5983 asection *sec;
5984
5985 sec = m->sections[i];
5986 this_hdr = &(elf_section_data(sec)->this_hdr);
5987 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5988 && !ELF_TBSS_SPECIAL (this_hdr, p))
5989 {
5990 _bfd_error_handler
5991 /* xgettext:c-format */
5992 (_("%pB: section `%pA' can't be allocated in segment %d"),
5993 abfd, sec, j);
5994 print_segment_map (m);
5995 }
5996 }
5997 }
5998 }
5999
6000 elf_next_file_pos (abfd) = off;
6001
6002 if (link_info != NULL
6003 && phdr_load_seg != NULL
6004 && phdr_load_seg->includes_filehdr)
6005 {
6006 /* There is a segment that contains both the file headers and the
6007 program headers, so provide a symbol __ehdr_start pointing there.
6008 A program can use this to examine itself robustly. */
6009
6010 struct elf_link_hash_entry *hash
6011 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6012 false, false, true);
6013 /* If the symbol was referenced and not defined, define it. */
6014 if (hash != NULL
6015 && (hash->root.type == bfd_link_hash_new
6016 || hash->root.type == bfd_link_hash_undefined
6017 || hash->root.type == bfd_link_hash_undefweak
6018 || hash->root.type == bfd_link_hash_common))
6019 {
6020 asection *s = NULL;
6021 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6022
6023 if (phdr_load_seg->count != 0)
6024 /* The segment contains sections, so use the first one. */
6025 s = phdr_load_seg->sections[0];
6026 else
6027 /* Use the first (i.e. lowest-addressed) section in any segment. */
6028 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6029 if (m->p_type == PT_LOAD && m->count != 0)
6030 {
6031 s = m->sections[0];
6032 break;
6033 }
6034
6035 if (s != NULL)
6036 {
6037 hash->root.u.def.value = filehdr_vaddr - s->vma;
6038 hash->root.u.def.section = s;
6039 }
6040 else
6041 {
6042 hash->root.u.def.value = filehdr_vaddr;
6043 hash->root.u.def.section = bfd_abs_section_ptr;
6044 }
6045
6046 hash->root.type = bfd_link_hash_defined;
6047 hash->def_regular = 1;
6048 hash->non_elf = 0;
6049 }
6050 }
6051
6052 return true;
6053 }
6054
6055 /* Determine if a bfd is a debuginfo file. Unfortunately there
6056 is no defined method for detecting such files, so we have to
6057 use heuristics instead. */
6058
6059 bool
6060 is_debuginfo_file (bfd *abfd)
6061 {
6062 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6063 return false;
6064
6065 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6066 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6067 Elf_Internal_Shdr **headerp;
6068
6069 for (headerp = start_headers; headerp < end_headers; headerp ++)
6070 {
6071 Elf_Internal_Shdr *header = * headerp;
6072
6073 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6074 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6075 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6076 && header->sh_type != SHT_NOBITS
6077 && header->sh_type != SHT_NOTE)
6078 return false;
6079 }
6080
6081 return true;
6082 }
6083
6084 /* Assign file positions for the other sections, except for compressed debugging
6085 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6086
6087 static bool
6088 assign_file_positions_for_non_load_sections (bfd *abfd,
6089 struct bfd_link_info *link_info)
6090 {
6091 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6092 Elf_Internal_Shdr **i_shdrpp;
6093 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6094 Elf_Internal_Phdr *phdrs;
6095 Elf_Internal_Phdr *p;
6096 struct elf_segment_map *m;
6097 file_ptr off;
6098 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6099 bfd_vma maxpagesize;
6100
6101 if (link_info != NULL)
6102 maxpagesize = link_info->maxpagesize;
6103 else
6104 maxpagesize = bed->maxpagesize;
6105 i_shdrpp = elf_elfsections (abfd);
6106 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6107 off = elf_next_file_pos (abfd);
6108 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6109 {
6110 Elf_Internal_Shdr *hdr;
6111
6112 hdr = *hdrpp;
6113 if (hdr->bfd_section != NULL
6114 && (hdr->bfd_section->filepos != 0
6115 || (hdr->sh_type == SHT_NOBITS
6116 && hdr->contents == NULL)))
6117 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6118 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6119 {
6120 if (hdr->sh_size != 0
6121 /* PR 24717 - debuginfo files are known to be not strictly
6122 compliant with the ELF standard. In particular they often
6123 have .note.gnu.property sections that are outside of any
6124 loadable segment. This is not a problem for such files,
6125 so do not warn about them. */
6126 && ! is_debuginfo_file (abfd))
6127 _bfd_error_handler
6128 /* xgettext:c-format */
6129 (_("%pB: warning: allocated section `%s' not in segment"),
6130 abfd,
6131 (hdr->bfd_section == NULL
6132 ? "*unknown*"
6133 : hdr->bfd_section->name));
6134 /* We don't need to page align empty sections. */
6135 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6136 off += vma_page_aligned_bias (hdr->sh_addr, off,
6137 maxpagesize);
6138 else
6139 off += vma_page_aligned_bias (hdr->sh_addr, off,
6140 hdr->sh_addralign);
6141 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6142 false);
6143 }
6144 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6145 && hdr->bfd_section == NULL)
6146 /* We don't know the offset of these sections yet: their size has
6147 not been decided. */
6148 || (hdr->bfd_section != NULL
6149 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6150 || (bfd_section_is_ctf (hdr->bfd_section)
6151 && abfd->is_linker_output)))
6152 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6153 || (elf_symtab_shndx_list (abfd) != NULL
6154 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6155 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6156 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6157 hdr->sh_offset = -1;
6158 else
6159 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6160 }
6161 elf_next_file_pos (abfd) = off;
6162
6163 /* Now that we have set the section file positions, we can set up
6164 the file positions for the non PT_LOAD segments. */
6165 phdrs = elf_tdata (abfd)->phdr;
6166 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6167 {
6168 if (p->p_type == PT_GNU_RELRO)
6169 {
6170 bfd_vma start, end; /* Bytes. */
6171 bool ok;
6172
6173 if (link_info != NULL)
6174 {
6175 /* During linking the range of the RELRO segment is passed
6176 in link_info. Note that there may be padding between
6177 relro_start and the first RELRO section. */
6178 start = link_info->relro_start;
6179 end = link_info->relro_end;
6180 }
6181 else if (m->count != 0)
6182 {
6183 if (!m->p_size_valid)
6184 abort ();
6185 start = m->sections[0]->vma;
6186 end = start + m->p_size / opb;
6187 }
6188 else
6189 {
6190 start = 0;
6191 end = 0;
6192 }
6193
6194 ok = false;
6195 if (start < end)
6196 {
6197 struct elf_segment_map *lm;
6198 const Elf_Internal_Phdr *lp;
6199 unsigned int i;
6200
6201 /* Find a LOAD segment containing a section in the RELRO
6202 segment. */
6203 for (lm = elf_seg_map (abfd), lp = phdrs;
6204 lm != NULL;
6205 lm = lm->next, lp++)
6206 {
6207 if (lp->p_type == PT_LOAD
6208 && lm->count != 0
6209 && (lm->sections[lm->count - 1]->vma
6210 + (!IS_TBSS (lm->sections[lm->count - 1])
6211 ? lm->sections[lm->count - 1]->size / opb
6212 : 0)) > start
6213 && lm->sections[0]->vma < end)
6214 break;
6215 }
6216
6217 if (lm != NULL)
6218 {
6219 /* Find the section starting the RELRO segment. */
6220 for (i = 0; i < lm->count; i++)
6221 {
6222 asection *s = lm->sections[i];
6223 if (s->vma >= start
6224 && s->vma < end
6225 && s->size != 0)
6226 break;
6227 }
6228
6229 if (i < lm->count)
6230 {
6231 p->p_vaddr = lm->sections[i]->vma * opb;
6232 p->p_paddr = lm->sections[i]->lma * opb;
6233 p->p_offset = lm->sections[i]->filepos;
6234 p->p_memsz = end * opb - p->p_vaddr;
6235 p->p_filesz = p->p_memsz;
6236
6237 /* The RELRO segment typically ends a few bytes
6238 into .got.plt but other layouts are possible.
6239 In cases where the end does not match any
6240 loaded section (for instance is in file
6241 padding), trim p_filesz back to correspond to
6242 the end of loaded section contents. */
6243 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6244 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6245
6246 /* Preserve the alignment and flags if they are
6247 valid. The gold linker generates RW/4 for
6248 the PT_GNU_RELRO section. It is better for
6249 objcopy/strip to honor these attributes
6250 otherwise gdb will choke when using separate
6251 debug files. */
6252 if (!m->p_align_valid)
6253 p->p_align = 1;
6254 if (!m->p_flags_valid)
6255 p->p_flags = PF_R;
6256 ok = true;
6257 }
6258 }
6259 }
6260 if (link_info != NULL)
6261 BFD_ASSERT (ok);
6262 if (!ok)
6263 memset (p, 0, sizeof *p);
6264 }
6265 else if (p->p_type == PT_GNU_STACK)
6266 {
6267 if (m->p_size_valid)
6268 p->p_memsz = m->p_size;
6269 }
6270 else if (m->count != 0)
6271 {
6272 unsigned int i;
6273
6274 if (p->p_type != PT_LOAD
6275 && (p->p_type != PT_NOTE
6276 || bfd_get_format (abfd) != bfd_core))
6277 {
6278 /* A user specified segment layout may include a PHDR
6279 segment that overlaps with a LOAD segment... */
6280 if (p->p_type == PT_PHDR)
6281 {
6282 m->count = 0;
6283 continue;
6284 }
6285
6286 if (m->includes_filehdr || m->includes_phdrs)
6287 {
6288 /* PR 17512: file: 2195325e. */
6289 _bfd_error_handler
6290 (_("%pB: error: non-load segment %d includes file header "
6291 "and/or program header"),
6292 abfd, (int) (p - phdrs));
6293 return false;
6294 }
6295
6296 p->p_filesz = 0;
6297 p->p_offset = m->sections[0]->filepos;
6298 for (i = m->count; i-- != 0;)
6299 {
6300 asection *sect = m->sections[i];
6301 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6302 if (hdr->sh_type != SHT_NOBITS)
6303 {
6304 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6305 + hdr->sh_size);
6306 /* NB: p_memsz of the loadable PT_NOTE segment
6307 should be the same as p_filesz. */
6308 if (p->p_type == PT_NOTE
6309 && (hdr->sh_flags & SHF_ALLOC) != 0)
6310 p->p_memsz = p->p_filesz;
6311 break;
6312 }
6313 }
6314 }
6315 }
6316 }
6317
6318 return true;
6319 }
6320
6321 static elf_section_list *
6322 find_section_in_list (unsigned int i, elf_section_list * list)
6323 {
6324 for (;list != NULL; list = list->next)
6325 if (list->ndx == i)
6326 break;
6327 return list;
6328 }
6329
6330 /* Work out the file positions of all the sections. This is called by
6331 _bfd_elf_compute_section_file_positions. All the section sizes and
6332 VMAs must be known before this is called.
6333
6334 Reloc sections come in two flavours: Those processed specially as
6335 "side-channel" data attached to a section to which they apply, and those that
6336 bfd doesn't process as relocations. The latter sort are stored in a normal
6337 bfd section by bfd_section_from_shdr. We don't consider the former sort
6338 here, unless they form part of the loadable image. Reloc sections not
6339 assigned here (and compressed debugging sections and CTF sections which
6340 nothing else in the file can rely upon) will be handled later by
6341 assign_file_positions_for_relocs.
6342
6343 We also don't set the positions of the .symtab and .strtab here. */
6344
6345 static bool
6346 assign_file_positions_except_relocs (bfd *abfd,
6347 struct bfd_link_info *link_info)
6348 {
6349 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6350 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6352 unsigned int alloc;
6353
6354 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6355 && bfd_get_format (abfd) != bfd_core)
6356 {
6357 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6358 unsigned int num_sec = elf_numsections (abfd);
6359 Elf_Internal_Shdr **hdrpp;
6360 unsigned int i;
6361 file_ptr off;
6362
6363 /* Start after the ELF header. */
6364 off = i_ehdrp->e_ehsize;
6365
6366 /* We are not creating an executable, which means that we are
6367 not creating a program header, and that the actual order of
6368 the sections in the file is unimportant. */
6369 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6370 {
6371 Elf_Internal_Shdr *hdr;
6372
6373 hdr = *hdrpp;
6374 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6375 && hdr->bfd_section == NULL)
6376 /* Do not assign offsets for these sections yet: we don't know
6377 their sizes. */
6378 || (hdr->bfd_section != NULL
6379 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6380 || (bfd_section_is_ctf (hdr->bfd_section)
6381 && abfd->is_linker_output)))
6382 || i == elf_onesymtab (abfd)
6383 || (elf_symtab_shndx_list (abfd) != NULL
6384 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6385 || i == elf_strtab_sec (abfd)
6386 || i == elf_shstrtab_sec (abfd))
6387 {
6388 hdr->sh_offset = -1;
6389 }
6390 else
6391 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6392 }
6393
6394 elf_next_file_pos (abfd) = off;
6395 elf_program_header_size (abfd) = 0;
6396 }
6397 else
6398 {
6399 /* Assign file positions for the loaded sections based on the
6400 assignment of sections to segments. */
6401 if (!assign_file_positions_for_load_sections (abfd, link_info))
6402 return false;
6403
6404 /* And for non-load sections. */
6405 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6406 return false;
6407 }
6408
6409 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6410 return false;
6411
6412 /* Write out the program headers. */
6413 alloc = i_ehdrp->e_phnum;
6414 if (alloc != 0)
6415 {
6416 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6417 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6418 return false;
6419 }
6420
6421 return true;
6422 }
6423
6424 bool
6425 _bfd_elf_init_file_header (bfd *abfd,
6426 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6427 {
6428 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6429 struct elf_strtab_hash *shstrtab;
6430 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6431
6432 i_ehdrp = elf_elfheader (abfd);
6433
6434 shstrtab = _bfd_elf_strtab_init ();
6435 if (shstrtab == NULL)
6436 return false;
6437
6438 elf_shstrtab (abfd) = shstrtab;
6439
6440 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6441 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6442 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6443 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6444
6445 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6446 i_ehdrp->e_ident[EI_DATA] =
6447 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6448 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6449
6450 if ((abfd->flags & DYNAMIC) != 0)
6451 i_ehdrp->e_type = ET_DYN;
6452 else if ((abfd->flags & EXEC_P) != 0)
6453 i_ehdrp->e_type = ET_EXEC;
6454 else if (bfd_get_format (abfd) == bfd_core)
6455 i_ehdrp->e_type = ET_CORE;
6456 else
6457 i_ehdrp->e_type = ET_REL;
6458
6459 switch (bfd_get_arch (abfd))
6460 {
6461 case bfd_arch_unknown:
6462 i_ehdrp->e_machine = EM_NONE;
6463 break;
6464
6465 /* There used to be a long list of cases here, each one setting
6466 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6467 in the corresponding bfd definition. To avoid duplication,
6468 the switch was removed. Machines that need special handling
6469 can generally do it in elf_backend_final_write_processing(),
6470 unless they need the information earlier than the final write.
6471 Such need can generally be supplied by replacing the tests for
6472 e_machine with the conditions used to determine it. */
6473 default:
6474 i_ehdrp->e_machine = bed->elf_machine_code;
6475 }
6476
6477 i_ehdrp->e_version = bed->s->ev_current;
6478 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6479
6480 /* No program header, for now. */
6481 i_ehdrp->e_phoff = 0;
6482 i_ehdrp->e_phentsize = 0;
6483 i_ehdrp->e_phnum = 0;
6484
6485 /* Each bfd section is section header entry. */
6486 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6487 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6488
6489 elf_tdata (abfd)->symtab_hdr.sh_name =
6490 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6491 elf_tdata (abfd)->strtab_hdr.sh_name =
6492 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6493 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6494 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6495 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6496 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6497 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6498 return false;
6499
6500 return true;
6501 }
6502
6503 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6504
6505 FIXME: We used to have code here to sort the PT_LOAD segments into
6506 ascending order, as per the ELF spec. But this breaks some programs,
6507 including the Linux kernel. But really either the spec should be
6508 changed or the programs updated. */
6509
6510 bool
6511 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6512 {
6513 if (link_info != NULL && bfd_link_pie (link_info))
6514 {
6515 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6516 unsigned int num_segments = i_ehdrp->e_phnum;
6517 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6518 Elf_Internal_Phdr *segment = tdata->phdr;
6519 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6520
6521 /* Find the lowest p_vaddr in PT_LOAD segments. */
6522 bfd_vma p_vaddr = (bfd_vma) -1;
6523 for (; segment < end_segment; segment++)
6524 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6525 p_vaddr = segment->p_vaddr;
6526
6527 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6528 segments is non-zero. */
6529 if (p_vaddr)
6530 i_ehdrp->e_type = ET_EXEC;
6531 }
6532 return true;
6533 }
6534
6535 /* Assign file positions for all the reloc sections which are not part
6536 of the loadable file image, and the file position of section headers. */
6537
6538 static bool
6539 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6540 {
6541 file_ptr off;
6542 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6543 Elf_Internal_Shdr *shdrp;
6544 Elf_Internal_Ehdr *i_ehdrp;
6545 const struct elf_backend_data *bed;
6546
6547 off = elf_next_file_pos (abfd);
6548
6549 shdrpp = elf_elfsections (abfd);
6550 end_shdrpp = shdrpp + elf_numsections (abfd);
6551 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6552 {
6553 shdrp = *shdrpp;
6554 if (shdrp->sh_offset == -1)
6555 {
6556 asection *sec = shdrp->bfd_section;
6557 bool is_rel = (shdrp->sh_type == SHT_REL
6558 || shdrp->sh_type == SHT_RELA);
6559 bool is_ctf = sec && bfd_section_is_ctf (sec);
6560 if (is_rel
6561 || is_ctf
6562 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6563 {
6564 if (!is_rel && !is_ctf)
6565 {
6566 const char *name = sec->name;
6567 struct bfd_elf_section_data *d;
6568
6569 /* Compress DWARF debug sections. */
6570 if (!bfd_compress_section (abfd, sec,
6571 shdrp->contents))
6572 return false;
6573
6574 if (sec->compress_status == COMPRESS_SECTION_DONE
6575 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6576 {
6577 /* If section is compressed with zlib-gnu, convert
6578 section name from .debug_* to .zdebug_*. */
6579 char *new_name
6580 = convert_debug_to_zdebug (abfd, name);
6581 if (new_name == NULL)
6582 return false;
6583 name = new_name;
6584 }
6585 /* Add section name to section name section. */
6586 if (shdrp->sh_name != (unsigned int) -1)
6587 abort ();
6588 shdrp->sh_name
6589 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6590 name, false);
6591 d = elf_section_data (sec);
6592
6593 /* Add reloc section name to section name section. */
6594 if (d->rel.hdr
6595 && !_bfd_elf_set_reloc_sh_name (abfd,
6596 d->rel.hdr,
6597 name, false))
6598 return false;
6599 if (d->rela.hdr
6600 && !_bfd_elf_set_reloc_sh_name (abfd,
6601 d->rela.hdr,
6602 name, true))
6603 return false;
6604
6605 /* Update section size and contents. */
6606 shdrp->sh_size = sec->size;
6607 shdrp->contents = sec->contents;
6608 shdrp->bfd_section->contents = NULL;
6609 }
6610 else if (is_ctf)
6611 {
6612 /* Update section size and contents. */
6613 shdrp->sh_size = sec->size;
6614 shdrp->contents = sec->contents;
6615 }
6616
6617 off = _bfd_elf_assign_file_position_for_section (shdrp,
6618 off,
6619 true);
6620 }
6621 }
6622 }
6623
6624 /* Place section name section after DWARF debug sections have been
6625 compressed. */
6626 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6627 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6628 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6629 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6630
6631 /* Place the section headers. */
6632 i_ehdrp = elf_elfheader (abfd);
6633 bed = get_elf_backend_data (abfd);
6634 off = align_file_position (off, 1 << bed->s->log_file_align);
6635 i_ehdrp->e_shoff = off;
6636 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6637 elf_next_file_pos (abfd) = off;
6638
6639 return true;
6640 }
6641
6642 bool
6643 _bfd_elf_write_object_contents (bfd *abfd)
6644 {
6645 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6646 Elf_Internal_Shdr **i_shdrp;
6647 bool failed;
6648 unsigned int count, num_sec;
6649 struct elf_obj_tdata *t;
6650
6651 if (! abfd->output_has_begun
6652 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6653 return false;
6654 /* Do not rewrite ELF data when the BFD has been opened for update.
6655 abfd->output_has_begun was set to TRUE on opening, so creation of new
6656 sections, and modification of existing section sizes was restricted.
6657 This means the ELF header, program headers and section headers can't have
6658 changed.
6659 If the contents of any sections has been modified, then those changes have
6660 already been written to the BFD. */
6661 else if (abfd->direction == both_direction)
6662 {
6663 BFD_ASSERT (abfd->output_has_begun);
6664 return true;
6665 }
6666
6667 i_shdrp = elf_elfsections (abfd);
6668
6669 failed = false;
6670 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6671 if (failed)
6672 return false;
6673
6674 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6675 return false;
6676
6677 /* After writing the headers, we need to write the sections too... */
6678 num_sec = elf_numsections (abfd);
6679 for (count = 1; count < num_sec; count++)
6680 {
6681 i_shdrp[count]->sh_name
6682 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6683 i_shdrp[count]->sh_name);
6684 if (bed->elf_backend_section_processing)
6685 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6686 return false;
6687 if (i_shdrp[count]->contents)
6688 {
6689 bfd_size_type amt = i_shdrp[count]->sh_size;
6690
6691 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6692 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6693 return false;
6694 }
6695 }
6696
6697 /* Write out the section header names. */
6698 t = elf_tdata (abfd);
6699 if (elf_shstrtab (abfd) != NULL
6700 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6701 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6702 return false;
6703
6704 if (!(*bed->elf_backend_final_write_processing) (abfd))
6705 return false;
6706
6707 if (!bed->s->write_shdrs_and_ehdr (abfd))
6708 return false;
6709
6710 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6711 if (t->o->build_id.after_write_object_contents != NULL)
6712 return (*t->o->build_id.after_write_object_contents) (abfd);
6713
6714 return true;
6715 }
6716
6717 bool
6718 _bfd_elf_write_corefile_contents (bfd *abfd)
6719 {
6720 /* Hopefully this can be done just like an object file. */
6721 return _bfd_elf_write_object_contents (abfd);
6722 }
6723
6724 /* Given a section, search the header to find them. */
6725
6726 unsigned int
6727 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6728 {
6729 const struct elf_backend_data *bed;
6730 unsigned int sec_index;
6731
6732 if (elf_section_data (asect) != NULL
6733 && elf_section_data (asect)->this_idx != 0)
6734 return elf_section_data (asect)->this_idx;
6735
6736 if (bfd_is_abs_section (asect))
6737 sec_index = SHN_ABS;
6738 else if (bfd_is_com_section (asect))
6739 sec_index = SHN_COMMON;
6740 else if (bfd_is_und_section (asect))
6741 sec_index = SHN_UNDEF;
6742 else
6743 sec_index = SHN_BAD;
6744
6745 bed = get_elf_backend_data (abfd);
6746 if (bed->elf_backend_section_from_bfd_section)
6747 {
6748 int retval = sec_index;
6749
6750 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6751 return retval;
6752 }
6753
6754 if (sec_index == SHN_BAD)
6755 bfd_set_error (bfd_error_nonrepresentable_section);
6756
6757 return sec_index;
6758 }
6759
6760 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6761 on error. */
6762
6763 int
6764 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6765 {
6766 asymbol *asym_ptr = *asym_ptr_ptr;
6767 int idx;
6768 flagword flags = asym_ptr->flags;
6769
6770 /* When gas creates relocations against local labels, it creates its
6771 own symbol for the section, but does put the symbol into the
6772 symbol chain, so udata is 0. When the linker is generating
6773 relocatable output, this section symbol may be for one of the
6774 input sections rather than the output section. */
6775 if (asym_ptr->udata.i == 0
6776 && (flags & BSF_SECTION_SYM)
6777 && asym_ptr->section)
6778 {
6779 asection *sec;
6780 int indx;
6781
6782 sec = asym_ptr->section;
6783 if (sec->owner != abfd && sec->output_section != NULL)
6784 sec = sec->output_section;
6785 if (sec->owner == abfd
6786 && (indx = sec->index) < elf_num_section_syms (abfd)
6787 && elf_section_syms (abfd)[indx] != NULL)
6788 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6789 }
6790
6791 idx = asym_ptr->udata.i;
6792
6793 if (idx == 0)
6794 {
6795 /* This case can occur when using --strip-symbol on a symbol
6796 which is used in a relocation entry. */
6797 _bfd_error_handler
6798 /* xgettext:c-format */
6799 (_("%pB: symbol `%s' required but not present"),
6800 abfd, bfd_asymbol_name (asym_ptr));
6801 bfd_set_error (bfd_error_no_symbols);
6802 return -1;
6803 }
6804
6805 #if DEBUG & 4
6806 {
6807 fprintf (stderr,
6808 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6809 (long) asym_ptr, asym_ptr->name, idx, flags);
6810 fflush (stderr);
6811 }
6812 #endif
6813
6814 return idx;
6815 }
6816
6817 /* Rewrite program header information. */
6818
6819 static bool
6820 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6821 {
6822 Elf_Internal_Ehdr *iehdr;
6823 struct elf_segment_map *map;
6824 struct elf_segment_map *map_first;
6825 struct elf_segment_map **pointer_to_map;
6826 Elf_Internal_Phdr *segment;
6827 asection *section;
6828 unsigned int i;
6829 unsigned int num_segments;
6830 bool phdr_included = false;
6831 bool p_paddr_valid;
6832 struct elf_segment_map *phdr_adjust_seg = NULL;
6833 unsigned int phdr_adjust_num = 0;
6834 const struct elf_backend_data *bed;
6835 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
6836
6837 bed = get_elf_backend_data (ibfd);
6838 iehdr = elf_elfheader (ibfd);
6839
6840 map_first = NULL;
6841 pointer_to_map = &map_first;
6842
6843 num_segments = elf_elfheader (ibfd)->e_phnum;
6844
6845 /* Returns the end address of the segment + 1. */
6846 #define SEGMENT_END(segment, start) \
6847 (start + (segment->p_memsz > segment->p_filesz \
6848 ? segment->p_memsz : segment->p_filesz))
6849
6850 #define SECTION_SIZE(section, segment) \
6851 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6852 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6853 ? section->size : 0)
6854
6855 /* Returns TRUE if the given section is contained within
6856 the given segment. VMA addresses are compared. */
6857 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6858 (section->vma * (opb) >= segment->p_vaddr \
6859 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6860 <= (SEGMENT_END (segment, segment->p_vaddr))))
6861
6862 /* Returns TRUE if the given section is contained within
6863 the given segment. LMA addresses are compared. */
6864 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6865 (section->lma * (opb) >= base \
6866 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6867 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6868 <= SEGMENT_END (segment, base)))
6869
6870 /* Handle PT_NOTE segment. */
6871 #define IS_NOTE(p, s) \
6872 (p->p_type == PT_NOTE \
6873 && elf_section_type (s) == SHT_NOTE \
6874 && (bfd_vma) s->filepos >= p->p_offset \
6875 && ((bfd_vma) s->filepos + s->size \
6876 <= p->p_offset + p->p_filesz))
6877
6878 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6879 etc. */
6880 #define IS_COREFILE_NOTE(p, s) \
6881 (IS_NOTE (p, s) \
6882 && bfd_get_format (ibfd) == bfd_core \
6883 && s->vma == 0 \
6884 && s->lma == 0)
6885
6886 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6887 linker, which generates a PT_INTERP section with p_vaddr and
6888 p_memsz set to 0. */
6889 #define IS_SOLARIS_PT_INTERP(p, s) \
6890 (p->p_vaddr == 0 \
6891 && p->p_paddr == 0 \
6892 && p->p_memsz == 0 \
6893 && p->p_filesz > 0 \
6894 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6895 && s->size > 0 \
6896 && (bfd_vma) s->filepos >= p->p_offset \
6897 && ((bfd_vma) s->filepos + s->size \
6898 <= p->p_offset + p->p_filesz))
6899
6900 /* Decide if the given section should be included in the given segment.
6901 A section will be included if:
6902 1. It is within the address space of the segment -- we use the LMA
6903 if that is set for the segment and the VMA otherwise,
6904 2. It is an allocated section or a NOTE section in a PT_NOTE
6905 segment.
6906 3. There is an output section associated with it,
6907 4. The section has not already been allocated to a previous segment.
6908 5. PT_GNU_STACK segments do not include any sections.
6909 6. PT_TLS segment includes only SHF_TLS sections.
6910 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6911 8. PT_DYNAMIC should not contain empty sections at the beginning
6912 (with the possible exception of .dynamic). */
6913 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6914 ((((segment->p_paddr \
6915 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6916 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6917 && (section->flags & SEC_ALLOC) != 0) \
6918 || IS_NOTE (segment, section)) \
6919 && segment->p_type != PT_GNU_STACK \
6920 && (segment->p_type != PT_TLS \
6921 || (section->flags & SEC_THREAD_LOCAL)) \
6922 && (segment->p_type == PT_LOAD \
6923 || segment->p_type == PT_TLS \
6924 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6925 && (segment->p_type != PT_DYNAMIC \
6926 || SECTION_SIZE (section, segment) > 0 \
6927 || (segment->p_paddr \
6928 ? segment->p_paddr != section->lma * (opb) \
6929 : segment->p_vaddr != section->vma * (opb)) \
6930 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6931 && (segment->p_type != PT_LOAD || !section->segment_mark))
6932
6933 /* If the output section of a section in the input segment is NULL,
6934 it is removed from the corresponding output segment. */
6935 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6936 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6937 && section->output_section != NULL)
6938
6939 /* Returns TRUE iff seg1 starts after the end of seg2. */
6940 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6941 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6942
6943 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6944 their VMA address ranges and their LMA address ranges overlap.
6945 It is possible to have overlapping VMA ranges without overlapping LMA
6946 ranges. RedBoot images for example can have both .data and .bss mapped
6947 to the same VMA range, but with the .data section mapped to a different
6948 LMA. */
6949 #define SEGMENT_OVERLAPS(seg1, seg2) \
6950 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6951 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6952 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6953 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6954
6955 /* Initialise the segment mark field. */
6956 for (section = ibfd->sections; section != NULL; section = section->next)
6957 section->segment_mark = false;
6958
6959 /* The Solaris linker creates program headers in which all the
6960 p_paddr fields are zero. When we try to objcopy or strip such a
6961 file, we get confused. Check for this case, and if we find it
6962 don't set the p_paddr_valid fields. */
6963 p_paddr_valid = false;
6964 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6965 i < num_segments;
6966 i++, segment++)
6967 if (segment->p_paddr != 0)
6968 {
6969 p_paddr_valid = true;
6970 break;
6971 }
6972
6973 /* Scan through the segments specified in the program header
6974 of the input BFD. For this first scan we look for overlaps
6975 in the loadable segments. These can be created by weird
6976 parameters to objcopy. Also, fix some solaris weirdness. */
6977 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6978 i < num_segments;
6979 i++, segment++)
6980 {
6981 unsigned int j;
6982 Elf_Internal_Phdr *segment2;
6983
6984 if (segment->p_type == PT_INTERP)
6985 for (section = ibfd->sections; section; section = section->next)
6986 if (IS_SOLARIS_PT_INTERP (segment, section))
6987 {
6988 /* Mininal change so that the normal section to segment
6989 assignment code will work. */
6990 segment->p_vaddr = section->vma * opb;
6991 break;
6992 }
6993
6994 if (segment->p_type != PT_LOAD)
6995 {
6996 /* Remove PT_GNU_RELRO segment. */
6997 if (segment->p_type == PT_GNU_RELRO)
6998 segment->p_type = PT_NULL;
6999 continue;
7000 }
7001
7002 /* Determine if this segment overlaps any previous segments. */
7003 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7004 {
7005 bfd_signed_vma extra_length;
7006
7007 if (segment2->p_type != PT_LOAD
7008 || !SEGMENT_OVERLAPS (segment, segment2))
7009 continue;
7010
7011 /* Merge the two segments together. */
7012 if (segment2->p_vaddr < segment->p_vaddr)
7013 {
7014 /* Extend SEGMENT2 to include SEGMENT and then delete
7015 SEGMENT. */
7016 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7017 - SEGMENT_END (segment2, segment2->p_vaddr));
7018
7019 if (extra_length > 0)
7020 {
7021 segment2->p_memsz += extra_length;
7022 segment2->p_filesz += extra_length;
7023 }
7024
7025 segment->p_type = PT_NULL;
7026
7027 /* Since we have deleted P we must restart the outer loop. */
7028 i = 0;
7029 segment = elf_tdata (ibfd)->phdr;
7030 break;
7031 }
7032 else
7033 {
7034 /* Extend SEGMENT to include SEGMENT2 and then delete
7035 SEGMENT2. */
7036 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7037 - SEGMENT_END (segment, segment->p_vaddr));
7038
7039 if (extra_length > 0)
7040 {
7041 segment->p_memsz += extra_length;
7042 segment->p_filesz += extra_length;
7043 }
7044
7045 segment2->p_type = PT_NULL;
7046 }
7047 }
7048 }
7049
7050 /* The second scan attempts to assign sections to segments. */
7051 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7052 i < num_segments;
7053 i++, segment++)
7054 {
7055 unsigned int section_count;
7056 asection **sections;
7057 asection *output_section;
7058 unsigned int isec;
7059 asection *matching_lma;
7060 asection *suggested_lma;
7061 unsigned int j;
7062 size_t amt;
7063 asection *first_section;
7064
7065 if (segment->p_type == PT_NULL)
7066 continue;
7067
7068 first_section = NULL;
7069 /* Compute how many sections might be placed into this segment. */
7070 for (section = ibfd->sections, section_count = 0;
7071 section != NULL;
7072 section = section->next)
7073 {
7074 /* Find the first section in the input segment, which may be
7075 removed from the corresponding output segment. */
7076 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7077 {
7078 if (first_section == NULL)
7079 first_section = section;
7080 if (section->output_section != NULL)
7081 ++section_count;
7082 }
7083 }
7084
7085 /* Allocate a segment map big enough to contain
7086 all of the sections we have selected. */
7087 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7088 amt += section_count * sizeof (asection *);
7089 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7090 if (map == NULL)
7091 return false;
7092
7093 /* Initialise the fields of the segment map. Default to
7094 using the physical address of the segment in the input BFD. */
7095 map->next = NULL;
7096 map->p_type = segment->p_type;
7097 map->p_flags = segment->p_flags;
7098 map->p_flags_valid = 1;
7099
7100 if (map->p_type == PT_LOAD
7101 && (ibfd->flags & D_PAGED) != 0
7102 && maxpagesize > 1
7103 && segment->p_align > 1)
7104 {
7105 map->p_align = segment->p_align;
7106 if (segment->p_align > maxpagesize)
7107 map->p_align = maxpagesize;
7108 map->p_align_valid = 1;
7109 }
7110
7111 /* If the first section in the input segment is removed, there is
7112 no need to preserve segment physical address in the corresponding
7113 output segment. */
7114 if (!first_section || first_section->output_section != NULL)
7115 {
7116 map->p_paddr = segment->p_paddr;
7117 map->p_paddr_valid = p_paddr_valid;
7118 }
7119
7120 /* Determine if this segment contains the ELF file header
7121 and if it contains the program headers themselves. */
7122 map->includes_filehdr = (segment->p_offset == 0
7123 && segment->p_filesz >= iehdr->e_ehsize);
7124 map->includes_phdrs = 0;
7125
7126 if (!phdr_included || segment->p_type != PT_LOAD)
7127 {
7128 map->includes_phdrs =
7129 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7130 && (segment->p_offset + segment->p_filesz
7131 >= ((bfd_vma) iehdr->e_phoff
7132 + iehdr->e_phnum * iehdr->e_phentsize)));
7133
7134 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7135 phdr_included = true;
7136 }
7137
7138 if (section_count == 0)
7139 {
7140 /* Special segments, such as the PT_PHDR segment, may contain
7141 no sections, but ordinary, loadable segments should contain
7142 something. They are allowed by the ELF spec however, so only
7143 a warning is produced.
7144 There is however the valid use case of embedded systems which
7145 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7146 flash memory with zeros. No warning is shown for that case. */
7147 if (segment->p_type == PT_LOAD
7148 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7149 /* xgettext:c-format */
7150 _bfd_error_handler
7151 (_("%pB: warning: empty loadable segment detected"
7152 " at vaddr=%#" PRIx64 ", is this intentional?"),
7153 ibfd, (uint64_t) segment->p_vaddr);
7154
7155 map->p_vaddr_offset = segment->p_vaddr / opb;
7156 map->count = 0;
7157 *pointer_to_map = map;
7158 pointer_to_map = &map->next;
7159
7160 continue;
7161 }
7162
7163 /* Now scan the sections in the input BFD again and attempt
7164 to add their corresponding output sections to the segment map.
7165 The problem here is how to handle an output section which has
7166 been moved (ie had its LMA changed). There are four possibilities:
7167
7168 1. None of the sections have been moved.
7169 In this case we can continue to use the segment LMA from the
7170 input BFD.
7171
7172 2. All of the sections have been moved by the same amount.
7173 In this case we can change the segment's LMA to match the LMA
7174 of the first section.
7175
7176 3. Some of the sections have been moved, others have not.
7177 In this case those sections which have not been moved can be
7178 placed in the current segment which will have to have its size,
7179 and possibly its LMA changed, and a new segment or segments will
7180 have to be created to contain the other sections.
7181
7182 4. The sections have been moved, but not by the same amount.
7183 In this case we can change the segment's LMA to match the LMA
7184 of the first section and we will have to create a new segment
7185 or segments to contain the other sections.
7186
7187 In order to save time, we allocate an array to hold the section
7188 pointers that we are interested in. As these sections get assigned
7189 to a segment, they are removed from this array. */
7190
7191 amt = section_count * sizeof (asection *);
7192 sections = (asection **) bfd_malloc (amt);
7193 if (sections == NULL)
7194 return false;
7195
7196 /* Step One: Scan for segment vs section LMA conflicts.
7197 Also add the sections to the section array allocated above.
7198 Also add the sections to the current segment. In the common
7199 case, where the sections have not been moved, this means that
7200 we have completely filled the segment, and there is nothing
7201 more to do. */
7202 isec = 0;
7203 matching_lma = NULL;
7204 suggested_lma = NULL;
7205
7206 for (section = first_section, j = 0;
7207 section != NULL;
7208 section = section->next)
7209 {
7210 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7211 {
7212 output_section = section->output_section;
7213
7214 sections[j++] = section;
7215
7216 /* The Solaris native linker always sets p_paddr to 0.
7217 We try to catch that case here, and set it to the
7218 correct value. Note - some backends require that
7219 p_paddr be left as zero. */
7220 if (!p_paddr_valid
7221 && segment->p_vaddr != 0
7222 && !bed->want_p_paddr_set_to_zero
7223 && isec == 0
7224 && output_section->lma != 0
7225 && (align_power (segment->p_vaddr
7226 + (map->includes_filehdr
7227 ? iehdr->e_ehsize : 0)
7228 + (map->includes_phdrs
7229 ? iehdr->e_phnum * iehdr->e_phentsize
7230 : 0),
7231 output_section->alignment_power * opb)
7232 == (output_section->vma * opb)))
7233 map->p_paddr = segment->p_vaddr;
7234
7235 /* Match up the physical address of the segment with the
7236 LMA address of the output section. */
7237 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7238 opb)
7239 || IS_COREFILE_NOTE (segment, section)
7240 || (bed->want_p_paddr_set_to_zero
7241 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
7242 {
7243 if (matching_lma == NULL
7244 || output_section->lma < matching_lma->lma)
7245 matching_lma = output_section;
7246
7247 /* We assume that if the section fits within the segment
7248 then it does not overlap any other section within that
7249 segment. */
7250 map->sections[isec++] = output_section;
7251 }
7252 else if (suggested_lma == NULL)
7253 suggested_lma = output_section;
7254
7255 if (j == section_count)
7256 break;
7257 }
7258 }
7259
7260 BFD_ASSERT (j == section_count);
7261
7262 /* Step Two: Adjust the physical address of the current segment,
7263 if necessary. */
7264 if (isec == section_count)
7265 {
7266 /* All of the sections fitted within the segment as currently
7267 specified. This is the default case. Add the segment to
7268 the list of built segments and carry on to process the next
7269 program header in the input BFD. */
7270 map->count = section_count;
7271 *pointer_to_map = map;
7272 pointer_to_map = &map->next;
7273
7274 if (p_paddr_valid
7275 && !bed->want_p_paddr_set_to_zero)
7276 {
7277 bfd_vma hdr_size = 0;
7278 if (map->includes_filehdr)
7279 hdr_size = iehdr->e_ehsize;
7280 if (map->includes_phdrs)
7281 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7282
7283 /* Account for padding before the first section in the
7284 segment. */
7285 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7286 - matching_lma->lma);
7287 }
7288
7289 free (sections);
7290 continue;
7291 }
7292 else
7293 {
7294 /* Change the current segment's physical address to match
7295 the LMA of the first section that fitted, or if no
7296 section fitted, the first section. */
7297 if (matching_lma == NULL)
7298 matching_lma = suggested_lma;
7299
7300 map->p_paddr = matching_lma->lma * opb;
7301
7302 /* Offset the segment physical address from the lma
7303 to allow for space taken up by elf headers. */
7304 if (map->includes_phdrs)
7305 {
7306 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7307
7308 /* iehdr->e_phnum is just an estimate of the number
7309 of program headers that we will need. Make a note
7310 here of the number we used and the segment we chose
7311 to hold these headers, so that we can adjust the
7312 offset when we know the correct value. */
7313 phdr_adjust_num = iehdr->e_phnum;
7314 phdr_adjust_seg = map;
7315 }
7316
7317 if (map->includes_filehdr)
7318 {
7319 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7320 map->p_paddr -= iehdr->e_ehsize;
7321 /* We've subtracted off the size of headers from the
7322 first section lma, but there may have been some
7323 alignment padding before that section too. Try to
7324 account for that by adjusting the segment lma down to
7325 the same alignment. */
7326 if (segment->p_align != 0 && segment->p_align < align)
7327 align = segment->p_align;
7328 map->p_paddr &= -(align * opb);
7329 }
7330 }
7331
7332 /* Step Three: Loop over the sections again, this time assigning
7333 those that fit to the current segment and removing them from the
7334 sections array; but making sure not to leave large gaps. Once all
7335 possible sections have been assigned to the current segment it is
7336 added to the list of built segments and if sections still remain
7337 to be assigned, a new segment is constructed before repeating
7338 the loop. */
7339 isec = 0;
7340 do
7341 {
7342 map->count = 0;
7343 suggested_lma = NULL;
7344
7345 /* Fill the current segment with sections that fit. */
7346 for (j = 0; j < section_count; j++)
7347 {
7348 section = sections[j];
7349
7350 if (section == NULL)
7351 continue;
7352
7353 output_section = section->output_section;
7354
7355 BFD_ASSERT (output_section != NULL);
7356
7357 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7358 opb)
7359 || IS_COREFILE_NOTE (segment, section))
7360 {
7361 if (map->count == 0)
7362 {
7363 /* If the first section in a segment does not start at
7364 the beginning of the segment, then something is
7365 wrong. */
7366 if (align_power (map->p_paddr
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->lma * opb)
7374 goto sorry;
7375 }
7376 else
7377 {
7378 asection *prev_sec;
7379
7380 prev_sec = map->sections[map->count - 1];
7381
7382 /* If the gap between the end of the previous section
7383 and the start of this section is more than
7384 maxpagesize then we need to start a new segment. */
7385 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7386 maxpagesize)
7387 < BFD_ALIGN (output_section->lma, maxpagesize))
7388 || (prev_sec->lma + prev_sec->size
7389 > output_section->lma))
7390 {
7391 if (suggested_lma == NULL)
7392 suggested_lma = output_section;
7393
7394 continue;
7395 }
7396 }
7397
7398 map->sections[map->count++] = output_section;
7399 ++isec;
7400 sections[j] = NULL;
7401 if (segment->p_type == PT_LOAD)
7402 section->segment_mark = true;
7403 }
7404 else if (suggested_lma == NULL)
7405 suggested_lma = output_section;
7406 }
7407
7408 /* PR 23932. A corrupt input file may contain sections that cannot
7409 be assigned to any segment - because for example they have a
7410 negative size - or segments that do not contain any sections.
7411 But there are also valid reasons why a segment can be empty.
7412 So allow a count of zero. */
7413
7414 /* Add the current segment to the list of built segments. */
7415 *pointer_to_map = map;
7416 pointer_to_map = &map->next;
7417
7418 if (isec < section_count)
7419 {
7420 /* We still have not allocated all of the sections to
7421 segments. Create a new segment here, initialise it
7422 and carry on looping. */
7423 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7424 amt += section_count * sizeof (asection *);
7425 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7426 if (map == NULL)
7427 {
7428 free (sections);
7429 return false;
7430 }
7431
7432 /* Initialise the fields of the segment map. Set the physical
7433 physical address to the LMA of the first section that has
7434 not yet been assigned. */
7435 map->next = NULL;
7436 map->p_type = segment->p_type;
7437 map->p_flags = segment->p_flags;
7438 map->p_flags_valid = 1;
7439 map->p_paddr = suggested_lma->lma * opb;
7440 map->p_paddr_valid = p_paddr_valid;
7441 map->includes_filehdr = 0;
7442 map->includes_phdrs = 0;
7443 }
7444
7445 continue;
7446 sorry:
7447 bfd_set_error (bfd_error_sorry);
7448 free (sections);
7449 return false;
7450 }
7451 while (isec < section_count);
7452
7453 free (sections);
7454 }
7455
7456 elf_seg_map (obfd) = map_first;
7457
7458 /* If we had to estimate the number of program headers that were
7459 going to be needed, then check our estimate now and adjust
7460 the offset if necessary. */
7461 if (phdr_adjust_seg != NULL)
7462 {
7463 unsigned int count;
7464
7465 for (count = 0, map = map_first; map != NULL; map = map->next)
7466 count++;
7467
7468 if (count > phdr_adjust_num)
7469 phdr_adjust_seg->p_paddr
7470 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7471
7472 for (map = map_first; map != NULL; map = map->next)
7473 if (map->p_type == PT_PHDR)
7474 {
7475 bfd_vma adjust
7476 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7477 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7478 break;
7479 }
7480 }
7481
7482 #undef SEGMENT_END
7483 #undef SECTION_SIZE
7484 #undef IS_CONTAINED_BY_VMA
7485 #undef IS_CONTAINED_BY_LMA
7486 #undef IS_NOTE
7487 #undef IS_COREFILE_NOTE
7488 #undef IS_SOLARIS_PT_INTERP
7489 #undef IS_SECTION_IN_INPUT_SEGMENT
7490 #undef INCLUDE_SECTION_IN_SEGMENT
7491 #undef SEGMENT_AFTER_SEGMENT
7492 #undef SEGMENT_OVERLAPS
7493 return true;
7494 }
7495
7496 /* Copy ELF program header information. */
7497
7498 static bool
7499 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7500 {
7501 Elf_Internal_Ehdr *iehdr;
7502 struct elf_segment_map *map;
7503 struct elf_segment_map *map_first;
7504 struct elf_segment_map **pointer_to_map;
7505 Elf_Internal_Phdr *segment;
7506 unsigned int i;
7507 unsigned int num_segments;
7508 bool phdr_included = false;
7509 bool p_paddr_valid;
7510 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7511
7512 iehdr = elf_elfheader (ibfd);
7513
7514 map_first = NULL;
7515 pointer_to_map = &map_first;
7516
7517 /* If all the segment p_paddr fields are zero, don't set
7518 map->p_paddr_valid. */
7519 p_paddr_valid = false;
7520 num_segments = elf_elfheader (ibfd)->e_phnum;
7521 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7522 i < num_segments;
7523 i++, segment++)
7524 if (segment->p_paddr != 0)
7525 {
7526 p_paddr_valid = true;
7527 break;
7528 }
7529
7530 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7531 i < num_segments;
7532 i++, segment++)
7533 {
7534 asection *section;
7535 unsigned int section_count;
7536 size_t amt;
7537 Elf_Internal_Shdr *this_hdr;
7538 asection *first_section = NULL;
7539 asection *lowest_section;
7540
7541 /* Compute how many sections are in this segment. */
7542 for (section = ibfd->sections, section_count = 0;
7543 section != NULL;
7544 section = section->next)
7545 {
7546 this_hdr = &(elf_section_data(section)->this_hdr);
7547 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7548 {
7549 if (first_section == NULL)
7550 first_section = section;
7551 section_count++;
7552 }
7553 }
7554
7555 /* Allocate a segment map big enough to contain
7556 all of the sections we have selected. */
7557 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7558 amt += section_count * sizeof (asection *);
7559 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7560 if (map == NULL)
7561 return false;
7562
7563 /* Initialize the fields of the output segment map with the
7564 input segment. */
7565 map->next = NULL;
7566 map->p_type = segment->p_type;
7567 map->p_flags = segment->p_flags;
7568 map->p_flags_valid = 1;
7569 map->p_paddr = segment->p_paddr;
7570 map->p_paddr_valid = p_paddr_valid;
7571 map->p_align = segment->p_align;
7572 map->p_align_valid = 1;
7573 map->p_vaddr_offset = 0;
7574
7575 if (map->p_type == PT_GNU_RELRO
7576 || map->p_type == PT_GNU_STACK)
7577 {
7578 /* The PT_GNU_RELRO segment may contain the first a few
7579 bytes in the .got.plt section even if the whole .got.plt
7580 section isn't in the PT_GNU_RELRO segment. We won't
7581 change the size of the PT_GNU_RELRO segment.
7582 Similarly, PT_GNU_STACK size is significant on uclinux
7583 systems. */
7584 map->p_size = segment->p_memsz;
7585 map->p_size_valid = 1;
7586 }
7587
7588 /* Determine if this segment contains the ELF file header
7589 and if it contains the program headers themselves. */
7590 map->includes_filehdr = (segment->p_offset == 0
7591 && segment->p_filesz >= iehdr->e_ehsize);
7592
7593 map->includes_phdrs = 0;
7594 if (! phdr_included || segment->p_type != PT_LOAD)
7595 {
7596 map->includes_phdrs =
7597 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7598 && (segment->p_offset + segment->p_filesz
7599 >= ((bfd_vma) iehdr->e_phoff
7600 + iehdr->e_phnum * iehdr->e_phentsize)));
7601
7602 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7603 phdr_included = true;
7604 }
7605
7606 lowest_section = NULL;
7607 if (section_count != 0)
7608 {
7609 unsigned int isec = 0;
7610
7611 for (section = first_section;
7612 section != NULL;
7613 section = section->next)
7614 {
7615 this_hdr = &(elf_section_data(section)->this_hdr);
7616 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7617 {
7618 map->sections[isec++] = section->output_section;
7619 if ((section->flags & SEC_ALLOC) != 0)
7620 {
7621 bfd_vma seg_off;
7622
7623 if (lowest_section == NULL
7624 || section->lma < lowest_section->lma)
7625 lowest_section = section;
7626
7627 /* Section lmas are set up from PT_LOAD header
7628 p_paddr in _bfd_elf_make_section_from_shdr.
7629 If this header has a p_paddr that disagrees
7630 with the section lma, flag the p_paddr as
7631 invalid. */
7632 if ((section->flags & SEC_LOAD) != 0)
7633 seg_off = this_hdr->sh_offset - segment->p_offset;
7634 else
7635 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7636 if (section->lma * opb - segment->p_paddr != seg_off)
7637 map->p_paddr_valid = false;
7638 }
7639 if (isec == section_count)
7640 break;
7641 }
7642 }
7643 }
7644
7645 if (section_count == 0)
7646 map->p_vaddr_offset = segment->p_vaddr / opb;
7647 else if (map->p_paddr_valid)
7648 {
7649 /* Account for padding before the first section in the segment. */
7650 bfd_vma hdr_size = 0;
7651 if (map->includes_filehdr)
7652 hdr_size = iehdr->e_ehsize;
7653 if (map->includes_phdrs)
7654 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7655
7656 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7657 - (lowest_section ? lowest_section->lma : 0));
7658 }
7659
7660 map->count = section_count;
7661 *pointer_to_map = map;
7662 pointer_to_map = &map->next;
7663 }
7664
7665 elf_seg_map (obfd) = map_first;
7666 return true;
7667 }
7668
7669 /* Copy private BFD data. This copies or rewrites ELF program header
7670 information. */
7671
7672 static bool
7673 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7674 {
7675 bfd_vma maxpagesize;
7676
7677 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7678 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7679 return true;
7680
7681 if (elf_tdata (ibfd)->phdr == NULL)
7682 return true;
7683
7684 if (ibfd->xvec == obfd->xvec)
7685 {
7686 /* Check to see if any sections in the input BFD
7687 covered by ELF program header have changed. */
7688 Elf_Internal_Phdr *segment;
7689 asection *section, *osec;
7690 unsigned int i, num_segments;
7691 Elf_Internal_Shdr *this_hdr;
7692 const struct elf_backend_data *bed;
7693
7694 bed = get_elf_backend_data (ibfd);
7695
7696 /* Regenerate the segment map if p_paddr is set to 0. */
7697 if (bed->want_p_paddr_set_to_zero)
7698 goto rewrite;
7699
7700 /* Initialize the segment mark field. */
7701 for (section = obfd->sections; section != NULL;
7702 section = section->next)
7703 section->segment_mark = false;
7704
7705 num_segments = elf_elfheader (ibfd)->e_phnum;
7706 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7707 i < num_segments;
7708 i++, segment++)
7709 {
7710 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7711 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7712 which severly confuses things, so always regenerate the segment
7713 map in this case. */
7714 if (segment->p_paddr == 0
7715 && segment->p_memsz == 0
7716 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7717 goto rewrite;
7718
7719 for (section = ibfd->sections;
7720 section != NULL; section = section->next)
7721 {
7722 /* We mark the output section so that we know it comes
7723 from the input BFD. */
7724 osec = section->output_section;
7725 if (osec)
7726 osec->segment_mark = true;
7727
7728 /* Check if this section is covered by the segment. */
7729 this_hdr = &(elf_section_data(section)->this_hdr);
7730 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7731 {
7732 /* FIXME: Check if its output section is changed or
7733 removed. What else do we need to check? */
7734 if (osec == NULL
7735 || section->flags != osec->flags
7736 || section->lma != osec->lma
7737 || section->vma != osec->vma
7738 || section->size != osec->size
7739 || section->rawsize != osec->rawsize
7740 || section->alignment_power != osec->alignment_power)
7741 goto rewrite;
7742 }
7743 }
7744 }
7745
7746 /* Check to see if any output section do not come from the
7747 input BFD. */
7748 for (section = obfd->sections; section != NULL;
7749 section = section->next)
7750 {
7751 if (!section->segment_mark)
7752 goto rewrite;
7753 else
7754 section->segment_mark = false;
7755 }
7756
7757 return copy_elf_program_header (ibfd, obfd);
7758 }
7759
7760 rewrite:
7761 maxpagesize = 0;
7762 if (ibfd->xvec == obfd->xvec)
7763 {
7764 /* When rewriting program header, set the output maxpagesize to
7765 the maximum alignment of input PT_LOAD segments. */
7766 Elf_Internal_Phdr *segment;
7767 unsigned int i;
7768 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7769
7770 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7771 i < num_segments;
7772 i++, segment++)
7773 if (segment->p_type == PT_LOAD
7774 && maxpagesize < segment->p_align)
7775 {
7776 /* PR 17512: file: f17299af. */
7777 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7778 /* xgettext:c-format */
7779 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7780 PRIx64 " is too large"),
7781 ibfd, (uint64_t) segment->p_align);
7782 else
7783 maxpagesize = segment->p_align;
7784 }
7785 }
7786 if (maxpagesize == 0)
7787 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7788
7789 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7790 }
7791
7792 /* Initialize private output section information from input section. */
7793
7794 bool
7795 _bfd_elf_init_private_section_data (bfd *ibfd,
7796 asection *isec,
7797 bfd *obfd,
7798 asection *osec,
7799 struct bfd_link_info *link_info)
7800
7801 {
7802 Elf_Internal_Shdr *ihdr, *ohdr;
7803 bool final_link = (link_info != NULL
7804 && !bfd_link_relocatable (link_info));
7805
7806 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7807 || obfd->xvec->flavour != bfd_target_elf_flavour)
7808 return true;
7809
7810 BFD_ASSERT (elf_section_data (osec) != NULL);
7811
7812 /* If this is a known ABI section, ELF section type and flags may
7813 have been set up when OSEC was created. For normal sections we
7814 allow the user to override the type and flags other than
7815 SHF_MASKOS and SHF_MASKPROC. */
7816 if (elf_section_type (osec) == SHT_PROGBITS
7817 || elf_section_type (osec) == SHT_NOTE
7818 || elf_section_type (osec) == SHT_NOBITS)
7819 elf_section_type (osec) = SHT_NULL;
7820 /* For objcopy and relocatable link, copy the ELF section type from
7821 the input file if the BFD section flags are the same. (If they
7822 are different the user may be doing something like
7823 "objcopy --set-section-flags .text=alloc,data".) For a final
7824 link allow some flags that the linker clears to differ. */
7825 if (elf_section_type (osec) == SHT_NULL
7826 && (osec->flags == isec->flags
7827 || (final_link
7828 && ((osec->flags ^ isec->flags)
7829 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7830 elf_section_type (osec) = elf_section_type (isec);
7831
7832 /* FIXME: Is this correct for all OS/PROC specific flags? */
7833 elf_section_flags (osec) = (elf_section_flags (isec)
7834 & (SHF_MASKOS | SHF_MASKPROC));
7835
7836 /* Copy sh_info from input for mbind section. */
7837 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7838 && elf_section_flags (isec) & SHF_GNU_MBIND)
7839 elf_section_data (osec)->this_hdr.sh_info
7840 = elf_section_data (isec)->this_hdr.sh_info;
7841
7842 /* Set things up for objcopy and relocatable link. The output
7843 SHT_GROUP section will have its elf_next_in_group pointing back
7844 to the input group members. Ignore linker created group section.
7845 See elfNN_ia64_object_p in elfxx-ia64.c. */
7846 if ((link_info == NULL
7847 || !link_info->resolve_section_groups)
7848 && (elf_sec_group (isec) == NULL
7849 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7850 {
7851 if (elf_section_flags (isec) & SHF_GROUP)
7852 elf_section_flags (osec) |= SHF_GROUP;
7853 elf_next_in_group (osec) = elf_next_in_group (isec);
7854 elf_section_data (osec)->group = elf_section_data (isec)->group;
7855 }
7856
7857 /* If not decompress, preserve SHF_COMPRESSED. */
7858 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7859 elf_section_flags (osec) |= (elf_section_flags (isec)
7860 & SHF_COMPRESSED);
7861
7862 ihdr = &elf_section_data (isec)->this_hdr;
7863
7864 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7865 don't use the output section of the linked-to section since it
7866 may be NULL at this point. */
7867 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7868 {
7869 ohdr = &elf_section_data (osec)->this_hdr;
7870 ohdr->sh_flags |= SHF_LINK_ORDER;
7871 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7872 }
7873
7874 osec->use_rela_p = isec->use_rela_p;
7875
7876 return true;
7877 }
7878
7879 /* Copy private section information. This copies over the entsize
7880 field, and sometimes the info field. */
7881
7882 bool
7883 _bfd_elf_copy_private_section_data (bfd *ibfd,
7884 asection *isec,
7885 bfd *obfd,
7886 asection *osec)
7887 {
7888 Elf_Internal_Shdr *ihdr, *ohdr;
7889
7890 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7891 || obfd->xvec->flavour != bfd_target_elf_flavour)
7892 return true;
7893
7894 ihdr = &elf_section_data (isec)->this_hdr;
7895 ohdr = &elf_section_data (osec)->this_hdr;
7896
7897 ohdr->sh_entsize = ihdr->sh_entsize;
7898
7899 if (ihdr->sh_type == SHT_SYMTAB
7900 || ihdr->sh_type == SHT_DYNSYM
7901 || ihdr->sh_type == SHT_GNU_verneed
7902 || ihdr->sh_type == SHT_GNU_verdef)
7903 ohdr->sh_info = ihdr->sh_info;
7904
7905 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7906 NULL);
7907 }
7908
7909 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7910 necessary if we are removing either the SHT_GROUP section or any of
7911 the group member sections. DISCARDED is the value that a section's
7912 output_section has if the section will be discarded, NULL when this
7913 function is called from objcopy, bfd_abs_section_ptr when called
7914 from the linker. */
7915
7916 bool
7917 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7918 {
7919 asection *isec;
7920
7921 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7922 if (elf_section_type (isec) == SHT_GROUP)
7923 {
7924 asection *first = elf_next_in_group (isec);
7925 asection *s = first;
7926 bfd_size_type removed = 0;
7927
7928 while (s != NULL)
7929 {
7930 /* If this member section is being output but the
7931 SHT_GROUP section is not, then clear the group info
7932 set up by _bfd_elf_copy_private_section_data. */
7933 if (s->output_section != discarded
7934 && isec->output_section == discarded)
7935 {
7936 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7937 elf_group_name (s->output_section) = NULL;
7938 }
7939 else
7940 {
7941 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7942 if (s->output_section == discarded
7943 && isec->output_section != discarded)
7944 {
7945 /* Conversely, if the member section is not being
7946 output but the SHT_GROUP section is, then adjust
7947 its size. */
7948 removed += 4;
7949 if (elf_sec->rel.hdr != NULL
7950 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7951 removed += 4;
7952 if (elf_sec->rela.hdr != NULL
7953 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7954 removed += 4;
7955 }
7956 else
7957 {
7958 /* Also adjust for zero-sized relocation member
7959 section. */
7960 if (elf_sec->rel.hdr != NULL
7961 && elf_sec->rel.hdr->sh_size == 0)
7962 removed += 4;
7963 if (elf_sec->rela.hdr != NULL
7964 && elf_sec->rela.hdr->sh_size == 0)
7965 removed += 4;
7966 }
7967 }
7968 s = elf_next_in_group (s);
7969 if (s == first)
7970 break;
7971 }
7972 if (removed != 0)
7973 {
7974 if (discarded != NULL)
7975 {
7976 /* If we've been called for ld -r, then we need to
7977 adjust the input section size. */
7978 if (isec->rawsize == 0)
7979 isec->rawsize = isec->size;
7980 isec->size = isec->rawsize - removed;
7981 if (isec->size <= 4)
7982 {
7983 isec->size = 0;
7984 isec->flags |= SEC_EXCLUDE;
7985 }
7986 }
7987 else
7988 {
7989 /* Adjust the output section size when called from
7990 objcopy. */
7991 isec->output_section->size -= removed;
7992 if (isec->output_section->size <= 4)
7993 {
7994 isec->output_section->size = 0;
7995 isec->output_section->flags |= SEC_EXCLUDE;
7996 }
7997 }
7998 }
7999 }
8000
8001 return true;
8002 }
8003
8004 /* Copy private header information. */
8005
8006 bool
8007 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8008 {
8009 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8010 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8011 return true;
8012
8013 /* Copy over private BFD data if it has not already been copied.
8014 This must be done here, rather than in the copy_private_bfd_data
8015 entry point, because the latter is called after the section
8016 contents have been set, which means that the program headers have
8017 already been worked out. */
8018 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8019 {
8020 if (! copy_private_bfd_data (ibfd, obfd))
8021 return false;
8022 }
8023
8024 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8025 }
8026
8027 /* Copy private symbol information. If this symbol is in a section
8028 which we did not map into a BFD section, try to map the section
8029 index correctly. We use special macro definitions for the mapped
8030 section indices; these definitions are interpreted by the
8031 swap_out_syms function. */
8032
8033 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8034 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8035 #define MAP_STRTAB (SHN_HIOS + 3)
8036 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8037 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8038
8039 bool
8040 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8041 asymbol *isymarg,
8042 bfd *obfd,
8043 asymbol *osymarg)
8044 {
8045 elf_symbol_type *isym, *osym;
8046
8047 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8048 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8049 return true;
8050
8051 isym = elf_symbol_from (isymarg);
8052 osym = elf_symbol_from (osymarg);
8053
8054 if (isym != NULL
8055 && isym->internal_elf_sym.st_shndx != 0
8056 && osym != NULL
8057 && bfd_is_abs_section (isym->symbol.section))
8058 {
8059 unsigned int shndx;
8060
8061 shndx = isym->internal_elf_sym.st_shndx;
8062 if (shndx == elf_onesymtab (ibfd))
8063 shndx = MAP_ONESYMTAB;
8064 else if (shndx == elf_dynsymtab (ibfd))
8065 shndx = MAP_DYNSYMTAB;
8066 else if (shndx == elf_strtab_sec (ibfd))
8067 shndx = MAP_STRTAB;
8068 else if (shndx == elf_shstrtab_sec (ibfd))
8069 shndx = MAP_SHSTRTAB;
8070 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8071 shndx = MAP_SYM_SHNDX;
8072 osym->internal_elf_sym.st_shndx = shndx;
8073 }
8074
8075 return true;
8076 }
8077
8078 /* Swap out the symbols. */
8079
8080 static bool
8081 swap_out_syms (bfd *abfd,
8082 struct elf_strtab_hash **sttp,
8083 int relocatable_p,
8084 struct bfd_link_info *info)
8085 {
8086 const struct elf_backend_data *bed;
8087 unsigned int symcount;
8088 asymbol **syms;
8089 struct elf_strtab_hash *stt;
8090 Elf_Internal_Shdr *symtab_hdr;
8091 Elf_Internal_Shdr *symtab_shndx_hdr;
8092 Elf_Internal_Shdr *symstrtab_hdr;
8093 struct elf_sym_strtab *symstrtab;
8094 bfd_byte *outbound_syms;
8095 bfd_byte *outbound_shndx;
8096 unsigned long outbound_syms_index;
8097 unsigned long outbound_shndx_index;
8098 unsigned int idx;
8099 unsigned int num_locals;
8100 size_t amt;
8101 bool name_local_sections;
8102
8103 if (!elf_map_symbols (abfd, &num_locals))
8104 return false;
8105
8106 /* Dump out the symtabs. */
8107 stt = _bfd_elf_strtab_init ();
8108 if (stt == NULL)
8109 return false;
8110
8111 bed = get_elf_backend_data (abfd);
8112 symcount = bfd_get_symcount (abfd);
8113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8114 symtab_hdr->sh_type = SHT_SYMTAB;
8115 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8116 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8117 symtab_hdr->sh_info = num_locals + 1;
8118 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8119
8120 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8121 symstrtab_hdr->sh_type = SHT_STRTAB;
8122
8123 /* Allocate buffer to swap out the .strtab section. */
8124 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8125 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8126 {
8127 bfd_set_error (bfd_error_no_memory);
8128 _bfd_elf_strtab_free (stt);
8129 return false;
8130 }
8131
8132 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8133 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8134 {
8135 error_no_mem:
8136 bfd_set_error (bfd_error_no_memory);
8137 error_return:
8138 free (symstrtab);
8139 _bfd_elf_strtab_free (stt);
8140 return false;
8141 }
8142 symtab_hdr->contents = outbound_syms;
8143 outbound_syms_index = 0;
8144
8145 outbound_shndx = NULL;
8146 outbound_shndx_index = 0;
8147
8148 if (elf_symtab_shndx_list (abfd))
8149 {
8150 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8151 if (symtab_shndx_hdr->sh_name != 0)
8152 {
8153 if (_bfd_mul_overflow (symcount + 1,
8154 sizeof (Elf_External_Sym_Shndx), &amt))
8155 goto error_no_mem;
8156 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8157 if (outbound_shndx == NULL)
8158 goto error_return;
8159
8160 symtab_shndx_hdr->contents = outbound_shndx;
8161 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8162 symtab_shndx_hdr->sh_size = amt;
8163 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8164 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8165 }
8166 /* FIXME: What about any other headers in the list ? */
8167 }
8168
8169 /* Now generate the data (for "contents"). */
8170 {
8171 /* Fill in zeroth symbol and swap it out. */
8172 Elf_Internal_Sym sym;
8173 sym.st_name = 0;
8174 sym.st_value = 0;
8175 sym.st_size = 0;
8176 sym.st_info = 0;
8177 sym.st_other = 0;
8178 sym.st_shndx = SHN_UNDEF;
8179 sym.st_target_internal = 0;
8180 symstrtab[0].sym = sym;
8181 symstrtab[0].dest_index = outbound_syms_index;
8182 symstrtab[0].destshndx_index = outbound_shndx_index;
8183 outbound_syms_index++;
8184 if (outbound_shndx != NULL)
8185 outbound_shndx_index++;
8186 }
8187
8188 name_local_sections
8189 = (bed->elf_backend_name_local_section_symbols
8190 && bed->elf_backend_name_local_section_symbols (abfd));
8191
8192 syms = bfd_get_outsymbols (abfd);
8193 for (idx = 0; idx < symcount;)
8194 {
8195 Elf_Internal_Sym sym;
8196 bfd_vma value = syms[idx]->value;
8197 elf_symbol_type *type_ptr;
8198 flagword flags = syms[idx]->flags;
8199 int type;
8200
8201 if (!name_local_sections
8202 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8203 {
8204 /* Local section symbols have no name. */
8205 sym.st_name = (unsigned long) -1;
8206 }
8207 else
8208 {
8209 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8210 to get the final offset for st_name. */
8211 sym.st_name
8212 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8213 false);
8214 if (sym.st_name == (unsigned long) -1)
8215 goto error_return;
8216 }
8217
8218 type_ptr = elf_symbol_from (syms[idx]);
8219
8220 if ((flags & BSF_SECTION_SYM) == 0
8221 && bfd_is_com_section (syms[idx]->section))
8222 {
8223 /* ELF common symbols put the alignment into the `value' field,
8224 and the size into the `size' field. This is backwards from
8225 how BFD handles it, so reverse it here. */
8226 sym.st_size = value;
8227 if (type_ptr == NULL
8228 || type_ptr->internal_elf_sym.st_value == 0)
8229 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8230 else
8231 sym.st_value = type_ptr->internal_elf_sym.st_value;
8232 sym.st_shndx = _bfd_elf_section_from_bfd_section
8233 (abfd, syms[idx]->section);
8234 }
8235 else
8236 {
8237 asection *sec = syms[idx]->section;
8238 unsigned int shndx;
8239
8240 if (sec->output_section)
8241 {
8242 value += sec->output_offset;
8243 sec = sec->output_section;
8244 }
8245
8246 /* Don't add in the section vma for relocatable output. */
8247 if (! relocatable_p)
8248 value += sec->vma;
8249 sym.st_value = value;
8250 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8251
8252 if (bfd_is_abs_section (sec)
8253 && type_ptr != NULL
8254 && type_ptr->internal_elf_sym.st_shndx != 0)
8255 {
8256 /* This symbol is in a real ELF section which we did
8257 not create as a BFD section. Undo the mapping done
8258 by copy_private_symbol_data. */
8259 shndx = type_ptr->internal_elf_sym.st_shndx;
8260 switch (shndx)
8261 {
8262 case MAP_ONESYMTAB:
8263 shndx = elf_onesymtab (abfd);
8264 break;
8265 case MAP_DYNSYMTAB:
8266 shndx = elf_dynsymtab (abfd);
8267 break;
8268 case MAP_STRTAB:
8269 shndx = elf_strtab_sec (abfd);
8270 break;
8271 case MAP_SHSTRTAB:
8272 shndx = elf_shstrtab_sec (abfd);
8273 break;
8274 case MAP_SYM_SHNDX:
8275 if (elf_symtab_shndx_list (abfd))
8276 shndx = elf_symtab_shndx_list (abfd)->ndx;
8277 break;
8278 case SHN_COMMON:
8279 case SHN_ABS:
8280 shndx = SHN_ABS;
8281 break;
8282 default:
8283 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8284 {
8285 if (bed->symbol_section_index)
8286 shndx = bed->symbol_section_index (abfd, type_ptr);
8287 /* Otherwise just leave the index alone. */
8288 }
8289 else
8290 {
8291 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8292 _bfd_error_handler (_("%pB: \
8293 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8294 abfd, shndx);
8295 shndx = SHN_ABS;
8296 }
8297 break;
8298 }
8299 }
8300 else
8301 {
8302 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8303
8304 if (shndx == SHN_BAD)
8305 {
8306 asection *sec2;
8307
8308 /* Writing this would be a hell of a lot easier if
8309 we had some decent documentation on bfd, and
8310 knew what to expect of the library, and what to
8311 demand of applications. For example, it
8312 appears that `objcopy' might not set the
8313 section of a symbol to be a section that is
8314 actually in the output file. */
8315 sec2 = bfd_get_section_by_name (abfd, sec->name);
8316 if (sec2 != NULL)
8317 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8318 if (shndx == SHN_BAD)
8319 {
8320 /* xgettext:c-format */
8321 _bfd_error_handler
8322 (_("unable to find equivalent output section"
8323 " for symbol '%s' from section '%s'"),
8324 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8325 sec->name);
8326 bfd_set_error (bfd_error_invalid_operation);
8327 goto error_return;
8328 }
8329 }
8330 }
8331
8332 sym.st_shndx = shndx;
8333 }
8334
8335 if ((flags & BSF_THREAD_LOCAL) != 0)
8336 type = STT_TLS;
8337 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8338 type = STT_GNU_IFUNC;
8339 else if ((flags & BSF_FUNCTION) != 0)
8340 type = STT_FUNC;
8341 else if ((flags & BSF_OBJECT) != 0)
8342 type = STT_OBJECT;
8343 else if ((flags & BSF_RELC) != 0)
8344 type = STT_RELC;
8345 else if ((flags & BSF_SRELC) != 0)
8346 type = STT_SRELC;
8347 else
8348 type = STT_NOTYPE;
8349
8350 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8351 type = STT_TLS;
8352
8353 /* Processor-specific types. */
8354 if (type_ptr != NULL
8355 && bed->elf_backend_get_symbol_type)
8356 type = ((*bed->elf_backend_get_symbol_type)
8357 (&type_ptr->internal_elf_sym, type));
8358
8359 if (flags & BSF_SECTION_SYM)
8360 {
8361 if (flags & BSF_GLOBAL)
8362 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8363 else
8364 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8365 }
8366 else if (bfd_is_com_section (syms[idx]->section))
8367 {
8368 if (type != STT_TLS)
8369 {
8370 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8371 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8372 ? STT_COMMON : STT_OBJECT);
8373 else
8374 type = ((flags & BSF_ELF_COMMON) != 0
8375 ? STT_COMMON : STT_OBJECT);
8376 }
8377 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8378 }
8379 else if (bfd_is_und_section (syms[idx]->section))
8380 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8381 ? STB_WEAK
8382 : STB_GLOBAL),
8383 type);
8384 else if (flags & BSF_FILE)
8385 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8386 else
8387 {
8388 int bind = STB_LOCAL;
8389
8390 if (flags & BSF_LOCAL)
8391 bind = STB_LOCAL;
8392 else if (flags & BSF_GNU_UNIQUE)
8393 bind = STB_GNU_UNIQUE;
8394 else if (flags & BSF_WEAK)
8395 bind = STB_WEAK;
8396 else if (flags & BSF_GLOBAL)
8397 bind = STB_GLOBAL;
8398
8399 sym.st_info = ELF_ST_INFO (bind, type);
8400 }
8401
8402 if (type_ptr != NULL)
8403 {
8404 sym.st_other = type_ptr->internal_elf_sym.st_other;
8405 sym.st_target_internal
8406 = type_ptr->internal_elf_sym.st_target_internal;
8407 }
8408 else
8409 {
8410 sym.st_other = 0;
8411 sym.st_target_internal = 0;
8412 }
8413
8414 idx++;
8415 symstrtab[idx].sym = sym;
8416 symstrtab[idx].dest_index = outbound_syms_index;
8417 symstrtab[idx].destshndx_index = outbound_shndx_index;
8418
8419 outbound_syms_index++;
8420 if (outbound_shndx != NULL)
8421 outbound_shndx_index++;
8422 }
8423
8424 /* Finalize the .strtab section. */
8425 _bfd_elf_strtab_finalize (stt);
8426
8427 /* Swap out the .strtab section. */
8428 for (idx = 0; idx <= symcount; idx++)
8429 {
8430 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8431 if (elfsym->sym.st_name == (unsigned long) -1)
8432 elfsym->sym.st_name = 0;
8433 else
8434 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8435 elfsym->sym.st_name);
8436 if (info && info->callbacks->ctf_new_symbol)
8437 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8438 &elfsym->sym);
8439
8440 /* Inform the linker of the addition of this symbol. */
8441
8442 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8443 (outbound_syms
8444 + (elfsym->dest_index
8445 * bed->s->sizeof_sym)),
8446 (outbound_shndx
8447 + (elfsym->destshndx_index
8448 * sizeof (Elf_External_Sym_Shndx))));
8449 }
8450 free (symstrtab);
8451
8452 *sttp = stt;
8453 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8454 symstrtab_hdr->sh_type = SHT_STRTAB;
8455 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8456 symstrtab_hdr->sh_addr = 0;
8457 symstrtab_hdr->sh_entsize = 0;
8458 symstrtab_hdr->sh_link = 0;
8459 symstrtab_hdr->sh_info = 0;
8460 symstrtab_hdr->sh_addralign = 1;
8461
8462 return true;
8463 }
8464
8465 /* Return the number of bytes required to hold the symtab vector.
8466
8467 Note that we base it on the count plus 1, since we will null terminate
8468 the vector allocated based on this size. However, the ELF symbol table
8469 always has a dummy entry as symbol #0, so it ends up even. */
8470
8471 long
8472 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8473 {
8474 bfd_size_type symcount;
8475 long symtab_size;
8476 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8477
8478 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8479 if (symcount > LONG_MAX / sizeof (asymbol *))
8480 {
8481 bfd_set_error (bfd_error_file_too_big);
8482 return -1;
8483 }
8484 symtab_size = symcount * (sizeof (asymbol *));
8485 if (symcount == 0)
8486 symtab_size = sizeof (asymbol *);
8487 else if (!bfd_write_p (abfd))
8488 {
8489 ufile_ptr filesize = bfd_get_file_size (abfd);
8490
8491 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8492 {
8493 bfd_set_error (bfd_error_file_truncated);
8494 return -1;
8495 }
8496 }
8497
8498 return symtab_size;
8499 }
8500
8501 long
8502 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8503 {
8504 bfd_size_type symcount;
8505 long symtab_size;
8506 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8507
8508 if (elf_dynsymtab (abfd) == 0)
8509 {
8510 bfd_set_error (bfd_error_invalid_operation);
8511 return -1;
8512 }
8513
8514 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8515 if (symcount > LONG_MAX / sizeof (asymbol *))
8516 {
8517 bfd_set_error (bfd_error_file_too_big);
8518 return -1;
8519 }
8520 symtab_size = symcount * (sizeof (asymbol *));
8521 if (symcount == 0)
8522 symtab_size = sizeof (asymbol *);
8523 else if (!bfd_write_p (abfd))
8524 {
8525 ufile_ptr filesize = bfd_get_file_size (abfd);
8526
8527 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8528 {
8529 bfd_set_error (bfd_error_file_truncated);
8530 return -1;
8531 }
8532 }
8533
8534 return symtab_size;
8535 }
8536
8537 long
8538 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8539 {
8540 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8541 {
8542 /* Sanity check reloc section size. */
8543 struct bfd_elf_section_data *d = elf_section_data (asect);
8544 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8545 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8546 ufile_ptr filesize = bfd_get_file_size (abfd);
8547
8548 if (filesize != 0 && ext_rel_size > filesize)
8549 {
8550 bfd_set_error (bfd_error_file_truncated);
8551 return -1;
8552 }
8553 }
8554
8555 #if SIZEOF_LONG == SIZEOF_INT
8556 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8557 {
8558 bfd_set_error (bfd_error_file_too_big);
8559 return -1;
8560 }
8561 #endif
8562 return (asect->reloc_count + 1) * sizeof (arelent *);
8563 }
8564
8565 /* Canonicalize the relocs. */
8566
8567 long
8568 _bfd_elf_canonicalize_reloc (bfd *abfd,
8569 sec_ptr section,
8570 arelent **relptr,
8571 asymbol **symbols)
8572 {
8573 arelent *tblptr;
8574 unsigned int i;
8575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8576
8577 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8578 return -1;
8579
8580 tblptr = section->relocation;
8581 for (i = 0; i < section->reloc_count; i++)
8582 *relptr++ = tblptr++;
8583
8584 *relptr = NULL;
8585
8586 return section->reloc_count;
8587 }
8588
8589 long
8590 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8591 {
8592 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8593 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8594
8595 if (symcount >= 0)
8596 abfd->symcount = symcount;
8597 return symcount;
8598 }
8599
8600 long
8601 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8602 asymbol **allocation)
8603 {
8604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8605 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8606
8607 if (symcount >= 0)
8608 abfd->dynsymcount = symcount;
8609 return symcount;
8610 }
8611
8612 /* Return the size required for the dynamic reloc entries. Any loadable
8613 section that was actually installed in the BFD, and has type SHT_REL
8614 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8615 dynamic reloc section. */
8616
8617 long
8618 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8619 {
8620 bfd_size_type count, ext_rel_size;
8621 asection *s;
8622
8623 if (elf_dynsymtab (abfd) == 0)
8624 {
8625 bfd_set_error (bfd_error_invalid_operation);
8626 return -1;
8627 }
8628
8629 count = 1;
8630 ext_rel_size = 0;
8631 for (s = abfd->sections; s != NULL; s = s->next)
8632 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8633 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8634 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8635 {
8636 ext_rel_size += s->size;
8637 if (ext_rel_size < s->size)
8638 {
8639 bfd_set_error (bfd_error_file_truncated);
8640 return -1;
8641 }
8642 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8643 if (count > LONG_MAX / sizeof (arelent *))
8644 {
8645 bfd_set_error (bfd_error_file_too_big);
8646 return -1;
8647 }
8648 }
8649 if (count > 1 && !bfd_write_p (abfd))
8650 {
8651 /* Sanity check reloc section sizes. */
8652 ufile_ptr filesize = bfd_get_file_size (abfd);
8653 if (filesize != 0 && ext_rel_size > filesize)
8654 {
8655 bfd_set_error (bfd_error_file_truncated);
8656 return -1;
8657 }
8658 }
8659 return count * sizeof (arelent *);
8660 }
8661
8662 /* Canonicalize the dynamic relocation entries. Note that we return the
8663 dynamic relocations as a single block, although they are actually
8664 associated with particular sections; the interface, which was
8665 designed for SunOS style shared libraries, expects that there is only
8666 one set of dynamic relocs. Any loadable section that was actually
8667 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8668 dynamic symbol table, is considered to be a dynamic reloc section. */
8669
8670 long
8671 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8672 arelent **storage,
8673 asymbol **syms)
8674 {
8675 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8676 asection *s;
8677 long ret;
8678
8679 if (elf_dynsymtab (abfd) == 0)
8680 {
8681 bfd_set_error (bfd_error_invalid_operation);
8682 return -1;
8683 }
8684
8685 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8686 ret = 0;
8687 for (s = abfd->sections; s != NULL; s = s->next)
8688 {
8689 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8690 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8691 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8692 {
8693 arelent *p;
8694 long count, i;
8695
8696 if (! (*slurp_relocs) (abfd, s, syms, true))
8697 return -1;
8698 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8699 p = s->relocation;
8700 for (i = 0; i < count; i++)
8701 *storage++ = p++;
8702 ret += count;
8703 }
8704 }
8705
8706 *storage = NULL;
8707
8708 return ret;
8709 }
8710 \f
8711 /* Read in the version information. */
8712
8713 bool
8714 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8715 {
8716 bfd_byte *contents = NULL;
8717 unsigned int freeidx = 0;
8718 size_t amt;
8719
8720 if (elf_dynverref (abfd) != 0)
8721 {
8722 Elf_Internal_Shdr *hdr;
8723 Elf_External_Verneed *everneed;
8724 Elf_Internal_Verneed *iverneed;
8725 unsigned int i;
8726 bfd_byte *contents_end;
8727
8728 hdr = &elf_tdata (abfd)->dynverref_hdr;
8729
8730 if (hdr->sh_info == 0
8731 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8732 {
8733 error_return_bad_verref:
8734 _bfd_error_handler
8735 (_("%pB: .gnu.version_r invalid entry"), abfd);
8736 bfd_set_error (bfd_error_bad_value);
8737 error_return_verref:
8738 elf_tdata (abfd)->verref = NULL;
8739 elf_tdata (abfd)->cverrefs = 0;
8740 goto error_return;
8741 }
8742
8743 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8744 goto error_return_verref;
8745 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8746 if (contents == NULL)
8747 goto error_return_verref;
8748
8749 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8750 {
8751 bfd_set_error (bfd_error_file_too_big);
8752 goto error_return_verref;
8753 }
8754 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
8755 if (elf_tdata (abfd)->verref == NULL)
8756 goto error_return_verref;
8757
8758 BFD_ASSERT (sizeof (Elf_External_Verneed)
8759 == sizeof (Elf_External_Vernaux));
8760 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8761 everneed = (Elf_External_Verneed *) contents;
8762 iverneed = elf_tdata (abfd)->verref;
8763 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8764 {
8765 Elf_External_Vernaux *evernaux;
8766 Elf_Internal_Vernaux *ivernaux;
8767 unsigned int j;
8768
8769 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8770
8771 iverneed->vn_bfd = abfd;
8772
8773 iverneed->vn_filename =
8774 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8775 iverneed->vn_file);
8776 if (iverneed->vn_filename == NULL)
8777 goto error_return_bad_verref;
8778
8779 if (iverneed->vn_cnt == 0)
8780 iverneed->vn_auxptr = NULL;
8781 else
8782 {
8783 if (_bfd_mul_overflow (iverneed->vn_cnt,
8784 sizeof (Elf_Internal_Vernaux), &amt))
8785 {
8786 bfd_set_error (bfd_error_file_too_big);
8787 goto error_return_verref;
8788 }
8789 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8790 bfd_alloc (abfd, amt);
8791 if (iverneed->vn_auxptr == NULL)
8792 goto error_return_verref;
8793 }
8794
8795 if (iverneed->vn_aux
8796 > (size_t) (contents_end - (bfd_byte *) everneed))
8797 goto error_return_bad_verref;
8798
8799 evernaux = ((Elf_External_Vernaux *)
8800 ((bfd_byte *) everneed + iverneed->vn_aux));
8801 ivernaux = iverneed->vn_auxptr;
8802 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8803 {
8804 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8805
8806 ivernaux->vna_nodename =
8807 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8808 ivernaux->vna_name);
8809 if (ivernaux->vna_nodename == NULL)
8810 goto error_return_bad_verref;
8811
8812 if (ivernaux->vna_other > freeidx)
8813 freeidx = ivernaux->vna_other;
8814
8815 ivernaux->vna_nextptr = NULL;
8816 if (ivernaux->vna_next == 0)
8817 {
8818 iverneed->vn_cnt = j + 1;
8819 break;
8820 }
8821 if (j + 1 < iverneed->vn_cnt)
8822 ivernaux->vna_nextptr = ivernaux + 1;
8823
8824 if (ivernaux->vna_next
8825 > (size_t) (contents_end - (bfd_byte *) evernaux))
8826 goto error_return_bad_verref;
8827
8828 evernaux = ((Elf_External_Vernaux *)
8829 ((bfd_byte *) evernaux + ivernaux->vna_next));
8830 }
8831
8832 iverneed->vn_nextref = NULL;
8833 if (iverneed->vn_next == 0)
8834 break;
8835 if (i + 1 < hdr->sh_info)
8836 iverneed->vn_nextref = iverneed + 1;
8837
8838 if (iverneed->vn_next
8839 > (size_t) (contents_end - (bfd_byte *) everneed))
8840 goto error_return_bad_verref;
8841
8842 everneed = ((Elf_External_Verneed *)
8843 ((bfd_byte *) everneed + iverneed->vn_next));
8844 }
8845 elf_tdata (abfd)->cverrefs = i;
8846
8847 free (contents);
8848 contents = NULL;
8849 }
8850
8851 if (elf_dynverdef (abfd) != 0)
8852 {
8853 Elf_Internal_Shdr *hdr;
8854 Elf_External_Verdef *everdef;
8855 Elf_Internal_Verdef *iverdef;
8856 Elf_Internal_Verdef *iverdefarr;
8857 Elf_Internal_Verdef iverdefmem;
8858 unsigned int i;
8859 unsigned int maxidx;
8860 bfd_byte *contents_end_def, *contents_end_aux;
8861
8862 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8863
8864 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8865 {
8866 error_return_bad_verdef:
8867 _bfd_error_handler
8868 (_("%pB: .gnu.version_d invalid entry"), abfd);
8869 bfd_set_error (bfd_error_bad_value);
8870 error_return_verdef:
8871 elf_tdata (abfd)->verdef = NULL;
8872 elf_tdata (abfd)->cverdefs = 0;
8873 goto error_return;
8874 }
8875
8876 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8877 goto error_return_verdef;
8878 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8879 if (contents == NULL)
8880 goto error_return_verdef;
8881
8882 BFD_ASSERT (sizeof (Elf_External_Verdef)
8883 >= sizeof (Elf_External_Verdaux));
8884 contents_end_def = contents + hdr->sh_size
8885 - sizeof (Elf_External_Verdef);
8886 contents_end_aux = contents + hdr->sh_size
8887 - sizeof (Elf_External_Verdaux);
8888
8889 /* We know the number of entries in the section but not the maximum
8890 index. Therefore we have to run through all entries and find
8891 the maximum. */
8892 everdef = (Elf_External_Verdef *) contents;
8893 maxidx = 0;
8894 for (i = 0; i < hdr->sh_info; ++i)
8895 {
8896 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8897
8898 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8899 goto error_return_bad_verdef;
8900 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8901 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8902
8903 if (iverdefmem.vd_next == 0)
8904 break;
8905
8906 if (iverdefmem.vd_next
8907 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8908 goto error_return_bad_verdef;
8909
8910 everdef = ((Elf_External_Verdef *)
8911 ((bfd_byte *) everdef + iverdefmem.vd_next));
8912 }
8913
8914 if (default_imported_symver)
8915 {
8916 if (freeidx > maxidx)
8917 maxidx = ++freeidx;
8918 else
8919 freeidx = ++maxidx;
8920 }
8921 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
8922 {
8923 bfd_set_error (bfd_error_file_too_big);
8924 goto error_return_verdef;
8925 }
8926 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
8927 if (elf_tdata (abfd)->verdef == NULL)
8928 goto error_return_verdef;
8929
8930 elf_tdata (abfd)->cverdefs = maxidx;
8931
8932 everdef = (Elf_External_Verdef *) contents;
8933 iverdefarr = elf_tdata (abfd)->verdef;
8934 for (i = 0; i < hdr->sh_info; i++)
8935 {
8936 Elf_External_Verdaux *everdaux;
8937 Elf_Internal_Verdaux *iverdaux;
8938 unsigned int j;
8939
8940 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8941
8942 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8943 goto error_return_bad_verdef;
8944
8945 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8946 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8947
8948 iverdef->vd_bfd = abfd;
8949
8950 if (iverdef->vd_cnt == 0)
8951 iverdef->vd_auxptr = NULL;
8952 else
8953 {
8954 if (_bfd_mul_overflow (iverdef->vd_cnt,
8955 sizeof (Elf_Internal_Verdaux), &amt))
8956 {
8957 bfd_set_error (bfd_error_file_too_big);
8958 goto error_return_verdef;
8959 }
8960 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8961 bfd_alloc (abfd, amt);
8962 if (iverdef->vd_auxptr == NULL)
8963 goto error_return_verdef;
8964 }
8965
8966 if (iverdef->vd_aux
8967 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8968 goto error_return_bad_verdef;
8969
8970 everdaux = ((Elf_External_Verdaux *)
8971 ((bfd_byte *) everdef + iverdef->vd_aux));
8972 iverdaux = iverdef->vd_auxptr;
8973 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8974 {
8975 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8976
8977 iverdaux->vda_nodename =
8978 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8979 iverdaux->vda_name);
8980 if (iverdaux->vda_nodename == NULL)
8981 goto error_return_bad_verdef;
8982
8983 iverdaux->vda_nextptr = NULL;
8984 if (iverdaux->vda_next == 0)
8985 {
8986 iverdef->vd_cnt = j + 1;
8987 break;
8988 }
8989 if (j + 1 < iverdef->vd_cnt)
8990 iverdaux->vda_nextptr = iverdaux + 1;
8991
8992 if (iverdaux->vda_next
8993 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8994 goto error_return_bad_verdef;
8995
8996 everdaux = ((Elf_External_Verdaux *)
8997 ((bfd_byte *) everdaux + iverdaux->vda_next));
8998 }
8999
9000 iverdef->vd_nodename = NULL;
9001 if (iverdef->vd_cnt)
9002 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9003
9004 iverdef->vd_nextdef = NULL;
9005 if (iverdef->vd_next == 0)
9006 break;
9007 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9008 iverdef->vd_nextdef = iverdef + 1;
9009
9010 everdef = ((Elf_External_Verdef *)
9011 ((bfd_byte *) everdef + iverdef->vd_next));
9012 }
9013
9014 free (contents);
9015 contents = NULL;
9016 }
9017 else if (default_imported_symver)
9018 {
9019 if (freeidx < 3)
9020 freeidx = 3;
9021 else
9022 freeidx++;
9023
9024 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9025 {
9026 bfd_set_error (bfd_error_file_too_big);
9027 goto error_return;
9028 }
9029 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9030 if (elf_tdata (abfd)->verdef == NULL)
9031 goto error_return;
9032
9033 elf_tdata (abfd)->cverdefs = freeidx;
9034 }
9035
9036 /* Create a default version based on the soname. */
9037 if (default_imported_symver)
9038 {
9039 Elf_Internal_Verdef *iverdef;
9040 Elf_Internal_Verdaux *iverdaux;
9041
9042 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9043
9044 iverdef->vd_version = VER_DEF_CURRENT;
9045 iverdef->vd_flags = 0;
9046 iverdef->vd_ndx = freeidx;
9047 iverdef->vd_cnt = 1;
9048
9049 iverdef->vd_bfd = abfd;
9050
9051 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9052 if (iverdef->vd_nodename == NULL)
9053 goto error_return_verdef;
9054 iverdef->vd_nextdef = NULL;
9055 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9056 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9057 if (iverdef->vd_auxptr == NULL)
9058 goto error_return_verdef;
9059
9060 iverdaux = iverdef->vd_auxptr;
9061 iverdaux->vda_nodename = iverdef->vd_nodename;
9062 }
9063
9064 return true;
9065
9066 error_return:
9067 free (contents);
9068 return false;
9069 }
9070 \f
9071 asymbol *
9072 _bfd_elf_make_empty_symbol (bfd *abfd)
9073 {
9074 elf_symbol_type *newsym;
9075
9076 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9077 if (!newsym)
9078 return NULL;
9079 newsym->symbol.the_bfd = abfd;
9080 return &newsym->symbol;
9081 }
9082
9083 void
9084 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9085 asymbol *symbol,
9086 symbol_info *ret)
9087 {
9088 bfd_symbol_info (symbol, ret);
9089 }
9090
9091 /* Return whether a symbol name implies a local symbol. Most targets
9092 use this function for the is_local_label_name entry point, but some
9093 override it. */
9094
9095 bool
9096 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9097 const char *name)
9098 {
9099 /* Normal local symbols start with ``.L''. */
9100 if (name[0] == '.' && name[1] == 'L')
9101 return true;
9102
9103 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9104 DWARF debugging symbols starting with ``..''. */
9105 if (name[0] == '.' && name[1] == '.')
9106 return true;
9107
9108 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9109 emitting DWARF debugging output. I suspect this is actually a
9110 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9111 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9112 underscore to be emitted on some ELF targets). For ease of use,
9113 we treat such symbols as local. */
9114 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9115 return true;
9116
9117 /* Treat assembler generated fake symbols, dollar local labels and
9118 forward-backward labels (aka local labels) as locals.
9119 These labels have the form:
9120
9121 L0^A.* (fake symbols)
9122
9123 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9124
9125 Versions which start with .L will have already been matched above,
9126 so we only need to match the rest. */
9127 if (name[0] == 'L' && ISDIGIT (name[1]))
9128 {
9129 bool ret = false;
9130 const char * p;
9131 char c;
9132
9133 for (p = name + 2; (c = *p); p++)
9134 {
9135 if (c == 1 || c == 2)
9136 {
9137 if (c == 1 && p == name + 2)
9138 /* A fake symbol. */
9139 return true;
9140
9141 /* FIXME: We are being paranoid here and treating symbols like
9142 L0^Bfoo as if there were non-local, on the grounds that the
9143 assembler will never generate them. But can any symbol
9144 containing an ASCII value in the range 1-31 ever be anything
9145 other than some kind of local ? */
9146 ret = true;
9147 }
9148
9149 if (! ISDIGIT (c))
9150 {
9151 ret = false;
9152 break;
9153 }
9154 }
9155 return ret;
9156 }
9157
9158 return false;
9159 }
9160
9161 alent *
9162 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9163 asymbol *symbol ATTRIBUTE_UNUSED)
9164 {
9165 abort ();
9166 return NULL;
9167 }
9168
9169 bool
9170 _bfd_elf_set_arch_mach (bfd *abfd,
9171 enum bfd_architecture arch,
9172 unsigned long machine)
9173 {
9174 /* If this isn't the right architecture for this backend, and this
9175 isn't the generic backend, fail. */
9176 if (arch != get_elf_backend_data (abfd)->arch
9177 && arch != bfd_arch_unknown
9178 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9179 return false;
9180
9181 return bfd_default_set_arch_mach (abfd, arch, machine);
9182 }
9183
9184 /* Find the nearest line to a particular section and offset,
9185 for error reporting. */
9186
9187 bool
9188 _bfd_elf_find_nearest_line (bfd *abfd,
9189 asymbol **symbols,
9190 asection *section,
9191 bfd_vma offset,
9192 const char **filename_ptr,
9193 const char **functionname_ptr,
9194 unsigned int *line_ptr,
9195 unsigned int *discriminator_ptr)
9196 {
9197 bool found;
9198
9199 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
9200 filename_ptr, functionname_ptr,
9201 line_ptr, discriminator_ptr,
9202 dwarf_debug_sections,
9203 &elf_tdata (abfd)->dwarf2_find_line_info))
9204 return true;
9205
9206 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9207 filename_ptr, functionname_ptr, line_ptr))
9208 {
9209 if (!*functionname_ptr)
9210 _bfd_elf_find_function (abfd, symbols, section, offset,
9211 *filename_ptr ? NULL : filename_ptr,
9212 functionname_ptr);
9213 return true;
9214 }
9215
9216 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9217 &found, filename_ptr,
9218 functionname_ptr, line_ptr,
9219 &elf_tdata (abfd)->line_info))
9220 return false;
9221 if (found && (*functionname_ptr || *line_ptr))
9222 return true;
9223
9224 if (symbols == NULL)
9225 return false;
9226
9227 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9228 filename_ptr, functionname_ptr))
9229 return false;
9230
9231 *line_ptr = 0;
9232 return true;
9233 }
9234
9235 /* Find the line for a symbol. */
9236
9237 bool
9238 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9239 const char **filename_ptr, unsigned int *line_ptr)
9240 {
9241 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9242 filename_ptr, NULL, line_ptr, NULL,
9243 dwarf_debug_sections,
9244 &elf_tdata (abfd)->dwarf2_find_line_info);
9245 }
9246
9247 /* After a call to bfd_find_nearest_line, successive calls to
9248 bfd_find_inliner_info can be used to get source information about
9249 each level of function inlining that terminated at the address
9250 passed to bfd_find_nearest_line. Currently this is only supported
9251 for DWARF2 with appropriate DWARF3 extensions. */
9252
9253 bool
9254 _bfd_elf_find_inliner_info (bfd *abfd,
9255 const char **filename_ptr,
9256 const char **functionname_ptr,
9257 unsigned int *line_ptr)
9258 {
9259 bool found;
9260 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9261 functionname_ptr, line_ptr,
9262 & elf_tdata (abfd)->dwarf2_find_line_info);
9263 return found;
9264 }
9265
9266 int
9267 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9268 {
9269 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9270 int ret = bed->s->sizeof_ehdr;
9271
9272 if (!bfd_link_relocatable (info))
9273 {
9274 bfd_size_type phdr_size = elf_program_header_size (abfd);
9275
9276 if (phdr_size == (bfd_size_type) -1)
9277 {
9278 struct elf_segment_map *m;
9279
9280 phdr_size = 0;
9281 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9282 phdr_size += bed->s->sizeof_phdr;
9283
9284 if (phdr_size == 0)
9285 phdr_size = get_program_header_size (abfd, info);
9286 }
9287
9288 elf_program_header_size (abfd) = phdr_size;
9289 ret += phdr_size;
9290 }
9291
9292 return ret;
9293 }
9294
9295 bool
9296 _bfd_elf_set_section_contents (bfd *abfd,
9297 sec_ptr section,
9298 const void *location,
9299 file_ptr offset,
9300 bfd_size_type count)
9301 {
9302 Elf_Internal_Shdr *hdr;
9303 file_ptr pos;
9304
9305 if (! abfd->output_has_begun
9306 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9307 return false;
9308
9309 if (!count)
9310 return true;
9311
9312 hdr = &elf_section_data (section)->this_hdr;
9313 if (hdr->sh_offset == (file_ptr) -1)
9314 {
9315 unsigned char *contents;
9316
9317 if (bfd_section_is_ctf (section))
9318 /* Nothing to do with this section: the contents are generated
9319 later. */
9320 return true;
9321
9322 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9323 {
9324 _bfd_error_handler
9325 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9326 abfd, section);
9327 bfd_set_error (bfd_error_invalid_operation);
9328 return false;
9329 }
9330
9331 if ((offset + count) > hdr->sh_size)
9332 {
9333 _bfd_error_handler
9334 (_("%pB:%pA: error: attempting to write over the end of the section"),
9335 abfd, section);
9336
9337 bfd_set_error (bfd_error_invalid_operation);
9338 return false;
9339 }
9340
9341 contents = hdr->contents;
9342 if (contents == NULL)
9343 {
9344 _bfd_error_handler
9345 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9346 abfd, section);
9347
9348 bfd_set_error (bfd_error_invalid_operation);
9349 return false;
9350 }
9351
9352 memcpy (contents + offset, location, count);
9353 return true;
9354 }
9355
9356 pos = hdr->sh_offset + offset;
9357 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9358 || bfd_bwrite (location, count, abfd) != count)
9359 return false;
9360
9361 return true;
9362 }
9363
9364 bool
9365 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9366 arelent *cache_ptr ATTRIBUTE_UNUSED,
9367 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9368 {
9369 abort ();
9370 return false;
9371 }
9372
9373 /* Try to convert a non-ELF reloc into an ELF one. */
9374
9375 bool
9376 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9377 {
9378 /* Check whether we really have an ELF howto. */
9379
9380 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9381 {
9382 bfd_reloc_code_real_type code;
9383 reloc_howto_type *howto;
9384
9385 /* Alien reloc: Try to determine its type to replace it with an
9386 equivalent ELF reloc. */
9387
9388 if (areloc->howto->pc_relative)
9389 {
9390 switch (areloc->howto->bitsize)
9391 {
9392 case 8:
9393 code = BFD_RELOC_8_PCREL;
9394 break;
9395 case 12:
9396 code = BFD_RELOC_12_PCREL;
9397 break;
9398 case 16:
9399 code = BFD_RELOC_16_PCREL;
9400 break;
9401 case 24:
9402 code = BFD_RELOC_24_PCREL;
9403 break;
9404 case 32:
9405 code = BFD_RELOC_32_PCREL;
9406 break;
9407 case 64:
9408 code = BFD_RELOC_64_PCREL;
9409 break;
9410 default:
9411 goto fail;
9412 }
9413
9414 howto = bfd_reloc_type_lookup (abfd, code);
9415
9416 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9417 {
9418 if (howto->pcrel_offset)
9419 areloc->addend += areloc->address;
9420 else
9421 areloc->addend -= areloc->address; /* addend is unsigned!! */
9422 }
9423 }
9424 else
9425 {
9426 switch (areloc->howto->bitsize)
9427 {
9428 case 8:
9429 code = BFD_RELOC_8;
9430 break;
9431 case 14:
9432 code = BFD_RELOC_14;
9433 break;
9434 case 16:
9435 code = BFD_RELOC_16;
9436 break;
9437 case 26:
9438 code = BFD_RELOC_26;
9439 break;
9440 case 32:
9441 code = BFD_RELOC_32;
9442 break;
9443 case 64:
9444 code = BFD_RELOC_64;
9445 break;
9446 default:
9447 goto fail;
9448 }
9449
9450 howto = bfd_reloc_type_lookup (abfd, code);
9451 }
9452
9453 if (howto)
9454 areloc->howto = howto;
9455 else
9456 goto fail;
9457 }
9458
9459 return true;
9460
9461 fail:
9462 /* xgettext:c-format */
9463 _bfd_error_handler (_("%pB: %s unsupported"),
9464 abfd, areloc->howto->name);
9465 bfd_set_error (bfd_error_sorry);
9466 return false;
9467 }
9468
9469 bool
9470 _bfd_elf_close_and_cleanup (bfd *abfd)
9471 {
9472 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9473 if (tdata != NULL
9474 && (bfd_get_format (abfd) == bfd_object
9475 || bfd_get_format (abfd) == bfd_core))
9476 {
9477 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9478 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9479 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9480 }
9481
9482 return _bfd_generic_close_and_cleanup (abfd);
9483 }
9484
9485 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9486 in the relocation's offset. Thus we cannot allow any sort of sanity
9487 range-checking to interfere. There is nothing else to do in processing
9488 this reloc. */
9489
9490 bfd_reloc_status_type
9491 _bfd_elf_rel_vtable_reloc_fn
9492 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9493 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9494 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9495 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9496 {
9497 return bfd_reloc_ok;
9498 }
9499 \f
9500 /* Elf core file support. Much of this only works on native
9501 toolchains, since we rely on knowing the
9502 machine-dependent procfs structure in order to pick
9503 out details about the corefile. */
9504
9505 #ifdef HAVE_SYS_PROCFS_H
9506 # include <sys/procfs.h>
9507 #endif
9508
9509 /* Return a PID that identifies a "thread" for threaded cores, or the
9510 PID of the main process for non-threaded cores. */
9511
9512 static int
9513 elfcore_make_pid (bfd *abfd)
9514 {
9515 int pid;
9516
9517 pid = elf_tdata (abfd)->core->lwpid;
9518 if (pid == 0)
9519 pid = elf_tdata (abfd)->core->pid;
9520
9521 return pid;
9522 }
9523
9524 /* If there isn't a section called NAME, make one, using
9525 data from SECT. Note, this function will generate a
9526 reference to NAME, so you shouldn't deallocate or
9527 overwrite it. */
9528
9529 static bool
9530 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9531 {
9532 asection *sect2;
9533
9534 if (bfd_get_section_by_name (abfd, name) != NULL)
9535 return true;
9536
9537 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9538 if (sect2 == NULL)
9539 return false;
9540
9541 sect2->size = sect->size;
9542 sect2->filepos = sect->filepos;
9543 sect2->alignment_power = sect->alignment_power;
9544 return true;
9545 }
9546
9547 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9548 actually creates up to two pseudosections:
9549 - For the single-threaded case, a section named NAME, unless
9550 such a section already exists.
9551 - For the multi-threaded case, a section named "NAME/PID", where
9552 PID is elfcore_make_pid (abfd).
9553 Both pseudosections have identical contents. */
9554 bool
9555 _bfd_elfcore_make_pseudosection (bfd *abfd,
9556 char *name,
9557 size_t size,
9558 ufile_ptr filepos)
9559 {
9560 char buf[100];
9561 char *threaded_name;
9562 size_t len;
9563 asection *sect;
9564
9565 /* Build the section name. */
9566
9567 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9568 len = strlen (buf) + 1;
9569 threaded_name = (char *) bfd_alloc (abfd, len);
9570 if (threaded_name == NULL)
9571 return false;
9572 memcpy (threaded_name, buf, len);
9573
9574 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9575 SEC_HAS_CONTENTS);
9576 if (sect == NULL)
9577 return false;
9578 sect->size = size;
9579 sect->filepos = filepos;
9580 sect->alignment_power = 2;
9581
9582 return elfcore_maybe_make_sect (abfd, name, sect);
9583 }
9584
9585 static bool
9586 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9587 size_t offs)
9588 {
9589 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9590 SEC_HAS_CONTENTS);
9591
9592 if (sect == NULL)
9593 return false;
9594
9595 sect->size = note->descsz - offs;
9596 sect->filepos = note->descpos + offs;
9597 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9598
9599 return true;
9600 }
9601
9602 static bool
9603 elfcore_make_memtag_note_section (bfd *abfd, Elf_Internal_Note *note,
9604 size_t offs)
9605 {
9606 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".memtag",
9607 SEC_HAS_CONTENTS);
9608
9609 if (sect == NULL)
9610 return false;
9611
9612 sect->size = note->descsz - offs;
9613 sect->filepos = note->descpos + offs;
9614 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9615
9616 return true;
9617 }
9618
9619 /* prstatus_t exists on:
9620 solaris 2.5+
9621 linux 2.[01] + glibc
9622 unixware 4.2
9623 */
9624
9625 #if defined (HAVE_PRSTATUS_T)
9626
9627 static bool
9628 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9629 {
9630 size_t size;
9631 int offset;
9632
9633 if (note->descsz == sizeof (prstatus_t))
9634 {
9635 prstatus_t prstat;
9636
9637 size = sizeof (prstat.pr_reg);
9638 offset = offsetof (prstatus_t, pr_reg);
9639 memcpy (&prstat, note->descdata, sizeof (prstat));
9640
9641 /* Do not overwrite the core signal if it
9642 has already been set by another thread. */
9643 if (elf_tdata (abfd)->core->signal == 0)
9644 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9645 if (elf_tdata (abfd)->core->pid == 0)
9646 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9647
9648 /* pr_who exists on:
9649 solaris 2.5+
9650 unixware 4.2
9651 pr_who doesn't exist on:
9652 linux 2.[01]
9653 */
9654 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9655 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9656 #else
9657 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9658 #endif
9659 }
9660 #if defined (HAVE_PRSTATUS32_T)
9661 else if (note->descsz == sizeof (prstatus32_t))
9662 {
9663 /* 64-bit host, 32-bit corefile */
9664 prstatus32_t prstat;
9665
9666 size = sizeof (prstat.pr_reg);
9667 offset = offsetof (prstatus32_t, pr_reg);
9668 memcpy (&prstat, note->descdata, sizeof (prstat));
9669
9670 /* Do not overwrite the core signal if it
9671 has already been set by another thread. */
9672 if (elf_tdata (abfd)->core->signal == 0)
9673 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9674 if (elf_tdata (abfd)->core->pid == 0)
9675 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9676
9677 /* pr_who exists on:
9678 solaris 2.5+
9679 unixware 4.2
9680 pr_who doesn't exist on:
9681 linux 2.[01]
9682 */
9683 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9684 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9685 #else
9686 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9687 #endif
9688 }
9689 #endif /* HAVE_PRSTATUS32_T */
9690 else
9691 {
9692 /* Fail - we don't know how to handle any other
9693 note size (ie. data object type). */
9694 return true;
9695 }
9696
9697 /* Make a ".reg/999" section and a ".reg" section. */
9698 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9699 size, note->descpos + offset);
9700 }
9701 #endif /* defined (HAVE_PRSTATUS_T) */
9702
9703 /* Create a pseudosection containing the exact contents of NOTE. */
9704 static bool
9705 elfcore_make_note_pseudosection (bfd *abfd,
9706 char *name,
9707 Elf_Internal_Note *note)
9708 {
9709 return _bfd_elfcore_make_pseudosection (abfd, name,
9710 note->descsz, note->descpos);
9711 }
9712
9713 /* There isn't a consistent prfpregset_t across platforms,
9714 but it doesn't matter, because we don't have to pick this
9715 data structure apart. */
9716
9717 static bool
9718 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9719 {
9720 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9721 }
9722
9723 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9724 type of NT_PRXFPREG. Just include the whole note's contents
9725 literally. */
9726
9727 static bool
9728 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9729 {
9730 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9731 }
9732
9733 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9734 with a note type of NT_X86_XSTATE. Just include the whole note's
9735 contents literally. */
9736
9737 static bool
9738 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9739 {
9740 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9741 }
9742
9743 static bool
9744 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9745 {
9746 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9747 }
9748
9749 static bool
9750 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9751 {
9752 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9753 }
9754
9755 static bool
9756 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9757 {
9758 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9759 }
9760
9761 static bool
9762 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9763 {
9764 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9765 }
9766
9767 static bool
9768 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9769 {
9770 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9771 }
9772
9773 static bool
9774 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9775 {
9776 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9777 }
9778
9779 static bool
9780 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9781 {
9782 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9783 }
9784
9785 static bool
9786 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9787 {
9788 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9789 }
9790
9791 static bool
9792 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9793 {
9794 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9795 }
9796
9797 static bool
9798 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9799 {
9800 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9801 }
9802
9803 static bool
9804 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9805 {
9806 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9807 }
9808
9809 static bool
9810 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9811 {
9812 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9813 }
9814
9815 static bool
9816 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9817 {
9818 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9819 }
9820
9821 static bool
9822 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9823 {
9824 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9825 }
9826
9827 static bool
9828 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9829 {
9830 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9831 }
9832
9833 static bool
9834 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9835 {
9836 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9837 }
9838
9839 static bool
9840 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9841 {
9842 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9843 }
9844
9845 static bool
9846 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9847 {
9848 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9849 }
9850
9851 static bool
9852 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9853 {
9854 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9855 }
9856
9857 static bool
9858 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9859 {
9860 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9861 }
9862
9863 static bool
9864 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9865 {
9866 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9867 }
9868
9869 static bool
9870 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9871 {
9872 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9873 }
9874
9875 static bool
9876 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9877 {
9878 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9879 }
9880
9881 static bool
9882 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9883 {
9884 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9885 }
9886
9887 static bool
9888 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9889 {
9890 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9891 }
9892
9893 static bool
9894 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9895 {
9896 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9897 }
9898
9899 static bool
9900 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9901 {
9902 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9903 }
9904
9905 static bool
9906 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9907 {
9908 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9909 }
9910
9911 static bool
9912 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9913 {
9914 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9915 }
9916
9917 static bool
9918 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9919 {
9920 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9921 }
9922
9923 static bool
9924 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9925 {
9926 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9927 }
9928
9929 static bool
9930 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9931 {
9932 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9933 }
9934
9935 static bool
9936 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9937 {
9938 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9939 }
9940
9941 static bool
9942 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9943 {
9944 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9945 }
9946
9947 static bool
9948 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
9949 {
9950 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
9951 note);
9952 }
9953
9954 static bool
9955 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
9956 {
9957 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
9958 }
9959
9960 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
9961 successful otherwise, return FALSE. */
9962
9963 static bool
9964 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
9965 {
9966 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
9967 }
9968
9969 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
9970 successful otherwise, return FALSE. */
9971
9972 static bool
9973 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
9974 {
9975 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
9976 }
9977
9978 #if defined (HAVE_PRPSINFO_T)
9979 typedef prpsinfo_t elfcore_psinfo_t;
9980 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9981 typedef prpsinfo32_t elfcore_psinfo32_t;
9982 #endif
9983 #endif
9984
9985 #if defined (HAVE_PSINFO_T)
9986 typedef psinfo_t elfcore_psinfo_t;
9987 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9988 typedef psinfo32_t elfcore_psinfo32_t;
9989 #endif
9990 #endif
9991
9992 /* return a malloc'ed copy of a string at START which is at
9993 most MAX bytes long, possibly without a terminating '\0'.
9994 the copy will always have a terminating '\0'. */
9995
9996 char *
9997 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9998 {
9999 char *dups;
10000 char *end = (char *) memchr (start, '\0', max);
10001 size_t len;
10002
10003 if (end == NULL)
10004 len = max;
10005 else
10006 len = end - start;
10007
10008 dups = (char *) bfd_alloc (abfd, len + 1);
10009 if (dups == NULL)
10010 return NULL;
10011
10012 memcpy (dups, start, len);
10013 dups[len] = '\0';
10014
10015 return dups;
10016 }
10017
10018 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10019 static bool
10020 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10021 {
10022 if (note->descsz == sizeof (elfcore_psinfo_t))
10023 {
10024 elfcore_psinfo_t psinfo;
10025
10026 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10027
10028 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10029 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10030 #endif
10031 elf_tdata (abfd)->core->program
10032 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10033 sizeof (psinfo.pr_fname));
10034
10035 elf_tdata (abfd)->core->command
10036 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10037 sizeof (psinfo.pr_psargs));
10038 }
10039 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10040 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10041 {
10042 /* 64-bit host, 32-bit corefile */
10043 elfcore_psinfo32_t psinfo;
10044
10045 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10046
10047 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10048 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10049 #endif
10050 elf_tdata (abfd)->core->program
10051 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10052 sizeof (psinfo.pr_fname));
10053
10054 elf_tdata (abfd)->core->command
10055 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10056 sizeof (psinfo.pr_psargs));
10057 }
10058 #endif
10059
10060 else
10061 {
10062 /* Fail - we don't know how to handle any other
10063 note size (ie. data object type). */
10064 return true;
10065 }
10066
10067 /* Note that for some reason, a spurious space is tacked
10068 onto the end of the args in some (at least one anyway)
10069 implementations, so strip it off if it exists. */
10070
10071 {
10072 char *command = elf_tdata (abfd)->core->command;
10073 int n = strlen (command);
10074
10075 if (0 < n && command[n - 1] == ' ')
10076 command[n - 1] = '\0';
10077 }
10078
10079 return true;
10080 }
10081 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10082
10083 #if defined (HAVE_PSTATUS_T)
10084 static bool
10085 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10086 {
10087 if (note->descsz == sizeof (pstatus_t)
10088 #if defined (HAVE_PXSTATUS_T)
10089 || note->descsz == sizeof (pxstatus_t)
10090 #endif
10091 )
10092 {
10093 pstatus_t pstat;
10094
10095 memcpy (&pstat, note->descdata, sizeof (pstat));
10096
10097 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10098 }
10099 #if defined (HAVE_PSTATUS32_T)
10100 else if (note->descsz == sizeof (pstatus32_t))
10101 {
10102 /* 64-bit host, 32-bit corefile */
10103 pstatus32_t pstat;
10104
10105 memcpy (&pstat, note->descdata, sizeof (pstat));
10106
10107 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10108 }
10109 #endif
10110 /* Could grab some more details from the "representative"
10111 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10112 NT_LWPSTATUS note, presumably. */
10113
10114 return true;
10115 }
10116 #endif /* defined (HAVE_PSTATUS_T) */
10117
10118 #if defined (HAVE_LWPSTATUS_T)
10119 static bool
10120 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10121 {
10122 lwpstatus_t lwpstat;
10123 char buf[100];
10124 char *name;
10125 size_t len;
10126 asection *sect;
10127
10128 if (note->descsz != sizeof (lwpstat)
10129 #if defined (HAVE_LWPXSTATUS_T)
10130 && note->descsz != sizeof (lwpxstatus_t)
10131 #endif
10132 )
10133 return true;
10134
10135 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10136
10137 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10138 /* Do not overwrite the core signal if it has already been set by
10139 another thread. */
10140 if (elf_tdata (abfd)->core->signal == 0)
10141 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10142
10143 /* Make a ".reg/999" section. */
10144
10145 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10146 len = strlen (buf) + 1;
10147 name = bfd_alloc (abfd, len);
10148 if (name == NULL)
10149 return false;
10150 memcpy (name, buf, len);
10151
10152 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10153 if (sect == NULL)
10154 return false;
10155
10156 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10157 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10158 sect->filepos = note->descpos
10159 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10160 #endif
10161
10162 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10163 sect->size = sizeof (lwpstat.pr_reg);
10164 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10165 #endif
10166
10167 sect->alignment_power = 2;
10168
10169 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10170 return false;
10171
10172 /* Make a ".reg2/999" section */
10173
10174 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10175 len = strlen (buf) + 1;
10176 name = bfd_alloc (abfd, len);
10177 if (name == NULL)
10178 return false;
10179 memcpy (name, buf, len);
10180
10181 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10182 if (sect == NULL)
10183 return false;
10184
10185 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10186 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10187 sect->filepos = note->descpos
10188 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10189 #endif
10190
10191 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10192 sect->size = sizeof (lwpstat.pr_fpreg);
10193 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10194 #endif
10195
10196 sect->alignment_power = 2;
10197
10198 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10199 }
10200 #endif /* defined (HAVE_LWPSTATUS_T) */
10201
10202 /* These constants, and the structure offsets used below, are defined by
10203 Cygwin's core_dump.h */
10204 #define NOTE_INFO_PROCESS 1
10205 #define NOTE_INFO_THREAD 2
10206 #define NOTE_INFO_MODULE 3
10207 #define NOTE_INFO_MODULE64 4
10208
10209 static bool
10210 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10211 {
10212 char buf[30];
10213 char *name;
10214 size_t len;
10215 unsigned int name_size;
10216 asection *sect;
10217 unsigned int type;
10218 int is_active_thread;
10219 bfd_vma base_addr;
10220
10221 if (note->descsz < 4)
10222 return true;
10223
10224 if (! startswith (note->namedata, "win32"))
10225 return true;
10226
10227 type = bfd_get_32 (abfd, note->descdata);
10228
10229 struct
10230 {
10231 const char *type_name;
10232 unsigned long min_size;
10233 } size_check[] =
10234 {
10235 { "NOTE_INFO_PROCESS", 12 },
10236 { "NOTE_INFO_THREAD", 12 },
10237 { "NOTE_INFO_MODULE", 12 },
10238 { "NOTE_INFO_MODULE64", 16 },
10239 };
10240
10241 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10242 return true;
10243
10244 if (note->descsz < size_check[type - 1].min_size)
10245 {
10246 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10247 abfd, size_check[type - 1].type_name, note->descsz);
10248 return true;
10249 }
10250
10251 switch (type)
10252 {
10253 case NOTE_INFO_PROCESS:
10254 /* FIXME: need to add ->core->command. */
10255 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10256 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10257 break;
10258
10259 case NOTE_INFO_THREAD:
10260 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10261 structure. */
10262 /* thread_info.tid */
10263 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10264
10265 len = strlen (buf) + 1;
10266 name = (char *) bfd_alloc (abfd, len);
10267 if (name == NULL)
10268 return false;
10269
10270 memcpy (name, buf, len);
10271
10272 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10273 if (sect == NULL)
10274 return false;
10275
10276 /* sizeof (thread_info.thread_context) */
10277 sect->size = note->descsz - 12;
10278 /* offsetof (thread_info.thread_context) */
10279 sect->filepos = note->descpos + 12;
10280 sect->alignment_power = 2;
10281
10282 /* thread_info.is_active_thread */
10283 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10284
10285 if (is_active_thread)
10286 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10287 return false;
10288 break;
10289
10290 case NOTE_INFO_MODULE:
10291 case NOTE_INFO_MODULE64:
10292 /* Make a ".module/xxxxxxxx" section. */
10293 if (type == NOTE_INFO_MODULE)
10294 {
10295 /* module_info.base_address */
10296 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10297 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10298 /* module_info.module_name_size */
10299 name_size = bfd_get_32 (abfd, note->descdata + 8);
10300 }
10301 else /* NOTE_INFO_MODULE64 */
10302 {
10303 /* module_info.base_address */
10304 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10305 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10306 /* module_info.module_name_size */
10307 name_size = bfd_get_32 (abfd, note->descdata + 12);
10308 }
10309
10310 len = strlen (buf) + 1;
10311 name = (char *) bfd_alloc (abfd, len);
10312 if (name == NULL)
10313 return false;
10314
10315 memcpy (name, buf, len);
10316
10317 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10318
10319 if (sect == NULL)
10320 return false;
10321
10322 if (note->descsz < 12 + name_size)
10323 {
10324 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
10325 abfd, note->descsz, name_size);
10326 return true;
10327 }
10328
10329 sect->size = note->descsz;
10330 sect->filepos = note->descpos;
10331 sect->alignment_power = 2;
10332 break;
10333
10334 default:
10335 return true;
10336 }
10337
10338 return true;
10339 }
10340
10341 static bool
10342 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10343 {
10344 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10345
10346 switch (note->type)
10347 {
10348 default:
10349 return true;
10350
10351 case NT_PRSTATUS:
10352 if (bed->elf_backend_grok_prstatus)
10353 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10354 return true;
10355 #if defined (HAVE_PRSTATUS_T)
10356 return elfcore_grok_prstatus (abfd, note);
10357 #else
10358 return true;
10359 #endif
10360
10361 #if defined (HAVE_PSTATUS_T)
10362 case NT_PSTATUS:
10363 return elfcore_grok_pstatus (abfd, note);
10364 #endif
10365
10366 #if defined (HAVE_LWPSTATUS_T)
10367 case NT_LWPSTATUS:
10368 return elfcore_grok_lwpstatus (abfd, note);
10369 #endif
10370
10371 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10372 return elfcore_grok_prfpreg (abfd, note);
10373
10374 case NT_WIN32PSTATUS:
10375 return elfcore_grok_win32pstatus (abfd, note);
10376
10377 case NT_PRXFPREG: /* Linux SSE extension */
10378 if (note->namesz == 6
10379 && strcmp (note->namedata, "LINUX") == 0)
10380 return elfcore_grok_prxfpreg (abfd, note);
10381 else
10382 return true;
10383
10384 case NT_X86_XSTATE: /* Linux XSAVE extension */
10385 if (note->namesz == 6
10386 && strcmp (note->namedata, "LINUX") == 0)
10387 return elfcore_grok_xstatereg (abfd, note);
10388 else
10389 return true;
10390
10391 case NT_PPC_VMX:
10392 if (note->namesz == 6
10393 && strcmp (note->namedata, "LINUX") == 0)
10394 return elfcore_grok_ppc_vmx (abfd, note);
10395 else
10396 return true;
10397
10398 case NT_PPC_VSX:
10399 if (note->namesz == 6
10400 && strcmp (note->namedata, "LINUX") == 0)
10401 return elfcore_grok_ppc_vsx (abfd, note);
10402 else
10403 return true;
10404
10405 case NT_PPC_TAR:
10406 if (note->namesz == 6
10407 && strcmp (note->namedata, "LINUX") == 0)
10408 return elfcore_grok_ppc_tar (abfd, note);
10409 else
10410 return true;
10411
10412 case NT_PPC_PPR:
10413 if (note->namesz == 6
10414 && strcmp (note->namedata, "LINUX") == 0)
10415 return elfcore_grok_ppc_ppr (abfd, note);
10416 else
10417 return true;
10418
10419 case NT_PPC_DSCR:
10420 if (note->namesz == 6
10421 && strcmp (note->namedata, "LINUX") == 0)
10422 return elfcore_grok_ppc_dscr (abfd, note);
10423 else
10424 return true;
10425
10426 case NT_PPC_EBB:
10427 if (note->namesz == 6
10428 && strcmp (note->namedata, "LINUX") == 0)
10429 return elfcore_grok_ppc_ebb (abfd, note);
10430 else
10431 return true;
10432
10433 case NT_PPC_PMU:
10434 if (note->namesz == 6
10435 && strcmp (note->namedata, "LINUX") == 0)
10436 return elfcore_grok_ppc_pmu (abfd, note);
10437 else
10438 return true;
10439
10440 case NT_PPC_TM_CGPR:
10441 if (note->namesz == 6
10442 && strcmp (note->namedata, "LINUX") == 0)
10443 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10444 else
10445 return true;
10446
10447 case NT_PPC_TM_CFPR:
10448 if (note->namesz == 6
10449 && strcmp (note->namedata, "LINUX") == 0)
10450 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10451 else
10452 return true;
10453
10454 case NT_PPC_TM_CVMX:
10455 if (note->namesz == 6
10456 && strcmp (note->namedata, "LINUX") == 0)
10457 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10458 else
10459 return true;
10460
10461 case NT_PPC_TM_CVSX:
10462 if (note->namesz == 6
10463 && strcmp (note->namedata, "LINUX") == 0)
10464 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10465 else
10466 return true;
10467
10468 case NT_PPC_TM_SPR:
10469 if (note->namesz == 6
10470 && strcmp (note->namedata, "LINUX") == 0)
10471 return elfcore_grok_ppc_tm_spr (abfd, note);
10472 else
10473 return true;
10474
10475 case NT_PPC_TM_CTAR:
10476 if (note->namesz == 6
10477 && strcmp (note->namedata, "LINUX") == 0)
10478 return elfcore_grok_ppc_tm_ctar (abfd, note);
10479 else
10480 return true;
10481
10482 case NT_PPC_TM_CPPR:
10483 if (note->namesz == 6
10484 && strcmp (note->namedata, "LINUX") == 0)
10485 return elfcore_grok_ppc_tm_cppr (abfd, note);
10486 else
10487 return true;
10488
10489 case NT_PPC_TM_CDSCR:
10490 if (note->namesz == 6
10491 && strcmp (note->namedata, "LINUX") == 0)
10492 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10493 else
10494 return true;
10495
10496 case NT_S390_HIGH_GPRS:
10497 if (note->namesz == 6
10498 && strcmp (note->namedata, "LINUX") == 0)
10499 return elfcore_grok_s390_high_gprs (abfd, note);
10500 else
10501 return true;
10502
10503 case NT_S390_TIMER:
10504 if (note->namesz == 6
10505 && strcmp (note->namedata, "LINUX") == 0)
10506 return elfcore_grok_s390_timer (abfd, note);
10507 else
10508 return true;
10509
10510 case NT_S390_TODCMP:
10511 if (note->namesz == 6
10512 && strcmp (note->namedata, "LINUX") == 0)
10513 return elfcore_grok_s390_todcmp (abfd, note);
10514 else
10515 return true;
10516
10517 case NT_S390_TODPREG:
10518 if (note->namesz == 6
10519 && strcmp (note->namedata, "LINUX") == 0)
10520 return elfcore_grok_s390_todpreg (abfd, note);
10521 else
10522 return true;
10523
10524 case NT_S390_CTRS:
10525 if (note->namesz == 6
10526 && strcmp (note->namedata, "LINUX") == 0)
10527 return elfcore_grok_s390_ctrs (abfd, note);
10528 else
10529 return true;
10530
10531 case NT_S390_PREFIX:
10532 if (note->namesz == 6
10533 && strcmp (note->namedata, "LINUX") == 0)
10534 return elfcore_grok_s390_prefix (abfd, note);
10535 else
10536 return true;
10537
10538 case NT_S390_LAST_BREAK:
10539 if (note->namesz == 6
10540 && strcmp (note->namedata, "LINUX") == 0)
10541 return elfcore_grok_s390_last_break (abfd, note);
10542 else
10543 return true;
10544
10545 case NT_S390_SYSTEM_CALL:
10546 if (note->namesz == 6
10547 && strcmp (note->namedata, "LINUX") == 0)
10548 return elfcore_grok_s390_system_call (abfd, note);
10549 else
10550 return true;
10551
10552 case NT_S390_TDB:
10553 if (note->namesz == 6
10554 && strcmp (note->namedata, "LINUX") == 0)
10555 return elfcore_grok_s390_tdb (abfd, note);
10556 else
10557 return true;
10558
10559 case NT_S390_VXRS_LOW:
10560 if (note->namesz == 6
10561 && strcmp (note->namedata, "LINUX") == 0)
10562 return elfcore_grok_s390_vxrs_low (abfd, note);
10563 else
10564 return true;
10565
10566 case NT_S390_VXRS_HIGH:
10567 if (note->namesz == 6
10568 && strcmp (note->namedata, "LINUX") == 0)
10569 return elfcore_grok_s390_vxrs_high (abfd, note);
10570 else
10571 return true;
10572
10573 case NT_S390_GS_CB:
10574 if (note->namesz == 6
10575 && strcmp (note->namedata, "LINUX") == 0)
10576 return elfcore_grok_s390_gs_cb (abfd, note);
10577 else
10578 return true;
10579
10580 case NT_S390_GS_BC:
10581 if (note->namesz == 6
10582 && strcmp (note->namedata, "LINUX") == 0)
10583 return elfcore_grok_s390_gs_bc (abfd, note);
10584 else
10585 return true;
10586
10587 case NT_ARC_V2:
10588 if (note->namesz == 6
10589 && strcmp (note->namedata, "LINUX") == 0)
10590 return elfcore_grok_arc_v2 (abfd, note);
10591 else
10592 return true;
10593
10594 case NT_ARM_VFP:
10595 if (note->namesz == 6
10596 && strcmp (note->namedata, "LINUX") == 0)
10597 return elfcore_grok_arm_vfp (abfd, note);
10598 else
10599 return true;
10600
10601 case NT_ARM_TLS:
10602 if (note->namesz == 6
10603 && strcmp (note->namedata, "LINUX") == 0)
10604 return elfcore_grok_aarch_tls (abfd, note);
10605 else
10606 return true;
10607
10608 case NT_ARM_HW_BREAK:
10609 if (note->namesz == 6
10610 && strcmp (note->namedata, "LINUX") == 0)
10611 return elfcore_grok_aarch_hw_break (abfd, note);
10612 else
10613 return true;
10614
10615 case NT_ARM_HW_WATCH:
10616 if (note->namesz == 6
10617 && strcmp (note->namedata, "LINUX") == 0)
10618 return elfcore_grok_aarch_hw_watch (abfd, note);
10619 else
10620 return true;
10621
10622 case NT_ARM_SVE:
10623 if (note->namesz == 6
10624 && strcmp (note->namedata, "LINUX") == 0)
10625 return elfcore_grok_aarch_sve (abfd, note);
10626 else
10627 return true;
10628
10629 case NT_ARM_PAC_MASK:
10630 if (note->namesz == 6
10631 && strcmp (note->namedata, "LINUX") == 0)
10632 return elfcore_grok_aarch_pauth (abfd, note);
10633 else
10634 return true;
10635
10636 case NT_ARM_TAGGED_ADDR_CTRL:
10637 if (note->namesz == 6
10638 && strcmp (note->namedata, "LINUX") == 0)
10639 return elfcore_grok_aarch_mte (abfd, note);
10640 else
10641 return true;
10642
10643 case NT_GDB_TDESC:
10644 if (note->namesz == 4
10645 && strcmp (note->namedata, "GDB") == 0)
10646 return elfcore_grok_gdb_tdesc (abfd, note);
10647 else
10648 return true;
10649
10650 case NT_RISCV_CSR:
10651 if (note->namesz == 4
10652 && strcmp (note->namedata, "GDB") == 0)
10653 return elfcore_grok_riscv_csr (abfd, note);
10654 else
10655 return true;
10656
10657 case NT_PRPSINFO:
10658 case NT_PSINFO:
10659 if (bed->elf_backend_grok_psinfo)
10660 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10661 return true;
10662 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10663 return elfcore_grok_psinfo (abfd, note);
10664 #else
10665 return true;
10666 #endif
10667
10668 case NT_AUXV:
10669 return elfcore_make_auxv_note_section (abfd, note, 0);
10670
10671 case NT_FILE:
10672 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10673 note);
10674
10675 case NT_SIGINFO:
10676 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10677 note);
10678
10679 case NT_MEMTAG:
10680 return elfcore_make_memtag_note_section (abfd, note, 0);
10681 }
10682 }
10683
10684 static bool
10685 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10686 {
10687 struct bfd_build_id* build_id;
10688
10689 if (note->descsz == 0)
10690 return false;
10691
10692 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10693 if (build_id == NULL)
10694 return false;
10695
10696 build_id->size = note->descsz;
10697 memcpy (build_id->data, note->descdata, note->descsz);
10698 abfd->build_id = build_id;
10699
10700 return true;
10701 }
10702
10703 static bool
10704 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10705 {
10706 switch (note->type)
10707 {
10708 default:
10709 return true;
10710
10711 case NT_GNU_PROPERTY_TYPE_0:
10712 return _bfd_elf_parse_gnu_properties (abfd, note);
10713
10714 case NT_GNU_BUILD_ID:
10715 return elfobj_grok_gnu_build_id (abfd, note);
10716 }
10717 }
10718
10719 static bool
10720 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10721 {
10722 struct sdt_note *cur =
10723 (struct sdt_note *) bfd_alloc (abfd,
10724 sizeof (struct sdt_note) + note->descsz);
10725
10726 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10727 cur->size = (bfd_size_type) note->descsz;
10728 memcpy (cur->data, note->descdata, note->descsz);
10729
10730 elf_tdata (abfd)->sdt_note_head = cur;
10731
10732 return true;
10733 }
10734
10735 static bool
10736 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10737 {
10738 switch (note->type)
10739 {
10740 case NT_STAPSDT:
10741 return elfobj_grok_stapsdt_note_1 (abfd, note);
10742
10743 default:
10744 return true;
10745 }
10746 }
10747
10748 static bool
10749 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10750 {
10751 size_t offset;
10752
10753 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10754 {
10755 case ELFCLASS32:
10756 if (note->descsz < 108)
10757 return false;
10758 break;
10759
10760 case ELFCLASS64:
10761 if (note->descsz < 120)
10762 return false;
10763 break;
10764
10765 default:
10766 return false;
10767 }
10768
10769 /* Check for version 1 in pr_version. */
10770 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10771 return false;
10772
10773 offset = 4;
10774
10775 /* Skip over pr_psinfosz. */
10776 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10777 offset += 4;
10778 else
10779 {
10780 offset += 4; /* Padding before pr_psinfosz. */
10781 offset += 8;
10782 }
10783
10784 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10785 elf_tdata (abfd)->core->program
10786 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10787 offset += 17;
10788
10789 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10790 elf_tdata (abfd)->core->command
10791 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10792 offset += 81;
10793
10794 /* Padding before pr_pid. */
10795 offset += 2;
10796
10797 /* The pr_pid field was added in version "1a". */
10798 if (note->descsz < offset + 4)
10799 return true;
10800
10801 elf_tdata (abfd)->core->pid
10802 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10803
10804 return true;
10805 }
10806
10807 static bool
10808 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10809 {
10810 size_t offset;
10811 size_t size;
10812 size_t min_size;
10813
10814 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10815 Also compute minimum size of this note. */
10816 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10817 {
10818 case ELFCLASS32:
10819 offset = 4 + 4;
10820 min_size = offset + (4 * 2) + 4 + 4 + 4;
10821 break;
10822
10823 case ELFCLASS64:
10824 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10825 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10826 break;
10827
10828 default:
10829 return false;
10830 }
10831
10832 if (note->descsz < min_size)
10833 return false;
10834
10835 /* Check for version 1 in pr_version. */
10836 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10837 return false;
10838
10839 /* Extract size of pr_reg from pr_gregsetsz. */
10840 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10841 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10842 {
10843 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10844 offset += 4 * 2;
10845 }
10846 else
10847 {
10848 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10849 offset += 8 * 2;
10850 }
10851
10852 /* Skip over pr_osreldate. */
10853 offset += 4;
10854
10855 /* Read signal from pr_cursig. */
10856 if (elf_tdata (abfd)->core->signal == 0)
10857 elf_tdata (abfd)->core->signal
10858 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10859 offset += 4;
10860
10861 /* Read TID from pr_pid. */
10862 elf_tdata (abfd)->core->lwpid
10863 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10864 offset += 4;
10865
10866 /* Padding before pr_reg. */
10867 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10868 offset += 4;
10869
10870 /* Make sure that there is enough data remaining in the note. */
10871 if ((note->descsz - offset) < size)
10872 return false;
10873
10874 /* Make a ".reg/999" section and a ".reg" section. */
10875 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10876 size, note->descpos + offset);
10877 }
10878
10879 static bool
10880 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10881 {
10882 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10883
10884 switch (note->type)
10885 {
10886 case NT_PRSTATUS:
10887 if (bed->elf_backend_grok_freebsd_prstatus)
10888 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10889 return true;
10890 return elfcore_grok_freebsd_prstatus (abfd, note);
10891
10892 case NT_FPREGSET:
10893 return elfcore_grok_prfpreg (abfd, note);
10894
10895 case NT_PRPSINFO:
10896 return elfcore_grok_freebsd_psinfo (abfd, note);
10897
10898 case NT_FREEBSD_THRMISC:
10899 if (note->namesz == 8)
10900 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10901 else
10902 return true;
10903
10904 case NT_FREEBSD_PROCSTAT_PROC:
10905 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10906 note);
10907
10908 case NT_FREEBSD_PROCSTAT_FILES:
10909 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10910 note);
10911
10912 case NT_FREEBSD_PROCSTAT_VMMAP:
10913 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10914 note);
10915
10916 case NT_FREEBSD_PROCSTAT_AUXV:
10917 return elfcore_make_auxv_note_section (abfd, note, 4);
10918
10919 case NT_X86_XSTATE:
10920 if (note->namesz == 8)
10921 return elfcore_grok_xstatereg (abfd, note);
10922 else
10923 return true;
10924
10925 case NT_FREEBSD_PTLWPINFO:
10926 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10927 note);
10928
10929 case NT_ARM_VFP:
10930 return elfcore_grok_arm_vfp (abfd, note);
10931
10932 default:
10933 return true;
10934 }
10935 }
10936
10937 static bool
10938 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10939 {
10940 char *cp;
10941
10942 cp = strchr (note->namedata, '@');
10943 if (cp != NULL)
10944 {
10945 *lwpidp = atoi(cp + 1);
10946 return true;
10947 }
10948 return false;
10949 }
10950
10951 static bool
10952 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10953 {
10954 if (note->descsz <= 0x7c + 31)
10955 return false;
10956
10957 /* Signal number at offset 0x08. */
10958 elf_tdata (abfd)->core->signal
10959 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10960
10961 /* Process ID at offset 0x50. */
10962 elf_tdata (abfd)->core->pid
10963 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10964
10965 /* Command name at 0x7c (max 32 bytes, including nul). */
10966 elf_tdata (abfd)->core->command
10967 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10968
10969 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10970 note);
10971 }
10972
10973 static bool
10974 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10975 {
10976 int lwp;
10977
10978 if (elfcore_netbsd_get_lwpid (note, &lwp))
10979 elf_tdata (abfd)->core->lwpid = lwp;
10980
10981 switch (note->type)
10982 {
10983 case NT_NETBSDCORE_PROCINFO:
10984 /* NetBSD-specific core "procinfo". Note that we expect to
10985 find this note before any of the others, which is fine,
10986 since the kernel writes this note out first when it
10987 creates a core file. */
10988 return elfcore_grok_netbsd_procinfo (abfd, note);
10989 case NT_NETBSDCORE_AUXV:
10990 /* NetBSD-specific Elf Auxiliary Vector data. */
10991 return elfcore_make_auxv_note_section (abfd, note, 4);
10992 case NT_NETBSDCORE_LWPSTATUS:
10993 return elfcore_make_note_pseudosection (abfd,
10994 ".note.netbsdcore.lwpstatus",
10995 note);
10996 default:
10997 break;
10998 }
10999
11000 /* As of March 2020 there are no other machine-independent notes
11001 defined for NetBSD core files. If the note type is less
11002 than the start of the machine-dependent note types, we don't
11003 understand it. */
11004
11005 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11006 return true;
11007
11008
11009 switch (bfd_get_arch (abfd))
11010 {
11011 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11012 PT_GETFPREGS == mach+2. */
11013
11014 case bfd_arch_aarch64:
11015 case bfd_arch_alpha:
11016 case bfd_arch_sparc:
11017 switch (note->type)
11018 {
11019 case NT_NETBSDCORE_FIRSTMACH+0:
11020 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11021
11022 case NT_NETBSDCORE_FIRSTMACH+2:
11023 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11024
11025 default:
11026 return true;
11027 }
11028
11029 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11030 There's also old PT___GETREGS40 == mach + 1 for old reg
11031 structure which lacks GBR. */
11032
11033 case bfd_arch_sh:
11034 switch (note->type)
11035 {
11036 case NT_NETBSDCORE_FIRSTMACH+3:
11037 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11038
11039 case NT_NETBSDCORE_FIRSTMACH+5:
11040 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11041
11042 default:
11043 return true;
11044 }
11045
11046 /* On all other arch's, PT_GETREGS == mach+1 and
11047 PT_GETFPREGS == mach+3. */
11048
11049 default:
11050 switch (note->type)
11051 {
11052 case NT_NETBSDCORE_FIRSTMACH+1:
11053 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11054
11055 case NT_NETBSDCORE_FIRSTMACH+3:
11056 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11057
11058 default:
11059 return true;
11060 }
11061 }
11062 /* NOTREACHED */
11063 }
11064
11065 static bool
11066 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11067 {
11068 if (note->descsz <= 0x48 + 31)
11069 return false;
11070
11071 /* Signal number at offset 0x08. */
11072 elf_tdata (abfd)->core->signal
11073 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11074
11075 /* Process ID at offset 0x20. */
11076 elf_tdata (abfd)->core->pid
11077 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11078
11079 /* Command name at 0x48 (max 32 bytes, including nul). */
11080 elf_tdata (abfd)->core->command
11081 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11082
11083 return true;
11084 }
11085
11086 static bool
11087 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11088 {
11089 if (note->type == NT_OPENBSD_PROCINFO)
11090 return elfcore_grok_openbsd_procinfo (abfd, note);
11091
11092 if (note->type == NT_OPENBSD_REGS)
11093 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11094
11095 if (note->type == NT_OPENBSD_FPREGS)
11096 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11097
11098 if (note->type == NT_OPENBSD_XFPREGS)
11099 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11100
11101 if (note->type == NT_OPENBSD_AUXV)
11102 return elfcore_make_auxv_note_section (abfd, note, 0);
11103
11104 if (note->type == NT_OPENBSD_WCOOKIE)
11105 {
11106 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11107 SEC_HAS_CONTENTS);
11108
11109 if (sect == NULL)
11110 return false;
11111 sect->size = note->descsz;
11112 sect->filepos = note->descpos;
11113 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11114
11115 return true;
11116 }
11117
11118 return true;
11119 }
11120
11121 static bool
11122 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11123 {
11124 void *ddata = note->descdata;
11125 char buf[100];
11126 char *name;
11127 asection *sect;
11128 short sig;
11129 unsigned flags;
11130
11131 if (note->descsz < 16)
11132 return false;
11133
11134 /* nto_procfs_status 'pid' field is at offset 0. */
11135 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11136
11137 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11138 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11139
11140 /* nto_procfs_status 'flags' field is at offset 8. */
11141 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11142
11143 /* nto_procfs_status 'what' field is at offset 14. */
11144 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11145 {
11146 elf_tdata (abfd)->core->signal = sig;
11147 elf_tdata (abfd)->core->lwpid = *tid;
11148 }
11149
11150 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11151 do not come from signals so we make sure we set the current
11152 thread just in case. */
11153 if (flags & 0x00000080)
11154 elf_tdata (abfd)->core->lwpid = *tid;
11155
11156 /* Make a ".qnx_core_status/%d" section. */
11157 sprintf (buf, ".qnx_core_status/%ld", *tid);
11158
11159 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11160 if (name == NULL)
11161 return false;
11162 strcpy (name, buf);
11163
11164 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11165 if (sect == NULL)
11166 return false;
11167
11168 sect->size = note->descsz;
11169 sect->filepos = note->descpos;
11170 sect->alignment_power = 2;
11171
11172 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11173 }
11174
11175 static bool
11176 elfcore_grok_nto_regs (bfd *abfd,
11177 Elf_Internal_Note *note,
11178 long tid,
11179 char *base)
11180 {
11181 char buf[100];
11182 char *name;
11183 asection *sect;
11184
11185 /* Make a "(base)/%d" section. */
11186 sprintf (buf, "%s/%ld", base, tid);
11187
11188 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11189 if (name == NULL)
11190 return false;
11191 strcpy (name, buf);
11192
11193 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11194 if (sect == NULL)
11195 return false;
11196
11197 sect->size = note->descsz;
11198 sect->filepos = note->descpos;
11199 sect->alignment_power = 2;
11200
11201 /* This is the current thread. */
11202 if (elf_tdata (abfd)->core->lwpid == tid)
11203 return elfcore_maybe_make_sect (abfd, base, sect);
11204
11205 return true;
11206 }
11207
11208 #define BFD_QNT_CORE_INFO 7
11209 #define BFD_QNT_CORE_STATUS 8
11210 #define BFD_QNT_CORE_GREG 9
11211 #define BFD_QNT_CORE_FPREG 10
11212
11213 static bool
11214 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11215 {
11216 /* Every GREG section has a STATUS section before it. Store the
11217 tid from the previous call to pass down to the next gregs
11218 function. */
11219 static long tid = 1;
11220
11221 switch (note->type)
11222 {
11223 case BFD_QNT_CORE_INFO:
11224 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11225 case BFD_QNT_CORE_STATUS:
11226 return elfcore_grok_nto_status (abfd, note, &tid);
11227 case BFD_QNT_CORE_GREG:
11228 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11229 case BFD_QNT_CORE_FPREG:
11230 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11231 default:
11232 return true;
11233 }
11234 }
11235
11236 static bool
11237 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11238 {
11239 char *name;
11240 asection *sect;
11241 size_t len;
11242
11243 /* Use note name as section name. */
11244 len = note->namesz;
11245 name = (char *) bfd_alloc (abfd, len);
11246 if (name == NULL)
11247 return false;
11248 memcpy (name, note->namedata, len);
11249 name[len - 1] = '\0';
11250
11251 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11252 if (sect == NULL)
11253 return false;
11254
11255 sect->size = note->descsz;
11256 sect->filepos = note->descpos;
11257 sect->alignment_power = 1;
11258
11259 return true;
11260 }
11261
11262 /* Function: elfcore_write_note
11263
11264 Inputs:
11265 buffer to hold note, and current size of buffer
11266 name of note
11267 type of note
11268 data for note
11269 size of data for note
11270
11271 Writes note to end of buffer. ELF64 notes are written exactly as
11272 for ELF32, despite the current (as of 2006) ELF gabi specifying
11273 that they ought to have 8-byte namesz and descsz field, and have
11274 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11275
11276 Return:
11277 Pointer to realloc'd buffer, *BUFSIZ updated. */
11278
11279 char *
11280 elfcore_write_note (bfd *abfd,
11281 char *buf,
11282 int *bufsiz,
11283 const char *name,
11284 int type,
11285 const void *input,
11286 int size)
11287 {
11288 Elf_External_Note *xnp;
11289 size_t namesz;
11290 size_t newspace;
11291 char *dest;
11292
11293 namesz = 0;
11294 if (name != NULL)
11295 namesz = strlen (name) + 1;
11296
11297 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11298
11299 buf = (char *) realloc (buf, *bufsiz + newspace);
11300 if (buf == NULL)
11301 return buf;
11302 dest = buf + *bufsiz;
11303 *bufsiz += newspace;
11304 xnp = (Elf_External_Note *) dest;
11305 H_PUT_32 (abfd, namesz, xnp->namesz);
11306 H_PUT_32 (abfd, size, xnp->descsz);
11307 H_PUT_32 (abfd, type, xnp->type);
11308 dest = xnp->name;
11309 if (name != NULL)
11310 {
11311 memcpy (dest, name, namesz);
11312 dest += namesz;
11313 while (namesz & 3)
11314 {
11315 *dest++ = '\0';
11316 ++namesz;
11317 }
11318 }
11319 memcpy (dest, input, size);
11320 dest += size;
11321 while (size & 3)
11322 {
11323 *dest++ = '\0';
11324 ++size;
11325 }
11326 return buf;
11327 }
11328
11329 /* gcc-8 warns (*) on all the strncpy calls in this function about
11330 possible string truncation. The "truncation" is not a bug. We
11331 have an external representation of structs with fields that are not
11332 necessarily NULL terminated and corresponding internal
11333 representation fields that are one larger so that they can always
11334 be NULL terminated.
11335 gcc versions between 4.2 and 4.6 do not allow pragma control of
11336 diagnostics inside functions, giving a hard error if you try to use
11337 the finer control available with later versions.
11338 gcc prior to 4.2 warns about diagnostic push and pop.
11339 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11340 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11341 (*) Depending on your system header files! */
11342 #if GCC_VERSION >= 8000
11343 # pragma GCC diagnostic push
11344 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11345 #endif
11346 char *
11347 elfcore_write_prpsinfo (bfd *abfd,
11348 char *buf,
11349 int *bufsiz,
11350 const char *fname,
11351 const char *psargs)
11352 {
11353 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11354
11355 if (bed->elf_backend_write_core_note != NULL)
11356 {
11357 char *ret;
11358 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11359 NT_PRPSINFO, fname, psargs);
11360 if (ret != NULL)
11361 return ret;
11362 }
11363
11364 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11365 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11366 if (bed->s->elfclass == ELFCLASS32)
11367 {
11368 # if defined (HAVE_PSINFO32_T)
11369 psinfo32_t data;
11370 int note_type = NT_PSINFO;
11371 # else
11372 prpsinfo32_t data;
11373 int note_type = NT_PRPSINFO;
11374 # endif
11375
11376 memset (&data, 0, sizeof (data));
11377 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11378 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11379 return elfcore_write_note (abfd, buf, bufsiz,
11380 "CORE", note_type, &data, sizeof (data));
11381 }
11382 else
11383 # endif
11384 {
11385 # if defined (HAVE_PSINFO_T)
11386 psinfo_t data;
11387 int note_type = NT_PSINFO;
11388 # else
11389 prpsinfo_t data;
11390 int note_type = NT_PRPSINFO;
11391 # endif
11392
11393 memset (&data, 0, sizeof (data));
11394 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11395 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11396 return elfcore_write_note (abfd, buf, bufsiz,
11397 "CORE", note_type, &data, sizeof (data));
11398 }
11399 #endif /* PSINFO_T or PRPSINFO_T */
11400
11401 free (buf);
11402 return NULL;
11403 }
11404 #if GCC_VERSION >= 8000
11405 # pragma GCC diagnostic pop
11406 #endif
11407
11408 char *
11409 elfcore_write_linux_prpsinfo32
11410 (bfd *abfd, char *buf, int *bufsiz,
11411 const struct elf_internal_linux_prpsinfo *prpsinfo)
11412 {
11413 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11414 {
11415 struct elf_external_linux_prpsinfo32_ugid16 data;
11416
11417 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11418 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11419 &data, sizeof (data));
11420 }
11421 else
11422 {
11423 struct elf_external_linux_prpsinfo32_ugid32 data;
11424
11425 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11426 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11427 &data, sizeof (data));
11428 }
11429 }
11430
11431 char *
11432 elfcore_write_linux_prpsinfo64
11433 (bfd *abfd, char *buf, int *bufsiz,
11434 const struct elf_internal_linux_prpsinfo *prpsinfo)
11435 {
11436 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11437 {
11438 struct elf_external_linux_prpsinfo64_ugid16 data;
11439
11440 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11441 return elfcore_write_note (abfd, buf, bufsiz,
11442 "CORE", NT_PRPSINFO, &data, sizeof (data));
11443 }
11444 else
11445 {
11446 struct elf_external_linux_prpsinfo64_ugid32 data;
11447
11448 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11449 return elfcore_write_note (abfd, buf, bufsiz,
11450 "CORE", NT_PRPSINFO, &data, sizeof (data));
11451 }
11452 }
11453
11454 char *
11455 elfcore_write_prstatus (bfd *abfd,
11456 char *buf,
11457 int *bufsiz,
11458 long pid,
11459 int cursig,
11460 const void *gregs)
11461 {
11462 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11463
11464 if (bed->elf_backend_write_core_note != NULL)
11465 {
11466 char *ret;
11467 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11468 NT_PRSTATUS,
11469 pid, cursig, gregs);
11470 if (ret != NULL)
11471 return ret;
11472 }
11473
11474 #if defined (HAVE_PRSTATUS_T)
11475 #if defined (HAVE_PRSTATUS32_T)
11476 if (bed->s->elfclass == ELFCLASS32)
11477 {
11478 prstatus32_t prstat;
11479
11480 memset (&prstat, 0, sizeof (prstat));
11481 prstat.pr_pid = pid;
11482 prstat.pr_cursig = cursig;
11483 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11484 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11485 NT_PRSTATUS, &prstat, sizeof (prstat));
11486 }
11487 else
11488 #endif
11489 {
11490 prstatus_t prstat;
11491
11492 memset (&prstat, 0, sizeof (prstat));
11493 prstat.pr_pid = pid;
11494 prstat.pr_cursig = cursig;
11495 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11496 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11497 NT_PRSTATUS, &prstat, sizeof (prstat));
11498 }
11499 #endif /* HAVE_PRSTATUS_T */
11500
11501 free (buf);
11502 return NULL;
11503 }
11504
11505 #if defined (HAVE_LWPSTATUS_T)
11506 char *
11507 elfcore_write_lwpstatus (bfd *abfd,
11508 char *buf,
11509 int *bufsiz,
11510 long pid,
11511 int cursig,
11512 const void *gregs)
11513 {
11514 lwpstatus_t lwpstat;
11515 const char *note_name = "CORE";
11516
11517 memset (&lwpstat, 0, sizeof (lwpstat));
11518 lwpstat.pr_lwpid = pid >> 16;
11519 lwpstat.pr_cursig = cursig;
11520 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11521 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11522 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11523 #if !defined(gregs)
11524 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11525 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11526 #else
11527 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11528 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11529 #endif
11530 #endif
11531 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11532 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11533 }
11534 #endif /* HAVE_LWPSTATUS_T */
11535
11536 #if defined (HAVE_PSTATUS_T)
11537 char *
11538 elfcore_write_pstatus (bfd *abfd,
11539 char *buf,
11540 int *bufsiz,
11541 long pid,
11542 int cursig ATTRIBUTE_UNUSED,
11543 const void *gregs ATTRIBUTE_UNUSED)
11544 {
11545 const char *note_name = "CORE";
11546 #if defined (HAVE_PSTATUS32_T)
11547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11548
11549 if (bed->s->elfclass == ELFCLASS32)
11550 {
11551 pstatus32_t pstat;
11552
11553 memset (&pstat, 0, sizeof (pstat));
11554 pstat.pr_pid = pid & 0xffff;
11555 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11556 NT_PSTATUS, &pstat, sizeof (pstat));
11557 return buf;
11558 }
11559 else
11560 #endif
11561 {
11562 pstatus_t pstat;
11563
11564 memset (&pstat, 0, sizeof (pstat));
11565 pstat.pr_pid = pid & 0xffff;
11566 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11567 NT_PSTATUS, &pstat, sizeof (pstat));
11568 return buf;
11569 }
11570 }
11571 #endif /* HAVE_PSTATUS_T */
11572
11573 char *
11574 elfcore_write_prfpreg (bfd *abfd,
11575 char *buf,
11576 int *bufsiz,
11577 const void *fpregs,
11578 int size)
11579 {
11580 const char *note_name = "CORE";
11581 return elfcore_write_note (abfd, buf, bufsiz,
11582 note_name, NT_FPREGSET, fpregs, size);
11583 }
11584
11585 char *
11586 elfcore_write_prxfpreg (bfd *abfd,
11587 char *buf,
11588 int *bufsiz,
11589 const void *xfpregs,
11590 int size)
11591 {
11592 char *note_name = "LINUX";
11593 return elfcore_write_note (abfd, buf, bufsiz,
11594 note_name, NT_PRXFPREG, xfpregs, size);
11595 }
11596
11597 char *
11598 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11599 const void *xfpregs, int size)
11600 {
11601 char *note_name;
11602 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11603 note_name = "FreeBSD";
11604 else
11605 note_name = "LINUX";
11606 return elfcore_write_note (abfd, buf, bufsiz,
11607 note_name, NT_X86_XSTATE, xfpregs, size);
11608 }
11609
11610 char *
11611 elfcore_write_ppc_vmx (bfd *abfd,
11612 char *buf,
11613 int *bufsiz,
11614 const void *ppc_vmx,
11615 int size)
11616 {
11617 char *note_name = "LINUX";
11618 return elfcore_write_note (abfd, buf, bufsiz,
11619 note_name, NT_PPC_VMX, ppc_vmx, size);
11620 }
11621
11622 char *
11623 elfcore_write_ppc_vsx (bfd *abfd,
11624 char *buf,
11625 int *bufsiz,
11626 const void *ppc_vsx,
11627 int size)
11628 {
11629 char *note_name = "LINUX";
11630 return elfcore_write_note (abfd, buf, bufsiz,
11631 note_name, NT_PPC_VSX, ppc_vsx, size);
11632 }
11633
11634 char *
11635 elfcore_write_ppc_tar (bfd *abfd,
11636 char *buf,
11637 int *bufsiz,
11638 const void *ppc_tar,
11639 int size)
11640 {
11641 char *note_name = "LINUX";
11642 return elfcore_write_note (abfd, buf, bufsiz,
11643 note_name, NT_PPC_TAR, ppc_tar, size);
11644 }
11645
11646 char *
11647 elfcore_write_ppc_ppr (bfd *abfd,
11648 char *buf,
11649 int *bufsiz,
11650 const void *ppc_ppr,
11651 int size)
11652 {
11653 char *note_name = "LINUX";
11654 return elfcore_write_note (abfd, buf, bufsiz,
11655 note_name, NT_PPC_PPR, ppc_ppr, size);
11656 }
11657
11658 char *
11659 elfcore_write_ppc_dscr (bfd *abfd,
11660 char *buf,
11661 int *bufsiz,
11662 const void *ppc_dscr,
11663 int size)
11664 {
11665 char *note_name = "LINUX";
11666 return elfcore_write_note (abfd, buf, bufsiz,
11667 note_name, NT_PPC_DSCR, ppc_dscr, size);
11668 }
11669
11670 char *
11671 elfcore_write_ppc_ebb (bfd *abfd,
11672 char *buf,
11673 int *bufsiz,
11674 const void *ppc_ebb,
11675 int size)
11676 {
11677 char *note_name = "LINUX";
11678 return elfcore_write_note (abfd, buf, bufsiz,
11679 note_name, NT_PPC_EBB, ppc_ebb, size);
11680 }
11681
11682 char *
11683 elfcore_write_ppc_pmu (bfd *abfd,
11684 char *buf,
11685 int *bufsiz,
11686 const void *ppc_pmu,
11687 int size)
11688 {
11689 char *note_name = "LINUX";
11690 return elfcore_write_note (abfd, buf, bufsiz,
11691 note_name, NT_PPC_PMU, ppc_pmu, size);
11692 }
11693
11694 char *
11695 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11696 char *buf,
11697 int *bufsiz,
11698 const void *ppc_tm_cgpr,
11699 int size)
11700 {
11701 char *note_name = "LINUX";
11702 return elfcore_write_note (abfd, buf, bufsiz,
11703 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11704 }
11705
11706 char *
11707 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11708 char *buf,
11709 int *bufsiz,
11710 const void *ppc_tm_cfpr,
11711 int size)
11712 {
11713 char *note_name = "LINUX";
11714 return elfcore_write_note (abfd, buf, bufsiz,
11715 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11716 }
11717
11718 char *
11719 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11720 char *buf,
11721 int *bufsiz,
11722 const void *ppc_tm_cvmx,
11723 int size)
11724 {
11725 char *note_name = "LINUX";
11726 return elfcore_write_note (abfd, buf, bufsiz,
11727 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11728 }
11729
11730 char *
11731 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11732 char *buf,
11733 int *bufsiz,
11734 const void *ppc_tm_cvsx,
11735 int size)
11736 {
11737 char *note_name = "LINUX";
11738 return elfcore_write_note (abfd, buf, bufsiz,
11739 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11740 }
11741
11742 char *
11743 elfcore_write_ppc_tm_spr (bfd *abfd,
11744 char *buf,
11745 int *bufsiz,
11746 const void *ppc_tm_spr,
11747 int size)
11748 {
11749 char *note_name = "LINUX";
11750 return elfcore_write_note (abfd, buf, bufsiz,
11751 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11752 }
11753
11754 char *
11755 elfcore_write_ppc_tm_ctar (bfd *abfd,
11756 char *buf,
11757 int *bufsiz,
11758 const void *ppc_tm_ctar,
11759 int size)
11760 {
11761 char *note_name = "LINUX";
11762 return elfcore_write_note (abfd, buf, bufsiz,
11763 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11764 }
11765
11766 char *
11767 elfcore_write_ppc_tm_cppr (bfd *abfd,
11768 char *buf,
11769 int *bufsiz,
11770 const void *ppc_tm_cppr,
11771 int size)
11772 {
11773 char *note_name = "LINUX";
11774 return elfcore_write_note (abfd, buf, bufsiz,
11775 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11776 }
11777
11778 char *
11779 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11780 char *buf,
11781 int *bufsiz,
11782 const void *ppc_tm_cdscr,
11783 int size)
11784 {
11785 char *note_name = "LINUX";
11786 return elfcore_write_note (abfd, buf, bufsiz,
11787 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11788 }
11789
11790 static char *
11791 elfcore_write_s390_high_gprs (bfd *abfd,
11792 char *buf,
11793 int *bufsiz,
11794 const void *s390_high_gprs,
11795 int size)
11796 {
11797 char *note_name = "LINUX";
11798 return elfcore_write_note (abfd, buf, bufsiz,
11799 note_name, NT_S390_HIGH_GPRS,
11800 s390_high_gprs, size);
11801 }
11802
11803 char *
11804 elfcore_write_s390_timer (bfd *abfd,
11805 char *buf,
11806 int *bufsiz,
11807 const void *s390_timer,
11808 int size)
11809 {
11810 char *note_name = "LINUX";
11811 return elfcore_write_note (abfd, buf, bufsiz,
11812 note_name, NT_S390_TIMER, s390_timer, size);
11813 }
11814
11815 char *
11816 elfcore_write_s390_todcmp (bfd *abfd,
11817 char *buf,
11818 int *bufsiz,
11819 const void *s390_todcmp,
11820 int size)
11821 {
11822 char *note_name = "LINUX";
11823 return elfcore_write_note (abfd, buf, bufsiz,
11824 note_name, NT_S390_TODCMP, s390_todcmp, size);
11825 }
11826
11827 char *
11828 elfcore_write_s390_todpreg (bfd *abfd,
11829 char *buf,
11830 int *bufsiz,
11831 const void *s390_todpreg,
11832 int size)
11833 {
11834 char *note_name = "LINUX";
11835 return elfcore_write_note (abfd, buf, bufsiz,
11836 note_name, NT_S390_TODPREG, s390_todpreg, size);
11837 }
11838
11839 char *
11840 elfcore_write_s390_ctrs (bfd *abfd,
11841 char *buf,
11842 int *bufsiz,
11843 const void *s390_ctrs,
11844 int size)
11845 {
11846 char *note_name = "LINUX";
11847 return elfcore_write_note (abfd, buf, bufsiz,
11848 note_name, NT_S390_CTRS, s390_ctrs, size);
11849 }
11850
11851 char *
11852 elfcore_write_s390_prefix (bfd *abfd,
11853 char *buf,
11854 int *bufsiz,
11855 const void *s390_prefix,
11856 int size)
11857 {
11858 char *note_name = "LINUX";
11859 return elfcore_write_note (abfd, buf, bufsiz,
11860 note_name, NT_S390_PREFIX, s390_prefix, size);
11861 }
11862
11863 char *
11864 elfcore_write_s390_last_break (bfd *abfd,
11865 char *buf,
11866 int *bufsiz,
11867 const void *s390_last_break,
11868 int size)
11869 {
11870 char *note_name = "LINUX";
11871 return elfcore_write_note (abfd, buf, bufsiz,
11872 note_name, NT_S390_LAST_BREAK,
11873 s390_last_break, size);
11874 }
11875
11876 char *
11877 elfcore_write_s390_system_call (bfd *abfd,
11878 char *buf,
11879 int *bufsiz,
11880 const void *s390_system_call,
11881 int size)
11882 {
11883 char *note_name = "LINUX";
11884 return elfcore_write_note (abfd, buf, bufsiz,
11885 note_name, NT_S390_SYSTEM_CALL,
11886 s390_system_call, size);
11887 }
11888
11889 char *
11890 elfcore_write_s390_tdb (bfd *abfd,
11891 char *buf,
11892 int *bufsiz,
11893 const void *s390_tdb,
11894 int size)
11895 {
11896 char *note_name = "LINUX";
11897 return elfcore_write_note (abfd, buf, bufsiz,
11898 note_name, NT_S390_TDB, s390_tdb, size);
11899 }
11900
11901 char *
11902 elfcore_write_s390_vxrs_low (bfd *abfd,
11903 char *buf,
11904 int *bufsiz,
11905 const void *s390_vxrs_low,
11906 int size)
11907 {
11908 char *note_name = "LINUX";
11909 return elfcore_write_note (abfd, buf, bufsiz,
11910 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11911 }
11912
11913 char *
11914 elfcore_write_s390_vxrs_high (bfd *abfd,
11915 char *buf,
11916 int *bufsiz,
11917 const void *s390_vxrs_high,
11918 int size)
11919 {
11920 char *note_name = "LINUX";
11921 return elfcore_write_note (abfd, buf, bufsiz,
11922 note_name, NT_S390_VXRS_HIGH,
11923 s390_vxrs_high, size);
11924 }
11925
11926 char *
11927 elfcore_write_s390_gs_cb (bfd *abfd,
11928 char *buf,
11929 int *bufsiz,
11930 const void *s390_gs_cb,
11931 int size)
11932 {
11933 char *note_name = "LINUX";
11934 return elfcore_write_note (abfd, buf, bufsiz,
11935 note_name, NT_S390_GS_CB,
11936 s390_gs_cb, size);
11937 }
11938
11939 char *
11940 elfcore_write_s390_gs_bc (bfd *abfd,
11941 char *buf,
11942 int *bufsiz,
11943 const void *s390_gs_bc,
11944 int size)
11945 {
11946 char *note_name = "LINUX";
11947 return elfcore_write_note (abfd, buf, bufsiz,
11948 note_name, NT_S390_GS_BC,
11949 s390_gs_bc, size);
11950 }
11951
11952 char *
11953 elfcore_write_arm_vfp (bfd *abfd,
11954 char *buf,
11955 int *bufsiz,
11956 const void *arm_vfp,
11957 int size)
11958 {
11959 char *note_name = "LINUX";
11960 return elfcore_write_note (abfd, buf, bufsiz,
11961 note_name, NT_ARM_VFP, arm_vfp, size);
11962 }
11963
11964 char *
11965 elfcore_write_aarch_tls (bfd *abfd,
11966 char *buf,
11967 int *bufsiz,
11968 const void *aarch_tls,
11969 int size)
11970 {
11971 char *note_name = "LINUX";
11972 return elfcore_write_note (abfd, buf, bufsiz,
11973 note_name, NT_ARM_TLS, aarch_tls, size);
11974 }
11975
11976 char *
11977 elfcore_write_aarch_hw_break (bfd *abfd,
11978 char *buf,
11979 int *bufsiz,
11980 const void *aarch_hw_break,
11981 int size)
11982 {
11983 char *note_name = "LINUX";
11984 return elfcore_write_note (abfd, buf, bufsiz,
11985 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11986 }
11987
11988 char *
11989 elfcore_write_aarch_hw_watch (bfd *abfd,
11990 char *buf,
11991 int *bufsiz,
11992 const void *aarch_hw_watch,
11993 int size)
11994 {
11995 char *note_name = "LINUX";
11996 return elfcore_write_note (abfd, buf, bufsiz,
11997 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11998 }
11999
12000 char *
12001 elfcore_write_aarch_sve (bfd *abfd,
12002 char *buf,
12003 int *bufsiz,
12004 const void *aarch_sve,
12005 int size)
12006 {
12007 char *note_name = "LINUX";
12008 return elfcore_write_note (abfd, buf, bufsiz,
12009 note_name, NT_ARM_SVE, aarch_sve, size);
12010 }
12011
12012 char *
12013 elfcore_write_aarch_pauth (bfd *abfd,
12014 char *buf,
12015 int *bufsiz,
12016 const void *aarch_pauth,
12017 int size)
12018 {
12019 char *note_name = "LINUX";
12020 return elfcore_write_note (abfd, buf, bufsiz,
12021 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12022 }
12023
12024 char *
12025 elfcore_write_aarch_mte (bfd *abfd,
12026 char *buf,
12027 int *bufsiz,
12028 const void *aarch_mte,
12029 int size)
12030 {
12031 char *note_name = "LINUX";
12032 return elfcore_write_note (abfd, buf, bufsiz,
12033 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12034 aarch_mte,
12035 size);
12036 }
12037
12038 char *
12039 elfcore_write_arc_v2 (bfd *abfd,
12040 char *buf,
12041 int *bufsiz,
12042 const void *arc_v2,
12043 int size)
12044 {
12045 char *note_name = "LINUX";
12046 return elfcore_write_note (abfd, buf, bufsiz,
12047 note_name, NT_ARC_V2, arc_v2, size);
12048 }
12049
12050 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12051 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12052 written into. Return a pointer to the new start of the note buffer, to
12053 replace BUF which may no longer be valid. */
12054
12055 char *
12056 elfcore_write_riscv_csr (bfd *abfd,
12057 char *buf,
12058 int *bufsiz,
12059 const void *csrs,
12060 int size)
12061 {
12062 const char *note_name = "GDB";
12063 return elfcore_write_note (abfd, buf, bufsiz,
12064 note_name, NT_RISCV_CSR, csrs, size);
12065 }
12066
12067 /* Write the target description (a string) pointed to by TDESC, length
12068 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12069 note is being written into. Return a pointer to the new start of the
12070 note buffer, to replace BUF which may no longer be valid. */
12071
12072 char *
12073 elfcore_write_gdb_tdesc (bfd *abfd,
12074 char *buf,
12075 int *bufsiz,
12076 const void *tdesc,
12077 int size)
12078 {
12079 const char *note_name = "GDB";
12080 return elfcore_write_note (abfd, buf, bufsiz,
12081 note_name, NT_GDB_TDESC, tdesc, size);
12082 }
12083
12084 char *
12085 elfcore_write_register_note (bfd *abfd,
12086 char *buf,
12087 int *bufsiz,
12088 const char *section,
12089 const void *data,
12090 int size)
12091 {
12092 if (strcmp (section, ".reg2") == 0)
12093 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12094 if (strcmp (section, ".reg-xfp") == 0)
12095 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12096 if (strcmp (section, ".reg-xstate") == 0)
12097 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12098 if (strcmp (section, ".reg-ppc-vmx") == 0)
12099 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12100 if (strcmp (section, ".reg-ppc-vsx") == 0)
12101 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12102 if (strcmp (section, ".reg-ppc-tar") == 0)
12103 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12104 if (strcmp (section, ".reg-ppc-ppr") == 0)
12105 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12106 if (strcmp (section, ".reg-ppc-dscr") == 0)
12107 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12108 if (strcmp (section, ".reg-ppc-ebb") == 0)
12109 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12110 if (strcmp (section, ".reg-ppc-pmu") == 0)
12111 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12112 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12113 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12114 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12115 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12116 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12117 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12118 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12119 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12120 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12121 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12122 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12123 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12124 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12125 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12126 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12127 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12128 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12129 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12130 if (strcmp (section, ".reg-s390-timer") == 0)
12131 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12132 if (strcmp (section, ".reg-s390-todcmp") == 0)
12133 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12134 if (strcmp (section, ".reg-s390-todpreg") == 0)
12135 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12136 if (strcmp (section, ".reg-s390-ctrs") == 0)
12137 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12138 if (strcmp (section, ".reg-s390-prefix") == 0)
12139 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12140 if (strcmp (section, ".reg-s390-last-break") == 0)
12141 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12142 if (strcmp (section, ".reg-s390-system-call") == 0)
12143 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12144 if (strcmp (section, ".reg-s390-tdb") == 0)
12145 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12146 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12147 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12148 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12149 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12150 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12151 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12152 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12153 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12154 if (strcmp (section, ".reg-arm-vfp") == 0)
12155 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12156 if (strcmp (section, ".reg-aarch-tls") == 0)
12157 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12158 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12159 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12160 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12161 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12162 if (strcmp (section, ".reg-aarch-sve") == 0)
12163 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12164 if (strcmp (section, ".reg-aarch-pauth") == 0)
12165 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12166 if (strcmp (section, ".reg-aarch-mte") == 0)
12167 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12168 if (strcmp (section, ".reg-arc-v2") == 0)
12169 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12170 if (strcmp (section, ".gdb-tdesc") == 0)
12171 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12172 if (strcmp (section, ".reg-riscv-csr") == 0)
12173 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12174 return NULL;
12175 }
12176
12177 char *
12178 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12179 const void *buf, int bufsiz)
12180 {
12181 return elfcore_write_note (obfd, note_data, note_size,
12182 "CORE", NT_FILE, buf, bufsiz);
12183 }
12184
12185 static bool
12186 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12187 size_t align)
12188 {
12189 char *p;
12190
12191 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12192 gABI specifies that PT_NOTE alignment should be aligned to 4
12193 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12194 align is less than 4, we use 4 byte alignment. */
12195 if (align < 4)
12196 align = 4;
12197 if (align != 4 && align != 8)
12198 return false;
12199
12200 p = buf;
12201 while (p < buf + size)
12202 {
12203 Elf_External_Note *xnp = (Elf_External_Note *) p;
12204 Elf_Internal_Note in;
12205
12206 if (offsetof (Elf_External_Note, name) > buf - p + size)
12207 return false;
12208
12209 in.type = H_GET_32 (abfd, xnp->type);
12210
12211 in.namesz = H_GET_32 (abfd, xnp->namesz);
12212 in.namedata = xnp->name;
12213 if (in.namesz > buf - in.namedata + size)
12214 return false;
12215
12216 in.descsz = H_GET_32 (abfd, xnp->descsz);
12217 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12218 in.descpos = offset + (in.descdata - buf);
12219 if (in.descsz != 0
12220 && (in.descdata >= buf + size
12221 || in.descsz > buf - in.descdata + size))
12222 return false;
12223
12224 switch (bfd_get_format (abfd))
12225 {
12226 default:
12227 return true;
12228
12229 case bfd_core:
12230 {
12231 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12232 struct
12233 {
12234 const char * string;
12235 size_t len;
12236 bool (*func) (bfd *, Elf_Internal_Note *);
12237 }
12238 grokers[] =
12239 {
12240 GROKER_ELEMENT ("", elfcore_grok_note),
12241 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12242 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12243 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
12244 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12245 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12246 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
12247 };
12248 #undef GROKER_ELEMENT
12249 int i;
12250
12251 for (i = ARRAY_SIZE (grokers); i--;)
12252 {
12253 if (in.namesz >= grokers[i].len
12254 && strncmp (in.namedata, grokers[i].string,
12255 grokers[i].len) == 0)
12256 {
12257 if (! grokers[i].func (abfd, & in))
12258 return false;
12259 break;
12260 }
12261 }
12262 break;
12263 }
12264
12265 case bfd_object:
12266 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12267 {
12268 if (! elfobj_grok_gnu_note (abfd, &in))
12269 return false;
12270 }
12271 else if (in.namesz == sizeof "stapsdt"
12272 && strcmp (in.namedata, "stapsdt") == 0)
12273 {
12274 if (! elfobj_grok_stapsdt_note (abfd, &in))
12275 return false;
12276 }
12277 break;
12278 }
12279
12280 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12281 }
12282
12283 return true;
12284 }
12285
12286 bool
12287 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12288 size_t align)
12289 {
12290 char *buf;
12291
12292 if (size == 0 || (size + 1) == 0)
12293 return true;
12294
12295 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12296 return false;
12297
12298 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12299 if (buf == NULL)
12300 return false;
12301
12302 /* PR 17512: file: ec08f814
12303 0-termintate the buffer so that string searches will not overflow. */
12304 buf[size] = 0;
12305
12306 if (!elf_parse_notes (abfd, buf, size, offset, align))
12307 {
12308 free (buf);
12309 return false;
12310 }
12311
12312 free (buf);
12313 return true;
12314 }
12315 \f
12316 /* Providing external access to the ELF program header table. */
12317
12318 /* Return an upper bound on the number of bytes required to store a
12319 copy of ABFD's program header table entries. Return -1 if an error
12320 occurs; bfd_get_error will return an appropriate code. */
12321
12322 long
12323 bfd_get_elf_phdr_upper_bound (bfd *abfd)
12324 {
12325 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12326 {
12327 bfd_set_error (bfd_error_wrong_format);
12328 return -1;
12329 }
12330
12331 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12332 }
12333
12334 /* Copy ABFD's program header table entries to *PHDRS. The entries
12335 will be stored as an array of Elf_Internal_Phdr structures, as
12336 defined in include/elf/internal.h. To find out how large the
12337 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12338
12339 Return the number of program header table entries read, or -1 if an
12340 error occurs; bfd_get_error will return an appropriate code. */
12341
12342 int
12343 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12344 {
12345 int num_phdrs;
12346
12347 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12348 {
12349 bfd_set_error (bfd_error_wrong_format);
12350 return -1;
12351 }
12352
12353 num_phdrs = elf_elfheader (abfd)->e_phnum;
12354 if (num_phdrs != 0)
12355 memcpy (phdrs, elf_tdata (abfd)->phdr,
12356 num_phdrs * sizeof (Elf_Internal_Phdr));
12357
12358 return num_phdrs;
12359 }
12360
12361 enum elf_reloc_type_class
12362 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12363 const asection *rel_sec ATTRIBUTE_UNUSED,
12364 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12365 {
12366 return reloc_class_normal;
12367 }
12368
12369 /* For RELA architectures, return the relocation value for a
12370 relocation against a local symbol. */
12371
12372 bfd_vma
12373 _bfd_elf_rela_local_sym (bfd *abfd,
12374 Elf_Internal_Sym *sym,
12375 asection **psec,
12376 Elf_Internal_Rela *rel)
12377 {
12378 asection *sec = *psec;
12379 bfd_vma relocation;
12380
12381 relocation = (sec->output_section->vma
12382 + sec->output_offset
12383 + sym->st_value);
12384 if ((sec->flags & SEC_MERGE)
12385 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12386 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12387 {
12388 rel->r_addend =
12389 _bfd_merged_section_offset (abfd, psec,
12390 elf_section_data (sec)->sec_info,
12391 sym->st_value + rel->r_addend);
12392 if (sec != *psec)
12393 {
12394 /* If we have changed the section, and our original section is
12395 marked with SEC_EXCLUDE, it means that the original
12396 SEC_MERGE section has been completely subsumed in some
12397 other SEC_MERGE section. In this case, we need to leave
12398 some info around for --emit-relocs. */
12399 if ((sec->flags & SEC_EXCLUDE) != 0)
12400 sec->kept_section = *psec;
12401 sec = *psec;
12402 }
12403 rel->r_addend -= relocation;
12404 rel->r_addend += sec->output_section->vma + sec->output_offset;
12405 }
12406 return relocation;
12407 }
12408
12409 bfd_vma
12410 _bfd_elf_rel_local_sym (bfd *abfd,
12411 Elf_Internal_Sym *sym,
12412 asection **psec,
12413 bfd_vma addend)
12414 {
12415 asection *sec = *psec;
12416
12417 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12418 return sym->st_value + addend;
12419
12420 return _bfd_merged_section_offset (abfd, psec,
12421 elf_section_data (sec)->sec_info,
12422 sym->st_value + addend);
12423 }
12424
12425 /* Adjust an address within a section. Given OFFSET within SEC, return
12426 the new offset within the section, based upon changes made to the
12427 section. Returns -1 if the offset is now invalid.
12428 The offset (in abnd out) is in target sized bytes, however big a
12429 byte may be. */
12430
12431 bfd_vma
12432 _bfd_elf_section_offset (bfd *abfd,
12433 struct bfd_link_info *info,
12434 asection *sec,
12435 bfd_vma offset)
12436 {
12437 switch (sec->sec_info_type)
12438 {
12439 case SEC_INFO_TYPE_STABS:
12440 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12441 offset);
12442 case SEC_INFO_TYPE_EH_FRAME:
12443 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12444
12445 default:
12446 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12447 {
12448 /* Reverse the offset. */
12449 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12450 bfd_size_type address_size = bed->s->arch_size / 8;
12451
12452 /* address_size and sec->size are in octets. Convert
12453 to bytes before subtracting the original offset. */
12454 offset = ((sec->size - address_size)
12455 / bfd_octets_per_byte (abfd, sec) - offset);
12456 }
12457 return offset;
12458 }
12459 }
12460 \f
12461 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12462 reconstruct an ELF file by reading the segments out of remote memory
12463 based on the ELF file header at EHDR_VMA and the ELF program headers it
12464 points to. If not null, *LOADBASEP is filled in with the difference
12465 between the VMAs from which the segments were read, and the VMAs the
12466 file headers (and hence BFD's idea of each section's VMA) put them at.
12467
12468 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12469 remote memory at target address VMA into the local buffer at MYADDR; it
12470 should return zero on success or an `errno' code on failure. TEMPL must
12471 be a BFD for an ELF target with the word size and byte order found in
12472 the remote memory. */
12473
12474 bfd *
12475 bfd_elf_bfd_from_remote_memory
12476 (bfd *templ,
12477 bfd_vma ehdr_vma,
12478 bfd_size_type size,
12479 bfd_vma *loadbasep,
12480 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12481 {
12482 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12483 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12484 }
12485 \f
12486 long
12487 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12488 long symcount ATTRIBUTE_UNUSED,
12489 asymbol **syms ATTRIBUTE_UNUSED,
12490 long dynsymcount,
12491 asymbol **dynsyms,
12492 asymbol **ret)
12493 {
12494 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12495 asection *relplt;
12496 asymbol *s;
12497 const char *relplt_name;
12498 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12499 arelent *p;
12500 long count, i, n;
12501 size_t size;
12502 Elf_Internal_Shdr *hdr;
12503 char *names;
12504 asection *plt;
12505
12506 *ret = NULL;
12507
12508 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12509 return 0;
12510
12511 if (dynsymcount <= 0)
12512 return 0;
12513
12514 if (!bed->plt_sym_val)
12515 return 0;
12516
12517 relplt_name = bed->relplt_name;
12518 if (relplt_name == NULL)
12519 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12520 relplt = bfd_get_section_by_name (abfd, relplt_name);
12521 if (relplt == NULL)
12522 return 0;
12523
12524 hdr = &elf_section_data (relplt)->this_hdr;
12525 if (hdr->sh_link != elf_dynsymtab (abfd)
12526 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12527 return 0;
12528
12529 plt = bfd_get_section_by_name (abfd, ".plt");
12530 if (plt == NULL)
12531 return 0;
12532
12533 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12534 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
12535 return -1;
12536
12537 count = relplt->size / hdr->sh_entsize;
12538 size = count * sizeof (asymbol);
12539 p = relplt->relocation;
12540 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12541 {
12542 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12543 if (p->addend != 0)
12544 {
12545 #ifdef BFD64
12546 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12547 #else
12548 size += sizeof ("+0x") - 1 + 8;
12549 #endif
12550 }
12551 }
12552
12553 s = *ret = (asymbol *) bfd_malloc (size);
12554 if (s == NULL)
12555 return -1;
12556
12557 names = (char *) (s + count);
12558 p = relplt->relocation;
12559 n = 0;
12560 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12561 {
12562 size_t len;
12563 bfd_vma addr;
12564
12565 addr = bed->plt_sym_val (i, plt, p);
12566 if (addr == (bfd_vma) -1)
12567 continue;
12568
12569 *s = **p->sym_ptr_ptr;
12570 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12571 we are defining a symbol, ensure one of them is set. */
12572 if ((s->flags & BSF_LOCAL) == 0)
12573 s->flags |= BSF_GLOBAL;
12574 s->flags |= BSF_SYNTHETIC;
12575 s->section = plt;
12576 s->value = addr - plt->vma;
12577 s->name = names;
12578 s->udata.p = NULL;
12579 len = strlen ((*p->sym_ptr_ptr)->name);
12580 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12581 names += len;
12582 if (p->addend != 0)
12583 {
12584 char buf[30], *a;
12585
12586 memcpy (names, "+0x", sizeof ("+0x") - 1);
12587 names += sizeof ("+0x") - 1;
12588 bfd_sprintf_vma (abfd, buf, p->addend);
12589 for (a = buf; *a == '0'; ++a)
12590 ;
12591 len = strlen (a);
12592 memcpy (names, a, len);
12593 names += len;
12594 }
12595 memcpy (names, "@plt", sizeof ("@plt"));
12596 names += sizeof ("@plt");
12597 ++s, ++n;
12598 }
12599
12600 return n;
12601 }
12602
12603 /* It is only used by x86-64 so far.
12604 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12605 but current usage would allow all of _bfd_std_section to be zero. */
12606 static const asymbol lcomm_sym
12607 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12608 asection _bfd_elf_large_com_section
12609 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12610 "LARGE_COMMON", 0, SEC_IS_COMMON);
12611
12612 bool
12613 _bfd_elf_final_write_processing (bfd *abfd)
12614 {
12615 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
12616
12617 i_ehdrp = elf_elfheader (abfd);
12618
12619 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12620 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12621
12622 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12623 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
12624 or STB_GNU_UNIQUE binding. */
12625 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12626 {
12627 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12628 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12629 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12630 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12631 {
12632 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12633 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
12634 "and FreeBSD targets"));
12635 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12636 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
12637 "only by GNU and FreeBSD targets"));
12638 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12639 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
12640 "only by GNU and FreeBSD targets"));
12641 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
12642 _bfd_error_handler (_("GNU_RETAIN section is supported "
12643 "only by GNU and FreeBSD targets"));
12644 bfd_set_error (bfd_error_sorry);
12645 return false;
12646 }
12647 }
12648 return true;
12649 }
12650
12651
12652 /* Return TRUE for ELF symbol types that represent functions.
12653 This is the default version of this function, which is sufficient for
12654 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12655
12656 bool
12657 _bfd_elf_is_function_type (unsigned int type)
12658 {
12659 return (type == STT_FUNC
12660 || type == STT_GNU_IFUNC);
12661 }
12662
12663 /* If the ELF symbol SYM might be a function in SEC, return the
12664 function size and set *CODE_OFF to the function's entry point,
12665 otherwise return zero. */
12666
12667 bfd_size_type
12668 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12669 bfd_vma *code_off)
12670 {
12671 bfd_size_type size;
12672 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
12673
12674 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12675 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12676 || sym->section != sec)
12677 return 0;
12678
12679 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
12680
12681 /* In theory we should check that the symbol's type satisfies
12682 _bfd_elf_is_function_type(), but there are some function-like
12683 symbols which would fail this test. (eg _start). Instead
12684 we check for hidden, local, notype symbols with zero size.
12685 This type of symbol is generated by the annobin plugin for gcc
12686 and clang, and should not be considered to be a function symbol. */
12687 if (size == 0
12688 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
12689 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
12690 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
12691 return 0;
12692
12693 *code_off = sym->value;
12694 /* Do not return 0 for the function's size. */
12695 return size ? size : 1;
12696 }
12697
12698 /* Set to non-zero to enable some debug messages. */
12699 #define DEBUG_SECONDARY_RELOCS 0
12700
12701 /* An internal-to-the-bfd-library only section type
12702 used to indicate a cached secondary reloc section. */
12703 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12704
12705 /* Create a BFD section to hold a secondary reloc section. */
12706
12707 bool
12708 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
12709 Elf_Internal_Shdr *hdr,
12710 const char * name,
12711 unsigned int shindex)
12712 {
12713 /* We only support RELA secondary relocs. */
12714 if (hdr->sh_type != SHT_RELA)
12715 return false;
12716
12717 #if DEBUG_SECONDARY_RELOCS
12718 fprintf (stderr, "secondary reloc section %s encountered\n", name);
12719 #endif
12720 hdr->sh_type = SHT_SECONDARY_RELOC;
12721 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
12722 }
12723
12724 /* Read in any secondary relocs associated with SEC. */
12725
12726 bool
12727 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
12728 asection * sec,
12729 asymbol ** symbols,
12730 bool dynamic)
12731 {
12732 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12733 asection * relsec;
12734 bool result = true;
12735 bfd_vma (*r_sym) (bfd_vma);
12736
12737 #if BFD_DEFAULT_TARGET_SIZE > 32
12738 if (bfd_arch_bits_per_address (abfd) != 32)
12739 r_sym = elf64_r_sym;
12740 else
12741 #endif
12742 r_sym = elf32_r_sym;
12743
12744 if (!elf_section_data (sec)->has_secondary_relocs)
12745 return true;
12746
12747 /* Discover if there are any secondary reloc sections
12748 associated with SEC. */
12749 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
12750 {
12751 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
12752
12753 if (hdr->sh_type == SHT_SECONDARY_RELOC
12754 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
12755 && (hdr->sh_entsize == ebd->s->sizeof_rel
12756 || hdr->sh_entsize == ebd->s->sizeof_rela))
12757 {
12758 bfd_byte * native_relocs;
12759 bfd_byte * native_reloc;
12760 arelent * internal_relocs;
12761 arelent * internal_reloc;
12762 unsigned int i;
12763 unsigned int entsize;
12764 unsigned int symcount;
12765 unsigned int reloc_count;
12766 size_t amt;
12767
12768 if (ebd->elf_info_to_howto == NULL)
12769 return false;
12770
12771 #if DEBUG_SECONDARY_RELOCS
12772 fprintf (stderr, "read secondary relocs for %s from %s\n",
12773 sec->name, relsec->name);
12774 #endif
12775 entsize = hdr->sh_entsize;
12776
12777 native_relocs = bfd_malloc (hdr->sh_size);
12778 if (native_relocs == NULL)
12779 {
12780 result = false;
12781 continue;
12782 }
12783
12784 reloc_count = NUM_SHDR_ENTRIES (hdr);
12785 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
12786 {
12787 free (native_relocs);
12788 bfd_set_error (bfd_error_file_too_big);
12789 result = false;
12790 continue;
12791 }
12792
12793 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
12794 if (internal_relocs == NULL)
12795 {
12796 free (native_relocs);
12797 result = false;
12798 continue;
12799 }
12800
12801 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
12802 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
12803 != hdr->sh_size))
12804 {
12805 free (native_relocs);
12806 /* The internal_relocs will be freed when
12807 the memory for the bfd is released. */
12808 result = false;
12809 continue;
12810 }
12811
12812 if (dynamic)
12813 symcount = bfd_get_dynamic_symcount (abfd);
12814 else
12815 symcount = bfd_get_symcount (abfd);
12816
12817 for (i = 0, internal_reloc = internal_relocs,
12818 native_reloc = native_relocs;
12819 i < reloc_count;
12820 i++, internal_reloc++, native_reloc += entsize)
12821 {
12822 bool res;
12823 Elf_Internal_Rela rela;
12824
12825 if (entsize == ebd->s->sizeof_rel)
12826 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
12827 else /* entsize == ebd->s->sizeof_rela */
12828 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
12829
12830 /* The address of an ELF reloc is section relative for an object
12831 file, and absolute for an executable file or shared library.
12832 The address of a normal BFD reloc is always section relative,
12833 and the address of a dynamic reloc is absolute.. */
12834 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
12835 internal_reloc->address = rela.r_offset;
12836 else
12837 internal_reloc->address = rela.r_offset - sec->vma;
12838
12839 if (r_sym (rela.r_info) == STN_UNDEF)
12840 {
12841 /* FIXME: This and the error case below mean that we
12842 have a symbol on relocs that is not elf_symbol_type. */
12843 internal_reloc->sym_ptr_ptr =
12844 bfd_abs_section_ptr->symbol_ptr_ptr;
12845 }
12846 else if (r_sym (rela.r_info) > symcount)
12847 {
12848 _bfd_error_handler
12849 /* xgettext:c-format */
12850 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12851 abfd, sec, i, (long) r_sym (rela.r_info));
12852 bfd_set_error (bfd_error_bad_value);
12853 internal_reloc->sym_ptr_ptr =
12854 bfd_abs_section_ptr->symbol_ptr_ptr;
12855 result = false;
12856 }
12857 else
12858 {
12859 asymbol **ps;
12860
12861 ps = symbols + r_sym (rela.r_info) - 1;
12862 internal_reloc->sym_ptr_ptr = ps;
12863 /* Make sure that this symbol is not removed by strip. */
12864 (*ps)->flags |= BSF_KEEP;
12865 }
12866
12867 internal_reloc->addend = rela.r_addend;
12868
12869 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
12870 if (! res || internal_reloc->howto == NULL)
12871 {
12872 #if DEBUG_SECONDARY_RELOCS
12873 fprintf (stderr, "there is no howto associated with reloc %lx\n",
12874 rela.r_info);
12875 #endif
12876 result = false;
12877 }
12878 }
12879
12880 free (native_relocs);
12881 /* Store the internal relocs. */
12882 elf_section_data (relsec)->sec_info = internal_relocs;
12883 }
12884 }
12885
12886 return result;
12887 }
12888
12889 /* Set the ELF section header fields of an output secondary reloc section. */
12890
12891 bool
12892 _bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
12893 bfd * obfd ATTRIBUTE_UNUSED,
12894 const Elf_Internal_Shdr * isection,
12895 Elf_Internal_Shdr * osection)
12896 {
12897 asection * isec;
12898 asection * osec;
12899 struct bfd_elf_section_data * esd;
12900
12901 if (isection == NULL)
12902 return false;
12903
12904 if (isection->sh_type != SHT_SECONDARY_RELOC)
12905 return true;
12906
12907 isec = isection->bfd_section;
12908 if (isec == NULL)
12909 return false;
12910
12911 osec = osection->bfd_section;
12912 if (osec == NULL)
12913 return false;
12914
12915 esd = elf_section_data (osec);
12916 BFD_ASSERT (esd->sec_info == NULL);
12917 esd->sec_info = elf_section_data (isec)->sec_info;
12918 osection->sh_type = SHT_RELA;
12919 osection->sh_link = elf_onesymtab (obfd);
12920 if (osection->sh_link == 0)
12921 {
12922 /* There is no symbol table - we are hosed... */
12923 _bfd_error_handler
12924 /* xgettext:c-format */
12925 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12926 obfd, osec);
12927 bfd_set_error (bfd_error_bad_value);
12928 return false;
12929 }
12930
12931 /* Find the output section that corresponds to the isection's sh_info link. */
12932 if (isection->sh_info == 0
12933 || isection->sh_info >= elf_numsections (ibfd))
12934 {
12935 _bfd_error_handler
12936 /* xgettext:c-format */
12937 (_("%pB(%pA): info section index is invalid"),
12938 obfd, osec);
12939 bfd_set_error (bfd_error_bad_value);
12940 return false;
12941 }
12942
12943 isection = elf_elfsections (ibfd)[isection->sh_info];
12944
12945 if (isection == NULL
12946 || isection->bfd_section == NULL
12947 || isection->bfd_section->output_section == NULL)
12948 {
12949 _bfd_error_handler
12950 /* xgettext:c-format */
12951 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12952 obfd, osec);
12953 bfd_set_error (bfd_error_bad_value);
12954 return false;
12955 }
12956
12957 esd = elf_section_data (isection->bfd_section->output_section);
12958 BFD_ASSERT (esd != NULL);
12959 osection->sh_info = esd->this_idx;
12960 esd->has_secondary_relocs = true;
12961 #if DEBUG_SECONDARY_RELOCS
12962 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
12963 osec->name, osection->sh_link, osection->sh_info);
12964 fprintf (stderr, "mark section %s as having secondary relocs\n",
12965 bfd_section_name (isection->bfd_section->output_section));
12966 #endif
12967
12968 return true;
12969 }
12970
12971 /* Write out a secondary reloc section.
12972
12973 FIXME: Currently this function can result in a serious performance penalty
12974 for files with secondary relocs and lots of sections. The proper way to
12975 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12976 relocs together and then to have this function just walk that chain. */
12977
12978 bool
12979 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
12980 {
12981 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
12982 bfd_vma addr_offset;
12983 asection * relsec;
12984 bfd_vma (*r_info) (bfd_vma, bfd_vma);
12985 bool result = true;
12986
12987 if (sec == NULL)
12988 return false;
12989
12990 #if BFD_DEFAULT_TARGET_SIZE > 32
12991 if (bfd_arch_bits_per_address (abfd) != 32)
12992 r_info = elf64_r_info;
12993 else
12994 #endif
12995 r_info = elf32_r_info;
12996
12997 /* The address of an ELF reloc is section relative for an object
12998 file, and absolute for an executable file or shared library.
12999 The address of a BFD reloc is always section relative. */
13000 addr_offset = 0;
13001 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13002 addr_offset = sec->vma;
13003
13004 /* Discover if there are any secondary reloc sections
13005 associated with SEC. */
13006 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13007 {
13008 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13009 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13010
13011 if (hdr->sh_type == SHT_RELA
13012 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13013 {
13014 asymbol * last_sym;
13015 int last_sym_idx;
13016 unsigned int reloc_count;
13017 unsigned int idx;
13018 unsigned int entsize;
13019 arelent * src_irel;
13020 bfd_byte * dst_rela;
13021
13022 if (hdr->contents != NULL)
13023 {
13024 _bfd_error_handler
13025 /* xgettext:c-format */
13026 (_("%pB(%pA): error: secondary reloc section processed twice"),
13027 abfd, relsec);
13028 bfd_set_error (bfd_error_bad_value);
13029 result = false;
13030 continue;
13031 }
13032
13033 entsize = hdr->sh_entsize;
13034 if (entsize == 0)
13035 {
13036 _bfd_error_handler
13037 /* xgettext:c-format */
13038 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
13039 abfd, relsec);
13040 bfd_set_error (bfd_error_bad_value);
13041 result = false;
13042 continue;
13043 }
13044 else if (entsize != ebd->s->sizeof_rel
13045 && entsize != ebd->s->sizeof_rela)
13046 {
13047 _bfd_error_handler
13048 /* xgettext:c-format */
13049 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
13050 abfd, relsec);
13051 bfd_set_error (bfd_error_bad_value);
13052 result = false;
13053 continue;
13054 }
13055
13056 reloc_count = hdr->sh_size / entsize;
13057 if (reloc_count <= 0)
13058 {
13059 _bfd_error_handler
13060 /* xgettext:c-format */
13061 (_("%pB(%pA): error: secondary reloc section is empty!"),
13062 abfd, relsec);
13063 bfd_set_error (bfd_error_bad_value);
13064 result = false;
13065 continue;
13066 }
13067
13068 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13069 if (hdr->contents == NULL)
13070 continue;
13071
13072 #if DEBUG_SECONDARY_RELOCS
13073 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13074 reloc_count, sec->name, relsec->name);
13075 #endif
13076 last_sym = NULL;
13077 last_sym_idx = 0;
13078 dst_rela = hdr->contents;
13079 src_irel = (arelent *) esd->sec_info;
13080 if (src_irel == NULL)
13081 {
13082 _bfd_error_handler
13083 /* xgettext:c-format */
13084 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
13085 abfd, relsec);
13086 bfd_set_error (bfd_error_bad_value);
13087 result = false;
13088 continue;
13089 }
13090
13091 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13092 {
13093 Elf_Internal_Rela src_rela;
13094 arelent *ptr;
13095 asymbol *sym;
13096 int n;
13097
13098 ptr = src_irel + idx;
13099 if (ptr == NULL)
13100 {
13101 _bfd_error_handler
13102 /* xgettext:c-format */
13103 (_("%pB(%pA): error: reloc table entry %u is empty"),
13104 abfd, relsec, idx);
13105 bfd_set_error (bfd_error_bad_value);
13106 result = false;
13107 break;
13108 }
13109
13110 if (ptr->sym_ptr_ptr == NULL)
13111 {
13112 /* FIXME: Is this an error ? */
13113 n = 0;
13114 }
13115 else
13116 {
13117 sym = *ptr->sym_ptr_ptr;
13118
13119 if (sym == last_sym)
13120 n = last_sym_idx;
13121 else
13122 {
13123 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13124 if (n < 0)
13125 {
13126 _bfd_error_handler
13127 /* xgettext:c-format */
13128 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
13129 abfd, relsec, idx);
13130 bfd_set_error (bfd_error_bad_value);
13131 result = false;
13132 n = 0;
13133 }
13134
13135 last_sym = sym;
13136 last_sym_idx = n;
13137 }
13138
13139 if (sym->the_bfd != NULL
13140 && sym->the_bfd->xvec != abfd->xvec
13141 && ! _bfd_elf_validate_reloc (abfd, ptr))
13142 {
13143 _bfd_error_handler
13144 /* xgettext:c-format */
13145 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
13146 abfd, relsec, idx);
13147 bfd_set_error (bfd_error_bad_value);
13148 result = false;
13149 n = 0;
13150 }
13151 }
13152
13153 src_rela.r_offset = ptr->address + addr_offset;
13154 if (ptr->howto == NULL)
13155 {
13156 _bfd_error_handler
13157 /* xgettext:c-format */
13158 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
13159 abfd, relsec, idx);
13160 bfd_set_error (bfd_error_bad_value);
13161 result = false;
13162 src_rela.r_info = r_info (0, 0);
13163 }
13164 else
13165 src_rela.r_info = r_info (n, ptr->howto->type);
13166 src_rela.r_addend = ptr->addend;
13167
13168 if (entsize == ebd->s->sizeof_rel)
13169 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13170 else /* entsize == ebd->s->sizeof_rela */
13171 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13172 }
13173 }
13174 }
13175
13176 return result;
13177 }
This page took 0.368362 seconds and 4 git commands to generate.