/[gcl]/gcl/binutils/bfd/elf.c
ViewVC logotype

Diff of /gcl/binutils/bfd/elf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by camm, Fri Aug 9 05:35:09 2002 UTC revision 1.2 by camm, Fri Sep 9 23:32:08 2005 UTC
# Line 1  Line 1 
1  /* ELF executable support for BFD.  /* ELF executable support for BFD.
    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002  
    Free Software Foundation, Inc.  
2    
3  This file is part of BFD, the Binary File Descriptor library.     Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4       2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5    
6  This program is free software; you can redistribute it and/or modify     This file is part of BFD, the Binary File Descriptor library.
7  it under the terms of the GNU General Public License as published by  
8  the Free Software Foundation; either version 2 of the License, or     This program is free software; you can redistribute it and/or modify
9  (at your option) any later version.     it under the terms of the GNU General Public License as published by
10       the Free Software Foundation; either version 2 of the License, or
11  This program is distributed in the hope that it will be useful,     (at your option) any later version.
12  but WITHOUT ANY WARRANTY; without even the implied warranty of  
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     This program is distributed in the hope that it will be useful,
14  GNU General Public License for more details.     but WITHOUT ANY WARRANTY; without even the implied warranty of
15       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  You should have received a copy of the GNU General Public License     GNU General Public License for more details.
17  along with this program; if not, write to the Free Software  
18  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */     You should have received a copy of the GNU General Public License
19       along with this program; if not, write to the Free Software
20       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21    
22  /*  SECTION  /*  SECTION
23    
# Line 40  Foundation, Inc., 59 Temple Place - Suit Line 41  Foundation, Inc., 59 Temple Place - Suit
41  #include "elf-bfd.h"  #include "elf-bfd.h"
42  #include "libiberty.h"  #include "libiberty.h"
43    
44  static INLINE struct elf_segment_map *make_mapping  static int elf_sort_sections (const void *, const void *);
45    PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));  static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
46  static boolean map_sections_to_segments PARAMS ((bfd *));  static bfd_boolean prep_headers (bfd *);
47  static int elf_sort_sections PARAMS ((const PTR, const PTR));  static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
48  static boolean assign_file_positions_for_segments PARAMS ((bfd *));  static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));  
 static boolean prep_headers PARAMS ((bfd *));  
 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));  
 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));  
 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));  
 static const char *group_signature PARAMS ((bfd *, Elf_Internal_Shdr *));  
 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));  
 static void merge_sections_remove_hook PARAMS ((bfd *, asection *));  
 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));  
 static boolean assign_section_numbers PARAMS ((bfd *));  
 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));  
 static boolean elf_map_symbols PARAMS ((bfd *));  
 static bfd_size_type get_program_header_size PARAMS ((bfd *));  
 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));  
 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,  
                                           bfd_vma, const char **,  
                                           const char **));  
 static int elfcore_make_pid PARAMS ((bfd *));  
 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));  
 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,  
                                                         Elf_Internal_Note *));  
 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));  
 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));  
 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));  
   
 static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));  
 static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,  
                                                      Elf_Internal_Note *));  
 static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));  
49    
50  /* Swap version information in and out.  The version information is  /* Swap version information in and out.  The version information is
51     currently size independent.  If that ever changes, this code will     currently size independent.  If that ever changes, this code will
# Line 82  static boolean elfcore_grok_netbsd_note Line 54  static boolean elfcore_grok_netbsd_note
54  /* Swap in a Verdef structure.  */  /* Swap in a Verdef structure.  */
55    
56  void  void
57  _bfd_elf_swap_verdef_in (abfd, src, dst)  _bfd_elf_swap_verdef_in (bfd *abfd,
58       bfd *abfd;                           const Elf_External_Verdef *src,
59       const Elf_External_Verdef *src;                           Elf_Internal_Verdef *dst)
      Elf_Internal_Verdef *dst;  
60  {  {
61    dst->vd_version = H_GET_16 (abfd, src->vd_version);    dst->vd_version = H_GET_16 (abfd, src->vd_version);
62    dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);    dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
# Line 99  _bfd_elf_swap_verdef_in (abfd, src, dst) Line 70  _bfd_elf_swap_verdef_in (abfd, src, dst)
70  /* Swap out a Verdef structure.  */  /* Swap out a Verdef structure.  */
71    
72  void  void
73  _bfd_elf_swap_verdef_out (abfd, src, dst)  _bfd_elf_swap_verdef_out (bfd *abfd,
74       bfd *abfd;                            const Elf_Internal_Verdef *src,
75       const Elf_Internal_Verdef *src;                            Elf_External_Verdef *dst)
      Elf_External_Verdef *dst;  
76  {  {
77    H_PUT_16 (abfd, src->vd_version, dst->vd_version);    H_PUT_16 (abfd, src->vd_version, dst->vd_version);
78    H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);    H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
# Line 116  _bfd_elf_swap_verdef_out (abfd, src, dst Line 86  _bfd_elf_swap_verdef_out (abfd, src, dst
86  /* Swap in a Verdaux structure.  */  /* Swap in a Verdaux structure.  */
87    
88  void  void
89  _bfd_elf_swap_verdaux_in (abfd, src, dst)  _bfd_elf_swap_verdaux_in (bfd *abfd,
90       bfd *abfd;                            const Elf_External_Verdaux *src,
91       const Elf_External_Verdaux *src;                            Elf_Internal_Verdaux *dst)
      Elf_Internal_Verdaux *dst;  
92  {  {
93    dst->vda_name = H_GET_32 (abfd, src->vda_name);    dst->vda_name = H_GET_32 (abfd, src->vda_name);
94    dst->vda_next = H_GET_32 (abfd, src->vda_next);    dst->vda_next = H_GET_32 (abfd, src->vda_next);
# Line 128  _bfd_elf_swap_verdaux_in (abfd, src, dst Line 97  _bfd_elf_swap_verdaux_in (abfd, src, dst
97  /* Swap out a Verdaux structure.  */  /* Swap out a Verdaux structure.  */
98    
99  void  void
100  _bfd_elf_swap_verdaux_out (abfd, src, dst)  _bfd_elf_swap_verdaux_out (bfd *abfd,
101       bfd *abfd;                             const Elf_Internal_Verdaux *src,
102       const Elf_Internal_Verdaux *src;                             Elf_External_Verdaux *dst)
      Elf_External_Verdaux *dst;  
103  {  {
104    H_PUT_32 (abfd, src->vda_name, dst->vda_name);    H_PUT_32 (abfd, src->vda_name, dst->vda_name);
105    H_PUT_32 (abfd, src->vda_next, dst->vda_next);    H_PUT_32 (abfd, src->vda_next, dst->vda_next);
# Line 140  _bfd_elf_swap_verdaux_out (abfd, src, ds Line 108  _bfd_elf_swap_verdaux_out (abfd, src, ds
108  /* Swap in a Verneed structure.  */  /* Swap in a Verneed structure.  */
109    
110  void  void
111  _bfd_elf_swap_verneed_in (abfd, src, dst)  _bfd_elf_swap_verneed_in (bfd *abfd,
112       bfd *abfd;                            const Elf_External_Verneed *src,
113       const Elf_External_Verneed *src;                            Elf_Internal_Verneed *dst)
      Elf_Internal_Verneed *dst;  
114  {  {
115    dst->vn_version = H_GET_16 (abfd, src->vn_version);    dst->vn_version = H_GET_16 (abfd, src->vn_version);
116    dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);    dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
# Line 155  _bfd_elf_swap_verneed_in (abfd, src, dst Line 122  _bfd_elf_swap_verneed_in (abfd, src, dst
122  /* Swap out a Verneed structure.  */  /* Swap out a Verneed structure.  */
123    
124  void  void
125  _bfd_elf_swap_verneed_out (abfd, src, dst)  _bfd_elf_swap_verneed_out (bfd *abfd,
126       bfd *abfd;                             const Elf_Internal_Verneed *src,
127       const Elf_Internal_Verneed *src;                             Elf_External_Verneed *dst)
      Elf_External_Verneed *dst;  
128  {  {
129    H_PUT_16 (abfd, src->vn_version, dst->vn_version);    H_PUT_16 (abfd, src->vn_version, dst->vn_version);
130    H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);    H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
# Line 170  _bfd_elf_swap_verneed_out (abfd, src, ds Line 136  _bfd_elf_swap_verneed_out (abfd, src, ds
136  /* Swap in a Vernaux structure.  */  /* Swap in a Vernaux structure.  */
137    
138  void  void
139  _bfd_elf_swap_vernaux_in (abfd, src, dst)  _bfd_elf_swap_vernaux_in (bfd *abfd,
140       bfd *abfd;                            const Elf_External_Vernaux *src,
141       const Elf_External_Vernaux *src;                            Elf_Internal_Vernaux *dst)
      Elf_Internal_Vernaux *dst;  
142  {  {
143    dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);    dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
144    dst->vna_flags = H_GET_16 (abfd, src->vna_flags);    dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
# Line 185  _bfd_elf_swap_vernaux_in (abfd, src, dst Line 150  _bfd_elf_swap_vernaux_in (abfd, src, dst
150  /* Swap out a Vernaux structure.  */  /* Swap out a Vernaux structure.  */
151    
152  void  void
153  _bfd_elf_swap_vernaux_out (abfd, src, dst)  _bfd_elf_swap_vernaux_out (bfd *abfd,
154       bfd *abfd;                             const Elf_Internal_Vernaux *src,
155       const Elf_Internal_Vernaux *src;                             Elf_External_Vernaux *dst)
      Elf_External_Vernaux *dst;  
156  {  {
157    H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);    H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
158    H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);    H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
# Line 200  _bfd_elf_swap_vernaux_out (abfd, src, ds Line 164  _bfd_elf_swap_vernaux_out (abfd, src, ds
164  /* Swap in a Versym structure.  */  /* Swap in a Versym structure.  */
165    
166  void  void
167  _bfd_elf_swap_versym_in (abfd, src, dst)  _bfd_elf_swap_versym_in (bfd *abfd,
168       bfd *abfd;                           const Elf_External_Versym *src,
169       const Elf_External_Versym *src;                           Elf_Internal_Versym *dst)
      Elf_Internal_Versym *dst;  
170  {  {
171    dst->vs_vers = H_GET_16 (abfd, src->vs_vers);    dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
172  }  }
# Line 211  _bfd_elf_swap_versym_in (abfd, src, dst) Line 174  _bfd_elf_swap_versym_in (abfd, src, dst)
174  /* Swap out a Versym structure.  */  /* Swap out a Versym structure.  */
175    
176  void  void
177  _bfd_elf_swap_versym_out (abfd, src, dst)  _bfd_elf_swap_versym_out (bfd *abfd,
178       bfd *abfd;                            const Elf_Internal_Versym *src,
179       const Elf_Internal_Versym *src;                            Elf_External_Versym *dst)
      Elf_External_Versym *dst;  
180  {  {
181    H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);    H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
182  }  }
# Line 223  _bfd_elf_swap_versym_out (abfd, src, dst Line 185  _bfd_elf_swap_versym_out (abfd, src, dst
185     cause invalid hash tables to be generated.  */     cause invalid hash tables to be generated.  */
186    
187  unsigned long  unsigned long
188  bfd_elf_hash (namearg)  bfd_elf_hash (const char *namearg)
      const char *namearg;  
189  {  {
190    const unsigned char *name = (const unsigned char *) namearg;    const unsigned char *name = (const unsigned char *) namearg;
191    unsigned long h = 0;    unsigned long h = 0;
# Line 242  bfd_elf_hash (namearg) Line 203  bfd_elf_hash (namearg)
203            h ^= g;            h ^= g;
204          }          }
205      }      }
206    return h;    return h & 0xffffffff;
207  }  }
208    
209  /* Read a specified number of bytes at a specified offset in an ELF  /* Read a specified number of bytes at a specified offset in an ELF
210     file, into a newly allocated buffer, and return a pointer to the     file, into a newly allocated buffer, and return a pointer to the
211     buffer.  */     buffer.  */
212    
213  static char *  static bfd_byte *
214  elf_read (abfd, offset, size)  elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
      bfd *abfd;  
      file_ptr offset;  
      bfd_size_type size;  
215  {  {
216    char *buf;    bfd_byte *buf;
217    
218    if ((buf = bfd_alloc (abfd, size)) == NULL)    if ((buf = bfd_alloc (abfd, size)) == NULL)
219      return NULL;      return NULL;
220    if (bfd_seek (abfd, offset, SEEK_SET) != 0)    if (bfd_seek (abfd, offset, SEEK_SET) != 0)
221      return NULL;      return NULL;
222    if (bfd_bread ((PTR) buf, size, abfd) != size)    if (bfd_bread (buf, size, abfd) != size)
223      {      {
224        if (bfd_get_error () != bfd_error_system_call)        if (bfd_get_error () != bfd_error_system_call)
225          bfd_set_error (bfd_error_file_truncated);          bfd_set_error (bfd_error_file_truncated);
# Line 270  elf_read (abfd, offset, size) Line 228  elf_read (abfd, offset, size)
228    return buf;    return buf;
229  }  }
230    
231  boolean  bfd_boolean
232  bfd_elf_mkobject (abfd)  bfd_elf_mkobject (bfd *abfd)
      bfd *abfd;  
233  {  {
234    /* This just does initialization.  */    /* This just does initialization.  */
235    /* coff_mkobject zalloc's space for tdata.coff_obj_data ...  */    /* coff_mkobject zalloc's space for tdata.coff_obj_data ...  */
236    bfd_size_type amt = sizeof (struct elf_obj_tdata);    elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
   elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);  
237    if (elf_tdata (abfd) == 0)    if (elf_tdata (abfd) == 0)
238      return false;      return FALSE;
239    /* Since everything is done at close time, do we need any    /* Since everything is done at close time, do we need any
240       initialization?  */       initialization?  */
241    
242    return true;    return TRUE;
243  }  }
244    
245  boolean  bfd_boolean
246  bfd_elf_mkcorefile (abfd)  bfd_elf_mkcorefile (bfd *abfd)
      bfd *abfd;  
247  {  {
248    /* I think this can be done just like an object file.  */    /* I think this can be done just like an object file.  */
249    return bfd_elf_mkobject (abfd);    return bfd_elf_mkobject (abfd);
250  }  }
251    
252  char *  char *
253  bfd_elf_get_str_section (abfd, shindex)  bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
      bfd *abfd;  
      unsigned int shindex;  
254  {  {
255    Elf_Internal_Shdr **i_shdrp;    Elf_Internal_Shdr **i_shdrp;
256    char *shstrtab = NULL;    bfd_byte *shstrtab = NULL;
257    file_ptr offset;    file_ptr offset;
258    bfd_size_type shstrtabsize;    bfd_size_type shstrtabsize;
259    
260    i_shdrp = elf_elfsections (abfd);    i_shdrp = elf_elfsections (abfd);
261    if (i_shdrp == 0 || i_shdrp[shindex] == 0)    if (i_shdrp == 0 || i_shdrp[shindex] == 0)
262      return 0;      return NULL;
263    
264    shstrtab = (char *) i_shdrp[shindex]->contents;    shstrtab = i_shdrp[shindex]->contents;
265    if (shstrtab == NULL)    if (shstrtab == NULL)
266      {      {
267        /* No cached one, attempt to read, and cache what we read.  */        /* No cached one, attempt to read, and cache what we read.  */
268        offset = i_shdrp[shindex]->sh_offset;        offset = i_shdrp[shindex]->sh_offset;
269        shstrtabsize = i_shdrp[shindex]->sh_size;        shstrtabsize = i_shdrp[shindex]->sh_size;
270        shstrtab = elf_read (abfd, offset, shstrtabsize);        shstrtab = elf_read (abfd, offset, shstrtabsize);
271        i_shdrp[shindex]->contents = (PTR) shstrtab;        i_shdrp[shindex]->contents = shstrtab;
272      }      }
273    return shstrtab;    return (char *) shstrtab;
274  }  }
275    
276  char *  char *
277  bfd_elf_string_from_elf_section (abfd, shindex, strindex)  bfd_elf_string_from_elf_section (bfd *abfd,
278       bfd *abfd;                                   unsigned int shindex,
279       unsigned int shindex;                                   unsigned int strindex)
      unsigned int strindex;  
280  {  {
281    Elf_Internal_Shdr *hdr;    Elf_Internal_Shdr *hdr;
282    
# Line 339  bfd_elf_string_from_elf_section (abfd, s Line 291  bfd_elf_string_from_elf_section (abfd, s
291    
292    if (strindex >= hdr->sh_size)    if (strindex >= hdr->sh_size)
293      {      {
294          unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
295        (*_bfd_error_handler)        (*_bfd_error_handler)
296          (_("%s: invalid string offset %u >= %lu for section `%s'"),          (_("%B: invalid string offset %u >= %lu for section `%s'"),
297           bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,           abfd, strindex, (unsigned long) hdr->sh_size,
298           ((shindex == elf_elfheader(abfd)->e_shstrndx           (shindex == shstrndx && strindex == hdr->sh_name
            && strindex == hdr->sh_name)  
299            ? ".shstrtab"            ? ".shstrtab"
300            : elf_string_from_elf_strtab (abfd, hdr->sh_name)));            : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
301        return "";        return "";
302      }      }
303    
# Line 359  bfd_elf_string_from_elf_section (abfd, s Line 311  bfd_elf_string_from_elf_section (abfd, s
311     symbols, and symbol section index extensions, respectively.  */     symbols, and symbol section index extensions, respectively.  */
312    
313  Elf_Internal_Sym *  Elf_Internal_Sym *
314  bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,  bfd_elf_get_elf_syms (bfd *ibfd,
315                        intsym_buf, extsym_buf, extshndx_buf)                        Elf_Internal_Shdr *symtab_hdr,
316       bfd *ibfd;                        size_t symcount,
317       Elf_Internal_Shdr *symtab_hdr;                        size_t symoffset,
318       size_t symcount;                        Elf_Internal_Sym *intsym_buf,
319       size_t symoffset;                        void *extsym_buf,
320       Elf_Internal_Sym *intsym_buf;                        Elf_External_Sym_Shndx *extshndx_buf)
      PTR extsym_buf;  
      Elf_External_Sym_Shndx *extshndx_buf;  
321  {  {
322    Elf_Internal_Shdr *shndx_hdr;    Elf_Internal_Shdr *shndx_hdr;
323    PTR alloc_ext;    void *alloc_ext;
324    const PTR esym;    const bfd_byte *esym;
325    Elf_External_Sym_Shndx *alloc_extshndx;    Elf_External_Sym_Shndx *alloc_extshndx;
326    Elf_External_Sym_Shndx *shndx;    Elf_External_Sym_Shndx *shndx;
327    Elf_Internal_Sym *isym;    Elf_Internal_Sym *isym;
328    Elf_Internal_Sym *isymend;    Elf_Internal_Sym *isymend;
329    struct elf_backend_data *bed;    const struct elf_backend_data *bed;
330    size_t extsym_size;    size_t extsym_size;
331    bfd_size_type amt;    bfd_size_type amt;
332    file_ptr pos;    file_ptr pos;
# Line 417  bfd_elf_get_elf_syms (ibfd, symtab_hdr, Line 367  bfd_elf_get_elf_syms (ibfd, symtab_hdr,
367        pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);        pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
368        if (extshndx_buf == NULL)        if (extshndx_buf == NULL)
369          {          {
370            alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);            alloc_extshndx = bfd_malloc (amt);
371            extshndx_buf = alloc_extshndx;            extshndx_buf = alloc_extshndx;
372          }          }
373        if (extshndx_buf == NULL        if (extshndx_buf == NULL
# Line 432  bfd_elf_get_elf_syms (ibfd, symtab_hdr, Line 382  bfd_elf_get_elf_syms (ibfd, symtab_hdr,
382    if (intsym_buf == NULL)    if (intsym_buf == NULL)
383      {      {
384        bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);        bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
385        intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);        intsym_buf = bfd_malloc (amt);
386        if (intsym_buf == NULL)        if (intsym_buf == NULL)
387          goto out;          goto out;
388      }      }
# Line 442  bfd_elf_get_elf_syms (ibfd, symtab_hdr, Line 392  bfd_elf_get_elf_syms (ibfd, symtab_hdr,
392    for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;    for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
393         isym < isymend;         isym < isymend;
394         esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)         esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
395      (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);      (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
396    
397   out:   out:
398    if (alloc_ext != NULL)    if (alloc_ext != NULL)
# Line 453  bfd_elf_get_elf_syms (ibfd, symtab_hdr, Line 403  bfd_elf_get_elf_syms (ibfd, symtab_hdr,
403    return intsym_buf;    return intsym_buf;
404  }  }
405    
406    /* Look up a symbol name.  */
407    const char *
408    bfd_elf_sym_name (bfd *abfd,
409                      Elf_Internal_Shdr *symtab_hdr,
410                      Elf_Internal_Sym *isym)
411    {
412      unsigned int iname = isym->st_name;
413      unsigned int shindex = symtab_hdr->sh_link;
414      if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
415          /* Check for a bogus st_shndx to avoid crashing.  */
416          && isym->st_shndx < elf_numsections (abfd)
417          && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
418        {
419          iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
420          shindex = elf_elfheader (abfd)->e_shstrndx;
421        }
422    
423      return bfd_elf_string_from_elf_section (abfd, shindex, iname);
424    }
425    
426  /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP  /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
427     sections.  The first element is the flags, the rest are section     sections.  The first element is the flags, the rest are section
428     pointers.  */     pointers.  */
# Line 466  typedef union elf_internal_group { Line 436  typedef union elf_internal_group {
436     signature just a string?  */     signature just a string?  */
437    
438  static const char *  static const char *
439  group_signature (abfd, ghdr)  group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
      bfd *abfd;  
      Elf_Internal_Shdr *ghdr;  
440  {  {
441    Elf_Internal_Shdr *hdr;    Elf_Internal_Shdr *hdr;
442    unsigned char esym[sizeof (Elf64_External_Sym)];    unsigned char esym[sizeof (Elf64_External_Sym)];
443    Elf_External_Sym_Shndx eshndx;    Elf_External_Sym_Shndx eshndx;
444    Elf_Internal_Sym isym;    Elf_Internal_Sym isym;
   unsigned int iname;  
   unsigned int shindex;  
445    
446    /* First we need to ensure the symbol table is available.  */    /* First we need to ensure the symbol table is available.  Make sure
447    if (! bfd_section_from_shdr (abfd, ghdr->sh_link))       that it is a symbol table section.  */
448      hdr = elf_elfsections (abfd) [ghdr->sh_link];
449      if (hdr->sh_type != SHT_SYMTAB
450          || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
451      return NULL;      return NULL;
452    
453    /* Go read the symbol.  */    /* Go read the symbol.  */
# Line 487  group_signature (abfd, ghdr) Line 456  group_signature (abfd, ghdr)
456                              &isym, esym, &eshndx) == NULL)                              &isym, esym, &eshndx) == NULL)
457      return NULL;      return NULL;
458    
459    /* Look up the symbol name.  */    return bfd_elf_sym_name (abfd, hdr, &isym);
   iname = isym.st_name;  
   shindex = hdr->sh_link;  
   if (iname == 0 && ELF_ST_TYPE (isym.st_info) == STT_SECTION)  
     {  
       iname = elf_elfsections (abfd)[isym.st_shndx]->sh_name;  
       shindex = elf_elfheader (abfd)->e_shstrndx;  
     }  
   
   return bfd_elf_string_from_elf_section (abfd, shindex, iname);  
460  }  }
461    
462  /* Set next_in_group list pointer, and group name for NEWSECT.  */  /* Set next_in_group list pointer, and group name for NEWSECT.  */
463    
464  static boolean  static bfd_boolean
465  setup_group (abfd, hdr, newsect)  setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
      bfd *abfd;  
      Elf_Internal_Shdr *hdr;  
      asection *newsect;  
466  {  {
467    unsigned int num_group = elf_tdata (abfd)->num_group;    unsigned int num_group = elf_tdata (abfd)->num_group;
468    
# Line 537  setup_group (abfd, hdr, newsect) Line 494  setup_group (abfd, hdr, newsect)
494            bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);            bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
495            elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);            elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
496            if (elf_tdata (abfd)->group_sect_ptr == NULL)            if (elf_tdata (abfd)->group_sect_ptr == NULL)
497              return false;              return FALSE;
498    
499            num_group = 0;            num_group = 0;
500            for (i = 0; i < shnum; i++)            for (i = 0; i < shnum; i++)
# Line 560  setup_group (abfd, hdr, newsect) Line 517  setup_group (abfd, hdr, newsect)
517                        || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0                        || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
518                        || (bfd_bread (shdr->contents, shdr->sh_size, abfd)                        || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
519                            != shdr->sh_size))                            != shdr->sh_size))
520                      return false;                      return FALSE;
521    
522                    /* Translate raw contents, a flag word followed by an                    /* Translate raw contents, a flag word followed by an
523                       array of elf section indices all in target byte order,                       array of elf section indices all in target byte order,
# Line 586  setup_group (abfd, hdr, newsect) Line 543  setup_group (abfd, hdr, newsect)
543                        if (idx >= shnum)                        if (idx >= shnum)
544                          {                          {
545                            ((*_bfd_error_handler)                            ((*_bfd_error_handler)
546                             (_("%s: invalid SHT_GROUP entry"),                             (_("%B: invalid SHT_GROUP entry"), abfd));
                             bfd_archive_filename (abfd)));  
547                            idx = 0;                            idx = 0;
548                          }                          }
549                        dest->shdr = elf_elfsections (abfd)[idx];                        dest->shdr = elf_elfsections (abfd)[idx];
# Line 637  setup_group (abfd, hdr, newsect) Line 593  setup_group (abfd, hdr, newsect)
593    
594                      gname = group_signature (abfd, shdr);                      gname = group_signature (abfd, shdr);
595                      if (gname == NULL)                      if (gname == NULL)
596                        return false;                        return FALSE;
597                      elf_group_name (newsect) = gname;                      elf_group_name (newsect) = gname;
598    
599                      /* Start a circular list with one element.  */                      /* Start a circular list with one element.  */
# Line 657  setup_group (abfd, hdr, newsect) Line 613  setup_group (abfd, hdr, newsect)
613    
614    if (elf_group_name (newsect) == NULL)    if (elf_group_name (newsect) == NULL)
615      {      {
616        (*_bfd_error_handler) (_("%s: no group info for section %s"),        (*_bfd_error_handler) (_("%B: no group info for section %A"),
617                               bfd_archive_filename (abfd), newsect->name);                               abfd, newsect);
618      }      }
619    return true;    return TRUE;
620  }  }
621    
622  boolean  bfd_boolean
623  bfd_elf_discard_group (abfd, group)  _bfd_elf_setup_group_pointers (bfd *abfd)
      bfd *abfd ATTRIBUTE_UNUSED;  
      asection *group;  
624  {  {
625    asection *first = elf_next_in_group (group);    unsigned int i;
626    asection *s = first;    unsigned int num_group = elf_tdata (abfd)->num_group;
627      bfd_boolean result = TRUE;
628    
629      if (num_group == (unsigned) -1)
630        return result;
631    
632    while (s != NULL)    for (i = 0; i < num_group; i++)
633      {      {
634        s->output_section = bfd_abs_section_ptr;        Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
635        s = elf_next_in_group (s);        Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
636        /* These lists are circular.  */        unsigned int n_elt = shdr->sh_size / 4;
637        if (s == first)  
638          break;        while (--n_elt != 0)
639            if ((++idx)->shdr->bfd_section)
640              elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
641            else if (idx->shdr->sh_type == SHT_RELA
642                     || idx->shdr->sh_type == SHT_REL)
643              /* We won't include relocation sections in section groups in
644                 output object files. We adjust the group section size here
645                 so that relocatable link will work correctly when
646                 relocation sections are in section group in input object
647                 files.  */
648              shdr->bfd_section->size -= 4;
649            else
650              {
651                /* There are some unknown sections in the group.  */
652                (*_bfd_error_handler)
653                  (_("%B: unknown [%d] section `%s' in group [%s]"),
654                   abfd,
655                   (unsigned int) idx->shdr->sh_type,
656                   bfd_elf_string_from_elf_section (abfd,
657                                                    (elf_elfheader (abfd)
658                                                     ->e_shstrndx),
659                                                    idx->shdr->sh_name),
660                   shdr->bfd_section->name);
661                result = FALSE;
662              }
663      }      }
664    return true;    return result;
665    }
666    
667    bfd_boolean
668    bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
669    {
670      return elf_next_in_group (sec) != NULL;
671  }  }
672    
673  /* Make a BFD section from an ELF section.  We store a pointer to the  /* Make a BFD section from an ELF section.  We store a pointer to the
674     BFD section in the bfd_section field of the header.  */     BFD section in the bfd_section field of the header.  */
675    
676  boolean  bfd_boolean
677  _bfd_elf_make_section_from_shdr (abfd, hdr, name)  _bfd_elf_make_section_from_shdr (bfd *abfd,
678       bfd *abfd;                                   Elf_Internal_Shdr *hdr,
679       Elf_Internal_Shdr *hdr;                                   const char *name)
      const char *name;  
680  {  {
681    asection *newsect;    asection *newsect;
682    flagword flags;    flagword flags;
683    struct elf_backend_data *bed;    const struct elf_backend_data *bed;
684    
685    if (hdr->bfd_section != NULL)    if (hdr->bfd_section != NULL)
686      {      {
687        BFD_ASSERT (strcmp (name,        BFD_ASSERT (strcmp (name,
688                            bfd_get_section_name (abfd, hdr->bfd_section)) == 0);                            bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
689        return true;        return TRUE;
690      }      }
691    
692    newsect = bfd_make_section_anyway (abfd, name);    newsect = bfd_make_section_anyway (abfd, name);
693    if (newsect == NULL)    if (newsect == NULL)
694      return false;      return FALSE;
695    
696      hdr->bfd_section = newsect;
697      elf_section_data (newsect)->this_hdr = *hdr;
698    
699      /* Always use the real type/flags.  */
700      elf_section_type (newsect) = hdr->sh_type;
701      elf_section_flags (newsect) = hdr->sh_flags;
702    
703    newsect->filepos = hdr->sh_offset;    newsect->filepos = hdr->sh_offset;
704    
# Line 712  _bfd_elf_make_section_from_shdr (abfd, h Line 706  _bfd_elf_make_section_from_shdr (abfd, h
706        || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)        || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
707        || ! bfd_set_section_alignment (abfd, newsect,        || ! bfd_set_section_alignment (abfd, newsect,
708                                        bfd_log2 ((bfd_vma) hdr->sh_addralign)))                                        bfd_log2 ((bfd_vma) hdr->sh_addralign)))
709      return false;      return FALSE;
710    
711    flags = SEC_NO_FLAGS;    flags = SEC_NO_FLAGS;
712    if (hdr->sh_type != SHT_NOBITS)    if (hdr->sh_type != SHT_NOBITS)
# Line 740  _bfd_elf_make_section_from_shdr (abfd, h Line 734  _bfd_elf_make_section_from_shdr (abfd, h
734      }      }
735    if (hdr->sh_flags & SHF_GROUP)    if (hdr->sh_flags & SHF_GROUP)
736      if (!setup_group (abfd, hdr, newsect))      if (!setup_group (abfd, hdr, newsect))
737        return false;        return FALSE;
738    if ((hdr->sh_flags & SHF_TLS) != 0)    if ((hdr->sh_flags & SHF_TLS) != 0)
739      flags |= SEC_THREAD_LOCAL;      flags |= SEC_THREAD_LOCAL;
740    
# Line 777  _bfd_elf_make_section_from_shdr (abfd, h Line 771  _bfd_elf_make_section_from_shdr (abfd, h
771    bed = get_elf_backend_data (abfd);    bed = get_elf_backend_data (abfd);
772    if (bed->elf_backend_section_flags)    if (bed->elf_backend_section_flags)
773      if (! bed->elf_backend_section_flags (&flags, hdr))      if (! bed->elf_backend_section_flags (&flags, hdr))
774        return false;        return FALSE;
775    
776    if (! bfd_set_section_flags (abfd, newsect, flags))    if (! bfd_set_section_flags (abfd, newsect, flags))
777      return false;      return FALSE;
778    
779    if ((flags & SEC_ALLOC) != 0)    if ((flags & SEC_ALLOC) != 0)
780      {      {
# Line 848  _bfd_elf_make_section_from_shdr (abfd, h Line 842  _bfd_elf_make_section_from_shdr (abfd, h
842          }          }
843      }      }
844    
845    hdr->bfd_section = newsect;    return TRUE;
   elf_section_data (newsect)->this_hdr = *hdr;  
   
   return true;  
846  }  }
847    
848  /*  /*
# Line 870  DESCRIPTION Line 861  DESCRIPTION
861  */  */
862    
863  struct elf_internal_shdr *  struct elf_internal_shdr *
864  bfd_elf_find_section (abfd, name)  bfd_elf_find_section (bfd *abfd, char *name)
      bfd *abfd;  
      char *name;  
865  {  {
866    Elf_Internal_Shdr **i_shdrp;    Elf_Internal_Shdr **i_shdrp;
867    char *shstrtab;    char *shstrtab;
# Line 901  const char *const bfd_elf_section_type_n Line 890  const char *const bfd_elf_section_type_n
890    "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",    "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
891  };  };
892    
893  /* ELF relocs are against symbols.  If we are producing relocateable  /* ELF relocs are against symbols.  If we are producing relocatable
894     output, and the reloc is against an external symbol, and nothing     output, and the reloc is against an external symbol, and nothing
895     has given us any additional addend, the resulting reloc will also     has given us any additional addend, the resulting reloc will also
896     be against the same symbol.  In such a case, we don't want to     be against the same symbol.  In such a case, we don't want to
# Line 909  const char *const bfd_elf_section_type_n Line 898  const char *const bfd_elf_section_type_n
898     all be done at final link time.  Rather than put special case code     all be done at final link time.  Rather than put special case code
899     into bfd_perform_relocation, all the reloc types use this howto     into bfd_perform_relocation, all the reloc types use this howto
900     function.  It just short circuits the reloc if producing     function.  It just short circuits the reloc if producing
901     relocateable output against an external symbol.  */     relocatable output against an external symbol.  */
902    
903  bfd_reloc_status_type  bfd_reloc_status_type
904  bfd_elf_generic_reloc (abfd,  bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
905                         reloc_entry,                         arelent *reloc_entry,
906                         symbol,                         asymbol *symbol,
907                         data,                         void *data ATTRIBUTE_UNUSED,
908                         input_section,                         asection *input_section,
909                         output_bfd,                         bfd *output_bfd,
910                         error_message)                         char **error_message ATTRIBUTE_UNUSED)
      bfd *abfd ATTRIBUTE_UNUSED;  
      arelent *reloc_entry;  
      asymbol *symbol;  
      PTR data ATTRIBUTE_UNUSED;  
      asection *input_section;  
      bfd *output_bfd;  
      char **error_message ATTRIBUTE_UNUSED;  
911  {  {
912    if (output_bfd != (bfd *) NULL    if (output_bfd != NULL
913        && (symbol->flags & BSF_SECTION_SYM) == 0        && (symbol->flags & BSF_SECTION_SYM) == 0
914        && (! reloc_entry->howto->partial_inplace        && (! reloc_entry->howto->partial_inplace
915            || reloc_entry->addend == 0))            || reloc_entry->addend == 0))
# Line 942  bfd_elf_generic_reloc (abfd, Line 924  bfd_elf_generic_reloc (abfd,
924  /* Make sure sec_info_type is cleared if sec_info is cleared too.  */  /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
925    
926  static void  static void
927  merge_sections_remove_hook (abfd, sec)  merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
928       bfd *abfd ATTRIBUTE_UNUSED;                              asection *sec)
929       asection *sec;  {
930  {    BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
931    struct bfd_elf_section_data *sec_data;    sec->sec_info_type = ELF_INFO_TYPE_NONE;
   
   sec_data = elf_section_data (sec);  
   BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);  
   sec_data->sec_info_type = ELF_INFO_TYPE_NONE;  
932  }  }
933    
934  /* Finish SHF_MERGE section merging.  */  /* Finish SHF_MERGE section merging.  */
935    
936  boolean  bfd_boolean
937  _bfd_elf_merge_sections (abfd, info)  _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
938       bfd *abfd;  {
939       struct bfd_link_info *info;    bfd *ibfd;
940  {    asection *sec;
941    if (!is_elf_hash_table (info))  
942      return false;    if (!is_elf_hash_table (info->hash))
943    if (elf_hash_table (info)->merge_info)      return FALSE;
944      _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,  
945      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
946        if ((ibfd->flags & DYNAMIC) == 0)
947          for (sec = ibfd->sections; sec != NULL; sec = sec->next)
948            if ((sec->flags & SEC_MERGE) != 0
949                && !bfd_is_abs_section (sec->output_section))
950              {
951                struct bfd_elf_section_data *secdata;
952    
953                secdata = elf_section_data (sec);
954                if (! _bfd_add_merge_section (abfd,
955                                              &elf_hash_table (info)->merge_info,
956                                              sec, &secdata->sec_info))
957                  return FALSE;
958                else if (secdata->sec_info)
959                  sec->sec_info_type = ELF_INFO_TYPE_MERGE;
960              }
961    
962      if (elf_hash_table (info)->merge_info != NULL)
963        _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
964                           merge_sections_remove_hook);                           merge_sections_remove_hook);
965    return true;    return TRUE;
966  }  }
967    
968  void  void
969  _bfd_elf_link_just_syms (sec, info)  _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
      asection *sec;  
      struct bfd_link_info *info;  
970  {  {
971    sec->output_section = bfd_abs_section_ptr;    sec->output_section = bfd_abs_section_ptr;
972    sec->output_offset = sec->vma;    sec->output_offset = sec->vma;
973    if (!is_elf_hash_table (info))    if (!is_elf_hash_table (info->hash))
974      return;      return;
975    
976    elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;    sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
977  }  }
978    
979  /* Copy the program header and other data from one object module to  /* Copy the program header and other data from one object module to
980     another.  */     another.  */
981    
982  boolean  bfd_boolean
983  _bfd_elf_copy_private_bfd_data (ibfd, obfd)  _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
      bfd *ibfd;  
      bfd *obfd;  
