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