elf.c revision 1.11 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 if (link_info == NULL)
5954 return FALSE;
5955 /* Arrange for the linker to exit with an error, deleting
5956 the output file unless --noinhibit-exec is given. */
5957 link_info->callbacks->info ("%X");
5958 }
5959
5960 /* Check that all sections are in a PT_LOAD segment.
5961 Don't check funky gdb generated core files. */
5962 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5963 {
5964 bfd_boolean check_vma = TRUE;
5965
5966 for (i = 1; i < m->count; i++)
5967 if (m->sections[i]->vma == m->sections[i - 1]->vma
5968 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5969 ->this_hdr), p) != 0
5970 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5971 ->this_hdr), p) != 0)
5972 {
5973 /* Looks like we have overlays packed into the segment. */
5974 check_vma = FALSE;
5975 break;
5976 }
5977
5978 for (i = 0; i < m->count; i++)
5979 {
5980 Elf_Internal_Shdr *this_hdr;
5981 asection *sec;
5982
5983 sec = m->sections[i];
5984 this_hdr = &(elf_section_data(sec)->this_hdr);
5985 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5986 && !ELF_TBSS_SPECIAL (this_hdr, p))
5987 {
5988 _bfd_error_handler
5989 /* xgettext:c-format */
5990 (_("%pB: section `%pA' can't be allocated in segment %d"),
5991 abfd, sec, j);
5992 print_segment_map (m);
5993 }
5994 }
5995 }
5996 }
5997
5998 elf_next_file_pos (abfd) = off;
5999
6000 if (link_info != NULL
6001 && phdr_load_seg != NULL
6002 && phdr_load_seg->includes_filehdr)
6003 {
6004 /* There is a segment that contains both the file headers and the
6005 program headers, so provide a symbol __ehdr_start pointing there.
6006 A program can use this to examine itself robustly. */
6007
6008 struct elf_link_hash_entry *hash
6009 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6010 FALSE, FALSE, TRUE);
6011 /* If the symbol was referenced and not defined, define it. */
6012 if (hash != NULL
6013 && (hash->root.type == bfd_link_hash_new
6014 || hash->root.type == bfd_link_hash_undefined
6015 || hash->root.type == bfd_link_hash_undefweak
6016 || hash->root.type == bfd_link_hash_common))
6017 {
6018 asection *s = NULL;
6019 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr;
6020
6021 if (phdr_load_seg->count != 0)
6022 /* The segment contains sections, so use the first one. */
6023 s = phdr_load_seg->sections[0];
6024 else
6025 /* Use the first (i.e. lowest-addressed) section in any segment. */
6026 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6027 if (m->p_type == PT_LOAD && m->count != 0)
6028 {
6029 s = m->sections[0];
6030 break;
6031 }
6032
6033 if (s != NULL)
6034 {
6035 hash->root.u.def.value = filehdr_vaddr - s->vma;
6036 hash->root.u.def.section = s;
6037 }
6038 else
6039 {
6040 hash->root.u.def.value = filehdr_vaddr;
6041 hash->root.u.def.section = bfd_abs_section_ptr;
6042 }
6043
6044 hash->root.type = bfd_link_hash_defined;
6045 hash->def_regular = 1;
6046 hash->non_elf = 0;
6047 }
6048 }
6049
6050 return TRUE;
6051 }
6052
6053 /* Determine if a bfd is a debuginfo file. Unfortunately there
6054 is no defined method for detecting such files, so we have to
6055 use heuristics instead. */
6056
6057 bfd_boolean
6058 is_debuginfo_file (bfd *abfd)
6059 {
6060 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6061 return FALSE;
6062
6063 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6064 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6065 Elf_Internal_Shdr **headerp;
6066
6067 for (headerp = start_headers; headerp < end_headers; headerp ++)
6068 {
6069 Elf_Internal_Shdr *header = * headerp;
6070
6071 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6072 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6073 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6074 && header->sh_type != SHT_NOBITS
6075 && header->sh_type != SHT_NOTE)
6076 return FALSE;
6077 }
6078
6079 return TRUE;
6080 }
6081
6082 /* Assign file positions for the other sections, except for compressed debugging
6083 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6084
6085 static bfd_boolean
6086 assign_file_positions_for_non_load_sections (bfd *abfd,
6087 struct bfd_link_info *link_info)
6088 {
6089 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6090 Elf_Internal_Shdr **i_shdrpp;
6091 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6092 Elf_Internal_Phdr *phdrs;
6093 Elf_Internal_Phdr *p;
6094 struct elf_segment_map *m;
6095 file_ptr off;
6096
6097 i_shdrpp = elf_elfsections (abfd);
6098 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6099 off = elf_next_file_pos (abfd);
6100 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6101 {
6102 Elf_Internal_Shdr *hdr;
6103
6104 hdr = *hdrpp;
6105 if (hdr->bfd_section != NULL
6106 && (hdr->bfd_section->filepos != 0
6107 || (hdr->sh_type == SHT_NOBITS
6108 && hdr->contents == NULL)))
6109 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6110 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6111 {
6112 if (hdr->sh_size != 0
6113 /* PR 24717 - debuginfo files are known to be not strictly
6114 compliant with the ELF standard. In particular they often
6115 have .note.gnu.property sections that are outside of any
6116 loadable segment. This is not a problem for such files,
6117 so do not warn about them. */
6118 && ! is_debuginfo_file (abfd))
6119 _bfd_error_handler
6120 /* xgettext:c-format */
6121 (_("%pB: warning: allocated section `%s' not in segment"),
6122 abfd,
6123 (hdr->bfd_section == NULL
6124 ? "*unknown*"
6125 : hdr->bfd_section->name));
6126 /* We don't need to page align empty sections. */
6127 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6128 off += vma_page_aligned_bias (hdr->sh_addr, off,
6129 bed->maxpagesize);
6130 else
6131 off += vma_page_aligned_bias (hdr->sh_addr, off,
6132 hdr->sh_addralign);
6133 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6134 FALSE);
6135 }
6136 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6137 && hdr->bfd_section == NULL)
6138 /* We don't know the offset of these sections yet: their size has
6139 not been decided. */
6140 || (hdr->bfd_section != NULL
6141 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6142 || (bfd_section_is_ctf (hdr->bfd_section)
6143 && abfd->is_linker_output)))
6144 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6145 || (elf_symtab_shndx_list (abfd) != NULL
6146 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6147 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6148 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6149 hdr->sh_offset = -1;
6150 else
6151 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6152 }
6153 elf_next_file_pos (abfd) = off;
6154
6155 /* Now that we have set the section file positions, we can set up
6156 the file positions for the non PT_LOAD segments. */
6157 phdrs = elf_tdata (abfd)->phdr;
6158 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6159 {
6160 if (p->p_type == PT_GNU_RELRO)
6161 {
6162 bfd_vma start, end;
6163 bfd_boolean ok;
6164
6165 if (link_info != NULL)
6166 {
6167 /* During linking the range of the RELRO segment is passed
6168 in link_info. Note that there may be padding between
6169 relro_start and the first RELRO section. */
6170 start = link_info->relro_start;
6171 end = link_info->relro_end;
6172 }
6173 else if (m->count != 0)
6174 {
6175 if (!m->p_size_valid)
6176 abort ();
6177 start = m->sections[0]->vma;
6178 end = start + m->p_size;
6179 }
6180 else
6181 {
6182 start = 0;
6183 end = 0;
6184 }
6185
6186 ok = FALSE;
6187 if (start < end)
6188 {
6189 struct elf_segment_map *lm;
6190 const Elf_Internal_Phdr *lp;
6191 unsigned int i;
6192
6193 /* Find a LOAD segment containing a section in the RELRO
6194 segment. */
6195 for (lm = elf_seg_map (abfd), lp = phdrs;
6196 lm != NULL;
6197 lm = lm->next, lp++)
6198 {
6199 if (lp->p_type == PT_LOAD
6200 && lm->count != 0
6201 && (lm->sections[lm->count - 1]->vma
6202 + (!IS_TBSS (lm->sections[lm->count - 1])
6203 ? lm->sections[lm->count - 1]->size
6204 : 0)) > start
6205 && lm->sections[0]->vma < end)
6206 break;
6207 }
6208
6209 if (lm != NULL)
6210 {
6211 /* Find the section starting the RELRO segment. */
6212 for (i = 0; i < lm->count; i++)
6213 {
6214 asection *s = lm->sections[i];
6215 if (s->vma >= start
6216 && s->vma < end
6217 && s->size != 0)
6218 break;
6219 }
6220
6221 if (i < lm->count)
6222 {
6223 p->p_vaddr = lm->sections[i]->vma;
6224 p->p_paddr = lm->sections[i]->lma;
6225 p->p_offset = lm->sections[i]->filepos;
6226 p->p_memsz = end - p->p_vaddr;
6227 p->p_filesz = p->p_memsz;
6228
6229 /* The RELRO segment typically ends a few bytes
6230 into .got.plt but other layouts are possible.
6231 In cases where the end does not match any
6232 loaded section (for instance is in file
6233 padding), trim p_filesz back to correspond to
6234 the end of loaded section contents. */
6235 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6236 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6237
6238 /* Preserve the alignment and flags if they are
6239 valid. The gold linker generates RW/4 for
6240 the PT_GNU_RELRO section. It is better for
6241 objcopy/strip to honor these attributes
6242 otherwise gdb will choke when using separate
6243 debug files. */
6244 if (!m->p_align_valid)
6245 p->p_align = 1;
6246 if (!m->p_flags_valid)
6247 p->p_flags = PF_R;
6248 ok = TRUE;
6249 }
6250 }
6251 }
6252 if (link_info != NULL)
6253 BFD_ASSERT (ok);
6254 if (!ok)
6255 memset (p, 0, sizeof *p);
6256 }
6257 else if (p->p_type == PT_GNU_STACK)
6258 {
6259 if (m->p_size_valid)
6260 p->p_memsz = m->p_size;
6261 }
6262 else if (m->count != 0)
6263 {
6264 unsigned int i;
6265
6266 if (p->p_type != PT_LOAD
6267 && (p->p_type != PT_NOTE
6268 || bfd_get_format (abfd) != bfd_core))
6269 {
6270 /* A user specified segment layout may include a PHDR
6271 segment that overlaps with a LOAD segment... */
6272 if (p->p_type == PT_PHDR)
6273 {
6274 m->count = 0;
6275 continue;
6276 }
6277
6278 if (m->includes_filehdr || m->includes_phdrs)
6279 {
6280 /* PR 17512: file: 2195325e. */
6281 _bfd_error_handler
6282 (_("%pB: error: non-load segment %d includes file header "
6283 "and/or program header"),
6284 abfd, (int) (p - phdrs));
6285 return FALSE;
6286 }
6287
6288 p->p_filesz = 0;
6289 p->p_offset = m->sections[0]->filepos;
6290 for (i = m->count; i-- != 0;)
6291 {
6292 asection *sect = m->sections[i];
6293 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6294 if (hdr->sh_type != SHT_NOBITS)
6295 {
6296 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6297 + hdr->sh_size);
6298 break;
6299 }
6300 }
6301 }
6302 }
6303 }
6304
6305 return TRUE;
6306 }
6307
6308 static elf_section_list *
6309 find_section_in_list (unsigned int i, elf_section_list * list)
6310 {
6311 for (;list != NULL; list = list->next)
6312 if (list->ndx == i)
6313 break;
6314 return list;
6315 }
6316
6317 /* Work out the file positions of all the sections. This is called by
6318 _bfd_elf_compute_section_file_positions. All the section sizes and
6319 VMAs must be known before this is called.
6320
6321 Reloc sections come in two flavours: Those processed specially as
6322 "side-channel" data attached to a section to which they apply, and those that
6323 bfd doesn't process as relocations. The latter sort are stored in a normal
6324 bfd section by bfd_section_from_shdr. We don't consider the former sort
6325 here, unless they form part of the loadable image. Reloc sections not
6326 assigned here (and compressed debugging sections and CTF sections which
6327 nothing else in the file can rely upon) will be handled later by
6328 assign_file_positions_for_relocs.
6329
6330 We also don't set the positions of the .symtab and .strtab here. */
6331
6332 static bfd_boolean
6333 assign_file_positions_except_relocs (bfd *abfd,
6334 struct bfd_link_info *link_info)
6335 {
6336 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6337 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6339 unsigned int alloc;
6340
6341 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6342 && bfd_get_format (abfd) != bfd_core)
6343 {
6344 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6345 unsigned int num_sec = elf_numsections (abfd);
6346 Elf_Internal_Shdr **hdrpp;
6347 unsigned int i;
6348 file_ptr off;
6349
6350 /* Start after the ELF header. */
6351 off = i_ehdrp->e_ehsize;
6352
6353 /* We are not creating an executable, which means that we are
6354 not creating a program header, and that the actual order of
6355 the sections in the file is unimportant. */
6356 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6357 {
6358 Elf_Internal_Shdr *hdr;
6359
6360 hdr = *hdrpp;
6361 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6362 && hdr->bfd_section == NULL)
6363 /* Do not assign offsets for these sections yet: we don't know
6364 their sizes. */
6365 || (hdr->bfd_section != NULL
6366 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6367 || (bfd_section_is_ctf (hdr->bfd_section)
6368 && abfd->is_linker_output)))
6369 || i == elf_onesymtab (abfd)
6370 || (elf_symtab_shndx_list (abfd) != NULL
6371 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6372 || i == elf_strtab_sec (abfd)
6373 || i == elf_shstrtab_sec (abfd))
6374 {
6375 hdr->sh_offset = -1;
6376 }
6377 else
6378 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6379 }
6380
6381 elf_next_file_pos (abfd) = off;
6382 elf_program_header_size (abfd) = 0;
6383 }
6384 else
6385 {
6386 /* Assign file positions for the loaded sections based on the
6387 assignment of sections to segments. */
6388 if (!assign_file_positions_for_load_sections (abfd, link_info))
6389 return FALSE;
6390
6391 /* And for non-load sections. */
6392 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6393 return FALSE;
6394 }
6395
6396 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6397 return FALSE;
6398
6399 /* Write out the program headers. */
6400 alloc = i_ehdrp->e_phnum;
6401 if (alloc != 0)
6402 {
6403 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6404 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6405 return FALSE;
6406 }
6407
6408 return TRUE;
6409 }
6410
6411 bfd_boolean
6412 _bfd_elf_init_file_header (bfd *abfd,
6413 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6414 {
6415 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6416 struct elf_strtab_hash *shstrtab;
6417 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6418
6419 i_ehdrp = elf_elfheader (abfd);
6420
6421 shstrtab = _bfd_elf_strtab_init ();
6422 if (shstrtab == NULL)
6423 return FALSE;
6424
6425 elf_shstrtab (abfd) = shstrtab;
6426
6427 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6428 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6429 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6430 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6431
6432 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6433 i_ehdrp->e_ident[EI_DATA] =
6434 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6435 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6436
6437 if ((abfd->flags & DYNAMIC) != 0)
6438 i_ehdrp->e_type = ET_DYN;
6439 else if ((abfd->flags & EXEC_P) != 0)
6440 i_ehdrp->e_type = ET_EXEC;
6441 else if (bfd_get_format (abfd) == bfd_core)
6442 i_ehdrp->e_type = ET_CORE;
6443 else
6444 i_ehdrp->e_type = ET_REL;
6445
6446 switch (bfd_get_arch (abfd))
6447 {
6448 case bfd_arch_unknown:
6449 i_ehdrp->e_machine = EM_NONE;
6450 break;
6451
6452 /* There used to be a long list of cases here, each one setting
6453 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6454 in the corresponding bfd definition. To avoid duplication,
6455 the switch was removed. Machines that need special handling
6456 can generally do it in elf_backend_final_write_processing(),
6457 unless they need the information earlier than the final write.
6458 Such need can generally be supplied by replacing the tests for
6459 e_machine with the conditions used to determine it. */
6460 default:
6461 i_ehdrp->e_machine = bed->elf_machine_code;
6462 }
6463
6464 i_ehdrp->e_version = bed->s->ev_current;
6465 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6466
6467 /* No program header, for now. */
6468 i_ehdrp->e_phoff = 0;
6469 i_ehdrp->e_phentsize = 0;
6470 i_ehdrp->e_phnum = 0;
6471
6472 /* Each bfd section is section header entry. */
6473 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6474 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6475
6476 elf_tdata (abfd)->symtab_hdr.sh_name =
6477 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6478 elf_tdata (abfd)->strtab_hdr.sh_name =
6479 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6480 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6481 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6482 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6483 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6484 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6485 return FALSE;
6486
6487 return TRUE;
6488 }
6489
6490 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6491
6492 FIXME: We used to have code here to sort the PT_LOAD segments into
6493 ascending order, as per the ELF spec. But this breaks some programs,
6494 including the Linux kernel. But really either the spec should be
6495 changed or the programs updated. */
6496
6497 bfd_boolean
6498 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6499 {
6500 if (link_info != NULL && bfd_link_pie (link_info))
6501 {
6502 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6503 unsigned int num_segments = i_ehdrp->e_phnum;
6504 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6505 Elf_Internal_Phdr *segment = tdata->phdr;
6506 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6507
6508 /* Find the lowest p_vaddr in PT_LOAD segments. */
6509 bfd_vma p_vaddr = (bfd_vma) -1;
6510 for (; segment < end_segment; segment++)
6511 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6512 p_vaddr = segment->p_vaddr;
6513
6514 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6515 segments is non-zero. */
6516 if (p_vaddr)
6517 i_ehdrp->e_type = ET_EXEC;
6518 }
6519 return TRUE;
6520 }
6521
6522 /* Assign file positions for all the reloc sections which are not part
6523 of the loadable file image, and the file position of section headers. */
6524
6525 static bfd_boolean
6526 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6527 {
6528 file_ptr off;
6529 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6530 Elf_Internal_Shdr *shdrp;
6531 Elf_Internal_Ehdr *i_ehdrp;
6532 const struct elf_backend_data *bed;
6533
6534 off = elf_next_file_pos (abfd);
6535
6536 shdrpp = elf_elfsections (abfd);
6537 end_shdrpp = shdrpp + elf_numsections (abfd);
6538 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6539 {
6540 shdrp = *shdrpp;
6541 if (shdrp->sh_offset == -1)
6542 {
6543 asection *sec = shdrp->bfd_section;
6544 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6545 || shdrp->sh_type == SHT_RELA);
6546 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
6547 if (is_rel
6548 || is_ctf
6549 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6550 {
6551 if (!is_rel && !is_ctf)
6552 {
6553 const char *name = sec->name;
6554 struct bfd_elf_section_data *d;
6555
6556 /* Compress DWARF debug sections. */
6557 if (!bfd_compress_section (abfd, sec,
6558 shdrp->contents))
6559 return FALSE;
6560
6561 if (sec->compress_status == COMPRESS_SECTION_DONE
6562 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6563 {
6564 /* If section is compressed with zlib-gnu, convert
6565 section name from .debug_* to .zdebug_*. */
6566 char *new_name
6567 = convert_debug_to_zdebug (abfd, name);
6568 if (new_name == NULL)
6569 return FALSE;
6570 name = new_name;
6571 }
6572 /* Add section name to section name section. */
6573 if (shdrp->sh_name != (unsigned int) -1)
6574 abort ();
6575 shdrp->sh_name
6576 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6577 name, FALSE);
6578 d = elf_section_data (sec);
6579
6580 /* Add reloc section name to section name section. */
6581 if (d->rel.hdr
6582 && !_bfd_elf_set_reloc_sh_name (abfd,
6583 d->rel.hdr,
6584 name, FALSE))
6585 return FALSE;
6586 if (d->rela.hdr
6587 && !_bfd_elf_set_reloc_sh_name (abfd,
6588 d->rela.hdr,
6589 name, TRUE))
6590 return FALSE;
6591
6592 /* Update section size and contents. */
6593 shdrp->sh_size = sec->size;
6594 shdrp->contents = sec->contents;
6595 shdrp->bfd_section->contents = NULL;
6596 }
6597 else if (is_ctf)
6598 {
6599 /* Update section size and contents. */
6600 shdrp->sh_size = sec->size;
6601 shdrp->contents = sec->contents;
6602 }
6603
6604 off = _bfd_elf_assign_file_position_for_section (shdrp,
6605 off,
6606 TRUE);
6607 }
6608 }
6609 }
6610
6611 /* Place section name section after DWARF debug sections have been
6612 compressed. */
6613 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6614 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6615 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6616 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6617
6618 /* Place the section headers. */
6619 i_ehdrp = elf_elfheader (abfd);
6620 bed = get_elf_backend_data (abfd);
6621 off = align_file_position (off, 1 << bed->s->log_file_align);
6622 i_ehdrp->e_shoff = off;
6623 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6624 elf_next_file_pos (abfd) = off;
6625
6626 return TRUE;
6627 }
6628
6629 bfd_boolean
6630 _bfd_elf_write_object_contents (bfd *abfd)
6631 {
6632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6633 Elf_Internal_Shdr **i_shdrp;
6634 bfd_boolean failed;
6635 unsigned int count, num_sec;
6636 struct elf_obj_tdata *t;
6637
6638 if (! abfd->output_has_begun
6639 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6640 return FALSE;
6641 /* Do not rewrite ELF data when the BFD has been opened for update.
6642 abfd->output_has_begun was set to TRUE on opening, so creation of new
6643 sections, and modification of existing section sizes was restricted.
6644 This means the ELF header, program headers and section headers can't have
6645 changed.
6646 If the contents of any sections has been modified, then those changes have
6647 already been written to the BFD. */
6648 else if (abfd->direction == both_direction)
6649 {
6650 BFD_ASSERT (abfd->output_has_begun);
6651 return TRUE;
6652 }
6653
6654 i_shdrp = elf_elfsections (abfd);
6655
6656 failed = FALSE;
6657 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6658 if (failed)
6659 return FALSE;
6660
6661 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6662 return FALSE;
6663
6664 /* After writing the headers, we need to write the sections too... */
6665 num_sec = elf_numsections (abfd);
6666 for (count = 1; count < num_sec; count++)
6667 {
6668 i_shdrp[count]->sh_name
6669 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6670 i_shdrp[count]->sh_name);
6671 if (bed->elf_backend_section_processing)
6672 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6673 return FALSE;
6674 if (i_shdrp[count]->contents)
6675 {
6676 bfd_size_type amt = i_shdrp[count]->sh_size;
6677
6678 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6679 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6680 return FALSE;
6681 }
6682 }
6683
6684 /* Write out the section header names. */
6685 t = elf_tdata (abfd);
6686 if (elf_shstrtab (abfd) != NULL
6687 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6688 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6689 return FALSE;
6690
6691 if (!(*bed->elf_backend_final_write_processing) (abfd))
6692 return FALSE;
6693
6694 if (!bed->s->write_shdrs_and_ehdr (abfd))
6695 return FALSE;
6696
6697 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6698 if (t->o->build_id.after_write_object_contents != NULL)
6699 return (*t->o->build_id.after_write_object_contents) (abfd);
6700
6701 return TRUE;
6702 }
6703
6704 bfd_boolean
6705 _bfd_elf_write_corefile_contents (bfd *abfd)
6706 {
6707 /* Hopefully this can be done just like an object file. */
6708 return _bfd_elf_write_object_contents (abfd);
6709 }
6710
6711 /* Given a section, search the header to find them. */
6712
6713 unsigned int
6714 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6715 {
6716 const struct elf_backend_data *bed;
6717 unsigned int sec_index;
6718
6719 if (elf_section_data (asect) != NULL
6720 && elf_section_data (asect)->this_idx != 0)
6721 return elf_section_data (asect)->this_idx;
6722
6723 if (bfd_is_abs_section (asect))
6724 sec_index = SHN_ABS;
6725 else if (bfd_is_com_section (asect))
6726 sec_index = SHN_COMMON;
6727 else if (bfd_is_und_section (asect))
6728 sec_index = SHN_UNDEF;
6729 else
6730 sec_index = SHN_BAD;
6731
6732 bed = get_elf_backend_data (abfd);
6733 if (bed->elf_backend_section_from_bfd_section)
6734 {
6735 int retval = sec_index;
6736
6737 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6738 return retval;
6739 }
6740
6741 if (sec_index == SHN_BAD)
6742 bfd_set_error (bfd_error_nonrepresentable_section);
6743
6744 return sec_index;
6745 }
6746
6747 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6748 on error. */
6749
6750 int
6751 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6752 {
6753 asymbol *asym_ptr = *asym_ptr_ptr;
6754 int idx;
6755 flagword flags = asym_ptr->flags;
6756
6757 /* When gas creates relocations against local labels, it creates its
6758 own symbol for the section, but does put the symbol into the
6759 symbol chain, so udata is 0. When the linker is generating
6760 relocatable output, this section symbol may be for one of the
6761 input sections rather than the output section. */
6762 if (asym_ptr->udata.i == 0
6763 && (flags & BSF_SECTION_SYM)
6764 && asym_ptr->section)
6765 {
6766 asection *sec;
6767 int indx;
6768
6769 sec = asym_ptr->section;
6770 if (sec->owner != abfd && sec->output_section != NULL)
6771 sec = sec->output_section;
6772 if (sec->owner == abfd
6773 && (indx = sec->index) < elf_num_section_syms (abfd)
6774 && elf_section_syms (abfd)[indx] != NULL)
6775 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6776 }
6777
6778 idx = asym_ptr->udata.i;
6779
6780 if (idx == 0)
6781 {
6782 /* This case can occur when using --strip-symbol on a symbol
6783 which is used in a relocation entry. */
6784 _bfd_error_handler
6785 /* xgettext:c-format */
6786 (_("%pB: symbol `%s' required but not present"),
6787 abfd, bfd_asymbol_name (asym_ptr));
6788 bfd_set_error (bfd_error_no_symbols);
6789 return -1;
6790 }
6791
6792 #if DEBUG & 4
6793 {
6794 fprintf (stderr,
6795 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6796 (long) asym_ptr, asym_ptr->name, idx, flags);
6797 fflush (stderr);
6798 }
6799 #endif
6800
6801 return idx;
6802 }
6803
6804 /* Rewrite program header information. */
6805
6806 static bfd_boolean
6807 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6808 {
6809 Elf_Internal_Ehdr *iehdr;
6810 struct elf_segment_map *map;
6811 struct elf_segment_map *map_first;
6812 struct elf_segment_map **pointer_to_map;
6813 Elf_Internal_Phdr *segment;
6814 asection *section;
6815 unsigned int i;
6816 unsigned int num_segments;
6817 bfd_boolean phdr_included = FALSE;
6818 bfd_boolean p_paddr_valid;
6819 bfd_vma maxpagesize;
6820 struct elf_segment_map *phdr_adjust_seg = NULL;
6821 unsigned int phdr_adjust_num = 0;
6822 const struct elf_backend_data *bed;
6823
6824 bed = get_elf_backend_data (ibfd);
6825 iehdr = elf_elfheader (ibfd);
6826
6827 map_first = NULL;
6828 pointer_to_map = &map_first;
6829
6830 num_segments = elf_elfheader (ibfd)->e_phnum;
6831 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6832
6833 /* Returns the end address of the segment + 1. */
6834 #define SEGMENT_END(segment, start) \
6835 (start + (segment->p_memsz > segment->p_filesz \
6836 ? segment->p_memsz : segment->p_filesz))
6837
6838 #define SECTION_SIZE(section, segment) \
6839 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6840 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6841 ? section->size : 0)
6842
6843 /* Returns TRUE if the given section is contained within
6844 the given segment. VMA addresses are compared. */
6845 #define IS_CONTAINED_BY_VMA(section, segment) \
6846 (section->vma >= segment->p_vaddr \
6847 && (section->vma + SECTION_SIZE (section, segment) \
6848 <= (SEGMENT_END (segment, segment->p_vaddr))))
6849
6850 /* Returns TRUE if the given section is contained within
6851 the given segment. LMA addresses are compared. */
6852 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6853 (section->lma >= base \
6854 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
6855 && (section->lma + SECTION_SIZE (section, segment) \
6856 <= SEGMENT_END (segment, base)))
6857
6858 /* Handle PT_NOTE segment. */
6859 #define IS_NOTE(p, s) \
6860 (p->p_type == PT_NOTE \
6861 && elf_section_type (s) == SHT_NOTE \
6862 && (bfd_vma) s->filepos >= p->p_offset \
6863 && ((bfd_vma) s->filepos + s->size \
6864 <= p->p_offset + p->p_filesz))
6865
6866 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6867 etc. */
6868 #define IS_COREFILE_NOTE(p, s) \
6869 (IS_NOTE (p, s) \
6870 && bfd_get_format (ibfd) == bfd_core \
6871 && s->vma == 0 \
6872 && s->lma == 0)
6873
6874 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6875 linker, which generates a PT_INTERP section with p_vaddr and
6876 p_memsz set to 0. */
6877 #define IS_SOLARIS_PT_INTERP(p, s) \
6878 (p->p_vaddr == 0 \
6879 && p->p_paddr == 0 \
6880 && p->p_memsz == 0 \
6881 && p->p_filesz > 0 \
6882 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6883 && s->size > 0 \
6884 && (bfd_vma) s->filepos >= p->p_offset \
6885 && ((bfd_vma) s->filepos + s->size \
6886 <= p->p_offset + p->p_filesz))
6887
6888 /* Decide if the given section should be included in the given segment.
6889 A section will be included if:
6890 1. It is within the address space of the segment -- we use the LMA
6891 if that is set for the segment and the VMA otherwise,
6892 2. It is an allocated section or a NOTE section in a PT_NOTE
6893 segment.
6894 3. There is an output section associated with it,
6895 4. The section has not already been allocated to a previous segment.
6896 5. PT_GNU_STACK segments do not include any sections.
6897 6. PT_TLS segment includes only SHF_TLS sections.
6898 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6899 8. PT_DYNAMIC should not contain empty sections at the beginning
6900 (with the possible exception of .dynamic). */
6901 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6902 ((((segment->p_paddr \
6903 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6904 : IS_CONTAINED_BY_VMA (section, segment)) \
6905 && (section->flags & SEC_ALLOC) != 0) \
6906 || IS_NOTE (segment, section)) \
6907 && segment->p_type != PT_GNU_STACK \
6908 && (segment->p_type != PT_TLS \
6909 || (section->flags & SEC_THREAD_LOCAL)) \
6910 && (segment->p_type == PT_LOAD \
6911 || segment->p_type == PT_TLS \
6912 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6913 && (segment->p_type != PT_DYNAMIC \
6914 || SECTION_SIZE (section, segment) > 0 \
6915 || (segment->p_paddr \
6916 ? segment->p_paddr != section->lma \
6917 : segment->p_vaddr != section->vma) \
6918 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6919 && (segment->p_type != PT_LOAD || !section->segment_mark))
6920
6921 /* If the output section of a section in the input segment is NULL,
6922 it is removed from the corresponding output segment. */
6923 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6924 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6925 && section->output_section != NULL)
6926
6927 /* Returns TRUE iff seg1 starts after the end of seg2. */
6928 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6929 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6930
6931 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6932 their VMA address ranges and their LMA address ranges overlap.
6933 It is possible to have overlapping VMA ranges without overlapping LMA
6934 ranges. RedBoot images for example can have both .data and .bss mapped
6935 to the same VMA range, but with the .data section mapped to a different
6936 LMA. */
6937 #define SEGMENT_OVERLAPS(seg1, seg2) \
6938 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6939 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6940 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6941 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6942
6943 /* Initialise the segment mark field. */
6944 for (section = ibfd->sections; section != NULL; section = section->next)
6945 section->segment_mark = FALSE;
6946
6947 /* The Solaris linker creates program headers in which all the
6948 p_paddr fields are zero. When we try to objcopy or strip such a
6949 file, we get confused. Check for this case, and if we find it
6950 don't set the p_paddr_valid fields. */
6951 p_paddr_valid = FALSE;
6952 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6953 i < num_segments;
6954 i++, segment++)
6955 if (segment->p_paddr != 0)
6956 {
6957 p_paddr_valid = TRUE;
6958 break;
6959 }
6960
6961 /* Scan through the segments specified in the program header
6962 of the input BFD. For this first scan we look for overlaps
6963 in the loadable segments. These can be created by weird
6964 parameters to objcopy. Also, fix some solaris weirdness. */
6965 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6966 i < num_segments;
6967 i++, segment++)
6968 {
6969 unsigned int j;
6970 Elf_Internal_Phdr *segment2;
6971
6972 if (segment->p_type == PT_INTERP)
6973 for (section = ibfd->sections; section; section = section->next)
6974 if (IS_SOLARIS_PT_INTERP (segment, section))
6975 {
6976 /* Mininal change so that the normal section to segment
6977 assignment code will work. */
6978 segment->p_vaddr = section->vma;
6979 break;
6980 }
6981
6982 if (segment->p_type != PT_LOAD)
6983 {
6984 /* Remove PT_GNU_RELRO segment. */
6985 if (segment->p_type == PT_GNU_RELRO)
6986 segment->p_type = PT_NULL;
6987 continue;
6988 }
6989
6990 /* Determine if this segment overlaps any previous segments. */
6991 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6992 {
6993 bfd_signed_vma extra_length;
6994
6995 if (segment2->p_type != PT_LOAD
6996 || !SEGMENT_OVERLAPS (segment, segment2))
6997 continue;
6998
6999 /* Merge the two segments together. */
7000 if (segment2->p_vaddr < segment->p_vaddr)
7001 {
7002 /* Extend SEGMENT2 to include SEGMENT and then delete
7003 SEGMENT. */
7004 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7005 - SEGMENT_END (segment2, segment2->p_vaddr));
7006
7007 if (extra_length > 0)
7008 {
7009 segment2->p_memsz += extra_length;
7010 segment2->p_filesz += extra_length;
7011 }
7012
7013 segment->p_type = PT_NULL;
7014
7015 /* Since we have deleted P we must restart the outer loop. */
7016 i = 0;
7017 segment = elf_tdata (ibfd)->phdr;
7018 break;
7019 }
7020 else
7021 {
7022 /* Extend SEGMENT to include SEGMENT2 and then delete
7023 SEGMENT2. */
7024 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7025 - SEGMENT_END (segment, segment->p_vaddr));
7026
7027 if (extra_length > 0)
7028 {
7029 segment->p_memsz += extra_length;
7030 segment->p_filesz += extra_length;
7031 }
7032
7033 segment2->p_type = PT_NULL;
7034 }
7035 }
7036 }
7037
7038 /* The second scan attempts to assign sections to segments. */
7039 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7040 i < num_segments;
7041 i++, segment++)
7042 {
7043 unsigned int section_count;
7044 asection **sections;
7045 asection *output_section;
7046 unsigned int isec;
7047 asection *matching_lma;
7048 asection *suggested_lma;
7049 unsigned int j;
7050 bfd_size_type amt;
7051 asection *first_section;
7052
7053 if (segment->p_type == PT_NULL)
7054 continue;
7055
7056 first_section = NULL;
7057 /* Compute how many sections might be placed into this segment. */
7058 for (section = ibfd->sections, section_count = 0;
7059 section != NULL;
7060 section = section->next)
7061 {
7062 /* Find the first section in the input segment, which may be
7063 removed from the corresponding output segment. */
7064 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
7065 {
7066 if (first_section == NULL)
7067 first_section = section;
7068 if (section->output_section != NULL)
7069 ++section_count;
7070 }
7071 }
7072
7073 /* Allocate a segment map big enough to contain
7074 all of the sections we have selected. */
7075 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7076 amt += (bfd_size_type) section_count * sizeof (asection *);
7077 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7078 if (map == NULL)
7079 return FALSE;
7080
7081 /* Initialise the fields of the segment map. Default to
7082 using the physical address of the segment in the input BFD. */
7083 map->next = NULL;
7084 map->p_type = segment->p_type;
7085 map->p_flags = segment->p_flags;
7086 map->p_flags_valid = 1;
7087
7088 /* If the first section in the input segment is removed, there is
7089 no need to preserve segment physical address in the corresponding
7090 output segment. */
7091 if (!first_section || first_section->output_section != NULL)
7092 {
7093 map->p_paddr = segment->p_paddr;
7094 map->p_paddr_valid = p_paddr_valid;
7095 }
7096
7097 /* Determine if this segment contains the ELF file header
7098 and if it contains the program headers themselves. */
7099 map->includes_filehdr = (segment->p_offset == 0
7100 && segment->p_filesz >= iehdr->e_ehsize);
7101 map->includes_phdrs = 0;
7102
7103 if (!phdr_included || segment->p_type != PT_LOAD)
7104 {
7105 map->includes_phdrs =
7106 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7107 && (segment->p_offset + segment->p_filesz
7108 >= ((bfd_vma) iehdr->e_phoff
7109 + iehdr->e_phnum * iehdr->e_phentsize)));
7110
7111 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7112 phdr_included = TRUE;
7113 }
7114
7115 if (section_count == 0)
7116 {
7117 /* Special segments, such as the PT_PHDR segment, may contain
7118 no sections, but ordinary, loadable segments should contain
7119 something. They are allowed by the ELF spec however, so only
7120 a warning is produced.
7121 There is however the valid use case of embedded systems which
7122 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7123 flash memory with zeros. No warning is shown for that case. */
7124 if (segment->p_type == PT_LOAD
7125 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7126 /* xgettext:c-format */
7127 _bfd_error_handler
7128 (_("%pB: warning: empty loadable segment detected"
7129 " at vaddr=%#" PRIx64 ", is this intentional?"),
7130 ibfd, (uint64_t) segment->p_vaddr);
7131
7132 map->p_vaddr_offset = segment->p_vaddr;
7133 map->count = 0;
7134 *pointer_to_map = map;
7135 pointer_to_map = &map->next;
7136
7137 continue;
7138 }
7139
7140 /* Now scan the sections in the input BFD again and attempt
7141 to add their corresponding output sections to the segment map.
7142 The problem here is how to handle an output section which has
7143 been moved (ie had its LMA changed). There are four possibilities:
7144
7145 1. None of the sections have been moved.
7146 In this case we can continue to use the segment LMA from the
7147 input BFD.
7148
7149 2. All of the sections have been moved by the same amount.
7150 In this case we can change the segment's LMA to match the LMA
7151 of the first section.
7152
7153 3. Some of the sections have been moved, others have not.
7154 In this case those sections which have not been moved can be
7155 placed in the current segment which will have to have its size,
7156 and possibly its LMA changed, and a new segment or segments will
7157 have to be created to contain the other sections.
7158
7159 4. The sections have been moved, but not by the same amount.
7160 In this case we can change the segment's LMA to match the LMA
7161 of the first section and we will have to create a new segment
7162 or segments to contain the other sections.
7163
7164 In order to save time, we allocate an array to hold the section
7165 pointers that we are interested in. As these sections get assigned
7166 to a segment, they are removed from this array. */
7167
7168 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
7169 if (sections == NULL)
7170 return FALSE;
7171
7172 /* Step One: Scan for segment vs section LMA conflicts.
7173 Also add the sections to the section array allocated above.
7174 Also add the sections to the current segment. In the common
7175 case, where the sections have not been moved, this means that
7176 we have completely filled the segment, and there is nothing
7177 more to do. */
7178 isec = 0;
7179 matching_lma = NULL;
7180 suggested_lma = NULL;
7181
7182 for (section = first_section, j = 0;
7183 section != NULL;
7184 section = section->next)
7185 {
7186 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
7187 {
7188 output_section = section->output_section;
7189
7190 sections[j++] = section;
7191
7192 /* The Solaris native linker always sets p_paddr to 0.
7193 We try to catch that case here, and set it to the
7194 correct value. Note - some backends require that
7195 p_paddr be left as zero. */
7196 if (!p_paddr_valid
7197 && segment->p_vaddr != 0
7198 && !bed->want_p_paddr_set_to_zero
7199 && isec == 0
7200 && output_section->lma != 0
7201 && (align_power (segment->p_vaddr
7202 + (map->includes_filehdr
7203 ? iehdr->e_ehsize : 0)
7204 + (map->includes_phdrs
7205 ? iehdr->e_phnum * iehdr->e_phentsize
7206 : 0),
7207 output_section->alignment_power)
7208 == output_section->vma))
7209 map->p_paddr = segment->p_vaddr;
7210
7211 /* Match up the physical address of the segment with the
7212 LMA address of the output section. */
7213 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7214 || IS_COREFILE_NOTE (segment, section)
7215 || (bed->want_p_paddr_set_to_zero
7216 && IS_CONTAINED_BY_VMA (output_section, segment)))
7217 {
7218 if (matching_lma == NULL
7219 || output_section->lma < matching_lma->lma)
7220 matching_lma = output_section;
7221
7222 /* We assume that if the section fits within the segment
7223 then it does not overlap any other section within that
7224 segment. */
7225 map->sections[isec++] = output_section;
7226 }
7227 else if (suggested_lma == NULL)
7228 suggested_lma = output_section;
7229
7230 if (j == section_count)
7231 break;
7232 }
7233 }
7234
7235 BFD_ASSERT (j == section_count);
7236
7237 /* Step Two: Adjust the physical address of the current segment,
7238 if necessary. */
7239 if (isec == section_count)
7240 {
7241 /* All of the sections fitted within the segment as currently
7242 specified. This is the default case. Add the segment to
7243 the list of built segments and carry on to process the next
7244 program header in the input BFD. */
7245 map->count = section_count;
7246 *pointer_to_map = map;
7247 pointer_to_map = &map->next;
7248
7249 if (p_paddr_valid
7250 && !bed->want_p_paddr_set_to_zero)
7251 {
7252 bfd_vma hdr_size = 0;
7253 if (map->includes_filehdr)
7254 hdr_size = iehdr->e_ehsize;
7255 if (map->includes_phdrs)
7256 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7257
7258 /* Account for padding before the first section in the
7259 segment. */
7260 map->p_vaddr_offset = map->p_paddr + hdr_size - matching_lma->lma;
7261 }
7262
7263 free (sections);
7264 continue;
7265 }
7266 else
7267 {
7268 /* Change the current segment's physical address to match
7269 the LMA of the first section that fitted, or if no
7270 section fitted, the first section. */
7271 if (matching_lma == NULL)
7272 matching_lma = suggested_lma;
7273
7274 map->p_paddr = matching_lma->lma;
7275
7276 /* Offset the segment physical address from the lma
7277 to allow for space taken up by elf headers. */
7278 if (map->includes_phdrs)
7279 {
7280 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7281
7282 /* iehdr->e_phnum is just an estimate of the number
7283 of program headers that we will need. Make a note
7284 here of the number we used and the segment we chose
7285 to hold these headers, so that we can adjust the
7286 offset when we know the correct value. */
7287 phdr_adjust_num = iehdr->e_phnum;
7288 phdr_adjust_seg = map;
7289 }
7290
7291 if (map->includes_filehdr)
7292 {
7293 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7294 map->p_paddr -= iehdr->e_ehsize;
7295 /* We've subtracted off the size of headers from the
7296 first section lma, but there may have been some
7297 alignment padding before that section too. Try to
7298 account for that by adjusting the segment lma down to
7299 the same alignment. */
7300 if (segment->p_align != 0 && segment->p_align < align)
7301 align = segment->p_align;
7302 map->p_paddr &= -align;
7303 }
7304 }
7305
7306 /* Step Three: Loop over the sections again, this time assigning
7307 those that fit to the current segment and removing them from the
7308 sections array; but making sure not to leave large gaps. Once all
7309 possible sections have been assigned to the current segment it is
7310 added to the list of built segments and if sections still remain
7311 to be assigned, a new segment is constructed before repeating
7312 the loop. */
7313 isec = 0;
7314 do
7315 {
7316 map->count = 0;
7317 suggested_lma = NULL;
7318
7319 /* Fill the current segment with sections that fit. */
7320 for (j = 0; j < section_count; j++)
7321 {
7322 section = sections[j];
7323
7324 if (section == NULL)
7325 continue;
7326
7327 output_section = section->output_section;
7328
7329 BFD_ASSERT (output_section != NULL);
7330
7331 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7332 || IS_COREFILE_NOTE (segment, section))
7333 {
7334 if (map->count == 0)
7335 {
7336 /* If the first section in a segment does not start at
7337 the beginning of the segment, then something is
7338 wrong. */
7339 if (align_power (map->p_paddr
7340 + (map->includes_filehdr
7341 ? iehdr->e_ehsize : 0)
7342 + (map->includes_phdrs
7343 ? iehdr->e_phnum * iehdr->e_phentsize
7344 : 0),
7345 output_section->alignment_power)
7346 != output_section->lma)
7347 goto sorry;
7348 }
7349 else
7350 {
7351 asection *prev_sec;
7352
7353 prev_sec = map->sections[map->count - 1];
7354
7355 /* If the gap between the end of the previous section
7356 and the start of this section is more than
7357 maxpagesize then we need to start a new segment. */
7358 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7359 maxpagesize)
7360 < BFD_ALIGN (output_section->lma, maxpagesize))
7361 || (prev_sec->lma + prev_sec->size
7362 > output_section->lma))
7363 {
7364 if (suggested_lma == NULL)
7365 suggested_lma = output_section;
7366
7367 continue;
7368 }
7369 }
7370
7371 map->sections[map->count++] = output_section;
7372 ++isec;
7373 sections[j] = NULL;
7374 if (segment->p_type == PT_LOAD)
7375 section->segment_mark = TRUE;
7376 }
7377 else if (suggested_lma == NULL)
7378 suggested_lma = output_section;
7379 }
7380
7381 /* PR 23932. A corrupt input file may contain sections that cannot
7382 be assigned to any segment - because for example they have a
7383 negative size - or segments that do not contain any sections. */
7384 if (map->count == 0)
7385 {
7386 sorry:
7387 bfd_set_error (bfd_error_sorry);
7388 free (sections);
7389 return FALSE;
7390 }
7391
7392 /* Add the current segment to the list of built segments. */
7393 *pointer_to_map = map;
7394 pointer_to_map = &map->next;
7395
7396 if (isec < section_count)
7397 {
7398 /* We still have not allocated all of the sections to
7399 segments. Create a new segment here, initialise it
7400 and carry on looping. */
7401 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7402 amt += (bfd_size_type) section_count * sizeof (asection *);
7403 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7404 if (map == NULL)
7405 {
7406 free (sections);
7407 return FALSE;
7408 }
7409
7410 /* Initialise the fields of the segment map. Set the physical
7411 physical address to the LMA of the first section that has
7412 not yet been assigned. */
7413 map->next = NULL;
7414 map->p_type = segment->p_type;
7415 map->p_flags = segment->p_flags;
7416 map->p_flags_valid = 1;
7417 map->p_paddr = suggested_lma->lma;
7418 map->p_paddr_valid = p_paddr_valid;
7419 map->includes_filehdr = 0;
7420 map->includes_phdrs = 0;
7421 }
7422 }
7423 while (isec < section_count);
7424
7425 free (sections);
7426 }
7427
7428 elf_seg_map (obfd) = map_first;
7429
7430 /* If we had to estimate the number of program headers that were
7431 going to be needed, then check our estimate now and adjust
7432 the offset if necessary. */
7433 if (phdr_adjust_seg != NULL)
7434 {
7435 unsigned int count;
7436
7437 for (count = 0, map = map_first; map != NULL; map = map->next)
7438 count++;
7439
7440 if (count > phdr_adjust_num)
7441 phdr_adjust_seg->p_paddr
7442 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7443
7444 for (map = map_first; map != NULL; map = map->next)
7445 if (map->p_type == PT_PHDR)
7446 {
7447 bfd_vma adjust
7448 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7449 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7450 break;
7451 }
7452 }
7453
7454 #undef SEGMENT_END
7455 #undef SECTION_SIZE
7456 #undef IS_CONTAINED_BY_VMA
7457 #undef IS_CONTAINED_BY_LMA
7458 #undef IS_NOTE
7459 #undef IS_COREFILE_NOTE
7460 #undef IS_SOLARIS_PT_INTERP
7461 #undef IS_SECTION_IN_INPUT_SEGMENT
7462 #undef INCLUDE_SECTION_IN_SEGMENT
7463 #undef SEGMENT_AFTER_SEGMENT
7464 #undef SEGMENT_OVERLAPS
7465 return TRUE;
7466 }
7467
7468 /* Copy ELF program header information. */
7469
7470 static bfd_boolean
7471 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7472 {
7473 Elf_Internal_Ehdr *iehdr;
7474 struct elf_segment_map *map;
7475 struct elf_segment_map *map_first;
7476 struct elf_segment_map **pointer_to_map;
7477 Elf_Internal_Phdr *segment;
7478 unsigned int i;
7479 unsigned int num_segments;
7480 bfd_boolean phdr_included = FALSE;
7481 bfd_boolean p_paddr_valid;
7482
7483 iehdr = elf_elfheader (ibfd);
7484
7485 map_first = NULL;
7486 pointer_to_map = &map_first;
7487
7488 /* If all the segment p_paddr fields are zero, don't set
7489 map->p_paddr_valid. */
7490 p_paddr_valid = FALSE;
7491 num_segments = elf_elfheader (ibfd)->e_phnum;
7492 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7493 i < num_segments;
7494 i++, segment++)
7495 if (segment->p_paddr != 0)
7496 {
7497 p_paddr_valid = TRUE;
7498 break;
7499 }
7500
7501 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7502 i < num_segments;
7503 i++, segment++)
7504 {
7505 asection *section;
7506 unsigned int section_count;
7507 bfd_size_type amt;
7508 Elf_Internal_Shdr *this_hdr;
7509 asection *first_section = NULL;
7510 asection *lowest_section;
7511
7512 /* Compute how many sections are in this segment. */
7513 for (section = ibfd->sections, section_count = 0;
7514 section != NULL;
7515 section = section->next)
7516 {
7517 this_hdr = &(elf_section_data(section)->this_hdr);
7518 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7519 {
7520 if (first_section == NULL)
7521 first_section = section;
7522 section_count++;
7523 }
7524 }
7525
7526 /* Allocate a segment map big enough to contain
7527 all of the sections we have selected. */
7528 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7529 amt += (bfd_size_type) section_count * sizeof (asection *);
7530 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7531 if (map == NULL)
7532 return FALSE;
7533
7534 /* Initialize the fields of the output segment map with the
7535 input segment. */
7536 map->next = NULL;
7537 map->p_type = segment->p_type;
7538 map->p_flags = segment->p_flags;
7539 map->p_flags_valid = 1;
7540 map->p_paddr = segment->p_paddr;
7541 map->p_paddr_valid = p_paddr_valid;
7542 map->p_align = segment->p_align;
7543 map->p_align_valid = 1;
7544 map->p_vaddr_offset = 0;
7545
7546 if (map->p_type == PT_GNU_RELRO
7547 || map->p_type == PT_GNU_STACK)
7548 {
7549 /* The PT_GNU_RELRO segment may contain the first a few
7550 bytes in the .got.plt section even if the whole .got.plt
7551 section isn't in the PT_GNU_RELRO segment. We won't
7552 change the size of the PT_GNU_RELRO segment.
7553 Similarly, PT_GNU_STACK size is significant on uclinux
7554 systems. */
7555 map->p_size = segment->p_memsz;
7556 map->p_size_valid = 1;
7557 }
7558
7559 /* Determine if this segment contains the ELF file header
7560 and if it contains the program headers themselves. */
7561 map->includes_filehdr = (segment->p_offset == 0
7562 && segment->p_filesz >= iehdr->e_ehsize);
7563
7564 map->includes_phdrs = 0;
7565 if (! phdr_included || segment->p_type != PT_LOAD)
7566 {
7567 map->includes_phdrs =
7568 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7569 && (segment->p_offset + segment->p_filesz
7570 >= ((bfd_vma) iehdr->e_phoff
7571 + iehdr->e_phnum * iehdr->e_phentsize)));
7572
7573 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7574 phdr_included = TRUE;
7575 }
7576
7577 lowest_section = NULL;
7578 if (section_count != 0)
7579 {
7580 unsigned int isec = 0;
7581
7582 for (section = first_section;
7583 section != NULL;
7584 section = section->next)
7585 {
7586 this_hdr = &(elf_section_data(section)->this_hdr);
7587 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7588 {
7589 map->sections[isec++] = section->output_section;
7590 if ((section->flags & SEC_ALLOC) != 0)
7591 {
7592 bfd_vma seg_off;
7593
7594 if (lowest_section == NULL
7595 || section->lma < lowest_section->lma)
7596 lowest_section = section;
7597
7598 /* Section lmas are set up from PT_LOAD header
7599 p_paddr in _bfd_elf_make_section_from_shdr.
7600 If this header has a p_paddr that disagrees
7601 with the section lma, flag the p_paddr as
7602 invalid. */
7603 if ((section->flags & SEC_LOAD) != 0)
7604 seg_off = this_hdr->sh_offset - segment->p_offset;
7605 else
7606 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7607 if (section->lma - segment->p_paddr != seg_off)
7608 map->p_paddr_valid = FALSE;
7609 }
7610 if (isec == section_count)
7611 break;
7612 }
7613 }
7614 }
7615
7616 if (section_count == 0)
7617 map->p_vaddr_offset = segment->p_vaddr;
7618 else if (map->p_paddr_valid)
7619 {
7620 /* Account for padding before the first section in the segment. */
7621 bfd_vma hdr_size = 0;
7622 if (map->includes_filehdr)
7623 hdr_size = iehdr->e_ehsize;
7624 if (map->includes_phdrs)
7625 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7626
7627 map->p_vaddr_offset = (map->p_paddr + hdr_size
7628 - (lowest_section ? lowest_section->lma : 0));
7629 }
7630
7631 map->count = section_count;
7632 *pointer_to_map = map;
7633 pointer_to_map = &map->next;
7634 }
7635
7636 elf_seg_map (obfd) = map_first;
7637 return TRUE;
7638 }
7639
7640 /* Copy private BFD data. This copies or rewrites ELF program header
7641 information. */
7642
7643 static bfd_boolean
7644 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7645 {
7646 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7647 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7648 return TRUE;
7649
7650 if (elf_tdata (ibfd)->phdr == NULL)
7651 return TRUE;
7652
7653 if (ibfd->xvec == obfd->xvec)
7654 {
7655 /* Check to see if any sections in the input BFD
7656 covered by ELF program header have changed. */
7657 Elf_Internal_Phdr *segment;
7658 asection *section, *osec;
7659 unsigned int i, num_segments;
7660 Elf_Internal_Shdr *this_hdr;
7661 const struct elf_backend_data *bed;
7662
7663 bed = get_elf_backend_data (ibfd);
7664
7665 /* Regenerate the segment map if p_paddr is set to 0. */
7666 if (bed->want_p_paddr_set_to_zero)
7667 goto rewrite;
7668
7669 /* Initialize the segment mark field. */
7670 for (section = obfd->sections; section != NULL;
7671 section = section->next)
7672 section->segment_mark = FALSE;
7673
7674 num_segments = elf_elfheader (ibfd)->e_phnum;
7675 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7676 i < num_segments;
7677 i++, segment++)
7678 {
7679 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7680 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7681 which severly confuses things, so always regenerate the segment
7682 map in this case. */
7683 if (segment->p_paddr == 0
7684 && segment->p_memsz == 0
7685 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7686 goto rewrite;
7687
7688 for (section = ibfd->sections;
7689 section != NULL; section = section->next)
7690 {
7691 /* We mark the output section so that we know it comes
7692 from the input BFD. */
7693 osec = section->output_section;
7694 if (osec)
7695 osec->segment_mark = TRUE;
7696
7697 /* Check if this section is covered by the segment. */
7698 this_hdr = &(elf_section_data(section)->this_hdr);
7699 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7700 {
7701 /* FIXME: Check if its output section is changed or
7702 removed. What else do we need to check? */
7703 if (osec == NULL
7704 || section->flags != osec->flags
7705 || section->lma != osec->lma
7706 || section->vma != osec->vma
7707 || section->size != osec->size
7708 || section->rawsize != osec->rawsize
7709 || section->alignment_power != osec->alignment_power)
7710 goto rewrite;
7711 }
7712 }
7713 }
7714
7715 /* Check to see if any output section do not come from the
7716 input BFD. */
7717 for (section = obfd->sections; section != NULL;
7718 section = section->next)
7719 {
7720 if (!section->segment_mark)
7721 goto rewrite;
7722 else
7723 section->segment_mark = FALSE;
7724 }
7725
7726 return copy_elf_program_header (ibfd, obfd);
7727 }
7728
7729 rewrite:
7730 if (ibfd->xvec == obfd->xvec)
7731 {
7732 /* When rewriting program header, set the output maxpagesize to
7733 the maximum alignment of input PT_LOAD segments. */
7734 Elf_Internal_Phdr *segment;
7735 unsigned int i;
7736 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7737 bfd_vma maxpagesize = 0;
7738
7739 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7740 i < num_segments;
7741 i++, segment++)
7742 if (segment->p_type == PT_LOAD
7743 && maxpagesize < segment->p_align)
7744 {
7745 /* PR 17512: file: f17299af. */
7746 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7747 /* xgettext:c-format */
7748 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7749 PRIx64 " is too large"),
7750 ibfd, (uint64_t) segment->p_align);
7751 else
7752 maxpagesize = segment->p_align;
7753 }
7754
7755 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7756 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7757 }
7758
7759 return rewrite_elf_program_header (ibfd, obfd);
7760 }
7761
7762 /* Initialize private output section information from input section. */
7763
7764 bfd_boolean
7765 _bfd_elf_init_private_section_data (bfd *ibfd,
7766 asection *isec,
7767 bfd *obfd,
7768 asection *osec,
7769 struct bfd_link_info *link_info)
7770
7771 {
7772 Elf_Internal_Shdr *ihdr, *ohdr;
7773 bfd_boolean final_link = (link_info != NULL
7774 && !bfd_link_relocatable (link_info));
7775
7776 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7777 || obfd->xvec->flavour != bfd_target_elf_flavour)
7778 return TRUE;
7779
7780 BFD_ASSERT (elf_section_data (osec) != NULL);
7781
7782 /* For objcopy and relocatable link, don't copy the output ELF
7783 section type from input if the output BFD section flags have been
7784 set to something different. For a final link allow some flags
7785 that the linker clears to differ. */
7786 if (elf_section_type (osec) == SHT_NULL
7787 && (osec->flags == isec->flags
7788 || (final_link
7789 && ((osec->flags ^ isec->flags)
7790 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7791 elf_section_type (osec) = elf_section_type (isec);
7792
7793 /* FIXME: Is this correct for all OS/PROC specific flags? */
7794 elf_section_flags (osec) |= (elf_section_flags (isec)
7795 & (SHF_MASKOS | SHF_MASKPROC));
7796
7797 /* Copy sh_info from input for mbind section. */
7798 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7799 && elf_section_flags (isec) & SHF_GNU_MBIND)
7800 elf_section_data (osec)->this_hdr.sh_info
7801 = elf_section_data (isec)->this_hdr.sh_info;
7802
7803 /* Set things up for objcopy and relocatable link. The output
7804 SHT_GROUP section will have its elf_next_in_group pointing back
7805 to the input group members. Ignore linker created group section.
7806 See elfNN_ia64_object_p in elfxx-ia64.c. */
7807 if ((link_info == NULL
7808 || !link_info->resolve_section_groups)
7809 && (elf_sec_group (isec) == NULL
7810 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7811 {
7812 if (elf_section_flags (isec) & SHF_GROUP)
7813 elf_section_flags (osec) |= SHF_GROUP;
7814 elf_next_in_group (osec) = elf_next_in_group (isec);
7815 elf_section_data (osec)->group = elf_section_data (isec)->group;
7816 }
7817
7818 /* If not decompress, preserve SHF_COMPRESSED. */
7819 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7820 elf_section_flags (osec) |= (elf_section_flags (isec)
7821 & SHF_COMPRESSED);
7822
7823 ihdr = &elf_section_data (isec)->this_hdr;
7824
7825 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7826 don't use the output section of the linked-to section since it
7827 may be NULL at this point. */
7828 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7829 {
7830 ohdr = &elf_section_data (osec)->this_hdr;
7831 ohdr->sh_flags |= SHF_LINK_ORDER;
7832 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7833 }
7834
7835 osec->use_rela_p = isec->use_rela_p;
7836
7837 return TRUE;
7838 }
7839
7840 /* Copy private section information. This copies over the entsize
7841 field, and sometimes the info field. */
7842
7843 bfd_boolean
7844 _bfd_elf_copy_private_section_data (bfd *ibfd,
7845 asection *isec,
7846 bfd *obfd,
7847 asection *osec)
7848 {
7849 Elf_Internal_Shdr *ihdr, *ohdr;
7850
7851 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7852 || obfd->xvec->flavour != bfd_target_elf_flavour)
7853 return TRUE;
7854
7855 ihdr = &elf_section_data (isec)->this_hdr;
7856 ohdr = &elf_section_data (osec)->this_hdr;
7857
7858 ohdr->sh_entsize = ihdr->sh_entsize;
7859
7860 if (ihdr->sh_type == SHT_SYMTAB
7861 || ihdr->sh_type == SHT_DYNSYM
7862 || ihdr->sh_type == SHT_GNU_verneed
7863 || ihdr->sh_type == SHT_GNU_verdef)
7864 ohdr->sh_info = ihdr->sh_info;
7865
7866 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7867 NULL);
7868 }
7869
7870 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7871 necessary if we are removing either the SHT_GROUP section or any of
7872 the group member sections. DISCARDED is the value that a section's
7873 output_section has if the section will be discarded, NULL when this
7874 function is called from objcopy, bfd_abs_section_ptr when called
7875 from the linker. */
7876
7877 bfd_boolean
7878 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7879 {
7880 asection *isec;
7881
7882 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7883 if (elf_section_type (isec) == SHT_GROUP)
7884 {
7885 asection *first = elf_next_in_group (isec);
7886 asection *s = first;
7887 bfd_size_type removed = 0;
7888
7889 while (s != NULL)
7890 {
7891 /* If this member section is being output but the
7892 SHT_GROUP section is not, then clear the group info
7893 set up by _bfd_elf_copy_private_section_data. */
7894 if (s->output_section != discarded
7895 && isec->output_section == discarded)
7896 {
7897 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7898 elf_group_name (s->output_section) = NULL;
7899 }
7900 /* Conversely, if the member section is not being output
7901 but the SHT_GROUP section is, then adjust its size. */
7902 else if (s->output_section == discarded
7903 && isec->output_section != discarded)
7904 {
7905 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7906 removed += 4;
7907 if (elf_sec->rel.hdr != NULL
7908 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7909 removed += 4;
7910 if (elf_sec->rela.hdr != NULL
7911 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7912 removed += 4;
7913 }
7914 s = elf_next_in_group (s);
7915 if (s == first)
7916 break;
7917 }
7918 if (removed != 0)
7919 {
7920 if (discarded != NULL)
7921 {
7922 /* If we've been called for ld -r, then we need to
7923 adjust the input section size. */
7924 if (isec->rawsize == 0)
7925 isec->rawsize = isec->size;
7926 isec->size = isec->rawsize - removed;
7927 if (isec->size <= 4)
7928 {
7929 isec->size = 0;
7930 isec->flags |= SEC_EXCLUDE;
7931 }
7932 }
7933 else
7934 {
7935 /* Adjust the output section size when called from
7936 objcopy. */
7937 isec->output_section->size -= removed;
7938 if (isec->output_section->size <= 4)
7939 {
7940 isec->output_section->size = 0;
7941 isec->output_section->flags |= SEC_EXCLUDE;
7942 }
7943 }
7944 }
7945 }
7946
7947 return TRUE;
7948 }
7949
7950 /* Copy private header information. */
7951
7952 bfd_boolean
7953 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7954 {
7955 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7956 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7957 return TRUE;
7958
7959 /* Copy over private BFD data if it has not already been copied.
7960 This must be done here, rather than in the copy_private_bfd_data
7961 entry point, because the latter is called after the section
7962 contents have been set, which means that the program headers have
7963 already been worked out. */
7964 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7965 {
7966 if (! copy_private_bfd_data (ibfd, obfd))
7967 return FALSE;
7968 }
7969
7970 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7971 }
7972
7973 /* Copy private symbol information. If this symbol is in a section
7974 which we did not map into a BFD section, try to map the section
7975 index correctly. We use special macro definitions for the mapped
7976 section indices; these definitions are interpreted by the
7977 swap_out_syms function. */
7978
7979 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7980 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7981 #define MAP_STRTAB (SHN_HIOS + 3)
7982 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7983 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7984
7985 bfd_boolean
7986 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7987 asymbol *isymarg,
7988 bfd *obfd,
7989 asymbol *osymarg)
7990 {
7991 elf_symbol_type *isym, *osym;
7992
7993 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7994 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7995 return TRUE;
7996
7997 isym = elf_symbol_from (ibfd, isymarg);
7998 osym = elf_symbol_from (obfd, osymarg);
7999
8000 if (isym != NULL
8001 && isym->internal_elf_sym.st_shndx != 0
8002 && osym != NULL
8003 && bfd_is_abs_section (isym->symbol.section))
8004 {
8005 unsigned int shndx;
8006
8007 shndx = isym->internal_elf_sym.st_shndx;
8008 if (shndx == elf_onesymtab (ibfd))
8009 shndx = MAP_ONESYMTAB;
8010 else if (shndx == elf_dynsymtab (ibfd))
8011 shndx = MAP_DYNSYMTAB;
8012 else if (shndx == elf_strtab_sec (ibfd))
8013 shndx = MAP_STRTAB;
8014 else if (shndx == elf_shstrtab_sec (ibfd))
8015 shndx = MAP_SHSTRTAB;
8016 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8017 shndx = MAP_SYM_SHNDX;
8018 osym->internal_elf_sym.st_shndx = shndx;
8019 }
8020
8021 return TRUE;
8022 }
8023
8024 /* Swap out the symbols. */
8025
8026 static bfd_boolean
8027 swap_out_syms (bfd *abfd,
8028 struct elf_strtab_hash **sttp,
8029 int relocatable_p)
8030 {
8031 const struct elf_backend_data *bed;
8032 int symcount;
8033 asymbol **syms;
8034 struct elf_strtab_hash *stt;
8035 Elf_Internal_Shdr *symtab_hdr;
8036 Elf_Internal_Shdr *symtab_shndx_hdr;
8037 Elf_Internal_Shdr *symstrtab_hdr;
8038 struct elf_sym_strtab *symstrtab;
8039 bfd_byte *outbound_syms;
8040 bfd_byte *outbound_shndx;
8041 unsigned long outbound_syms_index;
8042 unsigned long outbound_shndx_index;
8043 int idx;
8044 unsigned int num_locals;
8045 bfd_size_type amt;
8046 bfd_boolean name_local_sections;
8047
8048 if (!elf_map_symbols (abfd, &num_locals))
8049 return FALSE;
8050
8051 /* Dump out the symtabs. */
8052 stt = _bfd_elf_strtab_init ();
8053 if (stt == NULL)
8054 return FALSE;
8055
8056 bed = get_elf_backend_data (abfd);
8057 symcount = bfd_get_symcount (abfd);
8058 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8059 symtab_hdr->sh_type = SHT_SYMTAB;
8060 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8061 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8062 symtab_hdr->sh_info = num_locals + 1;
8063 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8064
8065 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8066 symstrtab_hdr->sh_type = SHT_STRTAB;
8067
8068 /* Allocate buffer to swap out the .strtab section. */
8069 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
8070 sizeof (*symstrtab));
8071 if (symstrtab == NULL)
8072 {
8073 _bfd_elf_strtab_free (stt);
8074 return FALSE;
8075 }
8076
8077 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
8078 bed->s->sizeof_sym);
8079 if (outbound_syms == NULL)
8080 {
8081 error_return:
8082 _bfd_elf_strtab_free (stt);
8083 free (symstrtab);
8084 return FALSE;
8085 }
8086 symtab_hdr->contents = outbound_syms;
8087 outbound_syms_index = 0;
8088
8089 outbound_shndx = NULL;
8090 outbound_shndx_index = 0;
8091
8092 if (elf_symtab_shndx_list (abfd))
8093 {
8094 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8095 if (symtab_shndx_hdr->sh_name != 0)
8096 {
8097 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
8098 outbound_shndx = (bfd_byte *)
8099 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
8100 if (outbound_shndx == NULL)
8101 goto error_return;
8102
8103 symtab_shndx_hdr->contents = outbound_shndx;
8104 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8105 symtab_shndx_hdr->sh_size = amt;
8106 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8107 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8108 }
8109 /* FIXME: What about any other headers in the list ? */
8110 }
8111
8112 /* Now generate the data (for "contents"). */
8113 {
8114 /* Fill in zeroth symbol and swap it out. */
8115 Elf_Internal_Sym sym;
8116 sym.st_name = 0;
8117 sym.st_value = 0;
8118 sym.st_size = 0;
8119 sym.st_info = 0;
8120 sym.st_other = 0;
8121 sym.st_shndx = SHN_UNDEF;
8122 sym.st_target_internal = 0;
8123 symstrtab[0].sym = sym;
8124 symstrtab[0].dest_index = outbound_syms_index;
8125 symstrtab[0].destshndx_index = outbound_shndx_index;
8126 outbound_syms_index++;
8127 if (outbound_shndx != NULL)
8128 outbound_shndx_index++;
8129 }
8130
8131 name_local_sections
8132 = (bed->elf_backend_name_local_section_symbols
8133 && bed->elf_backend_name_local_section_symbols (abfd));
8134
8135 syms = bfd_get_outsymbols (abfd);
8136 for (idx = 0; idx < symcount;)
8137 {
8138 Elf_Internal_Sym sym;
8139 bfd_vma value = syms[idx]->value;
8140 elf_symbol_type *type_ptr;
8141 flagword flags = syms[idx]->flags;
8142 int type;
8143
8144 if (!name_local_sections
8145 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8146 {
8147 /* Local section symbols have no name. */
8148 sym.st_name = (unsigned long) -1;
8149 }
8150 else
8151 {
8152 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8153 to get the final offset for st_name. */
8154 sym.st_name
8155 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8156 FALSE);
8157 if (sym.st_name == (unsigned long) -1)
8158 goto error_return;
8159 }
8160
8161 type_ptr = elf_symbol_from (abfd, syms[idx]);
8162
8163 if ((flags & BSF_SECTION_SYM) == 0
8164 && bfd_is_com_section (syms[idx]->section))
8165 {
8166 /* ELF common symbols put the alignment into the `value' field,
8167 and the size into the `size' field. This is backwards from
8168 how BFD handles it, so reverse it here. */
8169 sym.st_size = value;
8170 if (type_ptr == NULL
8171 || type_ptr->internal_elf_sym.st_value == 0)
8172 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8173 else
8174 sym.st_value = type_ptr->internal_elf_sym.st_value;
8175 sym.st_shndx = _bfd_elf_section_from_bfd_section
8176 (abfd, syms[idx]->section);
8177 }
8178 else
8179 {
8180 asection *sec = syms[idx]->section;
8181 unsigned int shndx;
8182
8183 if (sec->output_section)
8184 {
8185 value += sec->output_offset;
8186 sec = sec->output_section;
8187 }
8188
8189 /* Don't add in the section vma for relocatable output. */
8190 if (! relocatable_p)
8191 value += sec->vma;
8192 sym.st_value = value;
8193 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8194
8195 if (bfd_is_abs_section (sec)
8196 && type_ptr != NULL
8197 && type_ptr->internal_elf_sym.st_shndx != 0)
8198 {
8199 /* This symbol is in a real ELF section which we did
8200 not create as a BFD section. Undo the mapping done
8201 by copy_private_symbol_data. */
8202 shndx = type_ptr->internal_elf_sym.st_shndx;
8203 switch (shndx)
8204 {
8205 case MAP_ONESYMTAB:
8206 shndx = elf_onesymtab (abfd);
8207 break;
8208 case MAP_DYNSYMTAB:
8209 shndx = elf_dynsymtab (abfd);
8210 break;
8211 case MAP_STRTAB:
8212 shndx = elf_strtab_sec (abfd);
8213 break;
8214 case MAP_SHSTRTAB:
8215 shndx = elf_shstrtab_sec (abfd);
8216 break;
8217 case MAP_SYM_SHNDX:
8218 if (elf_symtab_shndx_list (abfd))
8219 shndx = elf_symtab_shndx_list (abfd)->ndx;
8220 break;
8221 default:
8222 shndx = SHN_ABS;
8223 break;
8224 }
8225 }
8226 else
8227 {
8228 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8229
8230 if (shndx == SHN_BAD)
8231 {
8232 asection *sec2;
8233
8234 /* Writing this would be a hell of a lot easier if
8235 we had some decent documentation on bfd, and
8236 knew what to expect of the library, and what to
8237 demand of applications. For example, it
8238 appears that `objcopy' might not set the
8239 section of a symbol to be a section that is
8240 actually in the output file. */
8241 sec2 = bfd_get_section_by_name (abfd, sec->name);
8242 if (sec2 != NULL)
8243 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8244 if (shndx == SHN_BAD)
8245 {
8246 /* xgettext:c-format */
8247 _bfd_error_handler
8248 (_("unable to find equivalent output section"
8249 " for symbol '%s' from section '%s'"),
8250 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8251 sec->name);
8252 bfd_set_error (bfd_error_invalid_operation);
8253 goto error_return;
8254 }
8255 }
8256 }
8257
8258 sym.st_shndx = shndx;
8259 }
8260
8261 if ((flags & BSF_THREAD_LOCAL) != 0)
8262 type = STT_TLS;
8263 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8264 type = STT_GNU_IFUNC;
8265 else if ((flags & BSF_FUNCTION) != 0)
8266 type = STT_FUNC;
8267 else if ((flags & BSF_OBJECT) != 0)
8268 type = STT_OBJECT;
8269 else if ((flags & BSF_RELC) != 0)
8270 type = STT_RELC;
8271 else if ((flags & BSF_SRELC) != 0)
8272 type = STT_SRELC;
8273 else
8274 type = STT_NOTYPE;
8275
8276 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8277 type = STT_TLS;
8278
8279 /* Processor-specific types. */
8280 if (type_ptr != NULL
8281 && bed->elf_backend_get_symbol_type)
8282 type = ((*bed->elf_backend_get_symbol_type)
8283 (&type_ptr->internal_elf_sym, type));
8284
8285 if (flags & BSF_SECTION_SYM)
8286 {
8287 if (flags & BSF_GLOBAL)
8288 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8289 else
8290 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8291 }
8292 else if (bfd_is_com_section (syms[idx]->section))
8293 {
8294 if (type != STT_TLS)
8295 {
8296 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8297 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8298 ? STT_COMMON : STT_OBJECT);
8299 else
8300 type = ((flags & BSF_ELF_COMMON) != 0
8301 ? STT_COMMON : STT_OBJECT);
8302 }
8303 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8304 }
8305 else if (bfd_is_und_section (syms[idx]->section))
8306 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8307 ? STB_WEAK
8308 : STB_GLOBAL),
8309 type);
8310 else if (flags & BSF_FILE)
8311 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8312 else
8313 {
8314 int bind = STB_LOCAL;
8315
8316 if (flags & BSF_LOCAL)
8317 bind = STB_LOCAL;
8318 else if (flags & BSF_GNU_UNIQUE)
8319 bind = STB_GNU_UNIQUE;
8320 else if (flags & BSF_WEAK)
8321 bind = STB_WEAK;
8322 else if (flags & BSF_GLOBAL)
8323 bind = STB_GLOBAL;
8324
8325 sym.st_info = ELF_ST_INFO (bind, type);
8326 }
8327
8328 if (type_ptr != NULL)
8329 {
8330 sym.st_other = type_ptr->internal_elf_sym.st_other;
8331 sym.st_target_internal
8332 = type_ptr->internal_elf_sym.st_target_internal;
8333 }
8334 else
8335 {
8336 sym.st_other = 0;
8337 sym.st_target_internal = 0;
8338 }
8339
8340 idx++;
8341 symstrtab[idx].sym = sym;
8342 symstrtab[idx].dest_index = outbound_syms_index;
8343 symstrtab[idx].destshndx_index = outbound_shndx_index;
8344
8345 outbound_syms_index++;
8346 if (outbound_shndx != NULL)
8347 outbound_shndx_index++;
8348 }
8349
8350 /* Finalize the .strtab section. */
8351 _bfd_elf_strtab_finalize (stt);
8352
8353 /* Swap out the .strtab section. */
8354 for (idx = 0; idx <= symcount; idx++)
8355 {
8356 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8357 if (elfsym->sym.st_name == (unsigned long) -1)
8358 elfsym->sym.st_name = 0;
8359 else
8360 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8361 elfsym->sym.st_name);
8362 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8363 (outbound_syms
8364 + (elfsym->dest_index
8365 * bed->s->sizeof_sym)),
8366 (outbound_shndx
8367 + (elfsym->destshndx_index
8368 * sizeof (Elf_External_Sym_Shndx))));
8369 }
8370 free (symstrtab);
8371
8372 *sttp = stt;
8373 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8374 symstrtab_hdr->sh_type = SHT_STRTAB;
8375 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8376 symstrtab_hdr->sh_addr = 0;
8377 symstrtab_hdr->sh_entsize = 0;
8378 symstrtab_hdr->sh_link = 0;
8379 symstrtab_hdr->sh_info = 0;
8380 symstrtab_hdr->sh_addralign = 1;
8381
8382 return TRUE;
8383 }
8384
8385 /* Return the number of bytes required to hold the symtab vector.
8386
8387 Note that we base it on the count plus 1, since we will null terminate
8388 the vector allocated based on this size. However, the ELF symbol table
8389 always has a dummy entry as symbol #0, so it ends up even. */
8390
8391 long
8392 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8393 {
8394 bfd_size_type symcount;
8395 long symtab_size;
8396 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8397
8398 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8399 if (symcount >= LONG_MAX / sizeof (asymbol *))
8400 {
8401 bfd_set_error (bfd_error_file_too_big);
8402 return -1;
8403 }
8404 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8405 if (symcount > 0)
8406 symtab_size -= sizeof (asymbol *);
8407
8408 return symtab_size;
8409 }
8410
8411 long
8412 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8413 {
8414 bfd_size_type symcount;
8415 long symtab_size;
8416 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8417
8418 if (elf_dynsymtab (abfd) == 0)
8419 {
8420 bfd_set_error (bfd_error_invalid_operation);
8421 return -1;
8422 }
8423
8424 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8425 if (symcount >= LONG_MAX / sizeof (asymbol *))
8426 {
8427 bfd_set_error (bfd_error_file_too_big);
8428 return -1;
8429 }
8430 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8431 if (symcount > 0)
8432 symtab_size -= sizeof (asymbol *);
8433
8434 return symtab_size;
8435 }
8436
8437 long
8438 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8439 sec_ptr asect)
8440 {
8441 #if SIZEOF_LONG == SIZEOF_INT
8442 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8443 {
8444 bfd_set_error (bfd_error_file_too_big);
8445 return -1;
8446 }
8447 #endif
8448 return (asect->reloc_count + 1) * sizeof (arelent *);
8449 }
8450
8451 /* Canonicalize the relocs. */
8452
8453 long
8454 _bfd_elf_canonicalize_reloc (bfd *abfd,
8455 sec_ptr section,
8456 arelent **relptr,
8457 asymbol **symbols)
8458 {
8459 arelent *tblptr;
8460 unsigned int i;
8461 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8462
8463 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8464 return -1;
8465
8466 tblptr = section->relocation;
8467 for (i = 0; i < section->reloc_count; i++)
8468 *relptr++ = tblptr++;
8469
8470 *relptr = NULL;
8471
8472 return section->reloc_count;
8473 }
8474
8475 long
8476 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8477 {
8478 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8479 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8480
8481 if (symcount >= 0)
8482 abfd->symcount = symcount;
8483 return symcount;
8484 }
8485
8486 long
8487 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8488 asymbol **allocation)
8489 {
8490 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8491 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8492
8493 if (symcount >= 0)
8494 abfd->dynsymcount = symcount;
8495 return symcount;
8496 }
8497
8498 /* Return the size required for the dynamic reloc entries. Any loadable
8499 section that was actually installed in the BFD, and has type SHT_REL
8500 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8501 dynamic reloc section. */
8502
8503 long
8504 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8505 {
8506 bfd_size_type count;
8507 asection *s;
8508
8509 if (elf_dynsymtab (abfd) == 0)
8510 {
8511 bfd_set_error (bfd_error_invalid_operation);
8512 return -1;
8513 }
8514
8515 count = 1;
8516 for (s = abfd->sections; s != NULL; s = s->next)
8517 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8518 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8519 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8520 {
8521 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8522 if (count > LONG_MAX / sizeof (arelent *))
8523 {
8524 bfd_set_error (bfd_error_file_too_big);
8525 return -1;
8526 }
8527 }
8528 return count * sizeof (arelent *);
8529 }
8530
8531 /* Canonicalize the dynamic relocation entries. Note that we return the
8532 dynamic relocations as a single block, although they are actually
8533 associated with particular sections; the interface, which was
8534 designed for SunOS style shared libraries, expects that there is only
8535 one set of dynamic relocs. Any loadable section that was actually
8536 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8537 dynamic symbol table, is considered to be a dynamic reloc section. */
8538
8539 long
8540 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8541 arelent **storage,
8542 asymbol **syms)
8543 {
8544 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8545 asection *s;
8546 long ret;
8547
8548 if (elf_dynsymtab (abfd) == 0)
8549 {
8550 bfd_set_error (bfd_error_invalid_operation);
8551 return -1;
8552 }
8553
8554 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8555 ret = 0;
8556 for (s = abfd->sections; s != NULL; s = s->next)
8557 {
8558 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8559 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8560 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8561 {
8562 arelent *p;
8563 long count, i;
8564
8565 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8566 return -1;
8567 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8568 p = s->relocation;
8569 for (i = 0; i < count; i++)
8570 *storage++ = p++;
8571 ret += count;
8572 }
8573 }
8574
8575 *storage = NULL;
8576
8577 return ret;
8578 }
8579
8580 /* Read in the version information. */
8582
8583 bfd_boolean
8584 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8585 {
8586 bfd_byte *contents = NULL;
8587 unsigned int freeidx = 0;
8588
8589 if (elf_dynverref (abfd) != 0)
8590 {
8591 Elf_Internal_Shdr *hdr;
8592 Elf_External_Verneed *everneed;
8593 Elf_Internal_Verneed *iverneed;
8594 unsigned int i;
8595 bfd_byte *contents_end;
8596
8597 hdr = &elf_tdata (abfd)->dynverref_hdr;
8598
8599 if (hdr->sh_info == 0
8600 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8601 {
8602 error_return_bad_verref:
8603 _bfd_error_handler
8604 (_("%pB: .gnu.version_r invalid entry"), abfd);
8605 bfd_set_error (bfd_error_bad_value);
8606 error_return_verref:
8607 elf_tdata (abfd)->verref = NULL;
8608 elf_tdata (abfd)->cverrefs = 0;
8609 goto error_return;
8610 }
8611
8612 ufile_ptr filesize = bfd_get_file_size (abfd);
8613 if (filesize > 0 && filesize < hdr->sh_size)
8614 {
8615 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8616 of memory. */
8617 bfd_set_error (bfd_error_no_memory);
8618 _bfd_error_handler
8619 /* xgettext:c-format */
8620 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8621 abfd, (uint64_t) hdr->sh_size);
8622 goto error_return_verref;
8623 }
8624 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8625 if (contents == NULL)
8626 goto error_return_verref;
8627
8628 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8629 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8630 goto error_return_verref;
8631
8632 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8633 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8634
8635 if (elf_tdata (abfd)->verref == NULL)
8636 goto error_return_verref;
8637
8638 BFD_ASSERT (sizeof (Elf_External_Verneed)
8639 == sizeof (Elf_External_Vernaux));
8640 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8641 everneed = (Elf_External_Verneed *) contents;
8642 iverneed = elf_tdata (abfd)->verref;
8643 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8644 {
8645 Elf_External_Vernaux *evernaux;
8646 Elf_Internal_Vernaux *ivernaux;
8647 unsigned int j;
8648
8649 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8650
8651 iverneed->vn_bfd = abfd;
8652
8653 iverneed->vn_filename =
8654 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8655 iverneed->vn_file);
8656 if (iverneed->vn_filename == NULL)
8657 goto error_return_bad_verref;
8658
8659 if (iverneed->vn_cnt == 0)
8660 iverneed->vn_auxptr = NULL;
8661 else
8662 {
8663 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8664 bfd_alloc2 (abfd, iverneed->vn_cnt,
8665 sizeof (Elf_Internal_Vernaux));
8666 if (iverneed->vn_auxptr == NULL)
8667 goto error_return_verref;
8668 }
8669
8670 if (iverneed->vn_aux
8671 > (size_t) (contents_end - (bfd_byte *) everneed))
8672 goto error_return_bad_verref;
8673
8674 evernaux = ((Elf_External_Vernaux *)
8675 ((bfd_byte *) everneed + iverneed->vn_aux));
8676 ivernaux = iverneed->vn_auxptr;
8677 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8678 {
8679 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8680
8681 ivernaux->vna_nodename =
8682 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8683 ivernaux->vna_name);
8684 if (ivernaux->vna_nodename == NULL)
8685 goto error_return_bad_verref;
8686
8687 if (ivernaux->vna_other > freeidx)
8688 freeidx = ivernaux->vna_other;
8689
8690 ivernaux->vna_nextptr = NULL;
8691 if (ivernaux->vna_next == 0)
8692 {
8693 iverneed->vn_cnt = j + 1;
8694 break;
8695 }
8696 if (j + 1 < iverneed->vn_cnt)
8697 ivernaux->vna_nextptr = ivernaux + 1;
8698
8699 if (ivernaux->vna_next
8700 > (size_t) (contents_end - (bfd_byte *) evernaux))
8701 goto error_return_bad_verref;
8702
8703 evernaux = ((Elf_External_Vernaux *)
8704 ((bfd_byte *) evernaux + ivernaux->vna_next));
8705 }
8706
8707 iverneed->vn_nextref = NULL;
8708 if (iverneed->vn_next == 0)
8709 break;
8710 if (i + 1 < hdr->sh_info)
8711 iverneed->vn_nextref = iverneed + 1;
8712
8713 if (iverneed->vn_next
8714 > (size_t) (contents_end - (bfd_byte *) everneed))
8715 goto error_return_bad_verref;
8716
8717 everneed = ((Elf_External_Verneed *)
8718 ((bfd_byte *) everneed + iverneed->vn_next));
8719 }
8720 elf_tdata (abfd)->cverrefs = i;
8721
8722 free (contents);
8723 contents = NULL;
8724 }
8725
8726 if (elf_dynverdef (abfd) != 0)
8727 {
8728 Elf_Internal_Shdr *hdr;
8729 Elf_External_Verdef *everdef;
8730 Elf_Internal_Verdef *iverdef;
8731 Elf_Internal_Verdef *iverdefarr;
8732 Elf_Internal_Verdef iverdefmem;
8733 unsigned int i;
8734 unsigned int maxidx;
8735 bfd_byte *contents_end_def, *contents_end_aux;
8736
8737 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8738
8739 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8740 {
8741 error_return_bad_verdef:
8742 _bfd_error_handler
8743 (_("%pB: .gnu.version_d invalid entry"), abfd);
8744 bfd_set_error (bfd_error_bad_value);
8745 error_return_verdef:
8746 elf_tdata (abfd)->verdef = NULL;
8747 elf_tdata (abfd)->cverdefs = 0;
8748 goto error_return;
8749 }
8750
8751 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8752 if (contents == NULL)
8753 goto error_return_verdef;
8754 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8755 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8756 goto error_return_verdef;
8757
8758 BFD_ASSERT (sizeof (Elf_External_Verdef)
8759 >= sizeof (Elf_External_Verdaux));
8760 contents_end_def = contents + hdr->sh_size
8761 - sizeof (Elf_External_Verdef);
8762 contents_end_aux = contents + hdr->sh_size
8763 - sizeof (Elf_External_Verdaux);
8764
8765 /* We know the number of entries in the section but not the maximum
8766 index. Therefore we have to run through all entries and find
8767 the maximum. */
8768 everdef = (Elf_External_Verdef *) contents;
8769 maxidx = 0;
8770 for (i = 0; i < hdr->sh_info; ++i)
8771 {
8772 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8773
8774 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8775 goto error_return_bad_verdef;
8776 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8777 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8778
8779 if (iverdefmem.vd_next == 0)
8780 break;
8781
8782 if (iverdefmem.vd_next
8783 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8784 goto error_return_bad_verdef;
8785
8786 everdef = ((Elf_External_Verdef *)
8787 ((bfd_byte *) everdef + iverdefmem.vd_next));
8788 }
8789
8790 if (default_imported_symver)
8791 {
8792 if (freeidx > maxidx)
8793 maxidx = ++freeidx;
8794 else
8795 freeidx = ++maxidx;
8796 }
8797
8798 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8799 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8800 if (elf_tdata (abfd)->verdef == NULL)
8801 goto error_return_verdef;
8802
8803 elf_tdata (abfd)->cverdefs = maxidx;
8804
8805 everdef = (Elf_External_Verdef *) contents;
8806 iverdefarr = elf_tdata (abfd)->verdef;
8807 for (i = 0; i < hdr->sh_info; i++)
8808 {
8809 Elf_External_Verdaux *everdaux;
8810 Elf_Internal_Verdaux *iverdaux;
8811 unsigned int j;
8812
8813 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8814
8815 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8816 goto error_return_bad_verdef;
8817
8818 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8819 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8820
8821 iverdef->vd_bfd = abfd;
8822
8823 if (iverdef->vd_cnt == 0)
8824 iverdef->vd_auxptr = NULL;
8825 else
8826 {
8827 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8828 bfd_alloc2 (abfd, iverdef->vd_cnt,
8829 sizeof (Elf_Internal_Verdaux));
8830 if (iverdef->vd_auxptr == NULL)
8831 goto error_return_verdef;
8832 }
8833
8834 if (iverdef->vd_aux
8835 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8836 goto error_return_bad_verdef;
8837
8838 everdaux = ((Elf_External_Verdaux *)
8839 ((bfd_byte *) everdef + iverdef->vd_aux));
8840 iverdaux = iverdef->vd_auxptr;
8841 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8842 {
8843 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8844
8845 iverdaux->vda_nodename =
8846 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8847 iverdaux->vda_name);
8848 if (iverdaux->vda_nodename == NULL)
8849 goto error_return_bad_verdef;
8850
8851 iverdaux->vda_nextptr = NULL;
8852 if (iverdaux->vda_next == 0)
8853 {
8854 iverdef->vd_cnt = j + 1;
8855 break;
8856 }
8857 if (j + 1 < iverdef->vd_cnt)
8858 iverdaux->vda_nextptr = iverdaux + 1;
8859
8860 if (iverdaux->vda_next
8861 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8862 goto error_return_bad_verdef;
8863
8864 everdaux = ((Elf_External_Verdaux *)
8865 ((bfd_byte *) everdaux + iverdaux->vda_next));
8866 }
8867
8868 iverdef->vd_nodename = NULL;
8869 if (iverdef->vd_cnt)
8870 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8871
8872 iverdef->vd_nextdef = NULL;
8873 if (iverdef->vd_next == 0)
8874 break;
8875 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8876 iverdef->vd_nextdef = iverdef + 1;
8877
8878 everdef = ((Elf_External_Verdef *)
8879 ((bfd_byte *) everdef + iverdef->vd_next));
8880 }
8881
8882 free (contents);
8883 contents = NULL;
8884 }
8885 else if (default_imported_symver)
8886 {
8887 if (freeidx < 3)
8888 freeidx = 3;
8889 else
8890 freeidx++;
8891
8892 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8893 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8894 if (elf_tdata (abfd)->verdef == NULL)
8895 goto error_return;
8896
8897 elf_tdata (abfd)->cverdefs = freeidx;
8898 }
8899
8900 /* Create a default version based on the soname. */
8901 if (default_imported_symver)
8902 {
8903 Elf_Internal_Verdef *iverdef;
8904 Elf_Internal_Verdaux *iverdaux;
8905
8906 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8907
8908 iverdef->vd_version = VER_DEF_CURRENT;
8909 iverdef->vd_flags = 0;
8910 iverdef->vd_ndx = freeidx;
8911 iverdef->vd_cnt = 1;
8912
8913 iverdef->vd_bfd = abfd;
8914
8915 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8916 if (iverdef->vd_nodename == NULL)
8917 goto error_return_verdef;
8918 iverdef->vd_nextdef = NULL;
8919 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8920 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8921 if (iverdef->vd_auxptr == NULL)
8922 goto error_return_verdef;
8923
8924 iverdaux = iverdef->vd_auxptr;
8925 iverdaux->vda_nodename = iverdef->vd_nodename;
8926 }
8927
8928 return TRUE;
8929
8930 error_return:
8931 if (contents != NULL)
8932 free (contents);
8933 return FALSE;
8934 }
8935
8936 asymbol *
8938 _bfd_elf_make_empty_symbol (bfd *abfd)
8939 {
8940 elf_symbol_type *newsym;
8941
8942 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
8943 if (!newsym)
8944 return NULL;
8945 newsym->symbol.the_bfd = abfd;
8946 return &newsym->symbol;
8947 }
8948
8949 void
8950 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8951 asymbol *symbol,
8952 symbol_info *ret)
8953 {
8954 bfd_symbol_info (symbol, ret);
8955 }
8956
8957 /* Return whether a symbol name implies a local symbol. Most targets
8958 use this function for the is_local_label_name entry point, but some
8959 override it. */
8960
8961 bfd_boolean
8962 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8963 const char *name)
8964 {
8965 /* Normal local symbols start with ``.L''. */
8966 if (name[0] == '.' && name[1] == 'L')
8967 return TRUE;
8968
8969 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8970 DWARF debugging symbols starting with ``..''. */
8971 if (name[0] == '.' && name[1] == '.')
8972 return TRUE;
8973
8974 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8975 emitting DWARF debugging output. I suspect this is actually a
8976 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8977 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8978 underscore to be emitted on some ELF targets). For ease of use,
8979 we treat such symbols as local. */
8980 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8981 return TRUE;
8982
8983 /* Treat assembler generated fake symbols, dollar local labels and
8984 forward-backward labels (aka local labels) as locals.
8985 These labels have the form:
8986
8987 L0^A.* (fake symbols)
8988
8989 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8990
8991 Versions which start with .L will have already been matched above,
8992 so we only need to match the rest. */
8993 if (name[0] == 'L' && ISDIGIT (name[1]))
8994 {
8995 bfd_boolean ret = FALSE;
8996 const char * p;
8997 char c;
8998
8999 for (p = name + 2; (c = *p); p++)
9000 {
9001 if (c == 1 || c == 2)
9002 {
9003 if (c == 1 && p == name + 2)
9004 /* A fake symbol. */
9005 return TRUE;
9006
9007 /* FIXME: We are being paranoid here and treating symbols like
9008 L0^Bfoo as if there were non-local, on the grounds that the
9009 assembler will never generate them. But can any symbol
9010 containing an ASCII value in the range 1-31 ever be anything
9011 other than some kind of local ? */
9012 ret = TRUE;
9013 }
9014
9015 if (! ISDIGIT (c))
9016 {
9017 ret = FALSE;
9018 break;
9019 }
9020 }
9021 return ret;
9022 }
9023
9024 return FALSE;
9025 }
9026
9027 alent *
9028 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9029 asymbol *symbol ATTRIBUTE_UNUSED)
9030 {
9031 abort ();
9032 return NULL;
9033 }
9034
9035 bfd_boolean
9036 _bfd_elf_set_arch_mach (bfd *abfd,
9037 enum bfd_architecture arch,
9038 unsigned long machine)
9039 {
9040 /* If this isn't the right architecture for this backend, and this
9041 isn't the generic backend, fail. */
9042 if (arch != get_elf_backend_data (abfd)->arch
9043 && arch != bfd_arch_unknown
9044 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9045 return FALSE;
9046
9047 return bfd_default_set_arch_mach (abfd, arch, machine);
9048 }
9049
9050 /* Find the nearest line to a particular section and offset,
9051 for error reporting. */
9052
9053 bfd_boolean
9054 _bfd_elf_find_nearest_line (bfd *abfd,
9055 asymbol **symbols,
9056 asection *section,
9057 bfd_vma offset,
9058 const char **filename_ptr,
9059 const char **functionname_ptr,
9060 unsigned int *line_ptr,
9061 unsigned int *discriminator_ptr)
9062 {
9063 bfd_boolean found;
9064
9065 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
9066 filename_ptr, functionname_ptr,
9067 line_ptr, discriminator_ptr,
9068 dwarf_debug_sections,
9069 &elf_tdata (abfd)->dwarf2_find_line_info))
9070 return TRUE;
9071
9072 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9073 filename_ptr, functionname_ptr, line_ptr))
9074 {
9075 if (!*functionname_ptr)
9076 _bfd_elf_find_function (abfd, symbols, section, offset,
9077 *filename_ptr ? NULL : filename_ptr,
9078 functionname_ptr);
9079 return TRUE;
9080 }
9081
9082 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9083 &found, filename_ptr,
9084 functionname_ptr, line_ptr,
9085 &elf_tdata (abfd)->line_info))
9086 return FALSE;
9087 if (found && (*functionname_ptr || *line_ptr))
9088 return TRUE;
9089
9090 if (symbols == NULL)
9091 return FALSE;
9092
9093 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9094 filename_ptr, functionname_ptr))
9095 return FALSE;
9096
9097 *line_ptr = 0;
9098 return TRUE;
9099 }
9100
9101 /* Find the line for a symbol. */
9102
9103 bfd_boolean
9104 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9105 const char **filename_ptr, unsigned int *line_ptr)
9106 {
9107 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9108 filename_ptr, NULL, line_ptr, NULL,
9109 dwarf_debug_sections,
9110 &elf_tdata (abfd)->dwarf2_find_line_info);
9111 }
9112
9113 /* After a call to bfd_find_nearest_line, successive calls to
9114 bfd_find_inliner_info can be used to get source information about
9115 each level of function inlining that terminated at the address
9116 passed to bfd_find_nearest_line. Currently this is only supported
9117 for DWARF2 with appropriate DWARF3 extensions. */
9118
9119 bfd_boolean
9120 _bfd_elf_find_inliner_info (bfd *abfd,
9121 const char **filename_ptr,
9122 const char **functionname_ptr,
9123 unsigned int *line_ptr)
9124 {
9125 bfd_boolean found;
9126 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9127 functionname_ptr, line_ptr,
9128 & elf_tdata (abfd)->dwarf2_find_line_info);
9129 return found;
9130 }
9131
9132 int
9133 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9134 {
9135 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9136 int ret = bed->s->sizeof_ehdr;
9137
9138 if (!bfd_link_relocatable (info))
9139 {
9140 bfd_size_type phdr_size = elf_program_header_size (abfd);
9141
9142 if (phdr_size == (bfd_size_type) -1)
9143 {
9144 struct elf_segment_map *m;
9145
9146 phdr_size = 0;
9147 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9148 phdr_size += bed->s->sizeof_phdr;
9149
9150 if (phdr_size == 0)
9151 phdr_size = get_program_header_size (abfd, info);
9152 }
9153
9154 elf_program_header_size (abfd) = phdr_size;
9155 ret += phdr_size;
9156 }
9157
9158 return ret;
9159 }
9160
9161 bfd_boolean
9162 _bfd_elf_set_section_contents (bfd *abfd,
9163 sec_ptr section,
9164 const void *location,
9165 file_ptr offset,
9166 bfd_size_type count)
9167 {
9168 Elf_Internal_Shdr *hdr;
9169 file_ptr pos;
9170
9171 if (! abfd->output_has_begun
9172 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9173 return FALSE;
9174
9175 if (!count)
9176 return TRUE;
9177
9178 hdr = &elf_section_data (section)->this_hdr;
9179 if (hdr->sh_offset == (file_ptr) -1)
9180 {
9181 if (bfd_section_is_ctf (section))
9182 /* Nothing to do with this section: the contents are generated
9183 later. */
9184 return TRUE;
9185
9186 /* We must compress this section. Write output to the buffer. */
9187 unsigned char *contents = hdr->contents;
9188 if ((offset + count) > hdr->sh_size
9189 || (section->flags & SEC_ELF_COMPRESS) == 0
9190 || contents == NULL)
9191 abort ();
9192 memcpy (contents + offset, location, count);
9193 return TRUE;
9194 }
9195 pos = hdr->sh_offset + offset;
9196 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9197 || bfd_bwrite (location, count, abfd) != count)
9198 return FALSE;
9199
9200 return TRUE;
9201 }
9202
9203 bfd_boolean
9204 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9205 arelent *cache_ptr ATTRIBUTE_UNUSED,
9206 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9207 {
9208 abort ();
9209 return FALSE;
9210 }
9211
9212 /* Try to convert a non-ELF reloc into an ELF one. */
9213
9214 bfd_boolean
9215 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9216 {
9217 /* Check whether we really have an ELF howto. */
9218
9219 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9220 {
9221 bfd_reloc_code_real_type code;
9222 reloc_howto_type *howto;
9223
9224 /* Alien reloc: Try to determine its type to replace it with an
9225 equivalent ELF reloc. */
9226
9227 if (areloc->howto->pc_relative)
9228 {
9229 switch (areloc->howto->bitsize)
9230 {
9231 case 8:
9232 code = BFD_RELOC_8_PCREL;
9233 break;
9234 case 12:
9235 code = BFD_RELOC_12_PCREL;
9236 break;
9237 case 16:
9238 code = BFD_RELOC_16_PCREL;
9239 break;
9240 case 24:
9241 code = BFD_RELOC_24_PCREL;
9242 break;
9243 case 32:
9244 code = BFD_RELOC_32_PCREL;
9245 break;
9246 case 64:
9247 code = BFD_RELOC_64_PCREL;
9248 break;
9249 default:
9250 goto fail;
9251 }
9252
9253 howto = bfd_reloc_type_lookup (abfd, code);
9254
9255 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9256 {
9257 if (howto->pcrel_offset)
9258 areloc->addend += areloc->address;
9259 else
9260 areloc->addend -= areloc->address; /* addend is unsigned!! */
9261 }
9262 }
9263 else
9264 {
9265 switch (areloc->howto->bitsize)
9266 {
9267 case 8:
9268 code = BFD_RELOC_8;
9269 break;
9270 case 14:
9271 code = BFD_RELOC_14;
9272 break;
9273 case 16:
9274 code = BFD_RELOC_16;
9275 break;
9276 case 26:
9277 code = BFD_RELOC_26;
9278 break;
9279 case 32:
9280 code = BFD_RELOC_32;
9281 break;
9282 case 64:
9283 code = BFD_RELOC_64;
9284 break;
9285 default:
9286 goto fail;
9287 }
9288
9289 howto = bfd_reloc_type_lookup (abfd, code);
9290 }
9291
9292 if (howto)
9293 areloc->howto = howto;
9294 else
9295 goto fail;
9296 }
9297
9298 return TRUE;
9299
9300 fail:
9301 /* xgettext:c-format */
9302 _bfd_error_handler (_("%pB: %s unsupported"),
9303 abfd, areloc->howto->name);
9304 bfd_set_error (bfd_error_sorry);
9305 return FALSE;
9306 }
9307
9308 bfd_boolean
9309 _bfd_elf_close_and_cleanup (bfd *abfd)
9310 {
9311 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9312 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
9313 {
9314 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9315 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9316 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9317 }
9318
9319 return _bfd_generic_close_and_cleanup (abfd);
9320 }
9321
9322 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9323 in the relocation's offset. Thus we cannot allow any sort of sanity
9324 range-checking to interfere. There is nothing else to do in processing
9325 this reloc. */
9326
9327 bfd_reloc_status_type
9328 _bfd_elf_rel_vtable_reloc_fn
9329 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9330 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9331 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9332 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9333 {
9334 return bfd_reloc_ok;
9335 }
9336
9337 /* Elf core file support. Much of this only works on native
9339 toolchains, since we rely on knowing the
9340 machine-dependent procfs structure in order to pick
9341 out details about the corefile. */
9342
9343 #ifdef HAVE_SYS_PROCFS_H
9344 /* Needed for new procfs interface on sparc-solaris. */
9345 # define _STRUCTURED_PROC 1
9346 # include <sys/procfs.h>
9347 #endif
9348
9349 /* Return a PID that identifies a "thread" for threaded cores, or the
9350 PID of the main process for non-threaded cores. */
9351
9352 static int
9353 elfcore_make_pid (bfd *abfd)
9354 {
9355 int pid;
9356
9357 pid = elf_tdata (abfd)->core->lwpid;
9358 if (pid == 0)
9359 pid = elf_tdata (abfd)->core->pid;
9360
9361 return pid;
9362 }
9363
9364 /* If there isn't a section called NAME, make one, using
9365 data from SECT. Note, this function will generate a
9366 reference to NAME, so you shouldn't deallocate or
9367 overwrite it. */
9368
9369 static bfd_boolean
9370 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9371 {
9372 asection *sect2;
9373
9374 if (bfd_get_section_by_name (abfd, name) != NULL)
9375 return TRUE;
9376
9377 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9378 if (sect2 == NULL)
9379 return FALSE;
9380
9381 sect2->size = sect->size;
9382 sect2->filepos = sect->filepos;
9383 sect2->alignment_power = sect->alignment_power;
9384 return TRUE;
9385 }
9386
9387 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9388 actually creates up to two pseudosections:
9389 - For the single-threaded case, a section named NAME, unless
9390 such a section already exists.
9391 - For the multi-threaded case, a section named "NAME/PID", where
9392 PID is elfcore_make_pid (abfd).
9393 Both pseudosections have identical contents. */
9394 bfd_boolean
9395 _bfd_elfcore_make_pseudosection (bfd *abfd,
9396 char *name,
9397 size_t size,
9398 ufile_ptr filepos)
9399 {
9400 char buf[100];
9401 char *threaded_name;
9402 size_t len;
9403 asection *sect;
9404
9405 /* Build the section name. */
9406
9407 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9408 len = strlen (buf) + 1;
9409 threaded_name = (char *) bfd_alloc (abfd, len);
9410 if (threaded_name == NULL)
9411 return FALSE;
9412 memcpy (threaded_name, buf, len);
9413
9414 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9415 SEC_HAS_CONTENTS);
9416 if (sect == NULL)
9417 return FALSE;
9418 sect->size = size;
9419 sect->filepos = filepos;
9420 sect->alignment_power = 2;
9421
9422 return elfcore_maybe_make_sect (abfd, name, sect);
9423 }
9424
9425 static bfd_boolean
9426 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9427 size_t offs)
9428 {
9429 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9430 SEC_HAS_CONTENTS);
9431
9432 if (sect == NULL)
9433 return FALSE;
9434
9435 sect->size = note->descsz - offs;
9436 sect->filepos = note->descpos + offs;
9437 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9438
9439 return TRUE;
9440 }
9441
9442 /* prstatus_t exists on:
9443 solaris 2.5+
9444 linux 2.[01] + glibc
9445 unixware 4.2
9446 */
9447
9448 #if defined (HAVE_PRSTATUS_T)
9449
9450 static bfd_boolean
9451 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9452 {
9453 size_t size;
9454 int offset;
9455
9456 if (note->descsz == sizeof (prstatus_t))
9457 {
9458 prstatus_t prstat;
9459
9460 size = sizeof (prstat.pr_reg);
9461 offset = offsetof (prstatus_t, pr_reg);
9462 memcpy (&prstat, note->descdata, sizeof (prstat));
9463
9464 /* Do not overwrite the core signal if it
9465 has already been set by another thread. */
9466 if (elf_tdata (abfd)->core->signal == 0)
9467 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9468 if (elf_tdata (abfd)->core->pid == 0)
9469 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9470
9471 /* pr_who exists on:
9472 solaris 2.5+
9473 unixware 4.2
9474 pr_who doesn't exist on:
9475 linux 2.[01]
9476 */
9477 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9478 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9479 #else
9480 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9481 #endif
9482 }
9483 #if defined (HAVE_PRSTATUS32_T)
9484 else if (note->descsz == sizeof (prstatus32_t))
9485 {
9486 /* 64-bit host, 32-bit corefile */
9487 prstatus32_t prstat;
9488
9489 size = sizeof (prstat.pr_reg);
9490 offset = offsetof (prstatus32_t, pr_reg);
9491 memcpy (&prstat, note->descdata, sizeof (prstat));
9492
9493 /* Do not overwrite the core signal if it
9494 has already been set by another thread. */
9495 if (elf_tdata (abfd)->core->signal == 0)
9496 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9497 if (elf_tdata (abfd)->core->pid == 0)
9498 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9499
9500 /* pr_who exists on:
9501 solaris 2.5+
9502 unixware 4.2
9503 pr_who doesn't exist on:
9504 linux 2.[01]
9505 */
9506 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9507 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9508 #else
9509 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9510 #endif
9511 }
9512 #endif /* HAVE_PRSTATUS32_T */
9513 else
9514 {
9515 /* Fail - we don't know how to handle any other
9516 note size (ie. data object type). */
9517 return TRUE;
9518 }
9519
9520 /* Make a ".reg/999" section and a ".reg" section. */
9521 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9522 size, note->descpos + offset);
9523 }
9524 #endif /* defined (HAVE_PRSTATUS_T) */
9525
9526 /* Create a pseudosection containing the exact contents of NOTE. */
9527 static bfd_boolean
9528 elfcore_make_note_pseudosection (bfd *abfd,
9529 char *name,
9530 Elf_Internal_Note *note)
9531 {
9532 return _bfd_elfcore_make_pseudosection (abfd, name,
9533 note->descsz, note->descpos);
9534 }
9535
9536 /* There isn't a consistent prfpregset_t across platforms,
9537 but it doesn't matter, because we don't have to pick this
9538 data structure apart. */
9539
9540 static bfd_boolean
9541 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9542 {
9543 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9544 }
9545
9546 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9547 type of NT_PRXFPREG. Just include the whole note's contents
9548 literally. */
9549
9550 static bfd_boolean
9551 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9552 {
9553 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9554 }
9555
9556 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9557 with a note type of NT_X86_XSTATE. Just include the whole note's
9558 contents literally. */
9559
9560 static bfd_boolean
9561 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9562 {
9563 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9564 }
9565
9566 static bfd_boolean
9567 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9568 {
9569 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9570 }
9571
9572 static bfd_boolean
9573 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9574 {
9575 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9576 }
9577
9578 static bfd_boolean
9579 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9580 {
9581 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9582 }
9583
9584 static bfd_boolean
9585 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9586 {
9587 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9588 }
9589
9590 static bfd_boolean
9591 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9592 {
9593 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9594 }
9595
9596 static bfd_boolean
9597 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9598 {
9599 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9600 }
9601
9602 static bfd_boolean
9603 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9604 {
9605 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9606 }
9607
9608 static bfd_boolean
9609 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9610 {
9611 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9612 }
9613
9614 static bfd_boolean
9615 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9616 {
9617 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9618 }
9619
9620 static bfd_boolean
9621 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9622 {
9623 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9624 }
9625
9626 static bfd_boolean
9627 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9628 {
9629 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9630 }
9631
9632 static bfd_boolean
9633 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9634 {
9635 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9636 }
9637
9638 static bfd_boolean
9639 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9640 {
9641 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9642 }
9643
9644 static bfd_boolean
9645 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9646 {
9647 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9648 }
9649
9650 static bfd_boolean
9651 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9652 {
9653 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9654 }
9655
9656 static bfd_boolean
9657 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9658 {
9659 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9660 }
9661
9662 static bfd_boolean
9663 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9664 {
9665 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9666 }
9667
9668 static bfd_boolean
9669 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9670 {
9671 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9672 }
9673
9674 static bfd_boolean
9675 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9676 {
9677 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9678 }
9679
9680 static bfd_boolean
9681 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9682 {
9683 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9684 }
9685
9686 static bfd_boolean
9687 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9688 {
9689 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9690 }
9691
9692 static bfd_boolean
9693 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9694 {
9695 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9696 }
9697
9698 static bfd_boolean
9699 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9700 {
9701 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9702 }
9703
9704 static bfd_boolean
9705 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9706 {
9707 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9708 }
9709
9710 static bfd_boolean
9711 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9712 {
9713 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9714 }
9715
9716 static bfd_boolean
9717 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9718 {
9719 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9720 }
9721
9722 static bfd_boolean
9723 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9724 {
9725 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9726 }
9727
9728 static bfd_boolean
9729 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9730 {
9731 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9732 }
9733
9734 static bfd_boolean
9735 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9736 {
9737 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9738 }
9739
9740 static bfd_boolean
9741 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9742 {
9743 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9744 }
9745
9746 static bfd_boolean
9747 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9748 {
9749 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9750 }
9751
9752 static bfd_boolean
9753 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9754 {
9755 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9756 }
9757
9758 static bfd_boolean
9759 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9760 {
9761 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9762 }
9763
9764 static bfd_boolean
9765 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9766 {
9767 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9768 }
9769
9770 #if defined (HAVE_PRPSINFO_T)
9771 typedef prpsinfo_t elfcore_psinfo_t;
9772 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9773 typedef prpsinfo32_t elfcore_psinfo32_t;
9774 #endif
9775 #endif
9776
9777 #if defined (HAVE_PSINFO_T)
9778 typedef psinfo_t elfcore_psinfo_t;
9779 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9780 typedef psinfo32_t elfcore_psinfo32_t;
9781 #endif
9782 #endif
9783
9784 /* return a malloc'ed copy of a string at START which is at
9785 most MAX bytes long, possibly without a terminating '\0'.
9786 the copy will always have a terminating '\0'. */
9787
9788 char *
9789 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9790 {
9791 char *dups;
9792 char *end = (char *) memchr (start, '\0', max);
9793 size_t len;
9794
9795 if (end == NULL)
9796 len = max;
9797 else
9798 len = end - start;
9799
9800 dups = (char *) bfd_alloc (abfd, len + 1);
9801 if (dups == NULL)
9802 return NULL;
9803
9804 memcpy (dups, start, len);
9805 dups[len] = '\0';
9806
9807 return dups;
9808 }
9809
9810 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9811 static bfd_boolean
9812 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9813 {
9814 if (note->descsz == sizeof (elfcore_psinfo_t))
9815 {
9816 elfcore_psinfo_t psinfo;
9817
9818 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9819
9820 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9821 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9822 #endif
9823 elf_tdata (abfd)->core->program
9824 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9825 sizeof (psinfo.pr_fname));
9826
9827 elf_tdata (abfd)->core->command
9828 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9829 sizeof (psinfo.pr_psargs));
9830 }
9831 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9832 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9833 {
9834 /* 64-bit host, 32-bit corefile */
9835 elfcore_psinfo32_t psinfo;
9836
9837 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9838
9839 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9840 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9841 #endif
9842 elf_tdata (abfd)->core->program
9843 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9844 sizeof (psinfo.pr_fname));
9845
9846 elf_tdata (abfd)->core->command
9847 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9848 sizeof (psinfo.pr_psargs));
9849 }
9850 #endif
9851
9852 else
9853 {
9854 /* Fail - we don't know how to handle any other
9855 note size (ie. data object type). */
9856 return TRUE;
9857 }
9858
9859 /* Note that for some reason, a spurious space is tacked
9860 onto the end of the args in some (at least one anyway)
9861 implementations, so strip it off if it exists. */
9862
9863 {
9864 char *command = elf_tdata (abfd)->core->command;
9865 int n = strlen (command);
9866
9867 if (0 < n && command[n - 1] == ' ')
9868 command[n - 1] = '\0';
9869 }
9870
9871 return TRUE;
9872 }
9873 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9874
9875 #if defined (HAVE_PSTATUS_T)
9876 static bfd_boolean
9877 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9878 {
9879 if (note->descsz == sizeof (pstatus_t)
9880 #if defined (HAVE_PXSTATUS_T)
9881 || note->descsz == sizeof (pxstatus_t)
9882 #endif
9883 )
9884 {
9885 pstatus_t pstat;
9886
9887 memcpy (&pstat, note->descdata, sizeof (pstat));
9888
9889 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9890 }
9891 #if defined (HAVE_PSTATUS32_T)
9892 else if (note->descsz == sizeof (pstatus32_t))
9893 {
9894 /* 64-bit host, 32-bit corefile */
9895 pstatus32_t pstat;
9896
9897 memcpy (&pstat, note->descdata, sizeof (pstat));
9898
9899 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9900 }
9901 #endif
9902 /* Could grab some more details from the "representative"
9903 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9904 NT_LWPSTATUS note, presumably. */
9905
9906 return TRUE;
9907 }
9908 #endif /* defined (HAVE_PSTATUS_T) */
9909
9910 #if defined (HAVE_LWPSTATUS_T)
9911 static bfd_boolean
9912 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9913 {
9914 lwpstatus_t lwpstat;
9915 char buf[100];
9916 char *name;
9917 size_t len;
9918 asection *sect;
9919
9920 if (note->descsz != sizeof (lwpstat)
9921 #if defined (HAVE_LWPXSTATUS_T)
9922 && note->descsz != sizeof (lwpxstatus_t)
9923 #endif
9924 )
9925 return TRUE;
9926
9927 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9928
9929 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9930 /* Do not overwrite the core signal if it has already been set by
9931 another thread. */
9932 if (elf_tdata (abfd)->core->signal == 0)
9933 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9934
9935 /* Make a ".reg/999" section. */
9936
9937 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9938 len = strlen (buf) + 1;
9939 name = bfd_alloc (abfd, len);
9940 if (name == NULL)
9941 return FALSE;
9942 memcpy (name, buf, len);
9943
9944 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9945 if (sect == NULL)
9946 return FALSE;
9947
9948 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9949 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9950 sect->filepos = note->descpos
9951 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9952 #endif
9953
9954 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9955 sect->size = sizeof (lwpstat.pr_reg);
9956 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9957 #endif
9958
9959 sect->alignment_power = 2;
9960
9961 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9962 return FALSE;
9963
9964 /* Make a ".reg2/999" section */
9965
9966 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9967 len = strlen (buf) + 1;
9968 name = bfd_alloc (abfd, len);
9969 if (name == NULL)
9970 return FALSE;
9971 memcpy (name, buf, len);
9972
9973 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9974 if (sect == NULL)
9975 return FALSE;
9976
9977 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9978 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9979 sect->filepos = note->descpos
9980 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9981 #endif
9982
9983 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9984 sect->size = sizeof (lwpstat.pr_fpreg);
9985 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9986 #endif
9987
9988 sect->alignment_power = 2;
9989
9990 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9991 }
9992 #endif /* defined (HAVE_LWPSTATUS_T) */
9993
9994 static bfd_boolean
9995 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9996 {
9997 char buf[30];
9998 char *name;
9999 size_t len;
10000 asection *sect;
10001 int type;
10002 int is_active_thread;
10003 bfd_vma base_addr;
10004
10005 if (note->descsz < 728)
10006 return TRUE;
10007
10008 if (! CONST_STRNEQ (note->namedata, "win32"))
10009 return TRUE;
10010
10011 type = bfd_get_32 (abfd, note->descdata);
10012
10013 switch (type)
10014 {
10015 case 1 /* NOTE_INFO_PROCESS */:
10016 /* FIXME: need to add ->core->command. */
10017 /* process_info.pid */
10018 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
10019 /* process_info.signal */
10020 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
10021 break;
10022
10023 case 2 /* NOTE_INFO_THREAD */:
10024 /* Make a ".reg/999" section. */
10025 /* thread_info.tid */
10026 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
10027
10028 len = strlen (buf) + 1;
10029 name = (char *) bfd_alloc (abfd, len);
10030 if (name == NULL)
10031 return FALSE;
10032
10033 memcpy (name, buf, len);
10034
10035 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10036 if (sect == NULL)
10037 return FALSE;
10038
10039 /* sizeof (thread_info.thread_context) */
10040 sect->size = 716;
10041 /* offsetof (thread_info.thread_context) */
10042 sect->filepos = note->descpos + 12;
10043 sect->alignment_power = 2;
10044
10045 /* thread_info.is_active_thread */
10046 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10047
10048 if (is_active_thread)
10049 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10050 return FALSE;
10051 break;
10052
10053 case 3 /* NOTE_INFO_MODULE */:
10054 /* Make a ".module/xxxxxxxx" section. */
10055 /* module_info.base_address */
10056 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10057 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10058
10059 len = strlen (buf) + 1;
10060 name = (char *) bfd_alloc (abfd, len);
10061 if (name == NULL)
10062 return FALSE;
10063
10064 memcpy (name, buf, len);
10065
10066 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10067
10068 if (sect == NULL)
10069 return FALSE;
10070
10071 sect->size = note->descsz;
10072 sect->filepos = note->descpos;
10073 sect->alignment_power = 2;
10074 break;
10075
10076 default:
10077 return TRUE;
10078 }
10079
10080 return TRUE;
10081 }
10082
10083 static bfd_boolean
10084 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10085 {
10086 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10087
10088 switch (note->type)
10089 {
10090 default:
10091 return TRUE;
10092
10093 case NT_PRSTATUS:
10094 if (bed->elf_backend_grok_prstatus)
10095 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10096 return TRUE;
10097 #if defined (HAVE_PRSTATUS_T)
10098 return elfcore_grok_prstatus (abfd, note);
10099 #else
10100 return TRUE;
10101 #endif
10102
10103 #if defined (HAVE_PSTATUS_T)
10104 case NT_PSTATUS:
10105 return elfcore_grok_pstatus (abfd, note);
10106 #endif
10107
10108 #if defined (HAVE_LWPSTATUS_T)
10109 case NT_LWPSTATUS:
10110 return elfcore_grok_lwpstatus (abfd, note);
10111 #endif
10112
10113 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10114 return elfcore_grok_prfpreg (abfd, note);
10115
10116 case NT_WIN32PSTATUS:
10117 return elfcore_grok_win32pstatus (abfd, note);
10118
10119 case NT_PRXFPREG: /* Linux SSE extension */
10120 if (note->namesz == 6
10121 && strcmp (note->namedata, "LINUX") == 0)
10122 return elfcore_grok_prxfpreg (abfd, note);
10123 else
10124 return TRUE;
10125
10126 case NT_X86_XSTATE: /* Linux XSAVE extension */
10127 if (note->namesz == 6
10128 && strcmp (note->namedata, "LINUX") == 0)
10129 return elfcore_grok_xstatereg (abfd, note);
10130 else
10131 return TRUE;
10132
10133 case NT_PPC_VMX:
10134 if (note->namesz == 6
10135 && strcmp (note->namedata, "LINUX") == 0)
10136 return elfcore_grok_ppc_vmx (abfd, note);
10137 else
10138 return TRUE;
10139
10140 case NT_PPC_VSX:
10141 if (note->namesz == 6
10142 && strcmp (note->namedata, "LINUX") == 0)
10143 return elfcore_grok_ppc_vsx (abfd, note);
10144 else
10145 return TRUE;
10146
10147 case NT_PPC_TAR:
10148 if (note->namesz == 6
10149 && strcmp (note->namedata, "LINUX") == 0)
10150 return elfcore_grok_ppc_tar (abfd, note);
10151 else
10152 return TRUE;
10153
10154 case NT_PPC_PPR:
10155 if (note->namesz == 6
10156 && strcmp (note->namedata, "LINUX") == 0)
10157 return elfcore_grok_ppc_ppr (abfd, note);
10158 else
10159 return TRUE;
10160
10161 case NT_PPC_DSCR:
10162 if (note->namesz == 6
10163 && strcmp (note->namedata, "LINUX") == 0)
10164 return elfcore_grok_ppc_dscr (abfd, note);
10165 else
10166 return TRUE;
10167
10168 case NT_PPC_EBB:
10169 if (note->namesz == 6
10170 && strcmp (note->namedata, "LINUX") == 0)
10171 return elfcore_grok_ppc_ebb (abfd, note);
10172 else
10173 return TRUE;
10174
10175 case NT_PPC_PMU:
10176 if (note->namesz == 6
10177 && strcmp (note->namedata, "LINUX") == 0)
10178 return elfcore_grok_ppc_pmu (abfd, note);
10179 else
10180 return TRUE;
10181
10182 case NT_PPC_TM_CGPR:
10183 if (note->namesz == 6
10184 && strcmp (note->namedata, "LINUX") == 0)
10185 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10186 else
10187 return TRUE;
10188
10189 case NT_PPC_TM_CFPR:
10190 if (note->namesz == 6
10191 && strcmp (note->namedata, "LINUX") == 0)
10192 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10193 else
10194 return TRUE;
10195
10196 case NT_PPC_TM_CVMX:
10197 if (note->namesz == 6
10198 && strcmp (note->namedata, "LINUX") == 0)
10199 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10200 else
10201 return TRUE;
10202
10203 case NT_PPC_TM_CVSX:
10204 if (note->namesz == 6
10205 && strcmp (note->namedata, "LINUX") == 0)
10206 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10207 else
10208 return TRUE;
10209
10210 case NT_PPC_TM_SPR:
10211 if (note->namesz == 6
10212 && strcmp (note->namedata, "LINUX") == 0)
10213 return elfcore_grok_ppc_tm_spr (abfd, note);
10214 else
10215 return TRUE;
10216
10217 case NT_PPC_TM_CTAR:
10218 if (note->namesz == 6
10219 && strcmp (note->namedata, "LINUX") == 0)
10220 return elfcore_grok_ppc_tm_ctar (abfd, note);
10221 else
10222 return TRUE;
10223
10224 case NT_PPC_TM_CPPR:
10225 if (note->namesz == 6
10226 && strcmp (note->namedata, "LINUX") == 0)
10227 return elfcore_grok_ppc_tm_cppr (abfd, note);
10228 else
10229 return TRUE;
10230
10231 case NT_PPC_TM_CDSCR:
10232 if (note->namesz == 6
10233 && strcmp (note->namedata, "LINUX") == 0)
10234 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10235 else
10236 return TRUE;
10237
10238 case NT_S390_HIGH_GPRS:
10239 if (note->namesz == 6
10240 && strcmp (note->namedata, "LINUX") == 0)
10241 return elfcore_grok_s390_high_gprs (abfd, note);
10242 else
10243 return TRUE;
10244
10245 case NT_S390_TIMER:
10246 if (note->namesz == 6
10247 && strcmp (note->namedata, "LINUX") == 0)
10248 return elfcore_grok_s390_timer (abfd, note);
10249 else
10250 return TRUE;
10251
10252 case NT_S390_TODCMP:
10253 if (note->namesz == 6
10254 && strcmp (note->namedata, "LINUX") == 0)
10255 return elfcore_grok_s390_todcmp (abfd, note);
10256 else
10257 return TRUE;
10258
10259 case NT_S390_TODPREG:
10260 if (note->namesz == 6
10261 && strcmp (note->namedata, "LINUX") == 0)
10262 return elfcore_grok_s390_todpreg (abfd, note);
10263 else
10264 return TRUE;
10265
10266 case NT_S390_CTRS:
10267 if (note->namesz == 6
10268 && strcmp (note->namedata, "LINUX") == 0)
10269 return elfcore_grok_s390_ctrs (abfd, note);
10270 else
10271 return TRUE;
10272
10273 case NT_S390_PREFIX:
10274 if (note->namesz == 6
10275 && strcmp (note->namedata, "LINUX") == 0)
10276 return elfcore_grok_s390_prefix (abfd, note);
10277 else
10278 return TRUE;
10279
10280 case NT_S390_LAST_BREAK:
10281 if (note->namesz == 6
10282 && strcmp (note->namedata, "LINUX") == 0)
10283 return elfcore_grok_s390_last_break (abfd, note);
10284 else
10285 return TRUE;
10286
10287 case NT_S390_SYSTEM_CALL:
10288 if (note->namesz == 6
10289 && strcmp (note->namedata, "LINUX") == 0)
10290 return elfcore_grok_s390_system_call (abfd, note);
10291 else
10292 return TRUE;
10293
10294 case NT_S390_TDB:
10295 if (note->namesz == 6
10296 && strcmp (note->namedata, "LINUX") == 0)
10297 return elfcore_grok_s390_tdb (abfd, note);
10298 else
10299 return TRUE;
10300
10301 case NT_S390_VXRS_LOW:
10302 if (note->namesz == 6
10303 && strcmp (note->namedata, "LINUX") == 0)
10304 return elfcore_grok_s390_vxrs_low (abfd, note);
10305 else
10306 return TRUE;
10307
10308 case NT_S390_VXRS_HIGH:
10309 if (note->namesz == 6
10310 && strcmp (note->namedata, "LINUX") == 0)
10311 return elfcore_grok_s390_vxrs_high (abfd, note);
10312 else
10313 return TRUE;
10314
10315 case NT_S390_GS_CB:
10316 if (note->namesz == 6
10317 && strcmp (note->namedata, "LINUX") == 0)
10318 return elfcore_grok_s390_gs_cb (abfd, note);
10319 else
10320 return TRUE;
10321
10322 case NT_S390_GS_BC:
10323 if (note->namesz == 6
10324 && strcmp (note->namedata, "LINUX") == 0)
10325 return elfcore_grok_s390_gs_bc (abfd, note);
10326 else
10327 return TRUE;
10328
10329 case NT_ARM_VFP:
10330 if (note->namesz == 6
10331 && strcmp (note->namedata, "LINUX") == 0)
10332 return elfcore_grok_arm_vfp (abfd, note);
10333 else
10334 return TRUE;
10335
10336 case NT_ARM_TLS:
10337 if (note->namesz == 6
10338 && strcmp (note->namedata, "LINUX") == 0)
10339 return elfcore_grok_aarch_tls (abfd, note);
10340 else
10341 return TRUE;
10342
10343 case NT_ARM_HW_BREAK:
10344 if (note->namesz == 6
10345 && strcmp (note->namedata, "LINUX") == 0)
10346 return elfcore_grok_aarch_hw_break (abfd, note);
10347 else
10348 return TRUE;
10349
10350 case NT_ARM_HW_WATCH:
10351 if (note->namesz == 6
10352 && strcmp (note->namedata, "LINUX") == 0)
10353 return elfcore_grok_aarch_hw_watch (abfd, note);
10354 else
10355 return TRUE;
10356
10357 case NT_ARM_SVE:
10358 if (note->namesz == 6
10359 && strcmp (note->namedata, "LINUX") == 0)
10360 return elfcore_grok_aarch_sve (abfd, note);
10361 else
10362 return TRUE;
10363
10364 case NT_ARM_PAC_MASK:
10365 if (note->namesz == 6
10366 && strcmp (note->namedata, "LINUX") == 0)
10367 return elfcore_grok_aarch_pauth (abfd, note);
10368 else
10369 return TRUE;
10370
10371 case NT_PRPSINFO:
10372 case NT_PSINFO:
10373 if (bed->elf_backend_grok_psinfo)
10374 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10375 return TRUE;
10376 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10377 return elfcore_grok_psinfo (abfd, note);
10378 #else
10379 return TRUE;
10380 #endif
10381
10382 case NT_AUXV:
10383 return elfcore_make_auxv_note_section (abfd, note, 0);
10384
10385 case NT_FILE:
10386 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10387 note);
10388
10389 case NT_SIGINFO:
10390 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10391 note);
10392
10393 }
10394 }
10395
10396 static bfd_boolean
10397 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10398 {
10399 struct bfd_build_id* build_id;
10400
10401 if (note->descsz == 0)
10402 return FALSE;
10403
10404 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10405 if (build_id == NULL)
10406 return FALSE;
10407
10408 build_id->size = note->descsz;
10409 memcpy (build_id->data, note->descdata, note->descsz);
10410 abfd->build_id = build_id;
10411
10412 return TRUE;
10413 }
10414
10415 static bfd_boolean
10416 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10417 {
10418 switch (note->type)
10419 {
10420 default:
10421 return TRUE;
10422
10423 case NT_GNU_PROPERTY_TYPE_0:
10424 return _bfd_elf_parse_gnu_properties (abfd, note);
10425
10426 case NT_GNU_BUILD_ID:
10427 return elfobj_grok_gnu_build_id (abfd, note);
10428 }
10429 }
10430
10431 static bfd_boolean
10432 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10433 {
10434 struct sdt_note *cur =
10435 (struct sdt_note *) bfd_alloc (abfd,
10436 sizeof (struct sdt_note) + note->descsz);
10437
10438 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10439 cur->size = (bfd_size_type) note->descsz;
10440 memcpy (cur->data, note->descdata, note->descsz);
10441
10442 elf_tdata (abfd)->sdt_note_head = cur;
10443
10444 return TRUE;
10445 }
10446
10447 static bfd_boolean
10448 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10449 {
10450 switch (note->type)
10451 {
10452 case NT_STAPSDT:
10453 return elfobj_grok_stapsdt_note_1 (abfd, note);
10454
10455 default:
10456 return TRUE;
10457 }
10458 }
10459
10460 static bfd_boolean
10461 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10462 {
10463 size_t offset;
10464
10465 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10466 {
10467 case ELFCLASS32:
10468 if (note->descsz < 108)
10469 return FALSE;
10470 break;
10471
10472 case ELFCLASS64:
10473 if (note->descsz < 120)
10474 return FALSE;
10475 break;
10476
10477 default:
10478 return FALSE;
10479 }
10480
10481 /* Check for version 1 in pr_version. */
10482 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10483 return FALSE;
10484
10485 offset = 4;
10486
10487 /* Skip over pr_psinfosz. */
10488 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10489 offset += 4;
10490 else
10491 {
10492 offset += 4; /* Padding before pr_psinfosz. */
10493 offset += 8;
10494 }
10495
10496 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10497 elf_tdata (abfd)->core->program
10498 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10499 offset += 17;
10500
10501 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10502 elf_tdata (abfd)->core->command
10503 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10504 offset += 81;
10505
10506 /* Padding before pr_pid. */
10507 offset += 2;
10508
10509 /* The pr_pid field was added in version "1a". */
10510 if (note->descsz < offset + 4)
10511 return TRUE;
10512
10513 elf_tdata (abfd)->core->pid
10514 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10515
10516 return TRUE;
10517 }
10518
10519 static bfd_boolean
10520 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10521 {
10522 size_t offset;
10523 size_t size;
10524 size_t min_size;
10525
10526 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10527 Also compute minimum size of this note. */
10528 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10529 {
10530 case ELFCLASS32:
10531 offset = 4 + 4;
10532 min_size = offset + (4 * 2) + 4 + 4 + 4;
10533 break;
10534
10535 case ELFCLASS64:
10536 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10537 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10538 break;
10539
10540 default:
10541 return FALSE;
10542 }
10543
10544 if (note->descsz < min_size)
10545 return FALSE;
10546
10547 /* Check for version 1 in pr_version. */
10548 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10549 return FALSE;
10550
10551 /* Extract size of pr_reg from pr_gregsetsz. */
10552 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10553 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10554 {
10555 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10556 offset += 4 * 2;
10557 }
10558 else
10559 {
10560 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10561 offset += 8 * 2;
10562 }
10563
10564 /* Skip over pr_osreldate. */
10565 offset += 4;
10566
10567 /* Read signal from pr_cursig. */
10568 if (elf_tdata (abfd)->core->signal == 0)
10569 elf_tdata (abfd)->core->signal
10570 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10571 offset += 4;
10572
10573 /* Read TID from pr_pid. */
10574 elf_tdata (abfd)->core->lwpid
10575 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10576 offset += 4;
10577
10578 /* Padding before pr_reg. */
10579 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
10580 offset += 4;
10581
10582 /* Make sure that there is enough data remaining in the note. */
10583 if ((note->descsz - offset) < size)
10584 return FALSE;
10585
10586 /* Make a ".reg/999" section and a ".reg" section. */
10587 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10588 size, note->descpos + offset);
10589 }
10590
10591 static bfd_boolean
10592 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10593 {
10594 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10595
10596 switch (note->type)
10597 {
10598 case NT_PRSTATUS:
10599 if (bed->elf_backend_grok_freebsd_prstatus)
10600 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10601 return TRUE;
10602 return elfcore_grok_freebsd_prstatus (abfd, note);
10603
10604 case NT_FPREGSET:
10605 return elfcore_grok_prfpreg (abfd, note);
10606
10607 case NT_PRPSINFO:
10608 return elfcore_grok_freebsd_psinfo (abfd, note);
10609
10610 case NT_FREEBSD_THRMISC:
10611 if (note->namesz == 8)
10612 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10613 else
10614 return TRUE;
10615
10616 case NT_FREEBSD_PROCSTAT_PROC:
10617 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10618 note);
10619
10620 case NT_FREEBSD_PROCSTAT_FILES:
10621 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10622 note);
10623
10624 case NT_FREEBSD_PROCSTAT_VMMAP:
10625 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10626 note);
10627
10628 case NT_FREEBSD_PROCSTAT_AUXV:
10629 return elfcore_make_auxv_note_section (abfd, note, 4);
10630
10631 case NT_X86_XSTATE:
10632 if (note->namesz == 8)
10633 return elfcore_grok_xstatereg (abfd, note);
10634 else
10635 return TRUE;
10636
10637 case NT_FREEBSD_PTLWPINFO:
10638 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10639 note);
10640
10641 case NT_ARM_VFP:
10642 return elfcore_grok_arm_vfp (abfd, note);
10643
10644 default:
10645 return TRUE;
10646 }
10647 }
10648
10649 static bfd_boolean
10650 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10651 {
10652 char *cp;
10653
10654 cp = strchr (note->namedata, '@');
10655 if (cp != NULL)
10656 {
10657 *lwpidp = atoi(cp + 1);
10658 return TRUE;
10659 }
10660 return FALSE;
10661 }
10662
10663 static bfd_boolean
10664 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10665 {
10666 if (note->descsz <= 0x7c + 31)
10667 return FALSE;
10668
10669 /* Signal number at offset 0x08. */
10670 elf_tdata (abfd)->core->signal
10671 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10672
10673 /* Process ID at offset 0x50. */
10674 elf_tdata (abfd)->core->pid
10675 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10676
10677 /* Command name at 0x7c (max 32 bytes, including nul). */
10678 elf_tdata (abfd)->core->command
10679 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10680
10681 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10682 note);
10683 }
10684
10685
10686 static bfd_boolean
10687 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10688 {
10689 int lwp;
10690
10691 if (elfcore_netbsd_get_lwpid (note, &lwp))
10692 elf_tdata (abfd)->core->lwpid = lwp;
10693
10694 switch (note->type)
10695 {
10696 case NT_NETBSDCORE_PROCINFO:
10697 /* NetBSD-specific core "procinfo". Note that we expect to
10698 find this note before any of the others, which is fine,
10699 since the kernel writes this note out first when it
10700 creates a core file. */
10701 return elfcore_grok_netbsd_procinfo (abfd, note);
10702 #ifdef NT_NETBSDCORE_AUXV
10703 case NT_NETBSDCORE_AUXV:
10704 /* NetBSD-specific Elf Auxiliary Vector data. */
10705 return elfcore_make_auxv_note_section (abfd, note, 0);
10706 #endif
10707 default:
10708 break;
10709 }
10710
10711 /* As of March 2017 there are no other machine-independent notes
10712 defined for NetBSD core files. If the note type is less
10713 than the start of the machine-dependent note types, we don't
10714 understand it. */
10715
10716 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10717 return TRUE;
10718
10719
10720 switch (bfd_get_arch (abfd))
10721 {
10722 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10723 PT_GETFPREGS == mach+2. */
10724
10725 case bfd_arch_alpha:
10726 case bfd_arch_sparc:
10727 switch (note->type)
10728 {
10729 case NT_NETBSDCORE_FIRSTMACH+0:
10730 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10731
10732 case NT_NETBSDCORE_FIRSTMACH+2:
10733 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10734
10735 default:
10736 return TRUE;
10737 }
10738
10739 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10740 There's also old PT___GETREGS40 == mach + 1 for old reg
10741 structure which lacks GBR. */
10742
10743 case bfd_arch_sh:
10744 switch (note->type)
10745 {
10746 case NT_NETBSDCORE_FIRSTMACH+3:
10747 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10748
10749 case NT_NETBSDCORE_FIRSTMACH+5:
10750 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10751
10752 default:
10753 return TRUE;
10754 }
10755
10756 /* On all other arch's, PT_GETREGS == mach+1 and
10757 PT_GETFPREGS == mach+3. */
10758
10759 default:
10760 switch (note->type)
10761 {
10762 case NT_NETBSDCORE_FIRSTMACH+1:
10763 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10764
10765 case NT_NETBSDCORE_FIRSTMACH+3:
10766 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10767
10768 default:
10769 return TRUE;
10770 }
10771 }
10772 /* NOTREACHED */
10773 }
10774
10775 static bfd_boolean
10776 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10777 {
10778 if (note->descsz <= 0x48 + 31)
10779 return FALSE;
10780
10781 /* Signal number at offset 0x08. */
10782 elf_tdata (abfd)->core->signal
10783 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10784
10785 /* Process ID at offset 0x20. */
10786 elf_tdata (abfd)->core->pid
10787 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10788
10789 /* Command name at 0x48 (max 32 bytes, including nul). */
10790 elf_tdata (abfd)->core->command
10791 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10792
10793 return TRUE;
10794 }
10795
10796 static bfd_boolean
10797 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10798 {
10799 if (note->type == NT_OPENBSD_PROCINFO)
10800 return elfcore_grok_openbsd_procinfo (abfd, note);
10801
10802 if (note->type == NT_OPENBSD_REGS)
10803 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10804
10805 if (note->type == NT_OPENBSD_FPREGS)
10806 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10807
10808 if (note->type == NT_OPENBSD_XFPREGS)
10809 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10810
10811 if (note->type == NT_OPENBSD_AUXV)
10812 return elfcore_make_auxv_note_section (abfd, note, 0);
10813
10814 if (note->type == NT_OPENBSD_WCOOKIE)
10815 {
10816 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10817 SEC_HAS_CONTENTS);
10818
10819 if (sect == NULL)
10820 return FALSE;
10821 sect->size = note->descsz;
10822 sect->filepos = note->descpos;
10823 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10824
10825 return TRUE;
10826 }
10827
10828 return TRUE;
10829 }
10830
10831 static bfd_boolean
10832 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10833 {
10834 void *ddata = note->descdata;
10835 char buf[100];
10836 char *name;
10837 asection *sect;
10838 short sig;
10839 unsigned flags;
10840
10841 if (note->descsz < 16)
10842 return FALSE;
10843
10844 /* nto_procfs_status 'pid' field is at offset 0. */
10845 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10846
10847 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10848 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10849
10850 /* nto_procfs_status 'flags' field is at offset 8. */
10851 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10852
10853 /* nto_procfs_status 'what' field is at offset 14. */
10854 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10855 {
10856 elf_tdata (abfd)->core->signal = sig;
10857 elf_tdata (abfd)->core->lwpid = *tid;
10858 }
10859
10860 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10861 do not come from signals so we make sure we set the current
10862 thread just in case. */
10863 if (flags & 0x00000080)
10864 elf_tdata (abfd)->core->lwpid = *tid;
10865
10866 /* Make a ".qnx_core_status/%d" section. */
10867 sprintf (buf, ".qnx_core_status/%ld", *tid);
10868
10869 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10870 if (name == NULL)
10871 return FALSE;
10872 strcpy (name, buf);
10873
10874 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10875 if (sect == NULL)
10876 return FALSE;
10877
10878 sect->size = note->descsz;
10879 sect->filepos = note->descpos;
10880 sect->alignment_power = 2;
10881
10882 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10883 }
10884
10885 static bfd_boolean
10886 elfcore_grok_nto_regs (bfd *abfd,
10887 Elf_Internal_Note *note,
10888 long tid,
10889 char *base)
10890 {
10891 char buf[100];
10892 char *name;
10893 asection *sect;
10894
10895 /* Make a "(base)/%d" section. */
10896 sprintf (buf, "%s/%ld", base, tid);
10897
10898 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10899 if (name == NULL)
10900 return FALSE;
10901 strcpy (name, buf);
10902
10903 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10904 if (sect == NULL)
10905 return FALSE;
10906
10907 sect->size = note->descsz;
10908 sect->filepos = note->descpos;
10909 sect->alignment_power = 2;
10910
10911 /* This is the current thread. */
10912 if (elf_tdata (abfd)->core->lwpid == tid)
10913 return elfcore_maybe_make_sect (abfd, base, sect);
10914
10915 return TRUE;
10916 }
10917
10918 #define BFD_QNT_CORE_INFO 7
10919 #define BFD_QNT_CORE_STATUS 8
10920 #define BFD_QNT_CORE_GREG 9
10921 #define BFD_QNT_CORE_FPREG 10
10922
10923 static bfd_boolean
10924 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10925 {
10926 /* Every GREG section has a STATUS section before it. Store the
10927 tid from the previous call to pass down to the next gregs
10928 function. */
10929 static long tid = 1;
10930
10931 switch (note->type)
10932 {
10933 case BFD_QNT_CORE_INFO:
10934 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10935 case BFD_QNT_CORE_STATUS:
10936 return elfcore_grok_nto_status (abfd, note, &tid);
10937 case BFD_QNT_CORE_GREG:
10938 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10939 case BFD_QNT_CORE_FPREG:
10940 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10941 default:
10942 return TRUE;
10943 }
10944 }
10945
10946 static bfd_boolean
10947 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10948 {
10949 char *name;
10950 asection *sect;
10951 size_t len;
10952
10953 /* Use note name as section name. */
10954 len = note->namesz;
10955 name = (char *) bfd_alloc (abfd, len);
10956 if (name == NULL)
10957 return FALSE;
10958 memcpy (name, note->namedata, len);
10959 name[len - 1] = '\0';
10960
10961 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10962 if (sect == NULL)
10963 return FALSE;
10964
10965 sect->size = note->descsz;
10966 sect->filepos = note->descpos;
10967 sect->alignment_power = 1;
10968
10969 return TRUE;
10970 }
10971
10972 /* Function: elfcore_write_note
10973
10974 Inputs:
10975 buffer to hold note, and current size of buffer
10976 name of note
10977 type of note
10978 data for note
10979 size of data for note
10980
10981 Writes note to end of buffer. ELF64 notes are written exactly as
10982 for ELF32, despite the current (as of 2006) ELF gabi specifying
10983 that they ought to have 8-byte namesz and descsz field, and have
10984 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10985
10986 Return:
10987 Pointer to realloc'd buffer, *BUFSIZ updated. */
10988
10989 char *
10990 elfcore_write_note (bfd *abfd,
10991 char *buf,
10992 int *bufsiz,
10993 const char *name,
10994 int type,
10995 const void *input,
10996 int size)
10997 {
10998 Elf_External_Note *xnp;
10999 size_t namesz;
11000 size_t newspace;
11001 char *dest;
11002
11003 namesz = 0;
11004 if (name != NULL)
11005 namesz = strlen (name) + 1;
11006
11007 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11008
11009 buf = (char *) realloc (buf, *bufsiz + newspace);
11010 if (buf == NULL)
11011 return buf;
11012 dest = buf + *bufsiz;
11013 *bufsiz += newspace;
11014 xnp = (Elf_External_Note *) dest;
11015 H_PUT_32 (abfd, namesz, xnp->namesz);
11016 H_PUT_32 (abfd, size, xnp->descsz);
11017 H_PUT_32 (abfd, type, xnp->type);
11018 dest = xnp->name;
11019 if (name != NULL)
11020 {
11021 memcpy (dest, name, namesz);
11022 dest += namesz;
11023 while (namesz & 3)
11024 {
11025 *dest++ = '\0';
11026 ++namesz;
11027 }
11028 }
11029 memcpy (dest, input, size);
11030 dest += size;
11031 while (size & 3)
11032 {
11033 *dest++ = '\0';
11034 ++size;
11035 }
11036 return buf;
11037 }
11038
11039 /* gcc-8 warns (*) on all the strncpy calls in this function about
11040 possible string truncation. The "truncation" is not a bug. We
11041 have an external representation of structs with fields that are not
11042 necessarily NULL terminated and corresponding internal
11043 representation fields that are one larger so that they can always
11044 be NULL terminated.
11045 gcc versions between 4.2 and 4.6 do not allow pragma control of
11046 diagnostics inside functions, giving a hard error if you try to use
11047 the finer control available with later versions.
11048 gcc prior to 4.2 warns about diagnostic push and pop.
11049 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11050 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11051 (*) Depending on your system header files! */
11052 #if GCC_VERSION >= 8000
11053 # pragma GCC diagnostic push
11054 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11055 #endif
11056 char *
11057 elfcore_write_prpsinfo (bfd *abfd,
11058 char *buf,
11059 int *bufsiz,
11060 const char *fname,
11061 const char *psargs)
11062 {
11063 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11064
11065 if (bed->elf_backend_write_core_note != NULL)
11066 {
11067 char *ret;
11068 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11069 NT_PRPSINFO, fname, psargs);
11070 if (ret != NULL)
11071 return ret;
11072 }
11073
11074 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11075 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11076 if (bed->s->elfclass == ELFCLASS32)
11077 {
11078 # if defined (HAVE_PSINFO32_T)
11079 psinfo32_t data;
11080 int note_type = NT_PSINFO;
11081 # else
11082 prpsinfo32_t data;
11083 int note_type = NT_PRPSINFO;
11084 # endif
11085
11086 memset (&data, 0, sizeof (data));
11087 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11088 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11089 return elfcore_write_note (abfd, buf, bufsiz,
11090 "CORE", note_type, &data, sizeof (data));
11091 }
11092 else
11093 # endif
11094 {
11095 # if defined (HAVE_PSINFO_T)
11096 psinfo_t data;
11097 int note_type = NT_PSINFO;
11098 # else
11099 prpsinfo_t data;
11100 int note_type = NT_PRPSINFO;
11101 # endif
11102
11103 memset (&data, 0, sizeof (data));
11104 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11105 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11106 return elfcore_write_note (abfd, buf, bufsiz,
11107 "CORE", note_type, &data, sizeof (data));
11108 }
11109 #endif /* PSINFO_T or PRPSINFO_T */
11110
11111 free (buf);
11112 return NULL;
11113 }
11114 #if GCC_VERSION >= 8000
11115 # pragma GCC diagnostic pop
11116 #endif
11117
11118 char *
11119 elfcore_write_linux_prpsinfo32
11120 (bfd *abfd, char *buf, int *bufsiz,
11121 const struct elf_internal_linux_prpsinfo *prpsinfo)
11122 {
11123 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11124 {
11125 struct elf_external_linux_prpsinfo32_ugid16 data;
11126
11127 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11128 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11129 &data, sizeof (data));
11130 }
11131 else
11132 {
11133 struct elf_external_linux_prpsinfo32_ugid32 data;
11134
11135 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11136 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11137 &data, sizeof (data));
11138 }
11139 }
11140
11141 char *
11142 elfcore_write_linux_prpsinfo64
11143 (bfd *abfd, char *buf, int *bufsiz,
11144 const struct elf_internal_linux_prpsinfo *prpsinfo)
11145 {
11146 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11147 {
11148 struct elf_external_linux_prpsinfo64_ugid16 data;
11149
11150 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11151 return elfcore_write_note (abfd, buf, bufsiz,
11152 "CORE", NT_PRPSINFO, &data, sizeof (data));
11153 }
11154 else
11155 {
11156 struct elf_external_linux_prpsinfo64_ugid32 data;
11157
11158 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11159 return elfcore_write_note (abfd, buf, bufsiz,
11160 "CORE", NT_PRPSINFO, &data, sizeof (data));
11161 }
11162 }
11163
11164 char *
11165 elfcore_write_prstatus (bfd *abfd,
11166 char *buf,
11167 int *bufsiz,
11168 long pid,
11169 int cursig,
11170 const void *gregs)
11171 {
11172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11173
11174 if (bed->elf_backend_write_core_note != NULL)
11175 {
11176 char *ret;
11177 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11178 NT_PRSTATUS,
11179 pid, cursig, gregs);
11180 if (ret != NULL)
11181 return ret;
11182 }
11183
11184 #if defined (HAVE_PRSTATUS_T)
11185 #if defined (HAVE_PRSTATUS32_T)
11186 if (bed->s->elfclass == ELFCLASS32)
11187 {
11188 prstatus32_t prstat;
11189
11190 memset (&prstat, 0, sizeof (prstat));
11191 prstat.pr_pid = pid;
11192 prstat.pr_cursig = cursig;
11193 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11194 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11195 NT_PRSTATUS, &prstat, sizeof (prstat));
11196 }
11197 else
11198 #endif
11199 {
11200 prstatus_t prstat;
11201
11202 memset (&prstat, 0, sizeof (prstat));
11203 prstat.pr_pid = pid;
11204 prstat.pr_cursig = cursig;
11205 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11206 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11207 NT_PRSTATUS, &prstat, sizeof (prstat));
11208 }
11209 #endif /* HAVE_PRSTATUS_T */
11210
11211 free (buf);
11212 return NULL;
11213 }
11214
11215 #if defined (HAVE_LWPSTATUS_T)
11216 char *
11217 elfcore_write_lwpstatus (bfd *abfd,
11218 char *buf,
11219 int *bufsiz,
11220 long pid,
11221 int cursig,
11222 const void *gregs)
11223 {
11224 lwpstatus_t lwpstat;
11225 const char *note_name = "CORE";
11226
11227 memset (&lwpstat, 0, sizeof (lwpstat));
11228 lwpstat.pr_lwpid = pid >> 16;
11229 lwpstat.pr_cursig = cursig;
11230 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11231 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11232 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11233 #if !defined(gregs)
11234 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11235 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11236 #else
11237 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11238 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11239 #endif
11240 #endif
11241 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11242 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11243 }
11244 #endif /* HAVE_LWPSTATUS_T */
11245
11246 #if defined (HAVE_PSTATUS_T)
11247 char *
11248 elfcore_write_pstatus (bfd *abfd,
11249 char *buf,
11250 int *bufsiz,
11251 long pid,
11252 int cursig ATTRIBUTE_UNUSED,
11253 const void *gregs ATTRIBUTE_UNUSED)
11254 {
11255 const char *note_name = "CORE";
11256 #if defined (HAVE_PSTATUS32_T)
11257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11258
11259 if (bed->s->elfclass == ELFCLASS32)
11260 {
11261 pstatus32_t pstat;
11262
11263 memset (&pstat, 0, sizeof (pstat));
11264 pstat.pr_pid = pid & 0xffff;
11265 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11266 NT_PSTATUS, &pstat, sizeof (pstat));
11267 return buf;
11268 }
11269 else
11270 #endif
11271 {
11272 pstatus_t pstat;
11273
11274 memset (&pstat, 0, sizeof (pstat));
11275 pstat.pr_pid = pid & 0xffff;
11276 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11277 NT_PSTATUS, &pstat, sizeof (pstat));
11278 return buf;
11279 }
11280 }
11281 #endif /* HAVE_PSTATUS_T */
11282
11283 char *
11284 elfcore_write_prfpreg (bfd *abfd,
11285 char *buf,
11286 int *bufsiz,
11287 const void *fpregs,
11288 int size)
11289 {
11290 const char *note_name = "CORE";
11291 return elfcore_write_note (abfd, buf, bufsiz,
11292 note_name, NT_FPREGSET, fpregs, size);
11293 }
11294
11295 char *
11296 elfcore_write_prxfpreg (bfd *abfd,
11297 char *buf,
11298 int *bufsiz,
11299 const void *xfpregs,
11300 int size)
11301 {
11302 char *note_name = "LINUX";
11303 return elfcore_write_note (abfd, buf, bufsiz,
11304 note_name, NT_PRXFPREG, xfpregs, size);
11305 }
11306
11307 char *
11308 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11309 const void *xfpregs, int size)
11310 {
11311 char *note_name;
11312 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11313 note_name = "FreeBSD";
11314 else
11315 note_name = "LINUX";
11316 return elfcore_write_note (abfd, buf, bufsiz,
11317 note_name, NT_X86_XSTATE, xfpregs, size);
11318 }
11319
11320 char *
11321 elfcore_write_ppc_vmx (bfd *abfd,
11322 char *buf,
11323 int *bufsiz,
11324 const void *ppc_vmx,
11325 int size)
11326 {
11327 char *note_name = "LINUX";
11328 return elfcore_write_note (abfd, buf, bufsiz,
11329 note_name, NT_PPC_VMX, ppc_vmx, size);
11330 }
11331
11332 char *
11333 elfcore_write_ppc_vsx (bfd *abfd,
11334 char *buf,
11335 int *bufsiz,
11336 const void *ppc_vsx,
11337 int size)
11338 {
11339 char *note_name = "LINUX";
11340 return elfcore_write_note (abfd, buf, bufsiz,
11341 note_name, NT_PPC_VSX, ppc_vsx, size);
11342 }
11343
11344 char *
11345 elfcore_write_ppc_tar (bfd *abfd,
11346 char *buf,
11347 int *bufsiz,
11348 const void *ppc_tar,
11349 int size)
11350 {
11351 char *note_name = "LINUX";
11352 return elfcore_write_note (abfd, buf, bufsiz,
11353 note_name, NT_PPC_TAR, ppc_tar, size);
11354 }
11355
11356 char *
11357 elfcore_write_ppc_ppr (bfd *abfd,
11358 char *buf,
11359 int *bufsiz,
11360 const void *ppc_ppr,
11361 int size)
11362 {
11363 char *note_name = "LINUX";
11364 return elfcore_write_note (abfd, buf, bufsiz,
11365 note_name, NT_PPC_PPR, ppc_ppr, size);
11366 }
11367
11368 char *
11369 elfcore_write_ppc_dscr (bfd *abfd,
11370 char *buf,
11371 int *bufsiz,
11372 const void *ppc_dscr,
11373 int size)
11374 {
11375 char *note_name = "LINUX";
11376 return elfcore_write_note (abfd, buf, bufsiz,
11377 note_name, NT_PPC_DSCR, ppc_dscr, size);
11378 }
11379
11380 char *
11381 elfcore_write_ppc_ebb (bfd *abfd,
11382 char *buf,
11383 int *bufsiz,
11384 const void *ppc_ebb,
11385 int size)
11386 {
11387 char *note_name = "LINUX";
11388 return elfcore_write_note (abfd, buf, bufsiz,
11389 note_name, NT_PPC_EBB, ppc_ebb, size);
11390 }
11391
11392 char *
11393 elfcore_write_ppc_pmu (bfd *abfd,
11394 char *buf,
11395 int *bufsiz,
11396 const void *ppc_pmu,
11397 int size)
11398 {
11399 char *note_name = "LINUX";
11400 return elfcore_write_note (abfd, buf, bufsiz,
11401 note_name, NT_PPC_PMU, ppc_pmu, size);
11402 }
11403
11404 char *
11405 elfcore_write_ppc_tm_cgpr (bfd *abfd,
11406 char *buf,
11407 int *bufsiz,
11408 const void *ppc_tm_cgpr,
11409 int size)
11410 {
11411 char *note_name = "LINUX";
11412 return elfcore_write_note (abfd, buf, bufsiz,
11413 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
11414 }
11415
11416 char *
11417 elfcore_write_ppc_tm_cfpr (bfd *abfd,
11418 char *buf,
11419 int *bufsiz,
11420 const void *ppc_tm_cfpr,
11421 int size)
11422 {
11423 char *note_name = "LINUX";
11424 return elfcore_write_note (abfd, buf, bufsiz,
11425 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
11426 }
11427
11428 char *
11429 elfcore_write_ppc_tm_cvmx (bfd *abfd,
11430 char *buf,
11431 int *bufsiz,
11432 const void *ppc_tm_cvmx,
11433 int size)
11434 {
11435 char *note_name = "LINUX";
11436 return elfcore_write_note (abfd, buf, bufsiz,
11437 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
11438 }
11439
11440 char *
11441 elfcore_write_ppc_tm_cvsx (bfd *abfd,
11442 char *buf,
11443 int *bufsiz,
11444 const void *ppc_tm_cvsx,
11445 int size)
11446 {
11447 char *note_name = "LINUX";
11448 return elfcore_write_note (abfd, buf, bufsiz,
11449 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
11450 }
11451
11452 char *
11453 elfcore_write_ppc_tm_spr (bfd *abfd,
11454 char *buf,
11455 int *bufsiz,
11456 const void *ppc_tm_spr,
11457 int size)
11458 {
11459 char *note_name = "LINUX";
11460 return elfcore_write_note (abfd, buf, bufsiz,
11461 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
11462 }
11463
11464 char *
11465 elfcore_write_ppc_tm_ctar (bfd *abfd,
11466 char *buf,
11467 int *bufsiz,
11468 const void *ppc_tm_ctar,
11469 int size)
11470 {
11471 char *note_name = "LINUX";
11472 return elfcore_write_note (abfd, buf, bufsiz,
11473 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
11474 }
11475
11476 char *
11477 elfcore_write_ppc_tm_cppr (bfd *abfd,
11478 char *buf,
11479 int *bufsiz,
11480 const void *ppc_tm_cppr,
11481 int size)
11482 {
11483 char *note_name = "LINUX";
11484 return elfcore_write_note (abfd, buf, bufsiz,
11485 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
11486 }
11487
11488 char *
11489 elfcore_write_ppc_tm_cdscr (bfd *abfd,
11490 char *buf,
11491 int *bufsiz,
11492 const void *ppc_tm_cdscr,
11493 int size)
11494 {
11495 char *note_name = "LINUX";
11496 return elfcore_write_note (abfd, buf, bufsiz,
11497 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
11498 }
11499
11500 static char *
11501 elfcore_write_s390_high_gprs (bfd *abfd,
11502 char *buf,
11503 int *bufsiz,
11504 const void *s390_high_gprs,
11505 int size)
11506 {
11507 char *note_name = "LINUX";
11508 return elfcore_write_note (abfd, buf, bufsiz,
11509 note_name, NT_S390_HIGH_GPRS,
11510 s390_high_gprs, size);
11511 }
11512
11513 char *
11514 elfcore_write_s390_timer (bfd *abfd,
11515 char *buf,
11516 int *bufsiz,
11517 const void *s390_timer,
11518 int size)
11519 {
11520 char *note_name = "LINUX";
11521 return elfcore_write_note (abfd, buf, bufsiz,
11522 note_name, NT_S390_TIMER, s390_timer, size);
11523 }
11524
11525 char *
11526 elfcore_write_s390_todcmp (bfd *abfd,
11527 char *buf,
11528 int *bufsiz,
11529 const void *s390_todcmp,
11530 int size)
11531 {
11532 char *note_name = "LINUX";
11533 return elfcore_write_note (abfd, buf, bufsiz,
11534 note_name, NT_S390_TODCMP, s390_todcmp, size);
11535 }
11536
11537 char *
11538 elfcore_write_s390_todpreg (bfd *abfd,
11539 char *buf,
11540 int *bufsiz,
11541 const void *s390_todpreg,
11542 int size)
11543 {
11544 char *note_name = "LINUX";
11545 return elfcore_write_note (abfd, buf, bufsiz,
11546 note_name, NT_S390_TODPREG, s390_todpreg, size);
11547 }
11548
11549 char *
11550 elfcore_write_s390_ctrs (bfd *abfd,
11551 char *buf,
11552 int *bufsiz,
11553 const void *s390_ctrs,
11554 int size)
11555 {
11556 char *note_name = "LINUX";
11557 return elfcore_write_note (abfd, buf, bufsiz,
11558 note_name, NT_S390_CTRS, s390_ctrs, size);
11559 }
11560
11561 char *
11562 elfcore_write_s390_prefix (bfd *abfd,
11563 char *buf,
11564 int *bufsiz,
11565 const void *s390_prefix,
11566 int size)
11567 {
11568 char *note_name = "LINUX";
11569 return elfcore_write_note (abfd, buf, bufsiz,
11570 note_name, NT_S390_PREFIX, s390_prefix, size);
11571 }
11572
11573 char *
11574 elfcore_write_s390_last_break (bfd *abfd,
11575 char *buf,
11576 int *bufsiz,
11577 const void *s390_last_break,
11578 int size)
11579 {
11580 char *note_name = "LINUX";
11581 return elfcore_write_note (abfd, buf, bufsiz,
11582 note_name, NT_S390_LAST_BREAK,
11583 s390_last_break, size);
11584 }
11585
11586 char *
11587 elfcore_write_s390_system_call (bfd *abfd,
11588 char *buf,
11589 int *bufsiz,
11590 const void *s390_system_call,
11591 int size)
11592 {
11593 char *note_name = "LINUX";
11594 return elfcore_write_note (abfd, buf, bufsiz,
11595 note_name, NT_S390_SYSTEM_CALL,
11596 s390_system_call, size);
11597 }
11598
11599 char *
11600 elfcore_write_s390_tdb (bfd *abfd,
11601 char *buf,
11602 int *bufsiz,
11603 const void *s390_tdb,
11604 int size)
11605 {
11606 char *note_name = "LINUX";
11607 return elfcore_write_note (abfd, buf, bufsiz,
11608 note_name, NT_S390_TDB, s390_tdb, size);
11609 }
11610
11611 char *
11612 elfcore_write_s390_vxrs_low (bfd *abfd,
11613 char *buf,
11614 int *bufsiz,
11615 const void *s390_vxrs_low,
11616 int size)
11617 {
11618 char *note_name = "LINUX";
11619 return elfcore_write_note (abfd, buf, bufsiz,
11620 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11621 }
11622
11623 char *
11624 elfcore_write_s390_vxrs_high (bfd *abfd,
11625 char *buf,
11626 int *bufsiz,
11627 const void *s390_vxrs_high,
11628 int size)
11629 {
11630 char *note_name = "LINUX";
11631 return elfcore_write_note (abfd, buf, bufsiz,
11632 note_name, NT_S390_VXRS_HIGH,
11633 s390_vxrs_high, size);
11634 }
11635
11636 char *
11637 elfcore_write_s390_gs_cb (bfd *abfd,
11638 char *buf,
11639 int *bufsiz,
11640 const void *s390_gs_cb,
11641 int size)
11642 {
11643 char *note_name = "LINUX";
11644 return elfcore_write_note (abfd, buf, bufsiz,
11645 note_name, NT_S390_GS_CB,
11646 s390_gs_cb, size);
11647 }
11648
11649 char *
11650 elfcore_write_s390_gs_bc (bfd *abfd,
11651 char *buf,
11652 int *bufsiz,
11653 const void *s390_gs_bc,
11654 int size)
11655 {
11656 char *note_name = "LINUX";
11657 return elfcore_write_note (abfd, buf, bufsiz,
11658 note_name, NT_S390_GS_BC,
11659 s390_gs_bc, size);
11660 }
11661
11662 char *
11663 elfcore_write_arm_vfp (bfd *abfd,
11664 char *buf,
11665 int *bufsiz,
11666 const void *arm_vfp,
11667 int size)
11668 {
11669 char *note_name = "LINUX";
11670 return elfcore_write_note (abfd, buf, bufsiz,
11671 note_name, NT_ARM_VFP, arm_vfp, size);
11672 }
11673
11674 char *
11675 elfcore_write_aarch_tls (bfd *abfd,
11676 char *buf,
11677 int *bufsiz,
11678 const void *aarch_tls,
11679 int size)
11680 {
11681 char *note_name = "LINUX";
11682 return elfcore_write_note (abfd, buf, bufsiz,
11683 note_name, NT_ARM_TLS, aarch_tls, size);
11684 }
11685
11686 char *
11687 elfcore_write_aarch_hw_break (bfd *abfd,
11688 char *buf,
11689 int *bufsiz,
11690 const void *aarch_hw_break,
11691 int size)
11692 {
11693 char *note_name = "LINUX";
11694 return elfcore_write_note (abfd, buf, bufsiz,
11695 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11696 }
11697
11698 char *
11699 elfcore_write_aarch_hw_watch (bfd *abfd,
11700 char *buf,
11701 int *bufsiz,
11702 const void *aarch_hw_watch,
11703 int size)
11704 {
11705 char *note_name = "LINUX";
11706 return elfcore_write_note (abfd, buf, bufsiz,
11707 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11708 }
11709
11710 char *
11711 elfcore_write_aarch_sve (bfd *abfd,
11712 char *buf,
11713 int *bufsiz,
11714 const void *aarch_sve,
11715 int size)
11716 {
11717 char *note_name = "LINUX";
11718 return elfcore_write_note (abfd, buf, bufsiz,
11719 note_name, NT_ARM_SVE, aarch_sve, size);
11720 }
11721
11722 char *
11723 elfcore_write_aarch_pauth (bfd *abfd,
11724 char *buf,
11725 int *bufsiz,
11726 const void *aarch_pauth,
11727 int size)
11728 {
11729 char *note_name = "LINUX";
11730 return elfcore_write_note (abfd, buf, bufsiz,
11731 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11732 }
11733
11734 char *
11735 elfcore_write_register_note (bfd *abfd,
11736 char *buf,
11737 int *bufsiz,
11738 const char *section,
11739 const void *data,
11740 int size)
11741 {
11742 if (strcmp (section, ".reg2") == 0)
11743 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11744 if (strcmp (section, ".reg-xfp") == 0)
11745 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
11746 if (strcmp (section, ".reg-xstate") == 0)
11747 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
11748 if (strcmp (section, ".reg-ppc-vmx") == 0)
11749 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
11750 if (strcmp (section, ".reg-ppc-vsx") == 0)
11751 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
11752 if (strcmp (section, ".reg-ppc-tar") == 0)
11753 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11754 if (strcmp (section, ".reg-ppc-ppr") == 0)
11755 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11756 if (strcmp (section, ".reg-ppc-dscr") == 0)
11757 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11758 if (strcmp (section, ".reg-ppc-ebb") == 0)
11759 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11760 if (strcmp (section, ".reg-ppc-pmu") == 0)
11761 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11762 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11763 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11764 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11765 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11766 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11767 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11768 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11769 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11770 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11771 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11772 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11773 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11774 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11775 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11776 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11777 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
11778 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11779 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
11780 if (strcmp (section, ".reg-s390-timer") == 0)
11781 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11782 if (strcmp (section, ".reg-s390-todcmp") == 0)
11783 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11784 if (strcmp (section, ".reg-s390-todpreg") == 0)
11785 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11786 if (strcmp (section, ".reg-s390-ctrs") == 0)
11787 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11788 if (strcmp (section, ".reg-s390-prefix") == 0)
11789 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
11790 if (strcmp (section, ".reg-s390-last-break") == 0)
11791 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11792 if (strcmp (section, ".reg-s390-system-call") == 0)
11793 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
11794 if (strcmp (section, ".reg-s390-tdb") == 0)
11795 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
11796 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11797 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11798 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11799 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
11800 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11801 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11802 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11803 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
11804 if (strcmp (section, ".reg-arm-vfp") == 0)
11805 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
11806 if (strcmp (section, ".reg-aarch-tls") == 0)
11807 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11808 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11809 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11810 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11811 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
11812 if (strcmp (section, ".reg-aarch-sve") == 0)
11813 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
11814 if (strcmp (section, ".reg-aarch-pauth") == 0)
11815 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
11816 return NULL;
11817 }
11818
11819 static bfd_boolean
11820 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11821 size_t align)
11822 {
11823 char *p;
11824
11825 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11826 gABI specifies that PT_NOTE alignment should be aligned to 4
11827 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11828 align is less than 4, we use 4 byte alignment. */
11829 if (align < 4)
11830 align = 4;
11831 if (align != 4 && align != 8)
11832 return FALSE;
11833
11834 p = buf;
11835 while (p < buf + size)
11836 {
11837 Elf_External_Note *xnp = (Elf_External_Note *) p;
11838 Elf_Internal_Note in;
11839
11840 if (offsetof (Elf_External_Note, name) > buf - p + size)
11841 return FALSE;
11842
11843 in.type = H_GET_32 (abfd, xnp->type);
11844
11845 in.namesz = H_GET_32 (abfd, xnp->namesz);
11846 in.namedata = xnp->name;
11847 if (in.namesz > buf - in.namedata + size)
11848 return FALSE;
11849
11850 in.descsz = H_GET_32 (abfd, xnp->descsz);
11851 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
11852 in.descpos = offset + (in.descdata - buf);
11853 if (in.descsz != 0
11854 && (in.descdata >= buf + size
11855 || in.descsz > buf - in.descdata + size))
11856 return FALSE;
11857
11858 switch (bfd_get_format (abfd))
11859 {
11860 default:
11861 return TRUE;
11862
11863 case bfd_core:
11864 {
11865 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11866 struct
11867 {
11868 const char * string;
11869 size_t len;
11870 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
11871 }
11872 grokers[] =
11873 {
11874 GROKER_ELEMENT ("", elfcore_grok_note),
11875 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11876 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11877 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11878 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11879 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
11880 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note)
11881 };
11882 #undef GROKER_ELEMENT
11883 int i;
11884
11885 for (i = ARRAY_SIZE (grokers); i--;)
11886 {
11887 if (in.namesz >= grokers[i].len
11888 && strncmp (in.namedata, grokers[i].string,
11889 grokers[i].len) == 0)
11890 {
11891 if (! grokers[i].func (abfd, & in))
11892 return FALSE;
11893 break;
11894 }
11895 }
11896 break;
11897 }
11898
11899 case bfd_object:
11900 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11901 {
11902 if (! elfobj_grok_gnu_note (abfd, &in))
11903 return FALSE;
11904 }
11905 else if (in.namesz == sizeof "stapsdt"
11906 && strcmp (in.namedata, "stapsdt") == 0)
11907 {
11908 if (! elfobj_grok_stapsdt_note (abfd, &in))
11909 return FALSE;
11910 }
11911 break;
11912 }
11913
11914 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
11915 }
11916
11917 return TRUE;
11918 }
11919
11920 bfd_boolean
11921 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11922 size_t align)
11923 {
11924 char *buf;
11925
11926 if (size == 0 || (size + 1) == 0)
11927 return TRUE;
11928
11929 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11930 return FALSE;
11931
11932 buf = (char *) bfd_malloc (size + 1);
11933 if (buf == NULL)
11934 return FALSE;
11935
11936 /* PR 17512: file: ec08f814
11937 0-termintate the buffer so that string searches will not overflow. */
11938 buf[size] = 0;
11939
11940 if (bfd_bread (buf, size, abfd) != size
11941 || !elf_parse_notes (abfd, buf, size, offset, align))
11942 {
11943 free (buf);
11944 return FALSE;
11945 }
11946
11947 free (buf);
11948 return TRUE;
11949 }
11950
11951 /* Providing external access to the ELF program header table. */
11953
11954 /* Return an upper bound on the number of bytes required to store a
11955 copy of ABFD's program header table entries. Return -1 if an error
11956 occurs; bfd_get_error will return an appropriate code. */
11957
11958 long
11959 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11960 {
11961 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11962 {
11963 bfd_set_error (bfd_error_wrong_format);
11964 return -1;
11965 }
11966
11967 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11968 }
11969
11970 /* Copy ABFD's program header table entries to *PHDRS. The entries
11971 will be stored as an array of Elf_Internal_Phdr structures, as
11972 defined in include/elf/internal.h. To find out how large the
11973 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11974
11975 Return the number of program header table entries read, or -1 if an
11976 error occurs; bfd_get_error will return an appropriate code. */
11977
11978 int
11979 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11980 {
11981 int num_phdrs;
11982
11983 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11984 {
11985 bfd_set_error (bfd_error_wrong_format);
11986 return -1;
11987 }
11988
11989 num_phdrs = elf_elfheader (abfd)->e_phnum;
11990 if (num_phdrs != 0)
11991 memcpy (phdrs, elf_tdata (abfd)->phdr,
11992 num_phdrs * sizeof (Elf_Internal_Phdr));
11993
11994 return num_phdrs;
11995 }
11996
11997 enum elf_reloc_type_class
11998 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11999 const asection *rel_sec ATTRIBUTE_UNUSED,
12000 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12001 {
12002 return reloc_class_normal;
12003 }
12004
12005 /* For RELA architectures, return the relocation value for a
12006 relocation against a local symbol. */
12007
12008 bfd_vma
12009 _bfd_elf_rela_local_sym (bfd *abfd,
12010 Elf_Internal_Sym *sym,
12011 asection **psec,
12012 Elf_Internal_Rela *rel)
12013 {
12014 asection *sec = *psec;
12015 bfd_vma relocation;
12016
12017 relocation = (sec->output_section->vma
12018 + sec->output_offset
12019 + sym->st_value);
12020 if ((sec->flags & SEC_MERGE)
12021 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12022 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12023 {
12024 rel->r_addend =
12025 _bfd_merged_section_offset (abfd, psec,
12026 elf_section_data (sec)->sec_info,
12027 sym->st_value + rel->r_addend);
12028 if (sec != *psec)
12029 {
12030 /* If we have changed the section, and our original section is
12031 marked with SEC_EXCLUDE, it means that the original
12032 SEC_MERGE section has been completely subsumed in some
12033 other SEC_MERGE section. In this case, we need to leave
12034 some info around for --emit-relocs. */
12035 if ((sec->flags & SEC_EXCLUDE) != 0)
12036 sec->kept_section = *psec;
12037 sec = *psec;
12038 }
12039 rel->r_addend -= relocation;
12040 rel->r_addend += sec->output_section->vma + sec->output_offset;
12041 }
12042 return relocation;
12043 }
12044
12045 bfd_vma
12046 _bfd_elf_rel_local_sym (bfd *abfd,
12047 Elf_Internal_Sym *sym,
12048 asection **psec,
12049 bfd_vma addend)
12050 {
12051 asection *sec = *psec;
12052
12053 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12054 return sym->st_value + addend;
12055
12056 return _bfd_merged_section_offset (abfd, psec,
12057 elf_section_data (sec)->sec_info,
12058 sym->st_value + addend);
12059 }
12060
12061 /* Adjust an address within a section. Given OFFSET within SEC, return
12062 the new offset within the section, based upon changes made to the
12063 section. Returns -1 if the offset is now invalid.
12064 The offset (in abnd out) is in target sized bytes, however big a
12065 byte may be. */
12066
12067 bfd_vma
12068 _bfd_elf_section_offset (bfd *abfd,
12069 struct bfd_link_info *info,
12070 asection *sec,
12071 bfd_vma offset)
12072 {
12073 switch (sec->sec_info_type)
12074 {
12075 case SEC_INFO_TYPE_STABS:
12076 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12077 offset);
12078 case SEC_INFO_TYPE_EH_FRAME:
12079 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12080
12081 default:
12082 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12083 {
12084 /* Reverse the offset. */
12085 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12086 bfd_size_type address_size = bed->s->arch_size / 8;
12087
12088 /* address_size and sec->size are in octets. Convert
12089 to bytes before subtracting the original offset. */
12090 offset = ((sec->size - address_size)
12091 / bfd_octets_per_byte (abfd, sec) - offset);
12092 }
12093 return offset;
12094 }
12095 }
12096
12097 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12099 reconstruct an ELF file by reading the segments out of remote memory
12100 based on the ELF file header at EHDR_VMA and the ELF program headers it
12101 points to. If not null, *LOADBASEP is filled in with the difference
12102 between the VMAs from which the segments were read, and the VMAs the
12103 file headers (and hence BFD's idea of each section's VMA) put them at.
12104
12105 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12106 remote memory at target address VMA into the local buffer at MYADDR; it
12107 should return zero on success or an `errno' code on failure. TEMPL must
12108 be a BFD for an ELF target with the word size and byte order found in
12109 the remote memory. */
12110
12111 bfd *
12112 bfd_elf_bfd_from_remote_memory
12113 (bfd *templ,
12114 bfd_vma ehdr_vma,
12115 bfd_size_type size,
12116 bfd_vma *loadbasep,
12117 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12118 {
12119 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12120 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12121 }
12122
12123 long
12125 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12126 long symcount ATTRIBUTE_UNUSED,
12127 asymbol **syms ATTRIBUTE_UNUSED,
12128 long dynsymcount,
12129 asymbol **dynsyms,
12130 asymbol **ret)
12131 {
12132 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12133 asection *relplt;
12134 asymbol *s;
12135 const char *relplt_name;
12136 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12137 arelent *p;
12138 long count, i, n;
12139 size_t size;
12140 Elf_Internal_Shdr *hdr;
12141 char *names;
12142 asection *plt;
12143
12144 *ret = NULL;
12145
12146 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12147 return 0;
12148
12149 if (dynsymcount <= 0)
12150 return 0;
12151
12152 if (!bed->plt_sym_val)
12153 return 0;
12154
12155 relplt_name = bed->relplt_name;
12156 if (relplt_name == NULL)
12157 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12158 relplt = bfd_get_section_by_name (abfd, relplt_name);
12159 if (relplt == NULL)
12160 return 0;
12161
12162 hdr = &elf_section_data (relplt)->this_hdr;
12163 if (hdr->sh_link != elf_dynsymtab (abfd)
12164 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12165 return 0;
12166
12167 plt = bfd_get_section_by_name (abfd, ".plt");
12168 if (plt == NULL)
12169 return 0;
12170
12171 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12172 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
12173 return -1;
12174
12175 count = relplt->size / hdr->sh_entsize;
12176 size = count * sizeof (asymbol);
12177 p = relplt->relocation;
12178 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12179 {
12180 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12181 if (p->addend != 0)
12182 {
12183 #ifdef BFD64
12184 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12185 #else
12186 size += sizeof ("+0x") - 1 + 8;
12187 #endif
12188 }
12189 }
12190
12191 s = *ret = (asymbol *) bfd_malloc (size);
12192 if (s == NULL)
12193 return -1;
12194
12195 names = (char *) (s + count);
12196 p = relplt->relocation;
12197 n = 0;
12198 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12199 {
12200 size_t len;
12201 bfd_vma addr;
12202
12203 addr = bed->plt_sym_val (i, plt, p);
12204 if (addr == (bfd_vma) -1)
12205 continue;
12206
12207 *s = **p->sym_ptr_ptr;
12208 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12209 we are defining a symbol, ensure one of them is set. */
12210 if ((s->flags & BSF_LOCAL) == 0)
12211 s->flags |= BSF_GLOBAL;
12212 s->flags |= BSF_SYNTHETIC;
12213 s->section = plt;
12214 s->value = addr - plt->vma;
12215 s->name = names;
12216 s->udata.p = NULL;
12217 len = strlen ((*p->sym_ptr_ptr)->name);
12218 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12219 names += len;
12220 if (p->addend != 0)
12221 {
12222 char buf[30], *a;
12223
12224 memcpy (names, "+0x", sizeof ("+0x") - 1);
12225 names += sizeof ("+0x") - 1;
12226 bfd_sprintf_vma (abfd, buf, p->addend);
12227 for (a = buf; *a == '0'; ++a)
12228 ;
12229 len = strlen (a);
12230 memcpy (names, a, len);
12231 names += len;
12232 }
12233 memcpy (names, "@plt", sizeof ("@plt"));
12234 names += sizeof ("@plt");
12235 ++s, ++n;
12236 }
12237
12238 return n;
12239 }
12240
12241 /* It is only used by x86-64 so far.
12242 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12243 but current usage would allow all of _bfd_std_section to be zero. */
12244 static const asymbol lcomm_sym
12245 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
12246 asection _bfd_elf_large_com_section
12247 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
12248 "LARGE_COMMON", 0, SEC_IS_COMMON);
12249
12250 bfd_boolean
12251 _bfd_elf_final_write_processing (bfd *abfd)
12252 {
12253 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
12254
12255 i_ehdrp = elf_elfheader (abfd);
12256
12257 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12258 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
12259
12260 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12261 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12262 STB_GNU_UNIQUE binding. */
12263 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12264 {
12265 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12266 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12267 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12268 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12269 {
12270 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12271 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12272 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12273 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12274 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12275 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12276 bfd_set_error (bfd_error_sorry);
12277 return FALSE;
12278 }
12279 }
12280 return TRUE;
12281 }
12282
12283
12284 /* Return TRUE for ELF symbol types that represent functions.
12285 This is the default version of this function, which is sufficient for
12286 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12287
12288 bfd_boolean
12289 _bfd_elf_is_function_type (unsigned int type)
12290 {
12291 return (type == STT_FUNC
12292 || type == STT_GNU_IFUNC);
12293 }
12294
12295 /* If the ELF symbol SYM might be a function in SEC, return the
12296 function size and set *CODE_OFF to the function's entry point,
12297 otherwise return zero. */
12298
12299 bfd_size_type
12300 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12301 bfd_vma *code_off)
12302 {
12303 bfd_size_type size;
12304
12305 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
12306 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12307 || sym->section != sec)
12308 return 0;
12309
12310 *code_off = sym->value;
12311 size = 0;
12312 if (!(sym->flags & BSF_SYNTHETIC))
12313 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12314 if (size == 0)
12315 size = 1;
12316 return size;
12317 }
12318