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