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