984  {  {
985    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
986        || bfd_get_flavour (obfd) != bfd_target_elf_flavour)        || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
987      return true;      return TRUE;
988    
989    BFD_ASSERT (!elf_flags_init (obfd)    BFD_ASSERT (!elf_flags_init (obfd)
990                || (elf_elfheader (obfd)->e_flags                || (elf_elfheader (obfd)->e_flags
# Line 999  _bfd_elf_copy_private_bfd_data (ibfd, ob Line 992  _bfd_elf_copy_private_bfd_data (ibfd, ob
992    
993    elf_gp (obfd) = elf_gp (ibfd);    elf_gp (obfd) = elf_gp (ibfd);
994    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
995    elf_flags_init (obfd) = true;    elf_flags_init (obfd) = TRUE;
996    return true;    return TRUE;
997  }  }
998    
999  /* Print out the program headers.  */  /* Print out the program headers.  */
1000    
1001  boolean  bfd_boolean
1002  _bfd_elf_print_private_bfd_data (abfd, farg)  _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
      bfd *abfd;  
      PTR farg;  
1003  {  {
1004    FILE *f = (FILE *) farg;    FILE *f = farg;
1005    Elf_Internal_Phdr *p;    Elf_Internal_Phdr *p;
1006    asection *s;    asection *s;
1007    bfd_byte *dynbuf = NULL;    bfd_byte *dynbuf = NULL;
# Line 1038  _bfd_elf_print_private_bfd_data (abfd, f Line 1029  _bfd_elf_print_private_bfd_data (abfd, f
1029              case PT_PHDR: pt = "PHDR"; break;              case PT_PHDR: pt = "PHDR"; break;
1030              case PT_TLS: pt = "TLS"; break;              case PT_TLS: pt = "TLS"; break;
1031              case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;              case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1032                case PT_GNU_STACK: pt = "STACK"; break;
1033                case PT_GNU_RELRO: pt = "RELRO"; break;
1034              default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;              default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1035              }              }
1036            fprintf (f, "%8s off    0x", pt);            fprintf (f, "%8s off    0x", pt);
# Line 1068  _bfd_elf_print_private_bfd_data (abfd, f Line 1061  _bfd_elf_print_private_bfd_data (abfd, f
1061        unsigned long shlink;        unsigned long shlink;
1062        bfd_byte *extdyn, *extdynend;        bfd_byte *extdyn, *extdynend;
1063        size_t extdynsize;        size_t extdynsize;
1064        void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));        void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1065    
1066        fprintf (f, _("\nDynamic Section:\n"));        fprintf (f, _("\nDynamic Section:\n"));
1067    
1068        dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);        if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
       if (dynbuf == NULL)  
         goto error_return;  
       if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,  
                                       s->_raw_size))  
1069          goto error_return;          goto error_return;
1070    
1071        elfsec = _bfd_elf_section_from_bfd_section (abfd, s);        elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
# Line 1088  _bfd_elf_print_private_bfd_data (abfd, f Line 1077  _bfd_elf_print_private_bfd_data (abfd, f
1077        swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;        swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1078    
1079        extdyn = dynbuf;        extdyn = dynbuf;
1080        extdynend = extdyn + s->_raw_size;        extdynend = extdyn + s->size;
1081        for (; extdyn < extdynend; extdyn += extdynsize)        for (; extdyn < extdynend; extdyn += extdynsize)
1082          {          {
1083            Elf_Internal_Dyn dyn;            Elf_Internal_Dyn dyn;
1084            const char *name;            const char *name;
1085            char ab[20];            char ab[20];
1086            boolean stringp;            bfd_boolean stringp;
1087    
1088            (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);            (*swap_dyn_in) (abfd, extdyn, &dyn);
1089    
1090            if (dyn.d_tag == DT_NULL)            if (dyn.d_tag == DT_NULL)
1091              break;              break;
1092    
1093            stringp = false;            stringp = FALSE;
1094            switch (dyn.d_tag)            switch (dyn.d_tag)
1095              {              {
1096              default:              default:
# Line 1109  _bfd_elf_print_private_bfd_data (abfd, f Line 1098  _bfd_elf_print_private_bfd_data (abfd, f
1098                name = ab;                name = ab;
1099                break;                break;
1100    
1101              case DT_NEEDED: name = "NEEDED"; stringp = true; break;              case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1102              case DT_PLTRELSZ: name = "PLTRELSZ"; break;              case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1103              case DT_PLTGOT: name = "PLTGOT"; break;              case DT_PLTGOT: name = "PLTGOT"; break;
1104              case DT_HASH: name = "HASH"; break;              case DT_HASH: name = "HASH"; break;
# Line 1122  _bfd_elf_print_private_bfd_data (abfd, f Line 1111  _bfd_elf_print_private_bfd_data (abfd, f
1111              case DT_SYMENT: name = "SYMENT"; break;              case DT_SYMENT: name = "SYMENT"; break;
1112              case DT_INIT: name = "INIT"; break;              case DT_INIT: name = "INIT"; break;
1113              case DT_FINI: name = "FINI"; break;              case DT_FINI: name = "FINI"; break;
1114              case DT_SONAME: name = "SONAME"; stringp = true; break;              case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1115              case DT_RPATH: name = "RPATH"; stringp = true; break;              case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1116              case DT_SYMBOLIC: name = "SYMBOLIC"; break;              case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1117              case DT_REL: name = "REL"; break;              case DT_REL: name = "REL"; break;
1118              case DT_RELSZ: name = "RELSZ"; break;              case DT_RELSZ: name = "RELSZ"; break;
# Line 1137  _bfd_elf_print_private_bfd_data (abfd, f Line 1126  _bfd_elf_print_private_bfd_data (abfd, f
1126              case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;              case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1127              case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;              case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1128              case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;              case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1129              case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;              case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1130              case DT_FLAGS: name = "FLAGS"; break;              case DT_FLAGS: name = "FLAGS"; break;
1131              case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;              case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1132              case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;              case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
# Line 1149  _bfd_elf_print_private_bfd_data (abfd, f Line 1138  _bfd_elf_print_private_bfd_data (abfd, f
1138              case DT_POSFLAG_1: name = "POSFLAG_1"; break;              case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1139              case DT_SYMINSZ: name = "SYMINSZ"; break;              case DT_SYMINSZ: name = "SYMINSZ"; break;
1140              case DT_SYMINENT: name = "SYMINENT"; break;              case DT_SYMINENT: name = "SYMINENT"; break;
1141              case DT_CONFIG: name = "CONFIG"; stringp = true; break;              case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1142              case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;              case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1143              case DT_AUDIT: name = "AUDIT"; stringp = true; break;              case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1144              case DT_PLTPAD: name = "PLTPAD"; break;              case DT_PLTPAD: name = "PLTPAD"; break;
1145              case DT_MOVETAB: name = "MOVETAB"; break;              case DT_MOVETAB: name = "MOVETAB"; break;
1146              case DT_SYMINFO: name = "SYMINFO"; break;              case DT_SYMINFO: name = "SYMINFO"; break;
# Line 1163  _bfd_elf_print_private_bfd_data (abfd, f Line 1152  _bfd_elf_print_private_bfd_data (abfd, f
1152              case DT_VERDEFNUM: name = "VERDEFNUM"; break;              case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1153              case DT_VERNEED: name = "VERNEED"; break;              case DT_VERNEED: name = "VERNEED"; break;
1154              case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;              case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1155              case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;              case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1156              case DT_USED: name = "USED"; break;              case DT_USED: name = "USED"; break;
1157              case DT_FILTER: name = "FILTER"; stringp = true; break;              case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1158              }              }
1159    
1160            fprintf (f, "  %-11s ", name);            fprintf (f, "  %-11s ", name);
# Line 1191  _bfd_elf_print_private_bfd_data (abfd, f Line 1180  _bfd_elf_print_private_bfd_data (abfd, f
1180    if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)    if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1181        || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))        || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1182      {      {
1183        if (! _bfd_elf_slurp_version_tables (abfd))        if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1184          return false;          return FALSE;
1185      }      }
1186    
1187    if (elf_dynverdef (abfd) != 0)    if (elf_dynverdef (abfd) != 0)
# Line 1234  _bfd_elf_print_private_bfd_data (abfd, f Line 1223  _bfd_elf_print_private_bfd_data (abfd, f
1223          }          }
1224      }      }
1225    
1226    return true;    return TRUE;
1227    
1228   error_return:   error_return:
1229    if (dynbuf != NULL)    if (dynbuf != NULL)
1230      free (dynbuf);      free (dynbuf);
1231    return false;    return FALSE;
1232  }  }
1233    
1234  /* Display ELF-specific fields of a symbol.  */  /* Display ELF-specific fields of a symbol.  */
1235    
1236  void  void
1237  bfd_elf_print_symbol (abfd, filep, symbol, how)  bfd_elf_print_symbol (bfd *abfd,
1238       bfd *abfd;                        void *filep,
1239       PTR filep;                        asymbol *symbol,
1240       asymbol *symbol;                        bfd_print_symbol_type how)
      bfd_print_symbol_type how;  
1241  {  {
1242    FILE *file = (FILE *) filep;    FILE *file = filep;
1243    switch (how)    switch (how)
1244      {      {
1245      case bfd_print_symbol_name:      case bfd_print_symbol_name:
# Line 1266  bfd_elf_print_symbol (abfd, filep, symbo Line 1254  bfd_elf_print_symbol (abfd, filep, symbo
1254        {        {
1255          const char *section_name;          const char *section_name;
1256          const char *name = NULL;          const char *name = NULL;
1257          struct elf_backend_data *bed;          const struct elf_backend_data *bed;
1258          unsigned char st_other;          unsigned char st_other;
1259          bfd_vma val;          bfd_vma val;
1260    
# Line 1279  bfd_elf_print_symbol (abfd, filep, symbo Line 1267  bfd_elf_print_symbol (abfd, filep, symbo
1267          if (name == NULL)          if (name == NULL)
1268            {            {
1269              name = symbol->name;              name = symbol->name;
1270              bfd_print_symbol_vandf (abfd, (PTR) file, symbol);              bfd_print_symbol_vandf (abfd, file, symbol);
1271            }            }
1272    
1273          fprintf (file, " %s\t", section_name);          fprintf (file, " %s\t", section_name);
# Line 1368  bfd_elf_print_symbol (abfd, filep, symbo Line 1356  bfd_elf_print_symbol (abfd, filep, symbo
1356  /* Create an entry in an ELF linker hash table.  */  /* Create an entry in an ELF linker hash table.  */
1357    
1358  struct bfd_hash_entry *  struct bfd_hash_entry *
1359  _bfd_elf_link_hash_newfunc (entry, table, string)  _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1360       struct bfd_hash_entry *entry;                              struct bfd_hash_table *table,
1361       struct bfd_hash_table *table;                              const char *string)
      const char *string;  
1362  {  {
1363    /* Allocate the structure if it has not already been allocated by a    /* Allocate the structure if it has not already been allocated by a
1364       subclass.  */       subclass.  */
# Line 1391  _bfd_elf_link_hash_newfunc (entry, table Line 1378  _bfd_elf_link_hash_newfunc (entry, table
1378    
1379        /* Set local fields.  */        /* Set local fields.  */
1380        ret->indx = -1;        ret->indx = -1;
       ret->size = 0;  
1381        ret->dynindx = -1;        ret->dynindx = -1;
1382        ret->dynstr_index = 0;        ret->got = ret->plt = htab->init_refcount;
1383        ret->weakdef = NULL;        memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
1384        ret->got.refcount = htab->init_refcount;                                - offsetof (struct elf_link_hash_entry, size)));
       ret->plt.refcount = htab->init_refcount;  
       ret->linker_section_pointer = NULL;  
       ret->verinfo.verdef = NULL;  
       ret->vtable_entries_used = NULL;  
       ret->vtable_entries_size = 0;  
       ret->vtable_parent = NULL;  
       ret->type = STT_NOTYPE;  
       ret->other = 0;  
1385        /* Assume that we have been called by a non-ELF symbol reader.        /* Assume that we have been called by a non-ELF symbol reader.
1386           This flag is then reset by the code which reads an ELF input           This flag is then reset by the code which reads an ELF input
1387           file.  This ensures that a symbol created by a non-ELF symbol           file.  This ensures that a symbol created by a non-ELF symbol
1388           reader will have the flag set correctly.  */           reader will have the flag set correctly.  */
1389        ret->elf_link_hash_flags = ELF_LINK_NON_ELF;        ret->non_elf = 1;
1390      }      }
1391    
1392    return entry;    return entry;
# Line 1418  _bfd_elf_link_hash_newfunc (entry, table Line 1396  _bfd_elf_link_hash_newfunc (entry, table
1396     old indirect symbol.  Also used for copying flags to a weakdef.  */     old indirect symbol.  Also used for copying flags to a weakdef.  */
1397    
1398  void  void
1399  _bfd_elf_link_hash_copy_indirect (dir, ind)  _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1400       struct elf_link_hash_entry *dir, *ind;                                    struct elf_link_hash_entry *dir,
1401                                      struct elf_link_hash_entry *ind)
1402  {  {
1403    bfd_signed_vma tmp;    bfd_signed_vma tmp;
1404      bfd_signed_vma lowest_valid = bed->can_refcount;
1405    
1406    /* Copy down any references that we may have already seen to the    /* Copy down any references that we may have already seen to the
1407       symbol which just became indirect.  */       symbol which just became indirect.  */
1408    
1409    dir->elf_link_hash_flags |=    dir->ref_dynamic |= ind->ref_dynamic;
1410      (ind->elf_link_hash_flags    dir->ref_regular |= ind->ref_regular;
1411       & (ELF_LINK_HASH_REF_DYNAMIC    dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1412          | ELF_LINK_HASH_REF_REGULAR    dir->non_got_ref |= ind->non_got_ref;
1413          | ELF_LINK_HASH_REF_REGULAR_NONWEAK    dir->needs_plt |= ind->needs_plt;
1414          | ELF_LINK_NON_GOT_REF));    dir->pointer_equality_needed |= ind->pointer_equality_needed;
1415    
1416    if (ind->root.type != bfd_link_hash_indirect)    if (ind->root.type != bfd_link_hash_indirect)
1417      return;      return;
# Line 1439  _bfd_elf_link_hash_copy_indirect (dir, i Line 1419  _bfd_elf_link_hash_copy_indirect (dir, i
1419    /* Copy over the global and procedure linkage table refcount entries.    /* Copy over the global and procedure linkage table refcount entries.
1420       These may have been already set up by a check_relocs routine.  */       These may have been already set up by a check_relocs routine.  */
1421    tmp = dir->got.refcount;    tmp = dir->got.refcount;
1422    if (tmp <= 0)    if (tmp < lowest_valid)
1423      {      {
1424        dir->got.refcount = ind->got.refcount;        dir->got.refcount = ind->got.refcount;
1425        ind->got.refcount = tmp;        ind->got.refcount = tmp;
1426      }      }
1427    else    else
1428      BFD_ASSERT (ind->got.refcount <= 0);      BFD_ASSERT (ind->got.refcount < lowest_valid);
1429    
1430    tmp = dir->plt.refcount;    tmp = dir->plt.refcount;
1431    if (tmp <= 0)    if (tmp < lowest_valid)
1432      {      {
1433        dir->plt.refcount = ind->plt.refcount;        dir->plt.refcount = ind->plt.refcount;
1434        ind->plt.refcount = tmp;        ind->plt.refcount = tmp;
1435      }      }
1436    else    else
1437      BFD_ASSERT (ind->plt.refcount <= 0);      BFD_ASSERT (ind->plt.refcount < lowest_valid);
1438    
1439    if (dir->dynindx == -1)    if (dir->dynindx == -1)
1440      {      {
# Line 1468  _bfd_elf_link_hash_copy_indirect (dir, i Line 1448  _bfd_elf_link_hash_copy_indirect (dir, i
1448  }  }
1449    
1450  void  void
1451  _bfd_elf_link_hash_hide_symbol (info, h, force_local)  _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1452       struct bfd_link_info *info;                                  struct elf_link_hash_entry *h,
1453       struct elf_link_hash_entry *h;                                  bfd_boolean force_local)
      boolean force_local;  
1454  {  {
1455    h->plt.offset = (bfd_vma) -1;    h->plt = elf_hash_table (info)->init_offset;
1456    h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;    h->needs_plt = 0;
1457    if (force_local)    if (force_local)
1458      {      {
1459        h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;        h->forced_local = 1;
1460        if (h->dynindx != -1)        if (h->dynindx != -1)
1461          {          {
1462            h->dynindx = -1;            h->dynindx = -1;
# Line 1489  _bfd_elf_link_hash_hide_symbol (info, h, Line 1468  _bfd_elf_link_hash_hide_symbol (info, h,
1468    
1469  /* Initialize an ELF linker hash table.  */  /* Initialize an ELF linker hash table.  */
1470    
1471  boolean  bfd_boolean
1472  _bfd_elf_link_hash_table_init (table, abfd, newfunc)  _bfd_elf_link_hash_table_init
1473       struct elf_link_hash_table *table;    (struct elf_link_hash_table *table,
1474       bfd *abfd;     bfd *abfd,
1475       struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,     struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1476                                                  struct bfd_hash_table *,                                        struct bfd_hash_table *,
1477                                                  const char *));                                        const char *))
1478  {  {
1479    boolean ret;    bfd_boolean ret;
1480    
1481    table->dynamic_sections_created = false;    table->dynamic_sections_created = FALSE;
1482    table->dynobj = NULL;    table->dynobj = NULL;
1483    table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;    /* Make sure can_refcount is extended to the width and signedness of
1484         init_refcount before we subtract one from it.  */
1485      table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1486      table->init_refcount.refcount -= 1;
1487      table->init_offset.offset = -(bfd_vma) 1;
1488    /* The first dynamic symbol is a dummy.  */    /* The first dynamic symbol is a dummy.  */
1489    table->dynsymcount = 1;    table->dynsymcount = 1;
1490    table->dynstr = NULL;    table->dynstr = NULL;
1491    table->bucketcount = 0;    table->bucketcount = 0;
1492    table->needed = NULL;    table->needed = NULL;
   table->runpath = NULL;  
   table->loaded = NULL;  
1493    table->hgot = NULL;    table->hgot = NULL;
   table->stab_info = NULL;  
1494    table->merge_info = NULL;    table->merge_info = NULL;
1495      memset (&table->stab_info, 0, sizeof (table->stab_info));
1496      memset (&table->eh_info, 0, sizeof (table->eh_info));
1497    table->dynlocal = NULL;    table->dynlocal = NULL;
1498    ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);    table->runpath = NULL;
1499      table->tls_sec = NULL;
1500      table->tls_size = 0;
1501      table->loaded = NULL;
1502      table->is_relocatable_executable = FALSE;
1503    
1504      ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1505    table->root.type = bfd_link_elf_hash_table;    table->root.type = bfd_link_elf_hash_table;
1506    
1507    return ret;    return ret;
# Line 1522  _bfd_elf_link_hash_table_init (table, ab Line 1510  _bfd_elf_link_hash_table_init (table, ab
1510  /* Create an ELF linker hash table.  */  /* Create an ELF linker hash table.  */
1511    
1512  struct bfd_link_hash_table *  struct bfd_link_hash_table *
1513  _bfd_elf_link_hash_table_create (abfd)  _bfd_elf_link_hash_table_create (bfd *abfd)
      bfd *abfd;  
1514  {  {
1515    struct elf_link_hash_table *ret;    struct elf_link_hash_table *ret;
1516    bfd_size_type amt = sizeof (struct elf_link_hash_table);    bfd_size_type amt = sizeof (struct elf_link_hash_table);
1517    
1518    ret = (struct elf_link_hash_table *) bfd_malloc (amt);    ret = bfd_malloc (amt);
1519    if (ret == (struct elf_link_hash_table *) NULL)    if (ret == NULL)
1520      return NULL;      return NULL;
1521    
1522    if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))    if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
# Line 1543  _bfd_elf_link_hash_table_create (abfd) Line 1530  _bfd_elf_link_hash_table_create (abfd)
1530    
1531  /* This is a hook for the ELF emulation code in the generic linker to  /* This is a hook for the ELF emulation code in the generic linker to
1532     tell the backend linker what file name to use for the DT_NEEDED     tell the backend linker what file name to use for the DT_NEEDED
1533     entry for a dynamic object.  The generic linker passes name as an     entry for a dynamic object.  */
    empty string to indicate that no DT_NEEDED entry should be made.  */  
1534    
1535  void  void
1536  bfd_elf_set_dt_needed_name (abfd, name)  bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
      bfd *abfd;  
      const char *name;  
1537  {  {
1538    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1539        && bfd_get_format (abfd) == bfd_object)        && bfd_get_format (abfd) == bfd_object)
1540      elf_dt_name (abfd) = name;      elf_dt_name (abfd) = name;
1541  }  }
1542    
1543    int
1544    bfd_elf_get_dyn_lib_class (bfd *abfd)
1545    {
1546      int lib_class;
1547      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1548          && bfd_get_format (abfd) == bfd_object)
1549        lib_class = elf_dyn_lib_class (abfd);
1550      else
1551        lib_class = 0;
1552      return lib_class;
1553    }
1554    
1555  void  void
1556  bfd_elf_set_dt_needed_soname (abfd, name)  bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
      bfd *abfd;  
      const char *name;  
1557  {  {
1558    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1559        && bfd_get_format (abfd) == bfd_object)        && bfd_get_format (abfd) == bfd_object)
1560      elf_dt_soname (abfd) = name;      elf_dyn_lib_class (abfd) = lib_class;
1561  }  }
1562    
1563  /* Get the list of DT_NEEDED entries for a link.  This is a hook for  /* Get the list of DT_NEEDED entries for a link.  This is a hook for
1564     the linker ELF emulation code.  */     the linker ELF emulation code.  */
1565    
1566  struct bfd_link_needed_list *  struct bfd_link_needed_list *
1567  bfd_elf_get_needed_list (abfd, info)  bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1568       bfd *abfd ATTRIBUTE_UNUSED;                           struct bfd_link_info *info)
      struct bfd_link_info *info;  
1569  {  {
1570    if (info->hash->creator->flavour != bfd_target_elf_flavour)    if (! is_elf_hash_table (info->hash))
1571      return NULL;      return NULL;
1572    return elf_hash_table (info)->needed;    return elf_hash_table (info)->needed;
1573  }  }
# Line 1583  bfd_elf_get_needed_list (abfd, info) Line 1576  bfd_elf_get_needed_list (abfd, info)
1576     hook for the linker ELF emulation code.  */     hook for the linker ELF emulation code.  */
1577    
1578  struct bfd_link_needed_list *  struct bfd_link_needed_list *
1579  bfd_elf_get_runpath_list (abfd, info)  bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1580       bfd *abfd ATTRIBUTE_UNUSED;                            struct bfd_link_info *info)
      struct bfd_link_info *info;  
1581  {  {
1582    if (info->hash->creator->flavour != bfd_target_elf_flavour)    if (! is_elf_hash_table (info->hash))
1583      return NULL;      return NULL;
1584    return elf_hash_table (info)->runpath;    return elf_hash_table (info)->runpath;
1585  }  }
# Line 1597  bfd_elf_get_runpath_list (abfd, info) Line 1589  bfd_elf_get_runpath_list (abfd, info)
1589     passed to bfd_elf_set_dt_needed_name, or it is the filename.  */     passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
1590    
1591  const char *  const char *
1592  bfd_elf_get_dt_soname (abfd)  bfd_elf_get_dt_soname (bfd *abfd)
      bfd *abfd;  
1593  {  {
1594    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1595        && bfd_get_format (abfd) == bfd_object)        && bfd_get_format (abfd) == bfd_object)
# Line 1609  bfd_elf_get_dt_soname (abfd) Line 1600  bfd_elf_get_dt_soname (abfd)
1600  /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for  /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
1601     the ELF linker emulation code.  */     the ELF linker emulation code.  */
1602    
1603  boolean  bfd_boolean
1604  bfd_elf_get_bfd_needed_list (abfd, pneeded)  bfd_elf_get_bfd_needed_list (bfd *abfd,
1605       bfd *abfd;                               struct bfd_link_needed_list **pneeded)
      struct bfd_link_needed_list **pneeded;  
1606  {  {
1607    asection *s;    asection *s;
1608    bfd_byte *dynbuf = NULL;    bfd_byte *dynbuf = NULL;
# Line 1620  bfd_elf_get_bfd_needed_list (abfd, pneed Line 1610  bfd_elf_get_bfd_needed_list (abfd, pneed
1610    unsigned long shlink;    unsigned long shlink;
1611    bfd_byte *extdyn, *extdynend;    bfd_byte *extdyn, *extdynend;
1612    size_t extdynsize;    size_t extdynsize;
1613    void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));    void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1614    
1615    *pneeded = NULL;    *pneeded = NULL;
1616    
1617    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour    if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1618        || bfd_get_format (abfd) != bfd_object)        || bfd_get_format (abfd) != bfd_object)
1619      return true;      return TRUE;
1620    
1621    s = bfd_get_section_by_name (abfd, ".dynamic");    s = bfd_get_section_by_name (abfd, ".dynamic");
1622    if (s == NULL || s->_raw_size == 0)    if (s == NULL || s->size == 0)
1623      return true;      return TRUE;
1624    
1625    dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);    if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   if (dynbuf == NULL)  
     goto error_return;  
   
   if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,  
                                   s->_raw_size))  
1626      goto error_return;      goto error_return;
1627    
1628    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);    elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
# Line 1650  bfd_elf_get_bfd_needed_list (abfd, pneed Line 1635  bfd_elf_get_bfd_needed_list (abfd, pneed
1635    swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;    swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1636    
1637    extdyn = dynbuf;    extdyn = dynbuf;
1638    extdynend = extdyn + s->_raw_size;    extdynend = extdyn + s->size;
1639    for (; extdyn < extdynend; extdyn += extdynsize)    for (; extdyn < extdynend; extdyn += extdynsize)
1640      {      {
1641        Elf_Internal_Dyn dyn;        Elf_Internal_Dyn dyn;
1642    
1643        (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);        (*swap_dyn_in) (abfd, extdyn, &dyn);
1644    
1645        if (dyn.d_tag == DT_NULL)        if (dyn.d_tag == DT_NULL)
1646          break;          break;
# Line 1672  bfd_elf_get_bfd_needed_list (abfd, pneed Line 1657  bfd_elf_get_bfd_needed_list (abfd, pneed
1657              goto error_return;              goto error_return;
1658    
1659            amt = sizeof *l;            amt = sizeof *l;
1660            l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);            l = bfd_alloc (abfd, amt);
1661            if (l == NULL)            if (l == NULL)
1662              goto error_return;              goto error_return;
1663    
# Line 1685  bfd_elf_get_bfd_needed_list (abfd, pneed Line 1670  bfd_elf_get_bfd_needed_list (abfd, pneed
1670    
1671    free (dynbuf);    free (dynbuf);
1672    
1673    return true;    return TRUE;
1674    
1675   error_return:   error_return:
1676    if (dynbuf != NULL)    if (dynbuf != NULL)
1677      free (dynbuf);      free (dynbuf);
1678    return false;    return FALSE;
1679  }  }
1680    
1681  /* Allocate an ELF string table--force the first byte to be zero.  */  /* Allocate an ELF string table--force the first byte to be zero.  */
1682    
1683  struct bfd_strtab_hash *  struct bfd_strtab_hash *
1684  _bfd_elf_stringtab_init ()  _bfd_elf_stringtab_init (void)
1685  {  {
1686    struct bfd_strtab_hash *ret;    struct bfd_strtab_hash *ret;
1687    
# Line 1705  _bfd_elf_stringtab_init () Line 1690  _bfd_elf_stringtab_init ()
1690      {      {
1691        bfd_size_type loc;        bfd_size_type loc;
1692    
1693        loc = _bfd_stringtab_add (ret, "", true, false);        loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1694        BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);        BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1695        if (loc == (bfd_size_type) -1)        if (loc == (bfd_size_type) -1)
1696          {          {
# Line 1720  _bfd_elf_stringtab_init () Line 1705  _bfd_elf_stringtab_init ()
1705    
1706  /* Create a new bfd section from an ELF section header.  */  /* Create a new bfd section from an ELF section header.  */
1707    
1708  boolean  bfd_boolean
1709  bfd_section_from_shdr (abfd, shindex)  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
      bfd *abfd;  
      unsigned int shindex;  
1710  {  {
1711    Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];    Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1712    Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);    Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1713    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1714    const char *name;    const char *name;
1715    
1716    name = elf_string_from_elf_strtab (abfd, hdr->sh_name);    name = bfd_elf_string_from_elf_section (abfd,
1717                                              elf_elfheader (abfd)->e_shstrndx,
1718                                              hdr->sh_name);
1719    
1720    switch (hdr->sh_type)    switch (hdr->sh_type)
1721      {      {
1722      case SHT_NULL:      case SHT_NULL:
1723        /* Inactive section. Throw it away.  */        /* Inactive section. Throw it away.  */
1724        return true;        return TRUE;
1725    
1726      case SHT_PROGBITS:  /* Normal section with contents.  */      case SHT_PROGBITS:  /* Normal section with contents.  */
1727      case SHT_NOBITS:    /* .bss section.  */      case SHT_NOBITS:    /* .bss section.  */
# Line 1745  bfd_section_from_shdr (abfd, shindex) Line 1730  bfd_section_from_shdr (abfd, shindex)
1730      case SHT_INIT_ARRAY:        /* .init_array section.  */      case SHT_INIT_ARRAY:        /* .init_array section.  */
1731      case SHT_FINI_ARRAY:        /* .fini_array section.  */      case SHT_FINI_ARRAY:        /* .fini_array section.  */
1732      case SHT_PREINIT_ARRAY:     /* .preinit_array section.  */      case SHT_PREINIT_ARRAY:     /* .preinit_array section.  */
1733        case SHT_GNU_LIBLIST:       /* .gnu.liblist section.  */
1734        return _bfd_elf_make_section_from_shdr (abfd, hdr, name);        return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1735    
1736      case SHT_DYNAMIC:   /* Dynamic linking information.  */      case SHT_DYNAMIC:   /* Dynamic linking information.  */
1737        if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))        if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1738          return false;          return FALSE;
1739          if (hdr->sh_link > elf_numsections (abfd)
1740              || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1741            return FALSE;
1742        if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)        if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1743          {          {
1744            Elf_Internal_Shdr *dynsymhdr;            Elf_Internal_Shdr *dynsymhdr;
# Line 1782  bfd_section_from_shdr (abfd, shindex) Line 1771  bfd_section_from_shdr (abfd, shindex)
1771    
1772      case SHT_SYMTAB:            /* A symbol table */      case SHT_SYMTAB:            /* A symbol table */
1773        if (elf_onesymtab (abfd) == shindex)        if (elf_onesymtab (abfd) == shindex)
1774          return true;          return TRUE;
1775    
1776        BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);        BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1777        BFD_ASSERT (elf_onesymtab (abfd) == 0);        BFD_ASSERT (elf_onesymtab (abfd) == 0);
# Line 1795  bfd_section_from_shdr (abfd, shindex) Line 1784  bfd_section_from_shdr (abfd, shindex)
1784           SHF_ALLOC is set, and this is a shared object, then we also           SHF_ALLOC is set, and this is a shared object, then we also
1785           treat this section as a BFD section.  We can not base the           treat this section as a BFD section.  We can not base the
1786           decision purely on SHF_ALLOC, because that flag is sometimes           decision purely on SHF_ALLOC, because that flag is sometimes
1787           set in a relocateable object file, which would confuse the           set in a relocatable object file, which would confuse the
1788           linker.  */           linker.  */
1789        if ((hdr->sh_flags & SHF_ALLOC) != 0        if ((hdr->sh_flags & SHF_ALLOC) != 0
1790            && (abfd->flags & DYNAMIC) != 0            && (abfd->flags & DYNAMIC) != 0
1791            && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))            && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1792          return false;          return FALSE;
1793    
1794        return true;        /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1795             can't read symbols without that section loaded as well.  It
1796             is most likely specified by the next section header.  */
1797          if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1798            {
1799              unsigned int i, num_sec;
1800    
1801              num_sec = elf_numsections (abfd);
1802              for (i = shindex + 1; i < num_sec; i++)
1803                {
1804                  Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1805                  if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1806                      && hdr2->sh_link == shindex)
1807                    break;
1808                }
1809              if (i == num_sec)
1810                for (i = 1; i < shindex; i++)
1811                  {
1812                    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1813                    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1814                        && hdr2->sh_link == shindex)
1815                      break;
1816                  }
1817              if (i != shindex)
1818                return bfd_section_from_shdr (abfd, i);
1819            }
1820          return TRUE;
1821    
1822      case SHT_DYNSYM:            /* A dynamic symbol table */      case SHT_DYNSYM:            /* A dynamic symbol table */
1823        if (elf_dynsymtab (abfd) == shindex)        if (elf_dynsymtab (abfd) == shindex)
1824          return true;          return TRUE;
1825    
1826        BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);        BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1827        BFD_ASSERT (elf_dynsymtab (abfd) == 0);        BFD_ASSERT (elf_dynsymtab (abfd) == 0);
# Line 1821  bfd_section_from_shdr (abfd, shindex) Line 1836  bfd_section_from_shdr (abfd, shindex)
1836    
1837      case SHT_SYMTAB_SHNDX:      /* Symbol section indices when >64k sections */      case SHT_SYMTAB_SHNDX:      /* Symbol section indices when >64k sections */
1838        if (elf_symtab_shndx (abfd) == shindex)        if (elf_symtab_shndx (abfd) == shindex)
1839          return true;          return TRUE;
   
       /* Get the associated symbol table.  */  
       if (! bfd_section_from_shdr (abfd, hdr->sh_link)  
           || hdr->sh_link != elf_onesymtab (abfd))  
         return false;  
1840    
1841          BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1842        elf_symtab_shndx (abfd) = shindex;        elf_symtab_shndx (abfd) = shindex;
1843        elf_tdata (abfd)->symtab_shndx_hdr = *hdr;        elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1844        elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;        elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1845        return true;        return TRUE;
1846    
1847      case SHT_STRTAB:            /* A string table */      case SHT_STRTAB:            /* A string table */
1848        if (hdr->bfd_section != NULL)        if (hdr->bfd_section != NULL)
1849          return true;          return TRUE;
1850        if (ehdr->e_shstrndx == shindex)        if (ehdr->e_shstrndx == shindex)
1851          {          {
1852            elf_tdata (abfd)->shstrtab_hdr = *hdr;            elf_tdata (abfd)->shstrtab_hdr = *hdr;
1853            elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;            elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1854            return true;            return TRUE;
1855            }
1856          if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1857            {
1858            symtab_strtab:
1859              elf_tdata (abfd)->strtab_hdr = *hdr;
1860              elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1861              return TRUE;
1862            }
1863          if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1864            {
1865            dynsymtab_strtab:
1866              elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1867              hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1868              elf_elfsections (abfd)[shindex] = hdr;
1869              /* We also treat this as a regular section, so that objcopy
1870                 can handle it.  */
1871              return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1872          }          }
       {  
         unsigned int i, num_sec;  
1873    
1874          num_sec = elf_numsections (abfd);        /* If the string table isn't one of the above, then treat it as a
1875          for (i = 1; i < num_sec; i++)           regular section.  We need to scan all the headers to be sure,
1876            {           just in case this strtab section appeared before the above.  */
1877              Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];        if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1878              if (hdr2->sh_link == shindex)          {
1879                {            unsigned int i, num_sec;
                 if (! bfd_section_from_shdr (abfd, i))  
                   return false;  
                 if (elf_onesymtab (abfd) == i)  
                   {  
                     elf_tdata (abfd)->strtab_hdr = *hdr;  
                     elf_elfsections (abfd)[shindex] =  
                       &elf_tdata (abfd)->strtab_hdr;  
                     return true;  
                   }  
                 if (elf_dynsymtab (abfd) == i)  
                   {  
                     elf_tdata (abfd)->dynstrtab_hdr = *hdr;  
                     elf_elfsections (abfd)[shindex] = hdr =  
                       &elf_tdata (abfd)->dynstrtab_hdr;  
                     /* We also treat this as a regular section, so  
                        that objcopy can handle it.  */  
                     break;  
                   }  
 #if 0 /* Not handling other string tables specially right now.  */  
                 hdr2 = elf_elfsections (abfd)[i];       /* in case it moved */  
                 /* We have a strtab for some random other section.  */  
                 newsect = (asection *) hdr2->bfd_section;  
                 if (!newsect)  
                   break;  
                 hdr->bfd_section = newsect;  
                 hdr2 = &elf_section_data (newsect)->str_hdr;  
                 *hdr2 = *hdr;  
                 elf_elfsections (abfd)[shindex] = hdr2;  
 #endif  
               }  
           }  
       }  
1880    
1881              num_sec = elf_numsections (abfd);
1882              for (i = 1; i < num_sec; i++)
1883                {
1884                  Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1885                  if (hdr2->sh_link == shindex)
1886                    {
1887                      if (! bfd_section_from_shdr (abfd, i))
1888                        return FALSE;
1889                      if (elf_onesymtab (abfd) == i)
1890                        goto symtab_strtab;
1891                      if (elf_dynsymtab (abfd) == i)
1892                        goto dynsymtab_strtab;
1893                    }
1894                }
1895            }
1896        return _bfd_elf_make_section_from_shdr (abfd, hdr, name);        return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1897    
1898      case SHT_REL:      case SHT_REL:
# Line 1899  bfd_section_from_shdr (abfd, shindex) Line 1908  bfd_section_from_shdr (abfd, shindex)
1908              || hdr->sh_link >= num_sec)              || hdr->sh_link >= num_sec)
1909            {            {
1910              ((*_bfd_error_handler)              ((*_bfd_error_handler)
1911               (_("%s: invalid link %lu for reloc section %s (index %u)"),               (_("%B: invalid link %lu for reloc section %s (index %u)"),
1912                bfd_archive_filename (abfd), hdr->sh_link, name, shindex));                abfd, hdr->sh_link, name, shindex));
1913              return _bfd_elf_make_section_from_shdr (abfd, hdr, name);              return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1914            }            }
1915    
# Line 1936  bfd_section_from_shdr (abfd, shindex) Line 1945  bfd_section_from_shdr (abfd, shindex)
1945            }            }
1946    
1947          /* Get the symbol table.  */          /* Get the symbol table.  */
1948          if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB          if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1949                 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1950              && ! bfd_section_from_shdr (abfd, hdr->sh_link))              && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1951            return false;            return FALSE;
1952    
1953          /* If this reloc section does not use the main symbol table we          /* If this reloc section does not use the main symbol table we
1954             don't treat it as a reloc section.  BFD can't adequately             don't treat it as a reloc section.  BFD can't adequately
# Line 1950  bfd_section_from_shdr (abfd, shindex) Line 1960  bfd_section_from_shdr (abfd, shindex)
1960            return _bfd_elf_make_section_from_shdr (abfd, hdr, name);            return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1961    
1962          if (! bfd_section_from_shdr (abfd, hdr->sh_info))          if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1963            return false;            return FALSE;
1964          target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);          target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1965          if (target_sect == NULL)          if (target_sect == NULL)
1966            return false;            return FALSE;
1967    
1968          if ((target_sect->flags & SEC_RELOC) == 0          if ((target_sect->flags & SEC_RELOC) == 0
1969              || target_sect->reloc_count == 0)              || target_sect->reloc_count == 0)
# Line 1963  bfd_section_from_shdr (abfd, shindex) Line 1973  bfd_section_from_shdr (abfd, shindex)
1973              bfd_size_type amt;              bfd_size_type amt;
1974              BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);              BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1975              amt = sizeof (*hdr2);              amt = sizeof (*hdr2);
1976              hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);              hdr2 = bfd_alloc (abfd, amt);
1977              elf_section_data (target_sect)->rel_hdr2 = hdr2;              elf_section_data (target_sect)->rel_hdr2 = hdr2;
1978            }            }
1979          *hdr2 = *hdr;          *hdr2 = *hdr;
# Line 1975  bfd_section_from_shdr (abfd, shindex) Line 1985  bfd_section_from_shdr (abfd, shindex)
1985          /* In the section to which the relocations apply, mark whether          /* In the section to which the relocations apply, mark whether
1986             its relocations are of the REL or RELA variety.  */             its relocations are of the REL or RELA variety.  */
1987          if (hdr->sh_size != 0)          if (hdr->sh_size != 0)
1988            elf_section_data (target_sect)->use_rela_p            target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
             = (hdr->sh_type == SHT_RELA);  
