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