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