1989          abfd->flags |= HAS_RELOC;          abfd->flags |= HAS_RELOC;
1990          return true;          return TRUE;
1991        }        }
1992        break;        break;
1993    
# Line 2001  bfd_section_from_shdr (abfd, shindex) Line 2010  bfd_section_from_shdr (abfd, shindex)
2010        break;        break;
2011    
2012      case SHT_SHLIB:      case SHT_SHLIB:
2013        return true;        return TRUE;
2014    
2015      case SHT_GROUP:      case SHT_GROUP:
2016        /* We need a BFD section for objcopy and relocatable linking,        /* We need a BFD section for objcopy and relocatable linking,
# Line 2009  bfd_section_from_shdr (abfd, shindex) Line 2018  bfd_section_from_shdr (abfd, shindex)
2018           name.  */           name.  */
2019        name = group_signature (abfd, hdr);        name = group_signature (abfd, hdr);
2020        if (name == NULL)        if (name == NULL)
2021          return false;          return FALSE;
2022        if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))        if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2023          return false;          return FALSE;
2024        if (hdr->contents != NULL)        if (hdr->contents != NULL)
2025          {          {
2026            Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;            Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
# Line 2022  bfd_section_from_shdr (abfd, shindex) Line 2031  bfd_section_from_shdr (abfd, shindex)
2031              hdr->bfd_section->flags              hdr->bfd_section->flags
2032                |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;                |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2033    
2034              /* We try to keep the same section order as it comes in.  */
2035              idx += n_elt;
2036            while (--n_elt != 0)            while (--n_elt != 0)
2037              if ((s = (++idx)->shdr->bfd_section) != NULL              if ((s = (--idx)->shdr->bfd_section) != NULL
2038                  && elf_next_in_group (s) != NULL)                  && elf_next_in_group (s) != NULL)
2039                {                {
2040                  elf_next_in_group (hdr->bfd_section) = s;                  elf_next_in_group (hdr->bfd_section) = s;
# Line 2034  bfd_section_from_shdr (abfd, shindex) Line 2045  bfd_section_from_shdr (abfd, shindex)
2045    
2046      default:      default:
2047        /* Check for any processor-specific section types.  */        /* Check for any processor-specific section types.  */
2048        {        return bed->elf_backend_section_from_shdr (abfd, hdr, name);
         if (bed->elf_backend_section_from_shdr)  
           (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);  
       }  
       break;  
2049      }      }
2050    
2051    return true;    return TRUE;
2052  }  }
2053    
2054  /* Return the section for the local symbol specified by ABFD, R_SYMNDX.  /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2055     Return SEC for sections that have no elf section, and NULL on error.  */     Return SEC for sections that have no elf section, and NULL on error.  */
2056    
2057  asection *  asection *
2058  bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)  bfd_section_from_r_symndx (bfd *abfd,
2059       bfd *abfd;                             struct sym_sec_cache *cache,
2060       struct sym_sec_cache *cache;                             asection *sec,
2061       asection *sec;                             unsigned long r_symndx)
      unsigned long r_symndx;  
2062  {  {
2063    Elf_Internal_Shdr *symtab_hdr;    Elf_Internal_Shdr *symtab_hdr;
2064    unsigned char esym[sizeof (Elf64_External_Sym)];    unsigned char esym[sizeof (Elf64_External_Sym)];
# Line 2075  bfd_section_from_r_symndx (abfd, cache, Line 2081  bfd_section_from_r_symndx (abfd, cache,
2081      }      }
2082    cache->indx[ent] = r_symndx;    cache->indx[ent] = r_symndx;
2083    cache->sec[ent] = sec;    cache->sec[ent] = sec;
2084    if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)    if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2085          || isym.st_shndx > SHN_HIRESERVE)
2086      {      {
2087        asection *s;        asection *s;
2088        s = bfd_section_from_elf_index (abfd, isym.st_shndx);        s = bfd_section_from_elf_index (abfd, isym.st_shndx);
# Line 2089  bfd_section_from_r_symndx (abfd, cache, Line 2096  bfd_section_from_r_symndx (abfd, cache,
2096     section.  */     section.  */
2097    
2098  asection *  asection *
2099  bfd_section_from_elf_index (abfd, index)  bfd_section_from_elf_index (bfd *abfd, unsigned int index)
      bfd *abfd;  
      unsigned int index;  
2100  {  {
2101    if (index >= elf_numsections (abfd))    if (index >= elf_numsections (abfd))
2102      return NULL;      return NULL;
2103    return elf_elfsections (abfd)[index]->bfd_section;    return elf_elfsections (abfd)[index]->bfd_section;
2104  }  }
2105    
2106  boolean  static struct bfd_elf_special_section const special_sections[] =
2107  _bfd_elf_new_section_hook (abfd, sec)  {
2108       bfd *abfd;    { ".bss",            4, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2109       asection *sec;    { ".gnu.linkonce.b",15, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2110      { ".comment",        8,  0, SHT_PROGBITS, 0 },
2111      { ".data",           5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2112      { ".data1",          6,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2113      { ".debug",          6,  0, SHT_PROGBITS, 0 },
2114      { ".fini",           5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2115      { ".init",           5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2116      { ".line",           5,  0, SHT_PROGBITS, 0 },
2117      { ".rodata",         7, -2, SHT_PROGBITS, SHF_ALLOC },
2118      { ".rodata1",        8,  0, SHT_PROGBITS, SHF_ALLOC },
2119      { ".tbss",           5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2120      { ".tdata",          6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2121      { ".text",           5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2122      { ".init_array",    11,  0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2123      { ".fini_array",    11,  0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2124      { ".preinit_array", 14,  0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2125      { ".debug_line",    11,  0, SHT_PROGBITS, 0 },
2126      { ".debug_info",    11,  0, SHT_PROGBITS, 0 },
2127      { ".debug_abbrev",  13,  0, SHT_PROGBITS, 0 },
2128      { ".debug_aranges", 14,  0, SHT_PROGBITS, 0 },
2129      { ".dynamic",        8,  0, SHT_DYNAMIC,  SHF_ALLOC },
2130      { ".dynstr",         7,  0, SHT_STRTAB,   SHF_ALLOC },
2131      { ".dynsym",         7,  0, SHT_DYNSYM,   SHF_ALLOC },
2132      { ".got",            4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2133      { ".hash",           5,  0, SHT_HASH,     SHF_ALLOC },
2134      { ".interp",         7,  0, SHT_PROGBITS, 0 },
2135      { ".plt",            4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2136      { ".shstrtab",       9,  0, SHT_STRTAB,   0 },
2137      { ".strtab",         7,  0, SHT_STRTAB,   0 },
2138      { ".symtab",         7,  0, SHT_SYMTAB,   0 },
2139      { ".gnu.version",   12,  0, SHT_GNU_versym, 0 },
2140      { ".gnu.version_d", 14,  0, SHT_GNU_verdef, 0 },
2141      { ".gnu.version_r", 14,  0, SHT_GNU_verneed, 0 },
2142      { ".note.GNU-stack",15,  0, SHT_PROGBITS, 0 },
2143      { ".note",           5, -1, SHT_NOTE,     0 },
2144      { ".rela",           5, -1, SHT_RELA,     0 },
2145      { ".rel",            4, -1, SHT_REL,      0 },
2146      { ".stabstr",        5,  3, SHT_STRTAB,   0 },
2147      { ".gnu.liblist",   12,  0, SHT_GNU_LIBLIST, SHF_ALLOC },
2148      { ".gnu.conflict",  13,  0, SHT_RELA,     SHF_ALLOC },
2149      { NULL,              0,  0, 0,            0 }
2150    };
2151    
2152    static const struct bfd_elf_special_section *
2153    get_special_section (const char *name,
2154                         const struct bfd_elf_special_section *special_sections,
2155                         unsigned int rela)
2156    {
2157      int i;
2158      int len = strlen (name);
2159    
2160      for (i = 0; special_sections[i].prefix != NULL; i++)
2161        {
2162          int suffix_len;
2163          int prefix_len = special_sections[i].prefix_length;
2164    
2165          if (len < prefix_len)
2166            continue;
2167          if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2168            continue;
2169    
2170          suffix_len = special_sections[i].suffix_length;
2171          if (suffix_len <= 0)
2172            {
2173              if (name[prefix_len] != 0)
2174                {
2175                  if (suffix_len == 0)
2176                    continue;
2177                  if (name[prefix_len] != '.'
2178                      && (suffix_len == -2
2179                          || (rela && special_sections[i].type == SHT_REL)))
2180                    continue;
2181                }
2182            }
2183          else
2184            {
2185              if (len < prefix_len + suffix_len)
2186                continue;
2187              if (memcmp (name + len - suffix_len,
2188                          special_sections[i].prefix + prefix_len,
2189                          suffix_len) != 0)
2190                continue;
2191            }
2192          return &special_sections[i];
2193        }
2194    
2195      return NULL;
2196    }
2197    
2198    const struct bfd_elf_special_section *
2199    _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2200    {
2201      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2202      const struct bfd_elf_special_section *ssect = NULL;
2203    
2204      /* See if this is one of the special sections.  */
2205      if (name)
2206        {
2207          unsigned int rela = bed->default_use_rela_p;
2208    
2209          if (bed->special_sections)
2210            ssect = get_special_section (name, bed->special_sections, rela);
2211    
2212          if (! ssect)
2213            ssect = get_special_section (name, special_sections, rela);
2214        }
2215    
2216      return ssect;
2217    }
2218    
2219    bfd_boolean
2220    _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2221  {  {
2222    struct bfd_elf_section_data *sdata;    struct bfd_elf_section_data *sdata;
2223    bfd_size_type amt = sizeof (*sdata);    const struct bfd_elf_special_section *ssect;
2224    
2225    sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);    sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2226    if (!sdata)    if (sdata == NULL)
2227      return false;      {
2228    sec->used_by_bfd = (PTR) sdata;        sdata = bfd_zalloc (abfd, sizeof (*sdata));
2229          if (sdata == NULL)
2230            return FALSE;
2231          sec->used_by_bfd = sdata;
2232        }
2233    
2234      elf_section_type (sec) = SHT_NULL;
2235      ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2236      if (ssect != NULL)
2237        {
2238          elf_section_type (sec) = ssect->type;
2239          elf_section_flags (sec) = ssect->attr;
2240        }
2241    
2242    /* Indicate whether or not this section should use RELA relocations.  */    /* Indicate whether or not this section should use RELA relocations.  */
2243    sdata->use_rela_p    sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
     = get_elf_backend_data (abfd)->default_use_rela_p;  
2244    
2245    return true;    return TRUE;
2246  }  }
2247    
2248  /* Create a new bfd section from an ELF program header.  /* Create a new bfd section from an ELF program header.
# Line 2140  _bfd_elf_new_section_hook (abfd, sec) Line 2267  _bfd_elf_new_section_hook (abfd, sec)
2267    
2268   */   */
2269    
2270  boolean  bfd_boolean
2271  _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)  _bfd_elf_make_section_from_phdr (bfd *abfd,
2272       bfd *abfd;                                   Elf_Internal_Phdr *hdr,
2273       Elf_Internal_Phdr *hdr;                                   int index,
2274       int index;                                   const char *typename)
      const char *typename;  
2275  {  {
2276    asection *newsect;    asection *newsect;
2277    char *name;    char *name;
# Line 2158  _bfd_elf_make_section_from_phdr (abfd, h Line 2284  _bfd_elf_make_section_from_phdr (abfd, h
2284              && (hdr->p_memsz > hdr->p_filesz));              && (hdr->p_memsz > hdr->p_filesz));
2285    sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");    sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2286    len = strlen (namebuf) + 1;    len = strlen (namebuf) + 1;
2287    name = bfd_alloc (abfd, (bfd_size_type) len);    name = bfd_alloc (abfd, len);
2288    if (!name)    if (!name)
2289      return false;      return FALSE;
2290    memcpy (name, namebuf, len);    memcpy (name, namebuf, len);
2291    newsect = bfd_make_section (abfd, name);    newsect = bfd_make_section (abfd, name);
2292    if (newsect == NULL)    if (newsect == NULL)
2293      return false;      return FALSE;
2294    newsect->vma = hdr->p_vaddr;    newsect->vma = hdr->p_vaddr;
2295    newsect->lma = hdr->p_paddr;    newsect->lma = hdr->p_paddr;
2296    newsect->_raw_size = hdr->p_filesz;    newsect->size = hdr->p_filesz;
2297    newsect->filepos = hdr->p_offset;    newsect->filepos = hdr->p_offset;
2298    newsect->flags |= SEC_HAS_CONTENTS;    newsect->flags |= SEC_HAS_CONTENTS;
2299      newsect->alignment_power = bfd_log2 (hdr->p_align);
2300    if (hdr->p_type == PT_LOAD)    if (hdr->p_type == PT_LOAD)
2301      {      {
2302        newsect->flags |= SEC_ALLOC;        newsect->flags |= SEC_ALLOC;
# Line 2190  _bfd_elf_make_section_from_phdr (abfd, h Line 2317  _bfd_elf_make_section_from_phdr (abfd, h
2317      {      {
2318        sprintf (namebuf, "%s%db", typename, index);        sprintf (namebuf, "%s%db", typename, index);
2319        len = strlen (namebuf) + 1;        len = strlen (namebuf) + 1;
2320        name = bfd_alloc (abfd, (bfd_size_type) len);        name = bfd_alloc (abfd, len);
2321        if (!name)        if (!name)
2322          return false;          return FALSE;
2323        memcpy (name, namebuf, len);        memcpy (name, namebuf, len);
2324        newsect = bfd_make_section (abfd, name);        newsect = bfd_make_section (abfd, name);
2325        if (newsect == NULL)        if (newsect == NULL)
2326          return false;          return FALSE;
2327        newsect->vma = hdr->p_vaddr + hdr->p_filesz;        newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2328        newsect->lma = hdr->p_paddr + hdr->p_filesz;        newsect->lma = hdr->p_paddr + hdr->p_filesz;
2329        newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;        newsect->size = hdr->p_memsz - hdr->p_filesz;
2330        if (hdr->p_type == PT_LOAD)        if (hdr->p_type == PT_LOAD)
2331          {          {
2332            newsect->flags |= SEC_ALLOC;            newsect->flags |= SEC_ALLOC;
# Line 2210  _bfd_elf_make_section_from_phdr (abfd, h Line 2337  _bfd_elf_make_section_from_phdr (abfd, h
2337          newsect->flags |= SEC_READONLY;          newsect->flags |= SEC_READONLY;
2338      }      }
2339    
2340    return true;    return TRUE;
2341  }  }
2342    
2343  boolean  bfd_boolean
2344  bfd_section_from_phdr (abfd, hdr, index)  bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
      bfd *abfd;  
      Elf_Internal_Phdr *hdr;  
      int index;  
2345  {  {
2346    struct elf_backend_data *bed;    const struct elf_backend_data *bed;
2347    
2348    switch (hdr->p_type)    switch (hdr->p_type)
2349      {      {
# Line 2237  bfd_section_from_phdr (abfd, hdr, index) Line 2361  bfd_section_from_phdr (abfd, hdr, index)
2361    
2362      case PT_NOTE:      case PT_NOTE:
2363        if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))        if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2364          return false;          return FALSE;
2365        if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))        if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2366          return false;          return FALSE;
2367        return true;        return TRUE;
2368    
2369      case PT_SHLIB:      case PT_SHLIB:
2370        return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");        return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
# Line 2248  bfd_section_from_phdr (abfd, hdr, index) Line 2372  bfd_section_from_phdr (abfd, hdr, index)
2372      case PT_PHDR:      case PT_PHDR:
2373        return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");        return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2374    
2375        case PT_GNU_EH_FRAME:
2376          return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2377                                                  "eh_frame_hdr");
2378    
2379        case PT_GNU_STACK:
2380          return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2381    
2382        case PT_GNU_RELRO:
2383          return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2384    
2385      default:      default:
2386        /* Check for any processor-specific program segment types.        /* Check for any processor-specific program segment types.  */
          If no handler for them, default to making "segment" sections.  */  
2387        bed = get_elf_backend_data (abfd);        bed = get_elf_backend_data (abfd);
2388        if (bed->elf_backend_section_from_phdr)        return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
         return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);  
       else  
         return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");  
2389      }      }
2390  }  }
2391    
2392  /* Initialize REL_HDR, the section-header for new section, containing  /* Initialize REL_HDR, the section-header for new section, containing
2393     relocations against ASECT.  If USE_RELA_P is true, we use RELA     relocations against ASECT.  If USE_RELA_P is TRUE, we use RELA
2394     relocations; otherwise, we use REL relocations.  */     relocations; otherwise, we use REL relocations.  */
2395    
2396  boolean  bfd_boolean
2397  _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)  _bfd_elf_init_reloc_shdr (bfd *abfd,
2398       bfd *abfd;                            Elf_Internal_Shdr *rel_hdr,
2399       Elf_Internal_Shdr *rel_hdr;                            asection *asect,
2400       asection *asect;                            bfd_boolean use_rela_p)
      boolean use_rela_p;  
2401  {  {
2402    char *name;    char *name;
2403    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2404    bfd_size_type amt = sizeof ".rela" + strlen (asect->name);    bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2405    
2406    name = bfd_alloc (abfd, amt);    name = bfd_alloc (abfd, amt);
2407    if (name == NULL)    if (name == NULL)
2408      return false;      return FALSE;
2409    sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);    sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2410    rel_hdr->sh_name =    rel_hdr->sh_name =
2411      (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,      (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2412                                          false);                                          FALSE);
2413    if (rel_hdr->sh_name == (unsigned int) -1)    if (rel_hdr->sh_name == (unsigned int) -1)
2414      return false;      return FALSE;
2415    rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;    rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2416    rel_hdr->sh_entsize = (use_rela_p    rel_hdr->sh_entsize = (use_rela_p
2417                           ? bed->s->sizeof_rela                           ? bed->s->sizeof_rela
2418                           : bed->s->sizeof_rel);                           : bed->s->sizeof_rel);
2419    rel_hdr->sh_addralign = bed->s->file_align;    rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2420    rel_hdr->sh_flags = 0;    rel_hdr->sh_flags = 0;
2421    rel_hdr->sh_addr = 0;    rel_hdr->sh_addr = 0;
2422    rel_hdr->sh_size = 0;    rel_hdr->sh_size = 0;
2423    rel_hdr->sh_offset = 0;    rel_hdr->sh_offset = 0;
2424    
2425    return true;    return TRUE;
2426  }  }
2427    
2428  /* Set up an ELF internal section header for a section.  */  /* Set up an ELF internal section header for a section.  */
2429    
2430  static void  static void
2431  elf_fake_sections (abfd, asect, failedptrarg)  elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
      bfd *abfd;  
      asection *asect;  
      PTR failedptrarg;  
2432  {  {
2433    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2434    boolean *failedptr = (boolean *) failedptrarg;    bfd_boolean *failedptr = failedptrarg;
2435    Elf_Internal_Shdr *this_hdr;    Elf_Internal_Shdr *this_hdr;
2436    
2437    if (*failedptr)    if (*failedptr)
# Line 2317  elf_fake_sections (abfd, asect, failedpt Line 2443  elf_fake_sections (abfd, asect, failedpt
2443    
2444    this_hdr = &elf_section_data (asect)->this_hdr;    this_hdr = &elf_section_data (asect)->this_hdr;
2445    
2446    this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),    this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2447                                                             asect->name, false);                                                            asect->name, FALSE);
2448    if (this_hdr->sh_name == (unsigned long) -1)    if (this_hdr->sh_name == (unsigned int) -1)
2449      {      {
2450        *failedptr = true;        *failedptr = TRUE;
2451        return;        return;
2452      }      }
2453    
# Line 2334  elf_fake_sections (abfd, asect, failedpt Line 2460  elf_fake_sections (abfd, asect, failedpt
2460      this_hdr->sh_addr = 0;      this_hdr->sh_addr = 0;
2461    
2462    this_hdr->sh_offset = 0;    this_hdr->sh_offset = 0;
2463    this_hdr->sh_size = asect->_raw_size;    this_hdr->sh_size = asect->size;
2464    this_hdr->sh_link = 0;    this_hdr->sh_link = 0;
2465    this_hdr->sh_addralign = 1 << asect->alignment_power;    this_hdr->sh_addralign = 1 << asect->alignment_power;
2466    /* The sh_entsize and sh_info fields may have been set already by    /* The sh_entsize and sh_info fields may have been set already by
# Line 2343  elf_fake_sections (abfd, asect, failedpt Line 2469  elf_fake_sections (abfd, asect, failedpt
2469    this_hdr->bfd_section = asect;    this_hdr->bfd_section = asect;
2470    this_hdr->contents = NULL;    this_hdr->contents = NULL;
2471    
2472    /* FIXME: This should not be based on section names.  */    /* If the section type is unspecified, we set it based on
2473    if (strcmp (asect->name, ".dynstr") == 0)       asect->flags.  */
2474      this_hdr->sh_type = SHT_STRTAB;    if (this_hdr->sh_type == SHT_NULL)
2475    else if (strcmp (asect->name, ".hash") == 0)      {
2476      {        if ((asect->flags & SEC_GROUP) != 0)
2477        this_hdr->sh_type = SHT_HASH;          {
2478        this_hdr->sh_entsize = bed->s->sizeof_hash_entry;            /* We also need to mark SHF_GROUP here for relocatable
2479                 link.  */
2480              struct bfd_link_order *l;
2481              asection *elt;
2482    
2483              for (l = asect->link_order_head; l != NULL; l = l->next)
2484                if (l->type == bfd_indirect_link_order
2485                    && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2486                  do
2487                    {
2488                      /* The name is not important. Anything will do.  */
2489                      elf_group_name (elt->output_section) = "G";
2490                      elf_section_flags (elt->output_section) |= SHF_GROUP;
2491    
2492                      elt = elf_next_in_group (elt);
2493                      /* During a relocatable link, the lists are
2494                         circular.  */
2495                    }
2496                  while (elt != elf_next_in_group (l->u.indirect.section));
2497    
2498              this_hdr->sh_type = SHT_GROUP;
2499            }
2500          else if ((asect->flags & SEC_ALLOC) != 0
2501              && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2502                  || (asect->flags & SEC_NEVER_LOAD) != 0))
2503            this_hdr->sh_type = SHT_NOBITS;
2504          else
2505            this_hdr->sh_type = SHT_PROGBITS;
2506      }      }
2507    else if (strcmp (asect->name, ".dynsym") == 0)  
2508      switch (this_hdr->sh_type)
2509      {      {
2510        this_hdr->sh_type = SHT_DYNSYM;      default:
2511          break;
2512    
2513        case SHT_STRTAB:
2514        case SHT_INIT_ARRAY:
2515        case SHT_FINI_ARRAY:
2516        case SHT_PREINIT_ARRAY:
2517        case SHT_NOTE:
2518        case SHT_NOBITS:
2519        case SHT_PROGBITS:
2520          break;
2521    
2522        case SHT_HASH:
2523          this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2524          break;
2525    
2526        case SHT_DYNSYM:
2527        this_hdr->sh_entsize = bed->s->sizeof_sym;        this_hdr->sh_entsize = bed->s->sizeof_sym;
2528      }        break;
2529    else if (strcmp (asect->name, ".dynamic") == 0)  
2530      {      case SHT_DYNAMIC:
       this_hdr->sh_type = SHT_DYNAMIC;  
2531        this_hdr->sh_entsize = bed->s->sizeof_dyn;        this_hdr->sh_entsize = bed->s->sizeof_dyn;
2532      }        break;
2533    else if (strncmp (asect->name, ".rela", 5) == 0  
2534             && get_elf_backend_data (abfd)->may_use_rela_p)      case SHT_RELA:
2535      {        if (get_elf_backend_data (abfd)->may_use_rela_p)
2536        this_hdr->sh_type = SHT_RELA;          this_hdr->sh_entsize = bed->s->sizeof_rela;
2537        this_hdr->sh_entsize = bed->s->sizeof_rela;        break;
2538      }  
2539    else if (strncmp (asect->name, ".rel", 4) == 0       case SHT_REL:
2540             && get_elf_backend_data (abfd)->may_use_rel_p)        if (get_elf_backend_data (abfd)->may_use_rel_p)
2541      {          this_hdr->sh_entsize = bed->s->sizeof_rel;
2542        this_hdr->sh_type = SHT_REL;        break;
2543        this_hdr->sh_entsize = bed->s->sizeof_rel;  
2544      }       case SHT_GNU_versym:
   else if (strcmp (asect->name, ".init_array") == 0)  
     this_hdr->sh_type = SHT_INIT_ARRAY;  
   else if (strcmp (asect->name, ".fini_array") == 0)  
     this_hdr->sh_type = SHT_FINI_ARRAY;  
   else if (strcmp (asect->name, ".preinit_array") == 0)  
     this_hdr->sh_type = SHT_PREINIT_ARRAY;  
   else if (strncmp (asect->name, ".note", 5) == 0)  
     this_hdr->sh_type = SHT_NOTE;  
   else if (strncmp (asect->name, ".stab", 5) == 0  
            && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)  
     this_hdr->sh_type = SHT_STRTAB;  
   else if (strcmp (asect->name, ".gnu.version") == 0)  
     {  
       this_hdr->sh_type = SHT_GNU_versym;  
2545        this_hdr->sh_entsize = sizeof (Elf_External_Versym);        this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2546      }        break;
2547    else if (strcmp (asect->name, ".gnu.version_d") == 0)  
2548      {       case SHT_GNU_verdef:
       this_hdr->sh_type = SHT_GNU_verdef;  
2549        this_hdr->sh_entsize = 0;        this_hdr->sh_entsize = 0;
2550        /* objcopy or strip will copy over sh_info, but may not set        /* objcopy or strip will copy over sh_info, but may not set
2551           cverdefs.  The linker will set cverdefs, but sh_info will be           cverdefs.  The linker will set cverdefs, but sh_info will be
# Line 2401  elf_fake_sections (abfd, asect, failedpt Line 2555  elf_fake_sections (abfd, asect, failedpt
2555        else        else
2556          BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0          BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2557                      || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);                      || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2558      }        break;
2559    else if (strcmp (asect->name, ".gnu.version_r") == 0)  
2560      {      case SHT_GNU_verneed:
       this_hdr->sh_type = SHT_GNU_verneed;  
2561        this_hdr->sh_entsize = 0;        this_hdr->sh_entsize = 0;
2562        /* objcopy or strip will copy over sh_info, but may not set        /* objcopy or strip will copy over sh_info, but may not set
2563           cverrefs.  The linker will set cverrefs, but sh_info will be           cverrefs.  The linker will set cverrefs, but sh_info will be
# Line 2414  elf_fake_sections (abfd, asect, failedpt Line 2567  elf_fake_sections (abfd, asect, failedpt
2567        else        else
2568          BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0          BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2569                      || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);                      || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2570      }        break;
2571    else if ((asect->flags & SEC_GROUP) != 0)  
2572      {      case SHT_GROUP:
       this_hdr->sh_type = SHT_GROUP;  
2573        this_hdr->sh_entsize = 4;        this_hdr->sh_entsize = 4;
2574          break;
2575      }      }
   else if ((asect->flags & SEC_ALLOC) != 0  
            && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)  
                || (asect->flags & SEC_NEVER_LOAD) != 0))  
     this_hdr->sh_type = SHT_NOBITS;  
   else  
     this_hdr->sh_type = SHT_PROGBITS;  
2576    
2577    if ((asect->flags & SEC_ALLOC) != 0)    if ((asect->flags & SEC_ALLOC) != 0)
2578      this_hdr->sh_flags |= SHF_ALLOC;      this_hdr->sh_flags |= SHF_ALLOC;
# Line 2443  elf_fake_sections (abfd, asect, failedpt Line 2590  elf_fake_sections (abfd, asect, failedpt
2590    if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)    if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2591      this_hdr->sh_flags |= SHF_GROUP;      this_hdr->sh_flags |= SHF_GROUP;
2592    if ((asect->flags & SEC_THREAD_LOCAL) != 0)    if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2593      this_hdr->sh_flags |= SHF_TLS;      {
2594          this_hdr->sh_flags |= SHF_TLS;
2595          if (asect->size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2596            {
2597              struct bfd_link_order *o;
2598    
2599              this_hdr->sh_size = 0;
2600              for (o = asect->link_order_head; o != NULL; o = o->next)
2601                if (this_hdr->sh_size < o->offset + o->size)
2602                  this_hdr->sh_size = o->offset + o->size;
2603              if (this_hdr->sh_size)
2604                this_hdr->sh_type = SHT_NOBITS;
2605            }
2606        }
2607    
2608    /* Check for processor-specific section types.  */    /* Check for processor-specific section types.  */
2609    if (bed->elf_backend_fake_sections    if (bed->elf_backend_fake_sections
2610        && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))        && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2611      *failedptr = true;      *failedptr = TRUE;
2612    
2613    /* If the section has relocs, set up a section header for the    /* If the section has relocs, set up a section header for the
2614       SHT_REL[A] section.  If two relocation sections are required for       SHT_REL[A] section.  If two relocation sections are required for
# Line 2458  elf_fake_sections (abfd, asect, failedpt Line 2618  elf_fake_sections (abfd, asect, failedpt
2618        && !_bfd_elf_init_reloc_shdr (abfd,        && !_bfd_elf_init_reloc_shdr (abfd,
2619                                      &elf_section_data (asect)->rel_hdr,                                      &elf_section_data (asect)->rel_hdr,
2620                                      asect,                                      asect,
2621                                      elf_section_data (asect)->use_rela_p))                                      asect->use_rela_p))
2622      *failedptr = true;      *failedptr = TRUE;
2623  }  }
2624    
2625  /* Fill in the contents of a SHT_GROUP section.  */  /* Fill in the contents of a SHT_GROUP section.  */
2626    
2627  void  void
2628  bfd_elf_set_group_contents (abfd, sec, failedptrarg)  bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
      bfd *abfd;  
      asection *sec;  
      PTR failedptrarg;  
2629  {  {
2630    boolean *failedptr = (boolean *) failedptrarg;    bfd_boolean *failedptr = failedptrarg;
2631    unsigned long symindx;    unsigned long symindx;
2632    asection *elt, *first;    asection *elt, *first;
2633    unsigned char *loc;    unsigned char *loc;
2634    struct bfd_link_order *l;    struct bfd_link_order *l;
2635    boolean gas;    bfd_boolean gas;
2636    
2637    if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP    if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2638        || *failedptr)        || *failedptr)
# Line 2497  bfd_elf_set_group_contents (abfd, sec, f Line 2654  bfd_elf_set_group_contents (abfd, sec, f
2654    elf_section_data (sec)->this_hdr.sh_info = symindx;    elf_section_data (sec)->this_hdr.sh_info = symindx;
2655    
2656    /* The contents won't be allocated for "ld -r" or objcopy.  */    /* The contents won't be allocated for "ld -r" or objcopy.  */
2657    gas = true;    gas = TRUE;
2658    if (sec->contents == NULL)    if (sec->contents == NULL)
2659      {      {
2660        gas = false;        gas = FALSE;
2661        sec->contents = bfd_alloc (abfd, sec->_raw_size);        sec->contents = bfd_alloc (abfd, sec->size);
2662    
2663        /* Arrange for the section to be written out.  */        /* Arrange for the section to be written out.  */
2664        elf_section_data (sec)->this_hdr.contents = sec->contents;        elf_section_data (sec)->this_hdr.contents = sec->contents;
2665        if (sec->contents == NULL)        if (sec->contents == NULL)
2666          {          {
2667            *failedptr = true;            *failedptr = TRUE;
2668            return;            return;
2669          }          }
2670      }      }
2671    
2672    loc = sec->contents + sec->_raw_size;    loc = sec->contents + sec->size;
2673    
2674    /* Get the pointer to the first section in the group that gas    /* Get the pointer to the first section in the group that gas
2675       squirreled away here.  objcopy arranges for this to be set to the       squirreled away here.  objcopy arranges for this to be set to the
# Line 2557  bfd_elf_set_group_contents (abfd, sec, f Line 2714  bfd_elf_set_group_contents (abfd, sec, f
2714          }          }
2715        while (elt != elf_next_in_group (l->u.indirect.section));        while (elt != elf_next_in_group (l->u.indirect.section));
2716    
2717    /* With ld -r, merging SHT_GROUP sections results in wasted space    if ((loc -= 4) != sec->contents)
      due to allowing for the flag word on each input.  We may well  
      duplicate entries too.  */  
   while ((loc -= 4) > sec->contents)  
     H_PUT_32 (abfd, 0, loc);  
   
   if (loc != sec->contents)  
2718      abort ();      abort ();
2719    
2720    H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);    H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
# Line 2573  bfd_elf_set_group_contents (abfd, sec, f Line 2724  bfd_elf_set_group_contents (abfd, sec, f
2724     too.  The link/info pointers for the standard section types are filled     too.  The link/info pointers for the standard section types are filled
2725     in here too, while we're at it.  */     in here too, while we're at it.  */
2726    
2727  static boolean  static bfd_boolean
2728  assign_section_numbers (abfd)  assign_section_numbers (bfd *abfd)
      bfd *abfd;  
2729  {  {
2730    struct elf_obj_tdata *t = elf_tdata (abfd);    struct elf_obj_tdata *t = elf_tdata (abfd);
2731    asection *sec;    asection *sec;
2732    unsigned int section_number, secn;    unsigned int section_number, secn;
2733    Elf_Internal_Shdr **i_shdrp;    Elf_Internal_Shdr **i_shdrp;
2734    bfd_size_type amt;    bfd_size_type amt;
2735      struct bfd_elf_section_data *d;
2736    
2737    section_number = 1;    section_number = 1;
2738    
2739    _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));    _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2740    
2741      /* Put SHT_GROUP sections first.  */
2742    for (sec = abfd->sections; sec; sec = sec->next)    for (sec = abfd->sections; sec; sec = sec->next)
2743      {      {
2744        struct bfd_elf_section_data *d = elf_section_data (sec);        d = elf_section_data (sec);
2745    
2746        if (section_number == SHN_LORESERVE)        if (d->this_hdr.sh_type == SHT_GROUP)
2747          section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;          {
2748        d->this_idx = section_number++;            if (section_number == SHN_LORESERVE)
2749                section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2750              d->this_idx = section_number++;
2751            }
2752        }
2753    
2754      for (sec = abfd->sections; sec; sec = sec->next)
2755        {
2756          d = elf_section_data (sec);
2757    
2758          if (d->this_hdr.sh_type != SHT_GROUP)
2759            {
2760              if (section_number == SHN_LORESERVE)
2761                section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2762              d->this_idx = section_number++;
2763            }
2764        _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);        _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2765        if ((sec->flags & SEC_RELOC) == 0)        if ((sec->flags & SEC_RELOC) == 0)
2766          d->rel_idx = 0;          d->rel_idx = 0;
# Line 2635  assign_section_numbers (abfd) Line 2802  assign_section_numbers (abfd)
2802            t->symtab_shndx_section = section_number++;            t->symtab_shndx_section = section_number++;
2803            t->symtab_shndx_hdr.sh_name            t->symtab_shndx_hdr.sh_name
2804              = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),              = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2805                                                    ".symtab_shndx", false);                                                    ".symtab_shndx", FALSE);
2806            if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)            if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2807              return false;              return FALSE;
2808          }          }
2809        if (section_number == SHN_LORESERVE)        if (section_number == SHN_LORESERVE)
2810          section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;          section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
# Line 2656  assign_section_numbers (abfd) Line 2823  assign_section_numbers (abfd)
2823    /* Set up the list of section header pointers, in agreement with the    /* Set up the list of section header pointers, in agreement with the
2824       indices.  */       indices.  */
2825    amt = section_number * sizeof (Elf_Internal_Shdr *);    amt = section_number * sizeof (Elf_Internal_Shdr *);
2826    i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);    i_shdrp = bfd_zalloc (abfd, amt);
2827    if (i_shdrp == NULL)    if (i_shdrp == NULL)
2828      return false;      return FALSE;
2829    
2830    amt = sizeof (Elf_Internal_Shdr);    amt = sizeof (Elf_Internal_Shdr);
2831    i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);    i_shdrp[0] = bfd_zalloc (abfd, amt);
2832    if (i_shdrp[0] == NULL)    if (i_shdrp[0] == NULL)
2833      {      {
2834        bfd_release (abfd, i_shdrp);        bfd_release (abfd, i_shdrp);
2835        return false;        return FALSE;
2836      }      }
   memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));  
