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