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