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