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