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