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