2837    
2838    elf_elfsections (abfd) = i_shdrp;    elf_elfsections (abfd) = i_shdrp;
2839    
# Line 2683  assign_section_numbers (abfd) Line 2849  assign_section_numbers (abfd)
2849        i_shdrp[t->strtab_section] = &t->strtab_hdr;        i_shdrp[t->strtab_section] = &t->strtab_hdr;
2850        t->symtab_hdr.sh_link = t->strtab_section;        t->symtab_hdr.sh_link = t->strtab_section;
2851      }      }
2852    
2853    for (sec = abfd->sections; sec; sec = sec->next)    for (sec = abfd->sections; sec; sec = sec->next)
2854      {      {
2855        struct bfd_elf_section_data *d = elf_section_data (sec);        struct bfd_elf_section_data *d = elf_section_data (sec);
# Line 2711  assign_section_numbers (abfd) Line 2878  assign_section_numbers (abfd)
2878            d->rel_hdr2->sh_info = d->this_idx;            d->rel_hdr2->sh_info = d->this_idx;
2879          }          }
2880    
2881          /* We need to set up sh_link for SHF_LINK_ORDER.  */
2882          if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2883            {
2884              s = elf_linked_to_section (sec);
2885              if (s)
2886                d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2887              else
2888                {
2889                  struct bfd_link_order *p;
2890    
2891                  /* Find out what the corresponding section in output
2892                     is.  */
2893                  for (p = sec->link_order_head; p != NULL; p = p->next)
2894                    {
2895                      s = p->u.indirect.section;
2896                      if (p->type == bfd_indirect_link_order
2897                          && (bfd_get_flavour (s->owner)
2898                              == bfd_target_elf_flavour))
2899                        {
2900                          Elf_Internal_Shdr ** const elf_shdrp
2901                            = elf_elfsections (s->owner);
2902                          int elfsec
2903                            = _bfd_elf_section_from_bfd_section (s->owner, s);
2904                          elfsec = elf_shdrp[elfsec]->sh_link;
2905                          /* PR 290:
2906                             The Intel C compiler generates SHT_IA_64_UNWIND with
2907                             SHF_LINK_ORDER.  But it doesn't set theh sh_link or
2908                             sh_info fields.  Hence we could get the situation
2909                             where elfsec is 0.  */
2910                          if (elfsec == 0)
2911                            {
2912                              const struct elf_backend_data *bed
2913                                = get_elf_backend_data (abfd);
2914                              if (bed->link_order_error_handler)
2915                                bed->link_order_error_handler
2916                                  (_("%B: warning: sh_link not set for section `%S'"),
2917                                   abfd, s);
2918                            }
2919                          else
2920                            {
2921                              s = elf_shdrp[elfsec]->bfd_section->output_section;
2922                              BFD_ASSERT (s != NULL);
2923                              d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2924                            }
2925                          break;
2926                        }
2927                    }
2928                }
2929            }
2930    
2931        switch (d->this_hdr.sh_type)        switch (d->this_hdr.sh_type)
2932          {          {
2933          case SHT_REL:          case SHT_REL:
# Line 2748  assign_section_numbers (abfd) Line 2965  assign_section_numbers (abfd)
2965                char *alc;                char *alc;
2966    
2967                len = strlen (sec->name);                len = strlen (sec->name);
2968                alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));                alc = bfd_malloc (len - 2);
2969                if (alc == NULL)                if (alc == NULL)
2970                  return false;                  return FALSE;
2971                memcpy (alc, sec->name, len - 3);                memcpy (alc, sec->name, len - 3);
2972                alc[len - 3] = '\0';                alc[len - 3] = '\0';
2973                s = bfd_get_section_by_name (abfd, alc);                s = bfd_get_section_by_name (abfd, alc);
# Line 2779  assign_section_numbers (abfd) Line 2996  assign_section_numbers (abfd)
2996              d->this_hdr.sh_link = elf_section_data (s)->this_idx;              d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2997            break;            break;
2998    
2999            case SHT_GNU_LIBLIST:
3000              /* sh_link is the section header index of the prelink library
3001                 list
3002                 used for the dynamic entries, or the symbol table, or the
3003                 version strings.  */
3004              s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3005                                                 ? ".dynstr" : ".gnu.libstr");
3006              if (s != NULL)
3007                d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3008              break;
3009    
3010          case SHT_HASH:          case SHT_HASH:
3011          case SHT_GNU_versym:          case SHT_GNU_versym:
3012            /* sh_link is the section header index of the symbol table            /* sh_link is the section header index of the symbol table
# Line 2799  assign_section_numbers (abfd) Line 3027  assign_section_numbers (abfd)
3027      else      else
3028        i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),        i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3029                                                         i_shdrp[secn]->sh_name);                                                         i_shdrp[secn]->sh_name);
3030    return true;    return TRUE;
3031  }  }
3032    
3033  /* Map symbol from it's internal number to the external number, moving  /* Map symbol from it's internal number to the external number, moving
3034     all local symbols to be at the head of the list.  */     all local symbols to be at the head of the list.  */
3035    
3036  static INLINE int  static int
3037  sym_is_global (abfd, sym)  sym_is_global (bfd *abfd, asymbol *sym)
      bfd *abfd;  
      asymbol *sym;  
3038  {  {
3039    /* If the backend has a special mapping, use it.  */    /* If the backend has a special mapping, use it.  */
3040    if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3041      return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)    if (bed->elf_backend_sym_is_global)
3042              (abfd, sym));      return (*bed->elf_backend_sym_is_global) (abfd, sym);
3043    
3044    return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0    return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3045            || bfd_is_und_section (bfd_get_section (sym))            || bfd_is_und_section (bfd_get_section (sym))
3046            || bfd_is_com_section (bfd_get_section (sym)));            || bfd_is_com_section (bfd_get_section (sym)));
3047  }  }
3048    
3049  static boolean  static bfd_boolean
3050  elf_map_symbols (abfd)  elf_map_symbols (bfd *abfd)
      bfd *abfd;  
3051  {  {
3052    unsigned int symcount = bfd_get_symcount (abfd);    unsigned int symcount = bfd_get_symcount (abfd);
3053    asymbol **syms = bfd_get_outsymbols (abfd);    asymbol **syms = bfd_get_outsymbols (abfd);
# Line 2850  elf_map_symbols (abfd) Line 3075  elf_map_symbols (abfd)
3075    
3076    max_index++;    max_index++;
3077    amt = max_index * sizeof (asymbol *);    amt = max_index * sizeof (asymbol *);
3078    sect_syms = (asymbol **) bfd_zalloc (abfd, amt);    sect_syms = bfd_zalloc (abfd, amt);
3079    if (sect_syms == NULL)    if (sect_syms == NULL)
3080      return false;      return FALSE;
3081    elf_section_syms (abfd) = sect_syms;    elf_section_syms (abfd) = sect_syms;
3082    elf_num_section_syms (abfd) = max_index;    elf_num_section_syms (abfd) = max_index;
3083    
# Line 2924  elf_map_symbols (abfd) Line 3149  elf_map_symbols (abfd)
3149    
3150    /* Now sort the symbols so the local symbols are first.  */    /* Now sort the symbols so the local symbols are first.  */
3151    amt = (num_locals + num_globals) * sizeof (asymbol *);    amt = (num_locals + num_globals) * sizeof (asymbol *);
3152    new_syms = (asymbol **) bfd_alloc (abfd, amt);    new_syms = bfd_alloc (abfd, amt);
3153    
3154    if (new_syms == NULL)    if (new_syms == NULL)
3155      return false;      return FALSE;
3156    
3157    for (idx = 0; idx < symcount; idx++)    for (idx = 0; idx < symcount; idx++)
3158      {      {
# Line 2962  elf_map_symbols (abfd) Line 3187  elf_map_symbols (abfd)
3187    
3188    elf_num_locals (abfd) = num_locals;    elf_num_locals (abfd) = num_locals;
3189    elf_num_globals (abfd) = num_globals;    elf_num_globals (abfd) = num_globals;
3190    return true;    return TRUE;
3191  }  }
3192    
3193  /* Align to the maximum file alignment that could be required for any  /* Align to the maximum file alignment that could be required for any
3194     ELF data structure.  */     ELF data structure.  */
3195    
3196  static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));  static inline file_ptr
3197  static INLINE file_ptr  align_file_position (file_ptr off, int align)
 align_file_position (off, align)  
      file_ptr off;  
      int align;  
3198  {  {
3199    return (off + align - 1) & ~(align - 1);    return (off + align - 1) & ~(align - 1);
3200  }  }
# Line 2980  align_file_position (off, align) Line 3202  align_file_position (off, align)
3202  /* Assign a file position to a section, optionally aligning to the  /* Assign a file position to a section, optionally aligning to the
3203     required section alignment.  */     required section alignment.  */
3204    
3205  INLINE file_ptr  file_ptr
3206  _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)  _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3207       Elf_Internal_Shdr *i_shdrp;                                             file_ptr offset,
3208       file_ptr offset;                                             bfd_boolean align)
      boolean align;  
3209  {  {
3210    if (align)    if (align)
3211      {      {
# Line 3006  _bfd_elf_assign_file_position_for_sectio Line 3227  _bfd_elf_assign_file_position_for_sectio
3227     otherwise prepare to begin writing out the ELF file.  If LINK_INFO     otherwise prepare to begin writing out the ELF file.  If LINK_INFO
3228     is not NULL, this is being called by the ELF backend linker.  */     is not NULL, this is being called by the ELF backend linker.  */
3229    
3230  boolean  bfd_boolean
3231  _bfd_elf_compute_section_file_positions (abfd, link_info)  _bfd_elf_compute_section_file_positions (bfd *abfd,
3232       bfd *abfd;                                           struct bfd_link_info *link_info)
3233       struct bfd_link_info *link_info;  {
3234  {    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3235    struct elf_backend_data *bed = get_elf_backend_data (abfd);    bfd_boolean failed;
3236    boolean failed;    struct bfd_strtab_hash *strtab = NULL;
   struct bfd_strtab_hash *strtab;  
3237    Elf_Internal_Shdr *shstrtab_hdr;    Elf_Internal_Shdr *shstrtab_hdr;
3238    
3239    if (abfd->output_has_begun)    if (abfd->output_has_begun)
3240      return true;      return TRUE;
3241    
3242    /* Do any elf backend specific processing first.  */    /* Do any elf backend specific processing first.  */
3243    if (bed->elf_backend_begin_write_processing)    if (bed->elf_backend_begin_write_processing)
3244      (*bed->elf_backend_begin_write_processing) (abfd, link_info);      (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3245    
3246    if (! prep_headers (abfd))    if (! prep_headers (abfd))
3247      return false;      return FALSE;
3248    
3249    /* Post process the headers if necessary.  */    /* Post process the headers if necessary.  */
3250    if (bed->elf_backend_post_process_headers)    if (bed->elf_backend_post_process_headers)
3251      (*bed->elf_backend_post_process_headers) (abfd, link_info);      (*bed->elf_backend_post_process_headers) (abfd, link_info);
3252    
3253    failed = false;    failed = FALSE;
3254    bfd_map_over_sections (abfd, elf_fake_sections, &failed);    bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3255    if (failed)    if (failed)
3256      return false;      return FALSE;
3257    
3258    if (!assign_section_numbers (abfd))    if (!assign_section_numbers (abfd))
3259      return false;      return FALSE;
3260    
3261    /* The backend linker builds symbol table information itself.  */    /* The backend linker builds symbol table information itself.  */
3262    if (link_info == NULL && bfd_get_symcount (abfd) > 0)    if (link_info == NULL && bfd_get_symcount (abfd) > 0)
# Line 3045  _bfd_elf_compute_section_file_positions Line 3265  _bfd_elf_compute_section_file_positions
3265        int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));        int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3266    
3267        if (! swap_out_syms (abfd, &strtab, relocatable_p))        if (! swap_out_syms (abfd, &strtab, relocatable_p))
3268          return false;          return FALSE;
3269      }      }
3270    
3271    if (link_info == NULL)    if (link_info == NULL)
3272      {      {
3273        bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);        bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3274        if (failed)        if (failed)
3275          return false;          return FALSE;
3276      }      }
3277    
3278    shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;    shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
# Line 3067  _bfd_elf_compute_section_file_positions Line 3287  _bfd_elf_compute_section_file_positions
3287    /* sh_offset is set in assign_file_positions_except_relocs.  */    /* sh_offset is set in assign_file_positions_except_relocs.  */
3288    shstrtab_hdr->sh_addralign = 1;    shstrtab_hdr->sh_addralign = 1;
3289    
3290    if (!assign_file_positions_except_relocs (abfd))    if (!assign_file_positions_except_relocs (abfd, link_info))
3291      return false;      return FALSE;
3292    
3293    if (link_info == NULL && bfd_get_symcount (abfd) > 0)    if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3294      {      {
# Line 3078  _bfd_elf_compute_section_file_positions Line 3298  _bfd_elf_compute_section_file_positions
3298        off = elf_tdata (abfd)->next_file_pos;        off = elf_tdata (abfd)->next_file_pos;
3299    
3300        hdr = &elf_tdata (abfd)->symtab_hdr;        hdr = &elf_tdata (abfd)->symtab_hdr;
3301        off = _bfd_elf_assign_file_position_for_section (hdr, off, true);        off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3302    
3303        hdr = &elf_tdata (abfd)->symtab_shndx_hdr;        hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3304        if (hdr->sh_size != 0)        if (hdr->sh_size != 0)
3305          off = _bfd_elf_assign_file_position_for_section (hdr, off, true);          off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3306    
3307        hdr = &elf_tdata (abfd)->strtab_hdr;        hdr = &elf_tdata (abfd)->strtab_hdr;
3308        off = _bfd_elf_assign_file_position_for_section (hdr, off, true);        off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3309    
3310        elf_tdata (abfd)->next_file_pos = off;        elf_tdata (abfd)->next_file_pos = off;
3311    
# Line 3093  _bfd_elf_compute_section_file_positions Line 3313  _bfd_elf_compute_section_file_positions
3313           out.  */           out.  */
3314        if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0        if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3315            || ! _bfd_stringtab_emit (abfd, strtab))            || ! _bfd_stringtab_emit (abfd, strtab))
3316          return false;          return FALSE;
3317        _bfd_stringtab_free (strtab);        _bfd_stringtab_free (strtab);
3318      }      }
3319    
3320    abfd->output_has_begun = true;    abfd->output_has_begun = TRUE;
3321    
3322    return true;    return TRUE;
3323  }  }
3324    
3325  /* Create a mapping from a set of sections to a program segment.  */  /* Create a mapping from a set of sections to a program segment.  */
3326    
3327  static INLINE struct elf_segment_map *  static struct elf_segment_map *
3328  make_mapping (abfd, sections, from, to, phdr)  make_mapping (bfd *abfd,
3329       bfd *abfd;                asection **sections,
3330       asection **sections;                unsigned int from,
3331       unsigned int from;                unsigned int to,
3332       unsigned int to;                bfd_boolean phdr)
      boolean phdr;  
3333  {  {
3334    struct elf_segment_map *m;    struct elf_segment_map *m;
3335    unsigned int i;    unsigned int i;
# Line 3119  make_mapping (abfd, sections, from, to, Line 3338  make_mapping (abfd, sections, from, to,
3338    
3339    amt = sizeof (struct elf_segment_map);    amt = sizeof (struct elf_segment_map);
3340    amt += (to - from - 1) * sizeof (asection *);    amt += (to - from - 1) * sizeof (asection *);
3341    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);    m = bfd_zalloc (abfd, amt);
3342    if (m == NULL)    if (m == NULL)
3343      return NULL;      return NULL;
3344    m->next = NULL;    m->next = NULL;
# Line 3138  make_mapping (abfd, sections, from, to, Line 3357  make_mapping (abfd, sections, from, to,
3357    return m;    return m;
3358  }  }
3359    
3360    /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
3361       on failure.  */
3362    
3363    struct elf_segment_map *
3364    _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3365    {
3366      struct elf_segment_map *m;
3367    
3368      m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3369      if (m == NULL)
3370        return NULL;
3371      m->next = NULL;
3372      m->p_type = PT_DYNAMIC;
3373      m->count = 1;
3374      m->sections[0] = dynsec;
3375      
3376      return m;
3377    }
3378    
3379  /* Set up a mapping from BFD sections to program segments.  */  /* Set up a mapping from BFD sections to program segments.  */
3380    
3381  static boolean  static bfd_boolean
3382  map_sections_to_segments (abfd)  map_sections_to_segments (bfd *abfd)
      bfd *abfd;  
3383  {  {
3384    asection **sections = NULL;    asection **sections = NULL;
3385    asection *s;    asection *s;
# Line 3152  map_sections_to_segments (abfd) Line 3389  map_sections_to_segments (abfd)
3389    struct elf_segment_map **pm;    struct elf_segment_map **pm;
3390    struct elf_segment_map *m;    struct elf_segment_map *m;
3391    asection *last_hdr;    asection *last_hdr;
3392      bfd_vma last_size;
3393    unsigned int phdr_index;    unsigned int phdr_index;
3394    bfd_vma maxpagesize;    bfd_vma maxpagesize;
3395    asection **hdrpp;    asection **hdrpp;
3396    boolean phdr_in_segment = true;    bfd_boolean phdr_in_segment = TRUE;
3397    boolean writable;    bfd_boolean writable;
3398    int tls_count = 0;    int tls_count = 0;
3399    asection *first_tls = NULL;    asection *first_tls = NULL;
3400    asection *dynsec, *eh_frame_hdr;    asection *dynsec, *eh_frame_hdr;
3401    bfd_size_type amt;    bfd_size_type amt;
3402    
3403    if (elf_tdata (abfd)->segment_map != NULL)    if (elf_tdata (abfd)->segment_map != NULL)
3404      return true;      return TRUE;
3405    
3406    if (bfd_count_sections (abfd) == 0)    if (bfd_count_sections (abfd) == 0)
3407      return true;      return TRUE;
3408    
3409    /* Select the allocated sections, and sort them.  */    /* Select the allocated sections, and sort them.  */
3410    
3411    amt = bfd_count_sections (abfd) * sizeof (asection *);    amt = bfd_count_sections (abfd) * sizeof (asection *);
3412    sections = (asection **) bfd_malloc (amt);    sections = bfd_malloc (amt);
3413    if (sections == NULL)    if (sections == NULL)
3414      goto error_return;      goto error_return;
3415    
# Line 3201  map_sections_to_segments (abfd) Line 3439  map_sections_to_segments (abfd)
3439    if (s != NULL && (s->flags & SEC_LOAD) != 0)    if (s != NULL && (s->flags & SEC_LOAD) != 0)
3440      {      {
3441        amt = sizeof (struct elf_segment_map);        amt = sizeof (struct elf_segment_map);
3442        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);        m = bfd_zalloc (abfd, amt);
3443        if (m == NULL)        if (m == NULL)
3444          goto error_return;          goto error_return;
3445        m->next = NULL;        m->next = NULL;
# Line 3215  map_sections_to_segments (abfd) Line 3453  map_sections_to_segments (abfd)
3453        pm = &m->next;        pm = &m->next;
3454    
3455        amt = sizeof (struct elf_segment_map);        amt = sizeof (struct elf_segment_map);
3456        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);        m = bfd_zalloc (abfd, amt);
3457        if (m == NULL)        if (m == NULL)
3458          goto error_return;          goto error_return;
3459        m->next = NULL;        m->next = NULL;
# Line 3231  map_sections_to_segments (abfd) Line 3469  map_sections_to_segments (abfd)
3469       segment when the start of the second section can be placed within       segment when the start of the second section can be placed within
3470       a few bytes of the end of the first section.  */       a few bytes of the end of the first section.  */
3471    last_hdr = NULL;    last_hdr = NULL;
3472      last_size = 0;
3473    phdr_index = 0;    phdr_index = 0;
3474    maxpagesize = get_elf_backend_data (abfd)->maxpagesize;    maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3475    writable = false;    writable = FALSE;
3476    dynsec = bfd_get_section_by_name (abfd, ".dynamic");    dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3477    if (dynsec != NULL    if (dynsec != NULL
3478        && (dynsec->flags & SEC_LOAD) == 0)        && (dynsec->flags & SEC_LOAD) == 0)
# Line 3253  map_sections_to_segments (abfd) Line 3492  map_sections_to_segments (abfd)
3492        if ((abfd->flags & D_PAGED) == 0        if ((abfd->flags & D_PAGED) == 0
3493            || sections[0]->lma < phdr_size            || sections[0]->lma < phdr_size
3494            || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)            || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3495          phdr_in_segment = false;          phdr_in_segment = FALSE;
3496      }      }
3497    
3498    for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)    for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3499      {      {
3500        asection *hdr;        asection *hdr;
3501        boolean new_segment;        bfd_boolean new_segment;
3502    
3503        hdr = *hdrpp;        hdr = *hdrpp;
3504    
# Line 3270  map_sections_to_segments (abfd) Line 3509  map_sections_to_segments (abfd)
3509          {          {
3510            /* If we don't have a segment yet, then we don't need a new            /* If we don't have a segment yet, then we don't need a new
3511               one (we build the last one after this loop).  */               one (we build the last one after this loop).  */
3512            new_segment = false;            new_segment = FALSE;
3513          }          }
3514        else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)        else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3515          {          {
3516            /* If this section has a different relation between the            /* If this section has a different relation between the
3517               virtual address and the load address, then we need a new               virtual address and the load address, then we need a new
3518               segment.  */               segment.  */
3519            new_segment = true;            new_segment = TRUE;
3520          }          }
3521        else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)        else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3522                 < BFD_ALIGN (hdr->lma, maxpagesize))                 < BFD_ALIGN (hdr->lma, maxpagesize))
3523          {          {
3524            /* If putting this section in this segment would force us to            /* If putting this section in this segment would force us to
3525               skip a page in the segment, then we need a new segment.  */               skip a page in the segment, then we need a new segment.  */
3526            new_segment = true;            new_segment = TRUE;
3527          }          }
3528        else if ((last_hdr->flags & SEC_LOAD) == 0        else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3529                 && (hdr->flags & SEC_LOAD) != 0)                 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3530          {          {
3531            /* We don't want to put a loadable section after a            /* We don't want to put a loadable section after a
3532               nonloadable section in the same segment.  */               nonloadable section in the same segment.
3533            new_segment = true;               Consider .tbss sections as loadable for this purpose.  */
3534              new_segment = TRUE;
3535          }          }
3536        else if ((abfd->flags & D_PAGED) == 0)        else if ((abfd->flags & D_PAGED) == 0)
3537          {          {
3538            /* If the file is not demand paged, which means that we            /* If the file is not demand paged, which means that we
3539               don't require the sections to be correctly aligned in the               don't require the sections to be correctly aligned in the
3540               file, then there is no other reason for a new segment.  */               file, then there is no other reason for a new segment.  */
3541            new_segment = false;            new_segment = FALSE;
3542          }          }
3543        else if (! writable        else if (! writable
3544                 && (hdr->flags & SEC_READONLY) == 0                 && (hdr->flags & SEC_READONLY) == 0
3545                 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)                 && (((last_hdr->lma + last_size - 1)
3546                     == hdr->lma))                      & ~(maxpagesize - 1))
3547                       != (hdr->lma & ~(maxpagesize - 1))))
3548          {          {
3549            /* We don't want to put a writable section in a read only            /* We don't want to put a writable section in a read only
3550               segment, unless they are on the same page in memory               segment, unless they are on the same page in memory
# Line 3312  map_sections_to_segments (abfd) Line 3553  map_sections_to_segments (abfd)
3553               only case in which the new section is not on the same               only case in which the new section is not on the same
3554               page as the previous section is when the previous section               page as the previous section is when the previous section
3555               ends precisely on a page boundary.  */               ends precisely on a page boundary.  */
3556            new_segment = true;            new_segment = TRUE;
3557          }          }
3558        else        else
3559          {          {
3560            /* Otherwise, we can use the same segment.  */            /* Otherwise, we can use the same segment.  */
3561            new_segment = false;            new_segment = FALSE;
3562          }          }
3563    
3564        if (! new_segment)        if (! new_segment)
3565          {          {
3566            if ((hdr->flags & SEC_READONLY) == 0)            if ((hdr->flags & SEC_READONLY) == 0)
3567              writable = true;              writable = TRUE;
3568            last_hdr = hdr;            last_hdr = hdr;
3569              /* .tbss sections effectively have zero size.  */
3570              if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3571                last_size = hdr->size;
3572              else
3573                last_size = 0;
3574            continue;            continue;
3575          }          }
3576    
# Line 3339  map_sections_to_segments (abfd) Line 3585  map_sections_to_segments (abfd)
3585        pm = &m->next;        pm = &m->next;
3586    
3587        if ((hdr->flags & SEC_READONLY) == 0)        if ((hdr->flags & SEC_READONLY) == 0)
3588          writable = true;          writable = TRUE;
3589        else        else
3590          writable = false;          writable = FALSE;
3591    
3592        last_hdr = hdr;        last_hdr = hdr;
3593          /* .tbss sections effectively have zero size.  */
3594          if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3595            last_size = hdr->size;
3596          else
3597            last_size = 0;
3598        phdr_index = i;        phdr_index = i;
3599        phdr_in_segment = false;        phdr_in_segment = FALSE;
3600      }      }
3601    
3602    /* Create a final PT_LOAD program segment.  */    /* Create a final PT_LOAD program segment.  */
# Line 3362  map_sections_to_segments (abfd) Line 3613  map_sections_to_segments (abfd)
3613    /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */    /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
3614    if (dynsec != NULL)    if (dynsec != NULL)
3615      {      {
3616        amt = sizeof (struct elf_segment_map);        m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
       m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);  
3617        if (m == NULL)        if (m == NULL)
3618          goto error_return;          goto error_return;
       m->next = NULL;  
       m->p_type = PT_DYNAMIC;  
       m->count = 1;  
       m->sections[0] = dynsec;  
   
3619        *pm = m;        *pm = m;
3620        pm = &m->next;        pm = &m->next;
3621      }      }
# Line 3386  map_sections_to_segments (abfd) Line 3631  map_sections_to_segments (abfd)
3631            && strncmp (s->name, ".note", 5) == 0)            && strncmp (s->name, ".note", 5) == 0)
3632          {          {
3633            amt = sizeof (struct elf_segment_map);            amt = sizeof (struct elf_segment_map);
3634            m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);            m = bfd_zalloc (abfd, amt);
3635            if (m == NULL)            if (m == NULL)
3636              goto error_return;              goto error_return;
3637            m->next = NULL;            m->next = NULL;
# Line 3412  map_sections_to_segments (abfd) Line 3657  map_sections_to_segments (abfd)
3657    
3658        amt = sizeof (struct elf_segment_map);        amt = sizeof (struct elf_segment_map);
3659        amt += (tls_count - 1) * sizeof (asection *);        amt += (tls_count - 1) * sizeof (asection *);
3660        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);        m = bfd_zalloc (abfd, amt);
3661        if (m == NULL)        if (m == NULL)
3662          goto error_return;          goto error_return;
3663        m->next = NULL;        m->next = NULL;
# Line 3434  map_sections_to_segments (abfd) Line 3679  map_sections_to_segments (abfd)
3679    
3680    /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME    /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3681       segment.  */       segment.  */
3682    eh_frame_hdr = NULL;    eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3683    if (elf_tdata (abfd)->eh_frame_hdr)    if (eh_frame_hdr != NULL
3684      eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");        && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))  
3685      {      {
3686        amt = sizeof (struct elf_segment_map);        amt = sizeof (struct elf_segment_map);
3687        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);        m = bfd_zalloc (abfd, amt);
3688        if (m == NULL)        if (m == NULL)
3689          goto error_return;          goto error_return;
3690        m->next = NULL;        m->next = NULL;
3691        m->p_type = PT_GNU_EH_FRAME;        m->p_type = PT_GNU_EH_FRAME;
3692        m->count = 1;        m->count = 1;
3693        m->sections[0] = eh_frame_hdr;        m->sections[0] = eh_frame_hdr->output_section;
3694    
3695          *pm = m;
3696          pm = &m->next;
3697        }
3698    
3699      if (elf_tdata (abfd)->stack_flags)
3700        {
3701          amt = sizeof (struct elf_segment_map);
3702          m = bfd_zalloc (abfd, amt);
3703          if (m == NULL)
3704            goto error_return;
3705          m->next = NULL;
3706          m->p_type = PT_GNU_STACK;
3707          m->p_flags = elf_tdata (abfd)->stack_flags;
3708          m->p_flags_valid = 1;
3709    
3710          *pm = m;
3711          pm = &m->next;
3712        }
3713    
3714      if (elf_tdata (abfd)->relro)
3715        {
3716          amt = sizeof (struct elf_segment_map);
3717          m = bfd_zalloc (abfd, amt);
3718          if (m == NULL)
3719            goto error_return;
3720          m->next = NULL;
3721          m->p_type = PT_GNU_RELRO;
3722          m->p_flags = PF_R;
3723          m->p_flags_valid = 1;
3724    
3725        *pm = m;        *pm = m;
3726        pm = &m->next;        pm = &m->next;
# Line 3456  map_sections_to_segments (abfd) Line 3730  map_sections_to_segments (abfd)
3730    sections = NULL;    sections = NULL;
3731    
3732    elf_tdata (abfd)->segment_map = mfirst;    elf_tdata (abfd)->segment_map = mfirst;
3733    return true;    return TRUE;
3734    
3735   error_return:   error_return:
3736    if (sections != NULL)    if (sections != NULL)
3737      free (sections);      free (sections);
3738    return false;    return FALSE;
3739  }  }
3740    
3741  /* Sort sections by address.  */  /* Sort sections by address.  */
3742    
3743  static int  static int
3744  elf_sort_sections (arg1, arg2)  elf_sort_sections (const void *arg1, const void *arg2)
      const PTR arg1;  
      const PTR arg2;  
