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