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