3745  {  {
3746    const asection *sec1 = *(const asection **) arg1;    const asection *sec1 = *(const asection **) arg1;
3747    const asection *sec2 = *(const asection **) arg2;    const asection *sec2 = *(const asection **) arg2;
3748      bfd_size_type size1, size2;
3749    
3750    /* Sort by LMA first, since this is the address used to    /* Sort by LMA first, since this is the address used to
3751       place the section into a segment.  */       place the section into a segment.  */
# Line 3490  elf_sort_sections (arg1, arg2) Line 3763  elf_sort_sections (arg1, arg2)
3763    
3764    /* Put !SEC_LOAD sections after SEC_LOAD ones.  */    /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
3765    
3766  #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)  #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3767    
3768    if (TOEND (sec1))    if (TOEND (sec1))
3769      {      {
# Line 3512  elf_sort_sections (arg1, arg2) Line 3785  elf_sort_sections (arg1, arg2)
3785    /* Sort by size, to put zero sized sections    /* Sort by size, to put zero sized sections
3786       before others at the same address.  */       before others at the same address.  */
3787    
3788    if (sec1->_raw_size < sec2->_raw_size)    size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3789      size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3790    
3791      if (size1 < size2)
3792      return -1;      return -1;
3793    if (sec1->_raw_size > sec2->_raw_size)    if (size1 > size2)
3794      return 1;      return 1;
3795    
3796    return sec1->target_index - sec2->target_index;    return sec1->target_index - sec2->target_index;
3797  }  }
3798    
3799    /* Ian Lance Taylor writes:
3800    
3801       We shouldn't be using % with a negative signed number.  That's just
3802       not good.  We have to make sure either that the number is not
3803       negative, or that the number has an unsigned type.  When the types
3804       are all the same size they wind up as unsigned.  When file_ptr is a
3805       larger signed type, the arithmetic winds up as signed long long,
3806       which is wrong.
3807    
3808       What we're trying to say here is something like ``increase OFF by
3809       the least amount that will cause it to be equal to the VMA modulo
3810       the page size.''  */
3811    /* In other words, something like:
3812    
3813       vma_offset = m->sections[0]->vma % bed->maxpagesize;
3814       off_offset = off % bed->maxpagesize;
3815       if (vma_offset < off_offset)
3816         adjustment = vma_offset + bed->maxpagesize - off_offset;
3817       else
3818         adjustment = vma_offset - off_offset;
3819        
3820       which can can be collapsed into the expression below.  */
3821    
3822    static file_ptr
3823    vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3824    {
3825      return ((vma - off) % maxpagesize);
3826    }
3827    
3828  /* Assign file positions to the sections based on the mapping from  /* Assign file positions to the sections based on the mapping from
3829     sections to segments.  This function also sets up some fields in     sections to segments.  This function also sets up some fields in
3830     the file header, and writes out the program headers.  */     the file header, and writes out the program headers.  */
3831    
3832  static boolean  static bfd_boolean
3833  assign_file_positions_for_segments (abfd)  assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
      bfd *abfd;  
3834  {  {
3835    const struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3836    unsigned int count;    unsigned int count;
# Line 3542  assign_file_positions_for_segments (abfd Line 3846  assign_file_positions_for_segments (abfd
3846    if (elf_tdata (abfd)->segment_map == NULL)    if (elf_tdata (abfd)->segment_map == NULL)
3847      {      {
3848        if (! map_sections_to_segments (abfd))        if (! map_sections_to_segments (abfd))
3849          return false;          return FALSE;
3850      }      }
3851    else    else
3852      {      {
# Line 3578  assign_file_positions_for_segments (abfd Line 3882  assign_file_positions_for_segments (abfd
3882    
3883    if (bed->elf_backend_modify_segment_map)    if (bed->elf_backend_modify_segment_map)
3884      {      {
3885        if (! (*bed->elf_backend_modify_segment_map) (abfd))        if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3886          return false;          return FALSE;
3887      }      }
3888    
3889    count = 0;    count = 0;
# Line 3591  assign_file_positions_for_segments (abfd Line 3895  assign_file_positions_for_segments (abfd
3895    elf_elfheader (abfd)->e_phnum = count;    elf_elfheader (abfd)->e_phnum = count;
3896    
3897    if (count == 0)    if (count == 0)
3898      return true;      {
3899          elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
3900          return TRUE;
3901        }
3902    
3903    /* If we already counted the number of program segments, make sure    /* If we already counted the number of program segments, make sure
3904       that we allocated enough space.  This happens when SIZEOF_HEADERS       that we allocated enough space.  This happens when SIZEOF_HEADERS
# Line 3600  assign_file_positions_for_segments (abfd Line 3907  assign_file_positions_for_segments (abfd
3907    if (alloc != 0 && count > alloc)    if (alloc != 0 && count > alloc)
3908      {      {
3909        ((*_bfd_error_handler)        ((*_bfd_error_handler)
3910         (_("%s: Not enough room for program headers (allocated %u, need %u)"),         (_("%B: Not enough room for program headers (allocated %u, need %u)"),
3911          bfd_get_filename (abfd), alloc, count));          abfd, alloc, count));
3912        bfd_set_error (bfd_error_bad_value);        bfd_set_error (bfd_error_bad_value);
3913        return false;        return FALSE;
3914      }      }
3915    
3916    if (alloc == 0)    if (alloc == 0)
3917      alloc = count;      alloc = count;
3918    
3919    amt = alloc * sizeof (Elf_Internal_Phdr);    amt = alloc * sizeof (Elf_Internal_Phdr);
3920    phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);    phdrs = bfd_alloc (abfd, amt);
3921    if (phdrs == NULL)    if (phdrs == NULL)
3922      return false;      return FALSE;
3923    
3924    off = bed->s->sizeof_ehdr;    off = bed->s->sizeof_ehdr;
3925    off += alloc * bed->s->sizeof_phdr;    off += alloc * bed->s->sizeof_phdr;
# Line 3640  assign_file_positions_for_segments (abfd Line 3947  assign_file_positions_for_segments (abfd
3947          qsort (m->sections, (size_t) m->count, sizeof (asection *),          qsort (m->sections, (size_t) m->count, sizeof (asection *),
3948                 elf_sort_sections);                 elf_sort_sections);
3949    
3950          /* An ELF segment (described by Elf_Internal_Phdr) may contain a
3951             number of sections with contents contributing to both p_filesz
3952             and p_memsz, followed by a number of sections with no contents
3953             that just contribute to p_memsz.  In this loop, OFF tracks next
3954             available file offset for PT_LOAD and PT_NOTE segments.  VOFF is
3955             an adjustment we use for segments that have no file contents
3956             but need zero filled memory allocation.  */
3957          voff = 0;
3958        p->p_type = m->p_type;        p->p_type = m->p_type;
3959        p->p_flags = m->p_flags;        p->p_flags = m->p_flags;
3960    
3961        if (p->p_type == PT_LOAD        if (p->p_type == PT_LOAD
3962            && m->count > 0            && m->count > 0)
           && (m->sections[0]->flags & SEC_ALLOC) != 0)  
3963          {          {
3964              bfd_size_type align;
3965              bfd_vma adjust;
3966    
3967            if ((abfd->flags & D_PAGED) != 0)            if ((abfd->flags & D_PAGED) != 0)
3968              off += (m->sections[0]->vma - off) % bed->maxpagesize;              align = bed->maxpagesize;
3969            else            else
3970              {              {
3971                bfd_size_type align;                unsigned int align_power = 0;
   
               align = 0;  
3972                for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)                for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3973                  {                  {
3974                    bfd_size_type secalign;                    unsigned int secalign;
3975    
3976                    secalign = bfd_get_section_alignment (abfd, *secpp);                    secalign = bfd_get_section_alignment (abfd, *secpp);
3977                    if (secalign > align)                    if (secalign > align_power)
3978                      align = secalign;                      align_power = secalign;
3979                  }                  }
3980                  align = (bfd_size_type) 1 << align_power;
3981                }
3982    
3983                off += (m->sections[0]->vma - off) % (1 << align);            adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
3984              off += adjust;
3985              if (adjust != 0
3986                  && !m->includes_filehdr
3987                  && !m->includes_phdrs
3988                  && (ufile_ptr) off >= align)
3989                {
3990                  /* If the first section isn't loadable, the same holds for
3991                     any other sections.  Since the segment won't need file
3992                     space, we can make p_offset overlap some prior segment.
3993                     However, .tbss is special.  If a segment starts with
3994                     .tbss, we need to look at the next section to decide
3995                     whether the segment has any loadable sections.  */
3996                  i = 0;
3997                  while ((m->sections[i]->flags & SEC_LOAD) == 0)
3998                    {
3999                      if ((m->sections[i]->flags & SEC_THREAD_LOCAL) == 0
4000                          || ++i >= m->count)
4001                        {
4002                          off -= adjust;
4003                          voff = adjust - align;
4004                          break;
4005                        }
4006                    }
4007              }              }
4008          }          }
4009          /* Make sure the .dynamic section is the first section in the
4010             PT_DYNAMIC segment.  */
4011          else if (p->p_type == PT_DYNAMIC
4012                   && m->count > 1
4013                   && strcmp (m->sections[0]->name, ".dynamic") != 0)
4014            {
4015              _bfd_error_handler
4016                (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4017                 abfd);
4018              bfd_set_error (bfd_error_bad_value);
4019              return FALSE;
4020            }
4021    
4022        if (m->count == 0)        if (m->count == 0)
4023          p->p_vaddr = 0;          p->p_vaddr = 0;
# Line 3683  assign_file_positions_for_segments (abfd Line 4035  assign_file_positions_for_segments (abfd
4035            && (abfd->flags & D_PAGED) != 0)            && (abfd->flags & D_PAGED) != 0)
4036          p->p_align = bed->maxpagesize;          p->p_align = bed->maxpagesize;
4037        else if (m->count == 0)        else if (m->count == 0)
4038          p->p_align = bed->s->file_align;          p->p_align = 1 << bed->s->log_file_align;
4039        else        else
4040          p->p_align = 0;          p->p_align = 0;
4041    
# Line 3705  assign_file_positions_for_segments (abfd Line 4057  assign_file_positions_for_segments (abfd
4057                if (p->p_vaddr < (bfd_vma) off)                if (p->p_vaddr < (bfd_vma) off)
4058                  {                  {
4059                    (*_bfd_error_handler)                    (*_bfd_error_handler)
4060                      (_("%s: Not enough room for program headers, try linking with -N"),                      (_("%B: Not enough room for program headers, try linking with -N"),
4061                       bfd_get_filename (abfd));                       abfd);
4062                    bfd_set_error (bfd_error_bad_value);                    bfd_set_error (bfd_error_bad_value);
4063                    return false;                    return FALSE;
4064                  }                  }
4065    
4066                p->p_vaddr -= off;                p->p_vaddr -= off;
# Line 3764  assign_file_positions_for_segments (abfd Line 4116  assign_file_positions_for_segments (abfd
4116            || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))            || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4117          {          {
4118            if (! m->includes_filehdr && ! m->includes_phdrs)            if (! m->includes_filehdr && ! m->includes_phdrs)
4119              p->p_offset = off;              p->p_offset = off + voff;
4120            else            else
4121              {              {
4122                file_ptr adjust;                file_ptr adjust;
# Line 3775  assign_file_positions_for_segments (abfd Line 4127  assign_file_positions_for_segments (abfd
4127              }              }
4128          }          }
4129    
       voff = off;  
   
4130        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4131          {          {
4132            asection *sec;            asection *sec;
# Line 3787  assign_file_positions_for_segments (abfd Line 4137  assign_file_positions_for_segments (abfd
4137            flags = sec->flags;            flags = sec->flags;
4138            align = 1 << bfd_get_section_alignment (abfd, sec);            align = 1 << bfd_get_section_alignment (abfd, sec);
4139    
4140            /* The section may have artificial alignment forced by a            if (p->p_type == PT_LOAD
4141               link script.  Notice this case by the gap between the                || p->p_type == PT_TLS)
              cumulative phdr lma and the section's lma.  */  
           if (p->p_paddr + p->p_memsz < sec->lma)  
             {  
               bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);  
   
               p->p_memsz += adjust;  
               off += adjust;  
               voff += adjust;  
               if ((flags & SEC_LOAD) != 0)  
                 p->p_filesz += adjust;  
             }  
   
           if (p->p_type == PT_LOAD)  
4142              {              {
4143                bfd_signed_vma adjust;                bfd_signed_vma adjust;
4144    
4145                if ((flags & SEC_LOAD) != 0)                if ((flags & SEC_LOAD) != 0)
4146                  {                  {
4147                    adjust = sec->lma - (p->p_paddr + p->p_memsz);                    adjust = sec->lma - (p->p_paddr + p->p_filesz);
4148                    if (adjust < 0)                    if (adjust < 0)
4149                      adjust = 0;                      {
4150                          (*_bfd_error_handler)
4151                            (_("%B: section %A lma 0x%lx overlaps previous sections"),
4152                             abfd, sec, (unsigned long) sec->lma);
4153                          adjust = 0;
4154                        }
4155                      off += adjust;
4156                      p->p_filesz += adjust;
4157                      p->p_memsz += adjust;
4158                  }                  }
4159                else if ((flags & SEC_ALLOC) != 0)                /* .tbss is special.  It doesn't contribute to p_memsz of
4160                     normal segments.  */
4161                  else if ((flags & SEC_THREAD_LOCAL) == 0
4162                           || p->p_type == PT_TLS)
4163                  {                  {
4164                    /* The section VMA must equal the file position                    /* The section VMA must equal the file position
4165                       modulo the page size.  FIXME: I'm not sure if                       modulo the page size.  */
4166                       this adjustment is really necessary.  We used to                    bfd_size_type page = align;
                      not have the SEC_LOAD case just above, and then  
                      this was necessary, but now I'm not sure.  */  
4167                    if ((abfd->flags & D_PAGED) != 0)                    if ((abfd->flags & D_PAGED) != 0)
4168                      adjust = (sec->vma - voff) % bed->maxpagesize;                      page = bed->maxpagesize;
4169                    else                    adjust = vma_page_aligned_bias (sec->vma,
4170                      adjust = (sec->vma - voff) % align;                                                    p->p_vaddr + p->p_memsz,
4171                  }                                                    page);
               else  
                 adjust = 0;  
   
               if (adjust != 0)  
                 {  
                   if (i == 0)  
                     {  
                       (* _bfd_error_handler) (_("\  
 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),  
                                               bfd_section_name (abfd, sec),  
                                               sec->lma,  
                                               p->p_paddr);  
                       return false;  
                     }  
4172                    p->p_memsz += adjust;                    p->p_memsz += adjust;
                   off += adjust;  
                   voff += adjust;  
                   if ((flags & SEC_LOAD) != 0)  
                     p->p_filesz += adjust;  
4173                  }                  }
   
               sec->filepos = off;  
   
               /* We check SEC_HAS_CONTENTS here because if NOLOAD is  
                  used in a linker script we may have a section with  
                  SEC_LOAD clear but which is supposed to have  
                  contents.  */  
               if ((flags & SEC_LOAD) != 0  
                   || (flags & SEC_HAS_CONTENTS) != 0)  
                 off += sec->_raw_size;  
   
               if ((flags & SEC_ALLOC) != 0)  
                 voff += sec->_raw_size;  
4174              }              }
4175    
4176            if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)            if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4177              {              {
4178                /* The actual "note" segment has i == 0.                /* The section at i == 0 is the one that actually contains
4179                   This is the one that actually contains everything.  */                   everything.  */
4180                if (i == 0)                if (i == 0)
4181                  {                  {
4182                    sec->filepos = off;                    sec->filepos = off;
4183                    p->p_filesz = sec->_raw_size;                    off += sec->size;
4184                    off += sec->_raw_size;                    p->p_filesz = sec->size;
4185                    voff = off;                    p->p_memsz = 0;
4186                      p->p_align = 1;
4187                  }                  }
4188                else                else
4189                  {                  {
4190                    /* Fake sections -- don't need to be written.  */                    /* The rest are fake sections that shouldn't be written.  */
4191                    sec->filepos = 0;                    sec->filepos = 0;
4192                    sec->_raw_size = 0;                    sec->size = 0;
4193                    flags = sec->flags = 0;                    sec->flags = 0;
4194                      continue;
4195                  }                  }
               p->p_memsz = 0;  
               p->p_align = 1;  
4196              }              }
4197            else            else
4198              {              {
4199                p->p_memsz += sec->_raw_size;                if (p->p_type == PT_LOAD)
4200                    {
4201                      sec->filepos = off;
4202                      /* FIXME: The SEC_HAS_CONTENTS test here dates back to
4203                         1997, and the exact reason for it isn't clear.  One
4204                         plausible explanation is that it is to work around
4205                         a problem we have with linker scripts using data
4206                         statements in NOLOAD sections.  I don't think it
4207                         makes a great deal of sense to have such a section
4208                         assigned to a PT_LOAD segment, but apparently
4209                         people do this.  The data statement results in a
4210                         bfd_data_link_order being built, and these need
4211                         section contents to write into.  Eventually, we get
4212                         to _bfd_elf_write_object_contents which writes any
4213                         section with contents to the output.  Make room
4214                         here for the write, so that following segments are
4215                         not trashed.  */
4216                      if ((flags & SEC_LOAD) != 0
4217                          || (flags & SEC_HAS_CONTENTS) != 0)
4218                        off += sec->size;
4219                    }
4220    
4221                if ((flags & SEC_LOAD) != 0)                if ((flags & SEC_LOAD) != 0)
4222                  p->p_filesz += sec->_raw_size;                  {
4223                      p->p_filesz += sec->size;
4224                      p->p_memsz += sec->size;
4225                    }
4226                  /* PR ld/594:  Sections in note segments which are not loaded
4227                     contribute to the file size but not the in-memory size.  */
4228                  else if (p->p_type == PT_NOTE
4229                      && (flags & SEC_HAS_CONTENTS) != 0)
4230                    p->p_filesz += sec->size;
4231    
4232                  /* .tbss is special.  It doesn't contribute to p_memsz of
4233                     normal segments.  */
4234                  else if ((flags & SEC_THREAD_LOCAL) == 0
4235                           || p->p_type == PT_TLS)
4236                    p->p_memsz += sec->size;
4237    
4238                if (p->p_type == PT_TLS                if (p->p_type == PT_TLS
4239                    && sec->_raw_size == 0                    && sec->size == 0
4240                    && (sec->flags & SEC_HAS_CONTENTS) == 0)                    && (sec->flags & SEC_HAS_CONTENTS) == 0)
4241                  {                  {
4242                    struct bfd_link_order *o;                    struct bfd_link_order *o;
# Line 3925  Error: First section in segment (%s) sta Line 4274  Error: First section in segment (%s) sta
4274        if (p->p_type != PT_LOAD && m->count > 0)        if (p->p_type != PT_LOAD && m->count > 0)
4275          {          {
4276            BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);            BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4277              /* If the section has not yet been assigned a file position,
4278                 do so now.  The ARM BPABI requires that .dynamic section
4279                 not be marked SEC_ALLOC because it is not part of any
4280                 PT_LOAD segment, so it will not be processed above.  */
4281              if (p->p_type == PT_DYNAMIC && m->sections[0]->filepos == 0)
4282                {
4283                  unsigned int i;
4284                  Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4285    
4286                  i = 1;
4287                  while (i_shdrpp[i]->bfd_section != m->sections[0])
4288                    ++i;
4289                  off = (_bfd_elf_assign_file_position_for_section
4290                         (i_shdrpp[i], off, TRUE));
4291                  p->p_filesz = m->sections[0]->size;
4292                }
4293            p->p_offset = m->sections[0]->filepos;            p->p_offset = m->sections[0]->filepos;
4294          }          }
4295        if (m->count == 0)        if (m->count == 0)
# Line 3941  Error: First section in segment (%s) sta Line 4306  Error: First section in segment (%s) sta
4306                if (! m->p_paddr_valid)                if (! m->p_paddr_valid)
4307                  p->p_paddr = phdrs_paddr;                  p->p_paddr = phdrs_paddr;
4308              }              }
4309              else if (p->p_type == PT_GNU_RELRO)
4310                {
4311                  Elf_Internal_Phdr *lp;
4312    
4313                  for (lp = phdrs; lp < phdrs + count; ++lp)
4314                    {
4315                      if (lp->p_type == PT_LOAD
4316                          && lp->p_vaddr <= link_info->relro_end
4317                          && lp->p_vaddr >= link_info->relro_start
4318                          && lp->p_vaddr + lp->p_filesz
4319                             >= link_info->relro_end)
4320                        break;
4321                    }
4322    
4323                  if (lp < phdrs + count
4324                      && link_info->relro_end > lp->p_vaddr)
4325                    {
4326                      p->p_vaddr = lp->p_vaddr;
4327                      p->p_paddr = lp->p_paddr;
4328                      p->p_offset = lp->p_offset;
4329                      p->p_filesz = link_info->relro_end - lp->p_vaddr;
4330                      p->p_memsz = p->p_filesz;
4331                      p->p_align = 1;
4332                      p->p_flags = (lp->p_flags & ~PF_W);
4333                    }
4334                  else
4335                    {
4336                      memset (p, 0, sizeof *p);
4337                      p->p_type = PT_NULL;
4338                    }
4339                }
4340          }          }
4341      }      }
4342    
   /* If additional nonloadable filepos adjustments are required,  
      do them now. */  
   if (bed->set_nonloadable_filepos)  
     (*bed->set_nonloadable_filepos) (abfd, phdrs);  
   
4343    /* Clear out any program headers we allocated but did not use.  */    /* Clear out any program headers we allocated but did not use.  */
4344    for (; count < alloc; count++, p++)    for (; count < alloc; count++, p++)
4345      {      {
# Line 3963  Error: First section in segment (%s) sta Line 4354  Error: First section in segment (%s) sta
4354    /* Write out the program headers.  */    /* Write out the program headers.  */
4355    if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0    if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4356        || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)        || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4357      return false;      return FALSE;
4358    
4359    return true;    return TRUE;
4360  }  }
4361    
4362  /* Get the size of the program header.  /* Get the size of the program header.
# Line 3980  Error: First section in segment (%s) sta Line 4371  Error: First section in segment (%s) sta
4371     will be two segments.  */     will be two segments.  */
4372    
4373  static bfd_size_type  static bfd_size_type
4374  get_program_header_size (abfd)  get_program_header_size (bfd *abfd)
      bfd *abfd;  
4375  {  {
4376    size_t segs;    size_t segs;
4377    asection *s;    asection *s;
4378    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4379    
4380    /* We can't return a different result each time we're called.  */    /* We can't return a different result each time we're called.  */
4381    if (elf_tdata (abfd)->program_header_size != 0)    if (elf_tdata (abfd)->program_header_size != 0)
# Line 4022  get_program_header_size (abfd) Line 4412  get_program_header_size (abfd)
4412        ++segs;        ++segs;
4413      }      }
4414    
4415    if (elf_tdata (abfd)->eh_frame_hdr    if (elf_tdata (abfd)->eh_frame_hdr)
       && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)  
4416      {      {
4417        /* We need a PT_GNU_EH_FRAME segment.  */        /* We need a PT_GNU_EH_FRAME segment.  */
4418        ++segs;        ++segs;
4419      }      }
4420    
4421      if (elf_tdata (abfd)->stack_flags)
4422        {
4423          /* We need a PT_GNU_STACK segment.  */
4424          ++segs;
4425        }
4426    
4427      if (elf_tdata (abfd)->relro)
4428        {
4429          /* We need a PT_GNU_RELRO segment.  */
4430          ++segs;
4431        }
4432    
4433    for (s = abfd->sections; s != NULL; s = s->next)    for (s = abfd->sections; s != NULL; s = s->next)
4434      {      {
4435        if ((s->flags & SEC_LOAD) != 0        if ((s->flags & SEC_LOAD) != 0
# Line 4068  get_program_header_size (abfd) Line 4469  get_program_header_size (abfd)
4469     _bfd_elf_compute_section_file_positions.  All the section sizes and     _bfd_elf_compute_section_file_positions.  All the section sizes and
4470     VMAs must be known before this is called.     VMAs must be known before this is called.
4471    
4472     We do not consider reloc sections at this point, unless they form     Reloc sections come in two flavours: Those processed specially as
4473     part of the loadable image.  Reloc sections are assigned file     "side-channel" data attached to a section to which they apply, and
4474     positions in assign_file_positions_for_relocs, which is called by     those that bfd doesn't process as relocations.  The latter sort are
4475     write_object_contents and final_link.     stored in a normal bfd section by bfd_section_from_shdr.   We don't
4476       consider the former sort here, unless they form part of the loadable
4477       image.  Reloc sections not assigned here will be handled later by
4478       assign_file_positions_for_relocs.
4479    
4480     We also don't set the positions of the .symtab and .strtab here.  */     We also don't set the positions of the .symtab and .strtab here.  */
4481    
4482  static boolean  static bfd_boolean
4483  assign_file_positions_except_relocs (abfd)  assign_file_positions_except_relocs (bfd *abfd,
4484       bfd *abfd;                                       struct bfd_link_info *link_info)
4485  {  {
4486    struct elf_obj_tdata * const tdata = elf_tdata (abfd);    struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4487    Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);    Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4488    Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);    Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4489    unsigned int num_sec = elf_numsections (abfd);    unsigned int num_sec = elf_numsections (abfd);
4490    file_ptr off;    file_ptr off;
4491    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4492    
4493    if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0    if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4494        && bfd_get_format (abfd) != bfd_core)        && bfd_get_format (abfd) != bfd_core)
# Line 4103  assign_file_positions_except_relocs (abf Line 4507  assign_file_positions_except_relocs (abf
4507            Elf_Internal_Shdr *hdr;            Elf_Internal_Shdr *hdr;
4508    
4509            hdr = *hdrpp;            hdr = *hdrpp;
4510            if (hdr->sh_type == SHT_REL            if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4511                || hdr->sh_type == SHT_RELA                 && hdr->bfd_section == NULL)
4512                || i == tdata->symtab_section                || i == tdata->symtab_section
4513                || i == tdata->symtab_shndx_section                || i == tdata->symtab_shndx_section
4514                || i == tdata->strtab_section)                || i == tdata->strtab_section)
# Line 4112  assign_file_positions_except_relocs (abf Line 4516  assign_file_positions_except_relocs (abf
4516                hdr->sh_offset = -1;                hdr->sh_offset = -1;
4517              }              }
4518            else            else
4519              off = _bfd_elf_assign_file_position_for_section (hdr, off, true);              off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4520    
4521            if (i == SHN_LORESERVE - 1)            if (i == SHN_LORESERVE - 1)
4522              {              {
# Line 4128  assign_file_positions_except_relocs (abf Line 4532  assign_file_positions_except_relocs (abf
4532    
4533        /* Assign file positions for the loaded sections based on the        /* Assign file positions for the loaded sections based on the
4534           assignment of sections to segments.  */           assignment of sections to segments.  */
4535        if (! assign_file_positions_for_segments (abfd))        if (! assign_file_positions_for_segments (abfd, link_info))
4536          return false;          return FALSE;
4537    
4538        /* Assign file positions for the other sections.  */        /* Assign file positions for the other sections.  */
4539    
# Line 4145  assign_file_positions_except_relocs (abf Line 4549  assign_file_positions_except_relocs (abf
4549            else if ((hdr->sh_flags & SHF_ALLOC) != 0)            else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4550              {              {
4551                ((*_bfd_error_handler)                ((*_bfd_error_handler)
4552                 (_("%s: warning: allocated section `%s' not in segment"),                 (_("%B: warning: allocated section `%s' not in segment"),
4553                  bfd_get_filename (abfd),                  abfd,
4554                  (hdr->bfd_section == NULL                  (hdr->bfd_section == NULL
4555                   ? "*unknown*"                   ? "*unknown*"
4556                   : hdr->bfd_section->name)));                   : hdr->bfd_section->name)));
4557                if ((abfd->flags & D_PAGED) != 0)                if ((abfd->flags & D_PAGED) != 0)
4558                  off += (hdr->sh_addr - off) % bed->maxpagesize;                  off += vma_page_aligned_bias (hdr->sh_addr, off,
4559                                                  bed->maxpagesize);
4560                else                else
4561                  off += (hdr->sh_addr - off) % hdr->sh_addralign;                  off += vma_page_aligned_bias (hdr->sh_addr, off,
4562                                                  hdr->sh_addralign);
4563                off = _bfd_elf_assign_file_position_for_section (hdr, off,                off = _bfd_elf_assign_file_position_for_section (hdr, off,
4564                                                                 false);                                                                 FALSE);
4565              }              }
4566            else if (hdr->sh_type == SHT_REL            else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4567                     || hdr->sh_type == SHT_RELA                      && hdr->bfd_section == NULL)
4568                     || hdr == i_shdrpp[tdata->symtab_section]                     || hdr == i_shdrpp[tdata->symtab_section]
4569                     || hdr == i_shdrpp[tdata->symtab_shndx_section]                     || hdr == i_shdrpp[tdata->symtab_shndx_section]
4570                     || hdr == i_shdrpp[tdata->strtab_section])                     || hdr == i_shdrpp[tdata->strtab_section])
4571              hdr->sh_offset = -1;              hdr->sh_offset = -1;
4572            else            else
4573              off = _bfd_elf_assign_file_position_for_section (hdr, off, true);              off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4574    
4575            if (i == SHN_LORESERVE - 1)            if (i == SHN_LORESERVE - 1)
4576              {              {
# Line 4175  assign_file_positions_except_relocs (abf Line 4581  assign_file_positions_except_relocs (abf
4581      }      }
4582    
4583    /* Place the section headers.  */    /* Place the section headers.  */
4584    off = align_file_position (off, bed->s->file_align);    off = align_file_position (off, 1 << bed->s->log_file_align);
4585    i_ehdrp->e_shoff = off;    i_ehdrp->e_shoff = off;
4586    off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;    off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4587    
4588    elf_tdata (abfd)->next_file_pos = off;    elf_tdata (abfd)->next_file_pos = off;
4589    
4590    return true;    return TRUE;
4591  }  }
4592    
4593  static boolean  static bfd_boolean
4594  prep_headers (abfd)  prep_headers (bfd *abfd)
      bfd *abfd;  
4595  {  {
4596    Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */    Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
4597    Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */    Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4598    Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */    Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
4599    struct elf_strtab_hash *shstrtab;    struct elf_strtab_hash *shstrtab;
4600    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4601    
4602    i_ehdrp = elf_elfheader (abfd);    i_ehdrp = elf_elfheader (abfd);
4603    i_shdrp = elf_elfsections (abfd);    i_shdrp = elf_elfsections (abfd);
4604    
4605    shstrtab = _bfd_elf_strtab_init ();    shstrtab = _bfd_elf_strtab_init ();
4606    if (shstrtab == NULL)    if (shstrtab == NULL)
4607      return false;      return FALSE;
4608    
4609    elf_shstrtab (abfd) = shstrtab;    elf_shstrtab (abfd) = shstrtab;
4610    
# Line 4237  prep_headers (abfd) Line 4642  prep_headers (abfd)
4642           Such need can generally be supplied by replacing the tests for           Such need can generally be supplied by replacing the tests for
4643           e_machine with the conditions used to determine it.  */           e_machine with the conditions used to determine it.  */
4644      default:      default:
4645        if (get_elf_backend_data (abfd) != NULL)        i_ehdrp->e_machine = bed->elf_machine_code;
4646          i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;      }
       else  
         i_ehdrp->e_machine = EM_NONE;  
       }  
4647    
4648    i_ehdrp->e_version = bed->s->ev_current;    i_ehdrp->e_version = bed->s->ev_current;
4649    i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;    i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
# Line 4257  prep_headers (abfd) Line 4659  prep_headers (abfd)
4659    
4660    /* If we're building an executable, we'll need a program header table.  */    /* If we're building an executable, we'll need a program header table.  */
4661    if (abfd->flags & EXEC_P)    if (abfd->flags & EXEC_P)
4662      {      /* It all happens later.  */
4663        /* It all happens later.  */      ;
 #if 0  
       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);  
   
       /* elf_build_phdrs() returns a (NULL-terminated) array of  
          Elf_Internal_Phdrs.  */  
       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);  
       i_ehdrp->e_phoff = outbase;  
       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;  
 #endif  
     }  
4664    else    else
4665      {      {
4666        i_ehdrp->e_phentsize = 0;        i_ehdrp->e_phentsize = 0;
# Line 4277  prep_headers (abfd) Line 4669  prep_headers (abfd)
4669      }      }
4670    
4671    elf_tdata (abfd)->symtab_hdr.sh_name =    elf_tdata (abfd)->symtab_hdr.sh_name =
4672      (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);      (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4673    elf_tdata (abfd)->strtab_hdr.sh_name =    elf_tdata (abfd)->strtab_hdr.sh_name =
4674      (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);      (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4675    elf_tdata (abfd)->shstrtab_hdr.sh_name =    elf_tdata (abfd)->shstrtab_hdr.sh_name =
4676      (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);      (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4677    if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1    if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4678        || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1        || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4679        || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)        || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4680      return false;      return FALSE;
4681    
4682    return true;    return TRUE;
4683  }  }
4684    
4685  /* Assign file positions for all the reloc sections which are not part  /* Assign file positions for all the reloc sections which are not part
4686     of the loadable file image.  */     of the loadable file image.  */
4687    
4688  void  void
4689  _bfd_elf_assign_file_positions_for_relocs (abfd)  _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
      bfd *abfd;  
4690  {  {
4691    file_ptr off;    file_ptr off;
4692    unsigned int i, num_sec;    unsigned int i, num_sec;
# Line 4311  _bfd_elf_assign_file_positions_for_reloc Line 4702  _bfd_elf_assign_file_positions_for_reloc
4702        shdrp = *shdrpp;        shdrp = *shdrpp;
4703        if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)        if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4704            && shdrp->sh_offset == -1)            && shdrp->sh_offset == -1)
4705          off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);          off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4706      }      }
4707    
4708    elf_tdata (abfd)->next_file_pos = off;    elf_tdata (abfd)->next_file_pos = off;
4709  }  }
4710    
4711  boolean  bfd_boolean
4712  _bfd_elf_write_object_contents (abfd)  _bfd_elf_write_object_contents (bfd *abfd)
      bfd *abfd;  
4713  {  {
4714    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4715    Elf_Internal_Ehdr *i_ehdrp;    Elf_Internal_Ehdr *i_ehdrp;
4716    Elf_Internal_Shdr **i_shdrp;    Elf_Internal_Shdr **i_shdrp;
4717    boolean failed;    bfd_boolean failed;
4718    unsigned int count, num_sec;    unsigned int count, num_sec;
4719    
4720    if (! abfd->output_has_begun    if (! abfd->output_has_begun
4721        && ! _bfd_elf_compute_section_file_positions        && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4722               (abfd, (struct bfd_link_info *) NULL))      return FALSE;
     return false;  
4723    
4724    i_shdrp = elf_elfsections (abfd);    i_shdrp = elf_elfsections (abfd);
4725    i_ehdrp = elf_elfheader (abfd);    i_ehdrp = elf_elfheader (abfd);
4726    
4727    failed = false;    failed = FALSE;
4728    bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);    bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4729    if (failed)    if (failed)
4730      return false;      return FALSE;
4731    
4732    _bfd_elf_assign_file_positions_for_relocs (abfd);    _bfd_elf_assign_file_positions_for_relocs (abfd);
4733    
# Line 4354  _bfd_elf_write_object_contents (abfd) Line 4743  _bfd_elf_write_object_contents (abfd)
4743    
4744            if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0            if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4745                || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)                || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4746              return false;              return FALSE;
4747          }          }
4748        if (count == SHN_LORESERVE - 1)        if (count == SHN_LORESERVE - 1)
4749          count += SHN_HIRESERVE + 1 - SHN_LORESERVE;          count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
# Line 4363  _bfd_elf_write_object_contents (abfd) Line 4752  _bfd_elf_write_object_contents (abfd)
4752    /* Write out the section header names.  */    /* Write out the section header names.  */
4753    if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0    if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4754        || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))        || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4755      return false;      return FALSE;
4756    
4757    if (bed->elf_backend_final_write_processing)    if (bed->elf_backend_final_write_processing)
4758      (*bed->elf_backend_final_write_processing) (abfd,      (*bed->elf_backend_final_write_processing) (abfd,
# Line 4372  _bfd_elf_write_object_contents (abfd) Line 4761  _bfd_elf_write_object_contents (abfd)
4761    return bed->s->write_shdrs_and_ehdr (abfd);    return bed->s->write_shdrs_and_ehdr (abfd);
4762  }  }
4763    
4764  boolean  bfd_boolean
4765  _bfd_elf_write_corefile_contents (abfd)  _bfd_elf_write_corefile_contents (bfd *abfd)
      bfd *abfd;  
