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