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