4766  {  {
4767    /* Hopefully this can be done just like an object file.  */    /* Hopefully this can be done just like an object file.  */
4768    return _bfd_elf_write_object_contents (abfd);    return _bfd_elf_write_object_contents (abfd);
# Line 4383  _bfd_elf_write_corefile_contents (abfd) Line 4771  _bfd_elf_write_corefile_contents (abfd)
4771  /* Given a section, search the header to find them.  */  /* Given a section, search the header to find them.  */
4772    
4773  int  int
4774  _bfd_elf_section_from_bfd_section (abfd, asect)  _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
      bfd *abfd;  
      struct sec *asect;  
4775  {  {
4776    struct elf_backend_data *bed;    const struct elf_backend_data *bed;
4777    int index;    int index;
4778    
4779    if (elf_section_data (asect) != NULL    if (elf_section_data (asect) != NULL
# Line 4434  _bfd_elf_section_from_bfd_section (abfd, Line 4820  _bfd_elf_section_from_bfd_section (abfd,
4820     on error.  */     on error.  */
4821    
4822  int  int
4823  _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)  _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
      bfd *abfd;  
      asymbol **asym_ptr_ptr;  
4824  {  {
4825    asymbol *asym_ptr = *asym_ptr_ptr;    asymbol *asym_ptr = *asym_ptr_ptr;
4826    int idx;    int idx;
# Line 4469  _bfd_elf_symbol_from_bfd_symbol (abfd, a Line 4853  _bfd_elf_symbol_from_bfd_symbol (abfd, a
4853        /* This case can occur when using --strip-symbol on a symbol        /* This case can occur when using --strip-symbol on a symbol
4854           which is used in a relocation entry.  */           which is used in a relocation entry.  */
4855        (*_bfd_error_handler)        (*_bfd_error_handler)
4856          (_("%s: symbol `%s' required but not present"),          (_("%B: symbol `%s' required but not present"),
4857           bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));           abfd, bfd_asymbol_name (asym_ptr));
4858        bfd_set_error (bfd_error_no_symbols);        bfd_set_error (bfd_error_no_symbols);
4859        return -1;        return -1;
4860      }      }
# Line 4490  _bfd_elf_symbol_from_bfd_symbol (abfd, a Line 4874  _bfd_elf_symbol_from_bfd_symbol (abfd, a
4874    
4875  /* Copy private BFD data.  This copies any program header information.  */  /* Copy private BFD data.  This copies any program header information.  */
4876    
4877  static boolean  static bfd_boolean
4878  copy_private_bfd_data (ibfd, obfd)  copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4879       bfd *ibfd;  {
4880       bfd *obfd;    Elf_Internal_Ehdr *iehdr;
4881  {    struct elf_segment_map *map;
4882    Elf_Internal_Ehdr *       iehdr;    struct elf_segment_map *map_first;
4883    struct elf_segment_map *  map;    struct elf_segment_map **pointer_to_map;
4884    struct elf_segment_map *  map_first;    Elf_Internal_Phdr *segment;
4885    struct elf_segment_map ** pointer_to_map;    asection *section;
4886    Elf_Internal_Phdr *       segment;    unsigned int i;
4887    asection *                section;    unsigned int num_segments;
4888    unsigned int              i;    bfd_boolean phdr_included = FALSE;
4889    unsigned int              num_segments;    bfd_vma maxpagesize;
4890    boolean                   phdr_included = false;    struct elf_segment_map *phdr_adjust_seg = NULL;
4891    bfd_vma                   maxpagesize;    unsigned int phdr_adjust_num = 0;
4892    struct elf_segment_map *  phdr_adjust_seg = NULL;    const struct elf_backend_data *bed;
   unsigned int              phdr_adjust_num = 0;  
   struct elf_backend_data * bed;  
4893    
4894    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4895        || bfd_get_flavour (obfd) != bfd_target_elf_flavour)        || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4896      return true;      return TRUE;
4897    
4898    if (elf_tdata (ibfd)->phdr == NULL)    if (elf_tdata (ibfd)->phdr == NULL)
4899      return true;      return TRUE;
4900    
4901    bed = get_elf_backend_data (ibfd);    bed = get_elf_backend_data (ibfd);
4902    iehdr = elf_elfheader (ibfd);    iehdr = elf_elfheader (ibfd);
# Line 4530  copy_private_bfd_data (ibfd, obfd) Line 4912  copy_private_bfd_data (ibfd, obfd)
4912    (start + (segment->p_memsz > segment->p_filesz                        \    (start + (segment->p_memsz > segment->p_filesz                        \
4913              ? segment->p_memsz : segment->p_filesz))              ? segment->p_memsz : segment->p_filesz))
4914    
4915    /* Returns true if the given section is contained within  #define SECTION_SIZE(section, segment)                                  \
4916      (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))            \
4917        != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)                   \
4918       ? section->size : 0)
4919    
4920      /* Returns TRUE if the given section is contained within
4921       the given segment.  VMA addresses are compared.  */       the given segment.  VMA addresses are compared.  */
4922  #define IS_CONTAINED_BY_VMA(section, segment)                           \  #define IS_CONTAINED_BY_VMA(section, segment)                           \
4923    (section->vma >= segment->p_vaddr                                     \    (section->vma >= segment->p_vaddr                                     \
4924     && (section->vma + section->_raw_size                                \     && (section->vma + SECTION_SIZE (section, segment)                   \
4925         <= (SEGMENT_END (segment, segment->p_vaddr))))         <= (SEGMENT_END (segment, segment->p_vaddr))))
4926    
4927    /* Returns true if the given section is contained within    /* Returns TRUE if the given section is contained within
4928       the given segment.  LMA addresses are compared.  */       the given segment.  LMA addresses are compared.  */
4929  #define IS_CONTAINED_BY_LMA(section, segment, base)                     \  #define IS_CONTAINED_BY_LMA(section, segment, base)                     \
4930    (section->lma >= base                                                 \    (section->lma >= base                                                 \
4931     && (section->lma + section->_raw_size                                \     && (section->lma + SECTION_SIZE (section, segment)                   \
4932         <= SEGMENT_END (segment, base)))         <= SEGMENT_END (segment, base)))
4933    
   /* Returns true if the given section is contained within the  
      given segment.  Filepos addresses are compared in an elf  
      backend function. */  
 #define IS_CONTAINED_BY_FILEPOS(sec, seg, bed)                          \  
   (bed->is_contained_by_filepos                                         \  
    && (*bed->is_contained_by_filepos) (sec, seg))  
   
4934    /* Special case: corefile "NOTE" section containing regs, prpsinfo etc.  */    /* Special case: corefile "NOTE" section containing regs, prpsinfo etc.  */
4935  #define IS_COREFILE_NOTE(p, s)                                          \  #define IS_COREFILE_NOTE(p, s)                                          \
4936    (p->p_type == PT_NOTE                                                 \    (p->p_type == PT_NOTE                                                 \
4937     && bfd_get_format (ibfd) == bfd_core                                 \     && bfd_get_format (ibfd) == bfd_core                                 \
4938     && s->vma == 0 && s->lma == 0                                        \     && s->vma == 0 && s->lma == 0                                        \
4939     && (bfd_vma) s->filepos >= p->p_offset                               \     && (bfd_vma) s->filepos >= p->p_offset                               \
4940     && ((bfd_vma) s->filepos + s->_raw_size                              \     && ((bfd_vma) s->filepos + s->size                           \
4941         <= p->p_offset + p->p_filesz))         <= p->p_offset + p->p_filesz))
4942    
4943    /* The complicated case when p_vaddr is 0 is to handle the Solaris    /* The complicated case when p_vaddr is 0 is to handle the Solaris
# Line 4569  copy_private_bfd_data (ibfd, obfd) Line 4949  copy_private_bfd_data (ibfd, obfd)
4949     && p->p_memsz == 0                                                   \     && p->p_memsz == 0                                                   \
4950     && p->p_filesz > 0                                                   \     && p->p_filesz > 0                                                   \
4951     && (s->flags & SEC_HAS_CONTENTS) != 0                                \     && (s->flags & SEC_HAS_CONTENTS) != 0                                \
4952     && s->_raw_size > 0                                                  \     && s->size > 0                                                       \
4953     && (bfd_vma) s->filepos >= p->p_offset                               \     && (bfd_vma) s->filepos >= p->p_offset                               \
4954     && ((bfd_vma) s->filepos + s->_raw_size                              \     && ((bfd_vma) s->filepos + s->size                           \
4955         <= p->p_offset + p->p_filesz))         <= p->p_offset + p->p_filesz))
4956    
4957    /* Decide if the given section should be included in the given segment.    /* Decide if the given section should be included in the given segment.
# Line 4580  copy_private_bfd_data (ibfd, obfd) Line 4960  copy_private_bfd_data (ibfd, obfd)
4960            if that is set for the segment and the VMA otherwise,            if that is set for the segment and the VMA otherwise,
4961         2. It is an allocated segment,         2. It is an allocated segment,
4962         3. There is an output section associated with it,         3. There is an output section associated with it,
4963         4. The section has not already been allocated to a previous segment.  */         4. The section has not already been allocated to a previous segment.
4964           5. PT_GNU_STACK segments do not include any sections.
4965           6. PT_TLS segment includes only SHF_TLS sections.
4966           7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4967           8. PT_DYNAMIC should not contain empty sections at the beginning
4968              (with the possible exception of .dynamic).  */
4969  #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)               \  #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)               \
4970    ((((segment->p_paddr                                                  \    ((((segment->p_paddr                                                  \
4971        ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)        \        ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)        \
4972        : IS_CONTAINED_BY_VMA (section, segment))                         \        : IS_CONTAINED_BY_VMA (section, segment))                         \
4973       && (section->flags & SEC_ALLOC) != 0)                              \       && (section->flags & SEC_ALLOC) != 0)                              \
4974      || IS_COREFILE_NOTE (segment, section)                              \      || IS_COREFILE_NOTE (segment, section))                             \
     || (IS_CONTAINED_BY_FILEPOS (section, segment, bed)                 \  
         && (section->flags & SEC_ALLOC) == 0))                          \  
4975     && section->output_section != NULL                                   \     && section->output_section != NULL                                   \
4976       && segment->p_type != PT_GNU_STACK                                   \
4977       && (segment->p_type != PT_TLS                                        \
4978           || (section->flags & SEC_THREAD_LOCAL))                          \
4979       && (segment->p_type == PT_LOAD                                       \
4980           || segment->p_type == PT_TLS                                     \
4981           || (section->flags & SEC_THREAD_LOCAL) == 0)                     \
4982       && (segment->p_type != PT_DYNAMIC                                    \
4983           || SECTION_SIZE (section, segment) > 0                           \
4984           || (segment->p_paddr                                             \
4985               ? segment->p_paddr != section->lma                           \
4986               : segment->p_vaddr != section->vma)                          \
4987           || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")    \
4988               == 0))                                                       \
4989     && ! section->segment_mark)     && ! section->segment_mark)
4990    
4991    /* Returns true iff seg1 starts after the end of seg2.  */    /* Returns TRUE iff seg1 starts after the end of seg2.  */
4992  #define SEGMENT_AFTER_SEGMENT(seg1, seg2)                               \  #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)                        \
4993    (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))    (seg1->field >= SEGMENT_END (seg2, seg2->field))
4994    
4995    /* Returns true iff seg1 and seg2 overlap.  */    /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4996         their VMA address ranges and their LMA address ranges overlap.
4997         It is possible to have overlapping VMA ranges without overlapping LMA
4998         ranges.  RedBoot images for example can have both .data and .bss mapped
4999         to the same VMA range, but with the .data section mapped to a different
5000         LMA.  */
5001  #define SEGMENT_OVERLAPS(seg1, seg2)                                    \  #define SEGMENT_OVERLAPS(seg1, seg2)                                    \
5002    (!(SEGMENT_AFTER_SEGMENT (seg1, seg2)                                 \    (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)                     \
5003       || SEGMENT_AFTER_SEGMENT (seg2, seg1)))          || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))                 \
5004       && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)                     \
5005            || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5006    
5007    /* Initialise the segment mark field.  */    /* Initialise the segment mark field.  */
5008    for (section = ibfd->sections; section != NULL; section = section->next)    for (section = ibfd->sections; section != NULL; section = section->next)
5009      section->segment_mark = false;      section->segment_mark = FALSE;
5010    
5011    /* Scan through the segments specified in the program header    /* Scan through the segments specified in the program header
5012       of the input BFD.  For this first scan we look for overlaps       of the input BFD.  For this first scan we look for overlaps
# Line 4621  copy_private_bfd_data (ibfd, obfd) Line 5024  copy_private_bfd_data (ibfd, obfd)
5024            if (IS_SOLARIS_PT_INTERP (segment, section))            if (IS_SOLARIS_PT_INTERP (segment, section))
5025              {              {
5026                /* Mininal change so that the normal section to segment                /* Mininal change so that the normal section to segment
5027                   assigment code will work.  */                   assignment code will work.  */
5028                segment->p_vaddr = section->vma;                segment->p_vaddr = section->vma;
5029                break;                break;
5030              }              }
# Line 4697  copy_private_bfd_data (ibfd, obfd) Line 5100  copy_private_bfd_data (ibfd, obfd)
5100          continue;          continue;
5101    
5102        /* Compute how many sections might be placed into this segment.  */        /* Compute how many sections might be placed into this segment.  */
5103        section_count = 0;        for (section = ibfd->sections, section_count = 0;
5104        for (section = ibfd->sections; section != NULL; section = section->next)             section != NULL;
5105               section = section->next)
5106          if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))          if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5107            ++section_count;            ++section_count;
5108    
5109        /* Allocate a segment map big enough to contain all of the        /* Allocate a segment map big enough to contain
5110           sections we have selected.  */           all of the sections we have selected.  */
5111        amt = sizeof (struct elf_segment_map);        amt = sizeof (struct elf_segment_map);
5112        amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);        amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5113        map = (struct elf_segment_map *) bfd_alloc (obfd, amt);        map = bfd_alloc (obfd, amt);
5114        if (map == NULL)        if (map == NULL)
5115          return false;          return FALSE;
5116    
5117        /* Initialise the fields of the segment map.  Default to        /* Initialise the fields of the segment map.  Default to
5118           using the physical address of the segment in the input BFD.  */           using the physical address of the segment in the input BFD.  */
# Line 4735  copy_private_bfd_data (ibfd, obfd) Line 5139  copy_private_bfd_data (ibfd, obfd)
5139                       + iehdr->e_phnum * iehdr->e_phentsize)));                       + iehdr->e_phnum * iehdr->e_phentsize)));
5140    
5141            if (segment->p_type == PT_LOAD && map->includes_phdrs)            if (segment->p_type == PT_LOAD && map->includes_phdrs)
5142              phdr_included = true;              phdr_included = TRUE;
5143          }          }
5144    
5145        if (section_count == 0)        if (section_count == 0)
# Line 4746  copy_private_bfd_data (ibfd, obfd) Line 5150  copy_private_bfd_data (ibfd, obfd)
5150               a warning is produced.  */               a warning is produced.  */
5151            if (segment->p_type == PT_LOAD)            if (segment->p_type == PT_LOAD)
5152              (*_bfd_error_handler)              (*_bfd_error_handler)
5153                (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),                (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5154                 bfd_archive_filename (ibfd));                 ibfd);
5155    
5156            map->count = 0;            map->count = 0;
5157            *pointer_to_map = map;            *pointer_to_map = map;
# Line 4775  copy_private_bfd_data (ibfd, obfd) Line 5179  copy_private_bfd_data (ibfd, obfd)
5179              and possibly its LMA changed, and a new segment or segments will              and possibly its LMA changed, and a new segment or segments will
5180              have to be created to contain the other sections.              have to be created to contain the other sections.
5181    
5182           4. The sections have been moved, but not be the same amount.           4. The sections have been moved, but not by the same amount.
5183              In this case we can change the segment's LMA to match the LMA              In this case we can change the segment's LMA to match the LMA
5184              of the first section and we will have to create a new segment              of the first section and we will have to create a new segment
5185              or segments to contain the other sections.              or segments to contain the other sections.
# Line 4787  copy_private_bfd_data (ibfd, obfd) Line 5191  copy_private_bfd_data (ibfd, obfd)
5191        /* Gcc 2.96 miscompiles this code on mips. Don't do casting here        /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5192           to work around this long long bug.  */           to work around this long long bug.  */
5193        amt = section_count * sizeof (asection *);        amt = section_count * sizeof (asection *);
5194        sections = (asection **) bfd_malloc (amt);        sections = bfd_malloc (amt);
5195        if (sections == NULL)        if (sections == NULL)
5196          return false;          return FALSE;
5197    
5198        /* Step One: Scan for segment vs section LMA conflicts.        /* Step One: Scan for segment vs section LMA conflicts.
5199           Also add the sections to the section array allocated above.           Also add the sections to the section array allocated above.
# Line 4813  copy_private_bfd_data (ibfd, obfd) Line 5217  copy_private_bfd_data (ibfd, obfd)
5217    
5218                /* The Solaris native linker always sets p_paddr to 0.                /* The Solaris native linker always sets p_paddr to 0.
5219                   We try to catch that case here, and set it to the                   We try to catch that case here, and set it to the
5220                   correct value.  */                   correct value.  Note - some backends require that
5221                     p_paddr be left as zero.  */
5222                if (segment->p_paddr == 0                if (segment->p_paddr == 0
5223                    && segment->p_vaddr != 0                    && segment->p_vaddr != 0
5224                      && (! bed->want_p_paddr_set_to_zero)
5225                    && isec == 0                    && isec == 0
5226                    && output_section->lma != 0                    && output_section->lma != 0
5227                    && (output_section->vma == (segment->p_vaddr                    && (output_section->vma == (segment->p_vaddr
# Line 4831  copy_private_bfd_data (ibfd, obfd) Line 5237  copy_private_bfd_data (ibfd, obfd)
5237                /* Match up the physical address of the segment with the                /* Match up the physical address of the segment with the
5238                   LMA address of the output section.  */                   LMA address of the output section.  */
5239                if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)                if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5240                    || IS_CONTAINED_BY_FILEPOS (section, segment, bed)                    || IS_COREFILE_NOTE (segment, section)
5241                    || IS_COREFILE_NOTE (segment, section))                    || (bed->want_p_paddr_set_to_zero &&
5242                          IS_CONTAINED_BY_VMA (output_section, segment))
5243                    )
5244                  {                  {
5245                    if (matching_lma == 0)                    if (matching_lma == 0)
5246                      matching_lma = output_section->lma;                      matching_lma = output_section->lma;
# Line 4950  copy_private_bfd_data (ibfd, obfd) Line 5358  copy_private_bfd_data (ibfd, obfd)
5358                        /* If the gap between the end of the previous section                        /* If the gap between the end of the previous section
5359                           and the start of this section is more than                           and the start of this section is more than
5360                           maxpagesize then we need to start a new segment.  */                           maxpagesize then we need to start a new segment.  */
5361                        if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,                        if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5362                                        maxpagesize)                                        maxpagesize)
5363                             < BFD_ALIGN (output_section->lma, maxpagesize))                             < BFD_ALIGN (output_section->lma, maxpagesize))
5364                            || ((prev_sec->lma + prev_sec->_raw_size)                            || ((prev_sec->lma + prev_sec->size)
5365                                > output_section->lma))                                > output_section->lma))
5366                          {                          {
5367                            if (suggested_lma == 0)                            if (suggested_lma == 0)
# Line 4966  copy_private_bfd_data (ibfd, obfd) Line 5374  copy_private_bfd_data (ibfd, obfd)
5374                    map->sections[map->count++] = output_section;                    map->sections[map->count++] = output_section;
5375                    ++isec;                    ++isec;
5376                    sections[j] = NULL;                    sections[j] = NULL;
5377                    section->segment_mark = true;                    section->segment_mark = TRUE;
5378                  }                  }
5379                else if (suggested_lma == 0)                else if (suggested_lma == 0)
5380                  suggested_lma = output_section->lma;                  suggested_lma = output_section->lma;
# Line 4985  copy_private_bfd_data (ibfd, obfd) Line 5393  copy_private_bfd_data (ibfd, obfd)
5393                   and carry on looping.  */                   and carry on looping.  */
5394                amt = sizeof (struct elf_segment_map);                amt = sizeof (struct elf_segment_map);
5395                amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);                amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5396                map = (struct elf_segment_map *) bfd_alloc (obfd, amt);                map = bfd_alloc (obfd, amt);
5397                if (map == NULL)                if (map == NULL)
5398                  return false;                  {
5399                      free (sections);
5400                      return FALSE;
5401                    }
5402    
5403                /* Initialise the fields of the segment map.  Set the physical                /* Initialise the fields of the segment map.  Set the physical
5404                   physical address to the LMA of the first section that has                   physical address to the LMA of the first section that has
# Line 5015  copy_private_bfd_data (ibfd, obfd) Line 5426  copy_private_bfd_data (ibfd, obfd)
5426      if (map->p_paddr != 0)      if (map->p_paddr != 0)
5427        break;        break;
5428    if (map == NULL)    if (map == NULL)
5429      {      for (map = map_first; map != NULL; map = map->next)
5430        for (map = map_first; map != NULL; map = map->next)        map->p_paddr_valid = 0;
         map->p_paddr_valid = 0;  
     }  
5431    
5432    elf_tdata (obfd)->segment_map = map_first;    elf_tdata (obfd)->segment_map = map_first;
5433    
# Line 5037  copy_private_bfd_data (ibfd, obfd) Line 5446  copy_private_bfd_data (ibfd, obfd)
5446            -= (count - phdr_adjust_num) * iehdr->e_phentsize;            -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5447      }      }
5448    
 #if 0  
   /* Final Step: Sort the segments into ascending order of physical  
      address.  */  
   if (map_first != NULL)  
     {  
       struct elf_segment_map *prev;  
   
       prev = map_first;  
       for (map = map_first->next; map != NULL; prev = map, map = map->next)  
         {  
           /* Yes I know - its a bubble sort....  */  
           if (map->next != NULL && (map->next->p_paddr < map->p_paddr))  
             {  
               /* Swap map and map->next.  */  
               prev->next = map->next;  
               map->next = map->next->next;  
               prev->next->next = map;  
   
               /* Restart loop.  */  
               map = map_first;  
             }  
         }  
     }  
 #endif  
   
5449  #undef SEGMENT_END  #undef SEGMENT_END
5450    #undef SECTION_SIZE
5451  #undef IS_CONTAINED_BY_VMA  #undef IS_CONTAINED_BY_VMA
5452  #undef IS_CONTAINED_BY_LMA  #undef IS_CONTAINED_BY_LMA
 #undef IS_CONTAINED_BY_FILEPOS  
5453  #undef IS_COREFILE_NOTE  #undef IS_COREFILE_NOTE
5454  #undef IS_SOLARIS_PT_INTERP  #undef IS_SOLARIS_PT_INTERP
5455  #undef INCLUDE_SECTION_IN_SEGMENT  #undef INCLUDE_SECTION_IN_SEGMENT
5456  #undef SEGMENT_AFTER_SEGMENT  #undef SEGMENT_AFTER_SEGMENT
5457  #undef SEGMENT_OVERLAPS  #undef SEGMENT_OVERLAPS
5458    return true;    return TRUE;
5459  }  }
5460    
5461  /* Copy private section information.  This copies over the entsize  /* Copy private section information.  This copies over the entsize
5462     field, and sometimes the info field.  */     field, and sometimes the info field.  */
5463    
5464  boolean  bfd_boolean
5465  _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)  _bfd_elf_copy_private_section_data (bfd *ibfd,
5466       bfd *ibfd;                                      asection *isec,
5467       asection *isec;                                      bfd *obfd,
5468       bfd *obfd;                                      asection *osec)
      asection *osec;  
5469  {  {
5470    Elf_Internal_Shdr *ihdr, *ohdr;    Elf_Internal_Shdr *ihdr, *ohdr;
   const struct elf_backend_data *bed = get_elf_backend_data (ibfd);  
5471    
5472    if (ibfd->xvec->flavour != bfd_target_elf_flavour    if (ibfd->xvec->flavour != bfd_target_elf_flavour
5473        || obfd->xvec->flavour != bfd_target_elf_flavour)        || obfd->xvec->flavour != bfd_target_elf_flavour)
5474      return true;      return TRUE;
   
   /* Copy over private BFD data if it has not already been copied.  
      This must be done here, rather than in the copy_private_bfd_data  
      entry point, because the latter is called after the section  
      contents have been set, which means that the program headers have  
      already been worked out.  The backend function provides a way to  
      override the test conditions and code path for the call to  
      copy_private_bfd_data.  */  
   if (bed->copy_private_bfd_data_p)  
     {  
       if ((*bed->copy_private_bfd_data_p) (ibfd, isec, obfd, osec))  
         if (! copy_private_bfd_data (ibfd, obfd))  
           return false;  
     }  
   else if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)  
     {  
         asection *s;  
   
         /* Only set up the segments if there are no more SEC_ALLOC  
            sections.  FIXME: This won't do the right thing if objcopy is  
            used to remove the last SEC_ALLOC section, since objcopy  
            won't call this routine in that case.  */  
         for (s = isec->next; s != NULL; s = s->next)  
           if ((s->flags & SEC_ALLOC) != 0)  
             break;  
         if (s == NULL)  
           {  
             if (! copy_private_bfd_data (ibfd, obfd))  
               return false;  
           }  
     }  
5475    
5476    ihdr = &elf_section_data (isec)->this_hdr;    ihdr = &elf_section_data (isec)->this_hdr;
5477    ohdr = &elf_section_data (osec)->this_hdr;    ohdr = &elf_section_data (osec)->this_hdr;
# Line 5139  _bfd_elf_copy_private_section_data (ibfd Line 5490  _bfd_elf_copy_private_section_data (ibfd
5490    elf_next_in_group (osec) = elf_next_in_group (isec);    elf_next_in_group (osec) = elf_next_in_group (isec);
5491    elf_group_name (osec) = elf_group_name (isec);    elf_group_name (osec) = elf_group_name (isec);
5492    
5493    elf_section_data (osec)->use_rela_p    osec->use_rela_p = isec->use_rela_p;
     = elf_section_data (isec)->use_rela_p;  
5494    
5495    return true;    return TRUE;
5496    }
5497    
5498    /* Copy private header information.  */
5499    
5500    bfd_boolean
5501    _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5502    {
5503      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5504          || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5505        return TRUE;
5506    
5507      /* Copy over private BFD data if it has not already been copied.
5508         This must be done here, rather than in the copy_private_bfd_data
5509         entry point, because the latter is called after the section
5510         contents have been set, which means that the program headers have
5511         already been worked out.  */
5512      if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5513        {
5514          if (! copy_private_bfd_data (ibfd, obfd))
5515            return FALSE;
5516        }
5517    
5518      return TRUE;
5519  }  }
5520    
5521  /* Copy private symbol information.  If this symbol is in a section  /* Copy private symbol information.  If this symbol is in a section
# Line 5157  _bfd_elf_copy_private_section_data (ibfd Line 5530  _bfd_elf_copy_private_section_data (ibfd
5530  #define MAP_SHSTRTAB  (SHN_HIOS + 4)  #define MAP_SHSTRTAB  (SHN_HIOS + 4)
5531  #define MAP_SYM_SHNDX (SHN_HIOS + 5)  #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5532    
5533  boolean  bfd_boolean
5534  _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)  _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5535       bfd *ibfd;                                     asymbol *isymarg,
5536       asymbol *isymarg;                                     bfd *obfd,
5537       bfd *obfd;                                     asymbol *osymarg)
      asymbol *osymarg;  
5538  {  {
5539    elf_symbol_type *isym, *osym;    elf_symbol_type *isym, *osym;
5540    
5541    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5542        || bfd_get_flavour (obfd) != bfd_target_elf_flavour)        || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5543      return true;      return TRUE;
5544    
5545    isym = elf_symbol_from (ibfd, isymarg);    isym = elf_symbol_from (ibfd, isymarg);
5546    osym = elf_symbol_from (obfd, osymarg);    osym = elf_symbol_from (obfd, osymarg);
# Line 5193  _bfd_elf_copy_private_symbol_data (ibfd, Line 5565  _bfd_elf_copy_private_symbol_data (ibfd,
5565        osym->internal_elf_sym.st_shndx = shndx;        osym->internal_elf_sym.st_shndx = shndx;
5566      }      }
5567    
5568    return true;    return TRUE;
5569  }  }
5570    
5571  /* Swap out the symbols.  */  /* Swap out the symbols.  */
5572    
5573  static boolean  static bfd_boolean
5574  swap_out_syms (abfd, sttp, relocatable_p)  swap_out_syms (bfd *abfd,
5575       bfd *abfd;                 struct bfd_strtab_hash **sttp,
5576       struct bfd_strtab_hash **sttp;                 int relocatable_p)
      int relocatable_p;  
5577  {  {
5578    struct elf_backend_data *bed;    const struct elf_backend_data *bed;
5579    int symcount;    int symcount;
5580    asymbol **syms;    asymbol **syms;
5581    struct bfd_strtab_hash *stt;    struct bfd_strtab_hash *stt;
5582    Elf_Internal_Shdr *symtab_hdr;    Elf_Internal_Shdr *symtab_hdr;
5583    Elf_Internal_Shdr *symtab_shndx_hdr;    Elf_Internal_Shdr *symtab_shndx_hdr;
5584    Elf_Internal_Shdr *symstrtab_hdr;    Elf_Internal_Shdr *symstrtab_hdr;
5585    char *outbound_syms;    bfd_byte *outbound_syms;
5586    char *outbound_shndx;    bfd_byte *outbound_shndx;
5587    int idx;    int idx;
5588    bfd_size_type amt;    bfd_size_type amt;
5589      bfd_boolean name_local_sections;
5590    
5591    if (!elf_map_symbols (abfd))    if (!elf_map_symbols (abfd))
5592      return false;      return FALSE;
5593    
5594    /* Dump out the symtabs.  */    /* Dump out the symtabs.  */
5595    stt = _bfd_elf_stringtab_init ();    stt = _bfd_elf_stringtab_init ();
5596    if (stt == NULL)    if (stt == NULL)
5597      return false;      return FALSE;
5598    
5599    bed = get_elf_backend_data (abfd);    bed = get_elf_backend_data (abfd);
5600    symcount = bfd_get_symcount (abfd);    symcount = bfd_get_symcount (abfd);
# Line 5231  swap_out_syms (abfd, sttp, relocatable_p Line 5603  swap_out_syms (abfd, sttp, relocatable_p
5603    symtab_hdr->sh_entsize = bed->s->sizeof_sym;    symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5604    symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);    symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5605    symtab_hdr->sh_info = elf_num_locals (abfd) + 1;    symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5606    symtab_hdr->sh_addralign = bed->s->file_align;    symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5607    
5608    symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;    symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5609    symstrtab_hdr->sh_type = SHT_STRTAB;    symstrtab_hdr->sh_type = SHT_STRTAB;
# Line 5239  swap_out_syms (abfd, sttp, relocatable_p Line 5611  swap_out_syms (abfd, sttp, relocatable_p
5611    amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;    amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5612    outbound_syms = bfd_alloc (abfd, amt);    outbound_syms = bfd_alloc (abfd, amt);
5613    if (outbound_syms == NULL)    if (outbound_syms == NULL)
5614      return false;      {
5615    symtab_hdr->contents = (PTR) outbound_syms;        _bfd_stringtab_free (stt);
5616          return FALSE;
5617        }
5618      symtab_hdr->contents = outbound_syms;
5619    
5620    outbound_shndx = NULL;    outbound_shndx = NULL;
5621    symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;    symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
# Line 5249  swap_out_syms (abfd, sttp, relocatable_p Line 5624  swap_out_syms (abfd, sttp, relocatable_p
5624        amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);        amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5625        outbound_shndx = bfd_zalloc (abfd, amt);        outbound_shndx = bfd_zalloc (abfd, amt);
5626        if (outbound_shndx == NULL)        if (outbound_shndx == NULL)
5627          return false;          {
5628              _bfd_stringtab_free (stt);
5629              return FALSE;
5630            }
5631    
5632        symtab_shndx_hdr->contents = outbound_shndx;        symtab_shndx_hdr->contents = outbound_shndx;
5633        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;        symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5634        symtab_shndx_hdr->sh_size = amt;        symtab_shndx_hdr->sh_size = amt;
# Line 5257  swap_out_syms (abfd, sttp, relocatable_p Line 5636  swap_out_syms (abfd, sttp, relocatable_p
5636        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);        symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5637      }      }
5638    
5639    /* now generate the data (for "contents") */    /* Now generate the data (for "contents").  */
5640    {    {
5641      /* Fill in zeroth symbol and swap it out.  */      /* Fill in zeroth symbol and swap it out.  */
5642      Elf_Internal_Sym sym;      Elf_Internal_Sym sym;
# Line 5273  swap_out_syms (abfd, sttp, relocatable_p Line 5652  swap_out_syms (abfd, sttp, relocatable_p
5652        outbound_shndx += sizeof (Elf_External_Sym_Shndx);        outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5653    }    }
5654    
5655      name_local_sections
5656        = (bed->elf_backend_name_local_section_symbols
5657           && bed->elf_backend_name_local_section_symbols (abfd));
5658    
5659    syms = bfd_get_outsymbols (abfd);    syms = bfd_get_outsymbols (abfd);
5660    for (idx = 0; idx < symcount; idx++)    for (idx = 0; idx < symcount; idx++)
5661      {      {
# Line 5282  swap_out_syms (abfd, sttp, relocatable_p Line 5665  swap_out_syms (abfd, sttp, relocatable_p
5665        flagword flags = syms[idx]->flags;        flagword flags = syms[idx]->flags;
5666        int type;        int type;
5667    
5668        if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)        if (!name_local_sections
5669              && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5670          {          {
5671            /* Local section symbols have no name.  */            /* Local section symbols have no name.  */
5672            sym.st_name = 0;            sym.st_name = 0;
# Line 5291  swap_out_syms (abfd, sttp, relocatable_p Line 5675  swap_out_syms (abfd, sttp, relocatable_p
5675          {          {
5676            sym.st_name = (unsigned long) _bfd_stringtab_add (stt,            sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5677                                                              syms[idx]->name,                                                              syms[idx]->name,
5678                                                              true, false);                                                              TRUE, FALSE);
5679            if (sym.st_name == (unsigned long) -1)            if (sym.st_name == (unsigned long) -1)
5680              return false;              {
5681                  _bfd_stringtab_free (stt);
5682                  return FALSE;
5683                }
5684          }          }
5685    
5686        type_ptr = elf_symbol_from (abfd, syms[idx]);        type_ptr = elf_symbol_from (abfd, syms[idx]);
# Line 5323  swap_out_syms (abfd, sttp, relocatable_p Line 5710  swap_out_syms (abfd, sttp, relocatable_p
5710                value += sec->output_offset;                value += sec->output_offset;
5711                sec = sec->output_section;                sec = sec->output_section;
5712              }              }
5713    
5714            /* Don't add in the section vma for relocatable output.  */            /* Don't add in the section vma for relocatable output.  */
5715            if (! relocatable_p)            if (! relocatable_p)
5716              value += sec->vma;              value += sec->vma;
# Line 5374  swap_out_syms (abfd, sttp, relocatable_p Line 5762  swap_out_syms (abfd, sttp, relocatable_p
5762                       section of a symbol to be a section that is                       section of a symbol to be a section that is
5763                       actually in the output file.  */                       actually in the output file.  */
5764                    sec2 = bfd_get_section_by_name (abfd, sec->name);                    sec2 = bfd_get_section_by_name (abfd, sec->name);
5765                    BFD_ASSERT (sec2 != 0);                    if (sec2 == NULL)
5766                        {
5767                          _bfd_error_handler (_("\
5768    Unable to find equivalent output section for symbol '%s' from section '%s'"),
5769                                              syms[idx]->name ? syms[idx]->name : "<Local sym>",
5770                                              sec->name);
5771                          bfd_set_error (bfd_error_invalid_operation);
5772                          _bfd_stringtab_free (stt);
5773                          return FALSE;
5774                        }
5775    
5776                    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);                    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5777                    BFD_ASSERT (shndx != -1);                    BFD_ASSERT (shndx != -1);
5778                  }                  }
# Line 5395  swap_out_syms (abfd, sttp, relocatable_p Line 5793  swap_out_syms (abfd, sttp, relocatable_p
5793        if (syms[idx]->section->flags & SEC_THREAD_LOCAL)        if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5794          type = STT_TLS;          type = STT_TLS;
5795    
5796        /* Processor-specific types */        /* Processor-specific types.  */
5797        if (type_ptr != NULL        if (type_ptr != NULL
5798            && bed->elf_backend_get_symbol_type)            && bed->elf_backend_get_symbol_type)
5799          type = ((*bed->elf_backend_get_symbol_type)          type = ((*bed->elf_backend_get_symbol_type)
# Line 5453  swap_out_syms (abfd, sttp, relocatable_p Line 5851  swap_out_syms (abfd, sttp, relocatable_p
5851    symstrtab_hdr->sh_info = 0;    symstrtab_hdr->sh_info = 0;
5852    symstrtab_hdr->sh_addralign = 1;    symstrtab_hdr->sh_addralign = 1;
5853    
5854    return true;    return TRUE;
5855  }  }
5856    
5857  /* Return the number of bytes required to hold the symtab vector.  /* Return the number of bytes required to hold the symtab vector.
# Line 5463  swap_out_syms (abfd, sttp, relocatable_p Line 5861  swap_out_syms (abfd, sttp, relocatable_p
5861     always has a dummy entry as symbol #0, so it ends up even.  */     always has a dummy entry as symbol #0, so it ends up even.  */
5862    
5863  long  long
5864  _bfd_elf_get_symtab_upper_bound (abfd)  _bfd_elf_get_symtab_upper_bound (bfd *abfd)
      bfd *abfd;  
5865  {  {
5866    long symcount;    long symcount;
5867    long symtab_size;    long symtab_size;
# Line 5479  _bfd_elf_get_symtab_upper_bound (abfd) Line 5876  _bfd_elf_get_symtab_upper_bound (abfd)
5876  }  }
5877    
5878  long  long
5879  _bfd_elf_get_dynamic_symtab_upper_bound (abfd)  _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
      bfd *abfd;  
5880  {  {
5881    long symcount;    long symcount;
5882    long symtab_size;    long symtab_size;
# Line 5501  _bfd_elf_get_dynamic_symtab_upper_bound Line 5897  _bfd_elf_get_dynamic_symtab_upper_bound
5897  }  }
5898    
5899  long  long
5900  _bfd_elf_get_reloc_upper_bound (abfd, asect)  _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5901       bfd *abfd ATTRIBUTE_UNUSED;                                  sec_ptr asect)
      sec_ptr asect;  
5902  {  {
5903    return (asect->reloc_count + 1) * sizeof (arelent *);    return (asect->reloc_count + 1) * sizeof (arelent *);
5904  }  }
# Line 5511  _bfd_elf_get_reloc_upper_bound (abfd, as Line 5906  _bfd_elf_get_reloc_upper_bound (abfd, as
5906  /* Canonicalize the relocs.  */  /* Canonicalize the relocs.  */
5907    
5908  long  long
5909  _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)  _bfd_elf_canonicalize_reloc (bfd *abfd,
5910       bfd *abfd;                               sec_ptr section,
5911       sec_ptr section;                               arelent **relptr,
5912       arelent **relptr;                               asymbol **symbols)
      asymbol **symbols;  
5913  {  {
5914    arelent *tblptr;    arelent *tblptr;
5915    unsigned int i;    unsigned int i;
5916    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5917    
5918    if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))    if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5919      return -1;      return -1;
5920    
5921    tblptr = section->relocation;    tblptr = section->relocation;
# Line 5534  _bfd_elf_canonicalize_reloc (abfd, secti Line 5928  _bfd_elf_canonicalize_reloc (abfd, secti
5928  }  }
5929    
5930  long  long
5931  _bfd_elf_get_symtab (abfd, alocation)  _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
      bfd *abfd;  
      asymbol **alocation;  
5932  {  {
5933    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5934    long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);    long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5935    
5936    if (symcount >= 0)    if (symcount >= 0)
5937      bfd_get_symcount (abfd) = symcount;      bfd_get_symcount (abfd) = symcount;
# Line 5547  _bfd_elf_get_symtab (abfd, alocation) Line 5939  _bfd_elf_get_symtab (abfd, alocation)
5939  }  }
5940    
5941  long  long
5942  _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)  _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5943       bfd *abfd;                                        asymbol **allocation)
5944       asymbol **alocation;  {
5945  {    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5946    struct elf_backend_data *bed = get_elf_backend_data (abfd);    long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5947    return bed->s->slurp_symbol_table (abfd, alocation, true);  
5948      if (symcount >= 0)
5949        bfd_get_dynamic_symcount (abfd) = symcount;
5950      return symcount;
5951  }  }
5952    
5953  /* Return the size required for the dynamic reloc entries.  Any  /* Return the size required for the dynamic reloc entries.  Any loadable
5954     section that was actually installed in the BFD, and has type     section that was actually installed in the BFD, and has type SHT_REL
5955     SHT_REL or SHT_RELA, and uses the dynamic symbol table, is     or SHT_RELA, and uses the dynamic symbol table, is considered to be a
5956     considered to be a dynamic reloc section.  */     dynamic reloc section.  */
5957    
5958  long  long
5959  _bfd_elf_get_dynamic_reloc_upper_bound (abfd)  _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
      bfd *abfd;  
5960  {  {
5961    long ret;    long ret;
5962    asection *s;    asection *s;
# Line 5575  _bfd_elf_get_dynamic_reloc_upper_bound ( Line 5969  _bfd_elf_get_dynamic_reloc_upper_bound (
5969    
5970    ret = sizeof (arelent *);    ret = sizeof (arelent *);
5971    for (s = abfd->sections; s != NULL; s = s->next)    for (s = abfd->sections; s != NULL; s = s->next)
5972      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)      if ((s->flags & SEC_LOAD) != 0
5973            && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5974          && (elf_section_data (s)->this_hdr.sh_type == SHT_REL          && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5975              || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))              || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5976        ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)        ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
5977                * sizeof (arelent *));                * sizeof (arelent *));
5978    
5979    return ret;    return ret;
5980  }  }
5981    
5982  /* Canonicalize the dynamic relocation entries.  Note that we return  /* Canonicalize the dynamic relocation entries.  Note that we return the
5983     the dynamic relocations as a single block, although they are     dynamic relocations as a single block, although they are actually
5984     actually associated with particular sections; the interface, which     associated with particular sections; the interface, which was
5985     was designed for SunOS style shared libraries, expects that there     designed for SunOS style shared libraries, expects that there is only
5986     is only one set of dynamic relocs.  Any section that was actually     one set of dynamic relocs.  Any loadable section that was actually
5987     installed in the BFD, and has type SHT_REL or SHT_RELA, and uses     installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
5988     the dynamic symbol table, is considered to be a dynamic reloc     dynamic symbol table, is considered to be a dynamic reloc section.  */
    section.  */  
5989    
5990  long  long
5991  _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)  _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5992       bfd *abfd;                                       arelent **storage,
5993       arelent **storage;                                       asymbol **syms)
      asymbol **syms;  
