Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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