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