5994  {  {
5995    boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));    bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5996    asection *s;    asection *s;
5997    long ret;    long ret;
5998    
# Line 5613  _bfd_elf_canonicalize_dynamic_reloc (abf Line 6006  _bfd_elf_canonicalize_dynamic_reloc (abf
6006    ret = 0;    ret = 0;
6007    for (s = abfd->sections; s != NULL; s = s->next)    for (s = abfd->sections; s != NULL; s = s->next)
6008      {      {
6009        if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)        if ((s->flags & SEC_LOAD) != 0
6010              && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6011            && (elf_section_data (s)->this_hdr.sh_type == SHT_REL            && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6012                || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))                || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6013          {          {
6014            arelent *p;            arelent *p;
6015            long count, i;            long count, i;
6016    
6017            if (! (*slurp_relocs) (abfd, s, syms, true))            if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6018              return -1;              return -1;
6019            count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;            count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6020            p = s->relocation;            p = s->relocation;
6021            for (i = 0; i < count; i++)            for (i = 0; i < count; i++)
6022              *storage++ = p++;              *storage++ = p++;
# Line 5637  _bfd_elf_canonicalize_dynamic_reloc (abf Line 6031  _bfd_elf_canonicalize_dynamic_reloc (abf
6031    
6032  /* Read in the version information.  */  /* Read in the version information.  */
6033    
6034  boolean  bfd_boolean
6035  _bfd_elf_slurp_version_tables (abfd)  _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
      bfd *abfd;  
6036  {  {
6037    bfd_byte *contents = NULL;    bfd_byte *contents = NULL;
6038    bfd_size_type amt;    bfd_size_type amt;
6039      unsigned int freeidx = 0;
6040    
6041      if (elf_dynverref (abfd) != 0)
6042        {
6043          Elf_Internal_Shdr *hdr;
6044          Elf_External_Verneed *everneed;
6045          Elf_Internal_Verneed *iverneed;
6046          unsigned int i;
6047    
6048          hdr = &elf_tdata (abfd)->dynverref_hdr;
6049    
6050          amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
6051          elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
6052          if (elf_tdata (abfd)->verref == NULL)
6053            goto error_return;
6054    
6055          elf_tdata (abfd)->cverrefs = hdr->sh_info;
6056    
6057          contents = bfd_malloc (hdr->sh_size);
6058          if (contents == NULL)
6059            goto error_return;
6060          if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6061              || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6062            goto error_return;
6063    
6064          everneed = (Elf_External_Verneed *) contents;
6065          iverneed = elf_tdata (abfd)->verref;
6066          for (i = 0; i < hdr->sh_info; i++, iverneed++)
6067            {
6068              Elf_External_Vernaux *evernaux;
6069              Elf_Internal_Vernaux *ivernaux;
6070              unsigned int j;
6071    
6072              _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6073    
6074              iverneed->vn_bfd = abfd;
6075    
6076              iverneed->vn_filename =
6077                bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6078                                                 iverneed->vn_file);
6079              if (iverneed->vn_filename == NULL)
6080                goto error_return;
6081    
6082              amt = iverneed->vn_cnt;
6083              amt *= sizeof (Elf_Internal_Vernaux);
6084              iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6085    
6086              evernaux = ((Elf_External_Vernaux *)
6087                          ((bfd_byte *) everneed + iverneed->vn_aux));
6088              ivernaux = iverneed->vn_auxptr;
6089              for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6090                {
6091                  _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6092    
6093                  ivernaux->vna_nodename =
6094                    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6095                                                     ivernaux->vna_name);
6096                  if (ivernaux->vna_nodename == NULL)
6097                    goto error_return;
6098    
6099                  if (j + 1 < iverneed->vn_cnt)
6100                    ivernaux->vna_nextptr = ivernaux + 1;
6101                  else
6102                    ivernaux->vna_nextptr = NULL;
6103    
6104                  evernaux = ((Elf_External_Vernaux *)
6105                              ((bfd_byte *) evernaux + ivernaux->vna_next));
6106    
6107                  if (ivernaux->vna_other > freeidx)
6108                    freeidx = ivernaux->vna_other;
6109                }
6110    
6111              if (i + 1 < hdr->sh_info)
6112                iverneed->vn_nextref = iverneed + 1;
6113              else
6114                iverneed->vn_nextref = NULL;
6115    
6116              everneed = ((Elf_External_Verneed *)
6117                          ((bfd_byte *) everneed + iverneed->vn_next));
6118            }
6119    
6120          free (contents);
6121          contents = NULL;
6122        }
6123    
6124    if (elf_dynverdef (abfd) != 0)    if (elf_dynverdef (abfd) != 0)
6125      {      {
# Line 5656  _bfd_elf_slurp_version_tables (abfd) Line 6133  _bfd_elf_slurp_version_tables (abfd)
6133    
6134        hdr = &elf_tdata (abfd)->dynverdef_hdr;        hdr = &elf_tdata (abfd)->dynverdef_hdr;
6135    
6136        contents = (bfd_byte *) bfd_malloc (hdr->sh_size);        contents = bfd_malloc (hdr->sh_size);
6137        if (contents == NULL)        if (contents == NULL)
6138          goto error_return;          goto error_return;
6139        if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0        if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6140            || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)            || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6141          goto error_return;          goto error_return;
6142    
6143        /* We know the number of entries in the section but not the maximum        /* We know the number of entries in the section but not the maximum
# Line 5679  _bfd_elf_slurp_version_tables (abfd) Line 6156  _bfd_elf_slurp_version_tables (abfd)
6156                       ((bfd_byte *) everdef + iverdefmem.vd_next));                       ((bfd_byte *) everdef + iverdefmem.vd_next));
6157          }          }
6158    
6159          if (default_imported_symver)
6160            {
6161              if (freeidx > maxidx)
6162                maxidx = ++freeidx;
6163              else
6164                freeidx = ++maxidx;
6165            }
6166        amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);        amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
6167        elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);        elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6168        if (elf_tdata (abfd)->verdef == NULL)        if (elf_tdata (abfd)->verdef == NULL)
6169          goto error_return;          goto error_return;
6170    
# Line 5702  _bfd_elf_slurp_version_tables (abfd) Line 6186  _bfd_elf_slurp_version_tables (abfd)
6186            iverdef->vd_bfd = abfd;            iverdef->vd_bfd = abfd;
6187    
6188            amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);            amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
6189            iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);            iverdef->vd_auxptr = bfd_alloc (abfd, amt);
6190            if (iverdef->vd_auxptr == NULL)            if (iverdef->vd_auxptr == NULL)
6191              goto error_return;              goto error_return;
6192    
# Line 5742  _bfd_elf_slurp_version_tables (abfd) Line 6226  _bfd_elf_slurp_version_tables (abfd)
6226        free (contents);        free (contents);
6227        contents = NULL;        contents = NULL;
6228      }      }
6229      else if (default_imported_symver)
   if (elf_dynverref (abfd) != 0)  
6230      {      {
6231        Elf_Internal_Shdr *hdr;        if (freeidx < 3)
6232        Elf_External_Verneed *everneed;          freeidx = 3;
6233        Elf_Internal_Verneed *iverneed;        else
6234        unsigned int i;          freeidx++;
   
       hdr = &elf_tdata (abfd)->dynverref_hdr;  
   
       amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);  
       elf_tdata (abfd)->verref =  
         (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);  
       if (elf_tdata (abfd)->verref == NULL)  
         goto error_return;  
   
       elf_tdata (abfd)->cverrefs = hdr->sh_info;  
6235    
6236        contents = (bfd_byte *) bfd_malloc (hdr->sh_size);        amt = (bfd_size_type) freeidx * sizeof (Elf_Internal_Verdef);
6237        if (contents == NULL)        elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6238          goto error_return;        if (elf_tdata (abfd)->verdef == NULL)
       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0  
           || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)  
6239          goto error_return;          goto error_return;
6240    
6241        everneed = (Elf_External_Verneed *) contents;        elf_tdata (abfd)->cverdefs = freeidx;
6242        iverneed = elf_tdata (abfd)->verref;      }
       for (i = 0; i < hdr->sh_info; i++, iverneed++)  
         {  
           Elf_External_Vernaux *evernaux;  
           Elf_Internal_Vernaux *ivernaux;  
           unsigned int j;  
   
           _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);  
   
           iverneed->vn_bfd = abfd;  
   
           iverneed->vn_filename =  
             bfd_elf_string_from_elf_section (abfd, hdr->sh_link,  
                                              iverneed->vn_file);  
           if (iverneed->vn_filename == NULL)  
             goto error_return;  
   
           amt = iverneed->vn_cnt;  
           amt *= sizeof (Elf_Internal_Vernaux);  
           iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);  
6243    
6244            evernaux = ((Elf_External_Vernaux *)    /* Create a default version based on the soname.  */
6245                        ((bfd_byte *) everneed + iverneed->vn_aux));    if (default_imported_symver)
6246            ivernaux = iverneed->vn_auxptr;      {
6247            for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)        Elf_Internal_Verdef *iverdef;
6248              {        Elf_Internal_Verdaux *iverdaux;
               _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);  
6249    
6250                ivernaux->vna_nodename =        iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
                 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,  
                                                  ivernaux->vna_name);  
               if (ivernaux->vna_nodename == NULL)  
                 goto error_return;  
6251    
6252                if (j + 1 < iverneed->vn_cnt)        iverdef->vd_version = VER_DEF_CURRENT;
6253                  ivernaux->vna_nextptr = ivernaux + 1;        iverdef->vd_flags = 0;
6254                else        iverdef->vd_ndx = freeidx;
6255                  ivernaux->vna_nextptr = NULL;        iverdef->vd_cnt = 1;
6256    
6257                evernaux = ((Elf_External_Vernaux *)        iverdef->vd_bfd = abfd;
                           ((bfd_byte *) evernaux + ivernaux->vna_next));  
             }  
6258    
6259            if (i + 1 < hdr->sh_info)        iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6260              iverneed->vn_nextref = iverneed + 1;        if (iverdef->vd_nodename == NULL)
6261            else          goto error_return;
6262              iverneed->vn_nextref = NULL;        iverdef->vd_nextdef = NULL;
6263          amt = (bfd_size_type) sizeof (Elf_Internal_Verdaux);
6264            everneed = ((Elf_External_Verneed *)        iverdef->vd_auxptr = bfd_alloc (abfd, amt);
                       ((bfd_byte *) everneed + iverneed->vn_next));  
         }  
6265    
6266        free (contents);        iverdaux = iverdef->vd_auxptr;
6267        contents = NULL;        iverdaux->vda_nodename = iverdef->vd_nodename;
6268          iverdaux->vda_nextptr = NULL;
6269      }      }
6270    
6271    return true;    return TRUE;
6272    
6273   error_return:   error_return:
6274    if (contents == NULL)    if (contents != NULL)
6275      free (contents);      free (contents);
6276    return false;    return FALSE;
6277  }  }
6278    
6279  asymbol *  asymbol *
6280  _bfd_elf_make_empty_symbol (abfd)  _bfd_elf_make_empty_symbol (bfd *abfd)
      bfd *abfd;  
6281  {  {
6282    elf_symbol_type *newsym;    elf_symbol_type *newsym;
6283    bfd_size_type amt = sizeof (elf_symbol_type);    bfd_size_type amt = sizeof (elf_symbol_type);
6284    
6285    newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);    newsym = bfd_zalloc (abfd, amt);
6286    if (!newsym)    if (!newsym)
6287      return NULL;      return NULL;
6288    else    else
# Line 5850  _bfd_elf_make_empty_symbol (abfd) Line 6293  _bfd_elf_make_empty_symbol (abfd)
6293  }  }
6294    
6295  void  void
6296  _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)  _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6297       bfd *ignore_abfd ATTRIBUTE_UNUSED;                            asymbol *symbol,
6298       asymbol *symbol;                            symbol_info *ret)
      symbol_info *ret;  
6299  {  {
6300    bfd_symbol_info (symbol, ret);    bfd_symbol_info (symbol, ret);
6301  }  }
# Line 5862  _bfd_elf_get_symbol_info (ignore_abfd, s Line 6304  _bfd_elf_get_symbol_info (ignore_abfd, s
6304     use this function for the is_local_label_name entry point, but some     use this function for the is_local_label_name entry point, but some
6305     override it.  */     override it.  */
6306    
6307  boolean  bfd_boolean
6308  _bfd_elf_is_local_label_name (abfd, name)  _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6309       bfd *abfd ATTRIBUTE_UNUSED;                                const char *name)
      const char *name;  
6310  {  {
6311    /* Normal local symbols start with ``.L''.  */    /* Normal local symbols start with ``.L''.  */
6312    if (name[0] == '.' && name[1] == 'L')    if (name[0] == '.' && name[1] == 'L')
6313      return true;      return TRUE;
6314    
6315    /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate    /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6316       DWARF debugging symbols starting with ``..''.  */       DWARF debugging symbols starting with ``..''.  */
6317    if (name[0] == '.' && name[1] == '.')    if (name[0] == '.' && name[1] == '.')
6318      return true;      return TRUE;
6319    
6320    /* gcc will sometimes generate symbols beginning with ``_.L_'' when    /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6321       emitting DWARF debugging output.  I suspect this is actually a       emitting DWARF debugging output.  I suspect this is actually a
# Line 5883  _bfd_elf_is_local_label_name (abfd, name Line 6324  _bfd_elf_is_local_label_name (abfd, name
6324       underscore to be emitted on some ELF targets).  For ease of use,       underscore to be emitted on some ELF targets).  For ease of use,
6325       we treat such symbols as local.  */       we treat such symbols as local.  */
6326    if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')    if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6327      return true;      return TRUE;
6328    
6329    return false;    return FALSE;
6330  }  }
6331    
6332  alent *  alent *
6333  _bfd_elf_get_lineno (ignore_abfd, symbol)  _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6334       bfd *ignore_abfd ATTRIBUTE_UNUSED;                       asymbol *symbol ATTRIBUTE_UNUSED)
      asymbol *symbol ATTRIBUTE_UNUSED;  
6335  {  {
6336    abort ();    abort ();
6337    return NULL;    return NULL;
6338  }  }
6339    
6340  boolean  bfd_boolean
6341  _bfd_elf_set_arch_mach (abfd, arch, machine)  _bfd_elf_set_arch_mach (bfd *abfd,
6342       bfd *abfd;                          enum bfd_architecture arch,
6343       enum bfd_architecture arch;                          unsigned long machine)
      unsigned long machine;  
6344  {  {
6345    /* If this isn't the right architecture for this backend, and this    /* If this isn't the right architecture for this backend, and this
6346       isn't the generic backend, fail.  */       isn't the generic backend, fail.  */
6347    if (arch != get_elf_backend_data (abfd)->arch    if (arch != get_elf_backend_data (abfd)->arch
6348        && arch != bfd_arch_unknown        && arch != bfd_arch_unknown
6349        && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)        && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6350      return false;      return FALSE;
6351    
6352    return bfd_default_set_arch_mach (abfd, arch, machine);    return bfd_default_set_arch_mach (abfd, arch, machine);
6353  }  }
# Line 5916  _bfd_elf_set_arch_mach (abfd, arch, mach Line 6355  _bfd_elf_set_arch_mach (abfd, arch, mach
6355  /* Find the function to a particular section and offset,  /* Find the function to a particular section and offset,
6356     for error reporting.  */     for error reporting.  */
6357    
6358  static boolean  static bfd_boolean
6359  elf_find_function (abfd, section, symbols, offset,  elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6360                     filename_ptr, functionname_ptr)                     asection *section,
6361       bfd *abfd ATTRIBUTE_UNUSED;                     asymbol **symbols,
6362       asection *section;                     bfd_vma offset,
6363       asymbol **symbols;                     const char **filename_ptr,
6364       bfd_vma offset;                     const char **functionname_ptr)
      const char **filename_ptr;  
      const char **functionname_ptr;  
6365  {  {
6366    const char *filename;    const char *filename;
6367    asymbol *func;    asymbol *func, *file;
6368    bfd_vma low_func;    bfd_vma low_func;
6369    asymbol **p;    asymbol **p;
6370      /* ??? Given multiple file symbols, it is impossible to reliably
6371         choose the right file name for global symbols.  File symbols are
6372         local symbols, and thus all file symbols must sort before any
6373         global symbols.  The ELF spec may be interpreted to say that a
6374         file symbol must sort before other local symbols, but currently
6375         ld -r doesn't do this.  So, for ld -r output, it is possible to
6376         make a better choice of file name for local symbols by ignoring
6377         file symbols appearing after a given local symbol.  */
6378      enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6379    
6380    filename = NULL;    filename = NULL;
6381    func = NULL;    func = NULL;
6382      file = NULL;
6383    low_func = 0;    low_func = 0;
6384      state = nothing_seen;
6385    
6386    for (p = symbols; *p != NULL; p++)    for (p = symbols; *p != NULL; p++)
6387      {      {
# Line 5941  elf_find_function (abfd, section, symbol Line 6389  elf_find_function (abfd, section, symbol
6389    
6390        q = (elf_symbol_type *) *p;        q = (elf_symbol_type *) *p;
6391    
       if (bfd_get_section (&q->symbol) != section)  
         continue;  
   
6392        switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))        switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6393          {          {
6394          default:          default:
6395            break;            break;
6396          case STT_FILE:          case STT_FILE:
6397            filename = bfd_asymbol_name (&q->symbol);            file = &q->symbol;
6398            break;            if (state == symbol_seen)
6399                state = file_after_symbol_seen;
6400              continue;
6401            case STT_SECTION:
6402              continue;
6403          case STT_NOTYPE:          case STT_NOTYPE:
6404          case STT_FUNC:          case STT_FUNC:
6405            if (q->symbol.section == section            if (bfd_get_section (&q->symbol) == section
6406                && q->symbol.value >= low_func                && q->symbol.value >= low_func
6407                && q->symbol.value <= offset)                && q->symbol.value <= offset)
6408              {              {
6409                func = (asymbol *) q;                func = (asymbol *) q;
6410                low_func = q->symbol.value;                low_func = q->symbol.value;
6411                  if (file == NULL)
6412                    filename = NULL;
6413                  else if (ELF_ST_BIND (q->internal_elf_sym.st_info) != STB_LOCAL
6414                           && state == file_after_symbol_seen)
6415                    filename = NULL;
6416                  else
6417                    filename = bfd_asymbol_name (file);
6418              }              }
6419            break;            break;
6420          }          }
6421          if (state == nothing_seen)
6422            state = symbol_seen;
6423      }      }
6424    
6425    if (func == NULL)    if (func == NULL)
6426      return false;      return FALSE;
6427    
6428    if (filename_ptr)    if (filename_ptr)
6429      *filename_ptr = filename;      *filename_ptr = filename;
6430    if (functionname_ptr)    if (functionname_ptr)
6431      *functionname_ptr = bfd_asymbol_name (func);      *functionname_ptr = bfd_asymbol_name (func);
6432    
6433    return true;    return TRUE;
6434  }  }
6435    
6436  /* Find the nearest line to a particular section and offset,  /* Find the nearest line to a particular section and offset,
6437     for error reporting.  */     for error reporting.  */
6438    
6439  boolean  bfd_boolean
6440  _bfd_elf_find_nearest_line (abfd, section, symbols, offset,  _bfd_elf_find_nearest_line (bfd *abfd,
6441                              filename_ptr, functionname_ptr, line_ptr)                              asection *section,
6442       bfd *abfd;                              asymbol **symbols,
6443       asection *section;                              bfd_vma offset,
6444       asymbol **symbols;                              const char **filename_ptr,
6445       bfd_vma offset;                              const char **functionname_ptr,
6446       const char **filename_ptr;                              unsigned int *line_ptr)
      const char **functionname_ptr;  
      unsigned int *line_ptr;  
6447  {  {
6448    boolean found;    bfd_boolean found;
6449    
6450    if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,    if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6451                                       filename_ptr, functionname_ptr,                                       filename_ptr, functionname_ptr,
# Line 6000  _bfd_elf_find_nearest_line (abfd, sectio Line 6456  _bfd_elf_find_nearest_line (abfd, sectio
6456                             *filename_ptr ? NULL : filename_ptr,                             *filename_ptr ? NULL : filename_ptr,
6457                             functionname_ptr);                             functionname_ptr);
6458    
6459        return true;        return TRUE;
6460      }      }
6461    
6462    if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,    if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
# Line 6013  _bfd_elf_find_nearest_line (abfd, sectio Line 6469  _bfd_elf_find_nearest_line (abfd, sectio
6469                             *filename_ptr ? NULL : filename_ptr,                             *filename_ptr ? NULL : filename_ptr,
6470                             functionname_ptr);                             functionname_ptr);
6471    
6472        return true;        return TRUE;
6473      }      }
6474    
6475    if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,    if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6476                                               &found, filename_ptr,                                               &found, filename_ptr,
6477                                               functionname_ptr, line_ptr,                                               functionname_ptr, line_ptr,
6478                                               &elf_tdata (abfd)->line_info))                                               &elf_tdata (abfd)->line_info))
6479      return false;      return FALSE;
6480    if (found)    if (found && (*functionname_ptr || *line_ptr))
6481      return true;      return TRUE;
6482    
6483    if (symbols == NULL)    if (symbols == NULL)
6484      return false;      return FALSE;
6485    
6486    if (! elf_find_function (abfd, section, symbols, offset,    if (! elf_find_function (abfd, section, symbols, offset,
6487                             filename_ptr, functionname_ptr))                             filename_ptr, functionname_ptr))
6488      return false;      return FALSE;
6489    
6490    *line_ptr = 0;    *line_ptr = 0;
6491    return true;    return TRUE;
6492  }  }
6493    
6494  int  int
6495  _bfd_elf_sizeof_headers (abfd, reloc)  _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
      bfd *abfd;  
      boolean reloc;  
6496  {  {
6497    int ret;    int ret;
6498    
# Line 6048  _bfd_elf_sizeof_headers (abfd, reloc) Line 6502  _bfd_elf_sizeof_headers (abfd, reloc)
6502    return ret;    return ret;
6503  }  }
6504    
6505  boolean  bfd_boolean
6506  _bfd_elf_set_section_contents (abfd, section, location, offset, count)  _bfd_elf_set_section_contents (bfd *abfd,
6507       bfd *abfd;                                 sec_ptr section,
6508       sec_ptr section;                                 const void *location,
6509       PTR location;                                 file_ptr offset,
6510       file_ptr offset;                                 bfd_size_type count)
      bfd_size_type count;  
6511  {  {
6512    Elf_Internal_Shdr *hdr;    Elf_Internal_Shdr *hdr;
6513    bfd_signed_vma pos;    bfd_signed_vma pos;
6514    
6515    if (! abfd->output_has_begun    if (! abfd->output_has_begun
6516        && ! (_bfd_elf_compute_section_file_positions        && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6517              (abfd, (struct bfd_link_info *) NULL)))      return FALSE;
     return false;  
6518    
6519    hdr = &elf_section_data (section)->this_hdr;    hdr = &elf_section_data (section)->this_hdr;
6520    pos = hdr->sh_offset + offset;    pos = hdr->sh_offset + offset;
6521    if (bfd_seek (abfd, pos, SEEK_SET) != 0    if (bfd_seek (abfd, pos, SEEK_SET) != 0
6522        || bfd_bwrite (location, count, abfd) != count)        || bfd_bwrite (location, count, abfd) != count)
6523      return false;      return FALSE;
6524    
6525    return true;    return TRUE;
 }  
   
 void  
 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)  
      bfd *abfd ATTRIBUTE_UNUSED;  
      arelent *cache_ptr ATTRIBUTE_UNUSED;  
      Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;  
 {  
   abort ();  
6526  }  }
6527    
 #if 0  
6528  void  void
6529  _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)  _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6530       bfd *abfd;                             arelent *cache_ptr ATTRIBUTE_UNUSED,
6531       arelent *cache_ptr;                             Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
      Elf_Internal_Rel *dst;  
6532  {  {
6533    abort ();    abort ();
6534  }  }
 #endif  
6535    
6536  /* Try to convert a non-ELF reloc into an ELF one.  */  /* Try to convert a non-ELF reloc into an ELF one.  */
6537    
6538  boolean  bfd_boolean
6539  _bfd_elf_validate_reloc (abfd, areloc)  _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
      bfd *abfd;  
      arelent *areloc;  
6540  {  {
6541    /* Check whether we really have an ELF howto.  */    /* Check whether we really have an ELF howto.  */
6542    
# Line 6181  _bfd_elf_validate_reloc (abfd, areloc) Line 6619  _bfd_elf_validate_reloc (abfd, areloc)
6619          goto fail;          goto fail;
6620      }      }
6621    
6622    return true;    return TRUE;
6623    
6624   fail:   fail:
6625    (*_bfd_error_handler)    (*_bfd_error_handler)
6626      (_("%s: unsupported relocation type %s"),      (_("%B: unsupported relocation type %s"),
6627       bfd_archive_filename (abfd), areloc->howto->name);       abfd, areloc->howto->name);
6628    bfd_set_error (bfd_error_bad_value);    bfd_set_error (bfd_error_bad_value);
6629    return false;    return FALSE;
6630  }  }
6631    
6632  boolean  bfd_boolean
6633  _bfd_elf_close_and_cleanup (abfd)  _bfd_elf_close_and_cleanup (bfd *abfd)
      bfd *abfd;  
6634  {  {
6635    if (bfd_get_format (abfd) == bfd_object)    if (bfd_get_format (abfd) == bfd_object)
6636      {      {
# Line 6210  _bfd_elf_close_and_cleanup (abfd) Line 6647  _bfd_elf_close_and_cleanup (abfd)
6647     this reloc.  */     this reloc.  */
6648    
6649  bfd_reloc_status_type  bfd_reloc_status_type
6650  _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)  _bfd_elf_rel_vtable_reloc_fn
6651       bfd *abfd ATTRIBUTE_UNUSED;    (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6652       arelent *re ATTRIBUTE_UNUSED;     struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6653       struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;     void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6654       PTR data ATTRIBUTE_UNUSED;     bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
      asection *is ATTRIBUTE_UNUSED;  
      bfd *obfd ATTRIBUTE_UNUSED;  
      char **errmsg ATTRIBUTE_UNUSED;  
6655  {  {
6656    return bfd_reloc_ok;    return bfd_reloc_ok;
6657  }  }
# Line 6234  _bfd_elf_rel_vtable_reloc_fn (abfd, re, Line 6668  _bfd_elf_rel_vtable_reloc_fn (abfd, re,
6668  /* FIXME: this is kinda wrong, but it's what gdb wants.  */  /* FIXME: this is kinda wrong, but it's what gdb wants.  */
6669    
6670  static int  static int
6671  elfcore_make_pid (abfd)  elfcore_make_pid (bfd *abfd)
      bfd *abfd;  
6672  {  {
6673    return ((elf_tdata (abfd)->core_lwpid << 16)    return ((elf_tdata (abfd)->core_lwpid << 16)
6674            + (elf_tdata (abfd)->core_pid));            + (elf_tdata (abfd)->core_pid));
# Line 6246  elfcore_make_pid (abfd) Line 6679  elfcore_make_pid (abfd)
6679     reference to NAME, so you shouldn't deallocate or     reference to NAME, so you shouldn't deallocate or
6680     overwrite it.  */     overwrite it.  */
6681    
6682  static boolean  static bfd_boolean
6683  elfcore_maybe_make_sect (abfd, name, sect)  elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
      bfd *abfd;  
      char *name;  
      asection *sect;  
6684  {  {
6685    asection *sect2;    asection *sect2;
6686    
6687    if (bfd_get_section_by_name (abfd, name) != NULL)    if (bfd_get_section_by_name (abfd, name) != NULL)
6688      return true;      return TRUE;
6689    
6690    sect2 = bfd_make_section (abfd, name);    sect2 = bfd_make_section (abfd, name);
6691    if (sect2 == NULL)    if (sect2 == NULL)
6692      return false;      return FALSE;
6693    
6694    sect2->_raw_size = sect->_raw_size;    sect2->size = sect->size;
6695    sect2->filepos = sect->filepos;    sect2->filepos = sect->filepos;
6696    sect2->flags = sect->flags;    sect2->flags = sect->flags;
6697    sect2->alignment_power = sect->alignment_power;    sect2->alignment_power = sect->alignment_power;
6698    return true;    return TRUE;
6699  }  }
6700    
6701  /* Create a pseudosection containing SIZE bytes at FILEPOS.  This  /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
# Line 6275  elfcore_maybe_make_sect (abfd, name, sec Line 6705  elfcore_maybe_make_sect (abfd, name, sec
6705     - For the multi-threaded case, a section named "NAME/PID", where     - For the multi-threaded case, a section named "NAME/PID", where
6706       PID is elfcore_make_pid (abfd).       PID is elfcore_make_pid (abfd).
6707     Both pseudosections have identical contents. */     Both pseudosections have identical contents. */
6708  boolean  bfd_boolean
6709  _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)  _bfd_elfcore_make_pseudosection (bfd *abfd,
6710       bfd *abfd;                                   char *name,
6711       char *name;                                   size_t size,
6712       size_t size;                                   ufile_ptr filepos)
      ufile_ptr filepos;  
6713  {  {
6714    char buf[100];    char buf[100];
6715    char *threaded_name;    char *threaded_name;
# Line 6291  _bfd_elfcore_make_pseudosection (abfd, n Line 6720  _bfd_elfcore_make_pseudosection (abfd, n
6720    
6721    sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));    sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6722    len = strlen (buf) + 1;    len = strlen (buf) + 1;
6723    threaded_name = bfd_alloc (abfd, (bfd_size_type) len);    threaded_name = bfd_alloc (abfd, len);
6724    if (threaded_name == NULL)    if (threaded_name == NULL)
6725      return false;      return FALSE;
6726    memcpy (threaded_name, buf, len);    memcpy (threaded_name, buf, len);
6727    
6728    sect = bfd_make_section (abfd, threaded_name);    sect = bfd_make_section_anyway (abfd, threaded_name);
6729    if (sect == NULL)    if (sect == NULL)
6730      return false;      return FALSE;
6731    sect->_raw_size = size;    sect->size = size;
6732    sect->filepos = filepos;    sect->filepos = filepos;
6733    sect->flags = SEC_HAS_CONTENTS;    sect->flags = SEC_HAS_CONTENTS;
6734    sect->alignment_power = 2;    sect->alignment_power = 2;
# Line 6314  _bfd_elfcore_make_pseudosection (abfd, n Line 6743  _bfd_elfcore_make_pseudosection (abfd, n
6743  */  */
6744    
6745  #if defined (HAVE_PRSTATUS_T)  #if defined (HAVE_PRSTATUS_T)
 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));  
6746    
6747  static boolean  static bfd_boolean
6748  elfcore_grok_prstatus (abfd, note)  elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
6749  {  {
6750    size_t raw_size;    size_t size;
6751    int offset;    int offset;
6752    
6753    if (note->descsz == sizeof (prstatus_t))    if (note->descsz == sizeof (prstatus_t))
6754      {      {
6755        prstatus_t prstat;        prstatus_t prstat;
6756    
6757        raw_size = sizeof (prstat.pr_reg);        size = sizeof (prstat.pr_reg);
6758        offset   = offsetof (prstatus_t, pr_reg);        offset   = offsetof (prstatus_t, pr_reg);
6759        memcpy (&prstat, note->descdata, sizeof (prstat));        memcpy (&prstat, note->descdata, sizeof (prstat));
6760    
# Line 6354  elfcore_grok_prstatus (abfd, note) Line 6780  elfcore_grok_prstatus (abfd, note)
6780        /* 64-bit host, 32-bit corefile */        /* 64-bit host, 32-bit corefile */
6781        prstatus32_t prstat;        prstatus32_t prstat;
6782    
6783        raw_size = sizeof (prstat.pr_reg);        size = sizeof (prstat.pr_reg);
6784        offset   = offsetof (prstatus32_t, pr_reg);        offset   = offsetof (prstatus32_t, pr_reg);
6785        memcpy (&prstat, note->descdata, sizeof (prstat));        memcpy (&prstat, note->descdata, sizeof (prstat));
6786    
# Line 6379  elfcore_grok_prstatus (abfd, note) Line 6805  elfcore_grok_prstatus (abfd, note)
6805      {      {
6806        /* Fail - we don't know how to handle any other        /* Fail - we don't know how to handle any other
6807           note size (ie. data object type).  */           note size (ie. data object type).  */
6808        return true;        return TRUE;
6809      }      }
6810    
6811    /* Make a ".reg/999" section and a ".reg" section.  */    /* Make a ".reg/999" section and a ".reg" section.  */
6812    return _bfd_elfcore_make_pseudosection (abfd, ".reg",    return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6813                                            raw_size, note->descpos + offset);                                            size, note->descpos + offset);
6814  }  }
6815  #endif /* defined (HAVE_PRSTATUS_T) */  #endif /* defined (HAVE_PRSTATUS_T) */
6816    
6817  /* Create a pseudosection containing the exact contents of NOTE.  */  /* Create a pseudosection containing the exact contents of NOTE.  */
6818  static boolean  static bfd_boolean
6819  elfcore_make_note_pseudosection (abfd, name, note)  elfcore_make_note_pseudosection (bfd *abfd,
6820       bfd *abfd;                                   char *name,
6821       char *name;                                   Elf_Internal_Note *note)
      Elf_Internal_Note *note;  
6822  {  {
6823    return _bfd_elfcore_make_pseudosection (abfd, name,    return _bfd_elfcore_make_pseudosection (abfd, name,
6824                                            note->descsz, note->descpos);                                            note->descsz, note->descpos);
# Line 6403  elfcore_make_note_pseudosection (abfd, n Line 6828  elfcore_make_note_pseudosection (abfd, n
6828     but it doesn't matter, because we don't have to pick this     but it doesn't matter, because we don't have to pick this
6829     data structure apart.  */     data structure apart.  */
6830    
6831  static boolean  static bfd_boolean
6832  elfcore_grok_prfpreg (abfd, note)  elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
6833  {  {
6834    return elfcore_make_note_pseudosection (abfd, ".reg2", note);    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6835  }  }
# Line 6415  elfcore_grok_prfpreg (abfd, note) Line 6838  elfcore_grok_prfpreg (abfd, note)
6838     type of 5 (NT_PRXFPREG).  Just include the whole note's contents     type of 5 (NT_PRXFPREG).  Just include the whole note's contents
6839     literally.  */     literally.  */
6840    
6841  static boolean  static bfd_boolean
6842  elfcore_grok_prxfpreg (abfd, note)  elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
6843  {  {
6844    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6845  }  }
# Line 6442  typedef psinfo32_t elfcore_psinfo32_t; Line 6863  typedef psinfo32_t elfcore_psinfo32_t;
6863     the copy will always have a terminating '\0'.  */     the copy will always have a terminating '\0'.  */
6864    
6865  char *  char *
6866  _bfd_elfcore_strndup (abfd, start, max)  _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
      bfd *abfd;  
      char *start;  
      size_t max;  
6867  {  {
6868    char *dups;    char *dups;
6869    char *end = memchr (start, '\0', max);    char *end = memchr (start, '\0', max);
# Line 6456  _bfd_elfcore_strndup (abfd, start, max) Line 6874  _bfd_elfcore_strndup (abfd, start, max)
6874    else    else
6875      len = end - start;      len = end - start;
6876    
6877    dups = bfd_alloc (abfd, (bfd_size_type) len + 1);    dups = bfd_alloc (abfd, len + 1);
6878    if (dups == NULL)    if (dups == NULL)
6879      return NULL;      return NULL;
6880    
# Line 6467  _bfd_elfcore_strndup (abfd, start, max) Line 6885  _bfd_elfcore_strndup (abfd, start, max)
6885  }  }
6886    
6887  #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)  #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6888  static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));  static bfd_boolean
6889    elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 static boolean  
 elfcore_grok_psinfo (abfd, note)  
      bfd *abfd;  
      Elf_Internal_Note *note;  
6890  {  {
6891    if (note->descsz == sizeof (elfcore_psinfo_t))    if (note->descsz == sizeof (elfcore_psinfo_t))
6892      {      {
# Line 6510  elfcore_grok_psinfo (abfd, note) Line 6924  elfcore_grok_psinfo (abfd, note)
6924      {      {
6925        /* Fail - we don't know how to handle any other        /* Fail - we don't know how to handle any other
6926           note size (ie. data object type).  */           note size (ie. data object type).  */
6927        return true;        return TRUE;
6928      }      }
6929    
6930    /* Note that for some reason, a spurious space is tacked    /* Note that for some reason, a spurious space is tacked
# Line 6525  elfcore_grok_psinfo (abfd, note) Line 6939  elfcore_grok_psinfo (abfd, note)
6939        command[n - 1] = '\0';        command[n - 1] = '\0';
6940    }    }
6941    
6942    return true;    return TRUE;
6943  }  }
6944  #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */  #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6945    
6946  #if defined (HAVE_PSTATUS_T)  #if defined (HAVE_PSTATUS_T)
6947  static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));  static bfd_boolean
6948    elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
 static boolean  
 elfcore_grok_pstatus (abfd, note)  
      bfd *abfd;  
      Elf_Internal_Note *note;  
6949  {  {
6950    if (note->descsz == sizeof (pstatus_t)    if (note->descsz == sizeof (pstatus_t)
6951  #if defined (HAVE_PXSTATUS_T)  #if defined (HAVE_PXSTATUS_T)
# Line 6564  elfcore_grok_pstatus (abfd, note) Line 6974  elfcore_grok_pstatus (abfd, note)
6974       lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an       lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6975       NT_LWPSTATUS note, presumably.  */       NT_LWPSTATUS note, presumably.  */
6976    
6977    return true;    return TRUE;
6978  }  }
6979  #endif /* defined (HAVE_PSTATUS_T) */  #endif /* defined (HAVE_PSTATUS_T) */
6980    
6981  #if defined (HAVE_LWPSTATUS_T)  #if defined (HAVE_LWPSTATUS_T)
6982  static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));  static bfd_boolean
6983    elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
 static boolean  
 elfcore_grok_lwpstatus (abfd, note)  
      bfd *abfd;  
      Elf_Internal_Note *note;  
6984  {  {
6985    lwpstatus_t lwpstat;    lwpstatus_t lwpstat;
6986    char buf[100];    char buf[100];
# Line 6587  elfcore_grok_lwpstatus (abfd, note) Line 6993  elfcore_grok_lwpstatus (abfd, note)
6993        && note->descsz != sizeof (lwpxstatus_t)        && note->descsz != sizeof (lwpxstatus_t)
6994  #endif  #endif
6995        )        )
6996      return true;      return TRUE;
6997    
6998    memcpy (&lwpstat, note->descdata, sizeof (lwpstat));    memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6999    
# Line 6598  elfcore_grok_lwpstatus (abfd, note) Line 7004  elfcore_grok_lwpstatus (abfd, note)
7004    
7005    sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));    sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7006    len = strlen (buf) + 1;    len = strlen (buf) + 1;
7007    name = bfd_alloc (abfd, (bfd_size_type) len);    name = bfd_alloc (abfd, len);
7008    if (name == NULL)    if (name == NULL)
7009      return false;      return FALSE;
7010    memcpy (name, buf, len);    memcpy (name, buf, len);
7011    
7012    sect = bfd_make_section (abfd, name);    sect = bfd_make_section_anyway (abfd, name);
7013    if (sect == NULL)    if (sect == NULL)
7014      return false;      return FALSE;
7015    
7016  #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)  #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7017    sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);    sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7018    sect->filepos = note->descpos    sect->filepos = note->descpos
7019      + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);      + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7020  #endif  #endif
7021    
7022  #if defined (HAVE_LWPSTATUS_T_PR_REG)  #if defined (HAVE_LWPSTATUS_T_PR_REG)
7023    sect->_raw_size = sizeof (lwpstat.pr_reg);    sect->size = sizeof (lwpstat.pr_reg);
7024    sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);    sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7025  #endif  #endif
7026    
# Line 6622  elfcore_grok_lwpstatus (abfd, note) Line 7028  elfcore_grok_lwpstatus (abfd, note)
7028    sect->alignment_power = 2;    sect->alignment_power = 2;
7029    
7030    if (!elfcore_maybe_make_sect (abfd, ".reg", sect))    if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7031      return false;      return FALSE;
7032    
7033    /* Make a ".reg2/999" section */    /* Make a ".reg2/999" section */
7034    
7035    sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));    sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7036    len = strlen (buf) + 1;    len = strlen (buf) + 1;
7037    name = bfd_alloc (abfd, (bfd_size_type) len);    name = bfd_alloc (abfd, len);
7038    if (name == NULL)    if (name == NULL)
7039      return false;      return FALSE;
7040    memcpy (name, buf, len);    memcpy (name, buf, len);
7041    
7042    sect = bfd_make_section (abfd, name);    sect = bfd_make_section_anyway (abfd, name);
7043    if (sect == NULL)    if (sect == NULL)
7044      return false;      return FALSE;
7045    
7046  #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)  #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7047    sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);    sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7048    sect->filepos = note->descpos    sect->filepos = note->descpos
7049      + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);      + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7050  #endif  #endif
7051    
7052  #if defined (HAVE_LWPSTATUS_T_PR_FPREG)  #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7053    sect->_raw_size = sizeof (lwpstat.pr_fpreg);    sect->size = sizeof (lwpstat.pr_fpreg);
7054    sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);    sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7055  #endif  #endif
7056    
# Line 6656  elfcore_grok_lwpstatus (abfd, note) Line 7062  elfcore_grok_lwpstatus (abfd, note)
7062  #endif /* defined (HAVE_LWPSTATUS_T) */  #endif /* defined (HAVE_LWPSTATUS_T) */
7063    
7064  #if defined (HAVE_WIN32_PSTATUS_T)  #if defined (HAVE_WIN32_PSTATUS_T)
7065  static boolean  static bfd_boolean
7066  elfcore_grok_win32pstatus (abfd, note)  elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
7067  {  {
7068    char buf[30];    char buf[30];
7069    char *name;    char *name;
# Line 6668  elfcore_grok_win32pstatus (abfd, note) Line 7072  elfcore_grok_win32pstatus (abfd, note)
7072    win32_pstatus_t pstatus;    win32_pstatus_t pstatus;
7073    
7074    if (note->descsz < sizeof (pstatus))    if (note->descsz < sizeof (pstatus))
7075      return true;      return TRUE;
7076    
7077    memcpy (&pstatus, note->descdata, sizeof (pstatus));    memcpy (&pstatus, note->descdata, sizeof (pstatus));
7078    
# Line 6682  elfcore_grok_win32pstatus (abfd, note) Line 7086  elfcore_grok_win32pstatus (abfd, note)
7086    
7087      case NOTE_INFO_THREAD:      case NOTE_INFO_THREAD:
7088        /* Make a ".reg/999" section.  */        /* Make a ".reg/999" section.  */
7089        sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);        sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid);
7090    
7091        len = strlen (buf) + 1;        len = strlen (buf) + 1;
7092        name = bfd_alloc (abfd, (bfd_size_type) len);        name = bfd_alloc (abfd, len);
7093        if (name == NULL)        if (name == NULL)
7094          return false;          return FALSE;
7095    
7096        memcpy (name, buf, len);        memcpy (name, buf, len);
7097    
7098        sect = bfd_make_section (abfd, name);        sect = bfd_make_section_anyway (abfd, name);
7099        if (sect == NULL)        if (sect == NULL)
7100          return false;          return FALSE;
7101    
7102        sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);        sect->size = sizeof (pstatus.data.thread_info.thread_context);
7103        sect->filepos = (note->descpos        sect->filepos = (note->descpos
7104                         + offsetof (struct win32_pstatus,                         + offsetof (struct win32_pstatus,
7105                                     data.thread_info.thread_context));                                     data.thread_info.thread_context));
# Line 6704  elfcore_grok_win32pstatus (abfd, note) Line 7108  elfcore_grok_win32pstatus (abfd, note)
7108    
7109        if (pstatus.data.thread_info.is_active_thread)        if (pstatus.data.thread_info.is_active_thread)
7110          if (! elfcore_maybe_make_sect (abfd, ".reg", sect))          if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7111            return false;            return FALSE;
7112        break;        break;
7113    
7114      case NOTE_INFO_MODULE:      case NOTE_INFO_MODULE:
7115        /* Make a ".module/xxxxxxxx" section.  */        /* Make a ".module/xxxxxxxx" section.  */
7116        sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);        sprintf (buf, ".module/%08lx",
7117                   (long) pstatus.data.module_info.base_address);
7118    
7119        len = strlen (buf) + 1;        len = strlen (buf) + 1;
7120        name = bfd_alloc (abfd, (bfd_size_type) len);        name = bfd_alloc (abfd, len);
7121        if (name == NULL)        if (name == NULL)
7122          return false;          return FALSE;
7123    
7124        memcpy (name, buf, len);        memcpy (name, buf, len);
7125    
7126        sect = bfd_make_section (abfd, name);        sect = bfd_make_section_anyway (abfd, name);
7127    
7128        if (sect == NULL)        if (sect == NULL)
7129          return false;          return FALSE;
7130    
7131        sect->_raw_size = note->descsz;        sect->size = note->descsz;
7132        sect->filepos = note->descpos;        sect->filepos = note->descpos;
7133        sect->flags = SEC_HAS_CONTENTS;        sect->flags = SEC_HAS_CONTENTS;
7134        sect->alignment_power = 2;        sect->alignment_power = 2;
7135        break;        break;
7136    
7137      default:      default:
7138        return true;        return TRUE;
7139      }      }
7140    
7141    return true;    return TRUE;
7142  }  }
7143  #endif /* HAVE_WIN32_PSTATUS_T */  #endif /* HAVE_WIN32_PSTATUS_T */
7144    
7145  static boolean  static bfd_boolean
7146  elfcore_grok_note (abfd, note)  elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
7147  {  {
7148    struct elf_backend_data *bed = get_elf_backend_data (abfd);    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7149    
7150    switch (note->type)    switch (note->type)
7151      {      {
7152      default:      default:
7153        return true;        return TRUE;
7154    
7155      case NT_PRSTATUS:      case NT_PRSTATUS:
7156        if (bed->elf_backend_grok_prstatus)        if (bed->elf_backend_grok_prstatus)
7157          if ((*bed->elf_backend_grok_prstatus) (abfd, note))          if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7158            return true;            return TRUE;
7159  #if defined (HAVE_PRSTATUS_T)  #if defined (HAVE_PRSTATUS_T)
7160        return elfcore_grok_prstatus (abfd, note);        return elfcore_grok_prstatus (abfd, note);
7161  #else  #else
7162        return true;        return TRUE;
7163  #endif  #endif
7164    
7165  #if defined (HAVE_PSTATUS_T)  #if defined (HAVE_PSTATUS_T)
# Line 6778  elfcore_grok_note (abfd, note) Line 7181  elfcore_grok_note (abfd, note)
7181  #endif  #endif
7182    
7183      case NT_PRXFPREG:           /* Linux SSE extension */      case NT_PRXFPREG:           /* Linux SSE extension */
7184        if (note->namesz == 5        if (note->namesz == 6
7185            && ! strcmp (note->namedata, "LINUX"))            && strcmp (note->namedata, "LINUX") == 0)
7186          return elfcore_grok_prxfpreg (abfd, note);          return elfcore_grok_prxfpreg (abfd, note);
7187        else        else
7188          return true;          return TRUE;
7189    
7190      case NT_PRPSINFO:      case NT_PRPSINFO:
7191      case NT_PSINFO:      case NT_PSINFO:
7192        if (bed->elf_backend_grok_psinfo)        if (bed->elf_backend_grok_psinfo)
7193          if ((*bed->elf_backend_grok_psinfo) (abfd, note))          if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7194            return true;            return TRUE;
7195  #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)  #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7196        return elfcore_grok_psinfo (abfd, note);        return elfcore_grok_psinfo (abfd, note);
7197  #else  #else
7198        return true;        return TRUE;
7199  #endif  #endif
7200    
7201        case NT_AUXV:
7202          {
7203            asection *sect = bfd_make_section_anyway (abfd, ".auxv");
7204    
7205            if (sect == NULL)
7206              return FALSE;
7207            sect->size = note->descsz;
7208            sect->filepos = note->descpos;
7209            sect->flags = SEC_HAS_CONTENTS;
7210            sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7211    
7212            return TRUE;
7213          }
7214      }      }
7215  }  }
7216    
7217  static boolean  static bfd_boolean
7218  elfcore_netbsd_get_lwpid (note, lwpidp)  elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
      Elf_Internal_Note *note;  
      int *lwpidp;  
