Subversion Repositories Kolibri OS

Rev

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

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