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