7219  {  {
7220    char *cp;    char *cp;
7221    
# Line 6808  elfcore_netbsd_get_lwpid (note, lwpidp) Line 7223  elfcore_netbsd_get_lwpid (note, lwpidp)
7223    if (cp != NULL)    if (cp != NULL)
7224      {      {
7225        *lwpidp = atoi(cp + 1);        *lwpidp = atoi(cp + 1);
7226        return true;        return TRUE;
7227      }      }
7228    return false;    return FALSE;
7229  }  }
7230    
7231  static boolean  static bfd_boolean
7232  elfcore_grok_netbsd_procinfo (abfd, note)  elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
7233  {  {
7234    
7235    /* Signal number at offset 0x08. */    /* Signal number at offset 0x08. */
# Line 6831  elfcore_grok_netbsd_procinfo (abfd, note Line 7244  elfcore_grok_netbsd_procinfo (abfd, note
7244    elf_tdata (abfd)->core_command    elf_tdata (abfd)->core_command
7245      = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);      = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7246    
7247    return true;    return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7248                                              note);
7249  }  }
7250    
7251  static boolean  static bfd_boolean
7252  elfcore_grok_netbsd_note (abfd, note)  elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
      bfd *abfd;  
      Elf_Internal_Note *note;  
7253  {  {
7254    int lwp;    int lwp;
7255    
# Line 6860  elfcore_grok_netbsd_note (abfd, note) Line 7272  elfcore_grok_netbsd_note (abfd, note)
7272       understand it.  */       understand it.  */
7273    
7274    if (note->type < NT_NETBSDCORE_FIRSTMACH)    if (note->type < NT_NETBSDCORE_FIRSTMACH)
7275      return true;      return TRUE;
7276    
7277    
7278    switch (bfd_get_arch (abfd))    switch (bfd_get_arch (abfd))
# Line 6879  elfcore_grok_netbsd_note (abfd, note) Line 7291  elfcore_grok_netbsd_note (abfd, note)
7291            return elfcore_make_note_pseudosection (abfd, ".reg2", note);            return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7292    
7293          default:          default:
7294            return true;            return TRUE;
7295          }          }
7296    
7297      /* On all other arch's, PT_GETREGS == mach+1 and      /* On all other arch's, PT_GETREGS == mach+1 and
# Line 6895  elfcore_grok_netbsd_note (abfd, note) Line 7307  elfcore_grok_netbsd_note (abfd, note)
7307            return elfcore_make_note_pseudosection (abfd, ".reg2", note);            return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7308    
7309          default:          default:
7310            return true;            return TRUE;
7311          }          }
7312      }      }
7313      /* NOTREACHED */      /* NOTREACHED */
7314  }  }
7315    
7316    static bfd_boolean
7317    elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7318    {
7319      void *ddata = note->descdata;
7320      char buf[100];
7321      char *name;
7322      asection *sect;
7323      short sig;
7324      unsigned flags;
7325    
7326      /* nto_procfs_status 'pid' field is at offset 0.  */
7327      elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7328    
7329      /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
7330      *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7331    
7332      /* nto_procfs_status 'flags' field is at offset 8.  */
7333      flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7334    
7335      /* nto_procfs_status 'what' field is at offset 14.  */
7336      if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7337        {
7338          elf_tdata (abfd)->core_signal = sig;
7339          elf_tdata (abfd)->core_lwpid = *tid;
7340        }
7341    
7342      /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
7343         do not come from signals so we make sure we set the current
7344         thread just in case.  */
7345      if (flags & 0x00000080)
7346        elf_tdata (abfd)->core_lwpid = *tid;
7347    
7348      /* Make a ".qnx_core_status/%d" section.  */
7349      sprintf (buf, ".qnx_core_status/%d", *tid);
7350    
7351      name = bfd_alloc (abfd, strlen (buf) + 1);
7352      if (name == NULL)
7353        return FALSE;
7354      strcpy (name, buf);
7355    
7356      sect = bfd_make_section_anyway (abfd, name);
7357      if (sect == NULL)
7358        return FALSE;
7359    
7360      sect->size            = note->descsz;
7361      sect->filepos         = note->descpos;
7362      sect->flags           = SEC_HAS_CONTENTS;
7363      sect->alignment_power = 2;
7364    
7365      return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7366    }
7367    
7368    static bfd_boolean
7369    elfcore_grok_nto_regs (bfd *abfd,
7370                           Elf_Internal_Note *note,
7371                           pid_t tid,
7372                           char *base)
7373    {
7374      char buf[100];
7375      char *name;
7376      asection *sect;
7377    
7378      /* Make a "(base)/%d" section.  */
7379      sprintf (buf, "%s/%d", base, tid);
7380    
7381      name = bfd_alloc (abfd, strlen (buf) + 1);
7382      if (name == NULL)
7383        return FALSE;
7384      strcpy (name, buf);
7385    
7386      sect = bfd_make_section_anyway (abfd, name);
7387      if (sect == NULL)
7388        return FALSE;
7389    
7390      sect->size            = note->descsz;
7391      sect->filepos         = note->descpos;
7392      sect->flags           = SEC_HAS_CONTENTS;
7393      sect->alignment_power = 2;
7394    
7395      /* This is the current thread.  */
7396      if (elf_tdata (abfd)->core_lwpid == tid)
7397        return elfcore_maybe_make_sect (abfd, base, sect);
7398    
7399      return TRUE;
7400    }
7401    
7402    #define BFD_QNT_CORE_INFO       7
7403    #define BFD_QNT_CORE_STATUS     8
7404    #define BFD_QNT_CORE_GREG       9
7405    #define BFD_QNT_CORE_FPREG      10
7406    
7407    static bfd_boolean
7408    elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7409    {
7410      /* Every GREG section has a STATUS section before it.  Store the
7411         tid from the previous call to pass down to the next gregs
7412         function.  */
7413      static pid_t tid = 1;
7414    
7415      switch (note->type)
7416        {
7417        case BFD_QNT_CORE_INFO:
7418          return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7419        case BFD_QNT_CORE_STATUS:
7420          return elfcore_grok_nto_status (abfd, note, &tid);
7421        case BFD_QNT_CORE_GREG:
7422          return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
7423        case BFD_QNT_CORE_FPREG:
7424          return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
7425        default:
7426          return TRUE;
7427        }
7428    }
7429    
7430  /* Function: elfcore_write_note  /* Function: elfcore_write_note
7431    
7432     Inputs:     Inputs:
# Line 6914  elfcore_grok_netbsd_note (abfd, note) Line 7440  elfcore_grok_netbsd_note (abfd, note)
7440     End of buffer containing note.  */     End of buffer containing note.  */
7441    
7442  char *  char *
7443  elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)  elfcore_write_note (bfd  *abfd,
7444       bfd  *abfd;                      char *buf,
7445       char *buf;                      int  *bufsiz,
7446       int  *bufsiz;                      const char *name,
7447       const char *name;                      int  type,
7448       int  type;                      const void *input,
7449       const PTR input;                      int  size)
      int  size;  
7450  {  {
7451    Elf_External_Note *xnp;    Elf_External_Note *xnp;
7452    size_t namesz;    size_t namesz;
# Line 6933  elfcore_write_note (abfd, buf, bufsiz, n Line 7458  elfcore_write_note (abfd, buf, bufsiz, n
7458    pad = 0;    pad = 0;
7459    if (name != NULL)    if (name != NULL)
7460      {      {
7461        struct elf_backend_data *bed;        const struct elf_backend_data *bed;
7462    
7463        namesz = strlen (name) + 1;        namesz = strlen (name) + 1;
7464        bed = get_elf_backend_data (abfd);        bed = get_elf_backend_data (abfd);
7465        pad = -namesz & (bed->s->file_align - 1);        pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7466      }      }
7467    
7468    newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;    newspace = 12 + namesz + pad + size;
7469    
7470    p = realloc (buf, *bufsiz + newspace);    p = realloc (buf, *bufsiz + newspace);
7471    dest = p + *bufsiz;    dest = p + *bufsiz;
# Line 6966  elfcore_write_note (abfd, buf, bufsiz, n Line 7491  elfcore_write_note (abfd, buf, bufsiz, n
7491    
7492  #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)  #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7493  char *  char *
7494  elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)  elfcore_write_prpsinfo (bfd  *abfd,
7495       bfd  *abfd;                          char *buf,
7496       char *buf;                          int  *bufsiz,
7497       int  *bufsiz;                          const char *fname,
7498       const char *fname;                          const char *psargs)
      const char *psargs;  
7499  {  {
7500    int note_type;    int note_type;
7501    char *note_name = "CORE";    char *note_name = "CORE";
# Line 6994  elfcore_write_prpsinfo (abfd, buf, bufsi Line 7518  elfcore_write_prpsinfo (abfd, buf, bufsi
7518    
7519  #if defined (HAVE_PRSTATUS_T)  #if defined (HAVE_PRSTATUS_T)
7520  char *  char *
7521  elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)  elfcore_write_prstatus (bfd *abfd,
7522       bfd *abfd;                          char *buf,
7523       char *buf;                          int *bufsiz,
7524       int *bufsiz;                          long pid,
7525       long pid;                          int cursig,
7526       int cursig;                          const void *gregs)
      const PTR gregs;  
7527  {  {
7528    prstatus_t prstat;    prstatus_t prstat;
7529    char *note_name = "CORE";    char *note_name = "CORE";
# Line 7016  elfcore_write_prstatus (abfd, buf, bufsi Line 7539  elfcore_write_prstatus (abfd, buf, bufsi
7539    
7540  #if defined (HAVE_LWPSTATUS_T)  #if defined (HAVE_LWPSTATUS_T)
7541  char *  char *
7542  elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)  elfcore_write_lwpstatus (bfd *abfd,
7543       bfd *abfd;                           char *buf,
7544       char *buf;                           int *bufsiz,
7545       int *bufsiz;                           long pid,
7546       long pid;                           int cursig,
7547       int cursig;                           const void *gregs)
      const PTR gregs;  
7548  {  {
7549    lwpstatus_t lwpstat;    lwpstatus_t lwpstat;
7550    char *note_name = "CORE";    char *note_name = "CORE";
# Line 7048  elfcore_write_lwpstatus (abfd, buf, bufs Line 7570  elfcore_write_lwpstatus (abfd, buf, bufs
7570    
7571  #if defined (HAVE_PSTATUS_T)  #if defined (HAVE_PSTATUS_T)
7572  char *  char *
7573  elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)  elfcore_write_pstatus (bfd *abfd,
7574       bfd *abfd;                         char *buf,
7575       char *buf;                         int *bufsiz,
7576       int *bufsiz;                         long pid,
7577       long pid;                         int cursig,
7578       int cursig;                         const void *gregs)
      const PTR gregs;  
7579  {  {
7580    pstatus_t pstat;    pstatus_t pstat;
7581    char *note_name = "CORE";    char *note_name = "CORE";
# Line 7068  elfcore_write_pstatus (abfd, buf, bufsiz Line 7589  elfcore_write_pstatus (abfd, buf, bufsiz
7589  #endif /* HAVE_PSTATUS_T */  #endif /* HAVE_PSTATUS_T */
7590    
7591  char *  char *
7592  elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)  elfcore_write_prfpreg (bfd *abfd,
7593       bfd  *abfd;                         char *buf,
7594       char *buf;                         int *bufsiz,
7595       int  *bufsiz;                         const void *fpregs,
7596       const PTR fpregs;                         int size)
      int size;  
7597  {  {
7598    char *note_name = "CORE";    char *note_name = "CORE";
7599    return elfcore_write_note (abfd, buf, bufsiz,    return elfcore_write_note (abfd, buf, bufsiz,
# Line 7081  elfcore_write_prfpreg (abfd, buf, bufsiz Line 7601  elfcore_write_prfpreg (abfd, buf, bufsiz
7601  }  }
7602    
7603  char *  char *
7604  elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)  elfcore_write_prxfpreg (bfd *abfd,
7605       bfd  *abfd;                          char *buf,
7606       char *buf;                          int *bufsiz,
7607       int  *bufsiz;                          const void *xfpregs,
7608       const PTR xfpregs;                          int size)
      int size;  
7609  {  {
7610    char *note_name = "LINUX";    char *note_name = "LINUX";
7611    return elfcore_write_note (abfd, buf, bufsiz,    return elfcore_write_note (abfd, buf, bufsiz,
7612                               note_name, NT_PRXFPREG, xfpregs, size);                               note_name, NT_PRXFPREG, xfpregs, size);
7613  }  }
7614    
7615  static boolean  static bfd_boolean
7616  elfcore_read_notes (abfd, offset, size)  elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
      bfd *abfd;  
      file_ptr offset;  
      bfd_size_type size;  
7617  {  {
7618    char *buf;    char *buf;
7619    char *p;    char *p;
7620    
7621    if (size <= 0)    if (size <= 0)
7622      return true;      return TRUE;
7623    
7624    if (bfd_seek (abfd, offset, SEEK_SET) != 0)    if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7625      return false;      return FALSE;
7626    
7627    buf = bfd_malloc (size);    buf = bfd_malloc (size);
7628    if (buf == NULL)    if (buf == NULL)
7629      return false;      return FALSE;
7630    
7631    if (bfd_bread (buf, size, abfd) != size)    if (bfd_bread (buf, size, abfd) != size)
7632      {      {
7633      error:      error:
7634        free (buf);        free (buf);
7635        return false;        return FALSE;
7636      }      }
7637    
7638    p = buf;    p = buf;
# Line 7140  elfcore_read_notes (abfd, offset, size) Line 7656  elfcore_read_notes (abfd, offset, size)
7656            if (! elfcore_grok_netbsd_note (abfd, &in))            if (! elfcore_grok_netbsd_note (abfd, &in))
7657              goto error;              goto error;
7658          }          }
7659          else if (strncmp (in.namedata, "QNX", 3) == 0)
7660            {
7661              if (! elfcore_grok_nto_note (abfd, &in))
7662                goto error;
7663            }
7664        else        else
7665          {          {
7666            if (! elfcore_grok_note (abfd, &in))            if (! elfcore_grok_note (abfd, &in))
# Line 7150  elfcore_read_notes (abfd, offset, size) Line 7671  elfcore_read_notes (abfd, offset, size)
7671      }      }
7672    
7673    free (buf);    free (buf);
7674    return true;    return TRUE;
7675  }  }
7676    
7677  /* Providing external access to the ELF program header table.  */  /* Providing external access to the ELF program header table.  */
# Line 7160  elfcore_read_notes (abfd, offset, size) Line 7681  elfcore_read_notes (abfd, offset, size)
7681     occurs; bfd_get_error will return an appropriate code.  */     occurs; bfd_get_error will return an appropriate code.  */
7682    
7683  long  long
7684  bfd_get_elf_phdr_upper_bound (abfd)  bfd_get_elf_phdr_upper_bound (bfd *abfd)
      bfd *abfd;  
7685  {  {
7686    if (abfd->xvec->flavour != bfd_target_elf_flavour)    if (abfd->xvec->flavour != bfd_target_elf_flavour)
7687      {      {
# Line 7181  bfd_get_elf_phdr_upper_bound (abfd) Line 7701  bfd_get_elf_phdr_upper_bound (abfd)
7701     error occurs; bfd_get_error will return an appropriate code.  */     error occurs; bfd_get_error will return an appropriate code.  */
7702    
7703  int  int
7704  bfd_get_elf_phdrs (abfd, phdrs)  bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
      bfd *abfd;  
      void *phdrs;  
7705  {  {
7706    int num_phdrs;    int num_phdrs;
7707    
# Line 7201  bfd_get_elf_phdrs (abfd, phdrs) Line 7719  bfd_get_elf_phdrs (abfd, phdrs)
7719  }  }
7720    
7721  void  void
7722  _bfd_elf_sprintf_vma (abfd, buf, value)  _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
      bfd *abfd ATTRIBUTE_UNUSED;  
      char *buf;  
      bfd_vma value;  
7723  {  {
7724  #ifdef BFD64  #ifdef BFD64
7725    Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */    Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
# Line 7232  _bfd_elf_sprintf_vma (abfd, buf, value) Line 7747  _bfd_elf_sprintf_vma (abfd, buf, value)
7747  }  }
7748    
7749  void  void
7750  _bfd_elf_fprintf_vma (abfd, stream, value)  _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
      bfd *abfd ATTRIBUTE_UNUSED;  
      PTR stream;  
      bfd_vma value;  
7751  {  {
7752  #ifdef BFD64  #ifdef BFD64
7753    Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */    Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
# Line 7264  _bfd_elf_fprintf_vma (abfd, stream, valu Line 7776  _bfd_elf_fprintf_vma (abfd, stream, valu
7776  }  }
7777    
7778  enum elf_reloc_type_class  enum elf_reloc_type_class
7779  _bfd_elf_reloc_type_class (rela)  _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
      const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;  
7780  {  {
7781    return reloc_class_normal;    return reloc_class_normal;
7782  }  }
# Line 7274  _bfd_elf_reloc_type_class (rela) Line 7785  _bfd_elf_reloc_type_class (rela)
7785     relocation against a local symbol.  */     relocation against a local symbol.  */
7786    
7787  bfd_vma  bfd_vma
7788  _bfd_elf_rela_local_sym (abfd, sym, sec, rel)  _bfd_elf_rela_local_sym (bfd *abfd,
7789       bfd *abfd;                           Elf_Internal_Sym *sym,
7790       Elf_Internal_Sym *sym;                           asection **psec,
7791       asection *sec;                           Elf_Internal_Rela *rel)
      Elf_Internal_Rela *rel;  
7792  {  {
7793      asection *sec = *psec;
7794    bfd_vma relocation;    bfd_vma relocation;
7795    
7796    relocation = (sec->output_section->vma    relocation = (sec->output_section->vma
# Line 7287  _bfd_elf_rela_local_sym (abfd, sym, sec, Line 7798  _bfd_elf_rela_local_sym (abfd, sym, sec,
7798                  + sym->st_value);                  + sym->st_value);
7799    if ((sec->flags & SEC_MERGE)    if ((sec->flags & SEC_MERGE)
7800        && ELF_ST_TYPE (sym->st_info) == STT_SECTION        && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7801        && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)        && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7802      {      {
       asection *msec;  
   
       msec = sec;  
7803        rel->r_addend =        rel->r_addend =
7804          _bfd_merged_section_offset (abfd, &msec,          _bfd_merged_section_offset (abfd, psec,
7805                                      elf_section_data (sec)->sec_info,                                      elf_section_data (sec)->sec_info,
7806                                      sym->st_value + rel->r_addend,                                      sym->st_value + rel->r_addend);
7807                                      (bfd_vma) 0)        if (sec != *psec)
7808          - relocation;          {
7809        rel->r_addend += msec->output_section->vma + msec->output_offset;            /* If we have changed the section, and our original section is
7810                 marked with SEC_EXCLUDE, it means that the original
7811                 SEC_MERGE section has been completely subsumed in some
7812                 other SEC_MERGE section.  In this case, we need to leave
7813                 some info around for --emit-relocs.  */
7814              if ((sec->flags & SEC_EXCLUDE) != 0)
7815                sec->kept_section = *psec;
7816              sec = *psec;
7817            }
7818          rel->r_addend -= relocation;
7819          rel->r_addend += sec->output_section->vma + sec->output_offset;
7820      }      }
7821    return relocation;    return relocation;
7822  }  }
7823    
7824  bfd_vma  bfd_vma
7825  _bfd_elf_rel_local_sym (abfd, sym, psec, addend)  _bfd_elf_rel_local_sym (bfd *abfd,
7826       bfd *abfd;                          Elf_Internal_Sym *sym,
7827       Elf_Internal_Sym *sym;                          asection **psec,
7828       asection **psec;                          bfd_vma addend)
      bfd_vma addend;  
7829  {  {
7830    asection *sec = *psec;    asection *sec = *psec;
7831    
7832    if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)    if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7833      return sym->st_value + addend;      return sym->st_value + addend;
7834    
7835    return _bfd_merged_section_offset (abfd, psec,    return _bfd_merged_section_offset (abfd, psec,
7836                                       elf_section_data (sec)->sec_info,                                       elf_section_data (sec)->sec_info,
7837                                       sym->st_value + addend, (bfd_vma) 0);                                       sym->st_value + addend);
7838  }  }
7839    
7840  bfd_vma  bfd_vma
7841  _bfd_elf_section_offset (abfd, info, sec, offset)  _bfd_elf_section_offset (bfd *abfd,
7842       bfd *abfd;                           struct bfd_link_info *info,
7843       struct bfd_link_info *info;                           asection *sec,
7844       asection *sec;                           bfd_vma offset)
      bfd_vma offset;  
7845  {  {
7846    struct bfd_elf_section_data *sec_data;    switch (sec->sec_info_type)
   
   sec_data = elf_section_data (sec);  
   switch (sec_data->sec_info_type)  
7847      {      {
7848      case ELF_INFO_TYPE_STABS:      case ELF_INFO_TYPE_STABS:
7849        return _bfd_stab_section_offset        return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
7850          (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,                                         offset);
          offset);  
7851      case ELF_INFO_TYPE_EH_FRAME:      case ELF_INFO_TYPE_EH_FRAME:
7852        return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);        return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
7853      default:      default:
7854        return offset;        return offset;
7855      }      }
7856  }  }
7857    
7858    /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
7859       reconstruct an ELF file by reading the segments out of remote memory
7860       based on the ELF file header at EHDR_VMA and the ELF program headers it
7861       points to.  If not null, *LOADBASEP is filled in with the difference
7862       between the VMAs from which the segments were read, and the VMAs the
7863       file headers (and hence BFD's idea of each section's VMA) put them at.
7864    
7865       The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7866       remote memory at target address VMA into the local buffer at MYADDR; it
7867       should return zero on success or an `errno' code on failure.  TEMPL must
7868       be a BFD for an ELF target with the word size and byte order found in
7869       the remote memory.  */
7870    
7871    bfd *
7872    bfd_elf_bfd_from_remote_memory
7873      (bfd *templ,
7874       bfd_vma ehdr_vma,
7875       bfd_vma *loadbasep,
7876       int (*target_read_memory) (bfd_vma, bfd_byte *, int))
7877    {
7878      return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7879        (templ, ehdr_vma, loadbasep, target_read_memory);
7880    }
7881    
7882    long
7883    _bfd_elf_get_synthetic_symtab (bfd *abfd,
7884                                   long symcount ATTRIBUTE_UNUSED,
7885                                   asymbol **syms ATTRIBUTE_UNUSED,
7886                                   long dynsymcount,
7887                                   asymbol **dynsyms,
7888                                   asymbol **ret)
7889    {
7890      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7891      asection *relplt;
7892      asymbol *s;
7893      const char *relplt_name;
7894      bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7895      arelent *p;
7896      long count, i, n;
7897      size_t size;
7898      Elf_Internal_Shdr *hdr;
7899      char *names;
7900      asection *plt;
7901    
7902      *ret = NULL;
7903    
7904      if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
7905        return 0;
7906    
7907      if (dynsymcount <= 0)
7908        return 0;
7909    
7910      if (!bed->plt_sym_val)
7911        return 0;
7912    
7913      relplt_name = bed->relplt_name;
7914      if (relplt_name == NULL)
7915        relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7916      relplt = bfd_get_section_by_name (abfd, relplt_name);
7917      if (relplt == NULL)
7918        return 0;
7919    
7920      hdr = &elf_section_data (relplt)->this_hdr;
7921      if (hdr->sh_link != elf_dynsymtab (abfd)
7922          || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7923        return 0;
7924    
7925      plt = bfd_get_section_by_name (abfd, ".plt");
7926      if (plt == NULL)
7927        return 0;
7928    
7929      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7930      if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7931        return -1;
7932    
7933      count = relplt->size / hdr->sh_entsize;
7934      size = count * sizeof (asymbol);
7935      p = relplt->relocation;
7936      for (i = 0; i < count; i++, s++, p++)
7937        size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7938    
7939      s = *ret = bfd_malloc (size);
7940      if (s == NULL)
7941        return -1;
7942    
7943      names = (char *) (s + count);
7944      p = relplt->relocation;
7945      n = 0;
7946      for (i = 0; i < count; i++, s++, p++)
7947        {
7948          size_t len;
7949          bfd_vma addr;
7950    
7951          addr = bed->plt_sym_val (i, plt, p);
7952          if (addr == (bfd_vma) -1)
7953            continue;
7954    
7955          *s = **p->sym_ptr_ptr;
7956          s->section = plt;
7957          s->value = addr - plt->vma;
7958          s->name = names;
7959          len = strlen ((*p->sym_ptr_ptr)->name);
7960          memcpy (names, (*p->sym_ptr_ptr)->name, len);
7961          names += len;
7962          memcpy (names, "@plt", sizeof ("@plt"));
7963          names += sizeof ("@plt");
7964          ++n;
7965        }
7966    
7967      return n;
7968    }
7969    
7970    /* Sort symbol by binding and section. We want to put definitions
7971       sorted by section at the beginning.  */
7972    
7973    static int
7974    elf_sort_elf_symbol (const void *arg1, const void *arg2)
7975    {
7976      const Elf_Internal_Sym *s1;
7977      const Elf_Internal_Sym *s2;
7978      int shndx;
7979    
7980      /* Make sure that undefined symbols are at the end.  */
7981      s1 = (const Elf_Internal_Sym *) arg1;
7982      if (s1->st_shndx == SHN_UNDEF)
7983        return 1;
7984      s2 = (const Elf_Internal_Sym *) arg2;
7985      if (s2->st_shndx == SHN_UNDEF)
7986        return -1;
7987    
7988      /* Sorted by section index.  */
7989      shndx = s1->st_shndx - s2->st_shndx;
7990      if (shndx != 0)
7991        return shndx;
7992    
7993      /* Sorted by binding.  */
7994      return ELF_ST_BIND (s1->st_info)  - ELF_ST_BIND (s2->st_info);
7995    }
7996    
7997    struct elf_symbol
7998    {
7999      Elf_Internal_Sym *sym;
8000      const char *name;
8001    };
8002    
8003    static int
8004    elf_sym_name_compare (const void *arg1, const void *arg2)
8005    {
8006      const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8007      const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8008      return strcmp (s1->name, s2->name);
8009    }
8010    
8011    /* Check if 2 sections define the same set of local and global
8012       symbols.  */
8013    
8014    bfd_boolean
8015    bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2)
8016    {
8017      bfd *bfd1, *bfd2;
8018      const struct elf_backend_data *bed1, *bed2;
8019      Elf_Internal_Shdr *hdr1, *hdr2;
8020      bfd_size_type symcount1, symcount2;
8021      Elf_Internal_Sym *isymbuf1, *isymbuf2;
8022      Elf_Internal_Sym *isymstart1 = NULL, *isymstart2 = NULL, *isym;
8023      Elf_Internal_Sym *isymend;
8024      struct elf_symbol *symp, *symtable1 = NULL, *symtable2 = NULL;
8025      bfd_size_type count1, count2, i;
8026      int shndx1, shndx2;
8027      bfd_boolean result;
8028    
8029      bfd1 = sec1->owner;
8030      bfd2 = sec2->owner;
8031    
8032      /* If both are .gnu.linkonce sections, they have to have the same
8033         section name.  */
8034      if (strncmp (sec1->name, ".gnu.linkonce",
8035                   sizeof ".gnu.linkonce" - 1) == 0
8036          && strncmp (sec2->name, ".gnu.linkonce",
8037                      sizeof ".gnu.linkonce" - 1) == 0)
8038        return strcmp (sec1->name + sizeof ".gnu.linkonce",
8039                       sec2->name + sizeof ".gnu.linkonce") == 0;
8040    
8041      /* Both sections have to be in ELF.  */
8042      if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8043          || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8044        return FALSE;
8045    
8046      if (elf_section_type (sec1) != elf_section_type (sec2))
8047        return FALSE;
8048    
8049      if ((elf_section_flags (sec1) & SHF_GROUP) != 0
8050          && (elf_section_flags (sec2) & SHF_GROUP) != 0)
8051        {
8052          /* If both are members of section groups, they have to have the
8053             same group name.  */
8054          if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
8055            return FALSE;
8056        }
8057    
8058      shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8059      shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8060      if (shndx1 == -1 || shndx2 == -1)
8061        return FALSE;
8062    
8063      bed1 = get_elf_backend_data (bfd1);
8064      bed2 = get_elf_backend_data (bfd2);
8065      hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8066      symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8067      hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8068      symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8069    
8070      if (symcount1 == 0 || symcount2 == 0)
8071        return FALSE;
8072    
8073      isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8074                                       NULL, NULL, NULL);
8075      isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8076                                       NULL, NULL, NULL);
8077    
8078      result = FALSE;
8079      if (isymbuf1 == NULL || isymbuf2 == NULL)
8080        goto done;
8081    
8082      /* Sort symbols by binding and section. Global definitions are at
8083         the beginning.  */
8084      qsort (isymbuf1, symcount1, sizeof (Elf_Internal_Sym),
8085             elf_sort_elf_symbol);
8086      qsort (isymbuf2, symcount2, sizeof (Elf_Internal_Sym),
8087             elf_sort_elf_symbol);
8088    
8089      /* Count definitions in the section.  */
8090      count1 = 0;
8091      for (isym = isymbuf1, isymend = isym + symcount1;
8092           isym < isymend; isym++)
8093        {
8094          if (isym->st_shndx == (unsigned int) shndx1)
8095            {
8096              if (count1 == 0)
8097                isymstart1 = isym;
8098              count1++;
8099            }
8100    
8101          if (count1 && isym->st_shndx != (unsigned int) shndx1)
8102            break;
8103        }
8104    
8105      count2 = 0;
8106      for (isym = isymbuf2, isymend = isym + symcount2;
8107           isym < isymend; isym++)
8108        {
8109          if (isym->st_shndx == (unsigned int) shndx2)
8110            {
8111              if (count2 == 0)
8112                isymstart2 = isym;
8113              count2++;
8114            }
8115    
8116          if (count2 && isym->st_shndx != (unsigned int) shndx2)
8117            break;
8118        }
8119    
8120      if (count1 == 0 || count2 == 0 || count1 != count2)
8121        goto done;
8122    
8123      symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8124      symtable2 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8125    
8126      if (symtable1 == NULL || symtable2 == NULL)
8127        goto done;
8128    
8129      symp = symtable1;
8130      for (isym = isymstart1, isymend = isym + count1;
8131           isym < isymend; isym++)
8132        {
8133          symp->sym = isym;
8134          symp->name = bfd_elf_string_from_elf_section (bfd1,
8135                                                        hdr1->sh_link,
8136                                                        isym->st_name);
8137          symp++;
8138        }
8139    
8140      symp = symtable2;
8141      for (isym = isymstart2, isymend = isym + count1;
8142           isym < isymend; isym++)
8143        {
8144          symp->sym = isym;
8145          symp->name = bfd_elf_string_from_elf_section (bfd2,
8146                                                        hdr2->sh_link,
8147                                                        isym->st_name);
8148          symp++;
8149        }
8150      
8151      /* Sort symbol by name.  */
8152      qsort (symtable1, count1, sizeof (struct elf_symbol),
8153             elf_sym_name_compare);
8154      qsort (symtable2, count1, sizeof (struct elf_symbol),
8155             elf_sym_name_compare);
8156    
8157      for (i = 0; i < count1; i++)
8158        /* Two symbols must have the same binding, type and name.  */
8159        if (symtable1 [i].sym->st_info != symtable2 [i].sym->st_info
8160            || symtable1 [i].sym->st_other != symtable2 [i].sym->st_other
8161            || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8162          goto done;
8163    
8164      result = TRUE;
8165    
8166    done:
8167      if (symtable1)
8168        free (symtable1);
8169      if (symtable2)
8170        free (symtable2);
8171      if (isymbuf1)
8172        free (isymbuf1);
8173      if (isymbuf2)
8174        free (isymbuf2);
8175    
8176      return result;
8177    }

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26