Ignore:
Timestamp:
Feb 6, 2017, 1:00:00 PM (8 years ago)
Author:
Silvan Scherrer
Message:

binutils: update trunk to version 2.27

Location:
binutils/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • binutils/trunk

  • TabularUnified binutils/trunk/bfd/elf.c

    r970 r1973  
    11/* ELF executable support for BFD.
    22
    3    Copyright (C) 1993-2014 Free Software Foundation, Inc.
     3   Copyright (C) 1993-2016 Free Software Foundation, Inc.
    44
    55   This file is part of BFD, the Binary File Descriptor library.
     
    4343#include "libiberty.h"
    4444#include "safe-ctype.h"
    45 #include "elf-linux-psinfo.h"
     45#include "elf-linux-core.h"
    4646
    4747#ifdef CORE_HEADER
     
    5252static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
    5353static bfd_boolean prep_headers (bfd *);
    54 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
     54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
    5555static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
    5656static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
     
    298298         in case the string table is not terminated.  */
    299299      if (shstrtabsize + 1 <= 1
    300           || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
    301           || bfd_seek (abfd, offset, SEEK_SET) != 0)
     300          || bfd_seek (abfd, offset, SEEK_SET) != 0
     301          || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
    302302        shstrtab = NULL;
    303303      else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
     
    305305          if (bfd_get_error () != bfd_error_system_call)
    306306            bfd_set_error (bfd_error_file_truncated);
     307          bfd_release (abfd, shstrtab);
    307308          shstrtab = NULL;
    308309          /* Once we've failed to read it, make sure we don't keep
     
    333334  hdr = elf_elfsections (abfd)[shindex];
    334335
    335   if (hdr->contents == NULL
    336       && bfd_elf_get_str_section (abfd, shindex) == NULL)
    337     return NULL;
     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    }
    338349
    339350  if (strindex >= hdr->sh_size)
     
    390401  /* Normal syms might have section extension entries.  */
    391402  shndx_hdr = NULL;
    392   if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
    393     shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
     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        {
     411          /* PR 20063.  */
     412          if (entry->hdr.sh_link >= elf_numsections (ibfd))
     413            continue;
     414
     415          if (sections[entry->hdr.sh_link] == symtab_hdr)
     416            {
     417              shndx_hdr = & entry->hdr;
     418              break;
     419            };
     420        }
     421
     422      if (shndx_hdr == NULL)
     423        {
     424          if (symtab_hdr == & elf_symtab_hdr (ibfd))
     425            /* Not really accurate, but this was how the old code used to work.  */
     426            shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
     427          /* Otherwise we do nothing.  The assumption is that
     428             the index table will not be needed.  */
     429        }
     430    }
    394431
    395432  /* Read the symbols.  */
     
    399436  bed = get_elf_backend_data (ibfd);
    400437  extsym_size = bed->s->sizeof_sym;
    401   amt = symcount * extsym_size;
     438  amt = (bfd_size_type) symcount * extsym_size;
    402439  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    403440  if (extsym_buf == NULL)
     
    418455  else
    419456    {
    420       amt = symcount * sizeof (Elf_External_Sym_Shndx);
     457      amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
    421458      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    422459      if (extshndx_buf == NULL)
     
    637674                  src = shdr->contents + shdr->sh_size;
    638675                  dest = (Elf_Internal_Group *) (shdr->contents + amt);
     676
    639677                  while (1)
    640678                    {
     
    806844    {
    807845      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    808       Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
    809       unsigned int n_elt = shdr->sh_size / 4;
     846      Elf_Internal_Group *idx;
     847      unsigned int n_elt;
     848
     849      /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
     850      if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
     851        {
     852          (*_bfd_error_handler)
     853            (_("%B: section group entry number %u is corrupt"),
     854             abfd, i);
     855          result = FALSE;
     856          continue;
     857        }
     858
     859      idx = (Elf_Internal_Group *) shdr->contents;
     860      n_elt = shdr->sh_size / 4;
    810861
    811862      while (--n_elt != 0)
     
    844895}
    845896
     897static char *
     898convert_debug_to_zdebug (bfd *abfd, const char *name)
     899{
     900  unsigned int len = strlen (name);
     901  char *new_name = bfd_alloc (abfd, len + 2);
     902  if (new_name == NULL)
     903    return NULL;
     904  new_name[0] = '.';
     905  new_name[1] = 'z';
     906  memcpy (new_name + 2, name + 1, len);
     907  return new_name;
     908}
     909
     910static char *
     911convert_zdebug_to_debug (bfd *abfd, const char *name)
     912{
     913  unsigned int len = strlen (name);
     914  char *new_name = bfd_alloc (abfd, len);
     915  if (new_name == NULL)
     916    return NULL;
     917  new_name[0] = '.';
     918  memcpy (new_name + 1, name + 2, len - 1);
     919  return new_name;
     920}
     921
    846922/* Make a BFD section from an ELF section.  We store a pointer to the
    847923   BFD section in the bfd_section field of the header.  */
     
    901977      flags |= SEC_MERGE;
    902978      newsect->entsize = hdr->sh_entsize;
    903       if ((hdr->sh_flags & SHF_STRINGS) != 0)
    904         flags |= SEC_STRINGS;
    905     }
     979    }
     980  if ((hdr->sh_flags & SHF_STRINGS) != 0)
     981    flags |= SEC_STRINGS;
    906982  if (hdr->sh_flags & SHF_GROUP)
    907983    if (!setup_group (abfd, hdr, newsect))
     
    10301106    {
    10311107      enum { nothing, compress, decompress } action = nothing;
    1032       char *new_name;
    1033 
    1034       if (bfd_is_section_compressed (abfd, newsect))
     1108      int compression_header_size;
     1109      bfd_size_type uncompressed_size;
     1110      bfd_boolean compressed
     1111        = bfd_is_section_compressed_with_header (abfd, newsect,
     1112                                                 &compression_header_size,
     1113                                                 &uncompressed_size);
     1114
     1115      if (compressed)
    10351116        {
    10361117          /* Compressed section.  Check if we should decompress.  */
     
    10381119            action = decompress;
    10391120        }
    1040       else
    1041         {
    1042           /* Normal section.  Check if we should compress.  */
    1043           if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
     1121
     1122      /* Compress the uncompressed section or convert from/to .zdebug*
     1123         section.  Check if we should compress.  */
     1124      if (action == nothing)
     1125        {
     1126          if (newsect->size != 0
     1127              && (abfd->flags & BFD_COMPRESS)
     1128              && compression_header_size >= 0
     1129              && uncompressed_size > 0
     1130              && (!compressed
     1131                  || ((compression_header_size > 0)
     1132                      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
    10441133            action = compress;
    1045         }
    1046 
    1047       new_name = NULL;
    1048       switch (action)
    1049         {
    1050         case nothing:
    1051           break;
    1052         case compress:
     1134          else
     1135            return TRUE;
     1136        }
     1137
     1138      if (action == compress)
     1139        {
    10531140          if (!bfd_init_section_compress_status (abfd, newsect))
    10541141            {
     
    10581145              return FALSE;
    10591146            }
    1060           if (name[1] != 'z')
    1061             {
    1062               unsigned int len = strlen (name);
    1063 
    1064               new_name = bfd_alloc (abfd, len + 2);
    1065               if (new_name == NULL)
    1066                 return FALSE;
    1067               new_name[0] = '.';
    1068               new_name[1] = 'z';
    1069               memcpy (new_name + 2, name + 1, len);
    1070             }
    1071           break;
    1072         case decompress:
     1147        }
     1148      else
     1149        {
    10731150          if (!bfd_init_section_decompress_status (abfd, newsect))
    10741151            {
     
    10781155              return FALSE;
    10791156            }
    1080           if (name[1] == 'z')
     1157        }
     1158
     1159      if (abfd->is_linker_input)
     1160        {
     1161          if (name[1] == 'z'
     1162              && (action == decompress
     1163                  || (action == compress
     1164                      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
    10811165            {
    1082               unsigned int len = strlen (name);
    1083 
    1084               new_name = bfd_alloc (abfd, len);
     1166              /* Convert section name from .zdebug_* to .debug_* so
     1167                 that linker will consider this section as a debug
     1168                 section.  */
     1169              char *new_name = convert_zdebug_to_debug (abfd, name);
    10851170              if (new_name == NULL)
    10861171                return FALSE;
    1087               new_name[0] = '.';
    1088               memcpy (new_name + 1, name + 2, len - 1);
     1172              bfd_rename_section (abfd, newsect, new_name);
    10891173            }
    1090           break;
    1091         }
    1092       if (new_name != NULL)
    1093         bfd_rename_section (abfd, newsect, new_name);
     1174        }
     1175      else
     1176        /* For objdump, don't rename the section.  For objcopy, delay
     1177           section rename to elf_fake_sections.  */
     1178        newsect->flags |= SEC_ELF_RENAME;
    10941179    }
    10951180
     
    10971182}
    10981183
    1099 const char *const bfd_elf_section_type_names[] = {
     1184const char *const bfd_elf_section_type_names[] =
     1185{
    11001186  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
    11011187  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
     
    11351221
    11361222
     1223/* Returns TRUE if section A matches section B.
     1224   Names, addresses and links may be different, but everything else
     1225   should be the same.  */
     1226
     1227static bfd_boolean
     1228section_match (const Elf_Internal_Shdr * a,
     1229               const Elf_Internal_Shdr * b)
     1230{
     1231  return
     1232    a->sh_type         == b->sh_type
     1233    && (a->sh_flags & ~ SHF_INFO_LINK)
     1234    == (b->sh_flags & ~ SHF_INFO_LINK)
     1235    && a->sh_addralign == b->sh_addralign
     1236    && a->sh_size      == b->sh_size
     1237    && a->sh_entsize   == b->sh_entsize
     1238    /* FIXME: Check sh_addr ?  */
     1239    ;
     1240}
     1241
     1242/* Find a section in OBFD that has the same characteristics
     1243   as IHEADER.  Return the index of this section or SHN_UNDEF if
     1244   none can be found.  Check's section HINT first, as this is likely
     1245   to be the correct section.  */
     1246
     1247static unsigned int
     1248find_link (const bfd * obfd, const Elf_Internal_Shdr * iheader, const unsigned int hint)
     1249{
     1250  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
     1251  unsigned int i;
     1252
     1253  if (section_match (oheaders[hint], iheader))
     1254    return hint;
     1255
     1256  for (i = 1; i < elf_numsections (obfd); i++)
     1257    {
     1258      Elf_Internal_Shdr * oheader = oheaders[i];
     1259
     1260      if (section_match (oheader, iheader))
     1261        /* FIXME: Do we care if there is a potential for
     1262           multiple matches ?  */
     1263        return i;
     1264    }
     1265
     1266  return SHN_UNDEF;
     1267}
     1268
     1269/* PR 19938: Attempt to set the ELF section header fields of an OS or
     1270   Processor specific section, based upon a matching input section.
     1271   Returns TRUE upon success, FALSE otherwise.  */
     1272   
     1273static bfd_boolean
     1274copy_special_section_fields (const bfd *ibfd,
     1275                             bfd *obfd,
     1276                             const Elf_Internal_Shdr *iheader,
     1277                             Elf_Internal_Shdr *oheader,
     1278                             const unsigned int secnum)
     1279{
     1280  const struct elf_backend_data *bed = get_elf_backend_data (obfd);
     1281  const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
     1282  bfd_boolean changed = FALSE;
     1283  unsigned int sh_link;
     1284
     1285  if (oheader->sh_type == SHT_NOBITS)
     1286    {
     1287      /* This is a feature for objcopy --only-keep-debug:
     1288         When a section's type is changed to NOBITS, we preserve
     1289         the sh_link and sh_info fields so that they can be
     1290         matched up with the original.
     1291
     1292         Note: Strictly speaking these assignments are wrong.
     1293         The sh_link and sh_info fields should point to the
     1294         relevent sections in the output BFD, which may not be in
     1295         the same location as they were in the input BFD.  But
     1296         the whole point of this action is to preserve the
     1297         original values of the sh_link and sh_info fields, so
     1298         that they can be matched up with the section headers in
     1299         the original file.  So strictly speaking we may be
     1300         creating an invalid ELF file, but it is only for a file
     1301         that just contains debug info and only for sections
     1302         without any contents.  */
     1303      if (oheader->sh_link == 0)
     1304        oheader->sh_link = iheader->sh_link;
     1305      if (oheader->sh_info == 0)
     1306        oheader->sh_info = iheader->sh_info;
     1307      return TRUE;
     1308    }
     1309
     1310  /* Allow the target a chance to decide how these fields should be set.  */
     1311  if (bed->elf_backend_copy_special_section_fields != NULL
     1312      && bed->elf_backend_copy_special_section_fields
     1313      (ibfd, obfd, iheader, oheader))
     1314    return TRUE;
     1315
     1316  /* We have an iheader which might match oheader, and which has non-zero
     1317     sh_info and/or sh_link fields.  Attempt to follow those links and find
     1318     the section in the output bfd which corresponds to the linked section
     1319     in the input bfd.  */
     1320  if (iheader->sh_link != SHN_UNDEF)
     1321    {
     1322      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
     1323      if (sh_link != SHN_UNDEF)
     1324        {
     1325          oheader->sh_link = sh_link;
     1326          changed = TRUE;
     1327        }
     1328      else
     1329        /* FIXME: Should we install iheader->sh_link
     1330           if we could not find a match ?  */
     1331        (* _bfd_error_handler)
     1332          (_("%B: Failed to find link section for section %d"), obfd, secnum);
     1333    }
     1334
     1335  if (iheader->sh_info)
     1336    {
     1337      /* The sh_info field can hold arbitrary information, but if the
     1338         SHF_LINK_INFO flag is set then it should be interpreted as a
     1339         section index.  */
     1340      if (iheader->sh_flags & SHF_INFO_LINK)
     1341        {
     1342          sh_link = find_link (obfd, iheaders[iheader->sh_info],
     1343                               iheader->sh_info);
     1344          if (sh_link != SHN_UNDEF)
     1345            oheader->sh_flags |= SHF_INFO_LINK;
     1346        }
     1347      else
     1348        /* No idea what it means - just copy it.  */
     1349        sh_link = iheader->sh_info;
     1350
     1351      if (sh_link != SHN_UNDEF)
     1352        {
     1353          oheader->sh_info = sh_link;
     1354          changed = TRUE;
     1355        }
     1356      else
     1357        (* _bfd_error_handler)
     1358          (_("%B: Failed to find info section for section %d"), obfd, secnum);
     1359    }
     1360
     1361  return changed;
     1362}
     1363 
    11371364/* Copy the program header and other data from one object module to
    11381365   another.  */
     
    11411368_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
    11421369{
     1370  const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
     1371  Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
     1372  const struct elf_backend_data *bed;
     1373  unsigned int i;
     1374
    11431375  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
    1144       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
     1376    || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
    11451377    return TRUE;
    11461378
     
    11571389    elf_elfheader (ibfd)->e_ident[EI_OSABI];
    11581390
     1391  /* If set, copy the EI_ABIVERSION field.  */
     1392  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
     1393    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
     1394      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
     1395 
    11591396  /* Copy object attributes.  */
    11601397  _bfd_elf_copy_obj_attributes (ibfd, obfd);
     1398
     1399  if (iheaders == NULL || oheaders == NULL)
     1400    return TRUE;
     1401
     1402  bed = get_elf_backend_data (obfd);
     1403
     1404  /* Possibly copy other fields in the section header.  */
     1405  for (i = 1; i < elf_numsections (obfd); i++)
     1406    {
     1407      unsigned int j;
     1408      Elf_Internal_Shdr * oheader = oheaders[i];
     1409
     1410      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
     1411         because of a special case need for generating separate debug info
     1412         files.  See below for more details.  */
     1413      if (oheader == NULL
     1414          || (oheader->sh_type != SHT_NOBITS
     1415              && oheader->sh_type < SHT_LOOS))
     1416        continue;
     1417
     1418      /* Ignore empty sections, and sections whose
     1419         fields have already been initialised.  */
     1420      if (oheader->sh_size == 0
     1421          || (oheader->sh_info != 0 && oheader->sh_link != 0))
     1422        continue;
     1423
     1424      /* Scan for the matching section in the input bfd.
     1425         First we try for a direct mapping between the input and output sections.  */
     1426      for (j = 1; j < elf_numsections (ibfd); j++)
     1427        {
     1428          const Elf_Internal_Shdr * iheader = iheaders[j];
     1429
     1430          if (iheader == NULL)
     1431            continue;
     1432
     1433          if (oheader->bfd_section != NULL
     1434              && iheader->bfd_section != NULL
     1435              && iheader->bfd_section->output_section != NULL
     1436              && iheader->bfd_section->output_section == oheader->bfd_section)
     1437            {
     1438              /* We have found a connection from the input section to the
     1439                 output section.  Attempt to copy the header fields.  If
     1440                 this fails then do not try any further sections - there
     1441                 should only be a one-to-one mapping between input and output. */
     1442              if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
     1443                j = elf_numsections (ibfd);
     1444              break;
     1445            }
     1446        }
     1447
     1448      if (j < elf_numsections (ibfd))
     1449        continue;
     1450
     1451      /* That failed.  So try to deduce the corresponding input section.
     1452         Unfortunately we cannot compare names as the output string table
     1453         is empty, so instead we check size, address and type.  */
     1454      for (j = 1; j < elf_numsections (ibfd); j++)
     1455        {
     1456          const Elf_Internal_Shdr * iheader = iheaders[j];
     1457
     1458          if (iheader == NULL)
     1459            continue;
     1460
     1461          /* Try matching fields in the input section's header.
     1462             Since --only-keep-debug turns all non-debug sections into
     1463             SHT_NOBITS sections, the output SHT_NOBITS type matches any
     1464             input type.  */
     1465          if ((oheader->sh_type == SHT_NOBITS
     1466               || iheader->sh_type == oheader->sh_type)
     1467              && (iheader->sh_flags & ~ SHF_INFO_LINK)
     1468              == (oheader->sh_flags & ~ SHF_INFO_LINK)
     1469              && iheader->sh_addralign == oheader->sh_addralign
     1470              && iheader->sh_entsize == oheader->sh_entsize
     1471              && iheader->sh_size == oheader->sh_size
     1472              && iheader->sh_addr == oheader->sh_addr
     1473              && (iheader->sh_info != oheader->sh_info
     1474                  || iheader->sh_link != oheader->sh_link))
     1475            {
     1476              if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
     1477                break;
     1478            }
     1479        }
     1480
     1481      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
     1482        {
     1483          /* Final attempt.  Call the backend copy function
     1484             with a NULL input section.  */
     1485          if (bed->elf_backend_copy_special_section_fields != NULL)
     1486            bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
     1487        }
     1488    }
     1489
    11611490  return TRUE;
    11621491}
     
    12551584
    12561585      extdyn = dynbuf;
     1586      /* PR 17512: file: 6f427532.  */
     1587      if (s->size < extdynsize)
     1588        goto error_return;
    12571589      extdynend = extdyn + s->size;
    1258       for (; extdyn < extdynend; extdyn += extdynsize)
     1590      /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
     1591         Fix range check.  */
     1592      for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
    12591593        {
    12601594          Elf_Internal_Dyn dyn;
     
    14241758}
    14251759
     1760/* Get version string.  */
     1761
     1762const char *
     1763_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
     1764                                    bfd_boolean *hidden)
     1765{
     1766  const char *version_string = NULL;
     1767  if (elf_dynversym (abfd) != 0
     1768      && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
     1769    {
     1770      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
     1771
     1772      *hidden = (vernum & VERSYM_HIDDEN) != 0;
     1773      vernum &= VERSYM_VERSION;
     1774
     1775      if (vernum == 0)
     1776        version_string = "";
     1777      else if (vernum == 1)
     1778        version_string = "Base";
     1779      else if (vernum <= elf_tdata (abfd)->cverdefs)
     1780        version_string =
     1781          elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
     1782      else
     1783        {
     1784          Elf_Internal_Verneed *t;
     1785
     1786          version_string = "";
     1787          for (t = elf_tdata (abfd)->verref;
     1788               t != NULL;
     1789               t = t->vn_nextref)
     1790            {
     1791              Elf_Internal_Vernaux *a;
     1792
     1793              for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
     1794                {
     1795                  if (a->vna_other == vernum)
     1796                    {
     1797                      version_string = a->vna_nodename;
     1798                      break;
     1799                    }
     1800                }
     1801            }
     1802        }
     1803    }
     1804  return version_string;
     1805}
     1806
    14261807/* Display ELF-specific fields of a symbol.  */
    14271808
     
    14501831        unsigned char st_other;
    14511832        bfd_vma val;
     1833        const char *version_string;
     1834        bfd_boolean hidden;
    14521835
    14531836        section_name = symbol->section ? symbol->section->name : "(*none*)";
     
    14751858
    14761859        /* If we have version information, print it.  */
    1477         if (elf_dynversym (abfd) != 0
    1478             && (elf_dynverdef (abfd) != 0
    1479                 || elf_dynverref (abfd) != 0))
     1860        version_string = _bfd_elf_get_symbol_version_string (abfd,
     1861                                                             symbol,
     1862                                                             &hidden);
     1863        if (version_string)
    14801864          {
    1481             unsigned int vernum;
    1482             const char *version_string;
    1483 
    1484             vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
    1485 
    1486             if (vernum == 0)
    1487               version_string = "";
    1488             else if (vernum == 1)
    1489               version_string = "Base";
    1490             else if (vernum <= elf_tdata (abfd)->cverdefs)
    1491               version_string =
    1492                 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
    1493             else
    1494               {
    1495                 Elf_Internal_Verneed *t;
    1496 
    1497                 version_string = "";
    1498                 for (t = elf_tdata (abfd)->verref;
    1499                      t != NULL;
    1500                      t = t->vn_nextref)
    1501                   {
    1502                     Elf_Internal_Vernaux *a;
    1503 
    1504                     for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
    1505                       {
    1506                         if (a->vna_other == vernum)
    1507                           {
    1508                             version_string = a->vna_nodename;
    1509                             break;
    1510                           }
    1511                       }
    1512                   }
    1513               }
    1514 
    1515             if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
     1865            if (!hidden)
    15161866              fprintf (file, "  %-11s", version_string);
    15171867            else
     
    15461896}
    15471897
    1548 /* Allocate an ELF string table--force the first byte to be zero.  */
    1549 
    1550 struct bfd_strtab_hash *
    1551 _bfd_elf_stringtab_init (void)
    1552 {
    1553   struct bfd_strtab_hash *ret;
    1554 
    1555   ret = _bfd_stringtab_init ();
    1556   if (ret != NULL)
    1557     {
    1558       bfd_size_type loc;
    1559 
    1560       loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
    1561       BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
    1562       if (loc == (bfd_size_type) -1)
    1563         {
    1564           _bfd_stringtab_free (ret);
    1565           ret = NULL;
    1566         }
    1567     }
    1568   return ret;
    1569 }
    1570 
    15711898
    15721899/* ELF .o/exec file reading */
     
    15921919    {
    15931920      /* PR17512: A corrupt ELF binary might contain a recursive group of
    1594          sections, each the string indicies pointing to the next in the
     1921         sections, with each the string indicies pointing to the next in the
    15951922         loop.  Detect this here, by refusing to load a section that we are
    15961923         already in the process of loading.  We only trigger this test if
     
    17152042        }
    17162043
    1717       BFD_ASSERT (elf_onesymtab (abfd) == 0);
     2044      /* PR 18854: A binary might contain more than one symbol table.
     2045         Unusual, but possible.  Warn, but continue.  */
     2046      if (elf_onesymtab (abfd) != 0)
     2047        {
     2048          (*_bfd_error_handler)
     2049            (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
     2050             abfd, shindex);
     2051          goto success;
     2052        }
    17182053      elf_onesymtab (abfd) = shindex;
    1719       elf_tdata (abfd)->symtab_hdr = *hdr;
    1720       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
     2054      elf_symtab_hdr (abfd) = *hdr;
     2055      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
    17212056      abfd->flags |= HAS_SYMS;
    17222057
     
    17362071         can't read symbols without that section loaded as well.  It
    17372072         is most likely specified by the next section header.  */
    1738       if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
    1739         {
    1740           unsigned int i, num_sec;
    1741 
    1742           num_sec = elf_numsections (abfd);
    1743           for (i = shindex + 1; i < num_sec; i++)
     2073      {
     2074        elf_section_list * entry;
     2075        unsigned int i, num_sec;
     2076
     2077        for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
     2078          if (entry->hdr.sh_link == shindex)
     2079            goto success;
     2080
     2081        num_sec = elf_numsections (abfd);
     2082        for (i = shindex + 1; i < num_sec; i++)
     2083          {
     2084            Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
     2085
     2086            if (hdr2->sh_type == SHT_SYMTAB_SHNDX
     2087                && hdr2->sh_link == shindex)
     2088              break;
     2089          }
     2090
     2091        if (i == num_sec)
     2092          for (i = 1; i < shindex; i++)
    17442093            {
    17452094              Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
     2095
    17462096              if (hdr2->sh_type == SHT_SYMTAB_SHNDX
    17472097                  && hdr2->sh_link == shindex)
    17482098                break;
    17492099            }
    1750           if (i == num_sec)
    1751             for (i = 1; i < shindex; i++)
    1752               {
    1753                 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
    1754                 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
    1755                     && hdr2->sh_link == shindex)
    1756                   break;
    1757               }
    1758           if (i != shindex)
    1759             ret = bfd_section_from_shdr (abfd, i);
    1760         }
    1761       goto success;
     2100
     2101        if (i != shindex)
     2102          ret = bfd_section_from_shdr (abfd, i);
     2103        /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
     2104        goto success;
     2105      }
    17622106
    17632107    case SHT_DYNSYM:            /* A dynamic symbol table.  */
     
    17802124        }
    17812125
    1782       BFD_ASSERT (elf_dynsymtab (abfd) == 0);
     2126      /* PR 18854: A binary might contain more than one dynamic symbol table.
     2127         Unusual, but possible.  Warn, but continue.  */
     2128      if (elf_dynsymtab (abfd) != 0)
     2129        {
     2130          (*_bfd_error_handler)
     2131            (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
     2132             abfd, shindex);
     2133          goto success;
     2134        }
    17832135      elf_dynsymtab (abfd) = shindex;
    17842136      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
     
    17922144
    17932145    case SHT_SYMTAB_SHNDX:      /* Symbol section indices when >64k sections.  */
    1794       if (elf_symtab_shndx (abfd) == shindex)
     2146      {
     2147        elf_section_list * entry;
     2148
     2149        for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
     2150          if (entry->ndx == shindex)
     2151            goto success;
     2152       
     2153        entry = bfd_alloc (abfd, sizeof * entry);
     2154        if (entry == NULL)
     2155          goto fail;
     2156        entry->ndx = shindex;
     2157        entry->hdr = * hdr;
     2158        entry->next = elf_symtab_shndx_list (abfd);
     2159        elf_symtab_shndx_list (abfd) = entry;
     2160        elf_elfsections (abfd)[shindex] = & entry->hdr;
    17952161        goto success;
    1796 
    1797       BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
    1798       elf_symtab_shndx (abfd) = shindex;
    1799       elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
    1800       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
    1801       goto success;
     2162      }
    18022163
    18032164    case SHT_STRTAB:            /* A string table.  */
     
    18692230        unsigned int num_sec = elf_numsections (abfd);
    18702231        struct bfd_elf_section_data *esdt;
    1871         bfd_size_type amt;
    18722232
    18732233        if (hdr->sh_entsize
     
    19592319          p_hdr = &esdt->rel.hdr;
    19602320
    1961         BFD_ASSERT (*p_hdr == NULL);
    1962         amt = sizeof (*hdr2);
    1963         hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
     2321        /* PR 17512: file: 0b4f81b7.  */
     2322        if (*p_hdr != NULL)
     2323          goto fail;
     2324        hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
    19642325        if (hdr2 == NULL)
    19652326          goto fail;
     
    20162377        {
    20172378          Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
    2018           unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
     2379          unsigned int n_elt = hdr->sh_size / sizeof (* idx);
    20192380          asection *s;
    20202381
     2382          if (n_elt == 0)
     2383            goto fail;
    20212384          if (idx->flags & GRP_COMDAT)
    20222385            hdr->bfd_section->flags
     
    20252388          /* We try to keep the same section order as it comes in.  */
    20262389          idx += n_elt;
     2390
    20272391          while (--n_elt != 0)
    20282392            {
     
    26232987}
    26242988
     2989static bfd_boolean
     2990_bfd_elf_set_reloc_sh_name (bfd *abfd,
     2991                            Elf_Internal_Shdr *rel_hdr,
     2992                            const char *sec_name,
     2993                            bfd_boolean use_rela_p)
     2994{
     2995  char *name = (char *) bfd_alloc (abfd,
     2996                                   sizeof ".rela" + strlen (sec_name));
     2997  if (name == NULL)
     2998    return FALSE;
     2999
     3000  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
     3001  rel_hdr->sh_name =
     3002    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
     3003                                        FALSE);
     3004  if (rel_hdr->sh_name == (unsigned int) -1)
     3005    return FALSE;
     3006
     3007  return TRUE;
     3008}
     3009
    26253010/* Allocate and initialize a section-header for a new reloc section,
    26263011   containing relocations against ASECT.  It is stored in RELDATA.  If
     
    26313016_bfd_elf_init_reloc_shdr (bfd *abfd,
    26323017                          struct bfd_elf_section_reloc_data *reldata,
    2633                           asection *asect,
    2634                           bfd_boolean use_rela_p)
     3018                          const char *sec_name,
     3019                          bfd_boolean use_rela_p,
     3020                          bfd_boolean delay_st_name_p)
    26353021{
    26363022  Elf_Internal_Shdr *rel_hdr;
    2637   char *name;
    26383023  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    2639   bfd_size_type amt;
    2640 
    2641   amt = sizeof (Elf_Internal_Shdr);
     3024
    26423025  BFD_ASSERT (reldata->hdr == NULL);
    2643   rel_hdr = bfd_zalloc (abfd, amt);
     3026  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
    26443027  reldata->hdr = rel_hdr;
    26453028
    2646   amt = sizeof ".rela" + strlen (asect->name);
    2647   name = (char *) bfd_alloc (abfd, amt);
    2648   if (name == NULL)
    2649     return FALSE;
    2650   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
    2651   rel_hdr->sh_name =
    2652     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
    2653                                         FALSE);
    2654   if (rel_hdr->sh_name == (unsigned int) -1)
     3029  if (delay_st_name_p)
     3030    rel_hdr->sh_name = (unsigned int) -1;
     3031  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
     3032                                        use_rela_p))
    26553033    return FALSE;
    26563034  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
     
    26943072  Elf_Internal_Shdr *this_hdr;
    26953073  unsigned int sh_type;
     3074  const char *name = asect->name;
     3075  bfd_boolean delay_st_name_p = FALSE;
    26963076
    26973077  if (arg->failed)
     
    27043084  this_hdr = &esd->this_hdr;
    27053085
    2706   this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
    2707                                                           asect->name, FALSE);
    2708   if (this_hdr->sh_name == (unsigned int) -1)
    2709     {
    2710       arg->failed = TRUE;
    2711       return;
     3086  if (arg->link_info)
     3087    {
     3088      /* ld: compress DWARF debug sections with names: .debug_*.  */
     3089      if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
     3090          && (asect->flags & SEC_DEBUGGING)
     3091          && name[1] == 'd'
     3092          && name[6] == '_')
     3093        {
     3094          /* Set SEC_ELF_COMPRESS to indicate this section should be
     3095             compressed.  */
     3096          asect->flags |= SEC_ELF_COMPRESS;
     3097
     3098          /* If this section will be compressed, delay adding setion
     3099             name to section name section after it is compressed in
     3100             _bfd_elf_assign_file_positions_for_non_load.  */
     3101          delay_st_name_p = TRUE;
     3102        }
     3103    }
     3104  else if ((asect->flags & SEC_ELF_RENAME))
     3105    {
     3106      /* objcopy: rename output DWARF debug section.  */
     3107      if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
     3108        {
     3109          /* When we decompress or compress with SHF_COMPRESSED,
     3110             convert section name from .zdebug_* to .debug_* if
     3111             needed.  */
     3112          if (name[1] == 'z')
     3113            {
     3114              char *new_name = convert_zdebug_to_debug (abfd, name);
     3115              if (new_name == NULL)
     3116                {
     3117                  arg->failed = TRUE;
     3118                  return;
     3119                }
     3120              name = new_name;
     3121            }
     3122        }
     3123      else if (asect->compress_status == COMPRESS_SECTION_DONE)
     3124        {
     3125          /* PR binutils/18087: Compression does not always make a
     3126             section smaller.  So only rename the section when
     3127             compression has actually taken place.  If input section
     3128             name is .zdebug_*, we should never compress it again.  */
     3129          char *new_name = convert_debug_to_zdebug (abfd, name);
     3130          if (new_name == NULL)
     3131            {
     3132              arg->failed = TRUE;
     3133              return;
     3134            }
     3135          BFD_ASSERT (name[1] != 'z');
     3136          name = new_name;
     3137        }
     3138    }
     3139
     3140  if (delay_st_name_p)
     3141    this_hdr->sh_name = (unsigned int) -1;
     3142  else
     3143    {
     3144      this_hdr->sh_name
     3145        = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
     3146                                              name, FALSE);
     3147      if (this_hdr->sh_name == (unsigned int) -1)
     3148        {
     3149          arg->failed = TRUE;
     3150          return;
     3151        }
    27123152    }
    27133153
     
    27233163  this_hdr->sh_size = asect->size;
    27243164  this_hdr->sh_link = 0;
     3165  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
     3166  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
     3167    {
     3168      (*_bfd_error_handler)
     3169        (_("%B: error: Alignment power %d of section `%A' is too big"),
     3170         abfd, asect, asect->alignment_power);
     3171      arg->failed = TRUE;
     3172      return;
     3173    }
    27253174  this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
    27263175  /* The sh_entsize and sh_info fields may have been set already by
     
    27583207
    27593208    case SHT_STRTAB:
     3209    case SHT_NOTE:
     3210    case SHT_NOBITS:
     3211    case SHT_PROGBITS:
     3212      break;
     3213
    27603214    case SHT_INIT_ARRAY:
    27613215    case SHT_FINI_ARRAY:
    27623216    case SHT_PREINIT_ARRAY:
    2763     case SHT_NOTE:
    2764     case SHT_NOBITS:
    2765     case SHT_PROGBITS:
     3217      this_hdr->sh_entsize = bed->s->arch_size / 8;
    27663218      break;
    27673219
     
    28353287      this_hdr->sh_flags |= SHF_MERGE;
    28363288      this_hdr->sh_entsize = asect->entsize;
    2837       if ((asect->flags & SEC_STRINGS) != 0)
    2838         this_hdr->sh_flags |= SHF_STRINGS;
    2839     }
     3289    }
     3290  if ((asect->flags & SEC_STRINGS) != 0)
     3291    this_hdr->sh_flags |= SHF_STRINGS;
    28403292  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
    28413293    this_hdr->sh_flags |= SHF_GROUP;
     
    28713323          /* Do the normal setup if we wouldn't create any sections here.  */
    28723324          && esd->rel.count + esd->rela.count > 0
    2873           && (arg->link_info->relocatable || arg->link_info->emitrelocations))
     3325          && (bfd_link_relocatable (arg->link_info)
     3326              || arg->link_info->emitrelocations))
    28743327        {
    28753328          if (esd->rel.count && esd->rel.hdr == NULL
    2876               && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
     3329              && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE,
     3330                                            delay_st_name_p))
    28773331            {
    28783332              arg->failed = TRUE;
     
    28803334            }
    28813335          if (esd->rela.count && esd->rela.hdr == NULL
    2882               && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
     3336              && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE,
     3337                                            delay_st_name_p))
    28833338            {
    28843339              arg->failed = TRUE;
     
    28893344                                          (asect->use_rela_p
    28903345                                           ? &esd->rela : &esd->rel),
    2891                                           asect,
    2892                                           asect->use_rela_p))
     3346                                          name,
     3347                                          asect->use_rela_p,
     3348                                          delay_st_name_p))
    28933349          arg->failed = TRUE;
    28943350    }
     
    30243480}
    30253481
     3482/* Return the section which RELOC_SEC applies to.  */
     3483
     3484asection *
     3485_bfd_elf_get_reloc_section (asection *reloc_sec)
     3486{
     3487  const char *name;
     3488  unsigned int type;
     3489  bfd *abfd;
     3490
     3491  if (reloc_sec == NULL)
     3492    return NULL;
     3493
     3494  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
     3495  if (type != SHT_REL && type != SHT_RELA)
     3496    return NULL;
     3497
     3498  /* We look up the section the relocs apply to by name.  */
     3499  name = reloc_sec->name;
     3500  if (type == SHT_REL)
     3501    name += 4;
     3502  else
     3503    name += 5;
     3504
     3505  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
     3506     section apply to .got.plt section.  */
     3507  abfd = reloc_sec->owner;
     3508  if (get_elf_backend_data (abfd)->want_got_plt
     3509      && strcmp (name, ".plt") == 0)
     3510    {
     3511      /* .got.plt is a linker created input section.  It may be mapped
     3512         to some other output section.  Try two likely sections.  */
     3513      name = ".got.plt";
     3514      reloc_sec = bfd_get_section_by_name (abfd, name);
     3515      if (reloc_sec != NULL)
     3516        return reloc_sec;
     3517      name = ".got";
     3518    }
     3519
     3520  reloc_sec = bfd_get_section_by_name (abfd, name);
     3521  return reloc_sec;
     3522}
     3523
    30263524/* Assign all ELF section numbers.  The dummy first section is handled here
    30273525   too.  The link/info pointers for the standard section types are filled
     
    30333531  struct elf_obj_tdata *t = elf_tdata (abfd);
    30343532  asection *sec;
    3035   unsigned int section_number, secn;
     3533  unsigned int section_number;
    30363534  Elf_Internal_Shdr **i_shdrp;
    30373535  struct bfd_elf_section_data *d;
     
    30433541
    30443542  /* SHT_GROUP sections are in relocatable files only.  */
    3045   if (link_info == NULL || link_info->relocatable)
    3046     {
     3543  if (link_info == NULL || bfd_link_relocatable (link_info))
     3544    {
     3545      size_t reloc_count = 0;
     3546
    30473547      /* Put SHT_GROUP sections first.  */
    30483548      for (sec = abfd->sections; sec != NULL; sec = sec->next)
     
    30613561                d->this_idx = section_number++;
    30623562            }
    3063         }
     3563
     3564          /* Count relocations.  */
     3565          reloc_count += sec->reloc_count;
     3566        }
     3567
     3568      /* Clear HAS_RELOC if there are no relocations.  */
     3569      if (reloc_count == 0)
     3570        abfd->flags &= ~HAS_RELOC;
    30643571    }
    30653572
     
    30703577      if (d->this_hdr.sh_type != SHT_GROUP)
    30713578        d->this_idx = section_number++;
    3072       _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
     3579      if (d->this_hdr.sh_name != (unsigned int) -1)
     3580        _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
    30733581      if (d->rel.hdr)
    30743582        {
    30753583          d->rel.idx = section_number++;
    3076           _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
     3584          if (d->rel.hdr->sh_name != (unsigned int) -1)
     3585            _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
    30773586        }
    30783587      else
     
    30823591        {
    30833592          d->rela.idx = section_number++;
    3084           _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
     3593          if (d->rela.hdr->sh_name != (unsigned int) -1)
     3594            _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
    30853595        }
    30863596      else
     
    31023612      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
    31033613        {
    3104           elf_symtab_shndx (abfd) = section_number++;
    3105           t->symtab_shndx_hdr.sh_name
     3614          elf_section_list * entry;
     3615
     3616          BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
     3617
     3618          entry = bfd_zalloc (abfd, sizeof * entry);
     3619          entry->ndx = section_number++;
     3620          elf_symtab_shndx_list (abfd) = entry;
     3621          entry->hdr.sh_name
    31063622            = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
    31073623                                                  ".symtab_shndx", FALSE);
    3108           if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
     3624          if (entry->hdr.sh_name == (unsigned int) -1)
    31093625            return FALSE;
    31103626        }
     
    31193635      return FALSE;
    31203636    }
    3121 
    3122   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
    3123   t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
    31243637
    31253638  elf_numsections (abfd) = section_number;
     
    31493662      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
    31503663        {
    3151           i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
    3152           t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
     3664          elf_section_list * entry = elf_symtab_shndx_list (abfd);
     3665          BFD_ASSERT (entry != NULL);
     3666          i_shdrp[entry->ndx] = & entry->hdr;
     3667          entry->hdr.sh_link = elf_onesymtab (abfd);
    31533668        }
    31543669      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
     
    31593674    {
    31603675      asection *s;
    3161       const char *name;
    31623676
    31633677      d = elf_section_data (sec);
     
    32633777            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
    32643778
    3265           /* We look up the section the relocs apply to by name.  */
    3266           name = sec->name;
    3267           if (d->this_hdr.sh_type == SHT_REL)
    3268             name += 4;
    3269           else
    3270             name += 5;
    3271           s = bfd_get_section_by_name (abfd, name);
     3779          s = get_elf_backend_data (abfd)->get_reloc_section (sec);
    32723780          if (s != NULL)
    32733781            {
     
    33453853    }
    33463854
    3347   for (secn = 1; secn < section_number; ++secn)
    3348     if (i_shdrp[secn] == NULL)
    3349       i_shdrp[secn] = i_shdrp[0];
    3350     else
    3351       i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
    3352                                                        i_shdrp[secn]->sh_name);
     3855  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
     3856     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
     3857     debug section name from .debug_* to .zdebug_* if needed.  */
     3858
    33533859  return TRUE;
    33543860}
     
    34013907  unsigned int num_locals2 = 0;
    34023908  unsigned int num_globals2 = 0;
    3403   int max_index = 0;
     3909  unsigned int max_index = 0;
    34043910  unsigned int idx;
    34053911  asection *asect;
     
    35504056  struct fake_section_arg fsargs;
    35514057  bfd_boolean failed;
    3552   struct bfd_strtab_hash *strtab = NULL;
     4058  struct elf_strtab_hash *strtab = NULL;
    35534059  Elf_Internal_Shdr *shstrtab_hdr;
    35544060  bfd_boolean need_symtab;
     
    36014107  /* sh_name was set in prep_headers.  */
    36024108  shstrtab_hdr->sh_type = SHT_STRTAB;
    3603   shstrtab_hdr->sh_flags = 0;
     4109  shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
    36044110  shstrtab_hdr->sh_addr = 0;
    3605   shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
     4111  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
    36064112  shstrtab_hdr->sh_entsize = 0;
    36074113  shstrtab_hdr->sh_link = 0;
    36084114  shstrtab_hdr->sh_info = 0;
    3609   /* sh_offset is set in assign_file_positions_except_relocs.  */
     4115  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
    36104116  shstrtab_hdr->sh_addralign = 1;
    36114117
     
    36204126      off = elf_next_file_pos (abfd);
    36214127
    3622       hdr = &elf_tdata (abfd)->symtab_hdr;
     4128      hdr = & elf_symtab_hdr (abfd);
    36234129      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
    36244130
    3625       hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
    3626       if (hdr->sh_size != 0)
    3627         off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
     4131      if (elf_symtab_shndx_list (abfd) != NULL)
     4132        {
     4133          hdr = & elf_symtab_shndx_list (abfd)->hdr;
     4134          if (hdr->sh_size != 0)
     4135            off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
     4136          /* FIXME: What about other symtab_shndx sections in the list ?  */
     4137        }
    36284138
    36294139      hdr = &elf_tdata (abfd)->strtab_hdr;
     
    36354145         out.  */
    36364146      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
    3637           || ! _bfd_stringtab_emit (abfd, strtab))
     4147          || ! _bfd_elf_strtab_emit (abfd, strtab))
    36384148        return FALSE;
    3639       _bfd_stringtab_free (strtab);
     4149      _bfd_elf_strtab_free (strtab);
    36404150    }
    36414151
     
    39764486      phdr_index = 0;
    39774487      maxpagesize = bed->maxpagesize;
     4488      /* PR 17512: file: c8455299.
     4489         Avoid divide-by-zero errors later on.
     4490         FIXME: Should we abort if the maxpagesize is zero ?  */
     4491      if (maxpagesize == 0)
     4492        maxpagesize = 1;
    39784493      writable = FALSE;
    39794494      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
     
    40464561            }
    40474562          else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
    4048                    && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
     4563                   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
     4564                   && ((abfd->flags & D_PAGED) == 0
     4565                       || (((last_hdr->lma + last_size - 1) & -maxpagesize)
     4566                           != (hdr->lma & -maxpagesize))))
    40494567            {
    4050               /* We don't want to put a loadable section after a
    4051                  nonloadable section in the same segment.
    4052                  Consider .tbss sections as loadable for this purpose.  */
     4568              /* We don't want to put a loaded section after a
     4569                 nonloaded (ie. bss style) section in the same segment
     4570                 as that will force the non-loaded section to be loaded.
     4571                 Consider .tbss sections as loaded for this purpose.
     4572                 However, like the writable/non-writable case below,
     4573                 if they are on the same page then they must be put
     4574                 in the same segment.  */
    40534575              new_segment = TRUE;
    40544576            }
     
    43214843              m->next = NULL;
    43224844              m->p_type = PT_GNU_RELRO;
    4323               m->p_flags = PF_R;
    4324               m->p_flags_valid = 1;
    4325 
    43264845              *pm = m;
    43274846              pm = &m->next;
     
    49635482  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    49645483  Elf_Internal_Shdr **i_shdrpp;
    4965   Elf_Internal_Shdr **hdrpp;
     5484  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
    49665485  Elf_Internal_Phdr *phdrs;
    49675486  Elf_Internal_Phdr *p;
     
    49715490  bfd_vma phdrs_vaddr, phdrs_paddr;
    49725491  file_ptr off;
    4973   unsigned int num_sec;
    4974   unsigned int i;
    49755492  unsigned int count;
    49765493
    49775494  i_shdrpp = elf_elfsections (abfd);
    4978   num_sec = elf_numsections (abfd);
     5495  end_hdrpp = i_shdrpp + elf_numsections (abfd);
    49795496  off = elf_next_file_pos (abfd);
    4980   for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
     5497  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
    49815498    {
    49825499      Elf_Internal_Shdr *hdr;
     
    50095526      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
    50105527                && hdr->bfd_section == NULL)
     5528               || (hdr->bfd_section != NULL
     5529                   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
     5530                   /* Compress DWARF debug sections.  */
    50115531               || hdr == i_shdrpp[elf_onesymtab (abfd)]
    5012                || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
    5013                || hdr == i_shdrpp[elf_strtab_sec (abfd)])
     5532               || (elf_symtab_shndx_list (abfd) != NULL
     5533                   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
     5534               || hdr == i_shdrpp[elf_strtab_sec (abfd)]
     5535               || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
    50145536        hdr->sh_offset = -1;
    50155537      else
     
    51525674                p->p_align = 1;
    51535675              if (!m->p_flags_valid)
    5154                 p->p_flags = (lp->p_flags & ~PF_W);
     5676                p->p_flags = PF_R;
    51555677            }
    51565678          else
     
    51675689      else if (m->count != 0)
    51685690        {
     5691          unsigned int i;
    51695692          if (p->p_type != PT_LOAD
    51705693              && (p->p_type != PT_NOTE
    51715694                  || bfd_get_format (abfd) != bfd_core))
    51725695            {
    5173               BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
     5696              if (m->includes_filehdr || m->includes_phdrs)
     5697                {
     5698                  /* PR 17512: file: 2195325e.  */
     5699                  (*_bfd_error_handler)
     5700                    (_("%B: warning: non-load segment includes file header and/or program header"),
     5701                     abfd);
     5702                  return FALSE;
     5703                }
    51745704
    51755705              p->p_filesz = 0;
     
    52075737}
    52085738
     5739static elf_section_list *
     5740find_section_in_list (unsigned int i, elf_section_list * list)
     5741{
     5742  for (;list != NULL; list = list->next)
     5743    if (list->ndx == i)
     5744      break;
     5745  return list;
     5746}
     5747
    52095748/* Work out the file positions of all the sections.  This is called by
    52105749   _bfd_elf_compute_section_file_positions.  All the section sizes and
     
    52515790          if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
    52525791               && hdr->bfd_section == NULL)
     5792              || (hdr->bfd_section != NULL
     5793                  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
     5794                  /* Compress DWARF debug sections.  */
    52535795              || i == elf_onesymtab (abfd)
    5254               || i == elf_symtab_shndx (abfd)
    5255               || i == elf_strtab_sec (abfd))
     5796              || (elf_symtab_shndx_list (abfd) != NULL
     5797                  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
     5798              || i == elf_strtab_sec (abfd)
     5799              || i == elf_shstrtab_sec (abfd))
    52565800            {
    52575801              hdr->sh_offset = -1;
     
    52835827
    52845828      /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.  */
    5285       if (link_info != NULL
    5286           && link_info->executable
    5287           && link_info->shared)
     5829      if (link_info != NULL && bfd_link_pie (link_info))
    52885830        {
    52895831          unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
     
    54045946   of the loadable file image, and the file position of section headers.  */
    54055947
    5406 static void
    5407 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
     5948static bfd_boolean
     5949_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
    54085950{
    54095951  file_ptr off;
    5410   unsigned int i, num_sec;
    5411   Elf_Internal_Shdr **shdrpp;
     5952  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
     5953  Elf_Internal_Shdr *shdrp;
    54125954  Elf_Internal_Ehdr *i_ehdrp;
    54135955  const struct elf_backend_data *bed;
     
    54155957  off = elf_next_file_pos (abfd);
    54165958
    5417   num_sec = elf_numsections (abfd);
    5418   for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
    5419     {
    5420       Elf_Internal_Shdr *shdrp;
    5421 
     5959  shdrpp = elf_elfsections (abfd);
     5960  end_shdrpp = shdrpp + elf_numsections (abfd);
     5961  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
     5962    {
    54225963      shdrp = *shdrpp;
    5423       if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
    5424           && shdrp->sh_offset == -1)
    5425         off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
    5426     }
    5427 
    5428 /* Place the section headers.  */
     5964      if (shdrp->sh_offset == -1)
     5965        {
     5966          asection *sec = shdrp->bfd_section;
     5967          bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
     5968                                || shdrp->sh_type == SHT_RELA);
     5969          if (is_rel
     5970              || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
     5971            {
     5972              if (!is_rel)
     5973                {
     5974                  const char *name = sec->name;
     5975                  struct bfd_elf_section_data *d;
     5976
     5977                  /* Compress DWARF debug sections.  */
     5978                  if (!bfd_compress_section (abfd, sec,
     5979                                             shdrp->contents))
     5980                    return FALSE;
     5981
     5982                  if (sec->compress_status == COMPRESS_SECTION_DONE
     5983                      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
     5984                    {
     5985                      /* If section is compressed with zlib-gnu, convert
     5986                         section name from .debug_* to .zdebug_*.  */
     5987                      char *new_name
     5988                        = convert_debug_to_zdebug (abfd, name);
     5989                      if (new_name == NULL)
     5990                        return FALSE;
     5991                      name = new_name;
     5992                    }
     5993                  /* Add setion name to section name section.  */
     5994                  if (shdrp->sh_name != (unsigned int) -1)
     5995                    abort ();
     5996                  shdrp->sh_name
     5997                    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
     5998                                                          name, FALSE);
     5999                  d = elf_section_data (sec);
     6000
     6001                  /* Add reloc setion name to section name section.  */
     6002                  if (d->rel.hdr
     6003                      && !_bfd_elf_set_reloc_sh_name (abfd,
     6004                                                      d->rel.hdr,
     6005                                                      name, FALSE))
     6006                    return FALSE;
     6007                  if (d->rela.hdr
     6008                      && !_bfd_elf_set_reloc_sh_name (abfd,
     6009                                                      d->rela.hdr,
     6010                                                      name, TRUE))
     6011                    return FALSE;
     6012
     6013                  /* Update section size and contents.  */
     6014                  shdrp->sh_size = sec->size;
     6015                  shdrp->contents = sec->contents;
     6016                  shdrp->bfd_section->contents = NULL;
     6017                }
     6018              off = _bfd_elf_assign_file_position_for_section (shdrp,
     6019                                                               off,
     6020                                                               TRUE);
     6021            }
     6022        }
     6023    }
     6024
     6025  /* Place section name section after DWARF debug sections have been
     6026     compressed.  */
     6027  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
     6028  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
     6029  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
     6030  off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
     6031
     6032  /* Place the section headers.  */
    54296033  i_ehdrp = elf_elfheader (abfd);
    54306034  bed = get_elf_backend_data (abfd);
     
    54336037  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
    54346038  elf_next_file_pos (abfd) = off;
     6039
     6040  return TRUE;
    54356041}
    54366042
     
    54556061    return FALSE;
    54566062
    5457   _bfd_elf_assign_file_positions_for_relocs (abfd);
     6063  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
     6064    return FALSE;
    54586065
    54596066  /* After writing the headers, we need to write the sections too...  */
     
    54616068  for (count = 1; count < num_sec; count++)
    54626069    {
     6070      i_shdrp[count]->sh_name
     6071        = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
     6072                                  i_shdrp[count]->sh_name);
    54636073      if (bed->elf_backend_section_processing)
    54646074        (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
     
    59126522             a warning is produced.  */
    59136523          if (segment->p_type == PT_LOAD)
    5914             (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
    5915                                      " detected, is this intentional ?\n"),
     6524            (*_bfd_error_handler) (_("\
     6525%B: warning: Empty loadable segment detected, is this intentional ?"),
    59166526                                   ibfd);
    59176527
     
    59676577      first_suggested_lma = TRUE;
    59686578
    5969       for (section = ibfd->sections;
     6579      for (section = first_section, j = 0;
    59706580           section != NULL;
    59716581           section = section->next)
    5972         if (section == first_section)
    5973           break;
    5974 
    5975       for (j = 0; section != NULL; section = section->next)
    59766582        {
    59776583          if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
     
    65267132        if (segment->p_type == PT_LOAD
    65277133            && maxpagesize < segment->p_align)
    6528           maxpagesize = segment->p_align;
     7134          {
     7135            /* PR 17512: file: f17299af.  */
     7136            if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
     7137              (*_bfd_error_handler) (_("\
     7138%B: warning: segment alignment of 0x%llx is too large"),
     7139                                     ibfd, (long long) segment->p_align);
     7140            else
     7141              maxpagesize = segment->p_align;
     7142          }
    65297143
    65307144      if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
     
    65467160{
    65477161  Elf_Internal_Shdr *ihdr, *ohdr;
    6548   bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
     7162  bfd_boolean final_link = (link_info != NULL
     7163                            && !bfd_link_relocatable (link_info));
    65497164
    65507165  if (ibfd->xvec->flavour != bfd_target_elf_flavour
     
    65837198          elf_section_data (osec)->group = elf_section_data (isec)->group;
    65847199        }
     7200
     7201      /* If not decompress, preserve SHF_COMPRESSED.  */
     7202      if ((ibfd->flags & BFD_DECOMPRESS) == 0)
     7203        elf_section_flags (osec) |= (elf_section_flags (isec)
     7204                                     & SHF_COMPRESSED);
    65857205    }
    65867206
     
    67617381      else if (shndx == elf_shstrtab_sec (ibfd))
    67627382        shndx = MAP_SHSTRTAB;
    6763       else if (shndx == elf_symtab_shndx (ibfd))
     7383      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
    67647384        shndx = MAP_SYM_SHNDX;
    67657385      osym->internal_elf_sym.st_shndx = shndx;
     
    67737393static bfd_boolean
    67747394swap_out_syms (bfd *abfd,
    6775                struct bfd_strtab_hash **sttp,
     7395               struct elf_strtab_hash **sttp,
    67767396               int relocatable_p)
    67777397{
     
    67797399  int symcount;
    67807400  asymbol **syms;
    6781   struct bfd_strtab_hash *stt;
     7401  struct elf_strtab_hash *stt;
    67827402  Elf_Internal_Shdr *symtab_hdr;
    67837403  Elf_Internal_Shdr *symtab_shndx_hdr;
    67847404  Elf_Internal_Shdr *symstrtab_hdr;
     7405  struct elf_sym_strtab *symstrtab;
    67857406  bfd_byte *outbound_syms;
    67867407  bfd_byte *outbound_shndx;
     7408  unsigned long outbound_syms_index;
     7409  unsigned long outbound_shndx_index;
    67877410  int idx;
    67887411  unsigned int num_locals;
     
    67947417
    67957418  /* Dump out the symtabs.  */
    6796   stt = _bfd_elf_stringtab_init ();
     7419  stt = _bfd_elf_strtab_init ();
    67977420  if (stt == NULL)
    67987421    return FALSE;
     
    68107433  symstrtab_hdr->sh_type = SHT_STRTAB;
    68117434
     7435  /* Allocate buffer to swap out the .strtab section.  */
     7436  symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
     7437                                                    * sizeof (*symstrtab));
     7438  if (symstrtab == NULL)
     7439    {
     7440      _bfd_elf_strtab_free (stt);
     7441      return FALSE;
     7442    }
     7443
    68127444  outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
    68137445                                           bed->s->sizeof_sym);
    68147446  if (outbound_syms == NULL)
    68157447    {
    6816       _bfd_stringtab_free (stt);
     7448error_return:
     7449      _bfd_elf_strtab_free (stt);
     7450      free (symstrtab);
    68177451      return FALSE;
    68187452    }
    68197453  symtab_hdr->contents = outbound_syms;
     7454  outbound_syms_index = 0;
    68207455
    68217456  outbound_shndx = NULL;
    6822   symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
    6823   if (symtab_shndx_hdr->sh_name != 0)
    6824     {
    6825       amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
    6826       outbound_shndx =  (bfd_byte *)
    6827           bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
    6828       if (outbound_shndx == NULL)
    6829         {
    6830           _bfd_stringtab_free (stt);
    6831           return FALSE;
    6832         }
    6833 
    6834       symtab_shndx_hdr->contents = outbound_shndx;
    6835       symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
    6836       symtab_shndx_hdr->sh_size = amt;
    6837       symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
    6838       symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
     7457  outbound_shndx_index = 0;
     7458
     7459  if (elf_symtab_shndx_list (abfd))
     7460    {
     7461      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
     7462      if (symtab_shndx_hdr->sh_name != 0)
     7463        {
     7464          amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
     7465          outbound_shndx =  (bfd_byte *)
     7466            bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
     7467          if (outbound_shndx == NULL)
     7468            goto error_return;
     7469
     7470          symtab_shndx_hdr->contents = outbound_shndx;
     7471          symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
     7472          symtab_shndx_hdr->sh_size = amt;
     7473          symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
     7474          symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
     7475        }
     7476      /* FIXME: What about any other headers in the list ?  */
    68397477    }
    68407478
     
    68507488    sym.st_shndx = SHN_UNDEF;
    68517489    sym.st_target_internal = 0;
    6852     bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
    6853     outbound_syms += bed->s->sizeof_sym;
     7490    symstrtab[0].sym = sym;
     7491    symstrtab[0].dest_index = outbound_syms_index;
     7492    symstrtab[0].destshndx_index = outbound_shndx_index;
     7493    outbound_syms_index++;
    68547494    if (outbound_shndx != NULL)
    6855       outbound_shndx += sizeof (Elf_External_Sym_Shndx);
     7495      outbound_shndx_index++;
    68567496  }
    68577497
     
    68617501
    68627502  syms = bfd_get_outsymbols (abfd);
    6863   for (idx = 0; idx < symcount; idx++)
     7503  for (idx = 0; idx < symcount;)
    68647504    {
    68657505      Elf_Internal_Sym sym;
     
    68737513        {
    68747514          /* Local section symbols have no name.  */
    6875           sym.st_name = 0;
     7515          sym.st_name = (unsigned long) -1;
    68767516        }
    68777517      else
    68787518        {
    6879           sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
    6880                                                             syms[idx]->name,
    6881                                                             TRUE, FALSE);
     7519          /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
     7520             to get the final offset for st_name.  */
     7521          sym.st_name
     7522            = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
     7523                                                   FALSE);
    68827524          if (sym.st_name == (unsigned long) -1)
    6883             {
    6884               _bfd_stringtab_free (stt);
    6885               return FALSE;
    6886             }
     7525            goto error_return;
    68877526        }
    68887527
     
    69447583                  break;
    69457584                case MAP_SYM_SHNDX:
    6946                   shndx = elf_symtab_shndx (abfd);
     7585                  if (elf_symtab_shndx_list (abfd))
     7586                    shndx = elf_symtab_shndx_list (abfd)->ndx;
    69477587                  break;
    69487588                default:
     
    69747614                                          sec->name);
    69757615                      bfd_set_error (bfd_error_invalid_operation);
    6976                       _bfd_stringtab_free (stt);
    6977                       return FALSE;
     7616                      goto error_return;
    69787617                    }
    69797618
     
    70197658      else if (bfd_is_com_section (syms[idx]->section))
    70207659        {
    7021 #ifdef USE_STT_COMMON
    7022           if (type == STT_OBJECT)
    7023             sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
    7024           else
    7025 #endif
    7026             sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
     7660          if (type != STT_TLS)
     7661            {
     7662              if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
     7663                type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
     7664                        ? STT_COMMON : STT_OBJECT);
     7665              else
     7666                type = ((flags & BSF_ELF_COMMON) != 0
     7667                        ? STT_COMMON : STT_OBJECT);
     7668            }
     7669          sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
    70277670        }
    70287671      else if (bfd_is_und_section (syms[idx]->section))
     
    70617704        }
    70627705
    7063       bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
    7064       outbound_syms += bed->s->sizeof_sym;
     7706      idx++;
     7707      symstrtab[idx].sym = sym;
     7708      symstrtab[idx].dest_index = outbound_syms_index;
     7709      symstrtab[idx].destshndx_index = outbound_shndx_index;
     7710
     7711      outbound_syms_index++;
    70657712      if (outbound_shndx != NULL)
    7066         outbound_shndx += sizeof (Elf_External_Sym_Shndx);
    7067     }
     7713        outbound_shndx_index++;
     7714    }
     7715
     7716  /* Finalize the .strtab section.  */
     7717  _bfd_elf_strtab_finalize (stt);
     7718
     7719  /* Swap out the .strtab section.  */
     7720  for (idx = 0; idx <= symcount; idx++)
     7721    {
     7722      struct elf_sym_strtab *elfsym = &symstrtab[idx];
     7723      if (elfsym->sym.st_name == (unsigned long) -1)
     7724        elfsym->sym.st_name = 0;
     7725      else
     7726        elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
     7727                                                      elfsym->sym.st_name);
     7728      bed->s->swap_symbol_out (abfd, &elfsym->sym,
     7729                               (outbound_syms
     7730                                + (elfsym->dest_index
     7731                                   * bed->s->sizeof_sym)),
     7732                               (outbound_shndx
     7733                                + (elfsym->destshndx_index
     7734                                   * sizeof (Elf_External_Sym_Shndx))));
     7735    }
     7736  free (symstrtab);
    70687737
    70697738  *sttp = stt;
    7070   symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
     7739  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
    70717740  symstrtab_hdr->sh_type = SHT_STRTAB;
    7072 
    7073   symstrtab_hdr->sh_flags = 0;
     7741  symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
    70747742  symstrtab_hdr->sh_addr = 0;
    70757743  symstrtab_hdr->sh_entsize = 0;
     
    72737941      hdr = &elf_tdata (abfd)->dynverref_hdr;
    72747942
    7275       if (hdr->sh_info)
    7276         elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
    7277           bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
    7278       else
    7279         elf_tdata (abfd)->verref = NULL;
    7280        
    7281       if (elf_tdata (abfd)->verref == NULL)
    7282         goto error_return;
    7283 
    7284       elf_tdata (abfd)->cverrefs = hdr->sh_info;
    7285 
    7286       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
    7287       if (contents == NULL)
    7288         {
     7943      if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed))
     7944        {
     7945error_return_bad_verref:
     7946          (*_bfd_error_handler)
     7947            (_("%B: .gnu.version_r invalid entry"), abfd);
     7948          bfd_set_error (bfd_error_bad_value);
    72897949error_return_verref:
    72907950          elf_tdata (abfd)->verref = NULL;
     
    72927952          goto error_return;
    72937953        }
     7954
     7955      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
     7956      if (contents == NULL)
     7957        goto error_return_verref;
     7958
    72947959      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
    72957960          || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
    72967961        goto error_return_verref;
    72977962
    7298       if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
     7963      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
     7964        bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
     7965
     7966      if (elf_tdata (abfd)->verref == NULL)
    72997967        goto error_return_verref;
    73007968
     
    73187986                                             iverneed->vn_file);
    73197987          if (iverneed->vn_filename == NULL)
    7320             goto error_return_verref;
     7988            goto error_return_bad_verref;
    73217989
    73227990          if (iverneed->vn_cnt == 0)
     
    73338001          if (iverneed->vn_aux
    73348002              > (size_t) (contents_end - (bfd_byte *) everneed))
    7335             goto error_return_verref;
     8003            goto error_return_bad_verref;
    73368004
    73378005          evernaux = ((Elf_External_Vernaux *)
     
    73468014                                                 ivernaux->vna_name);
    73478015              if (ivernaux->vna_nodename == NULL)
    7348                 goto error_return_verref;
    7349 
     8016                goto error_return_bad_verref;
     8017
     8018              if (ivernaux->vna_other > freeidx)
     8019                freeidx = ivernaux->vna_other;
     8020
     8021              ivernaux->vna_nextptr = NULL;
     8022              if (ivernaux->vna_next == 0)
     8023                {
     8024                  iverneed->vn_cnt = j + 1;
     8025                  break;
     8026                }
    73508027              if (j + 1 < iverneed->vn_cnt)
    73518028                ivernaux->vna_nextptr = ivernaux + 1;
    7352               else
    7353                 ivernaux->vna_nextptr = NULL;
    73548029
    73558030              if (ivernaux->vna_next
    73568031                  > (size_t) (contents_end - (bfd_byte *) evernaux))
    7357                 goto error_return_verref;
     8032                goto error_return_bad_verref;
    73588033
    73598034              evernaux = ((Elf_External_Vernaux *)
    73608035                          ((bfd_byte *) evernaux + ivernaux->vna_next));
    7361 
    7362               if (ivernaux->vna_other > freeidx)
    7363                 freeidx = ivernaux->vna_other;
    73648036            }
    73658037
     8038          iverneed->vn_nextref = NULL;
     8039          if (iverneed->vn_next == 0)
     8040            break;
    73668041          if (i + 1 < hdr->sh_info)
    73678042            iverneed->vn_nextref = iverneed + 1;
    7368           else
    7369             iverneed->vn_nextref = NULL;
    73708043
    73718044          if (iverneed->vn_next
    73728045              > (size_t) (contents_end - (bfd_byte *) everneed))
    7373             goto error_return_verref;
     8046            goto error_return_bad_verref;
    73748047
    73758048          everneed = ((Elf_External_Verneed *)
    73768049                      ((bfd_byte *) everneed + iverneed->vn_next));
    73778050        }
     8051      elf_tdata (abfd)->cverrefs = i;
    73788052
    73798053      free (contents);
     
    73948068      hdr = &elf_tdata (abfd)->dynverdef_hdr;
    73958069
     8070      if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
     8071        {
     8072        error_return_bad_verdef:
     8073          (*_bfd_error_handler)
     8074            (_("%B: .gnu.version_d invalid entry"), abfd);
     8075          bfd_set_error (bfd_error_bad_value);
     8076        error_return_verdef:
     8077          elf_tdata (abfd)->verdef = NULL;
     8078          elf_tdata (abfd)->cverdefs = 0;
     8079          goto error_return;
     8080        }
     8081
    73968082      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
    73978083      if (contents == NULL)
    7398         goto error_return;
     8084        goto error_return_verdef;
    73998085      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
    74008086          || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
    7401         goto error_return;
    7402 
    7403       if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
    7404         goto error_return;
     8087        goto error_return_verdef;
    74058088
    74068089      BFD_ASSERT (sizeof (Elf_External_Verdef)
     
    74208103          _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
    74218104
     8105          if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
     8106            goto error_return_bad_verdef;
    74228107          if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
    74238108            maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
    74248109
     8110          if (iverdefmem.vd_next == 0)
     8111            break;
     8112
    74258113          if (iverdefmem.vd_next
    74268114              > (size_t) (contents_end_def - (bfd_byte *) everdef))
    7427             goto error_return;
     8115            goto error_return_bad_verdef;
    74288116
    74298117          everdef = ((Elf_External_Verdef *)
     
    74388126            freeidx = ++maxidx;
    74398127        }
    7440       if (maxidx)
    7441         elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
    7442           bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
    7443       else
    7444         elf_tdata (abfd)->verdef = NULL;
    7445 
     8128
     8129      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
     8130        bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
    74468131      if (elf_tdata (abfd)->verdef == NULL)
    7447         goto error_return;
     8132        goto error_return_verdef;
    74488133
    74498134      elf_tdata (abfd)->cverdefs = maxidx;
     
    74608145
    74618146          if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
    7462             {
    7463 error_return_verdef:
    7464               elf_tdata (abfd)->verdef = NULL;
    7465               elf_tdata (abfd)->cverdefs = 0;
    7466               goto error_return;
    7467             }
     8147            goto error_return_bad_verdef;
    74688148
    74698149          iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
    7470           memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
     8150          memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
    74718151
    74728152          iverdef->vd_bfd = abfd;
     
    74858165          if (iverdef->vd_aux
    74868166              > (size_t) (contents_end_aux - (bfd_byte *) everdef))
    7487             goto error_return_verdef;
     8167            goto error_return_bad_verdef;
    74888168
    74898169          everdaux = ((Elf_External_Verdaux *)
     
    74988178                                                 iverdaux->vda_name);
    74998179              if (iverdaux->vda_nodename == NULL)
    7500                 goto error_return_verdef;
    7501 
     8180                goto error_return_bad_verdef;
     8181
     8182              iverdaux->vda_nextptr = NULL;
     8183              if (iverdaux->vda_next == 0)
     8184                {
     8185                  iverdef->vd_cnt = j + 1;
     8186                  break;
     8187                }
    75028188              if (j + 1 < iverdef->vd_cnt)
    75038189                iverdaux->vda_nextptr = iverdaux + 1;
    7504               else
    7505                 iverdaux->vda_nextptr = NULL;
    75068190
    75078191              if (iverdaux->vda_next
    75088192                  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
    7509                 goto error_return_verdef;
     8193                goto error_return_bad_verdef;
    75108194
    75118195              everdaux = ((Elf_External_Verdaux *)
     
    75138197            }
    75148198
     8199          iverdef->vd_nodename = NULL;
    75158200          if (iverdef->vd_cnt)
    75168201            iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
    75178202
     8203          iverdef->vd_nextdef = NULL;
     8204          if (iverdef->vd_next == 0)
     8205            break;
    75188206          if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
    75198207            iverdef->vd_nextdef = iverdef + 1;
    7520           else
    7521             iverdef->vd_nextdef = NULL;
    75228208
    75238209          everdef = ((Elf_External_Verdef *)
     
    75628248        goto error_return_verdef;
    75638249      iverdef->vd_nextdef = NULL;
    7564       iverdef->vd_auxptr = (struct elf_internal_verdaux *)
    7565           bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
     8250      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
     8251                            bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
    75668252      if (iverdef->vd_auxptr == NULL)
    75678253        goto error_return_verdef;
     
    75698255      iverdaux = iverdef->vd_auxptr;
    75708256      iverdaux->vda_nodename = iverdef->vd_nodename;
    7571       iverdaux->vda_nextptr = NULL;
    75728257    }
    75738258
     
    76278312    return TRUE;
    76288313
     8314  /* Treat assembler generated fake symbols, dollar local labels and
     8315     forward-backward labels (aka local labels) as locals.
     8316     These labels have the form:
     8317
     8318       L0^A.*                                  (fake symbols)
     8319
     8320       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
     8321
     8322     Versions which start with .L will have already been matched above,
     8323     so we only need to match the rest.  */
     8324  if (name[0] == 'L' && ISDIGIT (name[1]))
     8325    {
     8326      bfd_boolean ret = FALSE;
     8327      const char * p;
     8328      char c;
     8329
     8330      for (p = name + 2; (c = *p); p++)
     8331        {
     8332          if (c == 1 || c == 2)
     8333            {
     8334              if (c == 1 && p == name + 2)
     8335                /* A fake symbol.  */
     8336                return TRUE;
     8337
     8338              /* FIXME: We are being paranoid here and treating symbols like
     8339                 L0^Bfoo as if there were non-local, on the grounds that the
     8340                 assembler will never generate them.  But can any symbol
     8341                 containing an ASCII value in the range 1-31 ever be anything
     8342                 other than some kind of local ?  */
     8343              ret = TRUE;
     8344            }
     8345
     8346          if (! ISDIGIT (c))
     8347            {
     8348              ret = FALSE;
     8349              break;
     8350            }
     8351        }
     8352      return ret;
     8353    }
     8354
    76298355  return FALSE;
    76308356}
     
    76518377
    76528378  return bfd_default_set_arch_mach (abfd, arch, machine);
    7653 }
    7654 
    7655 /* Find the function to a particular section and offset,
    7656    for error reporting.  */
    7657 
    7658 static bfd_boolean
    7659 elf_find_function (bfd *abfd,
    7660                    asymbol **symbols,
    7661                    asection *section,
    7662                    bfd_vma offset,
    7663                    const char **filename_ptr,
    7664                    const char **functionname_ptr)
    7665 {
    7666   struct elf_find_function_cache
    7667   {
    7668     asection *last_section;
    7669     asymbol *func;
    7670     const char *filename;
    7671     bfd_size_type func_size;
    7672   } *cache;
    7673 
    7674   if (symbols == NULL)
    7675     return FALSE;
    7676 
    7677   cache = elf_tdata (abfd)->elf_find_function_cache;
    7678   if (cache == NULL)
    7679     {
    7680       cache = bfd_zalloc (abfd, sizeof (*cache));
    7681       elf_tdata (abfd)->elf_find_function_cache = cache;
    7682       if (cache == NULL)
    7683         return FALSE;
    7684     }
    7685   if (cache->last_section != section
    7686       || cache->func == NULL
    7687       || offset < cache->func->value
    7688       || offset >= cache->func->value + cache->func_size)
    7689     {
    7690       asymbol *file;
    7691       bfd_vma low_func;
    7692       asymbol **p;
    7693       /* ??? Given multiple file symbols, it is impossible to reliably
    7694          choose the right file name for global symbols.  File symbols are
    7695          local symbols, and thus all file symbols must sort before any
    7696          global symbols.  The ELF spec may be interpreted to say that a
    7697          file symbol must sort before other local symbols, but currently
    7698          ld -r doesn't do this.  So, for ld -r output, it is possible to
    7699          make a better choice of file name for local symbols by ignoring
    7700          file symbols appearing after a given local symbol.  */
    7701       enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
    7702       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    7703 
    7704       file = NULL;
    7705       low_func = 0;
    7706       state = nothing_seen;
    7707       cache->filename = NULL;
    7708       cache->func = NULL;
    7709       cache->func_size = 0;
    7710       cache->last_section = section;
    7711 
    7712       for (p = symbols; *p != NULL; p++)
    7713         {
    7714           asymbol *sym = *p;
    7715           bfd_vma code_off;
    7716           bfd_size_type size;
    7717 
    7718           if ((sym->flags & BSF_FILE) != 0)
    7719             {
    7720               file = sym;
    7721               if (state == symbol_seen)
    7722                 state = file_after_symbol_seen;
    7723               continue;
    7724             }
    7725 
    7726           size = bed->maybe_function_sym (sym, section, &code_off);
    7727           if (size != 0
    7728               && code_off <= offset
    7729               && (code_off > low_func
    7730                   || (code_off == low_func
    7731                       && size > cache->func_size)))
    7732             {
    7733               cache->func = sym;
    7734               cache->func_size = size;
    7735               cache->filename = NULL;
    7736               low_func = code_off;
    7737               if (file != NULL
    7738                   && ((sym->flags & BSF_LOCAL) != 0
    7739                       || state != file_after_symbol_seen))
    7740                 cache->filename = bfd_asymbol_name (file);
    7741             }
    7742           if (state == nothing_seen)
    7743             state = symbol_seen;
    7744         }
    7745     }
    7746 
    7747   if (cache->func == NULL)
    7748     return FALSE;
    7749 
    7750   if (filename_ptr)
    7751     *filename_ptr = cache->filename;
    7752   if (functionname_ptr)
    7753     *functionname_ptr = bfd_asymbol_name (cache->func);
    7754 
    7755   return TRUE;
    77568379}
    77578380
     
    77758398                                     line_ptr, discriminator_ptr,
    77768399                                     dwarf_debug_sections, 0,
    7777                                      &elf_tdata (abfd)->dwarf2_find_line_info))
     8400                                     &elf_tdata (abfd)->dwarf2_find_line_info)
     8401      || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
     8402                                        filename_ptr, functionname_ptr,
     8403                                        line_ptr))
    77788404    {
    77798405      if (!*functionname_ptr)
    7780         elf_find_function (abfd, symbols, section, offset,
    7781                            *filename_ptr ? NULL : filename_ptr,
    7782                            functionname_ptr);
    7783 
    7784       return TRUE;
    7785     }
    7786 
    7787   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
    7788                                      filename_ptr, functionname_ptr, line_ptr))
    7789     {
    7790       if (!*functionname_ptr)
    7791         elf_find_function (abfd, symbols, section, offset,
    7792                            *filename_ptr ? NULL : filename_ptr,
    7793                            functionname_ptr);
    7794 
     8406        _bfd_elf_find_function (abfd, symbols, section, offset,
     8407                                *filename_ptr ? NULL : filename_ptr,
     8408                                functionname_ptr);
    77958409      return TRUE;
    77968410    }
     
    78078421    return FALSE;
    78088422
    7809   if (! elf_find_function (abfd, symbols, section, offset,
    7810                            filename_ptr, functionname_ptr))
     8423  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
     8424                                filename_ptr, functionname_ptr))
    78118425    return FALSE;
    78128426
     
    78528466  int ret = bed->s->sizeof_ehdr;
    78538467
    7854   if (!info->relocatable)
     8468  if (!bfd_link_relocatable (info))
    78558469    {
    78568470      bfd_size_type phdr_size = elf_program_header_size (abfd);
     
    78898503    return FALSE;
    78908504
     8505  if (!count)
     8506    return TRUE;
     8507
    78918508  hdr = &elf_section_data (section)->this_hdr;
     8509  if (hdr->sh_offset == (file_ptr) -1)
     8510    {
     8511      /* We must compress this section.  Write output to the buffer.  */
     8512      unsigned char *contents = hdr->contents;
     8513      if ((offset + count) > hdr->sh_size
     8514          || (section->flags & SEC_ELF_COMPRESS) == 0
     8515          || contents == NULL)
     8516        abort ();
     8517      memcpy (contents + offset, location, count);
     8518      return TRUE;
     8519    }
    78928520  pos = hdr->sh_offset + offset;
    78938521  if (bfd_seek (abfd, pos, SEEK_SET) != 0
     
    83108938
    83118939static bfd_boolean
     8940elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
     8941{
     8942  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
     8943}
     8944
     8945static bfd_boolean
     8946elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
     8947{
     8948  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
     8949}
     8950
     8951static bfd_boolean
    83128952elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
    83138953{
     
    87729412      else
    87739413        return TRUE;
     9414
     9415    case NT_S390_VXRS_LOW:
     9416      if (note->namesz == 6
     9417          && strcmp (note->namedata, "LINUX") == 0)
     9418        return elfcore_grok_s390_vxrs_low (abfd, note);
     9419      else
     9420        return TRUE;
     9421
     9422    case NT_S390_VXRS_HIGH:
     9423      if (note->namesz == 6
     9424          && strcmp (note->namedata, "LINUX") == 0)
     9425        return elfcore_grok_s390_vxrs_high (abfd, note);
     9426      else
     9427        return TRUE;
    87749428
    87759429    case NT_ARM_VFP:
     
    88339487      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
    88349488                                              note);
     9489
    88359490    }
    88369491}
     
    88399494elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
    88409495{
    8841   struct elf_obj_tdata *t;
     9496  struct bfd_build_id* build_id;
    88429497
    88439498  if (note->descsz == 0)
    88449499    return FALSE;
    88459500
    8846   t = elf_tdata (abfd);
    8847   t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
    8848   if (t->build_id == NULL)
     9501  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
     9502  if (build_id == NULL)
    88499503    return FALSE;
    88509504
    8851   t->build_id->size = note->descsz;
    8852   memcpy (t->build_id->data, note->descdata, note->descsz);
     9505  build_id->size = note->descsz;
     9506  memcpy (build_id->data, note->descdata, note->descsz);
     9507  abfd->build_id = build_id;
    88539508
    88549509  return TRUE;
     
    88919546    case NT_STAPSDT:
    88929547      return elfobj_grok_stapsdt_note_1 (abfd, note);
     9548
     9549    default:
     9550      return TRUE;
     9551    }
     9552}
     9553
     9554static bfd_boolean
     9555elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
     9556{
     9557  size_t offset;
     9558
     9559  /* Check for version 1 in pr_version. */
     9560  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
     9561    return FALSE;
     9562  offset = 4;
     9563
     9564  /* Skip over pr_psinfosz. */
     9565  switch (abfd->arch_info->bits_per_word)
     9566    {
     9567    case 32:
     9568      offset += 4;
     9569      break;
     9570
     9571    case 64:
     9572      offset += 4;      /* Padding before pr_psinfosz. */
     9573      offset += 8;
     9574      break;
     9575
     9576    default:
     9577      return FALSE;
     9578    }
     9579
     9580  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
     9581  elf_tdata (abfd)->core->program
     9582    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
     9583  offset += 17;
     9584
     9585  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
     9586  elf_tdata (abfd)->core->command
     9587    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
     9588
     9589  return TRUE;
     9590}
     9591
     9592static bfd_boolean
     9593elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
     9594{
     9595  size_t offset;
     9596  size_t size;
     9597
     9598  /* Check for version 1 in pr_version. */
     9599  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
     9600    return FALSE;
     9601  offset = 4;
     9602
     9603  /* Skip over pr_statussz.  */
     9604  switch (abfd->arch_info->bits_per_word)
     9605    {
     9606    case 32:
     9607      offset += 4;
     9608      break;
     9609
     9610    case 64:
     9611      offset += 4;      /* Padding before pr_statussz. */
     9612      offset += 8;
     9613      break;
     9614
     9615    default:
     9616      return FALSE;
     9617    }
     9618
     9619  /* Extract size of pr_reg from pr_gregsetsz.  */
     9620  if (abfd->arch_info->bits_per_word == 32)
     9621    size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
     9622  else
     9623    size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
     9624
     9625  /* Skip over pr_gregsetsz and pr_fpregsetsz. */
     9626  offset += (abfd->arch_info->bits_per_word / 8) * 2;
     9627
     9628  /* Skip over pr_osreldate. */
     9629  offset += 4;
     9630
     9631  /* Read signal from pr_cursig. */
     9632  if (elf_tdata (abfd)->core->signal == 0)
     9633    elf_tdata (abfd)->core->signal
     9634      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
     9635  offset += 4;
     9636
     9637  /* Read TID from pr_pid. */
     9638  elf_tdata (abfd)->core->lwpid
     9639      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
     9640  offset += 4;
     9641
     9642  /* Padding before pr_reg. */
     9643  if (abfd->arch_info->bits_per_word == 64)
     9644    offset += 4;
     9645
     9646  /* Make a ".reg/999" section and a ".reg" section.  */
     9647  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
     9648                                          size, note->descpos + offset);
     9649}
     9650
     9651static bfd_boolean
     9652elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
     9653{
     9654  switch (note->type)
     9655    {
     9656    case NT_PRSTATUS:
     9657      return elfcore_grok_freebsd_prstatus (abfd, note);
     9658
     9659    case NT_FPREGSET:
     9660      return elfcore_grok_prfpreg (abfd, note);
     9661
     9662    case NT_PRPSINFO:
     9663      return elfcore_grok_freebsd_psinfo (abfd, note);
     9664
     9665    case NT_FREEBSD_THRMISC:
     9666      if (note->namesz == 8)
     9667        return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
     9668      else
     9669        return TRUE;
     9670
     9671    case NT_FREEBSD_PROCSTAT_AUXV:
     9672      {
     9673        asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
     9674                                                             SEC_HAS_CONTENTS);
     9675
     9676        if (sect == NULL)
     9677          return FALSE;
     9678        sect->size = note->descsz - 4;
     9679        sect->filepos = note->descpos + 4;
     9680        sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
     9681
     9682        return TRUE;
     9683      }
     9684
     9685    case NT_X86_XSTATE:
     9686      if (note->namesz == 8)
     9687        return elfcore_grok_xstatereg (abfd, note);
     9688      else
     9689        return TRUE;
    88939690
    88949691    default:
     
    933010127  struct elf_external_linux_prpsinfo32 data;
    933110128
    9332   memset (&data, 0, sizeof (data));
    9333   LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
    9334 
     10129  swap_linux_prpsinfo32_out (abfd, prpsinfo, &data);
    933510130  return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
    933610131                             &data, sizeof (data));
     
    934410139  struct elf_external_linux_prpsinfo64 data;
    934510140
    9346   memset (&data, 0, sizeof (data));
    9347   LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
    9348 
     10141  swap_linux_prpsinfo64_out (abfd, prpsinfo, &data);
    934910142  return elfcore_write_note (abfd, buf, bufsiz,
    935010143                             "CORE", NT_PRPSINFO, &data, sizeof (data));
     
    949810291                         const void *xfpregs, int size)
    949910292{
    9500   char *note_name = "LINUX";
     10293  char *note_name;
     10294  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
     10295    note_name = "FreeBSD";
     10296  else
     10297    note_name = "LINUX";
    950110298  return elfcore_write_note (abfd, buf, bufsiz,
    950210299                             note_name, NT_X86_XSTATE, xfpregs, size);
     
    963610433  return elfcore_write_note (abfd, buf, bufsiz,
    963710434                             note_name, NT_S390_TDB, s390_tdb, size);
     10435}
     10436
     10437char *
     10438elfcore_write_s390_vxrs_low (bfd *abfd,
     10439                             char *buf,
     10440                             int *bufsiz,
     10441                             const void *s390_vxrs_low,
     10442                             int size)
     10443{
     10444  char *note_name = "LINUX";
     10445  return elfcore_write_note (abfd, buf, bufsiz,
     10446                             note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
     10447}
     10448
     10449char *
     10450elfcore_write_s390_vxrs_high (bfd *abfd,
     10451                             char *buf,
     10452                             int *bufsiz,
     10453                             const void *s390_vxrs_high,
     10454                             int size)
     10455{
     10456  char *note_name = "LINUX";
     10457  return elfcore_write_note (abfd, buf, bufsiz,
     10458                             note_name, NT_S390_VXRS_HIGH,
     10459                             s390_vxrs_high, size);
    963810460}
    963910461
     
    972210544  if (strcmp (section, ".reg-s390-tdb") == 0)
    972310545    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
     10546  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
     10547    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
     10548  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
     10549    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
    972410550  if (strcmp (section, ".reg-arm-vfp") == 0)
    972510551    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
     
    976910595
    977010596        case bfd_core:
    9771           if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
     10597          {
     10598#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
     10599            struct
    977210600            {
    9773               if (! elfcore_grok_netbsd_note (abfd, &in))
    9774                 return FALSE;
     10601              const char * string;
     10602              size_t len;
     10603              bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
    977510604            }
    9776           else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
     10605            grokers[] =
    977710606            {
    9778               if (! elfcore_grok_openbsd_note (abfd, &in))
    9779                 return FALSE;
    9780             }
    9781           else if (CONST_STRNEQ (in.namedata, "QNX"))
    9782             {
    9783               if (! elfcore_grok_nto_note (abfd, &in))
    9784                 return FALSE;
    9785             }
    9786           else if (CONST_STRNEQ (in.namedata, "SPU/"))
    9787             {
    9788               if (! elfcore_grok_spu_note (abfd, &in))
    9789                 return FALSE;
    9790             }
    9791           else
    9792             {
    9793               if (! elfcore_grok_note (abfd, &in))
    9794                 return FALSE;
    9795             }
    9796           break;
     10607              GROKER_ELEMENT ("", elfcore_grok_note),
     10608              GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
     10609              GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
     10610              GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
     10611              GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
     10612              GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
     10613            };
     10614#undef GROKER_ELEMENT
     10615            int i;
     10616
     10617            for (i = ARRAY_SIZE (grokers); i--;)
     10618              {
     10619                if (in.namesz >= grokers[i].len
     10620                    && strncmp (in.namedata, grokers[i].string,
     10621                                grokers[i].len) == 0)
     10622                  {
     10623                    if (! grokers[i].func (abfd, & in))
     10624                      return FALSE;
     10625                    break;
     10626                  }
     10627              }
     10628            break;
     10629          }
    979710630
    979810631        case bfd_object:
     
    982810661    return FALSE;
    982910662
    9830   buf = (char *) bfd_malloc (size);
     10663  buf = (char *) bfd_malloc (size + 1);
    983110664  if (buf == NULL)
    983210665    return FALSE;
     10666
     10667  /* PR 17512: file: ec08f814
     10668     0-termintate the buffer so that string searches will not overflow.  */
     10669  buf[size] = 0;
    983310670
    983410671  if (bfd_bread (buf, size, abfd) != size
     
    995210789}
    995310790
     10791/* Adjust an address within a section.  Given OFFSET within SEC, return
     10792   the new offset within the section, based upon changes made to the
     10793   section.  Returns -1 if the offset is now invalid.
     10794   The offset (in abnd out) is in target sized bytes, however big a
     10795   byte may be.  */
     10796
    995410797bfd_vma
    995510798_bfd_elf_section_offset (bfd *abfd,
     
    996510808    case SEC_INFO_TYPE_EH_FRAME:
    996610809      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
     10810
    996710811    default:
    996810812      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
    996910813        {
     10814          /* Reverse the offset.  */
    997010815          const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    997110816          bfd_size_type address_size = bed->s->arch_size / 8;
    9972           offset = sec->size - offset - address_size;
     10817
     10818          /* address_size and sec->size are in octets.  Convert
     10819             to bytes before subtracting the original offset.  */
     10820          offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
    997310821        }
    997410822      return offset;
Note: See TracChangeset for help on using the changeset viewer.