* readelf.c (byte_get_signed): New.
[binutils.git] / bfd / elf.c
blobe1cbe0aee0d8b093ce34a0570996191e58a6fd3c
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* SECTION
23 ELF backends
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
34 #define _SYSCALL32
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "bfdlink.h"
38 #include "libbfd.h"
39 #define ARCH_SIZE 0
40 #include "elf-bfd.h"
41 #include "libiberty.h"
43 static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, bfd_boolean));
45 static bfd_boolean map_sections_to_segments
46 PARAMS ((bfd *));
47 static int elf_sort_sections
48 PARAMS ((const PTR, const PTR));
49 static bfd_boolean assign_file_positions_for_segments
50 PARAMS ((bfd *));
51 static bfd_boolean assign_file_positions_except_relocs
52 PARAMS ((bfd *));
53 static bfd_boolean prep_headers
54 PARAMS ((bfd *));
55 static bfd_boolean swap_out_syms
56 PARAMS ((bfd *, struct bfd_strtab_hash **, int));
57 static bfd_boolean copy_private_bfd_data
58 PARAMS ((bfd *, bfd *));
59 static char *elf_read
60 PARAMS ((bfd *, file_ptr, bfd_size_type));
61 static const char *group_signature
62 PARAMS ((bfd *, Elf_Internal_Shdr *));
63 static bfd_boolean setup_group
64 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
65 static void merge_sections_remove_hook
66 PARAMS ((bfd *, asection *));
67 static void elf_fake_sections
68 PARAMS ((bfd *, asection *, PTR));
69 static bfd_boolean assign_section_numbers
70 PARAMS ((bfd *));
71 static INLINE int sym_is_global
72 PARAMS ((bfd *, asymbol *));
73 static bfd_boolean elf_map_symbols
74 PARAMS ((bfd *));
75 static bfd_size_type get_program_header_size
76 PARAMS ((bfd *));
77 static bfd_boolean elfcore_read_notes
78 PARAMS ((bfd *, file_ptr, bfd_size_type));
79 static bfd_boolean elf_find_function
80 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
81 const char **));
82 static int elfcore_make_pid
83 PARAMS ((bfd *));
84 static bfd_boolean elfcore_maybe_make_sect
85 PARAMS ((bfd *, char *, asection *));
86 static bfd_boolean elfcore_make_note_pseudosection
87 PARAMS ((bfd *, char *, Elf_Internal_Note *));
88 static bfd_boolean elfcore_grok_prfpreg
89 PARAMS ((bfd *, Elf_Internal_Note *));
90 static bfd_boolean elfcore_grok_prxfpreg
91 PARAMS ((bfd *, Elf_Internal_Note *));
92 static bfd_boolean elfcore_grok_note
93 PARAMS ((bfd *, Elf_Internal_Note *));
94 static bfd_boolean elfcore_netbsd_get_lwpid
95 PARAMS ((Elf_Internal_Note *, int *));
96 static bfd_boolean elfcore_grok_netbsd_procinfo
97 PARAMS ((bfd *, Elf_Internal_Note *));
98 static bfd_boolean elfcore_grok_netbsd_note
99 PARAMS ((bfd *, Elf_Internal_Note *));
100 static bfd_boolean elfcore_grok_nto_gregs
101 PARAMS ((bfd *, Elf_Internal_Note *, pid_t));
102 static bfd_boolean elfcore_grok_nto_status
103 PARAMS ((bfd *, Elf_Internal_Note *, pid_t *));
104 static bfd_boolean elfcore_grok_nto_note
105 PARAMS ((bfd *, Elf_Internal_Note *));
107 /* Swap version information in and out. The version information is
108 currently size independent. If that ever changes, this code will
109 need to move into elfcode.h. */
111 /* Swap in a Verdef structure. */
113 void
114 _bfd_elf_swap_verdef_in (abfd, src, dst)
115 bfd *abfd;
116 const Elf_External_Verdef *src;
117 Elf_Internal_Verdef *dst;
119 dst->vd_version = H_GET_16 (abfd, src->vd_version);
120 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
121 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
122 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
123 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
124 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
125 dst->vd_next = H_GET_32 (abfd, src->vd_next);
128 /* Swap out a Verdef structure. */
130 void
131 _bfd_elf_swap_verdef_out (abfd, src, dst)
132 bfd *abfd;
133 const Elf_Internal_Verdef *src;
134 Elf_External_Verdef *dst;
136 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
137 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
138 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
139 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
140 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
141 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
142 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
145 /* Swap in a Verdaux structure. */
147 void
148 _bfd_elf_swap_verdaux_in (abfd, src, dst)
149 bfd *abfd;
150 const Elf_External_Verdaux *src;
151 Elf_Internal_Verdaux *dst;
153 dst->vda_name = H_GET_32 (abfd, src->vda_name);
154 dst->vda_next = H_GET_32 (abfd, src->vda_next);
157 /* Swap out a Verdaux structure. */
159 void
160 _bfd_elf_swap_verdaux_out (abfd, src, dst)
161 bfd *abfd;
162 const Elf_Internal_Verdaux *src;
163 Elf_External_Verdaux *dst;
165 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
166 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
169 /* Swap in a Verneed structure. */
171 void
172 _bfd_elf_swap_verneed_in (abfd, src, dst)
173 bfd *abfd;
174 const Elf_External_Verneed *src;
175 Elf_Internal_Verneed *dst;
177 dst->vn_version = H_GET_16 (abfd, src->vn_version);
178 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
179 dst->vn_file = H_GET_32 (abfd, src->vn_file);
180 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
181 dst->vn_next = H_GET_32 (abfd, src->vn_next);
184 /* Swap out a Verneed structure. */
186 void
187 _bfd_elf_swap_verneed_out (abfd, src, dst)
188 bfd *abfd;
189 const Elf_Internal_Verneed *src;
190 Elf_External_Verneed *dst;
192 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
193 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
194 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
195 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
196 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
199 /* Swap in a Vernaux structure. */
201 void
202 _bfd_elf_swap_vernaux_in (abfd, src, dst)
203 bfd *abfd;
204 const Elf_External_Vernaux *src;
205 Elf_Internal_Vernaux *dst;
207 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
208 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
209 dst->vna_other = H_GET_16 (abfd, src->vna_other);
210 dst->vna_name = H_GET_32 (abfd, src->vna_name);
211 dst->vna_next = H_GET_32 (abfd, src->vna_next);
214 /* Swap out a Vernaux structure. */
216 void
217 _bfd_elf_swap_vernaux_out (abfd, src, dst)
218 bfd *abfd;
219 const Elf_Internal_Vernaux *src;
220 Elf_External_Vernaux *dst;
222 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
223 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
224 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
225 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
226 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
229 /* Swap in a Versym structure. */
231 void
232 _bfd_elf_swap_versym_in (abfd, src, dst)
233 bfd *abfd;
234 const Elf_External_Versym *src;
235 Elf_Internal_Versym *dst;
237 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
240 /* Swap out a Versym structure. */
242 void
243 _bfd_elf_swap_versym_out (abfd, src, dst)
244 bfd *abfd;
245 const Elf_Internal_Versym *src;
246 Elf_External_Versym *dst;
248 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
251 /* Standard ELF hash function. Do not change this function; you will
252 cause invalid hash tables to be generated. */
254 unsigned long
255 bfd_elf_hash (namearg)
256 const char *namearg;
258 const unsigned char *name = (const unsigned char *) namearg;
259 unsigned long h = 0;
260 unsigned long g;
261 int ch;
263 while ((ch = *name++) != '\0')
265 h = (h << 4) + ch;
266 if ((g = (h & 0xf0000000)) != 0)
268 h ^= g >> 24;
269 /* The ELF ABI says `h &= ~g', but this is equivalent in
270 this case and on some machines one insn instead of two. */
271 h ^= g;
274 return h & 0xffffffff;
277 /* Read a specified number of bytes at a specified offset in an ELF
278 file, into a newly allocated buffer, and return a pointer to the
279 buffer. */
281 static char *
282 elf_read (abfd, offset, size)
283 bfd *abfd;
284 file_ptr offset;
285 bfd_size_type size;
287 char *buf;
289 if ((buf = bfd_alloc (abfd, size)) == NULL)
290 return NULL;
291 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
292 return NULL;
293 if (bfd_bread ((PTR) buf, size, abfd) != size)
295 if (bfd_get_error () != bfd_error_system_call)
296 bfd_set_error (bfd_error_file_truncated);
297 return NULL;
299 return buf;
302 bfd_boolean
303 bfd_elf_mkobject (abfd)
304 bfd *abfd;
306 /* This just does initialization. */
307 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
308 bfd_size_type amt = sizeof (struct elf_obj_tdata);
309 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
310 if (elf_tdata (abfd) == 0)
311 return FALSE;
312 /* Since everything is done at close time, do we need any
313 initialization? */
315 return TRUE;
318 bfd_boolean
319 bfd_elf_mkcorefile (abfd)
320 bfd *abfd;
322 /* I think this can be done just like an object file. */
323 return bfd_elf_mkobject (abfd);
326 char *
327 bfd_elf_get_str_section (abfd, shindex)
328 bfd *abfd;
329 unsigned int shindex;
331 Elf_Internal_Shdr **i_shdrp;
332 char *shstrtab = NULL;
333 file_ptr offset;
334 bfd_size_type shstrtabsize;
336 i_shdrp = elf_elfsections (abfd);
337 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
338 return 0;
340 shstrtab = (char *) i_shdrp[shindex]->contents;
341 if (shstrtab == NULL)
343 /* No cached one, attempt to read, and cache what we read. */
344 offset = i_shdrp[shindex]->sh_offset;
345 shstrtabsize = i_shdrp[shindex]->sh_size;
346 shstrtab = elf_read (abfd, offset, shstrtabsize);
347 i_shdrp[shindex]->contents = (PTR) shstrtab;
349 return shstrtab;
352 char *
353 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
354 bfd *abfd;
355 unsigned int shindex;
356 unsigned int strindex;
358 Elf_Internal_Shdr *hdr;
360 if (strindex == 0)
361 return "";
363 hdr = elf_elfsections (abfd)[shindex];
365 if (hdr->contents == NULL
366 && bfd_elf_get_str_section (abfd, shindex) == NULL)
367 return NULL;
369 if (strindex >= hdr->sh_size)
371 (*_bfd_error_handler)
372 (_("%s: invalid string offset %u >= %lu for section `%s'"),
373 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
374 ((shindex == elf_elfheader(abfd)->e_shstrndx
375 && strindex == hdr->sh_name)
376 ? ".shstrtab"
377 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
378 return "";
381 return ((char *) hdr->contents) + strindex;
384 /* Read and convert symbols to internal format.
385 SYMCOUNT specifies the number of symbols to read, starting from
386 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
387 are non-NULL, they are used to store the internal symbols, external
388 symbols, and symbol section index extensions, respectively. */
390 Elf_Internal_Sym *
391 bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,
392 intsym_buf, extsym_buf, extshndx_buf)
393 bfd *ibfd;
394 Elf_Internal_Shdr *symtab_hdr;
395 size_t symcount;
396 size_t symoffset;
397 Elf_Internal_Sym *intsym_buf;
398 PTR extsym_buf;
399 Elf_External_Sym_Shndx *extshndx_buf;
401 Elf_Internal_Shdr *shndx_hdr;
402 PTR alloc_ext;
403 const bfd_byte *esym;
404 Elf_External_Sym_Shndx *alloc_extshndx;
405 Elf_External_Sym_Shndx *shndx;
406 Elf_Internal_Sym *isym;
407 Elf_Internal_Sym *isymend;
408 struct elf_backend_data *bed;
409 size_t extsym_size;
410 bfd_size_type amt;
411 file_ptr pos;
413 if (symcount == 0)
414 return intsym_buf;
416 /* Normal syms might have section extension entries. */
417 shndx_hdr = NULL;
418 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
419 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
421 /* Read the symbols. */
422 alloc_ext = NULL;
423 alloc_extshndx = NULL;
424 bed = get_elf_backend_data (ibfd);
425 extsym_size = bed->s->sizeof_sym;
426 amt = symcount * extsym_size;
427 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
428 if (extsym_buf == NULL)
430 alloc_ext = bfd_malloc (amt);
431 extsym_buf = alloc_ext;
433 if (extsym_buf == NULL
434 || bfd_seek (ibfd, pos, SEEK_SET) != 0
435 || bfd_bread (extsym_buf, amt, ibfd) != amt)
437 intsym_buf = NULL;
438 goto out;
441 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
442 extshndx_buf = NULL;
443 else
445 amt = symcount * sizeof (Elf_External_Sym_Shndx);
446 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
447 if (extshndx_buf == NULL)
449 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
450 extshndx_buf = alloc_extshndx;
452 if (extshndx_buf == NULL
453 || bfd_seek (ibfd, pos, SEEK_SET) != 0
454 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
456 intsym_buf = NULL;
457 goto out;
461 if (intsym_buf == NULL)
463 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
464 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);
465 if (intsym_buf == NULL)
466 goto out;
469 /* Convert the symbols to internal form. */
470 isymend = intsym_buf + symcount;
471 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
472 isym < isymend;
473 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
474 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);
476 out:
477 if (alloc_ext != NULL)
478 free (alloc_ext);
479 if (alloc_extshndx != NULL)
480 free (alloc_extshndx);
482 return intsym_buf;
485 /* Look up a symbol name. */
486 const char *
487 bfd_elf_local_sym_name (abfd, isym)
488 bfd *abfd;
489 Elf_Internal_Sym *isym;
491 unsigned int iname = isym->st_name;
492 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
493 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
495 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
496 shindex = elf_elfheader (abfd)->e_shstrndx;
499 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
502 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
503 sections. The first element is the flags, the rest are section
504 pointers. */
506 typedef union elf_internal_group {
507 Elf_Internal_Shdr *shdr;
508 unsigned int flags;
509 } Elf_Internal_Group;
511 /* Return the name of the group signature symbol. Why isn't the
512 signature just a string? */
514 static const char *
515 group_signature (abfd, ghdr)
516 bfd *abfd;
517 Elf_Internal_Shdr *ghdr;
519 Elf_Internal_Shdr *hdr;
520 unsigned char esym[sizeof (Elf64_External_Sym)];
521 Elf_External_Sym_Shndx eshndx;
522 Elf_Internal_Sym isym;
524 /* First we need to ensure the symbol table is available. */
525 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
526 return NULL;
528 /* Go read the symbol. */
529 hdr = &elf_tdata (abfd)->symtab_hdr;
530 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
531 &isym, esym, &eshndx) == NULL)
532 return NULL;
534 return bfd_elf_local_sym_name (abfd, &isym);
537 /* Set next_in_group list pointer, and group name for NEWSECT. */
539 static bfd_boolean
540 setup_group (abfd, hdr, newsect)
541 bfd *abfd;
542 Elf_Internal_Shdr *hdr;
543 asection *newsect;
545 unsigned int num_group = elf_tdata (abfd)->num_group;
547 /* If num_group is zero, read in all SHT_GROUP sections. The count
548 is set to -1 if there are no SHT_GROUP sections. */
549 if (num_group == 0)
551 unsigned int i, shnum;
553 /* First count the number of groups. If we have a SHT_GROUP
554 section with just a flag word (ie. sh_size is 4), ignore it. */
555 shnum = elf_numsections (abfd);
556 num_group = 0;
557 for (i = 0; i < shnum; i++)
559 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
560 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
561 num_group += 1;
564 if (num_group == 0)
565 num_group = (unsigned) -1;
566 elf_tdata (abfd)->num_group = num_group;
568 if (num_group > 0)
570 /* We keep a list of elf section headers for group sections,
571 so we can find them quickly. */
572 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
573 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
574 if (elf_tdata (abfd)->group_sect_ptr == NULL)
575 return FALSE;
577 num_group = 0;
578 for (i = 0; i < shnum; i++)
580 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
581 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
583 unsigned char *src;
584 Elf_Internal_Group *dest;
586 /* Add to list of sections. */
587 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
588 num_group += 1;
590 /* Read the raw contents. */
591 BFD_ASSERT (sizeof (*dest) >= 4);
592 amt = shdr->sh_size * sizeof (*dest) / 4;
593 shdr->contents = bfd_alloc (abfd, amt);
594 if (shdr->contents == NULL
595 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
596 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
597 != shdr->sh_size))
598 return FALSE;
600 /* Translate raw contents, a flag word followed by an
601 array of elf section indices all in target byte order,
602 to the flag word followed by an array of elf section
603 pointers. */
604 src = shdr->contents + shdr->sh_size;
605 dest = (Elf_Internal_Group *) (shdr->contents + amt);
606 while (1)
608 unsigned int idx;
610 src -= 4;
611 --dest;
612 idx = H_GET_32 (abfd, src);
613 if (src == shdr->contents)
615 dest->flags = idx;
616 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
617 shdr->bfd_section->flags
618 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
619 break;
621 if (idx >= shnum)
623 ((*_bfd_error_handler)
624 (_("%s: invalid SHT_GROUP entry"),
625 bfd_archive_filename (abfd)));
626 idx = 0;
628 dest->shdr = elf_elfsections (abfd)[idx];
635 if (num_group != (unsigned) -1)
637 unsigned int i;
639 for (i = 0; i < num_group; i++)
641 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
642 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
643 unsigned int n_elt = shdr->sh_size / 4;
645 /* Look through this group's sections to see if current
646 section is a member. */
647 while (--n_elt != 0)
648 if ((++idx)->shdr == hdr)
650 asection *s = NULL;
652 /* We are a member of this group. Go looking through
653 other members to see if any others are linked via
654 next_in_group. */
655 idx = (Elf_Internal_Group *) shdr->contents;
656 n_elt = shdr->sh_size / 4;
657 while (--n_elt != 0)
658 if ((s = (++idx)->shdr->bfd_section) != NULL
659 && elf_next_in_group (s) != NULL)
660 break;
661 if (n_elt != 0)
663 /* Snarf the group name from other member, and
664 insert current section in circular list. */
665 elf_group_name (newsect) = elf_group_name (s);
666 elf_next_in_group (newsect) = elf_next_in_group (s);
667 elf_next_in_group (s) = newsect;
669 else
671 const char *gname;
673 gname = group_signature (abfd, shdr);
674 if (gname == NULL)
675 return FALSE;
676 elf_group_name (newsect) = gname;
678 /* Start a circular list with one element. */
679 elf_next_in_group (newsect) = newsect;
682 /* If the group section has been created, point to the
683 new member. */
684 if (shdr->bfd_section != NULL)
685 elf_next_in_group (shdr->bfd_section) = newsect;
687 i = num_group - 1;
688 break;
693 if (elf_group_name (newsect) == NULL)
695 (*_bfd_error_handler) (_("%s: no group info for section %s"),
696 bfd_archive_filename (abfd), newsect->name);
698 return TRUE;
701 bfd_boolean
702 bfd_elf_discard_group (abfd, group)
703 bfd *abfd ATTRIBUTE_UNUSED;
704 asection *group;
706 asection *first = elf_next_in_group (group);
707 asection *s = first;
709 while (s != NULL)
711 s->output_section = bfd_abs_section_ptr;
712 s = elf_next_in_group (s);
713 /* These lists are circular. */
714 if (s == first)
715 break;
717 return TRUE;
720 /* Make a BFD section from an ELF section. We store a pointer to the
721 BFD section in the bfd_section field of the header. */
723 bfd_boolean
724 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
725 bfd *abfd;
726 Elf_Internal_Shdr *hdr;
727 const char *name;
729 asection *newsect;
730 flagword flags;
731 struct elf_backend_data *bed;
733 if (hdr->bfd_section != NULL)
735 BFD_ASSERT (strcmp (name,
736 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
737 return TRUE;
740 newsect = bfd_make_section_anyway (abfd, name);
741 if (newsect == NULL)
742 return FALSE;
744 newsect->filepos = hdr->sh_offset;
746 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
747 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
748 || ! bfd_set_section_alignment (abfd, newsect,
749 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
750 return FALSE;
752 flags = SEC_NO_FLAGS;
753 if (hdr->sh_type != SHT_NOBITS)
754 flags |= SEC_HAS_CONTENTS;
755 if (hdr->sh_type == SHT_GROUP)
756 flags |= SEC_GROUP | SEC_EXCLUDE;
757 if ((hdr->sh_flags & SHF_ALLOC) != 0)
759 flags |= SEC_ALLOC;
760 if (hdr->sh_type != SHT_NOBITS)
761 flags |= SEC_LOAD;
763 if ((hdr->sh_flags & SHF_WRITE) == 0)
764 flags |= SEC_READONLY;
765 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
766 flags |= SEC_CODE;
767 else if ((flags & SEC_LOAD) != 0)
768 flags |= SEC_DATA;
769 if ((hdr->sh_flags & SHF_MERGE) != 0)
771 flags |= SEC_MERGE;
772 newsect->entsize = hdr->sh_entsize;
773 if ((hdr->sh_flags & SHF_STRINGS) != 0)
774 flags |= SEC_STRINGS;
776 if (hdr->sh_flags & SHF_GROUP)
777 if (!setup_group (abfd, hdr, newsect))
778 return FALSE;
779 if ((hdr->sh_flags & SHF_TLS) != 0)
780 flags |= SEC_THREAD_LOCAL;
782 /* The debugging sections appear to be recognized only by name, not
783 any sort of flag. */
785 static const char *debug_sec_names [] =
787 ".debug",
788 ".gnu.linkonce.wi.",
789 ".line",
790 ".stab"
792 int i;
794 for (i = ARRAY_SIZE (debug_sec_names); i--;)
795 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
796 break;
798 if (i >= 0)
799 flags |= SEC_DEBUGGING;
802 /* As a GNU extension, if the name begins with .gnu.linkonce, we
803 only link a single copy of the section. This is used to support
804 g++. g++ will emit each template expansion in its own section.
805 The symbols will be defined as weak, so that multiple definitions
806 are permitted. The GNU linker extension is to actually discard
807 all but one of the sections. */
808 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
809 && elf_next_in_group (newsect) == NULL)
810 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
812 bed = get_elf_backend_data (abfd);
813 if (bed->elf_backend_section_flags)
814 if (! bed->elf_backend_section_flags (&flags, hdr))
815 return FALSE;
817 if (! bfd_set_section_flags (abfd, newsect, flags))
818 return FALSE;
820 if ((flags & SEC_ALLOC) != 0)
822 Elf_Internal_Phdr *phdr;
823 unsigned int i;
825 /* Look through the phdrs to see if we need to adjust the lma.
826 If all the p_paddr fields are zero, we ignore them, since
827 some ELF linkers produce such output. */
828 phdr = elf_tdata (abfd)->phdr;
829 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
831 if (phdr->p_paddr != 0)
832 break;
834 if (i < elf_elfheader (abfd)->e_phnum)
836 phdr = elf_tdata (abfd)->phdr;
837 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
839 /* This section is part of this segment if its file
840 offset plus size lies within the segment's memory
841 span and, if the section is loaded, the extent of the
842 loaded data lies within the extent of the segment.
844 Note - we used to check the p_paddr field as well, and
845 refuse to set the LMA if it was 0. This is wrong
846 though, as a perfectly valid initialised segment can
847 have a p_paddr of zero. Some architectures, eg ARM,
848 place special significance on the address 0 and
849 executables need to be able to have a segment which
850 covers this address. */
851 if (phdr->p_type == PT_LOAD
852 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
853 && (hdr->sh_offset + hdr->sh_size
854 <= phdr->p_offset + phdr->p_memsz)
855 && ((flags & SEC_LOAD) == 0
856 || (hdr->sh_offset + hdr->sh_size
857 <= phdr->p_offset + phdr->p_filesz)))
859 if ((flags & SEC_LOAD) == 0)
860 newsect->lma = (phdr->p_paddr
861 + hdr->sh_addr - phdr->p_vaddr);
862 else
863 /* We used to use the same adjustment for SEC_LOAD
864 sections, but that doesn't work if the segment
865 is packed with code from multiple VMAs.
866 Instead we calculate the section LMA based on
867 the segment LMA. It is assumed that the
868 segment will contain sections with contiguous
869 LMAs, even if the VMAs are not. */
870 newsect->lma = (phdr->p_paddr
871 + hdr->sh_offset - phdr->p_offset);
873 /* With contiguous segments, we can't tell from file
874 offsets whether a section with zero size should
875 be placed at the end of one segment or the
876 beginning of the next. Decide based on vaddr. */
877 if (hdr->sh_addr >= phdr->p_vaddr
878 && (hdr->sh_addr + hdr->sh_size
879 <= phdr->p_vaddr + phdr->p_memsz))
880 break;
886 hdr->bfd_section = newsect;
887 elf_section_data (newsect)->this_hdr = *hdr;
889 return TRUE;
893 INTERNAL_FUNCTION
894 bfd_elf_find_section
896 SYNOPSIS
897 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
899 DESCRIPTION
900 Helper functions for GDB to locate the string tables.
901 Since BFD hides string tables from callers, GDB needs to use an
902 internal hook to find them. Sun's .stabstr, in particular,
903 isn't even pointed to by the .stab section, so ordinary
904 mechanisms wouldn't work to find it, even if we had some.
907 struct elf_internal_shdr *
908 bfd_elf_find_section (abfd, name)
909 bfd *abfd;
910 char *name;
912 Elf_Internal_Shdr **i_shdrp;
913 char *shstrtab;
914 unsigned int max;
915 unsigned int i;
917 i_shdrp = elf_elfsections (abfd);
918 if (i_shdrp != NULL)
920 shstrtab = bfd_elf_get_str_section (abfd,
921 elf_elfheader (abfd)->e_shstrndx);
922 if (shstrtab != NULL)
924 max = elf_numsections (abfd);
925 for (i = 1; i < max; i++)
926 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
927 return i_shdrp[i];
930 return 0;
933 const char *const bfd_elf_section_type_names[] = {
934 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
935 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
936 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
939 /* ELF relocs are against symbols. If we are producing relocateable
940 output, and the reloc is against an external symbol, and nothing
941 has given us any additional addend, the resulting reloc will also
942 be against the same symbol. In such a case, we don't want to
943 change anything about the way the reloc is handled, since it will
944 all be done at final link time. Rather than put special case code
945 into bfd_perform_relocation, all the reloc types use this howto
946 function. It just short circuits the reloc if producing
947 relocateable output against an external symbol. */
949 bfd_reloc_status_type
950 bfd_elf_generic_reloc (abfd,
951 reloc_entry,
952 symbol,
953 data,
954 input_section,
955 output_bfd,
956 error_message)
957 bfd *abfd ATTRIBUTE_UNUSED;
958 arelent *reloc_entry;
959 asymbol *symbol;
960 PTR data ATTRIBUTE_UNUSED;
961 asection *input_section;
962 bfd *output_bfd;
963 char **error_message ATTRIBUTE_UNUSED;
965 if (output_bfd != (bfd *) NULL
966 && (symbol->flags & BSF_SECTION_SYM) == 0
967 && (! reloc_entry->howto->partial_inplace
968 || reloc_entry->addend == 0))
970 reloc_entry->address += input_section->output_offset;
971 return bfd_reloc_ok;
974 return bfd_reloc_continue;
977 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
979 static void
980 merge_sections_remove_hook (abfd, sec)
981 bfd *abfd ATTRIBUTE_UNUSED;
982 asection *sec;
984 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
985 sec->sec_info_type = ELF_INFO_TYPE_NONE;
988 /* Finish SHF_MERGE section merging. */
990 bfd_boolean
991 _bfd_elf_merge_sections (abfd, info)
992 bfd *abfd;
993 struct bfd_link_info *info;
995 if (!is_elf_hash_table (info))
996 return FALSE;
997 if (elf_hash_table (info)->merge_info)
998 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
999 merge_sections_remove_hook);
1000 return TRUE;
1003 void
1004 _bfd_elf_link_just_syms (sec, info)
1005 asection *sec;
1006 struct bfd_link_info *info;
1008 sec->output_section = bfd_abs_section_ptr;
1009 sec->output_offset = sec->vma;
1010 if (!is_elf_hash_table (info))
1011 return;
1013 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
1016 /* Copy the program header and other data from one object module to
1017 another. */
1019 bfd_boolean
1020 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
1021 bfd *ibfd;
1022 bfd *obfd;
1024 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1025 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1026 return TRUE;
1028 BFD_ASSERT (!elf_flags_init (obfd)
1029 || (elf_elfheader (obfd)->e_flags
1030 == elf_elfheader (ibfd)->e_flags));
1032 elf_gp (obfd) = elf_gp (ibfd);
1033 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1034 elf_flags_init (obfd) = TRUE;
1035 return TRUE;
1038 /* Print out the program headers. */
1040 bfd_boolean
1041 _bfd_elf_print_private_bfd_data (abfd, farg)
1042 bfd *abfd;
1043 PTR farg;
1045 FILE *f = (FILE *) farg;
1046 Elf_Internal_Phdr *p;
1047 asection *s;
1048 bfd_byte *dynbuf = NULL;
1050 p = elf_tdata (abfd)->phdr;
1051 if (p != NULL)
1053 unsigned int i, c;
1055 fprintf (f, _("\nProgram Header:\n"));
1056 c = elf_elfheader (abfd)->e_phnum;
1057 for (i = 0; i < c; i++, p++)
1059 const char *pt;
1060 char buf[20];
1062 switch (p->p_type)
1064 case PT_NULL: pt = "NULL"; break;
1065 case PT_LOAD: pt = "LOAD"; break;
1066 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1067 case PT_INTERP: pt = "INTERP"; break;
1068 case PT_NOTE: pt = "NOTE"; break;
1069 case PT_SHLIB: pt = "SHLIB"; break;
1070 case PT_PHDR: pt = "PHDR"; break;
1071 case PT_TLS: pt = "TLS"; break;
1072 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1073 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1075 fprintf (f, "%8s off 0x", pt);
1076 bfd_fprintf_vma (abfd, f, p->p_offset);
1077 fprintf (f, " vaddr 0x");
1078 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1079 fprintf (f, " paddr 0x");
1080 bfd_fprintf_vma (abfd, f, p->p_paddr);
1081 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1082 fprintf (f, " filesz 0x");
1083 bfd_fprintf_vma (abfd, f, p->p_filesz);
1084 fprintf (f, " memsz 0x");
1085 bfd_fprintf_vma (abfd, f, p->p_memsz);
1086 fprintf (f, " flags %c%c%c",
1087 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1088 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1089 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1090 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1091 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1092 fprintf (f, "\n");
1096 s = bfd_get_section_by_name (abfd, ".dynamic");
1097 if (s != NULL)
1099 int elfsec;
1100 unsigned long shlink;
1101 bfd_byte *extdyn, *extdynend;
1102 size_t extdynsize;
1103 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1105 fprintf (f, _("\nDynamic Section:\n"));
1107 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1108 if (dynbuf == NULL)
1109 goto error_return;
1110 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1111 s->_raw_size))
1112 goto error_return;
1114 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1115 if (elfsec == -1)
1116 goto error_return;
1117 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1119 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1120 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1122 extdyn = dynbuf;
1123 extdynend = extdyn + s->_raw_size;
1124 for (; extdyn < extdynend; extdyn += extdynsize)
1126 Elf_Internal_Dyn dyn;
1127 const char *name;
1128 char ab[20];
1129 bfd_boolean stringp;
1131 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1133 if (dyn.d_tag == DT_NULL)
1134 break;
1136 stringp = FALSE;
1137 switch (dyn.d_tag)
1139 default:
1140 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1141 name = ab;
1142 break;
1144 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1145 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1146 case DT_PLTGOT: name = "PLTGOT"; break;
1147 case DT_HASH: name = "HASH"; break;
1148 case DT_STRTAB: name = "STRTAB"; break;
1149 case DT_SYMTAB: name = "SYMTAB"; break;
1150 case DT_RELA: name = "RELA"; break;
1151 case DT_RELASZ: name = "RELASZ"; break;
1152 case DT_RELAENT: name = "RELAENT"; break;
1153 case DT_STRSZ: name = "STRSZ"; break;
1154 case DT_SYMENT: name = "SYMENT"; break;
1155 case DT_INIT: name = "INIT"; break;
1156 case DT_FINI: name = "FINI"; break;
1157 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1158 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1159 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1160 case DT_REL: name = "REL"; break;
1161 case DT_RELSZ: name = "RELSZ"; break;
1162 case DT_RELENT: name = "RELENT"; break;
1163 case DT_PLTREL: name = "PLTREL"; break;
1164 case DT_DEBUG: name = "DEBUG"; break;
1165 case DT_TEXTREL: name = "TEXTREL"; break;
1166 case DT_JMPREL: name = "JMPREL"; break;
1167 case DT_BIND_NOW: name = "BIND_NOW"; break;
1168 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1169 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1170 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1171 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1172 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1173 case DT_FLAGS: name = "FLAGS"; break;
1174 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1175 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1176 case DT_CHECKSUM: name = "CHECKSUM"; break;
1177 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1178 case DT_MOVEENT: name = "MOVEENT"; break;
1179 case DT_MOVESZ: name = "MOVESZ"; break;
1180 case DT_FEATURE: name = "FEATURE"; break;
1181 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1182 case DT_SYMINSZ: name = "SYMINSZ"; break;
1183 case DT_SYMINENT: name = "SYMINENT"; break;
1184 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1185 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1186 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1187 case DT_PLTPAD: name = "PLTPAD"; break;
1188 case DT_MOVETAB: name = "MOVETAB"; break;
1189 case DT_SYMINFO: name = "SYMINFO"; break;
1190 case DT_RELACOUNT: name = "RELACOUNT"; break;
1191 case DT_RELCOUNT: name = "RELCOUNT"; break;
1192 case DT_FLAGS_1: name = "FLAGS_1"; break;
1193 case DT_VERSYM: name = "VERSYM"; break;
1194 case DT_VERDEF: name = "VERDEF"; break;
1195 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1196 case DT_VERNEED: name = "VERNEED"; break;
1197 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1198 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1199 case DT_USED: name = "USED"; break;
1200 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1203 fprintf (f, " %-11s ", name);
1204 if (! stringp)
1205 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1206 else
1208 const char *string;
1209 unsigned int tagv = dyn.d_un.d_val;
1211 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1212 if (string == NULL)
1213 goto error_return;
1214 fprintf (f, "%s", string);
1216 fprintf (f, "\n");
1219 free (dynbuf);
1220 dynbuf = NULL;
1223 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1224 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1226 if (! _bfd_elf_slurp_version_tables (abfd))
1227 return FALSE;
1230 if (elf_dynverdef (abfd) != 0)
1232 Elf_Internal_Verdef *t;
1234 fprintf (f, _("\nVersion definitions:\n"));
1235 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1237 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1238 t->vd_flags, t->vd_hash, t->vd_nodename);
1239 if (t->vd_auxptr->vda_nextptr != NULL)
1241 Elf_Internal_Verdaux *a;
1243 fprintf (f, "\t");
1244 for (a = t->vd_auxptr->vda_nextptr;
1245 a != NULL;
1246 a = a->vda_nextptr)
1247 fprintf (f, "%s ", a->vda_nodename);
1248 fprintf (f, "\n");
1253 if (elf_dynverref (abfd) != 0)
1255 Elf_Internal_Verneed *t;
1257 fprintf (f, _("\nVersion References:\n"));
1258 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1260 Elf_Internal_Vernaux *a;
1262 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1263 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1264 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1265 a->vna_flags, a->vna_other, a->vna_nodename);
1269 return TRUE;
1271 error_return:
1272 if (dynbuf != NULL)
1273 free (dynbuf);
1274 return FALSE;
1277 /* Display ELF-specific fields of a symbol. */
1279 void
1280 bfd_elf_print_symbol (abfd, filep, symbol, how)
1281 bfd *abfd;
1282 PTR filep;
1283 asymbol *symbol;
1284 bfd_print_symbol_type how;
1286 FILE *file = (FILE *) filep;
1287 switch (how)
1289 case bfd_print_symbol_name:
1290 fprintf (file, "%s", symbol->name);
1291 break;
1292 case bfd_print_symbol_more:
1293 fprintf (file, "elf ");
1294 bfd_fprintf_vma (abfd, file, symbol->value);
1295 fprintf (file, " %lx", (long) symbol->flags);
1296 break;
1297 case bfd_print_symbol_all:
1299 const char *section_name;
1300 const char *name = NULL;
1301 struct elf_backend_data *bed;
1302 unsigned char st_other;
1303 bfd_vma val;
1305 section_name = symbol->section ? symbol->section->name : "(*none*)";
1307 bed = get_elf_backend_data (abfd);
1308 if (bed->elf_backend_print_symbol_all)
1309 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1311 if (name == NULL)
1313 name = symbol->name;
1314 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1317 fprintf (file, " %s\t", section_name);
1318 /* Print the "other" value for a symbol. For common symbols,
1319 we've already printed the size; now print the alignment.
1320 For other symbols, we have no specified alignment, and
1321 we've printed the address; now print the size. */
1322 if (bfd_is_com_section (symbol->section))
1323 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1324 else
1325 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1326 bfd_fprintf_vma (abfd, file, val);
1328 /* If we have version information, print it. */
1329 if (elf_tdata (abfd)->dynversym_section != 0
1330 && (elf_tdata (abfd)->dynverdef_section != 0
1331 || elf_tdata (abfd)->dynverref_section != 0))
1333 unsigned int vernum;
1334 const char *version_string;
1336 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1338 if (vernum == 0)
1339 version_string = "";
1340 else if (vernum == 1)
1341 version_string = "Base";
1342 else if (vernum <= elf_tdata (abfd)->cverdefs)
1343 version_string =
1344 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1345 else
1347 Elf_Internal_Verneed *t;
1349 version_string = "";
1350 for (t = elf_tdata (abfd)->verref;
1351 t != NULL;
1352 t = t->vn_nextref)
1354 Elf_Internal_Vernaux *a;
1356 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1358 if (a->vna_other == vernum)
1360 version_string = a->vna_nodename;
1361 break;
1367 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1368 fprintf (file, " %-11s", version_string);
1369 else
1371 int i;
1373 fprintf (file, " (%s)", version_string);
1374 for (i = 10 - strlen (version_string); i > 0; --i)
1375 putc (' ', file);
1379 /* If the st_other field is not zero, print it. */
1380 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1382 switch (st_other)
1384 case 0: break;
1385 case STV_INTERNAL: fprintf (file, " .internal"); break;
1386 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1387 case STV_PROTECTED: fprintf (file, " .protected"); break;
1388 default:
1389 /* Some other non-defined flags are also present, so print
1390 everything hex. */
1391 fprintf (file, " 0x%02x", (unsigned int) st_other);
1394 fprintf (file, " %s", name);
1396 break;
1400 /* Create an entry in an ELF linker hash table. */
1402 struct bfd_hash_entry *
1403 _bfd_elf_link_hash_newfunc (entry, table, string)
1404 struct bfd_hash_entry *entry;
1405 struct bfd_hash_table *table;
1406 const char *string;
1408 /* Allocate the structure if it has not already been allocated by a
1409 subclass. */
1410 if (entry == NULL)
1412 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1413 if (entry == NULL)
1414 return entry;
1417 /* Call the allocation method of the superclass. */
1418 entry = _bfd_link_hash_newfunc (entry, table, string);
1419 if (entry != NULL)
1421 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1422 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1424 /* Set local fields. */
1425 ret->indx = -1;
1426 ret->dynindx = -1;
1427 ret->dynstr_index = 0;
1428 ret->elf_hash_value = 0;
1429 ret->weakdef = NULL;
1430 ret->linker_section_pointer = NULL;
1431 ret->verinfo.verdef = NULL;
1432 ret->vtable_entries_size = 0;
1433 ret->vtable_entries_used = NULL;
1434 ret->vtable_parent = NULL;
1435 ret->got = htab->init_refcount;
1436 ret->plt = htab->init_refcount;
1437 ret->size = 0;
1438 ret->type = STT_NOTYPE;
1439 ret->other = 0;
1440 /* Assume that we have been called by a non-ELF symbol reader.
1441 This flag is then reset by the code which reads an ELF input
1442 file. This ensures that a symbol created by a non-ELF symbol
1443 reader will have the flag set correctly. */
1444 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1447 return entry;
1450 /* Copy data from an indirect symbol to its direct symbol, hiding the
1451 old indirect symbol. Also used for copying flags to a weakdef. */
1453 void
1454 _bfd_elf_link_hash_copy_indirect (bed, dir, ind)
1455 struct elf_backend_data *bed;
1456 struct elf_link_hash_entry *dir, *ind;
1458 bfd_signed_vma tmp;
1459 bfd_signed_vma lowest_valid = bed->can_refcount;
1461 /* Copy down any references that we may have already seen to the
1462 symbol which just became indirect. */
1464 dir->elf_link_hash_flags |=
1465 (ind->elf_link_hash_flags
1466 & (ELF_LINK_HASH_REF_DYNAMIC
1467 | ELF_LINK_HASH_REF_REGULAR
1468 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1469 | ELF_LINK_NON_GOT_REF));
1471 if (ind->root.type != bfd_link_hash_indirect)
1472 return;
1474 /* Copy over the global and procedure linkage table refcount entries.
1475 These may have been already set up by a check_relocs routine. */
1476 tmp = dir->got.refcount;
1477 if (tmp < lowest_valid)
1479 dir->got.refcount = ind->got.refcount;
1480 ind->got.refcount = tmp;
1482 else
1483 BFD_ASSERT (ind->got.refcount < lowest_valid);
1485 tmp = dir->plt.refcount;
1486 if (tmp < lowest_valid)
1488 dir->plt.refcount = ind->plt.refcount;
1489 ind->plt.refcount = tmp;
1491 else
1492 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1494 if (dir->dynindx == -1)
1496 dir->dynindx = ind->dynindx;
1497 dir->dynstr_index = ind->dynstr_index;
1498 ind->dynindx = -1;
1499 ind->dynstr_index = 0;
1501 else
1502 BFD_ASSERT (ind->dynindx == -1);
1505 void
1506 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1507 struct bfd_link_info *info;
1508 struct elf_link_hash_entry *h;
1509 bfd_boolean force_local;
1511 h->plt = elf_hash_table (info)->init_offset;
1512 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1513 if (force_local)
1515 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1516 if (h->dynindx != -1)
1518 h->dynindx = -1;
1519 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1520 h->dynstr_index);
1525 /* Initialize an ELF linker hash table. */
1527 bfd_boolean
1528 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1529 struct elf_link_hash_table *table;
1530 bfd *abfd;
1531 struct bfd_hash_entry *(*newfunc)
1532 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
1533 const char *));
1535 bfd_boolean ret;
1537 table->dynamic_sections_created = FALSE;
1538 table->dynobj = NULL;
1539 /* Make sure can_refcount is extended to the width and signedness of
1540 init_refcount before we subtract one from it. */
1541 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1542 table->init_refcount.refcount -= 1;
1543 table->init_offset.offset = -(bfd_vma) 1;
1544 /* The first dynamic symbol is a dummy. */
1545 table->dynsymcount = 1;
1546 table->dynstr = NULL;
1547 table->bucketcount = 0;
1548 table->needed = NULL;
1549 table->hgot = NULL;
1550 table->stab_info = NULL;
1551 table->merge_info = NULL;
1552 memset (&table->eh_info, 0, sizeof (table->eh_info));
1553 table->dynlocal = NULL;
1554 table->runpath = NULL;
1555 table->tls_segment = NULL;
1556 table->loaded = NULL;
1558 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1559 table->root.type = bfd_link_elf_hash_table;
1561 return ret;
1564 /* Create an ELF linker hash table. */
1566 struct bfd_link_hash_table *
1567 _bfd_elf_link_hash_table_create (abfd)
1568 bfd *abfd;
1570 struct elf_link_hash_table *ret;
1571 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1573 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
1574 if (ret == (struct elf_link_hash_table *) NULL)
1575 return NULL;
1577 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1579 free (ret);
1580 return NULL;
1583 return &ret->root;
1586 /* This is a hook for the ELF emulation code in the generic linker to
1587 tell the backend linker what file name to use for the DT_NEEDED
1588 entry for a dynamic object. The generic linker passes name as an
1589 empty string to indicate that no DT_NEEDED entry should be made. */
1591 void
1592 bfd_elf_set_dt_needed_name (abfd, name)
1593 bfd *abfd;
1594 const char *name;
1596 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1597 && bfd_get_format (abfd) == bfd_object)
1598 elf_dt_name (abfd) = name;
1601 void
1602 bfd_elf_set_dt_needed_soname (abfd, name)
1603 bfd *abfd;
1604 const char *name;
1606 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1607 && bfd_get_format (abfd) == bfd_object)
1608 elf_dt_soname (abfd) = name;
1611 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1612 the linker ELF emulation code. */
1614 struct bfd_link_needed_list *
1615 bfd_elf_get_needed_list (abfd, info)
1616 bfd *abfd ATTRIBUTE_UNUSED;
1617 struct bfd_link_info *info;
1619 if (! is_elf_hash_table (info))
1620 return NULL;
1621 return elf_hash_table (info)->needed;
1624 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1625 hook for the linker ELF emulation code. */
1627 struct bfd_link_needed_list *
1628 bfd_elf_get_runpath_list (abfd, info)
1629 bfd *abfd ATTRIBUTE_UNUSED;
1630 struct bfd_link_info *info;
1632 if (! is_elf_hash_table (info))
1633 return NULL;
1634 return elf_hash_table (info)->runpath;
1637 /* Get the name actually used for a dynamic object for a link. This
1638 is the SONAME entry if there is one. Otherwise, it is the string
1639 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1641 const char *
1642 bfd_elf_get_dt_soname (abfd)
1643 bfd *abfd;
1645 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1646 && bfd_get_format (abfd) == bfd_object)
1647 return elf_dt_name (abfd);
1648 return NULL;
1651 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1652 the ELF linker emulation code. */
1654 bfd_boolean
1655 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1656 bfd *abfd;
1657 struct bfd_link_needed_list **pneeded;
1659 asection *s;
1660 bfd_byte *dynbuf = NULL;
1661 int elfsec;
1662 unsigned long shlink;
1663 bfd_byte *extdyn, *extdynend;
1664 size_t extdynsize;
1665 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1667 *pneeded = NULL;
1669 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1670 || bfd_get_format (abfd) != bfd_object)
1671 return TRUE;
1673 s = bfd_get_section_by_name (abfd, ".dynamic");
1674 if (s == NULL || s->_raw_size == 0)
1675 return TRUE;
1677 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1678 if (dynbuf == NULL)
1679 goto error_return;
1681 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1682 s->_raw_size))
1683 goto error_return;
1685 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1686 if (elfsec == -1)
1687 goto error_return;
1689 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1691 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1692 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1694 extdyn = dynbuf;
1695 extdynend = extdyn + s->_raw_size;
1696 for (; extdyn < extdynend; extdyn += extdynsize)
1698 Elf_Internal_Dyn dyn;
1700 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1702 if (dyn.d_tag == DT_NULL)
1703 break;
1705 if (dyn.d_tag == DT_NEEDED)
1707 const char *string;
1708 struct bfd_link_needed_list *l;
1709 unsigned int tagv = dyn.d_un.d_val;
1710 bfd_size_type amt;
1712 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1713 if (string == NULL)
1714 goto error_return;
1716 amt = sizeof *l;
1717 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1718 if (l == NULL)
1719 goto error_return;
1721 l->by = abfd;
1722 l->name = string;
1723 l->next = *pneeded;
1724 *pneeded = l;
1728 free (dynbuf);
1730 return TRUE;
1732 error_return:
1733 if (dynbuf != NULL)
1734 free (dynbuf);
1735 return FALSE;
1738 /* Allocate an ELF string table--force the first byte to be zero. */
1740 struct bfd_strtab_hash *
1741 _bfd_elf_stringtab_init ()
1743 struct bfd_strtab_hash *ret;
1745 ret = _bfd_stringtab_init ();
1746 if (ret != NULL)
1748 bfd_size_type loc;
1750 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1751 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1752 if (loc == (bfd_size_type) -1)
1754 _bfd_stringtab_free (ret);
1755 ret = NULL;
1758 return ret;
1761 /* ELF .o/exec file reading */
1763 /* Create a new bfd section from an ELF section header. */
1765 bfd_boolean
1766 bfd_section_from_shdr (abfd, shindex)
1767 bfd *abfd;
1768 unsigned int shindex;
1770 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1771 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1772 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1773 const char *name;
1775 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1777 switch (hdr->sh_type)
1779 case SHT_NULL:
1780 /* Inactive section. Throw it away. */
1781 return TRUE;
1783 case SHT_PROGBITS: /* Normal section with contents. */
1784 case SHT_NOBITS: /* .bss section. */
1785 case SHT_HASH: /* .hash section. */
1786 case SHT_NOTE: /* .note section. */
1787 case SHT_INIT_ARRAY: /* .init_array section. */
1788 case SHT_FINI_ARRAY: /* .fini_array section. */
1789 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1790 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1792 case SHT_DYNAMIC: /* Dynamic linking information. */
1793 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1794 return FALSE;
1795 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1797 Elf_Internal_Shdr *dynsymhdr;
1799 /* The shared libraries distributed with hpux11 have a bogus
1800 sh_link field for the ".dynamic" section. Find the
1801 string table for the ".dynsym" section instead. */
1802 if (elf_dynsymtab (abfd) != 0)
1804 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1805 hdr->sh_link = dynsymhdr->sh_link;
1807 else
1809 unsigned int i, num_sec;
1811 num_sec = elf_numsections (abfd);
1812 for (i = 1; i < num_sec; i++)
1814 dynsymhdr = elf_elfsections (abfd)[i];
1815 if (dynsymhdr->sh_type == SHT_DYNSYM)
1817 hdr->sh_link = dynsymhdr->sh_link;
1818 break;
1823 break;
1825 case SHT_SYMTAB: /* A symbol table */
1826 if (elf_onesymtab (abfd) == shindex)
1827 return TRUE;
1829 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1830 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1831 elf_onesymtab (abfd) = shindex;
1832 elf_tdata (abfd)->symtab_hdr = *hdr;
1833 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1834 abfd->flags |= HAS_SYMS;
1836 /* Sometimes a shared object will map in the symbol table. If
1837 SHF_ALLOC is set, and this is a shared object, then we also
1838 treat this section as a BFD section. We can not base the
1839 decision purely on SHF_ALLOC, because that flag is sometimes
1840 set in a relocateable object file, which would confuse the
1841 linker. */
1842 if ((hdr->sh_flags & SHF_ALLOC) != 0
1843 && (abfd->flags & DYNAMIC) != 0
1844 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1845 return FALSE;
1847 return TRUE;
1849 case SHT_DYNSYM: /* A dynamic symbol table */
1850 if (elf_dynsymtab (abfd) == shindex)
1851 return TRUE;
1853 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1854 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1855 elf_dynsymtab (abfd) = shindex;
1856 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1857 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1858 abfd->flags |= HAS_SYMS;
1860 /* Besides being a symbol table, we also treat this as a regular
1861 section, so that objcopy can handle it. */
1862 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1864 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1865 if (elf_symtab_shndx (abfd) == shindex)
1866 return TRUE;
1868 /* Get the associated symbol table. */
1869 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1870 || hdr->sh_link != elf_onesymtab (abfd))
1871 return FALSE;
1873 elf_symtab_shndx (abfd) = shindex;
1874 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1875 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1876 return TRUE;
1878 case SHT_STRTAB: /* A string table */
1879 if (hdr->bfd_section != NULL)
1880 return TRUE;
1881 if (ehdr->e_shstrndx == shindex)
1883 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1884 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1885 return TRUE;
1888 unsigned int i, num_sec;
1890 num_sec = elf_numsections (abfd);
1891 for (i = 1; i < num_sec; i++)
1893 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1894 if (hdr2->sh_link == shindex)
1896 if (! bfd_section_from_shdr (abfd, i))
1897 return FALSE;
1898 if (elf_onesymtab (abfd) == i)
1900 elf_tdata (abfd)->strtab_hdr = *hdr;
1901 elf_elfsections (abfd)[shindex] =
1902 &elf_tdata (abfd)->strtab_hdr;
1903 return TRUE;
1905 if (elf_dynsymtab (abfd) == i)
1907 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1908 elf_elfsections (abfd)[shindex] = hdr =
1909 &elf_tdata (abfd)->dynstrtab_hdr;
1910 /* We also treat this as a regular section, so
1911 that objcopy can handle it. */
1912 break;
1914 #if 0 /* Not handling other string tables specially right now. */
1915 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1916 /* We have a strtab for some random other section. */
1917 newsect = (asection *) hdr2->bfd_section;
1918 if (!newsect)
1919 break;
1920 hdr->bfd_section = newsect;
1921 hdr2 = &elf_section_data (newsect)->str_hdr;
1922 *hdr2 = *hdr;
1923 elf_elfsections (abfd)[shindex] = hdr2;
1924 #endif
1929 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1931 case SHT_REL:
1932 case SHT_RELA:
1933 /* *These* do a lot of work -- but build no sections! */
1935 asection *target_sect;
1936 Elf_Internal_Shdr *hdr2;
1937 unsigned int num_sec = elf_numsections (abfd);
1939 /* Check for a bogus link to avoid crashing. */
1940 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1941 || hdr->sh_link >= num_sec)
1943 ((*_bfd_error_handler)
1944 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1945 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1946 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1949 /* For some incomprehensible reason Oracle distributes
1950 libraries for Solaris in which some of the objects have
1951 bogus sh_link fields. It would be nice if we could just
1952 reject them, but, unfortunately, some people need to use
1953 them. We scan through the section headers; if we find only
1954 one suitable symbol table, we clobber the sh_link to point
1955 to it. I hope this doesn't break anything. */
1956 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1957 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1959 unsigned int scan;
1960 int found;
1962 found = 0;
1963 for (scan = 1; scan < num_sec; scan++)
1965 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1966 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1968 if (found != 0)
1970 found = 0;
1971 break;
1973 found = scan;
1976 if (found != 0)
1977 hdr->sh_link = found;
1980 /* Get the symbol table. */
1981 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1982 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1983 return FALSE;
1985 /* If this reloc section does not use the main symbol table we
1986 don't treat it as a reloc section. BFD can't adequately
1987 represent such a section, so at least for now, we don't
1988 try. We just present it as a normal section. We also
1989 can't use it as a reloc section if it points to the null
1990 section. */
1991 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1992 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1994 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1995 return FALSE;
1996 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1997 if (target_sect == NULL)
1998 return FALSE;
2000 if ((target_sect->flags & SEC_RELOC) == 0
2001 || target_sect->reloc_count == 0)
2002 hdr2 = &elf_section_data (target_sect)->rel_hdr;
2003 else
2005 bfd_size_type amt;
2006 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
2007 amt = sizeof (*hdr2);
2008 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2009 elf_section_data (target_sect)->rel_hdr2 = hdr2;
2011 *hdr2 = *hdr;
2012 elf_elfsections (abfd)[shindex] = hdr2;
2013 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2014 target_sect->flags |= SEC_RELOC;
2015 target_sect->relocation = NULL;
2016 target_sect->rel_filepos = hdr->sh_offset;
2017 /* In the section to which the relocations apply, mark whether
2018 its relocations are of the REL or RELA variety. */
2019 if (hdr->sh_size != 0)
2020 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
2021 abfd->flags |= HAS_RELOC;
2022 return TRUE;
2024 break;
2026 case SHT_GNU_verdef:
2027 elf_dynverdef (abfd) = shindex;
2028 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2029 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2030 break;
2032 case SHT_GNU_versym:
2033 elf_dynversym (abfd) = shindex;
2034 elf_tdata (abfd)->dynversym_hdr = *hdr;
2035 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2036 break;
2038 case SHT_GNU_verneed:
2039 elf_dynverref (abfd) = shindex;
2040 elf_tdata (abfd)->dynverref_hdr = *hdr;
2041 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2042 break;
2044 case SHT_SHLIB:
2045 return TRUE;
2047 case SHT_GROUP:
2048 /* We need a BFD section for objcopy and relocatable linking,
2049 and it's handy to have the signature available as the section
2050 name. */
2051 name = group_signature (abfd, hdr);
2052 if (name == NULL)
2053 return FALSE;
2054 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2055 return FALSE;
2056 if (hdr->contents != NULL)
2058 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2059 unsigned int n_elt = hdr->sh_size / 4;
2060 asection *s;
2062 if (idx->flags & GRP_COMDAT)
2063 hdr->bfd_section->flags
2064 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2066 while (--n_elt != 0)
2067 if ((s = (++idx)->shdr->bfd_section) != NULL
2068 && elf_next_in_group (s) != NULL)
2070 elf_next_in_group (hdr->bfd_section) = s;
2071 break;
2074 break;
2076 default:
2077 /* Check for any processor-specific section types. */
2079 if (bed->elf_backend_section_from_shdr)
2080 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2082 break;
2085 return TRUE;
2088 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2089 Return SEC for sections that have no elf section, and NULL on error. */
2091 asection *
2092 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
2093 bfd *abfd;
2094 struct sym_sec_cache *cache;
2095 asection *sec;
2096 unsigned long r_symndx;
2098 Elf_Internal_Shdr *symtab_hdr;
2099 unsigned char esym[sizeof (Elf64_External_Sym)];
2100 Elf_External_Sym_Shndx eshndx;
2101 Elf_Internal_Sym isym;
2102 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2104 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2105 return cache->sec[ent];
2107 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2108 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2109 &isym, esym, &eshndx) == NULL)
2110 return NULL;
2112 if (cache->abfd != abfd)
2114 memset (cache->indx, -1, sizeof (cache->indx));
2115 cache->abfd = abfd;
2117 cache->indx[ent] = r_symndx;
2118 cache->sec[ent] = sec;
2119 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
2121 asection *s;
2122 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2123 if (s != NULL)
2124 cache->sec[ent] = s;
2126 return cache->sec[ent];
2129 /* Given an ELF section number, retrieve the corresponding BFD
2130 section. */
2132 asection *
2133 bfd_section_from_elf_index (abfd, index)
2134 bfd *abfd;
2135 unsigned int index;
2137 if (index >= elf_numsections (abfd))
2138 return NULL;
2139 return elf_elfsections (abfd)[index]->bfd_section;
2142 bfd_boolean
2143 _bfd_elf_new_section_hook (abfd, sec)
2144 bfd *abfd;
2145 asection *sec;
2147 struct bfd_elf_section_data *sdata;
2149 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2150 if (sdata == NULL)
2152 bfd_size_type amt = sizeof (*sdata);
2153 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
2154 if (sdata == NULL)
2155 return FALSE;
2156 sec->used_by_bfd = (PTR) sdata;
2159 /* Indicate whether or not this section should use RELA relocations. */
2160 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2162 return TRUE;
2165 /* Create a new bfd section from an ELF program header.
2167 Since program segments have no names, we generate a synthetic name
2168 of the form segment<NUM>, where NUM is generally the index in the
2169 program header table. For segments that are split (see below) we
2170 generate the names segment<NUM>a and segment<NUM>b.
2172 Note that some program segments may have a file size that is different than
2173 (less than) the memory size. All this means is that at execution the
2174 system must allocate the amount of memory specified by the memory size,
2175 but only initialize it with the first "file size" bytes read from the
2176 file. This would occur for example, with program segments consisting
2177 of combined data+bss.
2179 To handle the above situation, this routine generates TWO bfd sections
2180 for the single program segment. The first has the length specified by
2181 the file size of the segment, and the second has the length specified
2182 by the difference between the two sizes. In effect, the segment is split
2183 into it's initialized and uninitialized parts.
2187 bfd_boolean
2188 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
2189 bfd *abfd;
2190 Elf_Internal_Phdr *hdr;
2191 int index;
2192 const char *typename;
2194 asection *newsect;
2195 char *name;
2196 char namebuf[64];
2197 size_t len;
2198 int split;
2200 split = ((hdr->p_memsz > 0)
2201 && (hdr->p_filesz > 0)
2202 && (hdr->p_memsz > hdr->p_filesz));
2203 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2204 len = strlen (namebuf) + 1;
2205 name = bfd_alloc (abfd, (bfd_size_type) len);
2206 if (!name)
2207 return FALSE;
2208 memcpy (name, namebuf, len);
2209 newsect = bfd_make_section (abfd, name);
2210 if (newsect == NULL)
2211 return FALSE;
2212 newsect->vma = hdr->p_vaddr;
2213 newsect->lma = hdr->p_paddr;
2214 newsect->_raw_size = hdr->p_filesz;
2215 newsect->filepos = hdr->p_offset;
2216 newsect->flags |= SEC_HAS_CONTENTS;
2217 if (hdr->p_type == PT_LOAD)
2219 newsect->flags |= SEC_ALLOC;
2220 newsect->flags |= SEC_LOAD;
2221 if (hdr->p_flags & PF_X)
2223 /* FIXME: all we known is that it has execute PERMISSION,
2224 may be data. */
2225 newsect->flags |= SEC_CODE;
2228 if (!(hdr->p_flags & PF_W))
2230 newsect->flags |= SEC_READONLY;
2233 if (split)
2235 sprintf (namebuf, "%s%db", typename, index);
2236 len = strlen (namebuf) + 1;
2237 name = bfd_alloc (abfd, (bfd_size_type) len);
2238 if (!name)
2239 return FALSE;
2240 memcpy (name, namebuf, len);
2241 newsect = bfd_make_section (abfd, name);
2242 if (newsect == NULL)
2243 return FALSE;
2244 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2245 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2246 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2247 if (hdr->p_type == PT_LOAD)
2249 newsect->flags |= SEC_ALLOC;
2250 if (hdr->p_flags & PF_X)
2251 newsect->flags |= SEC_CODE;
2253 if (!(hdr->p_flags & PF_W))
2254 newsect->flags |= SEC_READONLY;
2257 return TRUE;
2260 bfd_boolean
2261 bfd_section_from_phdr (abfd, hdr, index)
2262 bfd *abfd;
2263 Elf_Internal_Phdr *hdr;
2264 int index;
2266 struct elf_backend_data *bed;
2268 switch (hdr->p_type)
2270 case PT_NULL:
2271 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2273 case PT_LOAD:
2274 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2276 case PT_DYNAMIC:
2277 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2279 case PT_INTERP:
2280 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2282 case PT_NOTE:
2283 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2284 return FALSE;
2285 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2286 return FALSE;
2287 return TRUE;
2289 case PT_SHLIB:
2290 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2292 case PT_PHDR:
2293 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2295 case PT_GNU_EH_FRAME:
2296 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2297 "eh_frame_hdr");
2299 default:
2300 /* Check for any processor-specific program segment types.
2301 If no handler for them, default to making "segment" sections. */
2302 bed = get_elf_backend_data (abfd);
2303 if (bed->elf_backend_section_from_phdr)
2304 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2305 else
2306 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2310 /* Initialize REL_HDR, the section-header for new section, containing
2311 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2312 relocations; otherwise, we use REL relocations. */
2314 bfd_boolean
2315 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2316 bfd *abfd;
2317 Elf_Internal_Shdr *rel_hdr;
2318 asection *asect;
2319 bfd_boolean use_rela_p;
2321 char *name;
2322 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2323 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2325 name = bfd_alloc (abfd, amt);
2326 if (name == NULL)
2327 return FALSE;
2328 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2329 rel_hdr->sh_name =
2330 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2331 FALSE);
2332 if (rel_hdr->sh_name == (unsigned int) -1)
2333 return FALSE;
2334 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2335 rel_hdr->sh_entsize = (use_rela_p
2336 ? bed->s->sizeof_rela
2337 : bed->s->sizeof_rel);
2338 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2339 rel_hdr->sh_flags = 0;
2340 rel_hdr->sh_addr = 0;
2341 rel_hdr->sh_size = 0;
2342 rel_hdr->sh_offset = 0;
2344 return TRUE;
2347 /* Set up an ELF internal section header for a section. */
2349 static void
2350 elf_fake_sections (abfd, asect, failedptrarg)
2351 bfd *abfd;
2352 asection *asect;
2353 PTR failedptrarg;
2355 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2356 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2357 Elf_Internal_Shdr *this_hdr;
2359 if (*failedptr)
2361 /* We already failed; just get out of the bfd_map_over_sections
2362 loop. */
2363 return;
2366 this_hdr = &elf_section_data (asect)->this_hdr;
2368 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2369 asect->name, FALSE);
2370 if (this_hdr->sh_name == (unsigned int) -1)
2372 *failedptr = TRUE;
2373 return;
2376 this_hdr->sh_flags = 0;
2378 if ((asect->flags & SEC_ALLOC) != 0
2379 || asect->user_set_vma)
2380 this_hdr->sh_addr = asect->vma;
2381 else
2382 this_hdr->sh_addr = 0;
2384 this_hdr->sh_offset = 0;
2385 this_hdr->sh_size = asect->_raw_size;
2386 this_hdr->sh_link = 0;
2387 this_hdr->sh_addralign = 1 << asect->alignment_power;
2388 /* The sh_entsize and sh_info fields may have been set already by
2389 copy_private_section_data. */
2391 this_hdr->bfd_section = asect;
2392 this_hdr->contents = NULL;
2394 /* FIXME: This should not be based on section names. */
2395 if (strcmp (asect->name, ".dynstr") == 0)
2396 this_hdr->sh_type = SHT_STRTAB;
2397 else if (strcmp (asect->name, ".hash") == 0)
2399 this_hdr->sh_type = SHT_HASH;
2400 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2402 else if (strcmp (asect->name, ".dynsym") == 0)
2404 this_hdr->sh_type = SHT_DYNSYM;
2405 this_hdr->sh_entsize = bed->s->sizeof_sym;
2407 else if (strcmp (asect->name, ".dynamic") == 0)
2409 this_hdr->sh_type = SHT_DYNAMIC;
2410 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2412 else if (strncmp (asect->name, ".rela", 5) == 0
2413 && get_elf_backend_data (abfd)->may_use_rela_p)
2415 this_hdr->sh_type = SHT_RELA;
2416 this_hdr->sh_entsize = bed->s->sizeof_rela;
2418 else if (strncmp (asect->name, ".rel", 4) == 0
2419 && get_elf_backend_data (abfd)->may_use_rel_p)
2421 this_hdr->sh_type = SHT_REL;
2422 this_hdr->sh_entsize = bed->s->sizeof_rel;
2424 else if (strcmp (asect->name, ".init_array") == 0)
2425 this_hdr->sh_type = SHT_INIT_ARRAY;
2426 else if (strcmp (asect->name, ".fini_array") == 0)
2427 this_hdr->sh_type = SHT_FINI_ARRAY;
2428 else if (strcmp (asect->name, ".preinit_array") == 0)
2429 this_hdr->sh_type = SHT_PREINIT_ARRAY;
2430 else if (strncmp (asect->name, ".note", 5) == 0)
2431 this_hdr->sh_type = SHT_NOTE;
2432 else if (strncmp (asect->name, ".stab", 5) == 0
2433 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2434 this_hdr->sh_type = SHT_STRTAB;
2435 else if (strcmp (asect->name, ".gnu.version") == 0)
2437 this_hdr->sh_type = SHT_GNU_versym;
2438 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2440 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2442 this_hdr->sh_type = SHT_GNU_verdef;
2443 this_hdr->sh_entsize = 0;
2444 /* objcopy or strip will copy over sh_info, but may not set
2445 cverdefs. The linker will set cverdefs, but sh_info will be
2446 zero. */
2447 if (this_hdr->sh_info == 0)
2448 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2449 else
2450 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2451 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2453 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2455 this_hdr->sh_type = SHT_GNU_verneed;
2456 this_hdr->sh_entsize = 0;
2457 /* objcopy or strip will copy over sh_info, but may not set
2458 cverrefs. The linker will set cverrefs, but sh_info will be
2459 zero. */
2460 if (this_hdr->sh_info == 0)
2461 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2462 else
2463 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2464 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2466 else if ((asect->flags & SEC_GROUP) != 0)
2468 this_hdr->sh_type = SHT_GROUP;
2469 this_hdr->sh_entsize = 4;
2471 else if ((asect->flags & SEC_ALLOC) != 0
2472 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2473 || (asect->flags & SEC_NEVER_LOAD) != 0))
2474 this_hdr->sh_type = SHT_NOBITS;
2475 else
2476 this_hdr->sh_type = SHT_PROGBITS;
2478 if ((asect->flags & SEC_ALLOC) != 0)
2479 this_hdr->sh_flags |= SHF_ALLOC;
2480 if ((asect->flags & SEC_READONLY) == 0)
2481 this_hdr->sh_flags |= SHF_WRITE;
2482 if ((asect->flags & SEC_CODE) != 0)
2483 this_hdr->sh_flags |= SHF_EXECINSTR;
2484 if ((asect->flags & SEC_MERGE) != 0)
2486 this_hdr->sh_flags |= SHF_MERGE;
2487 this_hdr->sh_entsize = asect->entsize;
2488 if ((asect->flags & SEC_STRINGS) != 0)
2489 this_hdr->sh_flags |= SHF_STRINGS;
2491 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2492 this_hdr->sh_flags |= SHF_GROUP;
2493 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2495 this_hdr->sh_flags |= SHF_TLS;
2496 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2498 struct bfd_link_order *o;
2500 this_hdr->sh_size = 0;
2501 for (o = asect->link_order_head; o != NULL; o = o->next)
2502 if (this_hdr->sh_size < o->offset + o->size)
2503 this_hdr->sh_size = o->offset + o->size;
2504 if (this_hdr->sh_size)
2505 this_hdr->sh_type = SHT_NOBITS;
2509 /* Check for processor-specific section types. */
2510 if (bed->elf_backend_fake_sections
2511 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2512 *failedptr = TRUE;
2514 /* If the section has relocs, set up a section header for the
2515 SHT_REL[A] section. If two relocation sections are required for
2516 this section, it is up to the processor-specific back-end to
2517 create the other. */
2518 if ((asect->flags & SEC_RELOC) != 0
2519 && !_bfd_elf_init_reloc_shdr (abfd,
2520 &elf_section_data (asect)->rel_hdr,
2521 asect,
2522 asect->use_rela_p))
2523 *failedptr = TRUE;
2526 /* Fill in the contents of a SHT_GROUP section. */
2528 void
2529 bfd_elf_set_group_contents (abfd, sec, failedptrarg)
2530 bfd *abfd;
2531 asection *sec;
2532 PTR failedptrarg;
2534 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2535 unsigned long symindx;
2536 asection *elt, *first;
2537 unsigned char *loc;
2538 struct bfd_link_order *l;
2539 bfd_boolean gas;
2541 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2542 || *failedptr)
2543 return;
2545 symindx = 0;
2546 if (elf_group_id (sec) != NULL)
2547 symindx = elf_group_id (sec)->udata.i;
2549 if (symindx == 0)
2551 /* If called from the assembler, swap_out_syms will have set up
2552 elf_section_syms; If called for "ld -r", use target_index. */
2553 if (elf_section_syms (abfd) != NULL)
2554 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2555 else
2556 symindx = sec->target_index;
2558 elf_section_data (sec)->this_hdr.sh_info = symindx;
2560 /* The contents won't be allocated for "ld -r" or objcopy. */
2561 gas = TRUE;
2562 if (sec->contents == NULL)
2564 gas = FALSE;
2565 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2567 /* Arrange for the section to be written out. */
2568 elf_section_data (sec)->this_hdr.contents = sec->contents;
2569 if (sec->contents == NULL)
2571 *failedptr = TRUE;
2572 return;
2576 loc = sec->contents + sec->_raw_size;
2578 /* Get the pointer to the first section in the group that gas
2579 squirreled away here. objcopy arranges for this to be set to the
2580 start of the input section group. */
2581 first = elt = elf_next_in_group (sec);
2583 /* First element is a flag word. Rest of section is elf section
2584 indices for all the sections of the group. Write them backwards
2585 just to keep the group in the same order as given in .section
2586 directives, not that it matters. */
2587 while (elt != NULL)
2589 asection *s;
2590 unsigned int idx;
2592 loc -= 4;
2593 s = elt;
2594 if (!gas)
2595 s = s->output_section;
2596 idx = 0;
2597 if (s != NULL)
2598 idx = elf_section_data (s)->this_idx;
2599 H_PUT_32 (abfd, idx, loc);
2600 elt = elf_next_in_group (elt);
2601 if (elt == first)
2602 break;
2605 /* If this is a relocatable link, then the above did nothing because
2606 SEC is the output section. Look through the input sections
2607 instead. */
2608 for (l = sec->link_order_head; l != NULL; l = l->next)
2609 if (l->type == bfd_indirect_link_order
2610 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2613 loc -= 4;
2614 H_PUT_32 (abfd,
2615 elf_section_data (elt->output_section)->this_idx, loc);
2616 elt = elf_next_in_group (elt);
2617 /* During a relocatable link, the lists are circular. */
2619 while (elt != elf_next_in_group (l->u.indirect.section));
2621 /* With ld -r, merging SHT_GROUP sections results in wasted space
2622 due to allowing for the flag word on each input. We may well
2623 duplicate entries too. */
2624 while ((loc -= 4) > sec->contents)
2625 H_PUT_32 (abfd, 0, loc);
2627 if (loc != sec->contents)
2628 abort ();
2630 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2633 /* Assign all ELF section numbers. The dummy first section is handled here
2634 too. The link/info pointers for the standard section types are filled
2635 in here too, while we're at it. */
2637 static bfd_boolean
2638 assign_section_numbers (abfd)
2639 bfd *abfd;
2641 struct elf_obj_tdata *t = elf_tdata (abfd);
2642 asection *sec;
2643 unsigned int section_number, secn;
2644 Elf_Internal_Shdr **i_shdrp;
2645 bfd_size_type amt;
2647 section_number = 1;
2649 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2651 for (sec = abfd->sections; sec; sec = sec->next)
2653 struct bfd_elf_section_data *d = elf_section_data (sec);
2655 if (section_number == SHN_LORESERVE)
2656 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2657 d->this_idx = section_number++;
2658 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2659 if ((sec->flags & SEC_RELOC) == 0)
2660 d->rel_idx = 0;
2661 else
2663 if (section_number == SHN_LORESERVE)
2664 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2665 d->rel_idx = section_number++;
2666 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2669 if (d->rel_hdr2)
2671 if (section_number == SHN_LORESERVE)
2672 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2673 d->rel_idx2 = section_number++;
2674 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2676 else
2677 d->rel_idx2 = 0;
2680 if (section_number == SHN_LORESERVE)
2681 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2682 t->shstrtab_section = section_number++;
2683 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2684 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2686 if (bfd_get_symcount (abfd) > 0)
2688 if (section_number == SHN_LORESERVE)
2689 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2690 t->symtab_section = section_number++;
2691 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2692 if (section_number > SHN_LORESERVE - 2)
2694 if (section_number == SHN_LORESERVE)
2695 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2696 t->symtab_shndx_section = section_number++;
2697 t->symtab_shndx_hdr.sh_name
2698 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2699 ".symtab_shndx", FALSE);
2700 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2701 return FALSE;
2703 if (section_number == SHN_LORESERVE)
2704 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2705 t->strtab_section = section_number++;
2706 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2709 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2710 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2712 elf_numsections (abfd) = section_number;
2713 elf_elfheader (abfd)->e_shnum = section_number;
2714 if (section_number > SHN_LORESERVE)
2715 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2717 /* Set up the list of section header pointers, in agreement with the
2718 indices. */
2719 amt = section_number * sizeof (Elf_Internal_Shdr *);
2720 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
2721 if (i_shdrp == NULL)
2722 return FALSE;
2724 amt = sizeof (Elf_Internal_Shdr);
2725 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
2726 if (i_shdrp[0] == NULL)
2728 bfd_release (abfd, i_shdrp);
2729 return FALSE;
2732 elf_elfsections (abfd) = i_shdrp;
2734 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2735 if (bfd_get_symcount (abfd) > 0)
2737 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2738 if (elf_numsections (abfd) > SHN_LORESERVE)
2740 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2741 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2743 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2744 t->symtab_hdr.sh_link = t->strtab_section;
2746 for (sec = abfd->sections; sec; sec = sec->next)
2748 struct bfd_elf_section_data *d = elf_section_data (sec);
2749 asection *s;
2750 const char *name;
2752 i_shdrp[d->this_idx] = &d->this_hdr;
2753 if (d->rel_idx != 0)
2754 i_shdrp[d->rel_idx] = &d->rel_hdr;
2755 if (d->rel_idx2 != 0)
2756 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2758 /* Fill in the sh_link and sh_info fields while we're at it. */
2760 /* sh_link of a reloc section is the section index of the symbol
2761 table. sh_info is the section index of the section to which
2762 the relocation entries apply. */
2763 if (d->rel_idx != 0)
2765 d->rel_hdr.sh_link = t->symtab_section;
2766 d->rel_hdr.sh_info = d->this_idx;
2768 if (d->rel_idx2 != 0)
2770 d->rel_hdr2->sh_link = t->symtab_section;
2771 d->rel_hdr2->sh_info = d->this_idx;
2774 switch (d->this_hdr.sh_type)
2776 case SHT_REL:
2777 case SHT_RELA:
2778 /* A reloc section which we are treating as a normal BFD
2779 section. sh_link is the section index of the symbol
2780 table. sh_info is the section index of the section to
2781 which the relocation entries apply. We assume that an
2782 allocated reloc section uses the dynamic symbol table.
2783 FIXME: How can we be sure? */
2784 s = bfd_get_section_by_name (abfd, ".dynsym");
2785 if (s != NULL)
2786 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2788 /* We look up the section the relocs apply to by name. */
2789 name = sec->name;
2790 if (d->this_hdr.sh_type == SHT_REL)
2791 name += 4;
2792 else
2793 name += 5;
2794 s = bfd_get_section_by_name (abfd, name);
2795 if (s != NULL)
2796 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2797 break;
2799 case SHT_STRTAB:
2800 /* We assume that a section named .stab*str is a stabs
2801 string section. We look for a section with the same name
2802 but without the trailing ``str'', and set its sh_link
2803 field to point to this section. */
2804 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2805 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2807 size_t len;
2808 char *alc;
2810 len = strlen (sec->name);
2811 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
2812 if (alc == NULL)
2813 return FALSE;
2814 memcpy (alc, sec->name, len - 3);
2815 alc[len - 3] = '\0';
2816 s = bfd_get_section_by_name (abfd, alc);
2817 free (alc);
2818 if (s != NULL)
2820 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2822 /* This is a .stab section. */
2823 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2824 elf_section_data (s)->this_hdr.sh_entsize
2825 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2828 break;
2830 case SHT_DYNAMIC:
2831 case SHT_DYNSYM:
2832 case SHT_GNU_verneed:
2833 case SHT_GNU_verdef:
2834 /* sh_link is the section header index of the string table
2835 used for the dynamic entries, or the symbol table, or the
2836 version strings. */
2837 s = bfd_get_section_by_name (abfd, ".dynstr");
2838 if (s != NULL)
2839 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2840 break;
2842 case SHT_HASH:
2843 case SHT_GNU_versym:
2844 /* sh_link is the section header index of the symbol table
2845 this hash table or version table is for. */
2846 s = bfd_get_section_by_name (abfd, ".dynsym");
2847 if (s != NULL)
2848 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2849 break;
2851 case SHT_GROUP:
2852 d->this_hdr.sh_link = t->symtab_section;
2856 for (secn = 1; secn < section_number; ++secn)
2857 if (i_shdrp[secn] == NULL)
2858 i_shdrp[secn] = i_shdrp[0];
2859 else
2860 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2861 i_shdrp[secn]->sh_name);
2862 return TRUE;
2865 /* Map symbol from it's internal number to the external number, moving
2866 all local symbols to be at the head of the list. */
2868 static INLINE int
2869 sym_is_global (abfd, sym)
2870 bfd *abfd;
2871 asymbol *sym;
2873 /* If the backend has a special mapping, use it. */
2874 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2875 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2876 (abfd, sym));
2878 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2879 || bfd_is_und_section (bfd_get_section (sym))
2880 || bfd_is_com_section (bfd_get_section (sym)));
2883 static bfd_boolean
2884 elf_map_symbols (abfd)
2885 bfd *abfd;
2887 unsigned int symcount = bfd_get_symcount (abfd);
2888 asymbol **syms = bfd_get_outsymbols (abfd);
2889 asymbol **sect_syms;
2890 unsigned int num_locals = 0;
2891 unsigned int num_globals = 0;
2892 unsigned int num_locals2 = 0;
2893 unsigned int num_globals2 = 0;
2894 int max_index = 0;
2895 unsigned int idx;
2896 asection *asect;
2897 asymbol **new_syms;
2898 bfd_size_type amt;
2900 #ifdef DEBUG
2901 fprintf (stderr, "elf_map_symbols\n");
2902 fflush (stderr);
2903 #endif
2905 for (asect = abfd->sections; asect; asect = asect->next)
2907 if (max_index < asect->index)
2908 max_index = asect->index;
2911 max_index++;
2912 amt = max_index * sizeof (asymbol *);
2913 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2914 if (sect_syms == NULL)
2915 return FALSE;
2916 elf_section_syms (abfd) = sect_syms;
2917 elf_num_section_syms (abfd) = max_index;
2919 /* Init sect_syms entries for any section symbols we have already
2920 decided to output. */
2921 for (idx = 0; idx < symcount; idx++)
2923 asymbol *sym = syms[idx];
2925 if ((sym->flags & BSF_SECTION_SYM) != 0
2926 && sym->value == 0)
2928 asection *sec;
2930 sec = sym->section;
2932 if (sec->owner != NULL)
2934 if (sec->owner != abfd)
2936 if (sec->output_offset != 0)
2937 continue;
2939 sec = sec->output_section;
2941 /* Empty sections in the input files may have had a
2942 section symbol created for them. (See the comment
2943 near the end of _bfd_generic_link_output_symbols in
2944 linker.c). If the linker script discards such
2945 sections then we will reach this point. Since we know
2946 that we cannot avoid this case, we detect it and skip
2947 the abort and the assignment to the sect_syms array.
2948 To reproduce this particular case try running the
2949 linker testsuite test ld-scripts/weak.exp for an ELF
2950 port that uses the generic linker. */
2951 if (sec->owner == NULL)
2952 continue;
2954 BFD_ASSERT (sec->owner == abfd);
2956 sect_syms[sec->index] = syms[idx];
2961 /* Classify all of the symbols. */
2962 for (idx = 0; idx < symcount; idx++)
2964 if (!sym_is_global (abfd, syms[idx]))
2965 num_locals++;
2966 else
2967 num_globals++;
2970 /* We will be adding a section symbol for each BFD section. Most normal
2971 sections will already have a section symbol in outsymbols, but
2972 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2973 at least in that case. */
2974 for (asect = abfd->sections; asect; asect = asect->next)
2976 if (sect_syms[asect->index] == NULL)
2978 if (!sym_is_global (abfd, asect->symbol))
2979 num_locals++;
2980 else
2981 num_globals++;
2985 /* Now sort the symbols so the local symbols are first. */
2986 amt = (num_locals + num_globals) * sizeof (asymbol *);
2987 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2989 if (new_syms == NULL)
2990 return FALSE;
2992 for (idx = 0; idx < symcount; idx++)
2994 asymbol *sym = syms[idx];
2995 unsigned int i;
2997 if (!sym_is_global (abfd, sym))
2998 i = num_locals2++;
2999 else
3000 i = num_locals + num_globals2++;
3001 new_syms[i] = sym;
3002 sym->udata.i = i + 1;
3004 for (asect = abfd->sections; asect; asect = asect->next)
3006 if (sect_syms[asect->index] == NULL)
3008 asymbol *sym = asect->symbol;
3009 unsigned int i;
3011 sect_syms[asect->index] = sym;
3012 if (!sym_is_global (abfd, sym))
3013 i = num_locals2++;
3014 else
3015 i = num_locals + num_globals2++;
3016 new_syms[i] = sym;
3017 sym->udata.i = i + 1;
3021 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3023 elf_num_locals (abfd) = num_locals;
3024 elf_num_globals (abfd) = num_globals;
3025 return TRUE;
3028 /* Align to the maximum file alignment that could be required for any
3029 ELF data structure. */
3031 static INLINE file_ptr align_file_position
3032 PARAMS ((file_ptr, int));
3033 static INLINE file_ptr
3034 align_file_position (off, align)
3035 file_ptr off;
3036 int align;
3038 return (off + align - 1) & ~(align - 1);
3041 /* Assign a file position to a section, optionally aligning to the
3042 required section alignment. */
3044 INLINE file_ptr
3045 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
3046 Elf_Internal_Shdr *i_shdrp;
3047 file_ptr offset;
3048 bfd_boolean align;
3050 if (align)
3052 unsigned int al;
3054 al = i_shdrp->sh_addralign;
3055 if (al > 1)
3056 offset = BFD_ALIGN (offset, al);
3058 i_shdrp->sh_offset = offset;
3059 if (i_shdrp->bfd_section != NULL)
3060 i_shdrp->bfd_section->filepos = offset;
3061 if (i_shdrp->sh_type != SHT_NOBITS)
3062 offset += i_shdrp->sh_size;
3063 return offset;
3066 /* Compute the file positions we are going to put the sections at, and
3067 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3068 is not NULL, this is being called by the ELF backend linker. */
3070 bfd_boolean
3071 _bfd_elf_compute_section_file_positions (abfd, link_info)
3072 bfd *abfd;
3073 struct bfd_link_info *link_info;
3075 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3076 bfd_boolean failed;
3077 struct bfd_strtab_hash *strtab;
3078 Elf_Internal_Shdr *shstrtab_hdr;
3080 if (abfd->output_has_begun)
3081 return TRUE;
3083 /* Do any elf backend specific processing first. */
3084 if (bed->elf_backend_begin_write_processing)
3085 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3087 if (! prep_headers (abfd))
3088 return FALSE;
3090 /* Post process the headers if necessary. */
3091 if (bed->elf_backend_post_process_headers)
3092 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3094 failed = FALSE;
3095 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3096 if (failed)
3097 return FALSE;
3099 if (!assign_section_numbers (abfd))
3100 return FALSE;
3102 /* The backend linker builds symbol table information itself. */
3103 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3105 /* Non-zero if doing a relocatable link. */
3106 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3108 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3109 return FALSE;
3112 if (link_info == NULL)
3114 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3115 if (failed)
3116 return FALSE;
3119 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3120 /* sh_name was set in prep_headers. */
3121 shstrtab_hdr->sh_type = SHT_STRTAB;
3122 shstrtab_hdr->sh_flags = 0;
3123 shstrtab_hdr->sh_addr = 0;
3124 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3125 shstrtab_hdr->sh_entsize = 0;
3126 shstrtab_hdr->sh_link = 0;
3127 shstrtab_hdr->sh_info = 0;
3128 /* sh_offset is set in assign_file_positions_except_relocs. */
3129 shstrtab_hdr->sh_addralign = 1;
3131 if (!assign_file_positions_except_relocs (abfd))
3132 return FALSE;
3134 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3136 file_ptr off;
3137 Elf_Internal_Shdr *hdr;
3139 off = elf_tdata (abfd)->next_file_pos;
3141 hdr = &elf_tdata (abfd)->symtab_hdr;
3142 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3144 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3145 if (hdr->sh_size != 0)
3146 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3148 hdr = &elf_tdata (abfd)->strtab_hdr;
3149 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3151 elf_tdata (abfd)->next_file_pos = off;
3153 /* Now that we know where the .strtab section goes, write it
3154 out. */
3155 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3156 || ! _bfd_stringtab_emit (abfd, strtab))
3157 return FALSE;
3158 _bfd_stringtab_free (strtab);
3161 abfd->output_has_begun = TRUE;
3163 return TRUE;
3166 /* Create a mapping from a set of sections to a program segment. */
3168 static INLINE struct elf_segment_map *
3169 make_mapping (abfd, sections, from, to, phdr)
3170 bfd *abfd;
3171 asection **sections;
3172 unsigned int from;
3173 unsigned int to;
3174 bfd_boolean phdr;
3176 struct elf_segment_map *m;
3177 unsigned int i;
3178 asection **hdrpp;
3179 bfd_size_type amt;
3181 amt = sizeof (struct elf_segment_map);
3182 amt += (to - from - 1) * sizeof (asection *);
3183 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3184 if (m == NULL)
3185 return NULL;
3186 m->next = NULL;
3187 m->p_type = PT_LOAD;
3188 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3189 m->sections[i - from] = *hdrpp;
3190 m->count = to - from;
3192 if (from == 0 && phdr)
3194 /* Include the headers in the first PT_LOAD segment. */
3195 m->includes_filehdr = 1;
3196 m->includes_phdrs = 1;
3199 return m;
3202 /* Set up a mapping from BFD sections to program segments. */
3204 static bfd_boolean
3205 map_sections_to_segments (abfd)
3206 bfd *abfd;
3208 asection **sections = NULL;
3209 asection *s;
3210 unsigned int i;
3211 unsigned int count;
3212 struct elf_segment_map *mfirst;
3213 struct elf_segment_map **pm;
3214 struct elf_segment_map *m;
3215 asection *last_hdr;
3216 unsigned int phdr_index;
3217 bfd_vma maxpagesize;
3218 asection **hdrpp;
3219 bfd_boolean phdr_in_segment = TRUE;
3220 bfd_boolean writable;
3221 int tls_count = 0;
3222 asection *first_tls = NULL;
3223 asection *dynsec, *eh_frame_hdr;
3224 bfd_size_type amt;
3226 if (elf_tdata (abfd)->segment_map != NULL)
3227 return TRUE;
3229 if (bfd_count_sections (abfd) == 0)
3230 return TRUE;
3232 /* Select the allocated sections, and sort them. */
3234 amt = bfd_count_sections (abfd) * sizeof (asection *);
3235 sections = (asection **) bfd_malloc (amt);
3236 if (sections == NULL)
3237 goto error_return;
3239 i = 0;
3240 for (s = abfd->sections; s != NULL; s = s->next)
3242 if ((s->flags & SEC_ALLOC) != 0)
3244 sections[i] = s;
3245 ++i;
3248 BFD_ASSERT (i <= bfd_count_sections (abfd));
3249 count = i;
3251 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3253 /* Build the mapping. */
3255 mfirst = NULL;
3256 pm = &mfirst;
3258 /* If we have a .interp section, then create a PT_PHDR segment for
3259 the program headers and a PT_INTERP segment for the .interp
3260 section. */
3261 s = bfd_get_section_by_name (abfd, ".interp");
3262 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3264 amt = sizeof (struct elf_segment_map);
3265 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3266 if (m == NULL)
3267 goto error_return;
3268 m->next = NULL;
3269 m->p_type = PT_PHDR;
3270 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3271 m->p_flags = PF_R | PF_X;
3272 m->p_flags_valid = 1;
3273 m->includes_phdrs = 1;
3275 *pm = m;
3276 pm = &m->next;
3278 amt = sizeof (struct elf_segment_map);
3279 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3280 if (m == NULL)
3281 goto error_return;
3282 m->next = NULL;
3283 m->p_type = PT_INTERP;
3284 m->count = 1;
3285 m->sections[0] = s;
3287 *pm = m;
3288 pm = &m->next;
3291 /* Look through the sections. We put sections in the same program
3292 segment when the start of the second section can be placed within
3293 a few bytes of the end of the first section. */
3294 last_hdr = NULL;
3295 phdr_index = 0;
3296 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3297 writable = FALSE;
3298 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3299 if (dynsec != NULL
3300 && (dynsec->flags & SEC_LOAD) == 0)
3301 dynsec = NULL;
3303 /* Deal with -Ttext or something similar such that the first section
3304 is not adjacent to the program headers. This is an
3305 approximation, since at this point we don't know exactly how many
3306 program headers we will need. */
3307 if (count > 0)
3309 bfd_size_type phdr_size;
3311 phdr_size = elf_tdata (abfd)->program_header_size;
3312 if (phdr_size == 0)
3313 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3314 if ((abfd->flags & D_PAGED) == 0
3315 || sections[0]->lma < phdr_size
3316 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3317 phdr_in_segment = FALSE;
3320 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3322 asection *hdr;
3323 bfd_boolean new_segment;
3325 hdr = *hdrpp;
3327 /* See if this section and the last one will fit in the same
3328 segment. */
3330 if (last_hdr == NULL)
3332 /* If we don't have a segment yet, then we don't need a new
3333 one (we build the last one after this loop). */
3334 new_segment = FALSE;
3336 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3338 /* If this section has a different relation between the
3339 virtual address and the load address, then we need a new
3340 segment. */
3341 new_segment = TRUE;
3343 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3344 < BFD_ALIGN (hdr->lma, maxpagesize))
3346 /* If putting this section in this segment would force us to
3347 skip a page in the segment, then we need a new segment. */
3348 new_segment = TRUE;
3350 else if ((last_hdr->flags & SEC_LOAD) == 0
3351 && (hdr->flags & SEC_LOAD) != 0)
3353 /* We don't want to put a loadable section after a
3354 nonloadable section in the same segment. */
3355 new_segment = TRUE;
3357 else if ((abfd->flags & D_PAGED) == 0)
3359 /* If the file is not demand paged, which means that we
3360 don't require the sections to be correctly aligned in the
3361 file, then there is no other reason for a new segment. */
3362 new_segment = FALSE;
3364 else if (! writable
3365 && (hdr->flags & SEC_READONLY) == 0
3366 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3367 & ~(maxpagesize - 1))
3368 != (hdr->lma & ~(maxpagesize - 1))))
3370 /* We don't want to put a writable section in a read only
3371 segment, unless they are on the same page in memory
3372 anyhow. We already know that the last section does not
3373 bring us past the current section on the page, so the
3374 only case in which the new section is not on the same
3375 page as the previous section is when the previous section
3376 ends precisely on a page boundary. */
3377 new_segment = TRUE;
3379 else
3381 /* Otherwise, we can use the same segment. */
3382 new_segment = FALSE;
3385 if (! new_segment)
3387 if ((hdr->flags & SEC_READONLY) == 0)
3388 writable = TRUE;
3389 last_hdr = hdr;
3390 continue;
3393 /* We need a new program segment. We must create a new program
3394 header holding all the sections from phdr_index until hdr. */
3396 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3397 if (m == NULL)
3398 goto error_return;
3400 *pm = m;
3401 pm = &m->next;
3403 if ((hdr->flags & SEC_READONLY) == 0)
3404 writable = TRUE;
3405 else
3406 writable = FALSE;
3408 last_hdr = hdr;
3409 phdr_index = i;
3410 phdr_in_segment = FALSE;
3413 /* Create a final PT_LOAD program segment. */
3414 if (last_hdr != NULL)
3416 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3417 if (m == NULL)
3418 goto error_return;
3420 *pm = m;
3421 pm = &m->next;
3424 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3425 if (dynsec != NULL)
3427 amt = sizeof (struct elf_segment_map);
3428 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3429 if (m == NULL)
3430 goto error_return;
3431 m->next = NULL;
3432 m->p_type = PT_DYNAMIC;
3433 m->count = 1;
3434 m->sections[0] = dynsec;
3436 *pm = m;
3437 pm = &m->next;
3440 /* For each loadable .note section, add a PT_NOTE segment. We don't
3441 use bfd_get_section_by_name, because if we link together
3442 nonloadable .note sections and loadable .note sections, we will
3443 generate two .note sections in the output file. FIXME: Using
3444 names for section types is bogus anyhow. */
3445 for (s = abfd->sections; s != NULL; s = s->next)
3447 if ((s->flags & SEC_LOAD) != 0
3448 && strncmp (s->name, ".note", 5) == 0)
3450 amt = sizeof (struct elf_segment_map);
3451 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3452 if (m == NULL)
3453 goto error_return;
3454 m->next = NULL;
3455 m->p_type = PT_NOTE;
3456 m->count = 1;
3457 m->sections[0] = s;
3459 *pm = m;
3460 pm = &m->next;
3462 if (s->flags & SEC_THREAD_LOCAL)
3464 if (! tls_count)
3465 first_tls = s;
3466 tls_count++;
3470 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3471 if (tls_count > 0)
3473 int i;
3475 amt = sizeof (struct elf_segment_map);
3476 amt += (tls_count - 1) * sizeof (asection *);
3477 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3478 if (m == NULL)
3479 goto error_return;
3480 m->next = NULL;
3481 m->p_type = PT_TLS;
3482 m->count = tls_count;
3483 /* Mandated PF_R. */
3484 m->p_flags = PF_R;
3485 m->p_flags_valid = 1;
3486 for (i = 0; i < tls_count; ++i)
3488 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3489 m->sections[i] = first_tls;
3490 first_tls = first_tls->next;
3493 *pm = m;
3494 pm = &m->next;
3497 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3498 segment. */
3499 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3500 if (eh_frame_hdr != NULL
3501 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3503 amt = sizeof (struct elf_segment_map);
3504 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3505 if (m == NULL)
3506 goto error_return;
3507 m->next = NULL;
3508 m->p_type = PT_GNU_EH_FRAME;
3509 m->count = 1;
3510 m->sections[0] = eh_frame_hdr->output_section;
3512 *pm = m;
3513 pm = &m->next;
3516 free (sections);
3517 sections = NULL;
3519 elf_tdata (abfd)->segment_map = mfirst;
3520 return TRUE;
3522 error_return:
3523 if (sections != NULL)
3524 free (sections);
3525 return FALSE;
3528 /* Sort sections by address. */
3530 static int
3531 elf_sort_sections (arg1, arg2)
3532 const PTR arg1;
3533 const PTR arg2;
3535 const asection *sec1 = *(const asection **) arg1;
3536 const asection *sec2 = *(const asection **) arg2;
3537 bfd_size_type size1, size2;
3539 /* Sort by LMA first, since this is the address used to
3540 place the section into a segment. */
3541 if (sec1->lma < sec2->lma)
3542 return -1;
3543 else if (sec1->lma > sec2->lma)
3544 return 1;
3546 /* Then sort by VMA. Normally the LMA and the VMA will be
3547 the same, and this will do nothing. */
3548 if (sec1->vma < sec2->vma)
3549 return -1;
3550 else if (sec1->vma > sec2->vma)
3551 return 1;
3553 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3555 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3557 if (TOEND (sec1))
3559 if (TOEND (sec2))
3561 /* If the indicies are the same, do not return 0
3562 here, but continue to try the next comparison. */
3563 if (sec1->target_index - sec2->target_index != 0)
3564 return sec1->target_index - sec2->target_index;
3566 else
3567 return 1;
3569 else if (TOEND (sec2))
3570 return -1;
3572 #undef TOEND
3574 /* Sort by size, to put zero sized sections
3575 before others at the same address. */
3577 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3578 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3580 if (size1 < size2)
3581 return -1;
3582 if (size1 > size2)
3583 return 1;
3585 return sec1->target_index - sec2->target_index;
3588 /* Assign file positions to the sections based on the mapping from
3589 sections to segments. This function also sets up some fields in
3590 the file header, and writes out the program headers. */
3592 static bfd_boolean
3593 assign_file_positions_for_segments (abfd)
3594 bfd *abfd;
3596 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3597 unsigned int count;
3598 struct elf_segment_map *m;
3599 unsigned int alloc;
3600 Elf_Internal_Phdr *phdrs;
3601 file_ptr off, voff;
3602 bfd_vma filehdr_vaddr, filehdr_paddr;
3603 bfd_vma phdrs_vaddr, phdrs_paddr;
3604 Elf_Internal_Phdr *p;
3605 bfd_size_type amt;
3607 if (elf_tdata (abfd)->segment_map == NULL)
3609 if (! map_sections_to_segments (abfd))
3610 return FALSE;
3612 else
3614 /* The placement algorithm assumes that non allocated sections are
3615 not in PT_LOAD segments. We ensure this here by removing such
3616 sections from the segment map. */
3617 for (m = elf_tdata (abfd)->segment_map;
3618 m != NULL;
3619 m = m->next)
3621 unsigned int new_count;
3622 unsigned int i;
3624 if (m->p_type != PT_LOAD)
3625 continue;
3627 new_count = 0;
3628 for (i = 0; i < m->count; i ++)
3630 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3632 if (i != new_count)
3633 m->sections[new_count] = m->sections[i];
3635 new_count ++;
3639 if (new_count != m->count)
3640 m->count = new_count;
3644 if (bed->elf_backend_modify_segment_map)
3646 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3647 return FALSE;
3650 count = 0;
3651 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3652 ++count;
3654 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3655 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3656 elf_elfheader (abfd)->e_phnum = count;
3658 if (count == 0)
3659 return TRUE;
3661 /* If we already counted the number of program segments, make sure
3662 that we allocated enough space. This happens when SIZEOF_HEADERS
3663 is used in a linker script. */
3664 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3665 if (alloc != 0 && count > alloc)
3667 ((*_bfd_error_handler)
3668 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3669 bfd_get_filename (abfd), alloc, count));
3670 bfd_set_error (bfd_error_bad_value);
3671 return FALSE;
3674 if (alloc == 0)
3675 alloc = count;
3677 amt = alloc * sizeof (Elf_Internal_Phdr);
3678 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3679 if (phdrs == NULL)
3680 return FALSE;
3682 off = bed->s->sizeof_ehdr;
3683 off += alloc * bed->s->sizeof_phdr;
3685 filehdr_vaddr = 0;
3686 filehdr_paddr = 0;
3687 phdrs_vaddr = 0;
3688 phdrs_paddr = 0;
3690 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3691 m != NULL;
3692 m = m->next, p++)
3694 unsigned int i;
3695 asection **secpp;
3697 /* If elf_segment_map is not from map_sections_to_segments, the
3698 sections may not be correctly ordered. NOTE: sorting should
3699 not be done to the PT_NOTE section of a corefile, which may
3700 contain several pseudo-sections artificially created by bfd.
3701 Sorting these pseudo-sections breaks things badly. */
3702 if (m->count > 1
3703 && !(elf_elfheader (abfd)->e_type == ET_CORE
3704 && m->p_type == PT_NOTE))
3705 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3706 elf_sort_sections);
3708 p->p_type = m->p_type;
3709 p->p_flags = m->p_flags;
3711 if (p->p_type == PT_LOAD
3712 && m->count > 0
3713 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3715 if ((abfd->flags & D_PAGED) != 0)
3716 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3717 else
3719 bfd_size_type align;
3721 align = 0;
3722 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3724 bfd_size_type secalign;
3726 secalign = bfd_get_section_alignment (abfd, *secpp);
3727 if (secalign > align)
3728 align = secalign;
3731 off += (m->sections[0]->vma - off) % (1 << align);
3735 if (m->count == 0)
3736 p->p_vaddr = 0;
3737 else
3738 p->p_vaddr = m->sections[0]->vma;
3740 if (m->p_paddr_valid)
3741 p->p_paddr = m->p_paddr;
3742 else if (m->count == 0)
3743 p->p_paddr = 0;
3744 else
3745 p->p_paddr = m->sections[0]->lma;
3747 if (p->p_type == PT_LOAD
3748 && (abfd->flags & D_PAGED) != 0)
3749 p->p_align = bed->maxpagesize;
3750 else if (m->count == 0)
3751 p->p_align = 1 << bed->s->log_file_align;
3752 else
3753 p->p_align = 0;
3755 p->p_offset = 0;
3756 p->p_filesz = 0;
3757 p->p_memsz = 0;
3759 if (m->includes_filehdr)
3761 if (! m->p_flags_valid)
3762 p->p_flags |= PF_R;
3763 p->p_offset = 0;
3764 p->p_filesz = bed->s->sizeof_ehdr;
3765 p->p_memsz = bed->s->sizeof_ehdr;
3766 if (m->count > 0)
3768 BFD_ASSERT (p->p_type == PT_LOAD);
3770 if (p->p_vaddr < (bfd_vma) off)
3772 (*_bfd_error_handler)
3773 (_("%s: Not enough room for program headers, try linking with -N"),
3774 bfd_get_filename (abfd));
3775 bfd_set_error (bfd_error_bad_value);
3776 return FALSE;
3779 p->p_vaddr -= off;
3780 if (! m->p_paddr_valid)
3781 p->p_paddr -= off;
3783 if (p->p_type == PT_LOAD)
3785 filehdr_vaddr = p->p_vaddr;
3786 filehdr_paddr = p->p_paddr;
3790 if (m->includes_phdrs)
3792 if (! m->p_flags_valid)
3793 p->p_flags |= PF_R;
3795 if (m->includes_filehdr)
3797 if (p->p_type == PT_LOAD)
3799 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3800 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3803 else
3805 p->p_offset = bed->s->sizeof_ehdr;
3807 if (m->count > 0)
3809 BFD_ASSERT (p->p_type == PT_LOAD);
3810 p->p_vaddr -= off - p->p_offset;
3811 if (! m->p_paddr_valid)
3812 p->p_paddr -= off - p->p_offset;
3815 if (p->p_type == PT_LOAD)
3817 phdrs_vaddr = p->p_vaddr;
3818 phdrs_paddr = p->p_paddr;
3820 else
3821 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3824 p->p_filesz += alloc * bed->s->sizeof_phdr;
3825 p->p_memsz += alloc * bed->s->sizeof_phdr;
3828 if (p->p_type == PT_LOAD
3829 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3831 if (! m->includes_filehdr && ! m->includes_phdrs)
3832 p->p_offset = off;
3833 else
3835 file_ptr adjust;
3837 adjust = off - (p->p_offset + p->p_filesz);
3838 p->p_filesz += adjust;
3839 p->p_memsz += adjust;
3843 voff = off;
3845 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3847 asection *sec;
3848 flagword flags;
3849 bfd_size_type align;
3851 sec = *secpp;
3852 flags = sec->flags;
3853 align = 1 << bfd_get_section_alignment (abfd, sec);
3855 /* The section may have artificial alignment forced by a
3856 link script. Notice this case by the gap between the
3857 cumulative phdr lma and the section's lma. */
3858 if (p->p_paddr + p->p_memsz < sec->lma)
3860 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3862 p->p_memsz += adjust;
3863 if (p->p_type == PT_LOAD
3864 || (p->p_type == PT_NOTE
3865 && bfd_get_format (abfd) == bfd_core))
3867 off += adjust;
3868 voff += adjust;
3870 if ((flags & SEC_LOAD) != 0
3871 || (flags & SEC_THREAD_LOCAL) != 0)
3872 p->p_filesz += adjust;
3875 if (p->p_type == PT_LOAD)
3877 bfd_signed_vma adjust;
3879 if ((flags & SEC_LOAD) != 0)
3881 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3882 if (adjust < 0)
3883 adjust = 0;
3885 else if ((flags & SEC_ALLOC) != 0)
3887 /* The section VMA must equal the file position
3888 modulo the page size. FIXME: I'm not sure if
3889 this adjustment is really necessary. We used to
3890 not have the SEC_LOAD case just above, and then
3891 this was necessary, but now I'm not sure. */
3892 if ((abfd->flags & D_PAGED) != 0)
3893 adjust = (sec->vma - voff) % bed->maxpagesize;
3894 else
3895 adjust = (sec->vma - voff) % align;
3897 else
3898 adjust = 0;
3900 if (adjust != 0)
3902 if (i == 0)
3904 (* _bfd_error_handler) (_("\
3905 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3906 bfd_section_name (abfd, sec),
3907 sec->lma,
3908 p->p_paddr);
3909 return FALSE;
3911 p->p_memsz += adjust;
3912 off += adjust;
3913 voff += adjust;
3914 if ((flags & SEC_LOAD) != 0)
3915 p->p_filesz += adjust;
3918 sec->filepos = off;
3920 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3921 used in a linker script we may have a section with
3922 SEC_LOAD clear but which is supposed to have
3923 contents. */
3924 if ((flags & SEC_LOAD) != 0
3925 || (flags & SEC_HAS_CONTENTS) != 0)
3926 off += sec->_raw_size;
3928 if ((flags & SEC_ALLOC) != 0
3929 && ((flags & SEC_LOAD) != 0
3930 || (flags & SEC_THREAD_LOCAL) == 0))
3931 voff += sec->_raw_size;
3934 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3936 /* The actual "note" segment has i == 0.
3937 This is the one that actually contains everything. */
3938 if (i == 0)
3940 sec->filepos = off;
3941 p->p_filesz = sec->_raw_size;
3942 off += sec->_raw_size;
3943 voff = off;
3945 else
3947 /* Fake sections -- don't need to be written. */
3948 sec->filepos = 0;
3949 sec->_raw_size = 0;
3950 flags = sec->flags = 0;
3952 p->p_memsz = 0;
3953 p->p_align = 1;
3955 else
3957 if ((sec->flags & SEC_LOAD) != 0
3958 || (sec->flags & SEC_THREAD_LOCAL) == 0
3959 || p->p_type == PT_TLS)
3960 p->p_memsz += sec->_raw_size;
3962 if ((flags & SEC_LOAD) != 0)
3963 p->p_filesz += sec->_raw_size;
3965 if (p->p_type == PT_TLS
3966 && sec->_raw_size == 0
3967 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3969 struct bfd_link_order *o;
3970 bfd_vma tbss_size = 0;
3972 for (o = sec->link_order_head; o != NULL; o = o->next)
3973 if (tbss_size < o->offset + o->size)
3974 tbss_size = o->offset + o->size;
3976 p->p_memsz += tbss_size;
3979 if (align > p->p_align
3980 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3981 p->p_align = align;
3984 if (! m->p_flags_valid)
3986 p->p_flags |= PF_R;
3987 if ((flags & SEC_CODE) != 0)
3988 p->p_flags |= PF_X;
3989 if ((flags & SEC_READONLY) == 0)
3990 p->p_flags |= PF_W;
3995 /* Now that we have set the section file positions, we can set up
3996 the file positions for the non PT_LOAD segments. */
3997 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3998 m != NULL;
3999 m = m->next, p++)
4001 if (p->p_type != PT_LOAD && m->count > 0)
4003 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4004 p->p_offset = m->sections[0]->filepos;
4006 if (m->count == 0)
4008 if (m->includes_filehdr)
4010 p->p_vaddr = filehdr_vaddr;
4011 if (! m->p_paddr_valid)
4012 p->p_paddr = filehdr_paddr;
4014 else if (m->includes_phdrs)
4016 p->p_vaddr = phdrs_vaddr;
4017 if (! m->p_paddr_valid)
4018 p->p_paddr = phdrs_paddr;
4023 /* Clear out any program headers we allocated but did not use. */
4024 for (; count < alloc; count++, p++)
4026 memset (p, 0, sizeof *p);
4027 p->p_type = PT_NULL;
4030 elf_tdata (abfd)->phdr = phdrs;
4032 elf_tdata (abfd)->next_file_pos = off;
4034 /* Write out the program headers. */
4035 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4036 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4037 return FALSE;
4039 return TRUE;
4042 /* Get the size of the program header.
4044 If this is called by the linker before any of the section VMA's are set, it
4045 can't calculate the correct value for a strange memory layout. This only
4046 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4047 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4048 data segment (exclusive of .interp and .dynamic).
4050 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4051 will be two segments. */
4053 static bfd_size_type
4054 get_program_header_size (abfd)
4055 bfd *abfd;
4057 size_t segs;
4058 asection *s;
4059 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4061 /* We can't return a different result each time we're called. */
4062 if (elf_tdata (abfd)->program_header_size != 0)
4063 return elf_tdata (abfd)->program_header_size;
4065 if (elf_tdata (abfd)->segment_map != NULL)
4067 struct elf_segment_map *m;
4069 segs = 0;
4070 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4071 ++segs;
4072 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4073 return elf_tdata (abfd)->program_header_size;
4076 /* Assume we will need exactly two PT_LOAD segments: one for text
4077 and one for data. */
4078 segs = 2;
4080 s = bfd_get_section_by_name (abfd, ".interp");
4081 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4083 /* If we have a loadable interpreter section, we need a
4084 PT_INTERP segment. In this case, assume we also need a
4085 PT_PHDR segment, although that may not be true for all
4086 targets. */
4087 segs += 2;
4090 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4092 /* We need a PT_DYNAMIC segment. */
4093 ++segs;
4096 if (elf_tdata (abfd)->eh_frame_hdr)
4098 /* We need a PT_GNU_EH_FRAME segment. */
4099 ++segs;
4102 for (s = abfd->sections; s != NULL; s = s->next)
4104 if ((s->flags & SEC_LOAD) != 0
4105 && strncmp (s->name, ".note", 5) == 0)
4107 /* We need a PT_NOTE segment. */
4108 ++segs;
4112 for (s = abfd->sections; s != NULL; s = s->next)
4114 if (s->flags & SEC_THREAD_LOCAL)
4116 /* We need a PT_TLS segment. */
4117 ++segs;
4118 break;
4122 /* Let the backend count up any program headers it might need. */
4123 if (bed->elf_backend_additional_program_headers)
4125 int a;
4127 a = (*bed->elf_backend_additional_program_headers) (abfd);
4128 if (a == -1)
4129 abort ();
4130 segs += a;
4133 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4134 return elf_tdata (abfd)->program_header_size;
4137 /* Work out the file positions of all the sections. This is called by
4138 _bfd_elf_compute_section_file_positions. All the section sizes and
4139 VMAs must be known before this is called.
4141 We do not consider reloc sections at this point, unless they form
4142 part of the loadable image. Reloc sections are assigned file
4143 positions in assign_file_positions_for_relocs, which is called by
4144 write_object_contents and final_link.
4146 We also don't set the positions of the .symtab and .strtab here. */
4148 static bfd_boolean
4149 assign_file_positions_except_relocs (abfd)
4150 bfd *abfd;
4152 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4153 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4154 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4155 unsigned int num_sec = elf_numsections (abfd);
4156 file_ptr off;
4157 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4159 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4160 && bfd_get_format (abfd) != bfd_core)
4162 Elf_Internal_Shdr **hdrpp;
4163 unsigned int i;
4165 /* Start after the ELF header. */
4166 off = i_ehdrp->e_ehsize;
4168 /* We are not creating an executable, which means that we are
4169 not creating a program header, and that the actual order of
4170 the sections in the file is unimportant. */
4171 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4173 Elf_Internal_Shdr *hdr;
4175 hdr = *hdrpp;
4176 if (hdr->sh_type == SHT_REL
4177 || hdr->sh_type == SHT_RELA
4178 || i == tdata->symtab_section
4179 || i == tdata->symtab_shndx_section
4180 || i == tdata->strtab_section)
4182 hdr->sh_offset = -1;
4184 else
4185 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4187 if (i == SHN_LORESERVE - 1)
4189 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4190 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4194 else
4196 unsigned int i;
4197 Elf_Internal_Shdr **hdrpp;
4199 /* Assign file positions for the loaded sections based on the
4200 assignment of sections to segments. */
4201 if (! assign_file_positions_for_segments (abfd))
4202 return FALSE;
4204 /* Assign file positions for the other sections. */
4206 off = elf_tdata (abfd)->next_file_pos;
4207 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4209 Elf_Internal_Shdr *hdr;
4211 hdr = *hdrpp;
4212 if (hdr->bfd_section != NULL
4213 && hdr->bfd_section->filepos != 0)
4214 hdr->sh_offset = hdr->bfd_section->filepos;
4215 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4217 ((*_bfd_error_handler)
4218 (_("%s: warning: allocated section `%s' not in segment"),
4219 bfd_get_filename (abfd),
4220 (hdr->bfd_section == NULL
4221 ? "*unknown*"
4222 : hdr->bfd_section->name)));
4223 if ((abfd->flags & D_PAGED) != 0)
4224 off += (hdr->sh_addr - off) % bed->maxpagesize;
4225 else
4226 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4227 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4228 FALSE);
4230 else if (hdr->sh_type == SHT_REL
4231 || hdr->sh_type == SHT_RELA
4232 || hdr == i_shdrpp[tdata->symtab_section]
4233 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4234 || hdr == i_shdrpp[tdata->strtab_section])
4235 hdr->sh_offset = -1;
4236 else
4237 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4239 if (i == SHN_LORESERVE - 1)
4241 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4242 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4247 /* Place the section headers. */
4248 off = align_file_position (off, 1 << bed->s->log_file_align);
4249 i_ehdrp->e_shoff = off;
4250 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4252 elf_tdata (abfd)->next_file_pos = off;
4254 return TRUE;
4257 static bfd_boolean
4258 prep_headers (abfd)
4259 bfd *abfd;
4261 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4262 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4263 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4264 struct elf_strtab_hash *shstrtab;
4265 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4267 i_ehdrp = elf_elfheader (abfd);
4268 i_shdrp = elf_elfsections (abfd);
4270 shstrtab = _bfd_elf_strtab_init ();
4271 if (shstrtab == NULL)
4272 return FALSE;
4274 elf_shstrtab (abfd) = shstrtab;
4276 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4277 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4278 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4279 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4281 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4282 i_ehdrp->e_ident[EI_DATA] =
4283 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4284 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4286 if ((abfd->flags & DYNAMIC) != 0)
4287 i_ehdrp->e_type = ET_DYN;
4288 else if ((abfd->flags & EXEC_P) != 0)
4289 i_ehdrp->e_type = ET_EXEC;
4290 else if (bfd_get_format (abfd) == bfd_core)
4291 i_ehdrp->e_type = ET_CORE;
4292 else
4293 i_ehdrp->e_type = ET_REL;
4295 switch (bfd_get_arch (abfd))
4297 case bfd_arch_unknown:
4298 i_ehdrp->e_machine = EM_NONE;
4299 break;
4301 /* There used to be a long list of cases here, each one setting
4302 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4303 in the corresponding bfd definition. To avoid duplication,
4304 the switch was removed. Machines that need special handling
4305 can generally do it in elf_backend_final_write_processing(),
4306 unless they need the information earlier than the final write.
4307 Such need can generally be supplied by replacing the tests for
4308 e_machine with the conditions used to determine it. */
4309 default:
4310 if (get_elf_backend_data (abfd) != NULL)
4311 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4312 else
4313 i_ehdrp->e_machine = EM_NONE;
4316 i_ehdrp->e_version = bed->s->ev_current;
4317 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4319 /* No program header, for now. */
4320 i_ehdrp->e_phoff = 0;
4321 i_ehdrp->e_phentsize = 0;
4322 i_ehdrp->e_phnum = 0;
4324 /* Each bfd section is section header entry. */
4325 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4326 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4328 /* If we're building an executable, we'll need a program header table. */
4329 if (abfd->flags & EXEC_P)
4331 /* It all happens later. */
4332 #if 0
4333 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4335 /* elf_build_phdrs() returns a (NULL-terminated) array of
4336 Elf_Internal_Phdrs. */
4337 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4338 i_ehdrp->e_phoff = outbase;
4339 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4340 #endif
4342 else
4344 i_ehdrp->e_phentsize = 0;
4345 i_phdrp = 0;
4346 i_ehdrp->e_phoff = 0;
4349 elf_tdata (abfd)->symtab_hdr.sh_name =
4350 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4351 elf_tdata (abfd)->strtab_hdr.sh_name =
4352 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4353 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4354 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4355 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4356 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4357 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4358 return FALSE;
4360 return TRUE;
4363 /* Assign file positions for all the reloc sections which are not part
4364 of the loadable file image. */
4366 void
4367 _bfd_elf_assign_file_positions_for_relocs (abfd)
4368 bfd *abfd;
4370 file_ptr off;
4371 unsigned int i, num_sec;
4372 Elf_Internal_Shdr **shdrpp;
4374 off = elf_tdata (abfd)->next_file_pos;
4376 num_sec = elf_numsections (abfd);
4377 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4379 Elf_Internal_Shdr *shdrp;
4381 shdrp = *shdrpp;
4382 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4383 && shdrp->sh_offset == -1)
4384 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4387 elf_tdata (abfd)->next_file_pos = off;
4390 bfd_boolean
4391 _bfd_elf_write_object_contents (abfd)
4392 bfd *abfd;
4394 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4395 Elf_Internal_Ehdr *i_ehdrp;
4396 Elf_Internal_Shdr **i_shdrp;
4397 bfd_boolean failed;
4398 unsigned int count, num_sec;
4400 if (! abfd->output_has_begun
4401 && ! _bfd_elf_compute_section_file_positions
4402 (abfd, (struct bfd_link_info *) NULL))
4403 return FALSE;
4405 i_shdrp = elf_elfsections (abfd);
4406 i_ehdrp = elf_elfheader (abfd);
4408 failed = FALSE;
4409 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4410 if (failed)
4411 return FALSE;
4413 _bfd_elf_assign_file_positions_for_relocs (abfd);
4415 /* After writing the headers, we need to write the sections too... */
4416 num_sec = elf_numsections (abfd);
4417 for (count = 1; count < num_sec; count++)
4419 if (bed->elf_backend_section_processing)
4420 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4421 if (i_shdrp[count]->contents)
4423 bfd_size_type amt = i_shdrp[count]->sh_size;
4425 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4426 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4427 return FALSE;
4429 if (count == SHN_LORESERVE - 1)
4430 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4433 /* Write out the section header names. */
4434 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4435 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4436 return FALSE;
4438 if (bed->elf_backend_final_write_processing)
4439 (*bed->elf_backend_final_write_processing) (abfd,
4440 elf_tdata (abfd)->linker);
4442 return bed->s->write_shdrs_and_ehdr (abfd);
4445 bfd_boolean
4446 _bfd_elf_write_corefile_contents (abfd)
4447 bfd *abfd;
4449 /* Hopefully this can be done just like an object file. */
4450 return _bfd_elf_write_object_contents (abfd);
4453 /* Given a section, search the header to find them. */
4456 _bfd_elf_section_from_bfd_section (abfd, asect)
4457 bfd *abfd;
4458 struct sec *asect;
4460 struct elf_backend_data *bed;
4461 int index;
4463 if (elf_section_data (asect) != NULL
4464 && elf_section_data (asect)->this_idx != 0)
4465 return elf_section_data (asect)->this_idx;
4467 if (bfd_is_abs_section (asect))
4468 index = SHN_ABS;
4469 else if (bfd_is_com_section (asect))
4470 index = SHN_COMMON;
4471 else if (bfd_is_und_section (asect))
4472 index = SHN_UNDEF;
4473 else
4475 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4476 int maxindex = elf_numsections (abfd);
4478 for (index = 1; index < maxindex; index++)
4480 Elf_Internal_Shdr *hdr = i_shdrp[index];
4482 if (hdr != NULL && hdr->bfd_section == asect)
4483 return index;
4485 index = -1;
4488 bed = get_elf_backend_data (abfd);
4489 if (bed->elf_backend_section_from_bfd_section)
4491 int retval = index;
4493 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4494 return retval;
4497 if (index == -1)
4498 bfd_set_error (bfd_error_nonrepresentable_section);
4500 return index;
4503 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4504 on error. */
4507 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4508 bfd *abfd;
4509 asymbol **asym_ptr_ptr;
4511 asymbol *asym_ptr = *asym_ptr_ptr;
4512 int idx;
4513 flagword flags = asym_ptr->flags;
4515 /* When gas creates relocations against local labels, it creates its
4516 own symbol for the section, but does put the symbol into the
4517 symbol chain, so udata is 0. When the linker is generating
4518 relocatable output, this section symbol may be for one of the
4519 input sections rather than the output section. */
4520 if (asym_ptr->udata.i == 0
4521 && (flags & BSF_SECTION_SYM)
4522 && asym_ptr->section)
4524 int indx;
4526 if (asym_ptr->section->output_section != NULL)
4527 indx = asym_ptr->section->output_section->index;
4528 else
4529 indx = asym_ptr->section->index;
4530 if (indx < elf_num_section_syms (abfd)
4531 && elf_section_syms (abfd)[indx] != NULL)
4532 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4535 idx = asym_ptr->udata.i;
4537 if (idx == 0)
4539 /* This case can occur when using --strip-symbol on a symbol
4540 which is used in a relocation entry. */
4541 (*_bfd_error_handler)
4542 (_("%s: symbol `%s' required but not present"),
4543 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4544 bfd_set_error (bfd_error_no_symbols);
4545 return -1;
4548 #if DEBUG & 4
4550 fprintf (stderr,
4551 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4552 (long) asym_ptr, asym_ptr->name, idx, flags,
4553 elf_symbol_flags (flags));
4554 fflush (stderr);
4556 #endif
4558 return idx;
4561 /* Copy private BFD data. This copies any program header information. */
4563 static bfd_boolean
4564 copy_private_bfd_data (ibfd, obfd)
4565 bfd *ibfd;
4566 bfd *obfd;
4568 Elf_Internal_Ehdr *iehdr;
4569 struct elf_segment_map *map;
4570 struct elf_segment_map *map_first;
4571 struct elf_segment_map **pointer_to_map;
4572 Elf_Internal_Phdr *segment;
4573 asection *section;
4574 unsigned int i;
4575 unsigned int num_segments;
4576 bfd_boolean phdr_included = FALSE;
4577 bfd_vma maxpagesize;
4578 struct elf_segment_map *phdr_adjust_seg = NULL;
4579 unsigned int phdr_adjust_num = 0;
4580 struct elf_backend_data *bed;
4582 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4583 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4584 return TRUE;
4586 if (elf_tdata (ibfd)->phdr == NULL)
4587 return TRUE;
4589 bed = get_elf_backend_data (ibfd);
4590 iehdr = elf_elfheader (ibfd);
4592 map_first = NULL;
4593 pointer_to_map = &map_first;
4595 num_segments = elf_elfheader (ibfd)->e_phnum;
4596 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4598 /* Returns the end address of the segment + 1. */
4599 #define SEGMENT_END(segment, start) \
4600 (start + (segment->p_memsz > segment->p_filesz \
4601 ? segment->p_memsz : segment->p_filesz))
4603 #define SECTION_SIZE(section, segment) \
4604 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4605 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4606 ? section->_raw_size : 0)
4608 /* Returns TRUE if the given section is contained within
4609 the given segment. VMA addresses are compared. */
4610 #define IS_CONTAINED_BY_VMA(section, segment) \
4611 (section->vma >= segment->p_vaddr \
4612 && (section->vma + SECTION_SIZE (section, segment) \
4613 <= (SEGMENT_END (segment, segment->p_vaddr))))
4615 /* Returns TRUE if the given section is contained within
4616 the given segment. LMA addresses are compared. */
4617 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4618 (section->lma >= base \
4619 && (section->lma + SECTION_SIZE (section, segment) \
4620 <= SEGMENT_END (segment, base)))
4622 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4623 #define IS_COREFILE_NOTE(p, s) \
4624 (p->p_type == PT_NOTE \
4625 && bfd_get_format (ibfd) == bfd_core \
4626 && s->vma == 0 && s->lma == 0 \
4627 && (bfd_vma) s->filepos >= p->p_offset \
4628 && ((bfd_vma) s->filepos + s->_raw_size \
4629 <= p->p_offset + p->p_filesz))
4631 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4632 linker, which generates a PT_INTERP section with p_vaddr and
4633 p_memsz set to 0. */
4634 #define IS_SOLARIS_PT_INTERP(p, s) \
4635 (p->p_vaddr == 0 \
4636 && p->p_paddr == 0 \
4637 && p->p_memsz == 0 \
4638 && p->p_filesz > 0 \
4639 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4640 && s->_raw_size > 0 \
4641 && (bfd_vma) s->filepos >= p->p_offset \
4642 && ((bfd_vma) s->filepos + s->_raw_size \
4643 <= p->p_offset + p->p_filesz))
4645 /* Decide if the given section should be included in the given segment.
4646 A section will be included if:
4647 1. It is within the address space of the segment -- we use the LMA
4648 if that is set for the segment and the VMA otherwise,
4649 2. It is an allocated segment,
4650 3. There is an output section associated with it,
4651 4. The section has not already been allocated to a previous segment.
4652 5. PT_TLS segment includes only SHF_TLS sections.
4653 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4654 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4655 ((((segment->p_paddr \
4656 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4657 : IS_CONTAINED_BY_VMA (section, segment)) \
4658 && (section->flags & SEC_ALLOC) != 0) \
4659 || IS_COREFILE_NOTE (segment, section)) \
4660 && section->output_section != NULL \
4661 && (segment->p_type != PT_TLS \
4662 || (section->flags & SEC_THREAD_LOCAL)) \
4663 && (segment->p_type == PT_LOAD \
4664 || segment->p_type == PT_TLS \
4665 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4666 && ! section->segment_mark)
4668 /* Returns TRUE iff seg1 starts after the end of seg2. */
4669 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4670 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4672 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4673 their VMA address ranges and their LMA address ranges overlap.
4674 It is possible to have overlapping VMA ranges without overlapping LMA
4675 ranges. RedBoot images for example can have both .data and .bss mapped
4676 to the same VMA range, but with the .data section mapped to a different
4677 LMA. */
4678 #define SEGMENT_OVERLAPS(seg1, seg2) \
4679 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4680 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4681 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4682 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4684 /* Initialise the segment mark field. */
4685 for (section = ibfd->sections; section != NULL; section = section->next)
4686 section->segment_mark = FALSE;
4688 /* Scan through the segments specified in the program header
4689 of the input BFD. For this first scan we look for overlaps
4690 in the loadable segments. These can be created by weird
4691 parameters to objcopy. Also, fix some solaris weirdness. */
4692 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4693 i < num_segments;
4694 i++, segment++)
4696 unsigned int j;
4697 Elf_Internal_Phdr *segment2;
4699 if (segment->p_type == PT_INTERP)
4700 for (section = ibfd->sections; section; section = section->next)
4701 if (IS_SOLARIS_PT_INTERP (segment, section))
4703 /* Mininal change so that the normal section to segment
4704 assigment code will work. */
4705 segment->p_vaddr = section->vma;
4706 break;
4709 if (segment->p_type != PT_LOAD)
4710 continue;
4712 /* Determine if this segment overlaps any previous segments. */
4713 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4715 bfd_signed_vma extra_length;
4717 if (segment2->p_type != PT_LOAD
4718 || ! SEGMENT_OVERLAPS (segment, segment2))
4719 continue;
4721 /* Merge the two segments together. */
4722 if (segment2->p_vaddr < segment->p_vaddr)
4724 /* Extend SEGMENT2 to include SEGMENT and then delete
4725 SEGMENT. */
4726 extra_length =
4727 SEGMENT_END (segment, segment->p_vaddr)
4728 - SEGMENT_END (segment2, segment2->p_vaddr);
4730 if (extra_length > 0)
4732 segment2->p_memsz += extra_length;
4733 segment2->p_filesz += extra_length;
4736 segment->p_type = PT_NULL;
4738 /* Since we have deleted P we must restart the outer loop. */
4739 i = 0;
4740 segment = elf_tdata (ibfd)->phdr;
4741 break;
4743 else
4745 /* Extend SEGMENT to include SEGMENT2 and then delete
4746 SEGMENT2. */
4747 extra_length =
4748 SEGMENT_END (segment2, segment2->p_vaddr)
4749 - SEGMENT_END (segment, segment->p_vaddr);
4751 if (extra_length > 0)
4753 segment->p_memsz += extra_length;
4754 segment->p_filesz += extra_length;
4757 segment2->p_type = PT_NULL;
4762 /* The second scan attempts to assign sections to segments. */
4763 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4764 i < num_segments;
4765 i ++, segment ++)
4767 unsigned int section_count;
4768 asection ** sections;
4769 asection * output_section;
4770 unsigned int isec;
4771 bfd_vma matching_lma;
4772 bfd_vma suggested_lma;
4773 unsigned int j;
4774 bfd_size_type amt;
4776 if (segment->p_type == PT_NULL)
4777 continue;
4779 /* Compute how many sections might be placed into this segment. */
4780 for (section = ibfd->sections, section_count = 0;
4781 section != NULL;
4782 section = section->next)
4783 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4784 ++section_count;
4786 /* Allocate a segment map big enough to contain
4787 all of the sections we have selected. */
4788 amt = sizeof (struct elf_segment_map);
4789 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4790 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4791 if (map == NULL)
4792 return FALSE;
4794 /* Initialise the fields of the segment map. Default to
4795 using the physical address of the segment in the input BFD. */
4796 map->next = NULL;
4797 map->p_type = segment->p_type;
4798 map->p_flags = segment->p_flags;
4799 map->p_flags_valid = 1;
4800 map->p_paddr = segment->p_paddr;
4801 map->p_paddr_valid = 1;
4803 /* Determine if this segment contains the ELF file header
4804 and if it contains the program headers themselves. */
4805 map->includes_filehdr = (segment->p_offset == 0
4806 && segment->p_filesz >= iehdr->e_ehsize);
4808 map->includes_phdrs = 0;
4810 if (! phdr_included || segment->p_type != PT_LOAD)
4812 map->includes_phdrs =
4813 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4814 && (segment->p_offset + segment->p_filesz
4815 >= ((bfd_vma) iehdr->e_phoff
4816 + iehdr->e_phnum * iehdr->e_phentsize)));
4818 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4819 phdr_included = TRUE;
4822 if (section_count == 0)
4824 /* Special segments, such as the PT_PHDR segment, may contain
4825 no sections, but ordinary, loadable segments should contain
4826 something. They are allowed by the ELF spec however, so only
4827 a warning is produced. */
4828 if (segment->p_type == PT_LOAD)
4829 (*_bfd_error_handler)
4830 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4831 bfd_archive_filename (ibfd));
4833 map->count = 0;
4834 *pointer_to_map = map;
4835 pointer_to_map = &map->next;
4837 continue;
4840 /* Now scan the sections in the input BFD again and attempt
4841 to add their corresponding output sections to the segment map.
4842 The problem here is how to handle an output section which has
4843 been moved (ie had its LMA changed). There are four possibilities:
4845 1. None of the sections have been moved.
4846 In this case we can continue to use the segment LMA from the
4847 input BFD.
4849 2. All of the sections have been moved by the same amount.
4850 In this case we can change the segment's LMA to match the LMA
4851 of the first section.
4853 3. Some of the sections have been moved, others have not.
4854 In this case those sections which have not been moved can be
4855 placed in the current segment which will have to have its size,
4856 and possibly its LMA changed, and a new segment or segments will
4857 have to be created to contain the other sections.
4859 4. The sections have been moved, but not by the same amount.
4860 In this case we can change the segment's LMA to match the LMA
4861 of the first section and we will have to create a new segment
4862 or segments to contain the other sections.
4864 In order to save time, we allocate an array to hold the section
4865 pointers that we are interested in. As these sections get assigned
4866 to a segment, they are removed from this array. */
4868 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4869 to work around this long long bug. */
4870 amt = section_count * sizeof (asection *);
4871 sections = (asection **) bfd_malloc (amt);
4872 if (sections == NULL)
4873 return FALSE;
4875 /* Step One: Scan for segment vs section LMA conflicts.
4876 Also add the sections to the section array allocated above.
4877 Also add the sections to the current segment. In the common
4878 case, where the sections have not been moved, this means that
4879 we have completely filled the segment, and there is nothing
4880 more to do. */
4881 isec = 0;
4882 matching_lma = 0;
4883 suggested_lma = 0;
4885 for (j = 0, section = ibfd->sections;
4886 section != NULL;
4887 section = section->next)
4889 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4891 output_section = section->output_section;
4893 sections[j ++] = section;
4895 /* The Solaris native linker always sets p_paddr to 0.
4896 We try to catch that case here, and set it to the
4897 correct value. Note - some backends require that
4898 p_paddr be left as zero. */
4899 if (segment->p_paddr == 0
4900 && segment->p_vaddr != 0
4901 && (! bed->want_p_paddr_set_to_zero)
4902 && isec == 0
4903 && output_section->lma != 0
4904 && (output_section->vma == (segment->p_vaddr
4905 + (map->includes_filehdr
4906 ? iehdr->e_ehsize
4907 : 0)
4908 + (map->includes_phdrs
4909 ? (iehdr->e_phnum
4910 * iehdr->e_phentsize)
4911 : 0))))
4912 map->p_paddr = segment->p_vaddr;
4914 /* Match up the physical address of the segment with the
4915 LMA address of the output section. */
4916 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4917 || IS_COREFILE_NOTE (segment, section)
4918 || (bed->want_p_paddr_set_to_zero &&
4919 IS_CONTAINED_BY_VMA (output_section, segment))
4922 if (matching_lma == 0)
4923 matching_lma = output_section->lma;
4925 /* We assume that if the section fits within the segment
4926 then it does not overlap any other section within that
4927 segment. */
4928 map->sections[isec ++] = output_section;
4930 else if (suggested_lma == 0)
4931 suggested_lma = output_section->lma;
4935 BFD_ASSERT (j == section_count);
4937 /* Step Two: Adjust the physical address of the current segment,
4938 if necessary. */
4939 if (isec == section_count)
4941 /* All of the sections fitted within the segment as currently
4942 specified. This is the default case. Add the segment to
4943 the list of built segments and carry on to process the next
4944 program header in the input BFD. */
4945 map->count = section_count;
4946 *pointer_to_map = map;
4947 pointer_to_map = &map->next;
4949 free (sections);
4950 continue;
4952 else
4954 if (matching_lma != 0)
4956 /* At least one section fits inside the current segment.
4957 Keep it, but modify its physical address to match the
4958 LMA of the first section that fitted. */
4959 map->p_paddr = matching_lma;
4961 else
4963 /* None of the sections fitted inside the current segment.
4964 Change the current segment's physical address to match
4965 the LMA of the first section. */
4966 map->p_paddr = suggested_lma;
4969 /* Offset the segment physical address from the lma
4970 to allow for space taken up by elf headers. */
4971 if (map->includes_filehdr)
4972 map->p_paddr -= iehdr->e_ehsize;
4974 if (map->includes_phdrs)
4976 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4978 /* iehdr->e_phnum is just an estimate of the number
4979 of program headers that we will need. Make a note
4980 here of the number we used and the segment we chose
4981 to hold these headers, so that we can adjust the
4982 offset when we know the correct value. */
4983 phdr_adjust_num = iehdr->e_phnum;
4984 phdr_adjust_seg = map;
4988 /* Step Three: Loop over the sections again, this time assigning
4989 those that fit to the current segment and removing them from the
4990 sections array; but making sure not to leave large gaps. Once all
4991 possible sections have been assigned to the current segment it is
4992 added to the list of built segments and if sections still remain
4993 to be assigned, a new segment is constructed before repeating
4994 the loop. */
4995 isec = 0;
4998 map->count = 0;
4999 suggested_lma = 0;
5001 /* Fill the current segment with sections that fit. */
5002 for (j = 0; j < section_count; j++)
5004 section = sections[j];
5006 if (section == NULL)
5007 continue;
5009 output_section = section->output_section;
5011 BFD_ASSERT (output_section != NULL);
5013 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5014 || IS_COREFILE_NOTE (segment, section))
5016 if (map->count == 0)
5018 /* If the first section in a segment does not start at
5019 the beginning of the segment, then something is
5020 wrong. */
5021 if (output_section->lma !=
5022 (map->p_paddr
5023 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5024 + (map->includes_phdrs
5025 ? iehdr->e_phnum * iehdr->e_phentsize
5026 : 0)))
5027 abort ();
5029 else
5031 asection * prev_sec;
5033 prev_sec = map->sections[map->count - 1];
5035 /* If the gap between the end of the previous section
5036 and the start of this section is more than
5037 maxpagesize then we need to start a new segment. */
5038 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5039 maxpagesize)
5040 < BFD_ALIGN (output_section->lma, maxpagesize))
5041 || ((prev_sec->lma + prev_sec->_raw_size)
5042 > output_section->lma))
5044 if (suggested_lma == 0)
5045 suggested_lma = output_section->lma;
5047 continue;
5051 map->sections[map->count++] = output_section;
5052 ++isec;
5053 sections[j] = NULL;
5054 section->segment_mark = TRUE;
5056 else if (suggested_lma == 0)
5057 suggested_lma = output_section->lma;
5060 BFD_ASSERT (map->count > 0);
5062 /* Add the current segment to the list of built segments. */
5063 *pointer_to_map = map;
5064 pointer_to_map = &map->next;
5066 if (isec < section_count)
5068 /* We still have not allocated all of the sections to
5069 segments. Create a new segment here, initialise it
5070 and carry on looping. */
5071 amt = sizeof (struct elf_segment_map);
5072 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5073 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5074 if (map == NULL)
5076 free (sections);
5077 return FALSE;
5080 /* Initialise the fields of the segment map. Set the physical
5081 physical address to the LMA of the first section that has
5082 not yet been assigned. */
5083 map->next = NULL;
5084 map->p_type = segment->p_type;
5085 map->p_flags = segment->p_flags;
5086 map->p_flags_valid = 1;
5087 map->p_paddr = suggested_lma;
5088 map->p_paddr_valid = 1;
5089 map->includes_filehdr = 0;
5090 map->includes_phdrs = 0;
5093 while (isec < section_count);
5095 free (sections);
5098 /* The Solaris linker creates program headers in which all the
5099 p_paddr fields are zero. When we try to objcopy or strip such a
5100 file, we get confused. Check for this case, and if we find it
5101 reset the p_paddr_valid fields. */
5102 for (map = map_first; map != NULL; map = map->next)
5103 if (map->p_paddr != 0)
5104 break;
5105 if (map == NULL)
5106 for (map = map_first; map != NULL; map = map->next)
5107 map->p_paddr_valid = 0;
5109 elf_tdata (obfd)->segment_map = map_first;
5111 /* If we had to estimate the number of program headers that were
5112 going to be needed, then check our estimate now and adjust
5113 the offset if necessary. */
5114 if (phdr_adjust_seg != NULL)
5116 unsigned int count;
5118 for (count = 0, map = map_first; map != NULL; map = map->next)
5119 count++;
5121 if (count > phdr_adjust_num)
5122 phdr_adjust_seg->p_paddr
5123 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5126 #if 0
5127 /* Final Step: Sort the segments into ascending order of physical
5128 address. */
5129 if (map_first != NULL)
5131 struct elf_segment_map *prev;
5133 prev = map_first;
5134 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5136 /* Yes I know - its a bubble sort.... */
5137 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5139 /* Swap map and map->next. */
5140 prev->next = map->next;
5141 map->next = map->next->next;
5142 prev->next->next = map;
5144 /* Restart loop. */
5145 map = map_first;
5149 #endif
5151 #undef SEGMENT_END
5152 #undef SECTION_SIZE
5153 #undef IS_CONTAINED_BY_VMA
5154 #undef IS_CONTAINED_BY_LMA
5155 #undef IS_COREFILE_NOTE
5156 #undef IS_SOLARIS_PT_INTERP
5157 #undef INCLUDE_SECTION_IN_SEGMENT
5158 #undef SEGMENT_AFTER_SEGMENT
5159 #undef SEGMENT_OVERLAPS
5160 return TRUE;
5163 /* Copy private section information. This copies over the entsize
5164 field, and sometimes the info field. */
5166 bfd_boolean
5167 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5168 bfd *ibfd;
5169 asection *isec;
5170 bfd *obfd;
5171 asection *osec;
5173 Elf_Internal_Shdr *ihdr, *ohdr;
5175 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5176 || obfd->xvec->flavour != bfd_target_elf_flavour)
5177 return TRUE;
5179 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5181 asection *s;
5183 /* Only set up the segments if there are no more SEC_ALLOC
5184 sections. FIXME: This won't do the right thing if objcopy is
5185 used to remove the last SEC_ALLOC section, since objcopy
5186 won't call this routine in that case. */
5187 for (s = isec->next; s != NULL; s = s->next)
5188 if ((s->flags & SEC_ALLOC) != 0)
5189 break;
5190 if (s == NULL)
5192 if (! copy_private_bfd_data (ibfd, obfd))
5193 return FALSE;
5197 ihdr = &elf_section_data (isec)->this_hdr;
5198 ohdr = &elf_section_data (osec)->this_hdr;
5200 ohdr->sh_entsize = ihdr->sh_entsize;
5202 if (ihdr->sh_type == SHT_SYMTAB
5203 || ihdr->sh_type == SHT_DYNSYM
5204 || ihdr->sh_type == SHT_GNU_verneed
5205 || ihdr->sh_type == SHT_GNU_verdef)
5206 ohdr->sh_info = ihdr->sh_info;
5208 /* Set things up for objcopy. The output SHT_GROUP section will
5209 have its elf_next_in_group pointing back to the input group
5210 members. */
5211 elf_next_in_group (osec) = elf_next_in_group (isec);
5212 elf_group_name (osec) = elf_group_name (isec);
5214 osec->use_rela_p = isec->use_rela_p;
5216 return TRUE;
5219 /* Copy private symbol information. If this symbol is in a section
5220 which we did not map into a BFD section, try to map the section
5221 index correctly. We use special macro definitions for the mapped
5222 section indices; these definitions are interpreted by the
5223 swap_out_syms function. */
5225 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5226 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5227 #define MAP_STRTAB (SHN_HIOS + 3)
5228 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5229 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5231 bfd_boolean
5232 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5233 bfd *ibfd;
5234 asymbol *isymarg;
5235 bfd *obfd;
5236 asymbol *osymarg;
5238 elf_symbol_type *isym, *osym;
5240 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5241 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5242 return TRUE;
5244 isym = elf_symbol_from (ibfd, isymarg);
5245 osym = elf_symbol_from (obfd, osymarg);
5247 if (isym != NULL
5248 && osym != NULL
5249 && bfd_is_abs_section (isym->symbol.section))
5251 unsigned int shndx;
5253 shndx = isym->internal_elf_sym.st_shndx;
5254 if (shndx == elf_onesymtab (ibfd))
5255 shndx = MAP_ONESYMTAB;
5256 else if (shndx == elf_dynsymtab (ibfd))
5257 shndx = MAP_DYNSYMTAB;
5258 else if (shndx == elf_tdata (ibfd)->strtab_section)
5259 shndx = MAP_STRTAB;
5260 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5261 shndx = MAP_SHSTRTAB;
5262 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5263 shndx = MAP_SYM_SHNDX;
5264 osym->internal_elf_sym.st_shndx = shndx;
5267 return TRUE;
5270 /* Swap out the symbols. */
5272 static bfd_boolean
5273 swap_out_syms (abfd, sttp, relocatable_p)
5274 bfd *abfd;
5275 struct bfd_strtab_hash **sttp;
5276 int relocatable_p;
5278 struct elf_backend_data *bed;
5279 int symcount;
5280 asymbol **syms;
5281 struct bfd_strtab_hash *stt;
5282 Elf_Internal_Shdr *symtab_hdr;
5283 Elf_Internal_Shdr *symtab_shndx_hdr;
5284 Elf_Internal_Shdr *symstrtab_hdr;
5285 char *outbound_syms;
5286 char *outbound_shndx;
5287 int idx;
5288 bfd_size_type amt;
5290 if (!elf_map_symbols (abfd))
5291 return FALSE;
5293 /* Dump out the symtabs. */
5294 stt = _bfd_elf_stringtab_init ();
5295 if (stt == NULL)
5296 return FALSE;
5298 bed = get_elf_backend_data (abfd);
5299 symcount = bfd_get_symcount (abfd);
5300 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5301 symtab_hdr->sh_type = SHT_SYMTAB;
5302 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5303 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5304 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5305 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5307 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5308 symstrtab_hdr->sh_type = SHT_STRTAB;
5310 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5311 outbound_syms = bfd_alloc (abfd, amt);
5312 if (outbound_syms == NULL)
5314 _bfd_stringtab_free (stt);
5315 return FALSE;
5317 symtab_hdr->contents = (PTR) outbound_syms;
5319 outbound_shndx = NULL;
5320 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5321 if (symtab_shndx_hdr->sh_name != 0)
5323 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5324 outbound_shndx = bfd_zalloc (abfd, amt);
5325 if (outbound_shndx == NULL)
5327 _bfd_stringtab_free (stt);
5328 return FALSE;
5331 symtab_shndx_hdr->contents = outbound_shndx;
5332 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5333 symtab_shndx_hdr->sh_size = amt;
5334 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5335 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5338 /* Now generate the data (for "contents"). */
5340 /* Fill in zeroth symbol and swap it out. */
5341 Elf_Internal_Sym sym;
5342 sym.st_name = 0;
5343 sym.st_value = 0;
5344 sym.st_size = 0;
5345 sym.st_info = 0;
5346 sym.st_other = 0;
5347 sym.st_shndx = SHN_UNDEF;
5348 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5349 outbound_syms += bed->s->sizeof_sym;
5350 if (outbound_shndx != NULL)
5351 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5354 syms = bfd_get_outsymbols (abfd);
5355 for (idx = 0; idx < symcount; idx++)
5357 Elf_Internal_Sym sym;
5358 bfd_vma value = syms[idx]->value;
5359 elf_symbol_type *type_ptr;
5360 flagword flags = syms[idx]->flags;
5361 int type;
5363 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5365 /* Local section symbols have no name. */
5366 sym.st_name = 0;
5368 else
5370 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5371 syms[idx]->name,
5372 TRUE, FALSE);
5373 if (sym.st_name == (unsigned long) -1)
5375 _bfd_stringtab_free (stt);
5376 return FALSE;
5380 type_ptr = elf_symbol_from (abfd, syms[idx]);
5382 if ((flags & BSF_SECTION_SYM) == 0
5383 && bfd_is_com_section (syms[idx]->section))
5385 /* ELF common symbols put the alignment into the `value' field,
5386 and the size into the `size' field. This is backwards from
5387 how BFD handles it, so reverse it here. */
5388 sym.st_size = value;
5389 if (type_ptr == NULL
5390 || type_ptr->internal_elf_sym.st_value == 0)
5391 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5392 else
5393 sym.st_value = type_ptr->internal_elf_sym.st_value;
5394 sym.st_shndx = _bfd_elf_section_from_bfd_section
5395 (abfd, syms[idx]->section);
5397 else
5399 asection *sec = syms[idx]->section;
5400 int shndx;
5402 if (sec->output_section)
5404 value += sec->output_offset;
5405 sec = sec->output_section;
5408 /* Don't add in the section vma for relocatable output. */
5409 if (! relocatable_p)
5410 value += sec->vma;
5411 sym.st_value = value;
5412 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5414 if (bfd_is_abs_section (sec)
5415 && type_ptr != NULL
5416 && type_ptr->internal_elf_sym.st_shndx != 0)
5418 /* This symbol is in a real ELF section which we did
5419 not create as a BFD section. Undo the mapping done
5420 by copy_private_symbol_data. */
5421 shndx = type_ptr->internal_elf_sym.st_shndx;
5422 switch (shndx)
5424 case MAP_ONESYMTAB:
5425 shndx = elf_onesymtab (abfd);
5426 break;
5427 case MAP_DYNSYMTAB:
5428 shndx = elf_dynsymtab (abfd);
5429 break;
5430 case MAP_STRTAB:
5431 shndx = elf_tdata (abfd)->strtab_section;
5432 break;
5433 case MAP_SHSTRTAB:
5434 shndx = elf_tdata (abfd)->shstrtab_section;
5435 break;
5436 case MAP_SYM_SHNDX:
5437 shndx = elf_tdata (abfd)->symtab_shndx_section;
5438 break;
5439 default:
5440 break;
5443 else
5445 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5447 if (shndx == -1)
5449 asection *sec2;
5451 /* Writing this would be a hell of a lot easier if
5452 we had some decent documentation on bfd, and
5453 knew what to expect of the library, and what to
5454 demand of applications. For example, it
5455 appears that `objcopy' might not set the
5456 section of a symbol to be a section that is
5457 actually in the output file. */
5458 sec2 = bfd_get_section_by_name (abfd, sec->name);
5459 if (sec2 == NULL)
5461 _bfd_error_handler (_("\
5462 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5463 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5464 sec->name);
5465 bfd_set_error (bfd_error_invalid_operation);
5466 _bfd_stringtab_free (stt);
5467 return FALSE;
5470 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5471 BFD_ASSERT (shndx != -1);
5475 sym.st_shndx = shndx;
5478 if ((flags & BSF_THREAD_LOCAL) != 0)
5479 type = STT_TLS;
5480 else if ((flags & BSF_FUNCTION) != 0)
5481 type = STT_FUNC;
5482 else if ((flags & BSF_OBJECT) != 0)
5483 type = STT_OBJECT;
5484 else
5485 type = STT_NOTYPE;
5487 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5488 type = STT_TLS;
5490 /* Processor-specific types. */
5491 if (type_ptr != NULL
5492 && bed->elf_backend_get_symbol_type)
5493 type = ((*bed->elf_backend_get_symbol_type)
5494 (&type_ptr->internal_elf_sym, type));
5496 if (flags & BSF_SECTION_SYM)
5498 if (flags & BSF_GLOBAL)
5499 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5500 else
5501 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5503 else if (bfd_is_com_section (syms[idx]->section))
5504 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5505 else if (bfd_is_und_section (syms[idx]->section))
5506 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5507 ? STB_WEAK
5508 : STB_GLOBAL),
5509 type);
5510 else if (flags & BSF_FILE)
5511 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5512 else
5514 int bind = STB_LOCAL;
5516 if (flags & BSF_LOCAL)
5517 bind = STB_LOCAL;
5518 else if (flags & BSF_WEAK)
5519 bind = STB_WEAK;
5520 else if (flags & BSF_GLOBAL)
5521 bind = STB_GLOBAL;
5523 sym.st_info = ELF_ST_INFO (bind, type);
5526 if (type_ptr != NULL)
5527 sym.st_other = type_ptr->internal_elf_sym.st_other;
5528 else
5529 sym.st_other = 0;
5531 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5532 outbound_syms += bed->s->sizeof_sym;
5533 if (outbound_shndx != NULL)
5534 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5537 *sttp = stt;
5538 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5539 symstrtab_hdr->sh_type = SHT_STRTAB;
5541 symstrtab_hdr->sh_flags = 0;
5542 symstrtab_hdr->sh_addr = 0;
5543 symstrtab_hdr->sh_entsize = 0;
5544 symstrtab_hdr->sh_link = 0;
5545 symstrtab_hdr->sh_info = 0;
5546 symstrtab_hdr->sh_addralign = 1;
5548 return TRUE;
5551 /* Return the number of bytes required to hold the symtab vector.
5553 Note that we base it on the count plus 1, since we will null terminate
5554 the vector allocated based on this size. However, the ELF symbol table
5555 always has a dummy entry as symbol #0, so it ends up even. */
5557 long
5558 _bfd_elf_get_symtab_upper_bound (abfd)
5559 bfd *abfd;
5561 long symcount;
5562 long symtab_size;
5563 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5565 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5566 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5567 if (symcount > 0)
5568 symtab_size -= sizeof (asymbol *);
5570 return symtab_size;
5573 long
5574 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5575 bfd *abfd;
5577 long symcount;
5578 long symtab_size;
5579 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5581 if (elf_dynsymtab (abfd) == 0)
5583 bfd_set_error (bfd_error_invalid_operation);
5584 return -1;
5587 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5588 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5589 if (symcount > 0)
5590 symtab_size -= sizeof (asymbol *);
5592 return symtab_size;
5595 long
5596 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5597 bfd *abfd ATTRIBUTE_UNUSED;
5598 sec_ptr asect;
5600 return (asect->reloc_count + 1) * sizeof (arelent *);
5603 /* Canonicalize the relocs. */
5605 long
5606 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5607 bfd *abfd;
5608 sec_ptr section;
5609 arelent **relptr;
5610 asymbol **symbols;
5612 arelent *tblptr;
5613 unsigned int i;
5614 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5616 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5617 return -1;
5619 tblptr = section->relocation;
5620 for (i = 0; i < section->reloc_count; i++)
5621 *relptr++ = tblptr++;
5623 *relptr = NULL;
5625 return section->reloc_count;
5628 long
5629 _bfd_elf_get_symtab (abfd, alocation)
5630 bfd *abfd;
5631 asymbol **alocation;
5633 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5634 long symcount = bed->s->slurp_symbol_table (abfd, alocation, FALSE);
5636 if (symcount >= 0)
5637 bfd_get_symcount (abfd) = symcount;
5638 return symcount;
5641 long
5642 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5643 bfd *abfd;
5644 asymbol **alocation;
5646 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5647 long symcount = bed->s->slurp_symbol_table (abfd, alocation, TRUE);
5649 if (symcount >= 0)
5650 bfd_get_dynamic_symcount (abfd) = symcount;
5651 return symcount;
5654 /* Return the size required for the dynamic reloc entries. Any
5655 section that was actually installed in the BFD, and has type
5656 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5657 considered to be a dynamic reloc section. */
5659 long
5660 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5661 bfd *abfd;
5663 long ret;
5664 asection *s;
5666 if (elf_dynsymtab (abfd) == 0)
5668 bfd_set_error (bfd_error_invalid_operation);
5669 return -1;
5672 ret = sizeof (arelent *);
5673 for (s = abfd->sections; s != NULL; s = s->next)
5674 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5675 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5676 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5677 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5678 * sizeof (arelent *));
5680 return ret;
5683 /* Canonicalize the dynamic relocation entries. Note that we return
5684 the dynamic relocations as a single block, although they are
5685 actually associated with particular sections; the interface, which
5686 was designed for SunOS style shared libraries, expects that there
5687 is only one set of dynamic relocs. Any section that was actually
5688 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5689 the dynamic symbol table, is considered to be a dynamic reloc
5690 section. */
5692 long
5693 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5694 bfd *abfd;
5695 arelent **storage;
5696 asymbol **syms;
5698 bfd_boolean (*slurp_relocs)
5699 PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
5700 asection *s;
5701 long ret;
5703 if (elf_dynsymtab (abfd) == 0)
5705 bfd_set_error (bfd_error_invalid_operation);
5706 return -1;
5709 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5710 ret = 0;
5711 for (s = abfd->sections; s != NULL; s = s->next)
5713 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5714 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5715 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5717 arelent *p;
5718 long count, i;
5720 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5721 return -1;
5722 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5723 p = s->relocation;
5724 for (i = 0; i < count; i++)
5725 *storage++ = p++;
5726 ret += count;
5730 *storage = NULL;
5732 return ret;
5735 /* Read in the version information. */
5737 bfd_boolean
5738 _bfd_elf_slurp_version_tables (abfd)
5739 bfd *abfd;
5741 bfd_byte *contents = NULL;
5742 bfd_size_type amt;
5744 if (elf_dynverdef (abfd) != 0)
5746 Elf_Internal_Shdr *hdr;
5747 Elf_External_Verdef *everdef;
5748 Elf_Internal_Verdef *iverdef;
5749 Elf_Internal_Verdef *iverdefarr;
5750 Elf_Internal_Verdef iverdefmem;
5751 unsigned int i;
5752 unsigned int maxidx;
5754 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5756 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5757 if (contents == NULL)
5758 goto error_return;
5759 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5760 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5761 goto error_return;
5763 /* We know the number of entries in the section but not the maximum
5764 index. Therefore we have to run through all entries and find
5765 the maximum. */
5766 everdef = (Elf_External_Verdef *) contents;
5767 maxidx = 0;
5768 for (i = 0; i < hdr->sh_info; ++i)
5770 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5772 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5773 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5775 everdef = ((Elf_External_Verdef *)
5776 ((bfd_byte *) everdef + iverdefmem.vd_next));
5779 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5780 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5781 if (elf_tdata (abfd)->verdef == NULL)
5782 goto error_return;
5784 elf_tdata (abfd)->cverdefs = maxidx;
5786 everdef = (Elf_External_Verdef *) contents;
5787 iverdefarr = elf_tdata (abfd)->verdef;
5788 for (i = 0; i < hdr->sh_info; i++)
5790 Elf_External_Verdaux *everdaux;
5791 Elf_Internal_Verdaux *iverdaux;
5792 unsigned int j;
5794 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5796 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5797 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5799 iverdef->vd_bfd = abfd;
5801 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5802 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5803 if (iverdef->vd_auxptr == NULL)
5804 goto error_return;
5806 everdaux = ((Elf_External_Verdaux *)
5807 ((bfd_byte *) everdef + iverdef->vd_aux));
5808 iverdaux = iverdef->vd_auxptr;
5809 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5811 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5813 iverdaux->vda_nodename =
5814 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5815 iverdaux->vda_name);
5816 if (iverdaux->vda_nodename == NULL)
5817 goto error_return;
5819 if (j + 1 < iverdef->vd_cnt)
5820 iverdaux->vda_nextptr = iverdaux + 1;
5821 else
5822 iverdaux->vda_nextptr = NULL;
5824 everdaux = ((Elf_External_Verdaux *)
5825 ((bfd_byte *) everdaux + iverdaux->vda_next));
5828 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5830 if (i + 1 < hdr->sh_info)
5831 iverdef->vd_nextdef = iverdef + 1;
5832 else
5833 iverdef->vd_nextdef = NULL;
5835 everdef = ((Elf_External_Verdef *)
5836 ((bfd_byte *) everdef + iverdef->vd_next));
5839 free (contents);
5840 contents = NULL;
5843 if (elf_dynverref (abfd) != 0)
5845 Elf_Internal_Shdr *hdr;
5846 Elf_External_Verneed *everneed;
5847 Elf_Internal_Verneed *iverneed;
5848 unsigned int i;
5850 hdr = &elf_tdata (abfd)->dynverref_hdr;
5852 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5853 elf_tdata (abfd)->verref =
5854 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5855 if (elf_tdata (abfd)->verref == NULL)
5856 goto error_return;
5858 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5860 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5861 if (contents == NULL)
5862 goto error_return;
5863 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5864 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5865 goto error_return;
5867 everneed = (Elf_External_Verneed *) contents;
5868 iverneed = elf_tdata (abfd)->verref;
5869 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5871 Elf_External_Vernaux *evernaux;
5872 Elf_Internal_Vernaux *ivernaux;
5873 unsigned int j;
5875 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5877 iverneed->vn_bfd = abfd;
5879 iverneed->vn_filename =
5880 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5881 iverneed->vn_file);
5882 if (iverneed->vn_filename == NULL)
5883 goto error_return;
5885 amt = iverneed->vn_cnt;
5886 amt *= sizeof (Elf_Internal_Vernaux);
5887 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5889 evernaux = ((Elf_External_Vernaux *)
5890 ((bfd_byte *) everneed + iverneed->vn_aux));
5891 ivernaux = iverneed->vn_auxptr;
5892 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5894 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5896 ivernaux->vna_nodename =
5897 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5898 ivernaux->vna_name);
5899 if (ivernaux->vna_nodename == NULL)
5900 goto error_return;
5902 if (j + 1 < iverneed->vn_cnt)
5903 ivernaux->vna_nextptr = ivernaux + 1;
5904 else
5905 ivernaux->vna_nextptr = NULL;
5907 evernaux = ((Elf_External_Vernaux *)
5908 ((bfd_byte *) evernaux + ivernaux->vna_next));
5911 if (i + 1 < hdr->sh_info)
5912 iverneed->vn_nextref = iverneed + 1;
5913 else
5914 iverneed->vn_nextref = NULL;
5916 everneed = ((Elf_External_Verneed *)
5917 ((bfd_byte *) everneed + iverneed->vn_next));
5920 free (contents);
5921 contents = NULL;
5924 return TRUE;
5926 error_return:
5927 if (contents != NULL)
5928 free (contents);
5929 return FALSE;
5932 asymbol *
5933 _bfd_elf_make_empty_symbol (abfd)
5934 bfd *abfd;
5936 elf_symbol_type *newsym;
5937 bfd_size_type amt = sizeof (elf_symbol_type);
5939 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5940 if (!newsym)
5941 return NULL;
5942 else
5944 newsym->symbol.the_bfd = abfd;
5945 return &newsym->symbol;
5949 void
5950 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5951 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5952 asymbol *symbol;
5953 symbol_info *ret;
5955 bfd_symbol_info (symbol, ret);
5958 /* Return whether a symbol name implies a local symbol. Most targets
5959 use this function for the is_local_label_name entry point, but some
5960 override it. */
5962 bfd_boolean
5963 _bfd_elf_is_local_label_name (abfd, name)
5964 bfd *abfd ATTRIBUTE_UNUSED;
5965 const char *name;
5967 /* Normal local symbols start with ``.L''. */
5968 if (name[0] == '.' && name[1] == 'L')
5969 return TRUE;
5971 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5972 DWARF debugging symbols starting with ``..''. */
5973 if (name[0] == '.' && name[1] == '.')
5974 return TRUE;
5976 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5977 emitting DWARF debugging output. I suspect this is actually a
5978 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5979 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5980 underscore to be emitted on some ELF targets). For ease of use,
5981 we treat such symbols as local. */
5982 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5983 return TRUE;
5985 return FALSE;
5988 alent *
5989 _bfd_elf_get_lineno (ignore_abfd, symbol)
5990 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5991 asymbol *symbol ATTRIBUTE_UNUSED;
5993 abort ();
5994 return NULL;
5997 bfd_boolean
5998 _bfd_elf_set_arch_mach (abfd, arch, machine)
5999 bfd *abfd;
6000 enum bfd_architecture arch;
6001 unsigned long machine;
6003 /* If this isn't the right architecture for this backend, and this
6004 isn't the generic backend, fail. */
6005 if (arch != get_elf_backend_data (abfd)->arch
6006 && arch != bfd_arch_unknown
6007 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6008 return FALSE;
6010 return bfd_default_set_arch_mach (abfd, arch, machine);
6013 /* Find the function to a particular section and offset,
6014 for error reporting. */
6016 static bfd_boolean
6017 elf_find_function (abfd, section, symbols, offset,
6018 filename_ptr, functionname_ptr)
6019 bfd *abfd ATTRIBUTE_UNUSED;
6020 asection *section;
6021 asymbol **symbols;
6022 bfd_vma offset;
6023 const char **filename_ptr;
6024 const char **functionname_ptr;
6026 const char *filename;
6027 asymbol *func;
6028 bfd_vma low_func;
6029 asymbol **p;
6031 filename = NULL;
6032 func = NULL;
6033 low_func = 0;
6035 for (p = symbols; *p != NULL; p++)
6037 elf_symbol_type *q;
6039 q = (elf_symbol_type *) *p;
6041 if (bfd_get_section (&q->symbol) != section)
6042 continue;
6044 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6046 default:
6047 break;
6048 case STT_FILE:
6049 filename = bfd_asymbol_name (&q->symbol);
6050 break;
6051 case STT_NOTYPE:
6052 case STT_FUNC:
6053 if (q->symbol.section == section
6054 && q->symbol.value >= low_func
6055 && q->symbol.value <= offset)
6057 func = (asymbol *) q;
6058 low_func = q->symbol.value;
6060 break;
6064 if (func == NULL)
6065 return FALSE;
6067 if (filename_ptr)
6068 *filename_ptr = filename;
6069 if (functionname_ptr)
6070 *functionname_ptr = bfd_asymbol_name (func);
6072 return TRUE;
6075 /* Find the nearest line to a particular section and offset,
6076 for error reporting. */
6078 bfd_boolean
6079 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6080 filename_ptr, functionname_ptr, line_ptr)
6081 bfd *abfd;
6082 asection *section;
6083 asymbol **symbols;
6084 bfd_vma offset;
6085 const char **filename_ptr;
6086 const char **functionname_ptr;
6087 unsigned int *line_ptr;
6089 bfd_boolean found;
6091 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6092 filename_ptr, functionname_ptr,
6093 line_ptr))
6095 if (!*functionname_ptr)
6096 elf_find_function (abfd, section, symbols, offset,
6097 *filename_ptr ? NULL : filename_ptr,
6098 functionname_ptr);
6100 return TRUE;
6103 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6104 filename_ptr, functionname_ptr,
6105 line_ptr, 0,
6106 &elf_tdata (abfd)->dwarf2_find_line_info))
6108 if (!*functionname_ptr)
6109 elf_find_function (abfd, section, symbols, offset,
6110 *filename_ptr ? NULL : filename_ptr,
6111 functionname_ptr);
6113 return TRUE;
6116 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6117 &found, filename_ptr,
6118 functionname_ptr, line_ptr,
6119 &elf_tdata (abfd)->line_info))
6120 return FALSE;
6121 if (found && (*functionname_ptr || *line_ptr))
6122 return TRUE;
6124 if (symbols == NULL)
6125 return FALSE;
6127 if (! elf_find_function (abfd, section, symbols, offset,
6128 filename_ptr, functionname_ptr))
6129 return FALSE;
6131 *line_ptr = 0;
6132 return TRUE;
6136 _bfd_elf_sizeof_headers (abfd, reloc)
6137 bfd *abfd;
6138 bfd_boolean reloc;
6140 int ret;
6142 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6143 if (! reloc)
6144 ret += get_program_header_size (abfd);
6145 return ret;
6148 bfd_boolean
6149 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
6150 bfd *abfd;
6151 sec_ptr section;
6152 PTR location;
6153 file_ptr offset;
6154 bfd_size_type count;
6156 Elf_Internal_Shdr *hdr;
6157 bfd_signed_vma pos;
6159 if (! abfd->output_has_begun
6160 && ! (_bfd_elf_compute_section_file_positions
6161 (abfd, (struct bfd_link_info *) NULL)))
6162 return FALSE;
6164 hdr = &elf_section_data (section)->this_hdr;
6165 pos = hdr->sh_offset + offset;
6166 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6167 || bfd_bwrite (location, count, abfd) != count)
6168 return FALSE;
6170 return TRUE;
6173 void
6174 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
6175 bfd *abfd ATTRIBUTE_UNUSED;
6176 arelent *cache_ptr ATTRIBUTE_UNUSED;
6177 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
6179 abort ();
6182 /* Try to convert a non-ELF reloc into an ELF one. */
6184 bfd_boolean
6185 _bfd_elf_validate_reloc (abfd, areloc)
6186 bfd *abfd;
6187 arelent *areloc;
6189 /* Check whether we really have an ELF howto. */
6191 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6193 bfd_reloc_code_real_type code;
6194 reloc_howto_type *howto;
6196 /* Alien reloc: Try to determine its type to replace it with an
6197 equivalent ELF reloc. */
6199 if (areloc->howto->pc_relative)
6201 switch (areloc->howto->bitsize)
6203 case 8:
6204 code = BFD_RELOC_8_PCREL;
6205 break;
6206 case 12:
6207 code = BFD_RELOC_12_PCREL;
6208 break;
6209 case 16:
6210 code = BFD_RELOC_16_PCREL;
6211 break;
6212 case 24:
6213 code = BFD_RELOC_24_PCREL;
6214 break;
6215 case 32:
6216 code = BFD_RELOC_32_PCREL;
6217 break;
6218 case 64:
6219 code = BFD_RELOC_64_PCREL;
6220 break;
6221 default:
6222 goto fail;
6225 howto = bfd_reloc_type_lookup (abfd, code);
6227 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6229 if (howto->pcrel_offset)
6230 areloc->addend += areloc->address;
6231 else
6232 areloc->addend -= areloc->address; /* addend is unsigned!! */
6235 else
6237 switch (areloc->howto->bitsize)
6239 case 8:
6240 code = BFD_RELOC_8;
6241 break;
6242 case 14:
6243 code = BFD_RELOC_14;
6244 break;
6245 case 16:
6246 code = BFD_RELOC_16;
6247 break;
6248 case 26:
6249 code = BFD_RELOC_26;
6250 break;
6251 case 32:
6252 code = BFD_RELOC_32;
6253 break;
6254 case 64:
6255 code = BFD_RELOC_64;
6256 break;
6257 default:
6258 goto fail;
6261 howto = bfd_reloc_type_lookup (abfd, code);
6264 if (howto)
6265 areloc->howto = howto;
6266 else
6267 goto fail;
6270 return TRUE;
6272 fail:
6273 (*_bfd_error_handler)
6274 (_("%s: unsupported relocation type %s"),
6275 bfd_archive_filename (abfd), areloc->howto->name);
6276 bfd_set_error (bfd_error_bad_value);
6277 return FALSE;
6280 bfd_boolean
6281 _bfd_elf_close_and_cleanup (abfd)
6282 bfd *abfd;
6284 if (bfd_get_format (abfd) == bfd_object)
6286 if (elf_shstrtab (abfd) != NULL)
6287 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6290 return _bfd_generic_close_and_cleanup (abfd);
6293 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6294 in the relocation's offset. Thus we cannot allow any sort of sanity
6295 range-checking to interfere. There is nothing else to do in processing
6296 this reloc. */
6298 bfd_reloc_status_type
6299 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
6300 bfd *abfd ATTRIBUTE_UNUSED;
6301 arelent *re ATTRIBUTE_UNUSED;
6302 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6303 PTR data ATTRIBUTE_UNUSED;
6304 asection *is ATTRIBUTE_UNUSED;
6305 bfd *obfd ATTRIBUTE_UNUSED;
6306 char **errmsg ATTRIBUTE_UNUSED;
6308 return bfd_reloc_ok;
6311 /* Elf core file support. Much of this only works on native
6312 toolchains, since we rely on knowing the
6313 machine-dependent procfs structure in order to pick
6314 out details about the corefile. */
6316 #ifdef HAVE_SYS_PROCFS_H
6317 # include <sys/procfs.h>
6318 #endif
6320 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6322 static int
6323 elfcore_make_pid (abfd)
6324 bfd *abfd;
6326 return ((elf_tdata (abfd)->core_lwpid << 16)
6327 + (elf_tdata (abfd)->core_pid));
6330 /* If there isn't a section called NAME, make one, using
6331 data from SECT. Note, this function will generate a
6332 reference to NAME, so you shouldn't deallocate or
6333 overwrite it. */
6335 static bfd_boolean
6336 elfcore_maybe_make_sect (abfd, name, sect)
6337 bfd *abfd;
6338 char *name;
6339 asection *sect;
6341 asection *sect2;
6343 if (bfd_get_section_by_name (abfd, name) != NULL)
6344 return TRUE;
6346 sect2 = bfd_make_section (abfd, name);
6347 if (sect2 == NULL)
6348 return FALSE;
6350 sect2->_raw_size = sect->_raw_size;
6351 sect2->filepos = sect->filepos;
6352 sect2->flags = sect->flags;
6353 sect2->alignment_power = sect->alignment_power;
6354 return TRUE;
6357 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6358 actually creates up to two pseudosections:
6359 - For the single-threaded case, a section named NAME, unless
6360 such a section already exists.
6361 - For the multi-threaded case, a section named "NAME/PID", where
6362 PID is elfcore_make_pid (abfd).
6363 Both pseudosections have identical contents. */
6364 bfd_boolean
6365 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6366 bfd *abfd;
6367 char *name;
6368 size_t size;
6369 ufile_ptr filepos;
6371 char buf[100];
6372 char *threaded_name;
6373 size_t len;
6374 asection *sect;
6376 /* Build the section name. */
6378 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6379 len = strlen (buf) + 1;
6380 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
6381 if (threaded_name == NULL)
6382 return FALSE;
6383 memcpy (threaded_name, buf, len);
6385 sect = bfd_make_section (abfd, threaded_name);
6386 if (sect == NULL)
6387 return FALSE;
6388 sect->_raw_size = size;
6389 sect->filepos = filepos;
6390 sect->flags = SEC_HAS_CONTENTS;
6391 sect->alignment_power = 2;
6393 return elfcore_maybe_make_sect (abfd, name, sect);
6396 /* prstatus_t exists on:
6397 solaris 2.5+
6398 linux 2.[01] + glibc
6399 unixware 4.2
6402 #if defined (HAVE_PRSTATUS_T)
6403 static bfd_boolean elfcore_grok_prstatus
6404 PARAMS ((bfd *, Elf_Internal_Note *));
6406 static bfd_boolean
6407 elfcore_grok_prstatus (abfd, note)
6408 bfd *abfd;
6409 Elf_Internal_Note *note;
6411 size_t raw_size;
6412 int offset;
6414 if (note->descsz == sizeof (prstatus_t))
6416 prstatus_t prstat;
6418 raw_size = sizeof (prstat.pr_reg);
6419 offset = offsetof (prstatus_t, pr_reg);
6420 memcpy (&prstat, note->descdata, sizeof (prstat));
6422 /* Do not overwrite the core signal if it
6423 has already been set by another thread. */
6424 if (elf_tdata (abfd)->core_signal == 0)
6425 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6426 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6428 /* pr_who exists on:
6429 solaris 2.5+
6430 unixware 4.2
6431 pr_who doesn't exist on:
6432 linux 2.[01]
6434 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6435 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6436 #endif
6438 #if defined (HAVE_PRSTATUS32_T)
6439 else if (note->descsz == sizeof (prstatus32_t))
6441 /* 64-bit host, 32-bit corefile */
6442 prstatus32_t prstat;
6444 raw_size = sizeof (prstat.pr_reg);
6445 offset = offsetof (prstatus32_t, pr_reg);
6446 memcpy (&prstat, note->descdata, sizeof (prstat));
6448 /* Do not overwrite the core signal if it
6449 has already been set by another thread. */
6450 if (elf_tdata (abfd)->core_signal == 0)
6451 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6452 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6454 /* pr_who exists on:
6455 solaris 2.5+
6456 unixware 4.2
6457 pr_who doesn't exist on:
6458 linux 2.[01]
6460 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6461 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6462 #endif
6464 #endif /* HAVE_PRSTATUS32_T */
6465 else
6467 /* Fail - we don't know how to handle any other
6468 note size (ie. data object type). */
6469 return TRUE;
6472 /* Make a ".reg/999" section and a ".reg" section. */
6473 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6474 raw_size, note->descpos + offset);
6476 #endif /* defined (HAVE_PRSTATUS_T) */
6478 /* Create a pseudosection containing the exact contents of NOTE. */
6479 static bfd_boolean
6480 elfcore_make_note_pseudosection (abfd, name, note)
6481 bfd *abfd;
6482 char *name;
6483 Elf_Internal_Note *note;
6485 return _bfd_elfcore_make_pseudosection (abfd, name,
6486 note->descsz, note->descpos);
6489 /* There isn't a consistent prfpregset_t across platforms,
6490 but it doesn't matter, because we don't have to pick this
6491 data structure apart. */
6493 static bfd_boolean
6494 elfcore_grok_prfpreg (abfd, note)
6495 bfd *abfd;
6496 Elf_Internal_Note *note;
6498 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6501 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6502 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6503 literally. */
6505 static bfd_boolean
6506 elfcore_grok_prxfpreg (abfd, note)
6507 bfd *abfd;
6508 Elf_Internal_Note *note;
6510 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6513 #if defined (HAVE_PRPSINFO_T)
6514 typedef prpsinfo_t elfcore_psinfo_t;
6515 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6516 typedef prpsinfo32_t elfcore_psinfo32_t;
6517 #endif
6518 #endif
6520 #if defined (HAVE_PSINFO_T)
6521 typedef psinfo_t elfcore_psinfo_t;
6522 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6523 typedef psinfo32_t elfcore_psinfo32_t;
6524 #endif
6525 #endif
6527 /* return a malloc'ed copy of a string at START which is at
6528 most MAX bytes long, possibly without a terminating '\0'.
6529 the copy will always have a terminating '\0'. */
6531 char *
6532 _bfd_elfcore_strndup (abfd, start, max)
6533 bfd *abfd;
6534 char *start;
6535 size_t max;
6537 char *dups;
6538 char *end = memchr (start, '\0', max);
6539 size_t len;
6541 if (end == NULL)
6542 len = max;
6543 else
6544 len = end - start;
6546 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6547 if (dups == NULL)
6548 return NULL;
6550 memcpy (dups, start, len);
6551 dups[len] = '\0';
6553 return dups;
6556 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6557 static bfd_boolean elfcore_grok_psinfo
6558 PARAMS ((bfd *, Elf_Internal_Note *));
6560 static bfd_boolean
6561 elfcore_grok_psinfo (abfd, note)
6562 bfd *abfd;
6563 Elf_Internal_Note *note;
6565 if (note->descsz == sizeof (elfcore_psinfo_t))
6567 elfcore_psinfo_t psinfo;
6569 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6571 elf_tdata (abfd)->core_program
6572 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6573 sizeof (psinfo.pr_fname));
6575 elf_tdata (abfd)->core_command
6576 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6577 sizeof (psinfo.pr_psargs));
6579 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6580 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6582 /* 64-bit host, 32-bit corefile */
6583 elfcore_psinfo32_t psinfo;
6585 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6587 elf_tdata (abfd)->core_program
6588 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6589 sizeof (psinfo.pr_fname));
6591 elf_tdata (abfd)->core_command
6592 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6593 sizeof (psinfo.pr_psargs));
6595 #endif
6597 else
6599 /* Fail - we don't know how to handle any other
6600 note size (ie. data object type). */
6601 return TRUE;
6604 /* Note that for some reason, a spurious space is tacked
6605 onto the end of the args in some (at least one anyway)
6606 implementations, so strip it off if it exists. */
6609 char *command = elf_tdata (abfd)->core_command;
6610 int n = strlen (command);
6612 if (0 < n && command[n - 1] == ' ')
6613 command[n - 1] = '\0';
6616 return TRUE;
6618 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6620 #if defined (HAVE_PSTATUS_T)
6621 static bfd_boolean elfcore_grok_pstatus
6622 PARAMS ((bfd *, Elf_Internal_Note *));
6624 static bfd_boolean
6625 elfcore_grok_pstatus (abfd, note)
6626 bfd *abfd;
6627 Elf_Internal_Note *note;
6629 if (note->descsz == sizeof (pstatus_t)
6630 #if defined (HAVE_PXSTATUS_T)
6631 || note->descsz == sizeof (pxstatus_t)
6632 #endif
6635 pstatus_t pstat;
6637 memcpy (&pstat, note->descdata, sizeof (pstat));
6639 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6641 #if defined (HAVE_PSTATUS32_T)
6642 else if (note->descsz == sizeof (pstatus32_t))
6644 /* 64-bit host, 32-bit corefile */
6645 pstatus32_t pstat;
6647 memcpy (&pstat, note->descdata, sizeof (pstat));
6649 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6651 #endif
6652 /* Could grab some more details from the "representative"
6653 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6654 NT_LWPSTATUS note, presumably. */
6656 return TRUE;
6658 #endif /* defined (HAVE_PSTATUS_T) */
6660 #if defined (HAVE_LWPSTATUS_T)
6661 static bfd_boolean elfcore_grok_lwpstatus
6662 PARAMS ((bfd *, Elf_Internal_Note *));
6664 static bfd_boolean
6665 elfcore_grok_lwpstatus (abfd, note)
6666 bfd *abfd;
6667 Elf_Internal_Note *note;
6669 lwpstatus_t lwpstat;
6670 char buf[100];
6671 char *name;
6672 size_t len;
6673 asection *sect;
6675 if (note->descsz != sizeof (lwpstat)
6676 #if defined (HAVE_LWPXSTATUS_T)
6677 && note->descsz != sizeof (lwpxstatus_t)
6678 #endif
6680 return TRUE;
6682 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6684 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6685 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6687 /* Make a ".reg/999" section. */
6689 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6690 len = strlen (buf) + 1;
6691 name = bfd_alloc (abfd, (bfd_size_type) len);
6692 if (name == NULL)
6693 return FALSE;
6694 memcpy (name, buf, len);
6696 sect = bfd_make_section (abfd, name);
6697 if (sect == NULL)
6698 return FALSE;
6700 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6701 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6702 sect->filepos = note->descpos
6703 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6704 #endif
6706 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6707 sect->_raw_size = sizeof (lwpstat.pr_reg);
6708 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6709 #endif
6711 sect->flags = SEC_HAS_CONTENTS;
6712 sect->alignment_power = 2;
6714 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6715 return FALSE;
6717 /* Make a ".reg2/999" section */
6719 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6720 len = strlen (buf) + 1;
6721 name = bfd_alloc (abfd, (bfd_size_type) len);
6722 if (name == NULL)
6723 return FALSE;
6724 memcpy (name, buf, len);
6726 sect = bfd_make_section (abfd, name);
6727 if (sect == NULL)
6728 return FALSE;
6730 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6731 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6732 sect->filepos = note->descpos
6733 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6734 #endif
6736 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6737 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6738 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6739 #endif
6741 sect->flags = SEC_HAS_CONTENTS;
6742 sect->alignment_power = 2;
6744 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6746 #endif /* defined (HAVE_LWPSTATUS_T) */
6748 #if defined (HAVE_WIN32_PSTATUS_T)
6749 static bfd_boolean
6750 elfcore_grok_win32pstatus (abfd, note)
6751 bfd *abfd;
6752 Elf_Internal_Note *note;
6754 char buf[30];
6755 char *name;
6756 size_t len;
6757 asection *sect;
6758 win32_pstatus_t pstatus;
6760 if (note->descsz < sizeof (pstatus))
6761 return TRUE;
6763 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6765 switch (pstatus.data_type)
6767 case NOTE_INFO_PROCESS:
6768 /* FIXME: need to add ->core_command. */
6769 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6770 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6771 break;
6773 case NOTE_INFO_THREAD:
6774 /* Make a ".reg/999" section. */
6775 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6777 len = strlen (buf) + 1;
6778 name = bfd_alloc (abfd, (bfd_size_type) len);
6779 if (name == NULL)
6780 return FALSE;
6782 memcpy (name, buf, len);
6784 sect = bfd_make_section (abfd, name);
6785 if (sect == NULL)
6786 return FALSE;
6788 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6789 sect->filepos = (note->descpos
6790 + offsetof (struct win32_pstatus,
6791 data.thread_info.thread_context));
6792 sect->flags = SEC_HAS_CONTENTS;
6793 sect->alignment_power = 2;
6795 if (pstatus.data.thread_info.is_active_thread)
6796 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6797 return FALSE;
6798 break;
6800 case NOTE_INFO_MODULE:
6801 /* Make a ".module/xxxxxxxx" section. */
6802 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6804 len = strlen (buf) + 1;
6805 name = bfd_alloc (abfd, (bfd_size_type) len);
6806 if (name == NULL)
6807 return FALSE;
6809 memcpy (name, buf, len);
6811 sect = bfd_make_section (abfd, name);
6813 if (sect == NULL)
6814 return FALSE;
6816 sect->_raw_size = note->descsz;
6817 sect->filepos = note->descpos;
6818 sect->flags = SEC_HAS_CONTENTS;
6819 sect->alignment_power = 2;
6820 break;
6822 default:
6823 return TRUE;
6826 return TRUE;
6828 #endif /* HAVE_WIN32_PSTATUS_T */
6830 static bfd_boolean
6831 elfcore_grok_note (abfd, note)
6832 bfd *abfd;
6833 Elf_Internal_Note *note;
6835 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6837 switch (note->type)
6839 default:
6840 return TRUE;
6842 case NT_PRSTATUS:
6843 if (bed->elf_backend_grok_prstatus)
6844 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6845 return TRUE;
6846 #if defined (HAVE_PRSTATUS_T)
6847 return elfcore_grok_prstatus (abfd, note);
6848 #else
6849 return TRUE;
6850 #endif
6852 #if defined (HAVE_PSTATUS_T)
6853 case NT_PSTATUS:
6854 return elfcore_grok_pstatus (abfd, note);
6855 #endif
6857 #if defined (HAVE_LWPSTATUS_T)
6858 case NT_LWPSTATUS:
6859 return elfcore_grok_lwpstatus (abfd, note);
6860 #endif
6862 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6863 return elfcore_grok_prfpreg (abfd, note);
6865 #if defined (HAVE_WIN32_PSTATUS_T)
6866 case NT_WIN32PSTATUS:
6867 return elfcore_grok_win32pstatus (abfd, note);
6868 #endif
6870 case NT_PRXFPREG: /* Linux SSE extension */
6871 if (note->namesz == 6
6872 && strcmp (note->namedata, "LINUX") == 0)
6873 return elfcore_grok_prxfpreg (abfd, note);
6874 else
6875 return TRUE;
6877 case NT_PRPSINFO:
6878 case NT_PSINFO:
6879 if (bed->elf_backend_grok_psinfo)
6880 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6881 return TRUE;
6882 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6883 return elfcore_grok_psinfo (abfd, note);
6884 #else
6885 return TRUE;
6886 #endif
6888 case NT_AUXV:
6890 asection *sect = bfd_make_section (abfd, ".auxv");
6892 if (sect == NULL)
6893 return FALSE;
6894 sect->_raw_size = note->descsz;
6895 sect->filepos = note->descpos;
6896 sect->flags = SEC_HAS_CONTENTS;
6897 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6899 return TRUE;
6904 static bfd_boolean
6905 elfcore_netbsd_get_lwpid (note, lwpidp)
6906 Elf_Internal_Note *note;
6907 int *lwpidp;
6909 char *cp;
6911 cp = strchr (note->namedata, '@');
6912 if (cp != NULL)
6914 *lwpidp = atoi(cp + 1);
6915 return TRUE;
6917 return FALSE;
6920 static bfd_boolean
6921 elfcore_grok_netbsd_procinfo (abfd, note)
6922 bfd *abfd;
6923 Elf_Internal_Note *note;
6926 /* Signal number at offset 0x08. */
6927 elf_tdata (abfd)->core_signal
6928 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6930 /* Process ID at offset 0x50. */
6931 elf_tdata (abfd)->core_pid
6932 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6934 /* Command name at 0x7c (max 32 bytes, including nul). */
6935 elf_tdata (abfd)->core_command
6936 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6938 return TRUE;
6941 static bfd_boolean
6942 elfcore_grok_netbsd_note (abfd, note)
6943 bfd *abfd;
6944 Elf_Internal_Note *note;
6946 int lwp;
6948 if (elfcore_netbsd_get_lwpid (note, &lwp))
6949 elf_tdata (abfd)->core_lwpid = lwp;
6951 if (note->type == NT_NETBSDCORE_PROCINFO)
6953 /* NetBSD-specific core "procinfo". Note that we expect to
6954 find this note before any of the others, which is fine,
6955 since the kernel writes this note out first when it
6956 creates a core file. */
6958 return elfcore_grok_netbsd_procinfo (abfd, note);
6961 /* As of Jan 2002 there are no other machine-independent notes
6962 defined for NetBSD core files. If the note type is less
6963 than the start of the machine-dependent note types, we don't
6964 understand it. */
6966 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6967 return TRUE;
6970 switch (bfd_get_arch (abfd))
6972 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6973 PT_GETFPREGS == mach+2. */
6975 case bfd_arch_alpha:
6976 case bfd_arch_sparc:
6977 switch (note->type)
6979 case NT_NETBSDCORE_FIRSTMACH+0:
6980 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6982 case NT_NETBSDCORE_FIRSTMACH+2:
6983 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6985 default:
6986 return TRUE;
6989 /* On all other arch's, PT_GETREGS == mach+1 and
6990 PT_GETFPREGS == mach+3. */
6992 default:
6993 switch (note->type)
6995 case NT_NETBSDCORE_FIRSTMACH+1:
6996 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6998 case NT_NETBSDCORE_FIRSTMACH+3:
6999 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7001 default:
7002 return TRUE;
7005 /* NOTREACHED */
7008 static bfd_boolean
7009 elfcore_grok_nto_status (abfd, note, tid)
7010 bfd *abfd;
7011 Elf_Internal_Note *note;
7012 pid_t *tid;
7014 void *ddata = note->descdata;
7015 char buf[100];
7016 char *name;
7017 asection *sect;
7018 short sig;
7019 unsigned flags;
7021 /* nto_procfs_status 'pid' field is at offset 0. */
7022 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7024 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7025 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7027 /* nto_procfs_status 'flags' field is at offset 8. */
7028 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7030 /* nto_procfs_status 'what' field is at offset 14. */
7031 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7033 elf_tdata (abfd)->core_signal = sig;
7034 elf_tdata (abfd)->core_lwpid = *tid;
7037 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7038 do not come from signals so we make sure we set the current
7039 thread just in case. */
7040 if (flags & 0x00000080)
7041 elf_tdata (abfd)->core_lwpid = *tid;
7043 /* Make a ".qnx_core_status/%d" section. */
7044 sprintf (buf, ".qnx_core_status/%d", *tid);
7046 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
7047 if (name == NULL)
7048 return FALSE;
7049 strcpy (name, buf);
7051 sect = bfd_make_section (abfd, name);
7052 if (sect == NULL)
7053 return FALSE;
7055 sect->_raw_size = note->descsz;
7056 sect->filepos = note->descpos;
7057 sect->flags = SEC_HAS_CONTENTS;
7058 sect->alignment_power = 2;
7060 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7063 static bfd_boolean
7064 elfcore_grok_nto_gregs (abfd, note, tid)
7065 bfd *abfd;
7066 Elf_Internal_Note *note;
7067 pid_t tid;
7069 char buf[100];
7070 char *name;
7071 asection *sect;
7073 /* Make a ".reg/%d" section. */
7074 sprintf (buf, ".reg/%d", tid);
7076 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
7077 if (name == NULL)
7078 return FALSE;
7079 strcpy (name, buf);
7081 sect = bfd_make_section (abfd, name);
7082 if (sect == NULL)
7083 return FALSE;
7085 sect->_raw_size = note->descsz;
7086 sect->filepos = note->descpos;
7087 sect->flags = SEC_HAS_CONTENTS;
7088 sect->alignment_power = 2;
7090 /* This is the current thread. */
7091 if (elf_tdata (abfd)->core_lwpid == tid)
7092 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7094 return TRUE;
7097 #define BFD_QNT_CORE_INFO 7
7098 #define BFD_QNT_CORE_STATUS 8
7099 #define BFD_QNT_CORE_GREG 9
7100 #define BFD_QNT_CORE_FPREG 10
7102 static bfd_boolean
7103 elfcore_grok_nto_note (abfd, note)
7104 bfd *abfd;
7105 Elf_Internal_Note *note;
7107 /* Every GREG section has a STATUS section before it. Store the
7108 tid from the previous call to pass down to the next gregs
7109 function. */
7110 static pid_t tid = 1;
7112 switch (note->type)
7114 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7115 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7116 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7117 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7118 default: return TRUE;
7122 /* Function: elfcore_write_note
7124 Inputs:
7125 buffer to hold note
7126 name of note
7127 type of note
7128 data for note
7129 size of data for note
7131 Return:
7132 End of buffer containing note. */
7134 char *
7135 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
7136 bfd *abfd;
7137 char *buf;
7138 int *bufsiz;
7139 const char *name;
7140 int type;
7141 const PTR input;
7142 int size;
7144 Elf_External_Note *xnp;
7145 size_t namesz;
7146 size_t pad;
7147 size_t newspace;
7148 char *p, *dest;
7150 namesz = 0;
7151 pad = 0;
7152 if (name != NULL)
7154 struct elf_backend_data *bed;
7156 namesz = strlen (name) + 1;
7157 bed = get_elf_backend_data (abfd);
7158 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7161 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
7163 p = realloc (buf, *bufsiz + newspace);
7164 dest = p + *bufsiz;
7165 *bufsiz += newspace;
7166 xnp = (Elf_External_Note *) dest;
7167 H_PUT_32 (abfd, namesz, xnp->namesz);
7168 H_PUT_32 (abfd, size, xnp->descsz);
7169 H_PUT_32 (abfd, type, xnp->type);
7170 dest = xnp->name;
7171 if (name != NULL)
7173 memcpy (dest, name, namesz);
7174 dest += namesz;
7175 while (pad != 0)
7177 *dest++ = '\0';
7178 --pad;
7181 memcpy (dest, input, size);
7182 return p;
7185 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7186 char *
7187 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
7188 bfd *abfd;
7189 char *buf;
7190 int *bufsiz;
7191 const char *fname;
7192 const char *psargs;
7194 int note_type;
7195 char *note_name = "CORE";
7197 #if defined (HAVE_PSINFO_T)
7198 psinfo_t data;
7199 note_type = NT_PSINFO;
7200 #else
7201 prpsinfo_t data;
7202 note_type = NT_PRPSINFO;
7203 #endif
7205 memset (&data, 0, sizeof (data));
7206 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7207 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7208 return elfcore_write_note (abfd, buf, bufsiz,
7209 note_name, note_type, &data, sizeof (data));
7211 #endif /* PSINFO_T or PRPSINFO_T */
7213 #if defined (HAVE_PRSTATUS_T)
7214 char *
7215 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7216 bfd *abfd;
7217 char *buf;
7218 int *bufsiz;
7219 long pid;
7220 int cursig;
7221 const PTR gregs;
7223 prstatus_t prstat;
7224 char *note_name = "CORE";
7226 memset (&prstat, 0, sizeof (prstat));
7227 prstat.pr_pid = pid;
7228 prstat.pr_cursig = cursig;
7229 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7230 return elfcore_write_note (abfd, buf, bufsiz,
7231 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7233 #endif /* HAVE_PRSTATUS_T */
7235 #if defined (HAVE_LWPSTATUS_T)
7236 char *
7237 elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7238 bfd *abfd;
7239 char *buf;
7240 int *bufsiz;
7241 long pid;
7242 int cursig;
7243 const PTR gregs;
7245 lwpstatus_t lwpstat;
7246 char *note_name = "CORE";
7248 memset (&lwpstat, 0, sizeof (lwpstat));
7249 lwpstat.pr_lwpid = pid >> 16;
7250 lwpstat.pr_cursig = cursig;
7251 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7252 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7253 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7254 #if !defined(gregs)
7255 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7256 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7257 #else
7258 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7259 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7260 #endif
7261 #endif
7262 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7263 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7265 #endif /* HAVE_LWPSTATUS_T */
7267 #if defined (HAVE_PSTATUS_T)
7268 char *
7269 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7270 bfd *abfd;
7271 char *buf;
7272 int *bufsiz;
7273 long pid;
7274 int cursig;
7275 const PTR gregs;
7277 pstatus_t pstat;
7278 char *note_name = "CORE";
7280 memset (&pstat, 0, sizeof (pstat));
7281 pstat.pr_pid = pid & 0xffff;
7282 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7283 NT_PSTATUS, &pstat, sizeof (pstat));
7284 return buf;
7286 #endif /* HAVE_PSTATUS_T */
7288 char *
7289 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7290 bfd *abfd;
7291 char *buf;
7292 int *bufsiz;
7293 const PTR fpregs;
7294 int size;
7296 char *note_name = "CORE";
7297 return elfcore_write_note (abfd, buf, bufsiz,
7298 note_name, NT_FPREGSET, fpregs, size);
7301 char *
7302 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7303 bfd *abfd;
7304 char *buf;
7305 int *bufsiz;
7306 const PTR xfpregs;
7307 int size;
7309 char *note_name = "LINUX";
7310 return elfcore_write_note (abfd, buf, bufsiz,
7311 note_name, NT_PRXFPREG, xfpregs, size);
7314 static bfd_boolean
7315 elfcore_read_notes (abfd, offset, size)
7316 bfd *abfd;
7317 file_ptr offset;
7318 bfd_size_type size;
7320 char *buf;
7321 char *p;
7323 if (size <= 0)
7324 return TRUE;
7326 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7327 return FALSE;
7329 buf = bfd_malloc (size);
7330 if (buf == NULL)
7331 return FALSE;
7333 if (bfd_bread (buf, size, abfd) != size)
7335 error:
7336 free (buf);
7337 return FALSE;
7340 p = buf;
7341 while (p < buf + size)
7343 /* FIXME: bad alignment assumption. */
7344 Elf_External_Note *xnp = (Elf_External_Note *) p;
7345 Elf_Internal_Note in;
7347 in.type = H_GET_32 (abfd, xnp->type);
7349 in.namesz = H_GET_32 (abfd, xnp->namesz);
7350 in.namedata = xnp->name;
7352 in.descsz = H_GET_32 (abfd, xnp->descsz);
7353 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7354 in.descpos = offset + (in.descdata - buf);
7356 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7358 if (! elfcore_grok_netbsd_note (abfd, &in))
7359 goto error;
7361 else if (strncmp (in.namedata, "QNX", 3) == 0)
7363 if (! elfcore_grok_nto_note (abfd, &in))
7364 goto error;
7366 else
7368 if (! elfcore_grok_note (abfd, &in))
7369 goto error;
7372 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7375 free (buf);
7376 return TRUE;
7379 /* Providing external access to the ELF program header table. */
7381 /* Return an upper bound on the number of bytes required to store a
7382 copy of ABFD's program header table entries. Return -1 if an error
7383 occurs; bfd_get_error will return an appropriate code. */
7385 long
7386 bfd_get_elf_phdr_upper_bound (abfd)
7387 bfd *abfd;
7389 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7391 bfd_set_error (bfd_error_wrong_format);
7392 return -1;
7395 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7398 /* Copy ABFD's program header table entries to *PHDRS. The entries
7399 will be stored as an array of Elf_Internal_Phdr structures, as
7400 defined in include/elf/internal.h. To find out how large the
7401 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7403 Return the number of program header table entries read, or -1 if an
7404 error occurs; bfd_get_error will return an appropriate code. */
7407 bfd_get_elf_phdrs (abfd, phdrs)
7408 bfd *abfd;
7409 void *phdrs;
7411 int num_phdrs;
7413 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7415 bfd_set_error (bfd_error_wrong_format);
7416 return -1;
7419 num_phdrs = elf_elfheader (abfd)->e_phnum;
7420 memcpy (phdrs, elf_tdata (abfd)->phdr,
7421 num_phdrs * sizeof (Elf_Internal_Phdr));
7423 return num_phdrs;
7426 void
7427 _bfd_elf_sprintf_vma (abfd, buf, value)
7428 bfd *abfd ATTRIBUTE_UNUSED;
7429 char *buf;
7430 bfd_vma value;
7432 #ifdef BFD64
7433 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7435 i_ehdrp = elf_elfheader (abfd);
7436 if (i_ehdrp == NULL)
7437 sprintf_vma (buf, value);
7438 else
7440 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7442 #if BFD_HOST_64BIT_LONG
7443 sprintf (buf, "%016lx", value);
7444 #else
7445 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7446 _bfd_int64_low (value));
7447 #endif
7449 else
7450 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7452 #else
7453 sprintf_vma (buf, value);
7454 #endif
7457 void
7458 _bfd_elf_fprintf_vma (abfd, stream, value)
7459 bfd *abfd ATTRIBUTE_UNUSED;
7460 PTR stream;
7461 bfd_vma value;
7463 #ifdef BFD64
7464 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7466 i_ehdrp = elf_elfheader (abfd);
7467 if (i_ehdrp == NULL)
7468 fprintf_vma ((FILE *) stream, value);
7469 else
7471 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7473 #if BFD_HOST_64BIT_LONG
7474 fprintf ((FILE *) stream, "%016lx", value);
7475 #else
7476 fprintf ((FILE *) stream, "%08lx%08lx",
7477 _bfd_int64_high (value), _bfd_int64_low (value));
7478 #endif
7480 else
7481 fprintf ((FILE *) stream, "%08lx",
7482 (unsigned long) (value & 0xffffffff));
7484 #else
7485 fprintf_vma ((FILE *) stream, value);
7486 #endif
7489 enum elf_reloc_type_class
7490 _bfd_elf_reloc_type_class (rela)
7491 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
7493 return reloc_class_normal;
7496 /* For RELA architectures, return the relocation value for a
7497 relocation against a local symbol. */
7499 bfd_vma
7500 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7501 bfd *abfd;
7502 Elf_Internal_Sym *sym;
7503 asection *sec;
7504 Elf_Internal_Rela *rel;
7506 bfd_vma relocation;
7508 relocation = (sec->output_section->vma
7509 + sec->output_offset
7510 + sym->st_value);
7511 if ((sec->flags & SEC_MERGE)
7512 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7513 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7515 asection *msec;
7517 msec = sec;
7518 rel->r_addend =
7519 _bfd_merged_section_offset (abfd, &msec,
7520 elf_section_data (sec)->sec_info,
7521 sym->st_value + rel->r_addend,
7522 (bfd_vma) 0)
7523 - relocation;
7524 rel->r_addend += msec->output_section->vma + msec->output_offset;
7526 return relocation;
7529 bfd_vma
7530 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7531 bfd *abfd;
7532 Elf_Internal_Sym *sym;
7533 asection **psec;
7534 bfd_vma addend;
7536 asection *sec = *psec;
7538 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7539 return sym->st_value + addend;
7541 return _bfd_merged_section_offset (abfd, psec,
7542 elf_section_data (sec)->sec_info,
7543 sym->st_value + addend, (bfd_vma) 0);
7546 bfd_vma
7547 _bfd_elf_section_offset (abfd, info, sec, offset)
7548 bfd *abfd;
7549 struct bfd_link_info *info;
7550 asection *sec;
7551 bfd_vma offset;
7553 struct bfd_elf_section_data *sec_data;
7555 sec_data = elf_section_data (sec);
7556 switch (sec->sec_info_type)
7558 case ELF_INFO_TYPE_STABS:
7559 return _bfd_stab_section_offset (abfd,
7560 &elf_hash_table (info)->merge_info,
7561 sec, &sec_data->sec_info, offset);
7562 case ELF_INFO_TYPE_EH_FRAME:
7563 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7564 default:
7565 return offset;
7569 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7570 reconstruct an ELF file by reading the segments out of remote memory
7571 based on the ELF file header at EHDR_VMA and the ELF program headers it
7572 points to. If not null, *LOADBASEP is filled in with the difference
7573 between the VMAs from which the segments were read, and the VMAs the
7574 file headers (and hence BFD's idea of each section's VMA) put them at.
7576 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7577 remote memory at target address VMA into the local buffer at MYADDR; it
7578 should return zero on success or an `errno' code on failure. TEMPL must
7579 be a BFD for an ELF target with the word size and byte order found in
7580 the remote memory. */
7582 bfd *
7583 bfd_elf_bfd_from_remote_memory (templ, ehdr_vma, loadbasep, target_read_memory)
7584 bfd *templ;
7585 bfd_vma ehdr_vma;
7586 bfd_vma *loadbasep;
7587 int (*target_read_memory) PARAMS ((bfd_vma vma, char *myaddr, int len));
7589 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7590 (templ, ehdr_vma, loadbasep, target_read_memory);