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