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