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