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