elf.c revision 77301
1/* ELF executable support for BFD.
2   Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20/* $FreeBSD: head/contrib/binutils/bfd/elf.c 77301 2001-05-28 05:49:34Z obrien $ */
21
22/*
23
24SECTION
25	ELF backends
26
27	BFD support for ELF formats is being worked on.
28	Currently, the best supported back ends are for sparc and i386
29	(running svr4 or Solaris 2).
30
31	Documentation of the internals of the support code still needs
32	to be written.  The code is changing quickly enough that we
33	haven't bothered yet.
34 */
35
36/* For sparc64-cross-sparc32.  */
37#define _SYSCALL32
38#include "bfd.h"
39#include "sysdep.h"
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
44
45static INLINE struct elf_segment_map *make_mapping
46  PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47static boolean map_sections_to_segments PARAMS ((bfd *));
48static int elf_sort_sections PARAMS ((const PTR, const PTR));
49static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51static boolean prep_headers PARAMS ((bfd *));
52static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
54static char *elf_read PARAMS ((bfd *, long, unsigned int));
55static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
56static boolean assign_section_numbers PARAMS ((bfd *));
57static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
58static boolean elf_map_symbols PARAMS ((bfd *));
59static bfd_size_type get_program_header_size PARAMS ((bfd *));
60static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
61
62/* Swap version information in and out.  The version information is
63   currently size independent.  If that ever changes, this code will
64   need to move into elfcode.h.  */
65
66/* Swap in a Verdef structure.  */
67
68void
69_bfd_elf_swap_verdef_in (abfd, src, dst)
70     bfd *abfd;
71     const Elf_External_Verdef *src;
72     Elf_Internal_Verdef *dst;
73{
74  dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
75  dst->vd_flags   = bfd_h_get_16 (abfd, src->vd_flags);
76  dst->vd_ndx     = bfd_h_get_16 (abfd, src->vd_ndx);
77  dst->vd_cnt     = bfd_h_get_16 (abfd, src->vd_cnt);
78  dst->vd_hash    = bfd_h_get_32 (abfd, src->vd_hash);
79  dst->vd_aux     = bfd_h_get_32 (abfd, src->vd_aux);
80  dst->vd_next    = bfd_h_get_32 (abfd, src->vd_next);
81}
82
83/* Swap out a Verdef structure.  */
84
85void
86_bfd_elf_swap_verdef_out (abfd, src, dst)
87     bfd *abfd;
88     const Elf_Internal_Verdef *src;
89     Elf_External_Verdef *dst;
90{
91  bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
92  bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
93  bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
94  bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
95  bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
96  bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
97  bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
98}
99
100/* Swap in a Verdaux structure.  */
101
102void
103_bfd_elf_swap_verdaux_in (abfd, src, dst)
104     bfd *abfd;
105     const Elf_External_Verdaux *src;
106     Elf_Internal_Verdaux *dst;
107{
108  dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
109  dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
110}
111
112/* Swap out a Verdaux structure.  */
113
114void
115_bfd_elf_swap_verdaux_out (abfd, src, dst)
116     bfd *abfd;
117     const Elf_Internal_Verdaux *src;
118     Elf_External_Verdaux *dst;
119{
120  bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
121  bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
122}
123
124/* Swap in a Verneed structure.  */
125
126void
127_bfd_elf_swap_verneed_in (abfd, src, dst)
128     bfd *abfd;
129     const Elf_External_Verneed *src;
130     Elf_Internal_Verneed *dst;
131{
132  dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
133  dst->vn_cnt     = bfd_h_get_16 (abfd, src->vn_cnt);
134  dst->vn_file    = bfd_h_get_32 (abfd, src->vn_file);
135  dst->vn_aux     = bfd_h_get_32 (abfd, src->vn_aux);
136  dst->vn_next    = bfd_h_get_32 (abfd, src->vn_next);
137}
138
139/* Swap out a Verneed structure.  */
140
141void
142_bfd_elf_swap_verneed_out (abfd, src, dst)
143     bfd *abfd;
144     const Elf_Internal_Verneed *src;
145     Elf_External_Verneed *dst;
146{
147  bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
148  bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
149  bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
150  bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
151  bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
152}
153
154/* Swap in a Vernaux structure.  */
155
156void
157_bfd_elf_swap_vernaux_in (abfd, src, dst)
158     bfd *abfd;
159     const Elf_External_Vernaux *src;
160     Elf_Internal_Vernaux *dst;
161{
162  dst->vna_hash  = bfd_h_get_32 (abfd, src->vna_hash);
163  dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
164  dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
165  dst->vna_name  = bfd_h_get_32 (abfd, src->vna_name);
166  dst->vna_next  = bfd_h_get_32 (abfd, src->vna_next);
167}
168
169/* Swap out a Vernaux structure.  */
170
171void
172_bfd_elf_swap_vernaux_out (abfd, src, dst)
173     bfd *abfd;
174     const Elf_Internal_Vernaux *src;
175     Elf_External_Vernaux *dst;
176{
177  bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
178  bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
179  bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
180  bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
181  bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
182}
183
184/* Swap in a Versym structure.  */
185
186void
187_bfd_elf_swap_versym_in (abfd, src, dst)
188     bfd *abfd;
189     const Elf_External_Versym *src;
190     Elf_Internal_Versym *dst;
191{
192  dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
193}
194
195/* Swap out a Versym structure.  */
196
197void
198_bfd_elf_swap_versym_out (abfd, src, dst)
199     bfd *abfd;
200     const Elf_Internal_Versym *src;
201     Elf_External_Versym *dst;
202{
203  bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
204}
205
206/* Standard ELF hash function.  Do not change this function; you will
207   cause invalid hash tables to be generated.  */
208
209unsigned long
210bfd_elf_hash (namearg)
211     const char *namearg;
212{
213  const unsigned char *name = (const unsigned char *) namearg;
214  unsigned long h = 0;
215  unsigned long g;
216  int ch;
217
218  while ((ch = *name++) != '\0')
219    {
220      h = (h << 4) + ch;
221      if ((g = (h & 0xf0000000)) != 0)
222	{
223	  h ^= g >> 24;
224	  /* The ELF ABI says `h &= ~g', but this is equivalent in
225	     this case and on some machines one insn instead of two.  */
226	  h ^= g;
227	}
228    }
229  return h;
230}
231
232/* Read a specified number of bytes at a specified offset in an ELF
233   file, into a newly allocated buffer, and return a pointer to the
234   buffer.  */
235
236static char *
237elf_read (abfd, offset, size)
238     bfd *abfd;
239     long offset;
240     unsigned int size;
241{
242  char *buf;
243
244  if ((buf = bfd_alloc (abfd, size)) == NULL)
245    return NULL;
246  if (bfd_seek (abfd, offset, SEEK_SET) == -1)
247    return NULL;
248  if (bfd_read ((PTR) buf, size, 1, abfd) != size)
249    {
250      if (bfd_get_error () != bfd_error_system_call)
251	bfd_set_error (bfd_error_file_truncated);
252      return NULL;
253    }
254  return buf;
255}
256
257boolean
258bfd_elf_mkobject (abfd)
259     bfd *abfd;
260{
261  /* This just does initialization.  */
262  /* coff_mkobject zalloc's space for tdata.coff_obj_data ...  */
263  elf_tdata (abfd) = (struct elf_obj_tdata *)
264    bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
265  if (elf_tdata (abfd) == 0)
266    return false;
267  /* Since everything is done at close time, do we need any
268     initialization?  */
269
270  return true;
271}
272
273boolean
274bfd_elf_mkcorefile (abfd)
275     bfd *abfd;
276{
277  /* I think this can be done just like an object file.  */
278  return bfd_elf_mkobject (abfd);
279}
280
281char *
282bfd_elf_get_str_section (abfd, shindex)
283     bfd *abfd;
284     unsigned int shindex;
285{
286  Elf_Internal_Shdr **i_shdrp;
287  char *shstrtab = NULL;
288  unsigned int offset;
289  unsigned int shstrtabsize;
290
291  i_shdrp = elf_elfsections (abfd);
292  if (i_shdrp == 0 || i_shdrp[shindex] == 0)
293    return 0;
294
295  shstrtab = (char *) i_shdrp[shindex]->contents;
296  if (shstrtab == NULL)
297    {
298      /* No cached one, attempt to read, and cache what we read.  */
299      offset = i_shdrp[shindex]->sh_offset;
300      shstrtabsize = i_shdrp[shindex]->sh_size;
301      shstrtab = elf_read (abfd, offset, shstrtabsize);
302      i_shdrp[shindex]->contents = (PTR) shstrtab;
303    }
304  return shstrtab;
305}
306
307char *
308bfd_elf_string_from_elf_section (abfd, shindex, strindex)
309     bfd *abfd;
310     unsigned int shindex;
311     unsigned int strindex;
312{
313  Elf_Internal_Shdr *hdr;
314
315  if (strindex == 0)
316    return "";
317
318  hdr = elf_elfsections (abfd)[shindex];
319
320  if (hdr->contents == NULL
321      && bfd_elf_get_str_section (abfd, shindex) == NULL)
322    return NULL;
323
324  if (strindex >= hdr->sh_size)
325    {
326      (*_bfd_error_handler)
327	(_("%s: invalid string offset %u >= %lu for section `%s'"),
328	 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
329	 ((shindex == elf_elfheader(abfd)->e_shstrndx
330	   && strindex == hdr->sh_name)
331	  ? ".shstrtab"
332	  : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
333      return "";
334    }
335
336  return ((char *) hdr->contents) + strindex;
337}
338
339/* Make a BFD section from an ELF section.  We store a pointer to the
340   BFD section in the bfd_section field of the header.  */
341
342boolean
343_bfd_elf_make_section_from_shdr (abfd, hdr, name)
344     bfd *abfd;
345     Elf_Internal_Shdr *hdr;
346     const char *name;
347{
348  asection *newsect;
349  flagword flags;
350  struct elf_backend_data *bed;
351
352  if (hdr->bfd_section != NULL)
353    {
354      BFD_ASSERT (strcmp (name,
355			  bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
356      return true;
357    }
358
359  newsect = bfd_make_section_anyway (abfd, name);
360  if (newsect == NULL)
361    return false;
362
363  newsect->filepos = hdr->sh_offset;
364
365  if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
366      || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
367      || ! bfd_set_section_alignment (abfd, newsect,
368				      bfd_log2 (hdr->sh_addralign)))
369    return false;
370
371  flags = SEC_NO_FLAGS;
372  if (hdr->sh_type != SHT_NOBITS)
373    flags |= SEC_HAS_CONTENTS;
374  if ((hdr->sh_flags & SHF_ALLOC) != 0)
375    {
376      flags |= SEC_ALLOC;
377      if (hdr->sh_type != SHT_NOBITS)
378	flags |= SEC_LOAD;
379    }
380  if ((hdr->sh_flags & SHF_WRITE) == 0)
381    flags |= SEC_READONLY;
382  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
383    flags |= SEC_CODE;
384  else if ((flags & SEC_LOAD) != 0)
385    flags |= SEC_DATA;
386
387  /* The debugging sections appear to be recognized only by name, not
388     any sort of flag.  */
389  {
390    static const char *debug_sec_names [] =
391    {
392      ".debug",
393      ".gnu.linkonce.wi.",
394      ".line",
395      ".stab"
396    };
397    int i;
398
399    for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
400      if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
401	break;
402
403    if (i >= 0)
404      flags |= SEC_DEBUGGING;
405  }
406
407  /* As a GNU extension, if the name begins with .gnu.linkonce, we
408     only link a single copy of the section.  This is used to support
409     g++.  g++ will emit each template expansion in its own section.
410     The symbols will be defined as weak, so that multiple definitions
411     are permitted.  The GNU linker extension is to actually discard
412     all but one of the sections.  */
413  if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
414    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
415
416  bed = get_elf_backend_data (abfd);
417  if (bed->elf_backend_section_flags)
418    if (! bed->elf_backend_section_flags (&flags, hdr))
419      return false;
420
421  if (! bfd_set_section_flags (abfd, newsect, flags))
422    return false;
423
424  if ((flags & SEC_ALLOC) != 0)
425    {
426      Elf_Internal_Phdr *phdr;
427      unsigned int i;
428
429      /* Look through the phdrs to see if we need to adjust the lma.
430         If all the p_paddr fields are zero, we ignore them, since
431         some ELF linkers produce such output.  */
432      phdr = elf_tdata (abfd)->phdr;
433      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
434	{
435	  if (phdr->p_paddr != 0)
436	    break;
437	}
438      if (i < elf_elfheader (abfd)->e_phnum)
439	{
440	  phdr = elf_tdata (abfd)->phdr;
441	  for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
442	    {
443	      if (phdr->p_type == PT_LOAD
444		  && phdr->p_vaddr != phdr->p_paddr
445		  && phdr->p_vaddr <= hdr->sh_addr
446		  && (phdr->p_vaddr + phdr->p_memsz
447		      >= hdr->sh_addr + hdr->sh_size)
448		  && ((flags & SEC_LOAD) == 0
449		      || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
450			  && (phdr->p_offset + phdr->p_filesz
451			      >= hdr->sh_offset + hdr->sh_size))))
452		{
453		  newsect->lma += phdr->p_paddr - phdr->p_vaddr;
454		  break;
455		}
456	    }
457	}
458    }
459
460  hdr->bfd_section = newsect;
461  elf_section_data (newsect)->this_hdr = *hdr;
462
463  return true;
464}
465
466/*
467INTERNAL_FUNCTION
468	bfd_elf_find_section
469
470SYNOPSIS
471	struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
472
473DESCRIPTION
474	Helper functions for GDB to locate the string tables.
475	Since BFD hides string tables from callers, GDB needs to use an
476	internal hook to find them.  Sun's .stabstr, in particular,
477	isn't even pointed to by the .stab section, so ordinary
478	mechanisms wouldn't work to find it, even if we had some.
479*/
480
481struct elf_internal_shdr *
482bfd_elf_find_section (abfd, name)
483     bfd *abfd;
484     char *name;
485{
486  Elf_Internal_Shdr **i_shdrp;
487  char *shstrtab;
488  unsigned int max;
489  unsigned int i;
490
491  i_shdrp = elf_elfsections (abfd);
492  if (i_shdrp != NULL)
493    {
494      shstrtab = bfd_elf_get_str_section
495	(abfd, elf_elfheader (abfd)->e_shstrndx);
496      if (shstrtab != NULL)
497	{
498	  max = elf_elfheader (abfd)->e_shnum;
499	  for (i = 1; i < max; i++)
500	    if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
501	      return i_shdrp[i];
502	}
503    }
504  return 0;
505}
506
507const char *const bfd_elf_section_type_names[] = {
508  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
509  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
510  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
511};
512
513/* ELF relocs are against symbols.  If we are producing relocateable
514   output, and the reloc is against an external symbol, and nothing
515   has given us any additional addend, the resulting reloc will also
516   be against the same symbol.  In such a case, we don't want to
517   change anything about the way the reloc is handled, since it will
518   all be done at final link time.  Rather than put special case code
519   into bfd_perform_relocation, all the reloc types use this howto
520   function.  It just short circuits the reloc if producing
521   relocateable output against an external symbol.  */
522
523bfd_reloc_status_type
524bfd_elf_generic_reloc (abfd,
525		       reloc_entry,
526		       symbol,
527		       data,
528		       input_section,
529		       output_bfd,
530		       error_message)
531     bfd *abfd ATTRIBUTE_UNUSED;
532     arelent *reloc_entry;
533     asymbol *symbol;
534     PTR data ATTRIBUTE_UNUSED;
535     asection *input_section;
536     bfd *output_bfd;
537     char **error_message ATTRIBUTE_UNUSED;
538{
539  if (output_bfd != (bfd *) NULL
540      && (symbol->flags & BSF_SECTION_SYM) == 0
541      && (! reloc_entry->howto->partial_inplace
542	  || reloc_entry->addend == 0))
543    {
544      reloc_entry->address += input_section->output_offset;
545      return bfd_reloc_ok;
546    }
547
548  return bfd_reloc_continue;
549}
550
551/* Print out the program headers.  */
552
553boolean
554_bfd_elf_print_private_bfd_data (abfd, farg)
555     bfd *abfd;
556     PTR farg;
557{
558  FILE *f = (FILE *) farg;
559  Elf_Internal_Phdr *p;
560  asection *s;
561  bfd_byte *dynbuf = NULL;
562
563  p = elf_tdata (abfd)->phdr;
564  if (p != NULL)
565    {
566      unsigned int i, c;
567
568      fprintf (f, _("\nProgram Header:\n"));
569      c = elf_elfheader (abfd)->e_phnum;
570      for (i = 0; i < c; i++, p++)
571	{
572	  const char *s;
573	  char buf[20];
574
575	  switch (p->p_type)
576	    {
577	    case PT_NULL: s = "NULL"; break;
578	    case PT_LOAD: s = "LOAD"; break;
579	    case PT_DYNAMIC: s = "DYNAMIC"; break;
580	    case PT_INTERP: s = "INTERP"; break;
581	    case PT_NOTE: s = "NOTE"; break;
582	    case PT_SHLIB: s = "SHLIB"; break;
583	    case PT_PHDR: s = "PHDR"; break;
584	    default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
585	    }
586	  fprintf (f, "%8s off    0x", s);
587	  fprintf_vma (f, p->p_offset);
588	  fprintf (f, " vaddr 0x");
589	  fprintf_vma (f, p->p_vaddr);
590	  fprintf (f, " paddr 0x");
591	  fprintf_vma (f, p->p_paddr);
592	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
593	  fprintf (f, "         filesz 0x");
594	  fprintf_vma (f, p->p_filesz);
595	  fprintf (f, " memsz 0x");
596	  fprintf_vma (f, p->p_memsz);
597	  fprintf (f, " flags %c%c%c",
598		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
599		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
600		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
601	  if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
602	    fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
603	  fprintf (f, "\n");
604	}
605    }
606
607  s = bfd_get_section_by_name (abfd, ".dynamic");
608  if (s != NULL)
609    {
610      int elfsec;
611      unsigned long link;
612      bfd_byte *extdyn, *extdynend;
613      size_t extdynsize;
614      void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
615
616      fprintf (f, _("\nDynamic Section:\n"));
617
618      dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
619      if (dynbuf == NULL)
620	goto error_return;
621      if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
622				      s->_raw_size))
623	goto error_return;
624
625      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
626      if (elfsec == -1)
627	goto error_return;
628      link = elf_elfsections (abfd)[elfsec]->sh_link;
629
630      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
631      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
632
633      extdyn = dynbuf;
634      extdynend = extdyn + s->_raw_size;
635      for (; extdyn < extdynend; extdyn += extdynsize)
636	{
637	  Elf_Internal_Dyn dyn;
638	  const char *name;
639	  char ab[20];
640	  boolean stringp;
641
642	  (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
643
644	  if (dyn.d_tag == DT_NULL)
645	    break;
646
647	  stringp = false;
648	  switch (dyn.d_tag)
649	    {
650	    default:
651	      sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
652	      name = ab;
653	      break;
654
655	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
656	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
657	    case DT_PLTGOT: name = "PLTGOT"; break;
658	    case DT_HASH: name = "HASH"; break;
659	    case DT_STRTAB: name = "STRTAB"; break;
660	    case DT_SYMTAB: name = "SYMTAB"; break;
661	    case DT_RELA: name = "RELA"; break;
662	    case DT_RELASZ: name = "RELASZ"; break;
663	    case DT_RELAENT: name = "RELAENT"; break;
664	    case DT_STRSZ: name = "STRSZ"; break;
665	    case DT_SYMENT: name = "SYMENT"; break;
666	    case DT_INIT: name = "INIT"; break;
667	    case DT_FINI: name = "FINI"; break;
668	    case DT_SONAME: name = "SONAME"; stringp = true; break;
669	    case DT_RPATH: name = "RPATH"; stringp = true; break;
670	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
671	    case DT_REL: name = "REL"; break;
672	    case DT_RELSZ: name = "RELSZ"; break;
673	    case DT_RELENT: name = "RELENT"; break;
674	    case DT_PLTREL: name = "PLTREL"; break;
675	    case DT_DEBUG: name = "DEBUG"; break;
676	    case DT_TEXTREL: name = "TEXTREL"; break;
677	    case DT_JMPREL: name = "JMPREL"; break;
678	    case DT_BIND_NOW: name = "BIND_NOW"; break;
679	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
680	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
681	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
682	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
683	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
684	    case DT_FLAGS: name = "FLAGS"; break;
685	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
686	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
687	    case DT_CHECKSUM: name = "CHECKSUM"; break;
688	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
689	    case DT_MOVEENT: name = "MOVEENT"; break;
690	    case DT_MOVESZ: name = "MOVESZ"; break;
691	    case DT_FEATURE: name = "FEATURE"; break;
692	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
693	    case DT_SYMINSZ: name = "SYMINSZ"; break;
694	    case DT_SYMINENT: name = "SYMINENT"; break;
695	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
696	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
697	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
698	    case DT_PLTPAD: name = "PLTPAD"; break;
699	    case DT_MOVETAB: name = "MOVETAB"; break;
700	    case DT_SYMINFO: name = "SYMINFO"; break;
701	    case DT_RELACOUNT: name = "RELACOUNT"; break;
702	    case DT_RELCOUNT: name = "RELCOUNT"; break;
703	    case DT_FLAGS_1: name = "FLAGS_1"; break;
704	    case DT_VERSYM: name = "VERSYM"; break;
705	    case DT_VERDEF: name = "VERDEF"; break;
706	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
707	    case DT_VERNEED: name = "VERNEED"; break;
708	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
709	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
710	    case DT_USED: name = "USED"; break;
711	    case DT_FILTER: name = "FILTER"; stringp = true; break;
712	    }
713
714	  fprintf (f, "  %-11s ", name);
715	  if (! stringp)
716	    fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
717	  else
718	    {
719	      const char *string;
720
721	      string = bfd_elf_string_from_elf_section (abfd, link,
722							dyn.d_un.d_val);
723	      if (string == NULL)
724		goto error_return;
725	      fprintf (f, "%s", string);
726	    }
727	  fprintf (f, "\n");
728	}
729
730      free (dynbuf);
731      dynbuf = NULL;
732    }
733
734  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
735      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
736    {
737      if (! _bfd_elf_slurp_version_tables (abfd))
738	return false;
739    }
740
741  if (elf_dynverdef (abfd) != 0)
742    {
743      Elf_Internal_Verdef *t;
744
745      fprintf (f, _("\nVersion definitions:\n"));
746      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
747	{
748	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
749		   t->vd_flags, t->vd_hash, t->vd_nodename);
750	  if (t->vd_auxptr->vda_nextptr != NULL)
751	    {
752	      Elf_Internal_Verdaux *a;
753
754	      fprintf (f, "\t");
755	      for (a = t->vd_auxptr->vda_nextptr;
756		   a != NULL;
757		   a = a->vda_nextptr)
758		fprintf (f, "%s ", a->vda_nodename);
759	      fprintf (f, "\n");
760	    }
761	}
762    }
763
764  if (elf_dynverref (abfd) != 0)
765    {
766      Elf_Internal_Verneed *t;
767
768      fprintf (f, _("\nVersion References:\n"));
769      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
770	{
771	  Elf_Internal_Vernaux *a;
772
773	  fprintf (f, _("  required from %s:\n"), t->vn_filename);
774	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
775	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
776		     a->vna_flags, a->vna_other, a->vna_nodename);
777	}
778    }
779
780  return true;
781
782 error_return:
783  if (dynbuf != NULL)
784    free (dynbuf);
785  return false;
786}
787
788/* Display ELF-specific fields of a symbol.  */
789
790void
791bfd_elf_print_symbol (abfd, filep, symbol, how)
792     bfd *abfd;
793     PTR filep;
794     asymbol *symbol;
795     bfd_print_symbol_type how;
796{
797  FILE *file = (FILE *) filep;
798  switch (how)
799    {
800    case bfd_print_symbol_name:
801      fprintf (file, "%s", symbol->name);
802      break;
803    case bfd_print_symbol_more:
804      fprintf (file, "elf ");
805      fprintf_vma (file, symbol->value);
806      fprintf (file, " %lx", (long) symbol->flags);
807      break;
808    case bfd_print_symbol_all:
809      {
810	CONST char *section_name;
811	CONST char *name = NULL;
812	struct elf_backend_data *bed;
813	unsigned char st_other;
814
815	section_name = symbol->section ? symbol->section->name : "(*none*)";
816
817	bed = get_elf_backend_data (abfd);
818	if (bed->elf_backend_print_symbol_all)
819	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
820
821	if (name == NULL)
822	  {
823	    name = symbol->name;
824	    bfd_print_symbol_vandf ((PTR) file, symbol);
825	  }
826
827	fprintf (file, " %s\t", section_name);
828	/* Print the "other" value for a symbol.  For common symbols,
829	   we've already printed the size; now print the alignment.
830	   For other symbols, we have no specified alignment, and
831	   we've printed the address; now print the size.  */
832	fprintf_vma (file,
833		     (bfd_is_com_section (symbol->section)
834		      ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
835		      : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
836
837	/* If we have version information, print it.  */
838	if (elf_tdata (abfd)->dynversym_section != 0
839	    && (elf_tdata (abfd)->dynverdef_section != 0
840		|| elf_tdata (abfd)->dynverref_section != 0))
841	  {
842	    unsigned int vernum;
843	    const char *version_string;
844
845	    vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
846
847	    if (vernum == 0)
848	      version_string = "";
849	    else if (vernum == 1)
850	      version_string = "Base";
851	    else if (vernum <= elf_tdata (abfd)->cverdefs)
852	      version_string =
853		elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
854	    else
855	      {
856		Elf_Internal_Verneed *t;
857
858		version_string = "";
859		for (t = elf_tdata (abfd)->verref;
860		     t != NULL;
861		     t = t->vn_nextref)
862		  {
863		    Elf_Internal_Vernaux *a;
864
865		    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
866		      {
867			if (a->vna_other == vernum)
868			  {
869			    version_string = a->vna_nodename;
870			    break;
871			  }
872		      }
873		  }
874	      }
875
876	    if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
877	      fprintf (file, "  %-11s", version_string);
878	    else
879	      {
880		int i;
881
882		fprintf (file, " (%s)", version_string);
883		for (i = 10 - strlen (version_string); i > 0; --i)
884		  putc (' ', file);
885	      }
886	  }
887
888	/* If the st_other field is not zero, print it.  */
889	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
890
891	switch (st_other)
892	  {
893	  case 0: break;
894	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
895	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
896	  case STV_PROTECTED: fprintf (file, " .protected"); break;
897	  default:
898	    /* Some other non-defined flags are also present, so print
899	       everything hex.  */
900	    fprintf (file, " 0x%02x", (unsigned int) st_other);
901	  }
902
903	fprintf (file, " %s", name);
904      }
905      break;
906    }
907}
908
909/* Create an entry in an ELF linker hash table.  */
910
911struct bfd_hash_entry *
912_bfd_elf_link_hash_newfunc (entry, table, string)
913     struct bfd_hash_entry *entry;
914     struct bfd_hash_table *table;
915     const char *string;
916{
917  struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
918
919  /* Allocate the structure if it has not already been allocated by a
920     subclass.  */
921  if (ret == (struct elf_link_hash_entry *) NULL)
922    ret = ((struct elf_link_hash_entry *)
923	   bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
924  if (ret == (struct elf_link_hash_entry *) NULL)
925    return (struct bfd_hash_entry *) ret;
926
927  /* Call the allocation method of the superclass.  */
928  ret = ((struct elf_link_hash_entry *)
929	 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
930				 table, string));
931  if (ret != (struct elf_link_hash_entry *) NULL)
932    {
933      /* Set local fields.  */
934      ret->indx = -1;
935      ret->size = 0;
936      ret->dynindx = -1;
937      ret->dynstr_index = 0;
938      ret->weakdef = NULL;
939      ret->got.offset = (bfd_vma) -1;
940      ret->plt.offset = (bfd_vma) -1;
941      ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
942      ret->verinfo.verdef = NULL;
943      ret->vtable_entries_used = NULL;
944      ret->vtable_entries_size = 0;
945      ret->vtable_parent = NULL;
946      ret->type = STT_NOTYPE;
947      ret->other = 0;
948      /* Assume that we have been called by a non-ELF symbol reader.
949         This flag is then reset by the code which reads an ELF input
950         file.  This ensures that a symbol created by a non-ELF symbol
951         reader will have the flag set correctly.  */
952      ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
953    }
954
955  return (struct bfd_hash_entry *) ret;
956}
957
958/* Copy data from an indirect symbol to its direct symbol, hiding the
959   old indirect symbol.  */
960
961void
962_bfd_elf_link_hash_copy_indirect (dir, ind)
963     struct elf_link_hash_entry *dir, *ind;
964{
965  /* Copy down any references that we may have already seen to the
966     symbol which just became indirect.  */
967
968  dir->elf_link_hash_flags |=
969    (ind->elf_link_hash_flags
970     & (ELF_LINK_HASH_REF_DYNAMIC
971	| ELF_LINK_HASH_REF_REGULAR
972	| ELF_LINK_HASH_REF_REGULAR_NONWEAK
973	| ELF_LINK_NON_GOT_REF));
974
975  /* Copy over the global and procedure linkage table offset entries.
976     These may have been already set up by a check_relocs routine.  */
977  if (dir->got.offset == (bfd_vma) -1)
978    {
979      dir->got.offset = ind->got.offset;
980      ind->got.offset = (bfd_vma) -1;
981    }
982  BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
983
984  if (dir->plt.offset == (bfd_vma) -1)
985    {
986      dir->plt.offset = ind->plt.offset;
987      ind->plt.offset = (bfd_vma) -1;
988    }
989  BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
990
991  if (dir->dynindx == -1)
992    {
993      dir->dynindx = ind->dynindx;
994      dir->dynstr_index = ind->dynstr_index;
995      ind->dynindx = -1;
996      ind->dynstr_index = 0;
997    }
998  BFD_ASSERT (ind->dynindx == -1);
999}
1000
1001void
1002_bfd_elf_link_hash_hide_symbol (info, h)
1003     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1004     struct elf_link_hash_entry *h;
1005{
1006  h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1007  h->dynindx = -1;
1008  h->plt.offset = (bfd_vma) -1;
1009}
1010
1011/* Initialize an ELF linker hash table.  */
1012
1013boolean
1014_bfd_elf_link_hash_table_init (table, abfd, newfunc)
1015     struct elf_link_hash_table *table;
1016     bfd *abfd;
1017     struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1018						struct bfd_hash_table *,
1019						const char *));
1020{
1021  table->dynamic_sections_created = false;
1022  table->dynobj = NULL;
1023  /* The first dynamic symbol is a dummy.  */
1024  table->dynsymcount = 1;
1025  table->dynstr = NULL;
1026  table->bucketcount = 0;
1027  table->needed = NULL;
1028  table->runpath = NULL;
1029  table->hgot = NULL;
1030  table->stab_info = NULL;
1031  table->dynlocal = NULL;
1032  return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1033}
1034
1035/* Create an ELF linker hash table.  */
1036
1037struct bfd_link_hash_table *
1038_bfd_elf_link_hash_table_create (abfd)
1039     bfd *abfd;
1040{
1041  struct elf_link_hash_table *ret;
1042
1043  ret = ((struct elf_link_hash_table *)
1044	 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1045  if (ret == (struct elf_link_hash_table *) NULL)
1046    return NULL;
1047
1048  if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1049    {
1050      bfd_release (abfd, ret);
1051      return NULL;
1052    }
1053
1054  return &ret->root;
1055}
1056
1057/* This is a hook for the ELF emulation code in the generic linker to
1058   tell the backend linker what file name to use for the DT_NEEDED
1059   entry for a dynamic object.  The generic linker passes name as an
1060   empty string to indicate that no DT_NEEDED entry should be made.  */
1061
1062void
1063bfd_elf_set_dt_needed_name (abfd, name)
1064     bfd *abfd;
1065     const char *name;
1066{
1067  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1068      && bfd_get_format (abfd) == bfd_object)
1069    elf_dt_name (abfd) = name;
1070}
1071
1072void
1073bfd_elf_set_dt_needed_soname (abfd, name)
1074     bfd *abfd;
1075     const char *name;
1076{
1077  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1078      && bfd_get_format (abfd) == bfd_object)
1079    elf_dt_soname (abfd) = name;
1080}
1081
1082/* Get the list of DT_NEEDED entries for a link.  This is a hook for
1083   the linker ELF emulation code.  */
1084
1085struct bfd_link_needed_list *
1086bfd_elf_get_needed_list (abfd, info)
1087     bfd *abfd ATTRIBUTE_UNUSED;
1088     struct bfd_link_info *info;
1089{
1090  if (info->hash->creator->flavour != bfd_target_elf_flavour)
1091    return NULL;
1092  return elf_hash_table (info)->needed;
1093}
1094
1095/* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
1096   hook for the linker ELF emulation code.  */
1097
1098struct bfd_link_needed_list *
1099bfd_elf_get_runpath_list (abfd, info)
1100     bfd *abfd ATTRIBUTE_UNUSED;
1101     struct bfd_link_info *info;
1102{
1103  if (info->hash->creator->flavour != bfd_target_elf_flavour)
1104    return NULL;
1105  return elf_hash_table (info)->runpath;
1106}
1107
1108/* Get the name actually used for a dynamic object for a link.  This
1109   is the SONAME entry if there is one.  Otherwise, it is the string
1110   passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
1111
1112const char *
1113bfd_elf_get_dt_soname (abfd)
1114     bfd *abfd;
1115{
1116  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1117      && bfd_get_format (abfd) == bfd_object)
1118    return elf_dt_name (abfd);
1119  return NULL;
1120}
1121
1122/* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
1123   the ELF linker emulation code.  */
1124
1125boolean
1126bfd_elf_get_bfd_needed_list (abfd, pneeded)
1127     bfd *abfd;
1128     struct bfd_link_needed_list **pneeded;
1129{
1130  asection *s;
1131  bfd_byte *dynbuf = NULL;
1132  int elfsec;
1133  unsigned long link;
1134  bfd_byte *extdyn, *extdynend;
1135  size_t extdynsize;
1136  void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1137
1138  *pneeded = NULL;
1139
1140  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1141      || bfd_get_format (abfd) != bfd_object)
1142    return true;
1143
1144  s = bfd_get_section_by_name (abfd, ".dynamic");
1145  if (s == NULL || s->_raw_size == 0)
1146    return true;
1147
1148  dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1149  if (dynbuf == NULL)
1150    goto error_return;
1151
1152  if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1153				  s->_raw_size))
1154    goto error_return;
1155
1156  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1157  if (elfsec == -1)
1158    goto error_return;
1159
1160  link = elf_elfsections (abfd)[elfsec]->sh_link;
1161
1162  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1163  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1164
1165  extdyn = dynbuf;
1166  extdynend = extdyn + s->_raw_size;
1167  for (; extdyn < extdynend; extdyn += extdynsize)
1168    {
1169      Elf_Internal_Dyn dyn;
1170
1171      (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1172
1173      if (dyn.d_tag == DT_NULL)
1174	break;
1175
1176      if (dyn.d_tag == DT_NEEDED)
1177	{
1178	  const char *string;
1179	  struct bfd_link_needed_list *l;
1180
1181	  string = bfd_elf_string_from_elf_section (abfd, link,
1182						    dyn.d_un.d_val);
1183	  if (string == NULL)
1184	    goto error_return;
1185
1186	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1187	  if (l == NULL)
1188	    goto error_return;
1189
1190	  l->by = abfd;
1191	  l->name = string;
1192	  l->next = *pneeded;
1193	  *pneeded = l;
1194	}
1195    }
1196
1197  free (dynbuf);
1198
1199  return true;
1200
1201 error_return:
1202  if (dynbuf != NULL)
1203    free (dynbuf);
1204  return false;
1205}
1206
1207/* Allocate an ELF string table--force the first byte to be zero.  */
1208
1209struct bfd_strtab_hash *
1210_bfd_elf_stringtab_init ()
1211{
1212  struct bfd_strtab_hash *ret;
1213
1214  ret = _bfd_stringtab_init ();
1215  if (ret != NULL)
1216    {
1217      bfd_size_type loc;
1218
1219      loc = _bfd_stringtab_add (ret, "", true, false);
1220      BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1221      if (loc == (bfd_size_type) -1)
1222	{
1223	  _bfd_stringtab_free (ret);
1224	  ret = NULL;
1225	}
1226    }
1227  return ret;
1228}
1229
1230/* ELF .o/exec file reading */
1231
1232/* Create a new bfd section from an ELF section header.  */
1233
1234boolean
1235bfd_section_from_shdr (abfd, shindex)
1236     bfd *abfd;
1237     unsigned int shindex;
1238{
1239  Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1240  Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1241  struct elf_backend_data *bed = get_elf_backend_data (abfd);
1242  char *name;
1243
1244  name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1245
1246  switch (hdr->sh_type)
1247    {
1248    case SHT_NULL:
1249      /* Inactive section. Throw it away.  */
1250      return true;
1251
1252    case SHT_PROGBITS:	/* Normal section with contents.  */
1253    case SHT_DYNAMIC:	/* Dynamic linking information.  */
1254    case SHT_NOBITS:	/* .bss section.  */
1255    case SHT_HASH:	/* .hash section.  */
1256    case SHT_NOTE:	/* .note section.  */
1257      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1258
1259    case SHT_SYMTAB:		/* A symbol table */
1260      if (elf_onesymtab (abfd) == shindex)
1261	return true;
1262
1263      BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1264      BFD_ASSERT (elf_onesymtab (abfd) == 0);
1265      elf_onesymtab (abfd) = shindex;
1266      elf_tdata (abfd)->symtab_hdr = *hdr;
1267      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1268      abfd->flags |= HAS_SYMS;
1269
1270      /* Sometimes a shared object will map in the symbol table.  If
1271         SHF_ALLOC is set, and this is a shared object, then we also
1272         treat this section as a BFD section.  We can not base the
1273         decision purely on SHF_ALLOC, because that flag is sometimes
1274         set in a relocateable object file, which would confuse the
1275         linker.  */
1276      if ((hdr->sh_flags & SHF_ALLOC) != 0
1277	  && (abfd->flags & DYNAMIC) != 0
1278	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1279	return false;
1280
1281      return true;
1282
1283    case SHT_DYNSYM:		/* A dynamic symbol table */
1284      if (elf_dynsymtab (abfd) == shindex)
1285	return true;
1286
1287      BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1288      BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1289      elf_dynsymtab (abfd) = shindex;
1290      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1291      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1292      abfd->flags |= HAS_SYMS;
1293
1294      /* Besides being a symbol table, we also treat this as a regular
1295	 section, so that objcopy can handle it.  */
1296      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1297
1298    case SHT_STRTAB:		/* A string table */
1299      if (hdr->bfd_section != NULL)
1300	return true;
1301      if (ehdr->e_shstrndx == shindex)
1302	{
1303	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
1304	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1305	  return true;
1306	}
1307      {
1308	unsigned int i;
1309
1310	for (i = 1; i < ehdr->e_shnum; i++)
1311	  {
1312	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1313	    if (hdr2->sh_link == shindex)
1314	      {
1315		if (! bfd_section_from_shdr (abfd, i))
1316		  return false;
1317		if (elf_onesymtab (abfd) == i)
1318		  {
1319		    elf_tdata (abfd)->strtab_hdr = *hdr;
1320		    elf_elfsections (abfd)[shindex] =
1321		      &elf_tdata (abfd)->strtab_hdr;
1322		    return true;
1323		  }
1324		if (elf_dynsymtab (abfd) == i)
1325		  {
1326		    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1327		    elf_elfsections (abfd)[shindex] = hdr =
1328		      &elf_tdata (abfd)->dynstrtab_hdr;
1329		    /* We also treat this as a regular section, so
1330		       that objcopy can handle it.  */
1331		    break;
1332		  }
1333#if 0 /* Not handling other string tables specially right now.  */
1334		hdr2 = elf_elfsections (abfd)[i];	/* in case it moved */
1335		/* We have a strtab for some random other section.  */
1336		newsect = (asection *) hdr2->bfd_section;
1337		if (!newsect)
1338		  break;
1339		hdr->bfd_section = newsect;
1340		hdr2 = &elf_section_data (newsect)->str_hdr;
1341		*hdr2 = *hdr;
1342		elf_elfsections (abfd)[shindex] = hdr2;
1343#endif
1344	      }
1345	  }
1346      }
1347
1348      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1349
1350    case SHT_REL:
1351    case SHT_RELA:
1352      /* *These* do a lot of work -- but build no sections!  */
1353      {
1354	asection *target_sect;
1355	Elf_Internal_Shdr *hdr2;
1356
1357	/* Check for a bogus link to avoid crashing.  */
1358	if (hdr->sh_link >= ehdr->e_shnum)
1359	  {
1360	    ((*_bfd_error_handler)
1361	     (_("%s: invalid link %lu for reloc section %s (index %u)"),
1362	      bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1363	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1364	  }
1365
1366	/* For some incomprehensible reason Oracle distributes
1367	   libraries for Solaris in which some of the objects have
1368	   bogus sh_link fields.  It would be nice if we could just
1369	   reject them, but, unfortunately, some people need to use
1370	   them.  We scan through the section headers; if we find only
1371	   one suitable symbol table, we clobber the sh_link to point
1372	   to it.  I hope this doesn't break anything.  */
1373	if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1374	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1375	  {
1376	    int scan;
1377	    int found;
1378
1379	    found = 0;
1380	    for (scan = 1; scan < ehdr->e_shnum; scan++)
1381	      {
1382		if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1383		    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1384		  {
1385		    if (found != 0)
1386		      {
1387			found = 0;
1388			break;
1389		      }
1390		    found = scan;
1391		  }
1392	      }
1393	    if (found != 0)
1394	      hdr->sh_link = found;
1395	  }
1396
1397	/* Get the symbol table.  */
1398	if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1399	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1400	  return false;
1401
1402	/* If this reloc section does not use the main symbol table we
1403	   don't treat it as a reloc section.  BFD can't adequately
1404	   represent such a section, so at least for now, we don't
1405	   try.  We just present it as a normal section.  We also
1406	   can't use it as a reloc section if it points to the null
1407	   section.  */
1408	if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1409	  return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1410
1411	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1412	  return false;
1413	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1414	if (target_sect == NULL)
1415	  return false;
1416
1417	if ((target_sect->flags & SEC_RELOC) == 0
1418	    || target_sect->reloc_count == 0)
1419	  hdr2 = &elf_section_data (target_sect)->rel_hdr;
1420	else
1421	  {
1422	    BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1423	    hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1424	    elf_section_data (target_sect)->rel_hdr2 = hdr2;
1425	  }
1426	*hdr2 = *hdr;
1427	elf_elfsections (abfd)[shindex] = hdr2;
1428	target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1429	target_sect->flags |= SEC_RELOC;
1430	target_sect->relocation = NULL;
1431	target_sect->rel_filepos = hdr->sh_offset;
1432	/* In the section to which the relocations apply, mark whether
1433	   its relocations are of the REL or RELA variety.  */
1434	if (hdr->sh_size != 0)
1435	  elf_section_data (target_sect)->use_rela_p
1436	    = (hdr->sh_type == SHT_RELA);
1437	abfd->flags |= HAS_RELOC;
1438	return true;
1439      }
1440      break;
1441
1442    case SHT_GNU_verdef:
1443      elf_dynverdef (abfd) = shindex;
1444      elf_tdata (abfd)->dynverdef_hdr = *hdr;
1445      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1446      break;
1447
1448    case SHT_GNU_versym:
1449      elf_dynversym (abfd) = shindex;
1450      elf_tdata (abfd)->dynversym_hdr = *hdr;
1451      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1452      break;
1453
1454    case SHT_GNU_verneed:
1455      elf_dynverref (abfd) = shindex;
1456      elf_tdata (abfd)->dynverref_hdr = *hdr;
1457      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1458      break;
1459
1460    case SHT_SHLIB:
1461      return true;
1462
1463    default:
1464      /* Check for any processor-specific section types.  */
1465      {
1466	if (bed->elf_backend_section_from_shdr)
1467	  (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1468      }
1469      break;
1470    }
1471
1472  return true;
1473}
1474
1475/* Given an ELF section number, retrieve the corresponding BFD
1476   section.  */
1477
1478asection *
1479bfd_section_from_elf_index (abfd, index)
1480     bfd *abfd;
1481     unsigned int index;
1482{
1483  BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1484  if (index >= elf_elfheader (abfd)->e_shnum)
1485    return NULL;
1486  return elf_elfsections (abfd)[index]->bfd_section;
1487}
1488
1489boolean
1490_bfd_elf_new_section_hook (abfd, sec)
1491     bfd *abfd;
1492     asection *sec;
1493{
1494  struct bfd_elf_section_data *sdata;
1495
1496  sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1497  if (!sdata)
1498    return false;
1499  sec->used_by_bfd = (PTR) sdata;
1500
1501  /* Indicate whether or not this section should use RELA relocations.  */
1502  sdata->use_rela_p
1503    = get_elf_backend_data (abfd)->default_use_rela_p;
1504
1505  return true;
1506}
1507
1508/* Create a new bfd section from an ELF program header.
1509
1510   Since program segments have no names, we generate a synthetic name
1511   of the form segment<NUM>, where NUM is generally the index in the
1512   program header table.  For segments that are split (see below) we
1513   generate the names segment<NUM>a and segment<NUM>b.
1514
1515   Note that some program segments may have a file size that is different than
1516   (less than) the memory size.  All this means is that at execution the
1517   system must allocate the amount of memory specified by the memory size,
1518   but only initialize it with the first "file size" bytes read from the
1519   file.  This would occur for example, with program segments consisting
1520   of combined data+bss.
1521
1522   To handle the above situation, this routine generates TWO bfd sections
1523   for the single program segment.  The first has the length specified by
1524   the file size of the segment, and the second has the length specified
1525   by the difference between the two sizes.  In effect, the segment is split
1526   into it's initialized and uninitialized parts.
1527
1528 */
1529
1530boolean
1531_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1532     bfd *abfd;
1533     Elf_Internal_Phdr *hdr;
1534     int index;
1535     const char *typename;
1536{
1537  asection *newsect;
1538  char *name;
1539  char namebuf[64];
1540  int split;
1541
1542  split = ((hdr->p_memsz > 0)
1543	    && (hdr->p_filesz > 0)
1544	    && (hdr->p_memsz > hdr->p_filesz));
1545  sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1546  name = bfd_alloc (abfd, strlen (namebuf) + 1);
1547  if (!name)
1548    return false;
1549  strcpy (name, namebuf);
1550  newsect = bfd_make_section (abfd, name);
1551  if (newsect == NULL)
1552    return false;
1553  newsect->vma = hdr->p_vaddr;
1554  newsect->lma = hdr->p_paddr;
1555  newsect->_raw_size = hdr->p_filesz;
1556  newsect->filepos = hdr->p_offset;
1557  newsect->flags |= SEC_HAS_CONTENTS;
1558  if (hdr->p_type == PT_LOAD)
1559    {
1560      newsect->flags |= SEC_ALLOC;
1561      newsect->flags |= SEC_LOAD;
1562      if (hdr->p_flags & PF_X)
1563	{
1564	  /* FIXME: all we known is that it has execute PERMISSION,
1565	     may be data.  */
1566	  newsect->flags |= SEC_CODE;
1567	}
1568    }
1569  if (!(hdr->p_flags & PF_W))
1570    {
1571      newsect->flags |= SEC_READONLY;
1572    }
1573
1574  if (split)
1575    {
1576      sprintf (namebuf, "%s%db", typename, index);
1577      name = bfd_alloc (abfd, strlen (namebuf) + 1);
1578      if (!name)
1579	return false;
1580      strcpy (name, namebuf);
1581      newsect = bfd_make_section (abfd, name);
1582      if (newsect == NULL)
1583	return false;
1584      newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1585      newsect->lma = hdr->p_paddr + hdr->p_filesz;
1586      newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1587      if (hdr->p_type == PT_LOAD)
1588	{
1589	  newsect->flags |= SEC_ALLOC;
1590	  if (hdr->p_flags & PF_X)
1591	    newsect->flags |= SEC_CODE;
1592	}
1593      if (!(hdr->p_flags & PF_W))
1594	newsect->flags |= SEC_READONLY;
1595    }
1596
1597  return true;
1598}
1599
1600boolean
1601bfd_section_from_phdr (abfd, hdr, index)
1602     bfd *abfd;
1603     Elf_Internal_Phdr *hdr;
1604     int index;
1605{
1606  struct elf_backend_data *bed;
1607
1608  switch (hdr->p_type)
1609    {
1610    case PT_NULL:
1611      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1612
1613    case PT_LOAD:
1614      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1615
1616    case PT_DYNAMIC:
1617      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1618
1619    case PT_INTERP:
1620      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1621
1622    case PT_NOTE:
1623      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1624	return false;
1625      if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1626	return false;
1627      return true;
1628
1629    case PT_SHLIB:
1630      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1631
1632    case PT_PHDR:
1633      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1634
1635    default:
1636      /* Check for any processor-specific program segment types.
1637         If no handler for them, default to making "segment" sections.  */
1638      bed = get_elf_backend_data (abfd);
1639      if (bed->elf_backend_section_from_phdr)
1640	return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1641      else
1642	return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1643    }
1644}
1645
1646/* Initialize REL_HDR, the section-header for new section, containing
1647   relocations against ASECT.  If USE_RELA_P is true, we use RELA
1648   relocations; otherwise, we use REL relocations.  */
1649
1650boolean
1651_bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1652     bfd *abfd;
1653     Elf_Internal_Shdr *rel_hdr;
1654     asection *asect;
1655     boolean use_rela_p;
1656{
1657  char *name;
1658  struct elf_backend_data *bed;
1659
1660  bed = get_elf_backend_data (abfd);
1661  name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1662  if (name == NULL)
1663    return false;
1664  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1665  rel_hdr->sh_name =
1666    (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1667				       true, false);
1668  if (rel_hdr->sh_name == (unsigned int) -1)
1669    return false;
1670  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1671  rel_hdr->sh_entsize = (use_rela_p
1672			 ? bed->s->sizeof_rela
1673			 : bed->s->sizeof_rel);
1674  rel_hdr->sh_addralign = bed->s->file_align;
1675  rel_hdr->sh_flags = 0;
1676  rel_hdr->sh_addr = 0;
1677  rel_hdr->sh_size = 0;
1678  rel_hdr->sh_offset = 0;
1679
1680  return true;
1681}
1682
1683/* Set up an ELF internal section header for a section.  */
1684
1685static void
1686elf_fake_sections (abfd, asect, failedptrarg)
1687     bfd *abfd;
1688     asection *asect;
1689     PTR failedptrarg;
1690{
1691  struct elf_backend_data *bed = get_elf_backend_data (abfd);
1692  boolean *failedptr = (boolean *) failedptrarg;
1693  Elf_Internal_Shdr *this_hdr;
1694
1695  if (*failedptr)
1696    {
1697      /* We already failed; just get out of the bfd_map_over_sections
1698         loop.  */
1699      return;
1700    }
1701
1702  this_hdr = &elf_section_data (asect)->this_hdr;
1703
1704  this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1705							  asect->name,
1706							  true, false);
1707  if (this_hdr->sh_name == (unsigned long) -1)
1708    {
1709      *failedptr = true;
1710      return;
1711    }
1712
1713  this_hdr->sh_flags = 0;
1714
1715  if ((asect->flags & SEC_ALLOC) != 0
1716      || asect->user_set_vma)
1717    this_hdr->sh_addr = asect->vma;
1718  else
1719    this_hdr->sh_addr = 0;
1720
1721  this_hdr->sh_offset = 0;
1722  this_hdr->sh_size = asect->_raw_size;
1723  this_hdr->sh_link = 0;
1724  this_hdr->sh_addralign = 1 << asect->alignment_power;
1725  /* The sh_entsize and sh_info fields may have been set already by
1726     copy_private_section_data.  */
1727
1728  this_hdr->bfd_section = asect;
1729  this_hdr->contents = NULL;
1730
1731  /* FIXME: This should not be based on section names.  */
1732  if (strcmp (asect->name, ".dynstr") == 0)
1733    this_hdr->sh_type = SHT_STRTAB;
1734  else if (strcmp (asect->name, ".hash") == 0)
1735    {
1736      this_hdr->sh_type = SHT_HASH;
1737      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1738    }
1739  else if (strcmp (asect->name, ".dynsym") == 0)
1740    {
1741      this_hdr->sh_type = SHT_DYNSYM;
1742      this_hdr->sh_entsize = bed->s->sizeof_sym;
1743    }
1744  else if (strcmp (asect->name, ".dynamic") == 0)
1745    {
1746      this_hdr->sh_type = SHT_DYNAMIC;
1747      this_hdr->sh_entsize = bed->s->sizeof_dyn;
1748    }
1749  else if (strncmp (asect->name, ".rela", 5) == 0
1750	   && get_elf_backend_data (abfd)->may_use_rela_p)
1751    {
1752      this_hdr->sh_type = SHT_RELA;
1753      this_hdr->sh_entsize = bed->s->sizeof_rela;
1754    }
1755  else if (strncmp (asect->name, ".rel", 4) == 0
1756	   && get_elf_backend_data (abfd)->may_use_rel_p)
1757    {
1758      this_hdr->sh_type = SHT_REL;
1759      this_hdr->sh_entsize = bed->s->sizeof_rel;
1760    }
1761  else if (strncmp (asect->name, ".note", 5) == 0)
1762    this_hdr->sh_type = SHT_NOTE;
1763  else if (strncmp (asect->name, ".stab", 5) == 0
1764	   && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1765    this_hdr->sh_type = SHT_STRTAB;
1766  else if (strcmp (asect->name, ".gnu.version") == 0)
1767    {
1768      this_hdr->sh_type = SHT_GNU_versym;
1769      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1770    }
1771  else if (strcmp (asect->name, ".gnu.version_d") == 0)
1772    {
1773      this_hdr->sh_type = SHT_GNU_verdef;
1774      this_hdr->sh_entsize = 0;
1775      /* objcopy or strip will copy over sh_info, but may not set
1776         cverdefs.  The linker will set cverdefs, but sh_info will be
1777         zero.  */
1778      if (this_hdr->sh_info == 0)
1779	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1780      else
1781	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1782		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1783    }
1784  else if (strcmp (asect->name, ".gnu.version_r") == 0)
1785    {
1786      this_hdr->sh_type = SHT_GNU_verneed;
1787      this_hdr->sh_entsize = 0;
1788      /* objcopy or strip will copy over sh_info, but may not set
1789         cverrefs.  The linker will set cverrefs, but sh_info will be
1790         zero.  */
1791      if (this_hdr->sh_info == 0)
1792	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1793      else
1794	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1795		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1796    }
1797  else if ((asect->flags & SEC_ALLOC) != 0
1798	   && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
1799    this_hdr->sh_type = SHT_NOBITS;
1800  else
1801    this_hdr->sh_type = SHT_PROGBITS;
1802
1803  if ((asect->flags & SEC_ALLOC) != 0)
1804    this_hdr->sh_flags |= SHF_ALLOC;
1805  if ((asect->flags & SEC_READONLY) == 0)
1806    this_hdr->sh_flags |= SHF_WRITE;
1807  if ((asect->flags & SEC_CODE) != 0)
1808    this_hdr->sh_flags |= SHF_EXECINSTR;
1809
1810  /* Check for processor-specific section types.  */
1811  if (bed->elf_backend_fake_sections)
1812    (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1813
1814  /* If the section has relocs, set up a section header for the
1815     SHT_REL[A] section.  If two relocation sections are required for
1816     this section, it is up to the processor-specific back-end to
1817     create the other.  */
1818  if ((asect->flags & SEC_RELOC) != 0
1819      && !_bfd_elf_init_reloc_shdr (abfd,
1820				    &elf_section_data (asect)->rel_hdr,
1821				    asect,
1822				    elf_section_data (asect)->use_rela_p))
1823    *failedptr = true;
1824}
1825
1826/* Assign all ELF section numbers.  The dummy first section is handled here
1827   too.  The link/info pointers for the standard section types are filled
1828   in here too, while we're at it.  */
1829
1830static boolean
1831assign_section_numbers (abfd)
1832     bfd *abfd;
1833{
1834  struct elf_obj_tdata *t = elf_tdata (abfd);
1835  asection *sec;
1836  unsigned int section_number;
1837  Elf_Internal_Shdr **i_shdrp;
1838
1839  section_number = 1;
1840
1841  for (sec = abfd->sections; sec; sec = sec->next)
1842    {
1843      struct bfd_elf_section_data *d = elf_section_data (sec);
1844
1845      d->this_idx = section_number++;
1846      if ((sec->flags & SEC_RELOC) == 0)
1847	d->rel_idx = 0;
1848      else
1849	d->rel_idx = section_number++;
1850
1851      if (d->rel_hdr2)
1852	d->rel_idx2 = section_number++;
1853      else
1854	d->rel_idx2 = 0;
1855    }
1856
1857  t->shstrtab_section = section_number++;
1858  elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1859  t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1860
1861  if (bfd_get_symcount (abfd) > 0)
1862    {
1863      t->symtab_section = section_number++;
1864      t->strtab_section = section_number++;
1865    }
1866
1867  elf_elfheader (abfd)->e_shnum = section_number;
1868
1869  /* Set up the list of section header pointers, in agreement with the
1870     indices.  */
1871  i_shdrp = ((Elf_Internal_Shdr **)
1872	     bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1873  if (i_shdrp == NULL)
1874    return false;
1875
1876  i_shdrp[0] = ((Elf_Internal_Shdr *)
1877		bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1878  if (i_shdrp[0] == NULL)
1879    {
1880      bfd_release (abfd, i_shdrp);
1881      return false;
1882    }
1883  memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1884
1885  elf_elfsections (abfd) = i_shdrp;
1886
1887  i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1888  if (bfd_get_symcount (abfd) > 0)
1889    {
1890      i_shdrp[t->symtab_section] = &t->symtab_hdr;
1891      i_shdrp[t->strtab_section] = &t->strtab_hdr;
1892      t->symtab_hdr.sh_link = t->strtab_section;
1893    }
1894  for (sec = abfd->sections; sec; sec = sec->next)
1895    {
1896      struct bfd_elf_section_data *d = elf_section_data (sec);
1897      asection *s;
1898      const char *name;
1899
1900      i_shdrp[d->this_idx] = &d->this_hdr;
1901      if (d->rel_idx != 0)
1902	i_shdrp[d->rel_idx] = &d->rel_hdr;
1903      if (d->rel_idx2 != 0)
1904	i_shdrp[d->rel_idx2] = d->rel_hdr2;
1905
1906      /* Fill in the sh_link and sh_info fields while we're at it.  */
1907
1908      /* sh_link of a reloc section is the section index of the symbol
1909	 table.  sh_info is the section index of the section to which
1910	 the relocation entries apply.  */
1911      if (d->rel_idx != 0)
1912	{
1913	  d->rel_hdr.sh_link = t->symtab_section;
1914	  d->rel_hdr.sh_info = d->this_idx;
1915	}
1916      if (d->rel_idx2 != 0)
1917	{
1918	  d->rel_hdr2->sh_link = t->symtab_section;
1919	  d->rel_hdr2->sh_info = d->this_idx;
1920	}
1921
1922      switch (d->this_hdr.sh_type)
1923	{
1924	case SHT_REL:
1925	case SHT_RELA:
1926	  /* A reloc section which we are treating as a normal BFD
1927	     section.  sh_link is the section index of the symbol
1928	     table.  sh_info is the section index of the section to
1929	     which the relocation entries apply.  We assume that an
1930	     allocated reloc section uses the dynamic symbol table.
1931	     FIXME: How can we be sure?  */
1932	  s = bfd_get_section_by_name (abfd, ".dynsym");
1933	  if (s != NULL)
1934	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1935
1936	  /* We look up the section the relocs apply to by name.  */
1937	  name = sec->name;
1938	  if (d->this_hdr.sh_type == SHT_REL)
1939	    name += 4;
1940	  else
1941	    name += 5;
1942	  s = bfd_get_section_by_name (abfd, name);
1943	  if (s != NULL)
1944	    d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1945	  break;
1946
1947	case SHT_STRTAB:
1948	  /* We assume that a section named .stab*str is a stabs
1949	     string section.  We look for a section with the same name
1950	     but without the trailing ``str'', and set its sh_link
1951	     field to point to this section.  */
1952	  if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1953	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1954	    {
1955	      size_t len;
1956	      char *alc;
1957
1958	      len = strlen (sec->name);
1959	      alc = (char *) bfd_malloc (len - 2);
1960	      if (alc == NULL)
1961		return false;
1962	      strncpy (alc, sec->name, len - 3);
1963	      alc[len - 3] = '\0';
1964	      s = bfd_get_section_by_name (abfd, alc);
1965	      free (alc);
1966	      if (s != NULL)
1967		{
1968		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1969
1970		  /* This is a .stab section.  */
1971		  elf_section_data (s)->this_hdr.sh_entsize =
1972		    4 + 2 * bfd_get_arch_size (abfd) / 8;
1973		}
1974	    }
1975	  break;
1976
1977	case SHT_DYNAMIC:
1978	case SHT_DYNSYM:
1979	case SHT_GNU_verneed:
1980	case SHT_GNU_verdef:
1981	  /* sh_link is the section header index of the string table
1982	     used for the dynamic entries, or the symbol table, or the
1983	     version strings.  */
1984	  s = bfd_get_section_by_name (abfd, ".dynstr");
1985	  if (s != NULL)
1986	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1987	  break;
1988
1989	case SHT_HASH:
1990	case SHT_GNU_versym:
1991	  /* sh_link is the section header index of the symbol table
1992	     this hash table or version table is for.  */
1993	  s = bfd_get_section_by_name (abfd, ".dynsym");
1994	  if (s != NULL)
1995	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1996	  break;
1997	}
1998    }
1999
2000  return true;
2001}
2002
2003/* Map symbol from it's internal number to the external number, moving
2004   all local symbols to be at the head of the list.  */
2005
2006static INLINE int
2007sym_is_global (abfd, sym)
2008     bfd *abfd;
2009     asymbol *sym;
2010{
2011  /* If the backend has a special mapping, use it.  */
2012  if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2013    return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2014	    (abfd, sym));
2015
2016  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2017	  || bfd_is_und_section (bfd_get_section (sym))
2018	  || bfd_is_com_section (bfd_get_section (sym)));
2019}
2020
2021static boolean
2022elf_map_symbols (abfd)
2023     bfd *abfd;
2024{
2025  int symcount = bfd_get_symcount (abfd);
2026  asymbol **syms = bfd_get_outsymbols (abfd);
2027  asymbol **sect_syms;
2028  int num_locals = 0;
2029  int num_globals = 0;
2030  int num_locals2 = 0;
2031  int num_globals2 = 0;
2032  int max_index = 0;
2033  int num_sections = 0;
2034  int idx;
2035  asection *asect;
2036  asymbol **new_syms;
2037  asymbol *sym;
2038
2039#ifdef DEBUG
2040  fprintf (stderr, "elf_map_symbols\n");
2041  fflush (stderr);
2042#endif
2043
2044  /* Add a section symbol for each BFD section.  FIXME: Is this really
2045     necessary?  */
2046  for (asect = abfd->sections; asect; asect = asect->next)
2047    {
2048      if (max_index < asect->index)
2049	max_index = asect->index;
2050    }
2051
2052  max_index++;
2053  sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2054  if (sect_syms == NULL)
2055    return false;
2056  elf_section_syms (abfd) = sect_syms;
2057
2058  for (idx = 0; idx < symcount; idx++)
2059    {
2060      sym = syms[idx];
2061
2062      if ((sym->flags & BSF_SECTION_SYM) != 0
2063	  && sym->value == 0)
2064	{
2065	  asection *sec;
2066
2067	  sec = sym->section;
2068
2069	  if (sec->owner != NULL)
2070	    {
2071	      if (sec->owner != abfd)
2072		{
2073		  if (sec->output_offset != 0)
2074		    continue;
2075
2076		  sec = sec->output_section;
2077
2078		  /* Empty sections in the input files may have had a section
2079		     symbol created for them.  (See the comment near the end of
2080		     _bfd_generic_link_output_symbols in linker.c).  If the linker
2081		     script discards such sections then we will reach this point.
2082		     Since we know that we cannot avoid this case, we detect it
2083		     and skip the abort and the assignment to the sect_syms array.
2084		     To reproduce this particular case try running the linker
2085		     testsuite test ld-scripts/weak.exp for an ELF port that uses
2086		     the generic linker.  */
2087		  if (sec->owner == NULL)
2088		    continue;
2089
2090		  BFD_ASSERT (sec->owner == abfd);
2091		}
2092	      sect_syms[sec->index] = syms[idx];
2093	    }
2094	}
2095    }
2096
2097  for (asect = abfd->sections; asect; asect = asect->next)
2098    {
2099      if (sect_syms[asect->index] != NULL)
2100	continue;
2101
2102      sym = bfd_make_empty_symbol (abfd);
2103      if (sym == NULL)
2104	return false;
2105      sym->the_bfd = abfd;
2106      sym->name = asect->name;
2107      sym->value = 0;
2108      /* Set the flags to 0 to indicate that this one was newly added.  */
2109      sym->flags = 0;
2110      sym->section = asect;
2111      sect_syms[asect->index] = sym;
2112      num_sections++;
2113#ifdef DEBUG
2114      fprintf (stderr,
2115 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2116	       asect->name, (long) asect->vma, asect->index, (long) asect);
2117#endif
2118    }
2119
2120  /* Classify all of the symbols.  */
2121  for (idx = 0; idx < symcount; idx++)
2122    {
2123      if (!sym_is_global (abfd, syms[idx]))
2124	num_locals++;
2125      else
2126	num_globals++;
2127    }
2128  for (asect = abfd->sections; asect; asect = asect->next)
2129    {
2130      if (sect_syms[asect->index] != NULL
2131	  && sect_syms[asect->index]->flags == 0)
2132	{
2133	  sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2134	  if (!sym_is_global (abfd, sect_syms[asect->index]))
2135	    num_locals++;
2136	  else
2137	    num_globals++;
2138	  sect_syms[asect->index]->flags = 0;
2139	}
2140    }
2141
2142  /* Now sort the symbols so the local symbols are first.  */
2143  new_syms = ((asymbol **)
2144	      bfd_alloc (abfd,
2145			 (num_locals + num_globals) * sizeof (asymbol *)));
2146  if (new_syms == NULL)
2147    return false;
2148
2149  for (idx = 0; idx < symcount; idx++)
2150    {
2151      asymbol *sym = syms[idx];
2152      int i;
2153
2154      if (!sym_is_global (abfd, sym))
2155	i = num_locals2++;
2156      else
2157	i = num_locals + num_globals2++;
2158      new_syms[i] = sym;
2159      sym->udata.i = i + 1;
2160    }
2161  for (asect = abfd->sections; asect; asect = asect->next)
2162    {
2163      if (sect_syms[asect->index] != NULL
2164	  && sect_syms[asect->index]->flags == 0)
2165	{
2166	  asymbol *sym = sect_syms[asect->index];
2167	  int i;
2168
2169	  sym->flags = BSF_SECTION_SYM;
2170	  if (!sym_is_global (abfd, sym))
2171	    i = num_locals2++;
2172	  else
2173	    i = num_locals + num_globals2++;
2174	  new_syms[i] = sym;
2175	  sym->udata.i = i + 1;
2176	}
2177    }
2178
2179  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2180
2181  elf_num_locals (abfd) = num_locals;
2182  elf_num_globals (abfd) = num_globals;
2183  return true;
2184}
2185
2186/* Align to the maximum file alignment that could be required for any
2187   ELF data structure.  */
2188
2189static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2190static INLINE file_ptr
2191align_file_position (off, align)
2192     file_ptr off;
2193     int align;
2194{
2195  return (off + align - 1) & ~(align - 1);
2196}
2197
2198/* Assign a file position to a section, optionally aligning to the
2199   required section alignment.  */
2200
2201INLINE file_ptr
2202_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2203     Elf_Internal_Shdr *i_shdrp;
2204     file_ptr offset;
2205     boolean align;
2206{
2207  if (align)
2208    {
2209      unsigned int al;
2210
2211      al = i_shdrp->sh_addralign;
2212      if (al > 1)
2213	offset = BFD_ALIGN (offset, al);
2214    }
2215  i_shdrp->sh_offset = offset;
2216  if (i_shdrp->bfd_section != NULL)
2217    i_shdrp->bfd_section->filepos = offset;
2218  if (i_shdrp->sh_type != SHT_NOBITS)
2219    offset += i_shdrp->sh_size;
2220  return offset;
2221}
2222
2223/* Compute the file positions we are going to put the sections at, and
2224   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
2225   is not NULL, this is being called by the ELF backend linker.  */
2226
2227boolean
2228_bfd_elf_compute_section_file_positions (abfd, link_info)
2229     bfd *abfd;
2230     struct bfd_link_info *link_info;
2231{
2232  struct elf_backend_data *bed = get_elf_backend_data (abfd);
2233  boolean failed;
2234  struct bfd_strtab_hash *strtab;
2235  Elf_Internal_Shdr *shstrtab_hdr;
2236
2237  if (abfd->output_has_begun)
2238    return true;
2239
2240  /* Do any elf backend specific processing first.  */
2241  if (bed->elf_backend_begin_write_processing)
2242    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2243
2244  if (! prep_headers (abfd))
2245    return false;
2246
2247  /* Post process the headers if necessary.  */
2248  if (bed->elf_backend_post_process_headers)
2249    (*bed->elf_backend_post_process_headers) (abfd, link_info);
2250
2251  failed = false;
2252  bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2253  if (failed)
2254    return false;
2255
2256  if (!assign_section_numbers (abfd))
2257    return false;
2258
2259  /* The backend linker builds symbol table information itself.  */
2260  if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2261    {
2262      /* Non-zero if doing a relocatable link.  */
2263      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2264
2265      if (! swap_out_syms (abfd, &strtab, relocatable_p))
2266	return false;
2267    }
2268
2269  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2270  /* sh_name was set in prep_headers.  */
2271  shstrtab_hdr->sh_type = SHT_STRTAB;
2272  shstrtab_hdr->sh_flags = 0;
2273  shstrtab_hdr->sh_addr = 0;
2274  shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2275  shstrtab_hdr->sh_entsize = 0;
2276  shstrtab_hdr->sh_link = 0;
2277  shstrtab_hdr->sh_info = 0;
2278  /* sh_offset is set in assign_file_positions_except_relocs.  */
2279  shstrtab_hdr->sh_addralign = 1;
2280
2281  if (!assign_file_positions_except_relocs (abfd))
2282    return false;
2283
2284  if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2285    {
2286      file_ptr off;
2287      Elf_Internal_Shdr *hdr;
2288
2289      off = elf_tdata (abfd)->next_file_pos;
2290
2291      hdr = &elf_tdata (abfd)->symtab_hdr;
2292      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2293
2294      hdr = &elf_tdata (abfd)->strtab_hdr;
2295      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2296
2297      elf_tdata (abfd)->next_file_pos = off;
2298
2299      /* Now that we know where the .strtab section goes, write it
2300         out.  */
2301      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2302	  || ! _bfd_stringtab_emit (abfd, strtab))
2303	return false;
2304      _bfd_stringtab_free (strtab);
2305    }
2306
2307  abfd->output_has_begun = true;
2308
2309  return true;
2310}
2311
2312/* Create a mapping from a set of sections to a program segment.  */
2313
2314static INLINE struct elf_segment_map *
2315make_mapping (abfd, sections, from, to, phdr)
2316     bfd *abfd;
2317     asection **sections;
2318     unsigned int from;
2319     unsigned int to;
2320     boolean phdr;
2321{
2322  struct elf_segment_map *m;
2323  unsigned int i;
2324  asection **hdrpp;
2325
2326  m = ((struct elf_segment_map *)
2327       bfd_zalloc (abfd,
2328		   (sizeof (struct elf_segment_map)
2329		    + (to - from - 1) * sizeof (asection *))));
2330  if (m == NULL)
2331    return NULL;
2332  m->next = NULL;
2333  m->p_type = PT_LOAD;
2334  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2335    m->sections[i - from] = *hdrpp;
2336  m->count = to - from;
2337
2338  if (from == 0 && phdr)
2339    {
2340      /* Include the headers in the first PT_LOAD segment.  */
2341      m->includes_filehdr = 1;
2342      m->includes_phdrs = 1;
2343    }
2344
2345  return m;
2346}
2347
2348/* Set up a mapping from BFD sections to program segments.  */
2349
2350static boolean
2351map_sections_to_segments (abfd)
2352     bfd *abfd;
2353{
2354  asection **sections = NULL;
2355  asection *s;
2356  unsigned int i;
2357  unsigned int count;
2358  struct elf_segment_map *mfirst;
2359  struct elf_segment_map **pm;
2360  struct elf_segment_map *m;
2361  asection *last_hdr;
2362  unsigned int phdr_index;
2363  bfd_vma maxpagesize;
2364  asection **hdrpp;
2365  boolean phdr_in_segment = true;
2366  boolean writable;
2367  asection *dynsec;
2368
2369  if (elf_tdata (abfd)->segment_map != NULL)
2370    return true;
2371
2372  if (bfd_count_sections (abfd) == 0)
2373    return true;
2374
2375  /* Select the allocated sections, and sort them.  */
2376
2377  sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2378				       * sizeof (asection *));
2379  if (sections == NULL)
2380    goto error_return;
2381
2382  i = 0;
2383  for (s = abfd->sections; s != NULL; s = s->next)
2384    {
2385      if ((s->flags & SEC_ALLOC) != 0)
2386	{
2387	  sections[i] = s;
2388	  ++i;
2389	}
2390    }
2391  BFD_ASSERT (i <= bfd_count_sections (abfd));
2392  count = i;
2393
2394  qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2395
2396  /* Build the mapping.  */
2397
2398  mfirst = NULL;
2399  pm = &mfirst;
2400
2401  /* If we have a .interp section, then create a PT_PHDR segment for
2402     the program headers and a PT_INTERP segment for the .interp
2403     section.  */
2404  s = bfd_get_section_by_name (abfd, ".interp");
2405  if (s != NULL && (s->flags & SEC_LOAD) != 0)
2406    {
2407      m = ((struct elf_segment_map *)
2408	   bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2409      if (m == NULL)
2410	goto error_return;
2411      m->next = NULL;
2412      m->p_type = PT_PHDR;
2413      /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
2414      m->p_flags = PF_R | PF_X;
2415      m->p_flags_valid = 1;
2416      m->includes_phdrs = 1;
2417
2418      *pm = m;
2419      pm = &m->next;
2420
2421      m = ((struct elf_segment_map *)
2422	   bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2423      if (m == NULL)
2424	goto error_return;
2425      m->next = NULL;
2426      m->p_type = PT_INTERP;
2427      m->count = 1;
2428      m->sections[0] = s;
2429
2430      *pm = m;
2431      pm = &m->next;
2432    }
2433
2434  /* Look through the sections.  We put sections in the same program
2435     segment when the start of the second section can be placed within
2436     a few bytes of the end of the first section.  */
2437  last_hdr = NULL;
2438  phdr_index = 0;
2439  maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2440  writable = false;
2441  dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2442  if (dynsec != NULL
2443      && (dynsec->flags & SEC_LOAD) == 0)
2444    dynsec = NULL;
2445
2446  /* Deal with -Ttext or something similar such that the first section
2447     is not adjacent to the program headers.  This is an
2448     approximation, since at this point we don't know exactly how many
2449     program headers we will need.  */
2450  if (count > 0)
2451    {
2452      bfd_size_type phdr_size;
2453
2454      phdr_size = elf_tdata (abfd)->program_header_size;
2455      if (phdr_size == 0)
2456	phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2457      if ((abfd->flags & D_PAGED) == 0
2458	  || sections[0]->lma < phdr_size
2459	  || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2460	phdr_in_segment = false;
2461    }
2462
2463  for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2464    {
2465      asection *hdr;
2466      boolean new_segment;
2467
2468      hdr = *hdrpp;
2469
2470      /* See if this section and the last one will fit in the same
2471         segment.  */
2472
2473      if (last_hdr == NULL)
2474	{
2475	  /* If we don't have a segment yet, then we don't need a new
2476	     one (we build the last one after this loop).  */
2477	  new_segment = false;
2478	}
2479      else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2480	{
2481	  /* If this section has a different relation between the
2482             virtual address and the load address, then we need a new
2483             segment.  */
2484	  new_segment = true;
2485	}
2486      else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2487	       < BFD_ALIGN (hdr->lma, maxpagesize))
2488	{
2489	  /* If putting this section in this segment would force us to
2490             skip a page in the segment, then we need a new segment.  */
2491	  new_segment = true;
2492	}
2493      else if ((last_hdr->flags & SEC_LOAD) == 0
2494	       && (hdr->flags & SEC_LOAD) != 0)
2495	{
2496	  /* We don't want to put a loadable section after a
2497             nonloadable section in the same segment.  */
2498	  new_segment = true;
2499	}
2500      else if ((abfd->flags & D_PAGED) == 0)
2501	{
2502	  /* If the file is not demand paged, which means that we
2503             don't require the sections to be correctly aligned in the
2504             file, then there is no other reason for a new segment.  */
2505	  new_segment = false;
2506	}
2507      else if (! writable
2508	       && (hdr->flags & SEC_READONLY) == 0
2509	       && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2510		   == hdr->lma))
2511	{
2512	  /* We don't want to put a writable section in a read only
2513             segment, unless they are on the same page in memory
2514             anyhow.  We already know that the last section does not
2515             bring us past the current section on the page, so the
2516             only case in which the new section is not on the same
2517             page as the previous section is when the previous section
2518             ends precisely on a page boundary.  */
2519	  new_segment = true;
2520	}
2521      else
2522	{
2523	  /* Otherwise, we can use the same segment.  */
2524	  new_segment = false;
2525	}
2526
2527      if (! new_segment)
2528	{
2529	  if ((hdr->flags & SEC_READONLY) == 0)
2530	    writable = true;
2531	  last_hdr = hdr;
2532	  continue;
2533	}
2534
2535      /* We need a new program segment.  We must create a new program
2536         header holding all the sections from phdr_index until hdr.  */
2537
2538      m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2539      if (m == NULL)
2540	goto error_return;
2541
2542      *pm = m;
2543      pm = &m->next;
2544
2545      if ((hdr->flags & SEC_READONLY) == 0)
2546	writable = true;
2547      else
2548	writable = false;
2549
2550      last_hdr = hdr;
2551      phdr_index = i;
2552      phdr_in_segment = false;
2553    }
2554
2555  /* Create a final PT_LOAD program segment.  */
2556  if (last_hdr != NULL)
2557    {
2558      m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2559      if (m == NULL)
2560	goto error_return;
2561
2562      *pm = m;
2563      pm = &m->next;
2564    }
2565
2566  /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
2567  if (dynsec != NULL)
2568    {
2569      m = ((struct elf_segment_map *)
2570	   bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2571      if (m == NULL)
2572	goto error_return;
2573      m->next = NULL;
2574      m->p_type = PT_DYNAMIC;
2575      m->count = 1;
2576      m->sections[0] = dynsec;
2577
2578      *pm = m;
2579      pm = &m->next;
2580    }
2581
2582  /* For each loadable .note section, add a PT_NOTE segment.  We don't
2583     use bfd_get_section_by_name, because if we link together
2584     nonloadable .note sections and loadable .note sections, we will
2585     generate two .note sections in the output file.  FIXME: Using
2586     names for section types is bogus anyhow.  */
2587  for (s = abfd->sections; s != NULL; s = s->next)
2588    {
2589      if ((s->flags & SEC_LOAD) != 0
2590	  && strncmp (s->name, ".note", 5) == 0)
2591	{
2592	  m = ((struct elf_segment_map *)
2593	       bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2594	  if (m == NULL)
2595	    goto error_return;
2596	  m->next = NULL;
2597	  m->p_type = PT_NOTE;
2598	  m->count = 1;
2599	  m->sections[0] = s;
2600
2601	  *pm = m;
2602	  pm = &m->next;
2603	}
2604    }
2605
2606  free (sections);
2607  sections = NULL;
2608
2609  elf_tdata (abfd)->segment_map = mfirst;
2610  return true;
2611
2612 error_return:
2613  if (sections != NULL)
2614    free (sections);
2615  return false;
2616}
2617
2618/* Sort sections by address.  */
2619
2620static int
2621elf_sort_sections (arg1, arg2)
2622     const PTR arg1;
2623     const PTR arg2;
2624{
2625  const asection *sec1 = *(const asection **) arg1;
2626  const asection *sec2 = *(const asection **) arg2;
2627
2628  /* Sort by LMA first, since this is the address used to
2629     place the section into a segment.  */
2630  if (sec1->lma < sec2->lma)
2631    return -1;
2632  else if (sec1->lma > sec2->lma)
2633    return 1;
2634
2635  /* Then sort by VMA.  Normally the LMA and the VMA will be
2636     the same, and this will do nothing.  */
2637  if (sec1->vma < sec2->vma)
2638    return -1;
2639  else if (sec1->vma > sec2->vma)
2640    return 1;
2641
2642  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
2643
2644#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2645
2646  if (TOEND (sec1))
2647    {
2648      if (TOEND (sec2))
2649	return sec1->target_index - sec2->target_index;
2650      else
2651	return 1;
2652    }
2653
2654  if (TOEND (sec2))
2655    return -1;
2656
2657#undef TOEND
2658
2659  /* Sort by size, to put zero sized sections before others at the
2660     same address.  */
2661
2662  if (sec1->_raw_size < sec2->_raw_size)
2663    return -1;
2664  if (sec1->_raw_size > sec2->_raw_size)
2665    return 1;
2666
2667  return sec1->target_index - sec2->target_index;
2668}
2669
2670/* Assign file positions to the sections based on the mapping from
2671   sections to segments.  This function also sets up some fields in
2672   the file header, and writes out the program headers.  */
2673
2674static boolean
2675assign_file_positions_for_segments (abfd)
2676     bfd *abfd;
2677{
2678  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2679  unsigned int count;
2680  struct elf_segment_map *m;
2681  unsigned int alloc;
2682  Elf_Internal_Phdr *phdrs;
2683  file_ptr off, voff;
2684  bfd_vma filehdr_vaddr, filehdr_paddr;
2685  bfd_vma phdrs_vaddr, phdrs_paddr;
2686  Elf_Internal_Phdr *p;
2687
2688  if (elf_tdata (abfd)->segment_map == NULL)
2689    {
2690      if (! map_sections_to_segments (abfd))
2691	return false;
2692    }
2693
2694  if (bed->elf_backend_modify_segment_map)
2695    {
2696      if (! (*bed->elf_backend_modify_segment_map) (abfd))
2697	return false;
2698    }
2699
2700  count = 0;
2701  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2702    ++count;
2703
2704  elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2705  elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2706  elf_elfheader (abfd)->e_phnum = count;
2707
2708  if (count == 0)
2709    return true;
2710
2711  /* If we already counted the number of program segments, make sure
2712     that we allocated enough space.  This happens when SIZEOF_HEADERS
2713     is used in a linker script.  */
2714  alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2715  if (alloc != 0 && count > alloc)
2716    {
2717      ((*_bfd_error_handler)
2718       (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2719	bfd_get_filename (abfd), alloc, count));
2720      bfd_set_error (bfd_error_bad_value);
2721      return false;
2722    }
2723
2724  if (alloc == 0)
2725    alloc = count;
2726
2727  phdrs = ((Elf_Internal_Phdr *)
2728	   bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2729  if (phdrs == NULL)
2730    return false;
2731
2732  off = bed->s->sizeof_ehdr;
2733  off += alloc * bed->s->sizeof_phdr;
2734
2735  filehdr_vaddr = 0;
2736  filehdr_paddr = 0;
2737  phdrs_vaddr = 0;
2738  phdrs_paddr = 0;
2739
2740  for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2741       m != NULL;
2742       m = m->next, p++)
2743    {
2744      unsigned int i;
2745      asection **secpp;
2746
2747      /* If elf_segment_map is not from map_sections_to_segments, the
2748         sections may not be correctly ordered.  */
2749      if (m->count > 0)
2750	qsort (m->sections, (size_t) m->count, sizeof (asection *),
2751	       elf_sort_sections);
2752
2753      p->p_type = m->p_type;
2754      p->p_flags = m->p_flags;
2755
2756      if (p->p_type == PT_LOAD
2757	  && m->count > 0
2758	  && (m->sections[0]->flags & SEC_ALLOC) != 0)
2759	{
2760	  if ((abfd->flags & D_PAGED) != 0)
2761	    off += (m->sections[0]->vma - off) % bed->maxpagesize;
2762	  else
2763	    {
2764	      bfd_size_type align;
2765
2766	      align = 0;
2767	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2768		{
2769		  bfd_size_type secalign;
2770
2771		  secalign = bfd_get_section_alignment (abfd, *secpp);
2772		  if (secalign > align)
2773		    align = secalign;
2774		}
2775
2776	      off += (m->sections[0]->vma - off) % (1 << align);
2777	    }
2778	}
2779
2780      if (m->count == 0)
2781	p->p_vaddr = 0;
2782      else
2783	p->p_vaddr = m->sections[0]->vma;
2784
2785      if (m->p_paddr_valid)
2786	p->p_paddr = m->p_paddr;
2787      else if (m->count == 0)
2788	p->p_paddr = 0;
2789      else
2790	p->p_paddr = m->sections[0]->lma;
2791
2792      if (p->p_type == PT_LOAD
2793	  && (abfd->flags & D_PAGED) != 0)
2794	p->p_align = bed->maxpagesize;
2795      else if (m->count == 0)
2796	p->p_align = bed->s->file_align;
2797      else
2798	p->p_align = 0;
2799
2800      p->p_offset = 0;
2801      p->p_filesz = 0;
2802      p->p_memsz = 0;
2803
2804      if (m->includes_filehdr)
2805	{
2806	  if (! m->p_flags_valid)
2807	    p->p_flags |= PF_R;
2808	  p->p_offset = 0;
2809	  p->p_filesz = bed->s->sizeof_ehdr;
2810	  p->p_memsz = bed->s->sizeof_ehdr;
2811	  if (m->count > 0)
2812	    {
2813	      BFD_ASSERT (p->p_type == PT_LOAD);
2814
2815	      if (p->p_vaddr < (bfd_vma) off)
2816		{
2817		  _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2818				      bfd_get_filename (abfd));
2819		  bfd_set_error (bfd_error_bad_value);
2820		  return false;
2821		}
2822
2823	      p->p_vaddr -= off;
2824	      if (! m->p_paddr_valid)
2825		p->p_paddr -= off;
2826	    }
2827	  if (p->p_type == PT_LOAD)
2828	    {
2829	      filehdr_vaddr = p->p_vaddr;
2830	      filehdr_paddr = p->p_paddr;
2831	    }
2832	}
2833
2834      if (m->includes_phdrs)
2835	{
2836	  if (! m->p_flags_valid)
2837	    p->p_flags |= PF_R;
2838
2839	  if (m->includes_filehdr)
2840	    {
2841	      if (p->p_type == PT_LOAD)
2842		{
2843		  phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2844		  phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2845		}
2846	    }
2847	  else
2848	    {
2849	      p->p_offset = bed->s->sizeof_ehdr;
2850
2851	      if (m->count > 0)
2852		{
2853		  BFD_ASSERT (p->p_type == PT_LOAD);
2854		  p->p_vaddr -= off - p->p_offset;
2855		  if (! m->p_paddr_valid)
2856		    p->p_paddr -= off - p->p_offset;
2857		}
2858
2859	      if (p->p_type == PT_LOAD)
2860		{
2861		  phdrs_vaddr = p->p_vaddr;
2862		  phdrs_paddr = p->p_paddr;
2863		}
2864	      else
2865		phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2866	    }
2867
2868	  p->p_filesz += alloc * bed->s->sizeof_phdr;
2869	  p->p_memsz += alloc * bed->s->sizeof_phdr;
2870	}
2871
2872      if (p->p_type == PT_LOAD
2873	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2874	{
2875	  if (! m->includes_filehdr && ! m->includes_phdrs)
2876	    p->p_offset = off;
2877	  else
2878	    {
2879	      file_ptr adjust;
2880
2881	      adjust = off - (p->p_offset + p->p_filesz);
2882	      p->p_filesz += adjust;
2883	      p->p_memsz += adjust;
2884	    }
2885	}
2886
2887      voff = off;
2888
2889      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2890	{
2891	  asection *sec;
2892	  flagword flags;
2893	  bfd_size_type align;
2894
2895	  sec = *secpp;
2896	  flags = sec->flags;
2897	  align = 1 << bfd_get_section_alignment (abfd, sec);
2898
2899	  /* The section may have artificial alignment forced by a
2900	     link script.  Notice this case by the gap between the
2901	     cumulative phdr vma and the section's vma.  */
2902	  if (p->p_vaddr + p->p_memsz < sec->vma)
2903	    {
2904	      bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2905
2906	      p->p_memsz += adjust;
2907	      off += adjust;
2908	      voff += adjust;
2909	      if ((flags & SEC_LOAD) != 0)
2910		p->p_filesz += adjust;
2911	    }
2912
2913	  if (p->p_type == PT_LOAD)
2914	    {
2915	      bfd_signed_vma adjust;
2916
2917	      if ((flags & SEC_LOAD) != 0)
2918		{
2919		  adjust = sec->lma - (p->p_paddr + p->p_memsz);
2920		  if (adjust < 0)
2921		    adjust = 0;
2922		}
2923	      else if ((flags & SEC_ALLOC) != 0)
2924		{
2925		  /* The section VMA must equal the file position
2926		     modulo the page size.  FIXME: I'm not sure if
2927		     this adjustment is really necessary.  We used to
2928		     not have the SEC_LOAD case just above, and then
2929		     this was necessary, but now I'm not sure.  */
2930		  if ((abfd->flags & D_PAGED) != 0)
2931		    adjust = (sec->vma - voff) % bed->maxpagesize;
2932		  else
2933		    adjust = (sec->vma - voff) % align;
2934		}
2935	      else
2936		adjust = 0;
2937
2938	      if (adjust != 0)
2939		{
2940		  if (i == 0)
2941		    {
2942		      (* _bfd_error_handler)
2943			(_("Error: First section in segment (%s) starts at 0x%x"),
2944			 bfd_section_name (abfd, sec), sec->lma);
2945		      (* _bfd_error_handler)
2946			(_("       whereas segment starts at 0x%x"),
2947			 p->p_paddr);
2948
2949		      return false;
2950		    }
2951		  p->p_memsz += adjust;
2952		  off += adjust;
2953		  voff += adjust;
2954		  if ((flags & SEC_LOAD) != 0)
2955		    p->p_filesz += adjust;
2956		}
2957
2958	      sec->filepos = off;
2959
2960	      /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2961                 used in a linker script we may have a section with
2962                 SEC_LOAD clear but which is supposed to have
2963                 contents.  */
2964	      if ((flags & SEC_LOAD) != 0
2965		  || (flags & SEC_HAS_CONTENTS) != 0)
2966		off += sec->_raw_size;
2967
2968	      if ((flags & SEC_ALLOC) != 0)
2969		voff += sec->_raw_size;
2970	    }
2971
2972	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2973	    {
2974	      /* The actual "note" segment has i == 0.
2975		 This is the one that actually contains everything.  */
2976	      if (i == 0)
2977		{
2978		  sec->filepos = off;
2979		  p->p_filesz = sec->_raw_size;
2980		  off += sec->_raw_size;
2981		  voff = off;
2982		}
2983	      else
2984		{
2985		  /* Fake sections -- don't need to be written.  */
2986		  sec->filepos = 0;
2987		  sec->_raw_size = 0;
2988		  flags = sec->flags = 0;
2989		}
2990	      p->p_memsz = 0;
2991	      p->p_align = 1;
2992	    }
2993	  else
2994	    {
2995	      p->p_memsz += sec->_raw_size;
2996
2997	      if ((flags & SEC_LOAD) != 0)
2998		p->p_filesz += sec->_raw_size;
2999
3000	      if (align > p->p_align
3001		  && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3002		p->p_align = align;
3003	    }
3004
3005	  if (! m->p_flags_valid)
3006	    {
3007	      p->p_flags |= PF_R;
3008	      if ((flags & SEC_CODE) != 0)
3009		p->p_flags |= PF_X;
3010	      if ((flags & SEC_READONLY) == 0)
3011		p->p_flags |= PF_W;
3012	    }
3013	}
3014    }
3015
3016  /* Now that we have set the section file positions, we can set up
3017     the file positions for the non PT_LOAD segments.  */
3018  for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3019       m != NULL;
3020       m = m->next, p++)
3021    {
3022      if (p->p_type != PT_LOAD && m->count > 0)
3023	{
3024	  BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3025	  p->p_offset = m->sections[0]->filepos;
3026	}
3027      if (m->count == 0)
3028	{
3029	  if (m->includes_filehdr)
3030	    {
3031	      p->p_vaddr = filehdr_vaddr;
3032	      if (! m->p_paddr_valid)
3033		p->p_paddr = filehdr_paddr;
3034	    }
3035	  else if (m->includes_phdrs)
3036	    {
3037	      p->p_vaddr = phdrs_vaddr;
3038	      if (! m->p_paddr_valid)
3039		p->p_paddr = phdrs_paddr;
3040	    }
3041	}
3042    }
3043
3044  /* Clear out any program headers we allocated but did not use.  */
3045  for (; count < alloc; count++, p++)
3046    {
3047      memset (p, 0, sizeof *p);
3048      p->p_type = PT_NULL;
3049    }
3050
3051  elf_tdata (abfd)->phdr = phdrs;
3052
3053  elf_tdata (abfd)->next_file_pos = off;
3054
3055  /* Write out the program headers.  */
3056  if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3057      || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3058    return false;
3059
3060  return true;
3061}
3062
3063/* Get the size of the program header.
3064
3065   If this is called by the linker before any of the section VMA's are set, it
3066   can't calculate the correct value for a strange memory layout.  This only
3067   happens when SIZEOF_HEADERS is used in a linker script.  In this case,
3068   SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3069   data segment (exclusive of .interp and .dynamic).
3070
3071   ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3072   will be two segments.  */
3073
3074static bfd_size_type
3075get_program_header_size (abfd)
3076     bfd *abfd;
3077{
3078  size_t segs;
3079  asection *s;
3080  struct elf_backend_data *bed = get_elf_backend_data (abfd);
3081
3082  /* We can't return a different result each time we're called.  */
3083  if (elf_tdata (abfd)->program_header_size != 0)
3084    return elf_tdata (abfd)->program_header_size;
3085
3086  if (elf_tdata (abfd)->segment_map != NULL)
3087    {
3088      struct elf_segment_map *m;
3089
3090      segs = 0;
3091      for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3092	++segs;
3093      elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3094      return elf_tdata (abfd)->program_header_size;
3095    }
3096
3097  /* Assume we will need exactly two PT_LOAD segments: one for text
3098     and one for data.  */
3099  segs = 2;
3100
3101  s = bfd_get_section_by_name (abfd, ".interp");
3102  if (s != NULL && (s->flags & SEC_LOAD) != 0)
3103    {
3104      /* If we have a loadable interpreter section, we need a
3105	 PT_INTERP segment.  In this case, assume we also need a
3106	 PT_PHDR segment, although that may not be true for all
3107	 targets.  */
3108      segs += 2;
3109    }
3110
3111  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3112    {
3113      /* We need a PT_DYNAMIC segment.  */
3114      ++segs;
3115    }
3116
3117  for (s = abfd->sections; s != NULL; s = s->next)
3118    {
3119      if ((s->flags & SEC_LOAD) != 0
3120	  && strncmp (s->name, ".note", 5) == 0)
3121	{
3122	  /* We need a PT_NOTE segment.  */
3123	  ++segs;
3124	}
3125    }
3126
3127  /* Let the backend count up any program headers it might need.  */
3128  if (bed->elf_backend_additional_program_headers)
3129    {
3130      int a;
3131
3132      a = (*bed->elf_backend_additional_program_headers) (abfd);
3133      if (a == -1)
3134	abort ();
3135      segs += a;
3136    }
3137
3138  elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3139  return elf_tdata (abfd)->program_header_size;
3140}
3141
3142/* Work out the file positions of all the sections.  This is called by
3143   _bfd_elf_compute_section_file_positions.  All the section sizes and
3144   VMAs must be known before this is called.
3145
3146   We do not consider reloc sections at this point, unless they form
3147   part of the loadable image.  Reloc sections are assigned file
3148   positions in assign_file_positions_for_relocs, which is called by
3149   write_object_contents and final_link.
3150
3151   We also don't set the positions of the .symtab and .strtab here.  */
3152
3153static boolean
3154assign_file_positions_except_relocs (abfd)
3155     bfd *abfd;
3156{
3157  struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3158  Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3159  Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3160  file_ptr off;
3161  struct elf_backend_data *bed = get_elf_backend_data (abfd);
3162
3163  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3164      && bfd_get_format (abfd) != bfd_core)
3165    {
3166      Elf_Internal_Shdr **hdrpp;
3167      unsigned int i;
3168
3169      /* Start after the ELF header.  */
3170      off = i_ehdrp->e_ehsize;
3171
3172      /* We are not creating an executable, which means that we are
3173	 not creating a program header, and that the actual order of
3174	 the sections in the file is unimportant.  */
3175      for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3176	{
3177	  Elf_Internal_Shdr *hdr;
3178
3179	  hdr = *hdrpp;
3180	  if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3181	    {
3182	      hdr->sh_offset = -1;
3183	      continue;
3184	    }
3185	  if (i == tdata->symtab_section
3186	      || i == tdata->strtab_section)
3187	    {
3188	      hdr->sh_offset = -1;
3189	      continue;
3190	    }
3191
3192	  off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3193	}
3194    }
3195  else
3196    {
3197      unsigned int i;
3198      Elf_Internal_Shdr **hdrpp;
3199
3200      /* Assign file positions for the loaded sections based on the
3201         assignment of sections to segments.  */
3202      if (! assign_file_positions_for_segments (abfd))
3203	return false;
3204
3205      /* Assign file positions for the other sections.  */
3206
3207      off = elf_tdata (abfd)->next_file_pos;
3208      for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3209	{
3210	  Elf_Internal_Shdr *hdr;
3211
3212	  hdr = *hdrpp;
3213	  if (hdr->bfd_section != NULL
3214	      && hdr->bfd_section->filepos != 0)
3215	    hdr->sh_offset = hdr->bfd_section->filepos;
3216	  else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3217	    {
3218	      ((*_bfd_error_handler)
3219	       (_("%s: warning: allocated section `%s' not in segment"),
3220		bfd_get_filename (abfd),
3221		(hdr->bfd_section == NULL
3222		 ? "*unknown*"
3223		 : hdr->bfd_section->name)));
3224	      if ((abfd->flags & D_PAGED) != 0)
3225		off += (hdr->sh_addr - off) % bed->maxpagesize;
3226	      else
3227		off += (hdr->sh_addr - off) % hdr->sh_addralign;
3228	      off = _bfd_elf_assign_file_position_for_section (hdr, off,
3229							       false);
3230	    }
3231	  else if (hdr->sh_type == SHT_REL
3232		   || hdr->sh_type == SHT_RELA
3233		   || hdr == i_shdrpp[tdata->symtab_section]
3234		   || hdr == i_shdrpp[tdata->strtab_section])
3235	    hdr->sh_offset = -1;
3236	  else
3237	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3238	}
3239    }
3240
3241  /* Place the section headers.  */
3242  off = align_file_position (off, bed->s->file_align);
3243  i_ehdrp->e_shoff = off;
3244  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3245
3246  elf_tdata (abfd)->next_file_pos = off;
3247
3248  return true;
3249}
3250
3251static boolean
3252prep_headers (abfd)
3253     bfd *abfd;
3254{
3255  Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
3256  Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3257  Elf_Internal_Shdr **i_shdrp;	/* Section header table, internal form */
3258  int count;
3259  struct bfd_strtab_hash *shstrtab;
3260  struct elf_backend_data *bed = get_elf_backend_data (abfd);
3261
3262  i_ehdrp = elf_elfheader (abfd);
3263  i_shdrp = elf_elfsections (abfd);
3264
3265  shstrtab = _bfd_elf_stringtab_init ();
3266  if (shstrtab == NULL)
3267    return false;
3268
3269  elf_shstrtab (abfd) = shstrtab;
3270
3271  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3272  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3273  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3274  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3275
3276  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3277  i_ehdrp->e_ident[EI_DATA] =
3278    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3279  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3280
3281  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
3282  i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3283
3284  for (count = EI_PAD; count < EI_NIDENT; count++)
3285    i_ehdrp->e_ident[count] = 0;
3286
3287
3288#ifdef WANT_OLD_BRANDELF_METHOD
3289#define _OLD_EI_BRAND_OFFSET 8
3290#define _OLD_BRANDING	"FreeBSD"
3291  strncpy((char *) &i_ehdrp->e_ident[_OLD_EI_BRAND_OFFSET], _OLD_BRANDING,
3292	  EI_NIDENT-_OLD_EI_BRAND_OFFSET);
3293#endif
3294
3295
3296  if ((abfd->flags & DYNAMIC) != 0)
3297    i_ehdrp->e_type = ET_DYN;
3298  else if ((abfd->flags & EXEC_P) != 0)
3299    i_ehdrp->e_type = ET_EXEC;
3300  else if (bfd_get_format (abfd) == bfd_core)
3301    i_ehdrp->e_type = ET_CORE;
3302  else
3303    i_ehdrp->e_type = ET_REL;
3304
3305  switch (bfd_get_arch (abfd))
3306    {
3307    case bfd_arch_unknown:
3308      i_ehdrp->e_machine = EM_NONE;
3309      break;
3310    case bfd_arch_sparc:
3311      if (bfd_get_arch_size (abfd) == 64)
3312	i_ehdrp->e_machine = EM_SPARCV9;
3313      else
3314	i_ehdrp->e_machine = EM_SPARC;
3315      break;
3316    case bfd_arch_i370:
3317      i_ehdrp->e_machine = EM_S370;
3318      break;
3319    case bfd_arch_i386:
3320      if (bfd_get_arch_size (abfd) == 64)
3321	i_ehdrp->e_machine = EM_X86_64;
3322      else
3323	i_ehdrp->e_machine = EM_386;
3324      break;
3325    case bfd_arch_ia64:
3326      i_ehdrp->e_machine = EM_IA_64;
3327      break;
3328    case bfd_arch_m68hc11:
3329      i_ehdrp->e_machine = EM_68HC11;
3330      break;
3331    case bfd_arch_m68hc12:
3332      i_ehdrp->e_machine = EM_68HC12;
3333      break;
3334    case bfd_arch_m68k:
3335      i_ehdrp->e_machine = EM_68K;
3336      break;
3337    case bfd_arch_m88k:
3338      i_ehdrp->e_machine = EM_88K;
3339      break;
3340    case bfd_arch_i860:
3341      i_ehdrp->e_machine = EM_860;
3342      break;
3343    case bfd_arch_i960:
3344      i_ehdrp->e_machine = EM_960;
3345      break;
3346    case bfd_arch_mips:	/* MIPS Rxxxx */
3347      i_ehdrp->e_machine = EM_MIPS;	/* only MIPS R3000 */
3348      break;
3349    case bfd_arch_hppa:
3350      i_ehdrp->e_machine = EM_PARISC;
3351      break;
3352    case bfd_arch_powerpc:
3353      i_ehdrp->e_machine = EM_PPC;
3354      break;
3355    case bfd_arch_alpha:
3356      i_ehdrp->e_machine = EM_ALPHA;
3357      break;
3358    case bfd_arch_sh:
3359      i_ehdrp->e_machine = EM_SH;
3360      break;
3361    case bfd_arch_d10v:
3362      i_ehdrp->e_machine = EM_CYGNUS_D10V;
3363      break;
3364    case bfd_arch_d30v:
3365      i_ehdrp->e_machine = EM_CYGNUS_D30V;
3366      break;
3367    case bfd_arch_fr30:
3368      i_ehdrp->e_machine = EM_CYGNUS_FR30;
3369      break;
3370    case bfd_arch_mcore:
3371      i_ehdrp->e_machine = EM_MCORE;
3372      break;
3373    case bfd_arch_avr:
3374      i_ehdrp->e_machine = EM_AVR;
3375      break;
3376    case bfd_arch_v850:
3377      switch (bfd_get_mach (abfd))
3378	{
3379	default:
3380	case 0:               i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3381	}
3382      break;
3383    case bfd_arch_arc:
3384      i_ehdrp->e_machine = EM_CYGNUS_ARC;
3385      break;
3386    case bfd_arch_arm:
3387      i_ehdrp->e_machine = EM_ARM;
3388      break;
3389    case bfd_arch_m32r:
3390      i_ehdrp->e_machine = EM_CYGNUS_M32R;
3391      break;
3392    case bfd_arch_mn10200:
3393      i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3394      break;
3395    case bfd_arch_mn10300:
3396      i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3397      break;
3398    case bfd_arch_pj:
3399      i_ehdrp->e_machine = EM_PJ;
3400      break;
3401    case bfd_arch_cris:
3402      i_ehdrp->e_machine = EM_CRIS;
3403      break;
3404      /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3405    default:
3406      i_ehdrp->e_machine = EM_NONE;
3407    }
3408  i_ehdrp->e_version = bed->s->ev_current;
3409  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3410
3411  /* No program header, for now.  */
3412  i_ehdrp->e_phoff = 0;
3413  i_ehdrp->e_phentsize = 0;
3414  i_ehdrp->e_phnum = 0;
3415
3416  /* Each bfd section is section header entry.  */
3417  i_ehdrp->e_entry = bfd_get_start_address (abfd);
3418  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3419
3420  /* If we're building an executable, we'll need a program header table.  */
3421  if (abfd->flags & EXEC_P)
3422    {
3423      /* It all happens later.  */
3424#if 0
3425      i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3426
3427      /* elf_build_phdrs() returns a (NULL-terminated) array of
3428	 Elf_Internal_Phdrs.  */
3429      i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3430      i_ehdrp->e_phoff = outbase;
3431      outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3432#endif
3433    }
3434  else
3435    {
3436      i_ehdrp->e_phentsize = 0;
3437      i_phdrp = 0;
3438      i_ehdrp->e_phoff = 0;
3439    }
3440
3441  elf_tdata (abfd)->symtab_hdr.sh_name =
3442    (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3443  elf_tdata (abfd)->strtab_hdr.sh_name =
3444    (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3445  elf_tdata (abfd)->shstrtab_hdr.sh_name =
3446    (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3447  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3448      || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3449      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3450    return false;
3451
3452  return true;
3453}
3454
3455/* Assign file positions for all the reloc sections which are not part
3456   of the loadable file image.  */
3457
3458void
3459_bfd_elf_assign_file_positions_for_relocs (abfd)
3460     bfd *abfd;
3461{
3462  file_ptr off;
3463  unsigned int i;
3464  Elf_Internal_Shdr **shdrpp;
3465
3466  off = elf_tdata (abfd)->next_file_pos;
3467
3468  for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3469       i < elf_elfheader (abfd)->e_shnum;
3470       i++, shdrpp++)
3471    {
3472      Elf_Internal_Shdr *shdrp;
3473
3474      shdrp = *shdrpp;
3475      if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3476	  && shdrp->sh_offset == -1)
3477	off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3478    }
3479
3480  elf_tdata (abfd)->next_file_pos = off;
3481}
3482
3483boolean
3484_bfd_elf_write_object_contents (abfd)
3485     bfd *abfd;
3486{
3487  struct elf_backend_data *bed = get_elf_backend_data (abfd);
3488  Elf_Internal_Ehdr *i_ehdrp;
3489  Elf_Internal_Shdr **i_shdrp;
3490  boolean failed;
3491  unsigned int count;
3492
3493  if (! abfd->output_has_begun
3494      && ! _bfd_elf_compute_section_file_positions
3495             (abfd, (struct bfd_link_info *) NULL))
3496    return false;
3497
3498  i_shdrp = elf_elfsections (abfd);
3499  i_ehdrp = elf_elfheader (abfd);
3500
3501  failed = false;
3502  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3503  if (failed)
3504    return false;
3505
3506  _bfd_elf_assign_file_positions_for_relocs (abfd);
3507
3508  /* After writing the headers, we need to write the sections too...  */
3509  for (count = 1; count < i_ehdrp->e_shnum; count++)
3510    {
3511      if (bed->elf_backend_section_processing)
3512	(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3513      if (i_shdrp[count]->contents)
3514	{
3515	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3516	      || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3517			     1, abfd)
3518		  != i_shdrp[count]->sh_size))
3519	    return false;
3520	}
3521    }
3522
3523  /* Write out the section header names.  */
3524  if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3525      || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3526    return false;
3527
3528  if (bed->elf_backend_final_write_processing)
3529    (*bed->elf_backend_final_write_processing) (abfd,
3530						elf_tdata (abfd)->linker);
3531
3532  return bed->s->write_shdrs_and_ehdr (abfd);
3533}
3534
3535boolean
3536_bfd_elf_write_corefile_contents (abfd)
3537     bfd *abfd;
3538{
3539  /* Hopefully this can be done just like an object file.  */
3540  return _bfd_elf_write_object_contents (abfd);
3541}
3542
3543/* Given a section, search the header to find them.  */
3544
3545int
3546_bfd_elf_section_from_bfd_section (abfd, asect)
3547     bfd *abfd;
3548     struct sec *asect;
3549{
3550  struct elf_backend_data *bed = get_elf_backend_data (abfd);
3551  Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3552  int index;
3553  Elf_Internal_Shdr *hdr;
3554  int maxindex = elf_elfheader (abfd)->e_shnum;
3555
3556  for (index = 0; index < maxindex; index++)
3557    {
3558      hdr = i_shdrp[index];
3559      if (hdr->bfd_section == asect)
3560	return index;
3561    }
3562
3563  if (bed->elf_backend_section_from_bfd_section)
3564    {
3565      for (index = 0; index < maxindex; index++)
3566	{
3567	  int retval;
3568
3569	  hdr = i_shdrp[index];
3570	  retval = index;
3571	  if ((*bed->elf_backend_section_from_bfd_section)
3572	      (abfd, hdr, asect, &retval))
3573	    return retval;
3574	}
3575    }
3576
3577  if (bfd_is_abs_section (asect))
3578    return SHN_ABS;
3579  if (bfd_is_com_section (asect))
3580    return SHN_COMMON;
3581  if (bfd_is_und_section (asect))
3582    return SHN_UNDEF;
3583
3584  bfd_set_error (bfd_error_nonrepresentable_section);
3585
3586  return -1;
3587}
3588
3589/* Given a BFD symbol, return the index in the ELF symbol table, or -1
3590   on error.  */
3591
3592int
3593_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3594     bfd *abfd;
3595     asymbol **asym_ptr_ptr;
3596{
3597  asymbol *asym_ptr = *asym_ptr_ptr;
3598  int idx;
3599  flagword flags = asym_ptr->flags;
3600
3601  /* When gas creates relocations against local labels, it creates its
3602     own symbol for the section, but does put the symbol into the
3603     symbol chain, so udata is 0.  When the linker is generating
3604     relocatable output, this section symbol may be for one of the
3605     input sections rather than the output section.  */
3606  if (asym_ptr->udata.i == 0
3607      && (flags & BSF_SECTION_SYM)
3608      && asym_ptr->section)
3609    {
3610      int indx;
3611
3612      if (asym_ptr->section->output_section != NULL)
3613	indx = asym_ptr->section->output_section->index;
3614      else
3615	indx = asym_ptr->section->index;
3616      if (elf_section_syms (abfd)[indx])
3617	asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3618    }
3619
3620  idx = asym_ptr->udata.i;
3621
3622  if (idx == 0)
3623    {
3624      /* This case can occur when using --strip-symbol on a symbol
3625         which is used in a relocation entry.  */
3626      (*_bfd_error_handler)
3627	(_("%s: symbol `%s' required but not present"),
3628	 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3629      bfd_set_error (bfd_error_no_symbols);
3630      return -1;
3631    }
3632
3633#if DEBUG & 4
3634  {
3635    fprintf (stderr,
3636	     _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3637	     (long) asym_ptr, asym_ptr->name, idx, flags,
3638	     elf_symbol_flags (flags));
3639    fflush (stderr);
3640  }
3641#endif
3642
3643  return idx;
3644}
3645
3646/* Copy private BFD data.  This copies any program header information.  */
3647
3648static boolean
3649copy_private_bfd_data (ibfd, obfd)
3650     bfd *ibfd;
3651     bfd *obfd;
3652{
3653  Elf_Internal_Ehdr *       iehdr;
3654  struct elf_segment_map *  map;
3655  struct elf_segment_map *  map_first;
3656  struct elf_segment_map ** pointer_to_map;
3657  Elf_Internal_Phdr *       segment;
3658  asection *                section;
3659  unsigned int              i;
3660  unsigned int              num_segments;
3661  boolean                   phdr_included = false;
3662  bfd_vma                   maxpagesize;
3663  struct elf_segment_map *  phdr_adjust_seg = NULL;
3664  unsigned int              phdr_adjust_num = 0;
3665
3666  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3667      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3668    return true;
3669
3670  if (elf_tdata (ibfd)->phdr == NULL)
3671    return true;
3672
3673  iehdr = elf_elfheader (ibfd);
3674
3675  map_first = NULL;
3676  pointer_to_map = &map_first;
3677
3678  num_segments = elf_elfheader (ibfd)->e_phnum;
3679  maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3680
3681  /* Returns the end address of the segment + 1.  */
3682#define SEGMENT_END(segment, start) 			\
3683  (start + (segment->p_memsz > segment->p_filesz 	\
3684   ? segment->p_memsz : segment->p_filesz))
3685
3686  /* Returns true if the given section is contained within
3687     the given segment.  VMA addresses are compared.  */
3688#define IS_CONTAINED_BY_VMA(section, segment)		\
3689  (section->vma >= segment->p_vaddr			\
3690   && (section->vma + section->_raw_size)		\
3691   <= (SEGMENT_END (segment, segment->p_vaddr)))
3692
3693  /* Returns true if the given section is contained within
3694     the given segment.  LMA addresses are compared.  */
3695#define IS_CONTAINED_BY_LMA(section, segment, base)	\
3696    (section->lma >= base				\
3697     && (section->lma + section->_raw_size)		\
3698     <= SEGMENT_END (segment, base))
3699
3700  /* Special case: corefile "NOTE" section containing regs, prpsinfo etc.  */
3701#define IS_COREFILE_NOTE(p, s)                          \
3702	    (p->p_type == PT_NOTE                       \
3703	     && bfd_get_format (ibfd) == bfd_core       \
3704	     && s->vma == 0 && s->lma == 0              \
3705	     && (bfd_vma) s->filepos >= p->p_offset     \
3706	     && (bfd_vma) s->filepos + s->_raw_size     \
3707	     <= p->p_offset + p->p_filesz)
3708
3709  /* The complicated case when p_vaddr is 0 is to handle the Solaris
3710     linker, which generates a PT_INTERP section with p_vaddr and
3711     p_memsz set to 0.  */
3712#define IS_SOLARIS_PT_INTERP(p, s)			\
3713	    (   p->p_vaddr == 0				\
3714	     && p->p_filesz > 0				\
3715	     && (s->flags & SEC_HAS_CONTENTS) != 0	\
3716	     && s->_raw_size > 0			\
3717	     && (bfd_vma) s->filepos >= p->p_offset	\
3718	     && ((bfd_vma) s->filepos + s->_raw_size	\
3719		     <= p->p_offset + p->p_filesz))
3720
3721  /* Decide if the given section should be included in the given segment.
3722     A section will be included if:
3723       1. It is within the address space of the segment,
3724       2. It is an allocated segment,
3725       3. There is an output section associated with it,
3726       4. The section has not already been allocated to a previous segment.  */
3727#define INCLUDE_SECTION_IN_SEGMENT(section, segment)	\
3728  ((((IS_CONTAINED_BY_VMA (section, segment) 		\
3729      || IS_SOLARIS_PT_INTERP (segment, section))	\
3730     && (section->flags & SEC_ALLOC) != 0)		\
3731    || IS_COREFILE_NOTE (segment, section))		\
3732   && section->output_section != NULL			\
3733   && section->segment_mark == false)
3734
3735  /* Returns true iff seg1 starts after the end of seg2.  */
3736#define SEGMENT_AFTER_SEGMENT(seg1, seg2)		\
3737    (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3738
3739  /* Returns true iff seg1 and seg2 overlap.  */
3740#define SEGMENT_OVERLAPS(seg1, seg2)			\
3741  (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3742
3743  /* Initialise the segment mark field.  */
3744  for (section = ibfd->sections; section != NULL; section = section->next)
3745    section->segment_mark = false;
3746
3747  /* Scan through the segments specified in the program header
3748     of the input BFD.  For this first scan we look for overlaps
3749     in the loadable segments.  These can be created by wierd
3750     parameters to objcopy.  */
3751  for (i = 0, segment = elf_tdata (ibfd)->phdr;
3752       i < num_segments;
3753       i++, segment++)
3754    {
3755      unsigned int j;
3756      Elf_Internal_Phdr *segment2;
3757
3758      if (segment->p_type != PT_LOAD)
3759	continue;
3760
3761      /* Determine if this segment overlaps any previous segments.  */
3762      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3763	{
3764	  bfd_signed_vma extra_length;
3765
3766	  if (segment2->p_type != PT_LOAD
3767	      || ! SEGMENT_OVERLAPS (segment, segment2))
3768	    continue;
3769
3770	  /* Merge the two segments together.  */
3771	  if (segment2->p_vaddr < segment->p_vaddr)
3772	    {
3773	      /* Extend SEGMENT2 to include SEGMENT and then delete
3774                 SEGMENT.  */
3775	      extra_length =
3776		SEGMENT_END (segment, segment->p_vaddr)
3777		- SEGMENT_END (segment2, segment2->p_vaddr);
3778
3779	      if (extra_length > 0)
3780		{
3781		  segment2->p_memsz  += extra_length;
3782		  segment2->p_filesz += extra_length;
3783		}
3784
3785	      segment->p_type = PT_NULL;
3786
3787	      /* Since we have deleted P we must restart the outer loop.  */
3788	      i = 0;
3789	      segment = elf_tdata (ibfd)->phdr;
3790	      break;
3791	    }
3792	  else
3793	    {
3794	      /* Extend SEGMENT to include SEGMENT2 and then delete
3795                 SEGMENT2.  */
3796	      extra_length =
3797		SEGMENT_END (segment2, segment2->p_vaddr)
3798		- SEGMENT_END (segment, segment->p_vaddr);
3799
3800	      if (extra_length > 0)
3801		{
3802		  segment->p_memsz  += extra_length;
3803		  segment->p_filesz += extra_length;
3804		}
3805
3806	      segment2->p_type = PT_NULL;
3807	    }
3808	}
3809    }
3810
3811  /* The second scan attempts to assign sections to segments.  */
3812  for (i = 0, segment = elf_tdata (ibfd)->phdr;
3813       i < num_segments;
3814       i ++, segment ++)
3815    {
3816      unsigned int  section_count;
3817      asection **   sections;
3818      asection *    output_section;
3819      unsigned int  isec;
3820      bfd_vma       matching_lma;
3821      bfd_vma       suggested_lma;
3822      unsigned int  j;
3823
3824      if (segment->p_type == PT_NULL)
3825	continue;
3826
3827      /* Compute how many sections might be placed into this segment.  */
3828      section_count = 0;
3829      for (section = ibfd->sections; section != NULL; section = section->next)
3830	if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3831	  ++section_count;
3832
3833      /* Allocate a segment map big enough to contain all of the
3834	 sections we have selected.  */
3835      map = ((struct elf_segment_map *)
3836	   bfd_alloc (obfd,
3837		      (sizeof (struct elf_segment_map)
3838		       + ((size_t) section_count - 1) * sizeof (asection *))));
3839      if (map == NULL)
3840	return false;
3841
3842      /* Initialise the fields of the segment map.  Default to
3843	 using the physical address of the segment in the input BFD.  */
3844      map->next          = NULL;
3845      map->p_type        = segment->p_type;
3846      map->p_flags       = segment->p_flags;
3847      map->p_flags_valid = 1;
3848      map->p_paddr       = segment->p_paddr;
3849      map->p_paddr_valid = 1;
3850
3851      /* Determine if this segment contains the ELF file header
3852	 and if it contains the program headers themselves.  */
3853      map->includes_filehdr = (segment->p_offset == 0
3854			       && segment->p_filesz >= iehdr->e_ehsize);
3855
3856      map->includes_phdrs = 0;
3857
3858      if (! phdr_included || segment->p_type != PT_LOAD)
3859	{
3860	  map->includes_phdrs =
3861	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3862	     && (segment->p_offset + segment->p_filesz
3863		 >= ((bfd_vma) iehdr->e_phoff
3864		     + iehdr->e_phnum * iehdr->e_phentsize)));
3865
3866	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
3867	    phdr_included = true;
3868	}
3869
3870      if (section_count == 0)
3871	{
3872	  /* Special segments, such as the PT_PHDR segment, may contain
3873	     no sections, but ordinary, loadable segments should contain
3874	     something.  */
3875	  if (segment->p_type == PT_LOAD)
3876	      _bfd_error_handler
3877		(_("%s: warning: Empty loadable segment detected\n"),
3878		 bfd_get_filename (ibfd));
3879
3880	  map->count = 0;
3881	  *pointer_to_map = map;
3882	  pointer_to_map = &map->next;
3883
3884	  continue;
3885	}
3886
3887      /* Now scan the sections in the input BFD again and attempt
3888	 to add their corresponding output sections to the segment map.
3889	 The problem here is how to handle an output section which has
3890	 been moved (ie had its LMA changed).  There are four possibilities:
3891
3892	 1. None of the sections have been moved.
3893	    In this case we can continue to use the segment LMA from the
3894	    input BFD.
3895
3896	 2. All of the sections have been moved by the same amount.
3897	    In this case we can change the segment's LMA to match the LMA
3898	    of the first section.
3899
3900	 3. Some of the sections have been moved, others have not.
3901	    In this case those sections which have not been moved can be
3902	    placed in the current segment which will have to have its size,
3903	    and possibly its LMA changed, and a new segment or segments will
3904	    have to be created to contain the other sections.
3905
3906	 4. The sections have been moved, but not be the same amount.
3907	    In this case we can change the segment's LMA to match the LMA
3908	    of the first section and we will have to create a new segment
3909	    or segments to contain the other sections.
3910
3911	 In order to save time, we allocate an array to hold the section
3912	 pointers that we are interested in.  As these sections get assigned
3913	 to a segment, they are removed from this array.  */
3914
3915      sections = (asection **) bfd_malloc
3916	(sizeof (asection *) * section_count);
3917      if (sections == NULL)
3918	return false;
3919
3920      /* Step One: Scan for segment vs section LMA conflicts.
3921	 Also add the sections to the section array allocated above.
3922	 Also add the sections to the current segment.  In the common
3923	 case, where the sections have not been moved, this means that
3924	 we have completely filled the segment, and there is nothing
3925	 more to do.  */
3926      isec = 0;
3927      matching_lma = 0;
3928      suggested_lma = 0;
3929
3930      for (j = 0, section = ibfd->sections;
3931	   section != NULL;
3932	   section = section->next)
3933	{
3934	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3935	    {
3936	      output_section = section->output_section;
3937
3938	      sections[j ++] = section;
3939
3940	      /* The Solaris native linker always sets p_paddr to 0.
3941		 We try to catch that case here, and set it to the
3942		 correct value.  */
3943	      if (segment->p_paddr == 0
3944		  && segment->p_vaddr != 0
3945		  && isec == 0
3946		  && output_section->lma != 0
3947		  && (output_section->vma == (segment->p_vaddr
3948					      + (map->includes_filehdr
3949						 ? iehdr->e_ehsize
3950						 : 0)
3951					      + (map->includes_phdrs
3952						 ? iehdr->e_phnum * iehdr->e_phentsize
3953						 : 0))))
3954		map->p_paddr = segment->p_vaddr;
3955
3956	      /* Match up the physical address of the segment with the
3957		 LMA address of the output section.  */
3958	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3959		  || IS_COREFILE_NOTE (segment, section))
3960		{
3961		  if (matching_lma == 0)
3962		    matching_lma = output_section->lma;
3963
3964		  /* We assume that if the section fits within the segment
3965		     then it does not overlap any other section within that
3966		     segment.  */
3967		  map->sections[isec ++] = output_section;
3968		}
3969	      else if (suggested_lma == 0)
3970		suggested_lma = output_section->lma;
3971	    }
3972	}
3973
3974      BFD_ASSERT (j == section_count);
3975
3976      /* Step Two: Adjust the physical address of the current segment,
3977	 if necessary.  */
3978      if (isec == section_count)
3979	{
3980	  /* All of the sections fitted within the segment as currently
3981	     specified.  This is the default case.  Add the segment to
3982	     the list of built segments and carry on to process the next
3983	     program header in the input BFD.  */
3984	  map->count = section_count;
3985	  *pointer_to_map = map;
3986	  pointer_to_map = &map->next;
3987
3988	  free (sections);
3989	  continue;
3990	}
3991      else
3992	{
3993	  if (matching_lma != 0)
3994	    {
3995	      /* At least one section fits inside the current segment.
3996		 Keep it, but modify its physical address to match the
3997		 LMA of the first section that fitted.  */
3998	      map->p_paddr = matching_lma;
3999	    }
4000	  else
4001	    {
4002	      /* None of the sections fitted inside the current segment.
4003		 Change the current segment's physical address to match
4004		 the LMA of the first section.  */
4005	      map->p_paddr = suggested_lma;
4006	    }
4007
4008	  /* Offset the segment physical address from the lma
4009	     to allow for space taken up by elf headers.  */
4010	  if (map->includes_filehdr)
4011	    map->p_paddr -= iehdr->e_ehsize;
4012
4013	  if (map->includes_phdrs)
4014	    {
4015	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4016
4017	      /* iehdr->e_phnum is just an estimate of the number
4018		 of program headers that we will need.  Make a note
4019		 here of the number we used and the segment we chose
4020		 to hold these headers, so that we can adjust the
4021		 offset when we know the correct value.  */
4022	      phdr_adjust_num = iehdr->e_phnum;
4023	      phdr_adjust_seg = map;
4024	    }
4025	}
4026
4027      /* Step Three: Loop over the sections again, this time assigning
4028	 those that fit to the current segment and remvoing them from the
4029	 sections array; but making sure not to leave large gaps.  Once all
4030	 possible sections have been assigned to the current segment it is
4031	 added to the list of built segments and if sections still remain
4032	 to be assigned, a new segment is constructed before repeating
4033	 the loop.  */
4034      isec = 0;
4035      do
4036	{
4037	  map->count = 0;
4038	  suggested_lma = 0;
4039
4040	  /* Fill the current segment with sections that fit.  */
4041	  for (j = 0; j < section_count; j++)
4042	    {
4043	      section = sections[j];
4044
4045	      if (section == NULL)
4046		continue;
4047
4048	      output_section = section->output_section;
4049
4050	      BFD_ASSERT (output_section != NULL);
4051
4052	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4053		  || IS_COREFILE_NOTE (segment, section))
4054		{
4055		  if (map->count == 0)
4056		    {
4057		      /* If the first section in a segment does not start at
4058			 the beginning of the segment, then something is
4059			 wrong.  */
4060		      if (output_section->lma !=
4061			  (map->p_paddr
4062			   + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4063			   + (map->includes_phdrs
4064			      ? iehdr->e_phnum * iehdr->e_phentsize
4065			      : 0)))
4066			abort ();
4067		    }
4068		  else
4069		    {
4070		      asection * prev_sec;
4071
4072		      prev_sec = map->sections[map->count - 1];
4073
4074		      /* If the gap between the end of the previous section
4075			 and the start of this section is more than
4076			 maxpagesize then we need to start a new segment.  */
4077		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4078			  < BFD_ALIGN (output_section->lma, maxpagesize))
4079			  || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4080			{
4081			  if (suggested_lma == 0)
4082			    suggested_lma = output_section->lma;
4083
4084			  continue;
4085			}
4086		    }
4087
4088		  map->sections[map->count++] = output_section;
4089		  ++isec;
4090		  sections[j] = NULL;
4091		  section->segment_mark = true;
4092		}
4093	      else if (suggested_lma == 0)
4094		suggested_lma = output_section->lma;
4095	    }
4096
4097	  BFD_ASSERT (map->count > 0);
4098
4099	  /* Add the current segment to the list of built segments.  */
4100	  *pointer_to_map = map;
4101	  pointer_to_map = &map->next;
4102
4103	  if (isec < section_count)
4104	    {
4105	      /* We still have not allocated all of the sections to
4106		 segments.  Create a new segment here, initialise it
4107		 and carry on looping.  */
4108	      map = ((struct elf_segment_map *)
4109		     bfd_alloc (obfd,
4110				(sizeof (struct elf_segment_map)
4111				 + ((size_t) section_count - 1)
4112				 * sizeof (asection *))));
4113	      if (map == NULL)
4114		return false;
4115
4116	      /* Initialise the fields of the segment map.  Set the physical
4117		 physical address to the LMA of the first section that has
4118		 not yet been assigned.  */
4119	      map->next             = NULL;
4120	      map->p_type           = segment->p_type;
4121	      map->p_flags          = segment->p_flags;
4122	      map->p_flags_valid    = 1;
4123	      map->p_paddr          = suggested_lma;
4124	      map->p_paddr_valid    = 1;
4125	      map->includes_filehdr = 0;
4126	      map->includes_phdrs   = 0;
4127	    }
4128	}
4129      while (isec < section_count);
4130
4131      free (sections);
4132    }
4133
4134  /* The Solaris linker creates program headers in which all the
4135     p_paddr fields are zero.  When we try to objcopy or strip such a
4136     file, we get confused.  Check for this case, and if we find it
4137     reset the p_paddr_valid fields.  */
4138  for (map = map_first; map != NULL; map = map->next)
4139    if (map->p_paddr != 0)
4140      break;
4141  if (map == NULL)
4142    {
4143      for (map = map_first; map != NULL; map = map->next)
4144	map->p_paddr_valid = 0;
4145    }
4146
4147  elf_tdata (obfd)->segment_map = map_first;
4148
4149  /* If we had to estimate the number of program headers that were
4150     going to be needed, then check our estimate know and adjust
4151     the offset if necessary.  */
4152  if (phdr_adjust_seg != NULL)
4153    {
4154      unsigned int count;
4155
4156      for (count = 0, map = map_first; map != NULL; map = map->next)
4157	count++;
4158
4159      if (count > phdr_adjust_num)
4160	phdr_adjust_seg->p_paddr
4161	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4162    }
4163
4164#if 0
4165  /* Final Step: Sort the segments into ascending order of physical
4166     address.  */
4167  if (map_first != NULL)
4168    {
4169      struct elf_segment_map *prev;
4170
4171      prev = map_first;
4172      for (map = map_first->next; map != NULL; prev = map, map = map->next)
4173	{
4174	  /* Yes I know - its a bubble sort....  */
4175	  if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4176	    {
4177	      /* Swap map and map->next.  */
4178	      prev->next = map->next;
4179	      map->next = map->next->next;
4180	      prev->next->next = map;
4181
4182	      /* Restart loop.  */
4183	      map = map_first;
4184	    }
4185	}
4186    }
4187#endif
4188
4189#undef SEGMENT_END
4190#undef IS_CONTAINED_BY_VMA
4191#undef IS_CONTAINED_BY_LMA
4192#undef IS_COREFILE_NOTE
4193#undef IS_SOLARIS_PT_INTERP
4194#undef INCLUDE_SECTION_IN_SEGMENT
4195#undef SEGMENT_AFTER_SEGMENT
4196#undef SEGMENT_OVERLAPS
4197  return true;
4198}
4199
4200/* Copy private section information.  This copies over the entsize
4201   field, and sometimes the info field.  */
4202
4203boolean
4204_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4205     bfd *ibfd;
4206     asection *isec;
4207     bfd *obfd;
4208     asection *osec;
4209{
4210  Elf_Internal_Shdr *ihdr, *ohdr;
4211
4212  if (ibfd->xvec->flavour != bfd_target_elf_flavour
4213      || obfd->xvec->flavour != bfd_target_elf_flavour)
4214    return true;
4215
4216  /* Copy over private BFD data if it has not already been copied.
4217     This must be done here, rather than in the copy_private_bfd_data
4218     entry point, because the latter is called after the section
4219     contents have been set, which means that the program headers have
4220     already been worked out.  */
4221  if (elf_tdata (obfd)->segment_map == NULL
4222      && elf_tdata (ibfd)->phdr != NULL)
4223    {
4224      asection *s;
4225
4226      /* Only set up the segments if there are no more SEC_ALLOC
4227         sections.  FIXME: This won't do the right thing if objcopy is
4228         used to remove the last SEC_ALLOC section, since objcopy
4229         won't call this routine in that case.  */
4230      for (s = isec->next; s != NULL; s = s->next)
4231	if ((s->flags & SEC_ALLOC) != 0)
4232	  break;
4233      if (s == NULL)
4234	{
4235	  if (! copy_private_bfd_data (ibfd, obfd))
4236	    return false;
4237	}
4238    }
4239
4240  ihdr = &elf_section_data (isec)->this_hdr;
4241  ohdr = &elf_section_data (osec)->this_hdr;
4242
4243  ohdr->sh_entsize = ihdr->sh_entsize;
4244
4245  if (ihdr->sh_type == SHT_SYMTAB
4246      || ihdr->sh_type == SHT_DYNSYM
4247      || ihdr->sh_type == SHT_GNU_verneed
4248      || ihdr->sh_type == SHT_GNU_verdef)
4249    ohdr->sh_info = ihdr->sh_info;
4250
4251  elf_section_data (osec)->use_rela_p
4252    = elf_section_data (isec)->use_rela_p;
4253
4254  return true;
4255}
4256
4257/* Copy private symbol information.  If this symbol is in a section
4258   which we did not map into a BFD section, try to map the section
4259   index correctly.  We use special macro definitions for the mapped
4260   section indices; these definitions are interpreted by the
4261   swap_out_syms function.  */
4262
4263#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4264#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4265#define MAP_STRTAB (SHN_LORESERVE - 3)
4266#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4267
4268boolean
4269_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4270     bfd *ibfd;
4271     asymbol *isymarg;
4272     bfd *obfd;
4273     asymbol *osymarg;
4274{
4275  elf_symbol_type *isym, *osym;
4276
4277  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4278      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4279    return true;
4280
4281  isym = elf_symbol_from (ibfd, isymarg);
4282  osym = elf_symbol_from (obfd, osymarg);
4283
4284  if (isym != NULL
4285      && osym != NULL
4286      && bfd_is_abs_section (isym->symbol.section))
4287    {
4288      unsigned int shndx;
4289
4290      shndx = isym->internal_elf_sym.st_shndx;
4291      if (shndx == elf_onesymtab (ibfd))
4292	shndx = MAP_ONESYMTAB;
4293      else if (shndx == elf_dynsymtab (ibfd))
4294	shndx = MAP_DYNSYMTAB;
4295      else if (shndx == elf_tdata (ibfd)->strtab_section)
4296	shndx = MAP_STRTAB;
4297      else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4298	shndx = MAP_SHSTRTAB;
4299      osym->internal_elf_sym.st_shndx = shndx;
4300    }
4301
4302  return true;
4303}
4304
4305/* Swap out the symbols.  */
4306
4307static boolean
4308swap_out_syms (abfd, sttp, relocatable_p)
4309     bfd *abfd;
4310     struct bfd_strtab_hash **sttp;
4311     int relocatable_p;
4312{
4313  struct elf_backend_data *bed = get_elf_backend_data (abfd);
4314
4315  if (!elf_map_symbols (abfd))
4316    return false;
4317
4318  /* Dump out the symtabs.  */
4319  {
4320    int symcount = bfd_get_symcount (abfd);
4321    asymbol **syms = bfd_get_outsymbols (abfd);
4322    struct bfd_strtab_hash *stt;
4323    Elf_Internal_Shdr *symtab_hdr;
4324    Elf_Internal_Shdr *symstrtab_hdr;
4325    char *outbound_syms;
4326    int idx;
4327
4328    stt = _bfd_elf_stringtab_init ();
4329    if (stt == NULL)
4330      return false;
4331
4332    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4333    symtab_hdr->sh_type = SHT_SYMTAB;
4334    symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4335    symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4336    symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4337    symtab_hdr->sh_addralign = bed->s->file_align;
4338
4339    symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4340    symstrtab_hdr->sh_type = SHT_STRTAB;
4341
4342    outbound_syms = bfd_alloc (abfd,
4343			       (1 + symcount) * bed->s->sizeof_sym);
4344    if (outbound_syms == NULL)
4345      return false;
4346    symtab_hdr->contents = (PTR) outbound_syms;
4347
4348    /* now generate the data (for "contents") */
4349    {
4350      /* Fill in zeroth symbol and swap it out.  */
4351      Elf_Internal_Sym sym;
4352      sym.st_name = 0;
4353      sym.st_value = 0;
4354      sym.st_size = 0;
4355      sym.st_info = 0;
4356      sym.st_other = 0;
4357      sym.st_shndx = SHN_UNDEF;
4358      bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4359      outbound_syms += bed->s->sizeof_sym;
4360    }
4361    for (idx = 0; idx < symcount; idx++)
4362      {
4363	Elf_Internal_Sym sym;
4364	bfd_vma value = syms[idx]->value;
4365	elf_symbol_type *type_ptr;
4366	flagword flags = syms[idx]->flags;
4367	int type;
4368
4369	if ((flags & BSF_SECTION_SYM) != 0)
4370	  {
4371	    /* Section symbols have no name.  */
4372	    sym.st_name = 0;
4373	  }
4374	else
4375	  {
4376	    sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4377							      syms[idx]->name,
4378							      true, false);
4379	    if (sym.st_name == (unsigned long) -1)
4380	      return false;
4381	  }
4382
4383	type_ptr = elf_symbol_from (abfd, syms[idx]);
4384
4385	if ((flags & BSF_SECTION_SYM) == 0
4386	    && bfd_is_com_section (syms[idx]->section))
4387	  {
4388	    /* ELF common symbols put the alignment into the `value' field,
4389	       and the size into the `size' field.  This is backwards from
4390	       how BFD handles it, so reverse it here.  */
4391	    sym.st_size = value;
4392	    if (type_ptr == NULL
4393		|| type_ptr->internal_elf_sym.st_value == 0)
4394	      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4395	    else
4396	      sym.st_value = type_ptr->internal_elf_sym.st_value;
4397	    sym.st_shndx = _bfd_elf_section_from_bfd_section
4398	      (abfd, syms[idx]->section);
4399	  }
4400	else
4401	  {
4402	    asection *sec = syms[idx]->section;
4403	    int shndx;
4404
4405	    if (sec->output_section)
4406	      {
4407		value += sec->output_offset;
4408		sec = sec->output_section;
4409	      }
4410	    /* Don't add in the section vma for relocatable output.  */
4411	    if (! relocatable_p)
4412	      value += sec->vma;
4413	    sym.st_value = value;
4414	    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4415
4416	    if (bfd_is_abs_section (sec)
4417		&& type_ptr != NULL
4418		&& type_ptr->internal_elf_sym.st_shndx != 0)
4419	      {
4420		/* This symbol is in a real ELF section which we did
4421                   not create as a BFD section.  Undo the mapping done
4422                   by copy_private_symbol_data.  */
4423		shndx = type_ptr->internal_elf_sym.st_shndx;
4424		switch (shndx)
4425		  {
4426		  case MAP_ONESYMTAB:
4427		    shndx = elf_onesymtab (abfd);
4428		    break;
4429		  case MAP_DYNSYMTAB:
4430		    shndx = elf_dynsymtab (abfd);
4431		    break;
4432		  case MAP_STRTAB:
4433		    shndx = elf_tdata (abfd)->strtab_section;
4434		    break;
4435		  case MAP_SHSTRTAB:
4436		    shndx = elf_tdata (abfd)->shstrtab_section;
4437		    break;
4438		  default:
4439		    break;
4440		  }
4441	      }
4442	    else
4443	      {
4444		shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4445
4446		if (shndx == -1)
4447		  {
4448		    asection *sec2;
4449
4450		    /* Writing this would be a hell of a lot easier if
4451		       we had some decent documentation on bfd, and
4452		       knew what to expect of the library, and what to
4453		       demand of applications.  For example, it
4454		       appears that `objcopy' might not set the
4455		       section of a symbol to be a section that is
4456		       actually in the output file.  */
4457		    sec2 = bfd_get_section_by_name (abfd, sec->name);
4458		    BFD_ASSERT (sec2 != 0);
4459		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4460		    BFD_ASSERT (shndx != -1);
4461		  }
4462	      }
4463
4464	    sym.st_shndx = shndx;
4465	  }
4466
4467	if ((flags & BSF_FUNCTION) != 0)
4468	  type = STT_FUNC;
4469	else if ((flags & BSF_OBJECT) != 0)
4470	  type = STT_OBJECT;
4471	else
4472	  type = STT_NOTYPE;
4473
4474        /* Processor-specific types */
4475        if (type_ptr != NULL
4476	    && bed->elf_backend_get_symbol_type)
4477          type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4478
4479	if (flags & BSF_SECTION_SYM)
4480	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4481	else if (bfd_is_com_section (syms[idx]->section))
4482	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4483	else if (bfd_is_und_section (syms[idx]->section))
4484	  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4485				      ? STB_WEAK
4486				      : STB_GLOBAL),
4487				     type);
4488	else if (flags & BSF_FILE)
4489	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4490	else
4491	  {
4492	    int bind = STB_LOCAL;
4493
4494	    if (flags & BSF_LOCAL)
4495	      bind = STB_LOCAL;
4496	    else if (flags & BSF_WEAK)
4497	      bind = STB_WEAK;
4498	    else if (flags & BSF_GLOBAL)
4499	      bind = STB_GLOBAL;
4500
4501	    sym.st_info = ELF_ST_INFO (bind, type);
4502	  }
4503
4504	if (type_ptr != NULL)
4505	  sym.st_other = type_ptr->internal_elf_sym.st_other;
4506	else
4507	  sym.st_other = 0;
4508
4509	bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4510	outbound_syms += bed->s->sizeof_sym;
4511      }
4512
4513    *sttp = stt;
4514    symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4515    symstrtab_hdr->sh_type = SHT_STRTAB;
4516
4517    symstrtab_hdr->sh_flags = 0;
4518    symstrtab_hdr->sh_addr = 0;
4519    symstrtab_hdr->sh_entsize = 0;
4520    symstrtab_hdr->sh_link = 0;
4521    symstrtab_hdr->sh_info = 0;
4522    symstrtab_hdr->sh_addralign = 1;
4523  }
4524
4525  return true;
4526}
4527
4528/* Return the number of bytes required to hold the symtab vector.
4529
4530   Note that we base it on the count plus 1, since we will null terminate
4531   the vector allocated based on this size.  However, the ELF symbol table
4532   always has a dummy entry as symbol #0, so it ends up even.  */
4533
4534long
4535_bfd_elf_get_symtab_upper_bound (abfd)
4536     bfd *abfd;
4537{
4538  long symcount;
4539  long symtab_size;
4540  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4541
4542  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4543  symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4544
4545  return symtab_size;
4546}
4547
4548long
4549_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4550     bfd *abfd;
4551{
4552  long symcount;
4553  long symtab_size;
4554  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4555
4556  if (elf_dynsymtab (abfd) == 0)
4557    {
4558      bfd_set_error (bfd_error_invalid_operation);
4559      return -1;
4560    }
4561
4562  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4563  symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4564
4565  return symtab_size;
4566}
4567
4568long
4569_bfd_elf_get_reloc_upper_bound (abfd, asect)
4570     bfd *abfd ATTRIBUTE_UNUSED;
4571     sec_ptr asect;
4572{
4573  return (asect->reloc_count + 1) * sizeof (arelent *);
4574}
4575
4576/* Canonicalize the relocs.  */
4577
4578long
4579_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4580     bfd *abfd;
4581     sec_ptr section;
4582     arelent **relptr;
4583     asymbol **symbols;
4584{
4585  arelent *tblptr;
4586  unsigned int i;
4587
4588  if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4589							   section,
4590							   symbols,
4591							   false))
4592    return -1;
4593
4594  tblptr = section->relocation;
4595  for (i = 0; i < section->reloc_count; i++)
4596    *relptr++ = tblptr++;
4597
4598  *relptr = NULL;
4599
4600  return section->reloc_count;
4601}
4602
4603long
4604_bfd_elf_get_symtab (abfd, alocation)
4605     bfd *abfd;
4606     asymbol **alocation;
4607{
4608  long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4609    (abfd, alocation, false);
4610
4611  if (symcount >= 0)
4612    bfd_get_symcount (abfd) = symcount;
4613  return symcount;
4614}
4615
4616long
4617_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4618     bfd *abfd;
4619     asymbol **alocation;
4620{
4621  return get_elf_backend_data (abfd)->s->slurp_symbol_table
4622    (abfd, alocation, true);
4623}
4624
4625/* Return the size required for the dynamic reloc entries.  Any
4626   section that was actually installed in the BFD, and has type
4627   SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4628   considered to be a dynamic reloc section.  */
4629
4630long
4631_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4632     bfd *abfd;
4633{
4634  long ret;
4635  asection *s;
4636
4637  if (elf_dynsymtab (abfd) == 0)
4638    {
4639      bfd_set_error (bfd_error_invalid_operation);
4640      return -1;
4641    }
4642
4643  ret = sizeof (arelent *);
4644  for (s = abfd->sections; s != NULL; s = s->next)
4645    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4646	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4647	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4648      ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4649	      * sizeof (arelent *));
4650
4651  return ret;
4652}
4653
4654/* Canonicalize the dynamic relocation entries.  Note that we return
4655   the dynamic relocations as a single block, although they are
4656   actually associated with particular sections; the interface, which
4657   was designed for SunOS style shared libraries, expects that there
4658   is only one set of dynamic relocs.  Any section that was actually
4659   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4660   the dynamic symbol table, is considered to be a dynamic reloc
4661   section.  */
4662
4663long
4664_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4665     bfd *abfd;
4666     arelent **storage;
4667     asymbol **syms;
4668{
4669  boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4670  asection *s;
4671  long ret;
4672
4673  if (elf_dynsymtab (abfd) == 0)
4674    {
4675      bfd_set_error (bfd_error_invalid_operation);
4676      return -1;
4677    }
4678
4679  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4680  ret = 0;
4681  for (s = abfd->sections; s != NULL; s = s->next)
4682    {
4683      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4684	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4685	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4686	{
4687	  arelent *p;
4688	  long count, i;
4689
4690	  if (! (*slurp_relocs) (abfd, s, syms, true))
4691	    return -1;
4692	  count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4693	  p = s->relocation;
4694	  for (i = 0; i < count; i++)
4695	    *storage++ = p++;
4696	  ret += count;
4697	}
4698    }
4699
4700  *storage = NULL;
4701
4702  return ret;
4703}
4704
4705/* Read in the version information.  */
4706
4707boolean
4708_bfd_elf_slurp_version_tables (abfd)
4709     bfd *abfd;
4710{
4711  bfd_byte *contents = NULL;
4712
4713  if (elf_dynverdef (abfd) != 0)
4714    {
4715      Elf_Internal_Shdr *hdr;
4716      Elf_External_Verdef *everdef;
4717      Elf_Internal_Verdef *iverdef;
4718      Elf_Internal_Verdef *iverdefarr;
4719      Elf_Internal_Verdef iverdefmem;
4720      unsigned int i;
4721      unsigned int maxidx;
4722
4723      hdr = &elf_tdata (abfd)->dynverdef_hdr;
4724
4725      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4726      if (contents == NULL)
4727	goto error_return;
4728      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4729	  || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4730	goto error_return;
4731
4732      /* We know the number of entries in the section but not the maximum
4733	 index.  Therefore we have to run through all entries and find
4734	 the maximum.  */
4735      everdef = (Elf_External_Verdef *) contents;
4736      maxidx = 0;
4737      for (i = 0; i < hdr->sh_info; ++i)
4738	{
4739	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4740
4741	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4742	    maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4743
4744	  everdef = ((Elf_External_Verdef *)
4745		     ((bfd_byte *) everdef + iverdefmem.vd_next));
4746	}
4747
4748      elf_tdata (abfd)->verdef =
4749	((Elf_Internal_Verdef *)
4750	 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4751      if (elf_tdata (abfd)->verdef == NULL)
4752	goto error_return;
4753
4754      elf_tdata (abfd)->cverdefs = maxidx;
4755
4756      everdef = (Elf_External_Verdef *) contents;
4757      iverdefarr = elf_tdata (abfd)->verdef;
4758      for (i = 0; i < hdr->sh_info; i++)
4759	{
4760	  Elf_External_Verdaux *everdaux;
4761	  Elf_Internal_Verdaux *iverdaux;
4762	  unsigned int j;
4763
4764	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4765
4766	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4767	  memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4768
4769	  iverdef->vd_bfd = abfd;
4770
4771	  iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4772				bfd_alloc (abfd,
4773					   (iverdef->vd_cnt
4774					    * sizeof (Elf_Internal_Verdaux))));
4775	  if (iverdef->vd_auxptr == NULL)
4776	    goto error_return;
4777
4778	  everdaux = ((Elf_External_Verdaux *)
4779		      ((bfd_byte *) everdef + iverdef->vd_aux));
4780	  iverdaux = iverdef->vd_auxptr;
4781	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4782	    {
4783	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4784
4785	      iverdaux->vda_nodename =
4786		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4787						 iverdaux->vda_name);
4788	      if (iverdaux->vda_nodename == NULL)
4789		goto error_return;
4790
4791	      if (j + 1 < iverdef->vd_cnt)
4792		iverdaux->vda_nextptr = iverdaux + 1;
4793	      else
4794		iverdaux->vda_nextptr = NULL;
4795
4796	      everdaux = ((Elf_External_Verdaux *)
4797			  ((bfd_byte *) everdaux + iverdaux->vda_next));
4798	    }
4799
4800	  iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4801
4802	  if (i + 1 < hdr->sh_info)
4803	    iverdef->vd_nextdef = iverdef + 1;
4804	  else
4805	    iverdef->vd_nextdef = NULL;
4806
4807	  everdef = ((Elf_External_Verdef *)
4808		     ((bfd_byte *) everdef + iverdef->vd_next));
4809	}
4810
4811      free (contents);
4812      contents = NULL;
4813    }
4814
4815  if (elf_dynverref (abfd) != 0)
4816    {
4817      Elf_Internal_Shdr *hdr;
4818      Elf_External_Verneed *everneed;
4819      Elf_Internal_Verneed *iverneed;
4820      unsigned int i;
4821
4822      hdr = &elf_tdata (abfd)->dynverref_hdr;
4823
4824      elf_tdata (abfd)->verref =
4825	((Elf_Internal_Verneed *)
4826	 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4827      if (elf_tdata (abfd)->verref == NULL)
4828	goto error_return;
4829
4830      elf_tdata (abfd)->cverrefs = hdr->sh_info;
4831
4832      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4833      if (contents == NULL)
4834	goto error_return;
4835      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4836	  || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4837	goto error_return;
4838
4839      everneed = (Elf_External_Verneed *) contents;
4840      iverneed = elf_tdata (abfd)->verref;
4841      for (i = 0; i < hdr->sh_info; i++, iverneed++)
4842	{
4843	  Elf_External_Vernaux *evernaux;
4844	  Elf_Internal_Vernaux *ivernaux;
4845	  unsigned int j;
4846
4847	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4848
4849	  iverneed->vn_bfd = abfd;
4850
4851	  iverneed->vn_filename =
4852	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4853					     iverneed->vn_file);
4854	  if (iverneed->vn_filename == NULL)
4855	    goto error_return;
4856
4857	  iverneed->vn_auxptr =
4858	    ((Elf_Internal_Vernaux *)
4859	     bfd_alloc (abfd,
4860			iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4861
4862	  evernaux = ((Elf_External_Vernaux *)
4863		      ((bfd_byte *) everneed + iverneed->vn_aux));
4864	  ivernaux = iverneed->vn_auxptr;
4865	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4866	    {
4867	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4868
4869	      ivernaux->vna_nodename =
4870		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4871						 ivernaux->vna_name);
4872	      if (ivernaux->vna_nodename == NULL)
4873		goto error_return;
4874
4875	      if (j + 1 < iverneed->vn_cnt)
4876		ivernaux->vna_nextptr = ivernaux + 1;
4877	      else
4878		ivernaux->vna_nextptr = NULL;
4879
4880	      evernaux = ((Elf_External_Vernaux *)
4881			  ((bfd_byte *) evernaux + ivernaux->vna_next));
4882	    }
4883
4884	  if (i + 1 < hdr->sh_info)
4885	    iverneed->vn_nextref = iverneed + 1;
4886	  else
4887	    iverneed->vn_nextref = NULL;
4888
4889	  everneed = ((Elf_External_Verneed *)
4890		      ((bfd_byte *) everneed + iverneed->vn_next));
4891	}
4892
4893      free (contents);
4894      contents = NULL;
4895    }
4896
4897  return true;
4898
4899 error_return:
4900  if (contents == NULL)
4901    free (contents);
4902  return false;
4903}
4904
4905asymbol *
4906_bfd_elf_make_empty_symbol (abfd)
4907     bfd *abfd;
4908{
4909  elf_symbol_type *newsym;
4910
4911  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4912  if (!newsym)
4913    return NULL;
4914  else
4915    {
4916      newsym->symbol.the_bfd = abfd;
4917      return &newsym->symbol;
4918    }
4919}
4920
4921void
4922_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4923     bfd *ignore_abfd ATTRIBUTE_UNUSED;
4924     asymbol *symbol;
4925     symbol_info *ret;
4926{
4927  bfd_symbol_info (symbol, ret);
4928}
4929
4930/* Return whether a symbol name implies a local symbol.  Most targets
4931   use this function for the is_local_label_name entry point, but some
4932   override it.  */
4933
4934boolean
4935_bfd_elf_is_local_label_name (abfd, name)
4936     bfd *abfd ATTRIBUTE_UNUSED;
4937     const char *name;
4938{
4939  /* Normal local symbols start with ``.L''.  */
4940  if (name[0] == '.' && name[1] == 'L')
4941    return true;
4942
4943  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4944     DWARF debugging symbols starting with ``..''.  */
4945  if (name[0] == '.' && name[1] == '.')
4946    return true;
4947
4948  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4949     emitting DWARF debugging output.  I suspect this is actually a
4950     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4951     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4952     underscore to be emitted on some ELF targets).  For ease of use,
4953     we treat such symbols as local.  */
4954  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4955    return true;
4956
4957  return false;
4958}
4959
4960alent *
4961_bfd_elf_get_lineno (ignore_abfd, symbol)
4962     bfd *ignore_abfd ATTRIBUTE_UNUSED;
4963     asymbol *symbol ATTRIBUTE_UNUSED;
4964{
4965  abort ();
4966  return NULL;
4967}
4968
4969boolean
4970_bfd_elf_set_arch_mach (abfd, arch, machine)
4971     bfd *abfd;
4972     enum bfd_architecture arch;
4973     unsigned long machine;
4974{
4975  /* If this isn't the right architecture for this backend, and this
4976     isn't the generic backend, fail.  */
4977  if (arch != get_elf_backend_data (abfd)->arch
4978      && arch != bfd_arch_unknown
4979      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4980    return false;
4981
4982  return bfd_default_set_arch_mach (abfd, arch, machine);
4983}
4984
4985/* Find the nearest line to a particular section and offset, for error
4986   reporting.  */
4987
4988boolean
4989_bfd_elf_find_nearest_line (abfd,
4990			    section,
4991			    symbols,
4992			    offset,
4993			    filename_ptr,
4994			    functionname_ptr,
4995			    line_ptr)
4996     bfd *abfd;
4997     asection *section;
4998     asymbol **symbols;
4999     bfd_vma offset;
5000     CONST char **filename_ptr;
5001     CONST char **functionname_ptr;
5002     unsigned int *line_ptr;
5003{
5004  boolean found;
5005  const char *filename;
5006  asymbol *func;
5007  bfd_vma low_func;
5008  asymbol **p;
5009
5010  if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5011				     filename_ptr, functionname_ptr,
5012				     line_ptr))
5013    return true;
5014
5015  if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5016				     filename_ptr, functionname_ptr,
5017				     line_ptr, 0,
5018				     &elf_tdata (abfd)->dwarf2_find_line_info))
5019    return true;
5020
5021  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5022					     &found, filename_ptr,
5023					     functionname_ptr, line_ptr,
5024					     &elf_tdata (abfd)->line_info))
5025    return false;
5026  if (found)
5027    return true;
5028
5029  if (symbols == NULL)
5030    return false;
5031
5032  filename = NULL;
5033  func = NULL;
5034  low_func = 0;
5035
5036  for (p = symbols; *p != NULL; p++)
5037    {
5038      elf_symbol_type *q;
5039
5040      q = (elf_symbol_type *) *p;
5041
5042      if (bfd_get_section (&q->symbol) != section)
5043	continue;
5044
5045      switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5046	{
5047	default:
5048	  break;
5049	case STT_FILE:
5050	  filename = bfd_asymbol_name (&q->symbol);
5051	  break;
5052	case STT_NOTYPE:
5053	case STT_FUNC:
5054	  if (q->symbol.section == section
5055	      && q->symbol.value >= low_func
5056	      && q->symbol.value <= offset)
5057	    {
5058	      func = (asymbol *) q;
5059	      low_func = q->symbol.value;
5060	    }
5061	  break;
5062	}
5063    }
5064
5065  if (func == NULL)
5066    return false;
5067
5068  *filename_ptr = filename;
5069  *functionname_ptr = bfd_asymbol_name (func);
5070  *line_ptr = 0;
5071  return true;
5072}
5073
5074int
5075_bfd_elf_sizeof_headers (abfd, reloc)
5076     bfd *abfd;
5077     boolean reloc;
5078{
5079  int ret;
5080
5081  ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5082  if (! reloc)
5083    ret += get_program_header_size (abfd);
5084  return ret;
5085}
5086
5087boolean
5088_bfd_elf_set_section_contents (abfd, section, location, offset, count)
5089     bfd *abfd;
5090     sec_ptr section;
5091     PTR location;
5092     file_ptr offset;
5093     bfd_size_type count;
5094{
5095  Elf_Internal_Shdr *hdr;
5096
5097  if (! abfd->output_has_begun
5098      && ! _bfd_elf_compute_section_file_positions
5099      (abfd, (struct bfd_link_info *) NULL))
5100    return false;
5101
5102  hdr = &elf_section_data (section)->this_hdr;
5103
5104  if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5105    return false;
5106  if (bfd_write (location, 1, count, abfd) != count)
5107    return false;
5108
5109  return true;
5110}
5111
5112void
5113_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5114     bfd *abfd ATTRIBUTE_UNUSED;
5115     arelent *cache_ptr ATTRIBUTE_UNUSED;
5116     Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5117{
5118  abort ();
5119}
5120
5121#if 0
5122void
5123_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5124     bfd *abfd;
5125     arelent *cache_ptr;
5126     Elf_Internal_Rel *dst;
5127{
5128  abort ();
5129}
5130#endif
5131
5132/* Try to convert a non-ELF reloc into an ELF one.  */
5133
5134boolean
5135_bfd_elf_validate_reloc (abfd, areloc)
5136     bfd *abfd;
5137     arelent *areloc;
5138{
5139  /* Check whether we really have an ELF howto.  */
5140
5141  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5142    {
5143      bfd_reloc_code_real_type code;
5144      reloc_howto_type *howto;
5145
5146      /* Alien reloc: Try to determine its type to replace it with an
5147	 equivalent ELF reloc.  */
5148
5149      if (areloc->howto->pc_relative)
5150	{
5151	  switch (areloc->howto->bitsize)
5152	    {
5153	    case 8:
5154	      code = BFD_RELOC_8_PCREL;
5155	      break;
5156	    case 12:
5157	      code = BFD_RELOC_12_PCREL;
5158	      break;
5159	    case 16:
5160	      code = BFD_RELOC_16_PCREL;
5161	      break;
5162	    case 24:
5163	      code = BFD_RELOC_24_PCREL;
5164	      break;
5165	    case 32:
5166	      code = BFD_RELOC_32_PCREL;
5167	      break;
5168	    case 64:
5169	      code = BFD_RELOC_64_PCREL;
5170	      break;
5171	    default:
5172	      goto fail;
5173	    }
5174
5175	  howto = bfd_reloc_type_lookup (abfd, code);
5176
5177	  if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5178	    {
5179	      if (howto->pcrel_offset)
5180		areloc->addend += areloc->address;
5181	      else
5182		areloc->addend -= areloc->address; /* addend is unsigned!! */
5183	    }
5184	}
5185      else
5186	{
5187	  switch (areloc->howto->bitsize)
5188	    {
5189	    case 8:
5190	      code = BFD_RELOC_8;
5191	      break;
5192	    case 14:
5193	      code = BFD_RELOC_14;
5194	      break;
5195	    case 16:
5196	      code = BFD_RELOC_16;
5197	      break;
5198	    case 26:
5199	      code = BFD_RELOC_26;
5200	      break;
5201	    case 32:
5202	      code = BFD_RELOC_32;
5203	      break;
5204	    case 64:
5205	      code = BFD_RELOC_64;
5206	      break;
5207	    default:
5208	      goto fail;
5209	    }
5210
5211	  howto = bfd_reloc_type_lookup (abfd, code);
5212	}
5213
5214      if (howto)
5215	areloc->howto = howto;
5216      else
5217	goto fail;
5218    }
5219
5220  return true;
5221
5222 fail:
5223  (*_bfd_error_handler)
5224    (_("%s: unsupported relocation type %s"),
5225     bfd_get_filename (abfd), areloc->howto->name);
5226  bfd_set_error (bfd_error_bad_value);
5227  return false;
5228}
5229
5230boolean
5231_bfd_elf_close_and_cleanup (abfd)
5232     bfd *abfd;
5233{
5234  if (bfd_get_format (abfd) == bfd_object)
5235    {
5236      if (elf_shstrtab (abfd) != NULL)
5237	_bfd_stringtab_free (elf_shstrtab (abfd));
5238    }
5239
5240  return _bfd_generic_close_and_cleanup (abfd);
5241}
5242
5243/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5244   in the relocation's offset.  Thus we cannot allow any sort of sanity
5245   range-checking to interfere.  There is nothing else to do in processing
5246   this reloc.  */
5247
5248bfd_reloc_status_type
5249_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5250     bfd *abfd ATTRIBUTE_UNUSED;
5251     arelent *re ATTRIBUTE_UNUSED;
5252     struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5253     PTR data ATTRIBUTE_UNUSED;
5254     asection *is ATTRIBUTE_UNUSED;
5255     bfd *obfd ATTRIBUTE_UNUSED;
5256     char **errmsg ATTRIBUTE_UNUSED;
5257{
5258  return bfd_reloc_ok;
5259}
5260
5261/* Elf core file support.  Much of this only works on native
5262   toolchains, since we rely on knowing the
5263   machine-dependent procfs structure in order to pick
5264   out details about the corefile.  */
5265
5266#ifdef HAVE_SYS_PROCFS_H
5267# include <sys/procfs.h>
5268#endif
5269
5270/* Define offsetof for those systems which lack it.  */
5271
5272#ifndef offsetof
5273# define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5274#endif
5275
5276/* FIXME: this is kinda wrong, but it's what gdb wants.  */
5277
5278static int
5279elfcore_make_pid (abfd)
5280     bfd *abfd;
5281{
5282  return ((elf_tdata (abfd)->core_lwpid << 16)
5283	  + (elf_tdata (abfd)->core_pid));
5284}
5285
5286/* If there isn't a section called NAME, make one, using
5287   data from SECT.  Note, this function will generate a
5288   reference to NAME, so you shouldn't deallocate or
5289   overwrite it.  */
5290
5291static boolean
5292elfcore_maybe_make_sect (abfd, name, sect)
5293     bfd *abfd;
5294     char *name;
5295     asection *sect;
5296{
5297  asection *sect2;
5298
5299  if (bfd_get_section_by_name (abfd, name) != NULL)
5300    return true;
5301
5302  sect2 = bfd_make_section (abfd, name);
5303  if (sect2 == NULL)
5304    return false;
5305
5306  sect2->_raw_size = sect->_raw_size;
5307  sect2->filepos = sect->filepos;
5308  sect2->flags = sect->flags;
5309  sect2->alignment_power = sect->alignment_power;
5310  return true;
5311}
5312
5313/* prstatus_t exists on:
5314     solaris 2.5+
5315     linux 2.[01] + glibc
5316     unixware 4.2
5317*/
5318
5319#if defined (HAVE_PRSTATUS_T)
5320static boolean
5321elfcore_grok_prstatus (abfd, note)
5322     bfd *abfd;
5323     Elf_Internal_Note *note;
5324{
5325  char buf[100];
5326  char *name;
5327  asection *sect;
5328  int raw_size;
5329  int offset;
5330
5331  if (note->descsz == sizeof (prstatus_t))
5332    {
5333      prstatus_t prstat;
5334
5335      raw_size = sizeof (prstat.pr_reg);
5336      offset   = offsetof (prstatus_t, pr_reg);
5337      memcpy (&prstat, note->descdata, sizeof (prstat));
5338
5339      elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5340      elf_tdata (abfd)->core_pid = prstat.pr_pid;
5341
5342      /* pr_who exists on:
5343	 solaris 2.5+
5344	 unixware 4.2
5345	 pr_who doesn't exist on:
5346	 linux 2.[01]
5347	 */
5348#if defined (HAVE_PRSTATUS_T_PR_WHO)
5349      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5350#endif
5351    }
5352#if defined (HAVE_PRSTATUS32_T)
5353  else if (note->descsz == sizeof (prstatus32_t))
5354    {
5355      /* 64-bit host, 32-bit corefile */
5356      prstatus32_t prstat;
5357
5358      raw_size = sizeof (prstat.pr_reg);
5359      offset   = offsetof (prstatus32_t, pr_reg);
5360      memcpy (&prstat, note->descdata, sizeof (prstat));
5361
5362      elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5363      elf_tdata (abfd)->core_pid = prstat.pr_pid;
5364
5365      /* pr_who exists on:
5366	 solaris 2.5+
5367	 unixware 4.2
5368	 pr_who doesn't exist on:
5369	 linux 2.[01]
5370	 */
5371#if defined (HAVE_PRSTATUS32_T_PR_WHO)
5372      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5373#endif
5374    }
5375#endif /* HAVE_PRSTATUS32_T */
5376  else
5377    {
5378      /* Fail - we don't know how to handle any other
5379	 note size (ie. data object type).  */
5380      return true;
5381    }
5382
5383  /* Make a ".reg/999" section.  */
5384
5385  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5386  name = bfd_alloc (abfd, strlen (buf) + 1);
5387  if (name == NULL)
5388    return false;
5389  strcpy (name, buf);
5390
5391  sect = bfd_make_section (abfd, name);
5392  if (sect == NULL)
5393    return false;
5394
5395  sect->_raw_size = raw_size;
5396  sect->filepos = note->descpos + offset;
5397
5398  sect->flags = SEC_HAS_CONTENTS;
5399  sect->alignment_power = 2;
5400
5401  if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5402    return false;
5403
5404  return true;
5405}
5406#endif /* defined (HAVE_PRSTATUS_T) */
5407
5408/* Create a pseudosection containing the exact contents of NOTE.  This
5409   actually creates up to two pseudosections:
5410   - For the single-threaded case, a section named NAME, unless
5411     such a section already exists.
5412   - For the multi-threaded case, a section named "NAME/PID", where
5413     PID is elfcore_make_pid (abfd).
5414   Both pseudosections have identical contents: the contents of NOTE.  */
5415
5416static boolean
5417elfcore_make_note_pseudosection (abfd, name, note)
5418     bfd *abfd;
5419     char *name;
5420     Elf_Internal_Note *note;
5421{
5422  char buf[100];
5423  char *threaded_name;
5424  asection *sect;
5425
5426  /* Build the section name.  */
5427
5428  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5429  threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5430  if (threaded_name == NULL)
5431    return false;
5432  strcpy (threaded_name, buf);
5433
5434  sect = bfd_make_section (abfd, threaded_name);
5435  if (sect == NULL)
5436    return false;
5437  sect->_raw_size = note->descsz;
5438  sect->filepos = note->descpos;
5439  sect->flags = SEC_HAS_CONTENTS;
5440  sect->alignment_power = 2;
5441
5442  if (! elfcore_maybe_make_sect (abfd, name, sect))
5443    return false;
5444
5445  return true;
5446}
5447
5448/* There isn't a consistent prfpregset_t across platforms,
5449   but it doesn't matter, because we don't have to pick this
5450   data structure apart.  */
5451
5452static boolean
5453elfcore_grok_prfpreg (abfd, note)
5454     bfd *abfd;
5455     Elf_Internal_Note *note;
5456{
5457  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5458}
5459
5460/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5461   type of 5 (NT_PRXFPREG).  Just include the whole note's contents
5462   literally.  */
5463
5464static boolean
5465elfcore_grok_prxfpreg (abfd, note)
5466     bfd *abfd;
5467     Elf_Internal_Note *note;
5468{
5469  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5470}
5471
5472#if defined (HAVE_PRPSINFO_T)
5473typedef prpsinfo_t   elfcore_psinfo_t;
5474#if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
5475typedef prpsinfo32_t elfcore_psinfo32_t;
5476#endif
5477#endif
5478
5479#if defined (HAVE_PSINFO_T)
5480typedef psinfo_t   elfcore_psinfo_t;
5481#if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
5482typedef psinfo32_t elfcore_psinfo32_t;
5483#endif
5484#endif
5485
5486#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5487
5488/* return a malloc'ed copy of a string at START which is at
5489   most MAX bytes long, possibly without a terminating '\0'.
5490   the copy will always have a terminating '\0'.  */
5491
5492static char*
5493elfcore_strndup (abfd, start, max)
5494     bfd *abfd;
5495     char *start;
5496     int max;
5497{
5498  char *dup;
5499  char *end = memchr (start, '\0', max);
5500  int len;
5501
5502  if (end == NULL)
5503    len = max;
5504  else
5505    len = end - start;
5506
5507  dup = bfd_alloc (abfd, len + 1);
5508  if (dup == NULL)
5509    return NULL;
5510
5511  memcpy (dup, start, len);
5512  dup[len] = '\0';
5513
5514  return dup;
5515}
5516
5517static boolean
5518elfcore_grok_psinfo (abfd, note)
5519     bfd *abfd;
5520     Elf_Internal_Note *note;
5521{
5522  if (note->descsz == sizeof (elfcore_psinfo_t))
5523    {
5524      elfcore_psinfo_t psinfo;
5525
5526      memcpy (&psinfo, note->descdata, sizeof (psinfo));
5527
5528      elf_tdata (abfd)->core_program
5529	= elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5530
5531      elf_tdata (abfd)->core_command
5532	= elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5533    }
5534#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5535  else if (note->descsz == sizeof (elfcore_psinfo32_t))
5536    {
5537      /* 64-bit host, 32-bit corefile */
5538      elfcore_psinfo32_t psinfo;
5539
5540      memcpy (&psinfo, note->descdata, sizeof (psinfo));
5541
5542      elf_tdata (abfd)->core_program
5543	= elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5544
5545      elf_tdata (abfd)->core_command
5546	= elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5547    }
5548#endif
5549
5550  else
5551    {
5552      /* Fail - we don't know how to handle any other
5553	 note size (ie. data object type).  */
5554      return true;
5555    }
5556
5557  /* Note that for some reason, a spurious space is tacked
5558     onto the end of the args in some (at least one anyway)
5559     implementations, so strip it off if it exists.  */
5560
5561  {
5562    char *command = elf_tdata (abfd)->core_command;
5563    int n = strlen (command);
5564
5565    if (0 < n && command[n - 1] == ' ')
5566      command[n - 1] = '\0';
5567  }
5568
5569  return true;
5570}
5571#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5572
5573#if defined (HAVE_PSTATUS_T)
5574static boolean
5575elfcore_grok_pstatus (abfd, note)
5576     bfd *abfd;
5577     Elf_Internal_Note *note;
5578{
5579  if (note->descsz == sizeof (pstatus_t)
5580#if defined (HAVE_PXSTATUS_T)
5581      || note->descsz == sizeof (pxstatus_t)
5582#endif
5583      )
5584    {
5585      pstatus_t pstat;
5586
5587      memcpy (&pstat, note->descdata, sizeof (pstat));
5588
5589      elf_tdata (abfd)->core_pid = pstat.pr_pid;
5590    }
5591#if defined (HAVE_PSTATUS32_T)
5592  else if (note->descsz == sizeof (pstatus32_t))
5593    {
5594      /* 64-bit host, 32-bit corefile */
5595      pstatus32_t pstat;
5596
5597      memcpy (&pstat, note->descdata, sizeof (pstat));
5598
5599      elf_tdata (abfd)->core_pid = pstat.pr_pid;
5600    }
5601#endif
5602  /* Could grab some more details from the "representative"
5603     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5604     NT_LWPSTATUS note, presumably.  */
5605
5606  return true;
5607}
5608#endif /* defined (HAVE_PSTATUS_T) */
5609
5610#if defined (HAVE_LWPSTATUS_T)
5611static boolean
5612elfcore_grok_lwpstatus (abfd, note)
5613     bfd *abfd;
5614     Elf_Internal_Note *note;
5615{
5616  lwpstatus_t lwpstat;
5617  char buf[100];
5618  char *name;
5619  asection *sect;
5620
5621  if (note->descsz != sizeof (lwpstat)
5622#if defined (HAVE_LWPXSTATUS_T)
5623      && note->descsz != sizeof (lwpxstatus_t)
5624#endif
5625      )
5626    return true;
5627
5628  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5629
5630  elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5631  elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5632
5633  /* Make a ".reg/999" section.  */
5634
5635  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5636  name = bfd_alloc (abfd, strlen (buf) + 1);
5637  if (name == NULL)
5638    return false;
5639  strcpy (name, buf);
5640
5641  sect = bfd_make_section (abfd, name);
5642  if (sect == NULL)
5643    return false;
5644
5645#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5646  sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5647  sect->filepos = note->descpos
5648    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5649#endif
5650
5651#if defined (HAVE_LWPSTATUS_T_PR_REG)
5652  sect->_raw_size = sizeof (lwpstat.pr_reg);
5653  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5654#endif
5655
5656  sect->flags = SEC_HAS_CONTENTS;
5657  sect->alignment_power = 2;
5658
5659  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5660    return false;
5661
5662  /* Make a ".reg2/999" section */
5663
5664  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5665  name = bfd_alloc (abfd, strlen (buf) + 1);
5666  if (name == NULL)
5667    return false;
5668  strcpy (name, buf);
5669
5670  sect = bfd_make_section (abfd, name);
5671  if (sect == NULL)
5672    return false;
5673
5674#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5675  sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5676  sect->filepos = note->descpos
5677    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5678#endif
5679
5680#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5681  sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5682  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5683#endif
5684
5685  sect->flags = SEC_HAS_CONTENTS;
5686  sect->alignment_power = 2;
5687
5688  if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5689    return false;
5690
5691  return true;
5692}
5693#endif /* defined (HAVE_LWPSTATUS_T) */
5694
5695#if defined (HAVE_WIN32_PSTATUS_T)
5696static boolean
5697elfcore_grok_win32pstatus (abfd, note)
5698     bfd *abfd;
5699     Elf_Internal_Note *note;
5700{
5701  char buf[30];
5702  char *name;
5703  asection *sect;
5704  win32_pstatus_t pstatus;
5705
5706  if (note->descsz < sizeof (pstatus))
5707    return true;
5708
5709  memcpy (&pstatus, note->descdata, note->descsz);
5710
5711  switch (pstatus.data_type)
5712    {
5713    case NOTE_INFO_PROCESS:
5714      /* FIXME: need to add ->core_command.  */
5715      elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5716      elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5717      break;
5718
5719    case NOTE_INFO_THREAD:
5720      /* Make a ".reg/999" section.  */
5721      sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5722
5723      name = bfd_alloc (abfd, strlen (buf) + 1);
5724      if (name == NULL)
5725	return false;
5726
5727      strcpy (name, buf);
5728
5729      sect = bfd_make_section (abfd, name);
5730      if (sect == NULL)
5731	return false;
5732
5733      sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5734      sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5735						data.thread_info.thread_context);
5736      sect->flags = SEC_HAS_CONTENTS;
5737      sect->alignment_power = 2;
5738
5739      if (pstatus.data.thread_info.is_active_thread)
5740	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5741	  return false;
5742      break;
5743
5744    case NOTE_INFO_MODULE:
5745      /* Make a ".module/xxxxxxxx" section.  */
5746      sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5747
5748      name = bfd_alloc (abfd, strlen (buf) + 1);
5749      if (name == NULL)
5750	return false;
5751
5752      strcpy (name, buf);
5753
5754      sect = bfd_make_section (abfd, name);
5755
5756      if (sect == NULL)
5757	return false;
5758
5759      sect->_raw_size = note->descsz;
5760      sect->filepos = note->descpos;
5761      sect->flags = SEC_HAS_CONTENTS;
5762      sect->alignment_power = 2;
5763      break;
5764
5765    default:
5766      return true;
5767    }
5768
5769  return true;
5770}
5771#endif /* HAVE_WIN32_PSTATUS_T */
5772
5773static boolean
5774elfcore_grok_note (abfd, note)
5775     bfd *abfd;
5776     Elf_Internal_Note *note;
5777{
5778  switch (note->type)
5779    {
5780    default:
5781      return true;
5782
5783#if defined (HAVE_PRSTATUS_T)
5784    case NT_PRSTATUS:
5785      return elfcore_grok_prstatus (abfd, note);
5786#endif
5787
5788#if defined (HAVE_PSTATUS_T)
5789    case NT_PSTATUS:
5790      return elfcore_grok_pstatus (abfd, note);
5791#endif
5792
5793#if defined (HAVE_LWPSTATUS_T)
5794    case NT_LWPSTATUS:
5795      return elfcore_grok_lwpstatus (abfd, note);
5796#endif
5797
5798    case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
5799      return elfcore_grok_prfpreg (abfd, note);
5800
5801#if defined (HAVE_WIN32_PSTATUS_T)
5802    case NT_WIN32PSTATUS:
5803      return elfcore_grok_win32pstatus (abfd, note);
5804#endif
5805
5806    case NT_PRXFPREG:		/* Linux SSE extension */
5807      if (note->namesz == 5
5808	  && ! strcmp (note->namedata, "LINUX"))
5809	return elfcore_grok_prxfpreg (abfd, note);
5810      else
5811	return true;
5812
5813#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5814    case NT_PRPSINFO:
5815    case NT_PSINFO:
5816      return elfcore_grok_psinfo (abfd, note);
5817#endif
5818    }
5819}
5820
5821static boolean
5822elfcore_read_notes (abfd, offset, size)
5823     bfd *abfd;
5824     bfd_vma offset;
5825     bfd_vma size;
5826{
5827  char *buf;
5828  char *p;
5829
5830  if (size <= 0)
5831    return true;
5832
5833  if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5834    return false;
5835
5836  buf = bfd_malloc ((size_t) size);
5837  if (buf == NULL)
5838    return false;
5839
5840  if (bfd_read (buf, size, 1, abfd) != size)
5841    {
5842    error:
5843      free (buf);
5844      return false;
5845    }
5846
5847  p = buf;
5848  while (p < buf + size)
5849    {
5850      /* FIXME: bad alignment assumption.  */
5851      Elf_External_Note *xnp = (Elf_External_Note *) p;
5852      Elf_Internal_Note in;
5853
5854      in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5855
5856      in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5857      in.namedata = xnp->name;
5858
5859      in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5860      in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5861      in.descpos = offset + (in.descdata - buf);
5862
5863      if (! elfcore_grok_note (abfd, &in))
5864	goto error;
5865
5866      p = in.descdata + BFD_ALIGN (in.descsz, 4);
5867    }
5868
5869  free (buf);
5870  return true;
5871}
5872
5873/* FIXME: This function is now unnecessary.  Callers can just call
5874   bfd_section_from_phdr directly.  */
5875
5876boolean
5877_bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5878     bfd *abfd;
5879     Elf_Internal_Phdr* phdr;
5880     int sec_num;
5881{
5882  if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5883    return false;
5884
5885  return true;
5886}
5887
5888/* Providing external access to the ELF program header table.  */
5889
5890/* Return an upper bound on the number of bytes required to store a
5891   copy of ABFD's program header table entries.  Return -1 if an error
5892   occurs; bfd_get_error will return an appropriate code.  */
5893
5894long
5895bfd_get_elf_phdr_upper_bound (abfd)
5896     bfd *abfd;
5897{
5898  if (abfd->xvec->flavour != bfd_target_elf_flavour)
5899    {
5900      bfd_set_error (bfd_error_wrong_format);
5901      return -1;
5902    }
5903
5904  return (elf_elfheader (abfd)->e_phnum
5905	  * sizeof (Elf_Internal_Phdr));
5906}
5907
5908/* Copy ABFD's program header table entries to *PHDRS.  The entries
5909   will be stored as an array of Elf_Internal_Phdr structures, as
5910   defined in include/elf/internal.h.  To find out how large the
5911   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5912
5913   Return the number of program header table entries read, or -1 if an
5914   error occurs; bfd_get_error will return an appropriate code.  */
5915
5916int
5917bfd_get_elf_phdrs (abfd, phdrs)
5918     bfd *abfd;
5919     void *phdrs;
5920{
5921  int num_phdrs;
5922
5923  if (abfd->xvec->flavour != bfd_target_elf_flavour)
5924    {
5925      bfd_set_error (bfd_error_wrong_format);
5926      return -1;
5927    }
5928
5929  num_phdrs = elf_elfheader (abfd)->e_phnum;
5930  memcpy (phdrs, elf_tdata (abfd)->phdr,
5931	  num_phdrs * sizeof (Elf_Internal_Phdr));
5932
5933  return num_phdrs;
5934}
5935