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