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